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 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
719 size_t count, loff_t *ppos)
721 struct task_struct *task;
722 char buffer[PROC_NUMBUF], *end;
723 struct mm_struct *mm;
725 memset(buffer, 0, sizeof(buffer));
726 if (count > sizeof(buffer) - 1)
727 count = sizeof(buffer) - 1;
728 if (copy_from_user(buffer, buf, count))
730 if (!simple_strtol(buffer, &end, 0))
734 task = get_proc_task(file->f_path.dentry->d_inode);
737 mm = get_task_mm(task);
742 put_task_struct(task);
743 if (end - buffer == 0)
748 static struct file_operations proc_clear_refs_operations = {
749 .write = clear_refs_write,
752 #ifdef CONFIG_AUDITSYSCALL
754 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
755 size_t count, loff_t *ppos)
757 struct inode * inode = file->f_path.dentry->d_inode;
758 struct task_struct *task = get_proc_task(inode);
760 char tmpbuf[TMPBUFLEN];
764 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
765 audit_get_loginuid(task->audit_context));
766 put_task_struct(task);
767 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
770 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
771 size_t count, loff_t *ppos)
773 struct inode * inode = file->f_path.dentry->d_inode;
778 if (!capable(CAP_AUDIT_CONTROL))
781 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
784 if (count >= PAGE_SIZE)
785 count = PAGE_SIZE - 1;
788 /* No partial writes. */
791 page = (char*)__get_free_page(GFP_USER);
795 if (copy_from_user(page, buf, count))
799 loginuid = simple_strtoul(page, &tmp, 10);
805 length = audit_set_loginuid(current, loginuid);
806 if (likely(length == 0))
810 free_page((unsigned long) page);
814 static const struct file_operations proc_loginuid_operations = {
815 .read = proc_loginuid_read,
816 .write = proc_loginuid_write,
820 #ifdef CONFIG_SECCOMP
821 static ssize_t seccomp_read(struct file *file, char __user *buf,
822 size_t count, loff_t *ppos)
824 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
826 loff_t __ppos = *ppos;
831 /* no need to print the trailing zero, so use only len */
832 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
833 put_task_struct(tsk);
836 if (count > len - __ppos)
837 count = len - __ppos;
838 if (copy_to_user(buf, __buf + __ppos, count))
840 *ppos = __ppos + count;
844 static ssize_t seccomp_write(struct file *file, const char __user *buf,
845 size_t count, loff_t *ppos)
847 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
848 char __buf[20], *end;
849 unsigned int seccomp_mode;
856 /* can set it only once to be even more secure */
858 if (unlikely(tsk->seccomp.mode))
862 memset(__buf, 0, sizeof(__buf));
863 count = min(count, sizeof(__buf) - 1);
864 if (copy_from_user(__buf, buf, count))
867 seccomp_mode = simple_strtoul(__buf, &end, 0);
871 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
872 tsk->seccomp.mode = seccomp_mode;
873 set_tsk_thread_flag(tsk, TIF_SECCOMP);
877 if (unlikely(!(end - __buf)))
879 result = end - __buf;
881 put_task_struct(tsk);
886 static const struct file_operations proc_seccomp_operations = {
887 .read = seccomp_read,
888 .write = seccomp_write,
890 #endif /* CONFIG_SECCOMP */
892 #ifdef CONFIG_FAULT_INJECTION
893 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
894 size_t count, loff_t *ppos)
896 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
897 char buffer[PROC_NUMBUF];
900 loff_t __ppos = *ppos;
904 make_it_fail = task->make_it_fail;
905 put_task_struct(task);
907 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
910 if (count > len-__ppos)
912 if (copy_to_user(buf, buffer + __ppos, count))
914 *ppos = __ppos + count;
918 static ssize_t proc_fault_inject_write(struct file * file,
919 const char __user * buf, size_t count, loff_t *ppos)
921 struct task_struct *task;
922 char buffer[PROC_NUMBUF], *end;
925 if (!capable(CAP_SYS_RESOURCE))
927 memset(buffer, 0, sizeof(buffer));
928 if (count > sizeof(buffer) - 1)
929 count = sizeof(buffer) - 1;
930 if (copy_from_user(buffer, buf, count))
932 make_it_fail = simple_strtol(buffer, &end, 0);
935 task = get_proc_task(file->f_dentry->d_inode);
938 task->make_it_fail = make_it_fail;
939 put_task_struct(task);
940 if (end - buffer == 0)
945 static const struct file_operations proc_fault_inject_operations = {
946 .read = proc_fault_inject_read,
947 .write = proc_fault_inject_write,
951 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
953 struct inode *inode = dentry->d_inode;
956 /* We don't need a base pointer in the /proc filesystem */
959 /* Are we allowed to snoop on the tasks file descriptors? */
960 if (!proc_fd_access_allowed(inode))
963 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
964 nd->last_type = LAST_BIND;
966 return ERR_PTR(error);
969 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
970 char __user *buffer, int buflen)
972 struct inode * inode;
973 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
979 inode = dentry->d_inode;
980 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
984 len = tmp + PAGE_SIZE - 1 - path;
988 if (copy_to_user(buffer, path, len))
991 free_page((unsigned long)tmp);
995 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
998 struct inode *inode = dentry->d_inode;
1000 struct vfsmount *mnt = NULL;
1002 /* Are we allowed to snoop on the tasks file descriptors? */
1003 if (!proc_fd_access_allowed(inode))
1006 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1010 error = do_proc_readlink(de, mnt, buffer, buflen);
1017 static const struct inode_operations proc_pid_link_inode_operations = {
1018 .readlink = proc_pid_readlink,
1019 .follow_link = proc_pid_follow_link,
1020 .setattr = proc_setattr,
1024 /* building an inode */
1026 static int task_dumpable(struct task_struct *task)
1029 struct mm_struct *mm;
1034 dumpable = mm->dumpable;
1042 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1044 struct inode * inode;
1045 struct proc_inode *ei;
1047 /* We need a new inode */
1049 inode = new_inode(sb);
1055 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1056 inode->i_op = &proc_def_inode_operations;
1059 * grab the reference to task.
1061 ei->pid = get_task_pid(task, PIDTYPE_PID);
1067 if (task_dumpable(task)) {
1068 inode->i_uid = task->euid;
1069 inode->i_gid = task->egid;
1071 security_task_to_inode(task, inode);
1081 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1083 struct inode *inode = dentry->d_inode;
1084 struct task_struct *task;
1085 generic_fillattr(inode, stat);
1090 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1092 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1093 task_dumpable(task)) {
1094 stat->uid = task->euid;
1095 stat->gid = task->egid;
1105 * Exceptional case: normally we are not allowed to unhash a busy
1106 * directory. In this case, however, we can do it - no aliasing problems
1107 * due to the way we treat inodes.
1109 * Rewrite the inode's ownerships here because the owning task may have
1110 * performed a setuid(), etc.
1112 * Before the /proc/pid/status file was created the only way to read
1113 * the effective uid of a /process was to stat /proc/pid. Reading
1114 * /proc/pid/status is slow enough that procps and other packages
1115 * kept stating /proc/pid. To keep the rules in /proc simple I have
1116 * made this apply to all per process world readable and executable
1119 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1121 struct inode *inode = dentry->d_inode;
1122 struct task_struct *task = get_proc_task(inode);
1124 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1125 task_dumpable(task)) {
1126 inode->i_uid = task->euid;
1127 inode->i_gid = task->egid;
1132 inode->i_mode &= ~(S_ISUID | S_ISGID);
1133 security_task_to_inode(task, inode);
1134 put_task_struct(task);
1141 static int pid_delete_dentry(struct dentry * dentry)
1143 /* Is the task we represent dead?
1144 * If so, then don't put the dentry on the lru list,
1145 * kill it immediately.
1147 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1150 static struct dentry_operations pid_dentry_operations =
1152 .d_revalidate = pid_revalidate,
1153 .d_delete = pid_delete_dentry,
1158 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1161 * Fill a directory entry.
1163 * If possible create the dcache entry and derive our inode number and
1164 * file type from dcache entry.
1166 * Since all of the proc inode numbers are dynamically generated, the inode
1167 * numbers do not exist until the inode is cache. This means creating the
1168 * the dcache entry in readdir is necessary to keep the inode numbers
1169 * reported by readdir in sync with the inode numbers reported
1172 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1173 char *name, int len,
1174 instantiate_t instantiate, struct task_struct *task, void *ptr)
1176 struct dentry *child, *dir = filp->f_path.dentry;
1177 struct inode *inode;
1180 unsigned type = DT_UNKNOWN;
1184 qname.hash = full_name_hash(name, len);
1186 child = d_lookup(dir, &qname);
1189 new = d_alloc(dir, &qname);
1191 child = instantiate(dir->d_inode, new, task, ptr);
1198 if (!child || IS_ERR(child) || !child->d_inode)
1199 goto end_instantiate;
1200 inode = child->d_inode;
1203 type = inode->i_mode >> 12;
1208 ino = find_inode_number(dir, &qname);
1211 return filldir(dirent, name, len, filp->f_pos, ino, type);
1214 static unsigned name_to_int(struct dentry *dentry)
1216 const char *name = dentry->d_name.name;
1217 int len = dentry->d_name.len;
1220 if (len > 1 && *name == '0')
1223 unsigned c = *name++ - '0';
1226 if (n >= (~0U-9)/10)
1236 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1238 struct task_struct *task = get_proc_task(inode);
1239 struct files_struct *files = NULL;
1241 int fd = proc_fd(inode);
1244 files = get_files_struct(task);
1245 put_task_struct(task);
1249 * We are not taking a ref to the file structure, so we must
1252 spin_lock(&files->file_lock);
1253 file = fcheck_files(files, fd);
1255 *mnt = mntget(file->f_path.mnt);
1256 *dentry = dget(file->f_path.dentry);
1257 spin_unlock(&files->file_lock);
1258 put_files_struct(files);
1261 spin_unlock(&files->file_lock);
1262 put_files_struct(files);
1267 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1269 struct inode *inode = dentry->d_inode;
1270 struct task_struct *task = get_proc_task(inode);
1271 int fd = proc_fd(inode);
1272 struct files_struct *files;
1275 files = get_files_struct(task);
1278 if (fcheck_files(files, fd)) {
1280 put_files_struct(files);
1281 if (task_dumpable(task)) {
1282 inode->i_uid = task->euid;
1283 inode->i_gid = task->egid;
1288 inode->i_mode &= ~(S_ISUID | S_ISGID);
1289 security_task_to_inode(task, inode);
1290 put_task_struct(task);
1294 put_files_struct(files);
1296 put_task_struct(task);
1302 static struct dentry_operations tid_fd_dentry_operations =
1304 .d_revalidate = tid_fd_revalidate,
1305 .d_delete = pid_delete_dentry,
1308 static struct dentry *proc_fd_instantiate(struct inode *dir,
1309 struct dentry *dentry, struct task_struct *task, void *ptr)
1311 unsigned fd = *(unsigned *)ptr;
1313 struct files_struct *files;
1314 struct inode *inode;
1315 struct proc_inode *ei;
1316 struct dentry *error = ERR_PTR(-ENOENT);
1318 inode = proc_pid_make_inode(dir->i_sb, task);
1323 files = get_files_struct(task);
1326 inode->i_mode = S_IFLNK;
1329 * We are not taking a ref to the file structure, so we must
1332 spin_lock(&files->file_lock);
1333 file = fcheck_files(files, fd);
1336 if (file->f_mode & 1)
1337 inode->i_mode |= S_IRUSR | S_IXUSR;
1338 if (file->f_mode & 2)
1339 inode->i_mode |= S_IWUSR | S_IXUSR;
1340 spin_unlock(&files->file_lock);
1341 put_files_struct(files);
1343 inode->i_op = &proc_pid_link_inode_operations;
1345 ei->op.proc_get_link = proc_fd_link;
1346 dentry->d_op = &tid_fd_dentry_operations;
1347 d_add(dentry, inode);
1348 /* Close the race of the process dying before we return the dentry */
1349 if (tid_fd_revalidate(dentry, NULL))
1355 spin_unlock(&files->file_lock);
1356 put_files_struct(files);
1362 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1364 struct task_struct *task = get_proc_task(dir);
1365 unsigned fd = name_to_int(dentry);
1366 struct dentry *result = ERR_PTR(-ENOENT);
1373 result = proc_fd_instantiate(dir, dentry, task, &fd);
1375 put_task_struct(task);
1380 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1381 struct task_struct *task, int fd)
1383 char name[PROC_NUMBUF];
1384 int len = snprintf(name, sizeof(name), "%d", fd);
1385 return proc_fill_cache(filp, dirent, filldir, name, len,
1386 proc_fd_instantiate, task, &fd);
1389 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1391 struct dentry *dentry = filp->f_path.dentry;
1392 struct inode *inode = dentry->d_inode;
1393 struct task_struct *p = get_proc_task(inode);
1394 unsigned int fd, tid, ino;
1396 struct files_struct * files;
1397 struct fdtable *fdt;
1408 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1412 ino = parent_ino(dentry);
1413 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1417 files = get_files_struct(p);
1421 fdt = files_fdtable(files);
1422 for (fd = filp->f_pos-2;
1424 fd++, filp->f_pos++) {
1426 if (!fcheck_files(files, fd))
1430 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1437 put_files_struct(files);
1445 static const struct file_operations proc_fd_operations = {
1446 .read = generic_read_dir,
1447 .readdir = proc_readfd,
1451 * proc directories can do almost nothing..
1453 static const struct inode_operations proc_fd_inode_operations = {
1454 .lookup = proc_lookupfd,
1455 .setattr = proc_setattr,
1458 static struct dentry *proc_pident_instantiate(struct inode *dir,
1459 struct dentry *dentry, struct task_struct *task, void *ptr)
1461 struct pid_entry *p = ptr;
1462 struct inode *inode;
1463 struct proc_inode *ei;
1464 struct dentry *error = ERR_PTR(-EINVAL);
1466 inode = proc_pid_make_inode(dir->i_sb, task);
1471 inode->i_mode = p->mode;
1472 if (S_ISDIR(inode->i_mode))
1473 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1475 inode->i_op = p->iop;
1477 inode->i_fop = p->fop;
1479 dentry->d_op = &pid_dentry_operations;
1480 d_add(dentry, inode);
1481 /* Close the race of the process dying before we return the dentry */
1482 if (pid_revalidate(dentry, NULL))
1488 static struct dentry *proc_pident_lookup(struct inode *dir,
1489 struct dentry *dentry,
1490 struct pid_entry *ents,
1493 struct inode *inode;
1494 struct dentry *error;
1495 struct task_struct *task = get_proc_task(dir);
1496 struct pid_entry *p, *last;
1498 error = ERR_PTR(-ENOENT);
1505 * Yes, it does not scale. And it should not. Don't add
1506 * new entries into /proc/<tgid>/ without very good reasons.
1508 last = &ents[nents - 1];
1509 for (p = ents; p <= last; p++) {
1510 if (p->len != dentry->d_name.len)
1512 if (!memcmp(dentry->d_name.name, p->name, p->len))
1518 error = proc_pident_instantiate(dir, dentry, task, p);
1520 put_task_struct(task);
1525 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1526 struct task_struct *task, struct pid_entry *p)
1528 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1529 proc_pident_instantiate, task, p);
1532 static int proc_pident_readdir(struct file *filp,
1533 void *dirent, filldir_t filldir,
1534 struct pid_entry *ents, unsigned int nents)
1538 struct dentry *dentry = filp->f_path.dentry;
1539 struct inode *inode = dentry->d_inode;
1540 struct task_struct *task = get_proc_task(inode);
1541 struct pid_entry *p, *last;
1555 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1561 ino = parent_ino(dentry);
1562 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1574 last = &ents[nents - 1];
1576 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1585 put_task_struct(task);
1590 #ifdef CONFIG_SECURITY
1591 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1592 size_t count, loff_t *ppos)
1594 struct inode * inode = file->f_path.dentry->d_inode;
1597 struct task_struct *task = get_proc_task(inode);
1602 length = security_getprocattr(task,
1603 (char*)file->f_path.dentry->d_name.name,
1605 put_task_struct(task);
1607 length = simple_read_from_buffer(buf, count, ppos, p, length);
1612 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1613 size_t count, loff_t *ppos)
1615 struct inode * inode = file->f_path.dentry->d_inode;
1618 struct task_struct *task = get_proc_task(inode);
1623 if (count > PAGE_SIZE)
1626 /* No partial writes. */
1632 page = (char*)__get_free_page(GFP_USER);
1637 if (copy_from_user(page, buf, count))
1640 length = security_setprocattr(task,
1641 (char*)file->f_path.dentry->d_name.name,
1642 (void*)page, count);
1644 free_page((unsigned long) page);
1646 put_task_struct(task);
1651 static const struct file_operations proc_pid_attr_operations = {
1652 .read = proc_pid_attr_read,
1653 .write = proc_pid_attr_write,
1656 static struct pid_entry attr_dir_stuff[] = {
1657 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1658 REG("prev", S_IRUGO, pid_attr),
1659 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1660 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1661 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1662 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1665 static int proc_attr_dir_readdir(struct file * filp,
1666 void * dirent, filldir_t filldir)
1668 return proc_pident_readdir(filp,dirent,filldir,
1669 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1672 static const struct file_operations proc_attr_dir_operations = {
1673 .read = generic_read_dir,
1674 .readdir = proc_attr_dir_readdir,
1677 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1678 struct dentry *dentry, struct nameidata *nd)
1680 return proc_pident_lookup(dir, dentry,
1681 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1684 static const struct inode_operations proc_attr_dir_inode_operations = {
1685 .lookup = proc_attr_dir_lookup,
1686 .getattr = pid_getattr,
1687 .setattr = proc_setattr,
1695 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1698 char tmp[PROC_NUMBUF];
1699 sprintf(tmp, "%d", current->tgid);
1700 return vfs_readlink(dentry,buffer,buflen,tmp);
1703 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1705 char tmp[PROC_NUMBUF];
1706 sprintf(tmp, "%d", current->tgid);
1707 return ERR_PTR(vfs_follow_link(nd,tmp));
1710 static const struct inode_operations proc_self_inode_operations = {
1711 .readlink = proc_self_readlink,
1712 .follow_link = proc_self_follow_link,
1718 * These are the directory entries in the root directory of /proc
1719 * that properly belong to the /proc filesystem, as they describe
1720 * describe something that is process related.
1722 static struct pid_entry proc_base_stuff[] = {
1723 NOD("self", S_IFLNK|S_IRWXUGO,
1724 &proc_self_inode_operations, NULL, {}),
1728 * Exceptional case: normally we are not allowed to unhash a busy
1729 * directory. In this case, however, we can do it - no aliasing problems
1730 * due to the way we treat inodes.
1732 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1734 struct inode *inode = dentry->d_inode;
1735 struct task_struct *task = get_proc_task(inode);
1737 put_task_struct(task);
1744 static struct dentry_operations proc_base_dentry_operations =
1746 .d_revalidate = proc_base_revalidate,
1747 .d_delete = pid_delete_dentry,
1750 static struct dentry *proc_base_instantiate(struct inode *dir,
1751 struct dentry *dentry, struct task_struct *task, void *ptr)
1753 struct pid_entry *p = ptr;
1754 struct inode *inode;
1755 struct proc_inode *ei;
1756 struct dentry *error = ERR_PTR(-EINVAL);
1758 /* Allocate the inode */
1759 error = ERR_PTR(-ENOMEM);
1760 inode = new_inode(dir->i_sb);
1764 /* Initialize the inode */
1766 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1769 * grab the reference to the task.
1771 ei->pid = get_task_pid(task, PIDTYPE_PID);
1777 inode->i_mode = p->mode;
1778 if (S_ISDIR(inode->i_mode))
1780 if (S_ISLNK(inode->i_mode))
1783 inode->i_op = p->iop;
1785 inode->i_fop = p->fop;
1787 dentry->d_op = &proc_base_dentry_operations;
1788 d_add(dentry, inode);
1797 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1799 struct dentry *error;
1800 struct task_struct *task = get_proc_task(dir);
1801 struct pid_entry *p, *last;
1803 error = ERR_PTR(-ENOENT);
1808 /* Lookup the directory entry */
1809 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1810 for (p = proc_base_stuff; p <= last; p++) {
1811 if (p->len != dentry->d_name.len)
1813 if (!memcmp(dentry->d_name.name, p->name, p->len))
1819 error = proc_base_instantiate(dir, dentry, task, p);
1822 put_task_struct(task);
1827 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1828 struct task_struct *task, struct pid_entry *p)
1830 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1831 proc_base_instantiate, task, p);
1834 #ifdef CONFIG_TASK_IO_ACCOUNTING
1835 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1837 return sprintf(buffer,
1838 #ifdef CONFIG_TASK_XACCT
1844 "read_bytes: %llu\n"
1845 "write_bytes: %llu\n"
1846 "cancelled_write_bytes: %llu\n",
1847 #ifdef CONFIG_TASK_XACCT
1848 (unsigned long long)task->rchar,
1849 (unsigned long long)task->wchar,
1850 (unsigned long long)task->syscr,
1851 (unsigned long long)task->syscw,
1853 (unsigned long long)task->ioac.read_bytes,
1854 (unsigned long long)task->ioac.write_bytes,
1855 (unsigned long long)task->ioac.cancelled_write_bytes);
1862 static const struct file_operations proc_task_operations;
1863 static const struct inode_operations proc_task_inode_operations;
1865 static struct pid_entry tgid_base_stuff[] = {
1866 DIR("task", S_IRUGO|S_IXUGO, task),
1867 DIR("fd", S_IRUSR|S_IXUSR, fd),
1868 INF("environ", S_IRUSR, pid_environ),
1869 INF("auxv", S_IRUSR, pid_auxv),
1870 INF("status", S_IRUGO, pid_status),
1871 INF("cmdline", S_IRUGO, pid_cmdline),
1872 INF("stat", S_IRUGO, tgid_stat),
1873 INF("statm", S_IRUGO, pid_statm),
1874 REG("maps", S_IRUGO, maps),
1876 REG("numa_maps", S_IRUGO, numa_maps),
1878 REG("mem", S_IRUSR|S_IWUSR, mem),
1879 #ifdef CONFIG_SECCOMP
1880 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1885 REG("mounts", S_IRUGO, mounts),
1886 REG("mountstats", S_IRUSR, mountstats),
1888 REG("clear_refs", S_IWUSR, clear_refs),
1889 REG("smaps", S_IRUGO, smaps),
1891 #ifdef CONFIG_SECURITY
1892 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1894 #ifdef CONFIG_KALLSYMS
1895 INF("wchan", S_IRUGO, pid_wchan),
1897 #ifdef CONFIG_SCHEDSTATS
1898 INF("schedstat", S_IRUGO, pid_schedstat),
1900 #ifdef CONFIG_CPUSETS
1901 REG("cpuset", S_IRUGO, cpuset),
1903 INF("oom_score", S_IRUGO, oom_score),
1904 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1905 #ifdef CONFIG_AUDITSYSCALL
1906 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
1908 #ifdef CONFIG_FAULT_INJECTION
1909 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1911 #ifdef CONFIG_TASK_IO_ACCOUNTING
1912 INF("io", S_IRUGO, pid_io_accounting),
1916 static int proc_tgid_base_readdir(struct file * filp,
1917 void * dirent, filldir_t filldir)
1919 return proc_pident_readdir(filp,dirent,filldir,
1920 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1923 static const struct file_operations proc_tgid_base_operations = {
1924 .read = generic_read_dir,
1925 .readdir = proc_tgid_base_readdir,
1928 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1929 return proc_pident_lookup(dir, dentry,
1930 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1933 static const struct inode_operations proc_tgid_base_inode_operations = {
1934 .lookup = proc_tgid_base_lookup,
1935 .getattr = pid_getattr,
1936 .setattr = proc_setattr,
1940 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1942 * @task: task that should be flushed.
1944 * Looks in the dcache for
1946 * /proc/@tgid/task/@pid
1947 * if either directory is present flushes it and all of it'ts children
1950 * It is safe and reasonable to cache /proc entries for a task until
1951 * that task exits. After that they just clog up the dcache with
1952 * useless entries, possibly causing useful dcache entries to be
1953 * flushed instead. This routine is proved to flush those useless
1954 * dcache entries at process exit time.
1956 * NOTE: This routine is just an optimization so it does not guarantee
1957 * that no dcache entries will exist at process exit time it
1958 * just makes it very unlikely that any will persist.
1960 void proc_flush_task(struct task_struct *task)
1962 struct dentry *dentry, *leader, *dir;
1963 char buf[PROC_NUMBUF];
1967 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1968 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1970 shrink_dcache_parent(dentry);
1975 if (thread_group_leader(task))
1979 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1980 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1985 name.len = strlen(name.name);
1986 dir = d_hash_and_lookup(leader, &name);
1988 goto out_put_leader;
1991 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1992 dentry = d_hash_and_lookup(dir, &name);
1994 shrink_dcache_parent(dentry);
2006 static struct dentry *proc_pid_instantiate(struct inode *dir,
2007 struct dentry * dentry,
2008 struct task_struct *task, void *ptr)
2010 struct dentry *error = ERR_PTR(-ENOENT);
2011 struct inode *inode;
2013 inode = proc_pid_make_inode(dir->i_sb, task);
2017 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2018 inode->i_op = &proc_tgid_base_inode_operations;
2019 inode->i_fop = &proc_tgid_base_operations;
2020 inode->i_flags|=S_IMMUTABLE;
2022 #ifdef CONFIG_SECURITY
2023 inode->i_nlink += 1;
2026 dentry->d_op = &pid_dentry_operations;
2028 d_add(dentry, inode);
2029 /* Close the race of the process dying before we return the dentry */
2030 if (pid_revalidate(dentry, NULL))
2036 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2038 struct dentry *result = ERR_PTR(-ENOENT);
2039 struct task_struct *task;
2042 result = proc_base_lookup(dir, dentry);
2043 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2046 tgid = name_to_int(dentry);
2051 task = find_task_by_pid(tgid);
2053 get_task_struct(task);
2058 result = proc_pid_instantiate(dir, dentry, task, NULL);
2059 put_task_struct(task);
2065 * Find the first task with tgid >= tgid
2068 static struct task_struct *next_tgid(unsigned int tgid)
2070 struct task_struct *task;
2076 pid = find_ge_pid(tgid);
2079 task = pid_task(pid, PIDTYPE_PID);
2080 /* What we to know is if the pid we have find is the
2081 * pid of a thread_group_leader. Testing for task
2082 * being a thread_group_leader is the obvious thing
2083 * todo but there is a window when it fails, due to
2084 * the pid transfer logic in de_thread.
2086 * So we perform the straight forward test of seeing
2087 * if the pid we have found is the pid of a thread
2088 * group leader, and don't worry if the task we have
2089 * found doesn't happen to be a thread group leader.
2090 * As we don't care in the case of readdir.
2092 if (!task || !has_group_leader_pid(task))
2094 get_task_struct(task);
2100 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2102 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2103 struct task_struct *task, int tgid)
2105 char name[PROC_NUMBUF];
2106 int len = snprintf(name, sizeof(name), "%d", tgid);
2107 return proc_fill_cache(filp, dirent, filldir, name, len,
2108 proc_pid_instantiate, task, NULL);
2111 /* for the /proc/ directory itself, after non-process stuff has been done */
2112 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2114 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2115 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2116 struct task_struct *task;
2122 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2123 struct pid_entry *p = &proc_base_stuff[nr];
2124 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2128 tgid = filp->f_pos - TGID_OFFSET;
2129 for (task = next_tgid(tgid);
2131 put_task_struct(task), task = next_tgid(tgid + 1)) {
2133 filp->f_pos = tgid + TGID_OFFSET;
2134 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2135 put_task_struct(task);
2139 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2141 put_task_struct(reaper);
2149 static struct pid_entry tid_base_stuff[] = {
2150 DIR("fd", S_IRUSR|S_IXUSR, fd),
2151 INF("environ", S_IRUSR, pid_environ),
2152 INF("auxv", S_IRUSR, pid_auxv),
2153 INF("status", S_IRUGO, pid_status),
2154 INF("cmdline", S_IRUGO, pid_cmdline),
2155 INF("stat", S_IRUGO, tid_stat),
2156 INF("statm", S_IRUGO, pid_statm),
2157 REG("maps", S_IRUGO, maps),
2159 REG("numa_maps", S_IRUGO, numa_maps),
2161 REG("mem", S_IRUSR|S_IWUSR, mem),
2162 #ifdef CONFIG_SECCOMP
2163 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2168 REG("mounts", S_IRUGO, mounts),
2170 REG("clear_refs", S_IWUSR, clear_refs),
2171 REG("smaps", S_IRUGO, smaps),
2173 #ifdef CONFIG_SECURITY
2174 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2176 #ifdef CONFIG_KALLSYMS
2177 INF("wchan", S_IRUGO, pid_wchan),
2179 #ifdef CONFIG_SCHEDSTATS
2180 INF("schedstat", S_IRUGO, pid_schedstat),
2182 #ifdef CONFIG_CPUSETS
2183 REG("cpuset", S_IRUGO, cpuset),
2185 INF("oom_score", S_IRUGO, oom_score),
2186 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2187 #ifdef CONFIG_AUDITSYSCALL
2188 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2190 #ifdef CONFIG_FAULT_INJECTION
2191 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2195 static int proc_tid_base_readdir(struct file * filp,
2196 void * dirent, filldir_t filldir)
2198 return proc_pident_readdir(filp,dirent,filldir,
2199 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2202 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2203 return proc_pident_lookup(dir, dentry,
2204 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2207 static const struct file_operations proc_tid_base_operations = {
2208 .read = generic_read_dir,
2209 .readdir = proc_tid_base_readdir,
2212 static const struct inode_operations proc_tid_base_inode_operations = {
2213 .lookup = proc_tid_base_lookup,
2214 .getattr = pid_getattr,
2215 .setattr = proc_setattr,
2218 static struct dentry *proc_task_instantiate(struct inode *dir,
2219 struct dentry *dentry, struct task_struct *task, void *ptr)
2221 struct dentry *error = ERR_PTR(-ENOENT);
2222 struct inode *inode;
2223 inode = proc_pid_make_inode(dir->i_sb, task);
2227 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2228 inode->i_op = &proc_tid_base_inode_operations;
2229 inode->i_fop = &proc_tid_base_operations;
2230 inode->i_flags|=S_IMMUTABLE;
2232 #ifdef CONFIG_SECURITY
2233 inode->i_nlink += 1;
2236 dentry->d_op = &pid_dentry_operations;
2238 d_add(dentry, inode);
2239 /* Close the race of the process dying before we return the dentry */
2240 if (pid_revalidate(dentry, NULL))
2246 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2248 struct dentry *result = ERR_PTR(-ENOENT);
2249 struct task_struct *task;
2250 struct task_struct *leader = get_proc_task(dir);
2256 tid = name_to_int(dentry);
2261 task = find_task_by_pid(tid);
2263 get_task_struct(task);
2267 if (leader->tgid != task->tgid)
2270 result = proc_task_instantiate(dir, dentry, task, NULL);
2272 put_task_struct(task);
2274 put_task_struct(leader);
2280 * Find the first tid of a thread group to return to user space.
2282 * Usually this is just the thread group leader, but if the users
2283 * buffer was too small or there was a seek into the middle of the
2284 * directory we have more work todo.
2286 * In the case of a short read we start with find_task_by_pid.
2288 * In the case of a seek we start with the leader and walk nr
2291 static struct task_struct *first_tid(struct task_struct *leader,
2294 struct task_struct *pos;
2297 /* Attempt to start with the pid of a thread */
2298 if (tid && (nr > 0)) {
2299 pos = find_task_by_pid(tid);
2300 if (pos && (pos->group_leader == leader))
2304 /* If nr exceeds the number of threads there is nothing todo */
2306 if (nr && nr >= get_nr_threads(leader))
2309 /* If we haven't found our starting place yet start
2310 * with the leader and walk nr threads forward.
2312 for (pos = leader; nr > 0; --nr) {
2313 pos = next_thread(pos);
2314 if (pos == leader) {
2320 get_task_struct(pos);
2327 * Find the next thread in the thread list.
2328 * Return NULL if there is an error or no next thread.
2330 * The reference to the input task_struct is released.
2332 static struct task_struct *next_tid(struct task_struct *start)
2334 struct task_struct *pos = NULL;
2336 if (pid_alive(start)) {
2337 pos = next_thread(start);
2338 if (thread_group_leader(pos))
2341 get_task_struct(pos);
2344 put_task_struct(start);
2348 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2349 struct task_struct *task, int tid)
2351 char name[PROC_NUMBUF];
2352 int len = snprintf(name, sizeof(name), "%d", tid);
2353 return proc_fill_cache(filp, dirent, filldir, name, len,
2354 proc_task_instantiate, task, NULL);
2357 /* for the /proc/TGID/task/ directories */
2358 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2360 struct dentry *dentry = filp->f_path.dentry;
2361 struct inode *inode = dentry->d_inode;
2362 struct task_struct *leader = NULL;
2363 struct task_struct *task;
2364 int retval = -ENOENT;
2367 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2369 task = get_proc_task(inode);
2373 if (pid_alive(task)) {
2374 leader = task->group_leader;
2375 get_task_struct(leader);
2378 put_task_struct(task);
2386 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2391 ino = parent_ino(dentry);
2392 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2398 /* f_version caches the tgid value that the last readdir call couldn't
2399 * return. lseek aka telldir automagically resets f_version to 0.
2401 tid = filp->f_version;
2402 filp->f_version = 0;
2403 for (task = first_tid(leader, tid, pos - 2);
2405 task = next_tid(task), pos++) {
2407 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2408 /* returning this tgid failed, save it as the first
2409 * pid for the next readir call */
2410 filp->f_version = tid;
2411 put_task_struct(task);
2417 put_task_struct(leader);
2422 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2424 struct inode *inode = dentry->d_inode;
2425 struct task_struct *p = get_proc_task(inode);
2426 generic_fillattr(inode, stat);
2430 stat->nlink += get_nr_threads(p);
2438 static const struct inode_operations proc_task_inode_operations = {
2439 .lookup = proc_task_lookup,
2440 .getattr = proc_task_getattr,
2441 .setattr = proc_setattr,
2444 static const struct file_operations proc_task_operations = {
2445 .read = generic_read_dir,
2446 .readdir = proc_task_readdir,