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 928578cea7SEric W. Biederman /* Worst case buffer size needed for holding an integer. */ 930187f879SAndrew Morton #define PROC_NUMBUF 13 948578cea7SEric W. Biederman 951da177e4SLinus Torvalds struct pid_entry { 961da177e4SLinus Torvalds char *name; 97c5141e6dSEric Dumazet int len; 981da177e4SLinus Torvalds mode_t mode; 99c5ef1c42SArjan van de Ven const struct inode_operations *iop; 10000977a59SArjan van de Ven const struct file_operations *fop; 10120cdc894SEric W. Biederman union proc_op op; 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 10461a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10520cdc894SEric W. Biederman .name = (NAME), \ 106c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10720cdc894SEric W. Biederman .mode = MODE, \ 10820cdc894SEric W. Biederman .iop = IOP, \ 10920cdc894SEric W. Biederman .fop = FOP, \ 11020cdc894SEric W. Biederman .op = OP, \ 11120cdc894SEric W. Biederman } 11220cdc894SEric W. Biederman 11361a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11461a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11520cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11620cdc894SEric W. Biederman {} ) 11761a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11861a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11920cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 12020cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 12161a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 12261a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12320cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12461a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12561a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12620cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12720cdc894SEric W. Biederman { .proc_read = &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 1560494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 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); 1681da177e4SLinus Torvalds *mnt = mntget(fs->pwdmnt); 1691da177e4SLinus Torvalds *dentry = dget(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 1771da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 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); 1891da177e4SLinus Torvalds *mnt = mntget(fs->rootmnt); 1901da177e4SLinus Torvalds *dentry = dget(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) && \ 2021da177e4SLinus Torvalds (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 2031da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds int res = 0; 2081da177e4SLinus Torvalds unsigned int len; 2091da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2101da177e4SLinus Torvalds if (!mm) 2111da177e4SLinus Torvalds goto out; 2121da177e4SLinus Torvalds if (!mm->arg_end) 2131da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds if (len > PAGE_SIZE) 2181da177e4SLinus Torvalds len = PAGE_SIZE; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2231da177e4SLinus Torvalds // assume application is using setproctitle(3). 2241da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2251da177e4SLinus Torvalds len = strnlen(buffer, res); 2261da177e4SLinus Torvalds if (len < res) { 2271da177e4SLinus Torvalds res = len; 2281da177e4SLinus Torvalds } else { 2291da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2301da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2311da177e4SLinus Torvalds len = PAGE_SIZE - res; 2321da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2331da177e4SLinus Torvalds res = strnlen(buffer, res); 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds out_mm: 2371da177e4SLinus Torvalds mmput(mm); 2381da177e4SLinus Torvalds out: 2391da177e4SLinus Torvalds return res; 2401da177e4SLinus Torvalds } 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2431da177e4SLinus Torvalds { 2441da177e4SLinus Torvalds int res = 0; 2451da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2461da177e4SLinus Torvalds if (mm) { 2471da177e4SLinus Torvalds unsigned int nwords = 0; 2481da177e4SLinus Torvalds do 2491da177e4SLinus Torvalds nwords += 2; 2501da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2511da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2521da177e4SLinus Torvalds if (res > PAGE_SIZE) 2531da177e4SLinus Torvalds res = PAGE_SIZE; 2541da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2551da177e4SLinus Torvalds mmput(mm); 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds return res; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2621da177e4SLinus Torvalds /* 2631da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2641da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2651da177e4SLinus Torvalds */ 2661da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2671da177e4SLinus Torvalds { 268ffb45122SAlexey Dobriyan unsigned long wchan; 2699281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds wchan = get_wchan(task); 2721da177e4SLinus Torvalds 2739d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2741da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2759d65cb4aSAlexey Dobriyan else 2769d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2811da177e4SLinus Torvalds /* 2821da177e4SLinus Torvalds * Provides /proc/PID/schedstat 2831da177e4SLinus Torvalds */ 2841da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 2851da177e4SLinus Torvalds { 286172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 2871da177e4SLinus Torvalds task->sched_info.cpu_time, 2881da177e4SLinus Torvalds task->sched_info.run_delay, 2892d72376bSIngo Molnar task->sched_info.pcount); 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds #endif 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* The badness from the OOM killer */ 2941da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 2951da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 2961da177e4SLinus Torvalds { 2971da177e4SLinus Torvalds unsigned long points; 2981da177e4SLinus Torvalds struct timespec uptime; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 30119c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 3021da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 30319c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 3041da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 307d85f50d5SNeil Horman struct limit_names { 308d85f50d5SNeil Horman char *name; 309d85f50d5SNeil Horman char *unit; 310d85f50d5SNeil Horman }; 311d85f50d5SNeil Horman 312d85f50d5SNeil Horman static const struct limit_names lnames[RLIM_NLIMITS] = { 313d85f50d5SNeil Horman [RLIMIT_CPU] = {"Max cpu time", "ms"}, 314d85f50d5SNeil Horman [RLIMIT_FSIZE] = {"Max file size", "bytes"}, 315d85f50d5SNeil Horman [RLIMIT_DATA] = {"Max data size", "bytes"}, 316d85f50d5SNeil Horman [RLIMIT_STACK] = {"Max stack size", "bytes"}, 317d85f50d5SNeil Horman [RLIMIT_CORE] = {"Max core file size", "bytes"}, 318d85f50d5SNeil Horman [RLIMIT_RSS] = {"Max resident set", "bytes"}, 319d85f50d5SNeil Horman [RLIMIT_NPROC] = {"Max processes", "processes"}, 320d85f50d5SNeil Horman [RLIMIT_NOFILE] = {"Max open files", "files"}, 321d85f50d5SNeil Horman [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"}, 322d85f50d5SNeil Horman [RLIMIT_AS] = {"Max address space", "bytes"}, 323d85f50d5SNeil Horman [RLIMIT_LOCKS] = {"Max file locks", "locks"}, 324d85f50d5SNeil Horman [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"}, 325d85f50d5SNeil Horman [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"}, 326d85f50d5SNeil Horman [RLIMIT_NICE] = {"Max nice priority", NULL}, 327d85f50d5SNeil Horman [RLIMIT_RTPRIO] = {"Max realtime priority", NULL}, 328d85f50d5SNeil Horman }; 329d85f50d5SNeil Horman 330d85f50d5SNeil Horman /* Display limits for a process */ 331d85f50d5SNeil Horman static int proc_pid_limits(struct task_struct *task, char *buffer) 332d85f50d5SNeil Horman { 333d85f50d5SNeil Horman unsigned int i; 334d85f50d5SNeil Horman int count = 0; 335d85f50d5SNeil Horman unsigned long flags; 336d85f50d5SNeil Horman char *bufptr = buffer; 337d85f50d5SNeil Horman 338d85f50d5SNeil Horman struct rlimit rlim[RLIM_NLIMITS]; 339d85f50d5SNeil Horman 340d85f50d5SNeil Horman rcu_read_lock(); 341d85f50d5SNeil Horman if (!lock_task_sighand(task,&flags)) { 342d85f50d5SNeil Horman rcu_read_unlock(); 343d85f50d5SNeil Horman return 0; 344d85f50d5SNeil Horman } 345d85f50d5SNeil Horman memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS); 346d85f50d5SNeil Horman unlock_task_sighand(task, &flags); 347d85f50d5SNeil Horman rcu_read_unlock(); 348d85f50d5SNeil Horman 349d85f50d5SNeil Horman /* 350d85f50d5SNeil Horman * print the file header 351d85f50d5SNeil Horman */ 352d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n", 353d85f50d5SNeil Horman "Limit", "Soft Limit", "Hard Limit", "Units"); 354d85f50d5SNeil Horman 355d85f50d5SNeil Horman for (i = 0; i < RLIM_NLIMITS; i++) { 356d85f50d5SNeil Horman if (rlim[i].rlim_cur == RLIM_INFINITY) 357d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20s ", 358d85f50d5SNeil Horman lnames[i].name, "unlimited"); 359d85f50d5SNeil Horman else 360d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-25s %-20lu ", 361d85f50d5SNeil Horman lnames[i].name, rlim[i].rlim_cur); 362d85f50d5SNeil Horman 363d85f50d5SNeil Horman if (rlim[i].rlim_max == RLIM_INFINITY) 364d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20s ", "unlimited"); 365d85f50d5SNeil Horman else 366d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-20lu ", 367d85f50d5SNeil Horman rlim[i].rlim_max); 368d85f50d5SNeil Horman 369d85f50d5SNeil Horman if (lnames[i].unit) 370d85f50d5SNeil Horman count += sprintf(&bufptr[count], "%-10s\n", 371d85f50d5SNeil Horman lnames[i].unit); 372d85f50d5SNeil Horman else 373d85f50d5SNeil Horman count += sprintf(&bufptr[count], "\n"); 374d85f50d5SNeil Horman } 375d85f50d5SNeil Horman 376d85f50d5SNeil Horman return count; 377d85f50d5SNeil Horman } 378d85f50d5SNeil Horman 3791da177e4SLinus Torvalds /************************************************************************/ 3801da177e4SLinus Torvalds /* Here the fs part begins */ 3811da177e4SLinus Torvalds /************************************************************************/ 3821da177e4SLinus Torvalds 3831da177e4SLinus Torvalds /* permission checks */ 384778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 3851da177e4SLinus Torvalds { 386778c1144SEric W. Biederman struct task_struct *task; 387778c1144SEric W. Biederman int allowed = 0; 388df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 389df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 390df26c40eSEric W. Biederman * information. 391778c1144SEric W. Biederman */ 392778c1144SEric W. Biederman task = get_proc_task(inode); 393df26c40eSEric W. Biederman if (task) { 394778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 395778c1144SEric W. Biederman put_task_struct(task); 396df26c40eSEric W. Biederman } 397778c1144SEric W. Biederman return allowed; 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds 4006d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 4016d76fa58SLinus Torvalds { 4026d76fa58SLinus Torvalds int error; 4036d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 4046d76fa58SLinus Torvalds 4056d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 4066d76fa58SLinus Torvalds return -EPERM; 4076d76fa58SLinus Torvalds 4086d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 4096d76fa58SLinus Torvalds if (!error) 4106d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 4116d76fa58SLinus Torvalds return error; 4126d76fa58SLinus Torvalds } 4136d76fa58SLinus Torvalds 414c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 4156d76fa58SLinus Torvalds .setattr = proc_setattr, 4166d76fa58SLinus Torvalds }; 4176d76fa58SLinus Torvalds 4181da177e4SLinus Torvalds extern struct seq_operations mounts_op; 4195addc5ddSAl Viro struct proc_mounts { 4205addc5ddSAl Viro struct seq_file m; 4215addc5ddSAl Viro int event; 4225addc5ddSAl Viro }; 4235addc5ddSAl Viro 4241da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 4251da177e4SLinus Torvalds { 42699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 427cf7b708cSPavel Emelyanov struct nsproxy *nsp; 4286b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 4295addc5ddSAl Viro struct proc_mounts *p; 4305addc5ddSAl Viro int ret = -EINVAL; 4315addc5ddSAl Viro 43299f89551SEric W. Biederman if (task) { 433cf7b708cSPavel Emelyanov rcu_read_lock(); 434cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 435cf7b708cSPavel Emelyanov if (nsp) { 436cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 4376b3286edSKirill Korotaev if (ns) 4386b3286edSKirill Korotaev get_mnt_ns(ns); 439863c4702SAlexey Dobriyan } 440cf7b708cSPavel Emelyanov rcu_read_unlock(); 441cf7b708cSPavel Emelyanov 44299f89551SEric W. Biederman put_task_struct(task); 44399f89551SEric W. Biederman } 4441da177e4SLinus Torvalds 4456b3286edSKirill Korotaev if (ns) { 4465addc5ddSAl Viro ret = -ENOMEM; 4475addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 4485addc5ddSAl Viro if (p) { 4495addc5ddSAl Viro file->private_data = &p->m; 4505addc5ddSAl Viro ret = seq_open(file, &mounts_op); 4515addc5ddSAl Viro if (!ret) { 4526b3286edSKirill Korotaev p->m.private = ns; 4536b3286edSKirill Korotaev p->event = ns->event; 4545addc5ddSAl Viro return 0; 4551da177e4SLinus Torvalds } 4565addc5ddSAl Viro kfree(p); 4575addc5ddSAl Viro } 4586b3286edSKirill Korotaev put_mnt_ns(ns); 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds return ret; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 4641da177e4SLinus Torvalds { 4651da177e4SLinus Torvalds struct seq_file *m = file->private_data; 4666b3286edSKirill Korotaev struct mnt_namespace *ns = m->private; 4676b3286edSKirill Korotaev put_mnt_ns(ns); 4681da177e4SLinus Torvalds return seq_release(inode, file); 4691da177e4SLinus Torvalds } 4701da177e4SLinus Torvalds 4715addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 4725addc5ddSAl Viro { 4735addc5ddSAl Viro struct proc_mounts *p = file->private_data; 4746b3286edSKirill Korotaev struct mnt_namespace *ns = p->m.private; 4755addc5ddSAl Viro unsigned res = 0; 4765addc5ddSAl Viro 4775addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 4785addc5ddSAl Viro 4795addc5ddSAl Viro spin_lock(&vfsmount_lock); 4805addc5ddSAl Viro if (p->event != ns->event) { 4815addc5ddSAl Viro p->event = ns->event; 4825addc5ddSAl Viro res = POLLERR; 4835addc5ddSAl Viro } 4845addc5ddSAl Viro spin_unlock(&vfsmount_lock); 4855addc5ddSAl Viro 4865addc5ddSAl Viro return res; 4875addc5ddSAl Viro } 4885addc5ddSAl Viro 48900977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 4901da177e4SLinus Torvalds .open = mounts_open, 4911da177e4SLinus Torvalds .read = seq_read, 4921da177e4SLinus Torvalds .llseek = seq_lseek, 4931da177e4SLinus Torvalds .release = mounts_release, 4945addc5ddSAl Viro .poll = mounts_poll, 4951da177e4SLinus Torvalds }; 4961da177e4SLinus Torvalds 497b4629fe2SChuck Lever extern struct seq_operations mountstats_op; 498b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 499b4629fe2SChuck Lever { 500b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 501b4629fe2SChuck Lever 502b4629fe2SChuck Lever if (!ret) { 503b4629fe2SChuck Lever struct seq_file *m = file->private_data; 504cf7b708cSPavel Emelyanov struct nsproxy *nsp; 5056b3286edSKirill Korotaev struct mnt_namespace *mnt_ns = NULL; 50699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 50799f89551SEric W. Biederman 50899f89551SEric W. Biederman if (task) { 509cf7b708cSPavel Emelyanov rcu_read_lock(); 510cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 511cf7b708cSPavel Emelyanov if (nsp) { 512cf7b708cSPavel Emelyanov mnt_ns = nsp->mnt_ns; 5136b3286edSKirill Korotaev if (mnt_ns) 5146b3286edSKirill Korotaev get_mnt_ns(mnt_ns); 515cf7b708cSPavel Emelyanov } 516cf7b708cSPavel Emelyanov rcu_read_unlock(); 517cf7b708cSPavel Emelyanov 51899f89551SEric W. Biederman put_task_struct(task); 51999f89551SEric W. Biederman } 520b4629fe2SChuck Lever 5216b3286edSKirill Korotaev if (mnt_ns) 5226b3286edSKirill Korotaev m->private = mnt_ns; 523b4629fe2SChuck Lever else { 524b4629fe2SChuck Lever seq_release(inode, file); 525b4629fe2SChuck Lever ret = -EINVAL; 526b4629fe2SChuck Lever } 527b4629fe2SChuck Lever } 528b4629fe2SChuck Lever return ret; 529b4629fe2SChuck Lever } 530b4629fe2SChuck Lever 53100977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 532b4629fe2SChuck Lever .open = mountstats_open, 533b4629fe2SChuck Lever .read = seq_read, 534b4629fe2SChuck Lever .llseek = seq_lseek, 535b4629fe2SChuck Lever .release = mounts_release, 536b4629fe2SChuck Lever }; 537b4629fe2SChuck Lever 5381da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 5411da177e4SLinus Torvalds size_t count, loff_t *ppos) 5421da177e4SLinus Torvalds { 5432fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 5441da177e4SLinus Torvalds unsigned long page; 5451da177e4SLinus Torvalds ssize_t length; 54699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 54799f89551SEric W. Biederman 54899f89551SEric W. Biederman length = -ESRCH; 54999f89551SEric W. Biederman if (!task) 55099f89551SEric W. Biederman goto out_no_task; 5511da177e4SLinus Torvalds 5521da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 5531da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 55499f89551SEric W. Biederman 55599f89551SEric W. Biederman length = -ENOMEM; 556e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 55799f89551SEric W. Biederman goto out; 5581da177e4SLinus Torvalds 5591da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds if (length >= 0) 5621da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 5631da177e4SLinus Torvalds free_page(page); 56499f89551SEric W. Biederman out: 56599f89551SEric W. Biederman put_task_struct(task); 56699f89551SEric W. Biederman out_no_task: 5671da177e4SLinus Torvalds return length; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 57000977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 5711da177e4SLinus Torvalds .read = proc_info_read, 5721da177e4SLinus Torvalds }; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 5771da177e4SLinus Torvalds return 0; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 5811da177e4SLinus Torvalds size_t count, loff_t *ppos) 5821da177e4SLinus Torvalds { 5832fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5841da177e4SLinus Torvalds char *page; 5851da177e4SLinus Torvalds unsigned long src = *ppos; 5861da177e4SLinus Torvalds int ret = -ESRCH; 5871da177e4SLinus Torvalds struct mm_struct *mm; 5881da177e4SLinus Torvalds 58999f89551SEric W. Biederman if (!task) 59099f89551SEric W. Biederman goto out_no_task; 59199f89551SEric W. Biederman 592ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 5931da177e4SLinus Torvalds goto out; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds ret = -ENOMEM; 596e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 5971da177e4SLinus Torvalds if (!page) 5981da177e4SLinus Torvalds goto out; 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds ret = 0; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds mm = get_task_mm(task); 6031da177e4SLinus Torvalds if (!mm) 6041da177e4SLinus Torvalds goto out_free; 6051da177e4SLinus Torvalds 6061da177e4SLinus Torvalds ret = -EIO; 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 6091da177e4SLinus Torvalds goto out_put; 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds ret = 0; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds while (count > 0) { 6141da177e4SLinus Torvalds int this_len, retval; 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 6171da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 618ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 6191da177e4SLinus Torvalds if (!ret) 6201da177e4SLinus Torvalds ret = -EIO; 6211da177e4SLinus Torvalds break; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 6251da177e4SLinus Torvalds ret = -EFAULT; 6261da177e4SLinus Torvalds break; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds ret += retval; 6301da177e4SLinus Torvalds src += retval; 6311da177e4SLinus Torvalds buf += retval; 6321da177e4SLinus Torvalds count -= retval; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds *ppos = src; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds out_put: 6371da177e4SLinus Torvalds mmput(mm); 6381da177e4SLinus Torvalds out_free: 6391da177e4SLinus Torvalds free_page((unsigned long) page); 6401da177e4SLinus Torvalds out: 64199f89551SEric W. Biederman put_task_struct(task); 64299f89551SEric W. Biederman out_no_task: 6431da177e4SLinus Torvalds return ret; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds #define mem_write NULL 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds #ifndef mem_write 6491da177e4SLinus Torvalds /* This is a security hazard */ 65063967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 6511da177e4SLinus Torvalds size_t count, loff_t *ppos) 6521da177e4SLinus Torvalds { 653f7ca54f4SFrederik Deweerdt int copied; 6541da177e4SLinus Torvalds char *page; 6552fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 6561da177e4SLinus Torvalds unsigned long dst = *ppos; 6571da177e4SLinus Torvalds 65899f89551SEric W. Biederman copied = -ESRCH; 65999f89551SEric W. Biederman if (!task) 66099f89551SEric W. Biederman goto out_no_task; 6611da177e4SLinus Torvalds 66299f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 66399f89551SEric W. Biederman goto out; 66499f89551SEric W. Biederman 66599f89551SEric W. Biederman copied = -ENOMEM; 666e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 6671da177e4SLinus Torvalds if (!page) 66899f89551SEric W. Biederman goto out; 6691da177e4SLinus Torvalds 670f7ca54f4SFrederik Deweerdt copied = 0; 6711da177e4SLinus Torvalds while (count > 0) { 6721da177e4SLinus Torvalds int this_len, retval; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 6751da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 6761da177e4SLinus Torvalds copied = -EFAULT; 6771da177e4SLinus Torvalds break; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 6801da177e4SLinus Torvalds if (!retval) { 6811da177e4SLinus Torvalds if (!copied) 6821da177e4SLinus Torvalds copied = -EIO; 6831da177e4SLinus Torvalds break; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds copied += retval; 6861da177e4SLinus Torvalds buf += retval; 6871da177e4SLinus Torvalds dst += retval; 6881da177e4SLinus Torvalds count -= retval; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds *ppos = dst; 6911da177e4SLinus Torvalds free_page((unsigned long) page); 69299f89551SEric W. Biederman out: 69399f89551SEric W. Biederman put_task_struct(task); 69499f89551SEric W. Biederman out_no_task: 6951da177e4SLinus Torvalds return copied; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds #endif 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 7001da177e4SLinus Torvalds { 7011da177e4SLinus Torvalds switch (orig) { 7021da177e4SLinus Torvalds case 0: 7031da177e4SLinus Torvalds file->f_pos = offset; 7041da177e4SLinus Torvalds break; 7051da177e4SLinus Torvalds case 1: 7061da177e4SLinus Torvalds file->f_pos += offset; 7071da177e4SLinus Torvalds break; 7081da177e4SLinus Torvalds default: 7091da177e4SLinus Torvalds return -EINVAL; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds force_successful_syscall_return(); 7121da177e4SLinus Torvalds return file->f_pos; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 71500977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 7161da177e4SLinus Torvalds .llseek = mem_lseek, 7171da177e4SLinus Torvalds .read = mem_read, 7181da177e4SLinus Torvalds .write = mem_write, 7191da177e4SLinus Torvalds .open = mem_open, 7201da177e4SLinus Torvalds }; 7211da177e4SLinus Torvalds 722315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 723315e28c8SJames Pearson size_t count, loff_t *ppos) 724315e28c8SJames Pearson { 725315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 726315e28c8SJames Pearson char *page; 727315e28c8SJames Pearson unsigned long src = *ppos; 728315e28c8SJames Pearson int ret = -ESRCH; 729315e28c8SJames Pearson struct mm_struct *mm; 730315e28c8SJames Pearson 731315e28c8SJames Pearson if (!task) 732315e28c8SJames Pearson goto out_no_task; 733315e28c8SJames Pearson 734315e28c8SJames Pearson if (!ptrace_may_attach(task)) 735315e28c8SJames Pearson goto out; 736315e28c8SJames Pearson 737315e28c8SJames Pearson ret = -ENOMEM; 738315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 739315e28c8SJames Pearson if (!page) 740315e28c8SJames Pearson goto out; 741315e28c8SJames Pearson 742315e28c8SJames Pearson ret = 0; 743315e28c8SJames Pearson 744315e28c8SJames Pearson mm = get_task_mm(task); 745315e28c8SJames Pearson if (!mm) 746315e28c8SJames Pearson goto out_free; 747315e28c8SJames Pearson 748315e28c8SJames Pearson while (count > 0) { 749315e28c8SJames Pearson int this_len, retval, max_len; 750315e28c8SJames Pearson 751315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 752315e28c8SJames Pearson 753315e28c8SJames Pearson if (this_len <= 0) 754315e28c8SJames Pearson break; 755315e28c8SJames Pearson 756315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 757315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 758315e28c8SJames Pearson 759315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 760315e28c8SJames Pearson page, this_len, 0); 761315e28c8SJames Pearson 762315e28c8SJames Pearson if (retval <= 0) { 763315e28c8SJames Pearson ret = retval; 764315e28c8SJames Pearson break; 765315e28c8SJames Pearson } 766315e28c8SJames Pearson 767315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 768315e28c8SJames Pearson ret = -EFAULT; 769315e28c8SJames Pearson break; 770315e28c8SJames Pearson } 771315e28c8SJames Pearson 772315e28c8SJames Pearson ret += retval; 773315e28c8SJames Pearson src += retval; 774315e28c8SJames Pearson buf += retval; 775315e28c8SJames Pearson count -= retval; 776315e28c8SJames Pearson } 777315e28c8SJames Pearson *ppos = src; 778315e28c8SJames Pearson 779315e28c8SJames Pearson mmput(mm); 780315e28c8SJames Pearson out_free: 781315e28c8SJames Pearson free_page((unsigned long) page); 782315e28c8SJames Pearson out: 783315e28c8SJames Pearson put_task_struct(task); 784315e28c8SJames Pearson out_no_task: 785315e28c8SJames Pearson return ret; 786315e28c8SJames Pearson } 787315e28c8SJames Pearson 788315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 789315e28c8SJames Pearson .read = environ_read, 790315e28c8SJames Pearson }; 791315e28c8SJames Pearson 7921da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 7931da177e4SLinus Torvalds size_t count, loff_t *ppos) 7941da177e4SLinus Torvalds { 7952fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7968578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 7971da177e4SLinus Torvalds size_t len; 79899f89551SEric W. Biederman int oom_adjust; 7991da177e4SLinus Torvalds 80099f89551SEric W. Biederman if (!task) 80199f89551SEric W. Biederman return -ESRCH; 80299f89551SEric W. Biederman oom_adjust = task->oomkilladj; 80399f89551SEric W. Biederman put_task_struct(task); 80499f89551SEric W. Biederman 8058578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 8060c28f287SAkinobu Mita 8070c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 8111da177e4SLinus Torvalds size_t count, loff_t *ppos) 8121da177e4SLinus Torvalds { 81399f89551SEric W. Biederman struct task_struct *task; 8148578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 8151da177e4SLinus Torvalds int oom_adjust; 8161da177e4SLinus Torvalds 8178578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 8188578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 8198578cea7SEric W. Biederman count = sizeof(buffer) - 1; 8201da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 8211da177e4SLinus Torvalds return -EFAULT; 8221da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 8238ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 8248ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 8251da177e4SLinus Torvalds return -EINVAL; 8261da177e4SLinus Torvalds if (*end == '\n') 8271da177e4SLinus Torvalds end++; 8282fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 82999f89551SEric W. Biederman if (!task) 83099f89551SEric W. Biederman return -ESRCH; 8318fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 8328fb4fc68SGuillem Jover put_task_struct(task); 8338fb4fc68SGuillem Jover return -EACCES; 8348fb4fc68SGuillem Jover } 8351da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 83699f89551SEric W. Biederman put_task_struct(task); 8371da177e4SLinus Torvalds if (end - buffer == 0) 8381da177e4SLinus Torvalds return -EIO; 8391da177e4SLinus Torvalds return end - buffer; 8401da177e4SLinus Torvalds } 8411da177e4SLinus Torvalds 84200977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 8431da177e4SLinus Torvalds .read = oom_adjust_read, 8441da177e4SLinus Torvalds .write = oom_adjust_write, 8451da177e4SLinus Torvalds }; 8461da177e4SLinus Torvalds 8474b8df891SDavid Rientjes #ifdef CONFIG_MMU 848b813e931SDavid Rientjes static ssize_t clear_refs_write(struct file *file, const char __user *buf, 849b813e931SDavid Rientjes size_t count, loff_t *ppos) 850b813e931SDavid Rientjes { 851b813e931SDavid Rientjes struct task_struct *task; 852b813e931SDavid Rientjes char buffer[PROC_NUMBUF], *end; 853b813e931SDavid Rientjes struct mm_struct *mm; 854b813e931SDavid Rientjes 855b813e931SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 856b813e931SDavid Rientjes if (count > sizeof(buffer) - 1) 857b813e931SDavid Rientjes count = sizeof(buffer) - 1; 858b813e931SDavid Rientjes if (copy_from_user(buffer, buf, count)) 859b813e931SDavid Rientjes return -EFAULT; 860b813e931SDavid Rientjes if (!simple_strtol(buffer, &end, 0)) 861b813e931SDavid Rientjes return -EINVAL; 862b813e931SDavid Rientjes if (*end == '\n') 863b813e931SDavid Rientjes end++; 864b813e931SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 865b813e931SDavid Rientjes if (!task) 866b813e931SDavid Rientjes return -ESRCH; 867b813e931SDavid Rientjes mm = get_task_mm(task); 868b813e931SDavid Rientjes if (mm) { 869b813e931SDavid Rientjes clear_refs_smap(mm); 870b813e931SDavid Rientjes mmput(mm); 871b813e931SDavid Rientjes } 872b813e931SDavid Rientjes put_task_struct(task); 873b813e931SDavid Rientjes if (end - buffer == 0) 874b813e931SDavid Rientjes return -EIO; 875b813e931SDavid Rientjes return end - buffer; 876b813e931SDavid Rientjes } 877b813e931SDavid Rientjes 878b813e931SDavid Rientjes static struct file_operations proc_clear_refs_operations = { 879b813e931SDavid Rientjes .write = clear_refs_write, 880b813e931SDavid Rientjes }; 8814b8df891SDavid Rientjes #endif 882b813e931SDavid Rientjes 8831da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 8841da177e4SLinus Torvalds #define TMPBUFLEN 21 8851da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 8861da177e4SLinus Torvalds size_t count, loff_t *ppos) 8871da177e4SLinus Torvalds { 8882fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 88999f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 8901da177e4SLinus Torvalds ssize_t length; 8911da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 8921da177e4SLinus Torvalds 89399f89551SEric W. Biederman if (!task) 89499f89551SEric W. Biederman return -ESRCH; 8951da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 8961da177e4SLinus Torvalds audit_get_loginuid(task->audit_context)); 89799f89551SEric W. Biederman put_task_struct(task); 8981da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 9021da177e4SLinus Torvalds size_t count, loff_t *ppos) 9031da177e4SLinus Torvalds { 9042fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 9051da177e4SLinus Torvalds char *page, *tmp; 9061da177e4SLinus Torvalds ssize_t length; 9071da177e4SLinus Torvalds uid_t loginuid; 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 9101da177e4SLinus Torvalds return -EPERM; 9111da177e4SLinus Torvalds 91213b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 9131da177e4SLinus Torvalds return -EPERM; 9141da177e4SLinus Torvalds 915e0182909SAl Viro if (count >= PAGE_SIZE) 916e0182909SAl Viro count = PAGE_SIZE - 1; 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds if (*ppos != 0) { 9191da177e4SLinus Torvalds /* No partial writes. */ 9201da177e4SLinus Torvalds return -EINVAL; 9211da177e4SLinus Torvalds } 922e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 9231da177e4SLinus Torvalds if (!page) 9241da177e4SLinus Torvalds return -ENOMEM; 9251da177e4SLinus Torvalds length = -EFAULT; 9261da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 9271da177e4SLinus Torvalds goto out_free_page; 9281da177e4SLinus Torvalds 929e0182909SAl Viro page[count] = '\0'; 9301da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 9311da177e4SLinus Torvalds if (tmp == page) { 9321da177e4SLinus Torvalds length = -EINVAL; 9331da177e4SLinus Torvalds goto out_free_page; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds } 93699f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 9371da177e4SLinus Torvalds if (likely(length == 0)) 9381da177e4SLinus Torvalds length = count; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds out_free_page: 9411da177e4SLinus Torvalds free_page((unsigned long) page); 9421da177e4SLinus Torvalds return length; 9431da177e4SLinus Torvalds } 9441da177e4SLinus Torvalds 94500977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 9461da177e4SLinus Torvalds .read = proc_loginuid_read, 9471da177e4SLinus Torvalds .write = proc_loginuid_write, 9481da177e4SLinus Torvalds }; 9491da177e4SLinus Torvalds #endif 9501da177e4SLinus Torvalds 951f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 952f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 953f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 954f4f154fdSAkinobu Mita { 955f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 956f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 957f4f154fdSAkinobu Mita size_t len; 958f4f154fdSAkinobu Mita int make_it_fail; 959f4f154fdSAkinobu Mita 960f4f154fdSAkinobu Mita if (!task) 961f4f154fdSAkinobu Mita return -ESRCH; 962f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 963f4f154fdSAkinobu Mita put_task_struct(task); 964f4f154fdSAkinobu Mita 965f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 9660c28f287SAkinobu Mita 9670c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 968f4f154fdSAkinobu Mita } 969f4f154fdSAkinobu Mita 970f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 971f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 972f4f154fdSAkinobu Mita { 973f4f154fdSAkinobu Mita struct task_struct *task; 974f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 975f4f154fdSAkinobu Mita int make_it_fail; 976f4f154fdSAkinobu Mita 977f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 978f4f154fdSAkinobu Mita return -EPERM; 979f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 980f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 981f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 982f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 983f4f154fdSAkinobu Mita return -EFAULT; 984f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 985f4f154fdSAkinobu Mita if (*end == '\n') 986f4f154fdSAkinobu Mita end++; 987f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 988f4f154fdSAkinobu Mita if (!task) 989f4f154fdSAkinobu Mita return -ESRCH; 990f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 991f4f154fdSAkinobu Mita put_task_struct(task); 992f4f154fdSAkinobu Mita if (end - buffer == 0) 993f4f154fdSAkinobu Mita return -EIO; 994f4f154fdSAkinobu Mita return end - buffer; 995f4f154fdSAkinobu Mita } 996f4f154fdSAkinobu Mita 99700977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 998f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 999f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 1000f4f154fdSAkinobu Mita }; 1001f4f154fdSAkinobu Mita #endif 1002f4f154fdSAkinobu Mita 100343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 100443ae34cbSIngo Molnar /* 100543ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 100643ae34cbSIngo Molnar */ 100743ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 100843ae34cbSIngo Molnar { 100943ae34cbSIngo Molnar struct inode *inode = m->private; 101043ae34cbSIngo Molnar struct task_struct *p; 101143ae34cbSIngo Molnar 101243ae34cbSIngo Molnar WARN_ON(!inode); 101343ae34cbSIngo Molnar 101443ae34cbSIngo Molnar p = get_proc_task(inode); 101543ae34cbSIngo Molnar if (!p) 101643ae34cbSIngo Molnar return -ESRCH; 101743ae34cbSIngo Molnar proc_sched_show_task(p, m); 101843ae34cbSIngo Molnar 101943ae34cbSIngo Molnar put_task_struct(p); 102043ae34cbSIngo Molnar 102143ae34cbSIngo Molnar return 0; 102243ae34cbSIngo Molnar } 102343ae34cbSIngo Molnar 102443ae34cbSIngo Molnar static ssize_t 102543ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 102643ae34cbSIngo Molnar size_t count, loff_t *offset) 102743ae34cbSIngo Molnar { 102843ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 102943ae34cbSIngo Molnar struct task_struct *p; 103043ae34cbSIngo Molnar 103143ae34cbSIngo Molnar WARN_ON(!inode); 103243ae34cbSIngo Molnar 103343ae34cbSIngo Molnar p = get_proc_task(inode); 103443ae34cbSIngo Molnar if (!p) 103543ae34cbSIngo Molnar return -ESRCH; 103643ae34cbSIngo Molnar proc_sched_set_task(p); 103743ae34cbSIngo Molnar 103843ae34cbSIngo Molnar put_task_struct(p); 103943ae34cbSIngo Molnar 104043ae34cbSIngo Molnar return count; 104143ae34cbSIngo Molnar } 104243ae34cbSIngo Molnar 104343ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 104443ae34cbSIngo Molnar { 104543ae34cbSIngo Molnar int ret; 104643ae34cbSIngo Molnar 104743ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 104843ae34cbSIngo Molnar if (!ret) { 104943ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 105043ae34cbSIngo Molnar 105143ae34cbSIngo Molnar m->private = inode; 105243ae34cbSIngo Molnar } 105343ae34cbSIngo Molnar return ret; 105443ae34cbSIngo Molnar } 105543ae34cbSIngo Molnar 105643ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 105743ae34cbSIngo Molnar .open = sched_open, 105843ae34cbSIngo Molnar .read = seq_read, 105943ae34cbSIngo Molnar .write = sched_write, 106043ae34cbSIngo Molnar .llseek = seq_lseek, 10615ea473a1SAlexey Dobriyan .release = single_release, 106243ae34cbSIngo Molnar }; 106343ae34cbSIngo Molnar 106443ae34cbSIngo Molnar #endif 106543ae34cbSIngo Molnar 1066008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 10671da177e4SLinus Torvalds { 10681da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10691da177e4SLinus Torvalds int error = -EACCES; 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 10721da177e4SLinus Torvalds path_release(nd); 10731da177e4SLinus Torvalds 1074778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1075778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10761da177e4SLinus Torvalds goto out; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 10791da177e4SLinus Torvalds nd->last_type = LAST_BIND; 10801da177e4SLinus Torvalds out: 1081008b150aSAl Viro return ERR_PTR(error); 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 10851da177e4SLinus Torvalds char __user *buffer, int buflen) 10861da177e4SLinus Torvalds { 10871da177e4SLinus Torvalds struct inode * inode; 1088e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 1089e12ba74dSMel Gorman char *path; 10901da177e4SLinus Torvalds int len; 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds if (!tmp) 10931da177e4SLinus Torvalds return -ENOMEM; 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds inode = dentry->d_inode; 10961da177e4SLinus Torvalds path = d_path(dentry, mnt, tmp, PAGE_SIZE); 10971da177e4SLinus Torvalds len = PTR_ERR(path); 10981da177e4SLinus Torvalds if (IS_ERR(path)) 10991da177e4SLinus Torvalds goto out; 11001da177e4SLinus Torvalds len = tmp + PAGE_SIZE - 1 - path; 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds if (len > buflen) 11031da177e4SLinus Torvalds len = buflen; 11041da177e4SLinus Torvalds if (copy_to_user(buffer, path, len)) 11051da177e4SLinus Torvalds len = -EFAULT; 11061da177e4SLinus Torvalds out: 11071da177e4SLinus Torvalds free_page((unsigned long)tmp); 11081da177e4SLinus Torvalds return len; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 11121da177e4SLinus Torvalds { 11131da177e4SLinus Torvalds int error = -EACCES; 11141da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 11151da177e4SLinus Torvalds struct dentry *de; 11161da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 11171da177e4SLinus Torvalds 1118778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1119778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 11201da177e4SLinus Torvalds goto out; 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 11231da177e4SLinus Torvalds if (error) 11241da177e4SLinus Torvalds goto out; 11251da177e4SLinus Torvalds 11261da177e4SLinus Torvalds error = do_proc_readlink(de, mnt, buffer, buflen); 11271da177e4SLinus Torvalds dput(de); 11281da177e4SLinus Torvalds mntput(mnt); 11291da177e4SLinus Torvalds out: 11301da177e4SLinus Torvalds return error; 11311da177e4SLinus Torvalds } 11321da177e4SLinus Torvalds 1133c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 11341da177e4SLinus Torvalds .readlink = proc_pid_readlink, 11356d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 11366d76fa58SLinus Torvalds .setattr = proc_setattr, 11371da177e4SLinus Torvalds }; 11381da177e4SLinus Torvalds 113928a6d671SEric W. Biederman 114028a6d671SEric W. Biederman /* building an inode */ 114128a6d671SEric W. Biederman 114228a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 114328a6d671SEric W. Biederman { 114428a6d671SEric W. Biederman int dumpable = 0; 114528a6d671SEric W. Biederman struct mm_struct *mm; 114628a6d671SEric W. Biederman 114728a6d671SEric W. Biederman task_lock(task); 114828a6d671SEric W. Biederman mm = task->mm; 114928a6d671SEric W. Biederman if (mm) 11506c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 115128a6d671SEric W. Biederman task_unlock(task); 115228a6d671SEric W. Biederman if(dumpable == 1) 115328a6d671SEric W. Biederman return 1; 115428a6d671SEric W. Biederman return 0; 115528a6d671SEric W. Biederman } 115628a6d671SEric W. Biederman 115728a6d671SEric W. Biederman 115861a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 115928a6d671SEric W. Biederman { 116028a6d671SEric W. Biederman struct inode * inode; 116128a6d671SEric W. Biederman struct proc_inode *ei; 116228a6d671SEric W. Biederman 116328a6d671SEric W. Biederman /* We need a new inode */ 116428a6d671SEric W. Biederman 116528a6d671SEric W. Biederman inode = new_inode(sb); 116628a6d671SEric W. Biederman if (!inode) 116728a6d671SEric W. Biederman goto out; 116828a6d671SEric W. Biederman 116928a6d671SEric W. Biederman /* Common stuff */ 117028a6d671SEric W. Biederman ei = PROC_I(inode); 117128a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 117228a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 117328a6d671SEric W. Biederman 117428a6d671SEric W. Biederman /* 117528a6d671SEric W. Biederman * grab the reference to task. 117628a6d671SEric W. Biederman */ 11771a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 117828a6d671SEric W. Biederman if (!ei->pid) 117928a6d671SEric W. Biederman goto out_unlock; 118028a6d671SEric W. Biederman 118128a6d671SEric W. Biederman inode->i_uid = 0; 118228a6d671SEric W. Biederman inode->i_gid = 0; 118328a6d671SEric W. Biederman if (task_dumpable(task)) { 118428a6d671SEric W. Biederman inode->i_uid = task->euid; 118528a6d671SEric W. Biederman inode->i_gid = task->egid; 118628a6d671SEric W. Biederman } 118728a6d671SEric W. Biederman security_task_to_inode(task, inode); 118828a6d671SEric W. Biederman 118928a6d671SEric W. Biederman out: 119028a6d671SEric W. Biederman return inode; 119128a6d671SEric W. Biederman 119228a6d671SEric W. Biederman out_unlock: 119328a6d671SEric W. Biederman iput(inode); 119428a6d671SEric W. Biederman return NULL; 119528a6d671SEric W. Biederman } 119628a6d671SEric W. Biederman 119728a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 119828a6d671SEric W. Biederman { 119928a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 120028a6d671SEric W. Biederman struct task_struct *task; 120128a6d671SEric W. Biederman generic_fillattr(inode, stat); 120228a6d671SEric W. Biederman 120328a6d671SEric W. Biederman rcu_read_lock(); 120428a6d671SEric W. Biederman stat->uid = 0; 120528a6d671SEric W. Biederman stat->gid = 0; 120628a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 120728a6d671SEric W. Biederman if (task) { 120828a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 120928a6d671SEric W. Biederman task_dumpable(task)) { 121028a6d671SEric W. Biederman stat->uid = task->euid; 121128a6d671SEric W. Biederman stat->gid = task->egid; 121228a6d671SEric W. Biederman } 121328a6d671SEric W. Biederman } 121428a6d671SEric W. Biederman rcu_read_unlock(); 121528a6d671SEric W. Biederman return 0; 121628a6d671SEric W. Biederman } 121728a6d671SEric W. Biederman 121828a6d671SEric W. Biederman /* dentry stuff */ 121928a6d671SEric W. Biederman 122028a6d671SEric W. Biederman /* 122128a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 122228a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 122328a6d671SEric W. Biederman * due to the way we treat inodes. 122428a6d671SEric W. Biederman * 122528a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 122628a6d671SEric W. Biederman * performed a setuid(), etc. 122728a6d671SEric W. Biederman * 122828a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 122928a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 123028a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 123128a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 123228a6d671SEric W. Biederman * made this apply to all per process world readable and executable 123328a6d671SEric W. Biederman * directories. 123428a6d671SEric W. Biederman */ 123528a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 123628a6d671SEric W. Biederman { 123728a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 123828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 123928a6d671SEric W. Biederman if (task) { 124028a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 124128a6d671SEric W. Biederman task_dumpable(task)) { 124228a6d671SEric W. Biederman inode->i_uid = task->euid; 124328a6d671SEric W. Biederman inode->i_gid = task->egid; 124428a6d671SEric W. Biederman } else { 124528a6d671SEric W. Biederman inode->i_uid = 0; 124628a6d671SEric W. Biederman inode->i_gid = 0; 124728a6d671SEric W. Biederman } 124828a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 124928a6d671SEric W. Biederman security_task_to_inode(task, inode); 125028a6d671SEric W. Biederman put_task_struct(task); 125128a6d671SEric W. Biederman return 1; 125228a6d671SEric W. Biederman } 125328a6d671SEric W. Biederman d_drop(dentry); 125428a6d671SEric W. Biederman return 0; 125528a6d671SEric W. Biederman } 125628a6d671SEric W. Biederman 125728a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 125828a6d671SEric W. Biederman { 125928a6d671SEric W. Biederman /* Is the task we represent dead? 126028a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 126128a6d671SEric W. Biederman * kill it immediately. 126228a6d671SEric W. Biederman */ 126328a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 126428a6d671SEric W. Biederman } 126528a6d671SEric W. Biederman 126628a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 126728a6d671SEric W. Biederman { 126828a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 126928a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 127028a6d671SEric W. Biederman }; 127128a6d671SEric W. Biederman 127228a6d671SEric W. Biederman /* Lookups */ 127328a6d671SEric W. Biederman 1274c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1275c5141e6dSEric Dumazet struct task_struct *, const void *); 127661a28784SEric W. Biederman 12771c0d04c9SEric W. Biederman /* 12781c0d04c9SEric W. Biederman * Fill a directory entry. 12791c0d04c9SEric W. Biederman * 12801c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 12811c0d04c9SEric W. Biederman * file type from dcache entry. 12821c0d04c9SEric W. Biederman * 12831c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 12841c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 12851c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 12861c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 12871c0d04c9SEric W. Biederman * by stat. 12881c0d04c9SEric W. Biederman */ 128961a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 129061a28784SEric W. Biederman char *name, int len, 1291c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 129261a28784SEric W. Biederman { 12932fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 129461a28784SEric W. Biederman struct inode *inode; 129561a28784SEric W. Biederman struct qstr qname; 129661a28784SEric W. Biederman ino_t ino = 0; 129761a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 129861a28784SEric W. Biederman 129961a28784SEric W. Biederman qname.name = name; 130061a28784SEric W. Biederman qname.len = len; 130161a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 130261a28784SEric W. Biederman 130361a28784SEric W. Biederman child = d_lookup(dir, &qname); 130461a28784SEric W. Biederman if (!child) { 130561a28784SEric W. Biederman struct dentry *new; 130661a28784SEric W. Biederman new = d_alloc(dir, &qname); 130761a28784SEric W. Biederman if (new) { 130861a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 130961a28784SEric W. Biederman if (child) 131061a28784SEric W. Biederman dput(new); 131161a28784SEric W. Biederman else 131261a28784SEric W. Biederman child = new; 131361a28784SEric W. Biederman } 131461a28784SEric W. Biederman } 131561a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 131661a28784SEric W. Biederman goto end_instantiate; 131761a28784SEric W. Biederman inode = child->d_inode; 131861a28784SEric W. Biederman if (inode) { 131961a28784SEric W. Biederman ino = inode->i_ino; 132061a28784SEric W. Biederman type = inode->i_mode >> 12; 132161a28784SEric W. Biederman } 132261a28784SEric W. Biederman dput(child); 132361a28784SEric W. Biederman end_instantiate: 132461a28784SEric W. Biederman if (!ino) 132561a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 132661a28784SEric W. Biederman if (!ino) 132761a28784SEric W. Biederman ino = 1; 132861a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 132961a28784SEric W. Biederman } 133061a28784SEric W. Biederman 133128a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 133228a6d671SEric W. Biederman { 133328a6d671SEric W. Biederman const char *name = dentry->d_name.name; 133428a6d671SEric W. Biederman int len = dentry->d_name.len; 133528a6d671SEric W. Biederman unsigned n = 0; 133628a6d671SEric W. Biederman 133728a6d671SEric W. Biederman if (len > 1 && *name == '0') 133828a6d671SEric W. Biederman goto out; 133928a6d671SEric W. Biederman while (len-- > 0) { 134028a6d671SEric W. Biederman unsigned c = *name++ - '0'; 134128a6d671SEric W. Biederman if (c > 9) 134228a6d671SEric W. Biederman goto out; 134328a6d671SEric W. Biederman if (n >= (~0U-9)/10) 134428a6d671SEric W. Biederman goto out; 134528a6d671SEric W. Biederman n *= 10; 134628a6d671SEric W. Biederman n += c; 134728a6d671SEric W. Biederman } 134828a6d671SEric W. Biederman return n; 134928a6d671SEric W. Biederman out: 135028a6d671SEric W. Biederman return ~0U; 135128a6d671SEric W. Biederman } 135228a6d671SEric W. Biederman 135327932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 135427932742SMiklos Szeredi 135527932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry, 135627932742SMiklos Szeredi struct vfsmount **mnt, char *info) 135728a6d671SEric W. Biederman { 135828a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 135928a6d671SEric W. Biederman struct files_struct *files = NULL; 136028a6d671SEric W. Biederman struct file *file; 136128a6d671SEric W. Biederman int fd = proc_fd(inode); 136228a6d671SEric W. Biederman 136328a6d671SEric W. Biederman if (task) { 136428a6d671SEric W. Biederman files = get_files_struct(task); 136528a6d671SEric W. Biederman put_task_struct(task); 136628a6d671SEric W. Biederman } 136728a6d671SEric W. Biederman if (files) { 136828a6d671SEric W. Biederman /* 136928a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 137028a6d671SEric W. Biederman * hold ->file_lock. 137128a6d671SEric W. Biederman */ 137228a6d671SEric W. Biederman spin_lock(&files->file_lock); 137328a6d671SEric W. Biederman file = fcheck_files(files, fd); 137428a6d671SEric W. Biederman if (file) { 137527932742SMiklos Szeredi if (mnt) 13762fddfeefSJosef "Jeff" Sipek *mnt = mntget(file->f_path.mnt); 137727932742SMiklos Szeredi if (dentry) 13782fddfeefSJosef "Jeff" Sipek *dentry = dget(file->f_path.dentry); 137927932742SMiklos Szeredi if (info) 138027932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 138127932742SMiklos Szeredi "pos:\t%lli\n" 138227932742SMiklos Szeredi "flags:\t0%o\n", 138327932742SMiklos Szeredi (long long) file->f_pos, 138427932742SMiklos Szeredi file->f_flags); 138528a6d671SEric W. Biederman spin_unlock(&files->file_lock); 138628a6d671SEric W. Biederman put_files_struct(files); 138728a6d671SEric W. Biederman return 0; 138828a6d671SEric W. Biederman } 138928a6d671SEric W. Biederman spin_unlock(&files->file_lock); 139028a6d671SEric W. Biederman put_files_struct(files); 139128a6d671SEric W. Biederman } 139228a6d671SEric W. Biederman return -ENOENT; 139328a6d671SEric W. Biederman } 139428a6d671SEric W. Biederman 139527932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry, 139627932742SMiklos Szeredi struct vfsmount **mnt) 139727932742SMiklos Szeredi { 139827932742SMiklos Szeredi return proc_fd_info(inode, dentry, mnt, NULL); 139927932742SMiklos Szeredi } 140027932742SMiklos Szeredi 140128a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 140228a6d671SEric W. Biederman { 140328a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 140428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 140528a6d671SEric W. Biederman int fd = proc_fd(inode); 140628a6d671SEric W. Biederman struct files_struct *files; 140728a6d671SEric W. Biederman 140828a6d671SEric W. Biederman if (task) { 140928a6d671SEric W. Biederman files = get_files_struct(task); 141028a6d671SEric W. Biederman if (files) { 141128a6d671SEric W. Biederman rcu_read_lock(); 141228a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 141328a6d671SEric W. Biederman rcu_read_unlock(); 141428a6d671SEric W. Biederman put_files_struct(files); 141528a6d671SEric W. Biederman if (task_dumpable(task)) { 141628a6d671SEric W. Biederman inode->i_uid = task->euid; 141728a6d671SEric W. Biederman inode->i_gid = task->egid; 141828a6d671SEric W. Biederman } else { 141928a6d671SEric W. Biederman inode->i_uid = 0; 142028a6d671SEric W. Biederman inode->i_gid = 0; 142128a6d671SEric W. Biederman } 142228a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 142328a6d671SEric W. Biederman security_task_to_inode(task, inode); 142428a6d671SEric W. Biederman put_task_struct(task); 142528a6d671SEric W. Biederman return 1; 142628a6d671SEric W. Biederman } 142728a6d671SEric W. Biederman rcu_read_unlock(); 142828a6d671SEric W. Biederman put_files_struct(files); 142928a6d671SEric W. Biederman } 143028a6d671SEric W. Biederman put_task_struct(task); 143128a6d671SEric W. Biederman } 143228a6d671SEric W. Biederman d_drop(dentry); 143328a6d671SEric W. Biederman return 0; 143428a6d671SEric W. Biederman } 143528a6d671SEric W. Biederman 143628a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 143728a6d671SEric W. Biederman { 143828a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 143928a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 144028a6d671SEric W. Biederman }; 144128a6d671SEric W. Biederman 1442444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1443c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 144428a6d671SEric W. Biederman { 1445c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 144628a6d671SEric W. Biederman struct file *file; 144728a6d671SEric W. Biederman struct files_struct *files; 144828a6d671SEric W. Biederman struct inode *inode; 144928a6d671SEric W. Biederman struct proc_inode *ei; 1450444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 145128a6d671SEric W. Biederman 145261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 145328a6d671SEric W. Biederman if (!inode) 145428a6d671SEric W. Biederman goto out; 145528a6d671SEric W. Biederman ei = PROC_I(inode); 145628a6d671SEric W. Biederman ei->fd = fd; 145728a6d671SEric W. Biederman files = get_files_struct(task); 145828a6d671SEric W. Biederman if (!files) 1459444ceed8SEric W. Biederman goto out_iput; 146028a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 146128a6d671SEric W. Biederman 146228a6d671SEric W. Biederman /* 146328a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 146428a6d671SEric W. Biederman * hold ->file_lock. 146528a6d671SEric W. Biederman */ 146628a6d671SEric W. Biederman spin_lock(&files->file_lock); 146728a6d671SEric W. Biederman file = fcheck_files(files, fd); 146828a6d671SEric W. Biederman if (!file) 1469444ceed8SEric W. Biederman goto out_unlock; 147028a6d671SEric W. Biederman if (file->f_mode & 1) 147128a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 147228a6d671SEric W. Biederman if (file->f_mode & 2) 147328a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 147428a6d671SEric W. Biederman spin_unlock(&files->file_lock); 147528a6d671SEric W. Biederman put_files_struct(files); 1476444ceed8SEric W. Biederman 147728a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 147828a6d671SEric W. Biederman inode->i_size = 64; 147928a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 148028a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 148128a6d671SEric W. Biederman d_add(dentry, inode); 148228a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 148328a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1484444ceed8SEric W. Biederman error = NULL; 1485444ceed8SEric W. Biederman 1486444ceed8SEric W. Biederman out: 1487444ceed8SEric W. Biederman return error; 1488444ceed8SEric W. Biederman out_unlock: 1489444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1490444ceed8SEric W. Biederman put_files_struct(files); 1491444ceed8SEric W. Biederman out_iput: 1492444ceed8SEric W. Biederman iput(inode); 1493444ceed8SEric W. Biederman goto out; 1494444ceed8SEric W. Biederman } 1495444ceed8SEric W. Biederman 149627932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 149727932742SMiklos Szeredi struct dentry *dentry, 149827932742SMiklos Szeredi instantiate_t instantiate) 1499444ceed8SEric W. Biederman { 1500444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1501444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1502444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1503444ceed8SEric W. Biederman 1504444ceed8SEric W. Biederman if (!task) 1505444ceed8SEric W. Biederman goto out_no_task; 1506444ceed8SEric W. Biederman if (fd == ~0U) 1507444ceed8SEric W. Biederman goto out; 1508444ceed8SEric W. Biederman 150927932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 151028a6d671SEric W. Biederman out: 151128a6d671SEric W. Biederman put_task_struct(task); 151228a6d671SEric W. Biederman out_no_task: 151328a6d671SEric W. Biederman return result; 151428a6d671SEric W. Biederman } 151528a6d671SEric W. Biederman 151627932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 151727932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 15181da177e4SLinus Torvalds { 15192fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 15205634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 152199f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 1522457c2510SPavel Emelyanov unsigned int fd, ino; 15231da177e4SLinus Torvalds int retval; 15241da177e4SLinus Torvalds struct files_struct * files; 1525badf1662SDipankar Sarma struct fdtable *fdt; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds retval = -ENOENT; 152899f89551SEric W. Biederman if (!p) 152999f89551SEric W. Biederman goto out_no_task; 15301da177e4SLinus Torvalds retval = 0; 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds fd = filp->f_pos; 15331da177e4SLinus Torvalds switch (fd) { 15341da177e4SLinus Torvalds case 0: 15351da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 15361da177e4SLinus Torvalds goto out; 15371da177e4SLinus Torvalds filp->f_pos++; 15381da177e4SLinus Torvalds case 1: 15395634708bSEric W. Biederman ino = parent_ino(dentry); 15401da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 15411da177e4SLinus Torvalds goto out; 15421da177e4SLinus Torvalds filp->f_pos++; 15431da177e4SLinus Torvalds default: 15441da177e4SLinus Torvalds files = get_files_struct(p); 15451da177e4SLinus Torvalds if (!files) 15461da177e4SLinus Torvalds goto out; 1547b835996fSDipankar Sarma rcu_read_lock(); 1548badf1662SDipankar Sarma fdt = files_fdtable(files); 15491da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1550badf1662SDipankar Sarma fd < fdt->max_fds; 15511da177e4SLinus Torvalds fd++, filp->f_pos++) { 155227932742SMiklos Szeredi char name[PROC_NUMBUF]; 155327932742SMiklos Szeredi int len; 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 15561da177e4SLinus Torvalds continue; 1557b835996fSDipankar Sarma rcu_read_unlock(); 15581da177e4SLinus Torvalds 155927932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 156027932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 156127932742SMiklos Szeredi name, len, instantiate, 156227932742SMiklos Szeredi p, &fd) < 0) { 1563b835996fSDipankar Sarma rcu_read_lock(); 15641da177e4SLinus Torvalds break; 15651da177e4SLinus Torvalds } 1566b835996fSDipankar Sarma rcu_read_lock(); 15671da177e4SLinus Torvalds } 1568b835996fSDipankar Sarma rcu_read_unlock(); 15691da177e4SLinus Torvalds put_files_struct(files); 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds out: 157299f89551SEric W. Biederman put_task_struct(p); 157399f89551SEric W. Biederman out_no_task: 15741da177e4SLinus Torvalds return retval; 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds 157727932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 157827932742SMiklos Szeredi struct nameidata *nd) 157927932742SMiklos Szeredi { 158027932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 158127932742SMiklos Szeredi } 158227932742SMiklos Szeredi 158327932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 158427932742SMiklos Szeredi { 158527932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 158627932742SMiklos Szeredi } 158727932742SMiklos Szeredi 158827932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 158927932742SMiklos Szeredi size_t len, loff_t *ppos) 159027932742SMiklos Szeredi { 159127932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 159227932742SMiklos Szeredi int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp); 159327932742SMiklos Szeredi if (!err) 159427932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 159527932742SMiklos Szeredi return err; 159627932742SMiklos Szeredi } 159727932742SMiklos Szeredi 159827932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 159927932742SMiklos Szeredi .open = nonseekable_open, 160027932742SMiklos Szeredi .read = proc_fdinfo_read, 160127932742SMiklos Szeredi }; 160227932742SMiklos Szeredi 160300977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 16041da177e4SLinus Torvalds .read = generic_read_dir, 16051da177e4SLinus Torvalds .readdir = proc_readfd, 16061da177e4SLinus Torvalds }; 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds /* 16098948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 16108948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 16118948e11fSAlexey Dobriyan */ 16128948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 16138948e11fSAlexey Dobriyan struct nameidata *nd) 16148948e11fSAlexey Dobriyan { 16158948e11fSAlexey Dobriyan int rv; 16168948e11fSAlexey Dobriyan 16178948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 16188948e11fSAlexey Dobriyan if (rv == 0) 16198948e11fSAlexey Dobriyan return 0; 16208948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 16218948e11fSAlexey Dobriyan rv = 0; 16228948e11fSAlexey Dobriyan return rv; 16238948e11fSAlexey Dobriyan } 16248948e11fSAlexey Dobriyan 16258948e11fSAlexey Dobriyan /* 16261da177e4SLinus Torvalds * proc directories can do almost nothing.. 16271da177e4SLinus Torvalds */ 1628c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 16291da177e4SLinus Torvalds .lookup = proc_lookupfd, 16308948e11fSAlexey Dobriyan .permission = proc_fd_permission, 16316d76fa58SLinus Torvalds .setattr = proc_setattr, 16321da177e4SLinus Torvalds }; 16331da177e4SLinus Torvalds 163427932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 163527932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 163627932742SMiklos Szeredi { 163727932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 163827932742SMiklos Szeredi struct inode *inode; 163927932742SMiklos Szeredi struct proc_inode *ei; 164027932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 164127932742SMiklos Szeredi 164227932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 164327932742SMiklos Szeredi if (!inode) 164427932742SMiklos Szeredi goto out; 164527932742SMiklos Szeredi ei = PROC_I(inode); 164627932742SMiklos Szeredi ei->fd = fd; 164727932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 164827932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 164927932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 165027932742SMiklos Szeredi d_add(dentry, inode); 165127932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 165227932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 165327932742SMiklos Szeredi error = NULL; 165427932742SMiklos Szeredi 165527932742SMiklos Szeredi out: 165627932742SMiklos Szeredi return error; 165727932742SMiklos Szeredi } 165827932742SMiklos Szeredi 165927932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 166027932742SMiklos Szeredi struct dentry *dentry, 166127932742SMiklos Szeredi struct nameidata *nd) 166227932742SMiklos Szeredi { 166327932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 166427932742SMiklos Szeredi } 166527932742SMiklos Szeredi 166627932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 166727932742SMiklos Szeredi { 166827932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 166927932742SMiklos Szeredi proc_fdinfo_instantiate); 167027932742SMiklos Szeredi } 167127932742SMiklos Szeredi 167227932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 167327932742SMiklos Szeredi .read = generic_read_dir, 167427932742SMiklos Szeredi .readdir = proc_readfdinfo, 167527932742SMiklos Szeredi }; 167627932742SMiklos Szeredi 167727932742SMiklos Szeredi /* 167827932742SMiklos Szeredi * proc directories can do almost nothing.. 167927932742SMiklos Szeredi */ 168027932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 168127932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 168227932742SMiklos Szeredi .setattr = proc_setattr, 168327932742SMiklos Szeredi }; 168427932742SMiklos Szeredi 168527932742SMiklos Szeredi 1686444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1687c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1688444ceed8SEric W. Biederman { 1689c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1690444ceed8SEric W. Biederman struct inode *inode; 1691444ceed8SEric W. Biederman struct proc_inode *ei; 1692444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1693444ceed8SEric W. Biederman 169461a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1695444ceed8SEric W. Biederman if (!inode) 1696444ceed8SEric W. Biederman goto out; 1697444ceed8SEric W. Biederman 1698444ceed8SEric W. Biederman ei = PROC_I(inode); 1699444ceed8SEric W. Biederman inode->i_mode = p->mode; 1700444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1701444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1702444ceed8SEric W. Biederman if (p->iop) 1703444ceed8SEric W. Biederman inode->i_op = p->iop; 1704444ceed8SEric W. Biederman if (p->fop) 1705444ceed8SEric W. Biederman inode->i_fop = p->fop; 1706444ceed8SEric W. Biederman ei->op = p->op; 1707444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1708444ceed8SEric W. Biederman d_add(dentry, inode); 1709444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1710444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1711444ceed8SEric W. Biederman error = NULL; 1712444ceed8SEric W. Biederman out: 1713444ceed8SEric W. Biederman return error; 1714444ceed8SEric W. Biederman } 1715444ceed8SEric W. Biederman 17161da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 17171da177e4SLinus Torvalds struct dentry *dentry, 1718c5141e6dSEric Dumazet const struct pid_entry *ents, 17197bcd6b0eSEric W. Biederman unsigned int nents) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds struct inode *inode; 1722cd6a3ce9SEric W. Biederman struct dentry *error; 172399f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1724c5141e6dSEric Dumazet const struct pid_entry *p, *last; 17251da177e4SLinus Torvalds 1726cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 17271da177e4SLinus Torvalds inode = NULL; 17281da177e4SLinus Torvalds 172999f89551SEric W. Biederman if (!task) 173099f89551SEric W. Biederman goto out_no_task; 17311da177e4SLinus Torvalds 173220cdc894SEric W. Biederman /* 173320cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 173420cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 173520cdc894SEric W. Biederman */ 17367bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 17377bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 17381da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 17391da177e4SLinus Torvalds continue; 17401da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 17411da177e4SLinus Torvalds break; 17421da177e4SLinus Torvalds } 17437bcd6b0eSEric W. Biederman if (p > last) 17441da177e4SLinus Torvalds goto out; 17451da177e4SLinus Torvalds 1746444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 17471da177e4SLinus Torvalds out: 174899f89551SEric W. Biederman put_task_struct(task); 174999f89551SEric W. Biederman out_no_task: 1750cd6a3ce9SEric W. Biederman return error; 17511da177e4SLinus Torvalds } 17521da177e4SLinus Torvalds 1753c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1754c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 175561a28784SEric W. Biederman { 175661a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 175761a28784SEric W. Biederman proc_pident_instantiate, task, p); 175861a28784SEric W. Biederman } 175961a28784SEric W. Biederman 176028a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 176128a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1762c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 176328a6d671SEric W. Biederman { 176428a6d671SEric W. Biederman int i; 17652fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 176628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 176728a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1768c5141e6dSEric Dumazet const struct pid_entry *p, *last; 176928a6d671SEric W. Biederman ino_t ino; 177028a6d671SEric W. Biederman int ret; 177128a6d671SEric W. Biederman 177228a6d671SEric W. Biederman ret = -ENOENT; 177328a6d671SEric W. Biederman if (!task) 177461a28784SEric W. Biederman goto out_no_task; 177528a6d671SEric W. Biederman 177628a6d671SEric W. Biederman ret = 0; 177728a6d671SEric W. Biederman i = filp->f_pos; 177828a6d671SEric W. Biederman switch (i) { 177928a6d671SEric W. Biederman case 0: 178028a6d671SEric W. Biederman ino = inode->i_ino; 178128a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 178228a6d671SEric W. Biederman goto out; 178328a6d671SEric W. Biederman i++; 178428a6d671SEric W. Biederman filp->f_pos++; 178528a6d671SEric W. Biederman /* fall through */ 178628a6d671SEric W. Biederman case 1: 178728a6d671SEric W. Biederman ino = parent_ino(dentry); 178828a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 178928a6d671SEric W. Biederman goto out; 179028a6d671SEric W. Biederman i++; 179128a6d671SEric W. Biederman filp->f_pos++; 179228a6d671SEric W. Biederman /* fall through */ 179328a6d671SEric W. Biederman default: 179428a6d671SEric W. Biederman i -= 2; 179528a6d671SEric W. Biederman if (i >= nents) { 179628a6d671SEric W. Biederman ret = 1; 179728a6d671SEric W. Biederman goto out; 179828a6d671SEric W. Biederman } 179928a6d671SEric W. Biederman p = ents + i; 18007bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 18017bcd6b0eSEric W. Biederman while (p <= last) { 180261a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 180328a6d671SEric W. Biederman goto out; 180428a6d671SEric W. Biederman filp->f_pos++; 180528a6d671SEric W. Biederman p++; 180628a6d671SEric W. Biederman } 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds 180928a6d671SEric W. Biederman ret = 1; 181028a6d671SEric W. Biederman out: 181161a28784SEric W. Biederman put_task_struct(task); 181261a28784SEric W. Biederman out_no_task: 181328a6d671SEric W. Biederman return ret; 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds 18161da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 181728a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 181828a6d671SEric W. Biederman size_t count, loff_t *ppos) 181928a6d671SEric W. Biederman { 18202fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 182104ff9708SAl Viro char *p = NULL; 182228a6d671SEric W. Biederman ssize_t length; 182328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 182428a6d671SEric W. Biederman 182528a6d671SEric W. Biederman if (!task) 182604ff9708SAl Viro return -ESRCH; 182728a6d671SEric W. Biederman 182828a6d671SEric W. Biederman length = security_getprocattr(task, 18292fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 183004ff9708SAl Viro &p); 183128a6d671SEric W. Biederman put_task_struct(task); 183204ff9708SAl Viro if (length > 0) 183304ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 183404ff9708SAl Viro kfree(p); 183528a6d671SEric W. Biederman return length; 183628a6d671SEric W. Biederman } 183728a6d671SEric W. Biederman 183828a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 183928a6d671SEric W. Biederman size_t count, loff_t *ppos) 184028a6d671SEric W. Biederman { 18412fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 184228a6d671SEric W. Biederman char *page; 184328a6d671SEric W. Biederman ssize_t length; 184428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 184528a6d671SEric W. Biederman 184628a6d671SEric W. Biederman length = -ESRCH; 184728a6d671SEric W. Biederman if (!task) 184828a6d671SEric W. Biederman goto out_no_task; 184928a6d671SEric W. Biederman if (count > PAGE_SIZE) 185028a6d671SEric W. Biederman count = PAGE_SIZE; 185128a6d671SEric W. Biederman 185228a6d671SEric W. Biederman /* No partial writes. */ 185328a6d671SEric W. Biederman length = -EINVAL; 185428a6d671SEric W. Biederman if (*ppos != 0) 185528a6d671SEric W. Biederman goto out; 185628a6d671SEric W. Biederman 185728a6d671SEric W. Biederman length = -ENOMEM; 1858e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 185928a6d671SEric W. Biederman if (!page) 186028a6d671SEric W. Biederman goto out; 186128a6d671SEric W. Biederman 186228a6d671SEric W. Biederman length = -EFAULT; 186328a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 186428a6d671SEric W. Biederman goto out_free; 186528a6d671SEric W. Biederman 186628a6d671SEric W. Biederman length = security_setprocattr(task, 18672fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 186828a6d671SEric W. Biederman (void*)page, count); 186928a6d671SEric W. Biederman out_free: 187028a6d671SEric W. Biederman free_page((unsigned long) page); 187128a6d671SEric W. Biederman out: 187228a6d671SEric W. Biederman put_task_struct(task); 187328a6d671SEric W. Biederman out_no_task: 187428a6d671SEric W. Biederman return length; 187528a6d671SEric W. Biederman } 187628a6d671SEric W. Biederman 187700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 187828a6d671SEric W. Biederman .read = proc_pid_attr_read, 187928a6d671SEric W. Biederman .write = proc_pid_attr_write, 188028a6d671SEric W. Biederman }; 188128a6d671SEric W. Biederman 1882c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 188361a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 188461a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 188561a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 188661a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 188761a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 188861a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 188928a6d671SEric W. Biederman }; 189028a6d671SEric W. Biederman 189172d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 18921da177e4SLinus Torvalds void * dirent, filldir_t filldir) 18931da177e4SLinus Torvalds { 18941da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 189572d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 18961da177e4SLinus Torvalds } 18971da177e4SLinus Torvalds 189800977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 18991da177e4SLinus Torvalds .read = generic_read_dir, 190072d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 19011da177e4SLinus Torvalds }; 19021da177e4SLinus Torvalds 190372d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 19041da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 19051da177e4SLinus Torvalds { 19067bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 19077bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 1910c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 191172d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 191299f89551SEric W. Biederman .getattr = pid_getattr, 19136d76fa58SLinus Torvalds .setattr = proc_setattr, 19141da177e4SLinus Torvalds }; 19151da177e4SLinus Torvalds 19161da177e4SLinus Torvalds #endif 19171da177e4SLinus Torvalds 19183cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 19193cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 19203cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 19213cb4a0bbSKawai, Hidehiro { 19223cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 19233cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 19243cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 19253cb4a0bbSKawai, Hidehiro size_t len; 19263cb4a0bbSKawai, Hidehiro int ret; 19273cb4a0bbSKawai, Hidehiro 19283cb4a0bbSKawai, Hidehiro if (!task) 19293cb4a0bbSKawai, Hidehiro return -ESRCH; 19303cb4a0bbSKawai, Hidehiro 19313cb4a0bbSKawai, Hidehiro ret = 0; 19323cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 19333cb4a0bbSKawai, Hidehiro if (mm) { 19343cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 19353cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 19363cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 19373cb4a0bbSKawai, Hidehiro mmput(mm); 19383cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 19393cb4a0bbSKawai, Hidehiro } 19403cb4a0bbSKawai, Hidehiro 19413cb4a0bbSKawai, Hidehiro put_task_struct(task); 19423cb4a0bbSKawai, Hidehiro 19433cb4a0bbSKawai, Hidehiro return ret; 19443cb4a0bbSKawai, Hidehiro } 19453cb4a0bbSKawai, Hidehiro 19463cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 19473cb4a0bbSKawai, Hidehiro const char __user *buf, 19483cb4a0bbSKawai, Hidehiro size_t count, 19493cb4a0bbSKawai, Hidehiro loff_t *ppos) 19503cb4a0bbSKawai, Hidehiro { 19513cb4a0bbSKawai, Hidehiro struct task_struct *task; 19523cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 19533cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 19543cb4a0bbSKawai, Hidehiro unsigned int val; 19553cb4a0bbSKawai, Hidehiro int ret; 19563cb4a0bbSKawai, Hidehiro int i; 19573cb4a0bbSKawai, Hidehiro unsigned long mask; 19583cb4a0bbSKawai, Hidehiro 19593cb4a0bbSKawai, Hidehiro ret = -EFAULT; 19603cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 19613cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 19623cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 19633cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 19643cb4a0bbSKawai, Hidehiro goto out_no_task; 19653cb4a0bbSKawai, Hidehiro 19663cb4a0bbSKawai, Hidehiro ret = -EINVAL; 19673cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 19683cb4a0bbSKawai, Hidehiro if (*end == '\n') 19693cb4a0bbSKawai, Hidehiro end++; 19703cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 19713cb4a0bbSKawai, Hidehiro goto out_no_task; 19723cb4a0bbSKawai, Hidehiro 19733cb4a0bbSKawai, Hidehiro ret = -ESRCH; 19743cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 19753cb4a0bbSKawai, Hidehiro if (!task) 19763cb4a0bbSKawai, Hidehiro goto out_no_task; 19773cb4a0bbSKawai, Hidehiro 19783cb4a0bbSKawai, Hidehiro ret = end - buffer; 19793cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 19803cb4a0bbSKawai, Hidehiro if (!mm) 19813cb4a0bbSKawai, Hidehiro goto out_no_mm; 19823cb4a0bbSKawai, Hidehiro 19833cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 19843cb4a0bbSKawai, Hidehiro if (val & mask) 19853cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19863cb4a0bbSKawai, Hidehiro else 19873cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19883cb4a0bbSKawai, Hidehiro } 19893cb4a0bbSKawai, Hidehiro 19903cb4a0bbSKawai, Hidehiro mmput(mm); 19913cb4a0bbSKawai, Hidehiro out_no_mm: 19923cb4a0bbSKawai, Hidehiro put_task_struct(task); 19933cb4a0bbSKawai, Hidehiro out_no_task: 19943cb4a0bbSKawai, Hidehiro return ret; 19953cb4a0bbSKawai, Hidehiro } 19963cb4a0bbSKawai, Hidehiro 19973cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 19983cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 19993cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 20003cb4a0bbSKawai, Hidehiro }; 20013cb4a0bbSKawai, Hidehiro #endif 20023cb4a0bbSKawai, Hidehiro 20031da177e4SLinus Torvalds /* 20041da177e4SLinus Torvalds * /proc/self: 20051da177e4SLinus Torvalds */ 20061da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 20071da177e4SLinus Torvalds int buflen) 20081da177e4SLinus Torvalds { 20098578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2010b488893aSPavel Emelyanov sprintf(tmp, "%d", task_tgid_vnr(current)); 20111da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 20121da177e4SLinus Torvalds } 20131da177e4SLinus Torvalds 2014008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 20151da177e4SLinus Torvalds { 20168578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 2017b488893aSPavel Emelyanov sprintf(tmp, "%d", task_tgid_vnr(current)); 2018008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 2021c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 20221da177e4SLinus Torvalds .readlink = proc_self_readlink, 20231da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 20241da177e4SLinus Torvalds }; 20251da177e4SLinus Torvalds 202628a6d671SEric W. Biederman /* 2027801199ceSEric W. Biederman * proc base 2028801199ceSEric W. Biederman * 2029801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 2030801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 2031801199ceSEric W. Biederman * describe something that is process related. 2032801199ceSEric W. Biederman */ 2033c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 203461a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 2035801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 2036801199ceSEric W. Biederman }; 2037801199ceSEric W. Biederman 2038801199ceSEric W. Biederman /* 2039801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 2040801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 2041801199ceSEric W. Biederman * due to the way we treat inodes. 2042801199ceSEric W. Biederman */ 2043801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 2044801199ceSEric W. Biederman { 2045801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 2046801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 2047801199ceSEric W. Biederman if (task) { 2048801199ceSEric W. Biederman put_task_struct(task); 2049801199ceSEric W. Biederman return 1; 2050801199ceSEric W. Biederman } 2051801199ceSEric W. Biederman d_drop(dentry); 2052801199ceSEric W. Biederman return 0; 2053801199ceSEric W. Biederman } 2054801199ceSEric W. Biederman 2055801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 2056801199ceSEric W. Biederman { 2057801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 2058801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 2059801199ceSEric W. Biederman }; 2060801199ceSEric W. Biederman 2061444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 2062c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2063801199ceSEric W. Biederman { 2064c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 2065801199ceSEric W. Biederman struct inode *inode; 2066801199ceSEric W. Biederman struct proc_inode *ei; 2067444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 2068801199ceSEric W. Biederman 2069801199ceSEric W. Biederman /* Allocate the inode */ 2070801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2071801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2072801199ceSEric W. Biederman if (!inode) 2073801199ceSEric W. Biederman goto out; 2074801199ceSEric W. Biederman 2075801199ceSEric W. Biederman /* Initialize the inode */ 2076801199ceSEric W. Biederman ei = PROC_I(inode); 2077801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2078801199ceSEric W. Biederman 2079801199ceSEric W. Biederman /* 2080801199ceSEric W. Biederman * grab the reference to the task. 2081801199ceSEric W. Biederman */ 20821a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2083801199ceSEric W. Biederman if (!ei->pid) 2084801199ceSEric W. Biederman goto out_iput; 2085801199ceSEric W. Biederman 2086801199ceSEric W. Biederman inode->i_uid = 0; 2087801199ceSEric W. Biederman inode->i_gid = 0; 2088801199ceSEric W. Biederman inode->i_mode = p->mode; 2089801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2090801199ceSEric W. Biederman inode->i_nlink = 2; 2091801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2092801199ceSEric W. Biederman inode->i_size = 64; 2093801199ceSEric W. Biederman if (p->iop) 2094801199ceSEric W. Biederman inode->i_op = p->iop; 2095801199ceSEric W. Biederman if (p->fop) 2096801199ceSEric W. Biederman inode->i_fop = p->fop; 2097801199ceSEric W. Biederman ei->op = p->op; 2098801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2099801199ceSEric W. Biederman d_add(dentry, inode); 2100801199ceSEric W. Biederman error = NULL; 2101801199ceSEric W. Biederman out: 2102801199ceSEric W. Biederman return error; 2103801199ceSEric W. Biederman out_iput: 2104801199ceSEric W. Biederman iput(inode); 2105801199ceSEric W. Biederman goto out; 2106801199ceSEric W. Biederman } 2107801199ceSEric W. Biederman 2108444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2109444ceed8SEric W. Biederman { 2110444ceed8SEric W. Biederman struct dentry *error; 2111444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2112c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2113444ceed8SEric W. Biederman 2114444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2115444ceed8SEric W. Biederman 2116444ceed8SEric W. Biederman if (!task) 2117444ceed8SEric W. Biederman goto out_no_task; 2118444ceed8SEric W. Biederman 2119444ceed8SEric W. Biederman /* Lookup the directory entry */ 21207bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 21217bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2122444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2123444ceed8SEric W. Biederman continue; 2124444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2125444ceed8SEric W. Biederman break; 2126444ceed8SEric W. Biederman } 21277bcd6b0eSEric W. Biederman if (p > last) 2128444ceed8SEric W. Biederman goto out; 2129444ceed8SEric W. Biederman 2130444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2131444ceed8SEric W. Biederman 2132444ceed8SEric W. Biederman out: 2133444ceed8SEric W. Biederman put_task_struct(task); 2134444ceed8SEric W. Biederman out_no_task: 2135444ceed8SEric W. Biederman return error; 2136444ceed8SEric W. Biederman } 2137444ceed8SEric W. Biederman 2138c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2139c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 214061a28784SEric W. Biederman { 214161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 214261a28784SEric W. Biederman proc_base_instantiate, task, p); 214361a28784SEric W. Biederman } 214461a28784SEric W. Biederman 2145aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2146aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2147aba76fdbSAndrew Morton { 2148aba76fdbSAndrew Morton return sprintf(buffer, 21494b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2150aba76fdbSAndrew Morton "rchar: %llu\n" 2151aba76fdbSAndrew Morton "wchar: %llu\n" 2152aba76fdbSAndrew Morton "syscr: %llu\n" 2153aba76fdbSAndrew Morton "syscw: %llu\n" 21544b98d11bSAlexey Dobriyan #endif 2155aba76fdbSAndrew Morton "read_bytes: %llu\n" 2156aba76fdbSAndrew Morton "write_bytes: %llu\n" 2157aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 21584b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2159aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2160aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2161aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2162aba76fdbSAndrew Morton (unsigned long long)task->syscw, 21634b98d11bSAlexey Dobriyan #endif 2164aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2165aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2166aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2167aba76fdbSAndrew Morton } 2168aba76fdbSAndrew Morton #endif 2169aba76fdbSAndrew Morton 2170801199ceSEric W. Biederman /* 217128a6d671SEric W. Biederman * Thread groups 217228a6d671SEric W. Biederman */ 217300977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2174c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 217520cdc894SEric W. Biederman 2176c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 217761a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 217861a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 217927932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2180315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 218161a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 218261a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 2183d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 218443ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 218543ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 218643ae34cbSIngo Molnar #endif 218761a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 218861a28784SEric W. Biederman INF("stat", S_IRUGO, tgid_stat), 218961a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 219061a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 219128a6d671SEric W. Biederman #ifdef CONFIG_NUMA 219261a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 219328a6d671SEric W. Biederman #endif 219461a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 219561a28784SEric W. Biederman LNK("cwd", cwd), 219661a28784SEric W. Biederman LNK("root", root), 219761a28784SEric W. Biederman LNK("exe", exe), 219861a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 219961a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 220028a6d671SEric W. Biederman #ifdef CONFIG_MMU 2201b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 220261a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 220328a6d671SEric W. Biederman #endif 220428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 220572d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 220628a6d671SEric W. Biederman #endif 220728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 220861a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 220928a6d671SEric W. Biederman #endif 221028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 221161a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 221228a6d671SEric W. Biederman #endif 22138793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 221461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 221528a6d671SEric W. Biederman #endif 2216a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2217a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2218a424316cSPaul Menage #endif 221961a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 222061a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 222128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 222261a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 222328a6d671SEric W. Biederman #endif 2224f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2225f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2226f4f154fdSAkinobu Mita #endif 22273cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 22283cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 22293cb4a0bbSKawai, Hidehiro #endif 2230aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2231aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2232aba76fdbSAndrew Morton #endif 223328a6d671SEric W. Biederman }; 223428a6d671SEric W. Biederman 223528a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 223628a6d671SEric W. Biederman void * dirent, filldir_t filldir) 223728a6d671SEric W. Biederman { 223828a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 223928a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 224028a6d671SEric W. Biederman } 224128a6d671SEric W. Biederman 224200977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 224328a6d671SEric W. Biederman .read = generic_read_dir, 224428a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 224528a6d671SEric W. Biederman }; 224628a6d671SEric W. Biederman 224728a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 22487bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 22497bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 225028a6d671SEric W. Biederman } 225128a6d671SEric W. Biederman 2252c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 225328a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 225428a6d671SEric W. Biederman .getattr = pid_getattr, 225528a6d671SEric W. Biederman .setattr = proc_setattr, 225628a6d671SEric W. Biederman }; 225728a6d671SEric W. Biederman 225860347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 22591da177e4SLinus Torvalds { 226048e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 22618578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 226248e6484dSEric W. Biederman struct qstr name; 22631da177e4SLinus Torvalds 226448e6484dSEric W. Biederman name.name = buf; 226560347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 226660347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 226748e6484dSEric W. Biederman if (dentry) { 226848e6484dSEric W. Biederman shrink_dcache_parent(dentry); 226948e6484dSEric W. Biederman d_drop(dentry); 227048e6484dSEric W. Biederman dput(dentry); 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 227360347f67SPavel Emelyanov if (tgid == 0) 227448e6484dSEric W. Biederman goto out; 22751da177e4SLinus Torvalds 227648e6484dSEric W. Biederman name.name = buf; 227760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 227860347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 227948e6484dSEric W. Biederman if (!leader) 228048e6484dSEric W. Biederman goto out; 228148e6484dSEric W. Biederman 228248e6484dSEric W. Biederman name.name = "task"; 228348e6484dSEric W. Biederman name.len = strlen(name.name); 228448e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 228548e6484dSEric W. Biederman if (!dir) 228648e6484dSEric W. Biederman goto out_put_leader; 228748e6484dSEric W. Biederman 228848e6484dSEric W. Biederman name.name = buf; 228960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 229048e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 229148e6484dSEric W. Biederman if (dentry) { 229248e6484dSEric W. Biederman shrink_dcache_parent(dentry); 229348e6484dSEric W. Biederman d_drop(dentry); 229448e6484dSEric W. Biederman dput(dentry); 22951da177e4SLinus Torvalds } 229648e6484dSEric W. Biederman 229748e6484dSEric W. Biederman dput(dir); 229848e6484dSEric W. Biederman out_put_leader: 229948e6484dSEric W. Biederman dput(leader); 230048e6484dSEric W. Biederman out: 230148e6484dSEric W. Biederman return; 23021da177e4SLinus Torvalds } 23031da177e4SLinus Torvalds 23040895e91dSRandy Dunlap /** 23050895e91dSRandy Dunlap * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 23060895e91dSRandy Dunlap * @task: task that should be flushed. 23070895e91dSRandy Dunlap * 23080895e91dSRandy Dunlap * When flushing dentries from proc, one needs to flush them from global 230960347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 23100895e91dSRandy Dunlap * in. This call is supposed to do all of this job. 23110895e91dSRandy Dunlap * 23120895e91dSRandy Dunlap * Looks in the dcache for 23130895e91dSRandy Dunlap * /proc/@pid 23140895e91dSRandy Dunlap * /proc/@tgid/task/@pid 23150895e91dSRandy Dunlap * if either directory is present flushes it and all of it'ts children 23160895e91dSRandy Dunlap * from the dcache. 23170895e91dSRandy Dunlap * 23180895e91dSRandy Dunlap * It is safe and reasonable to cache /proc entries for a task until 23190895e91dSRandy Dunlap * that task exits. After that they just clog up the dcache with 23200895e91dSRandy Dunlap * useless entries, possibly causing useful dcache entries to be 23210895e91dSRandy Dunlap * flushed instead. This routine is proved to flush those useless 23220895e91dSRandy Dunlap * dcache entries at process exit time. 23230895e91dSRandy Dunlap * 23240895e91dSRandy Dunlap * NOTE: This routine is just an optimization so it does not guarantee 23250895e91dSRandy Dunlap * that no dcache entries will exist at process exit time it 23260895e91dSRandy Dunlap * just makes it very unlikely that any will persist. 232760347f67SPavel Emelyanov */ 232860347f67SPavel Emelyanov 232960347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 233060347f67SPavel Emelyanov { 2331*9fcc2d15SEric W. Biederman int i; 2332*9fcc2d15SEric W. Biederman struct pid *pid, *tgid = NULL; 2333130f77ecSPavel Emelyanov struct upid *upid; 2334130f77ecSPavel Emelyanov 2335130f77ecSPavel Emelyanov pid = task_pid(task); 2336*9fcc2d15SEric W. Biederman if (thread_group_leader(task)) 2337130f77ecSPavel Emelyanov tgid = task_tgid(task); 2338*9fcc2d15SEric W. Biederman 2339*9fcc2d15SEric W. Biederman for (i = 0; i <= pid->level; i++) { 2340130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2341130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 2342*9fcc2d15SEric W. Biederman tgid ? tgid->numbers[i].nr : 0); 2343130f77ecSPavel Emelyanov } 23446f4e6433SPavel Emelyanov 23456f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 23466f4e6433SPavel Emelyanov if (upid->nr == 1) 23476f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 234860347f67SPavel Emelyanov } 234960347f67SPavel Emelyanov 23509711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 23519711ef99SAdrian Bunk struct dentry * dentry, 2352c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2353444ceed8SEric W. Biederman { 2354444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2355444ceed8SEric W. Biederman struct inode *inode; 2356444ceed8SEric W. Biederman 235761a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2358444ceed8SEric W. Biederman if (!inode) 2359444ceed8SEric W. Biederman goto out; 2360444ceed8SEric W. Biederman 2361444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2362444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2363444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2364444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 236527932742SMiklos Szeredi inode->i_nlink = 5; 2366444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2367444ceed8SEric W. Biederman inode->i_nlink += 1; 2368444ceed8SEric W. Biederman #endif 2369444ceed8SEric W. Biederman 2370444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2371444ceed8SEric W. Biederman 2372444ceed8SEric W. Biederman d_add(dentry, inode); 2373444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2374444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2375444ceed8SEric W. Biederman error = NULL; 2376444ceed8SEric W. Biederman out: 2377444ceed8SEric W. Biederman return error; 2378444ceed8SEric W. Biederman } 2379444ceed8SEric W. Biederman 23801da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 23811da177e4SLinus Torvalds { 2382cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 23831da177e4SLinus Torvalds struct task_struct *task; 23841da177e4SLinus Torvalds unsigned tgid; 2385b488893aSPavel Emelyanov struct pid_namespace *ns; 23861da177e4SLinus Torvalds 2387801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2388801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2389801199ceSEric W. Biederman goto out; 2390801199ceSEric W. Biederman 23911da177e4SLinus Torvalds tgid = name_to_int(dentry); 23921da177e4SLinus Torvalds if (tgid == ~0U) 23931da177e4SLinus Torvalds goto out; 23941da177e4SLinus Torvalds 2395b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 2396de758734SEric W. Biederman rcu_read_lock(); 2397b488893aSPavel Emelyanov task = find_task_by_pid_ns(tgid, ns); 23981da177e4SLinus Torvalds if (task) 23991da177e4SLinus Torvalds get_task_struct(task); 2400de758734SEric W. Biederman rcu_read_unlock(); 24011da177e4SLinus Torvalds if (!task) 24021da177e4SLinus Torvalds goto out; 24031da177e4SLinus Torvalds 2404444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 240548e6484dSEric W. Biederman put_task_struct(task); 24061da177e4SLinus Torvalds out: 2407cd6a3ce9SEric W. Biederman return result; 24081da177e4SLinus Torvalds } 24091da177e4SLinus Torvalds 24101da177e4SLinus Torvalds /* 24110804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 24120bc58a91SEric W. Biederman * 24131da177e4SLinus Torvalds */ 2414b488893aSPavel Emelyanov static struct task_struct *next_tgid(unsigned int tgid, 2415b488893aSPavel Emelyanov struct pid_namespace *ns) 24161da177e4SLinus Torvalds { 24170804ef4bSEric W. Biederman struct task_struct *task; 24180804ef4bSEric W. Biederman struct pid *pid; 24191da177e4SLinus Torvalds 24200804ef4bSEric W. Biederman rcu_read_lock(); 24210804ef4bSEric W. Biederman retry: 24220804ef4bSEric W. Biederman task = NULL; 2423b488893aSPavel Emelyanov pid = find_ge_pid(tgid, ns); 24240804ef4bSEric W. Biederman if (pid) { 2425b488893aSPavel Emelyanov tgid = pid_nr_ns(pid, ns) + 1; 24260804ef4bSEric W. Biederman task = pid_task(pid, PIDTYPE_PID); 24270804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 24280804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 24290804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 24300804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 24310804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 24320804ef4bSEric W. Biederman * 24330804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 24340804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 24350804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 24360804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 24370804ef4bSEric W. Biederman * As we don't care in the case of readdir. 24380bc58a91SEric W. Biederman */ 24390804ef4bSEric W. Biederman if (!task || !has_group_leader_pid(task)) 24400804ef4bSEric W. Biederman goto retry; 24410804ef4bSEric W. Biederman get_task_struct(task); 24421da177e4SLinus Torvalds } 2443454cc105SEric W. Biederman rcu_read_unlock(); 24440804ef4bSEric W. Biederman return task; 24451da177e4SLinus Torvalds } 24461da177e4SLinus Torvalds 24477bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 24481da177e4SLinus Torvalds 244961a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 245061a28784SEric W. Biederman struct task_struct *task, int tgid) 245161a28784SEric W. Biederman { 245261a28784SEric W. Biederman char name[PROC_NUMBUF]; 245361a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tgid); 245461a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 245561a28784SEric W. Biederman proc_pid_instantiate, task, NULL); 245661a28784SEric W. Biederman } 245761a28784SEric W. Biederman 24581da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 24591da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 24601da177e4SLinus Torvalds { 24611da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 24622fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 24630bc58a91SEric W. Biederman struct task_struct *task; 24640bc58a91SEric W. Biederman int tgid; 2465b488893aSPavel Emelyanov struct pid_namespace *ns; 24661da177e4SLinus Torvalds 246761a28784SEric W. Biederman if (!reaper) 246861a28784SEric W. Biederman goto out_no_task; 246961a28784SEric W. Biederman 24707bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2471c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 247261a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2473801199ceSEric W. Biederman goto out; 24741da177e4SLinus Torvalds } 24751da177e4SLinus Torvalds 2476b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 24770804ef4bSEric W. Biederman tgid = filp->f_pos - TGID_OFFSET; 2478b488893aSPavel Emelyanov for (task = next_tgid(tgid, ns); 24790bc58a91SEric W. Biederman task; 2480b488893aSPavel Emelyanov put_task_struct(task), task = next_tgid(tgid + 1, ns)) { 2481b488893aSPavel Emelyanov tgid = task_pid_nr_ns(task, ns); 24820804ef4bSEric W. Biederman filp->f_pos = tgid + TGID_OFFSET; 248361a28784SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) { 24840bc58a91SEric W. Biederman put_task_struct(task); 24850804ef4bSEric W. Biederman goto out; 24861da177e4SLinus Torvalds } 24871da177e4SLinus Torvalds } 24880804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 24890804ef4bSEric W. Biederman out: 249061a28784SEric W. Biederman put_task_struct(reaper); 249161a28784SEric W. Biederman out_no_task: 24921da177e4SLinus Torvalds return 0; 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds 24950bc58a91SEric W. Biederman /* 249628a6d671SEric W. Biederman * Tasks 249728a6d671SEric W. Biederman */ 2498c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 249961a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 250027932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2501315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 250261a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 250361a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 2504d85f50d5SNeil Horman INF("limits", S_IRUSR, pid_limits), 250543ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 250643ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 250743ae34cbSIngo Molnar #endif 250861a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 250961a28784SEric W. Biederman INF("stat", S_IRUGO, tid_stat), 251061a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 251161a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 251228a6d671SEric W. Biederman #ifdef CONFIG_NUMA 251361a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 251428a6d671SEric W. Biederman #endif 251561a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 251661a28784SEric W. Biederman LNK("cwd", cwd), 251761a28784SEric W. Biederman LNK("root", root), 251861a28784SEric W. Biederman LNK("exe", exe), 251961a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 252028a6d671SEric W. Biederman #ifdef CONFIG_MMU 2521b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 252261a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 252328a6d671SEric W. Biederman #endif 252428a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 252572d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 252628a6d671SEric W. Biederman #endif 252728a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 252861a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 252928a6d671SEric W. Biederman #endif 253028a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 253161a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 253228a6d671SEric W. Biederman #endif 25338793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 253461a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 253528a6d671SEric W. Biederman #endif 2536a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2537a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2538a424316cSPaul Menage #endif 253961a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 254061a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 254128a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 254261a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 254328a6d671SEric W. Biederman #endif 2544f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2545f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2546f4f154fdSAkinobu Mita #endif 254728a6d671SEric W. Biederman }; 254828a6d671SEric W. Biederman 254928a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 255028a6d671SEric W. Biederman void * dirent, filldir_t filldir) 255128a6d671SEric W. Biederman { 255228a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 255328a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 255428a6d671SEric W. Biederman } 255528a6d671SEric W. Biederman 255628a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 25577bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 25587bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 255928a6d671SEric W. Biederman } 256028a6d671SEric W. Biederman 256100977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 256228a6d671SEric W. Biederman .read = generic_read_dir, 256328a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 256428a6d671SEric W. Biederman }; 256528a6d671SEric W. Biederman 2566c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 256728a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 256828a6d671SEric W. Biederman .getattr = pid_getattr, 256928a6d671SEric W. Biederman .setattr = proc_setattr, 257028a6d671SEric W. Biederman }; 257128a6d671SEric W. Biederman 2572444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2573c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2574444ceed8SEric W. Biederman { 2575444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2576444ceed8SEric W. Biederman struct inode *inode; 257761a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2578444ceed8SEric W. Biederman 2579444ceed8SEric W. Biederman if (!inode) 2580444ceed8SEric W. Biederman goto out; 2581444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2582444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2583444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2584444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 258527932742SMiklos Szeredi inode->i_nlink = 4; 2586444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2587444ceed8SEric W. Biederman inode->i_nlink += 1; 2588444ceed8SEric W. Biederman #endif 2589444ceed8SEric W. Biederman 2590444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2591444ceed8SEric W. Biederman 2592444ceed8SEric W. Biederman d_add(dentry, inode); 2593444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2594444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2595444ceed8SEric W. Biederman error = NULL; 2596444ceed8SEric W. Biederman out: 2597444ceed8SEric W. Biederman return error; 2598444ceed8SEric W. Biederman } 2599444ceed8SEric W. Biederman 260028a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 260128a6d671SEric W. Biederman { 260228a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 260328a6d671SEric W. Biederman struct task_struct *task; 260428a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 260528a6d671SEric W. Biederman unsigned tid; 2606b488893aSPavel Emelyanov struct pid_namespace *ns; 260728a6d671SEric W. Biederman 260828a6d671SEric W. Biederman if (!leader) 260928a6d671SEric W. Biederman goto out_no_task; 261028a6d671SEric W. Biederman 261128a6d671SEric W. Biederman tid = name_to_int(dentry); 261228a6d671SEric W. Biederman if (tid == ~0U) 261328a6d671SEric W. Biederman goto out; 261428a6d671SEric W. Biederman 2615b488893aSPavel Emelyanov ns = dentry->d_sb->s_fs_info; 261628a6d671SEric W. Biederman rcu_read_lock(); 2617b488893aSPavel Emelyanov task = find_task_by_pid_ns(tid, ns); 261828a6d671SEric W. Biederman if (task) 261928a6d671SEric W. Biederman get_task_struct(task); 262028a6d671SEric W. Biederman rcu_read_unlock(); 262128a6d671SEric W. Biederman if (!task) 262228a6d671SEric W. Biederman goto out; 2623bac0abd6SPavel Emelyanov if (!same_thread_group(leader, task)) 262428a6d671SEric W. Biederman goto out_drop_task; 262528a6d671SEric W. Biederman 2626444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 262728a6d671SEric W. Biederman out_drop_task: 262828a6d671SEric W. Biederman put_task_struct(task); 262928a6d671SEric W. Biederman out: 263028a6d671SEric W. Biederman put_task_struct(leader); 263128a6d671SEric W. Biederman out_no_task: 263228a6d671SEric W. Biederman return result; 263328a6d671SEric W. Biederman } 263428a6d671SEric W. Biederman 263528a6d671SEric W. Biederman /* 26360bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 26370bc58a91SEric W. Biederman * 26380bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 26390bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 26400bc58a91SEric W. Biederman * directory we have more work todo. 26410bc58a91SEric W. Biederman * 26420bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 26430bc58a91SEric W. Biederman * 26440bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 26450bc58a91SEric W. Biederman * threads past it. 26460bc58a91SEric W. Biederman */ 2647cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2648b488893aSPavel Emelyanov int tid, int nr, struct pid_namespace *ns) 26490bc58a91SEric W. Biederman { 2650a872ff0cSOleg Nesterov struct task_struct *pos; 26510bc58a91SEric W. Biederman 2652cc288738SEric W. Biederman rcu_read_lock(); 26530bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 26540bc58a91SEric W. Biederman if (tid && (nr > 0)) { 2655b488893aSPavel Emelyanov pos = find_task_by_pid_ns(tid, ns); 2656a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2657a872ff0cSOleg Nesterov goto found; 26580bc58a91SEric W. Biederman } 26590bc58a91SEric W. Biederman 26600bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 26610bc58a91SEric W. Biederman pos = NULL; 2662a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2663a872ff0cSOleg Nesterov goto out; 2664a872ff0cSOleg Nesterov 2665a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2666a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2667a872ff0cSOleg Nesterov */ 2668a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2669a872ff0cSOleg Nesterov pos = next_thread(pos); 2670a872ff0cSOleg Nesterov if (pos == leader) { 2671a872ff0cSOleg Nesterov pos = NULL; 2672a872ff0cSOleg Nesterov goto out; 2673a872ff0cSOleg Nesterov } 2674a872ff0cSOleg Nesterov } 2675a872ff0cSOleg Nesterov found: 2676a872ff0cSOleg Nesterov get_task_struct(pos); 2677a872ff0cSOleg Nesterov out: 2678cc288738SEric W. Biederman rcu_read_unlock(); 26790bc58a91SEric W. Biederman return pos; 26800bc58a91SEric W. Biederman } 26810bc58a91SEric W. Biederman 26820bc58a91SEric W. Biederman /* 26830bc58a91SEric W. Biederman * Find the next thread in the thread list. 26840bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 26850bc58a91SEric W. Biederman * 26860bc58a91SEric W. Biederman * The reference to the input task_struct is released. 26870bc58a91SEric W. Biederman */ 26880bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 26890bc58a91SEric W. Biederman { 2690c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2691cc288738SEric W. Biederman rcu_read_lock(); 2692c1df7fb8SOleg Nesterov if (pid_alive(start)) { 26930bc58a91SEric W. Biederman pos = next_thread(start); 2694c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 26950bc58a91SEric W. Biederman pos = NULL; 2696c1df7fb8SOleg Nesterov else 2697c1df7fb8SOleg Nesterov get_task_struct(pos); 2698c1df7fb8SOleg Nesterov } 2699cc288738SEric W. Biederman rcu_read_unlock(); 27000bc58a91SEric W. Biederman put_task_struct(start); 27010bc58a91SEric W. Biederman return pos; 27020bc58a91SEric W. Biederman } 27030bc58a91SEric W. Biederman 270461a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 270561a28784SEric W. Biederman struct task_struct *task, int tid) 270661a28784SEric W. Biederman { 270761a28784SEric W. Biederman char name[PROC_NUMBUF]; 270861a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 270961a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 271061a28784SEric W. Biederman proc_task_instantiate, task, NULL); 271161a28784SEric W. Biederman } 271261a28784SEric W. Biederman 27131da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 27141da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 27151da177e4SLinus Torvalds { 27162fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 27171da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 27187d895244SGuillaume Chazarain struct task_struct *leader = NULL; 27190bc58a91SEric W. Biederman struct task_struct *task; 27201da177e4SLinus Torvalds int retval = -ENOENT; 27211da177e4SLinus Torvalds ino_t ino; 27220bc58a91SEric W. Biederman int tid; 27231da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 2724b488893aSPavel Emelyanov struct pid_namespace *ns; 27251da177e4SLinus Torvalds 27267d895244SGuillaume Chazarain task = get_proc_task(inode); 27277d895244SGuillaume Chazarain if (!task) 27287d895244SGuillaume Chazarain goto out_no_task; 27297d895244SGuillaume Chazarain rcu_read_lock(); 27307d895244SGuillaume Chazarain if (pid_alive(task)) { 27317d895244SGuillaume Chazarain leader = task->group_leader; 27327d895244SGuillaume Chazarain get_task_struct(leader); 27337d895244SGuillaume Chazarain } 27347d895244SGuillaume Chazarain rcu_read_unlock(); 27357d895244SGuillaume Chazarain put_task_struct(task); 273699f89551SEric W. Biederman if (!leader) 273799f89551SEric W. Biederman goto out_no_task; 27381da177e4SLinus Torvalds retval = 0; 27391da177e4SLinus Torvalds 27401da177e4SLinus Torvalds switch (pos) { 27411da177e4SLinus Torvalds case 0: 27421da177e4SLinus Torvalds ino = inode->i_ino; 27431da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 27441da177e4SLinus Torvalds goto out; 27451da177e4SLinus Torvalds pos++; 27461da177e4SLinus Torvalds /* fall through */ 27471da177e4SLinus Torvalds case 1: 27481da177e4SLinus Torvalds ino = parent_ino(dentry); 27491da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 27501da177e4SLinus Torvalds goto out; 27511da177e4SLinus Torvalds pos++; 27521da177e4SLinus Torvalds /* fall through */ 27531da177e4SLinus Torvalds } 27541da177e4SLinus Torvalds 27550bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 27560bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 27570bc58a91SEric W. Biederman */ 2758b488893aSPavel Emelyanov ns = filp->f_dentry->d_sb->s_fs_info; 27592b47c361SMathieu Desnoyers tid = (int)filp->f_version; 27600bc58a91SEric W. Biederman filp->f_version = 0; 2761b488893aSPavel Emelyanov for (task = first_tid(leader, tid, pos - 2, ns); 27620bc58a91SEric W. Biederman task; 27630bc58a91SEric W. Biederman task = next_tid(task), pos++) { 2764b488893aSPavel Emelyanov tid = task_pid_nr_ns(task, ns); 276561a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 27660bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 27670bc58a91SEric W. Biederman * pid for the next readir call */ 27682b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 27690bc58a91SEric W. Biederman put_task_struct(task); 27701da177e4SLinus Torvalds break; 27710bc58a91SEric W. Biederman } 27721da177e4SLinus Torvalds } 27731da177e4SLinus Torvalds out: 27741da177e4SLinus Torvalds filp->f_pos = pos; 277599f89551SEric W. Biederman put_task_struct(leader); 277699f89551SEric W. Biederman out_no_task: 27771da177e4SLinus Torvalds return retval; 27781da177e4SLinus Torvalds } 27796e66b52bSEric W. Biederman 27806e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 27816e66b52bSEric W. Biederman { 27826e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 278399f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 27846e66b52bSEric W. Biederman generic_fillattr(inode, stat); 27856e66b52bSEric W. Biederman 278699f89551SEric W. Biederman if (p) { 278799f89551SEric W. Biederman rcu_read_lock(); 278899f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 278999f89551SEric W. Biederman rcu_read_unlock(); 279099f89551SEric W. Biederman put_task_struct(p); 27916e66b52bSEric W. Biederman } 27926e66b52bSEric W. Biederman 27936e66b52bSEric W. Biederman return 0; 27946e66b52bSEric W. Biederman } 279528a6d671SEric W. Biederman 2796c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 279728a6d671SEric W. Biederman .lookup = proc_task_lookup, 279828a6d671SEric W. Biederman .getattr = proc_task_getattr, 279928a6d671SEric W. Biederman .setattr = proc_setattr, 280028a6d671SEric W. Biederman }; 280128a6d671SEric W. Biederman 280200977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 280328a6d671SEric W. Biederman .read = generic_read_dir, 280428a6d671SEric W. Biederman .readdir = proc_task_readdir, 280528a6d671SEric W. Biederman }; 2806