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> 591da177e4SLinus Torvalds #include <linux/string.h> 601da177e4SLinus Torvalds #include <linux/seq_file.h> 611da177e4SLinus Torvalds #include <linux/namei.h> 626b3286edSKirill Korotaev #include <linux/mnt_namespace.h> 631da177e4SLinus Torvalds #include <linux/mm.h> 64b835996fSDipankar Sarma #include <linux/rcupdate.h> 651da177e4SLinus Torvalds #include <linux/kallsyms.h> 66d85f50d5SNeil Horman #include <linux/resource.h> 675096add8SKees Cook #include <linux/module.h> 681da177e4SLinus Torvalds #include <linux/mount.h> 691da177e4SLinus Torvalds #include <linux/security.h> 701da177e4SLinus Torvalds #include <linux/ptrace.h> 71a424316cSPaul Menage #include <linux/cgroup.h> 721da177e4SLinus Torvalds #include <linux/cpuset.h> 731da177e4SLinus Torvalds #include <linux/audit.h> 745addc5ddSAl Viro #include <linux/poll.h> 751651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 768ac773b4SAlexey Dobriyan #include <linux/oom.h> 773cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 7860347f67SPavel Emelyanov #include <linux/pid_namespace.h> 791da177e4SLinus Torvalds #include "internal.h" 801da177e4SLinus Torvalds 810f2fe20fSEric W. Biederman /* NOTE: 820f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 830f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 840f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 850f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 860f2fe20fSEric W. Biederman * 870f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 880f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 890f2fe20fSEric W. Biederman */ 900f2fe20fSEric W. Biederman 911da177e4SLinus Torvalds struct pid_entry { 921da177e4SLinus Torvalds char *name; 93c5141e6dSEric Dumazet int len; 941da177e4SLinus Torvalds mode_t mode; 95c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9600977a59SArjan van de Ven const struct file_operations *fop; 9720cdc894SEric W. Biederman union proc_op op; 981da177e4SLinus Torvalds }; 991da177e4SLinus Torvalds 10061a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10120cdc894SEric W. Biederman .name = (NAME), \ 102c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10320cdc894SEric W. Biederman .mode = MODE, \ 10420cdc894SEric W. Biederman .iop = IOP, \ 10520cdc894SEric W. Biederman .fop = FOP, \ 10620cdc894SEric W. Biederman .op = OP, \ 10720cdc894SEric W. Biederman } 10820cdc894SEric W. Biederman 10961a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11061a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11120cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11220cdc894SEric W. Biederman {} ) 11361a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11461a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11520cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11620cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 11761a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 11861a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 11920cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12061a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12161a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12220cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12320cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 124be614086SEric W. Biederman #define ONE(NAME, MODE, OTYPE) \ 125be614086SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 126be614086SEric W. Biederman NULL, &proc_single_file_operations, \ 127be614086SEric W. Biederman { .proc_show = &proc_##OTYPE } ) 1281da177e4SLinus Torvalds 1295096add8SKees Cook int maps_protect; 1305096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1315096add8SKees Cook 1320494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds struct fs_struct *fs; 1350494f6ecSMiklos Szeredi task_lock(task); 1360494f6ecSMiklos Szeredi fs = task->fs; 1371da177e4SLinus Torvalds if(fs) 1381da177e4SLinus Torvalds atomic_inc(&fs->count); 1390494f6ecSMiklos Szeredi task_unlock(task); 1400494f6ecSMiklos Szeredi return fs; 1410494f6ecSMiklos Szeredi } 1420494f6ecSMiklos Szeredi 14399f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 14499f89551SEric W. Biederman { 14599f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 14699f89551SEric W. Biederman unsigned long flags; 14799f89551SEric W. Biederman int count = 0; 14899f89551SEric W. Biederman 14999f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 15099f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 15199f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 15299f89551SEric W. Biederman } 15399f89551SEric W. Biederman return count; 15499f89551SEric W. Biederman } 15599f89551SEric W. Biederman 1563dcd25f3SJan Blunck static int proc_cwd_link(struct inode *inode, struct path *path) 1570494f6ecSMiklos Szeredi { 15899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 15999f89551SEric W. Biederman struct fs_struct *fs = NULL; 1600494f6ecSMiklos Szeredi int result = -ENOENT; 16199f89551SEric W. Biederman 16299f89551SEric W. Biederman if (task) { 16399f89551SEric W. Biederman fs = get_fs_struct(task); 16499f89551SEric W. Biederman put_task_struct(task); 16599f89551SEric W. Biederman } 1661da177e4SLinus Torvalds if (fs) { 1671da177e4SLinus Torvalds read_lock(&fs->lock); 1683dcd25f3SJan Blunck *path = fs->pwd; 1693dcd25f3SJan Blunck path_get(&fs->pwd); 1701da177e4SLinus Torvalds read_unlock(&fs->lock); 1711da177e4SLinus Torvalds result = 0; 1721da177e4SLinus Torvalds put_fs_struct(fs); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds return result; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1773dcd25f3SJan Blunck static int proc_root_link(struct inode *inode, struct path *path) 1781da177e4SLinus Torvalds { 17999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 18099f89551SEric W. Biederman struct fs_struct *fs = NULL; 1811da177e4SLinus Torvalds int result = -ENOENT; 18299f89551SEric W. Biederman 18399f89551SEric W. Biederman if (task) { 18499f89551SEric W. Biederman fs = get_fs_struct(task); 18599f89551SEric W. Biederman put_task_struct(task); 18699f89551SEric W. Biederman } 1871da177e4SLinus Torvalds if (fs) { 1881da177e4SLinus Torvalds read_lock(&fs->lock); 1893dcd25f3SJan Blunck *path = fs->root; 1903dcd25f3SJan Blunck path_get(&fs->root); 1911da177e4SLinus Torvalds read_unlock(&fs->lock); 1921da177e4SLinus Torvalds result = 0; 1931da177e4SLinus Torvalds put_fs_struct(fs); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds return result; 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds #define MAY_PTRACE(task) \ 1991da177e4SLinus Torvalds (task == current || \ 2001da177e4SLinus Torvalds (task->parent == current && \ 2011da177e4SLinus Torvalds (task->ptrace & PT_PTRACED) && \ 2026d8982d9SMatthew Wilcox (task_is_stopped_or_traced(task)) && \ 2031da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 2041da177e4SLinus Torvalds 205831830b5SAl Viro struct mm_struct *mm_for_maps(struct task_struct *task) 206831830b5SAl Viro { 207831830b5SAl Viro struct mm_struct *mm = get_task_mm(task); 208831830b5SAl Viro if (!mm) 209831830b5SAl Viro return NULL; 210831830b5SAl Viro down_read(&mm->mmap_sem); 211831830b5SAl Viro task_lock(task); 212831830b5SAl Viro if (task->mm != mm) 213831830b5SAl Viro goto out; 214831830b5SAl Viro if (task->mm != current->mm && __ptrace_may_attach(task) < 0) 215831830b5SAl Viro goto out; 216831830b5SAl Viro task_unlock(task); 217831830b5SAl Viro return mm; 218831830b5SAl Viro out: 219831830b5SAl Viro task_unlock(task); 220831830b5SAl Viro up_read(&mm->mmap_sem); 221831830b5SAl Viro mmput(mm); 222831830b5SAl Viro return NULL; 223831830b5SAl Viro } 224831830b5SAl Viro 2251da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2261da177e4SLinus Torvalds { 2271da177e4SLinus Torvalds int res = 0; 2281da177e4SLinus Torvalds unsigned int len; 2291da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2301da177e4SLinus Torvalds if (!mm) 2311da177e4SLinus Torvalds goto out; 2321da177e4SLinus Torvalds if (!mm->arg_end) 2331da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds if (len > PAGE_SIZE) 2381da177e4SLinus Torvalds len = PAGE_SIZE; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2431da177e4SLinus Torvalds // assume application is using setproctitle(3). 2441da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2451da177e4SLinus Torvalds len = strnlen(buffer, res); 2461da177e4SLinus Torvalds if (len < res) { 2471da177e4SLinus Torvalds res = len; 2481da177e4SLinus Torvalds } else { 2491da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2501da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2511da177e4SLinus Torvalds len = PAGE_SIZE - res; 2521da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2531da177e4SLinus Torvalds res = strnlen(buffer, res); 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds out_mm: 2571da177e4SLinus Torvalds mmput(mm); 2581da177e4SLinus Torvalds out: 2591da177e4SLinus Torvalds return res; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds int res = 0; 2651da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2661da177e4SLinus Torvalds if (mm) { 2671da177e4SLinus Torvalds unsigned int nwords = 0; 2681da177e4SLinus Torvalds do 2691da177e4SLinus Torvalds nwords += 2; 2701da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2711da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2721da177e4SLinus Torvalds if (res > PAGE_SIZE) 2731da177e4SLinus Torvalds res = PAGE_SIZE; 2741da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2751da177e4SLinus Torvalds mmput(mm); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds return res; 2781da177e4SLinus Torvalds } 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2821da177e4SLinus Torvalds /* 2831da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2841da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2851da177e4SLinus Torvalds */ 2861da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2871da177e4SLinus Torvalds { 288ffb45122SAlexey Dobriyan unsigned long wchan; 2899281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds wchan = get_wchan(task); 2921da177e4SLinus Torvalds 2939d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2941da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2959d65cb4aSAlexey Dobriyan else 2969d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 3011da177e4SLinus Torvalds /* 3021da177e4SLinus Torvalds * Provides /proc/PID/schedstat 3031da177e4SLinus Torvalds */ 3041da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 3051da177e4SLinus Torvalds { 306172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 3071da177e4SLinus Torvalds task->sched_info.cpu_time, 3081da177e4SLinus Torvalds task->sched_info.run_delay, 3092d72376bSIngo Molnar task->sched_info.pcount); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds #endif 3121da177e4SLinus Torvalds 3139745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 3149745512cSArjan van de Ven static int lstats_show_proc(struct seq_file *m, void *v) 3159745512cSArjan van de Ven { 3169745512cSArjan van de Ven int i; 3179745512cSArjan van de Ven struct task_struct *task = m->private; 3189745512cSArjan van de Ven seq_puts(m, "Latency Top version : v0.1\n"); 3199745512cSArjan van de Ven 3209745512cSArjan van de Ven for (i = 0; i < 32; i++) { 3219745512cSArjan van de Ven if (task->latency_record[i].backtrace[0]) { 3229745512cSArjan van de Ven int q; 3239745512cSArjan van de Ven seq_printf(m, "%i %li %li ", 3249745512cSArjan van de Ven task->latency_record[i].count, 3259745512cSArjan van de Ven task->latency_record[i].time, 3269745512cSArjan van de Ven task->latency_record[i].max); 3279745512cSArjan van de Ven for (q = 0; q < LT_BACKTRACEDEPTH; q++) { 3289745512cSArjan van de Ven char sym[KSYM_NAME_LEN]; 3299745512cSArjan van de Ven char *c; 3309745512cSArjan van de Ven if (!task->latency_record[i].backtrace[q]) 3319745512cSArjan van de Ven break; 3329745512cSArjan van de Ven if (task->latency_record[i].backtrace[q] == ULONG_MAX) 3339745512cSArjan van de Ven break; 3349745512cSArjan van de Ven sprint_symbol(sym, task->latency_record[i].backtrace[q]); 3359745512cSArjan van de Ven c = strchr(sym, '+'); 3369745512cSArjan van de Ven if (c) 3379745512cSArjan van de Ven *c = 0; 3389745512cSArjan van de Ven seq_printf(m, "%s ", sym); 3399745512cSArjan van de Ven } 3409745512cSArjan van de Ven seq_printf(m, "\n"); 3419745512cSArjan van de Ven } 3429745512cSArjan van de Ven 3439745512cSArjan van de Ven } 3449745512cSArjan van de Ven return 0; 3459745512cSArjan van de Ven } 3469745512cSArjan van de Ven 3479745512cSArjan van de Ven static int lstats_open(struct inode *inode, struct file *file) 3489745512cSArjan van de Ven { 3499745512cSArjan van de Ven int ret; 3509745512cSArjan van de Ven struct seq_file *m; 3519745512cSArjan van de Ven struct task_struct *task = get_proc_task(inode); 3529745512cSArjan van de Ven 353ae002786SHiroshi Shimamoto if (!task) 354ae002786SHiroshi Shimamoto return -ENOENT; 3559745512cSArjan van de Ven ret = single_open(file, lstats_show_proc, NULL); 3569745512cSArjan van de Ven if (!ret) { 3579745512cSArjan van de Ven m = file->private_data; 3589745512cSArjan van de Ven m->private = task; 3599745512cSArjan van de Ven } 3609745512cSArjan van de Ven return ret; 3619745512cSArjan van de Ven } 3629745512cSArjan van de Ven 363*d6643d12SHiroshi Shimamoto static int lstats_release(struct inode *inode, struct file *file) 364*d6643d12SHiroshi Shimamoto { 365*d6643d12SHiroshi Shimamoto struct seq_file *m = file->private_data; 366*d6643d12SHiroshi Shimamoto struct task_struct *task = m->private; 367*d6643d12SHiroshi Shimamoto 368*d6643d12SHiroshi Shimamoto put_task_struct(task); 369*d6643d12SHiroshi Shimamoto return single_release(inode, file); 370*d6643d12SHiroshi Shimamoto } 371*d6643d12SHiroshi Shimamoto 3729745512cSArjan van de Ven static ssize_t lstats_write(struct file *file, const char __user *buf, 3739745512cSArjan van de Ven size_t count, loff_t *offs) 3749745512cSArjan van de Ven { 3759745512cSArjan van de Ven struct seq_file *m; 3769745512cSArjan van de Ven struct task_struct *task; 3779745512cSArjan van de Ven 3789745512cSArjan van de Ven m = file->private_data; 3799745512cSArjan van de Ven task = m->private; 3809745512cSArjan van de Ven clear_all_latency_tracing(task); 3819745512cSArjan van de Ven 3829745512cSArjan van de Ven return count; 3839745512cSArjan van de Ven } 3849745512cSArjan van de Ven 3859745512cSArjan van de Ven static const struct file_operations proc_lstats_operations = { 3869745512cSArjan van de Ven .open = lstats_open, 3879745512cSArjan van de Ven .read = seq_read, 3889745512cSArjan van de Ven .write = lstats_write, 3899745512cSArjan van de Ven .llseek = seq_lseek, 390*d6643d12SHiroshi Shimamoto .release = lstats_release, 3919745512cSArjan van de Ven }; 3929745512cSArjan van de Ven 3939745512cSArjan van de Ven #endif 3949745512cSArjan van de Ven 3951da177e4SLinus Torvalds /* The badness from the OOM killer */ 3961da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 3971da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 3981da177e4SLinus Torvalds { 3991da177e4SLinus Torvalds unsigned long points; 4001da177e4SLinus Torvalds struct timespec uptime; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 40319c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 4041da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 40519c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 4061da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 4071da177e4SLinus Torvalds } 4081da177e4SLinus Torvalds 409d85f50d5SNeil Horman struct limit_names { 410d85f50d5SNeil Horman char *name; 411d85f50d5SNeil Horman char *unit; 412d85f50d5SNeil Horman }; 413d85f50d5SNeil Horman 414d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 415d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 416d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 417d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 418d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 419d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 420d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 421d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 422d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 423d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 424d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 425d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 426d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 427d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 428d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 429d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 4308808117cSEugene Teo [RLIMIT_RTTIME] = {"Max realtime timeout", "us"}, 431d85f50d5SNeil Horman }; 432d85f50d5SNeil Horman 433d85f50d5SNeil Horman /* Display limits for a process */ 434d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 435d85f50d5SNeil Horman { 436d85f50d5SNeil Horman unsigned int i; 437d85f50d5SNeil Horman int count = 0; 438d85f50d5SNeil Horman unsigned long flags; 439d85f50d5SNeil Horman char *bufptr = buffer; 440d85f50d5SNeil Horman 441d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 442d85f50d5SNeil Horman 443d85f50d5SNeil Horman rcu_read_lock(); 444d85f50d5SNeil Horman if (!lock_task_sighand(task,&flags)) { 445d85f50d5SNeil Horman rcu_read_unlock(); 446d85f50d5SNeil Horman return 0; 447d85f50d5SNeil Horman } 448d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 449d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 450d85f50d5SNeil Horman rcu_read_unlock(); 451d85f50d5SNeil Horman 452d85f50d5SNeil Horman /* 453d85f50d5SNeil Horman * print the file header 454d85f50d5SNeil Horman */ 455d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 456d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 457d85f50d5SNeil Horman 458d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 459d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 460d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 461d85f50d5SNeil Horman lnames[i].name, "unlimited"); 462d85f50d5SNeil Horman else 463d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 464d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 465d85f50d5SNeil Horman 466d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 467d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 468d85f50d5SNeil Horman else 469d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 470d85f50d5SNeil Horman rlim[i].rlim_max); 471d85f50d5SNeil Horman 472d85f50d5SNeil Horman if (lnames[i].unit) 473d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 474d85f50d5SNeil Horman lnames[i].unit); 475d85f50d5SNeil Horman else 476d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 477d85f50d5SNeil Horman } 478d85f50d5SNeil Horman 479d85f50d5SNeil Horman return count; 480d85f50d5SNeil Horman } 481d85f50d5SNeil Horman 4821da177e4SLinus Torvalds /************************************************************************/ 4831da177e4SLinus Torvalds /* Here the fs part begins */ 4841da177e4SLinus Torvalds /************************************************************************/ 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds /* permission checks */ 487778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 4881da177e4SLinus Torvalds { 489778c1144SEric W. Biederman struct task_struct *task; 490778c1144SEric W. Biederman int allowed = 0; 491df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 492df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 493df26c40eSEric W. Biederman * information. 494778c1144SEric W. Biederman */ 495778c1144SEric W. Biederman task = get_proc_task(inode); 496df26c40eSEric W. Biederman if (task) { 497778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 498778c1144SEric W. Biederman put_task_struct(task); 499df26c40eSEric W. Biederman } 500778c1144SEric W. Biederman return allowed; 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds 5036d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 5046d76fa58SLinus Torvalds { 5056d76fa58SLinus Torvalds int error; 5066d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 5076d76fa58SLinus Torvalds 5086d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 5096d76fa58SLinus Torvalds return -EPERM; 5106d76fa58SLinus Torvalds 5116d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 5126d76fa58SLinus Torvalds if (!error) 5136d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 5146d76fa58SLinus Torvalds return error; 5156d76fa58SLinus Torvalds } 5166d76fa58SLinus Torvalds 517c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 5186d76fa58SLinus Torvalds .setattr = proc_setattr, 5196d76fa58SLinus Torvalds }; 5206d76fa58SLinus Torvalds 52103a44825SJan Engelhardt extern const struct seq_operations mounts_op; 5225addc5ddSAl Viro struct proc_mounts { 5235addc5ddSAl Viro struct seq_file m; 5245addc5ddSAl Viro int event; 5255addc5ddSAl Viro }; 5265addc5ddSAl Viro 5271da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 5281da177e4SLinus Torvalds { 52999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 530cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5316b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 5325addc5ddSAl Viro struct proc_mounts *p; 5335addc5ddSAl Viro int ret = -EINVAL; 5345addc5ddSAl Viro 53599f89551SEric W. Biederman if (task) { 536cf7b708cSPavel Emelyanov rcu_read_lock(); 537cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 538cf7b708cSPavel Emelyanov if (nsp) { 539cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 5406b3286edSKirill Korotaev if (ns) 5416b3286edSKirill Korotaev get_mnt_ns(ns); 542863c4702SAlexey Dobriyan } 543cf7b708cSPavel Emelyanov rcu_read_unlock(); 544cf7b708cSPavel Emelyanov 54599f89551SEric W. Biederman put_task_struct(task); 54699f89551SEric W. Biederman } 5471da177e4SLinus Torvalds 5486b3286edSKirill Korotaev if (ns) { 5495addc5ddSAl Viro ret = -ENOMEM; 5505addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 5515addc5ddSAl Viro if (p) { 5525addc5ddSAl Viro file->private_data = &p->m; 5535addc5ddSAl Viro ret = seq_open(file, &mounts_op); 5545addc5ddSAl Viro if (!ret) { 5556b3286edSKirill Korotaev p->m.private = ns; 5566b3286edSKirill Korotaev p->event = ns->event; 5575addc5ddSAl Viro return 0; 5581da177e4SLinus Torvalds } 5595addc5ddSAl Viro kfree(p); 5605addc5ddSAl Viro } 5616b3286edSKirill Korotaev put_mnt_ns(ns); 5621da177e4SLinus Torvalds } 5631da177e4SLinus Torvalds return ret; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds struct seq_file *m = file->private_data; 5696b3286edSKirill Korotaev struct mnt_namespace *ns = m->private; 5706b3286edSKirill Korotaev put_mnt_ns(ns); 5711da177e4SLinus Torvalds return seq_release(inode, file); 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 5745addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 5755addc5ddSAl Viro { 5765addc5ddSAl Viro struct proc_mounts *p = file->private_data; 5776b3286edSKirill Korotaev struct mnt_namespace *ns = p->m.private; 5785addc5ddSAl Viro unsigned res = 0; 5795addc5ddSAl Viro 5805addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 5815addc5ddSAl Viro 5825addc5ddSAl Viro spin_lock(&vfsmount_lock); 5835addc5ddSAl Viro if (p->event != ns->event) { 5845addc5ddSAl Viro p->event = ns->event; 5855addc5ddSAl Viro res = POLLERR; 5865addc5ddSAl Viro } 5875addc5ddSAl Viro spin_unlock(&vfsmount_lock); 5885addc5ddSAl Viro 5895addc5ddSAl Viro return res; 5905addc5ddSAl Viro } 5915addc5ddSAl Viro 59200977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 5931da177e4SLinus Torvalds .open = mounts_open, 5941da177e4SLinus Torvalds .read = seq_read, 5951da177e4SLinus Torvalds .llseek = seq_lseek, 5961da177e4SLinus Torvalds .release = mounts_release, 5975addc5ddSAl Viro .poll = mounts_poll, 5981da177e4SLinus Torvalds }; 5991da177e4SLinus Torvalds 60003a44825SJan Engelhardt extern const struct seq_operations mountstats_op; 601b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 602b4629fe2SChuck Lever { 603b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 604b4629fe2SChuck Lever 605b4629fe2SChuck Lever if (!ret) { 606b4629fe2SChuck Lever struct seq_file *m = file->private_data; 607cf7b708cSPavel Emelyanov struct nsproxy *nsp; 6086b3286edSKirill Korotaev struct mnt_namespace *mnt_ns = NULL; 60999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 61099f89551SEric W. Biederman 61199f89551SEric W. Biederman if (task) { 612cf7b708cSPavel Emelyanov rcu_read_lock(); 613cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 614cf7b708cSPavel Emelyanov if (nsp) { 615cf7b708cSPavel Emelyanov mnt_ns = nsp->mnt_ns; 6166b3286edSKirill Korotaev if (mnt_ns) 6176b3286edSKirill Korotaev get_mnt_ns(mnt_ns); 618cf7b708cSPavel Emelyanov } 619cf7b708cSPavel Emelyanov rcu_read_unlock(); 620cf7b708cSPavel Emelyanov 62199f89551SEric W. Biederman put_task_struct(task); 62299f89551SEric W. Biederman } 623b4629fe2SChuck Lever 6246b3286edSKirill Korotaev if (mnt_ns) 6256b3286edSKirill Korotaev m->private = mnt_ns; 626b4629fe2SChuck Lever else { 627b4629fe2SChuck Lever seq_release(inode, file); 628b4629fe2SChuck Lever ret = -EINVAL; 629b4629fe2SChuck Lever } 630b4629fe2SChuck Lever } 631b4629fe2SChuck Lever return ret; 632b4629fe2SChuck Lever } 633b4629fe2SChuck Lever 63400977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 635b4629fe2SChuck Lever .open = mountstats_open, 636b4629fe2SChuck Lever .read = seq_read, 637b4629fe2SChuck Lever .llseek = seq_lseek, 638b4629fe2SChuck Lever .release = mounts_release, 639b4629fe2SChuck Lever }; 640b4629fe2SChuck Lever 6411da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 6421da177e4SLinus Torvalds 6431da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 6441da177e4SLinus Torvalds size_t count, loff_t *ppos) 6451da177e4SLinus Torvalds { 6462fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 6471da177e4SLinus Torvalds unsigned long page; 6481da177e4SLinus Torvalds ssize_t length; 64999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 65099f89551SEric W. Biederman 65199f89551SEric W. Biederman length = -ESRCH; 65299f89551SEric W. Biederman if (!task) 65399f89551SEric W. Biederman goto out_no_task; 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 6561da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 65799f89551SEric W. Biederman 65899f89551SEric W. Biederman length = -ENOMEM; 659e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 66099f89551SEric W. Biederman goto out; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds if (length >= 0) 6651da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 6661da177e4SLinus Torvalds free_page(page); 66799f89551SEric W. Biederman out: 66899f89551SEric W. Biederman put_task_struct(task); 66999f89551SEric W. Biederman out_no_task: 6701da177e4SLinus Torvalds return length; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 67300977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 6741da177e4SLinus Torvalds .read = proc_info_read, 6751da177e4SLinus Torvalds }; 6761da177e4SLinus Torvalds 677be614086SEric W. Biederman static int proc_single_show(struct seq_file *m, void *v) 678be614086SEric W. Biederman { 679be614086SEric W. Biederman struct inode *inode = m->private; 680be614086SEric W. Biederman struct pid_namespace *ns; 681be614086SEric W. Biederman struct pid *pid; 682be614086SEric W. Biederman struct task_struct *task; 683be614086SEric W. Biederman int ret; 684be614086SEric W. Biederman 685be614086SEric W. Biederman ns = inode->i_sb->s_fs_info; 686be614086SEric W. Biederman pid = proc_pid(inode); 687be614086SEric W. Biederman task = get_pid_task(pid, PIDTYPE_PID); 688be614086SEric W. Biederman if (!task) 689be614086SEric W. Biederman return -ESRCH; 690be614086SEric W. Biederman 691be614086SEric W. Biederman ret = PROC_I(inode)->op.proc_show(m, ns, pid, task); 692be614086SEric W. Biederman 693be614086SEric W. Biederman put_task_struct(task); 694be614086SEric W. Biederman return ret; 695be614086SEric W. Biederman } 696be614086SEric W. Biederman 697be614086SEric W. Biederman static int proc_single_open(struct inode *inode, struct file *filp) 698be614086SEric W. Biederman { 699be614086SEric W. Biederman int ret; 700be614086SEric W. Biederman ret = single_open(filp, proc_single_show, NULL); 701be614086SEric W. Biederman if (!ret) { 702be614086SEric W. Biederman struct seq_file *m = filp->private_data; 703be614086SEric W. Biederman 704be614086SEric W. Biederman m->private = inode; 705be614086SEric W. Biederman } 706be614086SEric W. Biederman return ret; 707be614086SEric W. Biederman } 708be614086SEric W. Biederman 709be614086SEric W. Biederman static const struct file_operations proc_single_file_operations = { 710be614086SEric W. Biederman .open = proc_single_open, 711be614086SEric W. Biederman .read = seq_read, 712be614086SEric W. Biederman .llseek = seq_lseek, 713be614086SEric W. Biederman .release = single_release, 714be614086SEric W. Biederman }; 715be614086SEric W. Biederman 7161da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 7171da177e4SLinus Torvalds { 7181da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 7191da177e4SLinus Torvalds return 0; 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 7231da177e4SLinus Torvalds size_t count, loff_t *ppos) 7241da177e4SLinus Torvalds { 7252fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7261da177e4SLinus Torvalds char *page; 7271da177e4SLinus Torvalds unsigned long src = *ppos; 7281da177e4SLinus Torvalds int ret = -ESRCH; 7291da177e4SLinus Torvalds struct mm_struct *mm; 7301da177e4SLinus Torvalds 73199f89551SEric W. Biederman if (!task) 73299f89551SEric W. Biederman goto out_no_task; 73399f89551SEric W. Biederman 734ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 7351da177e4SLinus Torvalds goto out; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds ret = -ENOMEM; 738e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 7391da177e4SLinus Torvalds if (!page) 7401da177e4SLinus Torvalds goto out; 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds ret = 0; 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds mm = get_task_mm(task); 7451da177e4SLinus Torvalds if (!mm) 7461da177e4SLinus Torvalds goto out_free; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds ret = -EIO; 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 7511da177e4SLinus Torvalds goto out_put; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds ret = 0; 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds while (count > 0) { 7561da177e4SLinus Torvalds int this_len, retval; 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 7591da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 760ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 7611da177e4SLinus Torvalds if (!ret) 7621da177e4SLinus Torvalds ret = -EIO; 7631da177e4SLinus Torvalds break; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 7671da177e4SLinus Torvalds ret = -EFAULT; 7681da177e4SLinus Torvalds break; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds ret += retval; 7721da177e4SLinus Torvalds src += retval; 7731da177e4SLinus Torvalds buf += retval; 7741da177e4SLinus Torvalds count -= retval; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds *ppos = src; 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds out_put: 7791da177e4SLinus Torvalds mmput(mm); 7801da177e4SLinus Torvalds out_free: 7811da177e4SLinus Torvalds free_page((unsigned long) page); 7821da177e4SLinus Torvalds out: 78399f89551SEric W. Biederman put_task_struct(task); 78499f89551SEric W. Biederman out_no_task: 7851da177e4SLinus Torvalds return ret; 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds #define mem_write NULL 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds #ifndef mem_write 7911da177e4SLinus Torvalds /* This is a security hazard */ 79263967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 7931da177e4SLinus Torvalds size_t count, loff_t *ppos) 7941da177e4SLinus Torvalds { 795f7ca54f4SFrederik Deweerdt int copied; 7961da177e4SLinus Torvalds char *page; 7972fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7981da177e4SLinus Torvalds unsigned long dst = *ppos; 7991da177e4SLinus Torvalds 80099f89551SEric W. Biederman copied = -ESRCH; 80199f89551SEric W. Biederman if (!task) 80299f89551SEric W. Biederman goto out_no_task; 8031da177e4SLinus Torvalds 80499f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 80599f89551SEric W. Biederman goto out; 80699f89551SEric W. Biederman 80799f89551SEric W. Biederman copied = -ENOMEM; 808e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 8091da177e4SLinus Torvalds if (!page) 81099f89551SEric W. Biederman goto out; 8111da177e4SLinus Torvalds 812f7ca54f4SFrederik Deweerdt copied = 0; 8131da177e4SLinus Torvalds while (count > 0) { 8141da177e4SLinus Torvalds int this_len, retval; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 8171da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 8181da177e4SLinus Torvalds copied = -EFAULT; 8191da177e4SLinus Torvalds break; 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 8221da177e4SLinus Torvalds if (!retval) { 8231da177e4SLinus Torvalds if (!copied) 8241da177e4SLinus Torvalds copied = -EIO; 8251da177e4SLinus Torvalds break; 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds copied += retval; 8281da177e4SLinus Torvalds buf += retval; 8291da177e4SLinus Torvalds dst += retval; 8301da177e4SLinus Torvalds count -= retval; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds *ppos = dst; 8331da177e4SLinus Torvalds free_page((unsigned long) page); 83499f89551SEric W. Biederman out: 83599f89551SEric W. Biederman put_task_struct(task); 83699f89551SEric W. Biederman out_no_task: 8371da177e4SLinus Torvalds return copied; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds #endif 8401da177e4SLinus Torvalds 84185863e47SMatt Mackall loff_t mem_lseek(struct file *file, loff_t offset, int orig) 8421da177e4SLinus Torvalds { 8431da177e4SLinus Torvalds switch (orig) { 8441da177e4SLinus Torvalds case 0: 8451da177e4SLinus Torvalds file->f_pos = offset; 8461da177e4SLinus Torvalds break; 8471da177e4SLinus Torvalds case 1: 8481da177e4SLinus Torvalds file->f_pos += offset; 8491da177e4SLinus Torvalds break; 8501da177e4SLinus Torvalds default: 8511da177e4SLinus Torvalds return -EINVAL; 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds force_successful_syscall_return(); 8541da177e4SLinus Torvalds return file->f_pos; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 85700977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 8581da177e4SLinus Torvalds .llseek = mem_lseek, 8591da177e4SLinus Torvalds .read = mem_read, 8601da177e4SLinus Torvalds .write = mem_write, 8611da177e4SLinus Torvalds .open = mem_open, 8621da177e4SLinus Torvalds }; 8631da177e4SLinus Torvalds 864315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 865315e28c8SJames Pearson size_t count, loff_t *ppos) 866315e28c8SJames Pearson { 867315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 868315e28c8SJames Pearson char *page; 869315e28c8SJames Pearson unsigned long src = *ppos; 870315e28c8SJames Pearson int ret = -ESRCH; 871315e28c8SJames Pearson struct mm_struct *mm; 872315e28c8SJames Pearson 873315e28c8SJames Pearson if (!task) 874315e28c8SJames Pearson goto out_no_task; 875315e28c8SJames Pearson 876315e28c8SJames Pearson if (!ptrace_may_attach(task)) 877315e28c8SJames Pearson goto out; 878315e28c8SJames Pearson 879315e28c8SJames Pearson ret = -ENOMEM; 880315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 881315e28c8SJames Pearson if (!page) 882315e28c8SJames Pearson goto out; 883315e28c8SJames Pearson 884315e28c8SJames Pearson ret = 0; 885315e28c8SJames Pearson 886315e28c8SJames Pearson mm = get_task_mm(task); 887315e28c8SJames Pearson if (!mm) 888315e28c8SJames Pearson goto out_free; 889315e28c8SJames Pearson 890315e28c8SJames Pearson while (count > 0) { 891315e28c8SJames Pearson int this_len, retval, max_len; 892315e28c8SJames Pearson 893315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 894315e28c8SJames Pearson 895315e28c8SJames Pearson if (this_len <= 0) 896315e28c8SJames Pearson break; 897315e28c8SJames Pearson 898315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 899315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 900315e28c8SJames Pearson 901315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 902315e28c8SJames Pearson page, this_len, 0); 903315e28c8SJames Pearson 904315e28c8SJames Pearson if (retval <= 0) { 905315e28c8SJames Pearson ret = retval; 906315e28c8SJames Pearson break; 907315e28c8SJames Pearson } 908315e28c8SJames Pearson 909315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 910315e28c8SJames Pearson ret = -EFAULT; 911315e28c8SJames Pearson break; 912315e28c8SJames Pearson } 913315e28c8SJames Pearson 914315e28c8SJames Pearson ret += retval; 915315e28c8SJames Pearson src += retval; 916315e28c8SJames Pearson buf += retval; 917315e28c8SJames Pearson count -= retval; 918315e28c8SJames Pearson } 919315e28c8SJames Pearson *ppos = src; 920315e28c8SJames Pearson 921315e28c8SJames Pearson mmput(mm); 922315e28c8SJames Pearson out_free: 923315e28c8SJames Pearson free_page((unsigned long) page); 924315e28c8SJames Pearson out: 925315e28c8SJames Pearson put_task_struct(task); 926315e28c8SJames Pearson out_no_task: 927315e28c8SJames Pearson return ret; 928315e28c8SJames Pearson } 929315e28c8SJames Pearson 930315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 931315e28c8SJames Pearson .read = environ_read, 932315e28c8SJames Pearson }; 933315e28c8SJames Pearson 9341da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 9351da177e4SLinus Torvalds size_t count, loff_t *ppos) 9361da177e4SLinus Torvalds { 9372fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 9388578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 9391da177e4SLinus Torvalds size_t len; 94099f89551SEric W. Biederman int oom_adjust; 9411da177e4SLinus Torvalds 94299f89551SEric W. Biederman if (!task) 94399f89551SEric W. Biederman return -ESRCH; 94499f89551SEric W. Biederman oom_adjust = task->oomkilladj; 94599f89551SEric W. Biederman put_task_struct(task); 94699f89551SEric W. Biederman 9478578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 9480c28f287SAkinobu Mita 9490c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 9531da177e4SLinus Torvalds size_t count, loff_t *ppos) 9541da177e4SLinus Torvalds { 95599f89551SEric W. Biederman struct task_struct *task; 9568578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 9571da177e4SLinus Torvalds int oom_adjust; 9581da177e4SLinus Torvalds 9598578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 9608578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 9618578cea7SEric W. Biederman count = sizeof(buffer) - 1; 9621da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 9631da177e4SLinus Torvalds return -EFAULT; 9641da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 9658ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 9668ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 9671da177e4SLinus Torvalds return -EINVAL; 9681da177e4SLinus Torvalds if (*end == '\n') 9691da177e4SLinus Torvalds end++; 9702fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 97199f89551SEric W. Biederman if (!task) 97299f89551SEric W. Biederman return -ESRCH; 9738fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 9748fb4fc68SGuillem Jover put_task_struct(task); 9758fb4fc68SGuillem Jover return -EACCES; 9768fb4fc68SGuillem Jover } 9771da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 97899f89551SEric W. Biederman put_task_struct(task); 9791da177e4SLinus Torvalds if (end - buffer == 0) 9801da177e4SLinus Torvalds return -EIO; 9811da177e4SLinus Torvalds return end - buffer; 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds 98400977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 9851da177e4SLinus Torvalds .read = oom_adjust_read, 9861da177e4SLinus Torvalds .write = oom_adjust_write, 9871da177e4SLinus Torvalds }; 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 9901da177e4SLinus Torvalds #define TMPBUFLEN 21 9911da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 9921da177e4SLinus Torvalds size_t count, loff_t *ppos) 9931da177e4SLinus Torvalds { 9942fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 99599f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 9961da177e4SLinus Torvalds ssize_t length; 9971da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 9981da177e4SLinus Torvalds 99999f89551SEric W. Biederman if (!task) 100099f89551SEric W. Biederman return -ESRCH; 10011da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 10020c11b942SAl Viro audit_get_loginuid(task)); 100399f89551SEric W. Biederman put_task_struct(task); 10041da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 10081da177e4SLinus Torvalds size_t count, loff_t *ppos) 10091da177e4SLinus Torvalds { 10102fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 10111da177e4SLinus Torvalds char *page, *tmp; 10121da177e4SLinus Torvalds ssize_t length; 10131da177e4SLinus Torvalds uid_t loginuid; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 10161da177e4SLinus Torvalds return -EPERM; 10171da177e4SLinus Torvalds 101813b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 10191da177e4SLinus Torvalds return -EPERM; 10201da177e4SLinus Torvalds 1021e0182909SAl Viro if (count >= PAGE_SIZE) 1022e0182909SAl Viro count = PAGE_SIZE - 1; 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds if (*ppos != 0) { 10251da177e4SLinus Torvalds /* No partial writes. */ 10261da177e4SLinus Torvalds return -EINVAL; 10271da177e4SLinus Torvalds } 1028e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 10291da177e4SLinus Torvalds if (!page) 10301da177e4SLinus Torvalds return -ENOMEM; 10311da177e4SLinus Torvalds length = -EFAULT; 10321da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 10331da177e4SLinus Torvalds goto out_free_page; 10341da177e4SLinus Torvalds 1035e0182909SAl Viro page[count] = '\0'; 10361da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 10371da177e4SLinus Torvalds if (tmp == page) { 10381da177e4SLinus Torvalds length = -EINVAL; 10391da177e4SLinus Torvalds goto out_free_page; 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds } 104299f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 10431da177e4SLinus Torvalds if (likely(length == 0)) 10441da177e4SLinus Torvalds length = count; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds out_free_page: 10471da177e4SLinus Torvalds free_page((unsigned long) page); 10481da177e4SLinus Torvalds return length; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 105100977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 10521da177e4SLinus Torvalds .read = proc_loginuid_read, 10531da177e4SLinus Torvalds .write = proc_loginuid_write, 10541da177e4SLinus Torvalds }; 10551da177e4SLinus Torvalds #endif 10561da177e4SLinus Torvalds 1057f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 1058f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 1059f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 1060f4f154fdSAkinobu Mita { 1061f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 1062f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 1063f4f154fdSAkinobu Mita size_t len; 1064f4f154fdSAkinobu Mita int make_it_fail; 1065f4f154fdSAkinobu Mita 1066f4f154fdSAkinobu Mita if (!task) 1067f4f154fdSAkinobu Mita return -ESRCH; 1068f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 1069f4f154fdSAkinobu Mita put_task_struct(task); 1070f4f154fdSAkinobu Mita 1071f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 10720c28f287SAkinobu Mita 10730c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 1074f4f154fdSAkinobu Mita } 1075f4f154fdSAkinobu Mita 1076f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 1077f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 1078f4f154fdSAkinobu Mita { 1079f4f154fdSAkinobu Mita struct task_struct *task; 1080f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 1081f4f154fdSAkinobu Mita int make_it_fail; 1082f4f154fdSAkinobu Mita 1083f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 1084f4f154fdSAkinobu Mita return -EPERM; 1085f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 1086f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 1087f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 1088f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 1089f4f154fdSAkinobu Mita return -EFAULT; 1090f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 1091f4f154fdSAkinobu Mita if (*end == '\n') 1092f4f154fdSAkinobu Mita end++; 1093f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 1094f4f154fdSAkinobu Mita if (!task) 1095f4f154fdSAkinobu Mita return -ESRCH; 1096f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 1097f4f154fdSAkinobu Mita put_task_struct(task); 1098f4f154fdSAkinobu Mita if (end - buffer == 0) 1099f4f154fdSAkinobu Mita return -EIO; 1100f4f154fdSAkinobu Mita return end - buffer; 1101f4f154fdSAkinobu Mita } 1102f4f154fdSAkinobu Mita 110300977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 1104f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 1105f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1106f4f154fdSAkinobu Mita }; 1107f4f154fdSAkinobu Mita #endif 1108f4f154fdSAkinobu Mita 11099745512cSArjan van de Ven 111043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 111143ae34cbSIngo Molnar /* 111243ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 111343ae34cbSIngo Molnar */ 111443ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 111543ae34cbSIngo Molnar { 111643ae34cbSIngo Molnar struct inode *inode = m->private; 111743ae34cbSIngo Molnar struct task_struct *p; 111843ae34cbSIngo Molnar 111943ae34cbSIngo Molnar WARN_ON(!inode); 112043ae34cbSIngo Molnar 112143ae34cbSIngo Molnar p = get_proc_task(inode); 112243ae34cbSIngo Molnar if (!p) 112343ae34cbSIngo Molnar return -ESRCH; 112443ae34cbSIngo Molnar proc_sched_show_task(p, m); 112543ae34cbSIngo Molnar 112643ae34cbSIngo Molnar put_task_struct(p); 112743ae34cbSIngo Molnar 112843ae34cbSIngo Molnar return 0; 112943ae34cbSIngo Molnar } 113043ae34cbSIngo Molnar 113143ae34cbSIngo Molnar static ssize_t 113243ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 113343ae34cbSIngo Molnar size_t count, loff_t *offset) 113443ae34cbSIngo Molnar { 113543ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 113643ae34cbSIngo Molnar struct task_struct *p; 113743ae34cbSIngo Molnar 113843ae34cbSIngo Molnar WARN_ON(!inode); 113943ae34cbSIngo Molnar 114043ae34cbSIngo Molnar p = get_proc_task(inode); 114143ae34cbSIngo Molnar if (!p) 114243ae34cbSIngo Molnar return -ESRCH; 114343ae34cbSIngo Molnar proc_sched_set_task(p); 114443ae34cbSIngo Molnar 114543ae34cbSIngo Molnar put_task_struct(p); 114643ae34cbSIngo Molnar 114743ae34cbSIngo Molnar return count; 114843ae34cbSIngo Molnar } 114943ae34cbSIngo Molnar 115043ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 115143ae34cbSIngo Molnar { 115243ae34cbSIngo Molnar int ret; 115343ae34cbSIngo Molnar 115443ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 115543ae34cbSIngo Molnar if (!ret) { 115643ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 115743ae34cbSIngo Molnar 115843ae34cbSIngo Molnar m->private = inode; 115943ae34cbSIngo Molnar } 116043ae34cbSIngo Molnar return ret; 116143ae34cbSIngo Molnar } 116243ae34cbSIngo Molnar 116343ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 116443ae34cbSIngo Molnar .open = sched_open, 116543ae34cbSIngo Molnar .read = seq_read, 116643ae34cbSIngo Molnar .write = sched_write, 116743ae34cbSIngo Molnar .llseek = seq_lseek, 11685ea473a1SAlexey Dobriyan .release = single_release, 116943ae34cbSIngo Molnar }; 117043ae34cbSIngo Molnar 117143ae34cbSIngo Molnar #endif 117243ae34cbSIngo Molnar 1173008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 11741da177e4SLinus Torvalds { 11751da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11761da177e4SLinus Torvalds int error = -EACCES; 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 11791d957f9bSJan Blunck path_put(&nd->path); 11801da177e4SLinus Torvalds 1181778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1182778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 11831da177e4SLinus Torvalds goto out; 11841da177e4SLinus Torvalds 11853dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &nd->path); 11861da177e4SLinus Torvalds nd->last_type = LAST_BIND; 11871da177e4SLinus Torvalds out: 1188008b150aSAl Viro return ERR_PTR(error); 11891da177e4SLinus Torvalds } 11901da177e4SLinus Torvalds 11913dcd25f3SJan Blunck static int do_proc_readlink(struct path *path, char __user *buffer, int buflen) 11921da177e4SLinus Torvalds { 1193e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 11943dcd25f3SJan Blunck char *pathname; 11951da177e4SLinus Torvalds int len; 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds if (!tmp) 11981da177e4SLinus Torvalds return -ENOMEM; 11991da177e4SLinus Torvalds 1200cf28b486SJan Blunck pathname = d_path(path, tmp, PAGE_SIZE); 12013dcd25f3SJan Blunck len = PTR_ERR(pathname); 12023dcd25f3SJan Blunck if (IS_ERR(pathname)) 12031da177e4SLinus Torvalds goto out; 12043dcd25f3SJan Blunck len = tmp + PAGE_SIZE - 1 - pathname; 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds if (len > buflen) 12071da177e4SLinus Torvalds len = buflen; 12083dcd25f3SJan Blunck if (copy_to_user(buffer, pathname, len)) 12091da177e4SLinus Torvalds len = -EFAULT; 12101da177e4SLinus Torvalds out: 12111da177e4SLinus Torvalds free_page((unsigned long)tmp); 12121da177e4SLinus Torvalds return len; 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 12161da177e4SLinus Torvalds { 12171da177e4SLinus Torvalds int error = -EACCES; 12181da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 12193dcd25f3SJan Blunck struct path path; 12201da177e4SLinus Torvalds 1221778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1222778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 12231da177e4SLinus Torvalds goto out; 12241da177e4SLinus Torvalds 12253dcd25f3SJan Blunck error = PROC_I(inode)->op.proc_get_link(inode, &path); 12261da177e4SLinus Torvalds if (error) 12271da177e4SLinus Torvalds goto out; 12281da177e4SLinus Torvalds 12293dcd25f3SJan Blunck error = do_proc_readlink(&path, buffer, buflen); 12303dcd25f3SJan Blunck path_put(&path); 12311da177e4SLinus Torvalds out: 12321da177e4SLinus Torvalds return error; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 1235c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 12361da177e4SLinus Torvalds .readlink = proc_pid_readlink, 12376d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 12386d76fa58SLinus Torvalds .setattr = proc_setattr, 12391da177e4SLinus Torvalds }; 12401da177e4SLinus Torvalds 124128a6d671SEric W. Biederman 124228a6d671SEric W. Biederman /* building an inode */ 124328a6d671SEric W. Biederman 124428a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 124528a6d671SEric W. Biederman { 124628a6d671SEric W. Biederman int dumpable = 0; 124728a6d671SEric W. Biederman struct mm_struct *mm; 124828a6d671SEric W. Biederman 124928a6d671SEric W. Biederman task_lock(task); 125028a6d671SEric W. Biederman mm = task->mm; 125128a6d671SEric W. Biederman if (mm) 12526c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 125328a6d671SEric W. Biederman task_unlock(task); 125428a6d671SEric W. Biederman if(dumpable == 1) 125528a6d671SEric W. Biederman return 1; 125628a6d671SEric W. Biederman return 0; 125728a6d671SEric W. Biederman } 125828a6d671SEric W. Biederman 125928a6d671SEric W. Biederman 126061a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 126128a6d671SEric W. Biederman { 126228a6d671SEric W. Biederman struct inode * inode; 126328a6d671SEric W. Biederman struct proc_inode *ei; 126428a6d671SEric W. Biederman 126528a6d671SEric W. Biederman /* We need a new inode */ 126628a6d671SEric W. Biederman 126728a6d671SEric W. Biederman inode = new_inode(sb); 126828a6d671SEric W. Biederman if (!inode) 126928a6d671SEric W. Biederman goto out; 127028a6d671SEric W. Biederman 127128a6d671SEric W. Biederman /* Common stuff */ 127228a6d671SEric W. Biederman ei = PROC_I(inode); 127328a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 127428a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 127528a6d671SEric W. Biederman 127628a6d671SEric W. Biederman /* 127728a6d671SEric W. Biederman * grab the reference to task. 127828a6d671SEric W. Biederman */ 12791a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 128028a6d671SEric W. Biederman if (!ei->pid) 128128a6d671SEric W. Biederman goto out_unlock; 128228a6d671SEric W. Biederman 128328a6d671SEric W. Biederman inode->i_uid = 0; 128428a6d671SEric W. Biederman inode->i_gid = 0; 128528a6d671SEric W. Biederman if (task_dumpable(task)) { 128628a6d671SEric W. Biederman inode->i_uid = task->euid; 128728a6d671SEric W. Biederman inode->i_gid = task->egid; 128828a6d671SEric W. Biederman } 128928a6d671SEric W. Biederman security_task_to_inode(task, inode); 129028a6d671SEric W. Biederman 129128a6d671SEric W. Biederman out: 129228a6d671SEric W. Biederman return inode; 129328a6d671SEric W. Biederman 129428a6d671SEric W. Biederman out_unlock: 129528a6d671SEric W. Biederman iput(inode); 129628a6d671SEric W. Biederman return NULL; 129728a6d671SEric W. Biederman } 129828a6d671SEric W. Biederman 129928a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 130028a6d671SEric W. Biederman { 130128a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 130228a6d671SEric W. Biederman struct task_struct *task; 130328a6d671SEric W. Biederman generic_fillattr(inode, stat); 130428a6d671SEric W. Biederman 130528a6d671SEric W. Biederman rcu_read_lock(); 130628a6d671SEric W. Biederman stat->uid = 0; 130728a6d671SEric W. Biederman stat->gid = 0; 130828a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 130928a6d671SEric W. Biederman if (task) { 131028a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 131128a6d671SEric W. Biederman task_dumpable(task)) { 131228a6d671SEric W. Biederman stat->uid = task->euid; 131328a6d671SEric W. Biederman stat->gid = task->egid; 131428a6d671SEric W. Biederman } 131528a6d671SEric W. Biederman } 131628a6d671SEric W. Biederman rcu_read_unlock(); 131728a6d671SEric W. Biederman return 0; 131828a6d671SEric W. Biederman } 131928a6d671SEric W. Biederman 132028a6d671SEric W. Biederman /* dentry stuff */ 132128a6d671SEric W. Biederman 132228a6d671SEric W. Biederman /* 132328a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 132428a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 132528a6d671SEric W. Biederman * due to the way we treat inodes. 132628a6d671SEric W. Biederman * 132728a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 132828a6d671SEric W. Biederman * performed a setuid(), etc. 132928a6d671SEric W. Biederman * 133028a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 133128a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 133228a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 133328a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 133428a6d671SEric W. Biederman * made this apply to all per process world readable and executable 133528a6d671SEric W. Biederman * directories. 133628a6d671SEric W. Biederman */ 133728a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 133828a6d671SEric W. Biederman { 133928a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 134028a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 134128a6d671SEric W. Biederman if (task) { 134228a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 134328a6d671SEric W. Biederman task_dumpable(task)) { 134428a6d671SEric W. Biederman inode->i_uid = task->euid; 134528a6d671SEric W. Biederman inode->i_gid = task->egid; 134628a6d671SEric W. Biederman } else { 134728a6d671SEric W. Biederman inode->i_uid = 0; 134828a6d671SEric W. Biederman inode->i_gid = 0; 134928a6d671SEric W. Biederman } 135028a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 135128a6d671SEric W. Biederman security_task_to_inode(task, inode); 135228a6d671SEric W. Biederman put_task_struct(task); 135328a6d671SEric W. Biederman return 1; 135428a6d671SEric W. Biederman } 135528a6d671SEric W. Biederman d_drop(dentry); 135628a6d671SEric W. Biederman return 0; 135728a6d671SEric W. Biederman } 135828a6d671SEric W. Biederman 135928a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 136028a6d671SEric W. Biederman { 136128a6d671SEric W. Biederman /* Is the task we represent dead? 136228a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 136328a6d671SEric W. Biederman * kill it immediately. 136428a6d671SEric W. Biederman */ 136528a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 136628a6d671SEric W. Biederman } 136728a6d671SEric W. Biederman 136828a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 136928a6d671SEric W. Biederman { 137028a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 137128a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 137228a6d671SEric W. Biederman }; 137328a6d671SEric W. Biederman 137428a6d671SEric W. Biederman /* Lookups */ 137528a6d671SEric W. Biederman 1376c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1377c5141e6dSEric Dumazet struct task_struct *, const void *); 137861a28784SEric W. Biederman 13791c0d04c9SEric W. Biederman /* 13801c0d04c9SEric W. Biederman * Fill a directory entry. 13811c0d04c9SEric W. Biederman * 13821c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 13831c0d04c9SEric W. Biederman * file type from dcache entry. 13841c0d04c9SEric W. Biederman * 13851c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 13861c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 13871c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 13881c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 13891c0d04c9SEric W. Biederman * by stat. 13901c0d04c9SEric W. Biederman */ 139161a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 139261a28784SEric W. Biederman char *name, int len, 1393c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 139461a28784SEric W. Biederman { 13952fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 139661a28784SEric W. Biederman struct inode *inode; 139761a28784SEric W. Biederman struct qstr qname; 139861a28784SEric W. Biederman ino_t ino = 0; 139961a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 140061a28784SEric W. Biederman 140161a28784SEric W. Biederman qname.name = name; 140261a28784SEric W. Biederman qname.len = len; 140361a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 140461a28784SEric W. Biederman 140561a28784SEric W. Biederman child = d_lookup(dir, &qname); 140661a28784SEric W. Biederman if (!child) { 140761a28784SEric W. Biederman struct dentry *new; 140861a28784SEric W. Biederman new = d_alloc(dir, &qname); 140961a28784SEric W. Biederman if (new) { 141061a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 141161a28784SEric W. Biederman if (child) 141261a28784SEric W. Biederman dput(new); 141361a28784SEric W. Biederman else 141461a28784SEric W. Biederman child = new; 141561a28784SEric W. Biederman } 141661a28784SEric W. Biederman } 141761a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 141861a28784SEric W. Biederman goto end_instantiate; 141961a28784SEric W. Biederman inode = child->d_inode; 142061a28784SEric W. Biederman if (inode) { 142161a28784SEric W. Biederman ino = inode->i_ino; 142261a28784SEric W. Biederman type = inode->i_mode >> 12; 142361a28784SEric W. Biederman } 142461a28784SEric W. Biederman dput(child); 142561a28784SEric W. Biederman end_instantiate: 142661a28784SEric W. Biederman if (!ino) 142761a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 142861a28784SEric W. Biederman if (!ino) 142961a28784SEric W. Biederman ino = 1; 143061a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 143161a28784SEric W. Biederman } 143261a28784SEric W. Biederman 143328a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 143428a6d671SEric W. Biederman { 143528a6d671SEric W. Biederman const char *name = dentry->d_name.name; 143628a6d671SEric W. Biederman int len = dentry->d_name.len; 143728a6d671SEric W. Biederman unsigned n = 0; 143828a6d671SEric W. Biederman 143928a6d671SEric W. Biederman if (len > 1 && *name == '0') 144028a6d671SEric W. Biederman goto out; 144128a6d671SEric W. Biederman while (len-- > 0) { 144228a6d671SEric W. Biederman unsigned c = *name++ - '0'; 144328a6d671SEric W. Biederman if (c > 9) 144428a6d671SEric W. Biederman goto out; 144528a6d671SEric W. Biederman if (n >= (~0U-9)/10) 144628a6d671SEric W. Biederman goto out; 144728a6d671SEric W. Biederman n *= 10; 144828a6d671SEric W. Biederman n += c; 144928a6d671SEric W. Biederman } 145028a6d671SEric W. Biederman return n; 145128a6d671SEric W. Biederman out: 145228a6d671SEric W. Biederman return ~0U; 145328a6d671SEric W. Biederman } 145428a6d671SEric W. Biederman 145527932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 145627932742SMiklos Szeredi 14573dcd25f3SJan Blunck static int proc_fd_info(struct inode *inode, struct path *path, char *info) 145828a6d671SEric W. Biederman { 145928a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 146028a6d671SEric W. Biederman struct files_struct *files = NULL; 146128a6d671SEric W. Biederman struct file *file; 146228a6d671SEric W. Biederman int fd = proc_fd(inode); 146328a6d671SEric W. Biederman 146428a6d671SEric W. Biederman if (task) { 146528a6d671SEric W. Biederman files = get_files_struct(task); 146628a6d671SEric W. Biederman put_task_struct(task); 146728a6d671SEric W. Biederman } 146828a6d671SEric W. Biederman if (files) { 146928a6d671SEric W. Biederman /* 147028a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 147128a6d671SEric W. Biederman * hold ->file_lock. 147228a6d671SEric W. Biederman */ 147328a6d671SEric W. Biederman spin_lock(&files->file_lock); 147428a6d671SEric W. Biederman file = fcheck_files(files, fd); 147528a6d671SEric W. Biederman if (file) { 14763dcd25f3SJan Blunck if (path) { 14773dcd25f3SJan Blunck *path = file->f_path; 14783dcd25f3SJan Blunck path_get(&file->f_path); 14793dcd25f3SJan Blunck } 148027932742SMiklos Szeredi if (info) 148127932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 148227932742SMiklos Szeredi "pos:\t%lli\n" 148327932742SMiklos Szeredi "flags:\t0%o\n", 148427932742SMiklos Szeredi (long long) file->f_pos, 148527932742SMiklos Szeredi file->f_flags); 148628a6d671SEric W. Biederman spin_unlock(&files->file_lock); 148728a6d671SEric W. Biederman put_files_struct(files); 148828a6d671SEric W. Biederman return 0; 148928a6d671SEric W. Biederman } 149028a6d671SEric W. Biederman spin_unlock(&files->file_lock); 149128a6d671SEric W. Biederman put_files_struct(files); 149228a6d671SEric W. Biederman } 149328a6d671SEric W. Biederman return -ENOENT; 149428a6d671SEric W. Biederman } 149528a6d671SEric W. Biederman 14963dcd25f3SJan Blunck static int proc_fd_link(struct inode *inode, struct path *path) 149727932742SMiklos Szeredi { 14983dcd25f3SJan Blunck return proc_fd_info(inode, path, NULL); 149927932742SMiklos Szeredi } 150027932742SMiklos Szeredi 150128a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 150228a6d671SEric W. Biederman { 150328a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 150428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 150528a6d671SEric W. Biederman int fd = proc_fd(inode); 150628a6d671SEric W. Biederman struct files_struct *files; 150728a6d671SEric W. Biederman 150828a6d671SEric W. Biederman if (task) { 150928a6d671SEric W. Biederman files = get_files_struct(task); 151028a6d671SEric W. Biederman if (files) { 151128a6d671SEric W. Biederman rcu_read_lock(); 151228a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 151328a6d671SEric W. Biederman rcu_read_unlock(); 151428a6d671SEric W. Biederman put_files_struct(files); 151528a6d671SEric W. Biederman if (task_dumpable(task)) { 151628a6d671SEric W. Biederman inode->i_uid = task->euid; 151728a6d671SEric W. Biederman inode->i_gid = task->egid; 151828a6d671SEric W. Biederman } else { 151928a6d671SEric W. Biederman inode->i_uid = 0; 152028a6d671SEric W. Biederman inode->i_gid = 0; 152128a6d671SEric W. Biederman } 152228a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 152328a6d671SEric W. Biederman security_task_to_inode(task, inode); 152428a6d671SEric W. Biederman put_task_struct(task); 152528a6d671SEric W. Biederman return 1; 152628a6d671SEric W. Biederman } 152728a6d671SEric W. Biederman rcu_read_unlock(); 152828a6d671SEric W. Biederman put_files_struct(files); 152928a6d671SEric W. Biederman } 153028a6d671SEric W. Biederman put_task_struct(task); 153128a6d671SEric W. Biederman } 153228a6d671SEric W. Biederman d_drop(dentry); 153328a6d671SEric W. Biederman return 0; 153428a6d671SEric W. Biederman } 153528a6d671SEric W. Biederman 153628a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 153728a6d671SEric W. Biederman { 153828a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 153928a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 154028a6d671SEric W. Biederman }; 154128a6d671SEric W. Biederman 1542444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1543c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 154428a6d671SEric W. Biederman { 1545c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 154628a6d671SEric W. Biederman struct file *file; 154728a6d671SEric W. Biederman struct files_struct *files; 154828a6d671SEric W. Biederman struct inode *inode; 154928a6d671SEric W. Biederman struct proc_inode *ei; 1550444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 155128a6d671SEric W. Biederman 155261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 155328a6d671SEric W. Biederman if (!inode) 155428a6d671SEric W. Biederman goto out; 155528a6d671SEric W. Biederman ei = PROC_I(inode); 155628a6d671SEric W. Biederman ei->fd = fd; 155728a6d671SEric W. Biederman files = get_files_struct(task); 155828a6d671SEric W. Biederman if (!files) 1559444ceed8SEric W. Biederman goto out_iput; 156028a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 156128a6d671SEric W. Biederman 156228a6d671SEric W. Biederman /* 156328a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 156428a6d671SEric W. Biederman * hold ->file_lock. 156528a6d671SEric W. Biederman */ 156628a6d671SEric W. Biederman spin_lock(&files->file_lock); 156728a6d671SEric W. Biederman file = fcheck_files(files, fd); 156828a6d671SEric W. Biederman if (!file) 1569444ceed8SEric W. Biederman goto out_unlock; 157028a6d671SEric W. Biederman if (file->f_mode & 1) 157128a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 157228a6d671SEric W. Biederman if (file->f_mode & 2) 157328a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 157428a6d671SEric W. Biederman spin_unlock(&files->file_lock); 157528a6d671SEric W. Biederman put_files_struct(files); 1576444ceed8SEric W. Biederman 157728a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 157828a6d671SEric W. Biederman inode->i_size = 64; 157928a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 158028a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 158128a6d671SEric W. Biederman d_add(dentry, inode); 158228a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 158328a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1584444ceed8SEric W. Biederman error = NULL; 1585444ceed8SEric W. Biederman 1586444ceed8SEric W. Biederman out: 1587444ceed8SEric W. Biederman return error; 1588444ceed8SEric W. Biederman out_unlock: 1589444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1590444ceed8SEric W. Biederman put_files_struct(files); 1591444ceed8SEric W. Biederman out_iput: 1592444ceed8SEric W. Biederman iput(inode); 1593444ceed8SEric W. Biederman goto out; 1594444ceed8SEric W. Biederman } 1595444ceed8SEric W. Biederman 159627932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 159727932742SMiklos Szeredi struct dentry *dentry, 159827932742SMiklos Szeredi instantiate_t instantiate) 1599444ceed8SEric W. Biederman { 1600444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1601444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1602444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1603444ceed8SEric W. Biederman 1604444ceed8SEric W. Biederman if (!task) 1605444ceed8SEric W. Biederman goto out_no_task; 1606444ceed8SEric W. Biederman if (fd == ~0U) 1607444ceed8SEric W. Biederman goto out; 1608444ceed8SEric W. Biederman 160927932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 161028a6d671SEric W. Biederman out: 161128a6d671SEric W. Biederman put_task_struct(task); 161228a6d671SEric W. Biederman out_no_task: 161328a6d671SEric W. Biederman return result; 161428a6d671SEric W. Biederman } 161528a6d671SEric W. Biederman 161627932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 161727932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 16181da177e4SLinus Torvalds { 16192fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 16205634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 162199f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1622457c2510SPavel Emelyanov unsigned int fd, ino; 16231da177e4SLinus Torvalds int retval; 16241da177e4SLinus Torvalds struct files_struct * files; 1625badf1662SDipankar Sarma struct fdtable *fdt; 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds retval = -ENOENT; 162899f89551SEric W. Biederman if (!p) 162999f89551SEric W. Biederman goto out_no_task; 16301da177e4SLinus Torvalds retval = 0; 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds fd = filp->f_pos; 16331da177e4SLinus Torvalds switch (fd) { 16341da177e4SLinus Torvalds case 0: 16351da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 16361da177e4SLinus Torvalds goto out; 16371da177e4SLinus Torvalds filp->f_pos++; 16381da177e4SLinus Torvalds case 1: 16395634708bSEric W. Biederman ino = parent_ino(dentry); 16401da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 16411da177e4SLinus Torvalds goto out; 16421da177e4SLinus Torvalds filp->f_pos++; 16431da177e4SLinus Torvalds default: 16441da177e4SLinus Torvalds files = get_files_struct(p); 16451da177e4SLinus Torvalds if (!files) 16461da177e4SLinus Torvalds goto out; 1647b835996fSDipankar Sarma rcu_read_lock(); 1648badf1662SDipankar Sarma fdt = files_fdtable(files); 16491da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1650badf1662SDipankar Sarma fd < fdt->max_fds; 16511da177e4SLinus Torvalds fd++, filp->f_pos++) { 165227932742SMiklos Szeredi char name[PROC_NUMBUF]; 165327932742SMiklos Szeredi int len; 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 16561da177e4SLinus Torvalds continue; 1657b835996fSDipankar Sarma rcu_read_unlock(); 16581da177e4SLinus Torvalds 165927932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 166027932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 166127932742SMiklos Szeredi name, len, instantiate, 166227932742SMiklos Szeredi p, &fd) < 0) { 1663b835996fSDipankar Sarma rcu_read_lock(); 16641da177e4SLinus Torvalds break; 16651da177e4SLinus Torvalds } 1666b835996fSDipankar Sarma rcu_read_lock(); 16671da177e4SLinus Torvalds } 1668b835996fSDipankar Sarma rcu_read_unlock(); 16691da177e4SLinus Torvalds put_files_struct(files); 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds out: 167299f89551SEric W. Biederman put_task_struct(p); 167399f89551SEric W. Biederman out_no_task: 16741da177e4SLinus Torvalds return retval; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 167727932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 167827932742SMiklos Szeredi struct nameidata *nd) 167927932742SMiklos Szeredi { 168027932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 168127932742SMiklos Szeredi } 168227932742SMiklos Szeredi 168327932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 168427932742SMiklos Szeredi { 168527932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 168627932742SMiklos Szeredi } 168727932742SMiklos Szeredi 168827932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 168927932742SMiklos Szeredi size_t len, loff_t *ppos) 169027932742SMiklos Szeredi { 169127932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 16923dcd25f3SJan Blunck int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp); 169327932742SMiklos Szeredi if (!err) 169427932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 169527932742SMiklos Szeredi return err; 169627932742SMiklos Szeredi } 169727932742SMiklos Szeredi 169827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 169927932742SMiklos Szeredi .open = nonseekable_open, 170027932742SMiklos Szeredi .read = proc_fdinfo_read, 170127932742SMiklos Szeredi }; 170227932742SMiklos Szeredi 170300977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 17041da177e4SLinus Torvalds .read = generic_read_dir, 17051da177e4SLinus Torvalds .readdir = proc_readfd, 17061da177e4SLinus Torvalds }; 17071da177e4SLinus Torvalds 17081da177e4SLinus Torvalds /* 17098948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 17108948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 17118948e11fSAlexey Dobriyan */ 17128948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 17138948e11fSAlexey Dobriyan struct nameidata *nd) 17148948e11fSAlexey Dobriyan { 17158948e11fSAlexey Dobriyan int rv; 17168948e11fSAlexey Dobriyan 17178948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 17188948e11fSAlexey Dobriyan if (rv == 0) 17198948e11fSAlexey Dobriyan return 0; 17208948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 17218948e11fSAlexey Dobriyan rv = 0; 17228948e11fSAlexey Dobriyan return rv; 17238948e11fSAlexey Dobriyan } 17248948e11fSAlexey Dobriyan 17258948e11fSAlexey Dobriyan /* 17261da177e4SLinus Torvalds * proc directories can do almost nothing.. 17271da177e4SLinus Torvalds */ 1728c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 17291da177e4SLinus Torvalds .lookup = proc_lookupfd, 17308948e11fSAlexey Dobriyan .permission = proc_fd_permission, 17316d76fa58SLinus Torvalds .setattr = proc_setattr, 17321da177e4SLinus Torvalds }; 17331da177e4SLinus Torvalds 173427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 173527932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 173627932742SMiklos Szeredi { 173727932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 173827932742SMiklos Szeredi struct inode *inode; 173927932742SMiklos Szeredi struct proc_inode *ei; 174027932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 174127932742SMiklos Szeredi 174227932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 174327932742SMiklos Szeredi if (!inode) 174427932742SMiklos Szeredi goto out; 174527932742SMiklos Szeredi ei = PROC_I(inode); 174627932742SMiklos Szeredi ei->fd = fd; 174727932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 174827932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 174927932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 175027932742SMiklos Szeredi d_add(dentry, inode); 175127932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 175227932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 175327932742SMiklos Szeredi error = NULL; 175427932742SMiklos Szeredi 175527932742SMiklos Szeredi out: 175627932742SMiklos Szeredi return error; 175727932742SMiklos Szeredi } 175827932742SMiklos Szeredi 175927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 176027932742SMiklos Szeredi struct dentry *dentry, 176127932742SMiklos Szeredi struct nameidata *nd) 176227932742SMiklos Szeredi { 176327932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 176427932742SMiklos Szeredi } 176527932742SMiklos Szeredi 176627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 176727932742SMiklos Szeredi { 176827932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 176927932742SMiklos Szeredi proc_fdinfo_instantiate); 177027932742SMiklos Szeredi } 177127932742SMiklos Szeredi 177227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 177327932742SMiklos Szeredi .read = generic_read_dir, 177427932742SMiklos Szeredi .readdir = proc_readfdinfo, 177527932742SMiklos Szeredi }; 177627932742SMiklos Szeredi 177727932742SMiklos Szeredi /* 177827932742SMiklos Szeredi * proc directories can do almost nothing.. 177927932742SMiklos Szeredi */ 178027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 178127932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 178227932742SMiklos Szeredi .setattr = proc_setattr, 178327932742SMiklos Szeredi }; 178427932742SMiklos Szeredi 178527932742SMiklos Szeredi 1786444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1787c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1788444ceed8SEric W. Biederman { 1789c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1790444ceed8SEric W. Biederman struct inode *inode; 1791444ceed8SEric W. Biederman struct proc_inode *ei; 1792444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1793444ceed8SEric W. Biederman 179461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1795444ceed8SEric W. Biederman if (!inode) 1796444ceed8SEric W. Biederman goto out; 1797444ceed8SEric W. Biederman 1798444ceed8SEric W. Biederman ei = PROC_I(inode); 1799444ceed8SEric W. Biederman inode->i_mode = p->mode; 1800444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1801444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1802444ceed8SEric W. Biederman if (p->iop) 1803444ceed8SEric W. Biederman inode->i_op = p->iop; 1804444ceed8SEric W. Biederman if (p->fop) 1805444ceed8SEric W. Biederman inode->i_fop = p->fop; 1806444ceed8SEric W. Biederman ei->op = p->op; 1807444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1808444ceed8SEric W. Biederman d_add(dentry, inode); 1809444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1810444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1811444ceed8SEric W. Biederman error = NULL; 1812444ceed8SEric W. Biederman out: 1813444ceed8SEric W. Biederman return error; 1814444ceed8SEric W. Biederman } 1815444ceed8SEric W. Biederman 18161da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 18171da177e4SLinus Torvalds struct dentry *dentry, 1818c5141e6dSEric Dumazet const struct pid_entry *ents, 18197bcd6b0eSEric W. Biederman unsigned int nents) 18201da177e4SLinus Torvalds { 18211da177e4SLinus Torvalds struct inode *inode; 1822cd6a3ce9SEric W. Biederman struct dentry *error; 182399f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1824c5141e6dSEric Dumazet const struct pid_entry *p, *last; 18251da177e4SLinus Torvalds 1826cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 18271da177e4SLinus Torvalds inode = NULL; 18281da177e4SLinus Torvalds 182999f89551SEric W. Biederman if (!task) 183099f89551SEric W. Biederman goto out_no_task; 18311da177e4SLinus Torvalds 183220cdc894SEric W. Biederman /* 183320cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 183420cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 183520cdc894SEric W. Biederman */ 18367bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 18377bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 18381da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 18391da177e4SLinus Torvalds continue; 18401da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 18411da177e4SLinus Torvalds break; 18421da177e4SLinus Torvalds } 18437bcd6b0eSEric W. Biederman if (p > last) 18441da177e4SLinus Torvalds goto out; 18451da177e4SLinus Torvalds 1846444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 18471da177e4SLinus Torvalds out: 184899f89551SEric W. Biederman put_task_struct(task); 184999f89551SEric W. Biederman out_no_task: 1850cd6a3ce9SEric W. Biederman return error; 18511da177e4SLinus Torvalds } 18521da177e4SLinus Torvalds 1853c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1854c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 185561a28784SEric W. Biederman { 185661a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 185761a28784SEric W. Biederman proc_pident_instantiate, task, p); 185861a28784SEric W. Biederman } 185961a28784SEric W. Biederman 186028a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 186128a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1862c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 186328a6d671SEric W. Biederman { 186428a6d671SEric W. Biederman int i; 18652fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 186628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 186728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1868c5141e6dSEric Dumazet const struct pid_entry *p, *last; 186928a6d671SEric W. Biederman ino_t ino; 187028a6d671SEric W. Biederman int ret; 187128a6d671SEric W. Biederman 187228a6d671SEric W. Biederman ret = -ENOENT; 187328a6d671SEric W. Biederman if (!task) 187461a28784SEric W. Biederman goto out_no_task; 187528a6d671SEric W. Biederman 187628a6d671SEric W. Biederman ret = 0; 187728a6d671SEric W. Biederman i = filp->f_pos; 187828a6d671SEric W. Biederman switch (i) { 187928a6d671SEric W. Biederman case 0: 188028a6d671SEric W. Biederman ino = inode->i_ino; 188128a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 188228a6d671SEric W. Biederman goto out; 188328a6d671SEric W. Biederman i++; 188428a6d671SEric W. Biederman filp->f_pos++; 188528a6d671SEric W. Biederman /* fall through */ 188628a6d671SEric W. Biederman case 1: 188728a6d671SEric W. Biederman ino = parent_ino(dentry); 188828a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 188928a6d671SEric W. Biederman goto out; 189028a6d671SEric W. Biederman i++; 189128a6d671SEric W. Biederman filp->f_pos++; 189228a6d671SEric W. Biederman /* fall through */ 189328a6d671SEric W. Biederman default: 189428a6d671SEric W. Biederman i -= 2; 189528a6d671SEric W. Biederman if (i >= nents) { 189628a6d671SEric W. Biederman ret = 1; 189728a6d671SEric W. Biederman goto out; 189828a6d671SEric W. Biederman } 189928a6d671SEric W. Biederman p = ents + i; 19007bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 19017bcd6b0eSEric W. Biederman while (p <= last) { 190261a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 190328a6d671SEric W. Biederman goto out; 190428a6d671SEric W. Biederman filp->f_pos++; 190528a6d671SEric W. Biederman p++; 190628a6d671SEric W. Biederman } 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds 190928a6d671SEric W. Biederman ret = 1; 191028a6d671SEric W. Biederman out: 191161a28784SEric W. Biederman put_task_struct(task); 191261a28784SEric W. Biederman out_no_task: 191328a6d671SEric W. Biederman return ret; 19141da177e4SLinus Torvalds } 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 191728a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 191828a6d671SEric W. Biederman size_t count, loff_t *ppos) 191928a6d671SEric W. Biederman { 19202fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 192104ff9708SAl Viro char *p = NULL; 192228a6d671SEric W. Biederman ssize_t length; 192328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 192428a6d671SEric W. Biederman 192528a6d671SEric W. Biederman if (!task) 192604ff9708SAl Viro return -ESRCH; 192728a6d671SEric W. Biederman 192828a6d671SEric W. Biederman length = security_getprocattr(task, 19292fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 193004ff9708SAl Viro &p); 193128a6d671SEric W. Biederman put_task_struct(task); 193204ff9708SAl Viro if (length > 0) 193304ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 193404ff9708SAl Viro kfree(p); 193528a6d671SEric W. Biederman return length; 193628a6d671SEric W. Biederman } 193728a6d671SEric W. Biederman 193828a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 193928a6d671SEric W. Biederman size_t count, loff_t *ppos) 194028a6d671SEric W. Biederman { 19412fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 194228a6d671SEric W. Biederman char *page; 194328a6d671SEric W. Biederman ssize_t length; 194428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 194528a6d671SEric W. Biederman 194628a6d671SEric W. Biederman length = -ESRCH; 194728a6d671SEric W. Biederman if (!task) 194828a6d671SEric W. Biederman goto out_no_task; 194928a6d671SEric W. Biederman if (count > PAGE_SIZE) 195028a6d671SEric W. Biederman count = PAGE_SIZE; 195128a6d671SEric W. Biederman 195228a6d671SEric W. Biederman /* No partial writes. */ 195328a6d671SEric W. Biederman length = -EINVAL; 195428a6d671SEric W. Biederman if (*ppos != 0) 195528a6d671SEric W. Biederman goto out; 195628a6d671SEric W. Biederman 195728a6d671SEric W. Biederman length = -ENOMEM; 1958e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 195928a6d671SEric W. Biederman if (!page) 196028a6d671SEric W. Biederman goto out; 196128a6d671SEric W. Biederman 196228a6d671SEric W. Biederman length = -EFAULT; 196328a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 196428a6d671SEric W. Biederman goto out_free; 196528a6d671SEric W. Biederman 196628a6d671SEric W. Biederman length = security_setprocattr(task, 19672fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 196828a6d671SEric W. Biederman (void*)page, count); 196928a6d671SEric W. Biederman out_free: 197028a6d671SEric W. Biederman free_page((unsigned long) page); 197128a6d671SEric W. Biederman out: 197228a6d671SEric W. Biederman put_task_struct(task); 197328a6d671SEric W. Biederman out_no_task: 197428a6d671SEric W. Biederman return length; 197528a6d671SEric W. Biederman } 197628a6d671SEric W. Biederman 197700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 197828a6d671SEric W. Biederman .read = proc_pid_attr_read, 197928a6d671SEric W. Biederman .write = proc_pid_attr_write, 198028a6d671SEric W. Biederman }; 198128a6d671SEric W. Biederman 1982c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 198361a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 198461a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 198561a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 198661a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 198761a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 198861a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 198928a6d671SEric W. Biederman }; 199028a6d671SEric W. Biederman 199172d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 19921da177e4SLinus Torvalds void * dirent, filldir_t filldir) 19931da177e4SLinus Torvalds { 19941da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 199572d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 199800977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 19991da177e4SLinus Torvalds .read = generic_read_dir, 200072d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 20011da177e4SLinus Torvalds }; 20021da177e4SLinus Torvalds 200372d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 20041da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 20051da177e4SLinus Torvalds { 20067bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 20077bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 20081da177e4SLinus Torvalds } 20091da177e4SLinus Torvalds 2010c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 201172d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 201299f89551SEric W. Biederman .getattr = pid_getattr, 20136d76fa58SLinus Torvalds .setattr = proc_setattr, 20141da177e4SLinus Torvalds }; 20151da177e4SLinus Torvalds 20161da177e4SLinus Torvalds #endif 20171da177e4SLinus Torvalds 20183cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 20193cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 20203cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 20213cb4a0bbSKawai, Hidehiro { 20223cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 20233cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 20243cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 20253cb4a0bbSKawai, Hidehiro size_t len; 20263cb4a0bbSKawai, Hidehiro int ret; 20273cb4a0bbSKawai, Hidehiro 20283cb4a0bbSKawai, Hidehiro if (!task) 20293cb4a0bbSKawai, Hidehiro return -ESRCH; 20303cb4a0bbSKawai, Hidehiro 20313cb4a0bbSKawai, Hidehiro ret = 0; 20323cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 20333cb4a0bbSKawai, Hidehiro if (mm) { 20343cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 20353cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 20363cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 20373cb4a0bbSKawai, Hidehiro mmput(mm); 20383cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 20393cb4a0bbSKawai, Hidehiro } 20403cb4a0bbSKawai, Hidehiro 20413cb4a0bbSKawai, Hidehiro put_task_struct(task); 20423cb4a0bbSKawai, Hidehiro 20433cb4a0bbSKawai, Hidehiro return ret; 20443cb4a0bbSKawai, Hidehiro } 20453cb4a0bbSKawai, Hidehiro 20463cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 20473cb4a0bbSKawai, Hidehiro const char __user *buf, 20483cb4a0bbSKawai, Hidehiro size_t count, 20493cb4a0bbSKawai, Hidehiro loff_t *ppos) 20503cb4a0bbSKawai, Hidehiro { 20513cb4a0bbSKawai, Hidehiro struct task_struct *task; 20523cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 20533cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 20543cb4a0bbSKawai, Hidehiro unsigned int val; 20553cb4a0bbSKawai, Hidehiro int ret; 20563cb4a0bbSKawai, Hidehiro int i; 20573cb4a0bbSKawai, Hidehiro unsigned long mask; 20583cb4a0bbSKawai, Hidehiro 20593cb4a0bbSKawai, Hidehiro ret = -EFAULT; 20603cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 20613cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 20623cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 20633cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 20643cb4a0bbSKawai, Hidehiro goto out_no_task; 20653cb4a0bbSKawai, Hidehiro 20663cb4a0bbSKawai, Hidehiro ret = -EINVAL; 20673cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 20683cb4a0bbSKawai, Hidehiro if (*end == '\n') 20693cb4a0bbSKawai, Hidehiro end++; 20703cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 20713cb4a0bbSKawai, Hidehiro goto out_no_task; 20723cb4a0bbSKawai, Hidehiro 20733cb4a0bbSKawai, Hidehiro ret = -ESRCH; 20743cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 20753cb4a0bbSKawai, Hidehiro if (!task) 20763cb4a0bbSKawai, Hidehiro goto out_no_task; 20773cb4a0bbSKawai, Hidehiro 20783cb4a0bbSKawai, Hidehiro ret = end - buffer; 20793cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 20803cb4a0bbSKawai, Hidehiro if (!mm) 20813cb4a0bbSKawai, Hidehiro goto out_no_mm; 20823cb4a0bbSKawai, Hidehiro 20833cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 20843cb4a0bbSKawai, Hidehiro if (val & mask) 20853cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 20863cb4a0bbSKawai, Hidehiro else 20873cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 20883cb4a0bbSKawai, Hidehiro } 20893cb4a0bbSKawai, Hidehiro 20903cb4a0bbSKawai, Hidehiro mmput(mm); 20913cb4a0bbSKawai, Hidehiro out_no_mm: 20923cb4a0bbSKawai, Hidehiro put_task_struct(task); 20933cb4a0bbSKawai, Hidehiro out_no_task: 20943cb4a0bbSKawai, Hidehiro return ret; 20953cb4a0bbSKawai, Hidehiro } 20963cb4a0bbSKawai, Hidehiro 20973cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 20983cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 20993cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 21003cb4a0bbSKawai, Hidehiro }; 21013cb4a0bbSKawai, Hidehiro #endif 21023cb4a0bbSKawai, Hidehiro 21031da177e4SLinus Torvalds /* 21041da177e4SLinus Torvalds * /proc/self: 21051da177e4SLinus Torvalds */ 21061da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 21071da177e4SLinus Torvalds int buflen) 21081da177e4SLinus Torvalds { 2109488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2110b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 21118578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2112b55fcb22SAndrew Morton if (!tgid) 2113488e5bc4SEric W. Biederman return -ENOENT; 2114b55fcb22SAndrew Morton sprintf(tmp, "%d", tgid); 21151da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 21161da177e4SLinus Torvalds } 21171da177e4SLinus Torvalds 2118008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 21191da177e4SLinus Torvalds { 2120488e5bc4SEric W. Biederman struct pid_namespace *ns = dentry->d_sb->s_fs_info; 2121b55fcb22SAndrew Morton pid_t tgid = task_tgid_nr_ns(current, ns); 21228578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2123b55fcb22SAndrew Morton if (!tgid) 2124488e5bc4SEric W. Biederman return ERR_PTR(-ENOENT); 2125b55fcb22SAndrew Morton sprintf(tmp, "%d", task_tgid_nr_ns(current, ns)); 2126008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 2129c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 21301da177e4SLinus Torvalds .readlink = proc_self_readlink, 21311da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 21321da177e4SLinus Torvalds }; 21331da177e4SLinus Torvalds 213428a6d671SEric W. Biederman /* 2135801199ceSEric W. Biederman * proc base 2136801199ceSEric W. Biederman * 2137801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2138801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2139801199ceSEric W. Biederman * describe something that is process related. 2140801199ceSEric W. Biederman */ 2141c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 214261a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2143801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2144801199ceSEric W. Biederman }; 2145801199ceSEric W. Biederman 2146801199ceSEric W. Biederman /* 2147801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2148801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2149801199ceSEric W. Biederman * due to the way we treat inodes. 2150801199ceSEric W. Biederman */ 2151801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2152801199ceSEric W. Biederman { 2153801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2154801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2155801199ceSEric W. Biederman if (task) { 2156801199ceSEric W. Biederman put_task_struct(task); 2157801199ceSEric W. Biederman return 1; 2158801199ceSEric W. Biederman } 2159801199ceSEric W. Biederman d_drop(dentry); 2160801199ceSEric W. Biederman return 0; 2161801199ceSEric W. Biederman } 2162801199ceSEric W. Biederman 2163801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2164801199ceSEric W. Biederman { 2165801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2166801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2167801199ceSEric W. Biederman }; 2168801199ceSEric W. Biederman 2169444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2170c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2171801199ceSEric W. Biederman { 2172c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2173801199ceSEric W. Biederman struct inode *inode; 2174801199ceSEric W. Biederman struct proc_inode *ei; 2175444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2176801199ceSEric W. Biederman 2177801199ceSEric W. Biederman /* Allocate the inode */ 2178801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2179801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2180801199ceSEric W. Biederman if (!inode) 2181801199ceSEric W. Biederman goto out; 2182801199ceSEric W. Biederman 2183801199ceSEric W. Biederman /* Initialize the inode */ 2184801199ceSEric W. Biederman ei = PROC_I(inode); 2185801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2186801199ceSEric W. Biederman 2187801199ceSEric W. Biederman /* 2188801199ceSEric W. Biederman * grab the reference to the task. 2189801199ceSEric W. Biederman */ 21901a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2191801199ceSEric W. Biederman if (!ei->pid) 2192801199ceSEric W. Biederman goto out_iput; 2193801199ceSEric W. Biederman 2194801199ceSEric W. Biederman inode->i_uid = 0; 2195801199ceSEric W. Biederman inode->i_gid = 0; 2196801199ceSEric W. Biederman inode->i_mode = p->mode; 2197801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2198801199ceSEric W. Biederman inode->i_nlink = 2; 2199801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2200801199ceSEric W. Biederman inode->i_size = 64; 2201801199ceSEric W. Biederman if (p->iop) 2202801199ceSEric W. Biederman inode->i_op = p->iop; 2203801199ceSEric W. Biederman if (p->fop) 2204801199ceSEric W. Biederman inode->i_fop = p->fop; 2205801199ceSEric W. Biederman ei->op = p->op; 2206801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2207801199ceSEric W. Biederman d_add(dentry, inode); 2208801199ceSEric W. Biederman error = NULL; 2209801199ceSEric W. Biederman out: 2210801199ceSEric W. Biederman return error; 2211801199ceSEric W. Biederman out_iput: 2212801199ceSEric W. Biederman iput(inode); 2213801199ceSEric W. Biederman goto out; 2214801199ceSEric W. Biederman } 2215801199ceSEric W. Biederman 2216444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2217444ceed8SEric W. Biederman { 2218444ceed8SEric W. Biederman struct dentry *error; 2219444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2220c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2221444ceed8SEric W. Biederman 2222444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2223444ceed8SEric W. Biederman 2224444ceed8SEric W. Biederman if (!task) 2225444ceed8SEric W. Biederman goto out_no_task; 2226444ceed8SEric W. Biederman 2227444ceed8SEric W. Biederman /* Lookup the directory entry */ 22287bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 22297bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2230444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2231444ceed8SEric W. Biederman continue; 2232444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2233444ceed8SEric W. Biederman break; 2234444ceed8SEric W. Biederman } 22357bcd6b0eSEric W. Biederman if (p > last) 2236444ceed8SEric W. Biederman goto out; 2237444ceed8SEric W. Biederman 2238444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2239444ceed8SEric W. Biederman 2240444ceed8SEric W. Biederman out: 2241444ceed8SEric W. Biederman put_task_struct(task); 2242444ceed8SEric W. Biederman out_no_task: 2243444ceed8SEric W. Biederman return error; 2244444ceed8SEric W. Biederman } 2245444ceed8SEric W. Biederman 2246c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2247c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 224861a28784SEric W. Biederman { 224961a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 225061a28784SEric W. Biederman proc_base_instantiate, task, p); 225161a28784SEric W. Biederman } 225261a28784SEric W. Biederman 2253aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2254aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2255aba76fdbSAndrew Morton { 2256aba76fdbSAndrew Morton return sprintf(buffer, 22574b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2258aba76fdbSAndrew Morton "rchar: %llu\n" 2259aba76fdbSAndrew Morton "wchar: %llu\n" 2260aba76fdbSAndrew Morton "syscr: %llu\n" 2261aba76fdbSAndrew Morton "syscw: %llu\n" 22624b98d11bSAlexey Dobriyan #endif 2263aba76fdbSAndrew Morton "read_bytes: %llu\n" 2264aba76fdbSAndrew Morton "write_bytes: %llu\n" 2265aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 22664b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2267aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2268aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2269aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2270aba76fdbSAndrew Morton (unsigned long long)task->syscw, 22714b98d11bSAlexey Dobriyan #endif 2272aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2273aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2274aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2275aba76fdbSAndrew Morton } 2276aba76fdbSAndrew Morton #endif 2277aba76fdbSAndrew Morton 2278801199ceSEric W. Biederman /* 227928a6d671SEric W. Biederman * Thread groups 228028a6d671SEric W. Biederman */ 228100977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2282c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 228320cdc894SEric W. Biederman 2284c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 228561a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 228661a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 228727932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2288315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 228961a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2290df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2291d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 229243ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 229343ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 229443ae34cbSIngo Molnar #endif 229561a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2296ee992744SEric W. Biederman ONE("stat", S_IRUGO, tgid_stat), 2297a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 229861a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 229928a6d671SEric W. Biederman #ifdef CONFIG_NUMA 230061a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 230128a6d671SEric W. Biederman #endif 230261a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 230361a28784SEric W. Biederman LNK("cwd", cwd), 230461a28784SEric W. Biederman LNK("root", root), 230561a28784SEric W. Biederman LNK("exe", exe), 230661a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 230761a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 23081e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2309b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 231061a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 231185863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 231228a6d671SEric W. Biederman #endif 231328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 231472d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 231528a6d671SEric W. Biederman #endif 231628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 231761a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 231828a6d671SEric W. Biederman #endif 231928a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 232061a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 232128a6d671SEric W. Biederman #endif 23229745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 23239745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 23249745512cSArjan van de Ven #endif 23258793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 232661a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 232728a6d671SEric W. Biederman #endif 2328a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2329a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2330a424316cSPaul Menage #endif 233161a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 233261a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 233328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 233461a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 233528a6d671SEric W. Biederman #endif 2336f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2337f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2338f4f154fdSAkinobu Mita #endif 23393cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 23403cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 23413cb4a0bbSKawai, Hidehiro #endif 2342aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2343aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2344aba76fdbSAndrew Morton #endif 234528a6d671SEric W. Biederman }; 234628a6d671SEric W. Biederman 234728a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 234828a6d671SEric W. Biederman void * dirent, filldir_t filldir) 234928a6d671SEric W. Biederman { 235028a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 235128a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 235228a6d671SEric W. Biederman } 235328a6d671SEric W. Biederman 235400977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 235528a6d671SEric W. Biederman .read = generic_read_dir, 235628a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 235728a6d671SEric W. Biederman }; 235828a6d671SEric W. Biederman 235928a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 23607bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 23617bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 236228a6d671SEric W. Biederman } 236328a6d671SEric W. Biederman 2364c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 236528a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 236628a6d671SEric W. Biederman .getattr = pid_getattr, 236728a6d671SEric W. Biederman .setattr = proc_setattr, 236828a6d671SEric W. Biederman }; 236928a6d671SEric W. Biederman 237060347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 23711da177e4SLinus Torvalds { 237248e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 23738578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 237448e6484dSEric W. Biederman struct qstr name; 23751da177e4SLinus Torvalds 237648e6484dSEric W. Biederman name.name = buf; 237760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 237860347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 237948e6484dSEric W. Biederman if (dentry) { 23807766755aSAndrea Arcangeli if (!(current->flags & PF_EXITING)) 238148e6484dSEric W. Biederman shrink_dcache_parent(dentry); 238248e6484dSEric W. Biederman d_drop(dentry); 238348e6484dSEric W. Biederman dput(dentry); 23841da177e4SLinus Torvalds } 23851da177e4SLinus Torvalds 238660347f67SPavel Emelyanov if (tgid == 0) 238748e6484dSEric W. Biederman goto out; 23881da177e4SLinus Torvalds 238948e6484dSEric W. Biederman name.name = buf; 239060347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 239160347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 239248e6484dSEric W. Biederman if (!leader) 239348e6484dSEric W. Biederman goto out; 239448e6484dSEric W. Biederman 239548e6484dSEric W. Biederman name.name = "task"; 239648e6484dSEric W. Biederman name.len = strlen(name.name); 239748e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 239848e6484dSEric W. Biederman if (!dir) 239948e6484dSEric W. Biederman goto out_put_leader; 240048e6484dSEric W. Biederman 240148e6484dSEric W. Biederman name.name = buf; 240260347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 240348e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 240448e6484dSEric W. Biederman if (dentry) { 240548e6484dSEric W. Biederman shrink_dcache_parent(dentry); 240648e6484dSEric W. Biederman d_drop(dentry); 240748e6484dSEric W. Biederman dput(dentry); 24081da177e4SLinus Torvalds } 240948e6484dSEric W. Biederman 241048e6484dSEric W. Biederman dput(dir); 241148e6484dSEric W. Biederman out_put_leader: 241248e6484dSEric W. Biederman dput(leader); 241348e6484dSEric W. Biederman out: 241448e6484dSEric W. Biederman return; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24170895e91dSRandy Dunlap /** 24180895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 24190895e91dSRandy Dunlap * @task: task that should be flushed. 24200895e91dSRandy Dunlap * 24210895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 242260347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 24230895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 24240895e91dSRandy Dunlap * 24250895e91dSRandy Dunlap * Looks in the dcache for 24260895e91dSRandy Dunlap * /proc/@pid 24270895e91dSRandy Dunlap * /proc/@tgid/task/@pid 24280895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 24290895e91dSRandy Dunlap * from the dcache. 24300895e91dSRandy Dunlap * 24310895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 24320895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 24330895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 24340895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 24350895e91dSRandy Dunlap * dcache entries at process exit time. 24360895e91dSRandy Dunlap * 24370895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 24380895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 24390895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 244060347f67SPavel Emelyanov */ 244160347f67SPavel Emelyanov 244260347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 244360347f67SPavel Emelyanov { 24449fcc2d15SEric W. Biederman int i; 24459fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2446130f77ecSPavel Emelyanov struct upid *upid; 2447130f77ecSPavel Emelyanov 2448130f77ecSPavel Emelyanov pid = task_pid(task); 24499fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2450130f77ecSPavel Emelyanov tgid = task_tgid(task); 24519fcc2d15SEric W. Biederman 24529fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2453130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2454130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 24559fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2456130f77ecSPavel Emelyanov } 24576f4e6433SPavel Emelyanov 24586f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 24596f4e6433SPavel Emelyanov if (upid->nr == 1) 24606f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 246160347f67SPavel Emelyanov } 246260347f67SPavel Emelyanov 24639711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 24649711ef99SAdrian Bunk struct dentry * dentry, 2465c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2466444ceed8SEric W. Biederman { 2467444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2468444ceed8SEric W. Biederman struct inode *inode; 2469444ceed8SEric W. Biederman 247061a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2471444ceed8SEric W. Biederman if (!inode) 2472444ceed8SEric W. Biederman goto out; 2473444ceed8SEric W. Biederman 2474444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2475444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2476444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2477444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 247827932742SMiklos Szeredi inode->i_nlink = 5; 2479444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2480444ceed8SEric W. Biederman inode->i_nlink += 1; 2481444ceed8SEric W. Biederman #endif 2482444ceed8SEric W. Biederman 2483444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2484444ceed8SEric W. Biederman 2485444ceed8SEric W. Biederman d_add(dentry, inode); 2486444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2487444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2488444ceed8SEric W. Biederman error = NULL; 2489444ceed8SEric W. Biederman out: 2490444ceed8SEric W. Biederman return error; 2491444ceed8SEric W. Biederman } 2492444ceed8SEric W. Biederman 24931da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 24941da177e4SLinus Torvalds { 2495cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 24961da177e4SLinus Torvalds struct task_struct *task; 24971da177e4SLinus Torvalds unsigned tgid; 2498b488893aSPavel Emelyanov struct pid_namespace *ns; 24991da177e4SLinus Torvalds 2500801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2501801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2502801199ceSEric W. Biederman goto out; 2503801199ceSEric W. Biederman 25041da177e4SLinus Torvalds tgid = name_to_int(dentry); 25051da177e4SLinus Torvalds if (tgid == ~0U) 25061da177e4SLinus Torvalds goto out; 25071da177e4SLinus Torvalds 2508b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2509de758734SEric W. Biederman rcu_read_lock(); 2510b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 25111da177e4SLinus Torvalds if (task) 25121da177e4SLinus Torvalds get_task_struct(task); 2513de758734SEric W. Biederman rcu_read_unlock(); 25141da177e4SLinus Torvalds if (!task) 25151da177e4SLinus Torvalds goto out; 25161da177e4SLinus Torvalds 2517444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 251848e6484dSEric W. Biederman put_task_struct(task); 25191da177e4SLinus Torvalds out: 2520cd6a3ce9SEric W. Biederman return result; 25211da177e4SLinus Torvalds } 25221da177e4SLinus Torvalds 25231da177e4SLinus Torvalds /* 25240804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 25250bc58a91SEric W. Biederman * 25261da177e4SLinus Torvalds */ 252719fd4bb2SEric W. Biederman struct tgid_iter { 252819fd4bb2SEric W. Biederman unsigned int tgid; 25290804ef4bSEric W. Biederman struct task_struct *task; 253019fd4bb2SEric W. Biederman }; 253119fd4bb2SEric W. Biederman static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter) 253219fd4bb2SEric W. Biederman { 25330804ef4bSEric W. Biederman struct pid *pid; 25341da177e4SLinus Torvalds 253519fd4bb2SEric W. Biederman if (iter.task) 253619fd4bb2SEric W. Biederman put_task_struct(iter.task); 25370804ef4bSEric W. Biederman rcu_read_lock(); 25380804ef4bSEric W. Biederman retry: 253919fd4bb2SEric W. Biederman iter.task = NULL; 254019fd4bb2SEric W. Biederman pid = find_ge_pid(iter.tgid, ns); 25410804ef4bSEric W. Biederman if (pid) { 254219fd4bb2SEric W. Biederman iter.tgid = pid_nr_ns(pid, ns); 254319fd4bb2SEric W. Biederman iter.task = pid_task(pid, PIDTYPE_PID); 25440804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 25450804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 25460804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 25470804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 25480804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 25490804ef4bSEric W. Biederman * 25500804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 25510804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 25520804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 25530804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 25540804ef4bSEric W. Biederman * As we don't care in the case of readdir. 25550bc58a91SEric W. Biederman */ 255619fd4bb2SEric W. Biederman if (!iter.task || !has_group_leader_pid(iter.task)) { 255719fd4bb2SEric W. Biederman iter.tgid += 1; 25580804ef4bSEric W. Biederman goto retry; 255919fd4bb2SEric W. Biederman } 256019fd4bb2SEric W. Biederman get_task_struct(iter.task); 25611da177e4SLinus Torvalds } 2562454cc105SEric W. Biederman rcu_read_unlock(); 256319fd4bb2SEric W. Biederman return iter; 25641da177e4SLinus Torvalds } 25651da177e4SLinus Torvalds 25667bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 25671da177e4SLinus Torvalds 256861a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 256919fd4bb2SEric W. Biederman struct tgid_iter iter) 257061a28784SEric W. Biederman { 257161a28784SEric W. Biederman char name[PROC_NUMBUF]; 257219fd4bb2SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", iter.tgid); 257361a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 257419fd4bb2SEric W. Biederman proc_pid_instantiate, iter.task, NULL); 257561a28784SEric W. Biederman } 257661a28784SEric W. Biederman 25771da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 25781da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 25791da177e4SLinus Torvalds { 25801da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 25812fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 258219fd4bb2SEric W. Biederman struct tgid_iter iter; 2583b488893aSPavel Emelyanov struct pid_namespace *ns; 25841da177e4SLinus Torvalds 258561a28784SEric W. Biederman if (!reaper) 258661a28784SEric W. Biederman goto out_no_task; 258761a28784SEric W. Biederman 25887bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2589c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 259061a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2591801199ceSEric W. Biederman goto out; 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 2594b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 259519fd4bb2SEric W. Biederman iter.task = NULL; 259619fd4bb2SEric W. Biederman iter.tgid = filp->f_pos - TGID_OFFSET; 259719fd4bb2SEric W. Biederman for (iter = next_tgid(ns, iter); 259819fd4bb2SEric W. Biederman iter.task; 259919fd4bb2SEric W. Biederman iter.tgid += 1, iter = next_tgid(ns, iter)) { 260019fd4bb2SEric W. Biederman filp->f_pos = iter.tgid + TGID_OFFSET; 260119fd4bb2SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) { 260219fd4bb2SEric W. Biederman put_task_struct(iter.task); 26030804ef4bSEric W. Biederman goto out; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds } 26060804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 26070804ef4bSEric W. Biederman out: 260861a28784SEric W. Biederman put_task_struct(reaper); 260961a28784SEric W. Biederman out_no_task: 26101da177e4SLinus Torvalds return 0; 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds 26130bc58a91SEric W. Biederman /* 261428a6d671SEric W. Biederman * Tasks 261528a6d671SEric W. Biederman */ 2616c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 261761a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 261827932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2619315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 262061a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 2621df5f8314SEric W. Biederman ONE("status", S_IRUGO, pid_status), 2622d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 262343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 262443ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 262543ae34cbSIngo Molnar #endif 262661a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 2627ee992744SEric W. Biederman ONE("stat", S_IRUGO, tid_stat), 2628a56d3fc7SEric W. Biederman ONE("statm", S_IRUGO, pid_statm), 262961a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 263028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 263161a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 263228a6d671SEric W. Biederman #endif 263361a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 263461a28784SEric W. Biederman LNK("cwd", cwd), 263561a28784SEric W. Biederman LNK("root", root), 263661a28784SEric W. Biederman LNK("exe", exe), 263761a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 26381e883281SMatt Mackall #ifdef CONFIG_PROC_PAGE_MONITOR 2639b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 264061a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 264185863e47SMatt Mackall REG("pagemap", S_IRUSR, pagemap), 264228a6d671SEric W. Biederman #endif 264328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 264472d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 264528a6d671SEric W. Biederman #endif 264628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 264761a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 264828a6d671SEric W. Biederman #endif 264928a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 265061a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 265128a6d671SEric W. Biederman #endif 26529745512cSArjan van de Ven #ifdef CONFIG_LATENCYTOP 26539745512cSArjan van de Ven REG("latency", S_IRUGO, lstats), 26549745512cSArjan van de Ven #endif 26558793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 265661a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 265728a6d671SEric W. Biederman #endif 2658a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2659a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2660a424316cSPaul Menage #endif 266161a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 266261a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 266328a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 266461a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 266528a6d671SEric W. Biederman #endif 2666f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2667f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2668f4f154fdSAkinobu Mita #endif 266928a6d671SEric W. Biederman }; 267028a6d671SEric W. Biederman 267128a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 267228a6d671SEric W. Biederman void * dirent, filldir_t filldir) 267328a6d671SEric W. Biederman { 267428a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 267528a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 267628a6d671SEric W. Biederman } 267728a6d671SEric W. Biederman 267828a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 26797bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 26807bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 268128a6d671SEric W. Biederman } 268228a6d671SEric W. Biederman 268300977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 268428a6d671SEric W. Biederman .read = generic_read_dir, 268528a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 268628a6d671SEric W. Biederman }; 268728a6d671SEric W. Biederman 2688c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 268928a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 269028a6d671SEric W. Biederman .getattr = pid_getattr, 269128a6d671SEric W. Biederman .setattr = proc_setattr, 269228a6d671SEric W. Biederman }; 269328a6d671SEric W. Biederman 2694444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2695c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2696444ceed8SEric W. Biederman { 2697444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2698444ceed8SEric W. Biederman struct inode *inode; 269961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2700444ceed8SEric W. Biederman 2701444ceed8SEric W. Biederman if (!inode) 2702444ceed8SEric W. Biederman goto out; 2703444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2704444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2705444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2706444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 270727932742SMiklos Szeredi inode->i_nlink = 4; 2708444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2709444ceed8SEric W. Biederman inode->i_nlink += 1; 2710444ceed8SEric W. Biederman #endif 2711444ceed8SEric W. Biederman 2712444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2713444ceed8SEric W. Biederman 2714444ceed8SEric W. Biederman d_add(dentry, inode); 2715444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2716444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2717444ceed8SEric W. Biederman error = NULL; 2718444ceed8SEric W. Biederman out: 2719444ceed8SEric W. Biederman return error; 2720444ceed8SEric W. Biederman } 2721444ceed8SEric W. Biederman 272228a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 272328a6d671SEric W. Biederman { 272428a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 272528a6d671SEric W. Biederman struct task_struct *task; 272628a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 272728a6d671SEric W. Biederman unsigned tid; 2728b488893aSPavel Emelyanov struct pid_namespace *ns; 272928a6d671SEric W. Biederman 273028a6d671SEric W. Biederman if (!leader) 273128a6d671SEric W. Biederman goto out_no_task; 273228a6d671SEric W. Biederman 273328a6d671SEric W. Biederman tid = name_to_int(dentry); 273428a6d671SEric W. Biederman if (tid == ~0U) 273528a6d671SEric W. Biederman goto out; 273628a6d671SEric W. Biederman 2737b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 273828a6d671SEric W. Biederman rcu_read_lock(); 2739b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 274028a6d671SEric W. Biederman if (task) 274128a6d671SEric W. Biederman get_task_struct(task); 274228a6d671SEric W. Biederman rcu_read_unlock(); 274328a6d671SEric W. Biederman if (!task) 274428a6d671SEric W. Biederman goto out; 2745bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 274628a6d671SEric W. Biederman goto out_drop_task; 274728a6d671SEric W. Biederman 2748444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 274928a6d671SEric W. Biederman out_drop_task: 275028a6d671SEric W. Biederman put_task_struct(task); 275128a6d671SEric W. Biederman out: 275228a6d671SEric W. Biederman put_task_struct(leader); 275328a6d671SEric W. Biederman out_no_task: 275428a6d671SEric W. Biederman return result; 275528a6d671SEric W. Biederman } 275628a6d671SEric W. Biederman 275728a6d671SEric W. Biederman /* 27580bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 27590bc58a91SEric W. Biederman * 27600bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 27610bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 27620bc58a91SEric W. Biederman * directory we have more work todo. 27630bc58a91SEric W. Biederman * 27640bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 27650bc58a91SEric W. Biederman * 27660bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 27670bc58a91SEric W. Biederman * threads past it. 27680bc58a91SEric W. Biederman */ 2769cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2770b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 27710bc58a91SEric W. Biederman { 2772a872ff0cSOleg Nesterov struct task_struct *pos; 27730bc58a91SEric W. Biederman 2774cc288738SEric W. Biederman rcu_read_lock(); 27750bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 27760bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2777b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2778a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2779a872ff0cSOleg Nesterov goto found; 27800bc58a91SEric W. Biederman } 27810bc58a91SEric W. Biederman 27820bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 27830bc58a91SEric W. Biederman pos = NULL; 2784a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2785a872ff0cSOleg Nesterov goto out; 2786a872ff0cSOleg Nesterov 2787a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2788a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2789a872ff0cSOleg Nesterov */ 2790a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2791a872ff0cSOleg Nesterov pos = next_thread(pos); 2792a872ff0cSOleg Nesterov if (pos == leader) { 2793a872ff0cSOleg Nesterov pos = NULL; 2794a872ff0cSOleg Nesterov goto out; 2795a872ff0cSOleg Nesterov } 2796a872ff0cSOleg Nesterov } 2797a872ff0cSOleg Nesterov found: 2798a872ff0cSOleg Nesterov get_task_struct(pos); 2799a872ff0cSOleg Nesterov out: 2800cc288738SEric W. Biederman rcu_read_unlock(); 28010bc58a91SEric W. Biederman return pos; 28020bc58a91SEric W. Biederman } 28030bc58a91SEric W. Biederman 28040bc58a91SEric W. Biederman /* 28050bc58a91SEric W. Biederman * Find the next thread in the thread list. 28060bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 28070bc58a91SEric W. Biederman * 28080bc58a91SEric W. Biederman * The reference to the input task_struct is released. 28090bc58a91SEric W. Biederman */ 28100bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 28110bc58a91SEric W. Biederman { 2812c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2813cc288738SEric W. Biederman rcu_read_lock(); 2814c1df7fb8SOleg Nesterov if (pid_alive(start)) { 28150bc58a91SEric W. Biederman pos = next_thread(start); 2816c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 28170bc58a91SEric W. Biederman pos = NULL; 2818c1df7fb8SOleg Nesterov else 2819c1df7fb8SOleg Nesterov get_task_struct(pos); 2820c1df7fb8SOleg Nesterov } 2821cc288738SEric W. Biederman rcu_read_unlock(); 28220bc58a91SEric W. Biederman put_task_struct(start); 28230bc58a91SEric W. Biederman return pos; 28240bc58a91SEric W. Biederman } 28250bc58a91SEric W. Biederman 282661a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 282761a28784SEric W. Biederman struct task_struct *task, int tid) 282861a28784SEric W. Biederman { 282961a28784SEric W. Biederman char name[PROC_NUMBUF]; 283061a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 283161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 283261a28784SEric W. Biederman proc_task_instantiate, task, NULL); 283361a28784SEric W. Biederman } 283461a28784SEric W. Biederman 28351da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 28361da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 28371da177e4SLinus Torvalds { 28382fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 28391da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 28407d895244SGuillaume Chazarain struct task_struct *leader = NULL; 28410bc58a91SEric W. Biederman struct task_struct *task; 28421da177e4SLinus Torvalds int retval = -ENOENT; 28431da177e4SLinus Torvalds ino_t ino; 28440bc58a91SEric W. Biederman int tid; 28451da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 2846b488893aSPavel Emelyanov struct pid_namespace *ns; 28471da177e4SLinus Torvalds 28487d895244SGuillaume Chazarain task = get_proc_task(inode); 28497d895244SGuillaume Chazarain if (!task) 28507d895244SGuillaume Chazarain goto out_no_task; 28517d895244SGuillaume Chazarain rcu_read_lock(); 28527d895244SGuillaume Chazarain if (pid_alive(task)) { 28537d895244SGuillaume Chazarain leader = task->group_leader; 28547d895244SGuillaume Chazarain get_task_struct(leader); 28557d895244SGuillaume Chazarain } 28567d895244SGuillaume Chazarain rcu_read_unlock(); 28577d895244SGuillaume Chazarain put_task_struct(task); 285899f89551SEric W. Biederman if (!leader) 285999f89551SEric W. Biederman goto out_no_task; 28601da177e4SLinus Torvalds retval = 0; 28611da177e4SLinus Torvalds 28621da177e4SLinus Torvalds switch (pos) { 28631da177e4SLinus Torvalds case 0: 28641da177e4SLinus Torvalds ino = inode->i_ino; 28651da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 28661da177e4SLinus Torvalds goto out; 28671da177e4SLinus Torvalds pos++; 28681da177e4SLinus Torvalds /* fall through */ 28691da177e4SLinus Torvalds case 1: 28701da177e4SLinus Torvalds ino = parent_ino(dentry); 28711da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 28721da177e4SLinus Torvalds goto out; 28731da177e4SLinus Torvalds pos++; 28741da177e4SLinus Torvalds /* fall through */ 28751da177e4SLinus Torvalds } 28761da177e4SLinus Torvalds 28770bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 28780bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 28790bc58a91SEric W. Biederman */ 2880b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 28812b47c361SMathieu Desnoyers tid = (int)filp->f_version; 28820bc58a91SEric W. Biederman filp->f_version = 0; 2883b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 28840bc58a91SEric W. Biederman task; 28850bc58a91SEric W. Biederman task = next_tid(task), pos++) { 2886b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 288761a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 28880bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 28890bc58a91SEric W. Biederman * pid for the next readir call */ 28902b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 28910bc58a91SEric W. Biederman put_task_struct(task); 28921da177e4SLinus Torvalds break; 28930bc58a91SEric W. Biederman } 28941da177e4SLinus Torvalds } 28951da177e4SLinus Torvalds out: 28961da177e4SLinus Torvalds filp->f_pos = pos; 289799f89551SEric W. Biederman put_task_struct(leader); 289899f89551SEric W. Biederman out_no_task: 28991da177e4SLinus Torvalds return retval; 29001da177e4SLinus Torvalds } 29016e66b52bSEric W. Biederman 29026e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 29036e66b52bSEric W. Biederman { 29046e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 290599f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 29066e66b52bSEric W. Biederman generic_fillattr(inode, stat); 29076e66b52bSEric W. Biederman 290899f89551SEric W. Biederman if (p) { 290999f89551SEric W. Biederman rcu_read_lock(); 291099f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 291199f89551SEric W. Biederman rcu_read_unlock(); 291299f89551SEric W. Biederman put_task_struct(p); 29136e66b52bSEric W. Biederman } 29146e66b52bSEric W. Biederman 29156e66b52bSEric W. Biederman return 0; 29166e66b52bSEric W. Biederman } 291728a6d671SEric W. Biederman 2918c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 291928a6d671SEric W. Biederman .lookup = proc_task_lookup, 292028a6d671SEric W. Biederman .getattr = proc_task_getattr, 292128a6d671SEric W. Biederman .setattr = proc_setattr, 292228a6d671SEric W. Biederman }; 292328a6d671SEric W. Biederman 292400977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 292528a6d671SEric W. Biederman .read = generic_read_dir, 292628a6d671SEric W. Biederman .readdir = proc_task_readdir, 292728a6d671SEric W. Biederman }; 2928