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,
1041 #ifdef CONFIG_FAULT_INJECTION
1042 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1043 size_t count, loff_t *ppos)
1045 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1046 char buffer[PROC_NUMBUF];
1052 make_it_fail = task->make_it_fail;
1053 put_task_struct(task);
1055 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1057 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1060 static ssize_t proc_fault_inject_write(struct file * file,
1061 const char __user * buf, size_t count, loff_t *ppos)
1063 struct task_struct *task;
1064 char buffer[PROC_NUMBUF], *end;
1067 if (!capable(CAP_SYS_RESOURCE))
1069 memset(buffer, 0, sizeof(buffer));
1070 if (count > sizeof(buffer) - 1)
1071 count = sizeof(buffer) - 1;
1072 if (copy_from_user(buffer, buf, count))
1074 make_it_fail = simple_strtol(buffer, &end, 0);
1077 task = get_proc_task(file->f_dentry->d_inode);
1080 task->make_it_fail = make_it_fail;
1081 put_task_struct(task);
1082 if (end - buffer == 0)
1084 return end - buffer;
1087 static const struct file_operations proc_fault_inject_operations = {
1088 .read = proc_fault_inject_read,
1089 .write = proc_fault_inject_write,
1094 #ifdef CONFIG_SCHED_DEBUG
1096 * Print out various scheduling related per-task fields:
1098 static int sched_show(struct seq_file *m, void *v)
1100 struct inode *inode = m->private;
1101 struct task_struct *p;
1105 p = get_proc_task(inode);
1108 proc_sched_show_task(p, m);
1116 sched_write(struct file *file, const char __user *buf,
1117 size_t count, loff_t *offset)
1119 struct inode *inode = file->f_path.dentry->d_inode;
1120 struct task_struct *p;
1124 p = get_proc_task(inode);
1127 proc_sched_set_task(p);
1134 static int sched_open(struct inode *inode, struct file *filp)
1138 ret = single_open(filp, sched_show, NULL);
1140 struct seq_file *m = filp->private_data;
1147 static const struct file_operations proc_pid_sched_operations = {
1150 .write = sched_write,
1151 .llseek = seq_lseek,
1152 .release = single_release,
1157 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1159 struct inode *inode = dentry->d_inode;
1160 int error = -EACCES;
1162 /* We don't need a base pointer in the /proc filesystem */
1163 path_put(&nd->path);
1165 /* Are we allowed to snoop on the tasks file descriptors? */
1166 if (!proc_fd_access_allowed(inode))
1169 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1170 nd->last_type = LAST_BIND;
1172 return ERR_PTR(error);
1175 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1177 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1184 pathname = d_path(path, tmp, PAGE_SIZE);
1185 len = PTR_ERR(pathname);
1186 if (IS_ERR(pathname))
1188 len = tmp + PAGE_SIZE - 1 - pathname;
1192 if (copy_to_user(buffer, pathname, len))
1195 free_page((unsigned long)tmp);
1199 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1201 int error = -EACCES;
1202 struct inode *inode = dentry->d_inode;
1205 /* Are we allowed to snoop on the tasks file descriptors? */
1206 if (!proc_fd_access_allowed(inode))
1209 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1213 error = do_proc_readlink(&path, buffer, buflen);
1219 static const struct inode_operations proc_pid_link_inode_operations = {
1220 .readlink = proc_pid_readlink,
1221 .follow_link = proc_pid_follow_link,
1222 .setattr = proc_setattr,
1226 /* building an inode */
1228 static int task_dumpable(struct task_struct *task)
1231 struct mm_struct *mm;
1236 dumpable = get_dumpable(mm);
1244 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1246 struct inode * inode;
1247 struct proc_inode *ei;
1249 /* We need a new inode */
1251 inode = new_inode(sb);
1257 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1258 inode->i_op = &proc_def_inode_operations;
1261 * grab the reference to task.
1263 ei->pid = get_task_pid(task, PIDTYPE_PID);
1269 if (task_dumpable(task)) {
1270 inode->i_uid = task->euid;
1271 inode->i_gid = task->egid;
1273 security_task_to_inode(task, inode);
1283 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1285 struct inode *inode = dentry->d_inode;
1286 struct task_struct *task;
1287 generic_fillattr(inode, stat);
1292 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1294 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1295 task_dumpable(task)) {
1296 stat->uid = task->euid;
1297 stat->gid = task->egid;
1307 * Exceptional case: normally we are not allowed to unhash a busy
1308 * directory. In this case, however, we can do it - no aliasing problems
1309 * due to the way we treat inodes.
1311 * Rewrite the inode's ownerships here because the owning task may have
1312 * performed a setuid(), etc.
1314 * Before the /proc/pid/status file was created the only way to read
1315 * the effective uid of a /process was to stat /proc/pid. Reading
1316 * /proc/pid/status is slow enough that procps and other packages
1317 * kept stating /proc/pid. To keep the rules in /proc simple I have
1318 * made this apply to all per process world readable and executable
1321 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1323 struct inode *inode = dentry->d_inode;
1324 struct task_struct *task = get_proc_task(inode);
1326 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1327 task_dumpable(task)) {
1328 inode->i_uid = task->euid;
1329 inode->i_gid = task->egid;
1334 inode->i_mode &= ~(S_ISUID | S_ISGID);
1335 security_task_to_inode(task, inode);
1336 put_task_struct(task);
1343 static int pid_delete_dentry(struct dentry * dentry)
1345 /* Is the task we represent dead?
1346 * If so, then don't put the dentry on the lru list,
1347 * kill it immediately.
1349 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1352 static struct dentry_operations pid_dentry_operations =
1354 .d_revalidate = pid_revalidate,
1355 .d_delete = pid_delete_dentry,
1360 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1361 struct task_struct *, const void *);
1364 * Fill a directory entry.
1366 * If possible create the dcache entry and derive our inode number and
1367 * file type from dcache entry.
1369 * Since all of the proc inode numbers are dynamically generated, the inode
1370 * numbers do not exist until the inode is cache. This means creating the
1371 * the dcache entry in readdir is necessary to keep the inode numbers
1372 * reported by readdir in sync with the inode numbers reported
1375 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1376 char *name, int len,
1377 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1379 struct dentry *child, *dir = filp->f_path.dentry;
1380 struct inode *inode;
1383 unsigned type = DT_UNKNOWN;
1387 qname.hash = full_name_hash(name, len);
1389 child = d_lookup(dir, &qname);
1392 new = d_alloc(dir, &qname);
1394 child = instantiate(dir->d_inode, new, task, ptr);
1401 if (!child || IS_ERR(child) || !child->d_inode)
1402 goto end_instantiate;
1403 inode = child->d_inode;
1406 type = inode->i_mode >> 12;
1411 ino = find_inode_number(dir, &qname);
1414 return filldir(dirent, name, len, filp->f_pos, ino, type);
1417 static unsigned name_to_int(struct dentry *dentry)
1419 const char *name = dentry->d_name.name;
1420 int len = dentry->d_name.len;
1423 if (len > 1 && *name == '0')
1426 unsigned c = *name++ - '0';
1429 if (n >= (~0U-9)/10)
1439 #define PROC_FDINFO_MAX 64
1441 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1443 struct task_struct *task = get_proc_task(inode);
1444 struct files_struct *files = NULL;
1446 int fd = proc_fd(inode);
1449 files = get_files_struct(task);
1450 put_task_struct(task);
1454 * We are not taking a ref to the file structure, so we must
1457 spin_lock(&files->file_lock);
1458 file = fcheck_files(files, fd);
1461 *path = file->f_path;
1462 path_get(&file->f_path);
1465 snprintf(info, PROC_FDINFO_MAX,
1468 (long long) file->f_pos,
1470 spin_unlock(&files->file_lock);
1471 put_files_struct(files);
1474 spin_unlock(&files->file_lock);
1475 put_files_struct(files);
1480 static int proc_fd_link(struct inode *inode, struct path *path)
1482 return proc_fd_info(inode, path, NULL);
1485 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1487 struct inode *inode = dentry->d_inode;
1488 struct task_struct *task = get_proc_task(inode);
1489 int fd = proc_fd(inode);
1490 struct files_struct *files;
1493 files = get_files_struct(task);
1496 if (fcheck_files(files, fd)) {
1498 put_files_struct(files);
1499 if (task_dumpable(task)) {
1500 inode->i_uid = task->euid;
1501 inode->i_gid = task->egid;
1506 inode->i_mode &= ~(S_ISUID | S_ISGID);
1507 security_task_to_inode(task, inode);
1508 put_task_struct(task);
1512 put_files_struct(files);
1514 put_task_struct(task);
1520 static struct dentry_operations tid_fd_dentry_operations =
1522 .d_revalidate = tid_fd_revalidate,
1523 .d_delete = pid_delete_dentry,
1526 static struct dentry *proc_fd_instantiate(struct inode *dir,
1527 struct dentry *dentry, struct task_struct *task, const void *ptr)
1529 unsigned fd = *(const unsigned *)ptr;
1531 struct files_struct *files;
1532 struct inode *inode;
1533 struct proc_inode *ei;
1534 struct dentry *error = ERR_PTR(-ENOENT);
1536 inode = proc_pid_make_inode(dir->i_sb, task);
1541 files = get_files_struct(task);
1544 inode->i_mode = S_IFLNK;
1547 * We are not taking a ref to the file structure, so we must
1550 spin_lock(&files->file_lock);
1551 file = fcheck_files(files, fd);
1554 if (file->f_mode & 1)
1555 inode->i_mode |= S_IRUSR | S_IXUSR;
1556 if (file->f_mode & 2)
1557 inode->i_mode |= S_IWUSR | S_IXUSR;
1558 spin_unlock(&files->file_lock);
1559 put_files_struct(files);
1561 inode->i_op = &proc_pid_link_inode_operations;
1563 ei->op.proc_get_link = proc_fd_link;
1564 dentry->d_op = &tid_fd_dentry_operations;
1565 d_add(dentry, inode);
1566 /* Close the race of the process dying before we return the dentry */
1567 if (tid_fd_revalidate(dentry, NULL))
1573 spin_unlock(&files->file_lock);
1574 put_files_struct(files);
1580 static struct dentry *proc_lookupfd_common(struct inode *dir,
1581 struct dentry *dentry,
1582 instantiate_t instantiate)
1584 struct task_struct *task = get_proc_task(dir);
1585 unsigned fd = name_to_int(dentry);
1586 struct dentry *result = ERR_PTR(-ENOENT);
1593 result = instantiate(dir, dentry, task, &fd);
1595 put_task_struct(task);
1600 static int proc_readfd_common(struct file * filp, void * dirent,
1601 filldir_t filldir, instantiate_t instantiate)
1603 struct dentry *dentry = filp->f_path.dentry;
1604 struct inode *inode = dentry->d_inode;
1605 struct task_struct *p = get_proc_task(inode);
1606 unsigned int fd, ino;
1608 struct files_struct * files;
1609 struct fdtable *fdt;
1619 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1623 ino = parent_ino(dentry);
1624 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1628 files = get_files_struct(p);
1632 fdt = files_fdtable(files);
1633 for (fd = filp->f_pos-2;
1635 fd++, filp->f_pos++) {
1636 char name[PROC_NUMBUF];
1639 if (!fcheck_files(files, fd))
1643 len = snprintf(name, sizeof(name), "%d", fd);
1644 if (proc_fill_cache(filp, dirent, filldir,
1645 name, len, instantiate,
1653 put_files_struct(files);
1661 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1662 struct nameidata *nd)
1664 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1667 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1669 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1672 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1673 size_t len, loff_t *ppos)
1675 char tmp[PROC_FDINFO_MAX];
1676 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1678 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1682 static const struct file_operations proc_fdinfo_file_operations = {
1683 .open = nonseekable_open,
1684 .read = proc_fdinfo_read,
1687 static const struct file_operations proc_fd_operations = {
1688 .read = generic_read_dir,
1689 .readdir = proc_readfd,
1693 * /proc/pid/fd needs a special permission handler so that a process can still
1694 * access /proc/self/fd after it has executed a setuid().
1696 static int proc_fd_permission(struct inode *inode, int mask,
1697 struct nameidata *nd)
1701 rv = generic_permission(inode, mask, NULL);
1704 if (task_pid(current) == proc_pid(inode))
1710 * proc directories can do almost nothing..
1712 static const struct inode_operations proc_fd_inode_operations = {
1713 .lookup = proc_lookupfd,
1714 .permission = proc_fd_permission,
1715 .setattr = proc_setattr,
1718 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1719 struct dentry *dentry, struct task_struct *task, const void *ptr)
1721 unsigned fd = *(unsigned *)ptr;
1722 struct inode *inode;
1723 struct proc_inode *ei;
1724 struct dentry *error = ERR_PTR(-ENOENT);
1726 inode = proc_pid_make_inode(dir->i_sb, task);
1731 inode->i_mode = S_IFREG | S_IRUSR;
1732 inode->i_fop = &proc_fdinfo_file_operations;
1733 dentry->d_op = &tid_fd_dentry_operations;
1734 d_add(dentry, inode);
1735 /* Close the race of the process dying before we return the dentry */
1736 if (tid_fd_revalidate(dentry, NULL))
1743 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1744 struct dentry *dentry,
1745 struct nameidata *nd)
1747 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1750 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1752 return proc_readfd_common(filp, dirent, filldir,
1753 proc_fdinfo_instantiate);
1756 static const struct file_operations proc_fdinfo_operations = {
1757 .read = generic_read_dir,
1758 .readdir = proc_readfdinfo,
1762 * proc directories can do almost nothing..
1764 static const struct inode_operations proc_fdinfo_inode_operations = {
1765 .lookup = proc_lookupfdinfo,
1766 .setattr = proc_setattr,
1770 static struct dentry *proc_pident_instantiate(struct inode *dir,
1771 struct dentry *dentry, struct task_struct *task, const void *ptr)
1773 const struct pid_entry *p = ptr;
1774 struct inode *inode;
1775 struct proc_inode *ei;
1776 struct dentry *error = ERR_PTR(-EINVAL);
1778 inode = proc_pid_make_inode(dir->i_sb, task);
1783 inode->i_mode = p->mode;
1784 if (S_ISDIR(inode->i_mode))
1785 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1787 inode->i_op = p->iop;
1789 inode->i_fop = p->fop;
1791 dentry->d_op = &pid_dentry_operations;
1792 d_add(dentry, inode);
1793 /* Close the race of the process dying before we return the dentry */
1794 if (pid_revalidate(dentry, NULL))
1800 static struct dentry *proc_pident_lookup(struct inode *dir,
1801 struct dentry *dentry,
1802 const struct pid_entry *ents,
1805 struct inode *inode;
1806 struct dentry *error;
1807 struct task_struct *task = get_proc_task(dir);
1808 const struct pid_entry *p, *last;
1810 error = ERR_PTR(-ENOENT);
1817 * Yes, it does not scale. And it should not. Don't add
1818 * new entries into /proc/<tgid>/ without very good reasons.
1820 last = &ents[nents - 1];
1821 for (p = ents; p <= last; p++) {
1822 if (p->len != dentry->d_name.len)
1824 if (!memcmp(dentry->d_name.name, p->name, p->len))
1830 error = proc_pident_instantiate(dir, dentry, task, p);
1832 put_task_struct(task);
1837 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1838 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1840 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1841 proc_pident_instantiate, task, p);
1844 static int proc_pident_readdir(struct file *filp,
1845 void *dirent, filldir_t filldir,
1846 const struct pid_entry *ents, unsigned int nents)
1849 struct dentry *dentry = filp->f_path.dentry;
1850 struct inode *inode = dentry->d_inode;
1851 struct task_struct *task = get_proc_task(inode);
1852 const struct pid_entry *p, *last;
1865 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1871 ino = parent_ino(dentry);
1872 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1884 last = &ents[nents - 1];
1886 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1895 put_task_struct(task);
1900 #ifdef CONFIG_SECURITY
1901 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1902 size_t count, loff_t *ppos)
1904 struct inode * inode = file->f_path.dentry->d_inode;
1907 struct task_struct *task = get_proc_task(inode);
1912 length = security_getprocattr(task,
1913 (char*)file->f_path.dentry->d_name.name,
1915 put_task_struct(task);
1917 length = simple_read_from_buffer(buf, count, ppos, p, length);
1922 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1923 size_t count, loff_t *ppos)
1925 struct inode * inode = file->f_path.dentry->d_inode;
1928 struct task_struct *task = get_proc_task(inode);
1933 if (count > PAGE_SIZE)
1936 /* No partial writes. */
1942 page = (char*)__get_free_page(GFP_TEMPORARY);
1947 if (copy_from_user(page, buf, count))
1950 length = security_setprocattr(task,
1951 (char*)file->f_path.dentry->d_name.name,
1952 (void*)page, count);
1954 free_page((unsigned long) page);
1956 put_task_struct(task);
1961 static const struct file_operations proc_pid_attr_operations = {
1962 .read = proc_pid_attr_read,
1963 .write = proc_pid_attr_write,
1966 static const struct pid_entry attr_dir_stuff[] = {
1967 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1968 REG("prev", S_IRUGO, pid_attr),
1969 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1970 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1971 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1972 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1975 static int proc_attr_dir_readdir(struct file * filp,
1976 void * dirent, filldir_t filldir)
1978 return proc_pident_readdir(filp,dirent,filldir,
1979 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1982 static const struct file_operations proc_attr_dir_operations = {
1983 .read = generic_read_dir,
1984 .readdir = proc_attr_dir_readdir,
1987 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1988 struct dentry *dentry, struct nameidata *nd)
1990 return proc_pident_lookup(dir, dentry,
1991 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1994 static const struct inode_operations proc_attr_dir_inode_operations = {
1995 .lookup = proc_attr_dir_lookup,
1996 .getattr = pid_getattr,
1997 .setattr = proc_setattr,
2002 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2003 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2004 size_t count, loff_t *ppos)
2006 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2007 struct mm_struct *mm;
2008 char buffer[PROC_NUMBUF];
2016 mm = get_task_mm(task);
2018 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2019 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2020 MMF_DUMP_FILTER_SHIFT));
2022 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2025 put_task_struct(task);
2030 static ssize_t proc_coredump_filter_write(struct file *file,
2031 const char __user *buf,
2035 struct task_struct *task;
2036 struct mm_struct *mm;
2037 char buffer[PROC_NUMBUF], *end;
2044 memset(buffer, 0, sizeof(buffer));
2045 if (count > sizeof(buffer) - 1)
2046 count = sizeof(buffer) - 1;
2047 if (copy_from_user(buffer, buf, count))
2051 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2054 if (end - buffer == 0)
2058 task = get_proc_task(file->f_dentry->d_inode);
2063 mm = get_task_mm(task);
2067 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2069 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2071 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2076 put_task_struct(task);
2081 static const struct file_operations proc_coredump_filter_operations = {
2082 .read = proc_coredump_filter_read,
2083 .write = proc_coredump_filter_write,
2090 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2093 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2094 pid_t tgid = task_tgid_nr_ns(current, ns);
2095 char tmp[PROC_NUMBUF];
2098 sprintf(tmp, "%d", tgid);
2099 return vfs_readlink(dentry,buffer,buflen,tmp);
2102 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2104 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2105 pid_t tgid = task_tgid_nr_ns(current, ns);
2106 char tmp[PROC_NUMBUF];
2108 return ERR_PTR(-ENOENT);
2109 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2110 return ERR_PTR(vfs_follow_link(nd,tmp));
2113 static const struct inode_operations proc_self_inode_operations = {
2114 .readlink = proc_self_readlink,
2115 .follow_link = proc_self_follow_link,
2121 * These are the directory entries in the root directory of /proc
2122 * that properly belong to the /proc filesystem, as they describe
2123 * describe something that is process related.
2125 static const struct pid_entry proc_base_stuff[] = {
2126 NOD("self", S_IFLNK|S_IRWXUGO,
2127 &proc_self_inode_operations, NULL, {}),
2131 * Exceptional case: normally we are not allowed to unhash a busy
2132 * directory. In this case, however, we can do it - no aliasing problems
2133 * due to the way we treat inodes.
2135 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2137 struct inode *inode = dentry->d_inode;
2138 struct task_struct *task = get_proc_task(inode);
2140 put_task_struct(task);
2147 static struct dentry_operations proc_base_dentry_operations =
2149 .d_revalidate = proc_base_revalidate,
2150 .d_delete = pid_delete_dentry,
2153 static struct dentry *proc_base_instantiate(struct inode *dir,
2154 struct dentry *dentry, struct task_struct *task, const void *ptr)
2156 const struct pid_entry *p = ptr;
2157 struct inode *inode;
2158 struct proc_inode *ei;
2159 struct dentry *error = ERR_PTR(-EINVAL);
2161 /* Allocate the inode */
2162 error = ERR_PTR(-ENOMEM);
2163 inode = new_inode(dir->i_sb);
2167 /* Initialize the inode */
2169 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2172 * grab the reference to the task.
2174 ei->pid = get_task_pid(task, PIDTYPE_PID);
2180 inode->i_mode = p->mode;
2181 if (S_ISDIR(inode->i_mode))
2183 if (S_ISLNK(inode->i_mode))
2186 inode->i_op = p->iop;
2188 inode->i_fop = p->fop;
2190 dentry->d_op = &proc_base_dentry_operations;
2191 d_add(dentry, inode);
2200 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2202 struct dentry *error;
2203 struct task_struct *task = get_proc_task(dir);
2204 const struct pid_entry *p, *last;
2206 error = ERR_PTR(-ENOENT);
2211 /* Lookup the directory entry */
2212 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2213 for (p = proc_base_stuff; p <= last; p++) {
2214 if (p->len != dentry->d_name.len)
2216 if (!memcmp(dentry->d_name.name, p->name, p->len))
2222 error = proc_base_instantiate(dir, dentry, task, p);
2225 put_task_struct(task);
2230 static int proc_base_fill_cache(struct file *filp, void *dirent,
2231 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2233 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2234 proc_base_instantiate, task, p);
2237 #ifdef CONFIG_TASK_IO_ACCOUNTING
2238 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2240 return sprintf(buffer,
2241 #ifdef CONFIG_TASK_XACCT
2247 "read_bytes: %llu\n"
2248 "write_bytes: %llu\n"
2249 "cancelled_write_bytes: %llu\n",
2250 #ifdef CONFIG_TASK_XACCT
2251 (unsigned long long)task->rchar,
2252 (unsigned long long)task->wchar,
2253 (unsigned long long)task->syscr,
2254 (unsigned long long)task->syscw,
2256 (unsigned long long)task->ioac.read_bytes,
2257 (unsigned long long)task->ioac.write_bytes,
2258 (unsigned long long)task->ioac.cancelled_write_bytes);
2265 static const struct file_operations proc_task_operations;
2266 static const struct inode_operations proc_task_inode_operations;
2268 static const struct pid_entry tgid_base_stuff[] = {
2269 DIR("task", S_IRUGO|S_IXUGO, task),
2270 DIR("fd", S_IRUSR|S_IXUSR, fd),
2271 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2272 REG("environ", S_IRUSR, environ),
2273 INF("auxv", S_IRUSR, pid_auxv),
2274 ONE("status", S_IRUGO, pid_status),
2275 INF("limits", S_IRUSR, pid_limits),
2276 #ifdef CONFIG_SCHED_DEBUG
2277 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2279 INF("cmdline", S_IRUGO, pid_cmdline),
2280 ONE("stat", S_IRUGO, tgid_stat),
2281 ONE("statm", S_IRUGO, pid_statm),
2282 REG("maps", S_IRUGO, maps),
2284 REG("numa_maps", S_IRUGO, numa_maps),
2286 REG("mem", S_IRUSR|S_IWUSR, mem),
2290 REG("mounts", S_IRUGO, mounts),
2291 REG("mountstats", S_IRUSR, mountstats),
2292 #ifdef CONFIG_PROC_PAGE_MONITOR
2293 REG("clear_refs", S_IWUSR, clear_refs),
2294 REG("smaps", S_IRUGO, smaps),
2295 REG("pagemap", S_IRUSR, pagemap),
2297 #ifdef CONFIG_SECURITY
2298 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2300 #ifdef CONFIG_KALLSYMS
2301 INF("wchan", S_IRUGO, pid_wchan),
2303 #ifdef CONFIG_SCHEDSTATS
2304 INF("schedstat", S_IRUGO, pid_schedstat),
2306 #ifdef CONFIG_LATENCYTOP
2307 REG("latency", S_IRUGO, lstats),
2309 #ifdef CONFIG_PROC_PID_CPUSET
2310 REG("cpuset", S_IRUGO, cpuset),
2312 #ifdef CONFIG_CGROUPS
2313 REG("cgroup", S_IRUGO, cgroup),
2315 INF("oom_score", S_IRUGO, oom_score),
2316 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2317 #ifdef CONFIG_AUDITSYSCALL
2318 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2320 #ifdef CONFIG_FAULT_INJECTION
2321 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2323 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2324 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2326 #ifdef CONFIG_TASK_IO_ACCOUNTING
2327 INF("io", S_IRUGO, pid_io_accounting),
2331 static int proc_tgid_base_readdir(struct file * filp,
2332 void * dirent, filldir_t filldir)
2334 return proc_pident_readdir(filp,dirent,filldir,
2335 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2338 static const struct file_operations proc_tgid_base_operations = {
2339 .read = generic_read_dir,
2340 .readdir = proc_tgid_base_readdir,
2343 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2344 return proc_pident_lookup(dir, dentry,
2345 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2348 static const struct inode_operations proc_tgid_base_inode_operations = {
2349 .lookup = proc_tgid_base_lookup,
2350 .getattr = pid_getattr,
2351 .setattr = proc_setattr,
2354 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2356 struct dentry *dentry, *leader, *dir;
2357 char buf[PROC_NUMBUF];
2361 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2362 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2364 if (!(current->flags & PF_EXITING))
2365 shrink_dcache_parent(dentry);
2374 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2375 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2380 name.len = strlen(name.name);
2381 dir = d_hash_and_lookup(leader, &name);
2383 goto out_put_leader;
2386 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2387 dentry = d_hash_and_lookup(dir, &name);
2389 shrink_dcache_parent(dentry);
2402 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2403 * @task: task that should be flushed.
2405 * When flushing dentries from proc, one needs to flush them from global
2406 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2407 * in. This call is supposed to do all of this job.
2409 * Looks in the dcache for
2411 * /proc/@tgid/task/@pid
2412 * if either directory is present flushes it and all of it'ts children
2415 * It is safe and reasonable to cache /proc entries for a task until
2416 * that task exits. After that they just clog up the dcache with
2417 * useless entries, possibly causing useful dcache entries to be
2418 * flushed instead. This routine is proved to flush those useless
2419 * dcache entries at process exit time.
2421 * NOTE: This routine is just an optimization so it does not guarantee
2422 * that no dcache entries will exist at process exit time it
2423 * just makes it very unlikely that any will persist.
2426 void proc_flush_task(struct task_struct *task)
2429 struct pid *pid, *tgid = NULL;
2432 pid = task_pid(task);
2433 if (thread_group_leader(task))
2434 tgid = task_tgid(task);
2436 for (i = 0; i <= pid->level; i++) {
2437 upid = &pid->numbers[i];
2438 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2439 tgid ? tgid->numbers[i].nr : 0);
2442 upid = &pid->numbers[pid->level];
2444 pid_ns_release_proc(upid->ns);
2447 static struct dentry *proc_pid_instantiate(struct inode *dir,
2448 struct dentry * dentry,
2449 struct task_struct *task, const void *ptr)
2451 struct dentry *error = ERR_PTR(-ENOENT);
2452 struct inode *inode;
2454 inode = proc_pid_make_inode(dir->i_sb, task);
2458 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2459 inode->i_op = &proc_tgid_base_inode_operations;
2460 inode->i_fop = &proc_tgid_base_operations;
2461 inode->i_flags|=S_IMMUTABLE;
2463 #ifdef CONFIG_SECURITY
2464 inode->i_nlink += 1;
2467 dentry->d_op = &pid_dentry_operations;
2469 d_add(dentry, inode);
2470 /* Close the race of the process dying before we return the dentry */
2471 if (pid_revalidate(dentry, NULL))
2477 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2479 struct dentry *result = ERR_PTR(-ENOENT);
2480 struct task_struct *task;
2482 struct pid_namespace *ns;
2484 result = proc_base_lookup(dir, dentry);
2485 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2488 tgid = name_to_int(dentry);
2492 ns = dentry->d_sb->s_fs_info;
2494 task = find_task_by_pid_ns(tgid, ns);
2496 get_task_struct(task);
2501 result = proc_pid_instantiate(dir, dentry, task, NULL);
2502 put_task_struct(task);
2508 * Find the first task with tgid >= tgid
2513 struct task_struct *task;
2515 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2520 put_task_struct(iter.task);
2524 pid = find_ge_pid(iter.tgid, ns);
2526 iter.tgid = pid_nr_ns(pid, ns);
2527 iter.task = pid_task(pid, PIDTYPE_PID);
2528 /* What we to know is if the pid we have find is the
2529 * pid of a thread_group_leader. Testing for task
2530 * being a thread_group_leader is the obvious thing
2531 * todo but there is a window when it fails, due to
2532 * the pid transfer logic in de_thread.
2534 * So we perform the straight forward test of seeing
2535 * if the pid we have found is the pid of a thread
2536 * group leader, and don't worry if the task we have
2537 * found doesn't happen to be a thread group leader.
2538 * As we don't care in the case of readdir.
2540 if (!iter.task || !has_group_leader_pid(iter.task)) {
2544 get_task_struct(iter.task);
2550 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2552 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2553 struct tgid_iter iter)
2555 char name[PROC_NUMBUF];
2556 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2557 return proc_fill_cache(filp, dirent, filldir, name, len,
2558 proc_pid_instantiate, iter.task, NULL);
2561 /* for the /proc/ directory itself, after non-process stuff has been done */
2562 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2564 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2565 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2566 struct tgid_iter iter;
2567 struct pid_namespace *ns;
2572 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2573 const struct pid_entry *p = &proc_base_stuff[nr];
2574 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2578 ns = filp->f_dentry->d_sb->s_fs_info;
2580 iter.tgid = filp->f_pos - TGID_OFFSET;
2581 for (iter = next_tgid(ns, iter);
2583 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2584 filp->f_pos = iter.tgid + TGID_OFFSET;
2585 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2586 put_task_struct(iter.task);
2590 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2592 put_task_struct(reaper);
2600 static const struct pid_entry tid_base_stuff[] = {
2601 DIR("fd", S_IRUSR|S_IXUSR, fd),
2602 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2603 REG("environ", S_IRUSR, environ),
2604 INF("auxv", S_IRUSR, pid_auxv),
2605 ONE("status", S_IRUGO, pid_status),
2606 INF("limits", S_IRUSR, pid_limits),
2607 #ifdef CONFIG_SCHED_DEBUG
2608 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2610 INF("cmdline", S_IRUGO, pid_cmdline),
2611 ONE("stat", S_IRUGO, tid_stat),
2612 ONE("statm", S_IRUGO, pid_statm),
2613 REG("maps", S_IRUGO, maps),
2615 REG("numa_maps", S_IRUGO, numa_maps),
2617 REG("mem", S_IRUSR|S_IWUSR, mem),
2621 REG("mounts", S_IRUGO, mounts),
2622 #ifdef CONFIG_PROC_PAGE_MONITOR
2623 REG("clear_refs", S_IWUSR, clear_refs),
2624 REG("smaps", S_IRUGO, smaps),
2625 REG("pagemap", S_IRUSR, pagemap),
2627 #ifdef CONFIG_SECURITY
2628 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2630 #ifdef CONFIG_KALLSYMS
2631 INF("wchan", S_IRUGO, pid_wchan),
2633 #ifdef CONFIG_SCHEDSTATS
2634 INF("schedstat", S_IRUGO, pid_schedstat),
2636 #ifdef CONFIG_LATENCYTOP
2637 REG("latency", S_IRUGO, lstats),
2639 #ifdef CONFIG_PROC_PID_CPUSET
2640 REG("cpuset", S_IRUGO, cpuset),
2642 #ifdef CONFIG_CGROUPS
2643 REG("cgroup", S_IRUGO, cgroup),
2645 INF("oom_score", S_IRUGO, oom_score),
2646 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2647 #ifdef CONFIG_AUDITSYSCALL
2648 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2650 #ifdef CONFIG_FAULT_INJECTION
2651 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2655 static int proc_tid_base_readdir(struct file * filp,
2656 void * dirent, filldir_t filldir)
2658 return proc_pident_readdir(filp,dirent,filldir,
2659 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2662 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2663 return proc_pident_lookup(dir, dentry,
2664 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2667 static const struct file_operations proc_tid_base_operations = {
2668 .read = generic_read_dir,
2669 .readdir = proc_tid_base_readdir,
2672 static const struct inode_operations proc_tid_base_inode_operations = {
2673 .lookup = proc_tid_base_lookup,
2674 .getattr = pid_getattr,
2675 .setattr = proc_setattr,
2678 static struct dentry *proc_task_instantiate(struct inode *dir,
2679 struct dentry *dentry, struct task_struct *task, const void *ptr)
2681 struct dentry *error = ERR_PTR(-ENOENT);
2682 struct inode *inode;
2683 inode = proc_pid_make_inode(dir->i_sb, task);
2687 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2688 inode->i_op = &proc_tid_base_inode_operations;
2689 inode->i_fop = &proc_tid_base_operations;
2690 inode->i_flags|=S_IMMUTABLE;
2692 #ifdef CONFIG_SECURITY
2693 inode->i_nlink += 1;
2696 dentry->d_op = &pid_dentry_operations;
2698 d_add(dentry, inode);
2699 /* Close the race of the process dying before we return the dentry */
2700 if (pid_revalidate(dentry, NULL))
2706 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2708 struct dentry *result = ERR_PTR(-ENOENT);
2709 struct task_struct *task;
2710 struct task_struct *leader = get_proc_task(dir);
2712 struct pid_namespace *ns;
2717 tid = name_to_int(dentry);
2721 ns = dentry->d_sb->s_fs_info;
2723 task = find_task_by_pid_ns(tid, ns);
2725 get_task_struct(task);
2729 if (!same_thread_group(leader, task))
2732 result = proc_task_instantiate(dir, dentry, task, NULL);
2734 put_task_struct(task);
2736 put_task_struct(leader);
2742 * Find the first tid of a thread group to return to user space.
2744 * Usually this is just the thread group leader, but if the users
2745 * buffer was too small or there was a seek into the middle of the
2746 * directory we have more work todo.
2748 * In the case of a short read we start with find_task_by_pid.
2750 * In the case of a seek we start with the leader and walk nr
2753 static struct task_struct *first_tid(struct task_struct *leader,
2754 int tid, int nr, struct pid_namespace *ns)
2756 struct task_struct *pos;
2759 /* Attempt to start with the pid of a thread */
2760 if (tid && (nr > 0)) {
2761 pos = find_task_by_pid_ns(tid, ns);
2762 if (pos && (pos->group_leader == leader))
2766 /* If nr exceeds the number of threads there is nothing todo */
2768 if (nr && nr >= get_nr_threads(leader))
2771 /* If we haven't found our starting place yet start
2772 * with the leader and walk nr threads forward.
2774 for (pos = leader; nr > 0; --nr) {
2775 pos = next_thread(pos);
2776 if (pos == leader) {
2782 get_task_struct(pos);
2789 * Find the next thread in the thread list.
2790 * Return NULL if there is an error or no next thread.
2792 * The reference to the input task_struct is released.
2794 static struct task_struct *next_tid(struct task_struct *start)
2796 struct task_struct *pos = NULL;
2798 if (pid_alive(start)) {
2799 pos = next_thread(start);
2800 if (thread_group_leader(pos))
2803 get_task_struct(pos);
2806 put_task_struct(start);
2810 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2811 struct task_struct *task, int tid)
2813 char name[PROC_NUMBUF];
2814 int len = snprintf(name, sizeof(name), "%d", tid);
2815 return proc_fill_cache(filp, dirent, filldir, name, len,
2816 proc_task_instantiate, task, NULL);
2819 /* for the /proc/TGID/task/ directories */
2820 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2822 struct dentry *dentry = filp->f_path.dentry;
2823 struct inode *inode = dentry->d_inode;
2824 struct task_struct *leader = NULL;
2825 struct task_struct *task;
2826 int retval = -ENOENT;
2829 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2830 struct pid_namespace *ns;
2832 task = get_proc_task(inode);
2836 if (pid_alive(task)) {
2837 leader = task->group_leader;
2838 get_task_struct(leader);
2841 put_task_struct(task);
2849 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2854 ino = parent_ino(dentry);
2855 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2861 /* f_version caches the tgid value that the last readdir call couldn't
2862 * return. lseek aka telldir automagically resets f_version to 0.
2864 ns = filp->f_dentry->d_sb->s_fs_info;
2865 tid = (int)filp->f_version;
2866 filp->f_version = 0;
2867 for (task = first_tid(leader, tid, pos - 2, ns);
2869 task = next_tid(task), pos++) {
2870 tid = task_pid_nr_ns(task, ns);
2871 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2872 /* returning this tgid failed, save it as the first
2873 * pid for the next readir call */
2874 filp->f_version = (u64)tid;
2875 put_task_struct(task);
2881 put_task_struct(leader);
2886 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2888 struct inode *inode = dentry->d_inode;
2889 struct task_struct *p = get_proc_task(inode);
2890 generic_fillattr(inode, stat);
2894 stat->nlink += get_nr_threads(p);
2902 static const struct inode_operations proc_task_inode_operations = {
2903 .lookup = proc_task_lookup,
2904 .getattr = proc_task_getattr,
2905 .setattr = proc_setattr,
2908 static const struct file_operations proc_task_operations = {
2909 .read = generic_read_dir,
2910 .readdir = proc_task_readdir,