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/cpuset.h>
71 #include <linux/audit.h>
72 #include <linux/poll.h>
73 #include <linux/nsproxy.h>
74 #include <linux/oom.h>
75 #include <linux/elf.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_cmdline(struct task_struct *task, char * buffer)
206 struct mm_struct *mm = get_task_mm(task);
210 goto out_mm; /* Shh! No looking before we're done */
212 len = mm->arg_end - mm->arg_start;
217 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
219 // If the nul at the end of args has been overwritten, then
220 // assume application is using setproctitle(3).
221 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
222 len = strnlen(buffer, res);
226 len = mm->env_end - mm->env_start;
227 if (len > PAGE_SIZE - res)
228 len = PAGE_SIZE - res;
229 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
230 res = strnlen(buffer, res);
239 static int proc_pid_auxv(struct task_struct *task, char *buffer)
242 struct mm_struct *mm = get_task_mm(task);
244 unsigned int nwords = 0;
247 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
248 res = nwords * sizeof(mm->saved_auxv[0]);
251 memcpy(buffer, mm->saved_auxv, res);
258 #ifdef CONFIG_KALLSYMS
260 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
261 * Returns the resolved symbol. If that fails, simply return the address.
263 static int proc_pid_wchan(struct task_struct *task, char *buffer)
266 char symname[KSYM_NAME_LEN];
268 wchan = get_wchan(task);
270 if (lookup_symbol_name(wchan, symname) < 0)
271 return sprintf(buffer, "%lu", wchan);
273 return sprintf(buffer, "%s", symname);
275 #endif /* CONFIG_KALLSYMS */
277 #ifdef CONFIG_SCHEDSTATS
279 * Provides /proc/PID/schedstat
281 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
283 return sprintf(buffer, "%llu %llu %lu\n",
284 task->sched_info.cpu_time,
285 task->sched_info.run_delay,
286 task->sched_info.pcount);
290 /* The badness from the OOM killer */
291 unsigned long badness(struct task_struct *p, unsigned long uptime);
292 static int proc_oom_score(struct task_struct *task, char *buffer)
294 unsigned long points;
295 struct timespec uptime;
297 do_posix_clock_monotonic_gettime(&uptime);
298 read_lock(&tasklist_lock);
299 points = badness(task, uptime.tv_sec);
300 read_unlock(&tasklist_lock);
301 return sprintf(buffer, "%lu\n", points);
304 /************************************************************************/
305 /* Here the fs part begins */
306 /************************************************************************/
308 /* permission checks */
309 static int proc_fd_access_allowed(struct inode *inode)
311 struct task_struct *task;
313 /* Allow access to a task's file descriptors if it is us or we
314 * may use ptrace attach to the process and find out that
317 task = get_proc_task(inode);
319 allowed = ptrace_may_attach(task);
320 put_task_struct(task);
325 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
328 struct inode *inode = dentry->d_inode;
330 if (attr->ia_valid & ATTR_MODE)
333 error = inode_change_ok(inode, attr);
335 error = inode_setattr(inode, attr);
339 static const struct inode_operations proc_def_inode_operations = {
340 .setattr = proc_setattr,
343 extern struct seq_operations mounts_op;
349 static int mounts_open(struct inode *inode, struct file *file)
351 struct task_struct *task = get_proc_task(inode);
352 struct mnt_namespace *ns = NULL;
353 struct proc_mounts *p;
359 ns = task->nsproxy->mnt_ns;
364 put_task_struct(task);
369 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
371 file->private_data = &p->m;
372 ret = seq_open(file, &mounts_op);
375 p->event = ns->event;
385 static int mounts_release(struct inode *inode, struct file *file)
387 struct seq_file *m = file->private_data;
388 struct mnt_namespace *ns = m->private;
390 return seq_release(inode, file);
393 static unsigned mounts_poll(struct file *file, poll_table *wait)
395 struct proc_mounts *p = file->private_data;
396 struct mnt_namespace *ns = p->m.private;
399 poll_wait(file, &ns->poll, wait);
401 spin_lock(&vfsmount_lock);
402 if (p->event != ns->event) {
403 p->event = ns->event;
406 spin_unlock(&vfsmount_lock);
411 static const struct file_operations proc_mounts_operations = {
415 .release = mounts_release,
419 extern struct seq_operations mountstats_op;
420 static int mountstats_open(struct inode *inode, struct file *file)
422 int ret = seq_open(file, &mountstats_op);
425 struct seq_file *m = file->private_data;
426 struct mnt_namespace *mnt_ns = NULL;
427 struct task_struct *task = get_proc_task(inode);
432 mnt_ns = task->nsproxy->mnt_ns;
436 put_task_struct(task);
442 seq_release(inode, file);
449 static const struct file_operations proc_mountstats_operations = {
450 .open = mountstats_open,
453 .release = mounts_release,
456 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
458 static ssize_t proc_info_read(struct file * file, char __user * buf,
459 size_t count, loff_t *ppos)
461 struct inode * inode = file->f_path.dentry->d_inode;
464 struct task_struct *task = get_proc_task(inode);
470 if (count > PROC_BLOCK_SIZE)
471 count = PROC_BLOCK_SIZE;
474 if (!(page = __get_free_page(GFP_TEMPORARY)))
477 length = PROC_I(inode)->op.proc_read(task, (char*)page);
480 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
483 put_task_struct(task);
488 static const struct file_operations proc_info_file_operations = {
489 .read = proc_info_read,
492 static int mem_open(struct inode* inode, struct file* file)
494 file->private_data = (void*)((long)current->self_exec_id);
498 static ssize_t mem_read(struct file * file, char __user * buf,
499 size_t count, loff_t *ppos)
501 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
503 unsigned long src = *ppos;
505 struct mm_struct *mm;
510 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
514 page = (char *)__get_free_page(GFP_TEMPORARY);
520 mm = get_task_mm(task);
526 if (file->private_data != (void*)((long)current->self_exec_id))
532 int this_len, retval;
534 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
535 retval = access_process_vm(task, src, page, this_len, 0);
536 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
542 if (copy_to_user(buf, page, retval)) {
557 free_page((unsigned long) page);
559 put_task_struct(task);
564 #define mem_write NULL
567 /* This is a security hazard */
568 static ssize_t mem_write(struct file * file, const char __user *buf,
569 size_t count, loff_t *ppos)
573 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
574 unsigned long dst = *ppos;
580 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
584 page = (char *)__get_free_page(GFP_TEMPORARY);
590 int this_len, retval;
592 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
593 if (copy_from_user(page, buf, this_len)) {
597 retval = access_process_vm(task, dst, page, this_len, 1);
609 free_page((unsigned long) page);
611 put_task_struct(task);
617 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
621 file->f_pos = offset;
624 file->f_pos += offset;
629 force_successful_syscall_return();
633 static const struct file_operations proc_mem_operations = {
640 static ssize_t environ_read(struct file *file, char __user *buf,
641 size_t count, loff_t *ppos)
643 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
645 unsigned long src = *ppos;
647 struct mm_struct *mm;
652 if (!ptrace_may_attach(task))
656 page = (char *)__get_free_page(GFP_TEMPORARY);
662 mm = get_task_mm(task);
667 int this_len, retval, max_len;
669 this_len = mm->env_end - (mm->env_start + src);
674 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
675 this_len = (this_len > max_len) ? max_len : this_len;
677 retval = access_process_vm(task, (mm->env_start + src),
685 if (copy_to_user(buf, page, retval)) {
699 free_page((unsigned long) page);
701 put_task_struct(task);
706 static const struct file_operations proc_environ_operations = {
707 .read = environ_read,
710 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
711 size_t count, loff_t *ppos)
713 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
714 char buffer[PROC_NUMBUF];
720 oom_adjust = task->oomkilladj;
721 put_task_struct(task);
723 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
725 return simple_read_from_buffer(buf, count, ppos, buffer, len);
728 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
729 size_t count, loff_t *ppos)
731 struct task_struct *task;
732 char buffer[PROC_NUMBUF], *end;
735 memset(buffer, 0, sizeof(buffer));
736 if (count > sizeof(buffer) - 1)
737 count = sizeof(buffer) - 1;
738 if (copy_from_user(buffer, buf, count))
740 oom_adjust = simple_strtol(buffer, &end, 0);
741 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
742 oom_adjust != OOM_DISABLE)
746 task = get_proc_task(file->f_path.dentry->d_inode);
749 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
750 put_task_struct(task);
753 task->oomkilladj = oom_adjust;
754 put_task_struct(task);
755 if (end - buffer == 0)
760 static const struct file_operations proc_oom_adjust_operations = {
761 .read = oom_adjust_read,
762 .write = oom_adjust_write,
766 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
767 size_t count, loff_t *ppos)
769 struct task_struct *task;
770 char buffer[PROC_NUMBUF], *end;
771 struct mm_struct *mm;
773 memset(buffer, 0, sizeof(buffer));
774 if (count > sizeof(buffer) - 1)
775 count = sizeof(buffer) - 1;
776 if (copy_from_user(buffer, buf, count))
778 if (!simple_strtol(buffer, &end, 0))
782 task = get_proc_task(file->f_path.dentry->d_inode);
785 mm = get_task_mm(task);
790 put_task_struct(task);
791 if (end - buffer == 0)
796 static struct file_operations proc_clear_refs_operations = {
797 .write = clear_refs_write,
801 #ifdef CONFIG_AUDITSYSCALL
803 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
804 size_t count, loff_t *ppos)
806 struct inode * inode = file->f_path.dentry->d_inode;
807 struct task_struct *task = get_proc_task(inode);
809 char tmpbuf[TMPBUFLEN];
813 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
814 audit_get_loginuid(task->audit_context));
815 put_task_struct(task);
816 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
819 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
820 size_t count, loff_t *ppos)
822 struct inode * inode = file->f_path.dentry->d_inode;
827 if (!capable(CAP_AUDIT_CONTROL))
830 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
833 if (count >= PAGE_SIZE)
834 count = PAGE_SIZE - 1;
837 /* No partial writes. */
840 page = (char*)__get_free_page(GFP_TEMPORARY);
844 if (copy_from_user(page, buf, count))
848 loginuid = simple_strtoul(page, &tmp, 10);
854 length = audit_set_loginuid(current, loginuid);
855 if (likely(length == 0))
859 free_page((unsigned long) page);
863 static const struct file_operations proc_loginuid_operations = {
864 .read = proc_loginuid_read,
865 .write = proc_loginuid_write,
869 #ifdef CONFIG_FAULT_INJECTION
870 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
871 size_t count, loff_t *ppos)
873 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
874 char buffer[PROC_NUMBUF];
880 make_it_fail = task->make_it_fail;
881 put_task_struct(task);
883 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
885 return simple_read_from_buffer(buf, count, ppos, buffer, len);
888 static ssize_t proc_fault_inject_write(struct file * file,
889 const char __user * buf, size_t count, loff_t *ppos)
891 struct task_struct *task;
892 char buffer[PROC_NUMBUF], *end;
895 if (!capable(CAP_SYS_RESOURCE))
897 memset(buffer, 0, sizeof(buffer));
898 if (count > sizeof(buffer) - 1)
899 count = sizeof(buffer) - 1;
900 if (copy_from_user(buffer, buf, count))
902 make_it_fail = simple_strtol(buffer, &end, 0);
905 task = get_proc_task(file->f_dentry->d_inode);
908 task->make_it_fail = make_it_fail;
909 put_task_struct(task);
910 if (end - buffer == 0)
915 static const struct file_operations proc_fault_inject_operations = {
916 .read = proc_fault_inject_read,
917 .write = proc_fault_inject_write,
921 #ifdef CONFIG_SCHED_DEBUG
923 * Print out various scheduling related per-task fields:
925 static int sched_show(struct seq_file *m, void *v)
927 struct inode *inode = m->private;
928 struct task_struct *p;
932 p = get_proc_task(inode);
935 proc_sched_show_task(p, m);
943 sched_write(struct file *file, const char __user *buf,
944 size_t count, loff_t *offset)
946 struct inode *inode = file->f_path.dentry->d_inode;
947 struct task_struct *p;
951 p = get_proc_task(inode);
954 proc_sched_set_task(p);
961 static int sched_open(struct inode *inode, struct file *filp)
965 ret = single_open(filp, sched_show, NULL);
967 struct seq_file *m = filp->private_data;
974 static const struct file_operations proc_pid_sched_operations = {
977 .write = sched_write,
979 .release = single_release,
984 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
986 struct inode *inode = dentry->d_inode;
989 /* We don't need a base pointer in the /proc filesystem */
992 /* Are we allowed to snoop on the tasks file descriptors? */
993 if (!proc_fd_access_allowed(inode))
996 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
997 nd->last_type = LAST_BIND;
999 return ERR_PTR(error);
1002 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1003 char __user *buffer, int buflen)
1005 struct inode * inode;
1006 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1013 inode = dentry->d_inode;
1014 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1015 len = PTR_ERR(path);
1018 len = tmp + PAGE_SIZE - 1 - path;
1022 if (copy_to_user(buffer, path, len))
1025 free_page((unsigned long)tmp);
1029 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1031 int error = -EACCES;
1032 struct inode *inode = dentry->d_inode;
1034 struct vfsmount *mnt = NULL;
1036 /* Are we allowed to snoop on the tasks file descriptors? */
1037 if (!proc_fd_access_allowed(inode))
1040 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1044 error = do_proc_readlink(de, mnt, buffer, buflen);
1051 static const struct inode_operations proc_pid_link_inode_operations = {
1052 .readlink = proc_pid_readlink,
1053 .follow_link = proc_pid_follow_link,
1054 .setattr = proc_setattr,
1058 /* building an inode */
1060 static int task_dumpable(struct task_struct *task)
1063 struct mm_struct *mm;
1068 dumpable = get_dumpable(mm);
1076 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1078 struct inode * inode;
1079 struct proc_inode *ei;
1081 /* We need a new inode */
1083 inode = new_inode(sb);
1089 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1090 inode->i_op = &proc_def_inode_operations;
1093 * grab the reference to task.
1095 ei->pid = get_task_pid(task, PIDTYPE_PID);
1101 if (task_dumpable(task)) {
1102 inode->i_uid = task->euid;
1103 inode->i_gid = task->egid;
1105 security_task_to_inode(task, inode);
1115 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1117 struct inode *inode = dentry->d_inode;
1118 struct task_struct *task;
1119 generic_fillattr(inode, stat);
1124 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1126 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1127 task_dumpable(task)) {
1128 stat->uid = task->euid;
1129 stat->gid = task->egid;
1139 * Exceptional case: normally we are not allowed to unhash a busy
1140 * directory. In this case, however, we can do it - no aliasing problems
1141 * due to the way we treat inodes.
1143 * Rewrite the inode's ownerships here because the owning task may have
1144 * performed a setuid(), etc.
1146 * Before the /proc/pid/status file was created the only way to read
1147 * the effective uid of a /process was to stat /proc/pid. Reading
1148 * /proc/pid/status is slow enough that procps and other packages
1149 * kept stating /proc/pid. To keep the rules in /proc simple I have
1150 * made this apply to all per process world readable and executable
1153 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1155 struct inode *inode = dentry->d_inode;
1156 struct task_struct *task = get_proc_task(inode);
1158 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1159 task_dumpable(task)) {
1160 inode->i_uid = task->euid;
1161 inode->i_gid = task->egid;
1166 inode->i_mode &= ~(S_ISUID | S_ISGID);
1167 security_task_to_inode(task, inode);
1168 put_task_struct(task);
1175 static int pid_delete_dentry(struct dentry * dentry)
1177 /* Is the task we represent dead?
1178 * If so, then don't put the dentry on the lru list,
1179 * kill it immediately.
1181 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1184 static struct dentry_operations pid_dentry_operations =
1186 .d_revalidate = pid_revalidate,
1187 .d_delete = pid_delete_dentry,
1192 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1193 struct task_struct *, const void *);
1196 * Fill a directory entry.
1198 * If possible create the dcache entry and derive our inode number and
1199 * file type from dcache entry.
1201 * Since all of the proc inode numbers are dynamically generated, the inode
1202 * numbers do not exist until the inode is cache. This means creating the
1203 * the dcache entry in readdir is necessary to keep the inode numbers
1204 * reported by readdir in sync with the inode numbers reported
1207 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1208 char *name, int len,
1209 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1211 struct dentry *child, *dir = filp->f_path.dentry;
1212 struct inode *inode;
1215 unsigned type = DT_UNKNOWN;
1219 qname.hash = full_name_hash(name, len);
1221 child = d_lookup(dir, &qname);
1224 new = d_alloc(dir, &qname);
1226 child = instantiate(dir->d_inode, new, task, ptr);
1233 if (!child || IS_ERR(child) || !child->d_inode)
1234 goto end_instantiate;
1235 inode = child->d_inode;
1238 type = inode->i_mode >> 12;
1243 ino = find_inode_number(dir, &qname);
1246 return filldir(dirent, name, len, filp->f_pos, ino, type);
1249 static unsigned name_to_int(struct dentry *dentry)
1251 const char *name = dentry->d_name.name;
1252 int len = dentry->d_name.len;
1255 if (len > 1 && *name == '0')
1258 unsigned c = *name++ - '0';
1261 if (n >= (~0U-9)/10)
1271 #define PROC_FDINFO_MAX 64
1273 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1274 struct vfsmount **mnt, char *info)
1276 struct task_struct *task = get_proc_task(inode);
1277 struct files_struct *files = NULL;
1279 int fd = proc_fd(inode);
1282 files = get_files_struct(task);
1283 put_task_struct(task);
1287 * We are not taking a ref to the file structure, so we must
1290 spin_lock(&files->file_lock);
1291 file = fcheck_files(files, fd);
1294 *mnt = mntget(file->f_path.mnt);
1296 *dentry = dget(file->f_path.dentry);
1298 snprintf(info, PROC_FDINFO_MAX,
1301 (long long) file->f_pos,
1303 spin_unlock(&files->file_lock);
1304 put_files_struct(files);
1307 spin_unlock(&files->file_lock);
1308 put_files_struct(files);
1313 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1314 struct vfsmount **mnt)
1316 return proc_fd_info(inode, dentry, mnt, NULL);
1319 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1321 struct inode *inode = dentry->d_inode;
1322 struct task_struct *task = get_proc_task(inode);
1323 int fd = proc_fd(inode);
1324 struct files_struct *files;
1327 files = get_files_struct(task);
1330 if (fcheck_files(files, fd)) {
1332 put_files_struct(files);
1333 if (task_dumpable(task)) {
1334 inode->i_uid = task->euid;
1335 inode->i_gid = task->egid;
1340 inode->i_mode &= ~(S_ISUID | S_ISGID);
1341 security_task_to_inode(task, inode);
1342 put_task_struct(task);
1346 put_files_struct(files);
1348 put_task_struct(task);
1354 static struct dentry_operations tid_fd_dentry_operations =
1356 .d_revalidate = tid_fd_revalidate,
1357 .d_delete = pid_delete_dentry,
1360 static struct dentry *proc_fd_instantiate(struct inode *dir,
1361 struct dentry *dentry, struct task_struct *task, const void *ptr)
1363 unsigned fd = *(const unsigned *)ptr;
1365 struct files_struct *files;
1366 struct inode *inode;
1367 struct proc_inode *ei;
1368 struct dentry *error = ERR_PTR(-ENOENT);
1370 inode = proc_pid_make_inode(dir->i_sb, task);
1375 files = get_files_struct(task);
1378 inode->i_mode = S_IFLNK;
1381 * We are not taking a ref to the file structure, so we must
1384 spin_lock(&files->file_lock);
1385 file = fcheck_files(files, fd);
1388 if (file->f_mode & 1)
1389 inode->i_mode |= S_IRUSR | S_IXUSR;
1390 if (file->f_mode & 2)
1391 inode->i_mode |= S_IWUSR | S_IXUSR;
1392 spin_unlock(&files->file_lock);
1393 put_files_struct(files);
1395 inode->i_op = &proc_pid_link_inode_operations;
1397 ei->op.proc_get_link = proc_fd_link;
1398 dentry->d_op = &tid_fd_dentry_operations;
1399 d_add(dentry, inode);
1400 /* Close the race of the process dying before we return the dentry */
1401 if (tid_fd_revalidate(dentry, NULL))
1407 spin_unlock(&files->file_lock);
1408 put_files_struct(files);
1414 static struct dentry *proc_lookupfd_common(struct inode *dir,
1415 struct dentry *dentry,
1416 instantiate_t instantiate)
1418 struct task_struct *task = get_proc_task(dir);
1419 unsigned fd = name_to_int(dentry);
1420 struct dentry *result = ERR_PTR(-ENOENT);
1427 result = instantiate(dir, dentry, task, &fd);
1429 put_task_struct(task);
1434 static int proc_readfd_common(struct file * filp, void * dirent,
1435 filldir_t filldir, instantiate_t instantiate)
1437 struct dentry *dentry = filp->f_path.dentry;
1438 struct inode *inode = dentry->d_inode;
1439 struct task_struct *p = get_proc_task(inode);
1440 unsigned int fd, tid, ino;
1442 struct files_struct * files;
1443 struct fdtable *fdt;
1454 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1458 ino = parent_ino(dentry);
1459 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1463 files = get_files_struct(p);
1467 fdt = files_fdtable(files);
1468 for (fd = filp->f_pos-2;
1470 fd++, filp->f_pos++) {
1471 char name[PROC_NUMBUF];
1474 if (!fcheck_files(files, fd))
1478 len = snprintf(name, sizeof(name), "%d", fd);
1479 if (proc_fill_cache(filp, dirent, filldir,
1480 name, len, instantiate,
1488 put_files_struct(files);
1496 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1497 struct nameidata *nd)
1499 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1502 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1504 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1507 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1508 size_t len, loff_t *ppos)
1510 char tmp[PROC_FDINFO_MAX];
1511 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1513 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1517 static const struct file_operations proc_fdinfo_file_operations = {
1518 .open = nonseekable_open,
1519 .read = proc_fdinfo_read,
1522 static const struct file_operations proc_fd_operations = {
1523 .read = generic_read_dir,
1524 .readdir = proc_readfd,
1528 * /proc/pid/fd needs a special permission handler so that a process can still
1529 * access /proc/self/fd after it has executed a setuid().
1531 static int proc_fd_permission(struct inode *inode, int mask,
1532 struct nameidata *nd)
1536 rv = generic_permission(inode, mask, NULL);
1539 if (task_pid(current) == proc_pid(inode))
1545 * proc directories can do almost nothing..
1547 static const struct inode_operations proc_fd_inode_operations = {
1548 .lookup = proc_lookupfd,
1549 .permission = proc_fd_permission,
1550 .setattr = proc_setattr,
1553 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1554 struct dentry *dentry, struct task_struct *task, const void *ptr)
1556 unsigned fd = *(unsigned *)ptr;
1557 struct inode *inode;
1558 struct proc_inode *ei;
1559 struct dentry *error = ERR_PTR(-ENOENT);
1561 inode = proc_pid_make_inode(dir->i_sb, task);
1566 inode->i_mode = S_IFREG | S_IRUSR;
1567 inode->i_fop = &proc_fdinfo_file_operations;
1568 dentry->d_op = &tid_fd_dentry_operations;
1569 d_add(dentry, inode);
1570 /* Close the race of the process dying before we return the dentry */
1571 if (tid_fd_revalidate(dentry, NULL))
1578 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1579 struct dentry *dentry,
1580 struct nameidata *nd)
1582 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1585 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1587 return proc_readfd_common(filp, dirent, filldir,
1588 proc_fdinfo_instantiate);
1591 static const struct file_operations proc_fdinfo_operations = {
1592 .read = generic_read_dir,
1593 .readdir = proc_readfdinfo,
1597 * proc directories can do almost nothing..
1599 static const struct inode_operations proc_fdinfo_inode_operations = {
1600 .lookup = proc_lookupfdinfo,
1601 .setattr = proc_setattr,
1605 static struct dentry *proc_pident_instantiate(struct inode *dir,
1606 struct dentry *dentry, struct task_struct *task, const void *ptr)
1608 const struct pid_entry *p = ptr;
1609 struct inode *inode;
1610 struct proc_inode *ei;
1611 struct dentry *error = ERR_PTR(-EINVAL);
1613 inode = proc_pid_make_inode(dir->i_sb, task);
1618 inode->i_mode = p->mode;
1619 if (S_ISDIR(inode->i_mode))
1620 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1622 inode->i_op = p->iop;
1624 inode->i_fop = p->fop;
1626 dentry->d_op = &pid_dentry_operations;
1627 d_add(dentry, inode);
1628 /* Close the race of the process dying before we return the dentry */
1629 if (pid_revalidate(dentry, NULL))
1635 static struct dentry *proc_pident_lookup(struct inode *dir,
1636 struct dentry *dentry,
1637 const struct pid_entry *ents,
1640 struct inode *inode;
1641 struct dentry *error;
1642 struct task_struct *task = get_proc_task(dir);
1643 const struct pid_entry *p, *last;
1645 error = ERR_PTR(-ENOENT);
1652 * Yes, it does not scale. And it should not. Don't add
1653 * new entries into /proc/<tgid>/ without very good reasons.
1655 last = &ents[nents - 1];
1656 for (p = ents; p <= last; p++) {
1657 if (p->len != dentry->d_name.len)
1659 if (!memcmp(dentry->d_name.name, p->name, p->len))
1665 error = proc_pident_instantiate(dir, dentry, task, p);
1667 put_task_struct(task);
1672 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1673 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1675 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1676 proc_pident_instantiate, task, p);
1679 static int proc_pident_readdir(struct file *filp,
1680 void *dirent, filldir_t filldir,
1681 const struct pid_entry *ents, unsigned int nents)
1685 struct dentry *dentry = filp->f_path.dentry;
1686 struct inode *inode = dentry->d_inode;
1687 struct task_struct *task = get_proc_task(inode);
1688 const struct pid_entry *p, *last;
1702 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1708 ino = parent_ino(dentry);
1709 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1721 last = &ents[nents - 1];
1723 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1732 put_task_struct(task);
1737 #ifdef CONFIG_SECURITY
1738 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1739 size_t count, loff_t *ppos)
1741 struct inode * inode = file->f_path.dentry->d_inode;
1744 struct task_struct *task = get_proc_task(inode);
1749 length = security_getprocattr(task,
1750 (char*)file->f_path.dentry->d_name.name,
1752 put_task_struct(task);
1754 length = simple_read_from_buffer(buf, count, ppos, p, length);
1759 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1760 size_t count, loff_t *ppos)
1762 struct inode * inode = file->f_path.dentry->d_inode;
1765 struct task_struct *task = get_proc_task(inode);
1770 if (count > PAGE_SIZE)
1773 /* No partial writes. */
1779 page = (char*)__get_free_page(GFP_TEMPORARY);
1784 if (copy_from_user(page, buf, count))
1787 length = security_setprocattr(task,
1788 (char*)file->f_path.dentry->d_name.name,
1789 (void*)page, count);
1791 free_page((unsigned long) page);
1793 put_task_struct(task);
1798 static const struct file_operations proc_pid_attr_operations = {
1799 .read = proc_pid_attr_read,
1800 .write = proc_pid_attr_write,
1803 static const struct pid_entry attr_dir_stuff[] = {
1804 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1805 REG("prev", S_IRUGO, pid_attr),
1806 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1807 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1808 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1809 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1812 static int proc_attr_dir_readdir(struct file * filp,
1813 void * dirent, filldir_t filldir)
1815 return proc_pident_readdir(filp,dirent,filldir,
1816 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1819 static const struct file_operations proc_attr_dir_operations = {
1820 .read = generic_read_dir,
1821 .readdir = proc_attr_dir_readdir,
1824 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1825 struct dentry *dentry, struct nameidata *nd)
1827 return proc_pident_lookup(dir, dentry,
1828 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1831 static const struct inode_operations proc_attr_dir_inode_operations = {
1832 .lookup = proc_attr_dir_lookup,
1833 .getattr = pid_getattr,
1834 .setattr = proc_setattr,
1839 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1840 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1841 size_t count, loff_t *ppos)
1843 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1844 struct mm_struct *mm;
1845 char buffer[PROC_NUMBUF];
1853 mm = get_task_mm(task);
1855 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1856 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1857 MMF_DUMP_FILTER_SHIFT));
1859 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1862 put_task_struct(task);
1867 static ssize_t proc_coredump_filter_write(struct file *file,
1868 const char __user *buf,
1872 struct task_struct *task;
1873 struct mm_struct *mm;
1874 char buffer[PROC_NUMBUF], *end;
1881 memset(buffer, 0, sizeof(buffer));
1882 if (count > sizeof(buffer) - 1)
1883 count = sizeof(buffer) - 1;
1884 if (copy_from_user(buffer, buf, count))
1888 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1891 if (end - buffer == 0)
1895 task = get_proc_task(file->f_dentry->d_inode);
1900 mm = get_task_mm(task);
1904 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1906 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1908 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1913 put_task_struct(task);
1918 static const struct file_operations proc_coredump_filter_operations = {
1919 .read = proc_coredump_filter_read,
1920 .write = proc_coredump_filter_write,
1927 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1930 char tmp[PROC_NUMBUF];
1931 sprintf(tmp, "%d", current->tgid);
1932 return vfs_readlink(dentry,buffer,buflen,tmp);
1935 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1937 char tmp[PROC_NUMBUF];
1938 sprintf(tmp, "%d", current->tgid);
1939 return ERR_PTR(vfs_follow_link(nd,tmp));
1942 static const struct inode_operations proc_self_inode_operations = {
1943 .readlink = proc_self_readlink,
1944 .follow_link = proc_self_follow_link,
1950 * These are the directory entries in the root directory of /proc
1951 * that properly belong to the /proc filesystem, as they describe
1952 * describe something that is process related.
1954 static const struct pid_entry proc_base_stuff[] = {
1955 NOD("self", S_IFLNK|S_IRWXUGO,
1956 &proc_self_inode_operations, NULL, {}),
1960 * Exceptional case: normally we are not allowed to unhash a busy
1961 * directory. In this case, however, we can do it - no aliasing problems
1962 * due to the way we treat inodes.
1964 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1966 struct inode *inode = dentry->d_inode;
1967 struct task_struct *task = get_proc_task(inode);
1969 put_task_struct(task);
1976 static struct dentry_operations proc_base_dentry_operations =
1978 .d_revalidate = proc_base_revalidate,
1979 .d_delete = pid_delete_dentry,
1982 static struct dentry *proc_base_instantiate(struct inode *dir,
1983 struct dentry *dentry, struct task_struct *task, const void *ptr)
1985 const struct pid_entry *p = ptr;
1986 struct inode *inode;
1987 struct proc_inode *ei;
1988 struct dentry *error = ERR_PTR(-EINVAL);
1990 /* Allocate the inode */
1991 error = ERR_PTR(-ENOMEM);
1992 inode = new_inode(dir->i_sb);
1996 /* Initialize the inode */
1998 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2001 * grab the reference to the task.
2003 ei->pid = get_task_pid(task, PIDTYPE_PID);
2009 inode->i_mode = p->mode;
2010 if (S_ISDIR(inode->i_mode))
2012 if (S_ISLNK(inode->i_mode))
2015 inode->i_op = p->iop;
2017 inode->i_fop = p->fop;
2019 dentry->d_op = &proc_base_dentry_operations;
2020 d_add(dentry, inode);
2029 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2031 struct dentry *error;
2032 struct task_struct *task = get_proc_task(dir);
2033 const struct pid_entry *p, *last;
2035 error = ERR_PTR(-ENOENT);
2040 /* Lookup the directory entry */
2041 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2042 for (p = proc_base_stuff; p <= last; p++) {
2043 if (p->len != dentry->d_name.len)
2045 if (!memcmp(dentry->d_name.name, p->name, p->len))
2051 error = proc_base_instantiate(dir, dentry, task, p);
2054 put_task_struct(task);
2059 static int proc_base_fill_cache(struct file *filp, void *dirent,
2060 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2062 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2063 proc_base_instantiate, task, p);
2066 #ifdef CONFIG_TASK_IO_ACCOUNTING
2067 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2069 return sprintf(buffer,
2070 #ifdef CONFIG_TASK_XACCT
2076 "read_bytes: %llu\n"
2077 "write_bytes: %llu\n"
2078 "cancelled_write_bytes: %llu\n",
2079 #ifdef CONFIG_TASK_XACCT
2080 (unsigned long long)task->rchar,
2081 (unsigned long long)task->wchar,
2082 (unsigned long long)task->syscr,
2083 (unsigned long long)task->syscw,
2085 (unsigned long long)task->ioac.read_bytes,
2086 (unsigned long long)task->ioac.write_bytes,
2087 (unsigned long long)task->ioac.cancelled_write_bytes);
2094 static const struct file_operations proc_task_operations;
2095 static const struct inode_operations proc_task_inode_operations;
2097 static const struct pid_entry tgid_base_stuff[] = {
2098 DIR("task", S_IRUGO|S_IXUGO, task),
2099 DIR("fd", S_IRUSR|S_IXUSR, fd),
2100 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2101 REG("environ", S_IRUSR, environ),
2102 INF("auxv", S_IRUSR, pid_auxv),
2103 INF("status", S_IRUGO, pid_status),
2104 #ifdef CONFIG_SCHED_DEBUG
2105 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2107 INF("cmdline", S_IRUGO, pid_cmdline),
2108 INF("stat", S_IRUGO, tgid_stat),
2109 INF("statm", S_IRUGO, pid_statm),
2110 REG("maps", S_IRUGO, maps),
2112 REG("numa_maps", S_IRUGO, numa_maps),
2114 REG("mem", S_IRUSR|S_IWUSR, mem),
2118 REG("mounts", S_IRUGO, mounts),
2119 REG("mountstats", S_IRUSR, mountstats),
2121 REG("clear_refs", S_IWUSR, clear_refs),
2122 REG("smaps", S_IRUGO, smaps),
2124 #ifdef CONFIG_SECURITY
2125 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2127 #ifdef CONFIG_KALLSYMS
2128 INF("wchan", S_IRUGO, pid_wchan),
2130 #ifdef CONFIG_SCHEDSTATS
2131 INF("schedstat", S_IRUGO, pid_schedstat),
2133 #ifdef CONFIG_CPUSETS
2134 REG("cpuset", S_IRUGO, cpuset),
2136 INF("oom_score", S_IRUGO, oom_score),
2137 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2138 #ifdef CONFIG_AUDITSYSCALL
2139 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2141 #ifdef CONFIG_FAULT_INJECTION
2142 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2144 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2145 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2147 #ifdef CONFIG_TASK_IO_ACCOUNTING
2148 INF("io", S_IRUGO, pid_io_accounting),
2152 static int proc_tgid_base_readdir(struct file * filp,
2153 void * dirent, filldir_t filldir)
2155 return proc_pident_readdir(filp,dirent,filldir,
2156 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2159 static const struct file_operations proc_tgid_base_operations = {
2160 .read = generic_read_dir,
2161 .readdir = proc_tgid_base_readdir,
2164 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2165 return proc_pident_lookup(dir, dentry,
2166 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2169 static const struct inode_operations proc_tgid_base_inode_operations = {
2170 .lookup = proc_tgid_base_lookup,
2171 .getattr = pid_getattr,
2172 .setattr = proc_setattr,
2176 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2178 * @task: task that should be flushed.
2180 * Looks in the dcache for
2182 * /proc/@tgid/task/@pid
2183 * if either directory is present flushes it and all of it'ts children
2186 * It is safe and reasonable to cache /proc entries for a task until
2187 * that task exits. After that they just clog up the dcache with
2188 * useless entries, possibly causing useful dcache entries to be
2189 * flushed instead. This routine is proved to flush those useless
2190 * dcache entries at process exit time.
2192 * NOTE: This routine is just an optimization so it does not guarantee
2193 * that no dcache entries will exist at process exit time it
2194 * just makes it very unlikely that any will persist.
2196 void proc_flush_task(struct task_struct *task)
2198 struct dentry *dentry, *leader, *dir;
2199 char buf[PROC_NUMBUF];
2203 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2204 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2206 shrink_dcache_parent(dentry);
2211 if (thread_group_leader(task))
2215 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2216 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2221 name.len = strlen(name.name);
2222 dir = d_hash_and_lookup(leader, &name);
2224 goto out_put_leader;
2227 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2228 dentry = d_hash_and_lookup(dir, &name);
2230 shrink_dcache_parent(dentry);
2242 static struct dentry *proc_pid_instantiate(struct inode *dir,
2243 struct dentry * dentry,
2244 struct task_struct *task, const void *ptr)
2246 struct dentry *error = ERR_PTR(-ENOENT);
2247 struct inode *inode;
2249 inode = proc_pid_make_inode(dir->i_sb, task);
2253 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2254 inode->i_op = &proc_tgid_base_inode_operations;
2255 inode->i_fop = &proc_tgid_base_operations;
2256 inode->i_flags|=S_IMMUTABLE;
2258 #ifdef CONFIG_SECURITY
2259 inode->i_nlink += 1;
2262 dentry->d_op = &pid_dentry_operations;
2264 d_add(dentry, inode);
2265 /* Close the race of the process dying before we return the dentry */
2266 if (pid_revalidate(dentry, NULL))
2272 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2274 struct dentry *result = ERR_PTR(-ENOENT);
2275 struct task_struct *task;
2278 result = proc_base_lookup(dir, dentry);
2279 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2282 tgid = name_to_int(dentry);
2287 task = find_task_by_pid(tgid);
2289 get_task_struct(task);
2294 result = proc_pid_instantiate(dir, dentry, task, NULL);
2295 put_task_struct(task);
2301 * Find the first task with tgid >= tgid
2304 static struct task_struct *next_tgid(unsigned int tgid)
2306 struct task_struct *task;
2312 pid = find_ge_pid(tgid);
2315 task = pid_task(pid, PIDTYPE_PID);
2316 /* What we to know is if the pid we have find is the
2317 * pid of a thread_group_leader. Testing for task
2318 * being a thread_group_leader is the obvious thing
2319 * todo but there is a window when it fails, due to
2320 * the pid transfer logic in de_thread.
2322 * So we perform the straight forward test of seeing
2323 * if the pid we have found is the pid of a thread
2324 * group leader, and don't worry if the task we have
2325 * found doesn't happen to be a thread group leader.
2326 * As we don't care in the case of readdir.
2328 if (!task || !has_group_leader_pid(task))
2330 get_task_struct(task);
2336 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2338 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2339 struct task_struct *task, int tgid)
2341 char name[PROC_NUMBUF];
2342 int len = snprintf(name, sizeof(name), "%d", tgid);
2343 return proc_fill_cache(filp, dirent, filldir, name, len,
2344 proc_pid_instantiate, task, NULL);
2347 /* for the /proc/ directory itself, after non-process stuff has been done */
2348 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2350 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2351 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2352 struct task_struct *task;
2358 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2359 const struct pid_entry *p = &proc_base_stuff[nr];
2360 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2364 tgid = filp->f_pos - TGID_OFFSET;
2365 for (task = next_tgid(tgid);
2367 put_task_struct(task), task = next_tgid(tgid + 1)) {
2369 filp->f_pos = tgid + TGID_OFFSET;
2370 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2371 put_task_struct(task);
2375 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2377 put_task_struct(reaper);
2385 static const struct pid_entry tid_base_stuff[] = {
2386 DIR("fd", S_IRUSR|S_IXUSR, fd),
2387 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2388 REG("environ", S_IRUSR, environ),
2389 INF("auxv", S_IRUSR, pid_auxv),
2390 INF("status", S_IRUGO, pid_status),
2391 #ifdef CONFIG_SCHED_DEBUG
2392 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2394 INF("cmdline", S_IRUGO, pid_cmdline),
2395 INF("stat", S_IRUGO, tid_stat),
2396 INF("statm", S_IRUGO, pid_statm),
2397 REG("maps", S_IRUGO, maps),
2399 REG("numa_maps", S_IRUGO, numa_maps),
2401 REG("mem", S_IRUSR|S_IWUSR, mem),
2405 REG("mounts", S_IRUGO, mounts),
2407 REG("clear_refs", S_IWUSR, clear_refs),
2408 REG("smaps", S_IRUGO, smaps),
2410 #ifdef CONFIG_SECURITY
2411 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2413 #ifdef CONFIG_KALLSYMS
2414 INF("wchan", S_IRUGO, pid_wchan),
2416 #ifdef CONFIG_SCHEDSTATS
2417 INF("schedstat", S_IRUGO, pid_schedstat),
2419 #ifdef CONFIG_CPUSETS
2420 REG("cpuset", S_IRUGO, cpuset),
2422 INF("oom_score", S_IRUGO, oom_score),
2423 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2424 #ifdef CONFIG_AUDITSYSCALL
2425 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2427 #ifdef CONFIG_FAULT_INJECTION
2428 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2432 static int proc_tid_base_readdir(struct file * filp,
2433 void * dirent, filldir_t filldir)
2435 return proc_pident_readdir(filp,dirent,filldir,
2436 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2439 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2440 return proc_pident_lookup(dir, dentry,
2441 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2444 static const struct file_operations proc_tid_base_operations = {
2445 .read = generic_read_dir,
2446 .readdir = proc_tid_base_readdir,
2449 static const struct inode_operations proc_tid_base_inode_operations = {
2450 .lookup = proc_tid_base_lookup,
2451 .getattr = pid_getattr,
2452 .setattr = proc_setattr,
2455 static struct dentry *proc_task_instantiate(struct inode *dir,
2456 struct dentry *dentry, struct task_struct *task, const void *ptr)
2458 struct dentry *error = ERR_PTR(-ENOENT);
2459 struct inode *inode;
2460 inode = proc_pid_make_inode(dir->i_sb, task);
2464 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2465 inode->i_op = &proc_tid_base_inode_operations;
2466 inode->i_fop = &proc_tid_base_operations;
2467 inode->i_flags|=S_IMMUTABLE;
2469 #ifdef CONFIG_SECURITY
2470 inode->i_nlink += 1;
2473 dentry->d_op = &pid_dentry_operations;
2475 d_add(dentry, inode);
2476 /* Close the race of the process dying before we return the dentry */
2477 if (pid_revalidate(dentry, NULL))
2483 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2485 struct dentry *result = ERR_PTR(-ENOENT);
2486 struct task_struct *task;
2487 struct task_struct *leader = get_proc_task(dir);
2493 tid = name_to_int(dentry);
2498 task = find_task_by_pid(tid);
2500 get_task_struct(task);
2504 if (leader->tgid != task->tgid)
2507 result = proc_task_instantiate(dir, dentry, task, NULL);
2509 put_task_struct(task);
2511 put_task_struct(leader);
2517 * Find the first tid of a thread group to return to user space.
2519 * Usually this is just the thread group leader, but if the users
2520 * buffer was too small or there was a seek into the middle of the
2521 * directory we have more work todo.
2523 * In the case of a short read we start with find_task_by_pid.
2525 * In the case of a seek we start with the leader and walk nr
2528 static struct task_struct *first_tid(struct task_struct *leader,
2531 struct task_struct *pos;
2534 /* Attempt to start with the pid of a thread */
2535 if (tid && (nr > 0)) {
2536 pos = find_task_by_pid(tid);
2537 if (pos && (pos->group_leader == leader))
2541 /* If nr exceeds the number of threads there is nothing todo */
2543 if (nr && nr >= get_nr_threads(leader))
2546 /* If we haven't found our starting place yet start
2547 * with the leader and walk nr threads forward.
2549 for (pos = leader; nr > 0; --nr) {
2550 pos = next_thread(pos);
2551 if (pos == leader) {
2557 get_task_struct(pos);
2564 * Find the next thread in the thread list.
2565 * Return NULL if there is an error or no next thread.
2567 * The reference to the input task_struct is released.
2569 static struct task_struct *next_tid(struct task_struct *start)
2571 struct task_struct *pos = NULL;
2573 if (pid_alive(start)) {
2574 pos = next_thread(start);
2575 if (thread_group_leader(pos))
2578 get_task_struct(pos);
2581 put_task_struct(start);
2585 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2586 struct task_struct *task, int tid)
2588 char name[PROC_NUMBUF];
2589 int len = snprintf(name, sizeof(name), "%d", tid);
2590 return proc_fill_cache(filp, dirent, filldir, name, len,
2591 proc_task_instantiate, task, NULL);
2594 /* for the /proc/TGID/task/ directories */
2595 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2597 struct dentry *dentry = filp->f_path.dentry;
2598 struct inode *inode = dentry->d_inode;
2599 struct task_struct *leader = NULL;
2600 struct task_struct *task;
2601 int retval = -ENOENT;
2604 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2606 task = get_proc_task(inode);
2610 if (pid_alive(task)) {
2611 leader = task->group_leader;
2612 get_task_struct(leader);
2615 put_task_struct(task);
2623 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2628 ino = parent_ino(dentry);
2629 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2635 /* f_version caches the tgid value that the last readdir call couldn't
2636 * return. lseek aka telldir automagically resets f_version to 0.
2638 tid = (int)filp->f_version;
2639 filp->f_version = 0;
2640 for (task = first_tid(leader, tid, pos - 2);
2642 task = next_tid(task), pos++) {
2644 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2645 /* returning this tgid failed, save it as the first
2646 * pid for the next readir call */
2647 filp->f_version = (u64)tid;
2648 put_task_struct(task);
2654 put_task_struct(leader);
2659 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2661 struct inode *inode = dentry->d_inode;
2662 struct task_struct *p = get_proc_task(inode);
2663 generic_fillattr(inode, stat);
2667 stat->nlink += get_nr_threads(p);
2675 static const struct inode_operations proc_task_inode_operations = {
2676 .lookup = proc_task_lookup,
2677 .getattr = proc_task_getattr,
2678 .setattr = proc_setattr,
2681 static const struct file_operations proc_task_operations = {
2682 .read = generic_read_dir,
2683 .readdir = proc_task_readdir,