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> 665096add8SKees Cook #include <linux/module.h> 671da177e4SLinus Torvalds #include <linux/mount.h> 681da177e4SLinus Torvalds #include <linux/security.h> 691da177e4SLinus Torvalds #include <linux/ptrace.h> 70a424316cSPaul Menage #include <linux/cgroup.h> 711da177e4SLinus Torvalds #include <linux/cpuset.h> 721da177e4SLinus Torvalds #include <linux/audit.h> 735addc5ddSAl Viro #include <linux/poll.h> 741651e14eSSerge E. Hallyn #include <linux/nsproxy.h> 758ac773b4SAlexey Dobriyan #include <linux/oom.h> 763cb4a0bbSKawai, Hidehiro #include <linux/elf.h> 7760347f67SPavel Emelyanov #include <linux/pid_namespace.h> 781da177e4SLinus Torvalds #include "internal.h" 791da177e4SLinus Torvalds 800f2fe20fSEric W. Biederman /* NOTE: 810f2fe20fSEric W. Biederman * Implementing inode permission operations in /proc is almost 820f2fe20fSEric W. Biederman * certainly an error. Permission checks need to happen during 830f2fe20fSEric W. Biederman * each system call not at open time. The reason is that most of 840f2fe20fSEric W. Biederman * what we wish to check for permissions in /proc varies at runtime. 850f2fe20fSEric W. Biederman * 860f2fe20fSEric W. Biederman * The classic example of a problem is opening file descriptors 870f2fe20fSEric W. Biederman * in /proc for a task before it execs a suid executable. 880f2fe20fSEric W. Biederman */ 890f2fe20fSEric W. Biederman 901da177e4SLinus Torvalds 918578cea7SEric W. Biederman /* Worst case buffer size needed for holding an integer. */ 920187f879SAndrew Morton #define PROC_NUMBUF 13 938578cea7SEric W. Biederman 941da177e4SLinus Torvalds struct pid_entry { 951da177e4SLinus Torvalds char *name; 96c5141e6dSEric Dumazet int len; 971da177e4SLinus Torvalds mode_t mode; 98c5ef1c42SArjan van de Ven const struct inode_operations *iop; 9900977a59SArjan van de Ven const struct file_operations *fop; 10020cdc894SEric W. Biederman union proc_op op; 1011da177e4SLinus Torvalds }; 1021da177e4SLinus Torvalds 10361a28784SEric W. Biederman #define NOD(NAME, MODE, IOP, FOP, OP) { \ 10420cdc894SEric W. Biederman .name = (NAME), \ 105c5141e6dSEric Dumazet .len = sizeof(NAME) - 1, \ 10620cdc894SEric W. Biederman .mode = MODE, \ 10720cdc894SEric W. Biederman .iop = IOP, \ 10820cdc894SEric W. Biederman .fop = FOP, \ 10920cdc894SEric W. Biederman .op = OP, \ 11020cdc894SEric W. Biederman } 11120cdc894SEric W. Biederman 11261a28784SEric W. Biederman #define DIR(NAME, MODE, OTYPE) \ 11361a28784SEric W. Biederman NOD(NAME, (S_IFDIR|(MODE)), \ 11420cdc894SEric W. Biederman &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \ 11520cdc894SEric W. Biederman {} ) 11661a28784SEric W. Biederman #define LNK(NAME, OTYPE) \ 11761a28784SEric W. Biederman NOD(NAME, (S_IFLNK|S_IRWXUGO), \ 11820cdc894SEric W. Biederman &proc_pid_link_inode_operations, NULL, \ 11920cdc894SEric W. Biederman { .proc_get_link = &proc_##OTYPE##_link } ) 12061a28784SEric W. Biederman #define REG(NAME, MODE, OTYPE) \ 12161a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), NULL, \ 12220cdc894SEric W. Biederman &proc_##OTYPE##_operations, {}) 12361a28784SEric W. Biederman #define INF(NAME, MODE, OTYPE) \ 12461a28784SEric W. Biederman NOD(NAME, (S_IFREG|(MODE)), \ 12520cdc894SEric W. Biederman NULL, &proc_info_file_operations, \ 12620cdc894SEric W. Biederman { .proc_read = &proc_##OTYPE } ) 1271da177e4SLinus Torvalds 1285096add8SKees Cook int maps_protect; 1295096add8SKees Cook EXPORT_SYMBOL(maps_protect); 1305096add8SKees Cook 1310494f6ecSMiklos Szeredi static struct fs_struct *get_fs_struct(struct task_struct *task) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds struct fs_struct *fs; 1340494f6ecSMiklos Szeredi task_lock(task); 1350494f6ecSMiklos Szeredi fs = task->fs; 1361da177e4SLinus Torvalds if(fs) 1371da177e4SLinus Torvalds atomic_inc(&fs->count); 1380494f6ecSMiklos Szeredi task_unlock(task); 1390494f6ecSMiklos Szeredi return fs; 1400494f6ecSMiklos Szeredi } 1410494f6ecSMiklos Szeredi 14299f89551SEric W. Biederman static int get_nr_threads(struct task_struct *tsk) 14399f89551SEric W. Biederman { 14499f89551SEric W. Biederman /* Must be called with the rcu_read_lock held */ 14599f89551SEric W. Biederman unsigned long flags; 14699f89551SEric W. Biederman int count = 0; 14799f89551SEric W. Biederman 14899f89551SEric W. Biederman if (lock_task_sighand(tsk, &flags)) { 14999f89551SEric W. Biederman count = atomic_read(&tsk->signal->count); 15099f89551SEric W. Biederman unlock_task_sighand(tsk, &flags); 15199f89551SEric W. Biederman } 15299f89551SEric W. Biederman return count; 15399f89551SEric W. Biederman } 15499f89551SEric W. Biederman 1550494f6ecSMiklos Szeredi static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1560494f6ecSMiklos Szeredi { 15799f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 15899f89551SEric W. Biederman struct fs_struct *fs = NULL; 1590494f6ecSMiklos Szeredi int result = -ENOENT; 16099f89551SEric W. Biederman 16199f89551SEric W. Biederman if (task) { 16299f89551SEric W. Biederman fs = get_fs_struct(task); 16399f89551SEric W. Biederman put_task_struct(task); 16499f89551SEric W. Biederman } 1651da177e4SLinus Torvalds if (fs) { 1661da177e4SLinus Torvalds read_lock(&fs->lock); 1671da177e4SLinus Torvalds *mnt = mntget(fs->pwdmnt); 1681da177e4SLinus Torvalds *dentry = dget(fs->pwd); 1691da177e4SLinus Torvalds read_unlock(&fs->lock); 1701da177e4SLinus Torvalds result = 0; 1711da177e4SLinus Torvalds put_fs_struct(fs); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds return result; 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt) 1771da177e4SLinus Torvalds { 17899f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 17999f89551SEric W. Biederman struct fs_struct *fs = NULL; 1801da177e4SLinus Torvalds int result = -ENOENT; 18199f89551SEric W. Biederman 18299f89551SEric W. Biederman if (task) { 18399f89551SEric W. Biederman fs = get_fs_struct(task); 18499f89551SEric W. Biederman put_task_struct(task); 18599f89551SEric W. Biederman } 1861da177e4SLinus Torvalds if (fs) { 1871da177e4SLinus Torvalds read_lock(&fs->lock); 1881da177e4SLinus Torvalds *mnt = mntget(fs->rootmnt); 1891da177e4SLinus Torvalds *dentry = dget(fs->root); 1901da177e4SLinus Torvalds read_unlock(&fs->lock); 1911da177e4SLinus Torvalds result = 0; 1921da177e4SLinus Torvalds put_fs_struct(fs); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds return result; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds #define MAY_PTRACE(task) \ 1981da177e4SLinus Torvalds (task == current || \ 1991da177e4SLinus Torvalds (task->parent == current && \ 2001da177e4SLinus Torvalds (task->ptrace & PT_PTRACED) && \ 2011da177e4SLinus Torvalds (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \ 2021da177e4SLinus Torvalds security_ptrace(current,task) == 0)) 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds static int proc_pid_cmdline(struct task_struct *task, char * buffer) 2051da177e4SLinus Torvalds { 2061da177e4SLinus Torvalds int res = 0; 2071da177e4SLinus Torvalds unsigned int len; 2081da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2091da177e4SLinus Torvalds if (!mm) 2101da177e4SLinus Torvalds goto out; 2111da177e4SLinus Torvalds if (!mm->arg_end) 2121da177e4SLinus Torvalds goto out_mm; /* Shh! No looking before we're done */ 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds len = mm->arg_end - mm->arg_start; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds if (len > PAGE_SIZE) 2171da177e4SLinus Torvalds len = PAGE_SIZE; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds res = access_process_vm(task, mm->arg_start, buffer, len, 0); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds // If the nul at the end of args has been overwritten, then 2221da177e4SLinus Torvalds // assume application is using setproctitle(3). 2231da177e4SLinus Torvalds if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) { 2241da177e4SLinus Torvalds len = strnlen(buffer, res); 2251da177e4SLinus Torvalds if (len < res) { 2261da177e4SLinus Torvalds res = len; 2271da177e4SLinus Torvalds } else { 2281da177e4SLinus Torvalds len = mm->env_end - mm->env_start; 2291da177e4SLinus Torvalds if (len > PAGE_SIZE - res) 2301da177e4SLinus Torvalds len = PAGE_SIZE - res; 2311da177e4SLinus Torvalds res += access_process_vm(task, mm->env_start, buffer+res, len, 0); 2321da177e4SLinus Torvalds res = strnlen(buffer, res); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds out_mm: 2361da177e4SLinus Torvalds mmput(mm); 2371da177e4SLinus Torvalds out: 2381da177e4SLinus Torvalds return res; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds static int proc_pid_auxv(struct task_struct *task, char *buffer) 2421da177e4SLinus Torvalds { 2431da177e4SLinus Torvalds int res = 0; 2441da177e4SLinus Torvalds struct mm_struct *mm = get_task_mm(task); 2451da177e4SLinus Torvalds if (mm) { 2461da177e4SLinus Torvalds unsigned int nwords = 0; 2471da177e4SLinus Torvalds do 2481da177e4SLinus Torvalds nwords += 2; 2491da177e4SLinus Torvalds while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */ 2501da177e4SLinus Torvalds res = nwords * sizeof(mm->saved_auxv[0]); 2511da177e4SLinus Torvalds if (res > PAGE_SIZE) 2521da177e4SLinus Torvalds res = PAGE_SIZE; 2531da177e4SLinus Torvalds memcpy(buffer, mm->saved_auxv, res); 2541da177e4SLinus Torvalds mmput(mm); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds return res; 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds #ifdef CONFIG_KALLSYMS 2611da177e4SLinus Torvalds /* 2621da177e4SLinus Torvalds * Provides a wchan file via kallsyms in a proper one-value-per-file format. 2631da177e4SLinus Torvalds * Returns the resolved symbol. If that fails, simply return the address. 2641da177e4SLinus Torvalds */ 2651da177e4SLinus Torvalds static int proc_pid_wchan(struct task_struct *task, char *buffer) 2661da177e4SLinus Torvalds { 267ffb45122SAlexey Dobriyan unsigned long wchan; 2689281aceaSTejun Heo char symname[KSYM_NAME_LEN]; 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds wchan = get_wchan(task); 2711da177e4SLinus Torvalds 2729d65cb4aSAlexey Dobriyan if (lookup_symbol_name(wchan, symname) < 0) 2731da177e4SLinus Torvalds return sprintf(buffer, "%lu", wchan); 2749d65cb4aSAlexey Dobriyan else 2759d65cb4aSAlexey Dobriyan return sprintf(buffer, "%s", symname); 2761da177e4SLinus Torvalds } 2771da177e4SLinus Torvalds #endif /* CONFIG_KALLSYMS */ 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds #ifdef CONFIG_SCHEDSTATS 2801da177e4SLinus Torvalds /* 2811da177e4SLinus Torvalds * Provides /proc/PID/schedstat 2821da177e4SLinus Torvalds */ 2831da177e4SLinus Torvalds static int proc_pid_schedstat(struct task_struct *task, char *buffer) 2841da177e4SLinus Torvalds { 285172ba844SBalbir Singh return sprintf(buffer, "%llu %llu %lu\n", 2861da177e4SLinus Torvalds task->sched_info.cpu_time, 2871da177e4SLinus Torvalds task->sched_info.run_delay, 2882d72376bSIngo Molnar task->sched_info.pcount); 2891da177e4SLinus Torvalds } 2901da177e4SLinus Torvalds #endif 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds /* The badness from the OOM killer */ 2931da177e4SLinus Torvalds unsigned long badness(struct task_struct *p, unsigned long uptime); 2941da177e4SLinus Torvalds static int proc_oom_score(struct task_struct *task, char *buffer) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds unsigned long points; 2971da177e4SLinus Torvalds struct timespec uptime; 2981da177e4SLinus Torvalds 2991da177e4SLinus Torvalds do_posix_clock_monotonic_gettime(&uptime); 30019c5d45aSAlexey Dobriyan read_lock(&tasklist_lock); 3011da177e4SLinus Torvalds points = badness(task, uptime.tv_sec); 30219c5d45aSAlexey Dobriyan read_unlock(&tasklist_lock); 3031da177e4SLinus Torvalds return sprintf(buffer, "%lu\n", points); 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /************************************************************************/ 3071da177e4SLinus Torvalds /* Here the fs part begins */ 3081da177e4SLinus Torvalds /************************************************************************/ 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /* permission checks */ 311778c1144SEric W. Biederman static int proc_fd_access_allowed(struct inode *inode) 3121da177e4SLinus Torvalds { 313778c1144SEric W. Biederman struct task_struct *task; 314778c1144SEric W. Biederman int allowed = 0; 315df26c40eSEric W. Biederman /* Allow access to a task's file descriptors if it is us or we 316df26c40eSEric W. Biederman * may use ptrace attach to the process and find out that 317df26c40eSEric W. Biederman * information. 318778c1144SEric W. Biederman */ 319778c1144SEric W. Biederman task = get_proc_task(inode); 320df26c40eSEric W. Biederman if (task) { 321778c1144SEric W. Biederman allowed = ptrace_may_attach(task); 322778c1144SEric W. Biederman put_task_struct(task); 323df26c40eSEric W. Biederman } 324778c1144SEric W. Biederman return allowed; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3276d76fa58SLinus Torvalds static int proc_setattr(struct dentry *dentry, struct iattr *attr) 3286d76fa58SLinus Torvalds { 3296d76fa58SLinus Torvalds int error; 3306d76fa58SLinus Torvalds struct inode *inode = dentry->d_inode; 3316d76fa58SLinus Torvalds 3326d76fa58SLinus Torvalds if (attr->ia_valid & ATTR_MODE) 3336d76fa58SLinus Torvalds return -EPERM; 3346d76fa58SLinus Torvalds 3356d76fa58SLinus Torvalds error = inode_change_ok(inode, attr); 3366d76fa58SLinus Torvalds if (!error) 3376d76fa58SLinus Torvalds error = inode_setattr(inode, attr); 3386d76fa58SLinus Torvalds return error; 3396d76fa58SLinus Torvalds } 3406d76fa58SLinus Torvalds 341c5ef1c42SArjan van de Ven static const struct inode_operations proc_def_inode_operations = { 3426d76fa58SLinus Torvalds .setattr = proc_setattr, 3436d76fa58SLinus Torvalds }; 3446d76fa58SLinus Torvalds 3451da177e4SLinus Torvalds extern struct seq_operations mounts_op; 3465addc5ddSAl Viro struct proc_mounts { 3475addc5ddSAl Viro struct seq_file m; 3485addc5ddSAl Viro int event; 3495addc5ddSAl Viro }; 3505addc5ddSAl Viro 3511da177e4SLinus Torvalds static int mounts_open(struct inode *inode, struct file *file) 3521da177e4SLinus Torvalds { 35399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 354cf7b708cSPavel Emelyanov struct nsproxy *nsp; 3556b3286edSKirill Korotaev struct mnt_namespace *ns = NULL; 3565addc5ddSAl Viro struct proc_mounts *p; 3575addc5ddSAl Viro int ret = -EINVAL; 3585addc5ddSAl Viro 35999f89551SEric W. Biederman if (task) { 360cf7b708cSPavel Emelyanov rcu_read_lock(); 361cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 362cf7b708cSPavel Emelyanov if (nsp) { 363cf7b708cSPavel Emelyanov ns = nsp->mnt_ns; 3646b3286edSKirill Korotaev if (ns) 3656b3286edSKirill Korotaev get_mnt_ns(ns); 366863c4702SAlexey Dobriyan } 367cf7b708cSPavel Emelyanov rcu_read_unlock(); 368cf7b708cSPavel Emelyanov 36999f89551SEric W. Biederman put_task_struct(task); 37099f89551SEric W. Biederman } 3711da177e4SLinus Torvalds 3726b3286edSKirill Korotaev if (ns) { 3735addc5ddSAl Viro ret = -ENOMEM; 3745addc5ddSAl Viro p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL); 3755addc5ddSAl Viro if (p) { 3765addc5ddSAl Viro file->private_data = &p->m; 3775addc5ddSAl Viro ret = seq_open(file, &mounts_op); 3785addc5ddSAl Viro if (!ret) { 3796b3286edSKirill Korotaev p->m.private = ns; 3806b3286edSKirill Korotaev p->event = ns->event; 3815addc5ddSAl Viro return 0; 3821da177e4SLinus Torvalds } 3835addc5ddSAl Viro kfree(p); 3845addc5ddSAl Viro } 3856b3286edSKirill Korotaev put_mnt_ns(ns); 3861da177e4SLinus Torvalds } 3871da177e4SLinus Torvalds return ret; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds static int mounts_release(struct inode *inode, struct file *file) 3911da177e4SLinus Torvalds { 3921da177e4SLinus Torvalds struct seq_file *m = file->private_data; 3936b3286edSKirill Korotaev struct mnt_namespace *ns = m->private; 3946b3286edSKirill Korotaev put_mnt_ns(ns); 3951da177e4SLinus Torvalds return seq_release(inode, file); 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3985addc5ddSAl Viro static unsigned mounts_poll(struct file *file, poll_table *wait) 3995addc5ddSAl Viro { 4005addc5ddSAl Viro struct proc_mounts *p = file->private_data; 4016b3286edSKirill Korotaev struct mnt_namespace *ns = p->m.private; 4025addc5ddSAl Viro unsigned res = 0; 4035addc5ddSAl Viro 4045addc5ddSAl Viro poll_wait(file, &ns->poll, wait); 4055addc5ddSAl Viro 4065addc5ddSAl Viro spin_lock(&vfsmount_lock); 4075addc5ddSAl Viro if (p->event != ns->event) { 4085addc5ddSAl Viro p->event = ns->event; 4095addc5ddSAl Viro res = POLLERR; 4105addc5ddSAl Viro } 4115addc5ddSAl Viro spin_unlock(&vfsmount_lock); 4125addc5ddSAl Viro 4135addc5ddSAl Viro return res; 4145addc5ddSAl Viro } 4155addc5ddSAl Viro 41600977a59SArjan van de Ven static const struct file_operations proc_mounts_operations = { 4171da177e4SLinus Torvalds .open = mounts_open, 4181da177e4SLinus Torvalds .read = seq_read, 4191da177e4SLinus Torvalds .llseek = seq_lseek, 4201da177e4SLinus Torvalds .release = mounts_release, 4215addc5ddSAl Viro .poll = mounts_poll, 4221da177e4SLinus Torvalds }; 4231da177e4SLinus Torvalds 424b4629fe2SChuck Lever extern struct seq_operations mountstats_op; 425b4629fe2SChuck Lever static int mountstats_open(struct inode *inode, struct file *file) 426b4629fe2SChuck Lever { 427b4629fe2SChuck Lever int ret = seq_open(file, &mountstats_op); 428b4629fe2SChuck Lever 429b4629fe2SChuck Lever if (!ret) { 430b4629fe2SChuck Lever struct seq_file *m = file->private_data; 431cf7b708cSPavel Emelyanov struct nsproxy *nsp; 4326b3286edSKirill Korotaev struct mnt_namespace *mnt_ns = NULL; 43399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 43499f89551SEric W. Biederman 43599f89551SEric W. Biederman if (task) { 436cf7b708cSPavel Emelyanov rcu_read_lock(); 437cf7b708cSPavel Emelyanov nsp = task_nsproxy(task); 438cf7b708cSPavel Emelyanov if (nsp) { 439cf7b708cSPavel Emelyanov mnt_ns = nsp->mnt_ns; 4406b3286edSKirill Korotaev if (mnt_ns) 4416b3286edSKirill Korotaev get_mnt_ns(mnt_ns); 442cf7b708cSPavel Emelyanov } 443cf7b708cSPavel Emelyanov rcu_read_unlock(); 444cf7b708cSPavel Emelyanov 44599f89551SEric W. Biederman put_task_struct(task); 44699f89551SEric W. Biederman } 447b4629fe2SChuck Lever 4486b3286edSKirill Korotaev if (mnt_ns) 4496b3286edSKirill Korotaev m->private = mnt_ns; 450b4629fe2SChuck Lever else { 451b4629fe2SChuck Lever seq_release(inode, file); 452b4629fe2SChuck Lever ret = -EINVAL; 453b4629fe2SChuck Lever } 454b4629fe2SChuck Lever } 455b4629fe2SChuck Lever return ret; 456b4629fe2SChuck Lever } 457b4629fe2SChuck Lever 45800977a59SArjan van de Ven static const struct file_operations proc_mountstats_operations = { 459b4629fe2SChuck Lever .open = mountstats_open, 460b4629fe2SChuck Lever .read = seq_read, 461b4629fe2SChuck Lever .llseek = seq_lseek, 462b4629fe2SChuck Lever .release = mounts_release, 463b4629fe2SChuck Lever }; 464b4629fe2SChuck Lever 4651da177e4SLinus Torvalds #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */ 4661da177e4SLinus Torvalds 4671da177e4SLinus Torvalds static ssize_t proc_info_read(struct file * file, char __user * buf, 4681da177e4SLinus Torvalds size_t count, loff_t *ppos) 4691da177e4SLinus Torvalds { 4702fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 4711da177e4SLinus Torvalds unsigned long page; 4721da177e4SLinus Torvalds ssize_t length; 47399f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 47499f89551SEric W. Biederman 47599f89551SEric W. Biederman length = -ESRCH; 47699f89551SEric W. Biederman if (!task) 47799f89551SEric W. Biederman goto out_no_task; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds if (count > PROC_BLOCK_SIZE) 4801da177e4SLinus Torvalds count = PROC_BLOCK_SIZE; 48199f89551SEric W. Biederman 48299f89551SEric W. Biederman length = -ENOMEM; 483e12ba74dSMel Gorman if (!(page = __get_free_page(GFP_TEMPORARY))) 48499f89551SEric W. Biederman goto out; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds length = PROC_I(inode)->op.proc_read(task, (char*)page); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds if (length >= 0) 4891da177e4SLinus Torvalds length = simple_read_from_buffer(buf, count, ppos, (char *)page, length); 4901da177e4SLinus Torvalds free_page(page); 49199f89551SEric W. Biederman out: 49299f89551SEric W. Biederman put_task_struct(task); 49399f89551SEric W. Biederman out_no_task: 4941da177e4SLinus Torvalds return length; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 49700977a59SArjan van de Ven static const struct file_operations proc_info_file_operations = { 4981da177e4SLinus Torvalds .read = proc_info_read, 4991da177e4SLinus Torvalds }; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds static int mem_open(struct inode* inode, struct file* file) 5021da177e4SLinus Torvalds { 5031da177e4SLinus Torvalds file->private_data = (void*)((long)current->self_exec_id); 5041da177e4SLinus Torvalds return 0; 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds static ssize_t mem_read(struct file * file, char __user * buf, 5081da177e4SLinus Torvalds size_t count, loff_t *ppos) 5091da177e4SLinus Torvalds { 5102fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5111da177e4SLinus Torvalds char *page; 5121da177e4SLinus Torvalds unsigned long src = *ppos; 5131da177e4SLinus Torvalds int ret = -ESRCH; 5141da177e4SLinus Torvalds struct mm_struct *mm; 5151da177e4SLinus Torvalds 51699f89551SEric W. Biederman if (!task) 51799f89551SEric W. Biederman goto out_no_task; 51899f89551SEric W. Biederman 519ab8d11beSMiklos Szeredi if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 5201da177e4SLinus Torvalds goto out; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds ret = -ENOMEM; 523e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 5241da177e4SLinus Torvalds if (!page) 5251da177e4SLinus Torvalds goto out; 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds ret = 0; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds mm = get_task_mm(task); 5301da177e4SLinus Torvalds if (!mm) 5311da177e4SLinus Torvalds goto out_free; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds ret = -EIO; 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds if (file->private_data != (void*)((long)current->self_exec_id)) 5361da177e4SLinus Torvalds goto out_put; 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds ret = 0; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds while (count > 0) { 5411da177e4SLinus Torvalds int this_len, retval; 5421da177e4SLinus Torvalds 5431da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 5441da177e4SLinus Torvalds retval = access_process_vm(task, src, page, this_len, 0); 545ab8d11beSMiklos Szeredi if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) { 5461da177e4SLinus Torvalds if (!ret) 5471da177e4SLinus Torvalds ret = -EIO; 5481da177e4SLinus Torvalds break; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds if (copy_to_user(buf, page, retval)) { 5521da177e4SLinus Torvalds ret = -EFAULT; 5531da177e4SLinus Torvalds break; 5541da177e4SLinus Torvalds } 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds ret += retval; 5571da177e4SLinus Torvalds src += retval; 5581da177e4SLinus Torvalds buf += retval; 5591da177e4SLinus Torvalds count -= retval; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds *ppos = src; 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds out_put: 5641da177e4SLinus Torvalds mmput(mm); 5651da177e4SLinus Torvalds out_free: 5661da177e4SLinus Torvalds free_page((unsigned long) page); 5671da177e4SLinus Torvalds out: 56899f89551SEric W. Biederman put_task_struct(task); 56999f89551SEric W. Biederman out_no_task: 5701da177e4SLinus Torvalds return ret; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds #define mem_write NULL 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds #ifndef mem_write 5761da177e4SLinus Torvalds /* This is a security hazard */ 57763967fa9SGlauber de Oliveira Costa static ssize_t mem_write(struct file * file, const char __user *buf, 5781da177e4SLinus Torvalds size_t count, loff_t *ppos) 5791da177e4SLinus Torvalds { 580f7ca54f4SFrederik Deweerdt int copied; 5811da177e4SLinus Torvalds char *page; 5822fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 5831da177e4SLinus Torvalds unsigned long dst = *ppos; 5841da177e4SLinus Torvalds 58599f89551SEric W. Biederman copied = -ESRCH; 58699f89551SEric W. Biederman if (!task) 58799f89551SEric W. Biederman goto out_no_task; 5881da177e4SLinus Torvalds 58999f89551SEric W. Biederman if (!MAY_PTRACE(task) || !ptrace_may_attach(task)) 59099f89551SEric W. Biederman goto out; 59199f89551SEric W. Biederman 59299f89551SEric W. Biederman copied = -ENOMEM; 593e12ba74dSMel Gorman page = (char *)__get_free_page(GFP_TEMPORARY); 5941da177e4SLinus Torvalds if (!page) 59599f89551SEric W. Biederman goto out; 5961da177e4SLinus Torvalds 597f7ca54f4SFrederik Deweerdt copied = 0; 5981da177e4SLinus Torvalds while (count > 0) { 5991da177e4SLinus Torvalds int this_len, retval; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 6021da177e4SLinus Torvalds if (copy_from_user(page, buf, this_len)) { 6031da177e4SLinus Torvalds copied = -EFAULT; 6041da177e4SLinus Torvalds break; 6051da177e4SLinus Torvalds } 6061da177e4SLinus Torvalds retval = access_process_vm(task, dst, page, this_len, 1); 6071da177e4SLinus Torvalds if (!retval) { 6081da177e4SLinus Torvalds if (!copied) 6091da177e4SLinus Torvalds copied = -EIO; 6101da177e4SLinus Torvalds break; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds copied += retval; 6131da177e4SLinus Torvalds buf += retval; 6141da177e4SLinus Torvalds dst += retval; 6151da177e4SLinus Torvalds count -= retval; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds *ppos = dst; 6181da177e4SLinus Torvalds free_page((unsigned long) page); 61999f89551SEric W. Biederman out: 62099f89551SEric W. Biederman put_task_struct(task); 62199f89551SEric W. Biederman out_no_task: 6221da177e4SLinus Torvalds return copied; 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds #endif 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds static loff_t mem_lseek(struct file * file, loff_t offset, int orig) 6271da177e4SLinus Torvalds { 6281da177e4SLinus Torvalds switch (orig) { 6291da177e4SLinus Torvalds case 0: 6301da177e4SLinus Torvalds file->f_pos = offset; 6311da177e4SLinus Torvalds break; 6321da177e4SLinus Torvalds case 1: 6331da177e4SLinus Torvalds file->f_pos += offset; 6341da177e4SLinus Torvalds break; 6351da177e4SLinus Torvalds default: 6361da177e4SLinus Torvalds return -EINVAL; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds force_successful_syscall_return(); 6391da177e4SLinus Torvalds return file->f_pos; 6401da177e4SLinus Torvalds } 6411da177e4SLinus Torvalds 64200977a59SArjan van de Ven static const struct file_operations proc_mem_operations = { 6431da177e4SLinus Torvalds .llseek = mem_lseek, 6441da177e4SLinus Torvalds .read = mem_read, 6451da177e4SLinus Torvalds .write = mem_write, 6461da177e4SLinus Torvalds .open = mem_open, 6471da177e4SLinus Torvalds }; 6481da177e4SLinus Torvalds 649315e28c8SJames Pearson static ssize_t environ_read(struct file *file, char __user *buf, 650315e28c8SJames Pearson size_t count, loff_t *ppos) 651315e28c8SJames Pearson { 652315e28c8SJames Pearson struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 653315e28c8SJames Pearson char *page; 654315e28c8SJames Pearson unsigned long src = *ppos; 655315e28c8SJames Pearson int ret = -ESRCH; 656315e28c8SJames Pearson struct mm_struct *mm; 657315e28c8SJames Pearson 658315e28c8SJames Pearson if (!task) 659315e28c8SJames Pearson goto out_no_task; 660315e28c8SJames Pearson 661315e28c8SJames Pearson if (!ptrace_may_attach(task)) 662315e28c8SJames Pearson goto out; 663315e28c8SJames Pearson 664315e28c8SJames Pearson ret = -ENOMEM; 665315e28c8SJames Pearson page = (char *)__get_free_page(GFP_TEMPORARY); 666315e28c8SJames Pearson if (!page) 667315e28c8SJames Pearson goto out; 668315e28c8SJames Pearson 669315e28c8SJames Pearson ret = 0; 670315e28c8SJames Pearson 671315e28c8SJames Pearson mm = get_task_mm(task); 672315e28c8SJames Pearson if (!mm) 673315e28c8SJames Pearson goto out_free; 674315e28c8SJames Pearson 675315e28c8SJames Pearson while (count > 0) { 676315e28c8SJames Pearson int this_len, retval, max_len; 677315e28c8SJames Pearson 678315e28c8SJames Pearson this_len = mm->env_end - (mm->env_start + src); 679315e28c8SJames Pearson 680315e28c8SJames Pearson if (this_len <= 0) 681315e28c8SJames Pearson break; 682315e28c8SJames Pearson 683315e28c8SJames Pearson max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count; 684315e28c8SJames Pearson this_len = (this_len > max_len) ? max_len : this_len; 685315e28c8SJames Pearson 686315e28c8SJames Pearson retval = access_process_vm(task, (mm->env_start + src), 687315e28c8SJames Pearson page, this_len, 0); 688315e28c8SJames Pearson 689315e28c8SJames Pearson if (retval <= 0) { 690315e28c8SJames Pearson ret = retval; 691315e28c8SJames Pearson break; 692315e28c8SJames Pearson } 693315e28c8SJames Pearson 694315e28c8SJames Pearson if (copy_to_user(buf, page, retval)) { 695315e28c8SJames Pearson ret = -EFAULT; 696315e28c8SJames Pearson break; 697315e28c8SJames Pearson } 698315e28c8SJames Pearson 699315e28c8SJames Pearson ret += retval; 700315e28c8SJames Pearson src += retval; 701315e28c8SJames Pearson buf += retval; 702315e28c8SJames Pearson count -= retval; 703315e28c8SJames Pearson } 704315e28c8SJames Pearson *ppos = src; 705315e28c8SJames Pearson 706315e28c8SJames Pearson mmput(mm); 707315e28c8SJames Pearson out_free: 708315e28c8SJames Pearson free_page((unsigned long) page); 709315e28c8SJames Pearson out: 710315e28c8SJames Pearson put_task_struct(task); 711315e28c8SJames Pearson out_no_task: 712315e28c8SJames Pearson return ret; 713315e28c8SJames Pearson } 714315e28c8SJames Pearson 715315e28c8SJames Pearson static const struct file_operations proc_environ_operations = { 716315e28c8SJames Pearson .read = environ_read, 717315e28c8SJames Pearson }; 718315e28c8SJames Pearson 7191da177e4SLinus Torvalds static ssize_t oom_adjust_read(struct file *file, char __user *buf, 7201da177e4SLinus Torvalds size_t count, loff_t *ppos) 7211da177e4SLinus Torvalds { 7222fddfeefSJosef "Jeff" Sipek struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode); 7238578cea7SEric W. Biederman char buffer[PROC_NUMBUF]; 7241da177e4SLinus Torvalds size_t len; 72599f89551SEric W. Biederman int oom_adjust; 7261da177e4SLinus Torvalds 72799f89551SEric W. Biederman if (!task) 72899f89551SEric W. Biederman return -ESRCH; 72999f89551SEric W. Biederman oom_adjust = task->oomkilladj; 73099f89551SEric W. Biederman put_task_struct(task); 73199f89551SEric W. Biederman 7328578cea7SEric W. Biederman len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust); 7330c28f287SAkinobu Mita 7340c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 7351da177e4SLinus Torvalds } 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds static ssize_t oom_adjust_write(struct file *file, const char __user *buf, 7381da177e4SLinus Torvalds size_t count, loff_t *ppos) 7391da177e4SLinus Torvalds { 74099f89551SEric W. Biederman struct task_struct *task; 7418578cea7SEric W. Biederman char buffer[PROC_NUMBUF], *end; 7421da177e4SLinus Torvalds int oom_adjust; 7431da177e4SLinus Torvalds 7448578cea7SEric W. Biederman memset(buffer, 0, sizeof(buffer)); 7458578cea7SEric W. Biederman if (count > sizeof(buffer) - 1) 7468578cea7SEric W. Biederman count = sizeof(buffer) - 1; 7471da177e4SLinus Torvalds if (copy_from_user(buffer, buf, count)) 7481da177e4SLinus Torvalds return -EFAULT; 7491da177e4SLinus Torvalds oom_adjust = simple_strtol(buffer, &end, 0); 7508ac773b4SAlexey Dobriyan if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) && 7518ac773b4SAlexey Dobriyan oom_adjust != OOM_DISABLE) 7521da177e4SLinus Torvalds return -EINVAL; 7531da177e4SLinus Torvalds if (*end == '\n') 7541da177e4SLinus Torvalds end++; 7552fddfeefSJosef "Jeff" Sipek task = get_proc_task(file->f_path.dentry->d_inode); 75699f89551SEric W. Biederman if (!task) 75799f89551SEric W. Biederman return -ESRCH; 7588fb4fc68SGuillem Jover if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) { 7598fb4fc68SGuillem Jover put_task_struct(task); 7608fb4fc68SGuillem Jover return -EACCES; 7618fb4fc68SGuillem Jover } 7621da177e4SLinus Torvalds task->oomkilladj = oom_adjust; 76399f89551SEric W. Biederman put_task_struct(task); 7641da177e4SLinus Torvalds if (end - buffer == 0) 7651da177e4SLinus Torvalds return -EIO; 7661da177e4SLinus Torvalds return end - buffer; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 76900977a59SArjan van de Ven static const struct file_operations proc_oom_adjust_operations = { 7701da177e4SLinus Torvalds .read = oom_adjust_read, 7711da177e4SLinus Torvalds .write = oom_adjust_write, 7721da177e4SLinus Torvalds }; 7731da177e4SLinus Torvalds 7744b8df891SDavid Rientjes #ifdef CONFIG_MMU 775b813e931SDavid Rientjes static ssize_t clear_refs_write(struct file *file, const char __user *buf, 776b813e931SDavid Rientjes size_t count, loff_t *ppos) 777b813e931SDavid Rientjes { 778b813e931SDavid Rientjes struct task_struct *task; 779b813e931SDavid Rientjes char buffer[PROC_NUMBUF], *end; 780b813e931SDavid Rientjes struct mm_struct *mm; 781b813e931SDavid Rientjes 782b813e931SDavid Rientjes memset(buffer, 0, sizeof(buffer)); 783b813e931SDavid Rientjes if (count > sizeof(buffer) - 1) 784b813e931SDavid Rientjes count = sizeof(buffer) - 1; 785b813e931SDavid Rientjes if (copy_from_user(buffer, buf, count)) 786b813e931SDavid Rientjes return -EFAULT; 787b813e931SDavid Rientjes if (!simple_strtol(buffer, &end, 0)) 788b813e931SDavid Rientjes return -EINVAL; 789b813e931SDavid Rientjes if (*end == '\n') 790b813e931SDavid Rientjes end++; 791b813e931SDavid Rientjes task = get_proc_task(file->f_path.dentry->d_inode); 792b813e931SDavid Rientjes if (!task) 793b813e931SDavid Rientjes return -ESRCH; 794b813e931SDavid Rientjes mm = get_task_mm(task); 795b813e931SDavid Rientjes if (mm) { 796b813e931SDavid Rientjes clear_refs_smap(mm); 797b813e931SDavid Rientjes mmput(mm); 798b813e931SDavid Rientjes } 799b813e931SDavid Rientjes put_task_struct(task); 800b813e931SDavid Rientjes if (end - buffer == 0) 801b813e931SDavid Rientjes return -EIO; 802b813e931SDavid Rientjes return end - buffer; 803b813e931SDavid Rientjes } 804b813e931SDavid Rientjes 805b813e931SDavid Rientjes static struct file_operations proc_clear_refs_operations = { 806b813e931SDavid Rientjes .write = clear_refs_write, 807b813e931SDavid Rientjes }; 8084b8df891SDavid Rientjes #endif 809b813e931SDavid Rientjes 8101da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 8111da177e4SLinus Torvalds #define TMPBUFLEN 21 8121da177e4SLinus Torvalds static ssize_t proc_loginuid_read(struct file * file, char __user * buf, 8131da177e4SLinus Torvalds size_t count, loff_t *ppos) 8141da177e4SLinus Torvalds { 8152fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 81699f89551SEric W. Biederman struct task_struct *task = get_proc_task(inode); 8171da177e4SLinus Torvalds ssize_t length; 8181da177e4SLinus Torvalds char tmpbuf[TMPBUFLEN]; 8191da177e4SLinus Torvalds 82099f89551SEric W. Biederman if (!task) 82199f89551SEric W. Biederman return -ESRCH; 8221da177e4SLinus Torvalds length = scnprintf(tmpbuf, TMPBUFLEN, "%u", 8231da177e4SLinus Torvalds audit_get_loginuid(task->audit_context)); 82499f89551SEric W. Biederman put_task_struct(task); 8251da177e4SLinus Torvalds return simple_read_from_buffer(buf, count, ppos, tmpbuf, length); 8261da177e4SLinus Torvalds } 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds static ssize_t proc_loginuid_write(struct file * file, const char __user * buf, 8291da177e4SLinus Torvalds size_t count, loff_t *ppos) 8301da177e4SLinus Torvalds { 8312fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 8321da177e4SLinus Torvalds char *page, *tmp; 8331da177e4SLinus Torvalds ssize_t length; 8341da177e4SLinus Torvalds uid_t loginuid; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds if (!capable(CAP_AUDIT_CONTROL)) 8371da177e4SLinus Torvalds return -EPERM; 8381da177e4SLinus Torvalds 83913b41b09SEric W. Biederman if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) 8401da177e4SLinus Torvalds return -EPERM; 8411da177e4SLinus Torvalds 842e0182909SAl Viro if (count >= PAGE_SIZE) 843e0182909SAl Viro count = PAGE_SIZE - 1; 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds if (*ppos != 0) { 8461da177e4SLinus Torvalds /* No partial writes. */ 8471da177e4SLinus Torvalds return -EINVAL; 8481da177e4SLinus Torvalds } 849e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 8501da177e4SLinus Torvalds if (!page) 8511da177e4SLinus Torvalds return -ENOMEM; 8521da177e4SLinus Torvalds length = -EFAULT; 8531da177e4SLinus Torvalds if (copy_from_user(page, buf, count)) 8541da177e4SLinus Torvalds goto out_free_page; 8551da177e4SLinus Torvalds 856e0182909SAl Viro page[count] = '\0'; 8571da177e4SLinus Torvalds loginuid = simple_strtoul(page, &tmp, 10); 8581da177e4SLinus Torvalds if (tmp == page) { 8591da177e4SLinus Torvalds length = -EINVAL; 8601da177e4SLinus Torvalds goto out_free_page; 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds } 86399f89551SEric W. Biederman length = audit_set_loginuid(current, loginuid); 8641da177e4SLinus Torvalds if (likely(length == 0)) 8651da177e4SLinus Torvalds length = count; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds out_free_page: 8681da177e4SLinus Torvalds free_page((unsigned long) page); 8691da177e4SLinus Torvalds return length; 8701da177e4SLinus Torvalds } 8711da177e4SLinus Torvalds 87200977a59SArjan van de Ven static const struct file_operations proc_loginuid_operations = { 8731da177e4SLinus Torvalds .read = proc_loginuid_read, 8741da177e4SLinus Torvalds .write = proc_loginuid_write, 8751da177e4SLinus Torvalds }; 8761da177e4SLinus Torvalds #endif 8771da177e4SLinus Torvalds 878f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 879f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_read(struct file * file, char __user * buf, 880f4f154fdSAkinobu Mita size_t count, loff_t *ppos) 881f4f154fdSAkinobu Mita { 882f4f154fdSAkinobu Mita struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 883f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF]; 884f4f154fdSAkinobu Mita size_t len; 885f4f154fdSAkinobu Mita int make_it_fail; 886f4f154fdSAkinobu Mita 887f4f154fdSAkinobu Mita if (!task) 888f4f154fdSAkinobu Mita return -ESRCH; 889f4f154fdSAkinobu Mita make_it_fail = task->make_it_fail; 890f4f154fdSAkinobu Mita put_task_struct(task); 891f4f154fdSAkinobu Mita 892f4f154fdSAkinobu Mita len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail); 8930c28f287SAkinobu Mita 8940c28f287SAkinobu Mita return simple_read_from_buffer(buf, count, ppos, buffer, len); 895f4f154fdSAkinobu Mita } 896f4f154fdSAkinobu Mita 897f4f154fdSAkinobu Mita static ssize_t proc_fault_inject_write(struct file * file, 898f4f154fdSAkinobu Mita const char __user * buf, size_t count, loff_t *ppos) 899f4f154fdSAkinobu Mita { 900f4f154fdSAkinobu Mita struct task_struct *task; 901f4f154fdSAkinobu Mita char buffer[PROC_NUMBUF], *end; 902f4f154fdSAkinobu Mita int make_it_fail; 903f4f154fdSAkinobu Mita 904f4f154fdSAkinobu Mita if (!capable(CAP_SYS_RESOURCE)) 905f4f154fdSAkinobu Mita return -EPERM; 906f4f154fdSAkinobu Mita memset(buffer, 0, sizeof(buffer)); 907f4f154fdSAkinobu Mita if (count > sizeof(buffer) - 1) 908f4f154fdSAkinobu Mita count = sizeof(buffer) - 1; 909f4f154fdSAkinobu Mita if (copy_from_user(buffer, buf, count)) 910f4f154fdSAkinobu Mita return -EFAULT; 911f4f154fdSAkinobu Mita make_it_fail = simple_strtol(buffer, &end, 0); 912f4f154fdSAkinobu Mita if (*end == '\n') 913f4f154fdSAkinobu Mita end++; 914f4f154fdSAkinobu Mita task = get_proc_task(file->f_dentry->d_inode); 915f4f154fdSAkinobu Mita if (!task) 916f4f154fdSAkinobu Mita return -ESRCH; 917f4f154fdSAkinobu Mita task->make_it_fail = make_it_fail; 918f4f154fdSAkinobu Mita put_task_struct(task); 919f4f154fdSAkinobu Mita if (end - buffer == 0) 920f4f154fdSAkinobu Mita return -EIO; 921f4f154fdSAkinobu Mita return end - buffer; 922f4f154fdSAkinobu Mita } 923f4f154fdSAkinobu Mita 92400977a59SArjan van de Ven static const struct file_operations proc_fault_inject_operations = { 925f4f154fdSAkinobu Mita .read = proc_fault_inject_read, 926f4f154fdSAkinobu Mita .write = proc_fault_inject_write, 927f4f154fdSAkinobu Mita }; 928f4f154fdSAkinobu Mita #endif 929f4f154fdSAkinobu Mita 93043ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 93143ae34cbSIngo Molnar /* 93243ae34cbSIngo Molnar * Print out various scheduling related per-task fields: 93343ae34cbSIngo Molnar */ 93443ae34cbSIngo Molnar static int sched_show(struct seq_file *m, void *v) 93543ae34cbSIngo Molnar { 93643ae34cbSIngo Molnar struct inode *inode = m->private; 93743ae34cbSIngo Molnar struct task_struct *p; 93843ae34cbSIngo Molnar 93943ae34cbSIngo Molnar WARN_ON(!inode); 94043ae34cbSIngo Molnar 94143ae34cbSIngo Molnar p = get_proc_task(inode); 94243ae34cbSIngo Molnar if (!p) 94343ae34cbSIngo Molnar return -ESRCH; 94443ae34cbSIngo Molnar proc_sched_show_task(p, m); 94543ae34cbSIngo Molnar 94643ae34cbSIngo Molnar put_task_struct(p); 94743ae34cbSIngo Molnar 94843ae34cbSIngo Molnar return 0; 94943ae34cbSIngo Molnar } 95043ae34cbSIngo Molnar 95143ae34cbSIngo Molnar static ssize_t 95243ae34cbSIngo Molnar sched_write(struct file *file, const char __user *buf, 95343ae34cbSIngo Molnar size_t count, loff_t *offset) 95443ae34cbSIngo Molnar { 95543ae34cbSIngo Molnar struct inode *inode = file->f_path.dentry->d_inode; 95643ae34cbSIngo Molnar struct task_struct *p; 95743ae34cbSIngo Molnar 95843ae34cbSIngo Molnar WARN_ON(!inode); 95943ae34cbSIngo Molnar 96043ae34cbSIngo Molnar p = get_proc_task(inode); 96143ae34cbSIngo Molnar if (!p) 96243ae34cbSIngo Molnar return -ESRCH; 96343ae34cbSIngo Molnar proc_sched_set_task(p); 96443ae34cbSIngo Molnar 96543ae34cbSIngo Molnar put_task_struct(p); 96643ae34cbSIngo Molnar 96743ae34cbSIngo Molnar return count; 96843ae34cbSIngo Molnar } 96943ae34cbSIngo Molnar 97043ae34cbSIngo Molnar static int sched_open(struct inode *inode, struct file *filp) 97143ae34cbSIngo Molnar { 97243ae34cbSIngo Molnar int ret; 97343ae34cbSIngo Molnar 97443ae34cbSIngo Molnar ret = single_open(filp, sched_show, NULL); 97543ae34cbSIngo Molnar if (!ret) { 97643ae34cbSIngo Molnar struct seq_file *m = filp->private_data; 97743ae34cbSIngo Molnar 97843ae34cbSIngo Molnar m->private = inode; 97943ae34cbSIngo Molnar } 98043ae34cbSIngo Molnar return ret; 98143ae34cbSIngo Molnar } 98243ae34cbSIngo Molnar 98343ae34cbSIngo Molnar static const struct file_operations proc_pid_sched_operations = { 98443ae34cbSIngo Molnar .open = sched_open, 98543ae34cbSIngo Molnar .read = seq_read, 98643ae34cbSIngo Molnar .write = sched_write, 98743ae34cbSIngo Molnar .llseek = seq_lseek, 9885ea473a1SAlexey Dobriyan .release = single_release, 98943ae34cbSIngo Molnar }; 99043ae34cbSIngo Molnar 99143ae34cbSIngo Molnar #endif 99243ae34cbSIngo Molnar 993008b150aSAl Viro static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd) 9941da177e4SLinus Torvalds { 9951da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 9961da177e4SLinus Torvalds int error = -EACCES; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds /* We don't need a base pointer in the /proc filesystem */ 9991da177e4SLinus Torvalds path_release(nd); 10001da177e4SLinus Torvalds 1001778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1002778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10031da177e4SLinus Torvalds goto out; 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt); 10061da177e4SLinus Torvalds nd->last_type = LAST_BIND; 10071da177e4SLinus Torvalds out: 1008008b150aSAl Viro return ERR_PTR(error); 10091da177e4SLinus Torvalds } 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt, 10121da177e4SLinus Torvalds char __user *buffer, int buflen) 10131da177e4SLinus Torvalds { 10141da177e4SLinus Torvalds struct inode * inode; 1015e12ba74dSMel Gorman char *tmp = (char*)__get_free_page(GFP_TEMPORARY); 1016e12ba74dSMel Gorman char *path; 10171da177e4SLinus Torvalds int len; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds if (!tmp) 10201da177e4SLinus Torvalds return -ENOMEM; 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds inode = dentry->d_inode; 10231da177e4SLinus Torvalds path = d_path(dentry, mnt, tmp, PAGE_SIZE); 10241da177e4SLinus Torvalds len = PTR_ERR(path); 10251da177e4SLinus Torvalds if (IS_ERR(path)) 10261da177e4SLinus Torvalds goto out; 10271da177e4SLinus Torvalds len = tmp + PAGE_SIZE - 1 - path; 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds if (len > buflen) 10301da177e4SLinus Torvalds len = buflen; 10311da177e4SLinus Torvalds if (copy_to_user(buffer, path, len)) 10321da177e4SLinus Torvalds len = -EFAULT; 10331da177e4SLinus Torvalds out: 10341da177e4SLinus Torvalds free_page((unsigned long)tmp); 10351da177e4SLinus Torvalds return len; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen) 10391da177e4SLinus Torvalds { 10401da177e4SLinus Torvalds int error = -EACCES; 10411da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 10421da177e4SLinus Torvalds struct dentry *de; 10431da177e4SLinus Torvalds struct vfsmount *mnt = NULL; 10441da177e4SLinus Torvalds 1045778c1144SEric W. Biederman /* Are we allowed to snoop on the tasks file descriptors? */ 1046778c1144SEric W. Biederman if (!proc_fd_access_allowed(inode)) 10471da177e4SLinus Torvalds goto out; 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt); 10501da177e4SLinus Torvalds if (error) 10511da177e4SLinus Torvalds goto out; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds error = do_proc_readlink(de, mnt, buffer, buflen); 10541da177e4SLinus Torvalds dput(de); 10551da177e4SLinus Torvalds mntput(mnt); 10561da177e4SLinus Torvalds out: 10571da177e4SLinus Torvalds return error; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 1060c5ef1c42SArjan van de Ven static const struct inode_operations proc_pid_link_inode_operations = { 10611da177e4SLinus Torvalds .readlink = proc_pid_readlink, 10626d76fa58SLinus Torvalds .follow_link = proc_pid_follow_link, 10636d76fa58SLinus Torvalds .setattr = proc_setattr, 10641da177e4SLinus Torvalds }; 10651da177e4SLinus Torvalds 106628a6d671SEric W. Biederman 106728a6d671SEric W. Biederman /* building an inode */ 106828a6d671SEric W. Biederman 106928a6d671SEric W. Biederman static int task_dumpable(struct task_struct *task) 107028a6d671SEric W. Biederman { 107128a6d671SEric W. Biederman int dumpable = 0; 107228a6d671SEric W. Biederman struct mm_struct *mm; 107328a6d671SEric W. Biederman 107428a6d671SEric W. Biederman task_lock(task); 107528a6d671SEric W. Biederman mm = task->mm; 107628a6d671SEric W. Biederman if (mm) 10776c5d5238SKawai, Hidehiro dumpable = get_dumpable(mm); 107828a6d671SEric W. Biederman task_unlock(task); 107928a6d671SEric W. Biederman if(dumpable == 1) 108028a6d671SEric W. Biederman return 1; 108128a6d671SEric W. Biederman return 0; 108228a6d671SEric W. Biederman } 108328a6d671SEric W. Biederman 108428a6d671SEric W. Biederman 108561a28784SEric W. Biederman static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task) 108628a6d671SEric W. Biederman { 108728a6d671SEric W. Biederman struct inode * inode; 108828a6d671SEric W. Biederman struct proc_inode *ei; 108928a6d671SEric W. Biederman 109028a6d671SEric W. Biederman /* We need a new inode */ 109128a6d671SEric W. Biederman 109228a6d671SEric W. Biederman inode = new_inode(sb); 109328a6d671SEric W. Biederman if (!inode) 109428a6d671SEric W. Biederman goto out; 109528a6d671SEric W. Biederman 109628a6d671SEric W. Biederman /* Common stuff */ 109728a6d671SEric W. Biederman ei = PROC_I(inode); 109828a6d671SEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 109928a6d671SEric W. Biederman inode->i_op = &proc_def_inode_operations; 110028a6d671SEric W. Biederman 110128a6d671SEric W. Biederman /* 110228a6d671SEric W. Biederman * grab the reference to task. 110328a6d671SEric W. Biederman */ 11041a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 110528a6d671SEric W. Biederman if (!ei->pid) 110628a6d671SEric W. Biederman goto out_unlock; 110728a6d671SEric W. Biederman 110828a6d671SEric W. Biederman inode->i_uid = 0; 110928a6d671SEric W. Biederman inode->i_gid = 0; 111028a6d671SEric W. Biederman if (task_dumpable(task)) { 111128a6d671SEric W. Biederman inode->i_uid = task->euid; 111228a6d671SEric W. Biederman inode->i_gid = task->egid; 111328a6d671SEric W. Biederman } 111428a6d671SEric W. Biederman security_task_to_inode(task, inode); 111528a6d671SEric W. Biederman 111628a6d671SEric W. Biederman out: 111728a6d671SEric W. Biederman return inode; 111828a6d671SEric W. Biederman 111928a6d671SEric W. Biederman out_unlock: 112028a6d671SEric W. Biederman iput(inode); 112128a6d671SEric W. Biederman return NULL; 112228a6d671SEric W. Biederman } 112328a6d671SEric W. Biederman 112428a6d671SEric W. Biederman static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 112528a6d671SEric W. Biederman { 112628a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 112728a6d671SEric W. Biederman struct task_struct *task; 112828a6d671SEric W. Biederman generic_fillattr(inode, stat); 112928a6d671SEric W. Biederman 113028a6d671SEric W. Biederman rcu_read_lock(); 113128a6d671SEric W. Biederman stat->uid = 0; 113228a6d671SEric W. Biederman stat->gid = 0; 113328a6d671SEric W. Biederman task = pid_task(proc_pid(inode), PIDTYPE_PID); 113428a6d671SEric W. Biederman if (task) { 113528a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 113628a6d671SEric W. Biederman task_dumpable(task)) { 113728a6d671SEric W. Biederman stat->uid = task->euid; 113828a6d671SEric W. Biederman stat->gid = task->egid; 113928a6d671SEric W. Biederman } 114028a6d671SEric W. Biederman } 114128a6d671SEric W. Biederman rcu_read_unlock(); 114228a6d671SEric W. Biederman return 0; 114328a6d671SEric W. Biederman } 114428a6d671SEric W. Biederman 114528a6d671SEric W. Biederman /* dentry stuff */ 114628a6d671SEric W. Biederman 114728a6d671SEric W. Biederman /* 114828a6d671SEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 114928a6d671SEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 115028a6d671SEric W. Biederman * due to the way we treat inodes. 115128a6d671SEric W. Biederman * 115228a6d671SEric W. Biederman * Rewrite the inode's ownerships here because the owning task may have 115328a6d671SEric W. Biederman * performed a setuid(), etc. 115428a6d671SEric W. Biederman * 115528a6d671SEric W. Biederman * Before the /proc/pid/status file was created the only way to read 115628a6d671SEric W. Biederman * the effective uid of a /process was to stat /proc/pid. Reading 115728a6d671SEric W. Biederman * /proc/pid/status is slow enough that procps and other packages 115828a6d671SEric W. Biederman * kept stating /proc/pid. To keep the rules in /proc simple I have 115928a6d671SEric W. Biederman * made this apply to all per process world readable and executable 116028a6d671SEric W. Biederman * directories. 116128a6d671SEric W. Biederman */ 116228a6d671SEric W. Biederman static int pid_revalidate(struct dentry *dentry, struct nameidata *nd) 116328a6d671SEric W. Biederman { 116428a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 116528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 116628a6d671SEric W. Biederman if (task) { 116728a6d671SEric W. Biederman if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) || 116828a6d671SEric W. Biederman task_dumpable(task)) { 116928a6d671SEric W. Biederman inode->i_uid = task->euid; 117028a6d671SEric W. Biederman inode->i_gid = task->egid; 117128a6d671SEric W. Biederman } else { 117228a6d671SEric W. Biederman inode->i_uid = 0; 117328a6d671SEric W. Biederman inode->i_gid = 0; 117428a6d671SEric W. Biederman } 117528a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 117628a6d671SEric W. Biederman security_task_to_inode(task, inode); 117728a6d671SEric W. Biederman put_task_struct(task); 117828a6d671SEric W. Biederman return 1; 117928a6d671SEric W. Biederman } 118028a6d671SEric W. Biederman d_drop(dentry); 118128a6d671SEric W. Biederman return 0; 118228a6d671SEric W. Biederman } 118328a6d671SEric W. Biederman 118428a6d671SEric W. Biederman static int pid_delete_dentry(struct dentry * dentry) 118528a6d671SEric W. Biederman { 118628a6d671SEric W. Biederman /* Is the task we represent dead? 118728a6d671SEric W. Biederman * If so, then don't put the dentry on the lru list, 118828a6d671SEric W. Biederman * kill it immediately. 118928a6d671SEric W. Biederman */ 119028a6d671SEric W. Biederman return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first; 119128a6d671SEric W. Biederman } 119228a6d671SEric W. Biederman 119328a6d671SEric W. Biederman static struct dentry_operations pid_dentry_operations = 119428a6d671SEric W. Biederman { 119528a6d671SEric W. Biederman .d_revalidate = pid_revalidate, 119628a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 119728a6d671SEric W. Biederman }; 119828a6d671SEric W. Biederman 119928a6d671SEric W. Biederman /* Lookups */ 120028a6d671SEric W. Biederman 1201c5141e6dSEric Dumazet typedef struct dentry *instantiate_t(struct inode *, struct dentry *, 1202c5141e6dSEric Dumazet struct task_struct *, const void *); 120361a28784SEric W. Biederman 12041c0d04c9SEric W. Biederman /* 12051c0d04c9SEric W. Biederman * Fill a directory entry. 12061c0d04c9SEric W. Biederman * 12071c0d04c9SEric W. Biederman * If possible create the dcache entry and derive our inode number and 12081c0d04c9SEric W. Biederman * file type from dcache entry. 12091c0d04c9SEric W. Biederman * 12101c0d04c9SEric W. Biederman * Since all of the proc inode numbers are dynamically generated, the inode 12111c0d04c9SEric W. Biederman * numbers do not exist until the inode is cache. This means creating the 12121c0d04c9SEric W. Biederman * the dcache entry in readdir is necessary to keep the inode numbers 12131c0d04c9SEric W. Biederman * reported by readdir in sync with the inode numbers reported 12141c0d04c9SEric W. Biederman * by stat. 12151c0d04c9SEric W. Biederman */ 121661a28784SEric W. Biederman static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 121761a28784SEric W. Biederman char *name, int len, 1218c5141e6dSEric Dumazet instantiate_t instantiate, struct task_struct *task, const void *ptr) 121961a28784SEric W. Biederman { 12202fddfeefSJosef "Jeff" Sipek struct dentry *child, *dir = filp->f_path.dentry; 122161a28784SEric W. Biederman struct inode *inode; 122261a28784SEric W. Biederman struct qstr qname; 122361a28784SEric W. Biederman ino_t ino = 0; 122461a28784SEric W. Biederman unsigned type = DT_UNKNOWN; 122561a28784SEric W. Biederman 122661a28784SEric W. Biederman qname.name = name; 122761a28784SEric W. Biederman qname.len = len; 122861a28784SEric W. Biederman qname.hash = full_name_hash(name, len); 122961a28784SEric W. Biederman 123061a28784SEric W. Biederman child = d_lookup(dir, &qname); 123161a28784SEric W. Biederman if (!child) { 123261a28784SEric W. Biederman struct dentry *new; 123361a28784SEric W. Biederman new = d_alloc(dir, &qname); 123461a28784SEric W. Biederman if (new) { 123561a28784SEric W. Biederman child = instantiate(dir->d_inode, new, task, ptr); 123661a28784SEric W. Biederman if (child) 123761a28784SEric W. Biederman dput(new); 123861a28784SEric W. Biederman else 123961a28784SEric W. Biederman child = new; 124061a28784SEric W. Biederman } 124161a28784SEric W. Biederman } 124261a28784SEric W. Biederman if (!child || IS_ERR(child) || !child->d_inode) 124361a28784SEric W. Biederman goto end_instantiate; 124461a28784SEric W. Biederman inode = child->d_inode; 124561a28784SEric W. Biederman if (inode) { 124661a28784SEric W. Biederman ino = inode->i_ino; 124761a28784SEric W. Biederman type = inode->i_mode >> 12; 124861a28784SEric W. Biederman } 124961a28784SEric W. Biederman dput(child); 125061a28784SEric W. Biederman end_instantiate: 125161a28784SEric W. Biederman if (!ino) 125261a28784SEric W. Biederman ino = find_inode_number(dir, &qname); 125361a28784SEric W. Biederman if (!ino) 125461a28784SEric W. Biederman ino = 1; 125561a28784SEric W. Biederman return filldir(dirent, name, len, filp->f_pos, ino, type); 125661a28784SEric W. Biederman } 125761a28784SEric W. Biederman 125828a6d671SEric W. Biederman static unsigned name_to_int(struct dentry *dentry) 125928a6d671SEric W. Biederman { 126028a6d671SEric W. Biederman const char *name = dentry->d_name.name; 126128a6d671SEric W. Biederman int len = dentry->d_name.len; 126228a6d671SEric W. Biederman unsigned n = 0; 126328a6d671SEric W. Biederman 126428a6d671SEric W. Biederman if (len > 1 && *name == '0') 126528a6d671SEric W. Biederman goto out; 126628a6d671SEric W. Biederman while (len-- > 0) { 126728a6d671SEric W. Biederman unsigned c = *name++ - '0'; 126828a6d671SEric W. Biederman if (c > 9) 126928a6d671SEric W. Biederman goto out; 127028a6d671SEric W. Biederman if (n >= (~0U-9)/10) 127128a6d671SEric W. Biederman goto out; 127228a6d671SEric W. Biederman n *= 10; 127328a6d671SEric W. Biederman n += c; 127428a6d671SEric W. Biederman } 127528a6d671SEric W. Biederman return n; 127628a6d671SEric W. Biederman out: 127728a6d671SEric W. Biederman return ~0U; 127828a6d671SEric W. Biederman } 127928a6d671SEric W. Biederman 128027932742SMiklos Szeredi #define PROC_FDINFO_MAX 64 128127932742SMiklos Szeredi 128227932742SMiklos Szeredi static int proc_fd_info(struct inode *inode, struct dentry **dentry, 128327932742SMiklos Szeredi struct vfsmount **mnt, char *info) 128428a6d671SEric W. Biederman { 128528a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 128628a6d671SEric W. Biederman struct files_struct *files = NULL; 128728a6d671SEric W. Biederman struct file *file; 128828a6d671SEric W. Biederman int fd = proc_fd(inode); 128928a6d671SEric W. Biederman 129028a6d671SEric W. Biederman if (task) { 129128a6d671SEric W. Biederman files = get_files_struct(task); 129228a6d671SEric W. Biederman put_task_struct(task); 129328a6d671SEric W. Biederman } 129428a6d671SEric W. Biederman if (files) { 129528a6d671SEric W. Biederman /* 129628a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 129728a6d671SEric W. Biederman * hold ->file_lock. 129828a6d671SEric W. Biederman */ 129928a6d671SEric W. Biederman spin_lock(&files->file_lock); 130028a6d671SEric W. Biederman file = fcheck_files(files, fd); 130128a6d671SEric W. Biederman if (file) { 130227932742SMiklos Szeredi if (mnt) 13032fddfeefSJosef "Jeff" Sipek *mnt = mntget(file->f_path.mnt); 130427932742SMiklos Szeredi if (dentry) 13052fddfeefSJosef "Jeff" Sipek *dentry = dget(file->f_path.dentry); 130627932742SMiklos Szeredi if (info) 130727932742SMiklos Szeredi snprintf(info, PROC_FDINFO_MAX, 130827932742SMiklos Szeredi "pos:\t%lli\n" 130927932742SMiklos Szeredi "flags:\t0%o\n", 131027932742SMiklos Szeredi (long long) file->f_pos, 131127932742SMiklos Szeredi file->f_flags); 131228a6d671SEric W. Biederman spin_unlock(&files->file_lock); 131328a6d671SEric W. Biederman put_files_struct(files); 131428a6d671SEric W. Biederman return 0; 131528a6d671SEric W. Biederman } 131628a6d671SEric W. Biederman spin_unlock(&files->file_lock); 131728a6d671SEric W. Biederman put_files_struct(files); 131828a6d671SEric W. Biederman } 131928a6d671SEric W. Biederman return -ENOENT; 132028a6d671SEric W. Biederman } 132128a6d671SEric W. Biederman 132227932742SMiklos Szeredi static int proc_fd_link(struct inode *inode, struct dentry **dentry, 132327932742SMiklos Szeredi struct vfsmount **mnt) 132427932742SMiklos Szeredi { 132527932742SMiklos Szeredi return proc_fd_info(inode, dentry, mnt, NULL); 132627932742SMiklos Szeredi } 132727932742SMiklos Szeredi 132828a6d671SEric W. Biederman static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd) 132928a6d671SEric W. Biederman { 133028a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 133128a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 133228a6d671SEric W. Biederman int fd = proc_fd(inode); 133328a6d671SEric W. Biederman struct files_struct *files; 133428a6d671SEric W. Biederman 133528a6d671SEric W. Biederman if (task) { 133628a6d671SEric W. Biederman files = get_files_struct(task); 133728a6d671SEric W. Biederman if (files) { 133828a6d671SEric W. Biederman rcu_read_lock(); 133928a6d671SEric W. Biederman if (fcheck_files(files, fd)) { 134028a6d671SEric W. Biederman rcu_read_unlock(); 134128a6d671SEric W. Biederman put_files_struct(files); 134228a6d671SEric W. Biederman if (task_dumpable(task)) { 134328a6d671SEric W. Biederman inode->i_uid = task->euid; 134428a6d671SEric W. Biederman inode->i_gid = task->egid; 134528a6d671SEric W. Biederman } else { 134628a6d671SEric W. Biederman inode->i_uid = 0; 134728a6d671SEric W. Biederman inode->i_gid = 0; 134828a6d671SEric W. Biederman } 134928a6d671SEric W. Biederman inode->i_mode &= ~(S_ISUID | S_ISGID); 135028a6d671SEric W. Biederman security_task_to_inode(task, inode); 135128a6d671SEric W. Biederman put_task_struct(task); 135228a6d671SEric W. Biederman return 1; 135328a6d671SEric W. Biederman } 135428a6d671SEric W. Biederman rcu_read_unlock(); 135528a6d671SEric W. Biederman put_files_struct(files); 135628a6d671SEric W. Biederman } 135728a6d671SEric W. Biederman put_task_struct(task); 135828a6d671SEric W. Biederman } 135928a6d671SEric W. Biederman d_drop(dentry); 136028a6d671SEric W. Biederman return 0; 136128a6d671SEric W. Biederman } 136228a6d671SEric W. Biederman 136328a6d671SEric W. Biederman static struct dentry_operations tid_fd_dentry_operations = 136428a6d671SEric W. Biederman { 136528a6d671SEric W. Biederman .d_revalidate = tid_fd_revalidate, 136628a6d671SEric W. Biederman .d_delete = pid_delete_dentry, 136728a6d671SEric W. Biederman }; 136828a6d671SEric W. Biederman 1369444ceed8SEric W. Biederman static struct dentry *proc_fd_instantiate(struct inode *dir, 1370c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 137128a6d671SEric W. Biederman { 1372c5141e6dSEric Dumazet unsigned fd = *(const unsigned *)ptr; 137328a6d671SEric W. Biederman struct file *file; 137428a6d671SEric W. Biederman struct files_struct *files; 137528a6d671SEric W. Biederman struct inode *inode; 137628a6d671SEric W. Biederman struct proc_inode *ei; 1377444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 137828a6d671SEric W. Biederman 137961a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 138028a6d671SEric W. Biederman if (!inode) 138128a6d671SEric W. Biederman goto out; 138228a6d671SEric W. Biederman ei = PROC_I(inode); 138328a6d671SEric W. Biederman ei->fd = fd; 138428a6d671SEric W. Biederman files = get_files_struct(task); 138528a6d671SEric W. Biederman if (!files) 1386444ceed8SEric W. Biederman goto out_iput; 138728a6d671SEric W. Biederman inode->i_mode = S_IFLNK; 138828a6d671SEric W. Biederman 138928a6d671SEric W. Biederman /* 139028a6d671SEric W. Biederman * We are not taking a ref to the file structure, so we must 139128a6d671SEric W. Biederman * hold ->file_lock. 139228a6d671SEric W. Biederman */ 139328a6d671SEric W. Biederman spin_lock(&files->file_lock); 139428a6d671SEric W. Biederman file = fcheck_files(files, fd); 139528a6d671SEric W. Biederman if (!file) 1396444ceed8SEric W. Biederman goto out_unlock; 139728a6d671SEric W. Biederman if (file->f_mode & 1) 139828a6d671SEric W. Biederman inode->i_mode |= S_IRUSR | S_IXUSR; 139928a6d671SEric W. Biederman if (file->f_mode & 2) 140028a6d671SEric W. Biederman inode->i_mode |= S_IWUSR | S_IXUSR; 140128a6d671SEric W. Biederman spin_unlock(&files->file_lock); 140228a6d671SEric W. Biederman put_files_struct(files); 1403444ceed8SEric W. Biederman 140428a6d671SEric W. Biederman inode->i_op = &proc_pid_link_inode_operations; 140528a6d671SEric W. Biederman inode->i_size = 64; 140628a6d671SEric W. Biederman ei->op.proc_get_link = proc_fd_link; 140728a6d671SEric W. Biederman dentry->d_op = &tid_fd_dentry_operations; 140828a6d671SEric W. Biederman d_add(dentry, inode); 140928a6d671SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 141028a6d671SEric W. Biederman if (tid_fd_revalidate(dentry, NULL)) 1411444ceed8SEric W. Biederman error = NULL; 1412444ceed8SEric W. Biederman 1413444ceed8SEric W. Biederman out: 1414444ceed8SEric W. Biederman return error; 1415444ceed8SEric W. Biederman out_unlock: 1416444ceed8SEric W. Biederman spin_unlock(&files->file_lock); 1417444ceed8SEric W. Biederman put_files_struct(files); 1418444ceed8SEric W. Biederman out_iput: 1419444ceed8SEric W. Biederman iput(inode); 1420444ceed8SEric W. Biederman goto out; 1421444ceed8SEric W. Biederman } 1422444ceed8SEric W. Biederman 142327932742SMiklos Szeredi static struct dentry *proc_lookupfd_common(struct inode *dir, 142427932742SMiklos Szeredi struct dentry *dentry, 142527932742SMiklos Szeredi instantiate_t instantiate) 1426444ceed8SEric W. Biederman { 1427444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1428444ceed8SEric W. Biederman unsigned fd = name_to_int(dentry); 1429444ceed8SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 1430444ceed8SEric W. Biederman 1431444ceed8SEric W. Biederman if (!task) 1432444ceed8SEric W. Biederman goto out_no_task; 1433444ceed8SEric W. Biederman if (fd == ~0U) 1434444ceed8SEric W. Biederman goto out; 1435444ceed8SEric W. Biederman 143627932742SMiklos Szeredi result = instantiate(dir, dentry, task, &fd); 143728a6d671SEric W. Biederman out: 143828a6d671SEric W. Biederman put_task_struct(task); 143928a6d671SEric W. Biederman out_no_task: 144028a6d671SEric W. Biederman return result; 144128a6d671SEric W. Biederman } 144228a6d671SEric W. Biederman 144327932742SMiklos Szeredi static int proc_readfd_common(struct file * filp, void * dirent, 144427932742SMiklos Szeredi filldir_t filldir, instantiate_t instantiate) 14451da177e4SLinus Torvalds { 14462fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 14475634708bSEric W. Biederman struct inode *inode = dentry->d_inode; 144899f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 14491da177e4SLinus Torvalds unsigned int fd, tid, ino; 14501da177e4SLinus Torvalds int retval; 14511da177e4SLinus Torvalds struct files_struct * files; 1452badf1662SDipankar Sarma struct fdtable *fdt; 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds retval = -ENOENT; 145599f89551SEric W. Biederman if (!p) 145699f89551SEric W. Biederman goto out_no_task; 14571da177e4SLinus Torvalds retval = 0; 14581da177e4SLinus Torvalds tid = p->pid; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds fd = filp->f_pos; 14611da177e4SLinus Torvalds switch (fd) { 14621da177e4SLinus Torvalds case 0: 14631da177e4SLinus Torvalds if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0) 14641da177e4SLinus Torvalds goto out; 14651da177e4SLinus Torvalds filp->f_pos++; 14661da177e4SLinus Torvalds case 1: 14675634708bSEric W. Biederman ino = parent_ino(dentry); 14681da177e4SLinus Torvalds if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0) 14691da177e4SLinus Torvalds goto out; 14701da177e4SLinus Torvalds filp->f_pos++; 14711da177e4SLinus Torvalds default: 14721da177e4SLinus Torvalds files = get_files_struct(p); 14731da177e4SLinus Torvalds if (!files) 14741da177e4SLinus Torvalds goto out; 1475b835996fSDipankar Sarma rcu_read_lock(); 1476badf1662SDipankar Sarma fdt = files_fdtable(files); 14771da177e4SLinus Torvalds for (fd = filp->f_pos-2; 1478badf1662SDipankar Sarma fd < fdt->max_fds; 14791da177e4SLinus Torvalds fd++, filp->f_pos++) { 148027932742SMiklos Szeredi char name[PROC_NUMBUF]; 148127932742SMiklos Szeredi int len; 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds if (!fcheck_files(files, fd)) 14841da177e4SLinus Torvalds continue; 1485b835996fSDipankar Sarma rcu_read_unlock(); 14861da177e4SLinus Torvalds 148727932742SMiklos Szeredi len = snprintf(name, sizeof(name), "%d", fd); 148827932742SMiklos Szeredi if (proc_fill_cache(filp, dirent, filldir, 148927932742SMiklos Szeredi name, len, instantiate, 149027932742SMiklos Szeredi p, &fd) < 0) { 1491b835996fSDipankar Sarma rcu_read_lock(); 14921da177e4SLinus Torvalds break; 14931da177e4SLinus Torvalds } 1494b835996fSDipankar Sarma rcu_read_lock(); 14951da177e4SLinus Torvalds } 1496b835996fSDipankar Sarma rcu_read_unlock(); 14971da177e4SLinus Torvalds put_files_struct(files); 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds out: 150099f89551SEric W. Biederman put_task_struct(p); 150199f89551SEric W. Biederman out_no_task: 15021da177e4SLinus Torvalds return retval; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 150527932742SMiklos Szeredi static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry, 150627932742SMiklos Szeredi struct nameidata *nd) 150727932742SMiklos Szeredi { 150827932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fd_instantiate); 150927932742SMiklos Szeredi } 151027932742SMiklos Szeredi 151127932742SMiklos Szeredi static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir) 151227932742SMiklos Szeredi { 151327932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate); 151427932742SMiklos Szeredi } 151527932742SMiklos Szeredi 151627932742SMiklos Szeredi static ssize_t proc_fdinfo_read(struct file *file, char __user *buf, 151727932742SMiklos Szeredi size_t len, loff_t *ppos) 151827932742SMiklos Szeredi { 151927932742SMiklos Szeredi char tmp[PROC_FDINFO_MAX]; 152027932742SMiklos Szeredi int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp); 152127932742SMiklos Szeredi if (!err) 152227932742SMiklos Szeredi err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp)); 152327932742SMiklos Szeredi return err; 152427932742SMiklos Szeredi } 152527932742SMiklos Szeredi 152627932742SMiklos Szeredi static const struct file_operations proc_fdinfo_file_operations = { 152727932742SMiklos Szeredi .open = nonseekable_open, 152827932742SMiklos Szeredi .read = proc_fdinfo_read, 152927932742SMiklos Szeredi }; 153027932742SMiklos Szeredi 153100977a59SArjan van de Ven static const struct file_operations proc_fd_operations = { 15321da177e4SLinus Torvalds .read = generic_read_dir, 15331da177e4SLinus Torvalds .readdir = proc_readfd, 15341da177e4SLinus Torvalds }; 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds /* 15378948e11fSAlexey Dobriyan * /proc/pid/fd needs a special permission handler so that a process can still 15388948e11fSAlexey Dobriyan * access /proc/self/fd after it has executed a setuid(). 15398948e11fSAlexey Dobriyan */ 15408948e11fSAlexey Dobriyan static int proc_fd_permission(struct inode *inode, int mask, 15418948e11fSAlexey Dobriyan struct nameidata *nd) 15428948e11fSAlexey Dobriyan { 15438948e11fSAlexey Dobriyan int rv; 15448948e11fSAlexey Dobriyan 15458948e11fSAlexey Dobriyan rv = generic_permission(inode, mask, NULL); 15468948e11fSAlexey Dobriyan if (rv == 0) 15478948e11fSAlexey Dobriyan return 0; 15488948e11fSAlexey Dobriyan if (task_pid(current) == proc_pid(inode)) 15498948e11fSAlexey Dobriyan rv = 0; 15508948e11fSAlexey Dobriyan return rv; 15518948e11fSAlexey Dobriyan } 15528948e11fSAlexey Dobriyan 15538948e11fSAlexey Dobriyan /* 15541da177e4SLinus Torvalds * proc directories can do almost nothing.. 15551da177e4SLinus Torvalds */ 1556c5ef1c42SArjan van de Ven static const struct inode_operations proc_fd_inode_operations = { 15571da177e4SLinus Torvalds .lookup = proc_lookupfd, 15588948e11fSAlexey Dobriyan .permission = proc_fd_permission, 15596d76fa58SLinus Torvalds .setattr = proc_setattr, 15601da177e4SLinus Torvalds }; 15611da177e4SLinus Torvalds 156227932742SMiklos Szeredi static struct dentry *proc_fdinfo_instantiate(struct inode *dir, 156327932742SMiklos Szeredi struct dentry *dentry, struct task_struct *task, const void *ptr) 156427932742SMiklos Szeredi { 156527932742SMiklos Szeredi unsigned fd = *(unsigned *)ptr; 156627932742SMiklos Szeredi struct inode *inode; 156727932742SMiklos Szeredi struct proc_inode *ei; 156827932742SMiklos Szeredi struct dentry *error = ERR_PTR(-ENOENT); 156927932742SMiklos Szeredi 157027932742SMiklos Szeredi inode = proc_pid_make_inode(dir->i_sb, task); 157127932742SMiklos Szeredi if (!inode) 157227932742SMiklos Szeredi goto out; 157327932742SMiklos Szeredi ei = PROC_I(inode); 157427932742SMiklos Szeredi ei->fd = fd; 157527932742SMiklos Szeredi inode->i_mode = S_IFREG | S_IRUSR; 157627932742SMiklos Szeredi inode->i_fop = &proc_fdinfo_file_operations; 157727932742SMiklos Szeredi dentry->d_op = &tid_fd_dentry_operations; 157827932742SMiklos Szeredi d_add(dentry, inode); 157927932742SMiklos Szeredi /* Close the race of the process dying before we return the dentry */ 158027932742SMiklos Szeredi if (tid_fd_revalidate(dentry, NULL)) 158127932742SMiklos Szeredi error = NULL; 158227932742SMiklos Szeredi 158327932742SMiklos Szeredi out: 158427932742SMiklos Szeredi return error; 158527932742SMiklos Szeredi } 158627932742SMiklos Szeredi 158727932742SMiklos Szeredi static struct dentry *proc_lookupfdinfo(struct inode *dir, 158827932742SMiklos Szeredi struct dentry *dentry, 158927932742SMiklos Szeredi struct nameidata *nd) 159027932742SMiklos Szeredi { 159127932742SMiklos Szeredi return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate); 159227932742SMiklos Szeredi } 159327932742SMiklos Szeredi 159427932742SMiklos Szeredi static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir) 159527932742SMiklos Szeredi { 159627932742SMiklos Szeredi return proc_readfd_common(filp, dirent, filldir, 159727932742SMiklos Szeredi proc_fdinfo_instantiate); 159827932742SMiklos Szeredi } 159927932742SMiklos Szeredi 160027932742SMiklos Szeredi static const struct file_operations proc_fdinfo_operations = { 160127932742SMiklos Szeredi .read = generic_read_dir, 160227932742SMiklos Szeredi .readdir = proc_readfdinfo, 160327932742SMiklos Szeredi }; 160427932742SMiklos Szeredi 160527932742SMiklos Szeredi /* 160627932742SMiklos Szeredi * proc directories can do almost nothing.. 160727932742SMiklos Szeredi */ 160827932742SMiklos Szeredi static const struct inode_operations proc_fdinfo_inode_operations = { 160927932742SMiklos Szeredi .lookup = proc_lookupfdinfo, 161027932742SMiklos Szeredi .setattr = proc_setattr, 161127932742SMiklos Szeredi }; 161227932742SMiklos Szeredi 161327932742SMiklos Szeredi 1614444ceed8SEric W. Biederman static struct dentry *proc_pident_instantiate(struct inode *dir, 1615c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1616444ceed8SEric W. Biederman { 1617c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1618444ceed8SEric W. Biederman struct inode *inode; 1619444ceed8SEric W. Biederman struct proc_inode *ei; 1620444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1621444ceed8SEric W. Biederman 162261a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 1623444ceed8SEric W. Biederman if (!inode) 1624444ceed8SEric W. Biederman goto out; 1625444ceed8SEric W. Biederman 1626444ceed8SEric W. Biederman ei = PROC_I(inode); 1627444ceed8SEric W. Biederman inode->i_mode = p->mode; 1628444ceed8SEric W. Biederman if (S_ISDIR(inode->i_mode)) 1629444ceed8SEric W. Biederman inode->i_nlink = 2; /* Use getattr to fix if necessary */ 1630444ceed8SEric W. Biederman if (p->iop) 1631444ceed8SEric W. Biederman inode->i_op = p->iop; 1632444ceed8SEric W. Biederman if (p->fop) 1633444ceed8SEric W. Biederman inode->i_fop = p->fop; 1634444ceed8SEric W. Biederman ei->op = p->op; 1635444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 1636444ceed8SEric W. Biederman d_add(dentry, inode); 1637444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 1638444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 1639444ceed8SEric W. Biederman error = NULL; 1640444ceed8SEric W. Biederman out: 1641444ceed8SEric W. Biederman return error; 1642444ceed8SEric W. Biederman } 1643444ceed8SEric W. Biederman 16441da177e4SLinus Torvalds static struct dentry *proc_pident_lookup(struct inode *dir, 16451da177e4SLinus Torvalds struct dentry *dentry, 1646c5141e6dSEric Dumazet const struct pid_entry *ents, 16477bcd6b0eSEric W. Biederman unsigned int nents) 16481da177e4SLinus Torvalds { 16491da177e4SLinus Torvalds struct inode *inode; 1650cd6a3ce9SEric W. Biederman struct dentry *error; 165199f89551SEric W. Biederman struct task_struct *task = get_proc_task(dir); 1652c5141e6dSEric Dumazet const struct pid_entry *p, *last; 16531da177e4SLinus Torvalds 1654cd6a3ce9SEric W. Biederman error = ERR_PTR(-ENOENT); 16551da177e4SLinus Torvalds inode = NULL; 16561da177e4SLinus Torvalds 165799f89551SEric W. Biederman if (!task) 165899f89551SEric W. Biederman goto out_no_task; 16591da177e4SLinus Torvalds 166020cdc894SEric W. Biederman /* 166120cdc894SEric W. Biederman * Yes, it does not scale. And it should not. Don't add 166220cdc894SEric W. Biederman * new entries into /proc/<tgid>/ without very good reasons. 166320cdc894SEric W. Biederman */ 16647bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 16657bcd6b0eSEric W. Biederman for (p = ents; p <= last; p++) { 16661da177e4SLinus Torvalds if (p->len != dentry->d_name.len) 16671da177e4SLinus Torvalds continue; 16681da177e4SLinus Torvalds if (!memcmp(dentry->d_name.name, p->name, p->len)) 16691da177e4SLinus Torvalds break; 16701da177e4SLinus Torvalds } 16717bcd6b0eSEric W. Biederman if (p > last) 16721da177e4SLinus Torvalds goto out; 16731da177e4SLinus Torvalds 1674444ceed8SEric W. Biederman error = proc_pident_instantiate(dir, dentry, task, p); 16751da177e4SLinus Torvalds out: 167699f89551SEric W. Biederman put_task_struct(task); 167799f89551SEric W. Biederman out_no_task: 1678cd6a3ce9SEric W. Biederman return error; 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 1681c5141e6dSEric Dumazet static int proc_pident_fill_cache(struct file *filp, void *dirent, 1682c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 168361a28784SEric W. Biederman { 168461a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 168561a28784SEric W. Biederman proc_pident_instantiate, task, p); 168661a28784SEric W. Biederman } 168761a28784SEric W. Biederman 168828a6d671SEric W. Biederman static int proc_pident_readdir(struct file *filp, 168928a6d671SEric W. Biederman void *dirent, filldir_t filldir, 1690c5141e6dSEric Dumazet const struct pid_entry *ents, unsigned int nents) 169128a6d671SEric W. Biederman { 169228a6d671SEric W. Biederman int i; 169328a6d671SEric W. Biederman int pid; 16942fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 169528a6d671SEric W. Biederman struct inode *inode = dentry->d_inode; 169628a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 1697c5141e6dSEric Dumazet const struct pid_entry *p, *last; 169828a6d671SEric W. Biederman ino_t ino; 169928a6d671SEric W. Biederman int ret; 170028a6d671SEric W. Biederman 170128a6d671SEric W. Biederman ret = -ENOENT; 170228a6d671SEric W. Biederman if (!task) 170361a28784SEric W. Biederman goto out_no_task; 170428a6d671SEric W. Biederman 170528a6d671SEric W. Biederman ret = 0; 170628a6d671SEric W. Biederman pid = task->pid; 170728a6d671SEric W. Biederman i = filp->f_pos; 170828a6d671SEric W. Biederman switch (i) { 170928a6d671SEric W. Biederman case 0: 171028a6d671SEric W. Biederman ino = inode->i_ino; 171128a6d671SEric W. Biederman if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0) 171228a6d671SEric W. Biederman goto out; 171328a6d671SEric W. Biederman i++; 171428a6d671SEric W. Biederman filp->f_pos++; 171528a6d671SEric W. Biederman /* fall through */ 171628a6d671SEric W. Biederman case 1: 171728a6d671SEric W. Biederman ino = parent_ino(dentry); 171828a6d671SEric W. Biederman if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0) 171928a6d671SEric W. Biederman goto out; 172028a6d671SEric W. Biederman i++; 172128a6d671SEric W. Biederman filp->f_pos++; 172228a6d671SEric W. Biederman /* fall through */ 172328a6d671SEric W. Biederman default: 172428a6d671SEric W. Biederman i -= 2; 172528a6d671SEric W. Biederman if (i >= nents) { 172628a6d671SEric W. Biederman ret = 1; 172728a6d671SEric W. Biederman goto out; 172828a6d671SEric W. Biederman } 172928a6d671SEric W. Biederman p = ents + i; 17307bcd6b0eSEric W. Biederman last = &ents[nents - 1]; 17317bcd6b0eSEric W. Biederman while (p <= last) { 173261a28784SEric W. Biederman if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0) 173328a6d671SEric W. Biederman goto out; 173428a6d671SEric W. Biederman filp->f_pos++; 173528a6d671SEric W. Biederman p++; 173628a6d671SEric W. Biederman } 17371da177e4SLinus Torvalds } 17381da177e4SLinus Torvalds 173928a6d671SEric W. Biederman ret = 1; 174028a6d671SEric W. Biederman out: 174161a28784SEric W. Biederman put_task_struct(task); 174261a28784SEric W. Biederman out_no_task: 174328a6d671SEric W. Biederman return ret; 17441da177e4SLinus Torvalds } 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds #ifdef CONFIG_SECURITY 174728a6d671SEric W. Biederman static ssize_t proc_pid_attr_read(struct file * file, char __user * buf, 174828a6d671SEric W. Biederman size_t count, loff_t *ppos) 174928a6d671SEric W. Biederman { 17502fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 175104ff9708SAl Viro char *p = NULL; 175228a6d671SEric W. Biederman ssize_t length; 175328a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 175428a6d671SEric W. Biederman 175528a6d671SEric W. Biederman if (!task) 175604ff9708SAl Viro return -ESRCH; 175728a6d671SEric W. Biederman 175828a6d671SEric W. Biederman length = security_getprocattr(task, 17592fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 176004ff9708SAl Viro &p); 176128a6d671SEric W. Biederman put_task_struct(task); 176204ff9708SAl Viro if (length > 0) 176304ff9708SAl Viro length = simple_read_from_buffer(buf, count, ppos, p, length); 176404ff9708SAl Viro kfree(p); 176528a6d671SEric W. Biederman return length; 176628a6d671SEric W. Biederman } 176728a6d671SEric W. Biederman 176828a6d671SEric W. Biederman static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf, 176928a6d671SEric W. Biederman size_t count, loff_t *ppos) 177028a6d671SEric W. Biederman { 17712fddfeefSJosef "Jeff" Sipek struct inode * inode = file->f_path.dentry->d_inode; 177228a6d671SEric W. Biederman char *page; 177328a6d671SEric W. Biederman ssize_t length; 177428a6d671SEric W. Biederman struct task_struct *task = get_proc_task(inode); 177528a6d671SEric W. Biederman 177628a6d671SEric W. Biederman length = -ESRCH; 177728a6d671SEric W. Biederman if (!task) 177828a6d671SEric W. Biederman goto out_no_task; 177928a6d671SEric W. Biederman if (count > PAGE_SIZE) 178028a6d671SEric W. Biederman count = PAGE_SIZE; 178128a6d671SEric W. Biederman 178228a6d671SEric W. Biederman /* No partial writes. */ 178328a6d671SEric W. Biederman length = -EINVAL; 178428a6d671SEric W. Biederman if (*ppos != 0) 178528a6d671SEric W. Biederman goto out; 178628a6d671SEric W. Biederman 178728a6d671SEric W. Biederman length = -ENOMEM; 1788e12ba74dSMel Gorman page = (char*)__get_free_page(GFP_TEMPORARY); 178928a6d671SEric W. Biederman if (!page) 179028a6d671SEric W. Biederman goto out; 179128a6d671SEric W. Biederman 179228a6d671SEric W. Biederman length = -EFAULT; 179328a6d671SEric W. Biederman if (copy_from_user(page, buf, count)) 179428a6d671SEric W. Biederman goto out_free; 179528a6d671SEric W. Biederman 179628a6d671SEric W. Biederman length = security_setprocattr(task, 17972fddfeefSJosef "Jeff" Sipek (char*)file->f_path.dentry->d_name.name, 179828a6d671SEric W. Biederman (void*)page, count); 179928a6d671SEric W. Biederman out_free: 180028a6d671SEric W. Biederman free_page((unsigned long) page); 180128a6d671SEric W. Biederman out: 180228a6d671SEric W. Biederman put_task_struct(task); 180328a6d671SEric W. Biederman out_no_task: 180428a6d671SEric W. Biederman return length; 180528a6d671SEric W. Biederman } 180628a6d671SEric W. Biederman 180700977a59SArjan van de Ven static const struct file_operations proc_pid_attr_operations = { 180828a6d671SEric W. Biederman .read = proc_pid_attr_read, 180928a6d671SEric W. Biederman .write = proc_pid_attr_write, 181028a6d671SEric W. Biederman }; 181128a6d671SEric W. Biederman 1812c5141e6dSEric Dumazet static const struct pid_entry attr_dir_stuff[] = { 181361a28784SEric W. Biederman REG("current", S_IRUGO|S_IWUGO, pid_attr), 181461a28784SEric W. Biederman REG("prev", S_IRUGO, pid_attr), 181561a28784SEric W. Biederman REG("exec", S_IRUGO|S_IWUGO, pid_attr), 181661a28784SEric W. Biederman REG("fscreate", S_IRUGO|S_IWUGO, pid_attr), 181761a28784SEric W. Biederman REG("keycreate", S_IRUGO|S_IWUGO, pid_attr), 181861a28784SEric W. Biederman REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr), 181928a6d671SEric W. Biederman }; 182028a6d671SEric W. Biederman 182172d9dcfcSEric W. Biederman static int proc_attr_dir_readdir(struct file * filp, 18221da177e4SLinus Torvalds void * dirent, filldir_t filldir) 18231da177e4SLinus Torvalds { 18241da177e4SLinus Torvalds return proc_pident_readdir(filp,dirent,filldir, 182572d9dcfcSEric W. Biederman attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff)); 18261da177e4SLinus Torvalds } 18271da177e4SLinus Torvalds 182800977a59SArjan van de Ven static const struct file_operations proc_attr_dir_operations = { 18291da177e4SLinus Torvalds .read = generic_read_dir, 183072d9dcfcSEric W. Biederman .readdir = proc_attr_dir_readdir, 18311da177e4SLinus Torvalds }; 18321da177e4SLinus Torvalds 183372d9dcfcSEric W. Biederman static struct dentry *proc_attr_dir_lookup(struct inode *dir, 18341da177e4SLinus Torvalds struct dentry *dentry, struct nameidata *nd) 18351da177e4SLinus Torvalds { 18367bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 18377bcd6b0eSEric W. Biederman attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff)); 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 1840c5ef1c42SArjan van de Ven static const struct inode_operations proc_attr_dir_inode_operations = { 184172d9dcfcSEric W. Biederman .lookup = proc_attr_dir_lookup, 184299f89551SEric W. Biederman .getattr = pid_getattr, 18436d76fa58SLinus Torvalds .setattr = proc_setattr, 18441da177e4SLinus Torvalds }; 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds #endif 18471da177e4SLinus Torvalds 18483cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 18493cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf, 18503cb4a0bbSKawai, Hidehiro size_t count, loff_t *ppos) 18513cb4a0bbSKawai, Hidehiro { 18523cb4a0bbSKawai, Hidehiro struct task_struct *task = get_proc_task(file->f_dentry->d_inode); 18533cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 18543cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF]; 18553cb4a0bbSKawai, Hidehiro size_t len; 18563cb4a0bbSKawai, Hidehiro int ret; 18573cb4a0bbSKawai, Hidehiro 18583cb4a0bbSKawai, Hidehiro if (!task) 18593cb4a0bbSKawai, Hidehiro return -ESRCH; 18603cb4a0bbSKawai, Hidehiro 18613cb4a0bbSKawai, Hidehiro ret = 0; 18623cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 18633cb4a0bbSKawai, Hidehiro if (mm) { 18643cb4a0bbSKawai, Hidehiro len = snprintf(buffer, sizeof(buffer), "%08lx\n", 18653cb4a0bbSKawai, Hidehiro ((mm->flags & MMF_DUMP_FILTER_MASK) >> 18663cb4a0bbSKawai, Hidehiro MMF_DUMP_FILTER_SHIFT)); 18673cb4a0bbSKawai, Hidehiro mmput(mm); 18683cb4a0bbSKawai, Hidehiro ret = simple_read_from_buffer(buf, count, ppos, buffer, len); 18693cb4a0bbSKawai, Hidehiro } 18703cb4a0bbSKawai, Hidehiro 18713cb4a0bbSKawai, Hidehiro put_task_struct(task); 18723cb4a0bbSKawai, Hidehiro 18733cb4a0bbSKawai, Hidehiro return ret; 18743cb4a0bbSKawai, Hidehiro } 18753cb4a0bbSKawai, Hidehiro 18763cb4a0bbSKawai, Hidehiro static ssize_t proc_coredump_filter_write(struct file *file, 18773cb4a0bbSKawai, Hidehiro const char __user *buf, 18783cb4a0bbSKawai, Hidehiro size_t count, 18793cb4a0bbSKawai, Hidehiro loff_t *ppos) 18803cb4a0bbSKawai, Hidehiro { 18813cb4a0bbSKawai, Hidehiro struct task_struct *task; 18823cb4a0bbSKawai, Hidehiro struct mm_struct *mm; 18833cb4a0bbSKawai, Hidehiro char buffer[PROC_NUMBUF], *end; 18843cb4a0bbSKawai, Hidehiro unsigned int val; 18853cb4a0bbSKawai, Hidehiro int ret; 18863cb4a0bbSKawai, Hidehiro int i; 18873cb4a0bbSKawai, Hidehiro unsigned long mask; 18883cb4a0bbSKawai, Hidehiro 18893cb4a0bbSKawai, Hidehiro ret = -EFAULT; 18903cb4a0bbSKawai, Hidehiro memset(buffer, 0, sizeof(buffer)); 18913cb4a0bbSKawai, Hidehiro if (count > sizeof(buffer) - 1) 18923cb4a0bbSKawai, Hidehiro count = sizeof(buffer) - 1; 18933cb4a0bbSKawai, Hidehiro if (copy_from_user(buffer, buf, count)) 18943cb4a0bbSKawai, Hidehiro goto out_no_task; 18953cb4a0bbSKawai, Hidehiro 18963cb4a0bbSKawai, Hidehiro ret = -EINVAL; 18973cb4a0bbSKawai, Hidehiro val = (unsigned int)simple_strtoul(buffer, &end, 0); 18983cb4a0bbSKawai, Hidehiro if (*end == '\n') 18993cb4a0bbSKawai, Hidehiro end++; 19003cb4a0bbSKawai, Hidehiro if (end - buffer == 0) 19013cb4a0bbSKawai, Hidehiro goto out_no_task; 19023cb4a0bbSKawai, Hidehiro 19033cb4a0bbSKawai, Hidehiro ret = -ESRCH; 19043cb4a0bbSKawai, Hidehiro task = get_proc_task(file->f_dentry->d_inode); 19053cb4a0bbSKawai, Hidehiro if (!task) 19063cb4a0bbSKawai, Hidehiro goto out_no_task; 19073cb4a0bbSKawai, Hidehiro 19083cb4a0bbSKawai, Hidehiro ret = end - buffer; 19093cb4a0bbSKawai, Hidehiro mm = get_task_mm(task); 19103cb4a0bbSKawai, Hidehiro if (!mm) 19113cb4a0bbSKawai, Hidehiro goto out_no_mm; 19123cb4a0bbSKawai, Hidehiro 19133cb4a0bbSKawai, Hidehiro for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) { 19143cb4a0bbSKawai, Hidehiro if (val & mask) 19153cb4a0bbSKawai, Hidehiro set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19163cb4a0bbSKawai, Hidehiro else 19173cb4a0bbSKawai, Hidehiro clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags); 19183cb4a0bbSKawai, Hidehiro } 19193cb4a0bbSKawai, Hidehiro 19203cb4a0bbSKawai, Hidehiro mmput(mm); 19213cb4a0bbSKawai, Hidehiro out_no_mm: 19223cb4a0bbSKawai, Hidehiro put_task_struct(task); 19233cb4a0bbSKawai, Hidehiro out_no_task: 19243cb4a0bbSKawai, Hidehiro return ret; 19253cb4a0bbSKawai, Hidehiro } 19263cb4a0bbSKawai, Hidehiro 19273cb4a0bbSKawai, Hidehiro static const struct file_operations proc_coredump_filter_operations = { 19283cb4a0bbSKawai, Hidehiro .read = proc_coredump_filter_read, 19293cb4a0bbSKawai, Hidehiro .write = proc_coredump_filter_write, 19303cb4a0bbSKawai, Hidehiro }; 19313cb4a0bbSKawai, Hidehiro #endif 19323cb4a0bbSKawai, Hidehiro 19331da177e4SLinus Torvalds /* 19341da177e4SLinus Torvalds * /proc/self: 19351da177e4SLinus Torvalds */ 19361da177e4SLinus Torvalds static int proc_self_readlink(struct dentry *dentry, char __user *buffer, 19371da177e4SLinus Torvalds int buflen) 19381da177e4SLinus Torvalds { 19398578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19401da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 19411da177e4SLinus Torvalds return vfs_readlink(dentry,buffer,buflen,tmp); 19421da177e4SLinus Torvalds } 19431da177e4SLinus Torvalds 1944008b150aSAl Viro static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd) 19451da177e4SLinus Torvalds { 19468578cea7SEric W. Biederman char tmp[PROC_NUMBUF]; 19471da177e4SLinus Torvalds sprintf(tmp, "%d", current->tgid); 1948008b150aSAl Viro return ERR_PTR(vfs_follow_link(nd,tmp)); 19491da177e4SLinus Torvalds } 19501da177e4SLinus Torvalds 1951c5ef1c42SArjan van de Ven static const struct inode_operations proc_self_inode_operations = { 19521da177e4SLinus Torvalds .readlink = proc_self_readlink, 19531da177e4SLinus Torvalds .follow_link = proc_self_follow_link, 19541da177e4SLinus Torvalds }; 19551da177e4SLinus Torvalds 195628a6d671SEric W. Biederman /* 1957801199ceSEric W. Biederman * proc base 1958801199ceSEric W. Biederman * 1959801199ceSEric W. Biederman * These are the directory entries in the root directory of /proc 1960801199ceSEric W. Biederman * that properly belong to the /proc filesystem, as they describe 1961801199ceSEric W. Biederman * describe something that is process related. 1962801199ceSEric W. Biederman */ 1963c5141e6dSEric Dumazet static const struct pid_entry proc_base_stuff[] = { 196461a28784SEric W. Biederman NOD("self", S_IFLNK|S_IRWXUGO, 1965801199ceSEric W. Biederman &proc_self_inode_operations, NULL, {}), 1966801199ceSEric W. Biederman }; 1967801199ceSEric W. Biederman 1968801199ceSEric W. Biederman /* 1969801199ceSEric W. Biederman * Exceptional case: normally we are not allowed to unhash a busy 1970801199ceSEric W. Biederman * directory. In this case, however, we can do it - no aliasing problems 1971801199ceSEric W. Biederman * due to the way we treat inodes. 1972801199ceSEric W. Biederman */ 1973801199ceSEric W. Biederman static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd) 1974801199ceSEric W. Biederman { 1975801199ceSEric W. Biederman struct inode *inode = dentry->d_inode; 1976801199ceSEric W. Biederman struct task_struct *task = get_proc_task(inode); 1977801199ceSEric W. Biederman if (task) { 1978801199ceSEric W. Biederman put_task_struct(task); 1979801199ceSEric W. Biederman return 1; 1980801199ceSEric W. Biederman } 1981801199ceSEric W. Biederman d_drop(dentry); 1982801199ceSEric W. Biederman return 0; 1983801199ceSEric W. Biederman } 1984801199ceSEric W. Biederman 1985801199ceSEric W. Biederman static struct dentry_operations proc_base_dentry_operations = 1986801199ceSEric W. Biederman { 1987801199ceSEric W. Biederman .d_revalidate = proc_base_revalidate, 1988801199ceSEric W. Biederman .d_delete = pid_delete_dentry, 1989801199ceSEric W. Biederman }; 1990801199ceSEric W. Biederman 1991444ceed8SEric W. Biederman static struct dentry *proc_base_instantiate(struct inode *dir, 1992c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 1993801199ceSEric W. Biederman { 1994c5141e6dSEric Dumazet const struct pid_entry *p = ptr; 1995801199ceSEric W. Biederman struct inode *inode; 1996801199ceSEric W. Biederman struct proc_inode *ei; 1997444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-EINVAL); 1998801199ceSEric W. Biederman 1999801199ceSEric W. Biederman /* Allocate the inode */ 2000801199ceSEric W. Biederman error = ERR_PTR(-ENOMEM); 2001801199ceSEric W. Biederman inode = new_inode(dir->i_sb); 2002801199ceSEric W. Biederman if (!inode) 2003801199ceSEric W. Biederman goto out; 2004801199ceSEric W. Biederman 2005801199ceSEric W. Biederman /* Initialize the inode */ 2006801199ceSEric W. Biederman ei = PROC_I(inode); 2007801199ceSEric W. Biederman inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; 2008801199ceSEric W. Biederman 2009801199ceSEric W. Biederman /* 2010801199ceSEric W. Biederman * grab the reference to the task. 2011801199ceSEric W. Biederman */ 20121a657f78SOleg Nesterov ei->pid = get_task_pid(task, PIDTYPE_PID); 2013801199ceSEric W. Biederman if (!ei->pid) 2014801199ceSEric W. Biederman goto out_iput; 2015801199ceSEric W. Biederman 2016801199ceSEric W. Biederman inode->i_uid = 0; 2017801199ceSEric W. Biederman inode->i_gid = 0; 2018801199ceSEric W. Biederman inode->i_mode = p->mode; 2019801199ceSEric W. Biederman if (S_ISDIR(inode->i_mode)) 2020801199ceSEric W. Biederman inode->i_nlink = 2; 2021801199ceSEric W. Biederman if (S_ISLNK(inode->i_mode)) 2022801199ceSEric W. Biederman inode->i_size = 64; 2023801199ceSEric W. Biederman if (p->iop) 2024801199ceSEric W. Biederman inode->i_op = p->iop; 2025801199ceSEric W. Biederman if (p->fop) 2026801199ceSEric W. Biederman inode->i_fop = p->fop; 2027801199ceSEric W. Biederman ei->op = p->op; 2028801199ceSEric W. Biederman dentry->d_op = &proc_base_dentry_operations; 2029801199ceSEric W. Biederman d_add(dentry, inode); 2030801199ceSEric W. Biederman error = NULL; 2031801199ceSEric W. Biederman out: 2032801199ceSEric W. Biederman return error; 2033801199ceSEric W. Biederman out_iput: 2034801199ceSEric W. Biederman iput(inode); 2035801199ceSEric W. Biederman goto out; 2036801199ceSEric W. Biederman } 2037801199ceSEric W. Biederman 2038444ceed8SEric W. Biederman static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry) 2039444ceed8SEric W. Biederman { 2040444ceed8SEric W. Biederman struct dentry *error; 2041444ceed8SEric W. Biederman struct task_struct *task = get_proc_task(dir); 2042c5141e6dSEric Dumazet const struct pid_entry *p, *last; 2043444ceed8SEric W. Biederman 2044444ceed8SEric W. Biederman error = ERR_PTR(-ENOENT); 2045444ceed8SEric W. Biederman 2046444ceed8SEric W. Biederman if (!task) 2047444ceed8SEric W. Biederman goto out_no_task; 2048444ceed8SEric W. Biederman 2049444ceed8SEric W. Biederman /* Lookup the directory entry */ 20507bcd6b0eSEric W. Biederman last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1]; 20517bcd6b0eSEric W. Biederman for (p = proc_base_stuff; p <= last; p++) { 2052444ceed8SEric W. Biederman if (p->len != dentry->d_name.len) 2053444ceed8SEric W. Biederman continue; 2054444ceed8SEric W. Biederman if (!memcmp(dentry->d_name.name, p->name, p->len)) 2055444ceed8SEric W. Biederman break; 2056444ceed8SEric W. Biederman } 20577bcd6b0eSEric W. Biederman if (p > last) 2058444ceed8SEric W. Biederman goto out; 2059444ceed8SEric W. Biederman 2060444ceed8SEric W. Biederman error = proc_base_instantiate(dir, dentry, task, p); 2061444ceed8SEric W. Biederman 2062444ceed8SEric W. Biederman out: 2063444ceed8SEric W. Biederman put_task_struct(task); 2064444ceed8SEric W. Biederman out_no_task: 2065444ceed8SEric W. Biederman return error; 2066444ceed8SEric W. Biederman } 2067444ceed8SEric W. Biederman 2068c5141e6dSEric Dumazet static int proc_base_fill_cache(struct file *filp, void *dirent, 2069c5141e6dSEric Dumazet filldir_t filldir, struct task_struct *task, const struct pid_entry *p) 207061a28784SEric W. Biederman { 207161a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, p->name, p->len, 207261a28784SEric W. Biederman proc_base_instantiate, task, p); 207361a28784SEric W. Biederman } 207461a28784SEric W. Biederman 2075aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2076aba76fdbSAndrew Morton static int proc_pid_io_accounting(struct task_struct *task, char *buffer) 2077aba76fdbSAndrew Morton { 2078aba76fdbSAndrew Morton return sprintf(buffer, 20794b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2080aba76fdbSAndrew Morton "rchar: %llu\n" 2081aba76fdbSAndrew Morton "wchar: %llu\n" 2082aba76fdbSAndrew Morton "syscr: %llu\n" 2083aba76fdbSAndrew Morton "syscw: %llu\n" 20844b98d11bSAlexey Dobriyan #endif 2085aba76fdbSAndrew Morton "read_bytes: %llu\n" 2086aba76fdbSAndrew Morton "write_bytes: %llu\n" 2087aba76fdbSAndrew Morton "cancelled_write_bytes: %llu\n", 20884b98d11bSAlexey Dobriyan #ifdef CONFIG_TASK_XACCT 2089aba76fdbSAndrew Morton (unsigned long long)task->rchar, 2090aba76fdbSAndrew Morton (unsigned long long)task->wchar, 2091aba76fdbSAndrew Morton (unsigned long long)task->syscr, 2092aba76fdbSAndrew Morton (unsigned long long)task->syscw, 20934b98d11bSAlexey Dobriyan #endif 2094aba76fdbSAndrew Morton (unsigned long long)task->ioac.read_bytes, 2095aba76fdbSAndrew Morton (unsigned long long)task->ioac.write_bytes, 2096aba76fdbSAndrew Morton (unsigned long long)task->ioac.cancelled_write_bytes); 2097aba76fdbSAndrew Morton } 2098aba76fdbSAndrew Morton #endif 2099aba76fdbSAndrew Morton 2100801199ceSEric W. Biederman /* 210128a6d671SEric W. Biederman * Thread groups 210228a6d671SEric W. Biederman */ 210300977a59SArjan van de Ven static const struct file_operations proc_task_operations; 2104c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations; 210520cdc894SEric W. Biederman 2106c5141e6dSEric Dumazet static const struct pid_entry tgid_base_stuff[] = { 210761a28784SEric W. Biederman DIR("task", S_IRUGO|S_IXUGO, task), 210861a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 210927932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2110315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 211161a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 211261a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 211343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 211443ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 211543ae34cbSIngo Molnar #endif 211661a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 211761a28784SEric W. Biederman INF("stat", S_IRUGO, tgid_stat), 211861a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 211961a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 212028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 212161a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 212228a6d671SEric W. Biederman #endif 212361a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 212461a28784SEric W. Biederman LNK("cwd", cwd), 212561a28784SEric W. Biederman LNK("root", root), 212661a28784SEric W. Biederman LNK("exe", exe), 212761a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 212861a28784SEric W. Biederman REG("mountstats", S_IRUSR, mountstats), 212928a6d671SEric W. Biederman #ifdef CONFIG_MMU 2130b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 213161a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 213228a6d671SEric W. Biederman #endif 213328a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 213472d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 213528a6d671SEric W. Biederman #endif 213628a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 213761a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 213828a6d671SEric W. Biederman #endif 213928a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 214061a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 214128a6d671SEric W. Biederman #endif 21428793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 214361a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 214428a6d671SEric W. Biederman #endif 2145a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2146a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2147a424316cSPaul Menage #endif 214861a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 214961a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 215028a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 215161a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 215228a6d671SEric W. Biederman #endif 2153f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2154f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2155f4f154fdSAkinobu Mita #endif 21563cb4a0bbSKawai, Hidehiro #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE) 21573cb4a0bbSKawai, Hidehiro REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter), 21583cb4a0bbSKawai, Hidehiro #endif 2159aba76fdbSAndrew Morton #ifdef CONFIG_TASK_IO_ACCOUNTING 2160aba76fdbSAndrew Morton INF("io", S_IRUGO, pid_io_accounting), 2161aba76fdbSAndrew Morton #endif 216228a6d671SEric W. Biederman }; 216328a6d671SEric W. Biederman 216428a6d671SEric W. Biederman static int proc_tgid_base_readdir(struct file * filp, 216528a6d671SEric W. Biederman void * dirent, filldir_t filldir) 216628a6d671SEric W. Biederman { 216728a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 216828a6d671SEric W. Biederman tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff)); 216928a6d671SEric W. Biederman } 217028a6d671SEric W. Biederman 217100977a59SArjan van de Ven static const struct file_operations proc_tgid_base_operations = { 217228a6d671SEric W. Biederman .read = generic_read_dir, 217328a6d671SEric W. Biederman .readdir = proc_tgid_base_readdir, 217428a6d671SEric W. Biederman }; 217528a6d671SEric W. Biederman 217628a6d671SEric W. Biederman static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 21777bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 21787bcd6b0eSEric W. Biederman tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff)); 217928a6d671SEric W. Biederman } 218028a6d671SEric W. Biederman 2181c5ef1c42SArjan van de Ven static const struct inode_operations proc_tgid_base_inode_operations = { 218228a6d671SEric W. Biederman .lookup = proc_tgid_base_lookup, 218328a6d671SEric W. Biederman .getattr = pid_getattr, 218428a6d671SEric W. Biederman .setattr = proc_setattr, 218528a6d671SEric W. Biederman }; 218628a6d671SEric W. Biederman 21871da177e4SLinus Torvalds /** 218848e6484dSEric W. Biederman * proc_flush_task - Remove dcache entries for @task from the /proc dcache. 21891da177e4SLinus Torvalds * 219048e6484dSEric W. Biederman * @task: task that should be flushed. 21911da177e4SLinus Torvalds * 219248e6484dSEric W. Biederman * Looks in the dcache for 219348e6484dSEric W. Biederman * /proc/@pid 219448e6484dSEric W. Biederman * /proc/@tgid/task/@pid 219548e6484dSEric W. Biederman * if either directory is present flushes it and all of it'ts children 219648e6484dSEric W. Biederman * from the dcache. 219748e6484dSEric W. Biederman * 219848e6484dSEric W. Biederman * It is safe and reasonable to cache /proc entries for a task until 219948e6484dSEric W. Biederman * that task exits. After that they just clog up the dcache with 220048e6484dSEric W. Biederman * useless entries, possibly causing useful dcache entries to be 220148e6484dSEric W. Biederman * flushed instead. This routine is proved to flush those useless 220248e6484dSEric W. Biederman * dcache entries at process exit time. 220348e6484dSEric W. Biederman * 220448e6484dSEric W. Biederman * NOTE: This routine is just an optimization so it does not guarantee 220548e6484dSEric W. Biederman * that no dcache entries will exist at process exit time it 220648e6484dSEric W. Biederman * just makes it very unlikely that any will persist. 22071da177e4SLinus Torvalds */ 220860347f67SPavel Emelyanov static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid) 22091da177e4SLinus Torvalds { 221048e6484dSEric W. Biederman struct dentry *dentry, *leader, *dir; 22118578cea7SEric W. Biederman char buf[PROC_NUMBUF]; 221248e6484dSEric W. Biederman struct qstr name; 22131da177e4SLinus Torvalds 221448e6484dSEric W. Biederman name.name = buf; 221560347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 221660347f67SPavel Emelyanov dentry = d_hash_and_lookup(mnt->mnt_root, &name); 221748e6484dSEric W. Biederman if (dentry) { 221848e6484dSEric W. Biederman shrink_dcache_parent(dentry); 221948e6484dSEric W. Biederman d_drop(dentry); 222048e6484dSEric W. Biederman dput(dentry); 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds 222360347f67SPavel Emelyanov if (tgid == 0) 222448e6484dSEric W. Biederman goto out; 22251da177e4SLinus Torvalds 222648e6484dSEric W. Biederman name.name = buf; 222760347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", tgid); 222860347f67SPavel Emelyanov leader = d_hash_and_lookup(mnt->mnt_root, &name); 222948e6484dSEric W. Biederman if (!leader) 223048e6484dSEric W. Biederman goto out; 223148e6484dSEric W. Biederman 223248e6484dSEric W. Biederman name.name = "task"; 223348e6484dSEric W. Biederman name.len = strlen(name.name); 223448e6484dSEric W. Biederman dir = d_hash_and_lookup(leader, &name); 223548e6484dSEric W. Biederman if (!dir) 223648e6484dSEric W. Biederman goto out_put_leader; 223748e6484dSEric W. Biederman 223848e6484dSEric W. Biederman name.name = buf; 223960347f67SPavel Emelyanov name.len = snprintf(buf, sizeof(buf), "%d", pid); 224048e6484dSEric W. Biederman dentry = d_hash_and_lookup(dir, &name); 224148e6484dSEric W. Biederman if (dentry) { 224248e6484dSEric W. Biederman shrink_dcache_parent(dentry); 224348e6484dSEric W. Biederman d_drop(dentry); 224448e6484dSEric W. Biederman dput(dentry); 22451da177e4SLinus Torvalds } 224648e6484dSEric W. Biederman 224748e6484dSEric W. Biederman dput(dir); 224848e6484dSEric W. Biederman out_put_leader: 224948e6484dSEric W. Biederman dput(leader); 225048e6484dSEric W. Biederman out: 225148e6484dSEric W. Biederman return; 22521da177e4SLinus Torvalds } 22531da177e4SLinus Torvalds 225460347f67SPavel Emelyanov /* 225560347f67SPavel Emelyanov * when flushing dentries from proc one need to flush them from global 225660347f67SPavel Emelyanov * proc (proc_mnt) and from all the namespaces' procs this task was seen 225760347f67SPavel Emelyanov * in. this call is supposed to make all this job. 225860347f67SPavel Emelyanov */ 225960347f67SPavel Emelyanov 226060347f67SPavel Emelyanov void proc_flush_task(struct task_struct *task) 226160347f67SPavel Emelyanov { 2262130f77ecSPavel Emelyanov int i, leader; 2263130f77ecSPavel Emelyanov struct pid *pid, *tgid; 2264130f77ecSPavel Emelyanov struct upid *upid; 2265130f77ecSPavel Emelyanov 2266130f77ecSPavel Emelyanov leader = thread_group_leader(task); 2267130f77ecSPavel Emelyanov proc_flush_task_mnt(proc_mnt, task->pid, leader ? task->tgid : 0); 2268130f77ecSPavel Emelyanov pid = task_pid(task); 2269130f77ecSPavel Emelyanov if (pid->level == 0) 2270130f77ecSPavel Emelyanov return; 2271130f77ecSPavel Emelyanov 2272130f77ecSPavel Emelyanov tgid = task_tgid(task); 2273130f77ecSPavel Emelyanov for (i = 1; i <= pid->level; i++) { 2274130f77ecSPavel Emelyanov upid = &pid->numbers[i]; 2275130f77ecSPavel Emelyanov proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr, 2276130f77ecSPavel Emelyanov leader ? 0 : tgid->numbers[i].nr); 2277130f77ecSPavel Emelyanov } 2278*6f4e6433SPavel Emelyanov 2279*6f4e6433SPavel Emelyanov upid = &pid->numbers[pid->level]; 2280*6f4e6433SPavel Emelyanov if (upid->nr == 1) 2281*6f4e6433SPavel Emelyanov pid_ns_release_proc(upid->ns); 228260347f67SPavel Emelyanov } 228360347f67SPavel Emelyanov 22849711ef99SAdrian Bunk static struct dentry *proc_pid_instantiate(struct inode *dir, 22859711ef99SAdrian Bunk struct dentry * dentry, 2286c5141e6dSEric Dumazet struct task_struct *task, const void *ptr) 2287444ceed8SEric W. Biederman { 2288444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2289444ceed8SEric W. Biederman struct inode *inode; 2290444ceed8SEric W. Biederman 229161a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2292444ceed8SEric W. Biederman if (!inode) 2293444ceed8SEric W. Biederman goto out; 2294444ceed8SEric W. Biederman 2295444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2296444ceed8SEric W. Biederman inode->i_op = &proc_tgid_base_inode_operations; 2297444ceed8SEric W. Biederman inode->i_fop = &proc_tgid_base_operations; 2298444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 229927932742SMiklos Szeredi inode->i_nlink = 5; 2300444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2301444ceed8SEric W. Biederman inode->i_nlink += 1; 2302444ceed8SEric W. Biederman #endif 2303444ceed8SEric W. Biederman 2304444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2305444ceed8SEric W. Biederman 2306444ceed8SEric W. Biederman d_add(dentry, inode); 2307444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2308444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2309444ceed8SEric W. Biederman error = NULL; 2310444ceed8SEric W. Biederman out: 2311444ceed8SEric W. Biederman return error; 2312444ceed8SEric W. Biederman } 2313444ceed8SEric W. Biederman 23141da177e4SLinus Torvalds struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 23151da177e4SLinus Torvalds { 2316cd6a3ce9SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 23171da177e4SLinus Torvalds struct task_struct *task; 23181da177e4SLinus Torvalds unsigned tgid; 23191da177e4SLinus Torvalds 2320801199ceSEric W. Biederman result = proc_base_lookup(dir, dentry); 2321801199ceSEric W. Biederman if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT) 2322801199ceSEric W. Biederman goto out; 2323801199ceSEric W. Biederman 23241da177e4SLinus Torvalds tgid = name_to_int(dentry); 23251da177e4SLinus Torvalds if (tgid == ~0U) 23261da177e4SLinus Torvalds goto out; 23271da177e4SLinus Torvalds 2328de758734SEric W. Biederman rcu_read_lock(); 23291da177e4SLinus Torvalds task = find_task_by_pid(tgid); 23301da177e4SLinus Torvalds if (task) 23311da177e4SLinus Torvalds get_task_struct(task); 2332de758734SEric W. Biederman rcu_read_unlock(); 23331da177e4SLinus Torvalds if (!task) 23341da177e4SLinus Torvalds goto out; 23351da177e4SLinus Torvalds 2336444ceed8SEric W. Biederman result = proc_pid_instantiate(dir, dentry, task, NULL); 233748e6484dSEric W. Biederman put_task_struct(task); 23381da177e4SLinus Torvalds out: 2339cd6a3ce9SEric W. Biederman return result; 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds /* 23430804ef4bSEric W. Biederman * Find the first task with tgid >= tgid 23440bc58a91SEric W. Biederman * 23451da177e4SLinus Torvalds */ 23460804ef4bSEric W. Biederman static struct task_struct *next_tgid(unsigned int tgid) 23471da177e4SLinus Torvalds { 23480804ef4bSEric W. Biederman struct task_struct *task; 23490804ef4bSEric W. Biederman struct pid *pid; 23501da177e4SLinus Torvalds 23510804ef4bSEric W. Biederman rcu_read_lock(); 23520804ef4bSEric W. Biederman retry: 23530804ef4bSEric W. Biederman task = NULL; 2354198fe21bSPavel Emelyanov pid = find_ge_pid(tgid, &init_pid_ns); 23550804ef4bSEric W. Biederman if (pid) { 23560804ef4bSEric W. Biederman tgid = pid->nr + 1; 23570804ef4bSEric W. Biederman task = pid_task(pid, PIDTYPE_PID); 23580804ef4bSEric W. Biederman /* What we to know is if the pid we have find is the 23590804ef4bSEric W. Biederman * pid of a thread_group_leader. Testing for task 23600804ef4bSEric W. Biederman * being a thread_group_leader is the obvious thing 23610804ef4bSEric W. Biederman * todo but there is a window when it fails, due to 23620804ef4bSEric W. Biederman * the pid transfer logic in de_thread. 23630804ef4bSEric W. Biederman * 23640804ef4bSEric W. Biederman * So we perform the straight forward test of seeing 23650804ef4bSEric W. Biederman * if the pid we have found is the pid of a thread 23660804ef4bSEric W. Biederman * group leader, and don't worry if the task we have 23670804ef4bSEric W. Biederman * found doesn't happen to be a thread group leader. 23680804ef4bSEric W. Biederman * As we don't care in the case of readdir. 23690bc58a91SEric W. Biederman */ 23700804ef4bSEric W. Biederman if (!task || !has_group_leader_pid(task)) 23710804ef4bSEric W. Biederman goto retry; 23720804ef4bSEric W. Biederman get_task_struct(task); 23731da177e4SLinus Torvalds } 2374454cc105SEric W. Biederman rcu_read_unlock(); 23750804ef4bSEric W. Biederman return task; 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds 23787bcd6b0eSEric W. Biederman #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff)) 23791da177e4SLinus Torvalds 238061a28784SEric W. Biederman static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 238161a28784SEric W. Biederman struct task_struct *task, int tgid) 238261a28784SEric W. Biederman { 238361a28784SEric W. Biederman char name[PROC_NUMBUF]; 238461a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tgid); 238561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 238661a28784SEric W. Biederman proc_pid_instantiate, task, NULL); 238761a28784SEric W. Biederman } 238861a28784SEric W. Biederman 23891da177e4SLinus Torvalds /* for the /proc/ directory itself, after non-process stuff has been done */ 23901da177e4SLinus Torvalds int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir) 23911da177e4SLinus Torvalds { 23921da177e4SLinus Torvalds unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY; 23932fddfeefSJosef "Jeff" Sipek struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode); 23940bc58a91SEric W. Biederman struct task_struct *task; 23950bc58a91SEric W. Biederman int tgid; 23961da177e4SLinus Torvalds 239761a28784SEric W. Biederman if (!reaper) 239861a28784SEric W. Biederman goto out_no_task; 239961a28784SEric W. Biederman 24007bcd6b0eSEric W. Biederman for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) { 2401c5141e6dSEric Dumazet const struct pid_entry *p = &proc_base_stuff[nr]; 240261a28784SEric W. Biederman if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0) 2403801199ceSEric W. Biederman goto out; 24041da177e4SLinus Torvalds } 24051da177e4SLinus Torvalds 24060804ef4bSEric W. Biederman tgid = filp->f_pos - TGID_OFFSET; 24070804ef4bSEric W. Biederman for (task = next_tgid(tgid); 24080bc58a91SEric W. Biederman task; 24090804ef4bSEric W. Biederman put_task_struct(task), task = next_tgid(tgid + 1)) { 24100bc58a91SEric W. Biederman tgid = task->pid; 24110804ef4bSEric W. Biederman filp->f_pos = tgid + TGID_OFFSET; 241261a28784SEric W. Biederman if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) { 24130bc58a91SEric W. Biederman put_task_struct(task); 24140804ef4bSEric W. Biederman goto out; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds } 24170804ef4bSEric W. Biederman filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET; 24180804ef4bSEric W. Biederman out: 241961a28784SEric W. Biederman put_task_struct(reaper); 242061a28784SEric W. Biederman out_no_task: 24211da177e4SLinus Torvalds return 0; 24221da177e4SLinus Torvalds } 24231da177e4SLinus Torvalds 24240bc58a91SEric W. Biederman /* 242528a6d671SEric W. Biederman * Tasks 242628a6d671SEric W. Biederman */ 2427c5141e6dSEric Dumazet static const struct pid_entry tid_base_stuff[] = { 242861a28784SEric W. Biederman DIR("fd", S_IRUSR|S_IXUSR, fd), 242927932742SMiklos Szeredi DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo), 2430315e28c8SJames Pearson REG("environ", S_IRUSR, environ), 243161a28784SEric W. Biederman INF("auxv", S_IRUSR, pid_auxv), 243261a28784SEric W. Biederman INF("status", S_IRUGO, pid_status), 243343ae34cbSIngo Molnar #ifdef CONFIG_SCHED_DEBUG 243443ae34cbSIngo Molnar REG("sched", S_IRUGO|S_IWUSR, pid_sched), 243543ae34cbSIngo Molnar #endif 243661a28784SEric W. Biederman INF("cmdline", S_IRUGO, pid_cmdline), 243761a28784SEric W. Biederman INF("stat", S_IRUGO, tid_stat), 243861a28784SEric W. Biederman INF("statm", S_IRUGO, pid_statm), 243961a28784SEric W. Biederman REG("maps", S_IRUGO, maps), 244028a6d671SEric W. Biederman #ifdef CONFIG_NUMA 244161a28784SEric W. Biederman REG("numa_maps", S_IRUGO, numa_maps), 244228a6d671SEric W. Biederman #endif 244361a28784SEric W. Biederman REG("mem", S_IRUSR|S_IWUSR, mem), 244461a28784SEric W. Biederman LNK("cwd", cwd), 244561a28784SEric W. Biederman LNK("root", root), 244661a28784SEric W. Biederman LNK("exe", exe), 244761a28784SEric W. Biederman REG("mounts", S_IRUGO, mounts), 244828a6d671SEric W. Biederman #ifdef CONFIG_MMU 2449b813e931SDavid Rientjes REG("clear_refs", S_IWUSR, clear_refs), 245061a28784SEric W. Biederman REG("smaps", S_IRUGO, smaps), 245128a6d671SEric W. Biederman #endif 245228a6d671SEric W. Biederman #ifdef CONFIG_SECURITY 245372d9dcfcSEric W. Biederman DIR("attr", S_IRUGO|S_IXUGO, attr_dir), 245428a6d671SEric W. Biederman #endif 245528a6d671SEric W. Biederman #ifdef CONFIG_KALLSYMS 245661a28784SEric W. Biederman INF("wchan", S_IRUGO, pid_wchan), 245728a6d671SEric W. Biederman #endif 245828a6d671SEric W. Biederman #ifdef CONFIG_SCHEDSTATS 245961a28784SEric W. Biederman INF("schedstat", S_IRUGO, pid_schedstat), 246028a6d671SEric W. Biederman #endif 24618793d854SPaul Menage #ifdef CONFIG_PROC_PID_CPUSET 246261a28784SEric W. Biederman REG("cpuset", S_IRUGO, cpuset), 246328a6d671SEric W. Biederman #endif 2464a424316cSPaul Menage #ifdef CONFIG_CGROUPS 2465a424316cSPaul Menage REG("cgroup", S_IRUGO, cgroup), 2466a424316cSPaul Menage #endif 246761a28784SEric W. Biederman INF("oom_score", S_IRUGO, oom_score), 246861a28784SEric W. Biederman REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust), 246928a6d671SEric W. Biederman #ifdef CONFIG_AUDITSYSCALL 247061a28784SEric W. Biederman REG("loginuid", S_IWUSR|S_IRUGO, loginuid), 247128a6d671SEric W. Biederman #endif 2472f4f154fdSAkinobu Mita #ifdef CONFIG_FAULT_INJECTION 2473f4f154fdSAkinobu Mita REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject), 2474f4f154fdSAkinobu Mita #endif 247528a6d671SEric W. Biederman }; 247628a6d671SEric W. Biederman 247728a6d671SEric W. Biederman static int proc_tid_base_readdir(struct file * filp, 247828a6d671SEric W. Biederman void * dirent, filldir_t filldir) 247928a6d671SEric W. Biederman { 248028a6d671SEric W. Biederman return proc_pident_readdir(filp,dirent,filldir, 248128a6d671SEric W. Biederman tid_base_stuff,ARRAY_SIZE(tid_base_stuff)); 248228a6d671SEric W. Biederman } 248328a6d671SEric W. Biederman 248428a6d671SEric W. Biederman static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){ 24857bcd6b0eSEric W. Biederman return proc_pident_lookup(dir, dentry, 24867bcd6b0eSEric W. Biederman tid_base_stuff, ARRAY_SIZE(tid_base_stuff)); 248728a6d671SEric W. Biederman } 248828a6d671SEric W. Biederman 248900977a59SArjan van de Ven static const struct file_operations proc_tid_base_operations = { 249028a6d671SEric W. Biederman .read = generic_read_dir, 249128a6d671SEric W. Biederman .readdir = proc_tid_base_readdir, 249228a6d671SEric W. Biederman }; 249328a6d671SEric W. Biederman 2494c5ef1c42SArjan van de Ven static const struct inode_operations proc_tid_base_inode_operations = { 249528a6d671SEric W. Biederman .lookup = proc_tid_base_lookup, 249628a6d671SEric W. Biederman .getattr = pid_getattr, 249728a6d671SEric W. Biederman .setattr = proc_setattr, 249828a6d671SEric W. Biederman }; 249928a6d671SEric W. Biederman 2500444ceed8SEric W. Biederman static struct dentry *proc_task_instantiate(struct inode *dir, 2501c5141e6dSEric Dumazet struct dentry *dentry, struct task_struct *task, const void *ptr) 2502444ceed8SEric W. Biederman { 2503444ceed8SEric W. Biederman struct dentry *error = ERR_PTR(-ENOENT); 2504444ceed8SEric W. Biederman struct inode *inode; 250561a28784SEric W. Biederman inode = proc_pid_make_inode(dir->i_sb, task); 2506444ceed8SEric W. Biederman 2507444ceed8SEric W. Biederman if (!inode) 2508444ceed8SEric W. Biederman goto out; 2509444ceed8SEric W. Biederman inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO; 2510444ceed8SEric W. Biederman inode->i_op = &proc_tid_base_inode_operations; 2511444ceed8SEric W. Biederman inode->i_fop = &proc_tid_base_operations; 2512444ceed8SEric W. Biederman inode->i_flags|=S_IMMUTABLE; 251327932742SMiklos Szeredi inode->i_nlink = 4; 2514444ceed8SEric W. Biederman #ifdef CONFIG_SECURITY 2515444ceed8SEric W. Biederman inode->i_nlink += 1; 2516444ceed8SEric W. Biederman #endif 2517444ceed8SEric W. Biederman 2518444ceed8SEric W. Biederman dentry->d_op = &pid_dentry_operations; 2519444ceed8SEric W. Biederman 2520444ceed8SEric W. Biederman d_add(dentry, inode); 2521444ceed8SEric W. Biederman /* Close the race of the process dying before we return the dentry */ 2522444ceed8SEric W. Biederman if (pid_revalidate(dentry, NULL)) 2523444ceed8SEric W. Biederman error = NULL; 2524444ceed8SEric W. Biederman out: 2525444ceed8SEric W. Biederman return error; 2526444ceed8SEric W. Biederman } 2527444ceed8SEric W. Biederman 252828a6d671SEric W. Biederman static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd) 252928a6d671SEric W. Biederman { 253028a6d671SEric W. Biederman struct dentry *result = ERR_PTR(-ENOENT); 253128a6d671SEric W. Biederman struct task_struct *task; 253228a6d671SEric W. Biederman struct task_struct *leader = get_proc_task(dir); 253328a6d671SEric W. Biederman unsigned tid; 253428a6d671SEric W. Biederman 253528a6d671SEric W. Biederman if (!leader) 253628a6d671SEric W. Biederman goto out_no_task; 253728a6d671SEric W. Biederman 253828a6d671SEric W. Biederman tid = name_to_int(dentry); 253928a6d671SEric W. Biederman if (tid == ~0U) 254028a6d671SEric W. Biederman goto out; 254128a6d671SEric W. Biederman 254228a6d671SEric W. Biederman rcu_read_lock(); 254328a6d671SEric W. Biederman task = find_task_by_pid(tid); 254428a6d671SEric W. Biederman if (task) 254528a6d671SEric W. Biederman get_task_struct(task); 254628a6d671SEric W. Biederman rcu_read_unlock(); 254728a6d671SEric W. Biederman if (!task) 254828a6d671SEric W. Biederman goto out; 254928a6d671SEric W. Biederman if (leader->tgid != task->tgid) 255028a6d671SEric W. Biederman goto out_drop_task; 255128a6d671SEric W. Biederman 2552444ceed8SEric W. Biederman result = proc_task_instantiate(dir, dentry, task, NULL); 255328a6d671SEric W. Biederman out_drop_task: 255428a6d671SEric W. Biederman put_task_struct(task); 255528a6d671SEric W. Biederman out: 255628a6d671SEric W. Biederman put_task_struct(leader); 255728a6d671SEric W. Biederman out_no_task: 255828a6d671SEric W. Biederman return result; 255928a6d671SEric W. Biederman } 256028a6d671SEric W. Biederman 256128a6d671SEric W. Biederman /* 25620bc58a91SEric W. Biederman * Find the first tid of a thread group to return to user space. 25630bc58a91SEric W. Biederman * 25640bc58a91SEric W. Biederman * Usually this is just the thread group leader, but if the users 25650bc58a91SEric W. Biederman * buffer was too small or there was a seek into the middle of the 25660bc58a91SEric W. Biederman * directory we have more work todo. 25670bc58a91SEric W. Biederman * 25680bc58a91SEric W. Biederman * In the case of a short read we start with find_task_by_pid. 25690bc58a91SEric W. Biederman * 25700bc58a91SEric W. Biederman * In the case of a seek we start with the leader and walk nr 25710bc58a91SEric W. Biederman * threads past it. 25720bc58a91SEric W. Biederman */ 2573cc288738SEric W. Biederman static struct task_struct *first_tid(struct task_struct *leader, 2574cc288738SEric W. Biederman int tid, int nr) 25750bc58a91SEric W. Biederman { 2576a872ff0cSOleg Nesterov struct task_struct *pos; 25770bc58a91SEric W. Biederman 2578cc288738SEric W. Biederman rcu_read_lock(); 25790bc58a91SEric W. Biederman /* Attempt to start with the pid of a thread */ 25800bc58a91SEric W. Biederman if (tid && (nr > 0)) { 25810bc58a91SEric W. Biederman pos = find_task_by_pid(tid); 2582a872ff0cSOleg Nesterov if (pos && (pos->group_leader == leader)) 2583a872ff0cSOleg Nesterov goto found; 25840bc58a91SEric W. Biederman } 25850bc58a91SEric W. Biederman 25860bc58a91SEric W. Biederman /* If nr exceeds the number of threads there is nothing todo */ 25870bc58a91SEric W. Biederman pos = NULL; 2588a872ff0cSOleg Nesterov if (nr && nr >= get_nr_threads(leader)) 2589a872ff0cSOleg Nesterov goto out; 2590a872ff0cSOleg Nesterov 2591a872ff0cSOleg Nesterov /* If we haven't found our starting place yet start 2592a872ff0cSOleg Nesterov * with the leader and walk nr threads forward. 2593a872ff0cSOleg Nesterov */ 2594a872ff0cSOleg Nesterov for (pos = leader; nr > 0; --nr) { 2595a872ff0cSOleg Nesterov pos = next_thread(pos); 2596a872ff0cSOleg Nesterov if (pos == leader) { 2597a872ff0cSOleg Nesterov pos = NULL; 2598a872ff0cSOleg Nesterov goto out; 2599a872ff0cSOleg Nesterov } 2600a872ff0cSOleg Nesterov } 2601a872ff0cSOleg Nesterov found: 2602a872ff0cSOleg Nesterov get_task_struct(pos); 2603a872ff0cSOleg Nesterov out: 2604cc288738SEric W. Biederman rcu_read_unlock(); 26050bc58a91SEric W. Biederman return pos; 26060bc58a91SEric W. Biederman } 26070bc58a91SEric W. Biederman 26080bc58a91SEric W. Biederman /* 26090bc58a91SEric W. Biederman * Find the next thread in the thread list. 26100bc58a91SEric W. Biederman * Return NULL if there is an error or no next thread. 26110bc58a91SEric W. Biederman * 26120bc58a91SEric W. Biederman * The reference to the input task_struct is released. 26130bc58a91SEric W. Biederman */ 26140bc58a91SEric W. Biederman static struct task_struct *next_tid(struct task_struct *start) 26150bc58a91SEric W. Biederman { 2616c1df7fb8SOleg Nesterov struct task_struct *pos = NULL; 2617cc288738SEric W. Biederman rcu_read_lock(); 2618c1df7fb8SOleg Nesterov if (pid_alive(start)) { 26190bc58a91SEric W. Biederman pos = next_thread(start); 2620c1df7fb8SOleg Nesterov if (thread_group_leader(pos)) 26210bc58a91SEric W. Biederman pos = NULL; 2622c1df7fb8SOleg Nesterov else 2623c1df7fb8SOleg Nesterov get_task_struct(pos); 2624c1df7fb8SOleg Nesterov } 2625cc288738SEric W. Biederman rcu_read_unlock(); 26260bc58a91SEric W. Biederman put_task_struct(start); 26270bc58a91SEric W. Biederman return pos; 26280bc58a91SEric W. Biederman } 26290bc58a91SEric W. Biederman 263061a28784SEric W. Biederman static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir, 263161a28784SEric W. Biederman struct task_struct *task, int tid) 263261a28784SEric W. Biederman { 263361a28784SEric W. Biederman char name[PROC_NUMBUF]; 263461a28784SEric W. Biederman int len = snprintf(name, sizeof(name), "%d", tid); 263561a28784SEric W. Biederman return proc_fill_cache(filp, dirent, filldir, name, len, 263661a28784SEric W. Biederman proc_task_instantiate, task, NULL); 263761a28784SEric W. Biederman } 263861a28784SEric W. Biederman 26391da177e4SLinus Torvalds /* for the /proc/TGID/task/ directories */ 26401da177e4SLinus Torvalds static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir) 26411da177e4SLinus Torvalds { 26422fddfeefSJosef "Jeff" Sipek struct dentry *dentry = filp->f_path.dentry; 26431da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 26447d895244SGuillaume Chazarain struct task_struct *leader = NULL; 26450bc58a91SEric W. Biederman struct task_struct *task; 26461da177e4SLinus Torvalds int retval = -ENOENT; 26471da177e4SLinus Torvalds ino_t ino; 26480bc58a91SEric W. Biederman int tid; 26491da177e4SLinus Torvalds unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */ 26501da177e4SLinus Torvalds 26517d895244SGuillaume Chazarain task = get_proc_task(inode); 26527d895244SGuillaume Chazarain if (!task) 26537d895244SGuillaume Chazarain goto out_no_task; 26547d895244SGuillaume Chazarain rcu_read_lock(); 26557d895244SGuillaume Chazarain if (pid_alive(task)) { 26567d895244SGuillaume Chazarain leader = task->group_leader; 26577d895244SGuillaume Chazarain get_task_struct(leader); 26587d895244SGuillaume Chazarain } 26597d895244SGuillaume Chazarain rcu_read_unlock(); 26607d895244SGuillaume Chazarain put_task_struct(task); 266199f89551SEric W. Biederman if (!leader) 266299f89551SEric W. Biederman goto out_no_task; 26631da177e4SLinus Torvalds retval = 0; 26641da177e4SLinus Torvalds 26651da177e4SLinus Torvalds switch (pos) { 26661da177e4SLinus Torvalds case 0: 26671da177e4SLinus Torvalds ino = inode->i_ino; 26681da177e4SLinus Torvalds if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0) 26691da177e4SLinus Torvalds goto out; 26701da177e4SLinus Torvalds pos++; 26711da177e4SLinus Torvalds /* fall through */ 26721da177e4SLinus Torvalds case 1: 26731da177e4SLinus Torvalds ino = parent_ino(dentry); 26741da177e4SLinus Torvalds if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0) 26751da177e4SLinus Torvalds goto out; 26761da177e4SLinus Torvalds pos++; 26771da177e4SLinus Torvalds /* fall through */ 26781da177e4SLinus Torvalds } 26791da177e4SLinus Torvalds 26800bc58a91SEric W. Biederman /* f_version caches the tgid value that the last readdir call couldn't 26810bc58a91SEric W. Biederman * return. lseek aka telldir automagically resets f_version to 0. 26820bc58a91SEric W. Biederman */ 26832b47c361SMathieu Desnoyers tid = (int)filp->f_version; 26840bc58a91SEric W. Biederman filp->f_version = 0; 26850bc58a91SEric W. Biederman for (task = first_tid(leader, tid, pos - 2); 26860bc58a91SEric W. Biederman task; 26870bc58a91SEric W. Biederman task = next_tid(task), pos++) { 26880bc58a91SEric W. Biederman tid = task->pid; 268961a28784SEric W. Biederman if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) { 26900bc58a91SEric W. Biederman /* returning this tgid failed, save it as the first 26910bc58a91SEric W. Biederman * pid for the next readir call */ 26922b47c361SMathieu Desnoyers filp->f_version = (u64)tid; 26930bc58a91SEric W. Biederman put_task_struct(task); 26941da177e4SLinus Torvalds break; 26950bc58a91SEric W. Biederman } 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds out: 26981da177e4SLinus Torvalds filp->f_pos = pos; 269999f89551SEric W. Biederman put_task_struct(leader); 270099f89551SEric W. Biederman out_no_task: 27011da177e4SLinus Torvalds return retval; 27021da177e4SLinus Torvalds } 27036e66b52bSEric W. Biederman 27046e66b52bSEric W. Biederman static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) 27056e66b52bSEric W. Biederman { 27066e66b52bSEric W. Biederman struct inode *inode = dentry->d_inode; 270799f89551SEric W. Biederman struct task_struct *p = get_proc_task(inode); 27086e66b52bSEric W. Biederman generic_fillattr(inode, stat); 27096e66b52bSEric W. Biederman 271099f89551SEric W. Biederman if (p) { 271199f89551SEric W. Biederman rcu_read_lock(); 271299f89551SEric W. Biederman stat->nlink += get_nr_threads(p); 271399f89551SEric W. Biederman rcu_read_unlock(); 271499f89551SEric W. Biederman put_task_struct(p); 27156e66b52bSEric W. Biederman } 27166e66b52bSEric W. Biederman 27176e66b52bSEric W. Biederman return 0; 27186e66b52bSEric W. Biederman } 271928a6d671SEric W. Biederman 2720c5ef1c42SArjan van de Ven static const struct inode_operations proc_task_inode_operations = { 272128a6d671SEric W. Biederman .lookup = proc_task_lookup, 272228a6d671SEric W. Biederman .getattr = proc_task_getattr, 272328a6d671SEric W. Biederman .setattr = proc_setattr, 272428a6d671SEric W. Biederman }; 272528a6d671SEric W. Biederman 272600977a59SArjan van de Ven static const struct file_operations proc_task_operations = { 272728a6d671SEric W. Biederman .read = generic_read_dir, 272828a6d671SEric W. Biederman .readdir = proc_task_readdir, 272928a6d671SEric W. Biederman }; 2730