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.
95 const struct inode_operations *iop;
96 const struct file_operations *fop;
100 #define NOD(NAME, MODE, IOP, FOP, OP) { \
102 .len = sizeof(NAME) - 1, \
109 #define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 #define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
117 #define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
119 &proc_##OTYPE##_operations, {})
120 #define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
124 #define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &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 path *path)
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);
170 read_unlock(&fs->lock);
177 static int proc_root_link(struct inode *inode, struct path *path)
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);
191 read_unlock(&fs->lock);
198 #define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
202 (task_is_stopped_or_traced(task)) && \
203 security_ptrace(current,task) == 0))
205 struct mm_struct *mm_for_maps(struct task_struct *task)
207 struct mm_struct *mm = get_task_mm(task);
210 down_read(&mm->mmap_sem);
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
220 up_read(&mm->mmap_sem);
225 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
229 struct mm_struct *mm = get_task_mm(task);
233 goto out_mm; /* Shh! No looking before we're done */
235 len = mm->arg_end - mm->arg_start;
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
262 static int proc_pid_auxv(struct task_struct *task, char *buffer)
265 struct mm_struct *mm = get_task_mm(task);
267 unsigned int nwords = 0;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
274 memcpy(buffer, mm->saved_auxv, res);
281 #ifdef CONFIG_KALLSYMS
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
286 static int proc_pid_wchan(struct task_struct *task, char *buffer)
289 char symname[KSYM_NAME_LEN];
291 wchan = get_wchan(task);
293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
296 return sprintf(buffer, "%s", symname);
298 #endif /* CONFIG_KALLSYMS */
300 #ifdef CONFIG_SCHEDSTATS
302 * Provides /proc/PID/schedstat
304 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
306 return sprintf(buffer, "%llu %llu %lu\n",
307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
309 task->sched_info.pcount);
313 #ifdef CONFIG_LATENCYTOP
314 static int lstats_show_proc(struct seq_file *m, void *v)
317 struct inode *inode = m->private;
318 struct task_struct *task = get_proc_task(inode);
322 seq_puts(m, "Latency Top version : v0.1\n");
323 for (i = 0; i < 32; i++) {
324 if (task->latency_record[i].backtrace[0]) {
326 seq_printf(m, "%i %li %li ",
327 task->latency_record[i].count,
328 task->latency_record[i].time,
329 task->latency_record[i].max);
330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
331 char sym[KSYM_NAME_LEN];
333 if (!task->latency_record[i].backtrace[q])
335 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
337 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
338 c = strchr(sym, '+');
341 seq_printf(m, "%s ", sym);
347 put_task_struct(task);
351 static int lstats_open(struct inode *inode, struct file *file)
353 return single_open(file, lstats_show_proc, inode);
356 static ssize_t lstats_write(struct file *file, const char __user *buf,
357 size_t count, loff_t *offs)
359 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
363 clear_all_latency_tracing(task);
364 put_task_struct(task);
369 static const struct file_operations proc_lstats_operations = {
372 .write = lstats_write,
374 .release = single_release,
379 /* The badness from the OOM killer */
380 unsigned long badness(struct task_struct *p, unsigned long uptime);
381 static int proc_oom_score(struct task_struct *task, char *buffer)
383 unsigned long points;
384 struct timespec uptime;
386 do_posix_clock_monotonic_gettime(&uptime);
387 read_lock(&tasklist_lock);
388 points = badness(task, uptime.tv_sec);
389 read_unlock(&tasklist_lock);
390 return sprintf(buffer, "%lu\n", points);
398 static const struct limit_names lnames[RLIM_NLIMITS] = {
399 [RLIMIT_CPU] = {"Max cpu time", "ms"},
400 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
401 [RLIMIT_DATA] = {"Max data size", "bytes"},
402 [RLIMIT_STACK] = {"Max stack size", "bytes"},
403 [RLIMIT_CORE] = {"Max core file size", "bytes"},
404 [RLIMIT_RSS] = {"Max resident set", "bytes"},
405 [RLIMIT_NPROC] = {"Max processes", "processes"},
406 [RLIMIT_NOFILE] = {"Max open files", "files"},
407 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
408 [RLIMIT_AS] = {"Max address space", "bytes"},
409 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
410 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
411 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
412 [RLIMIT_NICE] = {"Max nice priority", NULL},
413 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
414 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
417 /* Display limits for a process */
418 static int proc_pid_limits(struct task_struct *task, char *buffer)
423 char *bufptr = buffer;
425 struct rlimit rlim[RLIM_NLIMITS];
428 if (!lock_task_sighand(task,&flags)) {
432 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
433 unlock_task_sighand(task, &flags);
437 * print the file header
439 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
440 "Limit", "Soft Limit", "Hard Limit", "Units");
442 for (i = 0; i < RLIM_NLIMITS; i++) {
443 if (rlim[i].rlim_cur == RLIM_INFINITY)
444 count += sprintf(&bufptr[count], "%-25s %-20s ",
445 lnames[i].name, "unlimited");
447 count += sprintf(&bufptr[count], "%-25s %-20lu ",
448 lnames[i].name, rlim[i].rlim_cur);
450 if (rlim[i].rlim_max == RLIM_INFINITY)
451 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
453 count += sprintf(&bufptr[count], "%-20lu ",
457 count += sprintf(&bufptr[count], "%-10s\n",
460 count += sprintf(&bufptr[count], "\n");
466 /************************************************************************/
467 /* Here the fs part begins */
468 /************************************************************************/
470 /* permission checks */
471 static int proc_fd_access_allowed(struct inode *inode)
473 struct task_struct *task;
475 /* Allow access to a task's file descriptors if it is us or we
476 * may use ptrace attach to the process and find out that
479 task = get_proc_task(inode);
481 allowed = ptrace_may_attach(task);
482 put_task_struct(task);
487 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
490 struct inode *inode = dentry->d_inode;
492 if (attr->ia_valid & ATTR_MODE)
495 error = inode_change_ok(inode, attr);
497 error = inode_setattr(inode, attr);
501 static const struct inode_operations proc_def_inode_operations = {
502 .setattr = proc_setattr,
505 extern const struct seq_operations mounts_op;
511 static int mounts_open(struct inode *inode, struct file *file)
513 struct task_struct *task = get_proc_task(inode);
515 struct mnt_namespace *ns = NULL;
516 struct proc_mounts *p;
521 nsp = task_nsproxy(task);
529 put_task_struct(task);
534 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
536 file->private_data = &p->m;
537 ret = seq_open(file, &mounts_op);
540 p->event = ns->event;
550 static int mounts_release(struct inode *inode, struct file *file)
552 struct seq_file *m = file->private_data;
553 struct mnt_namespace *ns = m->private;
555 return seq_release(inode, file);
558 static unsigned mounts_poll(struct file *file, poll_table *wait)
560 struct proc_mounts *p = file->private_data;
561 struct mnt_namespace *ns = p->m.private;
564 poll_wait(file, &ns->poll, wait);
566 spin_lock(&vfsmount_lock);
567 if (p->event != ns->event) {
568 p->event = ns->event;
571 spin_unlock(&vfsmount_lock);
576 static const struct file_operations proc_mounts_operations = {
580 .release = mounts_release,
584 extern const struct seq_operations mountstats_op;
585 static int mountstats_open(struct inode *inode, struct file *file)
587 int ret = seq_open(file, &mountstats_op);
590 struct seq_file *m = file->private_data;
592 struct mnt_namespace *mnt_ns = NULL;
593 struct task_struct *task = get_proc_task(inode);
597 nsp = task_nsproxy(task);
599 mnt_ns = nsp->mnt_ns;
605 put_task_struct(task);
611 seq_release(inode, file);
618 static const struct file_operations proc_mountstats_operations = {
619 .open = mountstats_open,
622 .release = mounts_release,
625 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
627 static ssize_t proc_info_read(struct file * file, char __user * buf,
628 size_t count, loff_t *ppos)
630 struct inode * inode = file->f_path.dentry->d_inode;
633 struct task_struct *task = get_proc_task(inode);
639 if (count > PROC_BLOCK_SIZE)
640 count = PROC_BLOCK_SIZE;
643 if (!(page = __get_free_page(GFP_TEMPORARY)))
646 length = PROC_I(inode)->op.proc_read(task, (char*)page);
649 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
652 put_task_struct(task);
657 static const struct file_operations proc_info_file_operations = {
658 .read = proc_info_read,
661 static int proc_single_show(struct seq_file *m, void *v)
663 struct inode *inode = m->private;
664 struct pid_namespace *ns;
666 struct task_struct *task;
669 ns = inode->i_sb->s_fs_info;
670 pid = proc_pid(inode);
671 task = get_pid_task(pid, PIDTYPE_PID);
675 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
677 put_task_struct(task);
681 static int proc_single_open(struct inode *inode, struct file *filp)
684 ret = single_open(filp, proc_single_show, NULL);
686 struct seq_file *m = filp->private_data;
693 static const struct file_operations proc_single_file_operations = {
694 .open = proc_single_open,
697 .release = single_release,
700 static int mem_open(struct inode* inode, struct file* file)
702 file->private_data = (void*)((long)current->self_exec_id);
706 static ssize_t mem_read(struct file * file, char __user * buf,
707 size_t count, loff_t *ppos)
709 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
711 unsigned long src = *ppos;
713 struct mm_struct *mm;
718 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
722 page = (char *)__get_free_page(GFP_TEMPORARY);
728 mm = get_task_mm(task);
734 if (file->private_data != (void*)((long)current->self_exec_id))
740 int this_len, retval;
742 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
743 retval = access_process_vm(task, src, page, this_len, 0);
744 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
750 if (copy_to_user(buf, page, retval)) {
765 free_page((unsigned long) page);
767 put_task_struct(task);
772 #define mem_write NULL
775 /* This is a security hazard */
776 static ssize_t mem_write(struct file * file, const char __user *buf,
777 size_t count, loff_t *ppos)
781 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
782 unsigned long dst = *ppos;
788 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
792 page = (char *)__get_free_page(GFP_TEMPORARY);
798 int this_len, retval;
800 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
801 if (copy_from_user(page, buf, this_len)) {
805 retval = access_process_vm(task, dst, page, this_len, 1);
817 free_page((unsigned long) page);
819 put_task_struct(task);
825 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
829 file->f_pos = offset;
832 file->f_pos += offset;
837 force_successful_syscall_return();
841 static const struct file_operations proc_mem_operations = {
848 static ssize_t environ_read(struct file *file, char __user *buf,
849 size_t count, loff_t *ppos)
851 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
853 unsigned long src = *ppos;
855 struct mm_struct *mm;
860 if (!ptrace_may_attach(task))
864 page = (char *)__get_free_page(GFP_TEMPORARY);
870 mm = get_task_mm(task);
875 int this_len, retval, max_len;
877 this_len = mm->env_end - (mm->env_start + src);
882 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
883 this_len = (this_len > max_len) ? max_len : this_len;
885 retval = access_process_vm(task, (mm->env_start + src),
893 if (copy_to_user(buf, page, retval)) {
907 free_page((unsigned long) page);
909 put_task_struct(task);
914 static const struct file_operations proc_environ_operations = {
915 .read = environ_read,
918 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
919 size_t count, loff_t *ppos)
921 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
922 char buffer[PROC_NUMBUF];
928 oom_adjust = task->oomkilladj;
929 put_task_struct(task);
931 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
933 return simple_read_from_buffer(buf, count, ppos, buffer, len);
936 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
937 size_t count, loff_t *ppos)
939 struct task_struct *task;
940 char buffer[PROC_NUMBUF], *end;
943 memset(buffer, 0, sizeof(buffer));
944 if (count > sizeof(buffer) - 1)
945 count = sizeof(buffer) - 1;
946 if (copy_from_user(buffer, buf, count))
948 oom_adjust = simple_strtol(buffer, &end, 0);
949 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
950 oom_adjust != OOM_DISABLE)
954 task = get_proc_task(file->f_path.dentry->d_inode);
957 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
958 put_task_struct(task);
961 task->oomkilladj = oom_adjust;
962 put_task_struct(task);
963 if (end - buffer == 0)
968 static const struct file_operations proc_oom_adjust_operations = {
969 .read = oom_adjust_read,
970 .write = oom_adjust_write,
973 #ifdef CONFIG_AUDITSYSCALL
975 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
976 size_t count, loff_t *ppos)
978 struct inode * inode = file->f_path.dentry->d_inode;
979 struct task_struct *task = get_proc_task(inode);
981 char tmpbuf[TMPBUFLEN];
985 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
986 audit_get_loginuid(task));
987 put_task_struct(task);
988 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
991 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
992 size_t count, loff_t *ppos)
994 struct inode * inode = file->f_path.dentry->d_inode;
999 if (!capable(CAP_AUDIT_CONTROL))
1002 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1005 if (count >= PAGE_SIZE)
1006 count = PAGE_SIZE - 1;
1009 /* No partial writes. */
1012 page = (char*)__get_free_page(GFP_TEMPORARY);
1016 if (copy_from_user(page, buf, count))
1020 loginuid = simple_strtoul(page, &tmp, 10);
1026 length = audit_set_loginuid(current, loginuid);
1027 if (likely(length == 0))
1031 free_page((unsigned long) page);
1035 static const struct file_operations proc_loginuid_operations = {
1036 .read = proc_loginuid_read,
1037 .write = proc_loginuid_write,
1040 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1041 size_t count, loff_t *ppos)
1043 struct inode * inode = file->f_path.dentry->d_inode;
1044 struct task_struct *task = get_proc_task(inode);
1046 char tmpbuf[TMPBUFLEN];
1050 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1051 audit_get_sessionid(task));
1052 put_task_struct(task);
1053 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1056 static const struct file_operations proc_sessionid_operations = {
1057 .read = proc_sessionid_read,
1061 #ifdef CONFIG_FAULT_INJECTION
1062 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1063 size_t count, loff_t *ppos)
1065 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1066 char buffer[PROC_NUMBUF];
1072 make_it_fail = task->make_it_fail;
1073 put_task_struct(task);
1075 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1077 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1080 static ssize_t proc_fault_inject_write(struct file * file,
1081 const char __user * buf, size_t count, loff_t *ppos)
1083 struct task_struct *task;
1084 char buffer[PROC_NUMBUF], *end;
1087 if (!capable(CAP_SYS_RESOURCE))
1089 memset(buffer, 0, sizeof(buffer));
1090 if (count > sizeof(buffer) - 1)
1091 count = sizeof(buffer) - 1;
1092 if (copy_from_user(buffer, buf, count))
1094 make_it_fail = simple_strtol(buffer, &end, 0);
1097 task = get_proc_task(file->f_dentry->d_inode);
1100 task->make_it_fail = make_it_fail;
1101 put_task_struct(task);
1102 if (end - buffer == 0)
1104 return end - buffer;
1107 static const struct file_operations proc_fault_inject_operations = {
1108 .read = proc_fault_inject_read,
1109 .write = proc_fault_inject_write,
1114 #ifdef CONFIG_SCHED_DEBUG
1116 * Print out various scheduling related per-task fields:
1118 static int sched_show(struct seq_file *m, void *v)
1120 struct inode *inode = m->private;
1121 struct task_struct *p;
1125 p = get_proc_task(inode);
1128 proc_sched_show_task(p, m);
1136 sched_write(struct file *file, const char __user *buf,
1137 size_t count, loff_t *offset)
1139 struct inode *inode = file->f_path.dentry->d_inode;
1140 struct task_struct *p;
1144 p = get_proc_task(inode);
1147 proc_sched_set_task(p);
1154 static int sched_open(struct inode *inode, struct file *filp)
1158 ret = single_open(filp, sched_show, NULL);
1160 struct seq_file *m = filp->private_data;
1167 static const struct file_operations proc_pid_sched_operations = {
1170 .write = sched_write,
1171 .llseek = seq_lseek,
1172 .release = single_release,
1177 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1179 struct inode *inode = dentry->d_inode;
1180 int error = -EACCES;
1182 /* We don't need a base pointer in the /proc filesystem */
1183 path_put(&nd->path);
1185 /* Are we allowed to snoop on the tasks file descriptors? */
1186 if (!proc_fd_access_allowed(inode))
1189 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1190 nd->last_type = LAST_BIND;
1192 return ERR_PTR(error);
1195 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1197 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1204 pathname = d_path(path, tmp, PAGE_SIZE);
1205 len = PTR_ERR(pathname);
1206 if (IS_ERR(pathname))
1208 len = tmp + PAGE_SIZE - 1 - pathname;
1212 if (copy_to_user(buffer, pathname, len))
1215 free_page((unsigned long)tmp);
1219 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1221 int error = -EACCES;
1222 struct inode *inode = dentry->d_inode;
1225 /* Are we allowed to snoop on the tasks file descriptors? */
1226 if (!proc_fd_access_allowed(inode))
1229 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1233 error = do_proc_readlink(&path, buffer, buflen);
1239 static const struct inode_operations proc_pid_link_inode_operations = {
1240 .readlink = proc_pid_readlink,
1241 .follow_link = proc_pid_follow_link,
1242 .setattr = proc_setattr,
1246 /* building an inode */
1248 static int task_dumpable(struct task_struct *task)
1251 struct mm_struct *mm;
1256 dumpable = get_dumpable(mm);
1264 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1266 struct inode * inode;
1267 struct proc_inode *ei;
1269 /* We need a new inode */
1271 inode = new_inode(sb);
1277 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1278 inode->i_op = &proc_def_inode_operations;
1281 * grab the reference to task.
1283 ei->pid = get_task_pid(task, PIDTYPE_PID);
1289 if (task_dumpable(task)) {
1290 inode->i_uid = task->euid;
1291 inode->i_gid = task->egid;
1293 security_task_to_inode(task, inode);
1303 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1305 struct inode *inode = dentry->d_inode;
1306 struct task_struct *task;
1307 generic_fillattr(inode, stat);
1312 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1314 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1315 task_dumpable(task)) {
1316 stat->uid = task->euid;
1317 stat->gid = task->egid;
1327 * Exceptional case: normally we are not allowed to unhash a busy
1328 * directory. In this case, however, we can do it - no aliasing problems
1329 * due to the way we treat inodes.
1331 * Rewrite the inode's ownerships here because the owning task may have
1332 * performed a setuid(), etc.
1334 * Before the /proc/pid/status file was created the only way to read
1335 * the effective uid of a /process was to stat /proc/pid. Reading
1336 * /proc/pid/status is slow enough that procps and other packages
1337 * kept stating /proc/pid. To keep the rules in /proc simple I have
1338 * made this apply to all per process world readable and executable
1341 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1343 struct inode *inode = dentry->d_inode;
1344 struct task_struct *task = get_proc_task(inode);
1346 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1347 task_dumpable(task)) {
1348 inode->i_uid = task->euid;
1349 inode->i_gid = task->egid;
1354 inode->i_mode &= ~(S_ISUID | S_ISGID);
1355 security_task_to_inode(task, inode);
1356 put_task_struct(task);
1363 static int pid_delete_dentry(struct dentry * dentry)
1365 /* Is the task we represent dead?
1366 * If so, then don't put the dentry on the lru list,
1367 * kill it immediately.
1369 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1372 static struct dentry_operations pid_dentry_operations =
1374 .d_revalidate = pid_revalidate,
1375 .d_delete = pid_delete_dentry,
1380 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1381 struct task_struct *, const void *);
1384 * Fill a directory entry.
1386 * If possible create the dcache entry and derive our inode number and
1387 * file type from dcache entry.
1389 * Since all of the proc inode numbers are dynamically generated, the inode
1390 * numbers do not exist until the inode is cache. This means creating the
1391 * the dcache entry in readdir is necessary to keep the inode numbers
1392 * reported by readdir in sync with the inode numbers reported
1395 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1396 char *name, int len,
1397 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1399 struct dentry *child, *dir = filp->f_path.dentry;
1400 struct inode *inode;
1403 unsigned type = DT_UNKNOWN;
1407 qname.hash = full_name_hash(name, len);
1409 child = d_lookup(dir, &qname);
1412 new = d_alloc(dir, &qname);
1414 child = instantiate(dir->d_inode, new, task, ptr);
1421 if (!child || IS_ERR(child) || !child->d_inode)
1422 goto end_instantiate;
1423 inode = child->d_inode;
1426 type = inode->i_mode >> 12;
1431 ino = find_inode_number(dir, &qname);
1434 return filldir(dirent, name, len, filp->f_pos, ino, type);
1437 static unsigned name_to_int(struct dentry *dentry)
1439 const char *name = dentry->d_name.name;
1440 int len = dentry->d_name.len;
1443 if (len > 1 && *name == '0')
1446 unsigned c = *name++ - '0';
1449 if (n >= (~0U-9)/10)
1459 #define PROC_FDINFO_MAX 64
1461 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1463 struct task_struct *task = get_proc_task(inode);
1464 struct files_struct *files = NULL;
1466 int fd = proc_fd(inode);
1469 files = get_files_struct(task);
1470 put_task_struct(task);
1474 * We are not taking a ref to the file structure, so we must
1477 spin_lock(&files->file_lock);
1478 file = fcheck_files(files, fd);
1481 *path = file->f_path;
1482 path_get(&file->f_path);
1485 snprintf(info, PROC_FDINFO_MAX,
1488 (long long) file->f_pos,
1490 spin_unlock(&files->file_lock);
1491 put_files_struct(files);
1494 spin_unlock(&files->file_lock);
1495 put_files_struct(files);
1500 static int proc_fd_link(struct inode *inode, struct path *path)
1502 return proc_fd_info(inode, path, NULL);
1505 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1507 struct inode *inode = dentry->d_inode;
1508 struct task_struct *task = get_proc_task(inode);
1509 int fd = proc_fd(inode);
1510 struct files_struct *files;
1513 files = get_files_struct(task);
1516 if (fcheck_files(files, fd)) {
1518 put_files_struct(files);
1519 if (task_dumpable(task)) {
1520 inode->i_uid = task->euid;
1521 inode->i_gid = task->egid;
1526 inode->i_mode &= ~(S_ISUID | S_ISGID);
1527 security_task_to_inode(task, inode);
1528 put_task_struct(task);
1532 put_files_struct(files);
1534 put_task_struct(task);
1540 static struct dentry_operations tid_fd_dentry_operations =
1542 .d_revalidate = tid_fd_revalidate,
1543 .d_delete = pid_delete_dentry,
1546 static struct dentry *proc_fd_instantiate(struct inode *dir,
1547 struct dentry *dentry, struct task_struct *task, const void *ptr)
1549 unsigned fd = *(const unsigned *)ptr;
1551 struct files_struct *files;
1552 struct inode *inode;
1553 struct proc_inode *ei;
1554 struct dentry *error = ERR_PTR(-ENOENT);
1556 inode = proc_pid_make_inode(dir->i_sb, task);
1561 files = get_files_struct(task);
1564 inode->i_mode = S_IFLNK;
1567 * We are not taking a ref to the file structure, so we must
1570 spin_lock(&files->file_lock);
1571 file = fcheck_files(files, fd);
1574 if (file->f_mode & 1)
1575 inode->i_mode |= S_IRUSR | S_IXUSR;
1576 if (file->f_mode & 2)
1577 inode->i_mode |= S_IWUSR | S_IXUSR;
1578 spin_unlock(&files->file_lock);
1579 put_files_struct(files);
1581 inode->i_op = &proc_pid_link_inode_operations;
1583 ei->op.proc_get_link = proc_fd_link;
1584 dentry->d_op = &tid_fd_dentry_operations;
1585 d_add(dentry, inode);
1586 /* Close the race of the process dying before we return the dentry */
1587 if (tid_fd_revalidate(dentry, NULL))
1593 spin_unlock(&files->file_lock);
1594 put_files_struct(files);
1600 static struct dentry *proc_lookupfd_common(struct inode *dir,
1601 struct dentry *dentry,
1602 instantiate_t instantiate)
1604 struct task_struct *task = get_proc_task(dir);
1605 unsigned fd = name_to_int(dentry);
1606 struct dentry *result = ERR_PTR(-ENOENT);
1613 result = instantiate(dir, dentry, task, &fd);
1615 put_task_struct(task);
1620 static int proc_readfd_common(struct file * filp, void * dirent,
1621 filldir_t filldir, instantiate_t instantiate)
1623 struct dentry *dentry = filp->f_path.dentry;
1624 struct inode *inode = dentry->d_inode;
1625 struct task_struct *p = get_proc_task(inode);
1626 unsigned int fd, ino;
1628 struct files_struct * files;
1629 struct fdtable *fdt;
1639 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1643 ino = parent_ino(dentry);
1644 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1648 files = get_files_struct(p);
1652 fdt = files_fdtable(files);
1653 for (fd = filp->f_pos-2;
1655 fd++, filp->f_pos++) {
1656 char name[PROC_NUMBUF];
1659 if (!fcheck_files(files, fd))
1663 len = snprintf(name, sizeof(name), "%d", fd);
1664 if (proc_fill_cache(filp, dirent, filldir,
1665 name, len, instantiate,
1673 put_files_struct(files);
1681 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1682 struct nameidata *nd)
1684 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1687 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1689 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1692 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1693 size_t len, loff_t *ppos)
1695 char tmp[PROC_FDINFO_MAX];
1696 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1698 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1702 static const struct file_operations proc_fdinfo_file_operations = {
1703 .open = nonseekable_open,
1704 .read = proc_fdinfo_read,
1707 static const struct file_operations proc_fd_operations = {
1708 .read = generic_read_dir,
1709 .readdir = proc_readfd,
1713 * /proc/pid/fd needs a special permission handler so that a process can still
1714 * access /proc/self/fd after it has executed a setuid().
1716 static int proc_fd_permission(struct inode *inode, int mask,
1717 struct nameidata *nd)
1721 rv = generic_permission(inode, mask, NULL);
1724 if (task_pid(current) == proc_pid(inode))
1730 * proc directories can do almost nothing..
1732 static const struct inode_operations proc_fd_inode_operations = {
1733 .lookup = proc_lookupfd,
1734 .permission = proc_fd_permission,
1735 .setattr = proc_setattr,
1738 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1739 struct dentry *dentry, struct task_struct *task, const void *ptr)
1741 unsigned fd = *(unsigned *)ptr;
1742 struct inode *inode;
1743 struct proc_inode *ei;
1744 struct dentry *error = ERR_PTR(-ENOENT);
1746 inode = proc_pid_make_inode(dir->i_sb, task);
1751 inode->i_mode = S_IFREG | S_IRUSR;
1752 inode->i_fop = &proc_fdinfo_file_operations;
1753 dentry->d_op = &tid_fd_dentry_operations;
1754 d_add(dentry, inode);
1755 /* Close the race of the process dying before we return the dentry */
1756 if (tid_fd_revalidate(dentry, NULL))
1763 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1764 struct dentry *dentry,
1765 struct nameidata *nd)
1767 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1770 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1772 return proc_readfd_common(filp, dirent, filldir,
1773 proc_fdinfo_instantiate);
1776 static const struct file_operations proc_fdinfo_operations = {
1777 .read = generic_read_dir,
1778 .readdir = proc_readfdinfo,
1782 * proc directories can do almost nothing..
1784 static const struct inode_operations proc_fdinfo_inode_operations = {
1785 .lookup = proc_lookupfdinfo,
1786 .setattr = proc_setattr,
1790 static struct dentry *proc_pident_instantiate(struct inode *dir,
1791 struct dentry *dentry, struct task_struct *task, const void *ptr)
1793 const struct pid_entry *p = ptr;
1794 struct inode *inode;
1795 struct proc_inode *ei;
1796 struct dentry *error = ERR_PTR(-EINVAL);
1798 inode = proc_pid_make_inode(dir->i_sb, task);
1803 inode->i_mode = p->mode;
1804 if (S_ISDIR(inode->i_mode))
1805 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1807 inode->i_op = p->iop;
1809 inode->i_fop = p->fop;
1811 dentry->d_op = &pid_dentry_operations;
1812 d_add(dentry, inode);
1813 /* Close the race of the process dying before we return the dentry */
1814 if (pid_revalidate(dentry, NULL))
1820 static struct dentry *proc_pident_lookup(struct inode *dir,
1821 struct dentry *dentry,
1822 const struct pid_entry *ents,
1825 struct inode *inode;
1826 struct dentry *error;
1827 struct task_struct *task = get_proc_task(dir);
1828 const struct pid_entry *p, *last;
1830 error = ERR_PTR(-ENOENT);
1837 * Yes, it does not scale. And it should not. Don't add
1838 * new entries into /proc/<tgid>/ without very good reasons.
1840 last = &ents[nents - 1];
1841 for (p = ents; p <= last; p++) {
1842 if (p->len != dentry->d_name.len)
1844 if (!memcmp(dentry->d_name.name, p->name, p->len))
1850 error = proc_pident_instantiate(dir, dentry, task, p);
1852 put_task_struct(task);
1857 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1858 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1860 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1861 proc_pident_instantiate, task, p);
1864 static int proc_pident_readdir(struct file *filp,
1865 void *dirent, filldir_t filldir,
1866 const struct pid_entry *ents, unsigned int nents)
1869 struct dentry *dentry = filp->f_path.dentry;
1870 struct inode *inode = dentry->d_inode;
1871 struct task_struct *task = get_proc_task(inode);
1872 const struct pid_entry *p, *last;
1885 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1891 ino = parent_ino(dentry);
1892 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1904 last = &ents[nents - 1];
1906 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1915 put_task_struct(task);
1920 #ifdef CONFIG_SECURITY
1921 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1922 size_t count, loff_t *ppos)
1924 struct inode * inode = file->f_path.dentry->d_inode;
1927 struct task_struct *task = get_proc_task(inode);
1932 length = security_getprocattr(task,
1933 (char*)file->f_path.dentry->d_name.name,
1935 put_task_struct(task);
1937 length = simple_read_from_buffer(buf, count, ppos, p, length);
1942 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1943 size_t count, loff_t *ppos)
1945 struct inode * inode = file->f_path.dentry->d_inode;
1948 struct task_struct *task = get_proc_task(inode);
1953 if (count > PAGE_SIZE)
1956 /* No partial writes. */
1962 page = (char*)__get_free_page(GFP_TEMPORARY);
1967 if (copy_from_user(page, buf, count))
1970 length = security_setprocattr(task,
1971 (char*)file->f_path.dentry->d_name.name,
1972 (void*)page, count);
1974 free_page((unsigned long) page);
1976 put_task_struct(task);
1981 static const struct file_operations proc_pid_attr_operations = {
1982 .read = proc_pid_attr_read,
1983 .write = proc_pid_attr_write,
1986 static const struct pid_entry attr_dir_stuff[] = {
1987 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1988 REG("prev", S_IRUGO, pid_attr),
1989 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1990 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1991 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1992 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1995 static int proc_attr_dir_readdir(struct file * filp,
1996 void * dirent, filldir_t filldir)
1998 return proc_pident_readdir(filp,dirent,filldir,
1999 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2002 static const struct file_operations proc_attr_dir_operations = {
2003 .read = generic_read_dir,
2004 .readdir = proc_attr_dir_readdir,
2007 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2008 struct dentry *dentry, struct nameidata *nd)
2010 return proc_pident_lookup(dir, dentry,
2011 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2014 static const struct inode_operations proc_attr_dir_inode_operations = {
2015 .lookup = proc_attr_dir_lookup,
2016 .getattr = pid_getattr,
2017 .setattr = proc_setattr,
2022 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2023 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2024 size_t count, loff_t *ppos)
2026 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2027 struct mm_struct *mm;
2028 char buffer[PROC_NUMBUF];
2036 mm = get_task_mm(task);
2038 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2039 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2040 MMF_DUMP_FILTER_SHIFT));
2042 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2045 put_task_struct(task);
2050 static ssize_t proc_coredump_filter_write(struct file *file,
2051 const char __user *buf,
2055 struct task_struct *task;
2056 struct mm_struct *mm;
2057 char buffer[PROC_NUMBUF], *end;
2064 memset(buffer, 0, sizeof(buffer));
2065 if (count > sizeof(buffer) - 1)
2066 count = sizeof(buffer) - 1;
2067 if (copy_from_user(buffer, buf, count))
2071 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2074 if (end - buffer == 0)
2078 task = get_proc_task(file->f_dentry->d_inode);
2083 mm = get_task_mm(task);
2087 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2089 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2091 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2096 put_task_struct(task);
2101 static const struct file_operations proc_coredump_filter_operations = {
2102 .read = proc_coredump_filter_read,
2103 .write = proc_coredump_filter_write,
2110 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2113 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2114 pid_t tgid = task_tgid_nr_ns(current, ns);
2115 char tmp[PROC_NUMBUF];
2118 sprintf(tmp, "%d", tgid);
2119 return vfs_readlink(dentry,buffer,buflen,tmp);
2122 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2124 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2125 pid_t tgid = task_tgid_nr_ns(current, ns);
2126 char tmp[PROC_NUMBUF];
2128 return ERR_PTR(-ENOENT);
2129 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2130 return ERR_PTR(vfs_follow_link(nd,tmp));
2133 static const struct inode_operations proc_self_inode_operations = {
2134 .readlink = proc_self_readlink,
2135 .follow_link = proc_self_follow_link,
2141 * These are the directory entries in the root directory of /proc
2142 * that properly belong to the /proc filesystem, as they describe
2143 * describe something that is process related.
2145 static const struct pid_entry proc_base_stuff[] = {
2146 NOD("self", S_IFLNK|S_IRWXUGO,
2147 &proc_self_inode_operations, NULL, {}),
2151 * Exceptional case: normally we are not allowed to unhash a busy
2152 * directory. In this case, however, we can do it - no aliasing problems
2153 * due to the way we treat inodes.
2155 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2157 struct inode *inode = dentry->d_inode;
2158 struct task_struct *task = get_proc_task(inode);
2160 put_task_struct(task);
2167 static struct dentry_operations proc_base_dentry_operations =
2169 .d_revalidate = proc_base_revalidate,
2170 .d_delete = pid_delete_dentry,
2173 static struct dentry *proc_base_instantiate(struct inode *dir,
2174 struct dentry *dentry, struct task_struct *task, const void *ptr)
2176 const struct pid_entry *p = ptr;
2177 struct inode *inode;
2178 struct proc_inode *ei;
2179 struct dentry *error = ERR_PTR(-EINVAL);
2181 /* Allocate the inode */
2182 error = ERR_PTR(-ENOMEM);
2183 inode = new_inode(dir->i_sb);
2187 /* Initialize the inode */
2189 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2192 * grab the reference to the task.
2194 ei->pid = get_task_pid(task, PIDTYPE_PID);
2200 inode->i_mode = p->mode;
2201 if (S_ISDIR(inode->i_mode))
2203 if (S_ISLNK(inode->i_mode))
2206 inode->i_op = p->iop;
2208 inode->i_fop = p->fop;
2210 dentry->d_op = &proc_base_dentry_operations;
2211 d_add(dentry, inode);
2220 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2222 struct dentry *error;
2223 struct task_struct *task = get_proc_task(dir);
2224 const struct pid_entry *p, *last;
2226 error = ERR_PTR(-ENOENT);
2231 /* Lookup the directory entry */
2232 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2233 for (p = proc_base_stuff; p <= last; p++) {
2234 if (p->len != dentry->d_name.len)
2236 if (!memcmp(dentry->d_name.name, p->name, p->len))
2242 error = proc_base_instantiate(dir, dentry, task, p);
2245 put_task_struct(task);
2250 static int proc_base_fill_cache(struct file *filp, void *dirent,
2251 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2253 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2254 proc_base_instantiate, task, p);
2257 #ifdef CONFIG_TASK_IO_ACCOUNTING
2258 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2260 return sprintf(buffer,
2261 #ifdef CONFIG_TASK_XACCT
2267 "read_bytes: %llu\n"
2268 "write_bytes: %llu\n"
2269 "cancelled_write_bytes: %llu\n",
2270 #ifdef CONFIG_TASK_XACCT
2271 (unsigned long long)task->rchar,
2272 (unsigned long long)task->wchar,
2273 (unsigned long long)task->syscr,
2274 (unsigned long long)task->syscw,
2276 (unsigned long long)task->ioac.read_bytes,
2277 (unsigned long long)task->ioac.write_bytes,
2278 (unsigned long long)task->ioac.cancelled_write_bytes);
2285 static const struct file_operations proc_task_operations;
2286 static const struct inode_operations proc_task_inode_operations;
2288 static const struct pid_entry tgid_base_stuff[] = {
2289 DIR("task", S_IRUGO|S_IXUGO, task),
2290 DIR("fd", S_IRUSR|S_IXUSR, fd),
2291 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2293 DIR("net", S_IRUGO|S_IXUSR, net),
2295 REG("environ", S_IRUSR, environ),
2296 INF("auxv", S_IRUSR, pid_auxv),
2297 ONE("status", S_IRUGO, pid_status),
2298 INF("limits", S_IRUSR, pid_limits),
2299 #ifdef CONFIG_SCHED_DEBUG
2300 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2302 INF("cmdline", S_IRUGO, pid_cmdline),
2303 ONE("stat", S_IRUGO, tgid_stat),
2304 ONE("statm", S_IRUGO, pid_statm),
2305 REG("maps", S_IRUGO, maps),
2307 REG("numa_maps", S_IRUGO, numa_maps),
2309 REG("mem", S_IRUSR|S_IWUSR, mem),
2313 REG("mounts", S_IRUGO, mounts),
2314 REG("mountstats", S_IRUSR, mountstats),
2315 #ifdef CONFIG_PROC_PAGE_MONITOR
2316 REG("clear_refs", S_IWUSR, clear_refs),
2317 REG("smaps", S_IRUGO, smaps),
2318 REG("pagemap", S_IRUSR, pagemap),
2320 #ifdef CONFIG_SECURITY
2321 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2323 #ifdef CONFIG_KALLSYMS
2324 INF("wchan", S_IRUGO, pid_wchan),
2326 #ifdef CONFIG_SCHEDSTATS
2327 INF("schedstat", S_IRUGO, pid_schedstat),
2329 #ifdef CONFIG_LATENCYTOP
2330 REG("latency", S_IRUGO, lstats),
2332 #ifdef CONFIG_PROC_PID_CPUSET
2333 REG("cpuset", S_IRUGO, cpuset),
2335 #ifdef CONFIG_CGROUPS
2336 REG("cgroup", S_IRUGO, cgroup),
2338 INF("oom_score", S_IRUGO, oom_score),
2339 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2340 #ifdef CONFIG_AUDITSYSCALL
2341 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2342 REG("sessionid", S_IRUSR, sessionid),
2344 #ifdef CONFIG_FAULT_INJECTION
2345 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2347 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2348 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2350 #ifdef CONFIG_TASK_IO_ACCOUNTING
2351 INF("io", S_IRUGO, pid_io_accounting),
2355 static int proc_tgid_base_readdir(struct file * filp,
2356 void * dirent, filldir_t filldir)
2358 return proc_pident_readdir(filp,dirent,filldir,
2359 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2362 static const struct file_operations proc_tgid_base_operations = {
2363 .read = generic_read_dir,
2364 .readdir = proc_tgid_base_readdir,
2367 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2368 return proc_pident_lookup(dir, dentry,
2369 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2372 static const struct inode_operations proc_tgid_base_inode_operations = {
2373 .lookup = proc_tgid_base_lookup,
2374 .getattr = pid_getattr,
2375 .setattr = proc_setattr,
2378 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2380 struct dentry *dentry, *leader, *dir;
2381 char buf[PROC_NUMBUF];
2385 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2386 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2388 if (!(current->flags & PF_EXITING))
2389 shrink_dcache_parent(dentry);
2398 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2399 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2404 name.len = strlen(name.name);
2405 dir = d_hash_and_lookup(leader, &name);
2407 goto out_put_leader;
2410 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2411 dentry = d_hash_and_lookup(dir, &name);
2413 shrink_dcache_parent(dentry);
2426 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2427 * @task: task that should be flushed.
2429 * When flushing dentries from proc, one needs to flush them from global
2430 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2431 * in. This call is supposed to do all of this job.
2433 * Looks in the dcache for
2435 * /proc/@tgid/task/@pid
2436 * if either directory is present flushes it and all of it'ts children
2439 * It is safe and reasonable to cache /proc entries for a task until
2440 * that task exits. After that they just clog up the dcache with
2441 * useless entries, possibly causing useful dcache entries to be
2442 * flushed instead. This routine is proved to flush those useless
2443 * dcache entries at process exit time.
2445 * NOTE: This routine is just an optimization so it does not guarantee
2446 * that no dcache entries will exist at process exit time it
2447 * just makes it very unlikely that any will persist.
2450 void proc_flush_task(struct task_struct *task)
2453 struct pid *pid, *tgid = NULL;
2456 pid = task_pid(task);
2457 if (thread_group_leader(task))
2458 tgid = task_tgid(task);
2460 for (i = 0; i <= pid->level; i++) {
2461 upid = &pid->numbers[i];
2462 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2463 tgid ? tgid->numbers[i].nr : 0);
2466 upid = &pid->numbers[pid->level];
2468 pid_ns_release_proc(upid->ns);
2471 static struct dentry *proc_pid_instantiate(struct inode *dir,
2472 struct dentry * dentry,
2473 struct task_struct *task, const void *ptr)
2475 struct dentry *error = ERR_PTR(-ENOENT);
2476 struct inode *inode;
2478 inode = proc_pid_make_inode(dir->i_sb, task);
2482 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2483 inode->i_op = &proc_tgid_base_inode_operations;
2484 inode->i_fop = &proc_tgid_base_operations;
2485 inode->i_flags|=S_IMMUTABLE;
2487 #ifdef CONFIG_SECURITY
2488 inode->i_nlink += 1;
2491 dentry->d_op = &pid_dentry_operations;
2493 d_add(dentry, inode);
2494 /* Close the race of the process dying before we return the dentry */
2495 if (pid_revalidate(dentry, NULL))
2501 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2503 struct dentry *result = ERR_PTR(-ENOENT);
2504 struct task_struct *task;
2506 struct pid_namespace *ns;
2508 result = proc_base_lookup(dir, dentry);
2509 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2512 tgid = name_to_int(dentry);
2516 ns = dentry->d_sb->s_fs_info;
2518 task = find_task_by_pid_ns(tgid, ns);
2520 get_task_struct(task);
2525 result = proc_pid_instantiate(dir, dentry, task, NULL);
2526 put_task_struct(task);
2532 * Find the first task with tgid >= tgid
2537 struct task_struct *task;
2539 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2544 put_task_struct(iter.task);
2548 pid = find_ge_pid(iter.tgid, ns);
2550 iter.tgid = pid_nr_ns(pid, ns);
2551 iter.task = pid_task(pid, PIDTYPE_PID);
2552 /* What we to know is if the pid we have find is the
2553 * pid of a thread_group_leader. Testing for task
2554 * being a thread_group_leader is the obvious thing
2555 * todo but there is a window when it fails, due to
2556 * the pid transfer logic in de_thread.
2558 * So we perform the straight forward test of seeing
2559 * if the pid we have found is the pid of a thread
2560 * group leader, and don't worry if the task we have
2561 * found doesn't happen to be a thread group leader.
2562 * As we don't care in the case of readdir.
2564 if (!iter.task || !has_group_leader_pid(iter.task)) {
2568 get_task_struct(iter.task);
2574 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2576 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2577 struct tgid_iter iter)
2579 char name[PROC_NUMBUF];
2580 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2581 return proc_fill_cache(filp, dirent, filldir, name, len,
2582 proc_pid_instantiate, iter.task, NULL);
2585 /* for the /proc/ directory itself, after non-process stuff has been done */
2586 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2588 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2589 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2590 struct tgid_iter iter;
2591 struct pid_namespace *ns;
2596 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2597 const struct pid_entry *p = &proc_base_stuff[nr];
2598 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2602 ns = filp->f_dentry->d_sb->s_fs_info;
2604 iter.tgid = filp->f_pos - TGID_OFFSET;
2605 for (iter = next_tgid(ns, iter);
2607 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2608 filp->f_pos = iter.tgid + TGID_OFFSET;
2609 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2610 put_task_struct(iter.task);
2614 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2616 put_task_struct(reaper);
2624 static const struct pid_entry tid_base_stuff[] = {
2625 DIR("fd", S_IRUSR|S_IXUSR, fd),
2626 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2627 REG("environ", S_IRUSR, environ),
2628 INF("auxv", S_IRUSR, pid_auxv),
2629 ONE("status", S_IRUGO, pid_status),
2630 INF("limits", S_IRUSR, pid_limits),
2631 #ifdef CONFIG_SCHED_DEBUG
2632 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2634 INF("cmdline", S_IRUGO, pid_cmdline),
2635 ONE("stat", S_IRUGO, tid_stat),
2636 ONE("statm", S_IRUGO, pid_statm),
2637 REG("maps", S_IRUGO, maps),
2639 REG("numa_maps", S_IRUGO, numa_maps),
2641 REG("mem", S_IRUSR|S_IWUSR, mem),
2645 REG("mounts", S_IRUGO, mounts),
2646 #ifdef CONFIG_PROC_PAGE_MONITOR
2647 REG("clear_refs", S_IWUSR, clear_refs),
2648 REG("smaps", S_IRUGO, smaps),
2649 REG("pagemap", S_IRUSR, pagemap),
2651 #ifdef CONFIG_SECURITY
2652 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2654 #ifdef CONFIG_KALLSYMS
2655 INF("wchan", S_IRUGO, pid_wchan),
2657 #ifdef CONFIG_SCHEDSTATS
2658 INF("schedstat", S_IRUGO, pid_schedstat),
2660 #ifdef CONFIG_LATENCYTOP
2661 REG("latency", S_IRUGO, lstats),
2663 #ifdef CONFIG_PROC_PID_CPUSET
2664 REG("cpuset", S_IRUGO, cpuset),
2666 #ifdef CONFIG_CGROUPS
2667 REG("cgroup", S_IRUGO, cgroup),
2669 INF("oom_score", S_IRUGO, oom_score),
2670 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2671 #ifdef CONFIG_AUDITSYSCALL
2672 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2673 REG("sessionid", S_IRUSR, sessionid),
2675 #ifdef CONFIG_FAULT_INJECTION
2676 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2680 static int proc_tid_base_readdir(struct file * filp,
2681 void * dirent, filldir_t filldir)
2683 return proc_pident_readdir(filp,dirent,filldir,
2684 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2687 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2688 return proc_pident_lookup(dir, dentry,
2689 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2692 static const struct file_operations proc_tid_base_operations = {
2693 .read = generic_read_dir,
2694 .readdir = proc_tid_base_readdir,
2697 static const struct inode_operations proc_tid_base_inode_operations = {
2698 .lookup = proc_tid_base_lookup,
2699 .getattr = pid_getattr,
2700 .setattr = proc_setattr,
2703 static struct dentry *proc_task_instantiate(struct inode *dir,
2704 struct dentry *dentry, struct task_struct *task, const void *ptr)
2706 struct dentry *error = ERR_PTR(-ENOENT);
2707 struct inode *inode;
2708 inode = proc_pid_make_inode(dir->i_sb, task);
2712 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2713 inode->i_op = &proc_tid_base_inode_operations;
2714 inode->i_fop = &proc_tid_base_operations;
2715 inode->i_flags|=S_IMMUTABLE;
2717 #ifdef CONFIG_SECURITY
2718 inode->i_nlink += 1;
2721 dentry->d_op = &pid_dentry_operations;
2723 d_add(dentry, inode);
2724 /* Close the race of the process dying before we return the dentry */
2725 if (pid_revalidate(dentry, NULL))
2731 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2733 struct dentry *result = ERR_PTR(-ENOENT);
2734 struct task_struct *task;
2735 struct task_struct *leader = get_proc_task(dir);
2737 struct pid_namespace *ns;
2742 tid = name_to_int(dentry);
2746 ns = dentry->d_sb->s_fs_info;
2748 task = find_task_by_pid_ns(tid, ns);
2750 get_task_struct(task);
2754 if (!same_thread_group(leader, task))
2757 result = proc_task_instantiate(dir, dentry, task, NULL);
2759 put_task_struct(task);
2761 put_task_struct(leader);
2767 * Find the first tid of a thread group to return to user space.
2769 * Usually this is just the thread group leader, but if the users
2770 * buffer was too small or there was a seek into the middle of the
2771 * directory we have more work todo.
2773 * In the case of a short read we start with find_task_by_pid.
2775 * In the case of a seek we start with the leader and walk nr
2778 static struct task_struct *first_tid(struct task_struct *leader,
2779 int tid, int nr, struct pid_namespace *ns)
2781 struct task_struct *pos;
2784 /* Attempt to start with the pid of a thread */
2785 if (tid && (nr > 0)) {
2786 pos = find_task_by_pid_ns(tid, ns);
2787 if (pos && (pos->group_leader == leader))
2791 /* If nr exceeds the number of threads there is nothing todo */
2793 if (nr && nr >= get_nr_threads(leader))
2796 /* If we haven't found our starting place yet start
2797 * with the leader and walk nr threads forward.
2799 for (pos = leader; nr > 0; --nr) {
2800 pos = next_thread(pos);
2801 if (pos == leader) {
2807 get_task_struct(pos);
2814 * Find the next thread in the thread list.
2815 * Return NULL if there is an error or no next thread.
2817 * The reference to the input task_struct is released.
2819 static struct task_struct *next_tid(struct task_struct *start)
2821 struct task_struct *pos = NULL;
2823 if (pid_alive(start)) {
2824 pos = next_thread(start);
2825 if (thread_group_leader(pos))
2828 get_task_struct(pos);
2831 put_task_struct(start);
2835 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2836 struct task_struct *task, int tid)
2838 char name[PROC_NUMBUF];
2839 int len = snprintf(name, sizeof(name), "%d", tid);
2840 return proc_fill_cache(filp, dirent, filldir, name, len,
2841 proc_task_instantiate, task, NULL);
2844 /* for the /proc/TGID/task/ directories */
2845 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2847 struct dentry *dentry = filp->f_path.dentry;
2848 struct inode *inode = dentry->d_inode;
2849 struct task_struct *leader = NULL;
2850 struct task_struct *task;
2851 int retval = -ENOENT;
2854 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2855 struct pid_namespace *ns;
2857 task = get_proc_task(inode);
2861 if (pid_alive(task)) {
2862 leader = task->group_leader;
2863 get_task_struct(leader);
2866 put_task_struct(task);
2874 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2879 ino = parent_ino(dentry);
2880 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2886 /* f_version caches the tgid value that the last readdir call couldn't
2887 * return. lseek aka telldir automagically resets f_version to 0.
2889 ns = filp->f_dentry->d_sb->s_fs_info;
2890 tid = (int)filp->f_version;
2891 filp->f_version = 0;
2892 for (task = first_tid(leader, tid, pos - 2, ns);
2894 task = next_tid(task), pos++) {
2895 tid = task_pid_nr_ns(task, ns);
2896 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2897 /* returning this tgid failed, save it as the first
2898 * pid for the next readir call */
2899 filp->f_version = (u64)tid;
2900 put_task_struct(task);
2906 put_task_struct(leader);
2911 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2913 struct inode *inode = dentry->d_inode;
2914 struct task_struct *p = get_proc_task(inode);
2915 generic_fillattr(inode, stat);
2919 stat->nlink += get_nr_threads(p);
2927 static const struct inode_operations proc_task_inode_operations = {
2928 .lookup = proc_task_lookup,
2929 .getattr = proc_task_getattr,
2930 .setattr = proc_setattr,
2933 static const struct file_operations proc_task_operations = {
2934 .read = generic_read_dir,
2935 .readdir = proc_task_readdir,