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 task_struct *task = m->private;
318 seq_puts(m, "Latency Top version : v0.1\n");
320 for (i = 0; i < 32; i++) {
321 if (task->latency_record[i].backtrace[0]) {
323 seq_printf(m, "%i %li %li ",
324 task->latency_record[i].count,
325 task->latency_record[i].time,
326 task->latency_record[i].max);
327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
328 char sym[KSYM_NAME_LEN];
330 if (!task->latency_record[i].backtrace[q])
332 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
334 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
335 c = strchr(sym, '+');
338 seq_printf(m, "%s ", sym);
347 static int lstats_open(struct inode *inode, struct file *file)
351 struct task_struct *task = get_proc_task(inode);
353 ret = single_open(file, lstats_show_proc, NULL);
355 m = file->private_data;
361 static ssize_t lstats_write(struct file *file, const char __user *buf,
362 size_t count, loff_t *offs)
365 struct task_struct *task;
367 m = file->private_data;
369 clear_all_latency_tracing(task);
374 static const struct file_operations proc_lstats_operations = {
377 .write = lstats_write,
379 .release = single_release,
384 /* The badness from the OOM killer */
385 unsigned long badness(struct task_struct *p, unsigned long uptime);
386 static int proc_oom_score(struct task_struct *task, char *buffer)
388 unsigned long points;
389 struct timespec uptime;
391 do_posix_clock_monotonic_gettime(&uptime);
392 read_lock(&tasklist_lock);
393 points = badness(task, uptime.tv_sec);
394 read_unlock(&tasklist_lock);
395 return sprintf(buffer, "%lu\n", points);
403 static const struct limit_names lnames[RLIM_NLIMITS] = {
404 [RLIMIT_CPU] = {"Max cpu time", "ms"},
405 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
406 [RLIMIT_DATA] = {"Max data size", "bytes"},
407 [RLIMIT_STACK] = {"Max stack size", "bytes"},
408 [RLIMIT_CORE] = {"Max core file size", "bytes"},
409 [RLIMIT_RSS] = {"Max resident set", "bytes"},
410 [RLIMIT_NPROC] = {"Max processes", "processes"},
411 [RLIMIT_NOFILE] = {"Max open files", "files"},
412 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
413 [RLIMIT_AS] = {"Max address space", "bytes"},
414 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
415 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
416 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
417 [RLIMIT_NICE] = {"Max nice priority", NULL},
418 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
419 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
422 /* Display limits for a process */
423 static int proc_pid_limits(struct task_struct *task, char *buffer)
428 char *bufptr = buffer;
430 struct rlimit rlim[RLIM_NLIMITS];
433 if (!lock_task_sighand(task,&flags)) {
437 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
438 unlock_task_sighand(task, &flags);
442 * print the file header
444 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
445 "Limit", "Soft Limit", "Hard Limit", "Units");
447 for (i = 0; i < RLIM_NLIMITS; i++) {
448 if (rlim[i].rlim_cur == RLIM_INFINITY)
449 count += sprintf(&bufptr[count], "%-25s %-20s ",
450 lnames[i].name, "unlimited");
452 count += sprintf(&bufptr[count], "%-25s %-20lu ",
453 lnames[i].name, rlim[i].rlim_cur);
455 if (rlim[i].rlim_max == RLIM_INFINITY)
456 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
458 count += sprintf(&bufptr[count], "%-20lu ",
462 count += sprintf(&bufptr[count], "%-10s\n",
465 count += sprintf(&bufptr[count], "\n");
471 /************************************************************************/
472 /* Here the fs part begins */
473 /************************************************************************/
475 /* permission checks */
476 static int proc_fd_access_allowed(struct inode *inode)
478 struct task_struct *task;
480 /* Allow access to a task's file descriptors if it is us or we
481 * may use ptrace attach to the process and find out that
484 task = get_proc_task(inode);
486 allowed = ptrace_may_attach(task);
487 put_task_struct(task);
492 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
495 struct inode *inode = dentry->d_inode;
497 if (attr->ia_valid & ATTR_MODE)
500 error = inode_change_ok(inode, attr);
502 error = inode_setattr(inode, attr);
506 static const struct inode_operations proc_def_inode_operations = {
507 .setattr = proc_setattr,
510 extern const struct seq_operations mounts_op;
516 static int mounts_open(struct inode *inode, struct file *file)
518 struct task_struct *task = get_proc_task(inode);
520 struct mnt_namespace *ns = NULL;
521 struct proc_mounts *p;
526 nsp = task_nsproxy(task);
534 put_task_struct(task);
539 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
541 file->private_data = &p->m;
542 ret = seq_open(file, &mounts_op);
545 p->event = ns->event;
555 static int mounts_release(struct inode *inode, struct file *file)
557 struct seq_file *m = file->private_data;
558 struct mnt_namespace *ns = m->private;
560 return seq_release(inode, file);
563 static unsigned mounts_poll(struct file *file, poll_table *wait)
565 struct proc_mounts *p = file->private_data;
566 struct mnt_namespace *ns = p->m.private;
569 poll_wait(file, &ns->poll, wait);
571 spin_lock(&vfsmount_lock);
572 if (p->event != ns->event) {
573 p->event = ns->event;
576 spin_unlock(&vfsmount_lock);
581 static const struct file_operations proc_mounts_operations = {
585 .release = mounts_release,
589 extern const struct seq_operations mountstats_op;
590 static int mountstats_open(struct inode *inode, struct file *file)
592 int ret = seq_open(file, &mountstats_op);
595 struct seq_file *m = file->private_data;
597 struct mnt_namespace *mnt_ns = NULL;
598 struct task_struct *task = get_proc_task(inode);
602 nsp = task_nsproxy(task);
604 mnt_ns = nsp->mnt_ns;
610 put_task_struct(task);
616 seq_release(inode, file);
623 static const struct file_operations proc_mountstats_operations = {
624 .open = mountstats_open,
627 .release = mounts_release,
630 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
632 static ssize_t proc_info_read(struct file * file, char __user * buf,
633 size_t count, loff_t *ppos)
635 struct inode * inode = file->f_path.dentry->d_inode;
638 struct task_struct *task = get_proc_task(inode);
644 if (count > PROC_BLOCK_SIZE)
645 count = PROC_BLOCK_SIZE;
648 if (!(page = __get_free_page(GFP_TEMPORARY)))
651 length = PROC_I(inode)->op.proc_read(task, (char*)page);
654 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
657 put_task_struct(task);
662 static const struct file_operations proc_info_file_operations = {
663 .read = proc_info_read,
666 static int proc_single_show(struct seq_file *m, void *v)
668 struct inode *inode = m->private;
669 struct pid_namespace *ns;
671 struct task_struct *task;
674 ns = inode->i_sb->s_fs_info;
675 pid = proc_pid(inode);
676 task = get_pid_task(pid, PIDTYPE_PID);
680 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
682 put_task_struct(task);
686 static int proc_single_open(struct inode *inode, struct file *filp)
689 ret = single_open(filp, proc_single_show, NULL);
691 struct seq_file *m = filp->private_data;
698 static const struct file_operations proc_single_file_operations = {
699 .open = proc_single_open,
702 .release = single_release,
705 static int mem_open(struct inode* inode, struct file* file)
707 file->private_data = (void*)((long)current->self_exec_id);
711 static ssize_t mem_read(struct file * file, char __user * buf,
712 size_t count, loff_t *ppos)
714 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
716 unsigned long src = *ppos;
718 struct mm_struct *mm;
723 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
727 page = (char *)__get_free_page(GFP_TEMPORARY);
733 mm = get_task_mm(task);
739 if (file->private_data != (void*)((long)current->self_exec_id))
745 int this_len, retval;
747 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
748 retval = access_process_vm(task, src, page, this_len, 0);
749 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
755 if (copy_to_user(buf, page, retval)) {
770 free_page((unsigned long) page);
772 put_task_struct(task);
777 #define mem_write NULL
780 /* This is a security hazard */
781 static ssize_t mem_write(struct file * file, const char __user *buf,
782 size_t count, loff_t *ppos)
786 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
787 unsigned long dst = *ppos;
793 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
797 page = (char *)__get_free_page(GFP_TEMPORARY);
803 int this_len, retval;
805 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
806 if (copy_from_user(page, buf, this_len)) {
810 retval = access_process_vm(task, dst, page, this_len, 1);
822 free_page((unsigned long) page);
824 put_task_struct(task);
830 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
834 file->f_pos = offset;
837 file->f_pos += offset;
842 force_successful_syscall_return();
846 static const struct file_operations proc_mem_operations = {
853 static ssize_t environ_read(struct file *file, char __user *buf,
854 size_t count, loff_t *ppos)
856 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
858 unsigned long src = *ppos;
860 struct mm_struct *mm;
865 if (!ptrace_may_attach(task))
869 page = (char *)__get_free_page(GFP_TEMPORARY);
875 mm = get_task_mm(task);
880 int this_len, retval, max_len;
882 this_len = mm->env_end - (mm->env_start + src);
887 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
888 this_len = (this_len > max_len) ? max_len : this_len;
890 retval = access_process_vm(task, (mm->env_start + src),
898 if (copy_to_user(buf, page, retval)) {
912 free_page((unsigned long) page);
914 put_task_struct(task);
919 static const struct file_operations proc_environ_operations = {
920 .read = environ_read,
923 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
924 size_t count, loff_t *ppos)
926 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
927 char buffer[PROC_NUMBUF];
933 oom_adjust = task->oomkilladj;
934 put_task_struct(task);
936 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
938 return simple_read_from_buffer(buf, count, ppos, buffer, len);
941 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
942 size_t count, loff_t *ppos)
944 struct task_struct *task;
945 char buffer[PROC_NUMBUF], *end;
948 memset(buffer, 0, sizeof(buffer));
949 if (count > sizeof(buffer) - 1)
950 count = sizeof(buffer) - 1;
951 if (copy_from_user(buffer, buf, count))
953 oom_adjust = simple_strtol(buffer, &end, 0);
954 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
955 oom_adjust != OOM_DISABLE)
959 task = get_proc_task(file->f_path.dentry->d_inode);
962 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
963 put_task_struct(task);
966 task->oomkilladj = oom_adjust;
967 put_task_struct(task);
968 if (end - buffer == 0)
973 static const struct file_operations proc_oom_adjust_operations = {
974 .read = oom_adjust_read,
975 .write = oom_adjust_write,
978 #ifdef CONFIG_AUDITSYSCALL
980 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
981 size_t count, loff_t *ppos)
983 struct inode * inode = file->f_path.dentry->d_inode;
984 struct task_struct *task = get_proc_task(inode);
986 char tmpbuf[TMPBUFLEN];
990 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
991 audit_get_loginuid(task));
992 put_task_struct(task);
993 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
996 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
997 size_t count, loff_t *ppos)
999 struct inode * inode = file->f_path.dentry->d_inode;
1004 if (!capable(CAP_AUDIT_CONTROL))
1007 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1010 if (count >= PAGE_SIZE)
1011 count = PAGE_SIZE - 1;
1014 /* No partial writes. */
1017 page = (char*)__get_free_page(GFP_TEMPORARY);
1021 if (copy_from_user(page, buf, count))
1025 loginuid = simple_strtoul(page, &tmp, 10);
1031 length = audit_set_loginuid(current, loginuid);
1032 if (likely(length == 0))
1036 free_page((unsigned long) page);
1040 static const struct file_operations proc_loginuid_operations = {
1041 .read = proc_loginuid_read,
1042 .write = proc_loginuid_write,
1046 #ifdef CONFIG_FAULT_INJECTION
1047 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1048 size_t count, loff_t *ppos)
1050 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1051 char buffer[PROC_NUMBUF];
1057 make_it_fail = task->make_it_fail;
1058 put_task_struct(task);
1060 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1062 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1065 static ssize_t proc_fault_inject_write(struct file * file,
1066 const char __user * buf, size_t count, loff_t *ppos)
1068 struct task_struct *task;
1069 char buffer[PROC_NUMBUF], *end;
1072 if (!capable(CAP_SYS_RESOURCE))
1074 memset(buffer, 0, sizeof(buffer));
1075 if (count > sizeof(buffer) - 1)
1076 count = sizeof(buffer) - 1;
1077 if (copy_from_user(buffer, buf, count))
1079 make_it_fail = simple_strtol(buffer, &end, 0);
1082 task = get_proc_task(file->f_dentry->d_inode);
1085 task->make_it_fail = make_it_fail;
1086 put_task_struct(task);
1087 if (end - buffer == 0)
1089 return end - buffer;
1092 static const struct file_operations proc_fault_inject_operations = {
1093 .read = proc_fault_inject_read,
1094 .write = proc_fault_inject_write,
1099 #ifdef CONFIG_SCHED_DEBUG
1101 * Print out various scheduling related per-task fields:
1103 static int sched_show(struct seq_file *m, void *v)
1105 struct inode *inode = m->private;
1106 struct task_struct *p;
1110 p = get_proc_task(inode);
1113 proc_sched_show_task(p, m);
1121 sched_write(struct file *file, const char __user *buf,
1122 size_t count, loff_t *offset)
1124 struct inode *inode = file->f_path.dentry->d_inode;
1125 struct task_struct *p;
1129 p = get_proc_task(inode);
1132 proc_sched_set_task(p);
1139 static int sched_open(struct inode *inode, struct file *filp)
1143 ret = single_open(filp, sched_show, NULL);
1145 struct seq_file *m = filp->private_data;
1152 static const struct file_operations proc_pid_sched_operations = {
1155 .write = sched_write,
1156 .llseek = seq_lseek,
1157 .release = single_release,
1162 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1164 struct inode *inode = dentry->d_inode;
1165 int error = -EACCES;
1167 /* We don't need a base pointer in the /proc filesystem */
1168 path_put(&nd->path);
1170 /* Are we allowed to snoop on the tasks file descriptors? */
1171 if (!proc_fd_access_allowed(inode))
1174 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1175 nd->last_type = LAST_BIND;
1177 return ERR_PTR(error);
1180 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1182 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1189 pathname = d_path(path, tmp, PAGE_SIZE);
1190 len = PTR_ERR(pathname);
1191 if (IS_ERR(pathname))
1193 len = tmp + PAGE_SIZE - 1 - pathname;
1197 if (copy_to_user(buffer, pathname, len))
1200 free_page((unsigned long)tmp);
1204 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1206 int error = -EACCES;
1207 struct inode *inode = dentry->d_inode;
1210 /* Are we allowed to snoop on the tasks file descriptors? */
1211 if (!proc_fd_access_allowed(inode))
1214 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1218 error = do_proc_readlink(&path, buffer, buflen);
1224 static const struct inode_operations proc_pid_link_inode_operations = {
1225 .readlink = proc_pid_readlink,
1226 .follow_link = proc_pid_follow_link,
1227 .setattr = proc_setattr,
1231 /* building an inode */
1233 static int task_dumpable(struct task_struct *task)
1236 struct mm_struct *mm;
1241 dumpable = get_dumpable(mm);
1249 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1251 struct inode * inode;
1252 struct proc_inode *ei;
1254 /* We need a new inode */
1256 inode = new_inode(sb);
1262 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1263 inode->i_op = &proc_def_inode_operations;
1266 * grab the reference to task.
1268 ei->pid = get_task_pid(task, PIDTYPE_PID);
1274 if (task_dumpable(task)) {
1275 inode->i_uid = task->euid;
1276 inode->i_gid = task->egid;
1278 security_task_to_inode(task, inode);
1288 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1290 struct inode *inode = dentry->d_inode;
1291 struct task_struct *task;
1292 generic_fillattr(inode, stat);
1297 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1299 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1300 task_dumpable(task)) {
1301 stat->uid = task->euid;
1302 stat->gid = task->egid;
1312 * Exceptional case: normally we are not allowed to unhash a busy
1313 * directory. In this case, however, we can do it - no aliasing problems
1314 * due to the way we treat inodes.
1316 * Rewrite the inode's ownerships here because the owning task may have
1317 * performed a setuid(), etc.
1319 * Before the /proc/pid/status file was created the only way to read
1320 * the effective uid of a /process was to stat /proc/pid. Reading
1321 * /proc/pid/status is slow enough that procps and other packages
1322 * kept stating /proc/pid. To keep the rules in /proc simple I have
1323 * made this apply to all per process world readable and executable
1326 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1328 struct inode *inode = dentry->d_inode;
1329 struct task_struct *task = get_proc_task(inode);
1331 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1332 task_dumpable(task)) {
1333 inode->i_uid = task->euid;
1334 inode->i_gid = task->egid;
1339 inode->i_mode &= ~(S_ISUID | S_ISGID);
1340 security_task_to_inode(task, inode);
1341 put_task_struct(task);
1348 static int pid_delete_dentry(struct dentry * dentry)
1350 /* Is the task we represent dead?
1351 * If so, then don't put the dentry on the lru list,
1352 * kill it immediately.
1354 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1357 static struct dentry_operations pid_dentry_operations =
1359 .d_revalidate = pid_revalidate,
1360 .d_delete = pid_delete_dentry,
1365 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1366 struct task_struct *, const void *);
1369 * Fill a directory entry.
1371 * If possible create the dcache entry and derive our inode number and
1372 * file type from dcache entry.
1374 * Since all of the proc inode numbers are dynamically generated, the inode
1375 * numbers do not exist until the inode is cache. This means creating the
1376 * the dcache entry in readdir is necessary to keep the inode numbers
1377 * reported by readdir in sync with the inode numbers reported
1380 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1381 char *name, int len,
1382 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1384 struct dentry *child, *dir = filp->f_path.dentry;
1385 struct inode *inode;
1388 unsigned type = DT_UNKNOWN;
1392 qname.hash = full_name_hash(name, len);
1394 child = d_lookup(dir, &qname);
1397 new = d_alloc(dir, &qname);
1399 child = instantiate(dir->d_inode, new, task, ptr);
1406 if (!child || IS_ERR(child) || !child->d_inode)
1407 goto end_instantiate;
1408 inode = child->d_inode;
1411 type = inode->i_mode >> 12;
1416 ino = find_inode_number(dir, &qname);
1419 return filldir(dirent, name, len, filp->f_pos, ino, type);
1422 static unsigned name_to_int(struct dentry *dentry)
1424 const char *name = dentry->d_name.name;
1425 int len = dentry->d_name.len;
1428 if (len > 1 && *name == '0')
1431 unsigned c = *name++ - '0';
1434 if (n >= (~0U-9)/10)
1444 #define PROC_FDINFO_MAX 64
1446 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1448 struct task_struct *task = get_proc_task(inode);
1449 struct files_struct *files = NULL;
1451 int fd = proc_fd(inode);
1454 files = get_files_struct(task);
1455 put_task_struct(task);
1459 * We are not taking a ref to the file structure, so we must
1462 spin_lock(&files->file_lock);
1463 file = fcheck_files(files, fd);
1466 *path = file->f_path;
1467 path_get(&file->f_path);
1470 snprintf(info, PROC_FDINFO_MAX,
1473 (long long) file->f_pos,
1475 spin_unlock(&files->file_lock);
1476 put_files_struct(files);
1479 spin_unlock(&files->file_lock);
1480 put_files_struct(files);
1485 static int proc_fd_link(struct inode *inode, struct path *path)
1487 return proc_fd_info(inode, path, NULL);
1490 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1492 struct inode *inode = dentry->d_inode;
1493 struct task_struct *task = get_proc_task(inode);
1494 int fd = proc_fd(inode);
1495 struct files_struct *files;
1498 files = get_files_struct(task);
1501 if (fcheck_files(files, fd)) {
1503 put_files_struct(files);
1504 if (task_dumpable(task)) {
1505 inode->i_uid = task->euid;
1506 inode->i_gid = task->egid;
1511 inode->i_mode &= ~(S_ISUID | S_ISGID);
1512 security_task_to_inode(task, inode);
1513 put_task_struct(task);
1517 put_files_struct(files);
1519 put_task_struct(task);
1525 static struct dentry_operations tid_fd_dentry_operations =
1527 .d_revalidate = tid_fd_revalidate,
1528 .d_delete = pid_delete_dentry,
1531 static struct dentry *proc_fd_instantiate(struct inode *dir,
1532 struct dentry *dentry, struct task_struct *task, const void *ptr)
1534 unsigned fd = *(const unsigned *)ptr;
1536 struct files_struct *files;
1537 struct inode *inode;
1538 struct proc_inode *ei;
1539 struct dentry *error = ERR_PTR(-ENOENT);
1541 inode = proc_pid_make_inode(dir->i_sb, task);
1546 files = get_files_struct(task);
1549 inode->i_mode = S_IFLNK;
1552 * We are not taking a ref to the file structure, so we must
1555 spin_lock(&files->file_lock);
1556 file = fcheck_files(files, fd);
1559 if (file->f_mode & 1)
1560 inode->i_mode |= S_IRUSR | S_IXUSR;
1561 if (file->f_mode & 2)
1562 inode->i_mode |= S_IWUSR | S_IXUSR;
1563 spin_unlock(&files->file_lock);
1564 put_files_struct(files);
1566 inode->i_op = &proc_pid_link_inode_operations;
1568 ei->op.proc_get_link = proc_fd_link;
1569 dentry->d_op = &tid_fd_dentry_operations;
1570 d_add(dentry, inode);
1571 /* Close the race of the process dying before we return the dentry */
1572 if (tid_fd_revalidate(dentry, NULL))
1578 spin_unlock(&files->file_lock);
1579 put_files_struct(files);
1585 static struct dentry *proc_lookupfd_common(struct inode *dir,
1586 struct dentry *dentry,
1587 instantiate_t instantiate)
1589 struct task_struct *task = get_proc_task(dir);
1590 unsigned fd = name_to_int(dentry);
1591 struct dentry *result = ERR_PTR(-ENOENT);
1598 result = instantiate(dir, dentry, task, &fd);
1600 put_task_struct(task);
1605 static int proc_readfd_common(struct file * filp, void * dirent,
1606 filldir_t filldir, instantiate_t instantiate)
1608 struct dentry *dentry = filp->f_path.dentry;
1609 struct inode *inode = dentry->d_inode;
1610 struct task_struct *p = get_proc_task(inode);
1611 unsigned int fd, ino;
1613 struct files_struct * files;
1614 struct fdtable *fdt;
1624 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1628 ino = parent_ino(dentry);
1629 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1633 files = get_files_struct(p);
1637 fdt = files_fdtable(files);
1638 for (fd = filp->f_pos-2;
1640 fd++, filp->f_pos++) {
1641 char name[PROC_NUMBUF];
1644 if (!fcheck_files(files, fd))
1648 len = snprintf(name, sizeof(name), "%d", fd);
1649 if (proc_fill_cache(filp, dirent, filldir,
1650 name, len, instantiate,
1658 put_files_struct(files);
1666 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1667 struct nameidata *nd)
1669 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1672 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1674 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1677 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1678 size_t len, loff_t *ppos)
1680 char tmp[PROC_FDINFO_MAX];
1681 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1683 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1687 static const struct file_operations proc_fdinfo_file_operations = {
1688 .open = nonseekable_open,
1689 .read = proc_fdinfo_read,
1692 static const struct file_operations proc_fd_operations = {
1693 .read = generic_read_dir,
1694 .readdir = proc_readfd,
1698 * /proc/pid/fd needs a special permission handler so that a process can still
1699 * access /proc/self/fd after it has executed a setuid().
1701 static int proc_fd_permission(struct inode *inode, int mask,
1702 struct nameidata *nd)
1706 rv = generic_permission(inode, mask, NULL);
1709 if (task_pid(current) == proc_pid(inode))
1715 * proc directories can do almost nothing..
1717 static const struct inode_operations proc_fd_inode_operations = {
1718 .lookup = proc_lookupfd,
1719 .permission = proc_fd_permission,
1720 .setattr = proc_setattr,
1723 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1724 struct dentry *dentry, struct task_struct *task, const void *ptr)
1726 unsigned fd = *(unsigned *)ptr;
1727 struct inode *inode;
1728 struct proc_inode *ei;
1729 struct dentry *error = ERR_PTR(-ENOENT);
1731 inode = proc_pid_make_inode(dir->i_sb, task);
1736 inode->i_mode = S_IFREG | S_IRUSR;
1737 inode->i_fop = &proc_fdinfo_file_operations;
1738 dentry->d_op = &tid_fd_dentry_operations;
1739 d_add(dentry, inode);
1740 /* Close the race of the process dying before we return the dentry */
1741 if (tid_fd_revalidate(dentry, NULL))
1748 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1749 struct dentry *dentry,
1750 struct nameidata *nd)
1752 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1755 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1757 return proc_readfd_common(filp, dirent, filldir,
1758 proc_fdinfo_instantiate);
1761 static const struct file_operations proc_fdinfo_operations = {
1762 .read = generic_read_dir,
1763 .readdir = proc_readfdinfo,
1767 * proc directories can do almost nothing..
1769 static const struct inode_operations proc_fdinfo_inode_operations = {
1770 .lookup = proc_lookupfdinfo,
1771 .setattr = proc_setattr,
1775 static struct dentry *proc_pident_instantiate(struct inode *dir,
1776 struct dentry *dentry, struct task_struct *task, const void *ptr)
1778 const struct pid_entry *p = ptr;
1779 struct inode *inode;
1780 struct proc_inode *ei;
1781 struct dentry *error = ERR_PTR(-EINVAL);
1783 inode = proc_pid_make_inode(dir->i_sb, task);
1788 inode->i_mode = p->mode;
1789 if (S_ISDIR(inode->i_mode))
1790 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1792 inode->i_op = p->iop;
1794 inode->i_fop = p->fop;
1796 dentry->d_op = &pid_dentry_operations;
1797 d_add(dentry, inode);
1798 /* Close the race of the process dying before we return the dentry */
1799 if (pid_revalidate(dentry, NULL))
1805 static struct dentry *proc_pident_lookup(struct inode *dir,
1806 struct dentry *dentry,
1807 const struct pid_entry *ents,
1810 struct inode *inode;
1811 struct dentry *error;
1812 struct task_struct *task = get_proc_task(dir);
1813 const struct pid_entry *p, *last;
1815 error = ERR_PTR(-ENOENT);
1822 * Yes, it does not scale. And it should not. Don't add
1823 * new entries into /proc/<tgid>/ without very good reasons.
1825 last = &ents[nents - 1];
1826 for (p = ents; p <= last; p++) {
1827 if (p->len != dentry->d_name.len)
1829 if (!memcmp(dentry->d_name.name, p->name, p->len))
1835 error = proc_pident_instantiate(dir, dentry, task, p);
1837 put_task_struct(task);
1842 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1843 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1845 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1846 proc_pident_instantiate, task, p);
1849 static int proc_pident_readdir(struct file *filp,
1850 void *dirent, filldir_t filldir,
1851 const struct pid_entry *ents, unsigned int nents)
1854 struct dentry *dentry = filp->f_path.dentry;
1855 struct inode *inode = dentry->d_inode;
1856 struct task_struct *task = get_proc_task(inode);
1857 const struct pid_entry *p, *last;
1870 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1876 ino = parent_ino(dentry);
1877 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1889 last = &ents[nents - 1];
1891 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1900 put_task_struct(task);
1905 #ifdef CONFIG_SECURITY
1906 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1907 size_t count, loff_t *ppos)
1909 struct inode * inode = file->f_path.dentry->d_inode;
1912 struct task_struct *task = get_proc_task(inode);
1917 length = security_getprocattr(task,
1918 (char*)file->f_path.dentry->d_name.name,
1920 put_task_struct(task);
1922 length = simple_read_from_buffer(buf, count, ppos, p, length);
1927 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1928 size_t count, loff_t *ppos)
1930 struct inode * inode = file->f_path.dentry->d_inode;
1933 struct task_struct *task = get_proc_task(inode);
1938 if (count > PAGE_SIZE)
1941 /* No partial writes. */
1947 page = (char*)__get_free_page(GFP_TEMPORARY);
1952 if (copy_from_user(page, buf, count))
1955 length = security_setprocattr(task,
1956 (char*)file->f_path.dentry->d_name.name,
1957 (void*)page, count);
1959 free_page((unsigned long) page);
1961 put_task_struct(task);
1966 static const struct file_operations proc_pid_attr_operations = {
1967 .read = proc_pid_attr_read,
1968 .write = proc_pid_attr_write,
1971 static const struct pid_entry attr_dir_stuff[] = {
1972 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1973 REG("prev", S_IRUGO, pid_attr),
1974 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1975 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1976 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1977 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1980 static int proc_attr_dir_readdir(struct file * filp,
1981 void * dirent, filldir_t filldir)
1983 return proc_pident_readdir(filp,dirent,filldir,
1984 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1987 static const struct file_operations proc_attr_dir_operations = {
1988 .read = generic_read_dir,
1989 .readdir = proc_attr_dir_readdir,
1992 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1993 struct dentry *dentry, struct nameidata *nd)
1995 return proc_pident_lookup(dir, dentry,
1996 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1999 static const struct inode_operations proc_attr_dir_inode_operations = {
2000 .lookup = proc_attr_dir_lookup,
2001 .getattr = pid_getattr,
2002 .setattr = proc_setattr,
2007 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2008 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2009 size_t count, loff_t *ppos)
2011 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2012 struct mm_struct *mm;
2013 char buffer[PROC_NUMBUF];
2021 mm = get_task_mm(task);
2023 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2024 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2025 MMF_DUMP_FILTER_SHIFT));
2027 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2030 put_task_struct(task);
2035 static ssize_t proc_coredump_filter_write(struct file *file,
2036 const char __user *buf,
2040 struct task_struct *task;
2041 struct mm_struct *mm;
2042 char buffer[PROC_NUMBUF], *end;
2049 memset(buffer, 0, sizeof(buffer));
2050 if (count > sizeof(buffer) - 1)
2051 count = sizeof(buffer) - 1;
2052 if (copy_from_user(buffer, buf, count))
2056 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2059 if (end - buffer == 0)
2063 task = get_proc_task(file->f_dentry->d_inode);
2068 mm = get_task_mm(task);
2072 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2074 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2076 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2081 put_task_struct(task);
2086 static const struct file_operations proc_coredump_filter_operations = {
2087 .read = proc_coredump_filter_read,
2088 .write = proc_coredump_filter_write,
2095 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2098 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2099 pid_t tgid = task_tgid_nr_ns(current, ns);
2100 char tmp[PROC_NUMBUF];
2103 sprintf(tmp, "%d", tgid);
2104 return vfs_readlink(dentry,buffer,buflen,tmp);
2107 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2109 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2110 pid_t tgid = task_tgid_nr_ns(current, ns);
2111 char tmp[PROC_NUMBUF];
2113 return ERR_PTR(-ENOENT);
2114 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2115 return ERR_PTR(vfs_follow_link(nd,tmp));
2118 static const struct inode_operations proc_self_inode_operations = {
2119 .readlink = proc_self_readlink,
2120 .follow_link = proc_self_follow_link,
2126 * These are the directory entries in the root directory of /proc
2127 * that properly belong to the /proc filesystem, as they describe
2128 * describe something that is process related.
2130 static const struct pid_entry proc_base_stuff[] = {
2131 NOD("self", S_IFLNK|S_IRWXUGO,
2132 &proc_self_inode_operations, NULL, {}),
2136 * Exceptional case: normally we are not allowed to unhash a busy
2137 * directory. In this case, however, we can do it - no aliasing problems
2138 * due to the way we treat inodes.
2140 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2142 struct inode *inode = dentry->d_inode;
2143 struct task_struct *task = get_proc_task(inode);
2145 put_task_struct(task);
2152 static struct dentry_operations proc_base_dentry_operations =
2154 .d_revalidate = proc_base_revalidate,
2155 .d_delete = pid_delete_dentry,
2158 static struct dentry *proc_base_instantiate(struct inode *dir,
2159 struct dentry *dentry, struct task_struct *task, const void *ptr)
2161 const struct pid_entry *p = ptr;
2162 struct inode *inode;
2163 struct proc_inode *ei;
2164 struct dentry *error = ERR_PTR(-EINVAL);
2166 /* Allocate the inode */
2167 error = ERR_PTR(-ENOMEM);
2168 inode = new_inode(dir->i_sb);
2172 /* Initialize the inode */
2174 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2177 * grab the reference to the task.
2179 ei->pid = get_task_pid(task, PIDTYPE_PID);
2185 inode->i_mode = p->mode;
2186 if (S_ISDIR(inode->i_mode))
2188 if (S_ISLNK(inode->i_mode))
2191 inode->i_op = p->iop;
2193 inode->i_fop = p->fop;
2195 dentry->d_op = &proc_base_dentry_operations;
2196 d_add(dentry, inode);
2205 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2207 struct dentry *error;
2208 struct task_struct *task = get_proc_task(dir);
2209 const struct pid_entry *p, *last;
2211 error = ERR_PTR(-ENOENT);
2216 /* Lookup the directory entry */
2217 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2218 for (p = proc_base_stuff; p <= last; p++) {
2219 if (p->len != dentry->d_name.len)
2221 if (!memcmp(dentry->d_name.name, p->name, p->len))
2227 error = proc_base_instantiate(dir, dentry, task, p);
2230 put_task_struct(task);
2235 static int proc_base_fill_cache(struct file *filp, void *dirent,
2236 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2238 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2239 proc_base_instantiate, task, p);
2242 #ifdef CONFIG_TASK_IO_ACCOUNTING
2243 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2245 return sprintf(buffer,
2246 #ifdef CONFIG_TASK_XACCT
2252 "read_bytes: %llu\n"
2253 "write_bytes: %llu\n"
2254 "cancelled_write_bytes: %llu\n",
2255 #ifdef CONFIG_TASK_XACCT
2256 (unsigned long long)task->rchar,
2257 (unsigned long long)task->wchar,
2258 (unsigned long long)task->syscr,
2259 (unsigned long long)task->syscw,
2261 (unsigned long long)task->ioac.read_bytes,
2262 (unsigned long long)task->ioac.write_bytes,
2263 (unsigned long long)task->ioac.cancelled_write_bytes);
2270 static const struct file_operations proc_task_operations;
2271 static const struct inode_operations proc_task_inode_operations;
2273 static const struct pid_entry tgid_base_stuff[] = {
2274 DIR("task", S_IRUGO|S_IXUGO, task),
2275 DIR("fd", S_IRUSR|S_IXUSR, fd),
2276 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2277 DIR("net", S_IRUGO|S_IXUSR, net),
2278 REG("environ", S_IRUSR, environ),
2279 INF("auxv", S_IRUSR, pid_auxv),
2280 ONE("status", S_IRUGO, pid_status),
2281 INF("limits", S_IRUSR, pid_limits),
2282 #ifdef CONFIG_SCHED_DEBUG
2283 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2285 INF("cmdline", S_IRUGO, pid_cmdline),
2286 ONE("stat", S_IRUGO, tgid_stat),
2287 ONE("statm", S_IRUGO, pid_statm),
2288 REG("maps", S_IRUGO, maps),
2290 REG("numa_maps", S_IRUGO, numa_maps),
2292 REG("mem", S_IRUSR|S_IWUSR, mem),
2296 REG("mounts", S_IRUGO, mounts),
2297 REG("mountstats", S_IRUSR, mountstats),
2298 #ifdef CONFIG_PROC_PAGE_MONITOR
2299 REG("clear_refs", S_IWUSR, clear_refs),
2300 REG("smaps", S_IRUGO, smaps),
2301 REG("pagemap", S_IRUSR, pagemap),
2303 #ifdef CONFIG_SECURITY
2304 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2306 #ifdef CONFIG_KALLSYMS
2307 INF("wchan", S_IRUGO, pid_wchan),
2309 #ifdef CONFIG_SCHEDSTATS
2310 INF("schedstat", S_IRUGO, pid_schedstat),
2312 #ifdef CONFIG_LATENCYTOP
2313 REG("latency", S_IRUGO, lstats),
2315 #ifdef CONFIG_PROC_PID_CPUSET
2316 REG("cpuset", S_IRUGO, cpuset),
2318 #ifdef CONFIG_CGROUPS
2319 REG("cgroup", S_IRUGO, cgroup),
2321 INF("oom_score", S_IRUGO, oom_score),
2322 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2323 #ifdef CONFIG_AUDITSYSCALL
2324 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2326 #ifdef CONFIG_FAULT_INJECTION
2327 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2329 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2330 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2332 #ifdef CONFIG_TASK_IO_ACCOUNTING
2333 INF("io", S_IRUGO, pid_io_accounting),
2337 static int proc_tgid_base_readdir(struct file * filp,
2338 void * dirent, filldir_t filldir)
2340 return proc_pident_readdir(filp,dirent,filldir,
2341 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2344 static const struct file_operations proc_tgid_base_operations = {
2345 .read = generic_read_dir,
2346 .readdir = proc_tgid_base_readdir,
2349 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2350 return proc_pident_lookup(dir, dentry,
2351 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2354 static const struct inode_operations proc_tgid_base_inode_operations = {
2355 .lookup = proc_tgid_base_lookup,
2356 .getattr = pid_getattr,
2357 .setattr = proc_setattr,
2360 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2362 struct dentry *dentry, *leader, *dir;
2363 char buf[PROC_NUMBUF];
2367 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2368 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2370 if (!(current->flags & PF_EXITING))
2371 shrink_dcache_parent(dentry);
2380 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2381 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2386 name.len = strlen(name.name);
2387 dir = d_hash_and_lookup(leader, &name);
2389 goto out_put_leader;
2392 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2393 dentry = d_hash_and_lookup(dir, &name);
2395 shrink_dcache_parent(dentry);
2408 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2409 * @task: task that should be flushed.
2411 * When flushing dentries from proc, one needs to flush them from global
2412 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2413 * in. This call is supposed to do all of this job.
2415 * Looks in the dcache for
2417 * /proc/@tgid/task/@pid
2418 * if either directory is present flushes it and all of it'ts children
2421 * It is safe and reasonable to cache /proc entries for a task until
2422 * that task exits. After that they just clog up the dcache with
2423 * useless entries, possibly causing useful dcache entries to be
2424 * flushed instead. This routine is proved to flush those useless
2425 * dcache entries at process exit time.
2427 * NOTE: This routine is just an optimization so it does not guarantee
2428 * that no dcache entries will exist at process exit time it
2429 * just makes it very unlikely that any will persist.
2432 void proc_flush_task(struct task_struct *task)
2435 struct pid *pid, *tgid = NULL;
2438 pid = task_pid(task);
2439 if (thread_group_leader(task))
2440 tgid = task_tgid(task);
2442 for (i = 0; i <= pid->level; i++) {
2443 upid = &pid->numbers[i];
2444 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2445 tgid ? tgid->numbers[i].nr : 0);
2448 upid = &pid->numbers[pid->level];
2450 pid_ns_release_proc(upid->ns);
2453 static struct dentry *proc_pid_instantiate(struct inode *dir,
2454 struct dentry * dentry,
2455 struct task_struct *task, const void *ptr)
2457 struct dentry *error = ERR_PTR(-ENOENT);
2458 struct inode *inode;
2460 inode = proc_pid_make_inode(dir->i_sb, task);
2464 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2465 inode->i_op = &proc_tgid_base_inode_operations;
2466 inode->i_fop = &proc_tgid_base_operations;
2467 inode->i_flags|=S_IMMUTABLE;
2469 #ifdef CONFIG_SECURITY
2470 inode->i_nlink += 1;
2473 dentry->d_op = &pid_dentry_operations;
2475 d_add(dentry, inode);
2476 /* Close the race of the process dying before we return the dentry */
2477 if (pid_revalidate(dentry, NULL))
2483 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2485 struct dentry *result = ERR_PTR(-ENOENT);
2486 struct task_struct *task;
2488 struct pid_namespace *ns;
2490 result = proc_base_lookup(dir, dentry);
2491 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2494 tgid = name_to_int(dentry);
2498 ns = dentry->d_sb->s_fs_info;
2500 task = find_task_by_pid_ns(tgid, ns);
2502 get_task_struct(task);
2507 result = proc_pid_instantiate(dir, dentry, task, NULL);
2508 put_task_struct(task);
2514 * Find the first task with tgid >= tgid
2519 struct task_struct *task;
2521 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2526 put_task_struct(iter.task);
2530 pid = find_ge_pid(iter.tgid, ns);
2532 iter.tgid = pid_nr_ns(pid, ns);
2533 iter.task = pid_task(pid, PIDTYPE_PID);
2534 /* What we to know is if the pid we have find is the
2535 * pid of a thread_group_leader. Testing for task
2536 * being a thread_group_leader is the obvious thing
2537 * todo but there is a window when it fails, due to
2538 * the pid transfer logic in de_thread.
2540 * So we perform the straight forward test of seeing
2541 * if the pid we have found is the pid of a thread
2542 * group leader, and don't worry if the task we have
2543 * found doesn't happen to be a thread group leader.
2544 * As we don't care in the case of readdir.
2546 if (!iter.task || !has_group_leader_pid(iter.task)) {
2550 get_task_struct(iter.task);
2556 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2558 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2559 struct tgid_iter iter)
2561 char name[PROC_NUMBUF];
2562 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2563 return proc_fill_cache(filp, dirent, filldir, name, len,
2564 proc_pid_instantiate, iter.task, NULL);
2567 /* for the /proc/ directory itself, after non-process stuff has been done */
2568 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2570 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2571 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2572 struct tgid_iter iter;
2573 struct pid_namespace *ns;
2578 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2579 const struct pid_entry *p = &proc_base_stuff[nr];
2580 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2584 ns = filp->f_dentry->d_sb->s_fs_info;
2586 iter.tgid = filp->f_pos - TGID_OFFSET;
2587 for (iter = next_tgid(ns, iter);
2589 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2590 filp->f_pos = iter.tgid + TGID_OFFSET;
2591 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2592 put_task_struct(iter.task);
2596 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2598 put_task_struct(reaper);
2606 static const struct pid_entry tid_base_stuff[] = {
2607 DIR("fd", S_IRUSR|S_IXUSR, fd),
2608 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2609 REG("environ", S_IRUSR, environ),
2610 INF("auxv", S_IRUSR, pid_auxv),
2611 ONE("status", S_IRUGO, pid_status),
2612 INF("limits", S_IRUSR, pid_limits),
2613 #ifdef CONFIG_SCHED_DEBUG
2614 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2616 INF("cmdline", S_IRUGO, pid_cmdline),
2617 ONE("stat", S_IRUGO, tid_stat),
2618 ONE("statm", S_IRUGO, pid_statm),
2619 REG("maps", S_IRUGO, maps),
2621 REG("numa_maps", S_IRUGO, numa_maps),
2623 REG("mem", S_IRUSR|S_IWUSR, mem),
2627 REG("mounts", S_IRUGO, mounts),
2628 #ifdef CONFIG_PROC_PAGE_MONITOR
2629 REG("clear_refs", S_IWUSR, clear_refs),
2630 REG("smaps", S_IRUGO, smaps),
2631 REG("pagemap", S_IRUSR, pagemap),
2633 #ifdef CONFIG_SECURITY
2634 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2636 #ifdef CONFIG_KALLSYMS
2637 INF("wchan", S_IRUGO, pid_wchan),
2639 #ifdef CONFIG_SCHEDSTATS
2640 INF("schedstat", S_IRUGO, pid_schedstat),
2642 #ifdef CONFIG_LATENCYTOP
2643 REG("latency", S_IRUGO, lstats),
2645 #ifdef CONFIG_PROC_PID_CPUSET
2646 REG("cpuset", S_IRUGO, cpuset),
2648 #ifdef CONFIG_CGROUPS
2649 REG("cgroup", S_IRUGO, cgroup),
2651 INF("oom_score", S_IRUGO, oom_score),
2652 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2653 #ifdef CONFIG_AUDITSYSCALL
2654 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2656 #ifdef CONFIG_FAULT_INJECTION
2657 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2661 static int proc_tid_base_readdir(struct file * filp,
2662 void * dirent, filldir_t filldir)
2664 return proc_pident_readdir(filp,dirent,filldir,
2665 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2668 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2669 return proc_pident_lookup(dir, dentry,
2670 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2673 static const struct file_operations proc_tid_base_operations = {
2674 .read = generic_read_dir,
2675 .readdir = proc_tid_base_readdir,
2678 static const struct inode_operations proc_tid_base_inode_operations = {
2679 .lookup = proc_tid_base_lookup,
2680 .getattr = pid_getattr,
2681 .setattr = proc_setattr,
2684 static struct dentry *proc_task_instantiate(struct inode *dir,
2685 struct dentry *dentry, struct task_struct *task, const void *ptr)
2687 struct dentry *error = ERR_PTR(-ENOENT);
2688 struct inode *inode;
2689 inode = proc_pid_make_inode(dir->i_sb, task);
2693 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2694 inode->i_op = &proc_tid_base_inode_operations;
2695 inode->i_fop = &proc_tid_base_operations;
2696 inode->i_flags|=S_IMMUTABLE;
2698 #ifdef CONFIG_SECURITY
2699 inode->i_nlink += 1;
2702 dentry->d_op = &pid_dentry_operations;
2704 d_add(dentry, inode);
2705 /* Close the race of the process dying before we return the dentry */
2706 if (pid_revalidate(dentry, NULL))
2712 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2714 struct dentry *result = ERR_PTR(-ENOENT);
2715 struct task_struct *task;
2716 struct task_struct *leader = get_proc_task(dir);
2718 struct pid_namespace *ns;
2723 tid = name_to_int(dentry);
2727 ns = dentry->d_sb->s_fs_info;
2729 task = find_task_by_pid_ns(tid, ns);
2731 get_task_struct(task);
2735 if (!same_thread_group(leader, task))
2738 result = proc_task_instantiate(dir, dentry, task, NULL);
2740 put_task_struct(task);
2742 put_task_struct(leader);
2748 * Find the first tid of a thread group to return to user space.
2750 * Usually this is just the thread group leader, but if the users
2751 * buffer was too small or there was a seek into the middle of the
2752 * directory we have more work todo.
2754 * In the case of a short read we start with find_task_by_pid.
2756 * In the case of a seek we start with the leader and walk nr
2759 static struct task_struct *first_tid(struct task_struct *leader,
2760 int tid, int nr, struct pid_namespace *ns)
2762 struct task_struct *pos;
2765 /* Attempt to start with the pid of a thread */
2766 if (tid && (nr > 0)) {
2767 pos = find_task_by_pid_ns(tid, ns);
2768 if (pos && (pos->group_leader == leader))
2772 /* If nr exceeds the number of threads there is nothing todo */
2774 if (nr && nr >= get_nr_threads(leader))
2777 /* If we haven't found our starting place yet start
2778 * with the leader and walk nr threads forward.
2780 for (pos = leader; nr > 0; --nr) {
2781 pos = next_thread(pos);
2782 if (pos == leader) {
2788 get_task_struct(pos);
2795 * Find the next thread in the thread list.
2796 * Return NULL if there is an error or no next thread.
2798 * The reference to the input task_struct is released.
2800 static struct task_struct *next_tid(struct task_struct *start)
2802 struct task_struct *pos = NULL;
2804 if (pid_alive(start)) {
2805 pos = next_thread(start);
2806 if (thread_group_leader(pos))
2809 get_task_struct(pos);
2812 put_task_struct(start);
2816 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2817 struct task_struct *task, int tid)
2819 char name[PROC_NUMBUF];
2820 int len = snprintf(name, sizeof(name), "%d", tid);
2821 return proc_fill_cache(filp, dirent, filldir, name, len,
2822 proc_task_instantiate, task, NULL);
2825 /* for the /proc/TGID/task/ directories */
2826 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2828 struct dentry *dentry = filp->f_path.dentry;
2829 struct inode *inode = dentry->d_inode;
2830 struct task_struct *leader = NULL;
2831 struct task_struct *task;
2832 int retval = -ENOENT;
2835 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2836 struct pid_namespace *ns;
2838 task = get_proc_task(inode);
2842 if (pid_alive(task)) {
2843 leader = task->group_leader;
2844 get_task_struct(leader);
2847 put_task_struct(task);
2855 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2860 ino = parent_ino(dentry);
2861 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2867 /* f_version caches the tgid value that the last readdir call couldn't
2868 * return. lseek aka telldir automagically resets f_version to 0.
2870 ns = filp->f_dentry->d_sb->s_fs_info;
2871 tid = (int)filp->f_version;
2872 filp->f_version = 0;
2873 for (task = first_tid(leader, tid, pos - 2, ns);
2875 task = next_tid(task), pos++) {
2876 tid = task_pid_nr_ns(task, ns);
2877 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2878 /* returning this tgid failed, save it as the first
2879 * pid for the next readir call */
2880 filp->f_version = (u64)tid;
2881 put_task_struct(task);
2887 put_task_struct(leader);
2892 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2894 struct inode *inode = dentry->d_inode;
2895 struct task_struct *p = get_proc_task(inode);
2896 generic_fillattr(inode, stat);
2900 stat->nlink += get_nr_threads(p);
2908 static const struct inode_operations proc_task_inode_operations = {
2909 .lookup = proc_task_lookup,
2910 .getattr = proc_task_getattr,
2911 .setattr = proc_setattr,
2914 static const struct file_operations proc_task_operations = {
2915 .read = generic_read_dir,
2916 .readdir = proc_task_readdir,