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> 72a424316cSPaul Menage #include <linux/cgroup.h> 731da177e4SLinus Torvalds #include <linux/cpuset.h> 741da177e4SLinus Torvalds #include <linux/audit.h> 755addc5ddSAl Viro #include <linux/poll.h> 761651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 778ac773b4SAlexey Dobriyan #include <linux/oom.h> 783cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 7960347f67SPavel Emelyanov #include <linux/pid_namespace.h> 801da177e4SLinus Torvalds #include "internal.h" 811da177e4SLinus Torvalds 820f2fe20fSEric W. Biederman /* NOTE: 830f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 840f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 850f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 860f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 870f2fe20fSEric W. Biederman * 880f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 890f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 900f2fe20fSEric W. Biederman */ 910f2fe20fSEric W. Biederman 921da177e4SLinus Torvalds struct pid_entry { 931da177e4SLinus Torvalds char *name; 94c5141e6dSEric Dumazet int len; 951da177e4SLinus Torvalds mode_t mode; 96c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9700977a59SArjan van de Ven const struct file_operations *fop; 9820cdc894SEric W. Biederman union proc_op op; 991da177e4SLinus Torvalds }; 1001da177e4SLinus Torvalds 10161a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10220cdc894SEric W. Biederman .name = (NAME), \ 103c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10420cdc894SEric W. Biederman .mode = MODE, \ 10520cdc894SEric W. Biederman .iop = IOP, \ 10620cdc894SEric W. Biederman .fop = FOP, \ 10720cdc894SEric W. Biederman .op = OP, \ 10820cdc894SEric W. Biederman } 10920cdc894SEric W. Biederman 11061a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11161a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11220cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11320cdc894SEric W. Biederman {} ) 11461a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11561a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11620cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11720cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11861a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 11961a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12020cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12161a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12320cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12420cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 125be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE) \ 126be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 127be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 128be614086SEric W. Biederman { .proc_show = &proc_##OTYPE } ) 1291da177e4SLinus Torvalds 130aed54175SVegard Nossum /* 131aed54175SVegard Nossum * Count the number of hardlinks for the pid_entry table, excluding the . 132aed54175SVegard Nossum * and .. links. 133aed54175SVegard Nossum */ 134aed54175SVegard Nossum static unsigned int pid_entry_count_dirs(const struct pid_entry *entries, 135aed54175SVegard Nossum unsigned int n) 136aed54175SVegard Nossum { 137aed54175SVegard Nossum unsigned int i; 138aed54175SVegard Nossum unsigned int count; 139aed54175SVegard Nossum 140aed54175SVegard Nossum count = 0; 141aed54175SVegard Nossum for (i = 0; i < n; ++i) { 142aed54175SVegard Nossum if (S_ISDIR(entries[i].mode)) 143aed54175SVegard Nossum ++count; 144aed54175SVegard Nossum } 145aed54175SVegard Nossum 146aed54175SVegard Nossum return count; 147aed54175SVegard Nossum } 148aed54175SVegard Nossum 1495096add8SKees Cook int maps_protect; 1505096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1515096add8SKees Cook 1520494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds struct fs_struct *fs; 1550494f6ecSMiklos Szeredi task_lock(task); 1560494f6ecSMiklos Szeredi fs = task->fs; 1571da177e4SLinus Torvalds if(fs) 1581da177e4SLinus Torvalds atomic_inc(&fs->count); 1590494f6ecSMiklos Szeredi task_unlock(task); 1600494f6ecSMiklos Szeredi return fs; 1610494f6ecSMiklos Szeredi } 1620494f6ecSMiklos Szeredi 16399f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 16499f89551SEric W. Biederman { 16599f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 16699f89551SEric W. Biederman unsigned long flags; 16799f89551SEric W. Biederman int count = 0; 16899f89551SEric W. Biederman 16999f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 17099f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 17199f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 17299f89551SEric W. Biederman } 17399f89551SEric W. Biederman return count; 17499f89551SEric W. Biederman } 17599f89551SEric W. Biederman 1763dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1770494f6ecSMiklos Szeredi { 17899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17999f89551SEric W. Biederman struct fs_struct *fs = NULL; 1800494f6ecSMiklos Szeredi int result = -ENOENT; 18199f89551SEric W. Biederman 18299f89551SEric W. Biederman if (task) { 18399f89551SEric W. Biederman fs = get_fs_struct(task); 18499f89551SEric W. Biederman put_task_struct(task); 18599f89551SEric W. Biederman } 1861da177e4SLinus Torvalds if (fs) { 1871da177e4SLinus Torvalds read_lock(&fs->lock); 1883dcd25f3SJan Blunck *path = fs->pwd; 1893dcd25f3SJan Blunck path_get(&fs->pwd); 1901da177e4SLinus Torvalds read_unlock(&fs->lock); 1911da177e4SLinus Torvalds result = 0; 1921da177e4SLinus Torvalds put_fs_struct(fs); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds return result; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1973dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1981da177e4SLinus Torvalds { 19999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 20099f89551SEric W. Biederman struct fs_struct *fs = NULL; 2011da177e4SLinus Torvalds int result = -ENOENT; 20299f89551SEric W. Biederman 20399f89551SEric W. Biederman if (task) { 20499f89551SEric W. Biederman fs = get_fs_struct(task); 20599f89551SEric W. Biederman put_task_struct(task); 20699f89551SEric W. Biederman } 2071da177e4SLinus Torvalds if (fs) { 2081da177e4SLinus Torvalds read_lock(&fs->lock); 2093dcd25f3SJan Blunck *path = fs->root; 2103dcd25f3SJan Blunck path_get(&fs->root); 2111da177e4SLinus Torvalds read_unlock(&fs->lock); 2121da177e4SLinus Torvalds result = 0; 2131da177e4SLinus Torvalds put_fs_struct(fs); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds return result; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 218638fa202SRoland McGrath /* 219638fa202SRoland McGrath * Return zero if current may access user memory in @task, -error if not. 220638fa202SRoland McGrath */ 221638fa202SRoland McGrath static int check_mem_permission(struct task_struct *task) 222638fa202SRoland McGrath { 223638fa202SRoland McGrath /* 224638fa202SRoland McGrath * A task can always look at itself, in case it chooses 225638fa202SRoland McGrath * to use system calls instead of load instructions. 226638fa202SRoland McGrath */ 227638fa202SRoland McGrath if (task == current) 228638fa202SRoland McGrath return 0; 229638fa202SRoland McGrath 230638fa202SRoland McGrath /* 231638fa202SRoland McGrath * If current is actively ptrace'ing, and would also be 232638fa202SRoland McGrath * permitted to freshly attach with ptrace now, permit it. 233638fa202SRoland McGrath */ 234638fa202SRoland McGrath if (task->parent == current && (task->ptrace & PT_PTRACED) && 235638fa202SRoland McGrath task_is_stopped_or_traced(task) && 236006ebb40SStephen Smalley ptrace_may_access(task, PTRACE_MODE_ATTACH)) 237638fa202SRoland McGrath return 0; 238638fa202SRoland McGrath 239638fa202SRoland McGrath /* 240638fa202SRoland McGrath * Noone else is allowed. 241638fa202SRoland McGrath */ 242638fa202SRoland McGrath return -EPERM; 243638fa202SRoland McGrath } 2441da177e4SLinus Torvalds 245831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 246831830b5SAl Viro { 247831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 248831830b5SAl Viro if (!mm) 249831830b5SAl Viro return NULL; 250831830b5SAl Viro down_read(&mm->mmap_sem); 251831830b5SAl Viro task_lock(task); 252831830b5SAl Viro if (task->mm != mm) 253831830b5SAl Viro goto out; 254006ebb40SStephen Smalley if (task->mm != current->mm && 255006ebb40SStephen Smalley __ptrace_may_access(task, PTRACE_MODE_READ) < 0) 256831830b5SAl Viro goto out; 257831830b5SAl Viro task_unlock(task); 258831830b5SAl Viro return mm; 259831830b5SAl Viro out: 260831830b5SAl Viro task_unlock(task); 261831830b5SAl Viro up_read(&mm->mmap_sem); 262831830b5SAl Viro mmput(mm); 263831830b5SAl Viro return NULL; 264831830b5SAl Viro } 265831830b5SAl Viro 2661da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds int res = 0; 2691da177e4SLinus Torvalds unsigned int len; 2701da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2711da177e4SLinus Torvalds if (!mm) 2721da177e4SLinus Torvalds goto out; 2731da177e4SLinus Torvalds if (!mm->arg_end) 2741da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds if (len > PAGE_SIZE) 2791da177e4SLinus Torvalds len = PAGE_SIZE; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2841da177e4SLinus Torvalds // assume application is using setproctitle(3). 2851da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2861da177e4SLinus Torvalds len = strnlen(buffer, res); 2871da177e4SLinus Torvalds if (len < res) { 2881da177e4SLinus Torvalds res = len; 2891da177e4SLinus Torvalds } else { 2901da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2911da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2921da177e4SLinus Torvalds len = PAGE_SIZE - res; 2931da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2941da177e4SLinus Torvalds res = strnlen(buffer, res); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds out_mm: 2981da177e4SLinus Torvalds mmput(mm); 2991da177e4SLinus Torvalds out: 3001da177e4SLinus Torvalds return res; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds int res = 0; 3061da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 3071da177e4SLinus Torvalds if (mm) { 3081da177e4SLinus Torvalds unsigned int nwords = 0; 3091da177e4SLinus Torvalds do 3101da177e4SLinus Torvalds nwords += 2; 3111da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 3121da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 3131da177e4SLinus Torvalds if (res > PAGE_SIZE) 3141da177e4SLinus Torvalds res = PAGE_SIZE; 3151da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 3161da177e4SLinus Torvalds mmput(mm); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds return res; 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 3231da177e4SLinus Torvalds /* 3241da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 3251da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 3261da177e4SLinus Torvalds */ 3271da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 3281da177e4SLinus Torvalds { 329ffb45122SAlexey Dobriyan unsigned long wchan; 3309281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds wchan = get_wchan(task); 3331da177e4SLinus Torvalds 3349d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 3351da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 3369d65cb4aSAlexey Dobriyan else 3379d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3421da177e4SLinus Torvalds /* 3431da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3441da177e4SLinus Torvalds */ 3451da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3461da177e4SLinus Torvalds { 347172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3481da177e4SLinus Torvalds task->sched_info.cpu_time, 3491da177e4SLinus Torvalds task->sched_info.run_delay, 3502d72376bSIngo Molnar task->sched_info.pcount); 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds #endif 3531da177e4SLinus Torvalds 3549745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3559745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3569745512cSArjan van de Ven { 3579745512cSArjan van de Ven int i; 35813d77c37SHiroshi Shimamoto struct inode *inode = m->private; 35913d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(inode); 3609745512cSArjan van de Ven 36113d77c37SHiroshi Shimamoto if (!task) 36213d77c37SHiroshi Shimamoto return -ESRCH; 36313d77c37SHiroshi Shimamoto seq_puts(m, "Latency Top version : v0.1\n"); 3649745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3659745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3669745512cSArjan van de Ven int q; 3679745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3689745512cSArjan van de Ven task->latency_record[i].count, 3699745512cSArjan van de Ven task->latency_record[i].time, 3709745512cSArjan van de Ven task->latency_record[i].max); 3719745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3729745512cSArjan van de Ven char sym[KSYM_NAME_LEN]; 3739745512cSArjan van de Ven char *c; 3749745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3759745512cSArjan van de Ven break; 3769745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3779745512cSArjan van de Ven break; 3789745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3799745512cSArjan van de Ven c = strchr(sym, '+'); 3809745512cSArjan van de Ven if (c) 3819745512cSArjan van de Ven *c = 0; 3829745512cSArjan van de Ven seq_printf(m, "%s ", sym); 3839745512cSArjan van de Ven } 3849745512cSArjan van de Ven seq_printf(m, "\n"); 3859745512cSArjan van de Ven } 3869745512cSArjan van de Ven 3879745512cSArjan van de Ven } 38813d77c37SHiroshi Shimamoto put_task_struct(task); 3899745512cSArjan van de Ven return 0; 3909745512cSArjan van de Ven } 3919745512cSArjan van de Ven 3929745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3939745512cSArjan van de Ven { 39413d77c37SHiroshi Shimamoto return single_open(file, lstats_show_proc, inode); 395d6643d12SHiroshi Shimamoto } 396d6643d12SHiroshi Shimamoto 3979745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3989745512cSArjan van de Ven size_t count, loff_t *offs) 3999745512cSArjan van de Ven { 40013d77c37SHiroshi Shimamoto struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 4019745512cSArjan van de Ven 40213d77c37SHiroshi Shimamoto if (!task) 40313d77c37SHiroshi Shimamoto return -ESRCH; 4049745512cSArjan van de Ven clear_all_latency_tracing(task); 40513d77c37SHiroshi Shimamoto put_task_struct(task); 4069745512cSArjan van de Ven 4079745512cSArjan van de Ven return count; 4089745512cSArjan van de Ven } 4099745512cSArjan van de Ven 4109745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 4119745512cSArjan van de Ven .open = lstats_open, 4129745512cSArjan van de Ven .read = seq_read, 4139745512cSArjan van de Ven .write = lstats_write, 4149745512cSArjan van de Ven .llseek = seq_lseek, 41513d77c37SHiroshi Shimamoto .release = single_release, 4169745512cSArjan van de Ven }; 4179745512cSArjan van de Ven 4189745512cSArjan van de Ven #endif 4199745512cSArjan van de Ven 4201da177e4SLinus Torvalds /* The badness from the OOM killer */ 4211da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 4221da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 4231da177e4SLinus Torvalds { 4241da177e4SLinus Torvalds unsigned long points; 4251da177e4SLinus Torvalds struct timespec uptime; 4261da177e4SLinus Torvalds 4271da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 42819c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4291da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 43019c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4311da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 434d85f50d5SNeil Horman struct limit_names { 435d85f50d5SNeil Horman char *name; 436d85f50d5SNeil Horman char *unit; 437d85f50d5SNeil Horman }; 438d85f50d5SNeil Horman 439d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 440d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 441d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 442d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 443d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 444d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 445d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 446d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 447d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 448d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 449d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 450d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 451d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 452d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 453d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 454d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4558808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 456d85f50d5SNeil Horman }; 457d85f50d5SNeil Horman 458d85f50d5SNeil Horman /* Display limits for a process */ 459d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 460d85f50d5SNeil Horman { 461d85f50d5SNeil Horman unsigned int i; 462d85f50d5SNeil Horman int count = 0; 463d85f50d5SNeil Horman unsigned long flags; 464d85f50d5SNeil Horman char *bufptr = buffer; 465d85f50d5SNeil Horman 466d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 467d85f50d5SNeil Horman 468d85f50d5SNeil Horman rcu_read_lock(); 469d85f50d5SNeil Horman if (!lock_task_sighand(task,&flags)) { 470d85f50d5SNeil Horman rcu_read_unlock(); 471d85f50d5SNeil Horman return 0; 472d85f50d5SNeil Horman } 473d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 474d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 475d85f50d5SNeil Horman rcu_read_unlock(); 476d85f50d5SNeil Horman 477d85f50d5SNeil Horman /* 478d85f50d5SNeil Horman * print the file header 479d85f50d5SNeil Horman */ 480d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 481d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 482d85f50d5SNeil Horman 483d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 484d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 485d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 486d85f50d5SNeil Horman lnames[i].name, "unlimited"); 487d85f50d5SNeil Horman else 488d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 489d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 490d85f50d5SNeil Horman 491d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 492d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 493d85f50d5SNeil Horman else 494d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 495d85f50d5SNeil Horman rlim[i].rlim_max); 496d85f50d5SNeil Horman 497d85f50d5SNeil Horman if (lnames[i].unit) 498d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 499d85f50d5SNeil Horman lnames[i].unit); 500d85f50d5SNeil Horman else 501d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 502d85f50d5SNeil Horman } 503d85f50d5SNeil Horman 504d85f50d5SNeil Horman return count; 505d85f50d5SNeil Horman } 506d85f50d5SNeil Horman 5071da177e4SLinus Torvalds /************************************************************************/ 5081da177e4SLinus Torvalds /* Here the fs part begins */ 5091da177e4SLinus Torvalds /************************************************************************/ 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds /* permission checks */ 512778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 5131da177e4SLinus Torvalds { 514778c1144SEric W. Biederman struct task_struct *task; 515778c1144SEric W. Biederman int allowed = 0; 516df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 517df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 518df26c40eSEric W. Biederman * information. 519778c1144SEric W. Biederman */ 520778c1144SEric W. Biederman task = get_proc_task(inode); 521df26c40eSEric W. Biederman if (task) { 522006ebb40SStephen Smalley allowed = ptrace_may_access(task, PTRACE_MODE_READ); 523778c1144SEric W. Biederman put_task_struct(task); 524df26c40eSEric W. Biederman } 525778c1144SEric W. Biederman return allowed; 5261da177e4SLinus Torvalds } 5271da177e4SLinus Torvalds 5286d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5296d76fa58SLinus Torvalds { 5306d76fa58SLinus Torvalds int error; 5316d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5326d76fa58SLinus Torvalds 5336d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5346d76fa58SLinus Torvalds return -EPERM; 5356d76fa58SLinus Torvalds 5366d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5376d76fa58SLinus Torvalds if (!error) 5386d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5396d76fa58SLinus Torvalds return error; 5406d76fa58SLinus Torvalds } 5416d76fa58SLinus Torvalds 542c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5436d76fa58SLinus Torvalds .setattr = proc_setattr, 5446d76fa58SLinus Torvalds }; 5456d76fa58SLinus Torvalds 546a1a2c409SMiklos Szeredi static int mounts_open_common(struct inode *inode, struct file *file, 547a1a2c409SMiklos Szeredi const struct seq_operations *op) 5481da177e4SLinus Torvalds { 54999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 550cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5516b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 552a1a2c409SMiklos Szeredi struct fs_struct *fs = NULL; 553a1a2c409SMiklos Szeredi struct path root; 5545addc5ddSAl Viro struct proc_mounts *p; 5555addc5ddSAl Viro int ret = -EINVAL; 5565addc5ddSAl Viro 55799f89551SEric W. Biederman if (task) { 558cf7b708cSPavel Emelyanov rcu_read_lock(); 559cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 560cf7b708cSPavel Emelyanov if (nsp) { 561cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5626b3286edSKirill Korotaev if (ns) 5636b3286edSKirill Korotaev get_mnt_ns(ns); 564863c4702SAlexey Dobriyan } 565cf7b708cSPavel Emelyanov rcu_read_unlock(); 566a1a2c409SMiklos Szeredi if (ns) 567a1a2c409SMiklos Szeredi fs = get_fs_struct(task); 56899f89551SEric W. Biederman put_task_struct(task); 56999f89551SEric W. Biederman } 5701da177e4SLinus Torvalds 571a1a2c409SMiklos Szeredi if (!ns) 572a1a2c409SMiklos Szeredi goto err; 573a1a2c409SMiklos Szeredi if (!fs) 574a1a2c409SMiklos Szeredi goto err_put_ns; 575a1a2c409SMiklos Szeredi 576a1a2c409SMiklos Szeredi read_lock(&fs->lock); 577a1a2c409SMiklos Szeredi root = fs->root; 578a1a2c409SMiklos Szeredi path_get(&root); 579a1a2c409SMiklos Szeredi read_unlock(&fs->lock); 580a1a2c409SMiklos Szeredi put_fs_struct(fs); 581a1a2c409SMiklos Szeredi 5825addc5ddSAl Viro ret = -ENOMEM; 5835addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 584a1a2c409SMiklos Szeredi if (!p) 585a1a2c409SMiklos Szeredi goto err_put_path; 586a1a2c409SMiklos Szeredi 5875addc5ddSAl Viro file->private_data = &p->m; 588a1a2c409SMiklos Szeredi ret = seq_open(file, op); 589a1a2c409SMiklos Szeredi if (ret) 590a1a2c409SMiklos Szeredi goto err_free; 591a1a2c409SMiklos Szeredi 592a1a2c409SMiklos Szeredi p->m.private = p; 593a1a2c409SMiklos Szeredi p->ns = ns; 594a1a2c409SMiklos Szeredi p->root = root; 5956b3286edSKirill Korotaev p->event = ns->event; 596a1a2c409SMiklos Szeredi 5975addc5ddSAl Viro return 0; 598a1a2c409SMiklos Szeredi 599a1a2c409SMiklos Szeredi err_free: 6005addc5ddSAl Viro kfree(p); 601a1a2c409SMiklos Szeredi err_put_path: 602a1a2c409SMiklos Szeredi path_put(&root); 603a1a2c409SMiklos Szeredi err_put_ns: 6046b3286edSKirill Korotaev put_mnt_ns(ns); 605a1a2c409SMiklos Szeredi err: 6061da177e4SLinus Torvalds return ret; 6071da177e4SLinus Torvalds } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 6101da177e4SLinus Torvalds { 611a1a2c409SMiklos Szeredi struct proc_mounts *p = file->private_data; 612a1a2c409SMiklos Szeredi path_put(&p->root); 613a1a2c409SMiklos Szeredi put_mnt_ns(p->ns); 6141da177e4SLinus Torvalds return seq_release(inode, file); 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6175addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 6185addc5ddSAl Viro { 6195addc5ddSAl Viro struct proc_mounts *p = file->private_data; 620a1a2c409SMiklos Szeredi struct mnt_namespace *ns = p->ns; 6215addc5ddSAl Viro unsigned res = 0; 6225addc5ddSAl Viro 6235addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 6245addc5ddSAl Viro 6255addc5ddSAl Viro spin_lock(&vfsmount_lock); 6265addc5ddSAl Viro if (p->event != ns->event) { 6275addc5ddSAl Viro p->event = ns->event; 6285addc5ddSAl Viro res = POLLERR; 6295addc5ddSAl Viro } 6305addc5ddSAl Viro spin_unlock(&vfsmount_lock); 6315addc5ddSAl Viro 6325addc5ddSAl Viro return res; 6335addc5ddSAl Viro } 6345addc5ddSAl Viro 635a1a2c409SMiklos Szeredi static int mounts_open(struct inode *inode, struct file *file) 636a1a2c409SMiklos Szeredi { 637a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mounts_op); 638a1a2c409SMiklos Szeredi } 639a1a2c409SMiklos Szeredi 64000977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 6411da177e4SLinus Torvalds .open = mounts_open, 6421da177e4SLinus Torvalds .read = seq_read, 6431da177e4SLinus Torvalds .llseek = seq_lseek, 6441da177e4SLinus Torvalds .release = mounts_release, 6455addc5ddSAl Viro .poll = mounts_poll, 6461da177e4SLinus Torvalds }; 6471da177e4SLinus Torvalds 6482d4d4864SRam Pai static int mountinfo_open(struct inode *inode, struct file *file) 6492d4d4864SRam Pai { 6502d4d4864SRam Pai return mounts_open_common(inode, file, &mountinfo_op); 6512d4d4864SRam Pai } 6522d4d4864SRam Pai 6532d4d4864SRam Pai static const struct file_operations proc_mountinfo_operations = { 6542d4d4864SRam Pai .open = mountinfo_open, 6552d4d4864SRam Pai .read = seq_read, 6562d4d4864SRam Pai .llseek = seq_lseek, 6572d4d4864SRam Pai .release = mounts_release, 6582d4d4864SRam Pai .poll = mounts_poll, 6592d4d4864SRam Pai }; 6602d4d4864SRam Pai 661b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 662b4629fe2SChuck Lever { 663a1a2c409SMiklos Szeredi return mounts_open_common(inode, file, &mountstats_op); 664b4629fe2SChuck Lever } 665b4629fe2SChuck Lever 66600977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 667b4629fe2SChuck Lever .open = mountstats_open, 668b4629fe2SChuck Lever .read = seq_read, 669b4629fe2SChuck Lever .llseek = seq_lseek, 670b4629fe2SChuck Lever .release = mounts_release, 671b4629fe2SChuck Lever }; 672b4629fe2SChuck Lever 6731da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6761da177e4SLinus Torvalds size_t count, loff_t *ppos) 6771da177e4SLinus Torvalds { 6782fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6791da177e4SLinus Torvalds unsigned long page; 6801da177e4SLinus Torvalds ssize_t length; 68199f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 68299f89551SEric W. Biederman 68399f89551SEric W. Biederman length = -ESRCH; 68499f89551SEric W. Biederman if (!task) 68599f89551SEric W. Biederman goto out_no_task; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6881da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 68999f89551SEric W. Biederman 69099f89551SEric W. Biederman length = -ENOMEM; 691e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 69299f89551SEric W. Biederman goto out; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds if (length >= 0) 6971da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6981da177e4SLinus Torvalds free_page(page); 69999f89551SEric W. Biederman out: 70099f89551SEric W. Biederman put_task_struct(task); 70199f89551SEric W. Biederman out_no_task: 7021da177e4SLinus Torvalds return length; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 70500977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 7061da177e4SLinus Torvalds .read = proc_info_read, 7071da177e4SLinus Torvalds }; 7081da177e4SLinus Torvalds 709be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 710be614086SEric W. Biederman { 711be614086SEric W. Biederman struct inode *inode = m->private; 712be614086SEric W. Biederman struct pid_namespace *ns; 713be614086SEric W. Biederman struct pid *pid; 714be614086SEric W. Biederman struct task_struct *task; 715be614086SEric W. Biederman int ret; 716be614086SEric W. Biederman 717be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 718be614086SEric W. Biederman pid = proc_pid(inode); 719be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 720be614086SEric W. Biederman if (!task) 721be614086SEric W. Biederman return -ESRCH; 722be614086SEric W. Biederman 723be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 724be614086SEric W. Biederman 725be614086SEric W. Biederman put_task_struct(task); 726be614086SEric W. Biederman return ret; 727be614086SEric W. Biederman } 728be614086SEric W. Biederman 729be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 730be614086SEric W. Biederman { 731be614086SEric W. Biederman int ret; 732be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 733be614086SEric W. Biederman if (!ret) { 734be614086SEric W. Biederman struct seq_file *m = filp->private_data; 735be614086SEric W. Biederman 736be614086SEric W. Biederman m->private = inode; 737be614086SEric W. Biederman } 738be614086SEric W. Biederman return ret; 739be614086SEric W. Biederman } 740be614086SEric W. Biederman 741be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 742be614086SEric W. Biederman .open = proc_single_open, 743be614086SEric W. Biederman .read = seq_read, 744be614086SEric W. Biederman .llseek = seq_lseek, 745be614086SEric W. Biederman .release = single_release, 746be614086SEric W. Biederman }; 747be614086SEric W. Biederman 7481da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7491da177e4SLinus Torvalds { 7501da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7511da177e4SLinus Torvalds return 0; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7551da177e4SLinus Torvalds size_t count, loff_t *ppos) 7561da177e4SLinus Torvalds { 7572fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7581da177e4SLinus Torvalds char *page; 7591da177e4SLinus Torvalds unsigned long src = *ppos; 7601da177e4SLinus Torvalds int ret = -ESRCH; 7611da177e4SLinus Torvalds struct mm_struct *mm; 7621da177e4SLinus Torvalds 76399f89551SEric W. Biederman if (!task) 76499f89551SEric W. Biederman goto out_no_task; 76599f89551SEric W. Biederman 766638fa202SRoland McGrath if (check_mem_permission(task)) 7671da177e4SLinus Torvalds goto out; 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds ret = -ENOMEM; 770e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7711da177e4SLinus Torvalds if (!page) 7721da177e4SLinus Torvalds goto out; 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds ret = 0; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds mm = get_task_mm(task); 7771da177e4SLinus Torvalds if (!mm) 7781da177e4SLinus Torvalds goto out_free; 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds ret = -EIO; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 7831da177e4SLinus Torvalds goto out_put; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds ret = 0; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds while (count > 0) { 7881da177e4SLinus Torvalds int this_len, retval; 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 7911da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 792638fa202SRoland McGrath if (!retval || check_mem_permission(task)) { 7931da177e4SLinus Torvalds if (!ret) 7941da177e4SLinus Torvalds ret = -EIO; 7951da177e4SLinus Torvalds break; 7961da177e4SLinus Torvalds } 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 7991da177e4SLinus Torvalds ret = -EFAULT; 8001da177e4SLinus Torvalds break; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds ret += retval; 8041da177e4SLinus Torvalds src += retval; 8051da177e4SLinus Torvalds buf += retval; 8061da177e4SLinus Torvalds count -= retval; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds *ppos = src; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds out_put: 8111da177e4SLinus Torvalds mmput(mm); 8121da177e4SLinus Torvalds out_free: 8131da177e4SLinus Torvalds free_page((unsigned long) page); 8141da177e4SLinus Torvalds out: 81599f89551SEric W. Biederman put_task_struct(task); 81699f89551SEric W. Biederman out_no_task: 8171da177e4SLinus Torvalds return ret; 8181da177e4SLinus Torvalds } 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds #define mem_write NULL 8211da177e4SLinus Torvalds 8221da177e4SLinus Torvalds #ifndef mem_write 8231da177e4SLinus Torvalds /* This is a security hazard */ 82463967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 8251da177e4SLinus Torvalds size_t count, loff_t *ppos) 8261da177e4SLinus Torvalds { 827f7ca54f4SFrederik Deweerdt int copied; 8281da177e4SLinus Torvalds char *page; 8292fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 8301da177e4SLinus Torvalds unsigned long dst = *ppos; 8311da177e4SLinus Torvalds 83299f89551SEric W. Biederman copied = -ESRCH; 83399f89551SEric W. Biederman if (!task) 83499f89551SEric W. Biederman goto out_no_task; 8351da177e4SLinus Torvalds 836638fa202SRoland McGrath if (check_mem_permission(task)) 83799f89551SEric W. Biederman goto out; 83899f89551SEric W. Biederman 83999f89551SEric W. Biederman copied = -ENOMEM; 840e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8411da177e4SLinus Torvalds if (!page) 84299f89551SEric W. Biederman goto out; 8431da177e4SLinus Torvalds 844f7ca54f4SFrederik Deweerdt copied = 0; 8451da177e4SLinus Torvalds while (count > 0) { 8461da177e4SLinus Torvalds int this_len, retval; 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8491da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8501da177e4SLinus Torvalds copied = -EFAULT; 8511da177e4SLinus Torvalds break; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8541da177e4SLinus Torvalds if (!retval) { 8551da177e4SLinus Torvalds if (!copied) 8561da177e4SLinus Torvalds copied = -EIO; 8571da177e4SLinus Torvalds break; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds copied += retval; 8601da177e4SLinus Torvalds buf += retval; 8611da177e4SLinus Torvalds dst += retval; 8621da177e4SLinus Torvalds count -= retval; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds *ppos = dst; 8651da177e4SLinus Torvalds free_page((unsigned long) page); 86699f89551SEric W. Biederman out: 86799f89551SEric W. Biederman put_task_struct(task); 86899f89551SEric W. Biederman out_no_task: 8691da177e4SLinus Torvalds return copied; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds #endif 8721da177e4SLinus Torvalds 87385863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 8741da177e4SLinus Torvalds { 8751da177e4SLinus Torvalds switch (orig) { 8761da177e4SLinus Torvalds case 0: 8771da177e4SLinus Torvalds file->f_pos = offset; 8781da177e4SLinus Torvalds break; 8791da177e4SLinus Torvalds case 1: 8801da177e4SLinus Torvalds file->f_pos += offset; 8811da177e4SLinus Torvalds break; 8821da177e4SLinus Torvalds default: 8831da177e4SLinus Torvalds return -EINVAL; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds force_successful_syscall_return(); 8861da177e4SLinus Torvalds return file->f_pos; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 88900977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 8901da177e4SLinus Torvalds .llseek = mem_lseek, 8911da177e4SLinus Torvalds .read = mem_read, 8921da177e4SLinus Torvalds .write = mem_write, 8931da177e4SLinus Torvalds .open = mem_open, 8941da177e4SLinus Torvalds }; 8951da177e4SLinus Torvalds 896315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 897315e28c8SJames Pearson size_t count, loff_t *ppos) 898315e28c8SJames Pearson { 899315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 900315e28c8SJames Pearson char *page; 901315e28c8SJames Pearson unsigned long src = *ppos; 902315e28c8SJames Pearson int ret = -ESRCH; 903315e28c8SJames Pearson struct mm_struct *mm; 904315e28c8SJames Pearson 905315e28c8SJames Pearson if (!task) 906315e28c8SJames Pearson goto out_no_task; 907315e28c8SJames Pearson 908006ebb40SStephen Smalley if (!ptrace_may_access(task, PTRACE_MODE_READ)) 909315e28c8SJames Pearson goto out; 910315e28c8SJames Pearson 911315e28c8SJames Pearson ret = -ENOMEM; 912315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 913315e28c8SJames Pearson if (!page) 914315e28c8SJames Pearson goto out; 915315e28c8SJames Pearson 916315e28c8SJames Pearson ret = 0; 917315e28c8SJames Pearson 918315e28c8SJames Pearson mm = get_task_mm(task); 919315e28c8SJames Pearson if (!mm) 920315e28c8SJames Pearson goto out_free; 921315e28c8SJames Pearson 922315e28c8SJames Pearson while (count > 0) { 923315e28c8SJames Pearson int this_len, retval, max_len; 924315e28c8SJames Pearson 925315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 926315e28c8SJames Pearson 927315e28c8SJames Pearson if (this_len <= 0) 928315e28c8SJames Pearson break; 929315e28c8SJames Pearson 930315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 931315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 932315e28c8SJames Pearson 933315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 934315e28c8SJames Pearson page, this_len, 0); 935315e28c8SJames Pearson 936315e28c8SJames Pearson if (retval <= 0) { 937315e28c8SJames Pearson ret = retval; 938315e28c8SJames Pearson break; 939315e28c8SJames Pearson } 940315e28c8SJames Pearson 941315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 942315e28c8SJames Pearson ret = -EFAULT; 943315e28c8SJames Pearson break; 944315e28c8SJames Pearson } 945315e28c8SJames Pearson 946315e28c8SJames Pearson ret += retval; 947315e28c8SJames Pearson src += retval; 948315e28c8SJames Pearson buf += retval; 949315e28c8SJames Pearson count -= retval; 950315e28c8SJames Pearson } 951315e28c8SJames Pearson *ppos = src; 952315e28c8SJames Pearson 953315e28c8SJames Pearson mmput(mm); 954315e28c8SJames Pearson out_free: 955315e28c8SJames Pearson free_page((unsigned long) page); 956315e28c8SJames Pearson out: 957315e28c8SJames Pearson put_task_struct(task); 958315e28c8SJames Pearson out_no_task: 959315e28c8SJames Pearson return ret; 960315e28c8SJames Pearson } 961315e28c8SJames Pearson 962315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 963315e28c8SJames Pearson .read = environ_read, 964315e28c8SJames Pearson }; 965315e28c8SJames Pearson 9661da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9671da177e4SLinus Torvalds size_t count, loff_t *ppos) 9681da177e4SLinus Torvalds { 9692fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9708578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9711da177e4SLinus Torvalds size_t len; 97299f89551SEric W. Biederman int oom_adjust; 9731da177e4SLinus Torvalds 97499f89551SEric W. Biederman if (!task) 97599f89551SEric W. Biederman return -ESRCH; 97699f89551SEric W. Biederman oom_adjust = task->oomkilladj; 97799f89551SEric W. Biederman put_task_struct(task); 97899f89551SEric W. Biederman 9798578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 9800c28f287SAkinobu Mita 9810c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 9851da177e4SLinus Torvalds size_t count, loff_t *ppos) 9861da177e4SLinus Torvalds { 98799f89551SEric W. Biederman struct task_struct *task; 9888578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 9891da177e4SLinus Torvalds int oom_adjust; 9901da177e4SLinus Torvalds 9918578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9928578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9938578cea7SEric W. Biederman count = sizeof(buffer) - 1; 9941da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 9951da177e4SLinus Torvalds return -EFAULT; 9961da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 9978ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 9988ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 9991da177e4SLinus Torvalds return -EINVAL; 10001da177e4SLinus Torvalds if (*end == '\n') 10011da177e4SLinus Torvalds end++; 10022fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 100399f89551SEric W. Biederman if (!task) 100499f89551SEric W. Biederman return -ESRCH; 10058fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 10068fb4fc68SGuillem Jover put_task_struct(task); 10078fb4fc68SGuillem Jover return -EACCES; 10088fb4fc68SGuillem Jover } 10091da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 101099f89551SEric W. Biederman put_task_struct(task); 10111da177e4SLinus Torvalds if (end - buffer == 0) 10121da177e4SLinus Torvalds return -EIO; 10131da177e4SLinus Torvalds return end - buffer; 10141da177e4SLinus Torvalds } 10151da177e4SLinus Torvalds 101600977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 10171da177e4SLinus Torvalds .read = oom_adjust_read, 10181da177e4SLinus Torvalds .write = oom_adjust_write, 10191da177e4SLinus Torvalds }; 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 10221da177e4SLinus Torvalds #define TMPBUFLEN 21 10231da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 10241da177e4SLinus Torvalds size_t count, loff_t *ppos) 10251da177e4SLinus Torvalds { 10262fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 102799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 10281da177e4SLinus Torvalds ssize_t length; 10291da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 10301da177e4SLinus Torvalds 103199f89551SEric W. Biederman if (!task) 103299f89551SEric W. Biederman return -ESRCH; 10331da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10340c11b942SAl Viro audit_get_loginuid(task)); 103599f89551SEric W. Biederman put_task_struct(task); 10361da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10401da177e4SLinus Torvalds size_t count, loff_t *ppos) 10411da177e4SLinus Torvalds { 10422fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10431da177e4SLinus Torvalds char *page, *tmp; 10441da177e4SLinus Torvalds ssize_t length; 10451da177e4SLinus Torvalds uid_t loginuid; 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10481da177e4SLinus Torvalds return -EPERM; 10491da177e4SLinus Torvalds 105013b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10511da177e4SLinus Torvalds return -EPERM; 10521da177e4SLinus Torvalds 1053e0182909SAl Viro if (count >= PAGE_SIZE) 1054e0182909SAl Viro count = PAGE_SIZE - 1; 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds if (*ppos != 0) { 10571da177e4SLinus Torvalds /* No partial writes. */ 10581da177e4SLinus Torvalds return -EINVAL; 10591da177e4SLinus Torvalds } 1060e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10611da177e4SLinus Torvalds if (!page) 10621da177e4SLinus Torvalds return -ENOMEM; 10631da177e4SLinus Torvalds length = -EFAULT; 10641da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10651da177e4SLinus Torvalds goto out_free_page; 10661da177e4SLinus Torvalds 1067e0182909SAl Viro page[count] = '\0'; 10681da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10691da177e4SLinus Torvalds if (tmp == page) { 10701da177e4SLinus Torvalds length = -EINVAL; 10711da177e4SLinus Torvalds goto out_free_page; 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds } 107499f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 10751da177e4SLinus Torvalds if (likely(length == 0)) 10761da177e4SLinus Torvalds length = count; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds out_free_page: 10791da177e4SLinus Torvalds free_page((unsigned long) page); 10801da177e4SLinus Torvalds return length; 10811da177e4SLinus Torvalds } 10821da177e4SLinus Torvalds 108300977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 10841da177e4SLinus Torvalds .read = proc_loginuid_read, 10851da177e4SLinus Torvalds .write = proc_loginuid_write, 10861da177e4SLinus Torvalds }; 10871e0bd755SEric Paris 10881e0bd755SEric Paris static ssize_t proc_sessionid_read(struct file * file, char __user * buf, 10891e0bd755SEric Paris size_t count, loff_t *ppos) 10901e0bd755SEric Paris { 10911e0bd755SEric Paris struct inode * inode = file->f_path.dentry->d_inode; 10921e0bd755SEric Paris struct task_struct *task = get_proc_task(inode); 10931e0bd755SEric Paris ssize_t length; 10941e0bd755SEric Paris char tmpbuf[TMPBUFLEN]; 10951e0bd755SEric Paris 10961e0bd755SEric Paris if (!task) 10971e0bd755SEric Paris return -ESRCH; 10981e0bd755SEric Paris length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10991e0bd755SEric Paris audit_get_sessionid(task)); 11001e0bd755SEric Paris put_task_struct(task); 11011e0bd755SEric Paris return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 11021e0bd755SEric Paris } 11031e0bd755SEric Paris 11041e0bd755SEric Paris static const struct file_operations proc_sessionid_operations = { 11051e0bd755SEric Paris .read = proc_sessionid_read, 11061e0bd755SEric Paris }; 11071da177e4SLinus Torvalds #endif 11081da177e4SLinus Torvalds 1109f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1110f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1111f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1112f4f154fdSAkinobu Mita { 1113f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1114f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1115f4f154fdSAkinobu Mita size_t len; 1116f4f154fdSAkinobu Mita int make_it_fail; 1117f4f154fdSAkinobu Mita 1118f4f154fdSAkinobu Mita if (!task) 1119f4f154fdSAkinobu Mita return -ESRCH; 1120f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1121f4f154fdSAkinobu Mita put_task_struct(task); 1122f4f154fdSAkinobu Mita 1123f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 11240c28f287SAkinobu Mita 11250c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1126f4f154fdSAkinobu Mita } 1127f4f154fdSAkinobu Mita 1128f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1129f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1130f4f154fdSAkinobu Mita { 1131f4f154fdSAkinobu Mita struct task_struct *task; 1132f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1133f4f154fdSAkinobu Mita int make_it_fail; 1134f4f154fdSAkinobu Mita 1135f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1136f4f154fdSAkinobu Mita return -EPERM; 1137f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1138f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1139f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1140f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1141f4f154fdSAkinobu Mita return -EFAULT; 1142f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1143f4f154fdSAkinobu Mita if (*end == '\n') 1144f4f154fdSAkinobu Mita end++; 1145f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1146f4f154fdSAkinobu Mita if (!task) 1147f4f154fdSAkinobu Mita return -ESRCH; 1148f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1149f4f154fdSAkinobu Mita put_task_struct(task); 1150f4f154fdSAkinobu Mita if (end - buffer == 0) 1151f4f154fdSAkinobu Mita return -EIO; 1152f4f154fdSAkinobu Mita return end - buffer; 1153f4f154fdSAkinobu Mita } 1154f4f154fdSAkinobu Mita 115500977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1156f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1157f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1158f4f154fdSAkinobu Mita }; 1159f4f154fdSAkinobu Mita #endif 1160f4f154fdSAkinobu Mita 11619745512cSArjan van de Ven 116243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 116343ae34cbSIngo Molnar /* 116443ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 116543ae34cbSIngo Molnar */ 116643ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 116743ae34cbSIngo Molnar { 116843ae34cbSIngo Molnar struct inode *inode = m->private; 116943ae34cbSIngo Molnar struct task_struct *p; 117043ae34cbSIngo Molnar 117143ae34cbSIngo Molnar WARN_ON(!inode); 117243ae34cbSIngo Molnar 117343ae34cbSIngo Molnar p = get_proc_task(inode); 117443ae34cbSIngo Molnar if (!p) 117543ae34cbSIngo Molnar return -ESRCH; 117643ae34cbSIngo Molnar proc_sched_show_task(p, m); 117743ae34cbSIngo Molnar 117843ae34cbSIngo Molnar put_task_struct(p); 117943ae34cbSIngo Molnar 118043ae34cbSIngo Molnar return 0; 118143ae34cbSIngo Molnar } 118243ae34cbSIngo Molnar 118343ae34cbSIngo Molnar static ssize_t 118443ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 118543ae34cbSIngo Molnar size_t count, loff_t *offset) 118643ae34cbSIngo Molnar { 118743ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 118843ae34cbSIngo Molnar struct task_struct *p; 118943ae34cbSIngo Molnar 119043ae34cbSIngo Molnar WARN_ON(!inode); 119143ae34cbSIngo Molnar 119243ae34cbSIngo Molnar p = get_proc_task(inode); 119343ae34cbSIngo Molnar if (!p) 119443ae34cbSIngo Molnar return -ESRCH; 119543ae34cbSIngo Molnar proc_sched_set_task(p); 119643ae34cbSIngo Molnar 119743ae34cbSIngo Molnar put_task_struct(p); 119843ae34cbSIngo Molnar 119943ae34cbSIngo Molnar return count; 120043ae34cbSIngo Molnar } 120143ae34cbSIngo Molnar 120243ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 120343ae34cbSIngo Molnar { 120443ae34cbSIngo Molnar int ret; 120543ae34cbSIngo Molnar 120643ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 120743ae34cbSIngo Molnar if (!ret) { 120843ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 120943ae34cbSIngo Molnar 121043ae34cbSIngo Molnar m->private = inode; 121143ae34cbSIngo Molnar } 121243ae34cbSIngo Molnar return ret; 121343ae34cbSIngo Molnar } 121443ae34cbSIngo Molnar 121543ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 121643ae34cbSIngo Molnar .open = sched_open, 121743ae34cbSIngo Molnar .read = seq_read, 121843ae34cbSIngo Molnar .write = sched_write, 121943ae34cbSIngo Molnar .llseek = seq_lseek, 12205ea473a1SAlexey Dobriyan .release = single_release, 122143ae34cbSIngo Molnar }; 122243ae34cbSIngo Molnar 122343ae34cbSIngo Molnar #endif 122443ae34cbSIngo Molnar 1225925d1c40SMatt Helsley /* 1226925d1c40SMatt Helsley * We added or removed a vma mapping the executable. The vmas are only mapped 1227925d1c40SMatt Helsley * during exec and are not mapped with the mmap system call. 1228925d1c40SMatt Helsley * Callers must hold down_write() on the mm's mmap_sem for these 1229925d1c40SMatt Helsley */ 1230925d1c40SMatt Helsley void added_exe_file_vma(struct mm_struct *mm) 1231925d1c40SMatt Helsley { 1232925d1c40SMatt Helsley mm->num_exe_file_vmas++; 1233925d1c40SMatt Helsley } 1234925d1c40SMatt Helsley 1235925d1c40SMatt Helsley void removed_exe_file_vma(struct mm_struct *mm) 1236925d1c40SMatt Helsley { 1237925d1c40SMatt Helsley mm->num_exe_file_vmas--; 1238925d1c40SMatt Helsley if ((mm->num_exe_file_vmas == 0) && mm->exe_file){ 1239925d1c40SMatt Helsley fput(mm->exe_file); 1240925d1c40SMatt Helsley mm->exe_file = NULL; 1241925d1c40SMatt Helsley } 1242925d1c40SMatt Helsley 1243925d1c40SMatt Helsley } 1244925d1c40SMatt Helsley 1245925d1c40SMatt Helsley void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1246925d1c40SMatt Helsley { 1247925d1c40SMatt Helsley if (new_exe_file) 1248925d1c40SMatt Helsley get_file(new_exe_file); 1249925d1c40SMatt Helsley if (mm->exe_file) 1250925d1c40SMatt Helsley fput(mm->exe_file); 1251925d1c40SMatt Helsley mm->exe_file = new_exe_file; 1252925d1c40SMatt Helsley mm->num_exe_file_vmas = 0; 1253925d1c40SMatt Helsley } 1254925d1c40SMatt Helsley 1255925d1c40SMatt Helsley struct file *get_mm_exe_file(struct mm_struct *mm) 1256925d1c40SMatt Helsley { 1257925d1c40SMatt Helsley struct file *exe_file; 1258925d1c40SMatt Helsley 1259925d1c40SMatt Helsley /* We need mmap_sem to protect against races with removal of 1260925d1c40SMatt Helsley * VM_EXECUTABLE vmas */ 1261925d1c40SMatt Helsley down_read(&mm->mmap_sem); 1262925d1c40SMatt Helsley exe_file = mm->exe_file; 1263925d1c40SMatt Helsley if (exe_file) 1264925d1c40SMatt Helsley get_file(exe_file); 1265925d1c40SMatt Helsley up_read(&mm->mmap_sem); 1266925d1c40SMatt Helsley return exe_file; 1267925d1c40SMatt Helsley } 1268925d1c40SMatt Helsley 1269925d1c40SMatt Helsley void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm) 1270925d1c40SMatt Helsley { 1271925d1c40SMatt Helsley /* It's safe to write the exe_file pointer without exe_file_lock because 1272925d1c40SMatt Helsley * this is called during fork when the task is not yet in /proc */ 1273925d1c40SMatt Helsley newmm->exe_file = get_mm_exe_file(oldmm); 1274925d1c40SMatt Helsley } 1275925d1c40SMatt Helsley 1276925d1c40SMatt Helsley static int proc_exe_link(struct inode *inode, struct path *exe_path) 1277925d1c40SMatt Helsley { 1278925d1c40SMatt Helsley struct task_struct *task; 1279925d1c40SMatt Helsley struct mm_struct *mm; 1280925d1c40SMatt Helsley struct file *exe_file; 1281925d1c40SMatt Helsley 1282925d1c40SMatt Helsley task = get_proc_task(inode); 1283925d1c40SMatt Helsley if (!task) 1284925d1c40SMatt Helsley return -ENOENT; 1285925d1c40SMatt Helsley mm = get_task_mm(task); 1286925d1c40SMatt Helsley put_task_struct(task); 1287925d1c40SMatt Helsley if (!mm) 1288925d1c40SMatt Helsley return -ENOENT; 1289925d1c40SMatt Helsley exe_file = get_mm_exe_file(mm); 1290925d1c40SMatt Helsley mmput(mm); 1291925d1c40SMatt Helsley if (exe_file) { 1292925d1c40SMatt Helsley *exe_path = exe_file->f_path; 1293925d1c40SMatt Helsley path_get(&exe_file->f_path); 1294925d1c40SMatt Helsley fput(exe_file); 1295925d1c40SMatt Helsley return 0; 1296925d1c40SMatt Helsley } else 1297925d1c40SMatt Helsley return -ENOENT; 1298925d1c40SMatt Helsley } 1299925d1c40SMatt Helsley 1300008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 13011da177e4SLinus Torvalds { 13021da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13031da177e4SLinus Torvalds int error = -EACCES; 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 13061d957f9bSJan Blunck path_put(&nd->path); 13071da177e4SLinus Torvalds 1308778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1309778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13101da177e4SLinus Torvalds goto out; 13111da177e4SLinus Torvalds 13123dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 13131da177e4SLinus Torvalds nd->last_type = LAST_BIND; 13141da177e4SLinus Torvalds out: 1315008b150aSAl Viro return ERR_PTR(error); 13161da177e4SLinus Torvalds } 13171da177e4SLinus Torvalds 13183dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 13191da177e4SLinus Torvalds { 1320e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 13213dcd25f3SJan Blunck char *pathname; 13221da177e4SLinus Torvalds int len; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds if (!tmp) 13251da177e4SLinus Torvalds return -ENOMEM; 13261da177e4SLinus Torvalds 1327cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 13283dcd25f3SJan Blunck len = PTR_ERR(pathname); 13293dcd25f3SJan Blunck if (IS_ERR(pathname)) 13301da177e4SLinus Torvalds goto out; 13313dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds if (len > buflen) 13341da177e4SLinus Torvalds len = buflen; 13353dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 13361da177e4SLinus Torvalds len = -EFAULT; 13371da177e4SLinus Torvalds out: 13381da177e4SLinus Torvalds free_page((unsigned long)tmp); 13391da177e4SLinus Torvalds return len; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 13431da177e4SLinus Torvalds { 13441da177e4SLinus Torvalds int error = -EACCES; 13451da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 13463dcd25f3SJan Blunck struct path path; 13471da177e4SLinus Torvalds 1348778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1349778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 13501da177e4SLinus Torvalds goto out; 13511da177e4SLinus Torvalds 13523dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 13531da177e4SLinus Torvalds if (error) 13541da177e4SLinus Torvalds goto out; 13551da177e4SLinus Torvalds 13563dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 13573dcd25f3SJan Blunck path_put(&path); 13581da177e4SLinus Torvalds out: 13591da177e4SLinus Torvalds return error; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 1362c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 13631da177e4SLinus Torvalds .readlink = proc_pid_readlink, 13646d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 13656d76fa58SLinus Torvalds .setattr = proc_setattr, 13661da177e4SLinus Torvalds }; 13671da177e4SLinus Torvalds 136828a6d671SEric W. Biederman 136928a6d671SEric W. Biederman /* building an inode */ 137028a6d671SEric W. Biederman 137128a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 137228a6d671SEric W. Biederman { 137328a6d671SEric W. Biederman int dumpable = 0; 137428a6d671SEric W. Biederman struct mm_struct *mm; 137528a6d671SEric W. Biederman 137628a6d671SEric W. Biederman task_lock(task); 137728a6d671SEric W. Biederman mm = task->mm; 137828a6d671SEric W. Biederman if (mm) 13796c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 138028a6d671SEric W. Biederman task_unlock(task); 138128a6d671SEric W. Biederman if(dumpable == 1) 138228a6d671SEric W. Biederman return 1; 138328a6d671SEric W. Biederman return 0; 138428a6d671SEric W. Biederman } 138528a6d671SEric W. Biederman 138628a6d671SEric W. Biederman 138761a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 138828a6d671SEric W. Biederman { 138928a6d671SEric W. Biederman struct inode * inode; 139028a6d671SEric W. Biederman struct proc_inode *ei; 139128a6d671SEric W. Biederman 139228a6d671SEric W. Biederman /* We need a new inode */ 139328a6d671SEric W. Biederman 139428a6d671SEric W. Biederman inode = new_inode(sb); 139528a6d671SEric W. Biederman if (!inode) 139628a6d671SEric W. Biederman goto out; 139728a6d671SEric W. Biederman 139828a6d671SEric W. Biederman /* Common stuff */ 139928a6d671SEric W. Biederman ei = PROC_I(inode); 140028a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 140128a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 140228a6d671SEric W. Biederman 140328a6d671SEric W. Biederman /* 140428a6d671SEric W. Biederman * grab the reference to task. 140528a6d671SEric W. Biederman */ 14061a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 140728a6d671SEric W. Biederman if (!ei->pid) 140828a6d671SEric W. Biederman goto out_unlock; 140928a6d671SEric W. Biederman 141028a6d671SEric W. Biederman inode->i_uid = 0; 141128a6d671SEric W. Biederman inode->i_gid = 0; 141228a6d671SEric W. Biederman if (task_dumpable(task)) { 141328a6d671SEric W. Biederman inode->i_uid = task->euid; 141428a6d671SEric W. Biederman inode->i_gid = task->egid; 141528a6d671SEric W. Biederman } 141628a6d671SEric W. Biederman security_task_to_inode(task, inode); 141728a6d671SEric W. Biederman 141828a6d671SEric W. Biederman out: 141928a6d671SEric W. Biederman return inode; 142028a6d671SEric W. Biederman 142128a6d671SEric W. Biederman out_unlock: 142228a6d671SEric W. Biederman iput(inode); 142328a6d671SEric W. Biederman return NULL; 142428a6d671SEric W. Biederman } 142528a6d671SEric W. Biederman 142628a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 142728a6d671SEric W. Biederman { 142828a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 142928a6d671SEric W. Biederman struct task_struct *task; 143028a6d671SEric W. Biederman generic_fillattr(inode, stat); 143128a6d671SEric W. Biederman 143228a6d671SEric W. Biederman rcu_read_lock(); 143328a6d671SEric W. Biederman stat->uid = 0; 143428a6d671SEric W. Biederman stat->gid = 0; 143528a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 143628a6d671SEric W. Biederman if (task) { 143728a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 143828a6d671SEric W. Biederman task_dumpable(task)) { 143928a6d671SEric W. Biederman stat->uid = task->euid; 144028a6d671SEric W. Biederman stat->gid = task->egid; 144128a6d671SEric W. Biederman } 144228a6d671SEric W. Biederman } 144328a6d671SEric W. Biederman rcu_read_unlock(); 144428a6d671SEric W. Biederman return 0; 144528a6d671SEric W. Biederman } 144628a6d671SEric W. Biederman 144728a6d671SEric W. Biederman /* dentry stuff */ 144828a6d671SEric W. Biederman 144928a6d671SEric W. Biederman /* 145028a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 145128a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 145228a6d671SEric W. Biederman * due to the way we treat inodes. 145328a6d671SEric W. Biederman * 145428a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 145528a6d671SEric W. Biederman * performed a setuid(), etc. 145628a6d671SEric W. Biederman * 145728a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 145828a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 145928a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 146028a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 146128a6d671SEric W. Biederman * made this apply to all per process world readable and executable 146228a6d671SEric W. Biederman * directories. 146328a6d671SEric W. Biederman */ 146428a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 146528a6d671SEric W. Biederman { 146628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 146728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 146828a6d671SEric W. Biederman if (task) { 146928a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 147028a6d671SEric W. Biederman task_dumpable(task)) { 147128a6d671SEric W. Biederman inode->i_uid = task->euid; 147228a6d671SEric W. Biederman inode->i_gid = task->egid; 147328a6d671SEric W. Biederman } else { 147428a6d671SEric W. Biederman inode->i_uid = 0; 147528a6d671SEric W. Biederman inode->i_gid = 0; 147628a6d671SEric W. Biederman } 147728a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 147828a6d671SEric W. Biederman security_task_to_inode(task, inode); 147928a6d671SEric W. Biederman put_task_struct(task); 148028a6d671SEric W. Biederman return 1; 148128a6d671SEric W. Biederman } 148228a6d671SEric W. Biederman d_drop(dentry); 148328a6d671SEric W. Biederman return 0; 148428a6d671SEric W. Biederman } 148528a6d671SEric W. Biederman 148628a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 148728a6d671SEric W. Biederman { 148828a6d671SEric W. Biederman /* Is the task we represent dead? 148928a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 149028a6d671SEric W. Biederman * kill it immediately. 149128a6d671SEric W. Biederman */ 149228a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 149328a6d671SEric W. Biederman } 149428a6d671SEric W. Biederman 149528a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 149628a6d671SEric W. Biederman { 149728a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 149828a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 149928a6d671SEric W. Biederman }; 150028a6d671SEric W. Biederman 150128a6d671SEric W. Biederman /* Lookups */ 150228a6d671SEric W. Biederman 1503c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1504c5141e6dSEric Dumazet struct task_struct *, const void *); 150561a28784SEric W. Biederman 15061c0d04c9SEric W. Biederman /* 15071c0d04c9SEric W. Biederman * Fill a directory entry. 15081c0d04c9SEric W. Biederman * 15091c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 15101c0d04c9SEric W. Biederman * file type from dcache entry. 15111c0d04c9SEric W. Biederman * 15121c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 15131c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 15141c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 15151c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 15161c0d04c9SEric W. Biederman * by stat. 15171c0d04c9SEric W. Biederman */ 151861a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 151961a28784SEric W. Biederman char *name, int len, 1520c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 152161a28784SEric W. Biederman { 15222fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 152361a28784SEric W. Biederman struct inode *inode; 152461a28784SEric W. Biederman struct qstr qname; 152561a28784SEric W. Biederman ino_t ino = 0; 152661a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 152761a28784SEric W. Biederman 152861a28784SEric W. Biederman qname.name = name; 152961a28784SEric W. Biederman qname.len = len; 153061a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 153161a28784SEric W. Biederman 153261a28784SEric W. Biederman child = d_lookup(dir, &qname); 153361a28784SEric W. Biederman if (!child) { 153461a28784SEric W. Biederman struct dentry *new; 153561a28784SEric W. Biederman new = d_alloc(dir, &qname); 153661a28784SEric W. Biederman if (new) { 153761a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 153861a28784SEric W. Biederman if (child) 153961a28784SEric W. Biederman dput(new); 154061a28784SEric W. Biederman else 154161a28784SEric W. Biederman child = new; 154261a28784SEric W. Biederman } 154361a28784SEric W. Biederman } 154461a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 154561a28784SEric W. Biederman goto end_instantiate; 154661a28784SEric W. Biederman inode = child->d_inode; 154761a28784SEric W. Biederman if (inode) { 154861a28784SEric W. Biederman ino = inode->i_ino; 154961a28784SEric W. Biederman type = inode->i_mode >> 12; 155061a28784SEric W. Biederman } 155161a28784SEric W. Biederman dput(child); 155261a28784SEric W. Biederman end_instantiate: 155361a28784SEric W. Biederman if (!ino) 155461a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 155561a28784SEric W. Biederman if (!ino) 155661a28784SEric W. Biederman ino = 1; 155761a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 155861a28784SEric W. Biederman } 155961a28784SEric W. Biederman 156028a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 156128a6d671SEric W. Biederman { 156228a6d671SEric W. Biederman const char *name = dentry->d_name.name; 156328a6d671SEric W. Biederman int len = dentry->d_name.len; 156428a6d671SEric W. Biederman unsigned n = 0; 156528a6d671SEric W. Biederman 156628a6d671SEric W. Biederman if (len > 1 && *name == '0') 156728a6d671SEric W. Biederman goto out; 156828a6d671SEric W. Biederman while (len-- > 0) { 156928a6d671SEric W. Biederman unsigned c = *name++ - '0'; 157028a6d671SEric W. Biederman if (c > 9) 157128a6d671SEric W. Biederman goto out; 157228a6d671SEric W. Biederman if (n >= (~0U-9)/10) 157328a6d671SEric W. Biederman goto out; 157428a6d671SEric W. Biederman n *= 10; 157528a6d671SEric W. Biederman n += c; 157628a6d671SEric W. Biederman } 157728a6d671SEric W. Biederman return n; 157828a6d671SEric W. Biederman out: 157928a6d671SEric W. Biederman return ~0U; 158028a6d671SEric W. Biederman } 158128a6d671SEric W. Biederman 158227932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 158327932742SMiklos Szeredi 15843dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 158528a6d671SEric W. Biederman { 158628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 158728a6d671SEric W. Biederman struct files_struct *files = NULL; 158828a6d671SEric W. Biederman struct file *file; 158928a6d671SEric W. Biederman int fd = proc_fd(inode); 159028a6d671SEric W. Biederman 159128a6d671SEric W. Biederman if (task) { 159228a6d671SEric W. Biederman files = get_files_struct(task); 159328a6d671SEric W. Biederman put_task_struct(task); 159428a6d671SEric W. Biederman } 159528a6d671SEric W. Biederman if (files) { 159628a6d671SEric W. Biederman /* 159728a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 159828a6d671SEric W. Biederman * hold ->file_lock. 159928a6d671SEric W. Biederman */ 160028a6d671SEric W. Biederman spin_lock(&files->file_lock); 160128a6d671SEric W. Biederman file = fcheck_files(files, fd); 160228a6d671SEric W. Biederman if (file) { 16033dcd25f3SJan Blunck if (path) { 16043dcd25f3SJan Blunck *path = file->f_path; 16053dcd25f3SJan Blunck path_get(&file->f_path); 16063dcd25f3SJan Blunck } 160727932742SMiklos Szeredi if (info) 160827932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 160927932742SMiklos Szeredi "pos:\t%lli\n" 161027932742SMiklos Szeredi "flags:\t0%o\n", 161127932742SMiklos Szeredi (long long) file->f_pos, 161227932742SMiklos Szeredi file->f_flags); 161328a6d671SEric W. Biederman spin_unlock(&files->file_lock); 161428a6d671SEric W. Biederman put_files_struct(files); 161528a6d671SEric W. Biederman return 0; 161628a6d671SEric W. Biederman } 161728a6d671SEric W. Biederman spin_unlock(&files->file_lock); 161828a6d671SEric W. Biederman put_files_struct(files); 161928a6d671SEric W. Biederman } 162028a6d671SEric W. Biederman return -ENOENT; 162128a6d671SEric W. Biederman } 162228a6d671SEric W. Biederman 16233dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 162427932742SMiklos Szeredi { 16253dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 162627932742SMiklos Szeredi } 162727932742SMiklos Szeredi 162828a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 162928a6d671SEric W. Biederman { 163028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 163128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 163228a6d671SEric W. Biederman int fd = proc_fd(inode); 163328a6d671SEric W. Biederman struct files_struct *files; 163428a6d671SEric W. Biederman 163528a6d671SEric W. Biederman if (task) { 163628a6d671SEric W. Biederman files = get_files_struct(task); 163728a6d671SEric W. Biederman if (files) { 163828a6d671SEric W. Biederman rcu_read_lock(); 163928a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 164028a6d671SEric W. Biederman rcu_read_unlock(); 164128a6d671SEric W. Biederman put_files_struct(files); 164228a6d671SEric W. Biederman if (task_dumpable(task)) { 164328a6d671SEric W. Biederman inode->i_uid = task->euid; 164428a6d671SEric W. Biederman inode->i_gid = task->egid; 164528a6d671SEric W. Biederman } else { 164628a6d671SEric W. Biederman inode->i_uid = 0; 164728a6d671SEric W. Biederman inode->i_gid = 0; 164828a6d671SEric W. Biederman } 164928a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 165028a6d671SEric W. Biederman security_task_to_inode(task, inode); 165128a6d671SEric W. Biederman put_task_struct(task); 165228a6d671SEric W. Biederman return 1; 165328a6d671SEric W. Biederman } 165428a6d671SEric W. Biederman rcu_read_unlock(); 165528a6d671SEric W. Biederman put_files_struct(files); 165628a6d671SEric W. Biederman } 165728a6d671SEric W. Biederman put_task_struct(task); 165828a6d671SEric W. Biederman } 165928a6d671SEric W. Biederman d_drop(dentry); 166028a6d671SEric W. Biederman return 0; 166128a6d671SEric W. Biederman } 166228a6d671SEric W. Biederman 166328a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 166428a6d671SEric W. Biederman { 166528a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 166628a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 166728a6d671SEric W. Biederman }; 166828a6d671SEric W. Biederman 1669444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1670c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 167128a6d671SEric W. Biederman { 1672c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 167328a6d671SEric W. Biederman struct file *file; 167428a6d671SEric W. Biederman struct files_struct *files; 167528a6d671SEric W. Biederman struct inode *inode; 167628a6d671SEric W. Biederman struct proc_inode *ei; 1677444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 167828a6d671SEric W. Biederman 167961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 168028a6d671SEric W. Biederman if (!inode) 168128a6d671SEric W. Biederman goto out; 168228a6d671SEric W. Biederman ei = PROC_I(inode); 168328a6d671SEric W. Biederman ei->fd = fd; 168428a6d671SEric W. Biederman files = get_files_struct(task); 168528a6d671SEric W. Biederman if (!files) 1686444ceed8SEric W. Biederman goto out_iput; 168728a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 168828a6d671SEric W. Biederman 168928a6d671SEric W. Biederman /* 169028a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 169128a6d671SEric W. Biederman * hold ->file_lock. 169228a6d671SEric W. Biederman */ 169328a6d671SEric W. Biederman spin_lock(&files->file_lock); 169428a6d671SEric W. Biederman file = fcheck_files(files, fd); 169528a6d671SEric W. Biederman if (!file) 1696444ceed8SEric W. Biederman goto out_unlock; 169728a6d671SEric W. Biederman if (file->f_mode & 1) 169828a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 169928a6d671SEric W. Biederman if (file->f_mode & 2) 170028a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 170128a6d671SEric W. Biederman spin_unlock(&files->file_lock); 170228a6d671SEric W. Biederman put_files_struct(files); 1703444ceed8SEric W. Biederman 170428a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 170528a6d671SEric W. Biederman inode->i_size = 64; 170628a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 170728a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 170828a6d671SEric W. Biederman d_add(dentry, inode); 170928a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 171028a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1711444ceed8SEric W. Biederman error = NULL; 1712444ceed8SEric W. Biederman 1713444ceed8SEric W. Biederman out: 1714444ceed8SEric W. Biederman return error; 1715444ceed8SEric W. Biederman out_unlock: 1716444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1717444ceed8SEric W. Biederman put_files_struct(files); 1718444ceed8SEric W. Biederman out_iput: 1719444ceed8SEric W. Biederman iput(inode); 1720444ceed8SEric W. Biederman goto out; 1721444ceed8SEric W. Biederman } 1722444ceed8SEric W. Biederman 172327932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 172427932742SMiklos Szeredi struct dentry *dentry, 172527932742SMiklos Szeredi instantiate_t instantiate) 1726444ceed8SEric W. Biederman { 1727444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1728444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1729444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1730444ceed8SEric W. Biederman 1731444ceed8SEric W. Biederman if (!task) 1732444ceed8SEric W. Biederman goto out_no_task; 1733444ceed8SEric W. Biederman if (fd == ~0U) 1734444ceed8SEric W. Biederman goto out; 1735444ceed8SEric W. Biederman 173627932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 173728a6d671SEric W. Biederman out: 173828a6d671SEric W. Biederman put_task_struct(task); 173928a6d671SEric W. Biederman out_no_task: 174028a6d671SEric W. Biederman return result; 174128a6d671SEric W. Biederman } 174228a6d671SEric W. Biederman 174327932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 174427932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 17451da177e4SLinus Torvalds { 17462fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 17475634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 174899f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1749457c2510SPavel Emelyanov unsigned int fd, ino; 17501da177e4SLinus Torvalds int retval; 17511da177e4SLinus Torvalds struct files_struct * files; 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds retval = -ENOENT; 175499f89551SEric W. Biederman if (!p) 175599f89551SEric W. Biederman goto out_no_task; 17561da177e4SLinus Torvalds retval = 0; 17571da177e4SLinus Torvalds 17581da177e4SLinus Torvalds fd = filp->f_pos; 17591da177e4SLinus Torvalds switch (fd) { 17601da177e4SLinus Torvalds case 0: 17611da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 17621da177e4SLinus Torvalds goto out; 17631da177e4SLinus Torvalds filp->f_pos++; 17641da177e4SLinus Torvalds case 1: 17655634708bSEric W. Biederman ino = parent_ino(dentry); 17661da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 17671da177e4SLinus Torvalds goto out; 17681da177e4SLinus Torvalds filp->f_pos++; 17691da177e4SLinus Torvalds default: 17701da177e4SLinus Torvalds files = get_files_struct(p); 17711da177e4SLinus Torvalds if (!files) 17721da177e4SLinus Torvalds goto out; 1773b835996fSDipankar Sarma rcu_read_lock(); 17741da177e4SLinus Torvalds for (fd = filp->f_pos-2; 17759b4f526cSAl Viro fd < files_fdtable(files)->max_fds; 17761da177e4SLinus Torvalds fd++, filp->f_pos++) { 177727932742SMiklos Szeredi char name[PROC_NUMBUF]; 177827932742SMiklos Szeredi int len; 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 17811da177e4SLinus Torvalds continue; 1782b835996fSDipankar Sarma rcu_read_unlock(); 17831da177e4SLinus Torvalds 178427932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 178527932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 178627932742SMiklos Szeredi name, len, instantiate, 178727932742SMiklos Szeredi p, &fd) < 0) { 1788b835996fSDipankar Sarma rcu_read_lock(); 17891da177e4SLinus Torvalds break; 17901da177e4SLinus Torvalds } 1791b835996fSDipankar Sarma rcu_read_lock(); 17921da177e4SLinus Torvalds } 1793b835996fSDipankar Sarma rcu_read_unlock(); 17941da177e4SLinus Torvalds put_files_struct(files); 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds out: 179799f89551SEric W. Biederman put_task_struct(p); 179899f89551SEric W. Biederman out_no_task: 17991da177e4SLinus Torvalds return retval; 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds 180227932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 180327932742SMiklos Szeredi struct nameidata *nd) 180427932742SMiklos Szeredi { 180527932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 180627932742SMiklos Szeredi } 180727932742SMiklos Szeredi 180827932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 180927932742SMiklos Szeredi { 181027932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 181127932742SMiklos Szeredi } 181227932742SMiklos Szeredi 181327932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 181427932742SMiklos Szeredi size_t len, loff_t *ppos) 181527932742SMiklos Szeredi { 181627932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 18173dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 181827932742SMiklos Szeredi if (!err) 181927932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 182027932742SMiklos Szeredi return err; 182127932742SMiklos Szeredi } 182227932742SMiklos Szeredi 182327932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 182427932742SMiklos Szeredi .open = nonseekable_open, 182527932742SMiklos Szeredi .read = proc_fdinfo_read, 182627932742SMiklos Szeredi }; 182727932742SMiklos Szeredi 182800977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 18291da177e4SLinus Torvalds .read = generic_read_dir, 18301da177e4SLinus Torvalds .readdir = proc_readfd, 18311da177e4SLinus Torvalds }; 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds /* 18348948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 18358948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 18368948e11fSAlexey Dobriyan */ 18378948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 18388948e11fSAlexey Dobriyan struct nameidata *nd) 18398948e11fSAlexey Dobriyan { 18408948e11fSAlexey Dobriyan int rv; 18418948e11fSAlexey Dobriyan 18428948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 18438948e11fSAlexey Dobriyan if (rv == 0) 18448948e11fSAlexey Dobriyan return 0; 18458948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 18468948e11fSAlexey Dobriyan rv = 0; 18478948e11fSAlexey Dobriyan return rv; 18488948e11fSAlexey Dobriyan } 18498948e11fSAlexey Dobriyan 18508948e11fSAlexey Dobriyan /* 18511da177e4SLinus Torvalds * proc directories can do almost nothing.. 18521da177e4SLinus Torvalds */ 1853c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 18541da177e4SLinus Torvalds .lookup = proc_lookupfd, 18558948e11fSAlexey Dobriyan .permission = proc_fd_permission, 18566d76fa58SLinus Torvalds .setattr = proc_setattr, 18571da177e4SLinus Torvalds }; 18581da177e4SLinus Torvalds 185927932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 186027932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 186127932742SMiklos Szeredi { 186227932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 186327932742SMiklos Szeredi struct inode *inode; 186427932742SMiklos Szeredi struct proc_inode *ei; 186527932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 186627932742SMiklos Szeredi 186727932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 186827932742SMiklos Szeredi if (!inode) 186927932742SMiklos Szeredi goto out; 187027932742SMiklos Szeredi ei = PROC_I(inode); 187127932742SMiklos Szeredi ei->fd = fd; 187227932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 187327932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 187427932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 187527932742SMiklos Szeredi d_add(dentry, inode); 187627932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 187727932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 187827932742SMiklos Szeredi error = NULL; 187927932742SMiklos Szeredi 188027932742SMiklos Szeredi out: 188127932742SMiklos Szeredi return error; 188227932742SMiklos Szeredi } 188327932742SMiklos Szeredi 188427932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 188527932742SMiklos Szeredi struct dentry *dentry, 188627932742SMiklos Szeredi struct nameidata *nd) 188727932742SMiklos Szeredi { 188827932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 188927932742SMiklos Szeredi } 189027932742SMiklos Szeredi 189127932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 189227932742SMiklos Szeredi { 189327932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 189427932742SMiklos Szeredi proc_fdinfo_instantiate); 189527932742SMiklos Szeredi } 189627932742SMiklos Szeredi 189727932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 189827932742SMiklos Szeredi .read = generic_read_dir, 189927932742SMiklos Szeredi .readdir = proc_readfdinfo, 190027932742SMiklos Szeredi }; 190127932742SMiklos Szeredi 190227932742SMiklos Szeredi /* 190327932742SMiklos Szeredi * proc directories can do almost nothing.. 190427932742SMiklos Szeredi */ 190527932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 190627932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 190727932742SMiklos Szeredi .setattr = proc_setattr, 190827932742SMiklos Szeredi }; 190927932742SMiklos Szeredi 191027932742SMiklos Szeredi 1911444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1912c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1913444ceed8SEric W. Biederman { 1914c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1915444ceed8SEric W. Biederman struct inode *inode; 1916444ceed8SEric W. Biederman struct proc_inode *ei; 1917444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1918444ceed8SEric W. Biederman 191961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1920444ceed8SEric W. Biederman if (!inode) 1921444ceed8SEric W. Biederman goto out; 1922444ceed8SEric W. Biederman 1923444ceed8SEric W. Biederman ei = PROC_I(inode); 1924444ceed8SEric W. Biederman inode->i_mode = p->mode; 1925444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1926444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1927444ceed8SEric W. Biederman if (p->iop) 1928444ceed8SEric W. Biederman inode->i_op = p->iop; 1929444ceed8SEric W. Biederman if (p->fop) 1930444ceed8SEric W. Biederman inode->i_fop = p->fop; 1931444ceed8SEric W. Biederman ei->op = p->op; 1932444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1933444ceed8SEric W. Biederman d_add(dentry, inode); 1934444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1935444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1936444ceed8SEric W. Biederman error = NULL; 1937444ceed8SEric W. Biederman out: 1938444ceed8SEric W. Biederman return error; 1939444ceed8SEric W. Biederman } 1940444ceed8SEric W. Biederman 19411da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 19421da177e4SLinus Torvalds struct dentry *dentry, 1943c5141e6dSEric Dumazet const struct pid_entry *ents, 19447bcd6b0eSEric W. Biederman unsigned int nents) 19451da177e4SLinus Torvalds { 19461da177e4SLinus Torvalds struct inode *inode; 1947cd6a3ce9SEric W. Biederman struct dentry *error; 194899f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1949c5141e6dSEric Dumazet const struct pid_entry *p, *last; 19501da177e4SLinus Torvalds 1951cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 19521da177e4SLinus Torvalds inode = NULL; 19531da177e4SLinus Torvalds 195499f89551SEric W. Biederman if (!task) 195599f89551SEric W. Biederman goto out_no_task; 19561da177e4SLinus Torvalds 195720cdc894SEric W. Biederman /* 195820cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 195920cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 196020cdc894SEric W. Biederman */ 19617bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19627bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 19631da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 19641da177e4SLinus Torvalds continue; 19651da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 19661da177e4SLinus Torvalds break; 19671da177e4SLinus Torvalds } 19687bcd6b0eSEric W. Biederman if (p > last) 19691da177e4SLinus Torvalds goto out; 19701da177e4SLinus Torvalds 1971444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 19721da177e4SLinus Torvalds out: 197399f89551SEric W. Biederman put_task_struct(task); 197499f89551SEric W. Biederman out_no_task: 1975cd6a3ce9SEric W. Biederman return error; 19761da177e4SLinus Torvalds } 19771da177e4SLinus Torvalds 1978c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1979c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 198061a28784SEric W. Biederman { 198161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 198261a28784SEric W. Biederman proc_pident_instantiate, task, p); 198361a28784SEric W. Biederman } 198461a28784SEric W. Biederman 198528a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 198628a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1987c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 198828a6d671SEric W. Biederman { 198928a6d671SEric W. Biederman int i; 19902fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 199128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 199228a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1993c5141e6dSEric Dumazet const struct pid_entry *p, *last; 199428a6d671SEric W. Biederman ino_t ino; 199528a6d671SEric W. Biederman int ret; 199628a6d671SEric W. Biederman 199728a6d671SEric W. Biederman ret = -ENOENT; 199828a6d671SEric W. Biederman if (!task) 199961a28784SEric W. Biederman goto out_no_task; 200028a6d671SEric W. Biederman 200128a6d671SEric W. Biederman ret = 0; 200228a6d671SEric W. Biederman i = filp->f_pos; 200328a6d671SEric W. Biederman switch (i) { 200428a6d671SEric W. Biederman case 0: 200528a6d671SEric W. Biederman ino = inode->i_ino; 200628a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 200728a6d671SEric W. Biederman goto out; 200828a6d671SEric W. Biederman i++; 200928a6d671SEric W. Biederman filp->f_pos++; 201028a6d671SEric W. Biederman /* fall through */ 201128a6d671SEric W. Biederman case 1: 201228a6d671SEric W. Biederman ino = parent_ino(dentry); 201328a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 201428a6d671SEric W. Biederman goto out; 201528a6d671SEric W. Biederman i++; 201628a6d671SEric W. Biederman filp->f_pos++; 201728a6d671SEric W. Biederman /* fall through */ 201828a6d671SEric W. Biederman default: 201928a6d671SEric W. Biederman i -= 2; 202028a6d671SEric W. Biederman if (i >= nents) { 202128a6d671SEric W. Biederman ret = 1; 202228a6d671SEric W. Biederman goto out; 202328a6d671SEric W. Biederman } 202428a6d671SEric W. Biederman p = ents + i; 20257bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 20267bcd6b0eSEric W. Biederman while (p <= last) { 202761a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 202828a6d671SEric W. Biederman goto out; 202928a6d671SEric W. Biederman filp->f_pos++; 203028a6d671SEric W. Biederman p++; 203128a6d671SEric W. Biederman } 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 203428a6d671SEric W. Biederman ret = 1; 203528a6d671SEric W. Biederman out: 203661a28784SEric W. Biederman put_task_struct(task); 203761a28784SEric W. Biederman out_no_task: 203828a6d671SEric W. Biederman return ret; 20391da177e4SLinus Torvalds } 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 204228a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 204328a6d671SEric W. Biederman size_t count, loff_t *ppos) 204428a6d671SEric W. Biederman { 20452fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 204604ff9708SAl Viro char *p = NULL; 204728a6d671SEric W. Biederman ssize_t length; 204828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 204928a6d671SEric W. Biederman 205028a6d671SEric W. Biederman if (!task) 205104ff9708SAl Viro return -ESRCH; 205228a6d671SEric W. Biederman 205328a6d671SEric W. Biederman length = security_getprocattr(task, 20542fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 205504ff9708SAl Viro &p); 205628a6d671SEric W. Biederman put_task_struct(task); 205704ff9708SAl Viro if (length > 0) 205804ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 205904ff9708SAl Viro kfree(p); 206028a6d671SEric W. Biederman return length; 206128a6d671SEric W. Biederman } 206228a6d671SEric W. Biederman 206328a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 206428a6d671SEric W. Biederman size_t count, loff_t *ppos) 206528a6d671SEric W. Biederman { 20662fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 206728a6d671SEric W. Biederman char *page; 206828a6d671SEric W. Biederman ssize_t length; 206928a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 207028a6d671SEric W. Biederman 207128a6d671SEric W. Biederman length = -ESRCH; 207228a6d671SEric W. Biederman if (!task) 207328a6d671SEric W. Biederman goto out_no_task; 207428a6d671SEric W. Biederman if (count > PAGE_SIZE) 207528a6d671SEric W. Biederman count = PAGE_SIZE; 207628a6d671SEric W. Biederman 207728a6d671SEric W. Biederman /* No partial writes. */ 207828a6d671SEric W. Biederman length = -EINVAL; 207928a6d671SEric W. Biederman if (*ppos != 0) 208028a6d671SEric W. Biederman goto out; 208128a6d671SEric W. Biederman 208228a6d671SEric W. Biederman length = -ENOMEM; 2083e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 208428a6d671SEric W. Biederman if (!page) 208528a6d671SEric W. Biederman goto out; 208628a6d671SEric W. Biederman 208728a6d671SEric W. Biederman length = -EFAULT; 208828a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 208928a6d671SEric W. Biederman goto out_free; 209028a6d671SEric W. Biederman 209128a6d671SEric W. Biederman length = security_setprocattr(task, 20922fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 209328a6d671SEric W. Biederman (void*)page, count); 209428a6d671SEric W. Biederman out_free: 209528a6d671SEric W. Biederman free_page((unsigned long) page); 209628a6d671SEric W. Biederman out: 209728a6d671SEric W. Biederman put_task_struct(task); 209828a6d671SEric W. Biederman out_no_task: 209928a6d671SEric W. Biederman return length; 210028a6d671SEric W. Biederman } 210128a6d671SEric W. Biederman 210200977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 210328a6d671SEric W. Biederman .read = proc_pid_attr_read, 210428a6d671SEric W. Biederman .write = proc_pid_attr_write, 210528a6d671SEric W. Biederman }; 210628a6d671SEric W. Biederman 2107c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 210861a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 210961a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 211061a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 211161a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 211261a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 211361a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 211428a6d671SEric W. Biederman }; 211528a6d671SEric W. Biederman 211672d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 21171da177e4SLinus Torvalds void * dirent, filldir_t filldir) 21181da177e4SLinus Torvalds { 21191da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 212072d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds 212300977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 21241da177e4SLinus Torvalds .read = generic_read_dir, 212572d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 21261da177e4SLinus Torvalds }; 21271da177e4SLinus Torvalds 212872d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 21291da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 21301da177e4SLinus Torvalds { 21317bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21327bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 21331da177e4SLinus Torvalds } 21341da177e4SLinus Torvalds 2135c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 213672d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 213799f89551SEric W. Biederman .getattr = pid_getattr, 21386d76fa58SLinus Torvalds .setattr = proc_setattr, 21391da177e4SLinus Torvalds }; 21401da177e4SLinus Torvalds 21411da177e4SLinus Torvalds #endif 21421da177e4SLinus Torvalds 21433cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21443cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 21453cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 21463cb4a0bbSKawai, Hidehiro { 21473cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 21483cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21493cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 21503cb4a0bbSKawai, Hidehiro size_t len; 21513cb4a0bbSKawai, Hidehiro int ret; 21523cb4a0bbSKawai, Hidehiro 21533cb4a0bbSKawai, Hidehiro if (!task) 21543cb4a0bbSKawai, Hidehiro return -ESRCH; 21553cb4a0bbSKawai, Hidehiro 21563cb4a0bbSKawai, Hidehiro ret = 0; 21573cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 21583cb4a0bbSKawai, Hidehiro if (mm) { 21593cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 21603cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 21613cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 21623cb4a0bbSKawai, Hidehiro mmput(mm); 21633cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 21643cb4a0bbSKawai, Hidehiro } 21653cb4a0bbSKawai, Hidehiro 21663cb4a0bbSKawai, Hidehiro put_task_struct(task); 21673cb4a0bbSKawai, Hidehiro 21683cb4a0bbSKawai, Hidehiro return ret; 21693cb4a0bbSKawai, Hidehiro } 21703cb4a0bbSKawai, Hidehiro 21713cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 21723cb4a0bbSKawai, Hidehiro const char __user *buf, 21733cb4a0bbSKawai, Hidehiro size_t count, 21743cb4a0bbSKawai, Hidehiro loff_t *ppos) 21753cb4a0bbSKawai, Hidehiro { 21763cb4a0bbSKawai, Hidehiro struct task_struct *task; 21773cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 21783cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 21793cb4a0bbSKawai, Hidehiro unsigned int val; 21803cb4a0bbSKawai, Hidehiro int ret; 21813cb4a0bbSKawai, Hidehiro int i; 21823cb4a0bbSKawai, Hidehiro unsigned long mask; 21833cb4a0bbSKawai, Hidehiro 21843cb4a0bbSKawai, Hidehiro ret = -EFAULT; 21853cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 21863cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 21873cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 21883cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 21893cb4a0bbSKawai, Hidehiro goto out_no_task; 21903cb4a0bbSKawai, Hidehiro 21913cb4a0bbSKawai, Hidehiro ret = -EINVAL; 21923cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 21933cb4a0bbSKawai, Hidehiro if (*end == '\n') 21943cb4a0bbSKawai, Hidehiro end++; 21953cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 21963cb4a0bbSKawai, Hidehiro goto out_no_task; 21973cb4a0bbSKawai, Hidehiro 21983cb4a0bbSKawai, Hidehiro ret = -ESRCH; 21993cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 22003cb4a0bbSKawai, Hidehiro if (!task) 22013cb4a0bbSKawai, Hidehiro goto out_no_task; 22023cb4a0bbSKawai, Hidehiro 22033cb4a0bbSKawai, Hidehiro ret = end - buffer; 22043cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 22053cb4a0bbSKawai, Hidehiro if (!mm) 22063cb4a0bbSKawai, Hidehiro goto out_no_mm; 22073cb4a0bbSKawai, Hidehiro 22083cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 22093cb4a0bbSKawai, Hidehiro if (val & mask) 22103cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22113cb4a0bbSKawai, Hidehiro else 22123cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 22133cb4a0bbSKawai, Hidehiro } 22143cb4a0bbSKawai, Hidehiro 22153cb4a0bbSKawai, Hidehiro mmput(mm); 22163cb4a0bbSKawai, Hidehiro out_no_mm: 22173cb4a0bbSKawai, Hidehiro put_task_struct(task); 22183cb4a0bbSKawai, Hidehiro out_no_task: 22193cb4a0bbSKawai, Hidehiro return ret; 22203cb4a0bbSKawai, Hidehiro } 22213cb4a0bbSKawai, Hidehiro 22223cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 22233cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 22243cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 22253cb4a0bbSKawai, Hidehiro }; 22263cb4a0bbSKawai, Hidehiro #endif 22273cb4a0bbSKawai, Hidehiro 22281da177e4SLinus Torvalds /* 22291da177e4SLinus Torvalds * /proc/self: 22301da177e4SLinus Torvalds */ 22311da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 22321da177e4SLinus Torvalds int buflen) 22331da177e4SLinus Torvalds { 2234488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2235b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22368578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2237b55fcb22SAndrew Morton if (!tgid) 2238488e5bc4SEric W. Biederman return -ENOENT; 2239b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 22401da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 22411da177e4SLinus Torvalds } 22421da177e4SLinus Torvalds 2243008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 22441da177e4SLinus Torvalds { 2245488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2246b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 22478578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2248b55fcb22SAndrew Morton if (!tgid) 2249488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2250b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2251008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 22521da177e4SLinus Torvalds } 22531da177e4SLinus Torvalds 2254c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 22551da177e4SLinus Torvalds .readlink = proc_self_readlink, 22561da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 22571da177e4SLinus Torvalds }; 22581da177e4SLinus Torvalds 225928a6d671SEric W. Biederman /* 2260801199ceSEric W. Biederman * proc base 2261801199ceSEric W. Biederman * 2262801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2263801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2264801199ceSEric W. Biederman * describe something that is process related. 2265801199ceSEric W. Biederman */ 2266c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 226761a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2268801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2269801199ceSEric W. Biederman }; 2270801199ceSEric W. Biederman 2271801199ceSEric W. Biederman /* 2272801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2273801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2274801199ceSEric W. Biederman * due to the way we treat inodes. 2275801199ceSEric W. Biederman */ 2276801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2277801199ceSEric W. Biederman { 2278801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2279801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2280801199ceSEric W. Biederman if (task) { 2281801199ceSEric W. Biederman put_task_struct(task); 2282801199ceSEric W. Biederman return 1; 2283801199ceSEric W. Biederman } 2284801199ceSEric W. Biederman d_drop(dentry); 2285801199ceSEric W. Biederman return 0; 2286801199ceSEric W. Biederman } 2287801199ceSEric W. Biederman 2288801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2289801199ceSEric W. Biederman { 2290801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2291801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2292801199ceSEric W. Biederman }; 2293801199ceSEric W. Biederman 2294444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2295c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2296801199ceSEric W. Biederman { 2297c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2298801199ceSEric W. Biederman struct inode *inode; 2299801199ceSEric W. Biederman struct proc_inode *ei; 2300444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2301801199ceSEric W. Biederman 2302801199ceSEric W. Biederman /* Allocate the inode */ 2303801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2304801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2305801199ceSEric W. Biederman if (!inode) 2306801199ceSEric W. Biederman goto out; 2307801199ceSEric W. Biederman 2308801199ceSEric W. Biederman /* Initialize the inode */ 2309801199ceSEric W. Biederman ei = PROC_I(inode); 2310801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2311801199ceSEric W. Biederman 2312801199ceSEric W. Biederman /* 2313801199ceSEric W. Biederman * grab the reference to the task. 2314801199ceSEric W. Biederman */ 23151a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2316801199ceSEric W. Biederman if (!ei->pid) 2317801199ceSEric W. Biederman goto out_iput; 2318801199ceSEric W. Biederman 2319801199ceSEric W. Biederman inode->i_uid = 0; 2320801199ceSEric W. Biederman inode->i_gid = 0; 2321801199ceSEric W. Biederman inode->i_mode = p->mode; 2322801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2323801199ceSEric W. Biederman inode->i_nlink = 2; 2324801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2325801199ceSEric W. Biederman inode->i_size = 64; 2326801199ceSEric W. Biederman if (p->iop) 2327801199ceSEric W. Biederman inode->i_op = p->iop; 2328801199ceSEric W. Biederman if (p->fop) 2329801199ceSEric W. Biederman inode->i_fop = p->fop; 2330801199ceSEric W. Biederman ei->op = p->op; 2331801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2332801199ceSEric W. Biederman d_add(dentry, inode); 2333801199ceSEric W. Biederman error = NULL; 2334801199ceSEric W. Biederman out: 2335801199ceSEric W. Biederman return error; 2336801199ceSEric W. Biederman out_iput: 2337801199ceSEric W. Biederman iput(inode); 2338801199ceSEric W. Biederman goto out; 2339801199ceSEric W. Biederman } 2340801199ceSEric W. Biederman 2341444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2342444ceed8SEric W. Biederman { 2343444ceed8SEric W. Biederman struct dentry *error; 2344444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2345c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2346444ceed8SEric W. Biederman 2347444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2348444ceed8SEric W. Biederman 2349444ceed8SEric W. Biederman if (!task) 2350444ceed8SEric W. Biederman goto out_no_task; 2351444ceed8SEric W. Biederman 2352444ceed8SEric W. Biederman /* Lookup the directory entry */ 23537bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 23547bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2355444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2356444ceed8SEric W. Biederman continue; 2357444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2358444ceed8SEric W. Biederman break; 2359444ceed8SEric W. Biederman } 23607bcd6b0eSEric W. Biederman if (p > last) 2361444ceed8SEric W. Biederman goto out; 2362444ceed8SEric W. Biederman 2363444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2364444ceed8SEric W. Biederman 2365444ceed8SEric W. Biederman out: 2366444ceed8SEric W. Biederman put_task_struct(task); 2367444ceed8SEric W. Biederman out_no_task: 2368444ceed8SEric W. Biederman return error; 2369444ceed8SEric W. Biederman } 2370444ceed8SEric W. Biederman 2371c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2372c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 237361a28784SEric W. Biederman { 237461a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 237561a28784SEric W. Biederman proc_base_instantiate, task, p); 237661a28784SEric W. Biederman } 237761a28784SEric W. Biederman 2378aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2379*297c5d92SAndrea Righi static int do_io_accounting(struct task_struct *task, char *buffer, int whole) 2380aba76fdbSAndrew Morton { 2381*297c5d92SAndrea Righi u64 rchar, wchar, syscr, syscw; 2382*297c5d92SAndrea Righi struct task_io_accounting ioac; 2383*297c5d92SAndrea Righi 2384*297c5d92SAndrea Righi if (!whole) { 2385*297c5d92SAndrea Righi rchar = task->rchar; 2386*297c5d92SAndrea Righi wchar = task->wchar; 2387*297c5d92SAndrea Righi syscr = task->syscr; 2388*297c5d92SAndrea Righi syscw = task->syscw; 2389*297c5d92SAndrea Righi memcpy(&ioac, &task->ioac, sizeof(ioac)); 2390*297c5d92SAndrea Righi } else { 2391*297c5d92SAndrea Righi unsigned long flags; 2392*297c5d92SAndrea Righi struct task_struct *t = task; 2393*297c5d92SAndrea Righi rchar = wchar = syscr = syscw = 0; 2394*297c5d92SAndrea Righi memset(&ioac, 0, sizeof(ioac)); 2395*297c5d92SAndrea Righi 2396*297c5d92SAndrea Righi rcu_read_lock(); 2397*297c5d92SAndrea Righi do { 2398*297c5d92SAndrea Righi rchar += t->rchar; 2399*297c5d92SAndrea Righi wchar += t->wchar; 2400*297c5d92SAndrea Righi syscr += t->syscr; 2401*297c5d92SAndrea Righi syscw += t->syscw; 2402*297c5d92SAndrea Righi 2403*297c5d92SAndrea Righi ioac.read_bytes += t->ioac.read_bytes; 2404*297c5d92SAndrea Righi ioac.write_bytes += t->ioac.write_bytes; 2405*297c5d92SAndrea Righi ioac.cancelled_write_bytes += 2406*297c5d92SAndrea Righi t->ioac.cancelled_write_bytes; 2407*297c5d92SAndrea Righi t = next_thread(t); 2408*297c5d92SAndrea Righi } while (t != task); 2409*297c5d92SAndrea Righi rcu_read_unlock(); 2410*297c5d92SAndrea Righi 2411*297c5d92SAndrea Righi if (lock_task_sighand(task, &flags)) { 2412*297c5d92SAndrea Righi struct signal_struct *sig = task->signal; 2413*297c5d92SAndrea Righi 2414*297c5d92SAndrea Righi rchar += sig->rchar; 2415*297c5d92SAndrea Righi wchar += sig->wchar; 2416*297c5d92SAndrea Righi syscr += sig->syscr; 2417*297c5d92SAndrea Righi syscw += sig->syscw; 2418*297c5d92SAndrea Righi 2419*297c5d92SAndrea Righi ioac.read_bytes += sig->ioac.read_bytes; 2420*297c5d92SAndrea Righi ioac.write_bytes += sig->ioac.write_bytes; 2421*297c5d92SAndrea Righi ioac.cancelled_write_bytes += 2422*297c5d92SAndrea Righi sig->ioac.cancelled_write_bytes; 2423*297c5d92SAndrea Righi 2424*297c5d92SAndrea Righi unlock_task_sighand(task, &flags); 2425*297c5d92SAndrea Righi } 2426*297c5d92SAndrea Righi } 2427*297c5d92SAndrea Righi 2428aba76fdbSAndrew Morton return sprintf(buffer, 2429aba76fdbSAndrew Morton "rchar: %llu\n" 2430aba76fdbSAndrew Morton "wchar: %llu\n" 2431aba76fdbSAndrew Morton "syscr: %llu\n" 2432aba76fdbSAndrew Morton "syscw: %llu\n" 2433aba76fdbSAndrew Morton "read_bytes: %llu\n" 2434aba76fdbSAndrew Morton "write_bytes: %llu\n" 2435aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 2436*297c5d92SAndrea Righi (unsigned long long)rchar, 2437*297c5d92SAndrea Righi (unsigned long long)wchar, 2438*297c5d92SAndrea Righi (unsigned long long)syscr, 2439*297c5d92SAndrea Righi (unsigned long long)syscw, 2440*297c5d92SAndrea Righi (unsigned long long)ioac.read_bytes, 2441*297c5d92SAndrea Righi (unsigned long long)ioac.write_bytes, 2442*297c5d92SAndrea Righi (unsigned long long)ioac.cancelled_write_bytes); 2443aba76fdbSAndrew Morton } 2444*297c5d92SAndrea Righi 2445*297c5d92SAndrea Righi static int proc_tid_io_accounting(struct task_struct *task, char *buffer) 2446*297c5d92SAndrea Righi { 2447*297c5d92SAndrea Righi return do_io_accounting(task, buffer, 0); 2448*297c5d92SAndrea Righi } 2449*297c5d92SAndrea Righi 2450*297c5d92SAndrea Righi static int proc_tgid_io_accounting(struct task_struct *task, char *buffer) 2451*297c5d92SAndrea Righi { 2452*297c5d92SAndrea Righi return do_io_accounting(task, buffer, 1); 2453*297c5d92SAndrea Righi } 2454*297c5d92SAndrea Righi #endif /* CONFIG_TASK_IO_ACCOUNTING */ 2455aba76fdbSAndrew Morton 2456801199ceSEric W. Biederman /* 245728a6d671SEric W. Biederman * Thread groups 245828a6d671SEric W. Biederman */ 245900977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2460c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 246120cdc894SEric W. Biederman 2462c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 246361a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 246461a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 246527932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2466b2211a36SAndrew Morton #ifdef CONFIG_NET 24674f42c288SAndre Noll DIR("net", S_IRUGO|S_IXUGO, net), 2468b2211a36SAndrew Morton #endif 2469315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 247061a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2471df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2472d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 247343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 247443ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 247543ae34cbSIngo Molnar #endif 247661a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2477ee992744SEric W. Biederman ONE("stat", S_IRUGO, tgid_stat), 2478a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 247961a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 248028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 248161a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 248228a6d671SEric W. Biederman #endif 248361a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 248461a28784SEric W. Biederman LNK("cwd", cwd), 248561a28784SEric W. Biederman LNK("root", root), 248661a28784SEric W. Biederman LNK("exe", exe), 248761a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 24882d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 248961a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 24901e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2491b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 249261a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 249385863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 249428a6d671SEric W. Biederman #endif 249528a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 249672d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 249728a6d671SEric W. Biederman #endif 249828a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 249961a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 250028a6d671SEric W. Biederman #endif 250128a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 250261a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 250328a6d671SEric W. Biederman #endif 25049745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 25059745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 25069745512cSArjan van de Ven #endif 25078793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 250861a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 250928a6d671SEric W. Biederman #endif 2510a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2511a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2512a424316cSPaul Menage #endif 251361a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 251461a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 251528a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 251661a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 25176ee65046SSteve Grubb REG("sessionid", S_IRUGO, sessionid), 251828a6d671SEric W. Biederman #endif 2519f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2520f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2521f4f154fdSAkinobu Mita #endif 25223cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 25233cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 25243cb4a0bbSKawai, Hidehiro #endif 2525aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2526*297c5d92SAndrea Righi INF("io", S_IRUGO, tgid_io_accounting), 2527aba76fdbSAndrew Morton #endif 252828a6d671SEric W. Biederman }; 252928a6d671SEric W. Biederman 253028a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 253128a6d671SEric W. Biederman void * dirent, filldir_t filldir) 253228a6d671SEric W. Biederman { 253328a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 253428a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 253528a6d671SEric W. Biederman } 253628a6d671SEric W. Biederman 253700977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 253828a6d671SEric W. Biederman .read = generic_read_dir, 253928a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 254028a6d671SEric W. Biederman }; 254128a6d671SEric W. Biederman 254228a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25437bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25447bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 254528a6d671SEric W. Biederman } 254628a6d671SEric W. Biederman 2547c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 254828a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 254928a6d671SEric W. Biederman .getattr = pid_getattr, 255028a6d671SEric W. Biederman .setattr = proc_setattr, 255128a6d671SEric W. Biederman }; 255228a6d671SEric W. Biederman 255360347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 25541da177e4SLinus Torvalds { 255548e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 25568578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 255748e6484dSEric W. Biederman struct qstr name; 25581da177e4SLinus Torvalds 255948e6484dSEric W. Biederman name.name = buf; 256060347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 256160347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 256248e6484dSEric W. Biederman if (dentry) { 25637766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 256448e6484dSEric W. Biederman shrink_dcache_parent(dentry); 256548e6484dSEric W. Biederman d_drop(dentry); 256648e6484dSEric W. Biederman dput(dentry); 25671da177e4SLinus Torvalds } 25681da177e4SLinus Torvalds 256960347f67SPavel Emelyanov if (tgid == 0) 257048e6484dSEric W. Biederman goto out; 25711da177e4SLinus Torvalds 257248e6484dSEric W. Biederman name.name = buf; 257360347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 257460347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 257548e6484dSEric W. Biederman if (!leader) 257648e6484dSEric W. Biederman goto out; 257748e6484dSEric W. Biederman 257848e6484dSEric W. Biederman name.name = "task"; 257948e6484dSEric W. Biederman name.len = strlen(name.name); 258048e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 258148e6484dSEric W. Biederman if (!dir) 258248e6484dSEric W. Biederman goto out_put_leader; 258348e6484dSEric W. Biederman 258448e6484dSEric W. Biederman name.name = buf; 258560347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 258648e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 258748e6484dSEric W. Biederman if (dentry) { 258848e6484dSEric W. Biederman shrink_dcache_parent(dentry); 258948e6484dSEric W. Biederman d_drop(dentry); 259048e6484dSEric W. Biederman dput(dentry); 25911da177e4SLinus Torvalds } 259248e6484dSEric W. Biederman 259348e6484dSEric W. Biederman dput(dir); 259448e6484dSEric W. Biederman out_put_leader: 259548e6484dSEric W. Biederman dput(leader); 259648e6484dSEric W. Biederman out: 259748e6484dSEric W. Biederman return; 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 26000895e91dSRandy Dunlap /** 26010895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 26020895e91dSRandy Dunlap * @task: task that should be flushed. 26030895e91dSRandy Dunlap * 26040895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 260560347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 26060895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 26070895e91dSRandy Dunlap * 26080895e91dSRandy Dunlap * Looks in the dcache for 26090895e91dSRandy Dunlap * /proc/@pid 26100895e91dSRandy Dunlap * /proc/@tgid/task/@pid 26110895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 26120895e91dSRandy Dunlap * from the dcache. 26130895e91dSRandy Dunlap * 26140895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 26150895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 26160895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 26170895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 26180895e91dSRandy Dunlap * dcache entries at process exit time. 26190895e91dSRandy Dunlap * 26200895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 26210895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 26220895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 262360347f67SPavel Emelyanov */ 262460347f67SPavel Emelyanov 262560347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 262660347f67SPavel Emelyanov { 26279fcc2d15SEric W. Biederman int i; 26289fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2629130f77ecSPavel Emelyanov struct upid *upid; 2630130f77ecSPavel Emelyanov 2631130f77ecSPavel Emelyanov pid = task_pid(task); 26329fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2633130f77ecSPavel Emelyanov tgid = task_tgid(task); 26349fcc2d15SEric W. Biederman 26359fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2636130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2637130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 26389fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2639130f77ecSPavel Emelyanov } 26406f4e6433SPavel Emelyanov 26416f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 26426f4e6433SPavel Emelyanov if (upid->nr == 1) 26436f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 264460347f67SPavel Emelyanov } 264560347f67SPavel Emelyanov 26469711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 26479711ef99SAdrian Bunk struct dentry * dentry, 2648c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2649444ceed8SEric W. Biederman { 2650444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2651444ceed8SEric W. Biederman struct inode *inode; 2652444ceed8SEric W. Biederman 265361a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2654444ceed8SEric W. Biederman if (!inode) 2655444ceed8SEric W. Biederman goto out; 2656444ceed8SEric W. Biederman 2657444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2658444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2659444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2660444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2661aed54175SVegard Nossum 2662aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff, 2663aed54175SVegard Nossum ARRAY_SIZE(tgid_base_stuff)); 2664444ceed8SEric W. Biederman 2665444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2666444ceed8SEric W. Biederman 2667444ceed8SEric W. Biederman d_add(dentry, inode); 2668444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2669444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2670444ceed8SEric W. Biederman error = NULL; 2671444ceed8SEric W. Biederman out: 2672444ceed8SEric W. Biederman return error; 2673444ceed8SEric W. Biederman } 2674444ceed8SEric W. Biederman 26751da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 26761da177e4SLinus Torvalds { 2677cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 26781da177e4SLinus Torvalds struct task_struct *task; 26791da177e4SLinus Torvalds unsigned tgid; 2680b488893aSPavel Emelyanov struct pid_namespace *ns; 26811da177e4SLinus Torvalds 2682801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2683801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2684801199ceSEric W. Biederman goto out; 2685801199ceSEric W. Biederman 26861da177e4SLinus Torvalds tgid = name_to_int(dentry); 26871da177e4SLinus Torvalds if (tgid == ~0U) 26881da177e4SLinus Torvalds goto out; 26891da177e4SLinus Torvalds 2690b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2691de758734SEric W. Biederman rcu_read_lock(); 2692b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 26931da177e4SLinus Torvalds if (task) 26941da177e4SLinus Torvalds get_task_struct(task); 2695de758734SEric W. Biederman rcu_read_unlock(); 26961da177e4SLinus Torvalds if (!task) 26971da177e4SLinus Torvalds goto out; 26981da177e4SLinus Torvalds 2699444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 270048e6484dSEric W. Biederman put_task_struct(task); 27011da177e4SLinus Torvalds out: 2702cd6a3ce9SEric W. Biederman return result; 27031da177e4SLinus Torvalds } 27041da177e4SLinus Torvalds 27051da177e4SLinus Torvalds /* 27060804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 27070bc58a91SEric W. Biederman * 27081da177e4SLinus Torvalds */ 270919fd4bb2SEric W. Biederman struct tgid_iter { 271019fd4bb2SEric W. Biederman unsigned int tgid; 27110804ef4bSEric W. Biederman struct task_struct *task; 271219fd4bb2SEric W. Biederman }; 271319fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 271419fd4bb2SEric W. Biederman { 27150804ef4bSEric W. Biederman struct pid *pid; 27161da177e4SLinus Torvalds 271719fd4bb2SEric W. Biederman if (iter.task) 271819fd4bb2SEric W. Biederman put_task_struct(iter.task); 27190804ef4bSEric W. Biederman rcu_read_lock(); 27200804ef4bSEric W. Biederman retry: 272119fd4bb2SEric W. Biederman iter.task = NULL; 272219fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 27230804ef4bSEric W. Biederman if (pid) { 272419fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 272519fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 27260804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 27270804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 27280804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 27290804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 27300804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 27310804ef4bSEric W. Biederman * 27320804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 27330804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 27340804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 27350804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 27360804ef4bSEric W. Biederman * As we don't care in the case of readdir. 27370bc58a91SEric W. Biederman */ 273819fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 273919fd4bb2SEric W. Biederman iter.tgid += 1; 27400804ef4bSEric W. Biederman goto retry; 274119fd4bb2SEric W. Biederman } 274219fd4bb2SEric W. Biederman get_task_struct(iter.task); 27431da177e4SLinus Torvalds } 2744454cc105SEric W. Biederman rcu_read_unlock(); 274519fd4bb2SEric W. Biederman return iter; 27461da177e4SLinus Torvalds } 27471da177e4SLinus Torvalds 27487bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 27491da177e4SLinus Torvalds 275061a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 275119fd4bb2SEric W. Biederman struct tgid_iter iter) 275261a28784SEric W. Biederman { 275361a28784SEric W. Biederman char name[PROC_NUMBUF]; 275419fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 275561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 275619fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 275761a28784SEric W. Biederman } 275861a28784SEric W. Biederman 27591da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 27601da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 27611da177e4SLinus Torvalds { 27621da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 27632fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 276419fd4bb2SEric W. Biederman struct tgid_iter iter; 2765b488893aSPavel Emelyanov struct pid_namespace *ns; 27661da177e4SLinus Torvalds 276761a28784SEric W. Biederman if (!reaper) 276861a28784SEric W. Biederman goto out_no_task; 276961a28784SEric W. Biederman 27707bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2771c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 277261a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2773801199ceSEric W. Biederman goto out; 27741da177e4SLinus Torvalds } 27751da177e4SLinus Torvalds 2776b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 277719fd4bb2SEric W. Biederman iter.task = NULL; 277819fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 277919fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 278019fd4bb2SEric W. Biederman iter.task; 278119fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 278219fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 278319fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 278419fd4bb2SEric W. Biederman put_task_struct(iter.task); 27850804ef4bSEric W. Biederman goto out; 27861da177e4SLinus Torvalds } 27871da177e4SLinus Torvalds } 27880804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 27890804ef4bSEric W. Biederman out: 279061a28784SEric W. Biederman put_task_struct(reaper); 279161a28784SEric W. Biederman out_no_task: 27921da177e4SLinus Torvalds return 0; 27931da177e4SLinus Torvalds } 27941da177e4SLinus Torvalds 27950bc58a91SEric W. Biederman /* 279628a6d671SEric W. Biederman * Tasks 279728a6d671SEric W. Biederman */ 2798c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 279961a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 280027932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2801315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 280261a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2803df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2804d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 280543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 280643ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 280743ae34cbSIngo Molnar #endif 280861a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2809ee992744SEric W. Biederman ONE("stat", S_IRUGO, tid_stat), 2810a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 281161a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 281228a6d671SEric W. Biederman #ifdef CONFIG_NUMA 281361a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 281428a6d671SEric W. Biederman #endif 281561a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 281661a28784SEric W. Biederman LNK("cwd", cwd), 281761a28784SEric W. Biederman LNK("root", root), 281861a28784SEric W. Biederman LNK("exe", exe), 281961a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 28202d4d4864SRam Pai REG("mountinfo", S_IRUGO, mountinfo), 28211e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2822b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 282361a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 282485863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 282528a6d671SEric W. Biederman #endif 282628a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 282772d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 282828a6d671SEric W. Biederman #endif 282928a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 283061a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 283128a6d671SEric W. Biederman #endif 283228a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 283361a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 283428a6d671SEric W. Biederman #endif 28359745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 28369745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 28379745512cSArjan van de Ven #endif 28388793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 283961a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 284028a6d671SEric W. Biederman #endif 2841a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2842a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2843a424316cSPaul Menage #endif 284461a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 284561a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 284628a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 284761a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 28481e0bd755SEric Paris REG("sessionid", S_IRUSR, sessionid), 284928a6d671SEric W. Biederman #endif 2850f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2851f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2852f4f154fdSAkinobu Mita #endif 2853*297c5d92SAndrea Righi #ifdef CONFIG_TASK_IO_ACCOUNTING 2854*297c5d92SAndrea Righi INF("io", S_IRUGO, tid_io_accounting), 2855*297c5d92SAndrea Righi #endif 285628a6d671SEric W. Biederman }; 285728a6d671SEric W. Biederman 285828a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 285928a6d671SEric W. Biederman void * dirent, filldir_t filldir) 286028a6d671SEric W. Biederman { 286128a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 286228a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 286328a6d671SEric W. Biederman } 286428a6d671SEric W. Biederman 286528a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 28667bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 28677bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 286828a6d671SEric W. Biederman } 286928a6d671SEric W. Biederman 287000977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 287128a6d671SEric W. Biederman .read = generic_read_dir, 287228a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 287328a6d671SEric W. Biederman }; 287428a6d671SEric W. Biederman 2875c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 287628a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 287728a6d671SEric W. Biederman .getattr = pid_getattr, 287828a6d671SEric W. Biederman .setattr = proc_setattr, 287928a6d671SEric W. Biederman }; 288028a6d671SEric W. Biederman 2881444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2882c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2883444ceed8SEric W. Biederman { 2884444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2885444ceed8SEric W. Biederman struct inode *inode; 288661a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2887444ceed8SEric W. Biederman 2888444ceed8SEric W. Biederman if (!inode) 2889444ceed8SEric W. Biederman goto out; 2890444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2891444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2892444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2893444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 2894aed54175SVegard Nossum 2895aed54175SVegard Nossum inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff, 2896aed54175SVegard Nossum ARRAY_SIZE(tid_base_stuff)); 2897444ceed8SEric W. Biederman 2898444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2899444ceed8SEric W. Biederman 2900444ceed8SEric W. Biederman d_add(dentry, inode); 2901444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2902444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2903444ceed8SEric W. Biederman error = NULL; 2904444ceed8SEric W. Biederman out: 2905444ceed8SEric W. Biederman return error; 2906444ceed8SEric W. Biederman } 2907444ceed8SEric W. Biederman 290828a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 290928a6d671SEric W. Biederman { 291028a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 291128a6d671SEric W. Biederman struct task_struct *task; 291228a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 291328a6d671SEric W. Biederman unsigned tid; 2914b488893aSPavel Emelyanov struct pid_namespace *ns; 291528a6d671SEric W. Biederman 291628a6d671SEric W. Biederman if (!leader) 291728a6d671SEric W. Biederman goto out_no_task; 291828a6d671SEric W. Biederman 291928a6d671SEric W. Biederman tid = name_to_int(dentry); 292028a6d671SEric W. Biederman if (tid == ~0U) 292128a6d671SEric W. Biederman goto out; 292228a6d671SEric W. Biederman 2923b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 292428a6d671SEric W. Biederman rcu_read_lock(); 2925b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 292628a6d671SEric W. Biederman if (task) 292728a6d671SEric W. Biederman get_task_struct(task); 292828a6d671SEric W. Biederman rcu_read_unlock(); 292928a6d671SEric W. Biederman if (!task) 293028a6d671SEric W. Biederman goto out; 2931bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 293228a6d671SEric W. Biederman goto out_drop_task; 293328a6d671SEric W. Biederman 2934444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 293528a6d671SEric W. Biederman out_drop_task: 293628a6d671SEric W. Biederman put_task_struct(task); 293728a6d671SEric W. Biederman out: 293828a6d671SEric W. Biederman put_task_struct(leader); 293928a6d671SEric W. Biederman out_no_task: 294028a6d671SEric W. Biederman return result; 294128a6d671SEric W. Biederman } 294228a6d671SEric W. Biederman 294328a6d671SEric W. Biederman /* 29440bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 29450bc58a91SEric W. Biederman * 29460bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 29470bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 29480bc58a91SEric W. Biederman * directory we have more work todo. 29490bc58a91SEric W. Biederman * 29500bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 29510bc58a91SEric W. Biederman * 29520bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 29530bc58a91SEric W. Biederman * threads past it. 29540bc58a91SEric W. Biederman */ 2955cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2956b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 29570bc58a91SEric W. Biederman { 2958a872ff0cSOleg Nesterov struct task_struct *pos; 29590bc58a91SEric W. Biederman 2960cc288738SEric W. Biederman rcu_read_lock(); 29610bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 29620bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2963b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2964a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2965a872ff0cSOleg Nesterov goto found; 29660bc58a91SEric W. Biederman } 29670bc58a91SEric W. Biederman 29680bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 29690bc58a91SEric W. Biederman pos = NULL; 2970a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2971a872ff0cSOleg Nesterov goto out; 2972a872ff0cSOleg Nesterov 2973a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2974a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2975a872ff0cSOleg Nesterov */ 2976a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2977a872ff0cSOleg Nesterov pos = next_thread(pos); 2978a872ff0cSOleg Nesterov if (pos == leader) { 2979a872ff0cSOleg Nesterov pos = NULL; 2980a872ff0cSOleg Nesterov goto out; 2981a872ff0cSOleg Nesterov } 2982a872ff0cSOleg Nesterov } 2983a872ff0cSOleg Nesterov found: 2984a872ff0cSOleg Nesterov get_task_struct(pos); 2985a872ff0cSOleg Nesterov out: 2986cc288738SEric W. Biederman rcu_read_unlock(); 29870bc58a91SEric W. Biederman return pos; 29880bc58a91SEric W. Biederman } 29890bc58a91SEric W. Biederman 29900bc58a91SEric W. Biederman /* 29910bc58a91SEric W. Biederman * Find the next thread in the thread list. 29920bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 29930bc58a91SEric W. Biederman * 29940bc58a91SEric W. Biederman * The reference to the input task_struct is released. 29950bc58a91SEric W. Biederman */ 29960bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 29970bc58a91SEric W. Biederman { 2998c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2999cc288738SEric W. Biederman rcu_read_lock(); 3000c1df7fb8SOleg Nesterov if (pid_alive(start)) { 30010bc58a91SEric W. Biederman pos = next_thread(start); 3002c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 30030bc58a91SEric W. Biederman pos = NULL; 3004c1df7fb8SOleg Nesterov else 3005c1df7fb8SOleg Nesterov get_task_struct(pos); 3006c1df7fb8SOleg Nesterov } 3007cc288738SEric W. Biederman rcu_read_unlock(); 30080bc58a91SEric W. Biederman put_task_struct(start); 30090bc58a91SEric W. Biederman return pos; 30100bc58a91SEric W. Biederman } 30110bc58a91SEric W. Biederman 301261a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 301361a28784SEric W. Biederman struct task_struct *task, int tid) 301461a28784SEric W. Biederman { 301561a28784SEric W. Biederman char name[PROC_NUMBUF]; 301661a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 301761a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 301861a28784SEric W. Biederman proc_task_instantiate, task, NULL); 301961a28784SEric W. Biederman } 302061a28784SEric W. Biederman 30211da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 30221da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 30231da177e4SLinus Torvalds { 30242fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 30251da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 30267d895244SGuillaume Chazarain struct task_struct *leader = NULL; 30270bc58a91SEric W. Biederman struct task_struct *task; 30281da177e4SLinus Torvalds int retval = -ENOENT; 30291da177e4SLinus Torvalds ino_t ino; 30300bc58a91SEric W. Biederman int tid; 30311da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 3032b488893aSPavel Emelyanov struct pid_namespace *ns; 30331da177e4SLinus Torvalds 30347d895244SGuillaume Chazarain task = get_proc_task(inode); 30357d895244SGuillaume Chazarain if (!task) 30367d895244SGuillaume Chazarain goto out_no_task; 30377d895244SGuillaume Chazarain rcu_read_lock(); 30387d895244SGuillaume Chazarain if (pid_alive(task)) { 30397d895244SGuillaume Chazarain leader = task->group_leader; 30407d895244SGuillaume Chazarain get_task_struct(leader); 30417d895244SGuillaume Chazarain } 30427d895244SGuillaume Chazarain rcu_read_unlock(); 30437d895244SGuillaume Chazarain put_task_struct(task); 304499f89551SEric W. Biederman if (!leader) 304599f89551SEric W. Biederman goto out_no_task; 30461da177e4SLinus Torvalds retval = 0; 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds switch (pos) { 30491da177e4SLinus Torvalds case 0: 30501da177e4SLinus Torvalds ino = inode->i_ino; 30511da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 30521da177e4SLinus Torvalds goto out; 30531da177e4SLinus Torvalds pos++; 30541da177e4SLinus Torvalds /* fall through */ 30551da177e4SLinus Torvalds case 1: 30561da177e4SLinus Torvalds ino = parent_ino(dentry); 30571da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 30581da177e4SLinus Torvalds goto out; 30591da177e4SLinus Torvalds pos++; 30601da177e4SLinus Torvalds /* fall through */ 30611da177e4SLinus Torvalds } 30621da177e4SLinus Torvalds 30630bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 30640bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 30650bc58a91SEric W. Biederman */ 3066b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 30672b47c361SMathieu Desnoyers tid = (int)filp->f_version; 30680bc58a91SEric W. Biederman filp->f_version = 0; 3069b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 30700bc58a91SEric W. Biederman task; 30710bc58a91SEric W. Biederman task = next_tid(task), pos++) { 3072b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 307361a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 30740bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 30750bc58a91SEric W. Biederman * pid for the next readir call */ 30762b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 30770bc58a91SEric W. Biederman put_task_struct(task); 30781da177e4SLinus Torvalds break; 30790bc58a91SEric W. Biederman } 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds out: 30821da177e4SLinus Torvalds filp->f_pos = pos; 308399f89551SEric W. Biederman put_task_struct(leader); 308499f89551SEric W. Biederman out_no_task: 30851da177e4SLinus Torvalds return retval; 30861da177e4SLinus Torvalds } 30876e66b52bSEric W. Biederman 30886e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 30896e66b52bSEric W. Biederman { 30906e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 309199f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 30926e66b52bSEric W. Biederman generic_fillattr(inode, stat); 30936e66b52bSEric W. Biederman 309499f89551SEric W. Biederman if (p) { 309599f89551SEric W. Biederman rcu_read_lock(); 309699f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 309799f89551SEric W. Biederman rcu_read_unlock(); 309899f89551SEric W. Biederman put_task_struct(p); 30996e66b52bSEric W. Biederman } 31006e66b52bSEric W. Biederman 31016e66b52bSEric W. Biederman return 0; 31026e66b52bSEric W. Biederman } 310328a6d671SEric W. Biederman 3104c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 310528a6d671SEric W. Biederman .lookup = proc_task_lookup, 310628a6d671SEric W. Biederman .getattr = proc_task_getattr, 310728a6d671SEric W. Biederman .setattr = proc_setattr, 310828a6d671SEric W. Biederman }; 310928a6d671SEric W. Biederman 311000977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 311128a6d671SEric W. Biederman .read = generic_read_dir, 311228a6d671SEric W. Biederman .readdir = proc_task_readdir, 311328a6d671SEric W. Biederman }; 3114