11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/proc/base.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * proc base directory handling functions 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * 1999, Al Viro. Rewritten. Now it covers the whole per-process part. 91da177e4SLinus Torvalds * Instead of using magical inumbers to determine the kind of object 101da177e4SLinus Torvalds * we allocate and fill in-core inodes upon lookup. They don't even 111da177e4SLinus Torvalds * go into icache. We cache the reference to task_struct upon lookup too. 121da177e4SLinus Torvalds * Eventually it should become a filesystem in its own. We don't use the 131da177e4SLinus Torvalds * rest of procfs anymore. 14e070ad49SMauricio Lin * 15e070ad49SMauricio Lin * 16e070ad49SMauricio Lin * Changelog: 17e070ad49SMauricio Lin * 17-Jan-2005 18e070ad49SMauricio Lin * Allan Bezerra 19e070ad49SMauricio Lin * Bruna Moreira <bruna.moreira@indt.org.br> 20e070ad49SMauricio Lin * Edjard Mota <edjard.mota@indt.org.br> 21e070ad49SMauricio Lin * Ilias Biris <ilias.biris@indt.org.br> 22e070ad49SMauricio Lin * Mauricio Lin <mauricio.lin@indt.org.br> 23e070ad49SMauricio Lin * 24e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 25e070ad49SMauricio Lin * 26e070ad49SMauricio Lin * A new process specific entry (smaps) included in /proc. It shows the 27e070ad49SMauricio Lin * size of rss for each memory area. The maps entry lacks information 28e070ad49SMauricio Lin * about physical memory size (rss) for each mapped file, i.e., 29e070ad49SMauricio Lin * rss information for executables and library files. 30e070ad49SMauricio Lin * This additional information is useful for any tools that need to know 31e070ad49SMauricio Lin * about physical memory consumption for a process specific library. 32e070ad49SMauricio Lin * 33e070ad49SMauricio Lin * Changelog: 34e070ad49SMauricio Lin * 21-Feb-2005 35e070ad49SMauricio Lin * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT 36e070ad49SMauricio Lin * Pud inclusion in the page table walking. 37e070ad49SMauricio Lin * 38e070ad49SMauricio Lin * ChangeLog: 39e070ad49SMauricio Lin * 10-Mar-2005 40e070ad49SMauricio Lin * 10LE Instituto Nokia de Tecnologia - INdT: 41e070ad49SMauricio Lin * A better way to walks through the page table as suggested by Hugh Dickins. 42e070ad49SMauricio Lin * 43e070ad49SMauricio Lin * Simo Piiroinen <simo.piiroinen@nokia.com>: 44e070ad49SMauricio Lin * Smaps information related to shared, private, clean and dirty pages. 45e070ad49SMauricio Lin * 46e070ad49SMauricio Lin * Paul Mundt <paul.mundt@nokia.com>: 47e070ad49SMauricio Lin * Overall revision about smaps. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #include <asm/uaccess.h> 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds #include <linux/errno.h> 531da177e4SLinus Torvalds #include <linux/time.h> 541da177e4SLinus Torvalds #include <linux/proc_fs.h> 551da177e4SLinus Torvalds #include <linux/stat.h> 561da177e4SLinus Torvalds #include <linux/init.h> 5716f7e0feSRandy Dunlap #include <linux/capability.h> 581da177e4SLinus Torvalds #include <linux/file.h> 599f3acc31SAl Viro #include <linux/fdtable.h> 601da177e4SLinus Torvalds #include <linux/string.h> 611da177e4SLinus Torvalds #include <linux/seq_file.h> 621da177e4SLinus Torvalds #include <linux/namei.h> 636b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 641da177e4SLinus Torvalds #include <linux/mm.h> 65b835996fSDipankar Sarma #include <linux/rcupdate.h> 661da177e4SLinus Torvalds #include <linux/kallsyms.h> 67d85f50d5SNeil Horman #include <linux/resource.h> 685096add8SKees Cook #include <linux/module.h> 691da177e4SLinus Torvalds #include <linux/mount.h> 701da177e4SLinus Torvalds #include <linux/security.h> 711da177e4SLinus Torvalds #include <linux/ptrace.h> 72*0d094efeSRoland McGrath #include <linux/tracehook.h> 73a424316cSPaul Menage #include <linux/cgroup.h> 741da177e4SLinus Torvalds #include <linux/cpuset.h> 751da177e4SLinus Torvalds #include <linux/audit.h> 765addc5ddSAl Viro #include <linux/poll.h> 771651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 788ac773b4SAlexey Dobriyan #include <linux/oom.h> 793cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 8060347f67SPavel Emelyanov #include <linux/pid_namespace.h> 811da177e4SLinus Torvalds #include "internal.h" 821da177e4SLinus Torvalds 830f2fe20fSEric W. Biederman /* NOTE: 840f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 850f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 860f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 870f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 880f2fe20fSEric W. Biederman * 890f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 900f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 910f2fe20fSEric W. Biederman */ 920f2fe20fSEric W. Biederman 931da177e4SLinus Torvalds struct pid_entry { 941da177e4SLinus Torvalds char *name; 95c5141e6dSEric Dumazet int len; 961da177e4SLinus Torvalds mode_t mode; 97c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9800977a59SArjan van de Ven const struct file_operations *fop; 9920cdc894SEric W. Biederman union proc_op op; 1001da177e4SLinus Torvalds }; 1011da177e4SLinus Torvalds 10261a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10320cdc894SEric W. Biederman .name = (NAME), \ 104c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10520cdc894SEric W. Biederman .mode = MODE, \ 10620cdc894SEric W. Biederman .iop = IOP, \ 10720cdc894SEric W. Biederman .fop = FOP, \ 10820cdc894SEric W. Biederman .op = OP, \ 10920cdc894SEric W. Biederman } 11020cdc894SEric W. Biederman 11161a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11261a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11320cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11420cdc894SEric W. Biederman {} ) 11561a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11661a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11720cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11820cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11961a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 12061a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12120cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12261a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12361a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12420cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12520cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 126be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE) \ 127be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 128be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 129be614086SEric W. Biederman { .proc_show = &proc_##OTYPE } ) 1301da177e4SLinus Torvalds 131aed54175SVegard Nossum /* 132aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 133aed54175SVegard Nossum * and .. links. 134aed54175SVegard Nossum */ 135aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 136aed54175SVegard Nossum unsigned int n) 137aed54175SVegard Nossum { 138aed54175SVegard Nossum unsigned int i; 139aed54175SVegard Nossum unsigned int count; 140aed54175SVegard Nossum 141aed54175SVegard Nossum count = 0; 142aed54175SVegard Nossum for (i = 0; i < n; ++i) { 143aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 144aed54175SVegard Nossum ++count; 145aed54175SVegard Nossum } 146aed54175SVegard Nossum 147aed54175SVegard Nossum return count; 148aed54175SVegard Nossum } 149aed54175SVegard Nossum 1505096add8SKees Cook int maps_protect; 1515096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1525096add8SKees Cook 1530494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds struct fs_struct *fs; 1560494f6ecSMiklos Szeredi task_lock(task); 1570494f6ecSMiklos Szeredi fs = task->fs; 1581da177e4SLinus Torvalds if(fs) 1591da177e4SLinus Torvalds atomic_inc(&fs->count); 1600494f6ecSMiklos Szeredi task_unlock(task); 1610494f6ecSMiklos Szeredi return fs; 1620494f6ecSMiklos Szeredi } 1630494f6ecSMiklos Szeredi 16499f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16599f89551SEric W. Biederman { 16699f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 16799f89551SEric W. Biederman unsigned long flags; 16899f89551SEric W. Biederman int count = 0; 16999f89551SEric W. Biederman 17099f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 17199f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 17299f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 17399f89551SEric W. Biederman } 17499f89551SEric W. Biederman return count; 17599f89551SEric W. Biederman } 17699f89551SEric W. Biederman 1773dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1780494f6ecSMiklos Szeredi { 17999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 18099f89551SEric W. Biederman struct fs_struct *fs = NULL; 1810494f6ecSMiklos Szeredi int result = -ENOENT; 18299f89551SEric W. Biederman 18399f89551SEric W. Biederman if (task) { 18499f89551SEric W. Biederman fs = get_fs_struct(task); 18599f89551SEric W. Biederman put_task_struct(task); 18699f89551SEric W. Biederman } 1871da177e4SLinus Torvalds if (fs) { 1881da177e4SLinus Torvalds read_lock(&fs->lock); 1893dcd25f3SJan Blunck *path = fs->pwd; 1903dcd25f3SJan Blunck path_get(&fs->pwd); 1911da177e4SLinus Torvalds read_unlock(&fs->lock); 1921da177e4SLinus Torvalds result = 0; 1931da177e4SLinus Torvalds put_fs_struct(fs); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds return result; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1983dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1991da177e4SLinus Torvalds { 20099f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 20199f89551SEric W. Biederman struct fs_struct *fs = NULL; 2021da177e4SLinus Torvalds int result = -ENOENT; 20399f89551SEric W. Biederman 20499f89551SEric W. Biederman if (task) { 20599f89551SEric W. Biederman fs = get_fs_struct(task); 20699f89551SEric W. Biederman put_task_struct(task); 20799f89551SEric W. Biederman } 2081da177e4SLinus Torvalds if (fs) { 2091da177e4SLinus Torvalds read_lock(&fs->lock); 2103dcd25f3SJan Blunck *path = fs->root; 2113dcd25f3SJan Blunck path_get(&fs->root); 2121da177e4SLinus Torvalds read_unlock(&fs->lock); 2131da177e4SLinus Torvalds result = 0; 2141da177e4SLinus Torvalds put_fs_struct(fs); 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds return result; 2171da177e4SLinus Torvalds } 2181da177e4SLinus Torvalds 219638fa202SRoland McGrath /* 220638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 221638fa202SRoland McGrath */ 222638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 223638fa202SRoland McGrath { 224638fa202SRoland McGrath /* 225638fa202SRoland McGrath * A task can always look at itself, in case it chooses 226638fa202SRoland McGrath * to use system calls instead of load instructions. 227638fa202SRoland McGrath */ 228638fa202SRoland McGrath if (task == current) 229638fa202SRoland McGrath return 0; 230638fa202SRoland McGrath 231638fa202SRoland McGrath /* 232638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 233638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 234638fa202SRoland McGrath */ 235*0d094efeSRoland McGrath if (task_is_stopped_or_traced(task)) { 236*0d094efeSRoland McGrath int match; 237*0d094efeSRoland McGrath rcu_read_lock(); 238*0d094efeSRoland McGrath match = (tracehook_tracer_task(task) == current); 239*0d094efeSRoland McGrath rcu_read_unlock(); 240*0d094efeSRoland McGrath if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH)) 241638fa202SRoland McGrath return 0; 242*0d094efeSRoland McGrath } 243638fa202SRoland McGrath 244638fa202SRoland McGrath /* 245638fa202SRoland McGrath * Noone else is allowed. 246638fa202SRoland McGrath */ 247638fa202SRoland McGrath return -EPERM; 248638fa202SRoland McGrath } 2491da177e4SLinus Torvalds 250831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 251831830b5SAl Viro { 252831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 253831830b5SAl Viro if (!mm) 254831830b5SAl Viro return NULL; 255831830b5SAl Viro down_read(&mm->mmap_sem); 256831830b5SAl Viro task_lock(task); 257831830b5SAl Viro if (task->mm != mm) 258831830b5SAl Viro goto out; 259006ebb40SStephen Smalley if (task->mm != current->mm && 260006ebb40SStephen Smalley __ptrace_may_access(task, PTRACE_MODE_READ) < 0) 261831830b5SAl Viro goto out; 262831830b5SAl Viro task_unlock(task); 263831830b5SAl Viro return mm; 264831830b5SAl Viro out: 265831830b5SAl Viro task_unlock(task); 266831830b5SAl Viro up_read(&mm->mmap_sem); 267831830b5SAl Viro mmput(mm); 268831830b5SAl Viro return NULL; 269831830b5SAl Viro } 270831830b5SAl Viro 2711da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds int res = 0; 2741da177e4SLinus Torvalds unsigned int len; 2751da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2761da177e4SLinus Torvalds if (!mm) 2771da177e4SLinus Torvalds goto out; 2781da177e4SLinus Torvalds if (!mm->arg_end) 2791da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds if (len > PAGE_SIZE) 2841da177e4SLinus Torvalds len = PAGE_SIZE; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2891da177e4SLinus Torvalds // assume application is using setproctitle(3). 2901da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2911da177e4SLinus Torvalds len = strnlen(buffer, res); 2921da177e4SLinus Torvalds if (len < res) { 2931da177e4SLinus Torvalds res = len; 2941da177e4SLinus Torvalds } else { 2951da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2961da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2971da177e4SLinus Torvalds len = PAGE_SIZE - res; 2981da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2991da177e4SLinus Torvalds res = strnlen(buffer, res); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds out_mm: 3031da177e4SLinus Torvalds mmput(mm); 3041da177e4SLinus Torvalds out: 3051da177e4SLinus Torvalds return res; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds int res = 0; 3111da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 3121da177e4SLinus Torvalds if (mm) { 3131da177e4SLinus Torvalds unsigned int nwords = 0; 3141da177e4SLinus Torvalds do 3151da177e4SLinus Torvalds nwords += 2; 3161da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3171da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3181da177e4SLinus Torvalds if (res > PAGE_SIZE) 3191da177e4SLinus Torvalds res = PAGE_SIZE; 3201da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3211da177e4SLinus Torvalds mmput(mm); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds return res; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3281da177e4SLinus Torvalds /* 3291da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3301da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3311da177e4SLinus Torvalds */ 3321da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3331da177e4SLinus Torvalds { 334ffb45122SAlexey Dobriyan unsigned long wchan; 3359281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds wchan = get_wchan(task); 3381da177e4SLinus Torvalds 3399d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 3401da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3419d65cb4aSAlexey Dobriyan else 3429d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3471da177e4SLinus Torvalds /* 3481da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3491da177e4SLinus Torvalds */ 3501da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3511da177e4SLinus Torvalds { 352172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3531da177e4SLinus Torvalds task->sched_info.cpu_time, 3541da177e4SLinus Torvalds task->sched_info.run_delay, 3552d72376bSIngo Molnar task->sched_info.pcount); 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds #endif 3581da177e4SLinus Torvalds 3599745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3609745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3619745512cSArjan van de Ven { 3629745512cSArjan van de Ven int i; 36313d77c37SHiroshi Shimamoto struct inode *inode = m->private; 36413d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3659745512cSArjan van de Ven 36613d77c37SHiroshi Shimamoto if (!task) 36713d77c37SHiroshi Shimamoto return -ESRCH; 36813d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3699745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3709745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3719745512cSArjan van de Ven int q; 3729745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3739745512cSArjan van de Ven task->latency_record[i].count, 3749745512cSArjan van de Ven task->latency_record[i].time, 3759745512cSArjan van de Ven task->latency_record[i].max); 3769745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3779745512cSArjan van de Ven char sym[KSYM_NAME_LEN]; 3789745512cSArjan van de Ven char *c; 3799745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3809745512cSArjan van de Ven break; 3819745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3829745512cSArjan van de Ven break; 3839745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3849745512cSArjan van de Ven c = strchr(sym, '+'); 3859745512cSArjan van de Ven if (c) 3869745512cSArjan van de Ven *c = 0; 3879745512cSArjan van de Ven seq_printf(m, "%s ", sym); 3889745512cSArjan van de Ven } 3899745512cSArjan van de Ven seq_printf(m, "\n"); 3909745512cSArjan van de Ven } 3919745512cSArjan van de Ven 3929745512cSArjan van de Ven } 39313d77c37SHiroshi Shimamoto put_task_struct(task); 3949745512cSArjan van de Ven return 0; 3959745512cSArjan van de Ven } 3969745512cSArjan van de Ven 3979745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3989745512cSArjan van de Ven { 39913d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 400d6643d12SHiroshi Shimamoto } 401d6643d12SHiroshi Shimamoto 4029745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 4039745512cSArjan van de Ven size_t count, loff_t *offs) 4049745512cSArjan van de Ven { 40513d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4069745512cSArjan van de Ven 40713d77c37SHiroshi Shimamoto if (!task) 40813d77c37SHiroshi Shimamoto return -ESRCH; 4099745512cSArjan van de Ven clear_all_latency_tracing(task); 41013d77c37SHiroshi Shimamoto put_task_struct(task); 4119745512cSArjan van de Ven 4129745512cSArjan van de Ven return count; 4139745512cSArjan van de Ven } 4149745512cSArjan van de Ven 4159745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4169745512cSArjan van de Ven .open = lstats_open, 4179745512cSArjan van de Ven .read = seq_read, 4189745512cSArjan van de Ven .write = lstats_write, 4199745512cSArjan van de Ven .llseek = seq_lseek, 42013d77c37SHiroshi Shimamoto .release = single_release, 4219745512cSArjan van de Ven }; 4229745512cSArjan van de Ven 4239745512cSArjan van de Ven #endif 4249745512cSArjan van de Ven 4251da177e4SLinus Torvalds /* The badness from the OOM killer */ 4261da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4271da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4281da177e4SLinus Torvalds { 4291da177e4SLinus Torvalds unsigned long points; 4301da177e4SLinus Torvalds struct timespec uptime; 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 43319c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4341da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 43519c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4361da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 439d85f50d5SNeil Horman struct limit_names { 440d85f50d5SNeil Horman char *name; 441d85f50d5SNeil Horman char *unit; 442d85f50d5SNeil Horman }; 443d85f50d5SNeil Horman 444d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 445d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 446d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 447d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 448d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 449d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 450d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 451d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 452d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 453d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 454d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 455d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 456d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 457d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 458d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 459d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4608808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 461d85f50d5SNeil Horman }; 462d85f50d5SNeil Horman 463d85f50d5SNeil Horman /* Display limits for a process */ 464d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 465d85f50d5SNeil Horman { 466d85f50d5SNeil Horman unsigned int i; 467d85f50d5SNeil Horman int count = 0; 468d85f50d5SNeil Horman unsigned long flags; 469d85f50d5SNeil Horman char *bufptr = buffer; 470d85f50d5SNeil Horman 471d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 472d85f50d5SNeil Horman 473d85f50d5SNeil Horman rcu_read_lock(); 474d85f50d5SNeil Horman if (!lock_task_sighand(task,&flags)) { 475d85f50d5SNeil Horman rcu_read_unlock(); 476d85f50d5SNeil Horman return 0; 477d85f50d5SNeil Horman } 478d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 479d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 480d85f50d5SNeil Horman rcu_read_unlock(); 481d85f50d5SNeil Horman 482d85f50d5SNeil Horman /* 483d85f50d5SNeil Horman * print the file header 484d85f50d5SNeil Horman */ 485d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 486d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 487d85f50d5SNeil Horman 488d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 489d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 490d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 491d85f50d5SNeil Horman lnames[i].name, "unlimited"); 492d85f50d5SNeil Horman else 493d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 494d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 495d85f50d5SNeil Horman 496d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 497d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 498d85f50d5SNeil Horman else 499d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 500d85f50d5SNeil Horman rlim[i].rlim_max); 501d85f50d5SNeil Horman 502d85f50d5SNeil Horman if (lnames[i].unit) 503d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 504d85f50d5SNeil Horman lnames[i].unit); 505d85f50d5SNeil Horman else 506d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 507d85f50d5SNeil Horman } 508d85f50d5SNeil Horman 509d85f50d5SNeil Horman return count; 510d85f50d5SNeil Horman } 511d85f50d5SNeil Horman 5121da177e4SLinus Torvalds /************************************************************************/ 5131da177e4SLinus Torvalds /* Here the fs part begins */ 5141da177e4SLinus Torvalds /************************************************************************/ 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* permission checks */ 517778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5181da177e4SLinus Torvalds { 519778c1144SEric W. Biederman struct task_struct *task; 520778c1144SEric W. Biederman int allowed = 0; 521df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 522df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 523df26c40eSEric W. Biederman * information. 524778c1144SEric W. Biederman */ 525778c1144SEric W. Biederman task = get_proc_task(inode); 526df26c40eSEric W. Biederman if (task) { 527006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 528778c1144SEric W. Biederman put_task_struct(task); 529df26c40eSEric W. Biederman } 530778c1144SEric W. Biederman return allowed; 5311da177e4SLinus Torvalds } 5321da177e4SLinus Torvalds 5336d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5346d76fa58SLinus Torvalds { 5356d76fa58SLinus Torvalds int error; 5366d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5376d76fa58SLinus Torvalds 5386d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5396d76fa58SLinus Torvalds return -EPERM; 5406d76fa58SLinus Torvalds 5416d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5426d76fa58SLinus Torvalds if (!error) 5436d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5446d76fa58SLinus Torvalds return error; 5456d76fa58SLinus Torvalds } 5466d76fa58SLinus Torvalds 547c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5486d76fa58SLinus Torvalds .setattr = proc_setattr, 5496d76fa58SLinus Torvalds }; 5506d76fa58SLinus Torvalds 551a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 552a1a2c409SMiklos Szeredi const struct seq_operations *op) 5531da177e4SLinus Torvalds { 55499f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 555cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5566b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 557a1a2c409SMiklos Szeredi struct fs_struct *fs = NULL; 558a1a2c409SMiklos Szeredi struct path root; 5595addc5ddSAl Viro struct proc_mounts *p; 5605addc5ddSAl Viro int ret = -EINVAL; 5615addc5ddSAl Viro 56299f89551SEric W. Biederman if (task) { 563cf7b708cSPavel Emelyanov rcu_read_lock(); 564cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 565cf7b708cSPavel Emelyanov if (nsp) { 566cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5676b3286edSKirill Korotaev if (ns) 5686b3286edSKirill Korotaev get_mnt_ns(ns); 569863c4702SAlexey Dobriyan } 570cf7b708cSPavel Emelyanov rcu_read_unlock(); 571a1a2c409SMiklos Szeredi if (ns) 572a1a2c409SMiklos Szeredi fs = get_fs_struct(task); 57399f89551SEric W. Biederman put_task_struct(task); 57499f89551SEric W. Biederman } 5751da177e4SLinus Torvalds 576a1a2c409SMiklos Szeredi if (!ns) 577a1a2c409SMiklos Szeredi goto err; 578a1a2c409SMiklos Szeredi if (!fs) 579a1a2c409SMiklos Szeredi goto err_put_ns; 580a1a2c409SMiklos Szeredi 581a1a2c409SMiklos Szeredi read_lock(&fs->lock); 582a1a2c409SMiklos Szeredi root = fs->root; 583a1a2c409SMiklos Szeredi path_get(&root); 584a1a2c409SMiklos Szeredi read_unlock(&fs->lock); 585a1a2c409SMiklos Szeredi put_fs_struct(fs); 586a1a2c409SMiklos Szeredi 5875addc5ddSAl Viro ret = -ENOMEM; 5885addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 589a1a2c409SMiklos Szeredi if (!p) 590a1a2c409SMiklos Szeredi goto err_put_path; 591a1a2c409SMiklos Szeredi 5925addc5ddSAl Viro file->private_data = &p->m; 593a1a2c409SMiklos Szeredi ret = seq_open(file, op); 594a1a2c409SMiklos Szeredi if (ret) 595a1a2c409SMiklos Szeredi goto err_free; 596a1a2c409SMiklos Szeredi 597a1a2c409SMiklos Szeredi p->m.private = p; 598a1a2c409SMiklos Szeredi p->ns = ns; 599a1a2c409SMiklos Szeredi p->root = root; 6006b3286edSKirill Korotaev p->event = ns->event; 601a1a2c409SMiklos Szeredi 6025addc5ddSAl Viro return 0; 603a1a2c409SMiklos Szeredi 604a1a2c409SMiklos Szeredi err_free: 6055addc5ddSAl Viro kfree(p); 606a1a2c409SMiklos Szeredi err_put_path: 607a1a2c409SMiklos Szeredi path_put(&root); 608a1a2c409SMiklos Szeredi err_put_ns: 6096b3286edSKirill Korotaev put_mnt_ns(ns); 610a1a2c409SMiklos Szeredi err: 6111da177e4SLinus Torvalds return ret; 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6151da177e4SLinus Torvalds { 616a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 617a1a2c409SMiklos Szeredi path_put(&p->root); 618a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6191da177e4SLinus Torvalds return seq_release(inode, file); 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds 6225addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6235addc5ddSAl Viro { 6245addc5ddSAl Viro struct proc_mounts *p = file->private_data; 625a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 6265addc5ddSAl Viro unsigned res = 0; 6275addc5ddSAl Viro 6285addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6295addc5ddSAl Viro 6305addc5ddSAl Viro spin_lock(&vfsmount_lock); 6315addc5ddSAl Viro if (p->event != ns->event) { 6325addc5ddSAl Viro p->event = ns->event; 6335addc5ddSAl Viro res = POLLERR; 6345addc5ddSAl Viro } 6355addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6365addc5ddSAl Viro 6375addc5ddSAl Viro return res; 6385addc5ddSAl Viro } 6395addc5ddSAl Viro 640a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 641a1a2c409SMiklos Szeredi { 642a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 643a1a2c409SMiklos Szeredi } 644a1a2c409SMiklos Szeredi 64500977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6461da177e4SLinus Torvalds .open = mounts_open, 6471da177e4SLinus Torvalds .read = seq_read, 6481da177e4SLinus Torvalds .llseek = seq_lseek, 6491da177e4SLinus Torvalds .release = mounts_release, 6505addc5ddSAl Viro .poll = mounts_poll, 6511da177e4SLinus Torvalds }; 6521da177e4SLinus Torvalds 6532d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6542d4d4864SRam Pai { 6552d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6562d4d4864SRam Pai } 6572d4d4864SRam Pai 6582d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6592d4d4864SRam Pai .open = mountinfo_open, 6602d4d4864SRam Pai .read = seq_read, 6612d4d4864SRam Pai .llseek = seq_lseek, 6622d4d4864SRam Pai .release = mounts_release, 6632d4d4864SRam Pai .poll = mounts_poll, 6642d4d4864SRam Pai }; 6652d4d4864SRam Pai 666b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 667b4629fe2SChuck Lever { 668a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 669b4629fe2SChuck Lever } 670b4629fe2SChuck Lever 67100977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 672b4629fe2SChuck Lever .open = mountstats_open, 673b4629fe2SChuck Lever .read = seq_read, 674b4629fe2SChuck Lever .llseek = seq_lseek, 675b4629fe2SChuck Lever .release = mounts_release, 676b4629fe2SChuck Lever }; 677b4629fe2SChuck Lever 6781da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6811da177e4SLinus Torvalds size_t count, loff_t *ppos) 6821da177e4SLinus Torvalds { 6832fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6841da177e4SLinus Torvalds unsigned long page; 6851da177e4SLinus Torvalds ssize_t length; 68699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 68799f89551SEric W. Biederman 68899f89551SEric W. Biederman length = -ESRCH; 68999f89551SEric W. Biederman if (!task) 69099f89551SEric W. Biederman goto out_no_task; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6931da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 69499f89551SEric W. Biederman 69599f89551SEric W. Biederman length = -ENOMEM; 696e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 69799f89551SEric W. Biederman goto out; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds if (length >= 0) 7021da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 7031da177e4SLinus Torvalds free_page(page); 70499f89551SEric W. Biederman out: 70599f89551SEric W. Biederman put_task_struct(task); 70699f89551SEric W. Biederman out_no_task: 7071da177e4SLinus Torvalds return length; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 71000977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7111da177e4SLinus Torvalds .read = proc_info_read, 7121da177e4SLinus Torvalds }; 7131da177e4SLinus Torvalds 714be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 715be614086SEric W. Biederman { 716be614086SEric W. Biederman struct inode *inode = m->private; 717be614086SEric W. Biederman struct pid_namespace *ns; 718be614086SEric W. Biederman struct pid *pid; 719be614086SEric W. Biederman struct task_struct *task; 720be614086SEric W. Biederman int ret; 721be614086SEric W. Biederman 722be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 723be614086SEric W. Biederman pid = proc_pid(inode); 724be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 725be614086SEric W. Biederman if (!task) 726be614086SEric W. Biederman return -ESRCH; 727be614086SEric W. Biederman 728be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 729be614086SEric W. Biederman 730be614086SEric W. Biederman put_task_struct(task); 731be614086SEric W. Biederman return ret; 732be614086SEric W. Biederman } 733be614086SEric W. Biederman 734be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 735be614086SEric W. Biederman { 736be614086SEric W. Biederman int ret; 737be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 738be614086SEric W. Biederman if (!ret) { 739be614086SEric W. Biederman struct seq_file *m = filp->private_data; 740be614086SEric W. Biederman 741be614086SEric W. Biederman m->private = inode; 742be614086SEric W. Biederman } 743be614086SEric W. Biederman return ret; 744be614086SEric W. Biederman } 745be614086SEric W. Biederman 746be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 747be614086SEric W. Biederman .open = proc_single_open, 748be614086SEric W. Biederman .read = seq_read, 749be614086SEric W. Biederman .llseek = seq_lseek, 750be614086SEric W. Biederman .release = single_release, 751be614086SEric W. Biederman }; 752be614086SEric W. Biederman 7531da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7561da177e4SLinus Torvalds return 0; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7601da177e4SLinus Torvalds size_t count, loff_t *ppos) 7611da177e4SLinus Torvalds { 7622fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7631da177e4SLinus Torvalds char *page; 7641da177e4SLinus Torvalds unsigned long src = *ppos; 7651da177e4SLinus Torvalds int ret = -ESRCH; 7661da177e4SLinus Torvalds struct mm_struct *mm; 7671da177e4SLinus Torvalds 76899f89551SEric W. Biederman if (!task) 76999f89551SEric W. Biederman goto out_no_task; 77099f89551SEric W. Biederman 771638fa202SRoland McGrath if (check_mem_permission(task)) 7721da177e4SLinus Torvalds goto out; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds ret = -ENOMEM; 775e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7761da177e4SLinus Torvalds if (!page) 7771da177e4SLinus Torvalds goto out; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds ret = 0; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds mm = get_task_mm(task); 7821da177e4SLinus Torvalds if (!mm) 7831da177e4SLinus Torvalds goto out_free; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds ret = -EIO; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 7881da177e4SLinus Torvalds goto out_put; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds ret = 0; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds while (count > 0) { 7931da177e4SLinus Torvalds int this_len, retval; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 7961da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 797638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 7981da177e4SLinus Torvalds if (!ret) 7991da177e4SLinus Torvalds ret = -EIO; 8001da177e4SLinus Torvalds break; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 8041da177e4SLinus Torvalds ret = -EFAULT; 8051da177e4SLinus Torvalds break; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds ret += retval; 8091da177e4SLinus Torvalds src += retval; 8101da177e4SLinus Torvalds buf += retval; 8111da177e4SLinus Torvalds count -= retval; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds *ppos = src; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds out_put: 8161da177e4SLinus Torvalds mmput(mm); 8171da177e4SLinus Torvalds out_free: 8181da177e4SLinus Torvalds free_page((unsigned long) page); 8191da177e4SLinus Torvalds out: 82099f89551SEric W. Biederman put_task_struct(task); 82199f89551SEric W. Biederman out_no_task: 8221da177e4SLinus Torvalds return ret; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds #define mem_write NULL 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds #ifndef mem_write 8281da177e4SLinus Torvalds /* This is a security hazard */ 82963967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8301da177e4SLinus Torvalds size_t count, loff_t *ppos) 8311da177e4SLinus Torvalds { 832f7ca54f4SFrederik Deweerdt int copied; 8331da177e4SLinus Torvalds char *page; 8342fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8351da177e4SLinus Torvalds unsigned long dst = *ppos; 8361da177e4SLinus Torvalds 83799f89551SEric W. Biederman copied = -ESRCH; 83899f89551SEric W. Biederman if (!task) 83999f89551SEric W. Biederman goto out_no_task; 8401da177e4SLinus Torvalds 841638fa202SRoland McGrath if (check_mem_permission(task)) 84299f89551SEric W. Biederman goto out; 84399f89551SEric W. Biederman 84499f89551SEric W. Biederman copied = -ENOMEM; 845e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8461da177e4SLinus Torvalds if (!page) 84799f89551SEric W. Biederman goto out; 8481da177e4SLinus Torvalds 849f7ca54f4SFrederik Deweerdt copied = 0; 8501da177e4SLinus Torvalds while (count > 0) { 8511da177e4SLinus Torvalds int this_len, retval; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8541da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8551da177e4SLinus Torvalds copied = -EFAULT; 8561da177e4SLinus Torvalds break; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8591da177e4SLinus Torvalds if (!retval) { 8601da177e4SLinus Torvalds if (!copied) 8611da177e4SLinus Torvalds copied = -EIO; 8621da177e4SLinus Torvalds break; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds copied += retval; 8651da177e4SLinus Torvalds buf += retval; 8661da177e4SLinus Torvalds dst += retval; 8671da177e4SLinus Torvalds count -= retval; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds *ppos = dst; 8701da177e4SLinus Torvalds free_page((unsigned long) page); 87199f89551SEric W. Biederman out: 87299f89551SEric W. Biederman put_task_struct(task); 87399f89551SEric W. Biederman out_no_task: 8741da177e4SLinus Torvalds return copied; 8751da177e4SLinus Torvalds } 8761da177e4SLinus Torvalds #endif 8771da177e4SLinus Torvalds 87885863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 8791da177e4SLinus Torvalds { 8801da177e4SLinus Torvalds switch (orig) { 8811da177e4SLinus Torvalds case 0: 8821da177e4SLinus Torvalds file->f_pos = offset; 8831da177e4SLinus Torvalds break; 8841da177e4SLinus Torvalds case 1: 8851da177e4SLinus Torvalds file->f_pos += offset; 8861da177e4SLinus Torvalds break; 8871da177e4SLinus Torvalds default: 8881da177e4SLinus Torvalds return -EINVAL; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds force_successful_syscall_return(); 8911da177e4SLinus Torvalds return file->f_pos; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds 89400977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 8951da177e4SLinus Torvalds .llseek = mem_lseek, 8961da177e4SLinus Torvalds .read = mem_read, 8971da177e4SLinus Torvalds .write = mem_write, 8981da177e4SLinus Torvalds .open = mem_open, 8991da177e4SLinus Torvalds }; 9001da177e4SLinus Torvalds 901315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 902315e28c8SJames Pearson size_t count, loff_t *ppos) 903315e28c8SJames Pearson { 904315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 905315e28c8SJames Pearson char *page; 906315e28c8SJames Pearson unsigned long src = *ppos; 907315e28c8SJames Pearson int ret = -ESRCH; 908315e28c8SJames Pearson struct mm_struct *mm; 909315e28c8SJames Pearson 910315e28c8SJames Pearson if (!task) 911315e28c8SJames Pearson goto out_no_task; 912315e28c8SJames Pearson 913006ebb40SStephen Smalley if (!ptrace_may_access(task, PTRACE_MODE_READ)) 914315e28c8SJames Pearson goto out; 915315e28c8SJames Pearson 916315e28c8SJames Pearson ret = -ENOMEM; 917315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 918315e28c8SJames Pearson if (!page) 919315e28c8SJames Pearson goto out; 920315e28c8SJames Pearson 921315e28c8SJames Pearson ret = 0; 922315e28c8SJames Pearson 923315e28c8SJames Pearson mm = get_task_mm(task); 924315e28c8SJames Pearson if (!mm) 925315e28c8SJames Pearson goto out_free; 926315e28c8SJames Pearson 927315e28c8SJames Pearson while (count > 0) { 928315e28c8SJames Pearson int this_len, retval, max_len; 929315e28c8SJames Pearson 930315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 931315e28c8SJames Pearson 932315e28c8SJames Pearson if (this_len <= 0) 933315e28c8SJames Pearson break; 934315e28c8SJames Pearson 935315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 936315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 937315e28c8SJames Pearson 938315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 939315e28c8SJames Pearson page, this_len, 0); 940315e28c8SJames Pearson 941315e28c8SJames Pearson if (retval <= 0) { 942315e28c8SJames Pearson ret = retval; 943315e28c8SJames Pearson break; 944315e28c8SJames Pearson } 945315e28c8SJames Pearson 946315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 947315e28c8SJames Pearson ret = -EFAULT; 948315e28c8SJames Pearson break; 949315e28c8SJames Pearson } 950315e28c8SJames Pearson 951315e28c8SJames Pearson ret += retval; 952315e28c8SJames Pearson src += retval; 953315e28c8SJames Pearson buf += retval; 954315e28c8SJames Pearson count -= retval; 955315e28c8SJames Pearson } 956315e28c8SJames Pearson *ppos = src; 957315e28c8SJames Pearson 958315e28c8SJames Pearson mmput(mm); 959315e28c8SJames Pearson out_free: 960315e28c8SJames Pearson free_page((unsigned long) page); 961315e28c8SJames Pearson out: 962315e28c8SJames Pearson put_task_struct(task); 963315e28c8SJames Pearson out_no_task: 964315e28c8SJames Pearson return ret; 965315e28c8SJames Pearson } 966315e28c8SJames Pearson 967315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 968315e28c8SJames Pearson .read = environ_read, 969315e28c8SJames Pearson }; 970315e28c8SJames Pearson 9711da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9721da177e4SLinus Torvalds size_t count, loff_t *ppos) 9731da177e4SLinus Torvalds { 9742fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9758578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9761da177e4SLinus Torvalds size_t len; 97799f89551SEric W. Biederman int oom_adjust; 9781da177e4SLinus Torvalds 97999f89551SEric W. Biederman if (!task) 98099f89551SEric W. Biederman return -ESRCH; 98199f89551SEric W. Biederman oom_adjust = task->oomkilladj; 98299f89551SEric W. Biederman put_task_struct(task); 98399f89551SEric W. Biederman 9848578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 9850c28f287SAkinobu Mita 9860c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 9901da177e4SLinus Torvalds size_t count, loff_t *ppos) 9911da177e4SLinus Torvalds { 99299f89551SEric W. Biederman struct task_struct *task; 9938578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 9941da177e4SLinus Torvalds int oom_adjust; 9951da177e4SLinus Torvalds 9968578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9978578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9988578cea7SEric W. Biederman count = sizeof(buffer) - 1; 9991da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 10001da177e4SLinus Torvalds return -EFAULT; 10011da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 10028ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 10038ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 10041da177e4SLinus Torvalds return -EINVAL; 10051da177e4SLinus Torvalds if (*end == '\n') 10061da177e4SLinus Torvalds end++; 10072fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 100899f89551SEric W. Biederman if (!task) 100999f89551SEric W. Biederman return -ESRCH; 10108fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10118fb4fc68SGuillem Jover put_task_struct(task); 10128fb4fc68SGuillem Jover return -EACCES; 10138fb4fc68SGuillem Jover } 10141da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 101599f89551SEric W. Biederman put_task_struct(task); 10161da177e4SLinus Torvalds if (end - buffer == 0) 10171da177e4SLinus Torvalds return -EIO; 10181da177e4SLinus Torvalds return end - buffer; 10191da177e4SLinus Torvalds } 10201da177e4SLinus Torvalds 102100977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10221da177e4SLinus Torvalds .read = oom_adjust_read, 10231da177e4SLinus Torvalds .write = oom_adjust_write, 10241da177e4SLinus Torvalds }; 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10271da177e4SLinus Torvalds #define TMPBUFLEN 21 10281da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10291da177e4SLinus Torvalds size_t count, loff_t *ppos) 10301da177e4SLinus Torvalds { 10312fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 103299f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10331da177e4SLinus Torvalds ssize_t length; 10341da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10351da177e4SLinus Torvalds 103699f89551SEric W. Biederman if (!task) 103799f89551SEric W. Biederman return -ESRCH; 10381da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10390c11b942SAl Viro audit_get_loginuid(task)); 104099f89551SEric W. Biederman put_task_struct(task); 10411da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10451da177e4SLinus Torvalds size_t count, loff_t *ppos) 10461da177e4SLinus Torvalds { 10472fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10481da177e4SLinus Torvalds char *page, *tmp; 10491da177e4SLinus Torvalds ssize_t length; 10501da177e4SLinus Torvalds uid_t loginuid; 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10531da177e4SLinus Torvalds return -EPERM; 10541da177e4SLinus Torvalds 105513b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10561da177e4SLinus Torvalds return -EPERM; 10571da177e4SLinus Torvalds 1058e0182909SAl Viro if (count >= PAGE_SIZE) 1059e0182909SAl Viro count = PAGE_SIZE - 1; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds if (*ppos != 0) { 10621da177e4SLinus Torvalds /* No partial writes. */ 10631da177e4SLinus Torvalds return -EINVAL; 10641da177e4SLinus Torvalds } 1065e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10661da177e4SLinus Torvalds if (!page) 10671da177e4SLinus Torvalds return -ENOMEM; 10681da177e4SLinus Torvalds length = -EFAULT; 10691da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10701da177e4SLinus Torvalds goto out_free_page; 10711da177e4SLinus Torvalds 1072e0182909SAl Viro page[count] = '\0'; 10731da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10741da177e4SLinus Torvalds if (tmp == page) { 10751da177e4SLinus Torvalds length = -EINVAL; 10761da177e4SLinus Torvalds goto out_free_page; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds } 107999f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 10801da177e4SLinus Torvalds if (likely(length == 0)) 10811da177e4SLinus Torvalds length = count; 10821da177e4SLinus Torvalds 10831da177e4SLinus Torvalds out_free_page: 10841da177e4SLinus Torvalds free_page((unsigned long) page); 10851da177e4SLinus Torvalds return length; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 108800977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 10891da177e4SLinus Torvalds .read = proc_loginuid_read, 10901da177e4SLinus Torvalds .write = proc_loginuid_write, 10911da177e4SLinus Torvalds }; 10921e0bd755SEric Paris 10931e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 10941e0bd755SEric Paris size_t count, loff_t *ppos) 10951e0bd755SEric Paris { 10961e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 10971e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 10981e0bd755SEric Paris ssize_t length; 10991e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 11001e0bd755SEric Paris 11011e0bd755SEric Paris if (!task) 11021e0bd755SEric Paris return -ESRCH; 11031e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 11041e0bd755SEric Paris audit_get_sessionid(task)); 11051e0bd755SEric Paris put_task_struct(task); 11061e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11071e0bd755SEric Paris } 11081e0bd755SEric Paris 11091e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11101e0bd755SEric Paris .read = proc_sessionid_read, 11111e0bd755SEric Paris }; 11121da177e4SLinus Torvalds #endif 11131da177e4SLinus Torvalds 1114f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1115f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1116f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1117f4f154fdSAkinobu Mita { 1118f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1119f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1120f4f154fdSAkinobu Mita size_t len; 1121f4f154fdSAkinobu Mita int make_it_fail; 1122f4f154fdSAkinobu Mita 1123f4f154fdSAkinobu Mita if (!task) 1124f4f154fdSAkinobu Mita return -ESRCH; 1125f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1126f4f154fdSAkinobu Mita put_task_struct(task); 1127f4f154fdSAkinobu Mita 1128f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11290c28f287SAkinobu Mita 11300c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1131f4f154fdSAkinobu Mita } 1132f4f154fdSAkinobu Mita 1133f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1134f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1135f4f154fdSAkinobu Mita { 1136f4f154fdSAkinobu Mita struct task_struct *task; 1137f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1138f4f154fdSAkinobu Mita int make_it_fail; 1139f4f154fdSAkinobu Mita 1140f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1141f4f154fdSAkinobu Mita return -EPERM; 1142f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1143f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1144f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1145f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1146f4f154fdSAkinobu Mita return -EFAULT; 1147f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1148f4f154fdSAkinobu Mita if (*end == '\n') 1149f4f154fdSAkinobu Mita end++; 1150f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1151f4f154fdSAkinobu Mita if (!task) 1152f4f154fdSAkinobu Mita return -ESRCH; 1153f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1154f4f154fdSAkinobu Mita put_task_struct(task); 1155f4f154fdSAkinobu Mita if (end - buffer == 0) 1156f4f154fdSAkinobu Mita return -EIO; 1157f4f154fdSAkinobu Mita return end - buffer; 1158f4f154fdSAkinobu Mita } 1159f4f154fdSAkinobu Mita 116000977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1161f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1162f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1163f4f154fdSAkinobu Mita }; 1164f4f154fdSAkinobu Mita #endif 1165f4f154fdSAkinobu Mita 11669745512cSArjan van de Ven 116743ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 116843ae34cbSIngo Molnar /* 116943ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 117043ae34cbSIngo Molnar */ 117143ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 117243ae34cbSIngo Molnar { 117343ae34cbSIngo Molnar struct inode *inode = m->private; 117443ae34cbSIngo Molnar struct task_struct *p; 117543ae34cbSIngo Molnar 117643ae34cbSIngo Molnar WARN_ON(!inode); 117743ae34cbSIngo Molnar 117843ae34cbSIngo Molnar p = get_proc_task(inode); 117943ae34cbSIngo Molnar if (!p) 118043ae34cbSIngo Molnar return -ESRCH; 118143ae34cbSIngo Molnar proc_sched_show_task(p, m); 118243ae34cbSIngo Molnar 118343ae34cbSIngo Molnar put_task_struct(p); 118443ae34cbSIngo Molnar 118543ae34cbSIngo Molnar return 0; 118643ae34cbSIngo Molnar } 118743ae34cbSIngo Molnar 118843ae34cbSIngo Molnar static ssize_t 118943ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 119043ae34cbSIngo Molnar size_t count, loff_t *offset) 119143ae34cbSIngo Molnar { 119243ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 119343ae34cbSIngo Molnar struct task_struct *p; 119443ae34cbSIngo Molnar 119543ae34cbSIngo Molnar WARN_ON(!inode); 119643ae34cbSIngo Molnar 119743ae34cbSIngo Molnar p = get_proc_task(inode); 119843ae34cbSIngo Molnar if (!p) 119943ae34cbSIngo Molnar return -ESRCH; 120043ae34cbSIngo Molnar proc_sched_set_task(p); 120143ae34cbSIngo Molnar 120243ae34cbSIngo Molnar put_task_struct(p); 120343ae34cbSIngo Molnar 120443ae34cbSIngo Molnar return count; 120543ae34cbSIngo Molnar } 120643ae34cbSIngo Molnar 120743ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 120843ae34cbSIngo Molnar { 120943ae34cbSIngo Molnar int ret; 121043ae34cbSIngo Molnar 121143ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 121243ae34cbSIngo Molnar if (!ret) { 121343ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 121443ae34cbSIngo Molnar 121543ae34cbSIngo Molnar m->private = inode; 121643ae34cbSIngo Molnar } 121743ae34cbSIngo Molnar return ret; 121843ae34cbSIngo Molnar } 121943ae34cbSIngo Molnar 122043ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 122143ae34cbSIngo Molnar .open = sched_open, 122243ae34cbSIngo Molnar .read = seq_read, 122343ae34cbSIngo Molnar .write = sched_write, 122443ae34cbSIngo Molnar .llseek = seq_lseek, 12255ea473a1SAlexey Dobriyan .release = single_release, 122643ae34cbSIngo Molnar }; 122743ae34cbSIngo Molnar 122843ae34cbSIngo Molnar #endif 122943ae34cbSIngo Molnar 1230925d1c40SMatt Helsley /* 1231925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1232925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1233925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1234925d1c40SMatt Helsley */ 1235925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1236925d1c40SMatt Helsley { 1237925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1238925d1c40SMatt Helsley } 1239925d1c40SMatt Helsley 1240925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1241925d1c40SMatt Helsley { 1242925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1243925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1244925d1c40SMatt Helsley fput(mm->exe_file); 1245925d1c40SMatt Helsley mm->exe_file = NULL; 1246925d1c40SMatt Helsley } 1247925d1c40SMatt Helsley 1248925d1c40SMatt Helsley } 1249925d1c40SMatt Helsley 1250925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1251925d1c40SMatt Helsley { 1252925d1c40SMatt Helsley if (new_exe_file) 1253925d1c40SMatt Helsley get_file(new_exe_file); 1254925d1c40SMatt Helsley if (mm->exe_file) 1255925d1c40SMatt Helsley fput(mm->exe_file); 1256925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1257925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1258925d1c40SMatt Helsley } 1259925d1c40SMatt Helsley 1260925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1261925d1c40SMatt Helsley { 1262925d1c40SMatt Helsley struct file *exe_file; 1263925d1c40SMatt Helsley 1264925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1265925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1266925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1267925d1c40SMatt Helsley exe_file = mm->exe_file; 1268925d1c40SMatt Helsley if (exe_file) 1269925d1c40SMatt Helsley get_file(exe_file); 1270925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1271925d1c40SMatt Helsley return exe_file; 1272925d1c40SMatt Helsley } 1273925d1c40SMatt Helsley 1274925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1275925d1c40SMatt Helsley { 1276925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1277925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1278925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1279925d1c40SMatt Helsley } 1280925d1c40SMatt Helsley 1281925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1282925d1c40SMatt Helsley { 1283925d1c40SMatt Helsley struct task_struct *task; 1284925d1c40SMatt Helsley struct mm_struct *mm; 1285925d1c40SMatt Helsley struct file *exe_file; 1286925d1c40SMatt Helsley 1287925d1c40SMatt Helsley task = get_proc_task(inode); 1288925d1c40SMatt Helsley if (!task) 1289925d1c40SMatt Helsley return -ENOENT; 1290925d1c40SMatt Helsley mm = get_task_mm(task); 1291925d1c40SMatt Helsley put_task_struct(task); 1292925d1c40SMatt Helsley if (!mm) 1293925d1c40SMatt Helsley return -ENOENT; 1294925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1295925d1c40SMatt Helsley mmput(mm); 1296925d1c40SMatt Helsley if (exe_file) { 1297925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1298925d1c40SMatt Helsley path_get(&exe_file->f_path); 1299925d1c40SMatt Helsley fput(exe_file); 1300925d1c40SMatt Helsley return 0; 1301925d1c40SMatt Helsley } else 1302925d1c40SMatt Helsley return -ENOENT; 1303925d1c40SMatt Helsley } 1304925d1c40SMatt Helsley 1305008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13061da177e4SLinus Torvalds { 13071da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13081da177e4SLinus Torvalds int error = -EACCES; 13091da177e4SLinus Torvalds 13101da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13111d957f9bSJan Blunck path_put(&nd->path); 13121da177e4SLinus Torvalds 1313778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1314778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13151da177e4SLinus Torvalds goto out; 13161da177e4SLinus Torvalds 13173dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13181da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13191da177e4SLinus Torvalds out: 1320008b150aSAl Viro return ERR_PTR(error); 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds 13233dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13241da177e4SLinus Torvalds { 1325e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13263dcd25f3SJan Blunck char *pathname; 13271da177e4SLinus Torvalds int len; 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds if (!tmp) 13301da177e4SLinus Torvalds return -ENOMEM; 13311da177e4SLinus Torvalds 1332cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13333dcd25f3SJan Blunck len = PTR_ERR(pathname); 13343dcd25f3SJan Blunck if (IS_ERR(pathname)) 13351da177e4SLinus Torvalds goto out; 13363dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds if (len > buflen) 13391da177e4SLinus Torvalds len = buflen; 13403dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13411da177e4SLinus Torvalds len = -EFAULT; 13421da177e4SLinus Torvalds out: 13431da177e4SLinus Torvalds free_page((unsigned long)tmp); 13441da177e4SLinus Torvalds return len; 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13481da177e4SLinus Torvalds { 13491da177e4SLinus Torvalds int error = -EACCES; 13501da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13513dcd25f3SJan Blunck struct path path; 13521da177e4SLinus Torvalds 1353778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1354778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13551da177e4SLinus Torvalds goto out; 13561da177e4SLinus Torvalds 13573dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13581da177e4SLinus Torvalds if (error) 13591da177e4SLinus Torvalds goto out; 13601da177e4SLinus Torvalds 13613dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13623dcd25f3SJan Blunck path_put(&path); 13631da177e4SLinus Torvalds out: 13641da177e4SLinus Torvalds return error; 13651da177e4SLinus Torvalds } 13661da177e4SLinus Torvalds 1367c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13681da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13696d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13706d76fa58SLinus Torvalds .setattr = proc_setattr, 13711da177e4SLinus Torvalds }; 13721da177e4SLinus Torvalds 137328a6d671SEric W. Biederman 137428a6d671SEric W. Biederman /* building an inode */ 137528a6d671SEric W. Biederman 137628a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 137728a6d671SEric W. Biederman { 137828a6d671SEric W. Biederman int dumpable = 0; 137928a6d671SEric W. Biederman struct mm_struct *mm; 138028a6d671SEric W. Biederman 138128a6d671SEric W. Biederman task_lock(task); 138228a6d671SEric W. Biederman mm = task->mm; 138328a6d671SEric W. Biederman if (mm) 13846c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 138528a6d671SEric W. Biederman task_unlock(task); 138628a6d671SEric W. Biederman if(dumpable == 1) 138728a6d671SEric W. Biederman return 1; 138828a6d671SEric W. Biederman return 0; 138928a6d671SEric W. Biederman } 139028a6d671SEric W. Biederman 139128a6d671SEric W. Biederman 139261a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 139328a6d671SEric W. Biederman { 139428a6d671SEric W. Biederman struct inode * inode; 139528a6d671SEric W. Biederman struct proc_inode *ei; 139628a6d671SEric W. Biederman 139728a6d671SEric W. Biederman /* We need a new inode */ 139828a6d671SEric W. Biederman 139928a6d671SEric W. Biederman inode = new_inode(sb); 140028a6d671SEric W. Biederman if (!inode) 140128a6d671SEric W. Biederman goto out; 140228a6d671SEric W. Biederman 140328a6d671SEric W. Biederman /* Common stuff */ 140428a6d671SEric W. Biederman ei = PROC_I(inode); 140528a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 140628a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 140728a6d671SEric W. Biederman 140828a6d671SEric W. Biederman /* 140928a6d671SEric W. Biederman * grab the reference to task. 141028a6d671SEric W. Biederman */ 14111a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 141228a6d671SEric W. Biederman if (!ei->pid) 141328a6d671SEric W. Biederman goto out_unlock; 141428a6d671SEric W. Biederman 141528a6d671SEric W. Biederman inode->i_uid = 0; 141628a6d671SEric W. Biederman inode->i_gid = 0; 141728a6d671SEric W. Biederman if (task_dumpable(task)) { 141828a6d671SEric W. Biederman inode->i_uid = task->euid; 141928a6d671SEric W. Biederman inode->i_gid = task->egid; 142028a6d671SEric W. Biederman } 142128a6d671SEric W. Biederman security_task_to_inode(task, inode); 142228a6d671SEric W. Biederman 142328a6d671SEric W. Biederman out: 142428a6d671SEric W. Biederman return inode; 142528a6d671SEric W. Biederman 142628a6d671SEric W. Biederman out_unlock: 142728a6d671SEric W. Biederman iput(inode); 142828a6d671SEric W. Biederman return NULL; 142928a6d671SEric W. Biederman } 143028a6d671SEric W. Biederman 143128a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 143228a6d671SEric W. Biederman { 143328a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 143428a6d671SEric W. Biederman struct task_struct *task; 143528a6d671SEric W. Biederman generic_fillattr(inode, stat); 143628a6d671SEric W. Biederman 143728a6d671SEric W. Biederman rcu_read_lock(); 143828a6d671SEric W. Biederman stat->uid = 0; 143928a6d671SEric W. Biederman stat->gid = 0; 144028a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 144128a6d671SEric W. Biederman if (task) { 144228a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 144328a6d671SEric W. Biederman task_dumpable(task)) { 144428a6d671SEric W. Biederman stat->uid = task->euid; 144528a6d671SEric W. Biederman stat->gid = task->egid; 144628a6d671SEric W. Biederman } 144728a6d671SEric W. Biederman } 144828a6d671SEric W. Biederman rcu_read_unlock(); 144928a6d671SEric W. Biederman return 0; 145028a6d671SEric W. Biederman } 145128a6d671SEric W. Biederman 145228a6d671SEric W. Biederman /* dentry stuff */ 145328a6d671SEric W. Biederman 145428a6d671SEric W. Biederman /* 145528a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 145628a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 145728a6d671SEric W. Biederman * due to the way we treat inodes. 145828a6d671SEric W. Biederman * 145928a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 146028a6d671SEric W. Biederman * performed a setuid(), etc. 146128a6d671SEric W. Biederman * 146228a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 146328a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 146428a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 146528a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 146628a6d671SEric W. Biederman * made this apply to all per process world readable and executable 146728a6d671SEric W. Biederman * directories. 146828a6d671SEric W. Biederman */ 146928a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 147028a6d671SEric W. Biederman { 147128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 147228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 147328a6d671SEric W. Biederman if (task) { 147428a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 147528a6d671SEric W. Biederman task_dumpable(task)) { 147628a6d671SEric W. Biederman inode->i_uid = task->euid; 147728a6d671SEric W. Biederman inode->i_gid = task->egid; 147828a6d671SEric W. Biederman } else { 147928a6d671SEric W. Biederman inode->i_uid = 0; 148028a6d671SEric W. Biederman inode->i_gid = 0; 148128a6d671SEric W. Biederman } 148228a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 148328a6d671SEric W. Biederman security_task_to_inode(task, inode); 148428a6d671SEric W. Biederman put_task_struct(task); 148528a6d671SEric W. Biederman return 1; 148628a6d671SEric W. Biederman } 148728a6d671SEric W. Biederman d_drop(dentry); 148828a6d671SEric W. Biederman return 0; 148928a6d671SEric W. Biederman } 149028a6d671SEric W. Biederman 149128a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 149228a6d671SEric W. Biederman { 149328a6d671SEric W. Biederman /* Is the task we represent dead? 149428a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 149528a6d671SEric W. Biederman * kill it immediately. 149628a6d671SEric W. Biederman */ 149728a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 149828a6d671SEric W. Biederman } 149928a6d671SEric W. Biederman 150028a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 150128a6d671SEric W. Biederman { 150228a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 150328a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 150428a6d671SEric W. Biederman }; 150528a6d671SEric W. Biederman 150628a6d671SEric W. Biederman /* Lookups */ 150728a6d671SEric W. Biederman 1508c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1509c5141e6dSEric Dumazet struct task_struct *, const void *); 151061a28784SEric W. Biederman 15111c0d04c9SEric W. Biederman /* 15121c0d04c9SEric W. Biederman * Fill a directory entry. 15131c0d04c9SEric W. Biederman * 15141c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15151c0d04c9SEric W. Biederman * file type from dcache entry. 15161c0d04c9SEric W. Biederman * 15171c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15181c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15191c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15201c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15211c0d04c9SEric W. Biederman * by stat. 15221c0d04c9SEric W. Biederman */ 152361a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 152461a28784SEric W. Biederman char *name, int len, 1525c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 152661a28784SEric W. Biederman { 15272fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 152861a28784SEric W. Biederman struct inode *inode; 152961a28784SEric W. Biederman struct qstr qname; 153061a28784SEric W. Biederman ino_t ino = 0; 153161a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 153261a28784SEric W. Biederman 153361a28784SEric W. Biederman qname.name = name; 153461a28784SEric W. Biederman qname.len = len; 153561a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 153661a28784SEric W. Biederman 153761a28784SEric W. Biederman child = d_lookup(dir, &qname); 153861a28784SEric W. Biederman if (!child) { 153961a28784SEric W. Biederman struct dentry *new; 154061a28784SEric W. Biederman new = d_alloc(dir, &qname); 154161a28784SEric W. Biederman if (new) { 154261a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 154361a28784SEric W. Biederman if (child) 154461a28784SEric W. Biederman dput(new); 154561a28784SEric W. Biederman else 154661a28784SEric W. Biederman child = new; 154761a28784SEric W. Biederman } 154861a28784SEric W. Biederman } 154961a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 155061a28784SEric W. Biederman goto end_instantiate; 155161a28784SEric W. Biederman inode = child->d_inode; 155261a28784SEric W. Biederman if (inode) { 155361a28784SEric W. Biederman ino = inode->i_ino; 155461a28784SEric W. Biederman type = inode->i_mode >> 12; 155561a28784SEric W. Biederman } 155661a28784SEric W. Biederman dput(child); 155761a28784SEric W. Biederman end_instantiate: 155861a28784SEric W. Biederman if (!ino) 155961a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 156061a28784SEric W. Biederman if (!ino) 156161a28784SEric W. Biederman ino = 1; 156261a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 156361a28784SEric W. Biederman } 156461a28784SEric W. Biederman 156528a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 156628a6d671SEric W. Biederman { 156728a6d671SEric W. Biederman const char *name = dentry->d_name.name; 156828a6d671SEric W. Biederman int len = dentry->d_name.len; 156928a6d671SEric W. Biederman unsigned n = 0; 157028a6d671SEric W. Biederman 157128a6d671SEric W. Biederman if (len > 1 && *name == '0') 157228a6d671SEric W. Biederman goto out; 157328a6d671SEric W. Biederman while (len-- > 0) { 157428a6d671SEric W. Biederman unsigned c = *name++ - '0'; 157528a6d671SEric W. Biederman if (c > 9) 157628a6d671SEric W. Biederman goto out; 157728a6d671SEric W. Biederman if (n >= (~0U-9)/10) 157828a6d671SEric W. Biederman goto out; 157928a6d671SEric W. Biederman n *= 10; 158028a6d671SEric W. Biederman n += c; 158128a6d671SEric W. Biederman } 158228a6d671SEric W. Biederman return n; 158328a6d671SEric W. Biederman out: 158428a6d671SEric W. Biederman return ~0U; 158528a6d671SEric W. Biederman } 158628a6d671SEric W. Biederman 158727932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 158827932742SMiklos Szeredi 15893dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 159028a6d671SEric W. Biederman { 159128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 159228a6d671SEric W. Biederman struct files_struct *files = NULL; 159328a6d671SEric W. Biederman struct file *file; 159428a6d671SEric W. Biederman int fd = proc_fd(inode); 159528a6d671SEric W. Biederman 159628a6d671SEric W. Biederman if (task) { 159728a6d671SEric W. Biederman files = get_files_struct(task); 159828a6d671SEric W. Biederman put_task_struct(task); 159928a6d671SEric W. Biederman } 160028a6d671SEric W. Biederman if (files) { 160128a6d671SEric W. Biederman /* 160228a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 160328a6d671SEric W. Biederman * hold ->file_lock. 160428a6d671SEric W. Biederman */ 160528a6d671SEric W. Biederman spin_lock(&files->file_lock); 160628a6d671SEric W. Biederman file = fcheck_files(files, fd); 160728a6d671SEric W. Biederman if (file) { 16083dcd25f3SJan Blunck if (path) { 16093dcd25f3SJan Blunck *path = file->f_path; 16103dcd25f3SJan Blunck path_get(&file->f_path); 16113dcd25f3SJan Blunck } 161227932742SMiklos Szeredi if (info) 161327932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 161427932742SMiklos Szeredi "pos:\t%lli\n" 161527932742SMiklos Szeredi "flags:\t0%o\n", 161627932742SMiklos Szeredi (long long) file->f_pos, 161727932742SMiklos Szeredi file->f_flags); 161828a6d671SEric W. Biederman spin_unlock(&files->file_lock); 161928a6d671SEric W. Biederman put_files_struct(files); 162028a6d671SEric W. Biederman return 0; 162128a6d671SEric W. Biederman } 162228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 162328a6d671SEric W. Biederman put_files_struct(files); 162428a6d671SEric W. Biederman } 162528a6d671SEric W. Biederman return -ENOENT; 162628a6d671SEric W. Biederman } 162728a6d671SEric W. Biederman 16283dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 162927932742SMiklos Szeredi { 16303dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 163127932742SMiklos Szeredi } 163227932742SMiklos Szeredi 163328a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 163428a6d671SEric W. Biederman { 163528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 163628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 163728a6d671SEric W. Biederman int fd = proc_fd(inode); 163828a6d671SEric W. Biederman struct files_struct *files; 163928a6d671SEric W. Biederman 164028a6d671SEric W. Biederman if (task) { 164128a6d671SEric W. Biederman files = get_files_struct(task); 164228a6d671SEric W. Biederman if (files) { 164328a6d671SEric W. Biederman rcu_read_lock(); 164428a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 164528a6d671SEric W. Biederman rcu_read_unlock(); 164628a6d671SEric W. Biederman put_files_struct(files); 164728a6d671SEric W. Biederman if (task_dumpable(task)) { 164828a6d671SEric W. Biederman inode->i_uid = task->euid; 164928a6d671SEric W. Biederman inode->i_gid = task->egid; 165028a6d671SEric W. Biederman } else { 165128a6d671SEric W. Biederman inode->i_uid = 0; 165228a6d671SEric W. Biederman inode->i_gid = 0; 165328a6d671SEric W. Biederman } 165428a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 165528a6d671SEric W. Biederman security_task_to_inode(task, inode); 165628a6d671SEric W. Biederman put_task_struct(task); 165728a6d671SEric W. Biederman return 1; 165828a6d671SEric W. Biederman } 165928a6d671SEric W. Biederman rcu_read_unlock(); 166028a6d671SEric W. Biederman put_files_struct(files); 166128a6d671SEric W. Biederman } 166228a6d671SEric W. Biederman put_task_struct(task); 166328a6d671SEric W. Biederman } 166428a6d671SEric W. Biederman d_drop(dentry); 166528a6d671SEric W. Biederman return 0; 166628a6d671SEric W. Biederman } 166728a6d671SEric W. Biederman 166828a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 166928a6d671SEric W. Biederman { 167028a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 167128a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 167228a6d671SEric W. Biederman }; 167328a6d671SEric W. Biederman 1674444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1675c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 167628a6d671SEric W. Biederman { 1677c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 167828a6d671SEric W. Biederman struct file *file; 167928a6d671SEric W. Biederman struct files_struct *files; 168028a6d671SEric W. Biederman struct inode *inode; 168128a6d671SEric W. Biederman struct proc_inode *ei; 1682444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 168328a6d671SEric W. Biederman 168461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 168528a6d671SEric W. Biederman if (!inode) 168628a6d671SEric W. Biederman goto out; 168728a6d671SEric W. Biederman ei = PROC_I(inode); 168828a6d671SEric W. Biederman ei->fd = fd; 168928a6d671SEric W. Biederman files = get_files_struct(task); 169028a6d671SEric W. Biederman if (!files) 1691444ceed8SEric W. Biederman goto out_iput; 169228a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 169328a6d671SEric W. Biederman 169428a6d671SEric W. Biederman /* 169528a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 169628a6d671SEric W. Biederman * hold ->file_lock. 169728a6d671SEric W. Biederman */ 169828a6d671SEric W. Biederman spin_lock(&files->file_lock); 169928a6d671SEric W. Biederman file = fcheck_files(files, fd); 170028a6d671SEric W. Biederman if (!file) 1701444ceed8SEric W. Biederman goto out_unlock; 170228a6d671SEric W. Biederman if (file->f_mode & 1) 170328a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 170428a6d671SEric W. Biederman if (file->f_mode & 2) 170528a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 170628a6d671SEric W. Biederman spin_unlock(&files->file_lock); 170728a6d671SEric W. Biederman put_files_struct(files); 1708444ceed8SEric W. Biederman 170928a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 171028a6d671SEric W. Biederman inode->i_size = 64; 171128a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 171228a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 171328a6d671SEric W. Biederman d_add(dentry, inode); 171428a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 171528a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1716444ceed8SEric W. Biederman error = NULL; 1717444ceed8SEric W. Biederman 1718444ceed8SEric W. Biederman out: 1719444ceed8SEric W. Biederman return error; 1720444ceed8SEric W. Biederman out_unlock: 1721444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1722444ceed8SEric W. Biederman put_files_struct(files); 1723444ceed8SEric W. Biederman out_iput: 1724444ceed8SEric W. Biederman iput(inode); 1725444ceed8SEric W. Biederman goto out; 1726444ceed8SEric W. Biederman } 1727444ceed8SEric W. Biederman 172827932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 172927932742SMiklos Szeredi struct dentry *dentry, 173027932742SMiklos Szeredi instantiate_t instantiate) 1731444ceed8SEric W. Biederman { 1732444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1733444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1734444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1735444ceed8SEric W. Biederman 1736444ceed8SEric W. Biederman if (!task) 1737444ceed8SEric W. Biederman goto out_no_task; 1738444ceed8SEric W. Biederman if (fd == ~0U) 1739444ceed8SEric W. Biederman goto out; 1740444ceed8SEric W. Biederman 174127932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 174228a6d671SEric W. Biederman out: 174328a6d671SEric W. Biederman put_task_struct(task); 174428a6d671SEric W. Biederman out_no_task: 174528a6d671SEric W. Biederman return result; 174628a6d671SEric W. Biederman } 174728a6d671SEric W. Biederman 174827932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 174927932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17501da177e4SLinus Torvalds { 17512fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17525634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 175399f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1754457c2510SPavel Emelyanov unsigned int fd, ino; 17551da177e4SLinus Torvalds int retval; 17561da177e4SLinus Torvalds struct files_struct * files; 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds retval = -ENOENT; 175999f89551SEric W. Biederman if (!p) 176099f89551SEric W. Biederman goto out_no_task; 17611da177e4SLinus Torvalds retval = 0; 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds fd = filp->f_pos; 17641da177e4SLinus Torvalds switch (fd) { 17651da177e4SLinus Torvalds case 0: 17661da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 17671da177e4SLinus Torvalds goto out; 17681da177e4SLinus Torvalds filp->f_pos++; 17691da177e4SLinus Torvalds case 1: 17705634708bSEric W. Biederman ino = parent_ino(dentry); 17711da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 17721da177e4SLinus Torvalds goto out; 17731da177e4SLinus Torvalds filp->f_pos++; 17741da177e4SLinus Torvalds default: 17751da177e4SLinus Torvalds files = get_files_struct(p); 17761da177e4SLinus Torvalds if (!files) 17771da177e4SLinus Torvalds goto out; 1778b835996fSDipankar Sarma rcu_read_lock(); 17791da177e4SLinus Torvalds for (fd = filp->f_pos-2; 17809b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 17811da177e4SLinus Torvalds fd++, filp->f_pos++) { 178227932742SMiklos Szeredi char name[PROC_NUMBUF]; 178327932742SMiklos Szeredi int len; 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 17861da177e4SLinus Torvalds continue; 1787b835996fSDipankar Sarma rcu_read_unlock(); 17881da177e4SLinus Torvalds 178927932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 179027932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 179127932742SMiklos Szeredi name, len, instantiate, 179227932742SMiklos Szeredi p, &fd) < 0) { 1793b835996fSDipankar Sarma rcu_read_lock(); 17941da177e4SLinus Torvalds break; 17951da177e4SLinus Torvalds } 1796b835996fSDipankar Sarma rcu_read_lock(); 17971da177e4SLinus Torvalds } 1798b835996fSDipankar Sarma rcu_read_unlock(); 17991da177e4SLinus Torvalds put_files_struct(files); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds out: 180299f89551SEric W. Biederman put_task_struct(p); 180399f89551SEric W. Biederman out_no_task: 18041da177e4SLinus Torvalds return retval; 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 180727932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 180827932742SMiklos Szeredi struct nameidata *nd) 180927932742SMiklos Szeredi { 181027932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 181127932742SMiklos Szeredi } 181227932742SMiklos Szeredi 181327932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 181427932742SMiklos Szeredi { 181527932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 181627932742SMiklos Szeredi } 181727932742SMiklos Szeredi 181827932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 181927932742SMiklos Szeredi size_t len, loff_t *ppos) 182027932742SMiklos Szeredi { 182127932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18223dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 182327932742SMiklos Szeredi if (!err) 182427932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 182527932742SMiklos Szeredi return err; 182627932742SMiklos Szeredi } 182727932742SMiklos Szeredi 182827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 182927932742SMiklos Szeredi .open = nonseekable_open, 183027932742SMiklos Szeredi .read = proc_fdinfo_read, 183127932742SMiklos Szeredi }; 183227932742SMiklos Szeredi 183300977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18341da177e4SLinus Torvalds .read = generic_read_dir, 18351da177e4SLinus Torvalds .readdir = proc_readfd, 18361da177e4SLinus Torvalds }; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds /* 18398948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18408948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18418948e11fSAlexey Dobriyan */ 18428948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 18438948e11fSAlexey Dobriyan struct nameidata *nd) 18448948e11fSAlexey Dobriyan { 18458948e11fSAlexey Dobriyan int rv; 18468948e11fSAlexey Dobriyan 18478948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18488948e11fSAlexey Dobriyan if (rv == 0) 18498948e11fSAlexey Dobriyan return 0; 18508948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18518948e11fSAlexey Dobriyan rv = 0; 18528948e11fSAlexey Dobriyan return rv; 18538948e11fSAlexey Dobriyan } 18548948e11fSAlexey Dobriyan 18558948e11fSAlexey Dobriyan /* 18561da177e4SLinus Torvalds * proc directories can do almost nothing.. 18571da177e4SLinus Torvalds */ 1858c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18591da177e4SLinus Torvalds .lookup = proc_lookupfd, 18608948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18616d76fa58SLinus Torvalds .setattr = proc_setattr, 18621da177e4SLinus Torvalds }; 18631da177e4SLinus Torvalds 186427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 186527932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 186627932742SMiklos Szeredi { 186727932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 186827932742SMiklos Szeredi struct inode *inode; 186927932742SMiklos Szeredi struct proc_inode *ei; 187027932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 187127932742SMiklos Szeredi 187227932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 187327932742SMiklos Szeredi if (!inode) 187427932742SMiklos Szeredi goto out; 187527932742SMiklos Szeredi ei = PROC_I(inode); 187627932742SMiklos Szeredi ei->fd = fd; 187727932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 187827932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 187927932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 188027932742SMiklos Szeredi d_add(dentry, inode); 188127932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 188227932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 188327932742SMiklos Szeredi error = NULL; 188427932742SMiklos Szeredi 188527932742SMiklos Szeredi out: 188627932742SMiklos Szeredi return error; 188727932742SMiklos Szeredi } 188827932742SMiklos Szeredi 188927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 189027932742SMiklos Szeredi struct dentry *dentry, 189127932742SMiklos Szeredi struct nameidata *nd) 189227932742SMiklos Szeredi { 189327932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 189427932742SMiklos Szeredi } 189527932742SMiklos Szeredi 189627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 189727932742SMiklos Szeredi { 189827932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 189927932742SMiklos Szeredi proc_fdinfo_instantiate); 190027932742SMiklos Szeredi } 190127932742SMiklos Szeredi 190227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 190327932742SMiklos Szeredi .read = generic_read_dir, 190427932742SMiklos Szeredi .readdir = proc_readfdinfo, 190527932742SMiklos Szeredi }; 190627932742SMiklos Szeredi 190727932742SMiklos Szeredi /* 190827932742SMiklos Szeredi * proc directories can do almost nothing.. 190927932742SMiklos Szeredi */ 191027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 191127932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 191227932742SMiklos Szeredi .setattr = proc_setattr, 191327932742SMiklos Szeredi }; 191427932742SMiklos Szeredi 191527932742SMiklos Szeredi 1916444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1917c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1918444ceed8SEric W. Biederman { 1919c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1920444ceed8SEric W. Biederman struct inode *inode; 1921444ceed8SEric W. Biederman struct proc_inode *ei; 1922444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1923444ceed8SEric W. Biederman 192461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1925444ceed8SEric W. Biederman if (!inode) 1926444ceed8SEric W. Biederman goto out; 1927444ceed8SEric W. Biederman 1928444ceed8SEric W. Biederman ei = PROC_I(inode); 1929444ceed8SEric W. Biederman inode->i_mode = p->mode; 1930444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1931444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1932444ceed8SEric W. Biederman if (p->iop) 1933444ceed8SEric W. Biederman inode->i_op = p->iop; 1934444ceed8SEric W. Biederman if (p->fop) 1935444ceed8SEric W. Biederman inode->i_fop = p->fop; 1936444ceed8SEric W. Biederman ei->op = p->op; 1937444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1938444ceed8SEric W. Biederman d_add(dentry, inode); 1939444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1940444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1941444ceed8SEric W. Biederman error = NULL; 1942444ceed8SEric W. Biederman out: 1943444ceed8SEric W. Biederman return error; 1944444ceed8SEric W. Biederman } 1945444ceed8SEric W. Biederman 19461da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19471da177e4SLinus Torvalds struct dentry *dentry, 1948c5141e6dSEric Dumazet const struct pid_entry *ents, 19497bcd6b0eSEric W. Biederman unsigned int nents) 19501da177e4SLinus Torvalds { 19511da177e4SLinus Torvalds struct inode *inode; 1952cd6a3ce9SEric W. Biederman struct dentry *error; 195399f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1954c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19551da177e4SLinus Torvalds 1956cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19571da177e4SLinus Torvalds inode = NULL; 19581da177e4SLinus Torvalds 195999f89551SEric W. Biederman if (!task) 196099f89551SEric W. Biederman goto out_no_task; 19611da177e4SLinus Torvalds 196220cdc894SEric W. Biederman /* 196320cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 196420cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 196520cdc894SEric W. Biederman */ 19667bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19677bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 19681da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 19691da177e4SLinus Torvalds continue; 19701da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 19711da177e4SLinus Torvalds break; 19721da177e4SLinus Torvalds } 19737bcd6b0eSEric W. Biederman if (p > last) 19741da177e4SLinus Torvalds goto out; 19751da177e4SLinus Torvalds 1976444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 19771da177e4SLinus Torvalds out: 197899f89551SEric W. Biederman put_task_struct(task); 197999f89551SEric W. Biederman out_no_task: 1980cd6a3ce9SEric W. Biederman return error; 19811da177e4SLinus Torvalds } 19821da177e4SLinus Torvalds 1983c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1984c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 198561a28784SEric W. Biederman { 198661a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 198761a28784SEric W. Biederman proc_pident_instantiate, task, p); 198861a28784SEric W. Biederman } 198961a28784SEric W. Biederman 199028a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 199128a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1992c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 199328a6d671SEric W. Biederman { 199428a6d671SEric W. Biederman int i; 19952fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 199628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 199728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1998c5141e6dSEric Dumazet const struct pid_entry *p, *last; 199928a6d671SEric W. Biederman ino_t ino; 200028a6d671SEric W. Biederman int ret; 200128a6d671SEric W. Biederman 200228a6d671SEric W. Biederman ret = -ENOENT; 200328a6d671SEric W. Biederman if (!task) 200461a28784SEric W. Biederman goto out_no_task; 200528a6d671SEric W. Biederman 200628a6d671SEric W. Biederman ret = 0; 200728a6d671SEric W. Biederman i = filp->f_pos; 200828a6d671SEric W. Biederman switch (i) { 200928a6d671SEric W. Biederman case 0: 201028a6d671SEric W. Biederman ino = inode->i_ino; 201128a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 201228a6d671SEric W. Biederman goto out; 201328a6d671SEric W. Biederman i++; 201428a6d671SEric W. Biederman filp->f_pos++; 201528a6d671SEric W. Biederman /* fall through */ 201628a6d671SEric W. Biederman case 1: 201728a6d671SEric W. Biederman ino = parent_ino(dentry); 201828a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 201928a6d671SEric W. Biederman goto out; 202028a6d671SEric W. Biederman i++; 202128a6d671SEric W. Biederman filp->f_pos++; 202228a6d671SEric W. Biederman /* fall through */ 202328a6d671SEric W. Biederman default: 202428a6d671SEric W. Biederman i -= 2; 202528a6d671SEric W. Biederman if (i >= nents) { 202628a6d671SEric W. Biederman ret = 1; 202728a6d671SEric W. Biederman goto out; 202828a6d671SEric W. Biederman } 202928a6d671SEric W. Biederman p = ents + i; 20307bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20317bcd6b0eSEric W. Biederman while (p <= last) { 203261a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 203328a6d671SEric W. Biederman goto out; 203428a6d671SEric W. Biederman filp->f_pos++; 203528a6d671SEric W. Biederman p++; 203628a6d671SEric W. Biederman } 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 203928a6d671SEric W. Biederman ret = 1; 204028a6d671SEric W. Biederman out: 204161a28784SEric W. Biederman put_task_struct(task); 204261a28784SEric W. Biederman out_no_task: 204328a6d671SEric W. Biederman return ret; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 204728a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 204828a6d671SEric W. Biederman size_t count, loff_t *ppos) 204928a6d671SEric W. Biederman { 20502fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 205104ff9708SAl Viro char *p = NULL; 205228a6d671SEric W. Biederman ssize_t length; 205328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 205428a6d671SEric W. Biederman 205528a6d671SEric W. Biederman if (!task) 205604ff9708SAl Viro return -ESRCH; 205728a6d671SEric W. Biederman 205828a6d671SEric W. Biederman length = security_getprocattr(task, 20592fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 206004ff9708SAl Viro &p); 206128a6d671SEric W. Biederman put_task_struct(task); 206204ff9708SAl Viro if (length > 0) 206304ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 206404ff9708SAl Viro kfree(p); 206528a6d671SEric W. Biederman return length; 206628a6d671SEric W. Biederman } 206728a6d671SEric W. Biederman 206828a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 206928a6d671SEric W. Biederman size_t count, loff_t *ppos) 207028a6d671SEric W. Biederman { 20712fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 207228a6d671SEric W. Biederman char *page; 207328a6d671SEric W. Biederman ssize_t length; 207428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 207528a6d671SEric W. Biederman 207628a6d671SEric W. Biederman length = -ESRCH; 207728a6d671SEric W. Biederman if (!task) 207828a6d671SEric W. Biederman goto out_no_task; 207928a6d671SEric W. Biederman if (count > PAGE_SIZE) 208028a6d671SEric W. Biederman count = PAGE_SIZE; 208128a6d671SEric W. Biederman 208228a6d671SEric W. Biederman /* No partial writes. */ 208328a6d671SEric W. Biederman length = -EINVAL; 208428a6d671SEric W. Biederman if (*ppos != 0) 208528a6d671SEric W. Biederman goto out; 208628a6d671SEric W. Biederman 208728a6d671SEric W. Biederman length = -ENOMEM; 2088e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 208928a6d671SEric W. Biederman if (!page) 209028a6d671SEric W. Biederman goto out; 209128a6d671SEric W. Biederman 209228a6d671SEric W. Biederman length = -EFAULT; 209328a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 209428a6d671SEric W. Biederman goto out_free; 209528a6d671SEric W. Biederman 209628a6d671SEric W. Biederman length = security_setprocattr(task, 20972fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 209828a6d671SEric W. Biederman (void*)page, count); 209928a6d671SEric W. Biederman out_free: 210028a6d671SEric W. Biederman free_page((unsigned long) page); 210128a6d671SEric W. Biederman out: 210228a6d671SEric W. Biederman put_task_struct(task); 210328a6d671SEric W. Biederman out_no_task: 210428a6d671SEric W. Biederman return length; 210528a6d671SEric W. Biederman } 210628a6d671SEric W. Biederman 210700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 210828a6d671SEric W. Biederman .read = proc_pid_attr_read, 210928a6d671SEric W. Biederman .write = proc_pid_attr_write, 211028a6d671SEric W. Biederman }; 211128a6d671SEric W. Biederman 2112c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 211361a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 211461a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 211561a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 211661a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 211761a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 211861a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 211928a6d671SEric W. Biederman }; 212028a6d671SEric W. Biederman 212172d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21221da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21231da177e4SLinus Torvalds { 21241da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 212572d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 212800977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21291da177e4SLinus Torvalds .read = generic_read_dir, 213072d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21311da177e4SLinus Torvalds }; 21321da177e4SLinus Torvalds 213372d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21341da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21351da177e4SLinus Torvalds { 21367bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21377bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21381da177e4SLinus Torvalds } 21391da177e4SLinus Torvalds 2140c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 214172d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 214299f89551SEric W. Biederman .getattr = pid_getattr, 21436d76fa58SLinus Torvalds .setattr = proc_setattr, 21441da177e4SLinus Torvalds }; 21451da177e4SLinus Torvalds 21461da177e4SLinus Torvalds #endif 21471da177e4SLinus Torvalds 21483cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21493cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21503cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21513cb4a0bbSKawai, Hidehiro { 21523cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21533cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21543cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21553cb4a0bbSKawai, Hidehiro size_t len; 21563cb4a0bbSKawai, Hidehiro int ret; 21573cb4a0bbSKawai, Hidehiro 21583cb4a0bbSKawai, Hidehiro if (!task) 21593cb4a0bbSKawai, Hidehiro return -ESRCH; 21603cb4a0bbSKawai, Hidehiro 21613cb4a0bbSKawai, Hidehiro ret = 0; 21623cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21633cb4a0bbSKawai, Hidehiro if (mm) { 21643cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 21653cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 21663cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 21673cb4a0bbSKawai, Hidehiro mmput(mm); 21683cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 21693cb4a0bbSKawai, Hidehiro } 21703cb4a0bbSKawai, Hidehiro 21713cb4a0bbSKawai, Hidehiro put_task_struct(task); 21723cb4a0bbSKawai, Hidehiro 21733cb4a0bbSKawai, Hidehiro return ret; 21743cb4a0bbSKawai, Hidehiro } 21753cb4a0bbSKawai, Hidehiro 21763cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 21773cb4a0bbSKawai, Hidehiro const char __user *buf, 21783cb4a0bbSKawai, Hidehiro size_t count, 21793cb4a0bbSKawai, Hidehiro loff_t *ppos) 21803cb4a0bbSKawai, Hidehiro { 21813cb4a0bbSKawai, Hidehiro struct task_struct *task; 21823cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21833cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 21843cb4a0bbSKawai, Hidehiro unsigned int val; 21853cb4a0bbSKawai, Hidehiro int ret; 21863cb4a0bbSKawai, Hidehiro int i; 21873cb4a0bbSKawai, Hidehiro unsigned long mask; 21883cb4a0bbSKawai, Hidehiro 21893cb4a0bbSKawai, Hidehiro ret = -EFAULT; 21903cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 21913cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 21923cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 21933cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 21943cb4a0bbSKawai, Hidehiro goto out_no_task; 21953cb4a0bbSKawai, Hidehiro 21963cb4a0bbSKawai, Hidehiro ret = -EINVAL; 21973cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 21983cb4a0bbSKawai, Hidehiro if (*end == '\n') 21993cb4a0bbSKawai, Hidehiro end++; 22003cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 22013cb4a0bbSKawai, Hidehiro goto out_no_task; 22023cb4a0bbSKawai, Hidehiro 22033cb4a0bbSKawai, Hidehiro ret = -ESRCH; 22043cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 22053cb4a0bbSKawai, Hidehiro if (!task) 22063cb4a0bbSKawai, Hidehiro goto out_no_task; 22073cb4a0bbSKawai, Hidehiro 22083cb4a0bbSKawai, Hidehiro ret = end - buffer; 22093cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22103cb4a0bbSKawai, Hidehiro if (!mm) 22113cb4a0bbSKawai, Hidehiro goto out_no_mm; 22123cb4a0bbSKawai, Hidehiro 22133cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22143cb4a0bbSKawai, Hidehiro if (val & mask) 22153cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22163cb4a0bbSKawai, Hidehiro else 22173cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22183cb4a0bbSKawai, Hidehiro } 22193cb4a0bbSKawai, Hidehiro 22203cb4a0bbSKawai, Hidehiro mmput(mm); 22213cb4a0bbSKawai, Hidehiro out_no_mm: 22223cb4a0bbSKawai, Hidehiro put_task_struct(task); 22233cb4a0bbSKawai, Hidehiro out_no_task: 22243cb4a0bbSKawai, Hidehiro return ret; 22253cb4a0bbSKawai, Hidehiro } 22263cb4a0bbSKawai, Hidehiro 22273cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22283cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22293cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22303cb4a0bbSKawai, Hidehiro }; 22313cb4a0bbSKawai, Hidehiro #endif 22323cb4a0bbSKawai, Hidehiro 22331da177e4SLinus Torvalds /* 22341da177e4SLinus Torvalds * /proc/self: 22351da177e4SLinus Torvalds */ 22361da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22371da177e4SLinus Torvalds int buflen) 22381da177e4SLinus Torvalds { 2239488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2240b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22418578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2242b55fcb22SAndrew Morton if (!tgid) 2243488e5bc4SEric W. Biederman return -ENOENT; 2244b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22451da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 2248008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22491da177e4SLinus Torvalds { 2250488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2251b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22528578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2253b55fcb22SAndrew Morton if (!tgid) 2254488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2255b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2256008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22571da177e4SLinus Torvalds } 22581da177e4SLinus Torvalds 2259c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 22601da177e4SLinus Torvalds .readlink = proc_self_readlink, 22611da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 22621da177e4SLinus Torvalds }; 22631da177e4SLinus Torvalds 226428a6d671SEric W. Biederman /* 2265801199ceSEric W. Biederman * proc base 2266801199ceSEric W. Biederman * 2267801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2268801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2269801199ceSEric W. Biederman * describe something that is process related. 2270801199ceSEric W. Biederman */ 2271c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 227261a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2273801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2274801199ceSEric W. Biederman }; 2275801199ceSEric W. Biederman 2276801199ceSEric W. Biederman /* 2277801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2278801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2279801199ceSEric W. Biederman * due to the way we treat inodes. 2280801199ceSEric W. Biederman */ 2281801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2282801199ceSEric W. Biederman { 2283801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2284801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2285801199ceSEric W. Biederman if (task) { 2286801199ceSEric W. Biederman put_task_struct(task); 2287801199ceSEric W. Biederman return 1; 2288801199ceSEric W. Biederman } 2289801199ceSEric W. Biederman d_drop(dentry); 2290801199ceSEric W. Biederman return 0; 2291801199ceSEric W. Biederman } 2292801199ceSEric W. Biederman 2293801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2294801199ceSEric W. Biederman { 2295801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2296801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2297801199ceSEric W. Biederman }; 2298801199ceSEric W. Biederman 2299444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2300c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2301801199ceSEric W. Biederman { 2302c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2303801199ceSEric W. Biederman struct inode *inode; 2304801199ceSEric W. Biederman struct proc_inode *ei; 2305444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2306801199ceSEric W. Biederman 2307801199ceSEric W. Biederman /* Allocate the inode */ 2308801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2309801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2310801199ceSEric W. Biederman if (!inode) 2311801199ceSEric W. Biederman goto out; 2312801199ceSEric W. Biederman 2313801199ceSEric W. Biederman /* Initialize the inode */ 2314801199ceSEric W. Biederman ei = PROC_I(inode); 2315801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2316801199ceSEric W. Biederman 2317801199ceSEric W. Biederman /* 2318801199ceSEric W. Biederman * grab the reference to the task. 2319801199ceSEric W. Biederman */ 23201a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2321801199ceSEric W. Biederman if (!ei->pid) 2322801199ceSEric W. Biederman goto out_iput; 2323801199ceSEric W. Biederman 2324801199ceSEric W. Biederman inode->i_uid = 0; 2325801199ceSEric W. Biederman inode->i_gid = 0; 2326801199ceSEric W. Biederman inode->i_mode = p->mode; 2327801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2328801199ceSEric W. Biederman inode->i_nlink = 2; 2329801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2330801199ceSEric W. Biederman inode->i_size = 64; 2331801199ceSEric W. Biederman if (p->iop) 2332801199ceSEric W. Biederman inode->i_op = p->iop; 2333801199ceSEric W. Biederman if (p->fop) 2334801199ceSEric W. Biederman inode->i_fop = p->fop; 2335801199ceSEric W. Biederman ei->op = p->op; 2336801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2337801199ceSEric W. Biederman d_add(dentry, inode); 2338801199ceSEric W. Biederman error = NULL; 2339801199ceSEric W. Biederman out: 2340801199ceSEric W. Biederman return error; 2341801199ceSEric W. Biederman out_iput: 2342801199ceSEric W. Biederman iput(inode); 2343801199ceSEric W. Biederman goto out; 2344801199ceSEric W. Biederman } 2345801199ceSEric W. Biederman 2346444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2347444ceed8SEric W. Biederman { 2348444ceed8SEric W. Biederman struct dentry *error; 2349444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2350c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2351444ceed8SEric W. Biederman 2352444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2353444ceed8SEric W. Biederman 2354444ceed8SEric W. Biederman if (!task) 2355444ceed8SEric W. Biederman goto out_no_task; 2356444ceed8SEric W. Biederman 2357444ceed8SEric W. Biederman /* Lookup the directory entry */ 23587bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23597bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2360444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2361444ceed8SEric W. Biederman continue; 2362444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2363444ceed8SEric W. Biederman break; 2364444ceed8SEric W. Biederman } 23657bcd6b0eSEric W. Biederman if (p > last) 2366444ceed8SEric W. Biederman goto out; 2367444ceed8SEric W. Biederman 2368444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2369444ceed8SEric W. Biederman 2370444ceed8SEric W. Biederman out: 2371444ceed8SEric W. Biederman put_task_struct(task); 2372444ceed8SEric W. Biederman out_no_task: 2373444ceed8SEric W. Biederman return error; 2374444ceed8SEric W. Biederman } 2375444ceed8SEric W. Biederman 2376c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2377c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 237861a28784SEric W. Biederman { 237961a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 238061a28784SEric W. Biederman proc_base_instantiate, task, p); 238161a28784SEric W. Biederman } 238261a28784SEric W. Biederman 2383aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2384297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2385aba76fdbSAndrew Morton { 2386297c5d92SAndrea Righi u64 rchar, wchar, syscr, syscw; 2387297c5d92SAndrea Righi struct task_io_accounting ioac; 2388297c5d92SAndrea Righi 2389297c5d92SAndrea Righi if (!whole) { 2390297c5d92SAndrea Righi rchar = task->rchar; 2391297c5d92SAndrea Righi wchar = task->wchar; 2392297c5d92SAndrea Righi syscr = task->syscr; 2393297c5d92SAndrea Righi syscw = task->syscw; 2394297c5d92SAndrea Righi memcpy(&ioac, &task->ioac, sizeof(ioac)); 2395297c5d92SAndrea Righi } else { 2396297c5d92SAndrea Righi unsigned long flags; 2397297c5d92SAndrea Righi struct task_struct *t = task; 2398297c5d92SAndrea Righi rchar = wchar = syscr = syscw = 0; 2399297c5d92SAndrea Righi memset(&ioac, 0, sizeof(ioac)); 2400297c5d92SAndrea Righi 2401297c5d92SAndrea Righi rcu_read_lock(); 2402297c5d92SAndrea Righi do { 2403297c5d92SAndrea Righi rchar += t->rchar; 2404297c5d92SAndrea Righi wchar += t->wchar; 2405297c5d92SAndrea Righi syscr += t->syscr; 2406297c5d92SAndrea Righi syscw += t->syscw; 2407297c5d92SAndrea Righi 2408297c5d92SAndrea Righi ioac.read_bytes += t->ioac.read_bytes; 2409297c5d92SAndrea Righi ioac.write_bytes += t->ioac.write_bytes; 2410297c5d92SAndrea Righi ioac.cancelled_write_bytes += 2411297c5d92SAndrea Righi t->ioac.cancelled_write_bytes; 2412297c5d92SAndrea Righi t = next_thread(t); 2413297c5d92SAndrea Righi } while (t != task); 2414297c5d92SAndrea Righi rcu_read_unlock(); 2415297c5d92SAndrea Righi 2416297c5d92SAndrea Righi if (lock_task_sighand(task, &flags)) { 2417297c5d92SAndrea Righi struct signal_struct *sig = task->signal; 2418297c5d92SAndrea Righi 2419297c5d92SAndrea Righi rchar += sig->rchar; 2420297c5d92SAndrea Righi wchar += sig->wchar; 2421297c5d92SAndrea Righi syscr += sig->syscr; 2422297c5d92SAndrea Righi syscw += sig->syscw; 2423297c5d92SAndrea Righi 2424297c5d92SAndrea Righi ioac.read_bytes += sig->ioac.read_bytes; 2425297c5d92SAndrea Righi ioac.write_bytes += sig->ioac.write_bytes; 2426297c5d92SAndrea Righi ioac.cancelled_write_bytes += 2427297c5d92SAndrea Righi sig->ioac.cancelled_write_bytes; 2428297c5d92SAndrea Righi 2429297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2430297c5d92SAndrea Righi } 2431297c5d92SAndrea Righi } 2432297c5d92SAndrea Righi 2433aba76fdbSAndrew Morton return sprintf(buffer, 2434aba76fdbSAndrew Morton "rchar: %llu\n" 2435aba76fdbSAndrew Morton "wchar: %llu\n" 2436aba76fdbSAndrew Morton "syscr: %llu\n" 2437aba76fdbSAndrew Morton "syscw: %llu\n" 2438aba76fdbSAndrew Morton "read_bytes: %llu\n" 2439aba76fdbSAndrew Morton "write_bytes: %llu\n" 2440aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 2441297c5d92SAndrea Righi (unsigned long long)rchar, 2442297c5d92SAndrea Righi (unsigned long long)wchar, 2443297c5d92SAndrea Righi (unsigned long long)syscr, 2444297c5d92SAndrea Righi (unsigned long long)syscw, 2445297c5d92SAndrea Righi (unsigned long long)ioac.read_bytes, 2446297c5d92SAndrea Righi (unsigned long long)ioac.write_bytes, 2447297c5d92SAndrea Righi (unsigned long long)ioac.cancelled_write_bytes); 2448aba76fdbSAndrew Morton } 2449297c5d92SAndrea Righi 2450297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2451297c5d92SAndrea Righi { 2452297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2453297c5d92SAndrea Righi } 2454297c5d92SAndrea Righi 2455297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2456297c5d92SAndrea Righi { 2457297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2458297c5d92SAndrea Righi } 2459297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2460aba76fdbSAndrew Morton 2461801199ceSEric W. Biederman /* 246228a6d671SEric W. Biederman * Thread groups 246328a6d671SEric W. Biederman */ 246400977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2465c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 246620cdc894SEric W. Biederman 2467c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 246861a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 246961a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 247027932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2471b2211a36SAndrew Morton #ifdef CONFIG_NET 24724f42c288SAndre Noll DIR("net", S_IRUGO|S_IXUGO, net), 2473b2211a36SAndrew Morton #endif 2474315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 247561a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2476df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2477d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 247843ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 247943ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 248043ae34cbSIngo Molnar #endif 248161a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2482ee992744SEric W. Biederman ONE("stat", S_IRUGO, tgid_stat), 2483a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 248461a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 248528a6d671SEric W. Biederman #ifdef CONFIG_NUMA 248661a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 248728a6d671SEric W. Biederman #endif 248861a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 248961a28784SEric W. Biederman LNK("cwd", cwd), 249061a28784SEric W. Biederman LNK("root", root), 249161a28784SEric W. Biederman LNK("exe", exe), 249261a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 24932d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 249461a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 24951e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2496b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 249761a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 249885863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 249928a6d671SEric W. Biederman #endif 250028a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 250172d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 250228a6d671SEric W. Biederman #endif 250328a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 250461a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 250528a6d671SEric W. Biederman #endif 250628a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 250761a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 250828a6d671SEric W. Biederman #endif 25099745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 25109745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 25119745512cSArjan van de Ven #endif 25128793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 251361a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 251428a6d671SEric W. Biederman #endif 2515a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2516a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2517a424316cSPaul Menage #endif 251861a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 251961a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 252028a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 252161a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 25226ee65046SSteve Grubb REG("sessionid", S_IRUGO, sessionid), 252328a6d671SEric W. Biederman #endif 2524f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2525f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2526f4f154fdSAkinobu Mita #endif 25273cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 25283cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 25293cb4a0bbSKawai, Hidehiro #endif 2530aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2531297c5d92SAndrea Righi INF("io", S_IRUGO, tgid_io_accounting), 2532aba76fdbSAndrew Morton #endif 253328a6d671SEric W. Biederman }; 253428a6d671SEric W. Biederman 253528a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 253628a6d671SEric W. Biederman void * dirent, filldir_t filldir) 253728a6d671SEric W. Biederman { 253828a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 253928a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 254028a6d671SEric W. Biederman } 254128a6d671SEric W. Biederman 254200977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 254328a6d671SEric W. Biederman .read = generic_read_dir, 254428a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 254528a6d671SEric W. Biederman }; 254628a6d671SEric W. Biederman 254728a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25487bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25497bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 255028a6d671SEric W. Biederman } 255128a6d671SEric W. Biederman 2552c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 255328a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 255428a6d671SEric W. Biederman .getattr = pid_getattr, 255528a6d671SEric W. Biederman .setattr = proc_setattr, 255628a6d671SEric W. Biederman }; 255728a6d671SEric W. Biederman 255860347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25591da177e4SLinus Torvalds { 256048e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25618578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 256248e6484dSEric W. Biederman struct qstr name; 25631da177e4SLinus Torvalds 256448e6484dSEric W. Biederman name.name = buf; 256560347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 256660347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 256748e6484dSEric W. Biederman if (dentry) { 25687766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 256948e6484dSEric W. Biederman shrink_dcache_parent(dentry); 257048e6484dSEric W. Biederman d_drop(dentry); 257148e6484dSEric W. Biederman dput(dentry); 25721da177e4SLinus Torvalds } 25731da177e4SLinus Torvalds 257460347f67SPavel Emelyanov if (tgid == 0) 257548e6484dSEric W. Biederman goto out; 25761da177e4SLinus Torvalds 257748e6484dSEric W. Biederman name.name = buf; 257860347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 257960347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 258048e6484dSEric W. Biederman if (!leader) 258148e6484dSEric W. Biederman goto out; 258248e6484dSEric W. Biederman 258348e6484dSEric W. Biederman name.name = "task"; 258448e6484dSEric W. Biederman name.len = strlen(name.name); 258548e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 258648e6484dSEric W. Biederman if (!dir) 258748e6484dSEric W. Biederman goto out_put_leader; 258848e6484dSEric W. Biederman 258948e6484dSEric W. Biederman name.name = buf; 259060347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 259148e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 259248e6484dSEric W. Biederman if (dentry) { 259348e6484dSEric W. Biederman shrink_dcache_parent(dentry); 259448e6484dSEric W. Biederman d_drop(dentry); 259548e6484dSEric W. Biederman dput(dentry); 25961da177e4SLinus Torvalds } 259748e6484dSEric W. Biederman 259848e6484dSEric W. Biederman dput(dir); 259948e6484dSEric W. Biederman out_put_leader: 260048e6484dSEric W. Biederman dput(leader); 260148e6484dSEric W. Biederman out: 260248e6484dSEric W. Biederman return; 26031da177e4SLinus Torvalds } 26041da177e4SLinus Torvalds 26050895e91dSRandy Dunlap /** 26060895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 26070895e91dSRandy Dunlap * @task: task that should be flushed. 26080895e91dSRandy Dunlap * 26090895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 261060347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 26110895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 26120895e91dSRandy Dunlap * 26130895e91dSRandy Dunlap * Looks in the dcache for 26140895e91dSRandy Dunlap * /proc/@pid 26150895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26160895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26170895e91dSRandy Dunlap * from the dcache. 26180895e91dSRandy Dunlap * 26190895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26200895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26210895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26220895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26230895e91dSRandy Dunlap * dcache entries at process exit time. 26240895e91dSRandy Dunlap * 26250895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26260895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26270895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 262860347f67SPavel Emelyanov */ 262960347f67SPavel Emelyanov 263060347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 263160347f67SPavel Emelyanov { 26329fcc2d15SEric W. Biederman int i; 26339fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2634130f77ecSPavel Emelyanov struct upid *upid; 2635130f77ecSPavel Emelyanov 2636130f77ecSPavel Emelyanov pid = task_pid(task); 26379fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2638130f77ecSPavel Emelyanov tgid = task_tgid(task); 26399fcc2d15SEric W. Biederman 26409fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2641130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2642130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26439fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2644130f77ecSPavel Emelyanov } 26456f4e6433SPavel Emelyanov 26466f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 26476f4e6433SPavel Emelyanov if (upid->nr == 1) 26486f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 264960347f67SPavel Emelyanov } 265060347f67SPavel Emelyanov 26519711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 26529711ef99SAdrian Bunk struct dentry * dentry, 2653c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2654444ceed8SEric W. Biederman { 2655444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2656444ceed8SEric W. Biederman struct inode *inode; 2657444ceed8SEric W. Biederman 265861a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2659444ceed8SEric W. Biederman if (!inode) 2660444ceed8SEric W. Biederman goto out; 2661444ceed8SEric W. Biederman 2662444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2663444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2664444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2665444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2666aed54175SVegard Nossum 2667aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2668aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2669444ceed8SEric W. Biederman 2670444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2671444ceed8SEric W. Biederman 2672444ceed8SEric W. Biederman d_add(dentry, inode); 2673444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2674444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2675444ceed8SEric W. Biederman error = NULL; 2676444ceed8SEric W. Biederman out: 2677444ceed8SEric W. Biederman return error; 2678444ceed8SEric W. Biederman } 2679444ceed8SEric W. Biederman 26801da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 26811da177e4SLinus Torvalds { 2682cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 26831da177e4SLinus Torvalds struct task_struct *task; 26841da177e4SLinus Torvalds unsigned tgid; 2685b488893aSPavel Emelyanov struct pid_namespace *ns; 26861da177e4SLinus Torvalds 2687801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2688801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2689801199ceSEric W. Biederman goto out; 2690801199ceSEric W. Biederman 26911da177e4SLinus Torvalds tgid = name_to_int(dentry); 26921da177e4SLinus Torvalds if (tgid == ~0U) 26931da177e4SLinus Torvalds goto out; 26941da177e4SLinus Torvalds 2695b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2696de758734SEric W. Biederman rcu_read_lock(); 2697b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 26981da177e4SLinus Torvalds if (task) 26991da177e4SLinus Torvalds get_task_struct(task); 2700de758734SEric W. Biederman rcu_read_unlock(); 27011da177e4SLinus Torvalds if (!task) 27021da177e4SLinus Torvalds goto out; 27031da177e4SLinus Torvalds 2704444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 270548e6484dSEric W. Biederman put_task_struct(task); 27061da177e4SLinus Torvalds out: 2707cd6a3ce9SEric W. Biederman return result; 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds /* 27110804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 27120bc58a91SEric W. Biederman * 27131da177e4SLinus Torvalds */ 271419fd4bb2SEric W. Biederman struct tgid_iter { 271519fd4bb2SEric W. Biederman unsigned int tgid; 27160804ef4bSEric W. Biederman struct task_struct *task; 271719fd4bb2SEric W. Biederman }; 271819fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 271919fd4bb2SEric W. Biederman { 27200804ef4bSEric W. Biederman struct pid *pid; 27211da177e4SLinus Torvalds 272219fd4bb2SEric W. Biederman if (iter.task) 272319fd4bb2SEric W. Biederman put_task_struct(iter.task); 27240804ef4bSEric W. Biederman rcu_read_lock(); 27250804ef4bSEric W. Biederman retry: 272619fd4bb2SEric W. Biederman iter.task = NULL; 272719fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27280804ef4bSEric W. Biederman if (pid) { 272919fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 273019fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27310804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27320804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27330804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27340804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27350804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27360804ef4bSEric W. Biederman * 27370804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27380804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27390804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27400804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27410804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27420bc58a91SEric W. Biederman */ 274319fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 274419fd4bb2SEric W. Biederman iter.tgid += 1; 27450804ef4bSEric W. Biederman goto retry; 274619fd4bb2SEric W. Biederman } 274719fd4bb2SEric W. Biederman get_task_struct(iter.task); 27481da177e4SLinus Torvalds } 2749454cc105SEric W. Biederman rcu_read_unlock(); 275019fd4bb2SEric W. Biederman return iter; 27511da177e4SLinus Torvalds } 27521da177e4SLinus Torvalds 27537bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 27541da177e4SLinus Torvalds 275561a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 275619fd4bb2SEric W. Biederman struct tgid_iter iter) 275761a28784SEric W. Biederman { 275861a28784SEric W. Biederman char name[PROC_NUMBUF]; 275919fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 276061a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 276119fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 276261a28784SEric W. Biederman } 276361a28784SEric W. Biederman 27641da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27651da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27661da177e4SLinus Torvalds { 27671da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 27682fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 276919fd4bb2SEric W. Biederman struct tgid_iter iter; 2770b488893aSPavel Emelyanov struct pid_namespace *ns; 27711da177e4SLinus Torvalds 277261a28784SEric W. Biederman if (!reaper) 277361a28784SEric W. Biederman goto out_no_task; 277461a28784SEric W. Biederman 27757bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2776c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 277761a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2778801199ceSEric W. Biederman goto out; 27791da177e4SLinus Torvalds } 27801da177e4SLinus Torvalds 2781b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 278219fd4bb2SEric W. Biederman iter.task = NULL; 278319fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 278419fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 278519fd4bb2SEric W. Biederman iter.task; 278619fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 278719fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 278819fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 278919fd4bb2SEric W. Biederman put_task_struct(iter.task); 27900804ef4bSEric W. Biederman goto out; 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds } 27930804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 27940804ef4bSEric W. Biederman out: 279561a28784SEric W. Biederman put_task_struct(reaper); 279661a28784SEric W. Biederman out_no_task: 27971da177e4SLinus Torvalds return 0; 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds 28000bc58a91SEric W. Biederman /* 280128a6d671SEric W. Biederman * Tasks 280228a6d671SEric W. Biederman */ 2803c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 280461a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 280527932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2806315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 280761a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2808df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2809d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 281043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 281143ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 281243ae34cbSIngo Molnar #endif 281361a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2814ee992744SEric W. Biederman ONE("stat", S_IRUGO, tid_stat), 2815a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 281661a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 281728a6d671SEric W. Biederman #ifdef CONFIG_NUMA 281861a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 281928a6d671SEric W. Biederman #endif 282061a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 282161a28784SEric W. Biederman LNK("cwd", cwd), 282261a28784SEric W. Biederman LNK("root", root), 282361a28784SEric W. Biederman LNK("exe", exe), 282461a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 28252d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 28261e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2827b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 282861a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 282985863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 283028a6d671SEric W. Biederman #endif 283128a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 283272d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 283328a6d671SEric W. Biederman #endif 283428a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 283561a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 283628a6d671SEric W. Biederman #endif 283728a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 283861a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 283928a6d671SEric W. Biederman #endif 28409745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 28419745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 28429745512cSArjan van de Ven #endif 28438793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 284461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 284528a6d671SEric W. Biederman #endif 2846a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2847a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2848a424316cSPaul Menage #endif 284961a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 285061a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 285128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 285261a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 28531e0bd755SEric Paris REG("sessionid", S_IRUSR, sessionid), 285428a6d671SEric W. Biederman #endif 2855f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2856f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2857f4f154fdSAkinobu Mita #endif 2858297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 2859297c5d92SAndrea Righi INF("io", S_IRUGO, tid_io_accounting), 2860297c5d92SAndrea Righi #endif 286128a6d671SEric W. Biederman }; 286228a6d671SEric W. Biederman 286328a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 286428a6d671SEric W. Biederman void * dirent, filldir_t filldir) 286528a6d671SEric W. Biederman { 286628a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 286728a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 286828a6d671SEric W. Biederman } 286928a6d671SEric W. Biederman 287028a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 28717bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 28727bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 287328a6d671SEric W. Biederman } 287428a6d671SEric W. Biederman 287500977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 287628a6d671SEric W. Biederman .read = generic_read_dir, 287728a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 287828a6d671SEric W. Biederman }; 287928a6d671SEric W. Biederman 2880c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 288128a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 288228a6d671SEric W. Biederman .getattr = pid_getattr, 288328a6d671SEric W. Biederman .setattr = proc_setattr, 288428a6d671SEric W. Biederman }; 288528a6d671SEric W. Biederman 2886444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2887c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2888444ceed8SEric W. Biederman { 2889444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2890444ceed8SEric W. Biederman struct inode *inode; 289161a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2892444ceed8SEric W. Biederman 2893444ceed8SEric W. Biederman if (!inode) 2894444ceed8SEric W. Biederman goto out; 2895444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2896444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2897444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2898444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2899aed54175SVegard Nossum 2900aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2901aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2902444ceed8SEric W. Biederman 2903444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2904444ceed8SEric W. Biederman 2905444ceed8SEric W. Biederman d_add(dentry, inode); 2906444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2907444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2908444ceed8SEric W. Biederman error = NULL; 2909444ceed8SEric W. Biederman out: 2910444ceed8SEric W. Biederman return error; 2911444ceed8SEric W. Biederman } 2912444ceed8SEric W. Biederman 291328a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 291428a6d671SEric W. Biederman { 291528a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 291628a6d671SEric W. Biederman struct task_struct *task; 291728a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 291828a6d671SEric W. Biederman unsigned tid; 2919b488893aSPavel Emelyanov struct pid_namespace *ns; 292028a6d671SEric W. Biederman 292128a6d671SEric W. Biederman if (!leader) 292228a6d671SEric W. Biederman goto out_no_task; 292328a6d671SEric W. Biederman 292428a6d671SEric W. Biederman tid = name_to_int(dentry); 292528a6d671SEric W. Biederman if (tid == ~0U) 292628a6d671SEric W. Biederman goto out; 292728a6d671SEric W. Biederman 2928b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 292928a6d671SEric W. Biederman rcu_read_lock(); 2930b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 293128a6d671SEric W. Biederman if (task) 293228a6d671SEric W. Biederman get_task_struct(task); 293328a6d671SEric W. Biederman rcu_read_unlock(); 293428a6d671SEric W. Biederman if (!task) 293528a6d671SEric W. Biederman goto out; 2936bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 293728a6d671SEric W. Biederman goto out_drop_task; 293828a6d671SEric W. Biederman 2939444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 294028a6d671SEric W. Biederman out_drop_task: 294128a6d671SEric W. Biederman put_task_struct(task); 294228a6d671SEric W. Biederman out: 294328a6d671SEric W. Biederman put_task_struct(leader); 294428a6d671SEric W. Biederman out_no_task: 294528a6d671SEric W. Biederman return result; 294628a6d671SEric W. Biederman } 294728a6d671SEric W. Biederman 294828a6d671SEric W. Biederman /* 29490bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 29500bc58a91SEric W. Biederman * 29510bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 29520bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 29530bc58a91SEric W. Biederman * directory we have more work todo. 29540bc58a91SEric W. Biederman * 29550bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 29560bc58a91SEric W. Biederman * 29570bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 29580bc58a91SEric W. Biederman * threads past it. 29590bc58a91SEric W. Biederman */ 2960cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2961b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 29620bc58a91SEric W. Biederman { 2963a872ff0cSOleg Nesterov struct task_struct *pos; 29640bc58a91SEric W. Biederman 2965cc288738SEric W. Biederman rcu_read_lock(); 29660bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 29670bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2968b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2969a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2970a872ff0cSOleg Nesterov goto found; 29710bc58a91SEric W. Biederman } 29720bc58a91SEric W. Biederman 29730bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 29740bc58a91SEric W. Biederman pos = NULL; 2975a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2976a872ff0cSOleg Nesterov goto out; 2977a872ff0cSOleg Nesterov 2978a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2979a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2980a872ff0cSOleg Nesterov */ 2981a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2982a872ff0cSOleg Nesterov pos = next_thread(pos); 2983a872ff0cSOleg Nesterov if (pos == leader) { 2984a872ff0cSOleg Nesterov pos = NULL; 2985a872ff0cSOleg Nesterov goto out; 2986a872ff0cSOleg Nesterov } 2987a872ff0cSOleg Nesterov } 2988a872ff0cSOleg Nesterov found: 2989a872ff0cSOleg Nesterov get_task_struct(pos); 2990a872ff0cSOleg Nesterov out: 2991cc288738SEric W. Biederman rcu_read_unlock(); 29920bc58a91SEric W. Biederman return pos; 29930bc58a91SEric W. Biederman } 29940bc58a91SEric W. Biederman 29950bc58a91SEric W. Biederman /* 29960bc58a91SEric W. Biederman * Find the next thread in the thread list. 29970bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 29980bc58a91SEric W. Biederman * 29990bc58a91SEric W. Biederman * The reference to the input task_struct is released. 30000bc58a91SEric W. Biederman */ 30010bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 30020bc58a91SEric W. Biederman { 3003c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 3004cc288738SEric W. Biederman rcu_read_lock(); 3005c1df7fb8SOleg Nesterov if (pid_alive(start)) { 30060bc58a91SEric W. Biederman pos = next_thread(start); 3007c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 30080bc58a91SEric W. Biederman pos = NULL; 3009c1df7fb8SOleg Nesterov else 3010c1df7fb8SOleg Nesterov get_task_struct(pos); 3011c1df7fb8SOleg Nesterov } 3012cc288738SEric W. Biederman rcu_read_unlock(); 30130bc58a91SEric W. Biederman put_task_struct(start); 30140bc58a91SEric W. Biederman return pos; 30150bc58a91SEric W. Biederman } 30160bc58a91SEric W. Biederman 301761a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 301861a28784SEric W. Biederman struct task_struct *task, int tid) 301961a28784SEric W. Biederman { 302061a28784SEric W. Biederman char name[PROC_NUMBUF]; 302161a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 302261a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 302361a28784SEric W. Biederman proc_task_instantiate, task, NULL); 302461a28784SEric W. Biederman } 302561a28784SEric W. Biederman 30261da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 30271da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 30281da177e4SLinus Torvalds { 30292fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 30301da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 30317d895244SGuillaume Chazarain struct task_struct *leader = NULL; 30320bc58a91SEric W. Biederman struct task_struct *task; 30331da177e4SLinus Torvalds int retval = -ENOENT; 30341da177e4SLinus Torvalds ino_t ino; 30350bc58a91SEric W. Biederman int tid; 30361da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 3037b488893aSPavel Emelyanov struct pid_namespace *ns; 30381da177e4SLinus Torvalds 30397d895244SGuillaume Chazarain task = get_proc_task(inode); 30407d895244SGuillaume Chazarain if (!task) 30417d895244SGuillaume Chazarain goto out_no_task; 30427d895244SGuillaume Chazarain rcu_read_lock(); 30437d895244SGuillaume Chazarain if (pid_alive(task)) { 30447d895244SGuillaume Chazarain leader = task->group_leader; 30457d895244SGuillaume Chazarain get_task_struct(leader); 30467d895244SGuillaume Chazarain } 30477d895244SGuillaume Chazarain rcu_read_unlock(); 30487d895244SGuillaume Chazarain put_task_struct(task); 304999f89551SEric W. Biederman if (!leader) 305099f89551SEric W. Biederman goto out_no_task; 30511da177e4SLinus Torvalds retval = 0; 30521da177e4SLinus Torvalds 30531da177e4SLinus Torvalds switch (pos) { 30541da177e4SLinus Torvalds case 0: 30551da177e4SLinus Torvalds ino = inode->i_ino; 30561da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 30571da177e4SLinus Torvalds goto out; 30581da177e4SLinus Torvalds pos++; 30591da177e4SLinus Torvalds /* fall through */ 30601da177e4SLinus Torvalds case 1: 30611da177e4SLinus Torvalds ino = parent_ino(dentry); 30621da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 30631da177e4SLinus Torvalds goto out; 30641da177e4SLinus Torvalds pos++; 30651da177e4SLinus Torvalds /* fall through */ 30661da177e4SLinus Torvalds } 30671da177e4SLinus Torvalds 30680bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 30690bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 30700bc58a91SEric W. Biederman */ 3071b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 30722b47c361SMathieu Desnoyers tid = (int)filp->f_version; 30730bc58a91SEric W. Biederman filp->f_version = 0; 3074b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 30750bc58a91SEric W. Biederman task; 30760bc58a91SEric W. Biederman task = next_tid(task), pos++) { 3077b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 307861a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 30790bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 30800bc58a91SEric W. Biederman * pid for the next readir call */ 30812b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 30820bc58a91SEric W. Biederman put_task_struct(task); 30831da177e4SLinus Torvalds break; 30840bc58a91SEric W. Biederman } 30851da177e4SLinus Torvalds } 30861da177e4SLinus Torvalds out: 30871da177e4SLinus Torvalds filp->f_pos = pos; 308899f89551SEric W. Biederman put_task_struct(leader); 308999f89551SEric W. Biederman out_no_task: 30901da177e4SLinus Torvalds return retval; 30911da177e4SLinus Torvalds } 30926e66b52bSEric W. Biederman 30936e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 30946e66b52bSEric W. Biederman { 30956e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 309699f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 30976e66b52bSEric W. Biederman generic_fillattr(inode, stat); 30986e66b52bSEric W. Biederman 309999f89551SEric W. Biederman if (p) { 310099f89551SEric W. Biederman rcu_read_lock(); 310199f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 310299f89551SEric W. Biederman rcu_read_unlock(); 310399f89551SEric W. Biederman put_task_struct(p); 31046e66b52bSEric W. Biederman } 31056e66b52bSEric W. Biederman 31066e66b52bSEric W. Biederman return 0; 31076e66b52bSEric W. Biederman } 310828a6d671SEric W. Biederman 3109c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 311028a6d671SEric W. Biederman .lookup = proc_task_lookup, 311128a6d671SEric W. Biederman .getattr = proc_task_getattr, 311228a6d671SEric W. Biederman .setattr = proc_setattr, 311328a6d671SEric W. Biederman }; 311428a6d671SEric W. Biederman 311500977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 311628a6d671SEric W. Biederman .read = generic_read_dir, 311728a6d671SEric W. Biederman .readdir = proc_task_readdir, 311828a6d671SEric W. Biederman }; 3119