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/resource.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/cgroup.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include <linux/elf.h>
78 #include <linux/pid_namespace.h>
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
92 /* Worst case buffer size needed for holding an integer. */
93 #define PROC_NUMBUF 13
99 const struct inode_operations *iop;
100 const struct file_operations *fop;
104 #define NOD(NAME, MODE, IOP, FOP, OP) { \
106 .len = sizeof(NAME) - 1, \
113 #define DIR(NAME, MODE, OTYPE) \
114 NOD(NAME, (S_IFDIR|(MODE)), \
115 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
117 #define LNK(NAME, OTYPE) \
118 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
119 &proc_pid_link_inode_operations, NULL, \
120 { .proc_get_link = &proc_##OTYPE##_link } )
121 #define REG(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), NULL, \
123 &proc_##OTYPE##_operations, {})
124 #define INF(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_info_file_operations, \
127 { .proc_read = &proc_##OTYPE } )
130 EXPORT_SYMBOL(maps_protect);
132 static struct fs_struct *get_fs_struct(struct task_struct *task)
134 struct fs_struct *fs;
138 atomic_inc(&fs->count);
143 static int get_nr_threads(struct task_struct *tsk)
145 /* Must be called with the rcu_read_lock held */
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
156 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
160 int result = -ENOENT;
163 fs = get_fs_struct(task);
164 put_task_struct(task);
167 read_lock(&fs->lock);
168 *mnt = mntget(fs->pwdmnt);
169 *dentry = dget(fs->pwd);
170 read_unlock(&fs->lock);
177 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
181 int result = -ENOENT;
184 fs = get_fs_struct(task);
185 put_task_struct(task);
188 read_lock(&fs->lock);
189 *mnt = mntget(fs->rootmnt);
190 *dentry = dget(fs->root);
191 read_unlock(&fs->lock);
198 #define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
202 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
203 security_ptrace(current,task) == 0))
205 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
209 struct mm_struct *mm = get_task_mm(task);
213 goto out_mm; /* Shh! No looking before we're done */
215 len = mm->arg_end - mm->arg_start;
220 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
222 // If the nul at the end of args has been overwritten, then
223 // assume application is using setproctitle(3).
224 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
225 len = strnlen(buffer, res);
229 len = mm->env_end - mm->env_start;
230 if (len > PAGE_SIZE - res)
231 len = PAGE_SIZE - res;
232 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
233 res = strnlen(buffer, res);
242 static int proc_pid_auxv(struct task_struct *task, char *buffer)
245 struct mm_struct *mm = get_task_mm(task);
247 unsigned int nwords = 0;
250 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
251 res = nwords * sizeof(mm->saved_auxv[0]);
254 memcpy(buffer, mm->saved_auxv, res);
261 #ifdef CONFIG_KALLSYMS
263 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
264 * Returns the resolved symbol. If that fails, simply return the address.
266 static int proc_pid_wchan(struct task_struct *task, char *buffer)
269 char symname[KSYM_NAME_LEN];
271 wchan = get_wchan(task);
273 if (lookup_symbol_name(wchan, symname) < 0)
274 return sprintf(buffer, "%lu", wchan);
276 return sprintf(buffer, "%s", symname);
278 #endif /* CONFIG_KALLSYMS */
280 #ifdef CONFIG_SCHEDSTATS
282 * Provides /proc/PID/schedstat
284 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
286 return sprintf(buffer, "%llu %llu %lu\n",
287 task->sched_info.cpu_time,
288 task->sched_info.run_delay,
289 task->sched_info.pcount);
293 /* The badness from the OOM killer */
294 unsigned long badness(struct task_struct *p, unsigned long uptime);
295 static int proc_oom_score(struct task_struct *task, char *buffer)
297 unsigned long points;
298 struct timespec uptime;
300 do_posix_clock_monotonic_gettime(&uptime);
301 read_lock(&tasklist_lock);
302 points = badness(task, uptime.tv_sec);
303 read_unlock(&tasklist_lock);
304 return sprintf(buffer, "%lu\n", points);
312 static const struct limit_names lnames[RLIM_NLIMITS] = {
313 [RLIMIT_CPU] = {"Max cpu time", "ms"},
314 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
315 [RLIMIT_DATA] = {"Max data size", "bytes"},
316 [RLIMIT_STACK] = {"Max stack size", "bytes"},
317 [RLIMIT_CORE] = {"Max core file size", "bytes"},
318 [RLIMIT_RSS] = {"Max resident set", "bytes"},
319 [RLIMIT_NPROC] = {"Max processes", "processes"},
320 [RLIMIT_NOFILE] = {"Max open files", "files"},
321 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
322 [RLIMIT_AS] = {"Max address space", "bytes"},
323 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
324 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
325 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
326 [RLIMIT_NICE] = {"Max nice priority", NULL},
327 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
330 /* Display limits for a process */
331 static int proc_pid_limits(struct task_struct *task, char *buffer)
336 char *bufptr = buffer;
338 struct rlimit rlim[RLIM_NLIMITS];
341 if (!lock_task_sighand(task,&flags)) {
345 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
346 unlock_task_sighand(task, &flags);
350 * print the file header
352 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
353 "Limit", "Soft Limit", "Hard Limit", "Units");
355 for (i = 0; i < RLIM_NLIMITS; i++) {
356 if (rlim[i].rlim_cur == RLIM_INFINITY)
357 count += sprintf(&bufptr[count], "%-25s %-20s ",
358 lnames[i].name, "unlimited");
360 count += sprintf(&bufptr[count], "%-25s %-20lu ",
361 lnames[i].name, rlim[i].rlim_cur);
363 if (rlim[i].rlim_max == RLIM_INFINITY)
364 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
366 count += sprintf(&bufptr[count], "%-20lu ",
370 count += sprintf(&bufptr[count], "%-10s\n",
373 count += sprintf(&bufptr[count], "\n");
379 /************************************************************************/
380 /* Here the fs part begins */
381 /************************************************************************/
383 /* permission checks */
384 static int proc_fd_access_allowed(struct inode *inode)
386 struct task_struct *task;
388 /* Allow access to a task's file descriptors if it is us or we
389 * may use ptrace attach to the process and find out that
392 task = get_proc_task(inode);
394 allowed = ptrace_may_attach(task);
395 put_task_struct(task);
400 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
403 struct inode *inode = dentry->d_inode;
405 if (attr->ia_valid & ATTR_MODE)
408 error = inode_change_ok(inode, attr);
410 error = inode_setattr(inode, attr);
414 static const struct inode_operations proc_def_inode_operations = {
415 .setattr = proc_setattr,
418 extern struct seq_operations mounts_op;
424 static int mounts_open(struct inode *inode, struct file *file)
426 struct task_struct *task = get_proc_task(inode);
428 struct mnt_namespace *ns = NULL;
429 struct proc_mounts *p;
434 nsp = task_nsproxy(task);
442 put_task_struct(task);
447 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
449 file->private_data = &p->m;
450 ret = seq_open(file, &mounts_op);
453 p->event = ns->event;
463 static int mounts_release(struct inode *inode, struct file *file)
465 struct seq_file *m = file->private_data;
466 struct mnt_namespace *ns = m->private;
468 return seq_release(inode, file);
471 static unsigned mounts_poll(struct file *file, poll_table *wait)
473 struct proc_mounts *p = file->private_data;
474 struct mnt_namespace *ns = p->m.private;
477 poll_wait(file, &ns->poll, wait);
479 spin_lock(&vfsmount_lock);
480 if (p->event != ns->event) {
481 p->event = ns->event;
484 spin_unlock(&vfsmount_lock);
489 static const struct file_operations proc_mounts_operations = {
493 .release = mounts_release,
497 extern struct seq_operations mountstats_op;
498 static int mountstats_open(struct inode *inode, struct file *file)
500 int ret = seq_open(file, &mountstats_op);
503 struct seq_file *m = file->private_data;
505 struct mnt_namespace *mnt_ns = NULL;
506 struct task_struct *task = get_proc_task(inode);
510 nsp = task_nsproxy(task);
512 mnt_ns = nsp->mnt_ns;
518 put_task_struct(task);
524 seq_release(inode, file);
531 static const struct file_operations proc_mountstats_operations = {
532 .open = mountstats_open,
535 .release = mounts_release,
538 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
540 static ssize_t proc_info_read(struct file * file, char __user * buf,
541 size_t count, loff_t *ppos)
543 struct inode * inode = file->f_path.dentry->d_inode;
546 struct task_struct *task = get_proc_task(inode);
552 if (count > PROC_BLOCK_SIZE)
553 count = PROC_BLOCK_SIZE;
556 if (!(page = __get_free_page(GFP_TEMPORARY)))
559 length = PROC_I(inode)->op.proc_read(task, (char*)page);
562 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
565 put_task_struct(task);
570 static const struct file_operations proc_info_file_operations = {
571 .read = proc_info_read,
574 static int mem_open(struct inode* inode, struct file* file)
576 file->private_data = (void*)((long)current->self_exec_id);
580 static ssize_t mem_read(struct file * file, char __user * buf,
581 size_t count, loff_t *ppos)
583 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
585 unsigned long src = *ppos;
587 struct mm_struct *mm;
592 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
596 page = (char *)__get_free_page(GFP_TEMPORARY);
602 mm = get_task_mm(task);
608 if (file->private_data != (void*)((long)current->self_exec_id))
614 int this_len, retval;
616 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
617 retval = access_process_vm(task, src, page, this_len, 0);
618 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
624 if (copy_to_user(buf, page, retval)) {
639 free_page((unsigned long) page);
641 put_task_struct(task);
646 #define mem_write NULL
649 /* This is a security hazard */
650 static ssize_t mem_write(struct file * file, const char __user *buf,
651 size_t count, loff_t *ppos)
655 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
656 unsigned long dst = *ppos;
662 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
666 page = (char *)__get_free_page(GFP_TEMPORARY);
672 int this_len, retval;
674 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
675 if (copy_from_user(page, buf, this_len)) {
679 retval = access_process_vm(task, dst, page, this_len, 1);
691 free_page((unsigned long) page);
693 put_task_struct(task);
699 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
703 file->f_pos = offset;
706 file->f_pos += offset;
711 force_successful_syscall_return();
715 static const struct file_operations proc_mem_operations = {
722 static ssize_t environ_read(struct file *file, char __user *buf,
723 size_t count, loff_t *ppos)
725 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
727 unsigned long src = *ppos;
729 struct mm_struct *mm;
734 if (!ptrace_may_attach(task))
738 page = (char *)__get_free_page(GFP_TEMPORARY);
744 mm = get_task_mm(task);
749 int this_len, retval, max_len;
751 this_len = mm->env_end - (mm->env_start + src);
756 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
757 this_len = (this_len > max_len) ? max_len : this_len;
759 retval = access_process_vm(task, (mm->env_start + src),
767 if (copy_to_user(buf, page, retval)) {
781 free_page((unsigned long) page);
783 put_task_struct(task);
788 static const struct file_operations proc_environ_operations = {
789 .read = environ_read,
792 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
793 size_t count, loff_t *ppos)
795 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
796 char buffer[PROC_NUMBUF];
802 oom_adjust = task->oomkilladj;
803 put_task_struct(task);
805 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
807 return simple_read_from_buffer(buf, count, ppos, buffer, len);
810 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
811 size_t count, loff_t *ppos)
813 struct task_struct *task;
814 char buffer[PROC_NUMBUF], *end;
817 memset(buffer, 0, sizeof(buffer));
818 if (count > sizeof(buffer) - 1)
819 count = sizeof(buffer) - 1;
820 if (copy_from_user(buffer, buf, count))
822 oom_adjust = simple_strtol(buffer, &end, 0);
823 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
824 oom_adjust != OOM_DISABLE)
828 task = get_proc_task(file->f_path.dentry->d_inode);
831 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
832 put_task_struct(task);
835 task->oomkilladj = oom_adjust;
836 put_task_struct(task);
837 if (end - buffer == 0)
842 static const struct file_operations proc_oom_adjust_operations = {
843 .read = oom_adjust_read,
844 .write = oom_adjust_write,
848 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
849 size_t count, loff_t *ppos)
851 struct task_struct *task;
852 char buffer[PROC_NUMBUF], *end;
853 struct mm_struct *mm;
855 memset(buffer, 0, sizeof(buffer));
856 if (count > sizeof(buffer) - 1)
857 count = sizeof(buffer) - 1;
858 if (copy_from_user(buffer, buf, count))
860 if (!simple_strtol(buffer, &end, 0))
864 task = get_proc_task(file->f_path.dentry->d_inode);
867 mm = get_task_mm(task);
872 put_task_struct(task);
873 if (end - buffer == 0)
878 static struct file_operations proc_clear_refs_operations = {
879 .write = clear_refs_write,
883 #ifdef CONFIG_AUDITSYSCALL
885 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
886 size_t count, loff_t *ppos)
888 struct inode * inode = file->f_path.dentry->d_inode;
889 struct task_struct *task = get_proc_task(inode);
891 char tmpbuf[TMPBUFLEN];
895 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
896 audit_get_loginuid(task->audit_context));
897 put_task_struct(task);
898 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
901 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
902 size_t count, loff_t *ppos)
904 struct inode * inode = file->f_path.dentry->d_inode;
909 if (!capable(CAP_AUDIT_CONTROL))
912 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
915 if (count >= PAGE_SIZE)
916 count = PAGE_SIZE - 1;
919 /* No partial writes. */
922 page = (char*)__get_free_page(GFP_TEMPORARY);
926 if (copy_from_user(page, buf, count))
930 loginuid = simple_strtoul(page, &tmp, 10);
936 length = audit_set_loginuid(current, loginuid);
937 if (likely(length == 0))
941 free_page((unsigned long) page);
945 static const struct file_operations proc_loginuid_operations = {
946 .read = proc_loginuid_read,
947 .write = proc_loginuid_write,
951 #ifdef CONFIG_FAULT_INJECTION
952 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
953 size_t count, loff_t *ppos)
955 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
956 char buffer[PROC_NUMBUF];
962 make_it_fail = task->make_it_fail;
963 put_task_struct(task);
965 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
967 return simple_read_from_buffer(buf, count, ppos, buffer, len);
970 static ssize_t proc_fault_inject_write(struct file * file,
971 const char __user * buf, size_t count, loff_t *ppos)
973 struct task_struct *task;
974 char buffer[PROC_NUMBUF], *end;
977 if (!capable(CAP_SYS_RESOURCE))
979 memset(buffer, 0, sizeof(buffer));
980 if (count > sizeof(buffer) - 1)
981 count = sizeof(buffer) - 1;
982 if (copy_from_user(buffer, buf, count))
984 make_it_fail = simple_strtol(buffer, &end, 0);
987 task = get_proc_task(file->f_dentry->d_inode);
990 task->make_it_fail = make_it_fail;
991 put_task_struct(task);
992 if (end - buffer == 0)
997 static const struct file_operations proc_fault_inject_operations = {
998 .read = proc_fault_inject_read,
999 .write = proc_fault_inject_write,
1003 #ifdef CONFIG_SCHED_DEBUG
1005 * Print out various scheduling related per-task fields:
1007 static int sched_show(struct seq_file *m, void *v)
1009 struct inode *inode = m->private;
1010 struct task_struct *p;
1014 p = get_proc_task(inode);
1017 proc_sched_show_task(p, m);
1025 sched_write(struct file *file, const char __user *buf,
1026 size_t count, loff_t *offset)
1028 struct inode *inode = file->f_path.dentry->d_inode;
1029 struct task_struct *p;
1033 p = get_proc_task(inode);
1036 proc_sched_set_task(p);
1043 static int sched_open(struct inode *inode, struct file *filp)
1047 ret = single_open(filp, sched_show, NULL);
1049 struct seq_file *m = filp->private_data;
1056 static const struct file_operations proc_pid_sched_operations = {
1059 .write = sched_write,
1060 .llseek = seq_lseek,
1061 .release = single_release,
1066 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1068 struct inode *inode = dentry->d_inode;
1069 int error = -EACCES;
1071 /* We don't need a base pointer in the /proc filesystem */
1074 /* Are we allowed to snoop on the tasks file descriptors? */
1075 if (!proc_fd_access_allowed(inode))
1078 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1079 nd->last_type = LAST_BIND;
1081 return ERR_PTR(error);
1084 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1085 char __user *buffer, int buflen)
1087 struct inode * inode;
1088 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1095 inode = dentry->d_inode;
1096 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1097 len = PTR_ERR(path);
1100 len = tmp + PAGE_SIZE - 1 - path;
1104 if (copy_to_user(buffer, path, len))
1107 free_page((unsigned long)tmp);
1111 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1113 int error = -EACCES;
1114 struct inode *inode = dentry->d_inode;
1116 struct vfsmount *mnt = NULL;
1118 /* Are we allowed to snoop on the tasks file descriptors? */
1119 if (!proc_fd_access_allowed(inode))
1122 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1126 error = do_proc_readlink(de, mnt, buffer, buflen);
1133 static const struct inode_operations proc_pid_link_inode_operations = {
1134 .readlink = proc_pid_readlink,
1135 .follow_link = proc_pid_follow_link,
1136 .setattr = proc_setattr,
1140 /* building an inode */
1142 static int task_dumpable(struct task_struct *task)
1145 struct mm_struct *mm;
1150 dumpable = get_dumpable(mm);
1158 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1160 struct inode * inode;
1161 struct proc_inode *ei;
1163 /* We need a new inode */
1165 inode = new_inode(sb);
1171 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1172 inode->i_op = &proc_def_inode_operations;
1175 * grab the reference to task.
1177 ei->pid = get_task_pid(task, PIDTYPE_PID);
1183 if (task_dumpable(task)) {
1184 inode->i_uid = task->euid;
1185 inode->i_gid = task->egid;
1187 security_task_to_inode(task, inode);
1197 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1199 struct inode *inode = dentry->d_inode;
1200 struct task_struct *task;
1201 generic_fillattr(inode, stat);
1206 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1208 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1209 task_dumpable(task)) {
1210 stat->uid = task->euid;
1211 stat->gid = task->egid;
1221 * Exceptional case: normally we are not allowed to unhash a busy
1222 * directory. In this case, however, we can do it - no aliasing problems
1223 * due to the way we treat inodes.
1225 * Rewrite the inode's ownerships here because the owning task may have
1226 * performed a setuid(), etc.
1228 * Before the /proc/pid/status file was created the only way to read
1229 * the effective uid of a /process was to stat /proc/pid. Reading
1230 * /proc/pid/status is slow enough that procps and other packages
1231 * kept stating /proc/pid. To keep the rules in /proc simple I have
1232 * made this apply to all per process world readable and executable
1235 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1237 struct inode *inode = dentry->d_inode;
1238 struct task_struct *task = get_proc_task(inode);
1240 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1241 task_dumpable(task)) {
1242 inode->i_uid = task->euid;
1243 inode->i_gid = task->egid;
1248 inode->i_mode &= ~(S_ISUID | S_ISGID);
1249 security_task_to_inode(task, inode);
1250 put_task_struct(task);
1257 static int pid_delete_dentry(struct dentry * dentry)
1259 /* Is the task we represent dead?
1260 * If so, then don't put the dentry on the lru list,
1261 * kill it immediately.
1263 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1266 static struct dentry_operations pid_dentry_operations =
1268 .d_revalidate = pid_revalidate,
1269 .d_delete = pid_delete_dentry,
1274 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1275 struct task_struct *, const void *);
1278 * Fill a directory entry.
1280 * If possible create the dcache entry and derive our inode number and
1281 * file type from dcache entry.
1283 * Since all of the proc inode numbers are dynamically generated, the inode
1284 * numbers do not exist until the inode is cache. This means creating the
1285 * the dcache entry in readdir is necessary to keep the inode numbers
1286 * reported by readdir in sync with the inode numbers reported
1289 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1290 char *name, int len,
1291 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1293 struct dentry *child, *dir = filp->f_path.dentry;
1294 struct inode *inode;
1297 unsigned type = DT_UNKNOWN;
1301 qname.hash = full_name_hash(name, len);
1303 child = d_lookup(dir, &qname);
1306 new = d_alloc(dir, &qname);
1308 child = instantiate(dir->d_inode, new, task, ptr);
1315 if (!child || IS_ERR(child) || !child->d_inode)
1316 goto end_instantiate;
1317 inode = child->d_inode;
1320 type = inode->i_mode >> 12;
1325 ino = find_inode_number(dir, &qname);
1328 return filldir(dirent, name, len, filp->f_pos, ino, type);
1331 static unsigned name_to_int(struct dentry *dentry)
1333 const char *name = dentry->d_name.name;
1334 int len = dentry->d_name.len;
1337 if (len > 1 && *name == '0')
1340 unsigned c = *name++ - '0';
1343 if (n >= (~0U-9)/10)
1353 #define PROC_FDINFO_MAX 64
1355 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1356 struct vfsmount **mnt, char *info)
1358 struct task_struct *task = get_proc_task(inode);
1359 struct files_struct *files = NULL;
1361 int fd = proc_fd(inode);
1364 files = get_files_struct(task);
1365 put_task_struct(task);
1369 * We are not taking a ref to the file structure, so we must
1372 spin_lock(&files->file_lock);
1373 file = fcheck_files(files, fd);
1376 *mnt = mntget(file->f_path.mnt);
1378 *dentry = dget(file->f_path.dentry);
1380 snprintf(info, PROC_FDINFO_MAX,
1383 (long long) file->f_pos,
1385 spin_unlock(&files->file_lock);
1386 put_files_struct(files);
1389 spin_unlock(&files->file_lock);
1390 put_files_struct(files);
1395 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1396 struct vfsmount **mnt)
1398 return proc_fd_info(inode, dentry, mnt, NULL);
1401 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1403 struct inode *inode = dentry->d_inode;
1404 struct task_struct *task = get_proc_task(inode);
1405 int fd = proc_fd(inode);
1406 struct files_struct *files;
1409 files = get_files_struct(task);
1412 if (fcheck_files(files, fd)) {
1414 put_files_struct(files);
1415 if (task_dumpable(task)) {
1416 inode->i_uid = task->euid;
1417 inode->i_gid = task->egid;
1422 inode->i_mode &= ~(S_ISUID | S_ISGID);
1423 security_task_to_inode(task, inode);
1424 put_task_struct(task);
1428 put_files_struct(files);
1430 put_task_struct(task);
1436 static struct dentry_operations tid_fd_dentry_operations =
1438 .d_revalidate = tid_fd_revalidate,
1439 .d_delete = pid_delete_dentry,
1442 static struct dentry *proc_fd_instantiate(struct inode *dir,
1443 struct dentry *dentry, struct task_struct *task, const void *ptr)
1445 unsigned fd = *(const unsigned *)ptr;
1447 struct files_struct *files;
1448 struct inode *inode;
1449 struct proc_inode *ei;
1450 struct dentry *error = ERR_PTR(-ENOENT);
1452 inode = proc_pid_make_inode(dir->i_sb, task);
1457 files = get_files_struct(task);
1460 inode->i_mode = S_IFLNK;
1463 * We are not taking a ref to the file structure, so we must
1466 spin_lock(&files->file_lock);
1467 file = fcheck_files(files, fd);
1470 if (file->f_mode & 1)
1471 inode->i_mode |= S_IRUSR | S_IXUSR;
1472 if (file->f_mode & 2)
1473 inode->i_mode |= S_IWUSR | S_IXUSR;
1474 spin_unlock(&files->file_lock);
1475 put_files_struct(files);
1477 inode->i_op = &proc_pid_link_inode_operations;
1479 ei->op.proc_get_link = proc_fd_link;
1480 dentry->d_op = &tid_fd_dentry_operations;
1481 d_add(dentry, inode);
1482 /* Close the race of the process dying before we return the dentry */
1483 if (tid_fd_revalidate(dentry, NULL))
1489 spin_unlock(&files->file_lock);
1490 put_files_struct(files);
1496 static struct dentry *proc_lookupfd_common(struct inode *dir,
1497 struct dentry *dentry,
1498 instantiate_t instantiate)
1500 struct task_struct *task = get_proc_task(dir);
1501 unsigned fd = name_to_int(dentry);
1502 struct dentry *result = ERR_PTR(-ENOENT);
1509 result = instantiate(dir, dentry, task, &fd);
1511 put_task_struct(task);
1516 static int proc_readfd_common(struct file * filp, void * dirent,
1517 filldir_t filldir, instantiate_t instantiate)
1519 struct dentry *dentry = filp->f_path.dentry;
1520 struct inode *inode = dentry->d_inode;
1521 struct task_struct *p = get_proc_task(inode);
1522 unsigned int fd, tid, ino;
1524 struct files_struct * files;
1525 struct fdtable *fdt;
1536 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1540 ino = parent_ino(dentry);
1541 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1545 files = get_files_struct(p);
1549 fdt = files_fdtable(files);
1550 for (fd = filp->f_pos-2;
1552 fd++, filp->f_pos++) {
1553 char name[PROC_NUMBUF];
1556 if (!fcheck_files(files, fd))
1560 len = snprintf(name, sizeof(name), "%d", fd);
1561 if (proc_fill_cache(filp, dirent, filldir,
1562 name, len, instantiate,
1570 put_files_struct(files);
1578 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1579 struct nameidata *nd)
1581 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1584 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1586 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1589 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1590 size_t len, loff_t *ppos)
1592 char tmp[PROC_FDINFO_MAX];
1593 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1595 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1599 static const struct file_operations proc_fdinfo_file_operations = {
1600 .open = nonseekable_open,
1601 .read = proc_fdinfo_read,
1604 static const struct file_operations proc_fd_operations = {
1605 .read = generic_read_dir,
1606 .readdir = proc_readfd,
1610 * /proc/pid/fd needs a special permission handler so that a process can still
1611 * access /proc/self/fd after it has executed a setuid().
1613 static int proc_fd_permission(struct inode *inode, int mask,
1614 struct nameidata *nd)
1618 rv = generic_permission(inode, mask, NULL);
1621 if (task_pid(current) == proc_pid(inode))
1627 * proc directories can do almost nothing..
1629 static const struct inode_operations proc_fd_inode_operations = {
1630 .lookup = proc_lookupfd,
1631 .permission = proc_fd_permission,
1632 .setattr = proc_setattr,
1635 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1636 struct dentry *dentry, struct task_struct *task, const void *ptr)
1638 unsigned fd = *(unsigned *)ptr;
1639 struct inode *inode;
1640 struct proc_inode *ei;
1641 struct dentry *error = ERR_PTR(-ENOENT);
1643 inode = proc_pid_make_inode(dir->i_sb, task);
1648 inode->i_mode = S_IFREG | S_IRUSR;
1649 inode->i_fop = &proc_fdinfo_file_operations;
1650 dentry->d_op = &tid_fd_dentry_operations;
1651 d_add(dentry, inode);
1652 /* Close the race of the process dying before we return the dentry */
1653 if (tid_fd_revalidate(dentry, NULL))
1660 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1661 struct dentry *dentry,
1662 struct nameidata *nd)
1664 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1667 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1669 return proc_readfd_common(filp, dirent, filldir,
1670 proc_fdinfo_instantiate);
1673 static const struct file_operations proc_fdinfo_operations = {
1674 .read = generic_read_dir,
1675 .readdir = proc_readfdinfo,
1679 * proc directories can do almost nothing..
1681 static const struct inode_operations proc_fdinfo_inode_operations = {
1682 .lookup = proc_lookupfdinfo,
1683 .setattr = proc_setattr,
1687 static struct dentry *proc_pident_instantiate(struct inode *dir,
1688 struct dentry *dentry, struct task_struct *task, const void *ptr)
1690 const struct pid_entry *p = ptr;
1691 struct inode *inode;
1692 struct proc_inode *ei;
1693 struct dentry *error = ERR_PTR(-EINVAL);
1695 inode = proc_pid_make_inode(dir->i_sb, task);
1700 inode->i_mode = p->mode;
1701 if (S_ISDIR(inode->i_mode))
1702 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1704 inode->i_op = p->iop;
1706 inode->i_fop = p->fop;
1708 dentry->d_op = &pid_dentry_operations;
1709 d_add(dentry, inode);
1710 /* Close the race of the process dying before we return the dentry */
1711 if (pid_revalidate(dentry, NULL))
1717 static struct dentry *proc_pident_lookup(struct inode *dir,
1718 struct dentry *dentry,
1719 const struct pid_entry *ents,
1722 struct inode *inode;
1723 struct dentry *error;
1724 struct task_struct *task = get_proc_task(dir);
1725 const struct pid_entry *p, *last;
1727 error = ERR_PTR(-ENOENT);
1734 * Yes, it does not scale. And it should not. Don't add
1735 * new entries into /proc/<tgid>/ without very good reasons.
1737 last = &ents[nents - 1];
1738 for (p = ents; p <= last; p++) {
1739 if (p->len != dentry->d_name.len)
1741 if (!memcmp(dentry->d_name.name, p->name, p->len))
1747 error = proc_pident_instantiate(dir, dentry, task, p);
1749 put_task_struct(task);
1754 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1755 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1757 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1758 proc_pident_instantiate, task, p);
1761 static int proc_pident_readdir(struct file *filp,
1762 void *dirent, filldir_t filldir,
1763 const struct pid_entry *ents, unsigned int nents)
1767 struct dentry *dentry = filp->f_path.dentry;
1768 struct inode *inode = dentry->d_inode;
1769 struct task_struct *task = get_proc_task(inode);
1770 const struct pid_entry *p, *last;
1784 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1790 ino = parent_ino(dentry);
1791 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1803 last = &ents[nents - 1];
1805 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1814 put_task_struct(task);
1819 #ifdef CONFIG_SECURITY
1820 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1821 size_t count, loff_t *ppos)
1823 struct inode * inode = file->f_path.dentry->d_inode;
1826 struct task_struct *task = get_proc_task(inode);
1831 length = security_getprocattr(task,
1832 (char*)file->f_path.dentry->d_name.name,
1834 put_task_struct(task);
1836 length = simple_read_from_buffer(buf, count, ppos, p, length);
1841 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1842 size_t count, loff_t *ppos)
1844 struct inode * inode = file->f_path.dentry->d_inode;
1847 struct task_struct *task = get_proc_task(inode);
1852 if (count > PAGE_SIZE)
1855 /* No partial writes. */
1861 page = (char*)__get_free_page(GFP_TEMPORARY);
1866 if (copy_from_user(page, buf, count))
1869 length = security_setprocattr(task,
1870 (char*)file->f_path.dentry->d_name.name,
1871 (void*)page, count);
1873 free_page((unsigned long) page);
1875 put_task_struct(task);
1880 static const struct file_operations proc_pid_attr_operations = {
1881 .read = proc_pid_attr_read,
1882 .write = proc_pid_attr_write,
1885 static const struct pid_entry attr_dir_stuff[] = {
1886 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1887 REG("prev", S_IRUGO, pid_attr),
1888 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1889 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1890 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1891 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1894 static int proc_attr_dir_readdir(struct file * filp,
1895 void * dirent, filldir_t filldir)
1897 return proc_pident_readdir(filp,dirent,filldir,
1898 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1901 static const struct file_operations proc_attr_dir_operations = {
1902 .read = generic_read_dir,
1903 .readdir = proc_attr_dir_readdir,
1906 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1907 struct dentry *dentry, struct nameidata *nd)
1909 return proc_pident_lookup(dir, dentry,
1910 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1913 static const struct inode_operations proc_attr_dir_inode_operations = {
1914 .lookup = proc_attr_dir_lookup,
1915 .getattr = pid_getattr,
1916 .setattr = proc_setattr,
1921 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1922 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1923 size_t count, loff_t *ppos)
1925 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1926 struct mm_struct *mm;
1927 char buffer[PROC_NUMBUF];
1935 mm = get_task_mm(task);
1937 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1938 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1939 MMF_DUMP_FILTER_SHIFT));
1941 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1944 put_task_struct(task);
1949 static ssize_t proc_coredump_filter_write(struct file *file,
1950 const char __user *buf,
1954 struct task_struct *task;
1955 struct mm_struct *mm;
1956 char buffer[PROC_NUMBUF], *end;
1963 memset(buffer, 0, sizeof(buffer));
1964 if (count > sizeof(buffer) - 1)
1965 count = sizeof(buffer) - 1;
1966 if (copy_from_user(buffer, buf, count))
1970 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1973 if (end - buffer == 0)
1977 task = get_proc_task(file->f_dentry->d_inode);
1982 mm = get_task_mm(task);
1986 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1988 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1990 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1995 put_task_struct(task);
2000 static const struct file_operations proc_coredump_filter_operations = {
2001 .read = proc_coredump_filter_read,
2002 .write = proc_coredump_filter_write,
2009 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2012 char tmp[PROC_NUMBUF];
2013 sprintf(tmp, "%d", task_tgid_vnr(current));
2014 return vfs_readlink(dentry,buffer,buflen,tmp);
2017 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2019 char tmp[PROC_NUMBUF];
2020 sprintf(tmp, "%d", task_tgid_vnr(current));
2021 return ERR_PTR(vfs_follow_link(nd,tmp));
2024 static const struct inode_operations proc_self_inode_operations = {
2025 .readlink = proc_self_readlink,
2026 .follow_link = proc_self_follow_link,
2032 * These are the directory entries in the root directory of /proc
2033 * that properly belong to the /proc filesystem, as they describe
2034 * describe something that is process related.
2036 static const struct pid_entry proc_base_stuff[] = {
2037 NOD("self", S_IFLNK|S_IRWXUGO,
2038 &proc_self_inode_operations, NULL, {}),
2042 * Exceptional case: normally we are not allowed to unhash a busy
2043 * directory. In this case, however, we can do it - no aliasing problems
2044 * due to the way we treat inodes.
2046 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2048 struct inode *inode = dentry->d_inode;
2049 struct task_struct *task = get_proc_task(inode);
2051 put_task_struct(task);
2058 static struct dentry_operations proc_base_dentry_operations =
2060 .d_revalidate = proc_base_revalidate,
2061 .d_delete = pid_delete_dentry,
2064 static struct dentry *proc_base_instantiate(struct inode *dir,
2065 struct dentry *dentry, struct task_struct *task, const void *ptr)
2067 const struct pid_entry *p = ptr;
2068 struct inode *inode;
2069 struct proc_inode *ei;
2070 struct dentry *error = ERR_PTR(-EINVAL);
2072 /* Allocate the inode */
2073 error = ERR_PTR(-ENOMEM);
2074 inode = new_inode(dir->i_sb);
2078 /* Initialize the inode */
2080 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2083 * grab the reference to the task.
2085 ei->pid = get_task_pid(task, PIDTYPE_PID);
2091 inode->i_mode = p->mode;
2092 if (S_ISDIR(inode->i_mode))
2094 if (S_ISLNK(inode->i_mode))
2097 inode->i_op = p->iop;
2099 inode->i_fop = p->fop;
2101 dentry->d_op = &proc_base_dentry_operations;
2102 d_add(dentry, inode);
2111 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2113 struct dentry *error;
2114 struct task_struct *task = get_proc_task(dir);
2115 const struct pid_entry *p, *last;
2117 error = ERR_PTR(-ENOENT);
2122 /* Lookup the directory entry */
2123 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2124 for (p = proc_base_stuff; p <= last; p++) {
2125 if (p->len != dentry->d_name.len)
2127 if (!memcmp(dentry->d_name.name, p->name, p->len))
2133 error = proc_base_instantiate(dir, dentry, task, p);
2136 put_task_struct(task);
2141 static int proc_base_fill_cache(struct file *filp, void *dirent,
2142 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2144 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2145 proc_base_instantiate, task, p);
2148 #ifdef CONFIG_TASK_IO_ACCOUNTING
2149 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2151 return sprintf(buffer,
2152 #ifdef CONFIG_TASK_XACCT
2158 "read_bytes: %llu\n"
2159 "write_bytes: %llu\n"
2160 "cancelled_write_bytes: %llu\n",
2161 #ifdef CONFIG_TASK_XACCT
2162 (unsigned long long)task->rchar,
2163 (unsigned long long)task->wchar,
2164 (unsigned long long)task->syscr,
2165 (unsigned long long)task->syscw,
2167 (unsigned long long)task->ioac.read_bytes,
2168 (unsigned long long)task->ioac.write_bytes,
2169 (unsigned long long)task->ioac.cancelled_write_bytes);
2176 static const struct file_operations proc_task_operations;
2177 static const struct inode_operations proc_task_inode_operations;
2179 static const struct pid_entry tgid_base_stuff[] = {
2180 DIR("task", S_IRUGO|S_IXUGO, task),
2181 DIR("fd", S_IRUSR|S_IXUSR, fd),
2182 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2183 REG("environ", S_IRUSR, environ),
2184 INF("auxv", S_IRUSR, pid_auxv),
2185 INF("status", S_IRUGO, pid_status),
2186 INF("limits", S_IRUSR, pid_limits),
2187 #ifdef CONFIG_SCHED_DEBUG
2188 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2190 INF("cmdline", S_IRUGO, pid_cmdline),
2191 INF("stat", S_IRUGO, tgid_stat),
2192 INF("statm", S_IRUGO, pid_statm),
2193 REG("maps", S_IRUGO, maps),
2195 REG("numa_maps", S_IRUGO, numa_maps),
2197 REG("mem", S_IRUSR|S_IWUSR, mem),
2201 REG("mounts", S_IRUGO, mounts),
2202 REG("mountstats", S_IRUSR, mountstats),
2204 REG("clear_refs", S_IWUSR, clear_refs),
2205 REG("smaps", S_IRUGO, smaps),
2207 #ifdef CONFIG_SECURITY
2208 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2210 #ifdef CONFIG_KALLSYMS
2211 INF("wchan", S_IRUGO, pid_wchan),
2213 #ifdef CONFIG_SCHEDSTATS
2214 INF("schedstat", S_IRUGO, pid_schedstat),
2216 #ifdef CONFIG_PROC_PID_CPUSET
2217 REG("cpuset", S_IRUGO, cpuset),
2219 #ifdef CONFIG_CGROUPS
2220 REG("cgroup", S_IRUGO, cgroup),
2222 INF("oom_score", S_IRUGO, oom_score),
2223 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2224 #ifdef CONFIG_AUDITSYSCALL
2225 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2227 #ifdef CONFIG_FAULT_INJECTION
2228 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2230 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2231 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2233 #ifdef CONFIG_TASK_IO_ACCOUNTING
2234 INF("io", S_IRUGO, pid_io_accounting),
2238 static int proc_tgid_base_readdir(struct file * filp,
2239 void * dirent, filldir_t filldir)
2241 return proc_pident_readdir(filp,dirent,filldir,
2242 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2245 static const struct file_operations proc_tgid_base_operations = {
2246 .read = generic_read_dir,
2247 .readdir = proc_tgid_base_readdir,
2250 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2251 return proc_pident_lookup(dir, dentry,
2252 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2255 static const struct inode_operations proc_tgid_base_inode_operations = {
2256 .lookup = proc_tgid_base_lookup,
2257 .getattr = pid_getattr,
2258 .setattr = proc_setattr,
2262 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2264 * @task: task that should be flushed.
2266 * Looks in the dcache for
2268 * /proc/@tgid/task/@pid
2269 * if either directory is present flushes it and all of it'ts children
2272 * It is safe and reasonable to cache /proc entries for a task until
2273 * that task exits. After that they just clog up the dcache with
2274 * useless entries, possibly causing useful dcache entries to be
2275 * flushed instead. This routine is proved to flush those useless
2276 * dcache entries at process exit time.
2278 * NOTE: This routine is just an optimization so it does not guarantee
2279 * that no dcache entries will exist at process exit time it
2280 * just makes it very unlikely that any will persist.
2282 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2284 struct dentry *dentry, *leader, *dir;
2285 char buf[PROC_NUMBUF];
2289 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2290 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2292 shrink_dcache_parent(dentry);
2301 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2302 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2307 name.len = strlen(name.name);
2308 dir = d_hash_and_lookup(leader, &name);
2310 goto out_put_leader;
2313 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2314 dentry = d_hash_and_lookup(dir, &name);
2316 shrink_dcache_parent(dentry);
2329 * when flushing dentries from proc one need to flush them from global
2330 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2331 * in. this call is supposed to make all this job.
2334 void proc_flush_task(struct task_struct *task)
2337 struct pid *pid, *tgid;
2340 leader = thread_group_leader(task);
2341 proc_flush_task_mnt(proc_mnt, task->pid, leader ? task->tgid : 0);
2342 pid = task_pid(task);
2343 if (pid->level == 0)
2346 tgid = task_tgid(task);
2347 for (i = 1; i <= pid->level; i++) {
2348 upid = &pid->numbers[i];
2349 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2350 leader ? 0 : tgid->numbers[i].nr);
2353 upid = &pid->numbers[pid->level];
2355 pid_ns_release_proc(upid->ns);
2358 static struct dentry *proc_pid_instantiate(struct inode *dir,
2359 struct dentry * dentry,
2360 struct task_struct *task, const void *ptr)
2362 struct dentry *error = ERR_PTR(-ENOENT);
2363 struct inode *inode;
2365 inode = proc_pid_make_inode(dir->i_sb, task);
2369 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2370 inode->i_op = &proc_tgid_base_inode_operations;
2371 inode->i_fop = &proc_tgid_base_operations;
2372 inode->i_flags|=S_IMMUTABLE;
2374 #ifdef CONFIG_SECURITY
2375 inode->i_nlink += 1;
2378 dentry->d_op = &pid_dentry_operations;
2380 d_add(dentry, inode);
2381 /* Close the race of the process dying before we return the dentry */
2382 if (pid_revalidate(dentry, NULL))
2388 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2390 struct dentry *result = ERR_PTR(-ENOENT);
2391 struct task_struct *task;
2393 struct pid_namespace *ns;
2395 result = proc_base_lookup(dir, dentry);
2396 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2399 tgid = name_to_int(dentry);
2403 ns = dentry->d_sb->s_fs_info;
2405 task = find_task_by_pid_ns(tgid, ns);
2407 get_task_struct(task);
2412 result = proc_pid_instantiate(dir, dentry, task, NULL);
2413 put_task_struct(task);
2419 * Find the first task with tgid >= tgid
2422 static struct task_struct *next_tgid(unsigned int tgid,
2423 struct pid_namespace *ns)
2425 struct task_struct *task;
2431 pid = find_ge_pid(tgid, ns);
2433 tgid = pid_nr_ns(pid, ns) + 1;
2434 task = pid_task(pid, PIDTYPE_PID);
2435 /* What we to know is if the pid we have find is the
2436 * pid of a thread_group_leader. Testing for task
2437 * being a thread_group_leader is the obvious thing
2438 * todo but there is a window when it fails, due to
2439 * the pid transfer logic in de_thread.
2441 * So we perform the straight forward test of seeing
2442 * if the pid we have found is the pid of a thread
2443 * group leader, and don't worry if the task we have
2444 * found doesn't happen to be a thread group leader.
2445 * As we don't care in the case of readdir.
2447 if (!task || !has_group_leader_pid(task))
2449 get_task_struct(task);
2455 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2457 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2458 struct task_struct *task, int tgid)
2460 char name[PROC_NUMBUF];
2461 int len = snprintf(name, sizeof(name), "%d", tgid);
2462 return proc_fill_cache(filp, dirent, filldir, name, len,
2463 proc_pid_instantiate, task, NULL);
2466 /* for the /proc/ directory itself, after non-process stuff has been done */
2467 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2469 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2470 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2471 struct task_struct *task;
2473 struct pid_namespace *ns;
2478 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2479 const struct pid_entry *p = &proc_base_stuff[nr];
2480 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2484 ns = filp->f_dentry->d_sb->s_fs_info;
2485 tgid = filp->f_pos - TGID_OFFSET;
2486 for (task = next_tgid(tgid, ns);
2488 put_task_struct(task), task = next_tgid(tgid + 1, ns)) {
2489 tgid = task_pid_nr_ns(task, ns);
2490 filp->f_pos = tgid + TGID_OFFSET;
2491 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2492 put_task_struct(task);
2496 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2498 put_task_struct(reaper);
2506 static const struct pid_entry tid_base_stuff[] = {
2507 DIR("fd", S_IRUSR|S_IXUSR, fd),
2508 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2509 REG("environ", S_IRUSR, environ),
2510 INF("auxv", S_IRUSR, pid_auxv),
2511 INF("status", S_IRUGO, pid_status),
2512 INF("limits", S_IRUSR, pid_limits),
2513 #ifdef CONFIG_SCHED_DEBUG
2514 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2516 INF("cmdline", S_IRUGO, pid_cmdline),
2517 INF("stat", S_IRUGO, tid_stat),
2518 INF("statm", S_IRUGO, pid_statm),
2519 REG("maps", S_IRUGO, maps),
2521 REG("numa_maps", S_IRUGO, numa_maps),
2523 REG("mem", S_IRUSR|S_IWUSR, mem),
2527 REG("mounts", S_IRUGO, mounts),
2529 REG("clear_refs", S_IWUSR, clear_refs),
2530 REG("smaps", S_IRUGO, smaps),
2532 #ifdef CONFIG_SECURITY
2533 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2535 #ifdef CONFIG_KALLSYMS
2536 INF("wchan", S_IRUGO, pid_wchan),
2538 #ifdef CONFIG_SCHEDSTATS
2539 INF("schedstat", S_IRUGO, pid_schedstat),
2541 #ifdef CONFIG_PROC_PID_CPUSET
2542 REG("cpuset", S_IRUGO, cpuset),
2544 #ifdef CONFIG_CGROUPS
2545 REG("cgroup", S_IRUGO, cgroup),
2547 INF("oom_score", S_IRUGO, oom_score),
2548 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2549 #ifdef CONFIG_AUDITSYSCALL
2550 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2552 #ifdef CONFIG_FAULT_INJECTION
2553 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2557 static int proc_tid_base_readdir(struct file * filp,
2558 void * dirent, filldir_t filldir)
2560 return proc_pident_readdir(filp,dirent,filldir,
2561 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2564 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2565 return proc_pident_lookup(dir, dentry,
2566 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2569 static const struct file_operations proc_tid_base_operations = {
2570 .read = generic_read_dir,
2571 .readdir = proc_tid_base_readdir,
2574 static const struct inode_operations proc_tid_base_inode_operations = {
2575 .lookup = proc_tid_base_lookup,
2576 .getattr = pid_getattr,
2577 .setattr = proc_setattr,
2580 static struct dentry *proc_task_instantiate(struct inode *dir,
2581 struct dentry *dentry, struct task_struct *task, const void *ptr)
2583 struct dentry *error = ERR_PTR(-ENOENT);
2584 struct inode *inode;
2585 inode = proc_pid_make_inode(dir->i_sb, task);
2589 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2590 inode->i_op = &proc_tid_base_inode_operations;
2591 inode->i_fop = &proc_tid_base_operations;
2592 inode->i_flags|=S_IMMUTABLE;
2594 #ifdef CONFIG_SECURITY
2595 inode->i_nlink += 1;
2598 dentry->d_op = &pid_dentry_operations;
2600 d_add(dentry, inode);
2601 /* Close the race of the process dying before we return the dentry */
2602 if (pid_revalidate(dentry, NULL))
2608 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2610 struct dentry *result = ERR_PTR(-ENOENT);
2611 struct task_struct *task;
2612 struct task_struct *leader = get_proc_task(dir);
2614 struct pid_namespace *ns;
2619 tid = name_to_int(dentry);
2623 ns = dentry->d_sb->s_fs_info;
2625 task = find_task_by_pid_ns(tid, ns);
2627 get_task_struct(task);
2631 if (!same_thread_group(leader, task))
2634 result = proc_task_instantiate(dir, dentry, task, NULL);
2636 put_task_struct(task);
2638 put_task_struct(leader);
2644 * Find the first tid of a thread group to return to user space.
2646 * Usually this is just the thread group leader, but if the users
2647 * buffer was too small or there was a seek into the middle of the
2648 * directory we have more work todo.
2650 * In the case of a short read we start with find_task_by_pid.
2652 * In the case of a seek we start with the leader and walk nr
2655 static struct task_struct *first_tid(struct task_struct *leader,
2656 int tid, int nr, struct pid_namespace *ns)
2658 struct task_struct *pos;
2661 /* Attempt to start with the pid of a thread */
2662 if (tid && (nr > 0)) {
2663 pos = find_task_by_pid_ns(tid, ns);
2664 if (pos && (pos->group_leader == leader))
2668 /* If nr exceeds the number of threads there is nothing todo */
2670 if (nr && nr >= get_nr_threads(leader))
2673 /* If we haven't found our starting place yet start
2674 * with the leader and walk nr threads forward.
2676 for (pos = leader; nr > 0; --nr) {
2677 pos = next_thread(pos);
2678 if (pos == leader) {
2684 get_task_struct(pos);
2691 * Find the next thread in the thread list.
2692 * Return NULL if there is an error or no next thread.
2694 * The reference to the input task_struct is released.
2696 static struct task_struct *next_tid(struct task_struct *start)
2698 struct task_struct *pos = NULL;
2700 if (pid_alive(start)) {
2701 pos = next_thread(start);
2702 if (thread_group_leader(pos))
2705 get_task_struct(pos);
2708 put_task_struct(start);
2712 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2713 struct task_struct *task, int tid)
2715 char name[PROC_NUMBUF];
2716 int len = snprintf(name, sizeof(name), "%d", tid);
2717 return proc_fill_cache(filp, dirent, filldir, name, len,
2718 proc_task_instantiate, task, NULL);
2721 /* for the /proc/TGID/task/ directories */
2722 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2724 struct dentry *dentry = filp->f_path.dentry;
2725 struct inode *inode = dentry->d_inode;
2726 struct task_struct *leader = NULL;
2727 struct task_struct *task;
2728 int retval = -ENOENT;
2731 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2732 struct pid_namespace *ns;
2734 task = get_proc_task(inode);
2738 if (pid_alive(task)) {
2739 leader = task->group_leader;
2740 get_task_struct(leader);
2743 put_task_struct(task);
2751 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2756 ino = parent_ino(dentry);
2757 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2763 /* f_version caches the tgid value that the last readdir call couldn't
2764 * return. lseek aka telldir automagically resets f_version to 0.
2766 ns = filp->f_dentry->d_sb->s_fs_info;
2767 tid = (int)filp->f_version;
2768 filp->f_version = 0;
2769 for (task = first_tid(leader, tid, pos - 2, ns);
2771 task = next_tid(task), pos++) {
2772 tid = task_pid_nr_ns(task, ns);
2773 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2774 /* returning this tgid failed, save it as the first
2775 * pid for the next readir call */
2776 filp->f_version = (u64)tid;
2777 put_task_struct(task);
2783 put_task_struct(leader);
2788 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2790 struct inode *inode = dentry->d_inode;
2791 struct task_struct *p = get_proc_task(inode);
2792 generic_fillattr(inode, stat);
2796 stat->nlink += get_nr_threads(p);
2804 static const struct inode_operations proc_task_inode_operations = {
2805 .lookup = proc_task_lookup,
2806 .getattr = proc_task_getattr,
2807 .setattr = proc_setattr,
2810 static const struct file_operations proc_task_operations = {
2811 .read = generic_read_dir,
2812 .readdir = proc_task_readdir,