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/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.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>
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
90 /* Worst case buffer size needed for holding an integer. */
91 #define PROC_NUMBUF 13
97 const struct inode_operations *iop;
98 const struct file_operations *fop;
102 #define NOD(NAME, MODE, IOP, FOP, OP) { \
103 .len = sizeof(NAME) - 1, \
111 #define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 #define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
119 #define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
121 &proc_##OTYPE##_operations, {})
122 #define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
128 EXPORT_SYMBOL(maps_protect);
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 proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
156 struct task_struct *task = get_proc_task(inode);
157 struct fs_struct *fs = NULL;
158 int result = -ENOENT;
161 fs = get_fs_struct(task);
162 put_task_struct(task);
165 read_lock(&fs->lock);
166 *mnt = mntget(fs->pwdmnt);
167 *dentry = dget(fs->pwd);
168 read_unlock(&fs->lock);
175 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
177 struct task_struct *task = get_proc_task(inode);
178 struct fs_struct *fs = NULL;
179 int result = -ENOENT;
182 fs = get_fs_struct(task);
183 put_task_struct(task);
186 read_lock(&fs->lock);
187 *mnt = mntget(fs->rootmnt);
188 *dentry = dget(fs->root);
189 read_unlock(&fs->lock);
196 #define MAY_PTRACE(task) \
197 (task == current || \
198 (task->parent == current && \
199 (task->ptrace & PT_PTRACED) && \
200 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 security_ptrace(current,task) == 0))
203 static int proc_pid_environ(struct task_struct *task, char * buffer)
206 struct mm_struct *mm = get_task_mm(task);
208 unsigned int len = mm->env_end - mm->env_start;
211 res = access_process_vm(task, mm->env_start, buffer, len, 0);
212 if (!ptrace_may_attach(task))
219 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
223 struct mm_struct *mm = get_task_mm(task);
227 goto out_mm; /* Shh! No looking before we're done */
229 len = mm->arg_end - mm->arg_start;
234 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
236 // If the nul at the end of args has been overwritten, then
237 // assume application is using setproctitle(3).
238 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
239 len = strnlen(buffer, res);
243 len = mm->env_end - mm->env_start;
244 if (len > PAGE_SIZE - res)
245 len = PAGE_SIZE - res;
246 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
247 res = strnlen(buffer, res);
256 static int proc_pid_auxv(struct task_struct *task, char *buffer)
259 struct mm_struct *mm = get_task_mm(task);
261 unsigned int nwords = 0;
264 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
265 res = nwords * sizeof(mm->saved_auxv[0]);
268 memcpy(buffer, mm->saved_auxv, res);
275 #ifdef CONFIG_KALLSYMS
277 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
278 * Returns the resolved symbol. If that fails, simply return the address.
280 static int proc_pid_wchan(struct task_struct *task, char *buffer)
283 const char *sym_name;
284 unsigned long wchan, size, offset;
285 char namebuf[KSYM_NAME_LEN+1];
287 wchan = get_wchan(task);
289 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
291 return sprintf(buffer, "%s", sym_name);
292 return sprintf(buffer, "%lu", wchan);
294 #endif /* CONFIG_KALLSYMS */
296 #ifdef CONFIG_SCHEDSTATS
298 * Provides /proc/PID/schedstat
300 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
302 return sprintf(buffer, "%lu %lu %lu\n",
303 task->sched_info.cpu_time,
304 task->sched_info.run_delay,
305 task->sched_info.pcnt);
309 /* The badness from the OOM killer */
310 unsigned long badness(struct task_struct *p, unsigned long uptime);
311 static int proc_oom_score(struct task_struct *task, char *buffer)
313 unsigned long points;
314 struct timespec uptime;
316 do_posix_clock_monotonic_gettime(&uptime);
317 points = badness(task, uptime.tv_sec);
318 return sprintf(buffer, "%lu\n", points);
321 /************************************************************************/
322 /* Here the fs part begins */
323 /************************************************************************/
325 /* permission checks */
326 static int proc_fd_access_allowed(struct inode *inode)
328 struct task_struct *task;
330 /* Allow access to a task's file descriptors if it is us or we
331 * may use ptrace attach to the process and find out that
334 task = get_proc_task(inode);
336 allowed = ptrace_may_attach(task);
337 put_task_struct(task);
342 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
345 struct inode *inode = dentry->d_inode;
347 if (attr->ia_valid & ATTR_MODE)
350 error = inode_change_ok(inode, attr);
352 error = security_inode_setattr(dentry, attr);
354 error = inode_setattr(inode, attr);
359 static const struct inode_operations proc_def_inode_operations = {
360 .setattr = proc_setattr,
363 extern struct seq_operations mounts_op;
369 static int mounts_open(struct inode *inode, struct file *file)
371 struct task_struct *task = get_proc_task(inode);
372 struct mnt_namespace *ns = NULL;
373 struct proc_mounts *p;
379 ns = task->nsproxy->mnt_ns;
384 put_task_struct(task);
389 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
391 file->private_data = &p->m;
392 ret = seq_open(file, &mounts_op);
395 p->event = ns->event;
405 static int mounts_release(struct inode *inode, struct file *file)
407 struct seq_file *m = file->private_data;
408 struct mnt_namespace *ns = m->private;
410 return seq_release(inode, file);
413 static unsigned mounts_poll(struct file *file, poll_table *wait)
415 struct proc_mounts *p = file->private_data;
416 struct mnt_namespace *ns = p->m.private;
419 poll_wait(file, &ns->poll, wait);
421 spin_lock(&vfsmount_lock);
422 if (p->event != ns->event) {
423 p->event = ns->event;
426 spin_unlock(&vfsmount_lock);
431 static const struct file_operations proc_mounts_operations = {
435 .release = mounts_release,
439 extern struct seq_operations mountstats_op;
440 static int mountstats_open(struct inode *inode, struct file *file)
442 int ret = seq_open(file, &mountstats_op);
445 struct seq_file *m = file->private_data;
446 struct mnt_namespace *mnt_ns = NULL;
447 struct task_struct *task = get_proc_task(inode);
452 mnt_ns = task->nsproxy->mnt_ns;
456 put_task_struct(task);
462 seq_release(inode, file);
469 static const struct file_operations proc_mountstats_operations = {
470 .open = mountstats_open,
473 .release = mounts_release,
476 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
478 static ssize_t proc_info_read(struct file * file, char __user * buf,
479 size_t count, loff_t *ppos)
481 struct inode * inode = file->f_path.dentry->d_inode;
484 struct task_struct *task = get_proc_task(inode);
490 if (count > PROC_BLOCK_SIZE)
491 count = PROC_BLOCK_SIZE;
494 if (!(page = __get_free_page(GFP_KERNEL)))
497 length = PROC_I(inode)->op.proc_read(task, (char*)page);
500 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
503 put_task_struct(task);
508 static const struct file_operations proc_info_file_operations = {
509 .read = proc_info_read,
512 static int mem_open(struct inode* inode, struct file* file)
514 file->private_data = (void*)((long)current->self_exec_id);
518 static ssize_t mem_read(struct file * file, char __user * buf,
519 size_t count, loff_t *ppos)
521 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
523 unsigned long src = *ppos;
525 struct mm_struct *mm;
530 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
534 page = (char *)__get_free_page(GFP_USER);
540 mm = get_task_mm(task);
546 if (file->private_data != (void*)((long)current->self_exec_id))
552 int this_len, retval;
554 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
555 retval = access_process_vm(task, src, page, this_len, 0);
556 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
562 if (copy_to_user(buf, page, retval)) {
577 free_page((unsigned long) page);
579 put_task_struct(task);
584 #define mem_write NULL
587 /* This is a security hazard */
588 static ssize_t mem_write(struct file * file, const char __user *buf,
589 size_t count, loff_t *ppos)
593 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
594 unsigned long dst = *ppos;
600 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
604 page = (char *)__get_free_page(GFP_USER);
610 int this_len, retval;
612 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
613 if (copy_from_user(page, buf, this_len)) {
617 retval = access_process_vm(task, dst, page, this_len, 1);
629 free_page((unsigned long) page);
631 put_task_struct(task);
637 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
641 file->f_pos = offset;
644 file->f_pos += offset;
649 force_successful_syscall_return();
653 static const struct file_operations proc_mem_operations = {
660 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
661 size_t count, loff_t *ppos)
663 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
664 char buffer[PROC_NUMBUF];
667 loff_t __ppos = *ppos;
671 oom_adjust = task->oomkilladj;
672 put_task_struct(task);
674 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
677 if (count > len-__ppos)
679 if (copy_to_user(buf, buffer + __ppos, count))
681 *ppos = __ppos + count;
685 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
686 size_t count, loff_t *ppos)
688 struct task_struct *task;
689 char buffer[PROC_NUMBUF], *end;
692 memset(buffer, 0, sizeof(buffer));
693 if (count > sizeof(buffer) - 1)
694 count = sizeof(buffer) - 1;
695 if (copy_from_user(buffer, buf, count))
697 oom_adjust = simple_strtol(buffer, &end, 0);
698 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
699 oom_adjust != OOM_DISABLE)
703 task = get_proc_task(file->f_path.dentry->d_inode);
706 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
707 put_task_struct(task);
710 task->oomkilladj = oom_adjust;
711 put_task_struct(task);
712 if (end - buffer == 0)
717 static const struct file_operations proc_oom_adjust_operations = {
718 .read = oom_adjust_read,
719 .write = oom_adjust_write,
722 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
723 size_t count, loff_t *ppos)
725 struct task_struct *task;
726 char buffer[PROC_NUMBUF], *end;
727 struct mm_struct *mm;
729 memset(buffer, 0, sizeof(buffer));
730 if (count > sizeof(buffer) - 1)
731 count = sizeof(buffer) - 1;
732 if (copy_from_user(buffer, buf, count))
734 if (!simple_strtol(buffer, &end, 0))
738 task = get_proc_task(file->f_path.dentry->d_inode);
741 mm = get_task_mm(task);
746 put_task_struct(task);
747 if (end - buffer == 0)
752 static struct file_operations proc_clear_refs_operations = {
753 .write = clear_refs_write,
756 #ifdef CONFIG_AUDITSYSCALL
758 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
759 size_t count, loff_t *ppos)
761 struct inode * inode = file->f_path.dentry->d_inode;
762 struct task_struct *task = get_proc_task(inode);
764 char tmpbuf[TMPBUFLEN];
768 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
769 audit_get_loginuid(task->audit_context));
770 put_task_struct(task);
771 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
774 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
775 size_t count, loff_t *ppos)
777 struct inode * inode = file->f_path.dentry->d_inode;
782 if (!capable(CAP_AUDIT_CONTROL))
785 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
788 if (count >= PAGE_SIZE)
789 count = PAGE_SIZE - 1;
792 /* No partial writes. */
795 page = (char*)__get_free_page(GFP_USER);
799 if (copy_from_user(page, buf, count))
803 loginuid = simple_strtoul(page, &tmp, 10);
809 length = audit_set_loginuid(current, loginuid);
810 if (likely(length == 0))
814 free_page((unsigned long) page);
818 static const struct file_operations proc_loginuid_operations = {
819 .read = proc_loginuid_read,
820 .write = proc_loginuid_write,
824 #ifdef CONFIG_SECCOMP
825 static ssize_t seccomp_read(struct file *file, char __user *buf,
826 size_t count, loff_t *ppos)
828 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
830 loff_t __ppos = *ppos;
835 /* no need to print the trailing zero, so use only len */
836 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
837 put_task_struct(tsk);
840 if (count > len - __ppos)
841 count = len - __ppos;
842 if (copy_to_user(buf, __buf + __ppos, count))
844 *ppos = __ppos + count;
848 static ssize_t seccomp_write(struct file *file, const char __user *buf,
849 size_t count, loff_t *ppos)
851 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
852 char __buf[20], *end;
853 unsigned int seccomp_mode;
860 /* can set it only once to be even more secure */
862 if (unlikely(tsk->seccomp.mode))
866 memset(__buf, 0, sizeof(__buf));
867 count = min(count, sizeof(__buf) - 1);
868 if (copy_from_user(__buf, buf, count))
871 seccomp_mode = simple_strtoul(__buf, &end, 0);
875 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
876 tsk->seccomp.mode = seccomp_mode;
877 set_tsk_thread_flag(tsk, TIF_SECCOMP);
881 if (unlikely(!(end - __buf)))
883 result = end - __buf;
885 put_task_struct(tsk);
890 static const struct file_operations proc_seccomp_operations = {
891 .read = seccomp_read,
892 .write = seccomp_write,
894 #endif /* CONFIG_SECCOMP */
896 #ifdef CONFIG_FAULT_INJECTION
897 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
898 size_t count, loff_t *ppos)
900 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
901 char buffer[PROC_NUMBUF];
904 loff_t __ppos = *ppos;
908 make_it_fail = task->make_it_fail;
909 put_task_struct(task);
911 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
914 if (count > len-__ppos)
916 if (copy_to_user(buf, buffer + __ppos, count))
918 *ppos = __ppos + count;
922 static ssize_t proc_fault_inject_write(struct file * file,
923 const char __user * buf, size_t count, loff_t *ppos)
925 struct task_struct *task;
926 char buffer[PROC_NUMBUF], *end;
929 if (!capable(CAP_SYS_RESOURCE))
931 memset(buffer, 0, sizeof(buffer));
932 if (count > sizeof(buffer) - 1)
933 count = sizeof(buffer) - 1;
934 if (copy_from_user(buffer, buf, count))
936 make_it_fail = simple_strtol(buffer, &end, 0);
939 task = get_proc_task(file->f_dentry->d_inode);
942 task->make_it_fail = make_it_fail;
943 put_task_struct(task);
944 if (end - buffer == 0)
949 static const struct file_operations proc_fault_inject_operations = {
950 .read = proc_fault_inject_read,
951 .write = proc_fault_inject_write,
955 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
957 struct inode *inode = dentry->d_inode;
960 /* We don't need a base pointer in the /proc filesystem */
963 /* Are we allowed to snoop on the tasks file descriptors? */
964 if (!proc_fd_access_allowed(inode))
967 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
968 nd->last_type = LAST_BIND;
970 return ERR_PTR(error);
973 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
974 char __user *buffer, int buflen)
976 struct inode * inode;
977 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
983 inode = dentry->d_inode;
984 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
988 len = tmp + PAGE_SIZE - 1 - path;
992 if (copy_to_user(buffer, path, len))
995 free_page((unsigned long)tmp);
999 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1001 int error = -EACCES;
1002 struct inode *inode = dentry->d_inode;
1004 struct vfsmount *mnt = NULL;
1006 /* Are we allowed to snoop on the tasks file descriptors? */
1007 if (!proc_fd_access_allowed(inode))
1010 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1014 error = do_proc_readlink(de, mnt, buffer, buflen);
1021 static const struct inode_operations proc_pid_link_inode_operations = {
1022 .readlink = proc_pid_readlink,
1023 .follow_link = proc_pid_follow_link,
1024 .setattr = proc_setattr,
1028 /* building an inode */
1030 static int task_dumpable(struct task_struct *task)
1033 struct mm_struct *mm;
1038 dumpable = mm->dumpable;
1046 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1048 struct inode * inode;
1049 struct proc_inode *ei;
1051 /* We need a new inode */
1053 inode = new_inode(sb);
1059 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1060 inode->i_op = &proc_def_inode_operations;
1063 * grab the reference to task.
1065 ei->pid = get_task_pid(task, PIDTYPE_PID);
1071 if (task_dumpable(task)) {
1072 inode->i_uid = task->euid;
1073 inode->i_gid = task->egid;
1075 security_task_to_inode(task, inode);
1085 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1087 struct inode *inode = dentry->d_inode;
1088 struct task_struct *task;
1089 generic_fillattr(inode, stat);
1094 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1096 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1097 task_dumpable(task)) {
1098 stat->uid = task->euid;
1099 stat->gid = task->egid;
1109 * Exceptional case: normally we are not allowed to unhash a busy
1110 * directory. In this case, however, we can do it - no aliasing problems
1111 * due to the way we treat inodes.
1113 * Rewrite the inode's ownerships here because the owning task may have
1114 * performed a setuid(), etc.
1116 * Before the /proc/pid/status file was created the only way to read
1117 * the effective uid of a /process was to stat /proc/pid. Reading
1118 * /proc/pid/status is slow enough that procps and other packages
1119 * kept stating /proc/pid. To keep the rules in /proc simple I have
1120 * made this apply to all per process world readable and executable
1123 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1125 struct inode *inode = dentry->d_inode;
1126 struct task_struct *task = get_proc_task(inode);
1128 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1129 task_dumpable(task)) {
1130 inode->i_uid = task->euid;
1131 inode->i_gid = task->egid;
1136 inode->i_mode &= ~(S_ISUID | S_ISGID);
1137 security_task_to_inode(task, inode);
1138 put_task_struct(task);
1145 static int pid_delete_dentry(struct dentry * dentry)
1147 /* Is the task we represent dead?
1148 * If so, then don't put the dentry on the lru list,
1149 * kill it immediately.
1151 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1154 static struct dentry_operations pid_dentry_operations =
1156 .d_revalidate = pid_revalidate,
1157 .d_delete = pid_delete_dentry,
1162 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1165 * Fill a directory entry.
1167 * If possible create the dcache entry and derive our inode number and
1168 * file type from dcache entry.
1170 * Since all of the proc inode numbers are dynamically generated, the inode
1171 * numbers do not exist until the inode is cache. This means creating the
1172 * the dcache entry in readdir is necessary to keep the inode numbers
1173 * reported by readdir in sync with the inode numbers reported
1176 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1177 char *name, int len,
1178 instantiate_t instantiate, struct task_struct *task, void *ptr)
1180 struct dentry *child, *dir = filp->f_path.dentry;
1181 struct inode *inode;
1184 unsigned type = DT_UNKNOWN;
1188 qname.hash = full_name_hash(name, len);
1190 child = d_lookup(dir, &qname);
1193 new = d_alloc(dir, &qname);
1195 child = instantiate(dir->d_inode, new, task, ptr);
1202 if (!child || IS_ERR(child) || !child->d_inode)
1203 goto end_instantiate;
1204 inode = child->d_inode;
1207 type = inode->i_mode >> 12;
1212 ino = find_inode_number(dir, &qname);
1215 return filldir(dirent, name, len, filp->f_pos, ino, type);
1218 static unsigned name_to_int(struct dentry *dentry)
1220 const char *name = dentry->d_name.name;
1221 int len = dentry->d_name.len;
1224 if (len > 1 && *name == '0')
1227 unsigned c = *name++ - '0';
1230 if (n >= (~0U-9)/10)
1240 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1242 struct task_struct *task = get_proc_task(inode);
1243 struct files_struct *files = NULL;
1245 int fd = proc_fd(inode);
1248 files = get_files_struct(task);
1249 put_task_struct(task);
1253 * We are not taking a ref to the file structure, so we must
1256 spin_lock(&files->file_lock);
1257 file = fcheck_files(files, fd);
1259 *mnt = mntget(file->f_path.mnt);
1260 *dentry = dget(file->f_path.dentry);
1261 spin_unlock(&files->file_lock);
1262 put_files_struct(files);
1265 spin_unlock(&files->file_lock);
1266 put_files_struct(files);
1271 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1273 struct inode *inode = dentry->d_inode;
1274 struct task_struct *task = get_proc_task(inode);
1275 int fd = proc_fd(inode);
1276 struct files_struct *files;
1279 files = get_files_struct(task);
1282 if (fcheck_files(files, fd)) {
1284 put_files_struct(files);
1285 if (task_dumpable(task)) {
1286 inode->i_uid = task->euid;
1287 inode->i_gid = task->egid;
1292 inode->i_mode &= ~(S_ISUID | S_ISGID);
1293 security_task_to_inode(task, inode);
1294 put_task_struct(task);
1298 put_files_struct(files);
1300 put_task_struct(task);
1306 static struct dentry_operations tid_fd_dentry_operations =
1308 .d_revalidate = tid_fd_revalidate,
1309 .d_delete = pid_delete_dentry,
1312 static struct dentry *proc_fd_instantiate(struct inode *dir,
1313 struct dentry *dentry, struct task_struct *task, void *ptr)
1315 unsigned fd = *(unsigned *)ptr;
1317 struct files_struct *files;
1318 struct inode *inode;
1319 struct proc_inode *ei;
1320 struct dentry *error = ERR_PTR(-ENOENT);
1322 inode = proc_pid_make_inode(dir->i_sb, task);
1327 files = get_files_struct(task);
1330 inode->i_mode = S_IFLNK;
1333 * We are not taking a ref to the file structure, so we must
1336 spin_lock(&files->file_lock);
1337 file = fcheck_files(files, fd);
1340 if (file->f_mode & 1)
1341 inode->i_mode |= S_IRUSR | S_IXUSR;
1342 if (file->f_mode & 2)
1343 inode->i_mode |= S_IWUSR | S_IXUSR;
1344 spin_unlock(&files->file_lock);
1345 put_files_struct(files);
1347 inode->i_op = &proc_pid_link_inode_operations;
1349 ei->op.proc_get_link = proc_fd_link;
1350 dentry->d_op = &tid_fd_dentry_operations;
1351 d_add(dentry, inode);
1352 /* Close the race of the process dying before we return the dentry */
1353 if (tid_fd_revalidate(dentry, NULL))
1359 spin_unlock(&files->file_lock);
1360 put_files_struct(files);
1366 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1368 struct task_struct *task = get_proc_task(dir);
1369 unsigned fd = name_to_int(dentry);
1370 struct dentry *result = ERR_PTR(-ENOENT);
1377 result = proc_fd_instantiate(dir, dentry, task, &fd);
1379 put_task_struct(task);
1384 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1385 struct task_struct *task, int fd)
1387 char name[PROC_NUMBUF];
1388 int len = snprintf(name, sizeof(name), "%d", fd);
1389 return proc_fill_cache(filp, dirent, filldir, name, len,
1390 proc_fd_instantiate, task, &fd);
1393 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1395 struct dentry *dentry = filp->f_path.dentry;
1396 struct inode *inode = dentry->d_inode;
1397 struct task_struct *p = get_proc_task(inode);
1398 unsigned int fd, tid, ino;
1400 struct files_struct * files;
1401 struct fdtable *fdt;
1412 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1416 ino = parent_ino(dentry);
1417 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1421 files = get_files_struct(p);
1425 fdt = files_fdtable(files);
1426 for (fd = filp->f_pos-2;
1428 fd++, filp->f_pos++) {
1430 if (!fcheck_files(files, fd))
1434 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1441 put_files_struct(files);
1449 static const struct file_operations proc_fd_operations = {
1450 .read = generic_read_dir,
1451 .readdir = proc_readfd,
1455 * /proc/pid/fd needs a special permission handler so that a process can still
1456 * access /proc/self/fd after it has executed a setuid().
1458 static int proc_fd_permission(struct inode *inode, int mask,
1459 struct nameidata *nd)
1463 rv = generic_permission(inode, mask, NULL);
1466 if (task_pid(current) == proc_pid(inode))
1472 * proc directories can do almost nothing..
1474 static const struct inode_operations proc_fd_inode_operations = {
1475 .lookup = proc_lookupfd,
1476 .permission = proc_fd_permission,
1477 .setattr = proc_setattr,
1480 static struct dentry *proc_pident_instantiate(struct inode *dir,
1481 struct dentry *dentry, struct task_struct *task, void *ptr)
1483 struct pid_entry *p = ptr;
1484 struct inode *inode;
1485 struct proc_inode *ei;
1486 struct dentry *error = ERR_PTR(-EINVAL);
1488 inode = proc_pid_make_inode(dir->i_sb, task);
1493 inode->i_mode = p->mode;
1494 if (S_ISDIR(inode->i_mode))
1495 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1497 inode->i_op = p->iop;
1499 inode->i_fop = p->fop;
1501 dentry->d_op = &pid_dentry_operations;
1502 d_add(dentry, inode);
1503 /* Close the race of the process dying before we return the dentry */
1504 if (pid_revalidate(dentry, NULL))
1510 static struct dentry *proc_pident_lookup(struct inode *dir,
1511 struct dentry *dentry,
1512 struct pid_entry *ents,
1515 struct inode *inode;
1516 struct dentry *error;
1517 struct task_struct *task = get_proc_task(dir);
1518 struct pid_entry *p, *last;
1520 error = ERR_PTR(-ENOENT);
1527 * Yes, it does not scale. And it should not. Don't add
1528 * new entries into /proc/<tgid>/ without very good reasons.
1530 last = &ents[nents - 1];
1531 for (p = ents; p <= last; p++) {
1532 if (p->len != dentry->d_name.len)
1534 if (!memcmp(dentry->d_name.name, p->name, p->len))
1540 error = proc_pident_instantiate(dir, dentry, task, p);
1542 put_task_struct(task);
1547 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1548 struct task_struct *task, struct pid_entry *p)
1550 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1551 proc_pident_instantiate, task, p);
1554 static int proc_pident_readdir(struct file *filp,
1555 void *dirent, filldir_t filldir,
1556 struct pid_entry *ents, unsigned int nents)
1560 struct dentry *dentry = filp->f_path.dentry;
1561 struct inode *inode = dentry->d_inode;
1562 struct task_struct *task = get_proc_task(inode);
1563 struct pid_entry *p, *last;
1577 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1583 ino = parent_ino(dentry);
1584 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1596 last = &ents[nents - 1];
1598 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1607 put_task_struct(task);
1612 #ifdef CONFIG_SECURITY
1613 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1614 size_t count, loff_t *ppos)
1616 struct inode * inode = file->f_path.dentry->d_inode;
1619 struct task_struct *task = get_proc_task(inode);
1624 length = security_getprocattr(task,
1625 (char*)file->f_path.dentry->d_name.name,
1627 put_task_struct(task);
1629 length = simple_read_from_buffer(buf, count, ppos, p, length);
1634 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1635 size_t count, loff_t *ppos)
1637 struct inode * inode = file->f_path.dentry->d_inode;
1640 struct task_struct *task = get_proc_task(inode);
1645 if (count > PAGE_SIZE)
1648 /* No partial writes. */
1654 page = (char*)__get_free_page(GFP_USER);
1659 if (copy_from_user(page, buf, count))
1662 length = security_setprocattr(task,
1663 (char*)file->f_path.dentry->d_name.name,
1664 (void*)page, count);
1666 free_page((unsigned long) page);
1668 put_task_struct(task);
1673 static const struct file_operations proc_pid_attr_operations = {
1674 .read = proc_pid_attr_read,
1675 .write = proc_pid_attr_write,
1678 static struct pid_entry attr_dir_stuff[] = {
1679 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1680 REG("prev", S_IRUGO, pid_attr),
1681 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1682 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1683 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1684 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1687 static int proc_attr_dir_readdir(struct file * filp,
1688 void * dirent, filldir_t filldir)
1690 return proc_pident_readdir(filp,dirent,filldir,
1691 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1694 static const struct file_operations proc_attr_dir_operations = {
1695 .read = generic_read_dir,
1696 .readdir = proc_attr_dir_readdir,
1699 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1700 struct dentry *dentry, struct nameidata *nd)
1702 return proc_pident_lookup(dir, dentry,
1703 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1706 static const struct inode_operations proc_attr_dir_inode_operations = {
1707 .lookup = proc_attr_dir_lookup,
1708 .getattr = pid_getattr,
1709 .setattr = proc_setattr,
1717 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1720 char tmp[PROC_NUMBUF];
1721 sprintf(tmp, "%d", current->tgid);
1722 return vfs_readlink(dentry,buffer,buflen,tmp);
1725 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1727 char tmp[PROC_NUMBUF];
1728 sprintf(tmp, "%d", current->tgid);
1729 return ERR_PTR(vfs_follow_link(nd,tmp));
1732 static const struct inode_operations proc_self_inode_operations = {
1733 .readlink = proc_self_readlink,
1734 .follow_link = proc_self_follow_link,
1740 * These are the directory entries in the root directory of /proc
1741 * that properly belong to the /proc filesystem, as they describe
1742 * describe something that is process related.
1744 static struct pid_entry proc_base_stuff[] = {
1745 NOD("self", S_IFLNK|S_IRWXUGO,
1746 &proc_self_inode_operations, NULL, {}),
1750 * Exceptional case: normally we are not allowed to unhash a busy
1751 * directory. In this case, however, we can do it - no aliasing problems
1752 * due to the way we treat inodes.
1754 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1756 struct inode *inode = dentry->d_inode;
1757 struct task_struct *task = get_proc_task(inode);
1759 put_task_struct(task);
1766 static struct dentry_operations proc_base_dentry_operations =
1768 .d_revalidate = proc_base_revalidate,
1769 .d_delete = pid_delete_dentry,
1772 static struct dentry *proc_base_instantiate(struct inode *dir,
1773 struct dentry *dentry, struct task_struct *task, void *ptr)
1775 struct pid_entry *p = ptr;
1776 struct inode *inode;
1777 struct proc_inode *ei;
1778 struct dentry *error = ERR_PTR(-EINVAL);
1780 /* Allocate the inode */
1781 error = ERR_PTR(-ENOMEM);
1782 inode = new_inode(dir->i_sb);
1786 /* Initialize the inode */
1788 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1791 * grab the reference to the task.
1793 ei->pid = get_task_pid(task, PIDTYPE_PID);
1799 inode->i_mode = p->mode;
1800 if (S_ISDIR(inode->i_mode))
1802 if (S_ISLNK(inode->i_mode))
1805 inode->i_op = p->iop;
1807 inode->i_fop = p->fop;
1809 dentry->d_op = &proc_base_dentry_operations;
1810 d_add(dentry, inode);
1819 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1821 struct dentry *error;
1822 struct task_struct *task = get_proc_task(dir);
1823 struct pid_entry *p, *last;
1825 error = ERR_PTR(-ENOENT);
1830 /* Lookup the directory entry */
1831 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1832 for (p = proc_base_stuff; p <= last; p++) {
1833 if (p->len != dentry->d_name.len)
1835 if (!memcmp(dentry->d_name.name, p->name, p->len))
1841 error = proc_base_instantiate(dir, dentry, task, p);
1844 put_task_struct(task);
1849 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1850 struct task_struct *task, struct pid_entry *p)
1852 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1853 proc_base_instantiate, task, p);
1856 #ifdef CONFIG_TASK_IO_ACCOUNTING
1857 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1859 return sprintf(buffer,
1860 #ifdef CONFIG_TASK_XACCT
1866 "read_bytes: %llu\n"
1867 "write_bytes: %llu\n"
1868 "cancelled_write_bytes: %llu\n",
1869 #ifdef CONFIG_TASK_XACCT
1870 (unsigned long long)task->rchar,
1871 (unsigned long long)task->wchar,
1872 (unsigned long long)task->syscr,
1873 (unsigned long long)task->syscw,
1875 (unsigned long long)task->ioac.read_bytes,
1876 (unsigned long long)task->ioac.write_bytes,
1877 (unsigned long long)task->ioac.cancelled_write_bytes);
1884 static const struct file_operations proc_task_operations;
1885 static const struct inode_operations proc_task_inode_operations;
1887 static struct pid_entry tgid_base_stuff[] = {
1888 DIR("task", S_IRUGO|S_IXUGO, task),
1889 DIR("fd", S_IRUSR|S_IXUSR, fd),
1890 INF("environ", S_IRUSR, pid_environ),
1891 INF("auxv", S_IRUSR, pid_auxv),
1892 INF("status", S_IRUGO, pid_status),
1893 INF("cmdline", S_IRUGO, pid_cmdline),
1894 INF("stat", S_IRUGO, tgid_stat),
1895 INF("statm", S_IRUGO, pid_statm),
1896 REG("maps", S_IRUGO, maps),
1898 REG("numa_maps", S_IRUGO, numa_maps),
1900 REG("mem", S_IRUSR|S_IWUSR, mem),
1901 #ifdef CONFIG_SECCOMP
1902 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1907 REG("mounts", S_IRUGO, mounts),
1908 REG("mountstats", S_IRUSR, mountstats),
1910 REG("clear_refs", S_IWUSR, clear_refs),
1911 REG("smaps", S_IRUGO, smaps),
1913 #ifdef CONFIG_SECURITY
1914 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1916 #ifdef CONFIG_KALLSYMS
1917 INF("wchan", S_IRUGO, pid_wchan),
1919 #ifdef CONFIG_SCHEDSTATS
1920 INF("schedstat", S_IRUGO, pid_schedstat),
1922 #ifdef CONFIG_CPUSETS
1923 REG("cpuset", S_IRUGO, cpuset),
1925 INF("oom_score", S_IRUGO, oom_score),
1926 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1927 #ifdef CONFIG_AUDITSYSCALL
1928 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
1930 #ifdef CONFIG_FAULT_INJECTION
1931 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1933 #ifdef CONFIG_TASK_IO_ACCOUNTING
1934 INF("io", S_IRUGO, pid_io_accounting),
1938 static int proc_tgid_base_readdir(struct file * filp,
1939 void * dirent, filldir_t filldir)
1941 return proc_pident_readdir(filp,dirent,filldir,
1942 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1945 static const struct file_operations proc_tgid_base_operations = {
1946 .read = generic_read_dir,
1947 .readdir = proc_tgid_base_readdir,
1950 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1951 return proc_pident_lookup(dir, dentry,
1952 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1955 static const struct inode_operations proc_tgid_base_inode_operations = {
1956 .lookup = proc_tgid_base_lookup,
1957 .getattr = pid_getattr,
1958 .setattr = proc_setattr,
1962 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1964 * @task: task that should be flushed.
1966 * Looks in the dcache for
1968 * /proc/@tgid/task/@pid
1969 * if either directory is present flushes it and all of it'ts children
1972 * It is safe and reasonable to cache /proc entries for a task until
1973 * that task exits. After that they just clog up the dcache with
1974 * useless entries, possibly causing useful dcache entries to be
1975 * flushed instead. This routine is proved to flush those useless
1976 * dcache entries at process exit time.
1978 * NOTE: This routine is just an optimization so it does not guarantee
1979 * that no dcache entries will exist at process exit time it
1980 * just makes it very unlikely that any will persist.
1982 void proc_flush_task(struct task_struct *task)
1984 struct dentry *dentry, *leader, *dir;
1985 char buf[PROC_NUMBUF];
1989 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1990 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1992 shrink_dcache_parent(dentry);
1997 if (thread_group_leader(task))
2001 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2002 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2007 name.len = strlen(name.name);
2008 dir = d_hash_and_lookup(leader, &name);
2010 goto out_put_leader;
2013 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2014 dentry = d_hash_and_lookup(dir, &name);
2016 shrink_dcache_parent(dentry);
2028 static struct dentry *proc_pid_instantiate(struct inode *dir,
2029 struct dentry * dentry,
2030 struct task_struct *task, void *ptr)
2032 struct dentry *error = ERR_PTR(-ENOENT);
2033 struct inode *inode;
2035 inode = proc_pid_make_inode(dir->i_sb, task);
2039 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2040 inode->i_op = &proc_tgid_base_inode_operations;
2041 inode->i_fop = &proc_tgid_base_operations;
2042 inode->i_flags|=S_IMMUTABLE;
2044 #ifdef CONFIG_SECURITY
2045 inode->i_nlink += 1;
2048 dentry->d_op = &pid_dentry_operations;
2050 d_add(dentry, inode);
2051 /* Close the race of the process dying before we return the dentry */
2052 if (pid_revalidate(dentry, NULL))
2058 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2060 struct dentry *result = ERR_PTR(-ENOENT);
2061 struct task_struct *task;
2064 result = proc_base_lookup(dir, dentry);
2065 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2068 tgid = name_to_int(dentry);
2073 task = find_task_by_pid(tgid);
2075 get_task_struct(task);
2080 result = proc_pid_instantiate(dir, dentry, task, NULL);
2081 put_task_struct(task);
2087 * Find the first task with tgid >= tgid
2090 static struct task_struct *next_tgid(unsigned int tgid)
2092 struct task_struct *task;
2098 pid = find_ge_pid(tgid);
2101 task = pid_task(pid, PIDTYPE_PID);
2102 /* What we to know is if the pid we have find is the
2103 * pid of a thread_group_leader. Testing for task
2104 * being a thread_group_leader is the obvious thing
2105 * todo but there is a window when it fails, due to
2106 * the pid transfer logic in de_thread.
2108 * So we perform the straight forward test of seeing
2109 * if the pid we have found is the pid of a thread
2110 * group leader, and don't worry if the task we have
2111 * found doesn't happen to be a thread group leader.
2112 * As we don't care in the case of readdir.
2114 if (!task || !has_group_leader_pid(task))
2116 get_task_struct(task);
2122 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2124 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2125 struct task_struct *task, int tgid)
2127 char name[PROC_NUMBUF];
2128 int len = snprintf(name, sizeof(name), "%d", tgid);
2129 return proc_fill_cache(filp, dirent, filldir, name, len,
2130 proc_pid_instantiate, task, NULL);
2133 /* for the /proc/ directory itself, after non-process stuff has been done */
2134 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2136 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2137 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2138 struct task_struct *task;
2144 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2145 struct pid_entry *p = &proc_base_stuff[nr];
2146 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2150 tgid = filp->f_pos - TGID_OFFSET;
2151 for (task = next_tgid(tgid);
2153 put_task_struct(task), task = next_tgid(tgid + 1)) {
2155 filp->f_pos = tgid + TGID_OFFSET;
2156 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2157 put_task_struct(task);
2161 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2163 put_task_struct(reaper);
2171 static struct pid_entry tid_base_stuff[] = {
2172 DIR("fd", S_IRUSR|S_IXUSR, fd),
2173 INF("environ", S_IRUSR, pid_environ),
2174 INF("auxv", S_IRUSR, pid_auxv),
2175 INF("status", S_IRUGO, pid_status),
2176 INF("cmdline", S_IRUGO, pid_cmdline),
2177 INF("stat", S_IRUGO, tid_stat),
2178 INF("statm", S_IRUGO, pid_statm),
2179 REG("maps", S_IRUGO, maps),
2181 REG("numa_maps", S_IRUGO, numa_maps),
2183 REG("mem", S_IRUSR|S_IWUSR, mem),
2184 #ifdef CONFIG_SECCOMP
2185 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2190 REG("mounts", S_IRUGO, mounts),
2192 REG("clear_refs", S_IWUSR, clear_refs),
2193 REG("smaps", S_IRUGO, smaps),
2195 #ifdef CONFIG_SECURITY
2196 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2198 #ifdef CONFIG_KALLSYMS
2199 INF("wchan", S_IRUGO, pid_wchan),
2201 #ifdef CONFIG_SCHEDSTATS
2202 INF("schedstat", S_IRUGO, pid_schedstat),
2204 #ifdef CONFIG_CPUSETS
2205 REG("cpuset", S_IRUGO, cpuset),
2207 INF("oom_score", S_IRUGO, oom_score),
2208 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2209 #ifdef CONFIG_AUDITSYSCALL
2210 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2212 #ifdef CONFIG_FAULT_INJECTION
2213 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2217 static int proc_tid_base_readdir(struct file * filp,
2218 void * dirent, filldir_t filldir)
2220 return proc_pident_readdir(filp,dirent,filldir,
2221 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2224 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2225 return proc_pident_lookup(dir, dentry,
2226 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2229 static const struct file_operations proc_tid_base_operations = {
2230 .read = generic_read_dir,
2231 .readdir = proc_tid_base_readdir,
2234 static const struct inode_operations proc_tid_base_inode_operations = {
2235 .lookup = proc_tid_base_lookup,
2236 .getattr = pid_getattr,
2237 .setattr = proc_setattr,
2240 static struct dentry *proc_task_instantiate(struct inode *dir,
2241 struct dentry *dentry, struct task_struct *task, void *ptr)
2243 struct dentry *error = ERR_PTR(-ENOENT);
2244 struct inode *inode;
2245 inode = proc_pid_make_inode(dir->i_sb, task);
2249 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2250 inode->i_op = &proc_tid_base_inode_operations;
2251 inode->i_fop = &proc_tid_base_operations;
2252 inode->i_flags|=S_IMMUTABLE;
2254 #ifdef CONFIG_SECURITY
2255 inode->i_nlink += 1;
2258 dentry->d_op = &pid_dentry_operations;
2260 d_add(dentry, inode);
2261 /* Close the race of the process dying before we return the dentry */
2262 if (pid_revalidate(dentry, NULL))
2268 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2270 struct dentry *result = ERR_PTR(-ENOENT);
2271 struct task_struct *task;
2272 struct task_struct *leader = get_proc_task(dir);
2278 tid = name_to_int(dentry);
2283 task = find_task_by_pid(tid);
2285 get_task_struct(task);
2289 if (leader->tgid != task->tgid)
2292 result = proc_task_instantiate(dir, dentry, task, NULL);
2294 put_task_struct(task);
2296 put_task_struct(leader);
2302 * Find the first tid of a thread group to return to user space.
2304 * Usually this is just the thread group leader, but if the users
2305 * buffer was too small or there was a seek into the middle of the
2306 * directory we have more work todo.
2308 * In the case of a short read we start with find_task_by_pid.
2310 * In the case of a seek we start with the leader and walk nr
2313 static struct task_struct *first_tid(struct task_struct *leader,
2316 struct task_struct *pos;
2319 /* Attempt to start with the pid of a thread */
2320 if (tid && (nr > 0)) {
2321 pos = find_task_by_pid(tid);
2322 if (pos && (pos->group_leader == leader))
2326 /* If nr exceeds the number of threads there is nothing todo */
2328 if (nr && nr >= get_nr_threads(leader))
2331 /* If we haven't found our starting place yet start
2332 * with the leader and walk nr threads forward.
2334 for (pos = leader; nr > 0; --nr) {
2335 pos = next_thread(pos);
2336 if (pos == leader) {
2342 get_task_struct(pos);
2349 * Find the next thread in the thread list.
2350 * Return NULL if there is an error or no next thread.
2352 * The reference to the input task_struct is released.
2354 static struct task_struct *next_tid(struct task_struct *start)
2356 struct task_struct *pos = NULL;
2358 if (pid_alive(start)) {
2359 pos = next_thread(start);
2360 if (thread_group_leader(pos))
2363 get_task_struct(pos);
2366 put_task_struct(start);
2370 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2371 struct task_struct *task, int tid)
2373 char name[PROC_NUMBUF];
2374 int len = snprintf(name, sizeof(name), "%d", tid);
2375 return proc_fill_cache(filp, dirent, filldir, name, len,
2376 proc_task_instantiate, task, NULL);
2379 /* for the /proc/TGID/task/ directories */
2380 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2382 struct dentry *dentry = filp->f_path.dentry;
2383 struct inode *inode = dentry->d_inode;
2384 struct task_struct *leader = NULL;
2385 struct task_struct *task;
2386 int retval = -ENOENT;
2389 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2391 task = get_proc_task(inode);
2395 if (pid_alive(task)) {
2396 leader = task->group_leader;
2397 get_task_struct(leader);
2400 put_task_struct(task);
2408 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2413 ino = parent_ino(dentry);
2414 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2420 /* f_version caches the tgid value that the last readdir call couldn't
2421 * return. lseek aka telldir automagically resets f_version to 0.
2423 tid = filp->f_version;
2424 filp->f_version = 0;
2425 for (task = first_tid(leader, tid, pos - 2);
2427 task = next_tid(task), pos++) {
2429 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2430 /* returning this tgid failed, save it as the first
2431 * pid for the next readir call */
2432 filp->f_version = tid;
2433 put_task_struct(task);
2439 put_task_struct(leader);
2444 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2446 struct inode *inode = dentry->d_inode;
2447 struct task_struct *p = get_proc_task(inode);
2448 generic_fillattr(inode, stat);
2452 stat->nlink += get_nr_threads(p);
2460 static const struct inode_operations proc_task_inode_operations = {
2461 .lookup = proc_task_lookup,
2462 .getattr = proc_task_getattr,
2463 .setattr = proc_setattr,
2466 static const struct file_operations proc_task_operations = {
2467 .read = generic_read_dir,
2468 .readdir = proc_task_readdir,