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/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
74 #include <linux/nsproxy.h>
75 #include <linux/oom.h>
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
89 /* Worst case buffer size needed for holding an integer. */
90 #define PROC_NUMBUF 13
96 struct inode_operations *iop;
97 struct file_operations *fop;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
102 .len = sizeof(NAME) - 1, \
110 #define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 #define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
118 #define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
120 &proc_##OTYPE##_operations, {})
121 #define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
126 static struct fs_struct *get_fs_struct(struct task_struct *task)
128 struct fs_struct *fs;
132 atomic_inc(&fs->count);
137 static int get_nr_threads(struct task_struct *tsk)
139 /* Must be called with the rcu_read_lock held */
143 if (lock_task_sighand(tsk, &flags)) {
144 count = atomic_read(&tsk->signal->count);
145 unlock_task_sighand(tsk, &flags);
150 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
152 struct task_struct *task = get_proc_task(inode);
153 struct fs_struct *fs = NULL;
154 int result = -ENOENT;
157 fs = get_fs_struct(task);
158 put_task_struct(task);
161 read_lock(&fs->lock);
162 *mnt = mntget(fs->pwdmnt);
163 *dentry = dget(fs->pwd);
164 read_unlock(&fs->lock);
171 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
173 struct task_struct *task = get_proc_task(inode);
174 struct fs_struct *fs = NULL;
175 int result = -ENOENT;
178 fs = get_fs_struct(task);
179 put_task_struct(task);
182 read_lock(&fs->lock);
183 *mnt = mntget(fs->rootmnt);
184 *dentry = dget(fs->root);
185 read_unlock(&fs->lock);
192 #define MAY_PTRACE(task) \
193 (task == current || \
194 (task->parent == current && \
195 (task->ptrace & PT_PTRACED) && \
196 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
197 security_ptrace(current,task) == 0))
199 static int proc_pid_environ(struct task_struct *task, char * buffer)
202 struct mm_struct *mm = get_task_mm(task);
204 unsigned int len = mm->env_end - mm->env_start;
207 res = access_process_vm(task, mm->env_start, buffer, len, 0);
208 if (!ptrace_may_attach(task))
215 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
219 struct mm_struct *mm = get_task_mm(task);
223 goto out_mm; /* Shh! No looking before we're done */
225 len = mm->arg_end - mm->arg_start;
230 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
232 // If the nul at the end of args has been overwritten, then
233 // assume application is using setproctitle(3).
234 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
235 len = strnlen(buffer, res);
239 len = mm->env_end - mm->env_start;
240 if (len > PAGE_SIZE - res)
241 len = PAGE_SIZE - res;
242 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
243 res = strnlen(buffer, res);
252 static int proc_pid_auxv(struct task_struct *task, char *buffer)
255 struct mm_struct *mm = get_task_mm(task);
257 unsigned int nwords = 0;
260 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
261 res = nwords * sizeof(mm->saved_auxv[0]);
264 memcpy(buffer, mm->saved_auxv, res);
271 #ifdef CONFIG_KALLSYMS
273 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
274 * Returns the resolved symbol. If that fails, simply return the address.
276 static int proc_pid_wchan(struct task_struct *task, char *buffer)
279 const char *sym_name;
280 unsigned long wchan, size, offset;
281 char namebuf[KSYM_NAME_LEN+1];
283 wchan = get_wchan(task);
285 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
287 return sprintf(buffer, "%s", sym_name);
288 return sprintf(buffer, "%lu", wchan);
290 #endif /* CONFIG_KALLSYMS */
292 #ifdef CONFIG_SCHEDSTATS
294 * Provides /proc/PID/schedstat
296 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
298 return sprintf(buffer, "%lu %lu %lu\n",
299 task->sched_info.cpu_time,
300 task->sched_info.run_delay,
301 task->sched_info.pcnt);
305 /* The badness from the OOM killer */
306 unsigned long badness(struct task_struct *p, unsigned long uptime);
307 static int proc_oom_score(struct task_struct *task, char *buffer)
309 unsigned long points;
310 struct timespec uptime;
312 do_posix_clock_monotonic_gettime(&uptime);
313 points = badness(task, uptime.tv_sec);
314 return sprintf(buffer, "%lu\n", points);
317 /************************************************************************/
318 /* Here the fs part begins */
319 /************************************************************************/
321 /* permission checks */
322 static int proc_fd_access_allowed(struct inode *inode)
324 struct task_struct *task;
326 /* Allow access to a task's file descriptors if it is us or we
327 * may use ptrace attach to the process and find out that
330 task = get_proc_task(inode);
332 allowed = ptrace_may_attach(task);
333 put_task_struct(task);
338 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
341 struct inode *inode = dentry->d_inode;
343 if (attr->ia_valid & ATTR_MODE)
346 error = inode_change_ok(inode, attr);
348 error = security_inode_setattr(dentry, attr);
350 error = inode_setattr(inode, attr);
355 static struct inode_operations proc_def_inode_operations = {
356 .setattr = proc_setattr,
359 extern struct seq_operations mounts_op;
365 static int mounts_open(struct inode *inode, struct file *file)
367 struct task_struct *task = get_proc_task(inode);
368 struct mnt_namespace *ns = NULL;
369 struct proc_mounts *p;
374 ns = task->nsproxy->mnt_ns;
378 put_task_struct(task);
383 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
385 file->private_data = &p->m;
386 ret = seq_open(file, &mounts_op);
389 p->event = ns->event;
399 static int mounts_release(struct inode *inode, struct file *file)
401 struct seq_file *m = file->private_data;
402 struct mnt_namespace *ns = m->private;
404 return seq_release(inode, file);
407 static unsigned mounts_poll(struct file *file, poll_table *wait)
409 struct proc_mounts *p = file->private_data;
410 struct mnt_namespace *ns = p->m.private;
413 poll_wait(file, &ns->poll, wait);
415 spin_lock(&vfsmount_lock);
416 if (p->event != ns->event) {
417 p->event = ns->event;
420 spin_unlock(&vfsmount_lock);
425 static struct file_operations proc_mounts_operations = {
429 .release = mounts_release,
433 extern struct seq_operations mountstats_op;
434 static int mountstats_open(struct inode *inode, struct file *file)
436 int ret = seq_open(file, &mountstats_op);
439 struct seq_file *m = file->private_data;
440 struct mnt_namespace *mnt_ns = NULL;
441 struct task_struct *task = get_proc_task(inode);
446 mnt_ns = task->nsproxy->mnt_ns;
450 put_task_struct(task);
456 seq_release(inode, file);
463 static struct file_operations proc_mountstats_operations = {
464 .open = mountstats_open,
467 .release = mounts_release,
470 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
472 static ssize_t proc_info_read(struct file * file, char __user * buf,
473 size_t count, loff_t *ppos)
475 struct inode * inode = file->f_path.dentry->d_inode;
478 struct task_struct *task = get_proc_task(inode);
484 if (count > PROC_BLOCK_SIZE)
485 count = PROC_BLOCK_SIZE;
488 if (!(page = __get_free_page(GFP_KERNEL)))
491 length = PROC_I(inode)->op.proc_read(task, (char*)page);
494 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
497 put_task_struct(task);
502 static struct file_operations proc_info_file_operations = {
503 .read = proc_info_read,
506 static int mem_open(struct inode* inode, struct file* file)
508 file->private_data = (void*)((long)current->self_exec_id);
512 static ssize_t mem_read(struct file * file, char __user * buf,
513 size_t count, loff_t *ppos)
515 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
517 unsigned long src = *ppos;
519 struct mm_struct *mm;
524 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
528 page = (char *)__get_free_page(GFP_USER);
534 mm = get_task_mm(task);
540 if (file->private_data != (void*)((long)current->self_exec_id))
546 int this_len, retval;
548 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
549 retval = access_process_vm(task, src, page, this_len, 0);
550 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
556 if (copy_to_user(buf, page, retval)) {
571 free_page((unsigned long) page);
573 put_task_struct(task);
578 #define mem_write NULL
581 /* This is a security hazard */
582 static ssize_t mem_write(struct file * file, const char * buf,
583 size_t count, loff_t *ppos)
587 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
588 unsigned long dst = *ppos;
594 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
598 page = (char *)__get_free_page(GFP_USER);
604 int this_len, retval;
606 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
607 if (copy_from_user(page, buf, this_len)) {
611 retval = access_process_vm(task, dst, page, this_len, 1);
623 free_page((unsigned long) page);
625 put_task_struct(task);
631 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
635 file->f_pos = offset;
638 file->f_pos += offset;
643 force_successful_syscall_return();
647 static struct file_operations proc_mem_operations = {
654 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
655 size_t count, loff_t *ppos)
657 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
658 char buffer[PROC_NUMBUF];
661 loff_t __ppos = *ppos;
665 oom_adjust = task->oomkilladj;
666 put_task_struct(task);
668 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
671 if (count > len-__ppos)
673 if (copy_to_user(buf, buffer + __ppos, count))
675 *ppos = __ppos + count;
679 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
680 size_t count, loff_t *ppos)
682 struct task_struct *task;
683 char buffer[PROC_NUMBUF], *end;
686 memset(buffer, 0, sizeof(buffer));
687 if (count > sizeof(buffer) - 1)
688 count = sizeof(buffer) - 1;
689 if (copy_from_user(buffer, buf, count))
691 oom_adjust = simple_strtol(buffer, &end, 0);
692 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
693 oom_adjust != OOM_DISABLE)
697 task = get_proc_task(file->f_path.dentry->d_inode);
700 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
701 put_task_struct(task);
704 task->oomkilladj = oom_adjust;
705 put_task_struct(task);
706 if (end - buffer == 0)
711 static struct file_operations proc_oom_adjust_operations = {
712 .read = oom_adjust_read,
713 .write = oom_adjust_write,
716 #ifdef CONFIG_AUDITSYSCALL
718 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
719 size_t count, loff_t *ppos)
721 struct inode * inode = file->f_path.dentry->d_inode;
722 struct task_struct *task = get_proc_task(inode);
724 char tmpbuf[TMPBUFLEN];
728 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
729 audit_get_loginuid(task->audit_context));
730 put_task_struct(task);
731 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
734 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
735 size_t count, loff_t *ppos)
737 struct inode * inode = file->f_path.dentry->d_inode;
742 if (!capable(CAP_AUDIT_CONTROL))
745 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
748 if (count >= PAGE_SIZE)
749 count = PAGE_SIZE - 1;
752 /* No partial writes. */
755 page = (char*)__get_free_page(GFP_USER);
759 if (copy_from_user(page, buf, count))
763 loginuid = simple_strtoul(page, &tmp, 10);
769 length = audit_set_loginuid(current, loginuid);
770 if (likely(length == 0))
774 free_page((unsigned long) page);
778 static struct file_operations proc_loginuid_operations = {
779 .read = proc_loginuid_read,
780 .write = proc_loginuid_write,
784 #ifdef CONFIG_SECCOMP
785 static ssize_t seccomp_read(struct file *file, char __user *buf,
786 size_t count, loff_t *ppos)
788 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
790 loff_t __ppos = *ppos;
795 /* no need to print the trailing zero, so use only len */
796 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
797 put_task_struct(tsk);
800 if (count > len - __ppos)
801 count = len - __ppos;
802 if (copy_to_user(buf, __buf + __ppos, count))
804 *ppos = __ppos + count;
808 static ssize_t seccomp_write(struct file *file, const char __user *buf,
809 size_t count, loff_t *ppos)
811 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
812 char __buf[20], *end;
813 unsigned int seccomp_mode;
820 /* can set it only once to be even more secure */
822 if (unlikely(tsk->seccomp.mode))
826 memset(__buf, 0, sizeof(__buf));
827 count = min(count, sizeof(__buf) - 1);
828 if (copy_from_user(__buf, buf, count))
831 seccomp_mode = simple_strtoul(__buf, &end, 0);
835 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
836 tsk->seccomp.mode = seccomp_mode;
837 set_tsk_thread_flag(tsk, TIF_SECCOMP);
841 if (unlikely(!(end - __buf)))
843 result = end - __buf;
845 put_task_struct(tsk);
850 static struct file_operations proc_seccomp_operations = {
851 .read = seccomp_read,
852 .write = seccomp_write,
854 #endif /* CONFIG_SECCOMP */
856 #ifdef CONFIG_FAULT_INJECTION
857 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
858 size_t count, loff_t *ppos)
860 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
861 char buffer[PROC_NUMBUF];
864 loff_t __ppos = *ppos;
868 make_it_fail = task->make_it_fail;
869 put_task_struct(task);
871 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
874 if (count > len-__ppos)
876 if (copy_to_user(buf, buffer + __ppos, count))
878 *ppos = __ppos + count;
882 static ssize_t proc_fault_inject_write(struct file * file,
883 const char __user * buf, size_t count, loff_t *ppos)
885 struct task_struct *task;
886 char buffer[PROC_NUMBUF], *end;
889 if (!capable(CAP_SYS_RESOURCE))
891 memset(buffer, 0, sizeof(buffer));
892 if (count > sizeof(buffer) - 1)
893 count = sizeof(buffer) - 1;
894 if (copy_from_user(buffer, buf, count))
896 make_it_fail = simple_strtol(buffer, &end, 0);
899 task = get_proc_task(file->f_dentry->d_inode);
902 task->make_it_fail = make_it_fail;
903 put_task_struct(task);
904 if (end - buffer == 0)
909 static struct file_operations proc_fault_inject_operations = {
910 .read = proc_fault_inject_read,
911 .write = proc_fault_inject_write,
915 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
917 struct inode *inode = dentry->d_inode;
920 /* We don't need a base pointer in the /proc filesystem */
923 /* Are we allowed to snoop on the tasks file descriptors? */
924 if (!proc_fd_access_allowed(inode))
927 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
928 nd->last_type = LAST_BIND;
930 return ERR_PTR(error);
933 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
934 char __user *buffer, int buflen)
936 struct inode * inode;
937 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
943 inode = dentry->d_inode;
944 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
948 len = tmp + PAGE_SIZE - 1 - path;
952 if (copy_to_user(buffer, path, len))
955 free_page((unsigned long)tmp);
959 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
962 struct inode *inode = dentry->d_inode;
964 struct vfsmount *mnt = NULL;
966 /* Are we allowed to snoop on the tasks file descriptors? */
967 if (!proc_fd_access_allowed(inode))
970 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
974 error = do_proc_readlink(de, mnt, buffer, buflen);
981 static struct inode_operations proc_pid_link_inode_operations = {
982 .readlink = proc_pid_readlink,
983 .follow_link = proc_pid_follow_link,
984 .setattr = proc_setattr,
988 /* building an inode */
990 static int task_dumpable(struct task_struct *task)
993 struct mm_struct *mm;
998 dumpable = mm->dumpable;
1006 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1008 struct inode * inode;
1009 struct proc_inode *ei;
1011 /* We need a new inode */
1013 inode = new_inode(sb);
1019 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1020 inode->i_op = &proc_def_inode_operations;
1023 * grab the reference to task.
1025 ei->pid = get_task_pid(task, PIDTYPE_PID);
1031 if (task_dumpable(task)) {
1032 inode->i_uid = task->euid;
1033 inode->i_gid = task->egid;
1035 security_task_to_inode(task, inode);
1045 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1047 struct inode *inode = dentry->d_inode;
1048 struct task_struct *task;
1049 generic_fillattr(inode, stat);
1054 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1056 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1057 task_dumpable(task)) {
1058 stat->uid = task->euid;
1059 stat->gid = task->egid;
1069 * Exceptional case: normally we are not allowed to unhash a busy
1070 * directory. In this case, however, we can do it - no aliasing problems
1071 * due to the way we treat inodes.
1073 * Rewrite the inode's ownerships here because the owning task may have
1074 * performed a setuid(), etc.
1076 * Before the /proc/pid/status file was created the only way to read
1077 * the effective uid of a /process was to stat /proc/pid. Reading
1078 * /proc/pid/status is slow enough that procps and other packages
1079 * kept stating /proc/pid. To keep the rules in /proc simple I have
1080 * made this apply to all per process world readable and executable
1083 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1085 struct inode *inode = dentry->d_inode;
1086 struct task_struct *task = get_proc_task(inode);
1088 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1089 task_dumpable(task)) {
1090 inode->i_uid = task->euid;
1091 inode->i_gid = task->egid;
1096 inode->i_mode &= ~(S_ISUID | S_ISGID);
1097 security_task_to_inode(task, inode);
1098 put_task_struct(task);
1105 static int pid_delete_dentry(struct dentry * dentry)
1107 /* Is the task we represent dead?
1108 * If so, then don't put the dentry on the lru list,
1109 * kill it immediately.
1111 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1114 static struct dentry_operations pid_dentry_operations =
1116 .d_revalidate = pid_revalidate,
1117 .d_delete = pid_delete_dentry,
1122 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1125 * Fill a directory entry.
1127 * If possible create the dcache entry and derive our inode number and
1128 * file type from dcache entry.
1130 * Since all of the proc inode numbers are dynamically generated, the inode
1131 * numbers do not exist until the inode is cache. This means creating the
1132 * the dcache entry in readdir is necessary to keep the inode numbers
1133 * reported by readdir in sync with the inode numbers reported
1136 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1137 char *name, int len,
1138 instantiate_t instantiate, struct task_struct *task, void *ptr)
1140 struct dentry *child, *dir = filp->f_path.dentry;
1141 struct inode *inode;
1144 unsigned type = DT_UNKNOWN;
1148 qname.hash = full_name_hash(name, len);
1150 child = d_lookup(dir, &qname);
1153 new = d_alloc(dir, &qname);
1155 child = instantiate(dir->d_inode, new, task, ptr);
1162 if (!child || IS_ERR(child) || !child->d_inode)
1163 goto end_instantiate;
1164 inode = child->d_inode;
1167 type = inode->i_mode >> 12;
1172 ino = find_inode_number(dir, &qname);
1175 return filldir(dirent, name, len, filp->f_pos, ino, type);
1178 static unsigned name_to_int(struct dentry *dentry)
1180 const char *name = dentry->d_name.name;
1181 int len = dentry->d_name.len;
1184 if (len > 1 && *name == '0')
1187 unsigned c = *name++ - '0';
1190 if (n >= (~0U-9)/10)
1200 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1202 struct task_struct *task = get_proc_task(inode);
1203 struct files_struct *files = NULL;
1205 int fd = proc_fd(inode);
1208 files = get_files_struct(task);
1209 put_task_struct(task);
1213 * We are not taking a ref to the file structure, so we must
1216 spin_lock(&files->file_lock);
1217 file = fcheck_files(files, fd);
1219 *mnt = mntget(file->f_path.mnt);
1220 *dentry = dget(file->f_path.dentry);
1221 spin_unlock(&files->file_lock);
1222 put_files_struct(files);
1225 spin_unlock(&files->file_lock);
1226 put_files_struct(files);
1231 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1233 struct inode *inode = dentry->d_inode;
1234 struct task_struct *task = get_proc_task(inode);
1235 int fd = proc_fd(inode);
1236 struct files_struct *files;
1239 files = get_files_struct(task);
1242 if (fcheck_files(files, fd)) {
1244 put_files_struct(files);
1245 if (task_dumpable(task)) {
1246 inode->i_uid = task->euid;
1247 inode->i_gid = task->egid;
1252 inode->i_mode &= ~(S_ISUID | S_ISGID);
1253 security_task_to_inode(task, inode);
1254 put_task_struct(task);
1258 put_files_struct(files);
1260 put_task_struct(task);
1266 static struct dentry_operations tid_fd_dentry_operations =
1268 .d_revalidate = tid_fd_revalidate,
1269 .d_delete = pid_delete_dentry,
1272 static struct dentry *proc_fd_instantiate(struct inode *dir,
1273 struct dentry *dentry, struct task_struct *task, void *ptr)
1275 unsigned fd = *(unsigned *)ptr;
1277 struct files_struct *files;
1278 struct inode *inode;
1279 struct proc_inode *ei;
1280 struct dentry *error = ERR_PTR(-ENOENT);
1282 inode = proc_pid_make_inode(dir->i_sb, task);
1287 files = get_files_struct(task);
1290 inode->i_mode = S_IFLNK;
1293 * We are not taking a ref to the file structure, so we must
1296 spin_lock(&files->file_lock);
1297 file = fcheck_files(files, fd);
1300 if (file->f_mode & 1)
1301 inode->i_mode |= S_IRUSR | S_IXUSR;
1302 if (file->f_mode & 2)
1303 inode->i_mode |= S_IWUSR | S_IXUSR;
1304 spin_unlock(&files->file_lock);
1305 put_files_struct(files);
1307 inode->i_op = &proc_pid_link_inode_operations;
1309 ei->op.proc_get_link = proc_fd_link;
1310 dentry->d_op = &tid_fd_dentry_operations;
1311 d_add(dentry, inode);
1312 /* Close the race of the process dying before we return the dentry */
1313 if (tid_fd_revalidate(dentry, NULL))
1319 spin_unlock(&files->file_lock);
1320 put_files_struct(files);
1326 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1328 struct task_struct *task = get_proc_task(dir);
1329 unsigned fd = name_to_int(dentry);
1330 struct dentry *result = ERR_PTR(-ENOENT);
1337 result = proc_fd_instantiate(dir, dentry, task, &fd);
1339 put_task_struct(task);
1344 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1345 struct task_struct *task, int fd)
1347 char name[PROC_NUMBUF];
1348 int len = snprintf(name, sizeof(name), "%d", fd);
1349 return proc_fill_cache(filp, dirent, filldir, name, len,
1350 proc_fd_instantiate, task, &fd);
1353 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1355 struct dentry *dentry = filp->f_path.dentry;
1356 struct inode *inode = dentry->d_inode;
1357 struct task_struct *p = get_proc_task(inode);
1358 unsigned int fd, tid, ino;
1360 struct files_struct * files;
1361 struct fdtable *fdt;
1372 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1376 ino = parent_ino(dentry);
1377 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1381 files = get_files_struct(p);
1385 fdt = files_fdtable(files);
1386 for (fd = filp->f_pos-2;
1388 fd++, filp->f_pos++) {
1390 if (!fcheck_files(files, fd))
1394 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1401 put_files_struct(files);
1409 static struct file_operations proc_fd_operations = {
1410 .read = generic_read_dir,
1411 .readdir = proc_readfd,
1415 * proc directories can do almost nothing..
1417 static struct inode_operations proc_fd_inode_operations = {
1418 .lookup = proc_lookupfd,
1419 .setattr = proc_setattr,
1422 static struct dentry *proc_pident_instantiate(struct inode *dir,
1423 struct dentry *dentry, struct task_struct *task, void *ptr)
1425 struct pid_entry *p = ptr;
1426 struct inode *inode;
1427 struct proc_inode *ei;
1428 struct dentry *error = ERR_PTR(-EINVAL);
1430 inode = proc_pid_make_inode(dir->i_sb, task);
1435 inode->i_mode = p->mode;
1436 if (S_ISDIR(inode->i_mode))
1437 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1439 inode->i_op = p->iop;
1441 inode->i_fop = p->fop;
1443 dentry->d_op = &pid_dentry_operations;
1444 d_add(dentry, inode);
1445 /* Close the race of the process dying before we return the dentry */
1446 if (pid_revalidate(dentry, NULL))
1452 static struct dentry *proc_pident_lookup(struct inode *dir,
1453 struct dentry *dentry,
1454 struct pid_entry *ents,
1457 struct inode *inode;
1458 struct dentry *error;
1459 struct task_struct *task = get_proc_task(dir);
1460 struct pid_entry *p, *last;
1462 error = ERR_PTR(-ENOENT);
1469 * Yes, it does not scale. And it should not. Don't add
1470 * new entries into /proc/<tgid>/ without very good reasons.
1472 last = &ents[nents - 1];
1473 for (p = ents; p <= last; p++) {
1474 if (p->len != dentry->d_name.len)
1476 if (!memcmp(dentry->d_name.name, p->name, p->len))
1482 error = proc_pident_instantiate(dir, dentry, task, p);
1484 put_task_struct(task);
1489 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1490 struct task_struct *task, struct pid_entry *p)
1492 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1493 proc_pident_instantiate, task, p);
1496 static int proc_pident_readdir(struct file *filp,
1497 void *dirent, filldir_t filldir,
1498 struct pid_entry *ents, unsigned int nents)
1502 struct dentry *dentry = filp->f_path.dentry;
1503 struct inode *inode = dentry->d_inode;
1504 struct task_struct *task = get_proc_task(inode);
1505 struct pid_entry *p, *last;
1519 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1525 ino = parent_ino(dentry);
1526 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1538 last = &ents[nents - 1];
1540 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1549 put_task_struct(task);
1554 #ifdef CONFIG_SECURITY
1555 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1556 size_t count, loff_t *ppos)
1558 struct inode * inode = file->f_path.dentry->d_inode;
1561 struct task_struct *task = get_proc_task(inode);
1567 if (count > PAGE_SIZE)
1570 if (!(page = __get_free_page(GFP_KERNEL)))
1573 length = security_getprocattr(task,
1574 (char*)file->f_path.dentry->d_name.name,
1575 (void*)page, count);
1577 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1580 put_task_struct(task);
1585 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1586 size_t count, loff_t *ppos)
1588 struct inode * inode = file->f_path.dentry->d_inode;
1591 struct task_struct *task = get_proc_task(inode);
1596 if (count > PAGE_SIZE)
1599 /* No partial writes. */
1605 page = (char*)__get_free_page(GFP_USER);
1610 if (copy_from_user(page, buf, count))
1613 length = security_setprocattr(task,
1614 (char*)file->f_path.dentry->d_name.name,
1615 (void*)page, count);
1617 free_page((unsigned long) page);
1619 put_task_struct(task);
1624 static struct file_operations proc_pid_attr_operations = {
1625 .read = proc_pid_attr_read,
1626 .write = proc_pid_attr_write,
1629 static struct pid_entry attr_dir_stuff[] = {
1630 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1631 REG("prev", S_IRUGO, pid_attr),
1632 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1633 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1634 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1635 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1638 static int proc_attr_dir_readdir(struct file * filp,
1639 void * dirent, filldir_t filldir)
1641 return proc_pident_readdir(filp,dirent,filldir,
1642 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1645 static struct file_operations proc_attr_dir_operations = {
1646 .read = generic_read_dir,
1647 .readdir = proc_attr_dir_readdir,
1650 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1651 struct dentry *dentry, struct nameidata *nd)
1653 return proc_pident_lookup(dir, dentry,
1654 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1657 static struct inode_operations proc_attr_dir_inode_operations = {
1658 .lookup = proc_attr_dir_lookup,
1659 .getattr = pid_getattr,
1660 .setattr = proc_setattr,
1668 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1671 char tmp[PROC_NUMBUF];
1672 sprintf(tmp, "%d", current->tgid);
1673 return vfs_readlink(dentry,buffer,buflen,tmp);
1676 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1678 char tmp[PROC_NUMBUF];
1679 sprintf(tmp, "%d", current->tgid);
1680 return ERR_PTR(vfs_follow_link(nd,tmp));
1683 static struct inode_operations proc_self_inode_operations = {
1684 .readlink = proc_self_readlink,
1685 .follow_link = proc_self_follow_link,
1691 * These are the directory entries in the root directory of /proc
1692 * that properly belong to the /proc filesystem, as they describe
1693 * describe something that is process related.
1695 static struct pid_entry proc_base_stuff[] = {
1696 NOD("self", S_IFLNK|S_IRWXUGO,
1697 &proc_self_inode_operations, NULL, {}),
1701 * Exceptional case: normally we are not allowed to unhash a busy
1702 * directory. In this case, however, we can do it - no aliasing problems
1703 * due to the way we treat inodes.
1705 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1707 struct inode *inode = dentry->d_inode;
1708 struct task_struct *task = get_proc_task(inode);
1710 put_task_struct(task);
1717 static struct dentry_operations proc_base_dentry_operations =
1719 .d_revalidate = proc_base_revalidate,
1720 .d_delete = pid_delete_dentry,
1723 static struct dentry *proc_base_instantiate(struct inode *dir,
1724 struct dentry *dentry, struct task_struct *task, void *ptr)
1726 struct pid_entry *p = ptr;
1727 struct inode *inode;
1728 struct proc_inode *ei;
1729 struct dentry *error = ERR_PTR(-EINVAL);
1731 /* Allocate the inode */
1732 error = ERR_PTR(-ENOMEM);
1733 inode = new_inode(dir->i_sb);
1737 /* Initialize the inode */
1739 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1742 * grab the reference to the task.
1744 ei->pid = get_task_pid(task, PIDTYPE_PID);
1750 inode->i_mode = p->mode;
1751 if (S_ISDIR(inode->i_mode))
1753 if (S_ISLNK(inode->i_mode))
1756 inode->i_op = p->iop;
1758 inode->i_fop = p->fop;
1760 dentry->d_op = &proc_base_dentry_operations;
1761 d_add(dentry, inode);
1770 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1772 struct dentry *error;
1773 struct task_struct *task = get_proc_task(dir);
1774 struct pid_entry *p, *last;
1776 error = ERR_PTR(-ENOENT);
1781 /* Lookup the directory entry */
1782 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1783 for (p = proc_base_stuff; p <= last; p++) {
1784 if (p->len != dentry->d_name.len)
1786 if (!memcmp(dentry->d_name.name, p->name, p->len))
1792 error = proc_base_instantiate(dir, dentry, task, p);
1795 put_task_struct(task);
1800 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1801 struct task_struct *task, struct pid_entry *p)
1803 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1804 proc_base_instantiate, task, p);
1807 #ifdef CONFIG_TASK_IO_ACCOUNTING
1808 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1810 return sprintf(buffer,
1815 "read_bytes: %llu\n"
1816 "write_bytes: %llu\n"
1817 "cancelled_write_bytes: %llu\n",
1818 (unsigned long long)task->rchar,
1819 (unsigned long long)task->wchar,
1820 (unsigned long long)task->syscr,
1821 (unsigned long long)task->syscw,
1822 (unsigned long long)task->ioac.read_bytes,
1823 (unsigned long long)task->ioac.write_bytes,
1824 (unsigned long long)task->ioac.cancelled_write_bytes);
1831 static struct file_operations proc_task_operations;
1832 static struct inode_operations proc_task_inode_operations;
1834 static struct pid_entry tgid_base_stuff[] = {
1835 DIR("task", S_IRUGO|S_IXUGO, task),
1836 DIR("fd", S_IRUSR|S_IXUSR, fd),
1837 INF("environ", S_IRUSR, pid_environ),
1838 INF("auxv", S_IRUSR, pid_auxv),
1839 INF("status", S_IRUGO, pid_status),
1840 INF("cmdline", S_IRUGO, pid_cmdline),
1841 INF("stat", S_IRUGO, tgid_stat),
1842 INF("statm", S_IRUGO, pid_statm),
1843 REG("maps", S_IRUGO, maps),
1845 REG("numa_maps", S_IRUGO, numa_maps),
1847 REG("mem", S_IRUSR|S_IWUSR, mem),
1848 #ifdef CONFIG_SECCOMP
1849 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1854 REG("mounts", S_IRUGO, mounts),
1855 REG("mountstats", S_IRUSR, mountstats),
1857 REG("smaps", S_IRUGO, smaps),
1859 #ifdef CONFIG_SECURITY
1860 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1862 #ifdef CONFIG_KALLSYMS
1863 INF("wchan", S_IRUGO, pid_wchan),
1865 #ifdef CONFIG_SCHEDSTATS
1866 INF("schedstat", S_IRUGO, pid_schedstat),
1868 #ifdef CONFIG_CPUSETS
1869 REG("cpuset", S_IRUGO, cpuset),
1871 INF("oom_score", S_IRUGO, oom_score),
1872 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1873 #ifdef CONFIG_AUDITSYSCALL
1874 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
1876 #ifdef CONFIG_FAULT_INJECTION
1877 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1879 #ifdef CONFIG_TASK_IO_ACCOUNTING
1880 INF("io", S_IRUGO, pid_io_accounting),
1884 static int proc_tgid_base_readdir(struct file * filp,
1885 void * dirent, filldir_t filldir)
1887 return proc_pident_readdir(filp,dirent,filldir,
1888 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1891 static struct file_operations proc_tgid_base_operations = {
1892 .read = generic_read_dir,
1893 .readdir = proc_tgid_base_readdir,
1896 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1897 return proc_pident_lookup(dir, dentry,
1898 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1901 static struct inode_operations proc_tgid_base_inode_operations = {
1902 .lookup = proc_tgid_base_lookup,
1903 .getattr = pid_getattr,
1904 .setattr = proc_setattr,
1908 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1910 * @task: task that should be flushed.
1912 * Looks in the dcache for
1914 * /proc/@tgid/task/@pid
1915 * if either directory is present flushes it and all of it'ts children
1918 * It is safe and reasonable to cache /proc entries for a task until
1919 * that task exits. After that they just clog up the dcache with
1920 * useless entries, possibly causing useful dcache entries to be
1921 * flushed instead. This routine is proved to flush those useless
1922 * dcache entries at process exit time.
1924 * NOTE: This routine is just an optimization so it does not guarantee
1925 * that no dcache entries will exist at process exit time it
1926 * just makes it very unlikely that any will persist.
1928 void proc_flush_task(struct task_struct *task)
1930 struct dentry *dentry, *leader, *dir;
1931 char buf[PROC_NUMBUF];
1935 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1936 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1938 shrink_dcache_parent(dentry);
1943 if (thread_group_leader(task))
1947 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1948 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1953 name.len = strlen(name.name);
1954 dir = d_hash_and_lookup(leader, &name);
1956 goto out_put_leader;
1959 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1960 dentry = d_hash_and_lookup(dir, &name);
1962 shrink_dcache_parent(dentry);
1974 static struct dentry *proc_pid_instantiate(struct inode *dir,
1975 struct dentry * dentry,
1976 struct task_struct *task, void *ptr)
1978 struct dentry *error = ERR_PTR(-ENOENT);
1979 struct inode *inode;
1981 inode = proc_pid_make_inode(dir->i_sb, task);
1985 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1986 inode->i_op = &proc_tgid_base_inode_operations;
1987 inode->i_fop = &proc_tgid_base_operations;
1988 inode->i_flags|=S_IMMUTABLE;
1990 #ifdef CONFIG_SECURITY
1991 inode->i_nlink += 1;
1994 dentry->d_op = &pid_dentry_operations;
1996 d_add(dentry, inode);
1997 /* Close the race of the process dying before we return the dentry */
1998 if (pid_revalidate(dentry, NULL))
2004 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2006 struct dentry *result = ERR_PTR(-ENOENT);
2007 struct task_struct *task;
2010 result = proc_base_lookup(dir, dentry);
2011 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2014 tgid = name_to_int(dentry);
2019 task = find_task_by_pid(tgid);
2021 get_task_struct(task);
2026 result = proc_pid_instantiate(dir, dentry, task, NULL);
2027 put_task_struct(task);
2033 * Find the first task with tgid >= tgid
2036 static struct task_struct *next_tgid(unsigned int tgid)
2038 struct task_struct *task;
2044 pid = find_ge_pid(tgid);
2047 task = pid_task(pid, PIDTYPE_PID);
2048 /* What we to know is if the pid we have find is the
2049 * pid of a thread_group_leader. Testing for task
2050 * being a thread_group_leader is the obvious thing
2051 * todo but there is a window when it fails, due to
2052 * the pid transfer logic in de_thread.
2054 * So we perform the straight forward test of seeing
2055 * if the pid we have found is the pid of a thread
2056 * group leader, and don't worry if the task we have
2057 * found doesn't happen to be a thread group leader.
2058 * As we don't care in the case of readdir.
2060 if (!task || !has_group_leader_pid(task))
2062 get_task_struct(task);
2068 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2070 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2071 struct task_struct *task, int tgid)
2073 char name[PROC_NUMBUF];
2074 int len = snprintf(name, sizeof(name), "%d", tgid);
2075 return proc_fill_cache(filp, dirent, filldir, name, len,
2076 proc_pid_instantiate, task, NULL);
2079 /* for the /proc/ directory itself, after non-process stuff has been done */
2080 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2082 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2083 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2084 struct task_struct *task;
2090 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2091 struct pid_entry *p = &proc_base_stuff[nr];
2092 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2096 tgid = filp->f_pos - TGID_OFFSET;
2097 for (task = next_tgid(tgid);
2099 put_task_struct(task), task = next_tgid(tgid + 1)) {
2101 filp->f_pos = tgid + TGID_OFFSET;
2102 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2103 put_task_struct(task);
2107 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2109 put_task_struct(reaper);
2117 static struct pid_entry tid_base_stuff[] = {
2118 DIR("fd", S_IRUSR|S_IXUSR, fd),
2119 INF("environ", S_IRUSR, pid_environ),
2120 INF("auxv", S_IRUSR, pid_auxv),
2121 INF("status", S_IRUGO, pid_status),
2122 INF("cmdline", S_IRUGO, pid_cmdline),
2123 INF("stat", S_IRUGO, tid_stat),
2124 INF("statm", S_IRUGO, pid_statm),
2125 REG("maps", S_IRUGO, maps),
2127 REG("numa_maps", S_IRUGO, numa_maps),
2129 REG("mem", S_IRUSR|S_IWUSR, mem),
2130 #ifdef CONFIG_SECCOMP
2131 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2136 REG("mounts", S_IRUGO, mounts),
2138 REG("smaps", S_IRUGO, smaps),
2140 #ifdef CONFIG_SECURITY
2141 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2143 #ifdef CONFIG_KALLSYMS
2144 INF("wchan", S_IRUGO, pid_wchan),
2146 #ifdef CONFIG_SCHEDSTATS
2147 INF("schedstat", S_IRUGO, pid_schedstat),
2149 #ifdef CONFIG_CPUSETS
2150 REG("cpuset", S_IRUGO, cpuset),
2152 INF("oom_score", S_IRUGO, oom_score),
2153 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2154 #ifdef CONFIG_AUDITSYSCALL
2155 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2157 #ifdef CONFIG_FAULT_INJECTION
2158 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2162 static int proc_tid_base_readdir(struct file * filp,
2163 void * dirent, filldir_t filldir)
2165 return proc_pident_readdir(filp,dirent,filldir,
2166 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2169 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2170 return proc_pident_lookup(dir, dentry,
2171 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2174 static struct file_operations proc_tid_base_operations = {
2175 .read = generic_read_dir,
2176 .readdir = proc_tid_base_readdir,
2179 static struct inode_operations proc_tid_base_inode_operations = {
2180 .lookup = proc_tid_base_lookup,
2181 .getattr = pid_getattr,
2182 .setattr = proc_setattr,
2185 static struct dentry *proc_task_instantiate(struct inode *dir,
2186 struct dentry *dentry, struct task_struct *task, void *ptr)
2188 struct dentry *error = ERR_PTR(-ENOENT);
2189 struct inode *inode;
2190 inode = proc_pid_make_inode(dir->i_sb, task);
2194 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2195 inode->i_op = &proc_tid_base_inode_operations;
2196 inode->i_fop = &proc_tid_base_operations;
2197 inode->i_flags|=S_IMMUTABLE;
2199 #ifdef CONFIG_SECURITY
2200 inode->i_nlink += 1;
2203 dentry->d_op = &pid_dentry_operations;
2205 d_add(dentry, inode);
2206 /* Close the race of the process dying before we return the dentry */
2207 if (pid_revalidate(dentry, NULL))
2213 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2215 struct dentry *result = ERR_PTR(-ENOENT);
2216 struct task_struct *task;
2217 struct task_struct *leader = get_proc_task(dir);
2223 tid = name_to_int(dentry);
2228 task = find_task_by_pid(tid);
2230 get_task_struct(task);
2234 if (leader->tgid != task->tgid)
2237 result = proc_task_instantiate(dir, dentry, task, NULL);
2239 put_task_struct(task);
2241 put_task_struct(leader);
2247 * Find the first tid of a thread group to return to user space.
2249 * Usually this is just the thread group leader, but if the users
2250 * buffer was too small or there was a seek into the middle of the
2251 * directory we have more work todo.
2253 * In the case of a short read we start with find_task_by_pid.
2255 * In the case of a seek we start with the leader and walk nr
2258 static struct task_struct *first_tid(struct task_struct *leader,
2261 struct task_struct *pos;
2264 /* Attempt to start with the pid of a thread */
2265 if (tid && (nr > 0)) {
2266 pos = find_task_by_pid(tid);
2267 if (pos && (pos->group_leader == leader))
2271 /* If nr exceeds the number of threads there is nothing todo */
2273 if (nr && nr >= get_nr_threads(leader))
2276 /* If we haven't found our starting place yet start
2277 * with the leader and walk nr threads forward.
2279 for (pos = leader; nr > 0; --nr) {
2280 pos = next_thread(pos);
2281 if (pos == leader) {
2287 get_task_struct(pos);
2294 * Find the next thread in the thread list.
2295 * Return NULL if there is an error or no next thread.
2297 * The reference to the input task_struct is released.
2299 static struct task_struct *next_tid(struct task_struct *start)
2301 struct task_struct *pos = NULL;
2303 if (pid_alive(start)) {
2304 pos = next_thread(start);
2305 if (thread_group_leader(pos))
2308 get_task_struct(pos);
2311 put_task_struct(start);
2315 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2316 struct task_struct *task, int tid)
2318 char name[PROC_NUMBUF];
2319 int len = snprintf(name, sizeof(name), "%d", tid);
2320 return proc_fill_cache(filp, dirent, filldir, name, len,
2321 proc_task_instantiate, task, NULL);
2324 /* for the /proc/TGID/task/ directories */
2325 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2327 struct dentry *dentry = filp->f_path.dentry;
2328 struct inode *inode = dentry->d_inode;
2329 struct task_struct *leader = get_proc_task(inode);
2330 struct task_struct *task;
2331 int retval = -ENOENT;
2334 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2343 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2348 ino = parent_ino(dentry);
2349 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2355 /* f_version caches the tgid value that the last readdir call couldn't
2356 * return. lseek aka telldir automagically resets f_version to 0.
2358 tid = filp->f_version;
2359 filp->f_version = 0;
2360 for (task = first_tid(leader, tid, pos - 2);
2362 task = next_tid(task), pos++) {
2364 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2365 /* returning this tgid failed, save it as the first
2366 * pid for the next readir call */
2367 filp->f_version = tid;
2368 put_task_struct(task);
2374 put_task_struct(leader);
2379 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2381 struct inode *inode = dentry->d_inode;
2382 struct task_struct *p = get_proc_task(inode);
2383 generic_fillattr(inode, stat);
2387 stat->nlink += get_nr_threads(p);
2395 static struct inode_operations proc_task_inode_operations = {
2396 .lookup = proc_task_lookup,
2397 .getattr = proc_task_getattr,
2398 .setattr = proc_setattr,
2401 static struct file_operations proc_task_operations = {
2402 .read = generic_read_dir,
2403 .readdir = proc_task_readdir,