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 const struct inode_operations *iop;
97 const 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 const 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;
375 ns = task->nsproxy->mnt_ns;
380 put_task_struct(task);
385 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
387 file->private_data = &p->m;
388 ret = seq_open(file, &mounts_op);
391 p->event = ns->event;
401 static int mounts_release(struct inode *inode, struct file *file)
403 struct seq_file *m = file->private_data;
404 struct mnt_namespace *ns = m->private;
406 return seq_release(inode, file);
409 static unsigned mounts_poll(struct file *file, poll_table *wait)
411 struct proc_mounts *p = file->private_data;
412 struct mnt_namespace *ns = p->m.private;
415 poll_wait(file, &ns->poll, wait);
417 spin_lock(&vfsmount_lock);
418 if (p->event != ns->event) {
419 p->event = ns->event;
422 spin_unlock(&vfsmount_lock);
427 static const struct file_operations proc_mounts_operations = {
431 .release = mounts_release,
435 extern struct seq_operations mountstats_op;
436 static int mountstats_open(struct inode *inode, struct file *file)
438 int ret = seq_open(file, &mountstats_op);
441 struct seq_file *m = file->private_data;
442 struct mnt_namespace *mnt_ns = NULL;
443 struct task_struct *task = get_proc_task(inode);
448 mnt_ns = task->nsproxy->mnt_ns;
452 put_task_struct(task);
458 seq_release(inode, file);
465 static const struct file_operations proc_mountstats_operations = {
466 .open = mountstats_open,
469 .release = mounts_release,
472 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
474 static ssize_t proc_info_read(struct file * file, char __user * buf,
475 size_t count, loff_t *ppos)
477 struct inode * inode = file->f_path.dentry->d_inode;
480 struct task_struct *task = get_proc_task(inode);
486 if (count > PROC_BLOCK_SIZE)
487 count = PROC_BLOCK_SIZE;
490 if (!(page = __get_free_page(GFP_KERNEL)))
493 length = PROC_I(inode)->op.proc_read(task, (char*)page);
496 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
499 put_task_struct(task);
504 static const struct file_operations proc_info_file_operations = {
505 .read = proc_info_read,
508 static int mem_open(struct inode* inode, struct file* file)
510 file->private_data = (void*)((long)current->self_exec_id);
514 static ssize_t mem_read(struct file * file, char __user * buf,
515 size_t count, loff_t *ppos)
517 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
519 unsigned long src = *ppos;
521 struct mm_struct *mm;
526 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
530 page = (char *)__get_free_page(GFP_USER);
536 mm = get_task_mm(task);
542 if (file->private_data != (void*)((long)current->self_exec_id))
548 int this_len, retval;
550 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
551 retval = access_process_vm(task, src, page, this_len, 0);
552 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
558 if (copy_to_user(buf, page, retval)) {
573 free_page((unsigned long) page);
575 put_task_struct(task);
580 #define mem_write NULL
583 /* This is a security hazard */
584 static ssize_t mem_write(struct file * file, const char __user *buf,
585 size_t count, loff_t *ppos)
589 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
590 unsigned long dst = *ppos;
596 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
600 page = (char *)__get_free_page(GFP_USER);
606 int this_len, retval;
608 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
609 if (copy_from_user(page, buf, this_len)) {
613 retval = access_process_vm(task, dst, page, this_len, 1);
625 free_page((unsigned long) page);
627 put_task_struct(task);
633 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
637 file->f_pos = offset;
640 file->f_pos += offset;
645 force_successful_syscall_return();
649 static const struct file_operations proc_mem_operations = {
656 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
657 size_t count, loff_t *ppos)
659 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
660 char buffer[PROC_NUMBUF];
663 loff_t __ppos = *ppos;
667 oom_adjust = task->oomkilladj;
668 put_task_struct(task);
670 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
673 if (count > len-__ppos)
675 if (copy_to_user(buf, buffer + __ppos, count))
677 *ppos = __ppos + count;
681 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
682 size_t count, loff_t *ppos)
684 struct task_struct *task;
685 char buffer[PROC_NUMBUF], *end;
688 memset(buffer, 0, sizeof(buffer));
689 if (count > sizeof(buffer) - 1)
690 count = sizeof(buffer) - 1;
691 if (copy_from_user(buffer, buf, count))
693 oom_adjust = simple_strtol(buffer, &end, 0);
694 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
695 oom_adjust != OOM_DISABLE)
699 task = get_proc_task(file->f_path.dentry->d_inode);
702 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
703 put_task_struct(task);
706 task->oomkilladj = oom_adjust;
707 put_task_struct(task);
708 if (end - buffer == 0)
713 static const struct file_operations proc_oom_adjust_operations = {
714 .read = oom_adjust_read,
715 .write = oom_adjust_write,
718 #ifdef CONFIG_AUDITSYSCALL
720 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
721 size_t count, loff_t *ppos)
723 struct inode * inode = file->f_path.dentry->d_inode;
724 struct task_struct *task = get_proc_task(inode);
726 char tmpbuf[TMPBUFLEN];
730 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
731 audit_get_loginuid(task->audit_context));
732 put_task_struct(task);
733 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
736 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
737 size_t count, loff_t *ppos)
739 struct inode * inode = file->f_path.dentry->d_inode;
744 if (!capable(CAP_AUDIT_CONTROL))
747 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
750 if (count >= PAGE_SIZE)
751 count = PAGE_SIZE - 1;
754 /* No partial writes. */
757 page = (char*)__get_free_page(GFP_USER);
761 if (copy_from_user(page, buf, count))
765 loginuid = simple_strtoul(page, &tmp, 10);
771 length = audit_set_loginuid(current, loginuid);
772 if (likely(length == 0))
776 free_page((unsigned long) page);
780 static const struct file_operations proc_loginuid_operations = {
781 .read = proc_loginuid_read,
782 .write = proc_loginuid_write,
786 #ifdef CONFIG_SECCOMP
787 static ssize_t seccomp_read(struct file *file, char __user *buf,
788 size_t count, loff_t *ppos)
790 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
792 loff_t __ppos = *ppos;
797 /* no need to print the trailing zero, so use only len */
798 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
799 put_task_struct(tsk);
802 if (count > len - __ppos)
803 count = len - __ppos;
804 if (copy_to_user(buf, __buf + __ppos, count))
806 *ppos = __ppos + count;
810 static ssize_t seccomp_write(struct file *file, const char __user *buf,
811 size_t count, loff_t *ppos)
813 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
814 char __buf[20], *end;
815 unsigned int seccomp_mode;
822 /* can set it only once to be even more secure */
824 if (unlikely(tsk->seccomp.mode))
828 memset(__buf, 0, sizeof(__buf));
829 count = min(count, sizeof(__buf) - 1);
830 if (copy_from_user(__buf, buf, count))
833 seccomp_mode = simple_strtoul(__buf, &end, 0);
837 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
838 tsk->seccomp.mode = seccomp_mode;
839 set_tsk_thread_flag(tsk, TIF_SECCOMP);
843 if (unlikely(!(end - __buf)))
845 result = end - __buf;
847 put_task_struct(tsk);
852 static const struct file_operations proc_seccomp_operations = {
853 .read = seccomp_read,
854 .write = seccomp_write,
856 #endif /* CONFIG_SECCOMP */
858 #ifdef CONFIG_FAULT_INJECTION
859 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
860 size_t count, loff_t *ppos)
862 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
863 char buffer[PROC_NUMBUF];
866 loff_t __ppos = *ppos;
870 make_it_fail = task->make_it_fail;
871 put_task_struct(task);
873 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
876 if (count > len-__ppos)
878 if (copy_to_user(buf, buffer + __ppos, count))
880 *ppos = __ppos + count;
884 static ssize_t proc_fault_inject_write(struct file * file,
885 const char __user * buf, size_t count, loff_t *ppos)
887 struct task_struct *task;
888 char buffer[PROC_NUMBUF], *end;
891 if (!capable(CAP_SYS_RESOURCE))
893 memset(buffer, 0, sizeof(buffer));
894 if (count > sizeof(buffer) - 1)
895 count = sizeof(buffer) - 1;
896 if (copy_from_user(buffer, buf, count))
898 make_it_fail = simple_strtol(buffer, &end, 0);
901 task = get_proc_task(file->f_dentry->d_inode);
904 task->make_it_fail = make_it_fail;
905 put_task_struct(task);
906 if (end - buffer == 0)
911 static const struct file_operations proc_fault_inject_operations = {
912 .read = proc_fault_inject_read,
913 .write = proc_fault_inject_write,
917 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
919 struct inode *inode = dentry->d_inode;
922 /* We don't need a base pointer in the /proc filesystem */
925 /* Are we allowed to snoop on the tasks file descriptors? */
926 if (!proc_fd_access_allowed(inode))
929 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
930 nd->last_type = LAST_BIND;
932 return ERR_PTR(error);
935 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
936 char __user *buffer, int buflen)
938 struct inode * inode;
939 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
945 inode = dentry->d_inode;
946 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
950 len = tmp + PAGE_SIZE - 1 - path;
954 if (copy_to_user(buffer, path, len))
957 free_page((unsigned long)tmp);
961 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
964 struct inode *inode = dentry->d_inode;
966 struct vfsmount *mnt = NULL;
968 /* Are we allowed to snoop on the tasks file descriptors? */
969 if (!proc_fd_access_allowed(inode))
972 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
976 error = do_proc_readlink(de, mnt, buffer, buflen);
983 static const struct inode_operations proc_pid_link_inode_operations = {
984 .readlink = proc_pid_readlink,
985 .follow_link = proc_pid_follow_link,
986 .setattr = proc_setattr,
990 /* building an inode */
992 static int task_dumpable(struct task_struct *task)
995 struct mm_struct *mm;
1000 dumpable = mm->dumpable;
1008 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1010 struct inode * inode;
1011 struct proc_inode *ei;
1013 /* We need a new inode */
1015 inode = new_inode(sb);
1021 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1022 inode->i_op = &proc_def_inode_operations;
1025 * grab the reference to task.
1027 ei->pid = get_task_pid(task, PIDTYPE_PID);
1033 if (task_dumpable(task)) {
1034 inode->i_uid = task->euid;
1035 inode->i_gid = task->egid;
1037 security_task_to_inode(task, inode);
1047 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1049 struct inode *inode = dentry->d_inode;
1050 struct task_struct *task;
1051 generic_fillattr(inode, stat);
1056 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1058 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1059 task_dumpable(task)) {
1060 stat->uid = task->euid;
1061 stat->gid = task->egid;
1071 * Exceptional case: normally we are not allowed to unhash a busy
1072 * directory. In this case, however, we can do it - no aliasing problems
1073 * due to the way we treat inodes.
1075 * Rewrite the inode's ownerships here because the owning task may have
1076 * performed a setuid(), etc.
1078 * Before the /proc/pid/status file was created the only way to read
1079 * the effective uid of a /process was to stat /proc/pid. Reading
1080 * /proc/pid/status is slow enough that procps and other packages
1081 * kept stating /proc/pid. To keep the rules in /proc simple I have
1082 * made this apply to all per process world readable and executable
1085 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1087 struct inode *inode = dentry->d_inode;
1088 struct task_struct *task = get_proc_task(inode);
1090 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1091 task_dumpable(task)) {
1092 inode->i_uid = task->euid;
1093 inode->i_gid = task->egid;
1098 inode->i_mode &= ~(S_ISUID | S_ISGID);
1099 security_task_to_inode(task, inode);
1100 put_task_struct(task);
1107 static int pid_delete_dentry(struct dentry * dentry)
1109 /* Is the task we represent dead?
1110 * If so, then don't put the dentry on the lru list,
1111 * kill it immediately.
1113 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1116 static struct dentry_operations pid_dentry_operations =
1118 .d_revalidate = pid_revalidate,
1119 .d_delete = pid_delete_dentry,
1124 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1127 * Fill a directory entry.
1129 * If possible create the dcache entry and derive our inode number and
1130 * file type from dcache entry.
1132 * Since all of the proc inode numbers are dynamically generated, the inode
1133 * numbers do not exist until the inode is cache. This means creating the
1134 * the dcache entry in readdir is necessary to keep the inode numbers
1135 * reported by readdir in sync with the inode numbers reported
1138 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1139 char *name, int len,
1140 instantiate_t instantiate, struct task_struct *task, void *ptr)
1142 struct dentry *child, *dir = filp->f_path.dentry;
1143 struct inode *inode;
1146 unsigned type = DT_UNKNOWN;
1150 qname.hash = full_name_hash(name, len);
1152 child = d_lookup(dir, &qname);
1155 new = d_alloc(dir, &qname);
1157 child = instantiate(dir->d_inode, new, task, ptr);
1164 if (!child || IS_ERR(child) || !child->d_inode)
1165 goto end_instantiate;
1166 inode = child->d_inode;
1169 type = inode->i_mode >> 12;
1174 ino = find_inode_number(dir, &qname);
1177 return filldir(dirent, name, len, filp->f_pos, ino, type);
1180 static unsigned name_to_int(struct dentry *dentry)
1182 const char *name = dentry->d_name.name;
1183 int len = dentry->d_name.len;
1186 if (len > 1 && *name == '0')
1189 unsigned c = *name++ - '0';
1192 if (n >= (~0U-9)/10)
1202 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1204 struct task_struct *task = get_proc_task(inode);
1205 struct files_struct *files = NULL;
1207 int fd = proc_fd(inode);
1210 files = get_files_struct(task);
1211 put_task_struct(task);
1215 * We are not taking a ref to the file structure, so we must
1218 spin_lock(&files->file_lock);
1219 file = fcheck_files(files, fd);
1221 *mnt = mntget(file->f_path.mnt);
1222 *dentry = dget(file->f_path.dentry);
1223 spin_unlock(&files->file_lock);
1224 put_files_struct(files);
1227 spin_unlock(&files->file_lock);
1228 put_files_struct(files);
1233 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1235 struct inode *inode = dentry->d_inode;
1236 struct task_struct *task = get_proc_task(inode);
1237 int fd = proc_fd(inode);
1238 struct files_struct *files;
1241 files = get_files_struct(task);
1244 if (fcheck_files(files, fd)) {
1246 put_files_struct(files);
1247 if (task_dumpable(task)) {
1248 inode->i_uid = task->euid;
1249 inode->i_gid = task->egid;
1254 inode->i_mode &= ~(S_ISUID | S_ISGID);
1255 security_task_to_inode(task, inode);
1256 put_task_struct(task);
1260 put_files_struct(files);
1262 put_task_struct(task);
1268 static struct dentry_operations tid_fd_dentry_operations =
1270 .d_revalidate = tid_fd_revalidate,
1271 .d_delete = pid_delete_dentry,
1274 static struct dentry *proc_fd_instantiate(struct inode *dir,
1275 struct dentry *dentry, struct task_struct *task, void *ptr)
1277 unsigned fd = *(unsigned *)ptr;
1279 struct files_struct *files;
1280 struct inode *inode;
1281 struct proc_inode *ei;
1282 struct dentry *error = ERR_PTR(-ENOENT);
1284 inode = proc_pid_make_inode(dir->i_sb, task);
1289 files = get_files_struct(task);
1292 inode->i_mode = S_IFLNK;
1295 * We are not taking a ref to the file structure, so we must
1298 spin_lock(&files->file_lock);
1299 file = fcheck_files(files, fd);
1302 if (file->f_mode & 1)
1303 inode->i_mode |= S_IRUSR | S_IXUSR;
1304 if (file->f_mode & 2)
1305 inode->i_mode |= S_IWUSR | S_IXUSR;
1306 spin_unlock(&files->file_lock);
1307 put_files_struct(files);
1309 inode->i_op = &proc_pid_link_inode_operations;
1311 ei->op.proc_get_link = proc_fd_link;
1312 dentry->d_op = &tid_fd_dentry_operations;
1313 d_add(dentry, inode);
1314 /* Close the race of the process dying before we return the dentry */
1315 if (tid_fd_revalidate(dentry, NULL))
1321 spin_unlock(&files->file_lock);
1322 put_files_struct(files);
1328 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1330 struct task_struct *task = get_proc_task(dir);
1331 unsigned fd = name_to_int(dentry);
1332 struct dentry *result = ERR_PTR(-ENOENT);
1339 result = proc_fd_instantiate(dir, dentry, task, &fd);
1341 put_task_struct(task);
1346 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1347 struct task_struct *task, int fd)
1349 char name[PROC_NUMBUF];
1350 int len = snprintf(name, sizeof(name), "%d", fd);
1351 return proc_fill_cache(filp, dirent, filldir, name, len,
1352 proc_fd_instantiate, task, &fd);
1355 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1357 struct dentry *dentry = filp->f_path.dentry;
1358 struct inode *inode = dentry->d_inode;
1359 struct task_struct *p = get_proc_task(inode);
1360 unsigned int fd, tid, ino;
1362 struct files_struct * files;
1363 struct fdtable *fdt;
1374 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1378 ino = parent_ino(dentry);
1379 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1383 files = get_files_struct(p);
1387 fdt = files_fdtable(files);
1388 for (fd = filp->f_pos-2;
1390 fd++, filp->f_pos++) {
1392 if (!fcheck_files(files, fd))
1396 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1403 put_files_struct(files);
1411 static const struct file_operations proc_fd_operations = {
1412 .read = generic_read_dir,
1413 .readdir = proc_readfd,
1417 * proc directories can do almost nothing..
1419 static const struct inode_operations proc_fd_inode_operations = {
1420 .lookup = proc_lookupfd,
1421 .setattr = proc_setattr,
1424 static struct dentry *proc_pident_instantiate(struct inode *dir,
1425 struct dentry *dentry, struct task_struct *task, void *ptr)
1427 struct pid_entry *p = ptr;
1428 struct inode *inode;
1429 struct proc_inode *ei;
1430 struct dentry *error = ERR_PTR(-EINVAL);
1432 inode = proc_pid_make_inode(dir->i_sb, task);
1437 inode->i_mode = p->mode;
1438 if (S_ISDIR(inode->i_mode))
1439 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1441 inode->i_op = p->iop;
1443 inode->i_fop = p->fop;
1445 dentry->d_op = &pid_dentry_operations;
1446 d_add(dentry, inode);
1447 /* Close the race of the process dying before we return the dentry */
1448 if (pid_revalidate(dentry, NULL))
1454 static struct dentry *proc_pident_lookup(struct inode *dir,
1455 struct dentry *dentry,
1456 struct pid_entry *ents,
1459 struct inode *inode;
1460 struct dentry *error;
1461 struct task_struct *task = get_proc_task(dir);
1462 struct pid_entry *p, *last;
1464 error = ERR_PTR(-ENOENT);
1471 * Yes, it does not scale. And it should not. Don't add
1472 * new entries into /proc/<tgid>/ without very good reasons.
1474 last = &ents[nents - 1];
1475 for (p = ents; p <= last; p++) {
1476 if (p->len != dentry->d_name.len)
1478 if (!memcmp(dentry->d_name.name, p->name, p->len))
1484 error = proc_pident_instantiate(dir, dentry, task, p);
1486 put_task_struct(task);
1491 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1492 struct task_struct *task, struct pid_entry *p)
1494 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1495 proc_pident_instantiate, task, p);
1498 static int proc_pident_readdir(struct file *filp,
1499 void *dirent, filldir_t filldir,
1500 struct pid_entry *ents, unsigned int nents)
1504 struct dentry *dentry = filp->f_path.dentry;
1505 struct inode *inode = dentry->d_inode;
1506 struct task_struct *task = get_proc_task(inode);
1507 struct pid_entry *p, *last;
1521 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1527 ino = parent_ino(dentry);
1528 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1540 last = &ents[nents - 1];
1542 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1551 put_task_struct(task);
1556 #ifdef CONFIG_SECURITY
1557 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1558 size_t count, loff_t *ppos)
1560 struct inode * inode = file->f_path.dentry->d_inode;
1563 struct task_struct *task = get_proc_task(inode);
1568 length = security_getprocattr(task,
1569 (char*)file->f_path.dentry->d_name.name,
1571 put_task_struct(task);
1573 length = simple_read_from_buffer(buf, count, ppos, p, length);
1578 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1579 size_t count, loff_t *ppos)
1581 struct inode * inode = file->f_path.dentry->d_inode;
1584 struct task_struct *task = get_proc_task(inode);
1589 if (count > PAGE_SIZE)
1592 /* No partial writes. */
1598 page = (char*)__get_free_page(GFP_USER);
1603 if (copy_from_user(page, buf, count))
1606 length = security_setprocattr(task,
1607 (char*)file->f_path.dentry->d_name.name,
1608 (void*)page, count);
1610 free_page((unsigned long) page);
1612 put_task_struct(task);
1617 static const struct file_operations proc_pid_attr_operations = {
1618 .read = proc_pid_attr_read,
1619 .write = proc_pid_attr_write,
1622 static struct pid_entry attr_dir_stuff[] = {
1623 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1624 REG("prev", S_IRUGO, pid_attr),
1625 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1626 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1627 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1628 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1631 static int proc_attr_dir_readdir(struct file * filp,
1632 void * dirent, filldir_t filldir)
1634 return proc_pident_readdir(filp,dirent,filldir,
1635 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1638 static const struct file_operations proc_attr_dir_operations = {
1639 .read = generic_read_dir,
1640 .readdir = proc_attr_dir_readdir,
1643 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1644 struct dentry *dentry, struct nameidata *nd)
1646 return proc_pident_lookup(dir, dentry,
1647 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1650 static const struct inode_operations proc_attr_dir_inode_operations = {
1651 .lookup = proc_attr_dir_lookup,
1652 .getattr = pid_getattr,
1653 .setattr = proc_setattr,
1661 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1664 char tmp[PROC_NUMBUF];
1665 sprintf(tmp, "%d", current->tgid);
1666 return vfs_readlink(dentry,buffer,buflen,tmp);
1669 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1671 char tmp[PROC_NUMBUF];
1672 sprintf(tmp, "%d", current->tgid);
1673 return ERR_PTR(vfs_follow_link(nd,tmp));
1676 static const struct inode_operations proc_self_inode_operations = {
1677 .readlink = proc_self_readlink,
1678 .follow_link = proc_self_follow_link,
1684 * These are the directory entries in the root directory of /proc
1685 * that properly belong to the /proc filesystem, as they describe
1686 * describe something that is process related.
1688 static struct pid_entry proc_base_stuff[] = {
1689 NOD("self", S_IFLNK|S_IRWXUGO,
1690 &proc_self_inode_operations, NULL, {}),
1694 * Exceptional case: normally we are not allowed to unhash a busy
1695 * directory. In this case, however, we can do it - no aliasing problems
1696 * due to the way we treat inodes.
1698 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1700 struct inode *inode = dentry->d_inode;
1701 struct task_struct *task = get_proc_task(inode);
1703 put_task_struct(task);
1710 static struct dentry_operations proc_base_dentry_operations =
1712 .d_revalidate = proc_base_revalidate,
1713 .d_delete = pid_delete_dentry,
1716 static struct dentry *proc_base_instantiate(struct inode *dir,
1717 struct dentry *dentry, struct task_struct *task, void *ptr)
1719 struct pid_entry *p = ptr;
1720 struct inode *inode;
1721 struct proc_inode *ei;
1722 struct dentry *error = ERR_PTR(-EINVAL);
1724 /* Allocate the inode */
1725 error = ERR_PTR(-ENOMEM);
1726 inode = new_inode(dir->i_sb);
1730 /* Initialize the inode */
1732 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1735 * grab the reference to the task.
1737 ei->pid = get_task_pid(task, PIDTYPE_PID);
1743 inode->i_mode = p->mode;
1744 if (S_ISDIR(inode->i_mode))
1746 if (S_ISLNK(inode->i_mode))
1749 inode->i_op = p->iop;
1751 inode->i_fop = p->fop;
1753 dentry->d_op = &proc_base_dentry_operations;
1754 d_add(dentry, inode);
1763 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1765 struct dentry *error;
1766 struct task_struct *task = get_proc_task(dir);
1767 struct pid_entry *p, *last;
1769 error = ERR_PTR(-ENOENT);
1774 /* Lookup the directory entry */
1775 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1776 for (p = proc_base_stuff; p <= last; p++) {
1777 if (p->len != dentry->d_name.len)
1779 if (!memcmp(dentry->d_name.name, p->name, p->len))
1785 error = proc_base_instantiate(dir, dentry, task, p);
1788 put_task_struct(task);
1793 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1794 struct task_struct *task, struct pid_entry *p)
1796 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1797 proc_base_instantiate, task, p);
1800 #ifdef CONFIG_TASK_IO_ACCOUNTING
1801 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1803 return sprintf(buffer,
1804 #ifdef CONFIG_TASK_XACCT
1810 "read_bytes: %llu\n"
1811 "write_bytes: %llu\n"
1812 "cancelled_write_bytes: %llu\n",
1813 #ifdef CONFIG_TASK_XACCT
1814 (unsigned long long)task->rchar,
1815 (unsigned long long)task->wchar,
1816 (unsigned long long)task->syscr,
1817 (unsigned long long)task->syscw,
1819 (unsigned long long)task->ioac.read_bytes,
1820 (unsigned long long)task->ioac.write_bytes,
1821 (unsigned long long)task->ioac.cancelled_write_bytes);
1828 static const struct file_operations proc_task_operations;
1829 static const struct inode_operations proc_task_inode_operations;
1831 static struct pid_entry tgid_base_stuff[] = {
1832 DIR("task", S_IRUGO|S_IXUGO, task),
1833 DIR("fd", S_IRUSR|S_IXUSR, fd),
1834 INF("environ", S_IRUSR, pid_environ),
1835 INF("auxv", S_IRUSR, pid_auxv),
1836 INF("status", S_IRUGO, pid_status),
1837 INF("cmdline", S_IRUGO, pid_cmdline),
1838 INF("stat", S_IRUGO, tgid_stat),
1839 INF("statm", S_IRUGO, pid_statm),
1840 REG("maps", S_IRUGO, maps),
1842 REG("numa_maps", S_IRUGO, numa_maps),
1844 REG("mem", S_IRUSR|S_IWUSR, mem),
1845 #ifdef CONFIG_SECCOMP
1846 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1851 REG("mounts", S_IRUGO, mounts),
1852 REG("mountstats", S_IRUSR, mountstats),
1854 REG("smaps", S_IRUGO, smaps),
1856 #ifdef CONFIG_SECURITY
1857 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1859 #ifdef CONFIG_KALLSYMS
1860 INF("wchan", S_IRUGO, pid_wchan),
1862 #ifdef CONFIG_SCHEDSTATS
1863 INF("schedstat", S_IRUGO, pid_schedstat),
1865 #ifdef CONFIG_CPUSETS
1866 REG("cpuset", S_IRUGO, cpuset),
1868 INF("oom_score", S_IRUGO, oom_score),
1869 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1870 #ifdef CONFIG_AUDITSYSCALL
1871 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
1873 #ifdef CONFIG_FAULT_INJECTION
1874 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1876 #ifdef CONFIG_TASK_IO_ACCOUNTING
1877 INF("io", S_IRUGO, pid_io_accounting),
1881 static int proc_tgid_base_readdir(struct file * filp,
1882 void * dirent, filldir_t filldir)
1884 return proc_pident_readdir(filp,dirent,filldir,
1885 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1888 static const struct file_operations proc_tgid_base_operations = {
1889 .read = generic_read_dir,
1890 .readdir = proc_tgid_base_readdir,
1893 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1894 return proc_pident_lookup(dir, dentry,
1895 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1898 static const struct inode_operations proc_tgid_base_inode_operations = {
1899 .lookup = proc_tgid_base_lookup,
1900 .getattr = pid_getattr,
1901 .setattr = proc_setattr,
1905 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1907 * @task: task that should be flushed.
1909 * Looks in the dcache for
1911 * /proc/@tgid/task/@pid
1912 * if either directory is present flushes it and all of it'ts children
1915 * It is safe and reasonable to cache /proc entries for a task until
1916 * that task exits. After that they just clog up the dcache with
1917 * useless entries, possibly causing useful dcache entries to be
1918 * flushed instead. This routine is proved to flush those useless
1919 * dcache entries at process exit time.
1921 * NOTE: This routine is just an optimization so it does not guarantee
1922 * that no dcache entries will exist at process exit time it
1923 * just makes it very unlikely that any will persist.
1925 void proc_flush_task(struct task_struct *task)
1927 struct dentry *dentry, *leader, *dir;
1928 char buf[PROC_NUMBUF];
1932 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1933 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1935 shrink_dcache_parent(dentry);
1940 if (thread_group_leader(task))
1944 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1945 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1950 name.len = strlen(name.name);
1951 dir = d_hash_and_lookup(leader, &name);
1953 goto out_put_leader;
1956 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1957 dentry = d_hash_and_lookup(dir, &name);
1959 shrink_dcache_parent(dentry);
1971 static struct dentry *proc_pid_instantiate(struct inode *dir,
1972 struct dentry * dentry,
1973 struct task_struct *task, void *ptr)
1975 struct dentry *error = ERR_PTR(-ENOENT);
1976 struct inode *inode;
1978 inode = proc_pid_make_inode(dir->i_sb, task);
1982 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1983 inode->i_op = &proc_tgid_base_inode_operations;
1984 inode->i_fop = &proc_tgid_base_operations;
1985 inode->i_flags|=S_IMMUTABLE;
1987 #ifdef CONFIG_SECURITY
1988 inode->i_nlink += 1;
1991 dentry->d_op = &pid_dentry_operations;
1993 d_add(dentry, inode);
1994 /* Close the race of the process dying before we return the dentry */
1995 if (pid_revalidate(dentry, NULL))
2001 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2003 struct dentry *result = ERR_PTR(-ENOENT);
2004 struct task_struct *task;
2007 result = proc_base_lookup(dir, dentry);
2008 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2011 tgid = name_to_int(dentry);
2016 task = find_task_by_pid(tgid);
2018 get_task_struct(task);
2023 result = proc_pid_instantiate(dir, dentry, task, NULL);
2024 put_task_struct(task);
2030 * Find the first task with tgid >= tgid
2033 static struct task_struct *next_tgid(unsigned int tgid)
2035 struct task_struct *task;
2041 pid = find_ge_pid(tgid);
2044 task = pid_task(pid, PIDTYPE_PID);
2045 /* What we to know is if the pid we have find is the
2046 * pid of a thread_group_leader. Testing for task
2047 * being a thread_group_leader is the obvious thing
2048 * todo but there is a window when it fails, due to
2049 * the pid transfer logic in de_thread.
2051 * So we perform the straight forward test of seeing
2052 * if the pid we have found is the pid of a thread
2053 * group leader, and don't worry if the task we have
2054 * found doesn't happen to be a thread group leader.
2055 * As we don't care in the case of readdir.
2057 if (!task || !has_group_leader_pid(task))
2059 get_task_struct(task);
2065 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2067 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2068 struct task_struct *task, int tgid)
2070 char name[PROC_NUMBUF];
2071 int len = snprintf(name, sizeof(name), "%d", tgid);
2072 return proc_fill_cache(filp, dirent, filldir, name, len,
2073 proc_pid_instantiate, task, NULL);
2076 /* for the /proc/ directory itself, after non-process stuff has been done */
2077 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2079 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2080 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2081 struct task_struct *task;
2087 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2088 struct pid_entry *p = &proc_base_stuff[nr];
2089 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2093 tgid = filp->f_pos - TGID_OFFSET;
2094 for (task = next_tgid(tgid);
2096 put_task_struct(task), task = next_tgid(tgid + 1)) {
2098 filp->f_pos = tgid + TGID_OFFSET;
2099 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2100 put_task_struct(task);
2104 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2106 put_task_struct(reaper);
2114 static struct pid_entry tid_base_stuff[] = {
2115 DIR("fd", S_IRUSR|S_IXUSR, fd),
2116 INF("environ", S_IRUSR, pid_environ),
2117 INF("auxv", S_IRUSR, pid_auxv),
2118 INF("status", S_IRUGO, pid_status),
2119 INF("cmdline", S_IRUGO, pid_cmdline),
2120 INF("stat", S_IRUGO, tid_stat),
2121 INF("statm", S_IRUGO, pid_statm),
2122 REG("maps", S_IRUGO, maps),
2124 REG("numa_maps", S_IRUGO, numa_maps),
2126 REG("mem", S_IRUSR|S_IWUSR, mem),
2127 #ifdef CONFIG_SECCOMP
2128 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2133 REG("mounts", S_IRUGO, mounts),
2135 REG("smaps", S_IRUGO, smaps),
2137 #ifdef CONFIG_SECURITY
2138 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2140 #ifdef CONFIG_KALLSYMS
2141 INF("wchan", S_IRUGO, pid_wchan),
2143 #ifdef CONFIG_SCHEDSTATS
2144 INF("schedstat", S_IRUGO, pid_schedstat),
2146 #ifdef CONFIG_CPUSETS
2147 REG("cpuset", S_IRUGO, cpuset),
2149 INF("oom_score", S_IRUGO, oom_score),
2150 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2151 #ifdef CONFIG_AUDITSYSCALL
2152 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2154 #ifdef CONFIG_FAULT_INJECTION
2155 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2159 static int proc_tid_base_readdir(struct file * filp,
2160 void * dirent, filldir_t filldir)
2162 return proc_pident_readdir(filp,dirent,filldir,
2163 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2166 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2167 return proc_pident_lookup(dir, dentry,
2168 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2171 static const struct file_operations proc_tid_base_operations = {
2172 .read = generic_read_dir,
2173 .readdir = proc_tid_base_readdir,
2176 static const struct inode_operations proc_tid_base_inode_operations = {
2177 .lookup = proc_tid_base_lookup,
2178 .getattr = pid_getattr,
2179 .setattr = proc_setattr,
2182 static struct dentry *proc_task_instantiate(struct inode *dir,
2183 struct dentry *dentry, struct task_struct *task, void *ptr)
2185 struct dentry *error = ERR_PTR(-ENOENT);
2186 struct inode *inode;
2187 inode = proc_pid_make_inode(dir->i_sb, task);
2191 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2192 inode->i_op = &proc_tid_base_inode_operations;
2193 inode->i_fop = &proc_tid_base_operations;
2194 inode->i_flags|=S_IMMUTABLE;
2196 #ifdef CONFIG_SECURITY
2197 inode->i_nlink += 1;
2200 dentry->d_op = &pid_dentry_operations;
2202 d_add(dentry, inode);
2203 /* Close the race of the process dying before we return the dentry */
2204 if (pid_revalidate(dentry, NULL))
2210 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2212 struct dentry *result = ERR_PTR(-ENOENT);
2213 struct task_struct *task;
2214 struct task_struct *leader = get_proc_task(dir);
2220 tid = name_to_int(dentry);
2225 task = find_task_by_pid(tid);
2227 get_task_struct(task);
2231 if (leader->tgid != task->tgid)
2234 result = proc_task_instantiate(dir, dentry, task, NULL);
2236 put_task_struct(task);
2238 put_task_struct(leader);
2244 * Find the first tid of a thread group to return to user space.
2246 * Usually this is just the thread group leader, but if the users
2247 * buffer was too small or there was a seek into the middle of the
2248 * directory we have more work todo.
2250 * In the case of a short read we start with find_task_by_pid.
2252 * In the case of a seek we start with the leader and walk nr
2255 static struct task_struct *first_tid(struct task_struct *leader,
2258 struct task_struct *pos;
2261 /* Attempt to start with the pid of a thread */
2262 if (tid && (nr > 0)) {
2263 pos = find_task_by_pid(tid);
2264 if (pos && (pos->group_leader == leader))
2268 /* If nr exceeds the number of threads there is nothing todo */
2270 if (nr && nr >= get_nr_threads(leader))
2273 /* If we haven't found our starting place yet start
2274 * with the leader and walk nr threads forward.
2276 for (pos = leader; nr > 0; --nr) {
2277 pos = next_thread(pos);
2278 if (pos == leader) {
2284 get_task_struct(pos);
2291 * Find the next thread in the thread list.
2292 * Return NULL if there is an error or no next thread.
2294 * The reference to the input task_struct is released.
2296 static struct task_struct *next_tid(struct task_struct *start)
2298 struct task_struct *pos = NULL;
2300 if (pid_alive(start)) {
2301 pos = next_thread(start);
2302 if (thread_group_leader(pos))
2305 get_task_struct(pos);
2308 put_task_struct(start);
2312 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2313 struct task_struct *task, int tid)
2315 char name[PROC_NUMBUF];
2316 int len = snprintf(name, sizeof(name), "%d", tid);
2317 return proc_fill_cache(filp, dirent, filldir, name, len,
2318 proc_task_instantiate, task, NULL);
2321 /* for the /proc/TGID/task/ directories */
2322 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2324 struct dentry *dentry = filp->f_path.dentry;
2325 struct inode *inode = dentry->d_inode;
2326 struct task_struct *leader = NULL;
2327 struct task_struct *task;
2328 int retval = -ENOENT;
2331 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2333 task = get_proc_task(inode);
2337 if (pid_alive(task)) {
2338 leader = task->group_leader;
2339 get_task_struct(leader);
2342 put_task_struct(task);
2350 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2355 ino = parent_ino(dentry);
2356 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2362 /* f_version caches the tgid value that the last readdir call couldn't
2363 * return. lseek aka telldir automagically resets f_version to 0.
2365 tid = filp->f_version;
2366 filp->f_version = 0;
2367 for (task = first_tid(leader, tid, pos - 2);
2369 task = next_tid(task), pos++) {
2371 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2372 /* returning this tgid failed, save it as the first
2373 * pid for the next readir call */
2374 filp->f_version = tid;
2375 put_task_struct(task);
2381 put_task_struct(leader);
2386 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2388 struct inode *inode = dentry->d_inode;
2389 struct task_struct *p = get_proc_task(inode);
2390 generic_fillattr(inode, stat);
2394 stat->nlink += get_nr_threads(p);
2402 static const struct inode_operations proc_task_inode_operations = {
2403 .lookup = proc_task_lookup,
2404 .getattr = proc_task_getattr,
2405 .setattr = proc_setattr,
2408 static const struct file_operations proc_task_operations = {
2409 .read = generic_read_dir,
2410 .readdir = proc_task_readdir,