4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/tracehook.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include <linux/vs_context.h>
78 #include <linux/vs_network.h>
83 * Implementing inode permission operations in /proc is almost
84 * certainly an error. Permission checks need to happen during
85 * each system call not at open time. The reason is that most of
86 * what we wish to check for permissions in /proc varies at runtime.
88 * The classic example of a problem is opening file descriptors
89 * in /proc for a task before it execs a suid executable.
93 /* Worst case buffer size needed for holding an integer. */
94 #define PROC_NUMBUF 13
100 struct inode_operations *iop;
101 struct file_operations *fop;
105 #define NOD(NAME, MODE, IOP, FOP, OP) { \
106 .len = sizeof(NAME) - 1, \
114 #define DIR(NAME, MODE, OTYPE) \
115 NOD(NAME, (S_IFDIR|(MODE)), \
116 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
118 #define LNK(NAME, OTYPE) \
119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
120 &proc_pid_link_inode_operations, NULL, \
121 { .proc_get_link = &proc_##OTYPE##_link } )
122 #define REG(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, \
124 &proc_##OTYPE##_operations, {})
125 #define INF(NAME, MODE, OTYPE) \
126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_info_file_operations, \
128 { .proc_read = &proc_##OTYPE } )
130 static struct fs_struct *get_fs_struct(struct task_struct *task)
132 struct fs_struct *fs;
136 atomic_inc(&fs->count);
141 static int get_nr_threads(struct task_struct *tsk)
143 /* Must be called with the rcu_read_lock held */
147 if (lock_task_sighand(tsk, &flags)) {
148 count = atomic_read(&tsk->signal->count);
149 unlock_task_sighand(tsk, &flags);
154 static int __ptrace_may_attach(struct task_struct *task)
156 /* May we inspect the given task?
157 * This check is used both for attaching with ptrace
158 * and for allowing access to sensitive information in /proc.
160 * ptrace_attach denies several cases that /proc allows
161 * because setting up the necessary parent/child relationship
162 * or halting the specified task is impossible.
165 /* Don't let security modules deny introspection */
168 if (((current->uid != task->euid) ||
169 (current->uid != task->suid) ||
170 (current->uid != task->uid) ||
171 (current->gid != task->egid) ||
172 (current->gid != task->sgid) ||
173 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
177 dumpable = task->mm->dumpable;
178 if (!dumpable && !capable(CAP_SYS_PTRACE))
180 if (!vx_check(task->xid, VS_ADMIN_P|VS_IDENT))
182 if (!vx_check(task->xid, VS_IDENT) &&
183 !task_vx_flags(task, VXF_STATE_ADMIN, 0))
186 return security_ptrace(current, task);
189 int ptrace_may_attach(struct task_struct *task)
193 err = __ptrace_may_attach(task);
199 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
201 struct task_struct *task = get_proc_task(inode);
202 struct fs_struct *fs = NULL;
203 int result = -ENOENT;
206 fs = get_fs_struct(task);
207 put_task_struct(task);
210 read_lock(&fs->lock);
211 *mnt = mntget(fs->pwdmnt);
212 *dentry = dget(fs->pwd);
213 read_unlock(&fs->lock);
220 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
222 struct task_struct *task = get_proc_task(inode);
223 struct fs_struct *fs = NULL;
224 int result = -ENOENT;
227 fs = get_fs_struct(task);
228 put_task_struct(task);
231 read_lock(&fs->lock);
232 *mnt = mntget(fs->rootmnt);
233 *dentry = dget(fs->root);
234 read_unlock(&fs->lock);
241 struct mm_struct *mm_for_maps(struct task_struct *task)
243 struct mm_struct *mm = get_task_mm(task);
246 down_read(&mm->mmap_sem);
250 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
256 up_read(&mm->mmap_sem);
261 static int proc_pid_environ(struct task_struct *task, char * buffer)
264 struct mm_struct *mm = get_task_mm(task);
266 unsigned int len = mm->env_end - mm->env_start;
269 res = access_process_vm(task, mm->env_start, buffer, len, 0);
270 if (!ptrace_may_attach(task))
277 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
281 struct mm_struct *mm = get_task_mm(task);
285 goto out_mm; /* Shh! No looking before we're done */
287 len = mm->arg_end - mm->arg_start;
292 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
294 // If the nul at the end of args has been overwritten, then
295 // assume application is using setproctitle(3).
296 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
297 len = strnlen(buffer, res);
301 len = mm->env_end - mm->env_start;
302 if (len > PAGE_SIZE - res)
303 len = PAGE_SIZE - res;
304 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
305 res = strnlen(buffer, res);
314 static int proc_pid_auxv(struct task_struct *task, char *buffer)
317 struct mm_struct *mm = get_task_mm(task);
319 unsigned int nwords = 0;
322 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
323 res = nwords * sizeof(mm->saved_auxv[0]);
326 memcpy(buffer, mm->saved_auxv, res);
333 #ifdef CONFIG_KALLSYMS
335 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
336 * Returns the resolved symbol. If that fails, simply return the address.
338 static int proc_pid_wchan(struct task_struct *task, char *buffer)
341 const char *sym_name;
342 unsigned long wchan, size, offset;
343 char namebuf[KSYM_NAME_LEN+1];
345 wchan = get_wchan(task);
347 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
349 return sprintf(buffer, "%s", sym_name);
350 return sprintf(buffer, "%lu", wchan);
352 #endif /* CONFIG_KALLSYMS */
354 #ifdef CONFIG_SCHEDSTATS
356 * Provides /proc/PID/schedstat
358 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
360 return sprintf(buffer, "%lu %lu %lu\n",
361 task->sched_info.cpu_time,
362 task->sched_info.run_delay,
363 task->sched_info.pcnt);
367 /* The badness from the OOM killer */
368 unsigned long badness(struct task_struct *p, unsigned long uptime);
369 static int proc_oom_score(struct task_struct *task, char *buffer)
371 unsigned long points;
372 struct timespec uptime;
374 do_posix_clock_monotonic_gettime(&uptime);
375 points = badness(task, uptime.tv_sec);
376 return sprintf(buffer, "%lu\n", points);
379 /************************************************************************/
380 /* Here the fs part begins */
381 /************************************************************************/
383 /* permission checks */
384 static int proc_fd_access_allowed(struct inode *inode)
386 struct task_struct *task;
388 /* Allow access to a task's file descriptors if it is us or we
389 * may use ptrace attach to the process and find out that
392 task = get_proc_task(inode);
394 allowed = ptrace_may_attach(task);
395 put_task_struct(task);
400 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
403 struct inode *inode = dentry->d_inode;
405 if (attr->ia_valid & ATTR_MODE)
408 error = inode_change_ok(inode, attr);
410 error = security_inode_setattr(dentry, attr);
412 error = inode_setattr(inode, attr);
417 static struct inode_operations proc_def_inode_operations = {
418 .setattr = proc_setattr,
421 extern struct seq_operations mounts_op;
427 static int mounts_open(struct inode *inode, struct file *file)
429 struct task_struct *task = get_proc_task(inode);
430 struct mnt_namespace *ns = NULL;
431 struct proc_mounts *p;
437 ns = task->nsproxy->mnt_ns;
442 put_task_struct(task);
447 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
449 file->private_data = &p->m;
450 ret = seq_open(file, &mounts_op);
453 p->event = ns->event;
463 static int mounts_release(struct inode *inode, struct file *file)
465 struct seq_file *m = file->private_data;
466 struct mnt_namespace *ns = m->private;
468 return seq_release(inode, file);
471 static unsigned mounts_poll(struct file *file, poll_table *wait)
473 struct proc_mounts *p = file->private_data;
474 struct mnt_namespace *ns = p->m.private;
477 poll_wait(file, &ns->poll, wait);
479 spin_lock(&vfsmount_lock);
480 if (p->event != ns->event) {
481 p->event = ns->event;
484 spin_unlock(&vfsmount_lock);
489 static struct file_operations proc_mounts_operations = {
493 .release = mounts_release,
497 extern struct seq_operations mountstats_op;
498 static int mountstats_open(struct inode *inode, struct file *file)
500 int ret = seq_open(file, &mountstats_op);
503 struct seq_file *m = file->private_data;
504 struct mnt_namespace *mnt_ns = NULL;
505 struct task_struct *task = get_proc_task(inode);
510 mnt_ns = task->nsproxy->mnt_ns;
514 put_task_struct(task);
520 seq_release(inode, file);
527 static struct file_operations proc_mountstats_operations = {
528 .open = mountstats_open,
531 .release = mounts_release,
534 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
536 static ssize_t proc_info_read(struct file * file, char __user * buf,
537 size_t count, loff_t *ppos)
539 struct inode * inode = file->f_path.dentry->d_inode;
542 struct task_struct *task = get_proc_task(inode);
548 if (count > PROC_BLOCK_SIZE)
549 count = PROC_BLOCK_SIZE;
552 if (!(page = __get_free_page(GFP_KERNEL)))
555 length = PROC_I(inode)->op.proc_read(task, (char*)page);
558 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
561 put_task_struct(task);
566 static struct file_operations proc_info_file_operations = {
567 .read = proc_info_read,
570 static int mem_open(struct inode* inode, struct file* file)
572 file->private_data = (void*)((long)current->self_exec_id);
576 static ssize_t mem_read(struct file * file, char __user * buf,
577 size_t count, loff_t *ppos)
579 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
581 unsigned long src = *ppos;
583 struct mm_struct *mm;
588 if (!tracehook_allow_access_process_vm(task)
589 || !ptrace_may_attach(task))
593 page = (char *)__get_free_page(GFP_USER);
599 mm = get_task_mm(task);
605 if (file->private_data != (void*)((long)current->self_exec_id))
611 int this_len, retval;
613 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
614 retval = access_process_vm(task, src, page, this_len, 0);
615 if (!retval || !tracehook_allow_access_process_vm(task)
616 || !ptrace_may_attach(task)) {
622 if (copy_to_user(buf, page, retval)) {
637 free_page((unsigned long) page);
639 put_task_struct(task);
644 #define mem_write NULL
647 /* This is a security hazard */
648 static ssize_t mem_write(struct file * file, const char * buf,
649 size_t count, loff_t *ppos)
653 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
654 unsigned long dst = *ppos;
660 if (!tracehook_allow_access_process_vm(task)
661 || !ptrace_may_attach(task))
665 page = (char *)__get_free_page(GFP_USER);
671 int this_len, retval;
673 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
674 if (copy_from_user(page, buf, this_len)) {
678 retval = access_process_vm(task, dst, page, this_len, 1);
690 free_page((unsigned long) page);
692 put_task_struct(task);
698 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
702 file->f_pos = offset;
705 file->f_pos += offset;
710 force_successful_syscall_return();
714 static struct file_operations proc_mem_operations = {
721 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
722 size_t count, loff_t *ppos)
724 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
725 char buffer[PROC_NUMBUF];
728 loff_t __ppos = *ppos;
732 oom_adjust = task->oomkilladj;
733 put_task_struct(task);
735 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
738 if (count > len-__ppos)
740 if (copy_to_user(buf, buffer + __ppos, count))
742 *ppos = __ppos + count;
746 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
747 size_t count, loff_t *ppos)
749 struct task_struct *task;
750 char buffer[PROC_NUMBUF], *end;
753 memset(buffer, 0, sizeof(buffer));
754 if (count > sizeof(buffer) - 1)
755 count = sizeof(buffer) - 1;
756 if (copy_from_user(buffer, buf, count))
758 oom_adjust = simple_strtol(buffer, &end, 0);
759 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
760 oom_adjust != OOM_DISABLE)
764 task = get_proc_task(file->f_path.dentry->d_inode);
767 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
768 put_task_struct(task);
771 task->oomkilladj = oom_adjust;
772 put_task_struct(task);
773 if (end - buffer == 0)
778 static struct file_operations proc_oom_adjust_operations = {
779 .read = oom_adjust_read,
780 .write = oom_adjust_write,
783 #ifdef CONFIG_AUDITSYSCALL
785 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
786 size_t count, loff_t *ppos)
788 struct inode * inode = file->f_path.dentry->d_inode;
789 struct task_struct *task = get_proc_task(inode);
791 char tmpbuf[TMPBUFLEN];
795 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
796 audit_get_loginuid(task->audit_context));
797 put_task_struct(task);
798 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
801 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
802 size_t count, loff_t *ppos)
804 struct inode * inode = file->f_path.dentry->d_inode;
809 if (!capable(CAP_AUDIT_CONTROL))
812 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
815 if (count >= PAGE_SIZE)
816 count = PAGE_SIZE - 1;
819 /* No partial writes. */
822 page = (char*)__get_free_page(GFP_USER);
826 if (copy_from_user(page, buf, count))
830 loginuid = simple_strtoul(page, &tmp, 10);
836 length = audit_set_loginuid(current, loginuid);
837 if (likely(length == 0))
841 free_page((unsigned long) page);
845 static struct file_operations proc_loginuid_operations = {
846 .read = proc_loginuid_read,
847 .write = proc_loginuid_write,
851 #ifdef CONFIG_SECCOMP
852 static ssize_t seccomp_read(struct file *file, char __user *buf,
853 size_t count, loff_t *ppos)
855 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
857 loff_t __ppos = *ppos;
862 /* no need to print the trailing zero, so use only len */
863 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
864 put_task_struct(tsk);
867 if (count > len - __ppos)
868 count = len - __ppos;
869 if (copy_to_user(buf, __buf + __ppos, count))
871 *ppos = __ppos + count;
875 static ssize_t seccomp_write(struct file *file, const char __user *buf,
876 size_t count, loff_t *ppos)
878 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
879 char __buf[20], *end;
880 unsigned int seccomp_mode;
887 /* can set it only once to be even more secure */
889 if (unlikely(tsk->seccomp.mode))
893 memset(__buf, 0, sizeof(__buf));
894 count = min(count, sizeof(__buf) - 1);
895 if (copy_from_user(__buf, buf, count))
898 seccomp_mode = simple_strtoul(__buf, &end, 0);
902 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
903 tsk->seccomp.mode = seccomp_mode;
904 set_tsk_thread_flag(tsk, TIF_SECCOMP);
908 if (unlikely(!(end - __buf)))
910 result = end - __buf;
912 put_task_struct(tsk);
917 static struct file_operations proc_seccomp_operations = {
918 .read = seccomp_read,
919 .write = seccomp_write,
921 #endif /* CONFIG_SECCOMP */
923 #ifdef CONFIG_FAULT_INJECTION
924 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
925 size_t count, loff_t *ppos)
927 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
928 char buffer[PROC_NUMBUF];
931 loff_t __ppos = *ppos;
935 make_it_fail = task->make_it_fail;
936 put_task_struct(task);
938 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
941 if (count > len-__ppos)
943 if (copy_to_user(buf, buffer + __ppos, count))
945 *ppos = __ppos + count;
949 static ssize_t proc_fault_inject_write(struct file * file,
950 const char __user * buf, size_t count, loff_t *ppos)
952 struct task_struct *task;
953 char buffer[PROC_NUMBUF], *end;
956 if (!capable(CAP_SYS_RESOURCE))
958 memset(buffer, 0, sizeof(buffer));
959 if (count > sizeof(buffer) - 1)
960 count = sizeof(buffer) - 1;
961 if (copy_from_user(buffer, buf, count))
963 make_it_fail = simple_strtol(buffer, &end, 0);
966 task = get_proc_task(file->f_dentry->d_inode);
969 task->make_it_fail = make_it_fail;
970 put_task_struct(task);
971 if (end - buffer == 0)
976 static struct file_operations proc_fault_inject_operations = {
977 .read = proc_fault_inject_read,
978 .write = proc_fault_inject_write,
982 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
984 struct inode *inode = dentry->d_inode;
987 /* We don't need a base pointer in the /proc filesystem */
990 /* Are we allowed to snoop on the tasks file descriptors? */
991 if (!proc_fd_access_allowed(inode))
994 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
995 nd->last_type = LAST_BIND;
997 return ERR_PTR(error);
1000 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1001 char __user *buffer, int buflen)
1003 struct inode * inode;
1004 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1010 inode = dentry->d_inode;
1011 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1012 len = PTR_ERR(path);
1015 len = tmp + PAGE_SIZE - 1 - path;
1019 if (copy_to_user(buffer, path, len))
1022 free_page((unsigned long)tmp);
1026 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1028 int error = -EACCES;
1029 struct inode *inode = dentry->d_inode;
1031 struct vfsmount *mnt = NULL;
1033 /* Are we allowed to snoop on the tasks file descriptors? */
1034 if (!proc_fd_access_allowed(inode))
1037 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1041 error = do_proc_readlink(de, mnt, buffer, buflen);
1048 static struct inode_operations proc_pid_link_inode_operations = {
1049 .readlink = proc_pid_readlink,
1050 .follow_link = proc_pid_follow_link,
1051 .setattr = proc_setattr,
1055 /* building an inode */
1057 static int task_dumpable(struct task_struct *task)
1060 struct mm_struct *mm;
1065 dumpable = mm->dumpable;
1073 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1075 struct inode * inode;
1076 struct proc_inode *ei;
1078 /* We need a new inode */
1080 inode = new_inode(sb);
1086 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1087 inode->i_op = &proc_def_inode_operations;
1090 * grab the reference to task.
1092 ei->pid = get_task_pid(task, PIDTYPE_PID);
1098 if (task_dumpable(task)) {
1099 inode->i_uid = task->euid;
1100 inode->i_gid = task->egid;
1102 /* procfs is xid tagged */
1103 inode->i_tag = (tag_t)vx_task_xid(task);
1104 security_task_to_inode(task, inode);
1114 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1116 struct inode *inode = dentry->d_inode;
1117 struct task_struct *task;
1118 generic_fillattr(inode, stat);
1123 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1125 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1126 task_dumpable(task)) {
1127 stat->uid = task->euid;
1128 stat->gid = task->egid;
1138 * Exceptional case: normally we are not allowed to unhash a busy
1139 * directory. In this case, however, we can do it - no aliasing problems
1140 * due to the way we treat inodes.
1142 * Rewrite the inode's ownerships here because the owning task may have
1143 * performed a setuid(), etc.
1145 * Before the /proc/pid/status file was created the only way to read
1146 * the effective uid of a /process was to stat /proc/pid. Reading
1147 * /proc/pid/status is slow enough that procps and other packages
1148 * kept stating /proc/pid. To keep the rules in /proc simple I have
1149 * made this apply to all per process world readable and executable
1152 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1154 struct inode *inode = dentry->d_inode;
1155 struct task_struct *task = get_proc_task(inode);
1157 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1158 task_dumpable(task)) {
1159 inode->i_uid = task->euid;
1160 inode->i_gid = task->egid;
1165 inode->i_mode &= ~(S_ISUID | S_ISGID);
1166 security_task_to_inode(task, inode);
1167 put_task_struct(task);
1174 static int pid_delete_dentry(struct dentry * dentry)
1176 /* Is the task we represent dead?
1177 * If so, then don't put the dentry on the lru list,
1178 * kill it immediately.
1180 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1183 static struct dentry_operations pid_dentry_operations =
1185 .d_revalidate = pid_revalidate,
1186 .d_delete = pid_delete_dentry,
1191 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1194 * Fill a directory entry.
1196 * If possible create the dcache entry and derive our inode number and
1197 * file type from dcache entry.
1199 * Since all of the proc inode numbers are dynamically generated, the inode
1200 * numbers do not exist until the inode is cache. This means creating the
1201 * the dcache entry in readdir is necessary to keep the inode numbers
1202 * reported by readdir in sync with the inode numbers reported
1205 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1206 char *name, int len,
1207 instantiate_t instantiate, struct task_struct *task, void *ptr)
1209 struct dentry *child, *dir = filp->f_path.dentry;
1210 struct inode *inode;
1213 unsigned type = DT_UNKNOWN;
1217 qname.hash = full_name_hash(name, len);
1219 child = d_lookup(dir, &qname);
1222 new = d_alloc(dir, &qname);
1224 child = instantiate(dir->d_inode, new, task, ptr);
1231 if (!child || IS_ERR(child) || !child->d_inode)
1232 goto end_instantiate;
1233 inode = child->d_inode;
1236 type = inode->i_mode >> 12;
1241 ino = find_inode_number(dir, &qname);
1244 return filldir(dirent, name, len, filp->f_pos, ino, type);
1247 static unsigned name_to_int(struct dentry *dentry)
1249 const char *name = dentry->d_name.name;
1250 int len = dentry->d_name.len;
1253 if (len > 1 && *name == '0')
1256 unsigned c = *name++ - '0';
1259 if (n >= (~0U-9)/10)
1269 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1271 struct task_struct *task = get_proc_task(inode);
1272 struct files_struct *files = NULL;
1274 int fd = proc_fd(inode);
1277 files = get_files_struct(task);
1278 put_task_struct(task);
1282 * We are not taking a ref to the file structure, so we must
1285 spin_lock(&files->file_lock);
1286 file = fcheck_files(files, fd);
1288 *mnt = mntget(file->f_path.mnt);
1289 *dentry = dget(file->f_path.dentry);
1290 spin_unlock(&files->file_lock);
1291 put_files_struct(files);
1294 spin_unlock(&files->file_lock);
1295 put_files_struct(files);
1300 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1302 struct inode *inode = dentry->d_inode;
1303 struct task_struct *task = get_proc_task(inode);
1304 int fd = proc_fd(inode);
1305 struct files_struct *files;
1308 files = get_files_struct(task);
1311 if (fcheck_files(files, fd)) {
1313 put_files_struct(files);
1314 if (task_dumpable(task)) {
1315 inode->i_uid = task->euid;
1316 inode->i_gid = task->egid;
1321 inode->i_mode &= ~(S_ISUID | S_ISGID);
1322 security_task_to_inode(task, inode);
1323 put_task_struct(task);
1327 put_files_struct(files);
1329 put_task_struct(task);
1335 static struct dentry_operations tid_fd_dentry_operations =
1337 .d_revalidate = tid_fd_revalidate,
1338 .d_delete = pid_delete_dentry,
1341 static struct dentry *proc_fd_instantiate(struct inode *dir,
1342 struct dentry *dentry, struct task_struct *task, void *ptr)
1344 unsigned fd = *(unsigned *)ptr;
1346 struct files_struct *files;
1347 struct inode *inode;
1348 struct proc_inode *ei;
1349 struct dentry *error = ERR_PTR(-ENOENT);
1351 inode = proc_pid_make_inode(dir->i_sb, task);
1356 files = get_files_struct(task);
1359 inode->i_mode = S_IFLNK;
1362 * We are not taking a ref to the file structure, so we must
1365 spin_lock(&files->file_lock);
1366 file = fcheck_files(files, fd);
1369 if (file->f_mode & 1)
1370 inode->i_mode |= S_IRUSR | S_IXUSR;
1371 if (file->f_mode & 2)
1372 inode->i_mode |= S_IWUSR | S_IXUSR;
1373 spin_unlock(&files->file_lock);
1374 put_files_struct(files);
1376 inode->i_op = &proc_pid_link_inode_operations;
1378 ei->op.proc_get_link = proc_fd_link;
1379 dentry->d_op = &tid_fd_dentry_operations;
1380 d_add(dentry, inode);
1381 /* Close the race of the process dying before we return the dentry */
1382 if (tid_fd_revalidate(dentry, NULL))
1388 spin_unlock(&files->file_lock);
1389 put_files_struct(files);
1395 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1397 struct task_struct *task = get_proc_task(dir);
1398 unsigned fd = name_to_int(dentry);
1399 struct dentry *result = ERR_PTR(-ENOENT);
1406 result = proc_fd_instantiate(dir, dentry, task, &fd);
1408 put_task_struct(task);
1413 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1414 struct task_struct *task, int fd)
1416 char name[PROC_NUMBUF];
1417 int len = snprintf(name, sizeof(name), "%d", fd);
1418 return proc_fill_cache(filp, dirent, filldir, name, len,
1419 proc_fd_instantiate, task, &fd);
1422 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1424 struct dentry *dentry = filp->f_path.dentry;
1425 struct inode *inode = dentry->d_inode;
1426 struct task_struct *p = get_proc_task(inode);
1427 unsigned int fd, tid, ino;
1429 struct files_struct * files;
1430 struct fdtable *fdt;
1441 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1445 ino = parent_ino(dentry);
1446 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1450 files = get_files_struct(p);
1454 fdt = files_fdtable(files);
1455 for (fd = filp->f_pos-2;
1457 fd++, filp->f_pos++) {
1459 if (!fcheck_files(files, fd))
1463 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1470 put_files_struct(files);
1478 static struct file_operations proc_fd_operations = {
1479 .read = generic_read_dir,
1480 .readdir = proc_readfd,
1484 * proc directories can do almost nothing..
1486 static struct inode_operations proc_fd_inode_operations = {
1487 .lookup = proc_lookupfd,
1488 .setattr = proc_setattr,
1491 static struct dentry *proc_pident_instantiate(struct inode *dir,
1492 struct dentry *dentry, struct task_struct *task, void *ptr)
1494 struct pid_entry *p = ptr;
1495 struct inode *inode;
1496 struct proc_inode *ei;
1497 struct dentry *error = ERR_PTR(-EINVAL);
1499 inode = proc_pid_make_inode(dir->i_sb, task);
1504 inode->i_mode = p->mode;
1505 if (S_ISDIR(inode->i_mode))
1506 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1508 inode->i_op = p->iop;
1510 inode->i_fop = p->fop;
1512 dentry->d_op = &pid_dentry_operations;
1513 d_add(dentry, inode);
1514 /* Close the race of the process dying before we return the dentry */
1515 if (pid_revalidate(dentry, NULL))
1521 static struct dentry *proc_pident_lookup(struct inode *dir,
1522 struct dentry *dentry,
1523 struct pid_entry *ents,
1526 struct inode *inode;
1527 struct dentry *error;
1528 struct task_struct *task = get_proc_task(dir);
1529 struct pid_entry *p, *last;
1531 error = ERR_PTR(-ENOENT);
1537 /* TODO: maybe we can come up with a generic approach? */
1538 if (task_vx_flags(task, VXF_HIDE_VINFO, 0) &&
1539 (dentry->d_name.len == 5) &&
1540 (!memcmp(dentry->d_name.name, "vinfo", 5) ||
1541 !memcmp(dentry->d_name.name, "ninfo", 5)))
1545 * Yes, it does not scale. And it should not. Don't add
1546 * new entries into /proc/<tgid>/ without very good reasons.
1548 last = &ents[nents - 1];
1549 for (p = ents; p <= last; p++) {
1550 if (p->len != dentry->d_name.len)
1552 if (!memcmp(dentry->d_name.name, p->name, p->len))
1558 error = proc_pident_instantiate(dir, dentry, task, p);
1560 put_task_struct(task);
1565 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1566 struct task_struct *task, struct pid_entry *p)
1568 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1569 proc_pident_instantiate, task, p);
1572 static int proc_pident_readdir(struct file *filp,
1573 void *dirent, filldir_t filldir,
1574 struct pid_entry *ents, unsigned int nents)
1578 struct dentry *dentry = filp->f_path.dentry;
1579 struct inode *inode = dentry->d_inode;
1580 struct task_struct *task = get_proc_task(inode);
1581 struct pid_entry *p, *last;
1595 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1601 ino = parent_ino(dentry);
1602 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1614 last = &ents[nents - 1];
1616 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1625 put_task_struct(task);
1630 #ifdef CONFIG_SECURITY
1631 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1632 size_t count, loff_t *ppos)
1634 struct inode * inode = file->f_path.dentry->d_inode;
1637 struct task_struct *task = get_proc_task(inode);
1643 if (count > PAGE_SIZE)
1646 if (!(page = __get_free_page(GFP_KERNEL)))
1649 length = security_getprocattr(task,
1650 (char*)file->f_path.dentry->d_name.name,
1651 (void*)page, count);
1653 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1656 put_task_struct(task);
1661 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1662 size_t count, loff_t *ppos)
1664 struct inode * inode = file->f_path.dentry->d_inode;
1667 struct task_struct *task = get_proc_task(inode);
1672 if (count > PAGE_SIZE)
1675 /* No partial writes. */
1681 page = (char*)__get_free_page(GFP_USER);
1686 if (copy_from_user(page, buf, count))
1689 length = security_setprocattr(task,
1690 (char*)file->f_path.dentry->d_name.name,
1691 (void*)page, count);
1693 free_page((unsigned long) page);
1695 put_task_struct(task);
1700 static struct file_operations proc_pid_attr_operations = {
1701 .read = proc_pid_attr_read,
1702 .write = proc_pid_attr_write,
1705 static struct pid_entry attr_dir_stuff[] = {
1706 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1707 REG("prev", S_IRUGO, pid_attr),
1708 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1709 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1710 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1711 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1714 static int proc_attr_dir_readdir(struct file * filp,
1715 void * dirent, filldir_t filldir)
1717 return proc_pident_readdir(filp,dirent,filldir,
1718 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1721 static struct file_operations proc_attr_dir_operations = {
1722 .read = generic_read_dir,
1723 .readdir = proc_attr_dir_readdir,
1726 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1727 struct dentry *dentry, struct nameidata *nd)
1729 return proc_pident_lookup(dir, dentry,
1730 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1733 static struct inode_operations proc_attr_dir_inode_operations = {
1734 .lookup = proc_attr_dir_lookup,
1735 .getattr = pid_getattr,
1736 .setattr = proc_setattr,
1744 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1747 char tmp[PROC_NUMBUF];
1748 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1749 return vfs_readlink(dentry,buffer,buflen,tmp);
1752 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1754 char tmp[PROC_NUMBUF];
1755 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1756 return ERR_PTR(vfs_follow_link(nd,tmp));
1759 static struct inode_operations proc_self_inode_operations = {
1760 .readlink = proc_self_readlink,
1761 .follow_link = proc_self_follow_link,
1767 * These are the directory entries in the root directory of /proc
1768 * that properly belong to the /proc filesystem, as they describe
1769 * describe something that is process related.
1771 static struct pid_entry proc_base_stuff[] = {
1772 NOD("self", S_IFLNK|S_IRWXUGO,
1773 &proc_self_inode_operations, NULL, {}),
1777 * Exceptional case: normally we are not allowed to unhash a busy
1778 * directory. In this case, however, we can do it - no aliasing problems
1779 * due to the way we treat inodes.
1781 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1783 struct inode *inode = dentry->d_inode;
1784 struct task_struct *task = get_proc_task(inode);
1786 put_task_struct(task);
1793 static struct dentry_operations proc_base_dentry_operations =
1795 .d_revalidate = proc_base_revalidate,
1796 .d_delete = pid_delete_dentry,
1799 static struct dentry *proc_base_instantiate(struct inode *dir,
1800 struct dentry *dentry, struct task_struct *task, void *ptr)
1802 struct pid_entry *p = ptr;
1803 struct inode *inode;
1804 struct proc_inode *ei;
1805 struct dentry *error = ERR_PTR(-EINVAL);
1807 /* Allocate the inode */
1808 error = ERR_PTR(-ENOMEM);
1809 inode = new_inode(dir->i_sb);
1813 /* Initialize the inode */
1815 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1818 * grab the reference to the task.
1820 ei->pid = get_task_pid(task, PIDTYPE_PID);
1826 inode->i_mode = p->mode;
1827 if (S_ISDIR(inode->i_mode))
1829 if (S_ISLNK(inode->i_mode))
1832 inode->i_op = p->iop;
1834 inode->i_fop = p->fop;
1836 dentry->d_op = &proc_base_dentry_operations;
1837 d_add(dentry, inode);
1846 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1848 struct dentry *error;
1849 struct task_struct *task = get_proc_task_real(dir);
1850 struct pid_entry *p, *last;
1852 error = ERR_PTR(-ENOENT);
1857 /* Lookup the directory entry */
1858 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1859 for (p = proc_base_stuff; p <= last; p++) {
1860 if (p->len != dentry->d_name.len)
1862 if (!memcmp(dentry->d_name.name, p->name, p->len))
1868 error = proc_base_instantiate(dir, dentry, task, p);
1871 put_task_struct(task);
1876 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1877 struct task_struct *task, struct pid_entry *p)
1879 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1880 proc_base_instantiate, task, p);
1883 #ifdef CONFIG_TASK_IO_ACCOUNTING
1884 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1886 return sprintf(buffer,
1891 "read_bytes: %llu\n"
1892 "write_bytes: %llu\n"
1893 "cancelled_write_bytes: %llu\n",
1894 (unsigned long long)task->rchar,
1895 (unsigned long long)task->wchar,
1896 (unsigned long long)task->syscr,
1897 (unsigned long long)task->syscw,
1898 (unsigned long long)task->ioac.read_bytes,
1899 (unsigned long long)task->ioac.write_bytes,
1900 (unsigned long long)task->ioac.cancelled_write_bytes);
1907 static struct file_operations proc_task_operations;
1908 static struct inode_operations proc_task_inode_operations;
1910 extern int proc_pid_vx_info(struct task_struct *, char *);
1911 extern int proc_pid_nx_info(struct task_struct *, char *);
1913 static struct pid_entry tgid_base_stuff[] = {
1914 DIR("task", S_IRUGO|S_IXUGO, task),
1915 DIR("fd", S_IRUSR|S_IXUSR, fd),
1916 INF("environ", S_IRUSR, pid_environ),
1917 INF("auxv", S_IRUSR, pid_auxv),
1918 INF("status", S_IRUGO, pid_status),
1919 INF("cmdline", S_IRUGO, pid_cmdline),
1920 INF("stat", S_IRUGO, tgid_stat),
1921 INF("statm", S_IRUGO, pid_statm),
1922 REG("maps", S_IRUSR, maps),
1924 REG("numa_maps", S_IRUGO, numa_maps),
1926 REG("mem", S_IRUSR|S_IWUSR, mem),
1927 #ifdef CONFIG_SECCOMP
1928 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1933 REG("mounts", S_IRUGO, mounts),
1934 REG("mountstats", S_IRUSR, mountstats),
1936 REG("smaps", S_IRUSR, smaps),
1938 #ifdef CONFIG_SECURITY
1939 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1941 #ifdef CONFIG_KALLSYMS
1942 INF("wchan", S_IRUGO, pid_wchan),
1944 #ifdef CONFIG_SCHEDSTATS
1945 INF("schedstat", S_IRUGO, pid_schedstat),
1947 #ifdef CONFIG_CPUSETS
1948 REG("cpuset", S_IRUGO, cpuset),
1950 INF("vinfo", S_IRUGO, pid_vx_info),
1951 INF("ninfo", S_IRUGO, pid_nx_info),
1952 INF("oom_score", S_IRUGO, oom_score),
1953 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1954 #ifdef CONFIG_AUDITSYSCALL
1955 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
1957 #ifdef CONFIG_FAULT_INJECTION
1958 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1960 #ifdef CONFIG_TASK_IO_ACCOUNTING
1961 INF("io", S_IRUGO, pid_io_accounting),
1965 static int proc_tgid_base_readdir(struct file * filp,
1966 void * dirent, filldir_t filldir)
1968 return proc_pident_readdir(filp,dirent,filldir,
1969 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1972 static struct file_operations proc_tgid_base_operations = {
1973 .read = generic_read_dir,
1974 .readdir = proc_tgid_base_readdir,
1977 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1978 return proc_pident_lookup(dir, dentry,
1979 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1982 static struct inode_operations proc_tgid_base_inode_operations = {
1983 .lookup = proc_tgid_base_lookup,
1984 .getattr = pid_getattr,
1985 .setattr = proc_setattr,
1989 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1991 * @task: task that should be flushed.
1993 * Looks in the dcache for
1995 * /proc/@tgid/task/@pid
1996 * if either directory is present flushes it and all of it'ts children
1999 * It is safe and reasonable to cache /proc entries for a task until
2000 * that task exits. After that they just clog up the dcache with
2001 * useless entries, possibly causing useful dcache entries to be
2002 * flushed instead. This routine is proved to flush those useless
2003 * dcache entries at process exit time.
2005 * NOTE: This routine is just an optimization so it does not guarantee
2006 * that no dcache entries will exist at process exit time it
2007 * just makes it very unlikely that any will persist.
2009 void proc_flush_task(struct task_struct *task)
2011 struct dentry *dentry, *leader, *dir;
2012 char buf[PROC_NUMBUF];
2016 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2017 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2019 shrink_dcache_parent(dentry);
2024 if (thread_group_leader(task))
2028 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2029 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2034 name.len = strlen(name.name);
2035 dir = d_hash_and_lookup(leader, &name);
2037 goto out_put_leader;
2040 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2041 dentry = d_hash_and_lookup(dir, &name);
2043 shrink_dcache_parent(dentry);
2055 static struct dentry *proc_pid_instantiate(struct inode *dir,
2056 struct dentry * dentry,
2057 struct task_struct *task, void *ptr)
2059 struct dentry *error = ERR_PTR(-ENOENT);
2060 struct inode *inode;
2062 inode = proc_pid_make_inode(dir->i_sb, task);
2066 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2067 inode->i_op = &proc_tgid_base_inode_operations;
2068 inode->i_fop = &proc_tgid_base_operations;
2069 inode->i_flags|=S_IMMUTABLE;
2071 #ifdef CONFIG_SECURITY
2072 inode->i_nlink += 1;
2075 dentry->d_op = &pid_dentry_operations;
2077 d_add(dentry, inode);
2078 /* Close the race of the process dying before we return the dentry */
2079 if (pid_revalidate(dentry, NULL))
2085 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2087 struct dentry *result = ERR_PTR(-ENOENT);
2088 struct task_struct *task;
2091 result = proc_base_lookup(dir, dentry);
2092 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2095 tgid = name_to_int(dentry);
2100 task = vx_find_proc_task_by_pid(tgid);
2102 get_task_struct(task);
2107 result = proc_pid_instantiate(dir, dentry, task, NULL);
2108 put_task_struct(task);
2114 * Find the first task with tgid >= tgid
2117 static struct task_struct *next_tgid(unsigned int tgid)
2119 struct task_struct *task;
2125 pid = find_ge_pid(tgid);
2128 task = pid_task(pid, PIDTYPE_PID);
2129 /* What we to know is if the pid we have find is the
2130 * pid of a thread_group_leader. Testing for task
2131 * being a thread_group_leader is the obvious thing
2132 * todo but there is a window when it fails, due to
2133 * the pid transfer logic in de_thread.
2135 * So we perform the straight forward test of seeing
2136 * if the pid we have found is the pid of a thread
2137 * group leader, and don't worry if the task we have
2138 * found doesn't happen to be a thread group leader.
2139 * As we don't care in the case of readdir.
2141 if (!task || !has_group_leader_pid(task))
2143 get_task_struct(task);
2149 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2151 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2152 struct task_struct *task, int tgid)
2154 char name[PROC_NUMBUF];
2155 int len = snprintf(name, sizeof(name), "%d", tgid);
2156 return proc_fill_cache(filp, dirent, filldir, name, len,
2157 proc_pid_instantiate, task, NULL);
2160 /* for the /proc/ directory itself, after non-process stuff has been done */
2161 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2163 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2164 struct task_struct *reaper = get_proc_task_real(filp->f_path.dentry->d_inode);
2165 struct task_struct *task;
2171 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2172 struct pid_entry *p = &proc_base_stuff[nr];
2173 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2177 tgid = filp->f_pos - TGID_OFFSET;
2178 for (task = next_tgid(tgid);
2180 put_task_struct(task), task = next_tgid(tgid + 1)) {
2182 filp->f_pos = tgid + TGID_OFFSET;
2183 if (!vx_proc_task_visible(task))
2185 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2186 put_task_struct(task);
2190 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2192 put_task_struct(reaper);
2200 static struct pid_entry tid_base_stuff[] = {
2201 DIR("fd", S_IRUSR|S_IXUSR, fd),
2202 INF("environ", S_IRUSR, pid_environ),
2203 INF("auxv", S_IRUSR, pid_auxv),
2204 INF("status", S_IRUGO, pid_status),
2205 INF("cmdline", S_IRUGO, pid_cmdline),
2206 INF("stat", S_IRUGO, tid_stat),
2207 INF("statm", S_IRUGO, pid_statm),
2208 REG("maps", S_IRUSR, maps),
2210 REG("numa_maps", S_IRUGO, numa_maps),
2212 REG("mem", S_IRUSR|S_IWUSR, mem),
2213 #ifdef CONFIG_SECCOMP
2214 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2219 REG("mounts", S_IRUGO, mounts),
2221 REG("smaps", S_IRUSR, smaps),
2223 #ifdef CONFIG_SECURITY
2224 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2226 #ifdef CONFIG_KALLSYMS
2227 INF("wchan", S_IRUGO, pid_wchan),
2229 #ifdef CONFIG_SCHEDSTATS
2230 INF("schedstat", S_IRUGO, pid_schedstat),
2232 #ifdef CONFIG_CPUSETS
2233 REG("cpuset", S_IRUGO, cpuset),
2235 INF("oom_score", S_IRUGO, oom_score),
2236 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2237 #ifdef CONFIG_AUDITSYSCALL
2238 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2240 #ifdef CONFIG_FAULT_INJECTION
2241 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2245 static int proc_tid_base_readdir(struct file * filp,
2246 void * dirent, filldir_t filldir)
2248 return proc_pident_readdir(filp,dirent,filldir,
2249 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2252 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2253 return proc_pident_lookup(dir, dentry,
2254 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2257 static struct file_operations proc_tid_base_operations = {
2258 .read = generic_read_dir,
2259 .readdir = proc_tid_base_readdir,
2262 static struct inode_operations proc_tid_base_inode_operations = {
2263 .lookup = proc_tid_base_lookup,
2264 .getattr = pid_getattr,
2265 .setattr = proc_setattr,
2268 static struct dentry *proc_task_instantiate(struct inode *dir,
2269 struct dentry *dentry, struct task_struct *task, void *ptr)
2271 struct dentry *error = ERR_PTR(-ENOENT);
2272 struct inode *inode;
2273 inode = proc_pid_make_inode(dir->i_sb, task);
2277 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2278 inode->i_op = &proc_tid_base_inode_operations;
2279 inode->i_fop = &proc_tid_base_operations;
2280 inode->i_flags|=S_IMMUTABLE;
2282 #ifdef CONFIG_SECURITY
2283 inode->i_nlink += 1;
2286 dentry->d_op = &pid_dentry_operations;
2288 d_add(dentry, inode);
2289 /* Close the race of the process dying before we return the dentry */
2290 if (pid_revalidate(dentry, NULL))
2296 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2298 struct dentry *result = ERR_PTR(-ENOENT);
2299 struct task_struct *task;
2300 struct task_struct *leader = get_proc_task(dir);
2306 tid = name_to_int(dentry);
2309 if (vx_current_initpid(tid))
2313 task = vx_find_proc_task_by_pid(tid);
2315 get_task_struct(task);
2319 if (leader->tgid != task->tgid)
2322 result = proc_task_instantiate(dir, dentry, task, NULL);
2324 put_task_struct(task);
2326 put_task_struct(leader);
2332 * Find the first tid of a thread group to return to user space.
2334 * Usually this is just the thread group leader, but if the users
2335 * buffer was too small or there was a seek into the middle of the
2336 * directory we have more work todo.
2338 * In the case of a short read we start with find_task_by_pid.
2340 * In the case of a seek we start with the leader and walk nr
2343 static struct task_struct *first_tid(struct task_struct *leader,
2346 struct task_struct *pos;
2349 /* Attempt to start with the pid of a thread */
2350 if (tid && (nr > 0)) {
2351 pos = find_task_by_pid(tid);
2352 if (pos && (pos->group_leader == leader))
2356 /* If nr exceeds the number of threads there is nothing todo */
2358 if (nr && nr >= get_nr_threads(leader))
2361 /* If we haven't found our starting place yet start
2362 * with the leader and walk nr threads forward.
2364 for (pos = leader; nr > 0; --nr) {
2365 pos = next_thread(pos);
2366 if (pos == leader) {
2372 get_task_struct(pos);
2379 * Find the next thread in the thread list.
2380 * Return NULL if there is an error or no next thread.
2382 * The reference to the input task_struct is released.
2384 static struct task_struct *next_tid(struct task_struct *start)
2386 struct task_struct *pos = NULL;
2388 if (pid_alive(start)) {
2389 pos = next_thread(start);
2390 if (thread_group_leader(pos))
2393 get_task_struct(pos);
2396 put_task_struct(start);
2400 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2401 struct task_struct *task, int tid)
2403 char name[PROC_NUMBUF];
2404 int len = snprintf(name, sizeof(name), "%d", tid);
2405 return proc_fill_cache(filp, dirent, filldir, name, len,
2406 proc_task_instantiate, task, NULL);
2409 /* for the /proc/TGID/task/ directories */
2410 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2412 struct dentry *dentry = filp->f_path.dentry;
2413 struct inode *inode = dentry->d_inode;
2414 struct task_struct *leader = NULL;
2415 struct task_struct *task;
2416 int retval = -ENOENT;
2419 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2421 task = get_proc_task(inode);
2425 if (pid_alive(task)) {
2426 leader = task->group_leader;
2427 get_task_struct(leader);
2430 put_task_struct(task);
2438 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2443 ino = parent_ino(dentry);
2444 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2450 /* f_version caches the tgid value that the last readdir call couldn't
2451 * return. lseek aka telldir automagically resets f_version to 0.
2453 tid = filp->f_version;
2454 filp->f_version = 0;
2455 for (task = first_tid(leader, tid, pos - 2);
2457 task = next_tid(task), pos++) {
2458 tid = vx_map_pid(task->pid);
2459 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2460 /* returning this tgid failed, save it as the first
2461 * pid for the next readir call */
2462 filp->f_version = tid;
2463 put_task_struct(task);
2469 put_task_struct(leader);
2474 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2476 struct inode *inode = dentry->d_inode;
2477 struct task_struct *p = get_proc_task(inode);
2478 generic_fillattr(inode, stat);
2482 stat->nlink += get_nr_threads(p);
2490 static struct inode_operations proc_task_inode_operations = {
2491 .lookup = proc_task_lookup,
2492 .getattr = proc_task_getattr,
2493 .setattr = proc_setattr,
2496 static struct file_operations proc_task_operations = {
2497 .read = generic_read_dir,
2498 .readdir = proc_task_readdir,