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/cgroup.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>
76 #include <linux/elf.h>
77 #include <linux/pid_namespace.h>
81 * Implementing inode permission operations in /proc is almost
82 * certainly an error. Permission checks need to happen during
83 * each system call not at open time. The reason is that most of
84 * what we wish to check for permissions in /proc varies at runtime.
86 * The classic example of a problem is opening file descriptors
87 * in /proc for a task before it execs a suid executable.
91 /* Worst case buffer size needed for holding an integer. */
92 #define PROC_NUMBUF 13
98 const struct inode_operations *iop;
99 const struct file_operations *fop;
103 #define NOD(NAME, MODE, IOP, FOP, OP) { \
105 .len = sizeof(NAME) - 1, \
112 #define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
116 #define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
120 #define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
122 &proc_##OTYPE##_operations, {})
123 #define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
129 EXPORT_SYMBOL(maps_protect);
131 static struct fs_struct *get_fs_struct(struct task_struct *task)
133 struct fs_struct *fs;
137 atomic_inc(&fs->count);
142 static int get_nr_threads(struct task_struct *tsk)
144 /* Must be called with the rcu_read_lock held */
148 if (lock_task_sighand(tsk, &flags)) {
149 count = atomic_read(&tsk->signal->count);
150 unlock_task_sighand(tsk, &flags);
155 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
157 struct task_struct *task = get_proc_task(inode);
158 struct fs_struct *fs = NULL;
159 int result = -ENOENT;
162 fs = get_fs_struct(task);
163 put_task_struct(task);
166 read_lock(&fs->lock);
167 *mnt = mntget(fs->pwdmnt);
168 *dentry = dget(fs->pwd);
169 read_unlock(&fs->lock);
176 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
178 struct task_struct *task = get_proc_task(inode);
179 struct fs_struct *fs = NULL;
180 int result = -ENOENT;
183 fs = get_fs_struct(task);
184 put_task_struct(task);
187 read_lock(&fs->lock);
188 *mnt = mntget(fs->rootmnt);
189 *dentry = dget(fs->root);
190 read_unlock(&fs->lock);
197 #define MAY_PTRACE(task) \
198 (task == current || \
199 (task->parent == current && \
200 (task->ptrace & PT_PTRACED) && \
201 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
202 security_ptrace(current,task) == 0))
204 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
208 struct mm_struct *mm = get_task_mm(task);
212 goto out_mm; /* Shh! No looking before we're done */
214 len = mm->arg_end - mm->arg_start;
219 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
221 // If the nul at the end of args has been overwritten, then
222 // assume application is using setproctitle(3).
223 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
224 len = strnlen(buffer, res);
228 len = mm->env_end - mm->env_start;
229 if (len > PAGE_SIZE - res)
230 len = PAGE_SIZE - res;
231 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
232 res = strnlen(buffer, res);
241 static int proc_pid_auxv(struct task_struct *task, char *buffer)
244 struct mm_struct *mm = get_task_mm(task);
246 unsigned int nwords = 0;
249 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
250 res = nwords * sizeof(mm->saved_auxv[0]);
253 memcpy(buffer, mm->saved_auxv, res);
260 #ifdef CONFIG_KALLSYMS
262 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
263 * Returns the resolved symbol. If that fails, simply return the address.
265 static int proc_pid_wchan(struct task_struct *task, char *buffer)
268 char symname[KSYM_NAME_LEN];
270 wchan = get_wchan(task);
272 if (lookup_symbol_name(wchan, symname) < 0)
273 return sprintf(buffer, "%lu", wchan);
275 return sprintf(buffer, "%s", symname);
277 #endif /* CONFIG_KALLSYMS */
279 #ifdef CONFIG_SCHEDSTATS
281 * Provides /proc/PID/schedstat
283 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
285 return sprintf(buffer, "%llu %llu %lu\n",
286 task->sched_info.cpu_time,
287 task->sched_info.run_delay,
288 task->sched_info.pcount);
292 /* The badness from the OOM killer */
293 unsigned long badness(struct task_struct *p, unsigned long uptime);
294 static int proc_oom_score(struct task_struct *task, char *buffer)
296 unsigned long points;
297 struct timespec uptime;
299 do_posix_clock_monotonic_gettime(&uptime);
300 read_lock(&tasklist_lock);
301 points = badness(task, uptime.tv_sec);
302 read_unlock(&tasklist_lock);
303 return sprintf(buffer, "%lu\n", points);
306 /************************************************************************/
307 /* Here the fs part begins */
308 /************************************************************************/
310 /* permission checks */
311 static int proc_fd_access_allowed(struct inode *inode)
313 struct task_struct *task;
315 /* Allow access to a task's file descriptors if it is us or we
316 * may use ptrace attach to the process and find out that
319 task = get_proc_task(inode);
321 allowed = ptrace_may_attach(task);
322 put_task_struct(task);
327 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
330 struct inode *inode = dentry->d_inode;
332 if (attr->ia_valid & ATTR_MODE)
335 error = inode_change_ok(inode, attr);
337 error = inode_setattr(inode, attr);
341 static const struct inode_operations proc_def_inode_operations = {
342 .setattr = proc_setattr,
345 extern struct seq_operations mounts_op;
351 static int mounts_open(struct inode *inode, struct file *file)
353 struct task_struct *task = get_proc_task(inode);
355 struct mnt_namespace *ns = NULL;
356 struct proc_mounts *p;
361 nsp = task_nsproxy(task);
369 put_task_struct(task);
374 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
376 file->private_data = &p->m;
377 ret = seq_open(file, &mounts_op);
380 p->event = ns->event;
390 static int mounts_release(struct inode *inode, struct file *file)
392 struct seq_file *m = file->private_data;
393 struct mnt_namespace *ns = m->private;
395 return seq_release(inode, file);
398 static unsigned mounts_poll(struct file *file, poll_table *wait)
400 struct proc_mounts *p = file->private_data;
401 struct mnt_namespace *ns = p->m.private;
404 poll_wait(file, &ns->poll, wait);
406 spin_lock(&vfsmount_lock);
407 if (p->event != ns->event) {
408 p->event = ns->event;
411 spin_unlock(&vfsmount_lock);
416 static const struct file_operations proc_mounts_operations = {
420 .release = mounts_release,
424 extern struct seq_operations mountstats_op;
425 static int mountstats_open(struct inode *inode, struct file *file)
427 int ret = seq_open(file, &mountstats_op);
430 struct seq_file *m = file->private_data;
432 struct mnt_namespace *mnt_ns = NULL;
433 struct task_struct *task = get_proc_task(inode);
437 nsp = task_nsproxy(task);
439 mnt_ns = nsp->mnt_ns;
445 put_task_struct(task);
451 seq_release(inode, file);
458 static const struct file_operations proc_mountstats_operations = {
459 .open = mountstats_open,
462 .release = mounts_release,
465 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
467 static ssize_t proc_info_read(struct file * file, char __user * buf,
468 size_t count, loff_t *ppos)
470 struct inode * inode = file->f_path.dentry->d_inode;
473 struct task_struct *task = get_proc_task(inode);
479 if (count > PROC_BLOCK_SIZE)
480 count = PROC_BLOCK_SIZE;
483 if (!(page = __get_free_page(GFP_TEMPORARY)))
486 length = PROC_I(inode)->op.proc_read(task, (char*)page);
489 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
492 put_task_struct(task);
497 static const struct file_operations proc_info_file_operations = {
498 .read = proc_info_read,
501 static int mem_open(struct inode* inode, struct file* file)
503 file->private_data = (void*)((long)current->self_exec_id);
507 static ssize_t mem_read(struct file * file, char __user * buf,
508 size_t count, loff_t *ppos)
510 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
512 unsigned long src = *ppos;
514 struct mm_struct *mm;
519 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
523 page = (char *)__get_free_page(GFP_TEMPORARY);
529 mm = get_task_mm(task);
535 if (file->private_data != (void*)((long)current->self_exec_id))
541 int this_len, retval;
543 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
544 retval = access_process_vm(task, src, page, this_len, 0);
545 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
551 if (copy_to_user(buf, page, retval)) {
566 free_page((unsigned long) page);
568 put_task_struct(task);
573 #define mem_write NULL
576 /* This is a security hazard */
577 static ssize_t mem_write(struct file * file, const char __user *buf,
578 size_t count, loff_t *ppos)
582 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
583 unsigned long dst = *ppos;
589 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
593 page = (char *)__get_free_page(GFP_TEMPORARY);
599 int this_len, retval;
601 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
602 if (copy_from_user(page, buf, this_len)) {
606 retval = access_process_vm(task, dst, page, this_len, 1);
618 free_page((unsigned long) page);
620 put_task_struct(task);
626 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
630 file->f_pos = offset;
633 file->f_pos += offset;
638 force_successful_syscall_return();
642 static const struct file_operations proc_mem_operations = {
649 static ssize_t environ_read(struct file *file, char __user *buf,
650 size_t count, loff_t *ppos)
652 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
654 unsigned long src = *ppos;
656 struct mm_struct *mm;
661 if (!ptrace_may_attach(task))
665 page = (char *)__get_free_page(GFP_TEMPORARY);
671 mm = get_task_mm(task);
676 int this_len, retval, max_len;
678 this_len = mm->env_end - (mm->env_start + src);
683 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
684 this_len = (this_len > max_len) ? max_len : this_len;
686 retval = access_process_vm(task, (mm->env_start + src),
694 if (copy_to_user(buf, page, retval)) {
708 free_page((unsigned long) page);
710 put_task_struct(task);
715 static const struct file_operations proc_environ_operations = {
716 .read = environ_read,
719 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
720 size_t count, loff_t *ppos)
722 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
723 char buffer[PROC_NUMBUF];
729 oom_adjust = task->oomkilladj;
730 put_task_struct(task);
732 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
734 return simple_read_from_buffer(buf, count, ppos, buffer, len);
737 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
738 size_t count, loff_t *ppos)
740 struct task_struct *task;
741 char buffer[PROC_NUMBUF], *end;
744 memset(buffer, 0, sizeof(buffer));
745 if (count > sizeof(buffer) - 1)
746 count = sizeof(buffer) - 1;
747 if (copy_from_user(buffer, buf, count))
749 oom_adjust = simple_strtol(buffer, &end, 0);
750 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
751 oom_adjust != OOM_DISABLE)
755 task = get_proc_task(file->f_path.dentry->d_inode);
758 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
759 put_task_struct(task);
762 task->oomkilladj = oom_adjust;
763 put_task_struct(task);
764 if (end - buffer == 0)
769 static const struct file_operations proc_oom_adjust_operations = {
770 .read = oom_adjust_read,
771 .write = oom_adjust_write,
775 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
776 size_t count, loff_t *ppos)
778 struct task_struct *task;
779 char buffer[PROC_NUMBUF], *end;
780 struct mm_struct *mm;
782 memset(buffer, 0, sizeof(buffer));
783 if (count > sizeof(buffer) - 1)
784 count = sizeof(buffer) - 1;
785 if (copy_from_user(buffer, buf, count))
787 if (!simple_strtol(buffer, &end, 0))
791 task = get_proc_task(file->f_path.dentry->d_inode);
794 mm = get_task_mm(task);
799 put_task_struct(task);
800 if (end - buffer == 0)
805 static struct file_operations proc_clear_refs_operations = {
806 .write = clear_refs_write,
810 #ifdef CONFIG_AUDITSYSCALL
812 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
813 size_t count, loff_t *ppos)
815 struct inode * inode = file->f_path.dentry->d_inode;
816 struct task_struct *task = get_proc_task(inode);
818 char tmpbuf[TMPBUFLEN];
822 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
823 audit_get_loginuid(task->audit_context));
824 put_task_struct(task);
825 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
828 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
829 size_t count, loff_t *ppos)
831 struct inode * inode = file->f_path.dentry->d_inode;
836 if (!capable(CAP_AUDIT_CONTROL))
839 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
842 if (count >= PAGE_SIZE)
843 count = PAGE_SIZE - 1;
846 /* No partial writes. */
849 page = (char*)__get_free_page(GFP_TEMPORARY);
853 if (copy_from_user(page, buf, count))
857 loginuid = simple_strtoul(page, &tmp, 10);
863 length = audit_set_loginuid(current, loginuid);
864 if (likely(length == 0))
868 free_page((unsigned long) page);
872 static const struct file_operations proc_loginuid_operations = {
873 .read = proc_loginuid_read,
874 .write = proc_loginuid_write,
878 #ifdef CONFIG_FAULT_INJECTION
879 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
880 size_t count, loff_t *ppos)
882 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
883 char buffer[PROC_NUMBUF];
889 make_it_fail = task->make_it_fail;
890 put_task_struct(task);
892 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
894 return simple_read_from_buffer(buf, count, ppos, buffer, len);
897 static ssize_t proc_fault_inject_write(struct file * file,
898 const char __user * buf, size_t count, loff_t *ppos)
900 struct task_struct *task;
901 char buffer[PROC_NUMBUF], *end;
904 if (!capable(CAP_SYS_RESOURCE))
906 memset(buffer, 0, sizeof(buffer));
907 if (count > sizeof(buffer) - 1)
908 count = sizeof(buffer) - 1;
909 if (copy_from_user(buffer, buf, count))
911 make_it_fail = simple_strtol(buffer, &end, 0);
914 task = get_proc_task(file->f_dentry->d_inode);
917 task->make_it_fail = make_it_fail;
918 put_task_struct(task);
919 if (end - buffer == 0)
924 static const struct file_operations proc_fault_inject_operations = {
925 .read = proc_fault_inject_read,
926 .write = proc_fault_inject_write,
930 #ifdef CONFIG_SCHED_DEBUG
932 * Print out various scheduling related per-task fields:
934 static int sched_show(struct seq_file *m, void *v)
936 struct inode *inode = m->private;
937 struct task_struct *p;
941 p = get_proc_task(inode);
944 proc_sched_show_task(p, m);
952 sched_write(struct file *file, const char __user *buf,
953 size_t count, loff_t *offset)
955 struct inode *inode = file->f_path.dentry->d_inode;
956 struct task_struct *p;
960 p = get_proc_task(inode);
963 proc_sched_set_task(p);
970 static int sched_open(struct inode *inode, struct file *filp)
974 ret = single_open(filp, sched_show, NULL);
976 struct seq_file *m = filp->private_data;
983 static const struct file_operations proc_pid_sched_operations = {
986 .write = sched_write,
988 .release = single_release,
993 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
995 struct inode *inode = dentry->d_inode;
998 /* We don't need a base pointer in the /proc filesystem */
1001 /* Are we allowed to snoop on the tasks file descriptors? */
1002 if (!proc_fd_access_allowed(inode))
1005 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1006 nd->last_type = LAST_BIND;
1008 return ERR_PTR(error);
1011 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1012 char __user *buffer, int buflen)
1014 struct inode * inode;
1015 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1022 inode = dentry->d_inode;
1023 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1024 len = PTR_ERR(path);
1027 len = tmp + PAGE_SIZE - 1 - path;
1031 if (copy_to_user(buffer, path, len))
1034 free_page((unsigned long)tmp);
1038 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1040 int error = -EACCES;
1041 struct inode *inode = dentry->d_inode;
1043 struct vfsmount *mnt = NULL;
1045 /* Are we allowed to snoop on the tasks file descriptors? */
1046 if (!proc_fd_access_allowed(inode))
1049 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1053 error = do_proc_readlink(de, mnt, buffer, buflen);
1060 static const struct inode_operations proc_pid_link_inode_operations = {
1061 .readlink = proc_pid_readlink,
1062 .follow_link = proc_pid_follow_link,
1063 .setattr = proc_setattr,
1067 /* building an inode */
1069 static int task_dumpable(struct task_struct *task)
1072 struct mm_struct *mm;
1077 dumpable = get_dumpable(mm);
1085 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1087 struct inode * inode;
1088 struct proc_inode *ei;
1090 /* We need a new inode */
1092 inode = new_inode(sb);
1098 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1099 inode->i_op = &proc_def_inode_operations;
1102 * grab the reference to task.
1104 ei->pid = get_task_pid(task, PIDTYPE_PID);
1110 if (task_dumpable(task)) {
1111 inode->i_uid = task->euid;
1112 inode->i_gid = task->egid;
1114 security_task_to_inode(task, inode);
1124 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1126 struct inode *inode = dentry->d_inode;
1127 struct task_struct *task;
1128 generic_fillattr(inode, stat);
1133 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1135 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1136 task_dumpable(task)) {
1137 stat->uid = task->euid;
1138 stat->gid = task->egid;
1148 * Exceptional case: normally we are not allowed to unhash a busy
1149 * directory. In this case, however, we can do it - no aliasing problems
1150 * due to the way we treat inodes.
1152 * Rewrite the inode's ownerships here because the owning task may have
1153 * performed a setuid(), etc.
1155 * Before the /proc/pid/status file was created the only way to read
1156 * the effective uid of a /process was to stat /proc/pid. Reading
1157 * /proc/pid/status is slow enough that procps and other packages
1158 * kept stating /proc/pid. To keep the rules in /proc simple I have
1159 * made this apply to all per process world readable and executable
1162 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1164 struct inode *inode = dentry->d_inode;
1165 struct task_struct *task = get_proc_task(inode);
1167 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1168 task_dumpable(task)) {
1169 inode->i_uid = task->euid;
1170 inode->i_gid = task->egid;
1175 inode->i_mode &= ~(S_ISUID | S_ISGID);
1176 security_task_to_inode(task, inode);
1177 put_task_struct(task);
1184 static int pid_delete_dentry(struct dentry * dentry)
1186 /* Is the task we represent dead?
1187 * If so, then don't put the dentry on the lru list,
1188 * kill it immediately.
1190 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1193 static struct dentry_operations pid_dentry_operations =
1195 .d_revalidate = pid_revalidate,
1196 .d_delete = pid_delete_dentry,
1201 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1202 struct task_struct *, const void *);
1205 * Fill a directory entry.
1207 * If possible create the dcache entry and derive our inode number and
1208 * file type from dcache entry.
1210 * Since all of the proc inode numbers are dynamically generated, the inode
1211 * numbers do not exist until the inode is cache. This means creating the
1212 * the dcache entry in readdir is necessary to keep the inode numbers
1213 * reported by readdir in sync with the inode numbers reported
1216 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1217 char *name, int len,
1218 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1220 struct dentry *child, *dir = filp->f_path.dentry;
1221 struct inode *inode;
1224 unsigned type = DT_UNKNOWN;
1228 qname.hash = full_name_hash(name, len);
1230 child = d_lookup(dir, &qname);
1233 new = d_alloc(dir, &qname);
1235 child = instantiate(dir->d_inode, new, task, ptr);
1242 if (!child || IS_ERR(child) || !child->d_inode)
1243 goto end_instantiate;
1244 inode = child->d_inode;
1247 type = inode->i_mode >> 12;
1252 ino = find_inode_number(dir, &qname);
1255 return filldir(dirent, name, len, filp->f_pos, ino, type);
1258 static unsigned name_to_int(struct dentry *dentry)
1260 const char *name = dentry->d_name.name;
1261 int len = dentry->d_name.len;
1264 if (len > 1 && *name == '0')
1267 unsigned c = *name++ - '0';
1270 if (n >= (~0U-9)/10)
1280 #define PROC_FDINFO_MAX 64
1282 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1283 struct vfsmount **mnt, char *info)
1285 struct task_struct *task = get_proc_task(inode);
1286 struct files_struct *files = NULL;
1288 int fd = proc_fd(inode);
1291 files = get_files_struct(task);
1292 put_task_struct(task);
1296 * We are not taking a ref to the file structure, so we must
1299 spin_lock(&files->file_lock);
1300 file = fcheck_files(files, fd);
1303 *mnt = mntget(file->f_path.mnt);
1305 *dentry = dget(file->f_path.dentry);
1307 snprintf(info, PROC_FDINFO_MAX,
1310 (long long) file->f_pos,
1312 spin_unlock(&files->file_lock);
1313 put_files_struct(files);
1316 spin_unlock(&files->file_lock);
1317 put_files_struct(files);
1322 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1323 struct vfsmount **mnt)
1325 return proc_fd_info(inode, dentry, mnt, NULL);
1328 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1330 struct inode *inode = dentry->d_inode;
1331 struct task_struct *task = get_proc_task(inode);
1332 int fd = proc_fd(inode);
1333 struct files_struct *files;
1336 files = get_files_struct(task);
1339 if (fcheck_files(files, fd)) {
1341 put_files_struct(files);
1342 if (task_dumpable(task)) {
1343 inode->i_uid = task->euid;
1344 inode->i_gid = task->egid;
1349 inode->i_mode &= ~(S_ISUID | S_ISGID);
1350 security_task_to_inode(task, inode);
1351 put_task_struct(task);
1355 put_files_struct(files);
1357 put_task_struct(task);
1363 static struct dentry_operations tid_fd_dentry_operations =
1365 .d_revalidate = tid_fd_revalidate,
1366 .d_delete = pid_delete_dentry,
1369 static struct dentry *proc_fd_instantiate(struct inode *dir,
1370 struct dentry *dentry, struct task_struct *task, const void *ptr)
1372 unsigned fd = *(const unsigned *)ptr;
1374 struct files_struct *files;
1375 struct inode *inode;
1376 struct proc_inode *ei;
1377 struct dentry *error = ERR_PTR(-ENOENT);
1379 inode = proc_pid_make_inode(dir->i_sb, task);
1384 files = get_files_struct(task);
1387 inode->i_mode = S_IFLNK;
1390 * We are not taking a ref to the file structure, so we must
1393 spin_lock(&files->file_lock);
1394 file = fcheck_files(files, fd);
1397 if (file->f_mode & 1)
1398 inode->i_mode |= S_IRUSR | S_IXUSR;
1399 if (file->f_mode & 2)
1400 inode->i_mode |= S_IWUSR | S_IXUSR;
1401 spin_unlock(&files->file_lock);
1402 put_files_struct(files);
1404 inode->i_op = &proc_pid_link_inode_operations;
1406 ei->op.proc_get_link = proc_fd_link;
1407 dentry->d_op = &tid_fd_dentry_operations;
1408 d_add(dentry, inode);
1409 /* Close the race of the process dying before we return the dentry */
1410 if (tid_fd_revalidate(dentry, NULL))
1416 spin_unlock(&files->file_lock);
1417 put_files_struct(files);
1423 static struct dentry *proc_lookupfd_common(struct inode *dir,
1424 struct dentry *dentry,
1425 instantiate_t instantiate)
1427 struct task_struct *task = get_proc_task(dir);
1428 unsigned fd = name_to_int(dentry);
1429 struct dentry *result = ERR_PTR(-ENOENT);
1436 result = instantiate(dir, dentry, task, &fd);
1438 put_task_struct(task);
1443 static int proc_readfd_common(struct file * filp, void * dirent,
1444 filldir_t filldir, instantiate_t instantiate)
1446 struct dentry *dentry = filp->f_path.dentry;
1447 struct inode *inode = dentry->d_inode;
1448 struct task_struct *p = get_proc_task(inode);
1449 unsigned int fd, tid, ino;
1451 struct files_struct * files;
1452 struct fdtable *fdt;
1463 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1467 ino = parent_ino(dentry);
1468 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1472 files = get_files_struct(p);
1476 fdt = files_fdtable(files);
1477 for (fd = filp->f_pos-2;
1479 fd++, filp->f_pos++) {
1480 char name[PROC_NUMBUF];
1483 if (!fcheck_files(files, fd))
1487 len = snprintf(name, sizeof(name), "%d", fd);
1488 if (proc_fill_cache(filp, dirent, filldir,
1489 name, len, instantiate,
1497 put_files_struct(files);
1505 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1506 struct nameidata *nd)
1508 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1511 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1513 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1516 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1517 size_t len, loff_t *ppos)
1519 char tmp[PROC_FDINFO_MAX];
1520 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1522 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1526 static const struct file_operations proc_fdinfo_file_operations = {
1527 .open = nonseekable_open,
1528 .read = proc_fdinfo_read,
1531 static const struct file_operations proc_fd_operations = {
1532 .read = generic_read_dir,
1533 .readdir = proc_readfd,
1537 * /proc/pid/fd needs a special permission handler so that a process can still
1538 * access /proc/self/fd after it has executed a setuid().
1540 static int proc_fd_permission(struct inode *inode, int mask,
1541 struct nameidata *nd)
1545 rv = generic_permission(inode, mask, NULL);
1548 if (task_pid(current) == proc_pid(inode))
1554 * proc directories can do almost nothing..
1556 static const struct inode_operations proc_fd_inode_operations = {
1557 .lookup = proc_lookupfd,
1558 .permission = proc_fd_permission,
1559 .setattr = proc_setattr,
1562 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1563 struct dentry *dentry, struct task_struct *task, const void *ptr)
1565 unsigned fd = *(unsigned *)ptr;
1566 struct inode *inode;
1567 struct proc_inode *ei;
1568 struct dentry *error = ERR_PTR(-ENOENT);
1570 inode = proc_pid_make_inode(dir->i_sb, task);
1575 inode->i_mode = S_IFREG | S_IRUSR;
1576 inode->i_fop = &proc_fdinfo_file_operations;
1577 dentry->d_op = &tid_fd_dentry_operations;
1578 d_add(dentry, inode);
1579 /* Close the race of the process dying before we return the dentry */
1580 if (tid_fd_revalidate(dentry, NULL))
1587 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1588 struct dentry *dentry,
1589 struct nameidata *nd)
1591 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1594 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1596 return proc_readfd_common(filp, dirent, filldir,
1597 proc_fdinfo_instantiate);
1600 static const struct file_operations proc_fdinfo_operations = {
1601 .read = generic_read_dir,
1602 .readdir = proc_readfdinfo,
1606 * proc directories can do almost nothing..
1608 static const struct inode_operations proc_fdinfo_inode_operations = {
1609 .lookup = proc_lookupfdinfo,
1610 .setattr = proc_setattr,
1614 static struct dentry *proc_pident_instantiate(struct inode *dir,
1615 struct dentry *dentry, struct task_struct *task, const void *ptr)
1617 const struct pid_entry *p = ptr;
1618 struct inode *inode;
1619 struct proc_inode *ei;
1620 struct dentry *error = ERR_PTR(-EINVAL);
1622 inode = proc_pid_make_inode(dir->i_sb, task);
1627 inode->i_mode = p->mode;
1628 if (S_ISDIR(inode->i_mode))
1629 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1631 inode->i_op = p->iop;
1633 inode->i_fop = p->fop;
1635 dentry->d_op = &pid_dentry_operations;
1636 d_add(dentry, inode);
1637 /* Close the race of the process dying before we return the dentry */
1638 if (pid_revalidate(dentry, NULL))
1644 static struct dentry *proc_pident_lookup(struct inode *dir,
1645 struct dentry *dentry,
1646 const struct pid_entry *ents,
1649 struct inode *inode;
1650 struct dentry *error;
1651 struct task_struct *task = get_proc_task(dir);
1652 const struct pid_entry *p, *last;
1654 error = ERR_PTR(-ENOENT);
1661 * Yes, it does not scale. And it should not. Don't add
1662 * new entries into /proc/<tgid>/ without very good reasons.
1664 last = &ents[nents - 1];
1665 for (p = ents; p <= last; p++) {
1666 if (p->len != dentry->d_name.len)
1668 if (!memcmp(dentry->d_name.name, p->name, p->len))
1674 error = proc_pident_instantiate(dir, dentry, task, p);
1676 put_task_struct(task);
1681 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1682 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1684 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1685 proc_pident_instantiate, task, p);
1688 static int proc_pident_readdir(struct file *filp,
1689 void *dirent, filldir_t filldir,
1690 const struct pid_entry *ents, unsigned int nents)
1694 struct dentry *dentry = filp->f_path.dentry;
1695 struct inode *inode = dentry->d_inode;
1696 struct task_struct *task = get_proc_task(inode);
1697 const struct pid_entry *p, *last;
1711 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1717 ino = parent_ino(dentry);
1718 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1730 last = &ents[nents - 1];
1732 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1741 put_task_struct(task);
1746 #ifdef CONFIG_SECURITY
1747 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1748 size_t count, loff_t *ppos)
1750 struct inode * inode = file->f_path.dentry->d_inode;
1753 struct task_struct *task = get_proc_task(inode);
1758 length = security_getprocattr(task,
1759 (char*)file->f_path.dentry->d_name.name,
1761 put_task_struct(task);
1763 length = simple_read_from_buffer(buf, count, ppos, p, length);
1768 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1769 size_t count, loff_t *ppos)
1771 struct inode * inode = file->f_path.dentry->d_inode;
1774 struct task_struct *task = get_proc_task(inode);
1779 if (count > PAGE_SIZE)
1782 /* No partial writes. */
1788 page = (char*)__get_free_page(GFP_TEMPORARY);
1793 if (copy_from_user(page, buf, count))
1796 length = security_setprocattr(task,
1797 (char*)file->f_path.dentry->d_name.name,
1798 (void*)page, count);
1800 free_page((unsigned long) page);
1802 put_task_struct(task);
1807 static const struct file_operations proc_pid_attr_operations = {
1808 .read = proc_pid_attr_read,
1809 .write = proc_pid_attr_write,
1812 static const struct pid_entry attr_dir_stuff[] = {
1813 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1814 REG("prev", S_IRUGO, pid_attr),
1815 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1816 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1817 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1818 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1821 static int proc_attr_dir_readdir(struct file * filp,
1822 void * dirent, filldir_t filldir)
1824 return proc_pident_readdir(filp,dirent,filldir,
1825 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1828 static const struct file_operations proc_attr_dir_operations = {
1829 .read = generic_read_dir,
1830 .readdir = proc_attr_dir_readdir,
1833 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1834 struct dentry *dentry, struct nameidata *nd)
1836 return proc_pident_lookup(dir, dentry,
1837 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1840 static const struct inode_operations proc_attr_dir_inode_operations = {
1841 .lookup = proc_attr_dir_lookup,
1842 .getattr = pid_getattr,
1843 .setattr = proc_setattr,
1848 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1849 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1850 size_t count, loff_t *ppos)
1852 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1853 struct mm_struct *mm;
1854 char buffer[PROC_NUMBUF];
1862 mm = get_task_mm(task);
1864 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1865 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1866 MMF_DUMP_FILTER_SHIFT));
1868 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1871 put_task_struct(task);
1876 static ssize_t proc_coredump_filter_write(struct file *file,
1877 const char __user *buf,
1881 struct task_struct *task;
1882 struct mm_struct *mm;
1883 char buffer[PROC_NUMBUF], *end;
1890 memset(buffer, 0, sizeof(buffer));
1891 if (count > sizeof(buffer) - 1)
1892 count = sizeof(buffer) - 1;
1893 if (copy_from_user(buffer, buf, count))
1897 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1900 if (end - buffer == 0)
1904 task = get_proc_task(file->f_dentry->d_inode);
1909 mm = get_task_mm(task);
1913 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1915 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1917 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1922 put_task_struct(task);
1927 static const struct file_operations proc_coredump_filter_operations = {
1928 .read = proc_coredump_filter_read,
1929 .write = proc_coredump_filter_write,
1936 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1939 char tmp[PROC_NUMBUF];
1940 sprintf(tmp, "%d", task_tgid_vnr(current));
1941 return vfs_readlink(dentry,buffer,buflen,tmp);
1944 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1946 char tmp[PROC_NUMBUF];
1947 sprintf(tmp, "%d", task_tgid_vnr(current));
1948 return ERR_PTR(vfs_follow_link(nd,tmp));
1951 static const struct inode_operations proc_self_inode_operations = {
1952 .readlink = proc_self_readlink,
1953 .follow_link = proc_self_follow_link,
1959 * These are the directory entries in the root directory of /proc
1960 * that properly belong to the /proc filesystem, as they describe
1961 * describe something that is process related.
1963 static const struct pid_entry proc_base_stuff[] = {
1964 NOD("self", S_IFLNK|S_IRWXUGO,
1965 &proc_self_inode_operations, NULL, {}),
1969 * Exceptional case: normally we are not allowed to unhash a busy
1970 * directory. In this case, however, we can do it - no aliasing problems
1971 * due to the way we treat inodes.
1973 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1975 struct inode *inode = dentry->d_inode;
1976 struct task_struct *task = get_proc_task(inode);
1978 put_task_struct(task);
1985 static struct dentry_operations proc_base_dentry_operations =
1987 .d_revalidate = proc_base_revalidate,
1988 .d_delete = pid_delete_dentry,
1991 static struct dentry *proc_base_instantiate(struct inode *dir,
1992 struct dentry *dentry, struct task_struct *task, const void *ptr)
1994 const struct pid_entry *p = ptr;
1995 struct inode *inode;
1996 struct proc_inode *ei;
1997 struct dentry *error = ERR_PTR(-EINVAL);
1999 /* Allocate the inode */
2000 error = ERR_PTR(-ENOMEM);
2001 inode = new_inode(dir->i_sb);
2005 /* Initialize the inode */
2007 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2010 * grab the reference to the task.
2012 ei->pid = get_task_pid(task, PIDTYPE_PID);
2018 inode->i_mode = p->mode;
2019 if (S_ISDIR(inode->i_mode))
2021 if (S_ISLNK(inode->i_mode))
2024 inode->i_op = p->iop;
2026 inode->i_fop = p->fop;
2028 dentry->d_op = &proc_base_dentry_operations;
2029 d_add(dentry, inode);
2038 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2040 struct dentry *error;
2041 struct task_struct *task = get_proc_task(dir);
2042 const struct pid_entry *p, *last;
2044 error = ERR_PTR(-ENOENT);
2049 /* Lookup the directory entry */
2050 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2051 for (p = proc_base_stuff; p <= last; p++) {
2052 if (p->len != dentry->d_name.len)
2054 if (!memcmp(dentry->d_name.name, p->name, p->len))
2060 error = proc_base_instantiate(dir, dentry, task, p);
2063 put_task_struct(task);
2068 static int proc_base_fill_cache(struct file *filp, void *dirent,
2069 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2071 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2072 proc_base_instantiate, task, p);
2075 #ifdef CONFIG_TASK_IO_ACCOUNTING
2076 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2078 return sprintf(buffer,
2079 #ifdef CONFIG_TASK_XACCT
2085 "read_bytes: %llu\n"
2086 "write_bytes: %llu\n"
2087 "cancelled_write_bytes: %llu\n",
2088 #ifdef CONFIG_TASK_XACCT
2089 (unsigned long long)task->rchar,
2090 (unsigned long long)task->wchar,
2091 (unsigned long long)task->syscr,
2092 (unsigned long long)task->syscw,
2094 (unsigned long long)task->ioac.read_bytes,
2095 (unsigned long long)task->ioac.write_bytes,
2096 (unsigned long long)task->ioac.cancelled_write_bytes);
2103 static const struct file_operations proc_task_operations;
2104 static const struct inode_operations proc_task_inode_operations;
2106 static const struct pid_entry tgid_base_stuff[] = {
2107 DIR("task", S_IRUGO|S_IXUGO, task),
2108 DIR("fd", S_IRUSR|S_IXUSR, fd),
2109 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2110 REG("environ", S_IRUSR, environ),
2111 INF("auxv", S_IRUSR, pid_auxv),
2112 INF("status", S_IRUGO, pid_status),
2113 #ifdef CONFIG_SCHED_DEBUG
2114 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2116 INF("cmdline", S_IRUGO, pid_cmdline),
2117 INF("stat", S_IRUGO, tgid_stat),
2118 INF("statm", S_IRUGO, pid_statm),
2119 REG("maps", S_IRUGO, maps),
2121 REG("numa_maps", S_IRUGO, numa_maps),
2123 REG("mem", S_IRUSR|S_IWUSR, mem),
2127 REG("mounts", S_IRUGO, mounts),
2128 REG("mountstats", S_IRUSR, mountstats),
2130 REG("clear_refs", S_IWUSR, clear_refs),
2131 REG("smaps", S_IRUGO, smaps),
2133 #ifdef CONFIG_SECURITY
2134 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2136 #ifdef CONFIG_KALLSYMS
2137 INF("wchan", S_IRUGO, pid_wchan),
2139 #ifdef CONFIG_SCHEDSTATS
2140 INF("schedstat", S_IRUGO, pid_schedstat),
2142 #ifdef CONFIG_PROC_PID_CPUSET
2143 REG("cpuset", S_IRUGO, cpuset),
2145 #ifdef CONFIG_CGROUPS
2146 REG("cgroup", S_IRUGO, cgroup),
2148 INF("oom_score", S_IRUGO, oom_score),
2149 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2150 #ifdef CONFIG_AUDITSYSCALL
2151 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2153 #ifdef CONFIG_FAULT_INJECTION
2154 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2156 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2157 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2159 #ifdef CONFIG_TASK_IO_ACCOUNTING
2160 INF("io", S_IRUGO, pid_io_accounting),
2164 static int proc_tgid_base_readdir(struct file * filp,
2165 void * dirent, filldir_t filldir)
2167 return proc_pident_readdir(filp,dirent,filldir,
2168 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2171 static const struct file_operations proc_tgid_base_operations = {
2172 .read = generic_read_dir,
2173 .readdir = proc_tgid_base_readdir,
2176 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2177 return proc_pident_lookup(dir, dentry,
2178 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2181 static const struct inode_operations proc_tgid_base_inode_operations = {
2182 .lookup = proc_tgid_base_lookup,
2183 .getattr = pid_getattr,
2184 .setattr = proc_setattr,
2188 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2190 * @task: task that should be flushed.
2192 * Looks in the dcache for
2194 * /proc/@tgid/task/@pid
2195 * if either directory is present flushes it and all of it'ts children
2198 * It is safe and reasonable to cache /proc entries for a task until
2199 * that task exits. After that they just clog up the dcache with
2200 * useless entries, possibly causing useful dcache entries to be
2201 * flushed instead. This routine is proved to flush those useless
2202 * dcache entries at process exit time.
2204 * NOTE: This routine is just an optimization so it does not guarantee
2205 * that no dcache entries will exist at process exit time it
2206 * just makes it very unlikely that any will persist.
2208 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2210 struct dentry *dentry, *leader, *dir;
2211 char buf[PROC_NUMBUF];
2215 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2216 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2218 shrink_dcache_parent(dentry);
2227 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2228 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2233 name.len = strlen(name.name);
2234 dir = d_hash_and_lookup(leader, &name);
2236 goto out_put_leader;
2239 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2240 dentry = d_hash_and_lookup(dir, &name);
2242 shrink_dcache_parent(dentry);
2255 * when flushing dentries from proc one need to flush them from global
2256 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2257 * in. this call is supposed to make all this job.
2260 void proc_flush_task(struct task_struct *task)
2263 struct pid *pid, *tgid;
2266 leader = thread_group_leader(task);
2267 proc_flush_task_mnt(proc_mnt, task->pid, leader ? task->tgid : 0);
2268 pid = task_pid(task);
2269 if (pid->level == 0)
2272 tgid = task_tgid(task);
2273 for (i = 1; i <= pid->level; i++) {
2274 upid = &pid->numbers[i];
2275 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2276 leader ? 0 : tgid->numbers[i].nr);
2279 upid = &pid->numbers[pid->level];
2281 pid_ns_release_proc(upid->ns);
2284 static struct dentry *proc_pid_instantiate(struct inode *dir,
2285 struct dentry * dentry,
2286 struct task_struct *task, const void *ptr)
2288 struct dentry *error = ERR_PTR(-ENOENT);
2289 struct inode *inode;
2291 inode = proc_pid_make_inode(dir->i_sb, task);
2295 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2296 inode->i_op = &proc_tgid_base_inode_operations;
2297 inode->i_fop = &proc_tgid_base_operations;
2298 inode->i_flags|=S_IMMUTABLE;
2300 #ifdef CONFIG_SECURITY
2301 inode->i_nlink += 1;
2304 dentry->d_op = &pid_dentry_operations;
2306 d_add(dentry, inode);
2307 /* Close the race of the process dying before we return the dentry */
2308 if (pid_revalidate(dentry, NULL))
2314 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2316 struct dentry *result = ERR_PTR(-ENOENT);
2317 struct task_struct *task;
2319 struct pid_namespace *ns;
2321 result = proc_base_lookup(dir, dentry);
2322 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2325 tgid = name_to_int(dentry);
2329 ns = dentry->d_sb->s_fs_info;
2331 task = find_task_by_pid_ns(tgid, ns);
2333 get_task_struct(task);
2338 result = proc_pid_instantiate(dir, dentry, task, NULL);
2339 put_task_struct(task);
2345 * Find the first task with tgid >= tgid
2348 static struct task_struct *next_tgid(unsigned int tgid,
2349 struct pid_namespace *ns)
2351 struct task_struct *task;
2357 pid = find_ge_pid(tgid, ns);
2359 tgid = pid_nr_ns(pid, ns) + 1;
2360 task = pid_task(pid, PIDTYPE_PID);
2361 /* What we to know is if the pid we have find is the
2362 * pid of a thread_group_leader. Testing for task
2363 * being a thread_group_leader is the obvious thing
2364 * todo but there is a window when it fails, due to
2365 * the pid transfer logic in de_thread.
2367 * So we perform the straight forward test of seeing
2368 * if the pid we have found is the pid of a thread
2369 * group leader, and don't worry if the task we have
2370 * found doesn't happen to be a thread group leader.
2371 * As we don't care in the case of readdir.
2373 if (!task || !has_group_leader_pid(task))
2375 get_task_struct(task);
2381 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2383 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2384 struct task_struct *task, int tgid)
2386 char name[PROC_NUMBUF];
2387 int len = snprintf(name, sizeof(name), "%d", tgid);
2388 return proc_fill_cache(filp, dirent, filldir, name, len,
2389 proc_pid_instantiate, task, NULL);
2392 /* for the /proc/ directory itself, after non-process stuff has been done */
2393 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2395 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2396 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2397 struct task_struct *task;
2399 struct pid_namespace *ns;
2404 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2405 const struct pid_entry *p = &proc_base_stuff[nr];
2406 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2410 ns = filp->f_dentry->d_sb->s_fs_info;
2411 tgid = filp->f_pos - TGID_OFFSET;
2412 for (task = next_tgid(tgid, ns);
2414 put_task_struct(task), task = next_tgid(tgid + 1, ns)) {
2415 tgid = task_pid_nr_ns(task, ns);
2416 filp->f_pos = tgid + TGID_OFFSET;
2417 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2418 put_task_struct(task);
2422 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2424 put_task_struct(reaper);
2432 static const struct pid_entry tid_base_stuff[] = {
2433 DIR("fd", S_IRUSR|S_IXUSR, fd),
2434 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2435 REG("environ", S_IRUSR, environ),
2436 INF("auxv", S_IRUSR, pid_auxv),
2437 INF("status", S_IRUGO, pid_status),
2438 #ifdef CONFIG_SCHED_DEBUG
2439 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2441 INF("cmdline", S_IRUGO, pid_cmdline),
2442 INF("stat", S_IRUGO, tid_stat),
2443 INF("statm", S_IRUGO, pid_statm),
2444 REG("maps", S_IRUGO, maps),
2446 REG("numa_maps", S_IRUGO, numa_maps),
2448 REG("mem", S_IRUSR|S_IWUSR, mem),
2452 REG("mounts", S_IRUGO, mounts),
2454 REG("clear_refs", S_IWUSR, clear_refs),
2455 REG("smaps", S_IRUGO, smaps),
2457 #ifdef CONFIG_SECURITY
2458 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2460 #ifdef CONFIG_KALLSYMS
2461 INF("wchan", S_IRUGO, pid_wchan),
2463 #ifdef CONFIG_SCHEDSTATS
2464 INF("schedstat", S_IRUGO, pid_schedstat),
2466 #ifdef CONFIG_PROC_PID_CPUSET
2467 REG("cpuset", S_IRUGO, cpuset),
2469 #ifdef CONFIG_CGROUPS
2470 REG("cgroup", S_IRUGO, cgroup),
2472 INF("oom_score", S_IRUGO, oom_score),
2473 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2474 #ifdef CONFIG_AUDITSYSCALL
2475 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2477 #ifdef CONFIG_FAULT_INJECTION
2478 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2482 static int proc_tid_base_readdir(struct file * filp,
2483 void * dirent, filldir_t filldir)
2485 return proc_pident_readdir(filp,dirent,filldir,
2486 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2489 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2490 return proc_pident_lookup(dir, dentry,
2491 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2494 static const struct file_operations proc_tid_base_operations = {
2495 .read = generic_read_dir,
2496 .readdir = proc_tid_base_readdir,
2499 static const struct inode_operations proc_tid_base_inode_operations = {
2500 .lookup = proc_tid_base_lookup,
2501 .getattr = pid_getattr,
2502 .setattr = proc_setattr,
2505 static struct dentry *proc_task_instantiate(struct inode *dir,
2506 struct dentry *dentry, struct task_struct *task, const void *ptr)
2508 struct dentry *error = ERR_PTR(-ENOENT);
2509 struct inode *inode;
2510 inode = proc_pid_make_inode(dir->i_sb, task);
2514 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2515 inode->i_op = &proc_tid_base_inode_operations;
2516 inode->i_fop = &proc_tid_base_operations;
2517 inode->i_flags|=S_IMMUTABLE;
2519 #ifdef CONFIG_SECURITY
2520 inode->i_nlink += 1;
2523 dentry->d_op = &pid_dentry_operations;
2525 d_add(dentry, inode);
2526 /* Close the race of the process dying before we return the dentry */
2527 if (pid_revalidate(dentry, NULL))
2533 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2535 struct dentry *result = ERR_PTR(-ENOENT);
2536 struct task_struct *task;
2537 struct task_struct *leader = get_proc_task(dir);
2539 struct pid_namespace *ns;
2544 tid = name_to_int(dentry);
2548 ns = dentry->d_sb->s_fs_info;
2550 task = find_task_by_pid_ns(tid, ns);
2552 get_task_struct(task);
2556 if (!same_thread_group(leader, task))
2559 result = proc_task_instantiate(dir, dentry, task, NULL);
2561 put_task_struct(task);
2563 put_task_struct(leader);
2569 * Find the first tid of a thread group to return to user space.
2571 * Usually this is just the thread group leader, but if the users
2572 * buffer was too small or there was a seek into the middle of the
2573 * directory we have more work todo.
2575 * In the case of a short read we start with find_task_by_pid.
2577 * In the case of a seek we start with the leader and walk nr
2580 static struct task_struct *first_tid(struct task_struct *leader,
2581 int tid, int nr, struct pid_namespace *ns)
2583 struct task_struct *pos;
2586 /* Attempt to start with the pid of a thread */
2587 if (tid && (nr > 0)) {
2588 pos = find_task_by_pid_ns(tid, ns);
2589 if (pos && (pos->group_leader == leader))
2593 /* If nr exceeds the number of threads there is nothing todo */
2595 if (nr && nr >= get_nr_threads(leader))
2598 /* If we haven't found our starting place yet start
2599 * with the leader and walk nr threads forward.
2601 for (pos = leader; nr > 0; --nr) {
2602 pos = next_thread(pos);
2603 if (pos == leader) {
2609 get_task_struct(pos);
2616 * Find the next thread in the thread list.
2617 * Return NULL if there is an error or no next thread.
2619 * The reference to the input task_struct is released.
2621 static struct task_struct *next_tid(struct task_struct *start)
2623 struct task_struct *pos = NULL;
2625 if (pid_alive(start)) {
2626 pos = next_thread(start);
2627 if (thread_group_leader(pos))
2630 get_task_struct(pos);
2633 put_task_struct(start);
2637 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2638 struct task_struct *task, int tid)
2640 char name[PROC_NUMBUF];
2641 int len = snprintf(name, sizeof(name), "%d", tid);
2642 return proc_fill_cache(filp, dirent, filldir, name, len,
2643 proc_task_instantiate, task, NULL);
2646 /* for the /proc/TGID/task/ directories */
2647 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2649 struct dentry *dentry = filp->f_path.dentry;
2650 struct inode *inode = dentry->d_inode;
2651 struct task_struct *leader = NULL;
2652 struct task_struct *task;
2653 int retval = -ENOENT;
2656 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2657 struct pid_namespace *ns;
2659 task = get_proc_task(inode);
2663 if (pid_alive(task)) {
2664 leader = task->group_leader;
2665 get_task_struct(leader);
2668 put_task_struct(task);
2676 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2681 ino = parent_ino(dentry);
2682 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2688 /* f_version caches the tgid value that the last readdir call couldn't
2689 * return. lseek aka telldir automagically resets f_version to 0.
2691 ns = filp->f_dentry->d_sb->s_fs_info;
2692 tid = (int)filp->f_version;
2693 filp->f_version = 0;
2694 for (task = first_tid(leader, tid, pos - 2, ns);
2696 task = next_tid(task), pos++) {
2697 tid = task_pid_nr_ns(task, ns);
2698 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2699 /* returning this tgid failed, save it as the first
2700 * pid for the next readir call */
2701 filp->f_version = (u64)tid;
2702 put_task_struct(task);
2708 put_task_struct(leader);
2713 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2715 struct inode *inode = dentry->d_inode;
2716 struct task_struct *p = get_proc_task(inode);
2717 generic_fillattr(inode, stat);
2721 stat->nlink += get_nr_threads(p);
2729 static const struct inode_operations proc_task_inode_operations = {
2730 .lookup = proc_task_lookup,
2731 .getattr = proc_task_getattr,
2732 .setattr = proc_setattr,
2735 static const struct file_operations proc_task_operations = {
2736 .read = generic_read_dir,
2737 .readdir = proc_task_readdir,