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/rcupdate.h>
65 #include <linux/kallsyms.h>
66 #include <linux/module.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) { \
103 .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 } )
127 EXPORT_SYMBOL(maps_protect);
129 static struct fs_struct *get_fs_struct(struct task_struct *task)
131 struct fs_struct *fs;
135 atomic_inc(&fs->count);
140 static int get_nr_threads(struct task_struct *tsk)
142 /* Must be called with the rcu_read_lock held */
146 if (lock_task_sighand(tsk, &flags)) {
147 count = atomic_read(&tsk->signal->count);
148 unlock_task_sighand(tsk, &flags);
153 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155 struct task_struct *task = get_proc_task(inode);
156 struct fs_struct *fs = NULL;
157 int result = -ENOENT;
160 fs = get_fs_struct(task);
161 put_task_struct(task);
164 read_lock(&fs->lock);
165 *mnt = mntget(fs->pwdmnt);
166 *dentry = dget(fs->pwd);
167 read_unlock(&fs->lock);
174 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176 struct task_struct *task = get_proc_task(inode);
177 struct fs_struct *fs = NULL;
178 int result = -ENOENT;
181 fs = get_fs_struct(task);
182 put_task_struct(task);
185 read_lock(&fs->lock);
186 *mnt = mntget(fs->rootmnt);
187 *dentry = dget(fs->root);
188 read_unlock(&fs->lock);
195 #define MAY_PTRACE(task) \
196 (task == current || \
197 (task->parent == current && \
198 (task->ptrace & PT_PTRACED) && \
199 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
200 security_ptrace(current,task) == 0))
202 static int proc_pid_environ(struct task_struct *task, char * buffer)
205 struct mm_struct *mm = get_task_mm(task);
210 if (!ptrace_may_attach(task))
213 len = mm->env_end - mm->env_start;
216 res = access_process_vm(task, mm->env_start, buffer, len, 0);
223 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
227 struct mm_struct *mm = get_task_mm(task);
231 goto out_mm; /* Shh! No looking before we're done */
233 len = mm->arg_end - mm->arg_start;
238 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
240 // If the nul at the end of args has been overwritten, then
241 // assume application is using setproctitle(3).
242 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
243 len = strnlen(buffer, res);
247 len = mm->env_end - mm->env_start;
248 if (len > PAGE_SIZE - res)
249 len = PAGE_SIZE - res;
250 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
251 res = strnlen(buffer, res);
260 static int proc_pid_auxv(struct task_struct *task, char *buffer)
263 struct mm_struct *mm = get_task_mm(task);
265 unsigned int nwords = 0;
268 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
269 res = nwords * sizeof(mm->saved_auxv[0]);
272 memcpy(buffer, mm->saved_auxv, res);
279 #ifdef CONFIG_KALLSYMS
281 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
282 * Returns the resolved symbol. If that fails, simply return the address.
284 static int proc_pid_wchan(struct task_struct *task, char *buffer)
287 char symname[KSYM_NAME_LEN+1];
289 wchan = get_wchan(task);
291 if (lookup_symbol_name(wchan, symname) < 0)
292 return sprintf(buffer, "%lu", wchan);
294 return sprintf(buffer, "%s", symname);
296 #endif /* CONFIG_KALLSYMS */
298 #ifdef CONFIG_SCHEDSTATS
300 * Provides /proc/PID/schedstat
302 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
304 return sprintf(buffer, "%llu %llu %lu\n",
305 task->sched_info.cpu_time,
306 task->sched_info.run_delay,
307 task->sched_info.pcnt);
311 /* The badness from the OOM killer */
312 unsigned long badness(struct task_struct *p, unsigned long uptime);
313 static int proc_oom_score(struct task_struct *task, char *buffer)
315 unsigned long points;
316 struct timespec uptime;
318 do_posix_clock_monotonic_gettime(&uptime);
319 read_lock(&tasklist_lock);
320 points = badness(task, uptime.tv_sec);
321 read_unlock(&tasklist_lock);
322 return sprintf(buffer, "%lu\n", points);
325 /************************************************************************/
326 /* Here the fs part begins */
327 /************************************************************************/
329 /* permission checks */
330 static int proc_fd_access_allowed(struct inode *inode)
332 struct task_struct *task;
334 /* Allow access to a task's file descriptors if it is us or we
335 * may use ptrace attach to the process and find out that
338 task = get_proc_task(inode);
340 allowed = ptrace_may_attach(task);
341 put_task_struct(task);
346 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
349 struct inode *inode = dentry->d_inode;
351 if (attr->ia_valid & ATTR_MODE)
354 error = inode_change_ok(inode, attr);
356 error = inode_setattr(inode, attr);
360 static const struct inode_operations proc_def_inode_operations = {
361 .setattr = proc_setattr,
364 extern struct seq_operations mounts_op;
370 static int mounts_open(struct inode *inode, struct file *file)
372 struct task_struct *task = get_proc_task(inode);
373 struct mnt_namespace *ns = NULL;
374 struct proc_mounts *p;
380 ns = task->nsproxy->mnt_ns;
385 put_task_struct(task);
390 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
392 file->private_data = &p->m;
393 ret = seq_open(file, &mounts_op);
396 p->event = ns->event;
406 static int mounts_release(struct inode *inode, struct file *file)
408 struct seq_file *m = file->private_data;
409 struct mnt_namespace *ns = m->private;
411 return seq_release(inode, file);
414 static unsigned mounts_poll(struct file *file, poll_table *wait)
416 struct proc_mounts *p = file->private_data;
417 struct mnt_namespace *ns = p->m.private;
420 poll_wait(file, &ns->poll, wait);
422 spin_lock(&vfsmount_lock);
423 if (p->event != ns->event) {
424 p->event = ns->event;
427 spin_unlock(&vfsmount_lock);
432 static const struct file_operations proc_mounts_operations = {
436 .release = mounts_release,
440 extern struct seq_operations mountstats_op;
441 static int mountstats_open(struct inode *inode, struct file *file)
443 int ret = seq_open(file, &mountstats_op);
446 struct seq_file *m = file->private_data;
447 struct mnt_namespace *mnt_ns = NULL;
448 struct task_struct *task = get_proc_task(inode);
453 mnt_ns = task->nsproxy->mnt_ns;
457 put_task_struct(task);
463 seq_release(inode, file);
470 static const struct file_operations proc_mountstats_operations = {
471 .open = mountstats_open,
474 .release = mounts_release,
477 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
479 static ssize_t proc_info_read(struct file * file, char __user * buf,
480 size_t count, loff_t *ppos)
482 struct inode * inode = file->f_path.dentry->d_inode;
485 struct task_struct *task = get_proc_task(inode);
491 if (count > PROC_BLOCK_SIZE)
492 count = PROC_BLOCK_SIZE;
495 if (!(page = __get_free_page(GFP_KERNEL)))
498 length = PROC_I(inode)->op.proc_read(task, (char*)page);
501 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
504 put_task_struct(task);
509 static const struct file_operations proc_info_file_operations = {
510 .read = proc_info_read,
513 static int mem_open(struct inode* inode, struct file* file)
515 file->private_data = (void*)((long)current->self_exec_id);
519 static ssize_t mem_read(struct file * file, char __user * buf,
520 size_t count, loff_t *ppos)
522 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
524 unsigned long src = *ppos;
526 struct mm_struct *mm;
531 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
535 page = (char *)__get_free_page(GFP_USER);
541 mm = get_task_mm(task);
547 if (file->private_data != (void*)((long)current->self_exec_id))
553 int this_len, retval;
555 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
556 retval = access_process_vm(task, src, page, this_len, 0);
557 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
563 if (copy_to_user(buf, page, retval)) {
578 free_page((unsigned long) page);
580 put_task_struct(task);
585 #define mem_write NULL
588 /* This is a security hazard */
589 static ssize_t mem_write(struct file * file, const char __user *buf,
590 size_t count, loff_t *ppos)
594 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
595 unsigned long dst = *ppos;
601 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
605 page = (char *)__get_free_page(GFP_USER);
611 int this_len, retval;
613 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
614 if (copy_from_user(page, buf, this_len)) {
618 retval = access_process_vm(task, dst, page, this_len, 1);
630 free_page((unsigned long) page);
632 put_task_struct(task);
638 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
642 file->f_pos = offset;
645 file->f_pos += offset;
650 force_successful_syscall_return();
654 static const struct file_operations proc_mem_operations = {
661 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
662 size_t count, loff_t *ppos)
664 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
665 char buffer[PROC_NUMBUF];
671 oom_adjust = task->oomkilladj;
672 put_task_struct(task);
674 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
676 return simple_read_from_buffer(buf, count, ppos, buffer, len);
679 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
680 size_t count, loff_t *ppos)
682 struct task_struct *task;
683 char buffer[PROC_NUMBUF], *end;
686 memset(buffer, 0, sizeof(buffer));
687 if (count > sizeof(buffer) - 1)
688 count = sizeof(buffer) - 1;
689 if (copy_from_user(buffer, buf, count))
691 oom_adjust = simple_strtol(buffer, &end, 0);
692 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
693 oom_adjust != OOM_DISABLE)
697 task = get_proc_task(file->f_path.dentry->d_inode);
700 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
701 put_task_struct(task);
704 task->oomkilladj = oom_adjust;
705 put_task_struct(task);
706 if (end - buffer == 0)
711 static const struct file_operations proc_oom_adjust_operations = {
712 .read = oom_adjust_read,
713 .write = oom_adjust_write,
717 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
718 size_t count, loff_t *ppos)
720 struct task_struct *task;
721 char buffer[PROC_NUMBUF], *end;
722 struct mm_struct *mm;
724 memset(buffer, 0, sizeof(buffer));
725 if (count > sizeof(buffer) - 1)
726 count = sizeof(buffer) - 1;
727 if (copy_from_user(buffer, buf, count))
729 if (!simple_strtol(buffer, &end, 0))
733 task = get_proc_task(file->f_path.dentry->d_inode);
736 mm = get_task_mm(task);
741 put_task_struct(task);
742 if (end - buffer == 0)
747 static struct file_operations proc_clear_refs_operations = {
748 .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);
830 /* no need to print the trailing zero, so use only len */
831 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
832 put_task_struct(tsk);
834 return simple_read_from_buffer(buf, count, ppos, __buf, len);
837 static ssize_t seccomp_write(struct file *file, const char __user *buf,
838 size_t count, loff_t *ppos)
840 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
841 char __buf[20], *end;
842 unsigned int seccomp_mode;
849 /* can set it only once to be even more secure */
851 if (unlikely(tsk->seccomp.mode))
855 memset(__buf, 0, sizeof(__buf));
856 count = min(count, sizeof(__buf) - 1);
857 if (copy_from_user(__buf, buf, count))
860 seccomp_mode = simple_strtoul(__buf, &end, 0);
864 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
865 tsk->seccomp.mode = seccomp_mode;
866 set_tsk_thread_flag(tsk, TIF_SECCOMP);
870 if (unlikely(!(end - __buf)))
872 result = end - __buf;
874 put_task_struct(tsk);
879 static const struct file_operations proc_seccomp_operations = {
880 .read = seccomp_read,
881 .write = seccomp_write,
883 #endif /* CONFIG_SECCOMP */
885 #ifdef CONFIG_FAULT_INJECTION
886 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
887 size_t count, loff_t *ppos)
889 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
890 char buffer[PROC_NUMBUF];
896 make_it_fail = task->make_it_fail;
897 put_task_struct(task);
899 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
901 return simple_read_from_buffer(buf, count, ppos, buffer, len);
904 static ssize_t proc_fault_inject_write(struct file * file,
905 const char __user * buf, size_t count, loff_t *ppos)
907 struct task_struct *task;
908 char buffer[PROC_NUMBUF], *end;
911 if (!capable(CAP_SYS_RESOURCE))
913 memset(buffer, 0, sizeof(buffer));
914 if (count > sizeof(buffer) - 1)
915 count = sizeof(buffer) - 1;
916 if (copy_from_user(buffer, buf, count))
918 make_it_fail = simple_strtol(buffer, &end, 0);
921 task = get_proc_task(file->f_dentry->d_inode);
924 task->make_it_fail = make_it_fail;
925 put_task_struct(task);
926 if (end - buffer == 0)
931 static const struct file_operations proc_fault_inject_operations = {
932 .read = proc_fault_inject_read,
933 .write = proc_fault_inject_write,
937 #ifdef CONFIG_SCHED_DEBUG
939 * Print out various scheduling related per-task fields:
941 static int sched_show(struct seq_file *m, void *v)
943 struct inode *inode = m->private;
944 struct task_struct *p;
948 p = get_proc_task(inode);
951 proc_sched_show_task(p, m);
959 sched_write(struct file *file, const char __user *buf,
960 size_t count, loff_t *offset)
962 struct inode *inode = file->f_path.dentry->d_inode;
963 struct task_struct *p;
967 p = get_proc_task(inode);
970 proc_sched_set_task(p);
977 static int sched_open(struct inode *inode, struct file *filp)
981 ret = single_open(filp, sched_show, NULL);
983 struct seq_file *m = filp->private_data;
990 static const struct file_operations proc_pid_sched_operations = {
993 .write = sched_write,
995 .release = seq_release,
1000 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1002 struct inode *inode = dentry->d_inode;
1003 int error = -EACCES;
1005 /* We don't need a base pointer in the /proc filesystem */
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, &nd->dentry, &nd->mnt);
1013 nd->last_type = LAST_BIND;
1015 return ERR_PTR(error);
1018 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1019 char __user *buffer, int buflen)
1021 struct inode * inode;
1022 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1028 inode = dentry->d_inode;
1029 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1030 len = PTR_ERR(path);
1033 len = tmp + PAGE_SIZE - 1 - path;
1037 if (copy_to_user(buffer, path, len))
1040 free_page((unsigned long)tmp);
1044 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1046 int error = -EACCES;
1047 struct inode *inode = dentry->d_inode;
1049 struct vfsmount *mnt = NULL;
1051 /* Are we allowed to snoop on the tasks file descriptors? */
1052 if (!proc_fd_access_allowed(inode))
1055 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1059 error = do_proc_readlink(de, mnt, buffer, buflen);
1066 static const struct inode_operations proc_pid_link_inode_operations = {
1067 .readlink = proc_pid_readlink,
1068 .follow_link = proc_pid_follow_link,
1069 .setattr = proc_setattr,
1073 /* building an inode */
1075 static int task_dumpable(struct task_struct *task)
1078 struct mm_struct *mm;
1083 dumpable = mm->dumpable;
1091 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1093 struct inode * inode;
1094 struct proc_inode *ei;
1096 /* We need a new inode */
1098 inode = new_inode(sb);
1104 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1105 inode->i_op = &proc_def_inode_operations;
1108 * grab the reference to task.
1110 ei->pid = get_task_pid(task, PIDTYPE_PID);
1116 if (task_dumpable(task)) {
1117 inode->i_uid = task->euid;
1118 inode->i_gid = task->egid;
1120 security_task_to_inode(task, inode);
1130 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1132 struct inode *inode = dentry->d_inode;
1133 struct task_struct *task;
1134 generic_fillattr(inode, stat);
1139 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1141 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1142 task_dumpable(task)) {
1143 stat->uid = task->euid;
1144 stat->gid = task->egid;
1154 * Exceptional case: normally we are not allowed to unhash a busy
1155 * directory. In this case, however, we can do it - no aliasing problems
1156 * due to the way we treat inodes.
1158 * Rewrite the inode's ownerships here because the owning task may have
1159 * performed a setuid(), etc.
1161 * Before the /proc/pid/status file was created the only way to read
1162 * the effective uid of a /process was to stat /proc/pid. Reading
1163 * /proc/pid/status is slow enough that procps and other packages
1164 * kept stating /proc/pid. To keep the rules in /proc simple I have
1165 * made this apply to all per process world readable and executable
1168 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1170 struct inode *inode = dentry->d_inode;
1171 struct task_struct *task = get_proc_task(inode);
1173 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1174 task_dumpable(task)) {
1175 inode->i_uid = task->euid;
1176 inode->i_gid = task->egid;
1181 inode->i_mode &= ~(S_ISUID | S_ISGID);
1182 security_task_to_inode(task, inode);
1183 put_task_struct(task);
1190 static int pid_delete_dentry(struct dentry * dentry)
1192 /* Is the task we represent dead?
1193 * If so, then don't put the dentry on the lru list,
1194 * kill it immediately.
1196 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1199 static struct dentry_operations pid_dentry_operations =
1201 .d_revalidate = pid_revalidate,
1202 .d_delete = pid_delete_dentry,
1207 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1208 struct task_struct *, const void *);
1211 * Fill a directory entry.
1213 * If possible create the dcache entry and derive our inode number and
1214 * file type from dcache entry.
1216 * Since all of the proc inode numbers are dynamically generated, the inode
1217 * numbers do not exist until the inode is cache. This means creating the
1218 * the dcache entry in readdir is necessary to keep the inode numbers
1219 * reported by readdir in sync with the inode numbers reported
1222 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1223 char *name, int len,
1224 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1226 struct dentry *child, *dir = filp->f_path.dentry;
1227 struct inode *inode;
1230 unsigned type = DT_UNKNOWN;
1234 qname.hash = full_name_hash(name, len);
1236 child = d_lookup(dir, &qname);
1239 new = d_alloc(dir, &qname);
1241 child = instantiate(dir->d_inode, new, task, ptr);
1248 if (!child || IS_ERR(child) || !child->d_inode)
1249 goto end_instantiate;
1250 inode = child->d_inode;
1253 type = inode->i_mode >> 12;
1258 ino = find_inode_number(dir, &qname);
1261 return filldir(dirent, name, len, filp->f_pos, ino, type);
1264 static unsigned name_to_int(struct dentry *dentry)
1266 const char *name = dentry->d_name.name;
1267 int len = dentry->d_name.len;
1270 if (len > 1 && *name == '0')
1273 unsigned c = *name++ - '0';
1276 if (n >= (~0U-9)/10)
1286 #define PROC_FDINFO_MAX 64
1288 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1289 struct vfsmount **mnt, char *info)
1291 struct task_struct *task = get_proc_task(inode);
1292 struct files_struct *files = NULL;
1294 int fd = proc_fd(inode);
1297 files = get_files_struct(task);
1298 put_task_struct(task);
1302 * We are not taking a ref to the file structure, so we must
1305 spin_lock(&files->file_lock);
1306 file = fcheck_files(files, fd);
1309 *mnt = mntget(file->f_path.mnt);
1311 *dentry = dget(file->f_path.dentry);
1313 snprintf(info, PROC_FDINFO_MAX,
1316 (long long) file->f_pos,
1318 spin_unlock(&files->file_lock);
1319 put_files_struct(files);
1322 spin_unlock(&files->file_lock);
1323 put_files_struct(files);
1328 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1329 struct vfsmount **mnt)
1331 return proc_fd_info(inode, dentry, mnt, NULL);
1334 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1336 struct inode *inode = dentry->d_inode;
1337 struct task_struct *task = get_proc_task(inode);
1338 int fd = proc_fd(inode);
1339 struct files_struct *files;
1342 files = get_files_struct(task);
1345 if (fcheck_files(files, fd)) {
1347 put_files_struct(files);
1348 if (task_dumpable(task)) {
1349 inode->i_uid = task->euid;
1350 inode->i_gid = task->egid;
1355 inode->i_mode &= ~(S_ISUID | S_ISGID);
1356 security_task_to_inode(task, inode);
1357 put_task_struct(task);
1361 put_files_struct(files);
1363 put_task_struct(task);
1369 static struct dentry_operations tid_fd_dentry_operations =
1371 .d_revalidate = tid_fd_revalidate,
1372 .d_delete = pid_delete_dentry,
1375 static struct dentry *proc_fd_instantiate(struct inode *dir,
1376 struct dentry *dentry, struct task_struct *task, const void *ptr)
1378 unsigned fd = *(const unsigned *)ptr;
1380 struct files_struct *files;
1381 struct inode *inode;
1382 struct proc_inode *ei;
1383 struct dentry *error = ERR_PTR(-ENOENT);
1385 inode = proc_pid_make_inode(dir->i_sb, task);
1390 files = get_files_struct(task);
1393 inode->i_mode = S_IFLNK;
1396 * We are not taking a ref to the file structure, so we must
1399 spin_lock(&files->file_lock);
1400 file = fcheck_files(files, fd);
1403 if (file->f_mode & 1)
1404 inode->i_mode |= S_IRUSR | S_IXUSR;
1405 if (file->f_mode & 2)
1406 inode->i_mode |= S_IWUSR | S_IXUSR;
1407 spin_unlock(&files->file_lock);
1408 put_files_struct(files);
1410 inode->i_op = &proc_pid_link_inode_operations;
1412 ei->op.proc_get_link = proc_fd_link;
1413 dentry->d_op = &tid_fd_dentry_operations;
1414 d_add(dentry, inode);
1415 /* Close the race of the process dying before we return the dentry */
1416 if (tid_fd_revalidate(dentry, NULL))
1422 spin_unlock(&files->file_lock);
1423 put_files_struct(files);
1429 static struct dentry *proc_lookupfd_common(struct inode *dir,
1430 struct dentry *dentry,
1431 instantiate_t instantiate)
1433 struct task_struct *task = get_proc_task(dir);
1434 unsigned fd = name_to_int(dentry);
1435 struct dentry *result = ERR_PTR(-ENOENT);
1442 result = instantiate(dir, dentry, task, &fd);
1444 put_task_struct(task);
1449 static int proc_readfd_common(struct file * filp, void * dirent,
1450 filldir_t filldir, instantiate_t instantiate)
1452 struct dentry *dentry = filp->f_path.dentry;
1453 struct inode *inode = dentry->d_inode;
1454 struct task_struct *p = get_proc_task(inode);
1455 unsigned int fd, tid, ino;
1457 struct files_struct * files;
1458 struct fdtable *fdt;
1469 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1473 ino = parent_ino(dentry);
1474 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1478 files = get_files_struct(p);
1482 fdt = files_fdtable(files);
1483 for (fd = filp->f_pos-2;
1485 fd++, filp->f_pos++) {
1486 char name[PROC_NUMBUF];
1489 if (!fcheck_files(files, fd))
1493 len = snprintf(name, sizeof(name), "%d", fd);
1494 if (proc_fill_cache(filp, dirent, filldir,
1495 name, len, instantiate,
1503 put_files_struct(files);
1511 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1512 struct nameidata *nd)
1514 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1517 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1519 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1522 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1523 size_t len, loff_t *ppos)
1525 char tmp[PROC_FDINFO_MAX];
1526 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1528 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1532 static const struct file_operations proc_fdinfo_file_operations = {
1533 .open = nonseekable_open,
1534 .read = proc_fdinfo_read,
1537 static const struct file_operations proc_fd_operations = {
1538 .read = generic_read_dir,
1539 .readdir = proc_readfd,
1543 * /proc/pid/fd needs a special permission handler so that a process can still
1544 * access /proc/self/fd after it has executed a setuid().
1546 static int proc_fd_permission(struct inode *inode, int mask,
1547 struct nameidata *nd)
1551 rv = generic_permission(inode, mask, NULL);
1554 if (task_pid(current) == proc_pid(inode))
1560 * proc directories can do almost nothing..
1562 static const struct inode_operations proc_fd_inode_operations = {
1563 .lookup = proc_lookupfd,
1564 .permission = proc_fd_permission,
1565 .setattr = proc_setattr,
1568 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1569 struct dentry *dentry, struct task_struct *task, const void *ptr)
1571 unsigned fd = *(unsigned *)ptr;
1572 struct inode *inode;
1573 struct proc_inode *ei;
1574 struct dentry *error = ERR_PTR(-ENOENT);
1576 inode = proc_pid_make_inode(dir->i_sb, task);
1581 inode->i_mode = S_IFREG | S_IRUSR;
1582 inode->i_fop = &proc_fdinfo_file_operations;
1583 dentry->d_op = &tid_fd_dentry_operations;
1584 d_add(dentry, inode);
1585 /* Close the race of the process dying before we return the dentry */
1586 if (tid_fd_revalidate(dentry, NULL))
1593 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1594 struct dentry *dentry,
1595 struct nameidata *nd)
1597 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1600 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1602 return proc_readfd_common(filp, dirent, filldir,
1603 proc_fdinfo_instantiate);
1606 static const struct file_operations proc_fdinfo_operations = {
1607 .read = generic_read_dir,
1608 .readdir = proc_readfdinfo,
1612 * proc directories can do almost nothing..
1614 static const struct inode_operations proc_fdinfo_inode_operations = {
1615 .lookup = proc_lookupfdinfo,
1616 .setattr = proc_setattr,
1620 static struct dentry *proc_pident_instantiate(struct inode *dir,
1621 struct dentry *dentry, struct task_struct *task, const void *ptr)
1623 const struct pid_entry *p = ptr;
1624 struct inode *inode;
1625 struct proc_inode *ei;
1626 struct dentry *error = ERR_PTR(-EINVAL);
1628 inode = proc_pid_make_inode(dir->i_sb, task);
1633 inode->i_mode = p->mode;
1634 if (S_ISDIR(inode->i_mode))
1635 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1637 inode->i_op = p->iop;
1639 inode->i_fop = p->fop;
1641 dentry->d_op = &pid_dentry_operations;
1642 d_add(dentry, inode);
1643 /* Close the race of the process dying before we return the dentry */
1644 if (pid_revalidate(dentry, NULL))
1650 static struct dentry *proc_pident_lookup(struct inode *dir,
1651 struct dentry *dentry,
1652 const struct pid_entry *ents,
1655 struct inode *inode;
1656 struct dentry *error;
1657 struct task_struct *task = get_proc_task(dir);
1658 const struct pid_entry *p, *last;
1660 error = ERR_PTR(-ENOENT);
1667 * Yes, it does not scale. And it should not. Don't add
1668 * new entries into /proc/<tgid>/ without very good reasons.
1670 last = &ents[nents - 1];
1671 for (p = ents; p <= last; p++) {
1672 if (p->len != dentry->d_name.len)
1674 if (!memcmp(dentry->d_name.name, p->name, p->len))
1680 error = proc_pident_instantiate(dir, dentry, task, p);
1682 put_task_struct(task);
1687 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1688 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1690 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1691 proc_pident_instantiate, task, p);
1694 static int proc_pident_readdir(struct file *filp,
1695 void *dirent, filldir_t filldir,
1696 const struct pid_entry *ents, unsigned int nents)
1700 struct dentry *dentry = filp->f_path.dentry;
1701 struct inode *inode = dentry->d_inode;
1702 struct task_struct *task = get_proc_task(inode);
1703 const struct pid_entry *p, *last;
1717 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1723 ino = parent_ino(dentry);
1724 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1736 last = &ents[nents - 1];
1738 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1747 put_task_struct(task);
1752 #ifdef CONFIG_SECURITY
1753 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1754 size_t count, loff_t *ppos)
1756 struct inode * inode = file->f_path.dentry->d_inode;
1759 struct task_struct *task = get_proc_task(inode);
1764 length = security_getprocattr(task,
1765 (char*)file->f_path.dentry->d_name.name,
1767 put_task_struct(task);
1769 length = simple_read_from_buffer(buf, count, ppos, p, length);
1774 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1775 size_t count, loff_t *ppos)
1777 struct inode * inode = file->f_path.dentry->d_inode;
1780 struct task_struct *task = get_proc_task(inode);
1785 if (count > PAGE_SIZE)
1788 /* No partial writes. */
1794 page = (char*)__get_free_page(GFP_USER);
1799 if (copy_from_user(page, buf, count))
1802 length = security_setprocattr(task,
1803 (char*)file->f_path.dentry->d_name.name,
1804 (void*)page, count);
1806 free_page((unsigned long) page);
1808 put_task_struct(task);
1813 static const struct file_operations proc_pid_attr_operations = {
1814 .read = proc_pid_attr_read,
1815 .write = proc_pid_attr_write,
1818 static const struct pid_entry attr_dir_stuff[] = {
1819 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1820 REG("prev", S_IRUGO, pid_attr),
1821 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1822 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1823 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1824 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1827 static int proc_attr_dir_readdir(struct file * filp,
1828 void * dirent, filldir_t filldir)
1830 return proc_pident_readdir(filp,dirent,filldir,
1831 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1834 static const struct file_operations proc_attr_dir_operations = {
1835 .read = generic_read_dir,
1836 .readdir = proc_attr_dir_readdir,
1839 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1840 struct dentry *dentry, struct nameidata *nd)
1842 return proc_pident_lookup(dir, dentry,
1843 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1846 static const struct inode_operations proc_attr_dir_inode_operations = {
1847 .lookup = proc_attr_dir_lookup,
1848 .getattr = pid_getattr,
1849 .setattr = proc_setattr,
1857 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1860 char tmp[PROC_NUMBUF];
1861 sprintf(tmp, "%d", current->tgid);
1862 return vfs_readlink(dentry,buffer,buflen,tmp);
1865 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1867 char tmp[PROC_NUMBUF];
1868 sprintf(tmp, "%d", current->tgid);
1869 return ERR_PTR(vfs_follow_link(nd,tmp));
1872 static const struct inode_operations proc_self_inode_operations = {
1873 .readlink = proc_self_readlink,
1874 .follow_link = proc_self_follow_link,
1880 * These are the directory entries in the root directory of /proc
1881 * that properly belong to the /proc filesystem, as they describe
1882 * describe something that is process related.
1884 static const struct pid_entry proc_base_stuff[] = {
1885 NOD("self", S_IFLNK|S_IRWXUGO,
1886 &proc_self_inode_operations, NULL, {}),
1890 * Exceptional case: normally we are not allowed to unhash a busy
1891 * directory. In this case, however, we can do it - no aliasing problems
1892 * due to the way we treat inodes.
1894 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1896 struct inode *inode = dentry->d_inode;
1897 struct task_struct *task = get_proc_task(inode);
1899 put_task_struct(task);
1906 static struct dentry_operations proc_base_dentry_operations =
1908 .d_revalidate = proc_base_revalidate,
1909 .d_delete = pid_delete_dentry,
1912 static struct dentry *proc_base_instantiate(struct inode *dir,
1913 struct dentry *dentry, struct task_struct *task, const void *ptr)
1915 const struct pid_entry *p = ptr;
1916 struct inode *inode;
1917 struct proc_inode *ei;
1918 struct dentry *error = ERR_PTR(-EINVAL);
1920 /* Allocate the inode */
1921 error = ERR_PTR(-ENOMEM);
1922 inode = new_inode(dir->i_sb);
1926 /* Initialize the inode */
1928 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1931 * grab the reference to the task.
1933 ei->pid = get_task_pid(task, PIDTYPE_PID);
1939 inode->i_mode = p->mode;
1940 if (S_ISDIR(inode->i_mode))
1942 if (S_ISLNK(inode->i_mode))
1945 inode->i_op = p->iop;
1947 inode->i_fop = p->fop;
1949 dentry->d_op = &proc_base_dentry_operations;
1950 d_add(dentry, inode);
1959 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1961 struct dentry *error;
1962 struct task_struct *task = get_proc_task(dir);
1963 const struct pid_entry *p, *last;
1965 error = ERR_PTR(-ENOENT);
1970 /* Lookup the directory entry */
1971 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1972 for (p = proc_base_stuff; p <= last; p++) {
1973 if (p->len != dentry->d_name.len)
1975 if (!memcmp(dentry->d_name.name, p->name, p->len))
1981 error = proc_base_instantiate(dir, dentry, task, p);
1984 put_task_struct(task);
1989 static int proc_base_fill_cache(struct file *filp, void *dirent,
1990 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1992 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1993 proc_base_instantiate, task, p);
1996 #ifdef CONFIG_TASK_IO_ACCOUNTING
1997 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1999 return sprintf(buffer,
2000 #ifdef CONFIG_TASK_XACCT
2006 "read_bytes: %llu\n"
2007 "write_bytes: %llu\n"
2008 "cancelled_write_bytes: %llu\n",
2009 #ifdef CONFIG_TASK_XACCT
2010 (unsigned long long)task->rchar,
2011 (unsigned long long)task->wchar,
2012 (unsigned long long)task->syscr,
2013 (unsigned long long)task->syscw,
2015 (unsigned long long)task->ioac.read_bytes,
2016 (unsigned long long)task->ioac.write_bytes,
2017 (unsigned long long)task->ioac.cancelled_write_bytes);
2024 static const struct file_operations proc_task_operations;
2025 static const struct inode_operations proc_task_inode_operations;
2027 static const struct pid_entry tgid_base_stuff[] = {
2028 DIR("task", S_IRUGO|S_IXUGO, task),
2029 DIR("fd", S_IRUSR|S_IXUSR, fd),
2030 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2031 INF("environ", S_IRUSR, pid_environ),
2032 INF("auxv", S_IRUSR, pid_auxv),
2033 INF("status", S_IRUGO, pid_status),
2034 #ifdef CONFIG_SCHED_DEBUG
2035 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2037 INF("cmdline", S_IRUGO, pid_cmdline),
2038 INF("stat", S_IRUGO, tgid_stat),
2039 INF("statm", S_IRUGO, pid_statm),
2040 REG("maps", S_IRUGO, maps),
2042 REG("numa_maps", S_IRUGO, numa_maps),
2044 REG("mem", S_IRUSR|S_IWUSR, mem),
2045 #ifdef CONFIG_SECCOMP
2046 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2051 REG("mounts", S_IRUGO, mounts),
2052 REG("mountstats", S_IRUSR, mountstats),
2054 REG("clear_refs", S_IWUSR, clear_refs),
2055 REG("smaps", S_IRUGO, smaps),
2057 #ifdef CONFIG_SECURITY
2058 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2060 #ifdef CONFIG_KALLSYMS
2061 INF("wchan", S_IRUGO, pid_wchan),
2063 #ifdef CONFIG_SCHEDSTATS
2064 INF("schedstat", S_IRUGO, pid_schedstat),
2066 #ifdef CONFIG_CPUSETS
2067 REG("cpuset", S_IRUGO, cpuset),
2069 INF("oom_score", S_IRUGO, oom_score),
2070 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2071 #ifdef CONFIG_AUDITSYSCALL
2072 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2074 #ifdef CONFIG_FAULT_INJECTION
2075 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2077 #ifdef CONFIG_TASK_IO_ACCOUNTING
2078 INF("io", S_IRUGO, pid_io_accounting),
2082 static int proc_tgid_base_readdir(struct file * filp,
2083 void * dirent, filldir_t filldir)
2085 return proc_pident_readdir(filp,dirent,filldir,
2086 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2089 static const struct file_operations proc_tgid_base_operations = {
2090 .read = generic_read_dir,
2091 .readdir = proc_tgid_base_readdir,
2094 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2095 return proc_pident_lookup(dir, dentry,
2096 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2099 static const struct inode_operations proc_tgid_base_inode_operations = {
2100 .lookup = proc_tgid_base_lookup,
2101 .getattr = pid_getattr,
2102 .setattr = proc_setattr,
2106 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2108 * @task: task that should be flushed.
2110 * Looks in the dcache for
2112 * /proc/@tgid/task/@pid
2113 * if either directory is present flushes it and all of it'ts children
2116 * It is safe and reasonable to cache /proc entries for a task until
2117 * that task exits. After that they just clog up the dcache with
2118 * useless entries, possibly causing useful dcache entries to be
2119 * flushed instead. This routine is proved to flush those useless
2120 * dcache entries at process exit time.
2122 * NOTE: This routine is just an optimization so it does not guarantee
2123 * that no dcache entries will exist at process exit time it
2124 * just makes it very unlikely that any will persist.
2126 void proc_flush_task(struct task_struct *task)
2128 struct dentry *dentry, *leader, *dir;
2129 char buf[PROC_NUMBUF];
2133 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2134 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2136 shrink_dcache_parent(dentry);
2141 if (thread_group_leader(task))
2145 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2146 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2151 name.len = strlen(name.name);
2152 dir = d_hash_and_lookup(leader, &name);
2154 goto out_put_leader;
2157 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2158 dentry = d_hash_and_lookup(dir, &name);
2160 shrink_dcache_parent(dentry);
2172 static struct dentry *proc_pid_instantiate(struct inode *dir,
2173 struct dentry * dentry,
2174 struct task_struct *task, const void *ptr)
2176 struct dentry *error = ERR_PTR(-ENOENT);
2177 struct inode *inode;
2179 inode = proc_pid_make_inode(dir->i_sb, task);
2183 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2184 inode->i_op = &proc_tgid_base_inode_operations;
2185 inode->i_fop = &proc_tgid_base_operations;
2186 inode->i_flags|=S_IMMUTABLE;
2188 #ifdef CONFIG_SECURITY
2189 inode->i_nlink += 1;
2192 dentry->d_op = &pid_dentry_operations;
2194 d_add(dentry, inode);
2195 /* Close the race of the process dying before we return the dentry */
2196 if (pid_revalidate(dentry, NULL))
2202 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2204 struct dentry *result = ERR_PTR(-ENOENT);
2205 struct task_struct *task;
2208 result = proc_base_lookup(dir, dentry);
2209 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2212 tgid = name_to_int(dentry);
2217 task = find_task_by_pid(tgid);
2219 get_task_struct(task);
2224 result = proc_pid_instantiate(dir, dentry, task, NULL);
2225 put_task_struct(task);
2231 * Find the first task with tgid >= tgid
2234 static struct task_struct *next_tgid(unsigned int tgid)
2236 struct task_struct *task;
2242 pid = find_ge_pid(tgid);
2245 task = pid_task(pid, PIDTYPE_PID);
2246 /* What we to know is if the pid we have find is the
2247 * pid of a thread_group_leader. Testing for task
2248 * being a thread_group_leader is the obvious thing
2249 * todo but there is a window when it fails, due to
2250 * the pid transfer logic in de_thread.
2252 * So we perform the straight forward test of seeing
2253 * if the pid we have found is the pid of a thread
2254 * group leader, and don't worry if the task we have
2255 * found doesn't happen to be a thread group leader.
2256 * As we don't care in the case of readdir.
2258 if (!task || !has_group_leader_pid(task))
2260 get_task_struct(task);
2266 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2268 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2269 struct task_struct *task, int tgid)
2271 char name[PROC_NUMBUF];
2272 int len = snprintf(name, sizeof(name), "%d", tgid);
2273 return proc_fill_cache(filp, dirent, filldir, name, len,
2274 proc_pid_instantiate, task, NULL);
2277 /* for the /proc/ directory itself, after non-process stuff has been done */
2278 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2280 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2281 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2282 struct task_struct *task;
2288 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2289 const struct pid_entry *p = &proc_base_stuff[nr];
2290 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2294 tgid = filp->f_pos - TGID_OFFSET;
2295 for (task = next_tgid(tgid);
2297 put_task_struct(task), task = next_tgid(tgid + 1)) {
2299 filp->f_pos = tgid + TGID_OFFSET;
2300 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2301 put_task_struct(task);
2305 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2307 put_task_struct(reaper);
2315 static const struct pid_entry tid_base_stuff[] = {
2316 DIR("fd", S_IRUSR|S_IXUSR, fd),
2317 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2318 INF("environ", S_IRUSR, pid_environ),
2319 INF("auxv", S_IRUSR, pid_auxv),
2320 INF("status", S_IRUGO, pid_status),
2321 #ifdef CONFIG_SCHED_DEBUG
2322 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2324 INF("cmdline", S_IRUGO, pid_cmdline),
2325 INF("stat", S_IRUGO, tid_stat),
2326 INF("statm", S_IRUGO, pid_statm),
2327 REG("maps", S_IRUGO, maps),
2329 REG("numa_maps", S_IRUGO, numa_maps),
2331 REG("mem", S_IRUSR|S_IWUSR, mem),
2332 #ifdef CONFIG_SECCOMP
2333 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2338 REG("mounts", S_IRUGO, mounts),
2340 REG("clear_refs", S_IWUSR, clear_refs),
2341 REG("smaps", S_IRUGO, smaps),
2343 #ifdef CONFIG_SECURITY
2344 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2346 #ifdef CONFIG_KALLSYMS
2347 INF("wchan", S_IRUGO, pid_wchan),
2349 #ifdef CONFIG_SCHEDSTATS
2350 INF("schedstat", S_IRUGO, pid_schedstat),
2352 #ifdef CONFIG_CPUSETS
2353 REG("cpuset", S_IRUGO, cpuset),
2355 INF("oom_score", S_IRUGO, oom_score),
2356 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2357 #ifdef CONFIG_AUDITSYSCALL
2358 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2360 #ifdef CONFIG_FAULT_INJECTION
2361 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2365 static int proc_tid_base_readdir(struct file * filp,
2366 void * dirent, filldir_t filldir)
2368 return proc_pident_readdir(filp,dirent,filldir,
2369 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2372 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2373 return proc_pident_lookup(dir, dentry,
2374 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2377 static const struct file_operations proc_tid_base_operations = {
2378 .read = generic_read_dir,
2379 .readdir = proc_tid_base_readdir,
2382 static const struct inode_operations proc_tid_base_inode_operations = {
2383 .lookup = proc_tid_base_lookup,
2384 .getattr = pid_getattr,
2385 .setattr = proc_setattr,
2388 static struct dentry *proc_task_instantiate(struct inode *dir,
2389 struct dentry *dentry, struct task_struct *task, const void *ptr)
2391 struct dentry *error = ERR_PTR(-ENOENT);
2392 struct inode *inode;
2393 inode = proc_pid_make_inode(dir->i_sb, task);
2397 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2398 inode->i_op = &proc_tid_base_inode_operations;
2399 inode->i_fop = &proc_tid_base_operations;
2400 inode->i_flags|=S_IMMUTABLE;
2402 #ifdef CONFIG_SECURITY
2403 inode->i_nlink += 1;
2406 dentry->d_op = &pid_dentry_operations;
2408 d_add(dentry, inode);
2409 /* Close the race of the process dying before we return the dentry */
2410 if (pid_revalidate(dentry, NULL))
2416 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2418 struct dentry *result = ERR_PTR(-ENOENT);
2419 struct task_struct *task;
2420 struct task_struct *leader = get_proc_task(dir);
2426 tid = name_to_int(dentry);
2431 task = find_task_by_pid(tid);
2433 get_task_struct(task);
2437 if (leader->tgid != task->tgid)
2440 result = proc_task_instantiate(dir, dentry, task, NULL);
2442 put_task_struct(task);
2444 put_task_struct(leader);
2450 * Find the first tid of a thread group to return to user space.
2452 * Usually this is just the thread group leader, but if the users
2453 * buffer was too small or there was a seek into the middle of the
2454 * directory we have more work todo.
2456 * In the case of a short read we start with find_task_by_pid.
2458 * In the case of a seek we start with the leader and walk nr
2461 static struct task_struct *first_tid(struct task_struct *leader,
2464 struct task_struct *pos;
2467 /* Attempt to start with the pid of a thread */
2468 if (tid && (nr > 0)) {
2469 pos = find_task_by_pid(tid);
2470 if (pos && (pos->group_leader == leader))
2474 /* If nr exceeds the number of threads there is nothing todo */
2476 if (nr && nr >= get_nr_threads(leader))
2479 /* If we haven't found our starting place yet start
2480 * with the leader and walk nr threads forward.
2482 for (pos = leader; nr > 0; --nr) {
2483 pos = next_thread(pos);
2484 if (pos == leader) {
2490 get_task_struct(pos);
2497 * Find the next thread in the thread list.
2498 * Return NULL if there is an error or no next thread.
2500 * The reference to the input task_struct is released.
2502 static struct task_struct *next_tid(struct task_struct *start)
2504 struct task_struct *pos = NULL;
2506 if (pid_alive(start)) {
2507 pos = next_thread(start);
2508 if (thread_group_leader(pos))
2511 get_task_struct(pos);
2514 put_task_struct(start);
2518 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2519 struct task_struct *task, int tid)
2521 char name[PROC_NUMBUF];
2522 int len = snprintf(name, sizeof(name), "%d", tid);
2523 return proc_fill_cache(filp, dirent, filldir, name, len,
2524 proc_task_instantiate, task, NULL);
2527 /* for the /proc/TGID/task/ directories */
2528 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2530 struct dentry *dentry = filp->f_path.dentry;
2531 struct inode *inode = dentry->d_inode;
2532 struct task_struct *leader = NULL;
2533 struct task_struct *task;
2534 int retval = -ENOENT;
2537 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2539 task = get_proc_task(inode);
2543 if (pid_alive(task)) {
2544 leader = task->group_leader;
2545 get_task_struct(leader);
2548 put_task_struct(task);
2556 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2561 ino = parent_ino(dentry);
2562 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2568 /* f_version caches the tgid value that the last readdir call couldn't
2569 * return. lseek aka telldir automagically resets f_version to 0.
2571 tid = filp->f_version;
2572 filp->f_version = 0;
2573 for (task = first_tid(leader, tid, pos - 2);
2575 task = next_tid(task), pos++) {
2577 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2578 /* returning this tgid failed, save it as the first
2579 * pid for the next readir call */
2580 filp->f_version = tid;
2581 put_task_struct(task);
2587 put_task_struct(leader);
2592 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2594 struct inode *inode = dentry->d_inode;
2595 struct task_struct *p = get_proc_task(inode);
2596 generic_fillattr(inode, stat);
2600 stat->nlink += get_nr_threads(p);
2608 static const struct inode_operations proc_task_inode_operations = {
2609 .lookup = proc_task_lookup,
2610 .getattr = proc_task_getattr,
2611 .setattr = proc_setattr,
2614 static const struct file_operations proc_task_operations = {
2615 .read = generic_read_dir,
2616 .readdir = proc_task_readdir,