4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
90 /* Worst case buffer size needed for holding an integer. */
91 #define PROC_NUMBUF 13
97 const struct inode_operations *iop;
98 const struct file_operations *fop;
102 #define NOD(NAME, MODE, IOP, FOP, OP) { \
104 .len = sizeof(NAME) - 1, \
111 #define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 #define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
119 #define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
121 &proc_##OTYPE##_operations, {})
122 #define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
128 EXPORT_SYMBOL(maps_protect);
130 static struct fs_struct *get_fs_struct(struct task_struct *task)
132 struct fs_struct *fs;
136 atomic_inc(&fs->count);
141 static int get_nr_threads(struct task_struct *tsk)
143 /* Must be called with the rcu_read_lock held */
147 if (lock_task_sighand(tsk, &flags)) {
148 count = atomic_read(&tsk->signal->count);
149 unlock_task_sighand(tsk, &flags);
154 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
156 struct task_struct *task = get_proc_task(inode);
157 struct fs_struct *fs = NULL;
158 int result = -ENOENT;
161 fs = get_fs_struct(task);
162 put_task_struct(task);
165 read_lock(&fs->lock);
166 *mnt = mntget(fs->pwdmnt);
167 *dentry = dget(fs->pwd);
168 read_unlock(&fs->lock);
175 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
177 struct task_struct *task = get_proc_task(inode);
178 struct fs_struct *fs = NULL;
179 int result = -ENOENT;
182 fs = get_fs_struct(task);
183 put_task_struct(task);
186 read_lock(&fs->lock);
187 *mnt = mntget(fs->rootmnt);
188 *dentry = dget(fs->root);
189 read_unlock(&fs->lock);
196 #define MAY_PTRACE(task) \
197 (task == current || \
198 (task->parent == current && \
199 (task->ptrace & PT_PTRACED) && \
200 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 security_ptrace(current,task) == 0))
203 static int proc_pid_environ(struct task_struct *task, char * buffer)
206 struct mm_struct *mm = get_task_mm(task);
208 unsigned int len = mm->env_end - mm->env_start;
211 res = access_process_vm(task, mm->env_start, buffer, len, 0);
212 if (!ptrace_may_attach(task))
219 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
223 struct mm_struct *mm = get_task_mm(task);
227 goto out_mm; /* Shh! No looking before we're done */
229 len = mm->arg_end - mm->arg_start;
234 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
236 // If the nul at the end of args has been overwritten, then
237 // assume application is using setproctitle(3).
238 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
239 len = strnlen(buffer, res);
243 len = mm->env_end - mm->env_start;
244 if (len > PAGE_SIZE - res)
245 len = PAGE_SIZE - res;
246 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
247 res = strnlen(buffer, res);
256 static int proc_pid_auxv(struct task_struct *task, char *buffer)
259 struct mm_struct *mm = get_task_mm(task);
261 unsigned int nwords = 0;
264 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
265 res = nwords * sizeof(mm->saved_auxv[0]);
268 memcpy(buffer, mm->saved_auxv, res);
275 #ifdef CONFIG_KALLSYMS
277 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
278 * Returns the resolved symbol. If that fails, simply return the address.
280 static int proc_pid_wchan(struct task_struct *task, char *buffer)
283 const char *sym_name;
284 unsigned long wchan, size, offset;
285 char namebuf[KSYM_NAME_LEN+1];
287 wchan = get_wchan(task);
289 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
291 return sprintf(buffer, "%s", sym_name);
292 return sprintf(buffer, "%lu", wchan);
294 #endif /* CONFIG_KALLSYMS */
296 #ifdef CONFIG_SCHEDSTATS
298 * Provides /proc/PID/schedstat
300 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
302 return sprintf(buffer, "%lu %lu %lu\n",
303 task->sched_info.cpu_time,
304 task->sched_info.run_delay,
305 task->sched_info.pcnt);
309 /* The badness from the OOM killer */
310 unsigned long badness(struct task_struct *p, unsigned long uptime);
311 static int proc_oom_score(struct task_struct *task, char *buffer)
313 unsigned long points;
314 struct timespec uptime;
316 do_posix_clock_monotonic_gettime(&uptime);
317 read_lock(&tasklist_lock);
318 points = badness(task, uptime.tv_sec);
319 read_unlock(&tasklist_lock);
320 return sprintf(buffer, "%lu\n", points);
323 /************************************************************************/
324 /* Here the fs part begins */
325 /************************************************************************/
327 /* permission checks */
328 static int proc_fd_access_allowed(struct inode *inode)
330 struct task_struct *task;
332 /* Allow access to a task's file descriptors if it is us or we
333 * may use ptrace attach to the process and find out that
336 task = get_proc_task(inode);
338 allowed = ptrace_may_attach(task);
339 put_task_struct(task);
344 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
347 struct inode *inode = dentry->d_inode;
349 if (attr->ia_valid & ATTR_MODE)
352 error = inode_change_ok(inode, attr);
354 error = security_inode_setattr(dentry, attr);
356 error = inode_setattr(inode, attr);
361 static const struct inode_operations proc_def_inode_operations = {
362 .setattr = proc_setattr,
365 extern struct seq_operations mounts_op;
371 static int mounts_open(struct inode *inode, struct file *file)
373 struct task_struct *task = get_proc_task(inode);
374 struct mnt_namespace *ns = NULL;
375 struct proc_mounts *p;
381 ns = task->nsproxy->mnt_ns;
386 put_task_struct(task);
391 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
393 file->private_data = &p->m;
394 ret = seq_open(file, &mounts_op);
397 p->event = ns->event;
407 static int mounts_release(struct inode *inode, struct file *file)
409 struct seq_file *m = file->private_data;
410 struct mnt_namespace *ns = m->private;
412 return seq_release(inode, file);
415 static unsigned mounts_poll(struct file *file, poll_table *wait)
417 struct proc_mounts *p = file->private_data;
418 struct mnt_namespace *ns = p->m.private;
421 poll_wait(file, &ns->poll, wait);
423 spin_lock(&vfsmount_lock);
424 if (p->event != ns->event) {
425 p->event = ns->event;
428 spin_unlock(&vfsmount_lock);
433 static const struct file_operations proc_mounts_operations = {
437 .release = mounts_release,
441 extern struct seq_operations mountstats_op;
442 static int mountstats_open(struct inode *inode, struct file *file)
444 int ret = seq_open(file, &mountstats_op);
447 struct seq_file *m = file->private_data;
448 struct mnt_namespace *mnt_ns = NULL;
449 struct task_struct *task = get_proc_task(inode);
454 mnt_ns = task->nsproxy->mnt_ns;
458 put_task_struct(task);
464 seq_release(inode, file);
471 static const struct file_operations proc_mountstats_operations = {
472 .open = mountstats_open,
475 .release = mounts_release,
478 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
480 static ssize_t proc_info_read(struct file * file, char __user * buf,
481 size_t count, loff_t *ppos)
483 struct inode * inode = file->f_path.dentry->d_inode;
486 struct task_struct *task = get_proc_task(inode);
492 if (count > PROC_BLOCK_SIZE)
493 count = PROC_BLOCK_SIZE;
496 if (!(page = __get_free_page(GFP_KERNEL)))
499 length = PROC_I(inode)->op.proc_read(task, (char*)page);
502 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
505 put_task_struct(task);
510 static const struct file_operations proc_info_file_operations = {
511 .read = proc_info_read,
514 static int mem_open(struct inode* inode, struct file* file)
516 file->private_data = (void*)((long)current->self_exec_id);
520 static ssize_t mem_read(struct file * file, char __user * buf,
521 size_t count, loff_t *ppos)
523 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
525 unsigned long src = *ppos;
527 struct mm_struct *mm;
532 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
536 page = (char *)__get_free_page(GFP_USER);
542 mm = get_task_mm(task);
548 if (file->private_data != (void*)((long)current->self_exec_id))
554 int this_len, retval;
556 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
557 retval = access_process_vm(task, src, page, this_len, 0);
558 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
564 if (copy_to_user(buf, page, retval)) {
579 free_page((unsigned long) page);
581 put_task_struct(task);
586 #define mem_write NULL
589 /* This is a security hazard */
590 static ssize_t mem_write(struct file * file, const char __user *buf,
591 size_t count, loff_t *ppos)
595 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
596 unsigned long dst = *ppos;
602 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
606 page = (char *)__get_free_page(GFP_USER);
612 int this_len, retval;
614 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
615 if (copy_from_user(page, buf, this_len)) {
619 retval = access_process_vm(task, dst, page, this_len, 1);
631 free_page((unsigned long) page);
633 put_task_struct(task);
639 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
643 file->f_pos = offset;
646 file->f_pos += offset;
651 force_successful_syscall_return();
655 static const struct file_operations proc_mem_operations = {
662 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
663 size_t count, loff_t *ppos)
665 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
666 char buffer[PROC_NUMBUF];
669 loff_t __ppos = *ppos;
673 oom_adjust = task->oomkilladj;
674 put_task_struct(task);
676 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
679 if (count > len-__ppos)
681 if (copy_to_user(buf, buffer + __ppos, count))
683 *ppos = __ppos + count;
687 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
688 size_t count, loff_t *ppos)
690 struct task_struct *task;
691 char buffer[PROC_NUMBUF], *end;
694 memset(buffer, 0, sizeof(buffer));
695 if (count > sizeof(buffer) - 1)
696 count = sizeof(buffer) - 1;
697 if (copy_from_user(buffer, buf, count))
699 oom_adjust = simple_strtol(buffer, &end, 0);
700 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
701 oom_adjust != OOM_DISABLE)
705 task = get_proc_task(file->f_path.dentry->d_inode);
708 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
709 put_task_struct(task);
712 task->oomkilladj = oom_adjust;
713 put_task_struct(task);
714 if (end - buffer == 0)
719 static const struct file_operations proc_oom_adjust_operations = {
720 .read = oom_adjust_read,
721 .write = oom_adjust_write,
724 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
725 size_t count, loff_t *ppos)
727 struct task_struct *task;
728 char buffer[PROC_NUMBUF], *end;
729 struct mm_struct *mm;
731 memset(buffer, 0, sizeof(buffer));
732 if (count > sizeof(buffer) - 1)
733 count = sizeof(buffer) - 1;
734 if (copy_from_user(buffer, buf, count))
736 if (!simple_strtol(buffer, &end, 0))
740 task = get_proc_task(file->f_path.dentry->d_inode);
743 mm = get_task_mm(task);
748 put_task_struct(task);
749 if (end - buffer == 0)
754 static struct file_operations proc_clear_refs_operations = {
755 .write = clear_refs_write,
758 #ifdef CONFIG_AUDITSYSCALL
760 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
761 size_t count, loff_t *ppos)
763 struct inode * inode = file->f_path.dentry->d_inode;
764 struct task_struct *task = get_proc_task(inode);
766 char tmpbuf[TMPBUFLEN];
770 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
771 audit_get_loginuid(task->audit_context));
772 put_task_struct(task);
773 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
776 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
777 size_t count, loff_t *ppos)
779 struct inode * inode = file->f_path.dentry->d_inode;
784 if (!capable(CAP_AUDIT_CONTROL))
787 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
790 if (count >= PAGE_SIZE)
791 count = PAGE_SIZE - 1;
794 /* No partial writes. */
797 page = (char*)__get_free_page(GFP_USER);
801 if (copy_from_user(page, buf, count))
805 loginuid = simple_strtoul(page, &tmp, 10);
811 length = audit_set_loginuid(current, loginuid);
812 if (likely(length == 0))
816 free_page((unsigned long) page);
820 static const struct file_operations proc_loginuid_operations = {
821 .read = proc_loginuid_read,
822 .write = proc_loginuid_write,
826 #ifdef CONFIG_SECCOMP
827 static ssize_t seccomp_read(struct file *file, char __user *buf,
828 size_t count, loff_t *ppos)
830 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
832 loff_t __ppos = *ppos;
837 /* no need to print the trailing zero, so use only len */
838 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
839 put_task_struct(tsk);
842 if (count > len - __ppos)
843 count = len - __ppos;
844 if (copy_to_user(buf, __buf + __ppos, count))
846 *ppos = __ppos + count;
850 static ssize_t seccomp_write(struct file *file, const char __user *buf,
851 size_t count, loff_t *ppos)
853 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
854 char __buf[20], *end;
855 unsigned int seccomp_mode;
862 /* can set it only once to be even more secure */
864 if (unlikely(tsk->seccomp.mode))
868 memset(__buf, 0, sizeof(__buf));
869 count = min(count, sizeof(__buf) - 1);
870 if (copy_from_user(__buf, buf, count))
873 seccomp_mode = simple_strtoul(__buf, &end, 0);
877 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
878 tsk->seccomp.mode = seccomp_mode;
879 set_tsk_thread_flag(tsk, TIF_SECCOMP);
883 if (unlikely(!(end - __buf)))
885 result = end - __buf;
887 put_task_struct(tsk);
892 static const struct file_operations proc_seccomp_operations = {
893 .read = seccomp_read,
894 .write = seccomp_write,
896 #endif /* CONFIG_SECCOMP */
898 #ifdef CONFIG_FAULT_INJECTION
899 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
900 size_t count, loff_t *ppos)
902 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
903 char buffer[PROC_NUMBUF];
906 loff_t __ppos = *ppos;
910 make_it_fail = task->make_it_fail;
911 put_task_struct(task);
913 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
916 if (count > len-__ppos)
918 if (copy_to_user(buf, buffer + __ppos, count))
920 *ppos = __ppos + count;
924 static ssize_t proc_fault_inject_write(struct file * file,
925 const char __user * buf, size_t count, loff_t *ppos)
927 struct task_struct *task;
928 char buffer[PROC_NUMBUF], *end;
931 if (!capable(CAP_SYS_RESOURCE))
933 memset(buffer, 0, sizeof(buffer));
934 if (count > sizeof(buffer) - 1)
935 count = sizeof(buffer) - 1;
936 if (copy_from_user(buffer, buf, count))
938 make_it_fail = simple_strtol(buffer, &end, 0);
941 task = get_proc_task(file->f_dentry->d_inode);
944 task->make_it_fail = make_it_fail;
945 put_task_struct(task);
946 if (end - buffer == 0)
951 static const struct file_operations proc_fault_inject_operations = {
952 .read = proc_fault_inject_read,
953 .write = proc_fault_inject_write,
957 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
959 struct inode *inode = dentry->d_inode;
962 /* We don't need a base pointer in the /proc filesystem */
965 /* Are we allowed to snoop on the tasks file descriptors? */
966 if (!proc_fd_access_allowed(inode))
969 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
970 nd->last_type = LAST_BIND;
972 return ERR_PTR(error);
975 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
976 char __user *buffer, int buflen)
978 struct inode * inode;
979 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
985 inode = dentry->d_inode;
986 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
990 len = tmp + PAGE_SIZE - 1 - path;
994 if (copy_to_user(buffer, path, len))
997 free_page((unsigned long)tmp);
1001 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1003 int error = -EACCES;
1004 struct inode *inode = dentry->d_inode;
1006 struct vfsmount *mnt = NULL;
1008 /* Are we allowed to snoop on the tasks file descriptors? */
1009 if (!proc_fd_access_allowed(inode))
1012 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1016 error = do_proc_readlink(de, mnt, buffer, buflen);
1023 static const struct inode_operations proc_pid_link_inode_operations = {
1024 .readlink = proc_pid_readlink,
1025 .follow_link = proc_pid_follow_link,
1026 .setattr = proc_setattr,
1030 /* building an inode */
1032 static int task_dumpable(struct task_struct *task)
1035 struct mm_struct *mm;
1040 dumpable = mm->dumpable;
1048 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1050 struct inode * inode;
1051 struct proc_inode *ei;
1053 /* We need a new inode */
1055 inode = new_inode(sb);
1061 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1062 inode->i_op = &proc_def_inode_operations;
1065 * grab the reference to task.
1067 ei->pid = get_task_pid(task, PIDTYPE_PID);
1073 if (task_dumpable(task)) {
1074 inode->i_uid = task->euid;
1075 inode->i_gid = task->egid;
1077 security_task_to_inode(task, inode);
1087 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1089 struct inode *inode = dentry->d_inode;
1090 struct task_struct *task;
1091 generic_fillattr(inode, stat);
1096 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1098 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1099 task_dumpable(task)) {
1100 stat->uid = task->euid;
1101 stat->gid = task->egid;
1111 * Exceptional case: normally we are not allowed to unhash a busy
1112 * directory. In this case, however, we can do it - no aliasing problems
1113 * due to the way we treat inodes.
1115 * Rewrite the inode's ownerships here because the owning task may have
1116 * performed a setuid(), etc.
1118 * Before the /proc/pid/status file was created the only way to read
1119 * the effective uid of a /process was to stat /proc/pid. Reading
1120 * /proc/pid/status is slow enough that procps and other packages
1121 * kept stating /proc/pid. To keep the rules in /proc simple I have
1122 * made this apply to all per process world readable and executable
1125 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1127 struct inode *inode = dentry->d_inode;
1128 struct task_struct *task = get_proc_task(inode);
1130 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1131 task_dumpable(task)) {
1132 inode->i_uid = task->euid;
1133 inode->i_gid = task->egid;
1138 inode->i_mode &= ~(S_ISUID | S_ISGID);
1139 security_task_to_inode(task, inode);
1140 put_task_struct(task);
1147 static int pid_delete_dentry(struct dentry * dentry)
1149 /* Is the task we represent dead?
1150 * If so, then don't put the dentry on the lru list,
1151 * kill it immediately.
1153 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1156 static struct dentry_operations pid_dentry_operations =
1158 .d_revalidate = pid_revalidate,
1159 .d_delete = pid_delete_dentry,
1164 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1165 struct task_struct *, const void *);
1168 * Fill a directory entry.
1170 * If possible create the dcache entry and derive our inode number and
1171 * file type from dcache entry.
1173 * Since all of the proc inode numbers are dynamically generated, the inode
1174 * numbers do not exist until the inode is cache. This means creating the
1175 * the dcache entry in readdir is necessary to keep the inode numbers
1176 * reported by readdir in sync with the inode numbers reported
1179 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1180 char *name, int len,
1181 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1183 struct dentry *child, *dir = filp->f_path.dentry;
1184 struct inode *inode;
1187 unsigned type = DT_UNKNOWN;
1191 qname.hash = full_name_hash(name, len);
1193 child = d_lookup(dir, &qname);
1196 new = d_alloc(dir, &qname);
1198 child = instantiate(dir->d_inode, new, task, ptr);
1205 if (!child || IS_ERR(child) || !child->d_inode)
1206 goto end_instantiate;
1207 inode = child->d_inode;
1210 type = inode->i_mode >> 12;
1215 ino = find_inode_number(dir, &qname);
1218 return filldir(dirent, name, len, filp->f_pos, ino, type);
1221 static unsigned name_to_int(struct dentry *dentry)
1223 const char *name = dentry->d_name.name;
1224 int len = dentry->d_name.len;
1227 if (len > 1 && *name == '0')
1230 unsigned c = *name++ - '0';
1233 if (n >= (~0U-9)/10)
1243 #define PROC_FDINFO_MAX 64
1245 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1246 struct vfsmount **mnt, char *info)
1248 struct task_struct *task = get_proc_task(inode);
1249 struct files_struct *files = NULL;
1251 int fd = proc_fd(inode);
1254 files = get_files_struct(task);
1255 put_task_struct(task);
1259 * We are not taking a ref to the file structure, so we must
1262 spin_lock(&files->file_lock);
1263 file = fcheck_files(files, fd);
1266 *mnt = mntget(file->f_path.mnt);
1268 *dentry = dget(file->f_path.dentry);
1270 snprintf(info, PROC_FDINFO_MAX,
1273 (long long) file->f_pos,
1275 spin_unlock(&files->file_lock);
1276 put_files_struct(files);
1279 spin_unlock(&files->file_lock);
1280 put_files_struct(files);
1285 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1286 struct vfsmount **mnt)
1288 return proc_fd_info(inode, dentry, mnt, NULL);
1291 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1293 struct inode *inode = dentry->d_inode;
1294 struct task_struct *task = get_proc_task(inode);
1295 int fd = proc_fd(inode);
1296 struct files_struct *files;
1299 files = get_files_struct(task);
1302 if (fcheck_files(files, fd)) {
1304 put_files_struct(files);
1305 if (task_dumpable(task)) {
1306 inode->i_uid = task->euid;
1307 inode->i_gid = task->egid;
1312 inode->i_mode &= ~(S_ISUID | S_ISGID);
1313 security_task_to_inode(task, inode);
1314 put_task_struct(task);
1318 put_files_struct(files);
1320 put_task_struct(task);
1326 static struct dentry_operations tid_fd_dentry_operations =
1328 .d_revalidate = tid_fd_revalidate,
1329 .d_delete = pid_delete_dentry,
1332 static struct dentry *proc_fd_instantiate(struct inode *dir,
1333 struct dentry *dentry, struct task_struct *task, const void *ptr)
1335 unsigned fd = *(const unsigned *)ptr;
1337 struct files_struct *files;
1338 struct inode *inode;
1339 struct proc_inode *ei;
1340 struct dentry *error = ERR_PTR(-ENOENT);
1342 inode = proc_pid_make_inode(dir->i_sb, task);
1347 files = get_files_struct(task);
1350 inode->i_mode = S_IFLNK;
1353 * We are not taking a ref to the file structure, so we must
1356 spin_lock(&files->file_lock);
1357 file = fcheck_files(files, fd);
1360 if (file->f_mode & 1)
1361 inode->i_mode |= S_IRUSR | S_IXUSR;
1362 if (file->f_mode & 2)
1363 inode->i_mode |= S_IWUSR | S_IXUSR;
1364 spin_unlock(&files->file_lock);
1365 put_files_struct(files);
1367 inode->i_op = &proc_pid_link_inode_operations;
1369 ei->op.proc_get_link = proc_fd_link;
1370 dentry->d_op = &tid_fd_dentry_operations;
1371 d_add(dentry, inode);
1372 /* Close the race of the process dying before we return the dentry */
1373 if (tid_fd_revalidate(dentry, NULL))
1379 spin_unlock(&files->file_lock);
1380 put_files_struct(files);
1386 static struct dentry *proc_lookupfd_common(struct inode *dir,
1387 struct dentry *dentry,
1388 instantiate_t instantiate)
1390 struct task_struct *task = get_proc_task(dir);
1391 unsigned fd = name_to_int(dentry);
1392 struct dentry *result = ERR_PTR(-ENOENT);
1399 result = instantiate(dir, dentry, task, &fd);
1401 put_task_struct(task);
1406 static int proc_readfd_common(struct file * filp, void * dirent,
1407 filldir_t filldir, instantiate_t instantiate)
1409 struct dentry *dentry = filp->f_path.dentry;
1410 struct inode *inode = dentry->d_inode;
1411 struct task_struct *p = get_proc_task(inode);
1412 unsigned int fd, tid, ino;
1414 struct files_struct * files;
1415 struct fdtable *fdt;
1426 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1430 ino = parent_ino(dentry);
1431 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1435 files = get_files_struct(p);
1439 fdt = files_fdtable(files);
1440 for (fd = filp->f_pos-2;
1442 fd++, filp->f_pos++) {
1443 char name[PROC_NUMBUF];
1446 if (!fcheck_files(files, fd))
1450 len = snprintf(name, sizeof(name), "%d", fd);
1451 if (proc_fill_cache(filp, dirent, filldir,
1452 name, len, instantiate,
1460 put_files_struct(files);
1468 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1469 struct nameidata *nd)
1471 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1474 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1476 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1479 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1480 size_t len, loff_t *ppos)
1482 char tmp[PROC_FDINFO_MAX];
1483 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1485 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1489 static const struct file_operations proc_fdinfo_file_operations = {
1490 .open = nonseekable_open,
1491 .read = proc_fdinfo_read,
1494 static const struct file_operations proc_fd_operations = {
1495 .read = generic_read_dir,
1496 .readdir = proc_readfd,
1500 * /proc/pid/fd needs a special permission handler so that a process can still
1501 * access /proc/self/fd after it has executed a setuid().
1503 static int proc_fd_permission(struct inode *inode, int mask,
1504 struct nameidata *nd)
1508 rv = generic_permission(inode, mask, NULL);
1511 if (task_pid(current) == proc_pid(inode))
1517 * proc directories can do almost nothing..
1519 static const struct inode_operations proc_fd_inode_operations = {
1520 .lookup = proc_lookupfd,
1521 .permission = proc_fd_permission,
1522 .setattr = proc_setattr,
1525 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1526 struct dentry *dentry, struct task_struct *task, const void *ptr)
1528 unsigned fd = *(unsigned *)ptr;
1529 struct inode *inode;
1530 struct proc_inode *ei;
1531 struct dentry *error = ERR_PTR(-ENOENT);
1533 inode = proc_pid_make_inode(dir->i_sb, task);
1538 inode->i_mode = S_IFREG | S_IRUSR;
1539 inode->i_fop = &proc_fdinfo_file_operations;
1540 dentry->d_op = &tid_fd_dentry_operations;
1541 d_add(dentry, inode);
1542 /* Close the race of the process dying before we return the dentry */
1543 if (tid_fd_revalidate(dentry, NULL))
1550 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1551 struct dentry *dentry,
1552 struct nameidata *nd)
1554 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1557 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1559 return proc_readfd_common(filp, dirent, filldir,
1560 proc_fdinfo_instantiate);
1563 static const struct file_operations proc_fdinfo_operations = {
1564 .read = generic_read_dir,
1565 .readdir = proc_readfdinfo,
1569 * proc directories can do almost nothing..
1571 static const struct inode_operations proc_fdinfo_inode_operations = {
1572 .lookup = proc_lookupfdinfo,
1573 .setattr = proc_setattr,
1577 static struct dentry *proc_pident_instantiate(struct inode *dir,
1578 struct dentry *dentry, struct task_struct *task, const void *ptr)
1580 const struct pid_entry *p = ptr;
1581 struct inode *inode;
1582 struct proc_inode *ei;
1583 struct dentry *error = ERR_PTR(-EINVAL);
1585 inode = proc_pid_make_inode(dir->i_sb, task);
1590 inode->i_mode = p->mode;
1591 if (S_ISDIR(inode->i_mode))
1592 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1594 inode->i_op = p->iop;
1596 inode->i_fop = p->fop;
1598 dentry->d_op = &pid_dentry_operations;
1599 d_add(dentry, inode);
1600 /* Close the race of the process dying before we return the dentry */
1601 if (pid_revalidate(dentry, NULL))
1607 static struct dentry *proc_pident_lookup(struct inode *dir,
1608 struct dentry *dentry,
1609 const struct pid_entry *ents,
1612 struct inode *inode;
1613 struct dentry *error;
1614 struct task_struct *task = get_proc_task(dir);
1615 const struct pid_entry *p, *last;
1617 error = ERR_PTR(-ENOENT);
1624 * Yes, it does not scale. And it should not. Don't add
1625 * new entries into /proc/<tgid>/ without very good reasons.
1627 last = &ents[nents - 1];
1628 for (p = ents; p <= last; p++) {
1629 if (p->len != dentry->d_name.len)
1631 if (!memcmp(dentry->d_name.name, p->name, p->len))
1637 error = proc_pident_instantiate(dir, dentry, task, p);
1639 put_task_struct(task);
1644 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1645 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1647 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1648 proc_pident_instantiate, task, p);
1651 static int proc_pident_readdir(struct file *filp,
1652 void *dirent, filldir_t filldir,
1653 const struct pid_entry *ents, unsigned int nents)
1657 struct dentry *dentry = filp->f_path.dentry;
1658 struct inode *inode = dentry->d_inode;
1659 struct task_struct *task = get_proc_task(inode);
1660 const struct pid_entry *p, *last;
1674 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1680 ino = parent_ino(dentry);
1681 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1693 last = &ents[nents - 1];
1695 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1704 put_task_struct(task);
1709 #ifdef CONFIG_SECURITY
1710 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1711 size_t count, loff_t *ppos)
1713 struct inode * inode = file->f_path.dentry->d_inode;
1716 struct task_struct *task = get_proc_task(inode);
1721 length = security_getprocattr(task,
1722 (char*)file->f_path.dentry->d_name.name,
1724 put_task_struct(task);
1726 length = simple_read_from_buffer(buf, count, ppos, p, length);
1731 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1732 size_t count, loff_t *ppos)
1734 struct inode * inode = file->f_path.dentry->d_inode;
1737 struct task_struct *task = get_proc_task(inode);
1742 if (count > PAGE_SIZE)
1745 /* No partial writes. */
1751 page = (char*)__get_free_page(GFP_USER);
1756 if (copy_from_user(page, buf, count))
1759 length = security_setprocattr(task,
1760 (char*)file->f_path.dentry->d_name.name,
1761 (void*)page, count);
1763 free_page((unsigned long) page);
1765 put_task_struct(task);
1770 static const struct file_operations proc_pid_attr_operations = {
1771 .read = proc_pid_attr_read,
1772 .write = proc_pid_attr_write,
1775 static const struct pid_entry attr_dir_stuff[] = {
1776 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1777 REG("prev", S_IRUGO, pid_attr),
1778 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1779 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1780 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1781 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1784 static int proc_attr_dir_readdir(struct file * filp,
1785 void * dirent, filldir_t filldir)
1787 return proc_pident_readdir(filp,dirent,filldir,
1788 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1791 static const struct file_operations proc_attr_dir_operations = {
1792 .read = generic_read_dir,
1793 .readdir = proc_attr_dir_readdir,
1796 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1797 struct dentry *dentry, struct nameidata *nd)
1799 return proc_pident_lookup(dir, dentry,
1800 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1803 static const struct inode_operations proc_attr_dir_inode_operations = {
1804 .lookup = proc_attr_dir_lookup,
1805 .getattr = pid_getattr,
1806 .setattr = proc_setattr,
1814 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1817 char tmp[PROC_NUMBUF];
1818 sprintf(tmp, "%d", current->tgid);
1819 return vfs_readlink(dentry,buffer,buflen,tmp);
1822 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1824 char tmp[PROC_NUMBUF];
1825 sprintf(tmp, "%d", current->tgid);
1826 return ERR_PTR(vfs_follow_link(nd,tmp));
1829 static const struct inode_operations proc_self_inode_operations = {
1830 .readlink = proc_self_readlink,
1831 .follow_link = proc_self_follow_link,
1837 * These are the directory entries in the root directory of /proc
1838 * that properly belong to the /proc filesystem, as they describe
1839 * describe something that is process related.
1841 static const struct pid_entry proc_base_stuff[] = {
1842 NOD("self", S_IFLNK|S_IRWXUGO,
1843 &proc_self_inode_operations, NULL, {}),
1847 * Exceptional case: normally we are not allowed to unhash a busy
1848 * directory. In this case, however, we can do it - no aliasing problems
1849 * due to the way we treat inodes.
1851 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1853 struct inode *inode = dentry->d_inode;
1854 struct task_struct *task = get_proc_task(inode);
1856 put_task_struct(task);
1863 static struct dentry_operations proc_base_dentry_operations =
1865 .d_revalidate = proc_base_revalidate,
1866 .d_delete = pid_delete_dentry,
1869 static struct dentry *proc_base_instantiate(struct inode *dir,
1870 struct dentry *dentry, struct task_struct *task, const void *ptr)
1872 const struct pid_entry *p = ptr;
1873 struct inode *inode;
1874 struct proc_inode *ei;
1875 struct dentry *error = ERR_PTR(-EINVAL);
1877 /* Allocate the inode */
1878 error = ERR_PTR(-ENOMEM);
1879 inode = new_inode(dir->i_sb);
1883 /* Initialize the inode */
1885 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1888 * grab the reference to the task.
1890 ei->pid = get_task_pid(task, PIDTYPE_PID);
1896 inode->i_mode = p->mode;
1897 if (S_ISDIR(inode->i_mode))
1899 if (S_ISLNK(inode->i_mode))
1902 inode->i_op = p->iop;
1904 inode->i_fop = p->fop;
1906 dentry->d_op = &proc_base_dentry_operations;
1907 d_add(dentry, inode);
1916 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1918 struct dentry *error;
1919 struct task_struct *task = get_proc_task(dir);
1920 const struct pid_entry *p, *last;
1922 error = ERR_PTR(-ENOENT);
1927 /* Lookup the directory entry */
1928 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1929 for (p = proc_base_stuff; p <= last; p++) {
1930 if (p->len != dentry->d_name.len)
1932 if (!memcmp(dentry->d_name.name, p->name, p->len))
1938 error = proc_base_instantiate(dir, dentry, task, p);
1941 put_task_struct(task);
1946 static int proc_base_fill_cache(struct file *filp, void *dirent,
1947 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1949 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1950 proc_base_instantiate, task, p);
1953 #ifdef CONFIG_TASK_IO_ACCOUNTING
1954 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1956 return sprintf(buffer,
1957 #ifdef CONFIG_TASK_XACCT
1963 "read_bytes: %llu\n"
1964 "write_bytes: %llu\n"
1965 "cancelled_write_bytes: %llu\n",
1966 #ifdef CONFIG_TASK_XACCT
1967 (unsigned long long)task->rchar,
1968 (unsigned long long)task->wchar,
1969 (unsigned long long)task->syscr,
1970 (unsigned long long)task->syscw,
1972 (unsigned long long)task->ioac.read_bytes,
1973 (unsigned long long)task->ioac.write_bytes,
1974 (unsigned long long)task->ioac.cancelled_write_bytes);
1981 static const struct file_operations proc_task_operations;
1982 static const struct inode_operations proc_task_inode_operations;
1984 static const struct pid_entry tgid_base_stuff[] = {
1985 DIR("task", S_IRUGO|S_IXUGO, task),
1986 DIR("fd", S_IRUSR|S_IXUSR, fd),
1987 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
1988 INF("environ", S_IRUSR, pid_environ),
1989 INF("auxv", S_IRUSR, pid_auxv),
1990 INF("status", S_IRUGO, pid_status),
1991 INF("cmdline", S_IRUGO, pid_cmdline),
1992 INF("stat", S_IRUGO, tgid_stat),
1993 INF("statm", S_IRUGO, pid_statm),
1994 REG("maps", S_IRUGO, maps),
1996 REG("numa_maps", S_IRUGO, numa_maps),
1998 REG("mem", S_IRUSR|S_IWUSR, mem),
1999 #ifdef CONFIG_SECCOMP
2000 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2005 REG("mounts", S_IRUGO, mounts),
2006 REG("mountstats", S_IRUSR, mountstats),
2008 REG("clear_refs", S_IWUSR, clear_refs),
2009 REG("smaps", S_IRUGO, smaps),
2011 #ifdef CONFIG_SECURITY
2012 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2014 #ifdef CONFIG_KALLSYMS
2015 INF("wchan", S_IRUGO, pid_wchan),
2017 #ifdef CONFIG_SCHEDSTATS
2018 INF("schedstat", S_IRUGO, pid_schedstat),
2020 #ifdef CONFIG_CPUSETS
2021 REG("cpuset", S_IRUGO, cpuset),
2023 INF("oom_score", S_IRUGO, oom_score),
2024 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2025 #ifdef CONFIG_AUDITSYSCALL
2026 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2028 #ifdef CONFIG_FAULT_INJECTION
2029 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2031 #ifdef CONFIG_TASK_IO_ACCOUNTING
2032 INF("io", S_IRUGO, pid_io_accounting),
2036 static int proc_tgid_base_readdir(struct file * filp,
2037 void * dirent, filldir_t filldir)
2039 return proc_pident_readdir(filp,dirent,filldir,
2040 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2043 static const struct file_operations proc_tgid_base_operations = {
2044 .read = generic_read_dir,
2045 .readdir = proc_tgid_base_readdir,
2048 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2049 return proc_pident_lookup(dir, dentry,
2050 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2053 static const struct inode_operations proc_tgid_base_inode_operations = {
2054 .lookup = proc_tgid_base_lookup,
2055 .getattr = pid_getattr,
2056 .setattr = proc_setattr,
2060 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2062 * @task: task that should be flushed.
2064 * Looks in the dcache for
2066 * /proc/@tgid/task/@pid
2067 * if either directory is present flushes it and all of it'ts children
2070 * It is safe and reasonable to cache /proc entries for a task until
2071 * that task exits. After that they just clog up the dcache with
2072 * useless entries, possibly causing useful dcache entries to be
2073 * flushed instead. This routine is proved to flush those useless
2074 * dcache entries at process exit time.
2076 * NOTE: This routine is just an optimization so it does not guarantee
2077 * that no dcache entries will exist at process exit time it
2078 * just makes it very unlikely that any will persist.
2080 void proc_flush_task(struct task_struct *task)
2082 struct dentry *dentry, *leader, *dir;
2083 char buf[PROC_NUMBUF];
2087 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2088 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2090 shrink_dcache_parent(dentry);
2095 if (thread_group_leader(task))
2099 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2100 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2105 name.len = strlen(name.name);
2106 dir = d_hash_and_lookup(leader, &name);
2108 goto out_put_leader;
2111 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2112 dentry = d_hash_and_lookup(dir, &name);
2114 shrink_dcache_parent(dentry);
2126 static struct dentry *proc_pid_instantiate(struct inode *dir,
2127 struct dentry * dentry,
2128 struct task_struct *task, const void *ptr)
2130 struct dentry *error = ERR_PTR(-ENOENT);
2131 struct inode *inode;
2133 inode = proc_pid_make_inode(dir->i_sb, task);
2137 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2138 inode->i_op = &proc_tgid_base_inode_operations;
2139 inode->i_fop = &proc_tgid_base_operations;
2140 inode->i_flags|=S_IMMUTABLE;
2142 #ifdef CONFIG_SECURITY
2143 inode->i_nlink += 1;
2146 dentry->d_op = &pid_dentry_operations;
2148 d_add(dentry, inode);
2149 /* Close the race of the process dying before we return the dentry */
2150 if (pid_revalidate(dentry, NULL))
2156 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2158 struct dentry *result = ERR_PTR(-ENOENT);
2159 struct task_struct *task;
2162 result = proc_base_lookup(dir, dentry);
2163 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2166 tgid = name_to_int(dentry);
2171 task = find_task_by_pid(tgid);
2173 get_task_struct(task);
2178 result = proc_pid_instantiate(dir, dentry, task, NULL);
2179 put_task_struct(task);
2185 * Find the first task with tgid >= tgid
2188 static struct task_struct *next_tgid(unsigned int tgid)
2190 struct task_struct *task;
2196 pid = find_ge_pid(tgid);
2199 task = pid_task(pid, PIDTYPE_PID);
2200 /* What we to know is if the pid we have find is the
2201 * pid of a thread_group_leader. Testing for task
2202 * being a thread_group_leader is the obvious thing
2203 * todo but there is a window when it fails, due to
2204 * the pid transfer logic in de_thread.
2206 * So we perform the straight forward test of seeing
2207 * if the pid we have found is the pid of a thread
2208 * group leader, and don't worry if the task we have
2209 * found doesn't happen to be a thread group leader.
2210 * As we don't care in the case of readdir.
2212 if (!task || !has_group_leader_pid(task))
2214 get_task_struct(task);
2220 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2222 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2223 struct task_struct *task, int tgid)
2225 char name[PROC_NUMBUF];
2226 int len = snprintf(name, sizeof(name), "%d", tgid);
2227 return proc_fill_cache(filp, dirent, filldir, name, len,
2228 proc_pid_instantiate, task, NULL);
2231 /* for the /proc/ directory itself, after non-process stuff has been done */
2232 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2234 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2235 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2236 struct task_struct *task;
2242 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2243 const struct pid_entry *p = &proc_base_stuff[nr];
2244 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2248 tgid = filp->f_pos - TGID_OFFSET;
2249 for (task = next_tgid(tgid);
2251 put_task_struct(task), task = next_tgid(tgid + 1)) {
2253 filp->f_pos = tgid + TGID_OFFSET;
2254 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2255 put_task_struct(task);
2259 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2261 put_task_struct(reaper);
2269 static const struct pid_entry tid_base_stuff[] = {
2270 DIR("fd", S_IRUSR|S_IXUSR, fd),
2271 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2272 INF("environ", S_IRUSR, pid_environ),
2273 INF("auxv", S_IRUSR, pid_auxv),
2274 INF("status", S_IRUGO, pid_status),
2275 INF("cmdline", S_IRUGO, pid_cmdline),
2276 INF("stat", S_IRUGO, tid_stat),
2277 INF("statm", S_IRUGO, pid_statm),
2278 REG("maps", S_IRUGO, maps),
2280 REG("numa_maps", S_IRUGO, numa_maps),
2282 REG("mem", S_IRUSR|S_IWUSR, mem),
2283 #ifdef CONFIG_SECCOMP
2284 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2289 REG("mounts", S_IRUGO, mounts),
2291 REG("clear_refs", S_IWUSR, clear_refs),
2292 REG("smaps", S_IRUGO, smaps),
2294 #ifdef CONFIG_SECURITY
2295 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2297 #ifdef CONFIG_KALLSYMS
2298 INF("wchan", S_IRUGO, pid_wchan),
2300 #ifdef CONFIG_SCHEDSTATS
2301 INF("schedstat", S_IRUGO, pid_schedstat),
2303 #ifdef CONFIG_CPUSETS
2304 REG("cpuset", S_IRUGO, cpuset),
2306 INF("oom_score", S_IRUGO, oom_score),
2307 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2308 #ifdef CONFIG_AUDITSYSCALL
2309 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2311 #ifdef CONFIG_FAULT_INJECTION
2312 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2316 static int proc_tid_base_readdir(struct file * filp,
2317 void * dirent, filldir_t filldir)
2319 return proc_pident_readdir(filp,dirent,filldir,
2320 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2323 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2324 return proc_pident_lookup(dir, dentry,
2325 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2328 static const struct file_operations proc_tid_base_operations = {
2329 .read = generic_read_dir,
2330 .readdir = proc_tid_base_readdir,
2333 static const struct inode_operations proc_tid_base_inode_operations = {
2334 .lookup = proc_tid_base_lookup,
2335 .getattr = pid_getattr,
2336 .setattr = proc_setattr,
2339 static struct dentry *proc_task_instantiate(struct inode *dir,
2340 struct dentry *dentry, struct task_struct *task, const void *ptr)
2342 struct dentry *error = ERR_PTR(-ENOENT);
2343 struct inode *inode;
2344 inode = proc_pid_make_inode(dir->i_sb, task);
2348 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2349 inode->i_op = &proc_tid_base_inode_operations;
2350 inode->i_fop = &proc_tid_base_operations;
2351 inode->i_flags|=S_IMMUTABLE;
2353 #ifdef CONFIG_SECURITY
2354 inode->i_nlink += 1;
2357 dentry->d_op = &pid_dentry_operations;
2359 d_add(dentry, inode);
2360 /* Close the race of the process dying before we return the dentry */
2361 if (pid_revalidate(dentry, NULL))
2367 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2369 struct dentry *result = ERR_PTR(-ENOENT);
2370 struct task_struct *task;
2371 struct task_struct *leader = get_proc_task(dir);
2377 tid = name_to_int(dentry);
2382 task = find_task_by_pid(tid);
2384 get_task_struct(task);
2388 if (leader->tgid != task->tgid)
2391 result = proc_task_instantiate(dir, dentry, task, NULL);
2393 put_task_struct(task);
2395 put_task_struct(leader);
2401 * Find the first tid of a thread group to return to user space.
2403 * Usually this is just the thread group leader, but if the users
2404 * buffer was too small or there was a seek into the middle of the
2405 * directory we have more work todo.
2407 * In the case of a short read we start with find_task_by_pid.
2409 * In the case of a seek we start with the leader and walk nr
2412 static struct task_struct *first_tid(struct task_struct *leader,
2415 struct task_struct *pos;
2418 /* Attempt to start with the pid of a thread */
2419 if (tid && (nr > 0)) {
2420 pos = find_task_by_pid(tid);
2421 if (pos && (pos->group_leader == leader))
2425 /* If nr exceeds the number of threads there is nothing todo */
2427 if (nr && nr >= get_nr_threads(leader))
2430 /* If we haven't found our starting place yet start
2431 * with the leader and walk nr threads forward.
2433 for (pos = leader; nr > 0; --nr) {
2434 pos = next_thread(pos);
2435 if (pos == leader) {
2441 get_task_struct(pos);
2448 * Find the next thread in the thread list.
2449 * Return NULL if there is an error or no next thread.
2451 * The reference to the input task_struct is released.
2453 static struct task_struct *next_tid(struct task_struct *start)
2455 struct task_struct *pos = NULL;
2457 if (pid_alive(start)) {
2458 pos = next_thread(start);
2459 if (thread_group_leader(pos))
2462 get_task_struct(pos);
2465 put_task_struct(start);
2469 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2470 struct task_struct *task, int tid)
2472 char name[PROC_NUMBUF];
2473 int len = snprintf(name, sizeof(name), "%d", tid);
2474 return proc_fill_cache(filp, dirent, filldir, name, len,
2475 proc_task_instantiate, task, NULL);
2478 /* for the /proc/TGID/task/ directories */
2479 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2481 struct dentry *dentry = filp->f_path.dentry;
2482 struct inode *inode = dentry->d_inode;
2483 struct task_struct *leader = NULL;
2484 struct task_struct *task;
2485 int retval = -ENOENT;
2488 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2490 task = get_proc_task(inode);
2494 if (pid_alive(task)) {
2495 leader = task->group_leader;
2496 get_task_struct(leader);
2499 put_task_struct(task);
2507 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2512 ino = parent_ino(dentry);
2513 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2519 /* f_version caches the tgid value that the last readdir call couldn't
2520 * return. lseek aka telldir automagically resets f_version to 0.
2522 tid = filp->f_version;
2523 filp->f_version = 0;
2524 for (task = first_tid(leader, tid, pos - 2);
2526 task = next_tid(task), pos++) {
2528 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2529 /* returning this tgid failed, save it as the first
2530 * pid for the next readir call */
2531 filp->f_version = tid;
2532 put_task_struct(task);
2538 put_task_struct(leader);
2543 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2545 struct inode *inode = dentry->d_inode;
2546 struct task_struct *p = get_proc_task(inode);
2547 generic_fillattr(inode, stat);
2551 stat->nlink += get_nr_threads(p);
2559 static const struct inode_operations proc_task_inode_operations = {
2560 .lookup = proc_task_lookup,
2561 .getattr = proc_task_getattr,
2562 .setattr = proc_setattr,
2565 static const struct file_operations proc_task_operations = {
2566 .read = generic_read_dir,
2567 .readdir = proc_task_readdir,