4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
64 #include <linux/rcupdate.h>
65 #include <linux/kallsyms.h>
66 #include <linux/resource.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/cgroup.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include <linux/elf.h>
78 #include <linux/pid_namespace.h>
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
92 /* Worst case buffer size needed for holding an integer. */
93 #define PROC_NUMBUF 13
99 const struct inode_operations *iop;
100 const struct file_operations *fop;
104 #define NOD(NAME, MODE, IOP, FOP, OP) { \
106 .len = sizeof(NAME) - 1, \
113 #define DIR(NAME, MODE, OTYPE) \
114 NOD(NAME, (S_IFDIR|(MODE)), \
115 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
117 #define LNK(NAME, OTYPE) \
118 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
119 &proc_pid_link_inode_operations, NULL, \
120 { .proc_get_link = &proc_##OTYPE##_link } )
121 #define REG(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), NULL, \
123 &proc_##OTYPE##_operations, {})
124 #define INF(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_info_file_operations, \
127 { .proc_read = &proc_##OTYPE } )
130 EXPORT_SYMBOL(maps_protect);
132 static struct fs_struct *get_fs_struct(struct task_struct *task)
134 struct fs_struct *fs;
138 atomic_inc(&fs->count);
143 static int get_nr_threads(struct task_struct *tsk)
145 /* Must be called with the rcu_read_lock held */
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
156 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
160 int result = -ENOENT;
163 fs = get_fs_struct(task);
164 put_task_struct(task);
167 read_lock(&fs->lock);
168 *mnt = mntget(fs->pwdmnt);
169 *dentry = dget(fs->pwd);
170 read_unlock(&fs->lock);
177 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
181 int result = -ENOENT;
184 fs = get_fs_struct(task);
185 put_task_struct(task);
188 read_lock(&fs->lock);
189 *mnt = mntget(fs->rootmnt);
190 *dentry = dget(fs->root);
191 read_unlock(&fs->lock);
198 #define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
202 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
203 security_ptrace(current,task) == 0))
205 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},
421 /* Display limits for a process */
422 static int proc_pid_limits(struct task_struct *task, char *buffer)
427 char *bufptr = buffer;
429 struct rlimit rlim[RLIM_NLIMITS];
432 if (!lock_task_sighand(task,&flags)) {
436 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
437 unlock_task_sighand(task, &flags);
441 * print the file header
443 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
444 "Limit", "Soft Limit", "Hard Limit", "Units");
446 for (i = 0; i < RLIM_NLIMITS; i++) {
447 if (rlim[i].rlim_cur == RLIM_INFINITY)
448 count += sprintf(&bufptr[count], "%-25s %-20s ",
449 lnames[i].name, "unlimited");
451 count += sprintf(&bufptr[count], "%-25s %-20lu ",
452 lnames[i].name, rlim[i].rlim_cur);
454 if (rlim[i].rlim_max == RLIM_INFINITY)
455 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
457 count += sprintf(&bufptr[count], "%-20lu ",
461 count += sprintf(&bufptr[count], "%-10s\n",
464 count += sprintf(&bufptr[count], "\n");
470 /************************************************************************/
471 /* Here the fs part begins */
472 /************************************************************************/
474 /* permission checks */
475 static int proc_fd_access_allowed(struct inode *inode)
477 struct task_struct *task;
479 /* Allow access to a task's file descriptors if it is us or we
480 * may use ptrace attach to the process and find out that
483 task = get_proc_task(inode);
485 allowed = ptrace_may_attach(task);
486 put_task_struct(task);
491 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
494 struct inode *inode = dentry->d_inode;
496 if (attr->ia_valid & ATTR_MODE)
499 error = inode_change_ok(inode, attr);
501 error = inode_setattr(inode, attr);
505 static const struct inode_operations proc_def_inode_operations = {
506 .setattr = proc_setattr,
509 extern struct seq_operations mounts_op;
515 static int mounts_open(struct inode *inode, struct file *file)
517 struct task_struct *task = get_proc_task(inode);
519 struct mnt_namespace *ns = NULL;
520 struct proc_mounts *p;
525 nsp = task_nsproxy(task);
533 put_task_struct(task);
538 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
540 file->private_data = &p->m;
541 ret = seq_open(file, &mounts_op);
544 p->event = ns->event;
554 static int mounts_release(struct inode *inode, struct file *file)
556 struct seq_file *m = file->private_data;
557 struct mnt_namespace *ns = m->private;
559 return seq_release(inode, file);
562 static unsigned mounts_poll(struct file *file, poll_table *wait)
564 struct proc_mounts *p = file->private_data;
565 struct mnt_namespace *ns = p->m.private;
568 poll_wait(file, &ns->poll, wait);
570 spin_lock(&vfsmount_lock);
571 if (p->event != ns->event) {
572 p->event = ns->event;
575 spin_unlock(&vfsmount_lock);
580 static const struct file_operations proc_mounts_operations = {
584 .release = mounts_release,
588 extern struct seq_operations mountstats_op;
589 static int mountstats_open(struct inode *inode, struct file *file)
591 int ret = seq_open(file, &mountstats_op);
594 struct seq_file *m = file->private_data;
596 struct mnt_namespace *mnt_ns = NULL;
597 struct task_struct *task = get_proc_task(inode);
601 nsp = task_nsproxy(task);
603 mnt_ns = nsp->mnt_ns;
609 put_task_struct(task);
615 seq_release(inode, file);
622 static const struct file_operations proc_mountstats_operations = {
623 .open = mountstats_open,
626 .release = mounts_release,
629 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
631 static ssize_t proc_info_read(struct file * file, char __user * buf,
632 size_t count, loff_t *ppos)
634 struct inode * inode = file->f_path.dentry->d_inode;
637 struct task_struct *task = get_proc_task(inode);
643 if (count > PROC_BLOCK_SIZE)
644 count = PROC_BLOCK_SIZE;
647 if (!(page = __get_free_page(GFP_TEMPORARY)))
650 length = PROC_I(inode)->op.proc_read(task, (char*)page);
653 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
656 put_task_struct(task);
661 static const struct file_operations proc_info_file_operations = {
662 .read = proc_info_read,
665 static int mem_open(struct inode* inode, struct file* file)
667 file->private_data = (void*)((long)current->self_exec_id);
671 static ssize_t mem_read(struct file * file, char __user * buf,
672 size_t count, loff_t *ppos)
674 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
676 unsigned long src = *ppos;
678 struct mm_struct *mm;
683 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
687 page = (char *)__get_free_page(GFP_TEMPORARY);
693 mm = get_task_mm(task);
699 if (file->private_data != (void*)((long)current->self_exec_id))
705 int this_len, retval;
707 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
708 retval = access_process_vm(task, src, page, this_len, 0);
709 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
715 if (copy_to_user(buf, page, retval)) {
730 free_page((unsigned long) page);
732 put_task_struct(task);
737 #define mem_write NULL
740 /* This is a security hazard */
741 static ssize_t mem_write(struct file * file, const char __user *buf,
742 size_t count, loff_t *ppos)
746 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
747 unsigned long dst = *ppos;
753 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
757 page = (char *)__get_free_page(GFP_TEMPORARY);
763 int this_len, retval;
765 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
766 if (copy_from_user(page, buf, this_len)) {
770 retval = access_process_vm(task, dst, page, this_len, 1);
782 free_page((unsigned long) page);
784 put_task_struct(task);
790 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
794 file->f_pos = offset;
797 file->f_pos += offset;
802 force_successful_syscall_return();
806 static const struct file_operations proc_mem_operations = {
813 static ssize_t environ_read(struct file *file, char __user *buf,
814 size_t count, loff_t *ppos)
816 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
818 unsigned long src = *ppos;
820 struct mm_struct *mm;
825 if (!ptrace_may_attach(task))
829 page = (char *)__get_free_page(GFP_TEMPORARY);
835 mm = get_task_mm(task);
840 int this_len, retval, max_len;
842 this_len = mm->env_end - (mm->env_start + src);
847 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
848 this_len = (this_len > max_len) ? max_len : this_len;
850 retval = access_process_vm(task, (mm->env_start + src),
858 if (copy_to_user(buf, page, retval)) {
872 free_page((unsigned long) page);
874 put_task_struct(task);
879 static const struct file_operations proc_environ_operations = {
880 .read = environ_read,
883 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
884 size_t count, loff_t *ppos)
886 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
887 char buffer[PROC_NUMBUF];
893 oom_adjust = task->oomkilladj;
894 put_task_struct(task);
896 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
898 return simple_read_from_buffer(buf, count, ppos, buffer, len);
901 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
902 size_t count, loff_t *ppos)
904 struct task_struct *task;
905 char buffer[PROC_NUMBUF], *end;
908 memset(buffer, 0, sizeof(buffer));
909 if (count > sizeof(buffer) - 1)
910 count = sizeof(buffer) - 1;
911 if (copy_from_user(buffer, buf, count))
913 oom_adjust = simple_strtol(buffer, &end, 0);
914 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
915 oom_adjust != OOM_DISABLE)
919 task = get_proc_task(file->f_path.dentry->d_inode);
922 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
923 put_task_struct(task);
926 task->oomkilladj = oom_adjust;
927 put_task_struct(task);
928 if (end - buffer == 0)
933 static const struct file_operations proc_oom_adjust_operations = {
934 .read = oom_adjust_read,
935 .write = oom_adjust_write,
939 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
940 size_t count, loff_t *ppos)
942 struct task_struct *task;
943 char buffer[PROC_NUMBUF], *end;
944 struct mm_struct *mm;
946 memset(buffer, 0, sizeof(buffer));
947 if (count > sizeof(buffer) - 1)
948 count = sizeof(buffer) - 1;
949 if (copy_from_user(buffer, buf, count))
951 if (!simple_strtol(buffer, &end, 0))
955 task = get_proc_task(file->f_path.dentry->d_inode);
958 mm = get_task_mm(task);
963 put_task_struct(task);
964 if (end - buffer == 0)
969 static struct file_operations proc_clear_refs_operations = {
970 .write = clear_refs_write,
974 #ifdef CONFIG_AUDITSYSCALL
976 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
977 size_t count, loff_t *ppos)
979 struct inode * inode = file->f_path.dentry->d_inode;
980 struct task_struct *task = get_proc_task(inode);
982 char tmpbuf[TMPBUFLEN];
986 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
987 audit_get_loginuid(task->audit_context));
988 put_task_struct(task);
989 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
992 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
993 size_t count, loff_t *ppos)
995 struct inode * inode = file->f_path.dentry->d_inode;
1000 if (!capable(CAP_AUDIT_CONTROL))
1003 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1006 if (count >= PAGE_SIZE)
1007 count = PAGE_SIZE - 1;
1010 /* No partial writes. */
1013 page = (char*)__get_free_page(GFP_TEMPORARY);
1017 if (copy_from_user(page, buf, count))
1021 loginuid = simple_strtoul(page, &tmp, 10);
1027 length = audit_set_loginuid(current, loginuid);
1028 if (likely(length == 0))
1032 free_page((unsigned long) page);
1036 static const struct file_operations proc_loginuid_operations = {
1037 .read = proc_loginuid_read,
1038 .write = proc_loginuid_write,
1042 #ifdef CONFIG_FAULT_INJECTION
1043 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1044 size_t count, loff_t *ppos)
1046 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1047 char buffer[PROC_NUMBUF];
1053 make_it_fail = task->make_it_fail;
1054 put_task_struct(task);
1056 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1058 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1061 static ssize_t proc_fault_inject_write(struct file * file,
1062 const char __user * buf, size_t count, loff_t *ppos)
1064 struct task_struct *task;
1065 char buffer[PROC_NUMBUF], *end;
1068 if (!capable(CAP_SYS_RESOURCE))
1070 memset(buffer, 0, sizeof(buffer));
1071 if (count > sizeof(buffer) - 1)
1072 count = sizeof(buffer) - 1;
1073 if (copy_from_user(buffer, buf, count))
1075 make_it_fail = simple_strtol(buffer, &end, 0);
1078 task = get_proc_task(file->f_dentry->d_inode);
1081 task->make_it_fail = make_it_fail;
1082 put_task_struct(task);
1083 if (end - buffer == 0)
1085 return end - buffer;
1088 static const struct file_operations proc_fault_inject_operations = {
1089 .read = proc_fault_inject_read,
1090 .write = proc_fault_inject_write,
1095 #ifdef CONFIG_SCHED_DEBUG
1097 * Print out various scheduling related per-task fields:
1099 static int sched_show(struct seq_file *m, void *v)
1101 struct inode *inode = m->private;
1102 struct task_struct *p;
1106 p = get_proc_task(inode);
1109 proc_sched_show_task(p, m);
1117 sched_write(struct file *file, const char __user *buf,
1118 size_t count, loff_t *offset)
1120 struct inode *inode = file->f_path.dentry->d_inode;
1121 struct task_struct *p;
1125 p = get_proc_task(inode);
1128 proc_sched_set_task(p);
1135 static int sched_open(struct inode *inode, struct file *filp)
1139 ret = single_open(filp, sched_show, NULL);
1141 struct seq_file *m = filp->private_data;
1148 static const struct file_operations proc_pid_sched_operations = {
1151 .write = sched_write,
1152 .llseek = seq_lseek,
1153 .release = single_release,
1158 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1160 struct inode *inode = dentry->d_inode;
1161 int error = -EACCES;
1163 /* We don't need a base pointer in the /proc filesystem */
1166 /* Are we allowed to snoop on the tasks file descriptors? */
1167 if (!proc_fd_access_allowed(inode))
1170 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1171 nd->last_type = LAST_BIND;
1173 return ERR_PTR(error);
1176 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1177 char __user *buffer, int buflen)
1179 struct inode * inode;
1180 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1187 inode = dentry->d_inode;
1188 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1189 len = PTR_ERR(path);
1192 len = tmp + PAGE_SIZE - 1 - path;
1196 if (copy_to_user(buffer, path, len))
1199 free_page((unsigned long)tmp);
1203 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1205 int error = -EACCES;
1206 struct inode *inode = dentry->d_inode;
1208 struct vfsmount *mnt = NULL;
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, &de, &mnt);
1218 error = do_proc_readlink(de, mnt, buffer, buflen);
1225 static const struct inode_operations proc_pid_link_inode_operations = {
1226 .readlink = proc_pid_readlink,
1227 .follow_link = proc_pid_follow_link,
1228 .setattr = proc_setattr,
1232 /* building an inode */
1234 static int task_dumpable(struct task_struct *task)
1237 struct mm_struct *mm;
1242 dumpable = get_dumpable(mm);
1250 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1252 struct inode * inode;
1253 struct proc_inode *ei;
1255 /* We need a new inode */
1257 inode = new_inode(sb);
1263 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1264 inode->i_op = &proc_def_inode_operations;
1267 * grab the reference to task.
1269 ei->pid = get_task_pid(task, PIDTYPE_PID);
1275 if (task_dumpable(task)) {
1276 inode->i_uid = task->euid;
1277 inode->i_gid = task->egid;
1279 security_task_to_inode(task, inode);
1289 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1291 struct inode *inode = dentry->d_inode;
1292 struct task_struct *task;
1293 generic_fillattr(inode, stat);
1298 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1300 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1301 task_dumpable(task)) {
1302 stat->uid = task->euid;
1303 stat->gid = task->egid;
1313 * Exceptional case: normally we are not allowed to unhash a busy
1314 * directory. In this case, however, we can do it - no aliasing problems
1315 * due to the way we treat inodes.
1317 * Rewrite the inode's ownerships here because the owning task may have
1318 * performed a setuid(), etc.
1320 * Before the /proc/pid/status file was created the only way to read
1321 * the effective uid of a /process was to stat /proc/pid. Reading
1322 * /proc/pid/status is slow enough that procps and other packages
1323 * kept stating /proc/pid. To keep the rules in /proc simple I have
1324 * made this apply to all per process world readable and executable
1327 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1329 struct inode *inode = dentry->d_inode;
1330 struct task_struct *task = get_proc_task(inode);
1332 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1333 task_dumpable(task)) {
1334 inode->i_uid = task->euid;
1335 inode->i_gid = task->egid;
1340 inode->i_mode &= ~(S_ISUID | S_ISGID);
1341 security_task_to_inode(task, inode);
1342 put_task_struct(task);
1349 static int pid_delete_dentry(struct dentry * dentry)
1351 /* Is the task we represent dead?
1352 * If so, then don't put the dentry on the lru list,
1353 * kill it immediately.
1355 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1358 static struct dentry_operations pid_dentry_operations =
1360 .d_revalidate = pid_revalidate,
1361 .d_delete = pid_delete_dentry,
1366 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1367 struct task_struct *, const void *);
1370 * Fill a directory entry.
1372 * If possible create the dcache entry and derive our inode number and
1373 * file type from dcache entry.
1375 * Since all of the proc inode numbers are dynamically generated, the inode
1376 * numbers do not exist until the inode is cache. This means creating the
1377 * the dcache entry in readdir is necessary to keep the inode numbers
1378 * reported by readdir in sync with the inode numbers reported
1381 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1382 char *name, int len,
1383 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1385 struct dentry *child, *dir = filp->f_path.dentry;
1386 struct inode *inode;
1389 unsigned type = DT_UNKNOWN;
1393 qname.hash = full_name_hash(name, len);
1395 child = d_lookup(dir, &qname);
1398 new = d_alloc(dir, &qname);
1400 child = instantiate(dir->d_inode, new, task, ptr);
1407 if (!child || IS_ERR(child) || !child->d_inode)
1408 goto end_instantiate;
1409 inode = child->d_inode;
1412 type = inode->i_mode >> 12;
1417 ino = find_inode_number(dir, &qname);
1420 return filldir(dirent, name, len, filp->f_pos, ino, type);
1423 static unsigned name_to_int(struct dentry *dentry)
1425 const char *name = dentry->d_name.name;
1426 int len = dentry->d_name.len;
1429 if (len > 1 && *name == '0')
1432 unsigned c = *name++ - '0';
1435 if (n >= (~0U-9)/10)
1445 #define PROC_FDINFO_MAX 64
1447 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1448 struct vfsmount **mnt, char *info)
1450 struct task_struct *task = get_proc_task(inode);
1451 struct files_struct *files = NULL;
1453 int fd = proc_fd(inode);
1456 files = get_files_struct(task);
1457 put_task_struct(task);
1461 * We are not taking a ref to the file structure, so we must
1464 spin_lock(&files->file_lock);
1465 file = fcheck_files(files, fd);
1468 *mnt = mntget(file->f_path.mnt);
1470 *dentry = dget(file->f_path.dentry);
1472 snprintf(info, PROC_FDINFO_MAX,
1475 (long long) file->f_pos,
1477 spin_unlock(&files->file_lock);
1478 put_files_struct(files);
1481 spin_unlock(&files->file_lock);
1482 put_files_struct(files);
1487 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1488 struct vfsmount **mnt)
1490 return proc_fd_info(inode, dentry, mnt, NULL);
1493 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1495 struct inode *inode = dentry->d_inode;
1496 struct task_struct *task = get_proc_task(inode);
1497 int fd = proc_fd(inode);
1498 struct files_struct *files;
1501 files = get_files_struct(task);
1504 if (fcheck_files(files, fd)) {
1506 put_files_struct(files);
1507 if (task_dumpable(task)) {
1508 inode->i_uid = task->euid;
1509 inode->i_gid = task->egid;
1514 inode->i_mode &= ~(S_ISUID | S_ISGID);
1515 security_task_to_inode(task, inode);
1516 put_task_struct(task);
1520 put_files_struct(files);
1522 put_task_struct(task);
1528 static struct dentry_operations tid_fd_dentry_operations =
1530 .d_revalidate = tid_fd_revalidate,
1531 .d_delete = pid_delete_dentry,
1534 static struct dentry *proc_fd_instantiate(struct inode *dir,
1535 struct dentry *dentry, struct task_struct *task, const void *ptr)
1537 unsigned fd = *(const unsigned *)ptr;
1539 struct files_struct *files;
1540 struct inode *inode;
1541 struct proc_inode *ei;
1542 struct dentry *error = ERR_PTR(-ENOENT);
1544 inode = proc_pid_make_inode(dir->i_sb, task);
1549 files = get_files_struct(task);
1552 inode->i_mode = S_IFLNK;
1555 * We are not taking a ref to the file structure, so we must
1558 spin_lock(&files->file_lock);
1559 file = fcheck_files(files, fd);
1562 if (file->f_mode & 1)
1563 inode->i_mode |= S_IRUSR | S_IXUSR;
1564 if (file->f_mode & 2)
1565 inode->i_mode |= S_IWUSR | S_IXUSR;
1566 spin_unlock(&files->file_lock);
1567 put_files_struct(files);
1569 inode->i_op = &proc_pid_link_inode_operations;
1571 ei->op.proc_get_link = proc_fd_link;
1572 dentry->d_op = &tid_fd_dentry_operations;
1573 d_add(dentry, inode);
1574 /* Close the race of the process dying before we return the dentry */
1575 if (tid_fd_revalidate(dentry, NULL))
1581 spin_unlock(&files->file_lock);
1582 put_files_struct(files);
1588 static struct dentry *proc_lookupfd_common(struct inode *dir,
1589 struct dentry *dentry,
1590 instantiate_t instantiate)
1592 struct task_struct *task = get_proc_task(dir);
1593 unsigned fd = name_to_int(dentry);
1594 struct dentry *result = ERR_PTR(-ENOENT);
1601 result = instantiate(dir, dentry, task, &fd);
1603 put_task_struct(task);
1608 static int proc_readfd_common(struct file * filp, void * dirent,
1609 filldir_t filldir, instantiate_t instantiate)
1611 struct dentry *dentry = filp->f_path.dentry;
1612 struct inode *inode = dentry->d_inode;
1613 struct task_struct *p = get_proc_task(inode);
1614 unsigned int fd, ino;
1616 struct files_struct * files;
1617 struct fdtable *fdt;
1627 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1631 ino = parent_ino(dentry);
1632 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1636 files = get_files_struct(p);
1640 fdt = files_fdtable(files);
1641 for (fd = filp->f_pos-2;
1643 fd++, filp->f_pos++) {
1644 char name[PROC_NUMBUF];
1647 if (!fcheck_files(files, fd))
1651 len = snprintf(name, sizeof(name), "%d", fd);
1652 if (proc_fill_cache(filp, dirent, filldir,
1653 name, len, instantiate,
1661 put_files_struct(files);
1669 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1670 struct nameidata *nd)
1672 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1675 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1677 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1680 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1681 size_t len, loff_t *ppos)
1683 char tmp[PROC_FDINFO_MAX];
1684 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1686 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1690 static const struct file_operations proc_fdinfo_file_operations = {
1691 .open = nonseekable_open,
1692 .read = proc_fdinfo_read,
1695 static const struct file_operations proc_fd_operations = {
1696 .read = generic_read_dir,
1697 .readdir = proc_readfd,
1701 * /proc/pid/fd needs a special permission handler so that a process can still
1702 * access /proc/self/fd after it has executed a setuid().
1704 static int proc_fd_permission(struct inode *inode, int mask,
1705 struct nameidata *nd)
1709 rv = generic_permission(inode, mask, NULL);
1712 if (task_pid(current) == proc_pid(inode))
1718 * proc directories can do almost nothing..
1720 static const struct inode_operations proc_fd_inode_operations = {
1721 .lookup = proc_lookupfd,
1722 .permission = proc_fd_permission,
1723 .setattr = proc_setattr,
1726 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1727 struct dentry *dentry, struct task_struct *task, const void *ptr)
1729 unsigned fd = *(unsigned *)ptr;
1730 struct inode *inode;
1731 struct proc_inode *ei;
1732 struct dentry *error = ERR_PTR(-ENOENT);
1734 inode = proc_pid_make_inode(dir->i_sb, task);
1739 inode->i_mode = S_IFREG | S_IRUSR;
1740 inode->i_fop = &proc_fdinfo_file_operations;
1741 dentry->d_op = &tid_fd_dentry_operations;
1742 d_add(dentry, inode);
1743 /* Close the race of the process dying before we return the dentry */
1744 if (tid_fd_revalidate(dentry, NULL))
1751 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1752 struct dentry *dentry,
1753 struct nameidata *nd)
1755 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1758 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1760 return proc_readfd_common(filp, dirent, filldir,
1761 proc_fdinfo_instantiate);
1764 static const struct file_operations proc_fdinfo_operations = {
1765 .read = generic_read_dir,
1766 .readdir = proc_readfdinfo,
1770 * proc directories can do almost nothing..
1772 static const struct inode_operations proc_fdinfo_inode_operations = {
1773 .lookup = proc_lookupfdinfo,
1774 .setattr = proc_setattr,
1778 static struct dentry *proc_pident_instantiate(struct inode *dir,
1779 struct dentry *dentry, struct task_struct *task, const void *ptr)
1781 const struct pid_entry *p = ptr;
1782 struct inode *inode;
1783 struct proc_inode *ei;
1784 struct dentry *error = ERR_PTR(-EINVAL);
1786 inode = proc_pid_make_inode(dir->i_sb, task);
1791 inode->i_mode = p->mode;
1792 if (S_ISDIR(inode->i_mode))
1793 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1795 inode->i_op = p->iop;
1797 inode->i_fop = p->fop;
1799 dentry->d_op = &pid_dentry_operations;
1800 d_add(dentry, inode);
1801 /* Close the race of the process dying before we return the dentry */
1802 if (pid_revalidate(dentry, NULL))
1808 static struct dentry *proc_pident_lookup(struct inode *dir,
1809 struct dentry *dentry,
1810 const struct pid_entry *ents,
1813 struct inode *inode;
1814 struct dentry *error;
1815 struct task_struct *task = get_proc_task(dir);
1816 const struct pid_entry *p, *last;
1818 error = ERR_PTR(-ENOENT);
1825 * Yes, it does not scale. And it should not. Don't add
1826 * new entries into /proc/<tgid>/ without very good reasons.
1828 last = &ents[nents - 1];
1829 for (p = ents; p <= last; p++) {
1830 if (p->len != dentry->d_name.len)
1832 if (!memcmp(dentry->d_name.name, p->name, p->len))
1838 error = proc_pident_instantiate(dir, dentry, task, p);
1840 put_task_struct(task);
1845 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1846 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1848 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1849 proc_pident_instantiate, task, p);
1852 static int proc_pident_readdir(struct file *filp,
1853 void *dirent, filldir_t filldir,
1854 const struct pid_entry *ents, unsigned int nents)
1857 struct dentry *dentry = filp->f_path.dentry;
1858 struct inode *inode = dentry->d_inode;
1859 struct task_struct *task = get_proc_task(inode);
1860 const struct pid_entry *p, *last;
1873 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1879 ino = parent_ino(dentry);
1880 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1892 last = &ents[nents - 1];
1894 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1903 put_task_struct(task);
1908 #ifdef CONFIG_SECURITY
1909 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1910 size_t count, loff_t *ppos)
1912 struct inode * inode = file->f_path.dentry->d_inode;
1915 struct task_struct *task = get_proc_task(inode);
1920 length = security_getprocattr(task,
1921 (char*)file->f_path.dentry->d_name.name,
1923 put_task_struct(task);
1925 length = simple_read_from_buffer(buf, count, ppos, p, length);
1930 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1931 size_t count, loff_t *ppos)
1933 struct inode * inode = file->f_path.dentry->d_inode;
1936 struct task_struct *task = get_proc_task(inode);
1941 if (count > PAGE_SIZE)
1944 /* No partial writes. */
1950 page = (char*)__get_free_page(GFP_TEMPORARY);
1955 if (copy_from_user(page, buf, count))
1958 length = security_setprocattr(task,
1959 (char*)file->f_path.dentry->d_name.name,
1960 (void*)page, count);
1962 free_page((unsigned long) page);
1964 put_task_struct(task);
1969 static const struct file_operations proc_pid_attr_operations = {
1970 .read = proc_pid_attr_read,
1971 .write = proc_pid_attr_write,
1974 static const struct pid_entry attr_dir_stuff[] = {
1975 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1976 REG("prev", S_IRUGO, pid_attr),
1977 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1978 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1979 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1980 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1983 static int proc_attr_dir_readdir(struct file * filp,
1984 void * dirent, filldir_t filldir)
1986 return proc_pident_readdir(filp,dirent,filldir,
1987 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1990 static const struct file_operations proc_attr_dir_operations = {
1991 .read = generic_read_dir,
1992 .readdir = proc_attr_dir_readdir,
1995 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1996 struct dentry *dentry, struct nameidata *nd)
1998 return proc_pident_lookup(dir, dentry,
1999 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2002 static const struct inode_operations proc_attr_dir_inode_operations = {
2003 .lookup = proc_attr_dir_lookup,
2004 .getattr = pid_getattr,
2005 .setattr = proc_setattr,
2010 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2011 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2012 size_t count, loff_t *ppos)
2014 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2015 struct mm_struct *mm;
2016 char buffer[PROC_NUMBUF];
2024 mm = get_task_mm(task);
2026 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2027 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2028 MMF_DUMP_FILTER_SHIFT));
2030 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2033 put_task_struct(task);
2038 static ssize_t proc_coredump_filter_write(struct file *file,
2039 const char __user *buf,
2043 struct task_struct *task;
2044 struct mm_struct *mm;
2045 char buffer[PROC_NUMBUF], *end;
2052 memset(buffer, 0, sizeof(buffer));
2053 if (count > sizeof(buffer) - 1)
2054 count = sizeof(buffer) - 1;
2055 if (copy_from_user(buffer, buf, count))
2059 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2062 if (end - buffer == 0)
2066 task = get_proc_task(file->f_dentry->d_inode);
2071 mm = get_task_mm(task);
2075 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2077 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2079 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2084 put_task_struct(task);
2089 static const struct file_operations proc_coredump_filter_operations = {
2090 .read = proc_coredump_filter_read,
2091 .write = proc_coredump_filter_write,
2098 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2101 char tmp[PROC_NUMBUF];
2102 sprintf(tmp, "%d", task_tgid_vnr(current));
2103 return vfs_readlink(dentry,buffer,buflen,tmp);
2106 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2108 char tmp[PROC_NUMBUF];
2109 sprintf(tmp, "%d", task_tgid_vnr(current));
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 INF("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 INF("stat", S_IRUGO, tgid_stat),
2281 INF("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),
2293 REG("clear_refs", S_IWUSR, clear_refs),
2294 REG("smaps", S_IRUGO, smaps),
2296 #ifdef CONFIG_SECURITY
2297 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2299 #ifdef CONFIG_KALLSYMS
2300 INF("wchan", S_IRUGO, pid_wchan),
2302 #ifdef CONFIG_SCHEDSTATS
2303 INF("schedstat", S_IRUGO, pid_schedstat),
2305 #ifdef CONFIG_LATENCYTOP
2306 REG("latency", S_IRUGO, lstats),
2308 #ifdef CONFIG_PROC_PID_CPUSET
2309 REG("cpuset", S_IRUGO, cpuset),
2311 #ifdef CONFIG_CGROUPS
2312 REG("cgroup", S_IRUGO, cgroup),
2314 INF("oom_score", S_IRUGO, oom_score),
2315 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2316 #ifdef CONFIG_AUDITSYSCALL
2317 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2319 #ifdef CONFIG_FAULT_INJECTION
2320 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2322 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2323 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2325 #ifdef CONFIG_TASK_IO_ACCOUNTING
2326 INF("io", S_IRUGO, pid_io_accounting),
2330 static int proc_tgid_base_readdir(struct file * filp,
2331 void * dirent, filldir_t filldir)
2333 return proc_pident_readdir(filp,dirent,filldir,
2334 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2337 static const struct file_operations proc_tgid_base_operations = {
2338 .read = generic_read_dir,
2339 .readdir = proc_tgid_base_readdir,
2342 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2343 return proc_pident_lookup(dir, dentry,
2344 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2347 static const struct inode_operations proc_tgid_base_inode_operations = {
2348 .lookup = proc_tgid_base_lookup,
2349 .getattr = pid_getattr,
2350 .setattr = proc_setattr,
2353 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2355 struct dentry *dentry, *leader, *dir;
2356 char buf[PROC_NUMBUF];
2360 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2361 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2363 shrink_dcache_parent(dentry);
2372 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2373 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2378 name.len = strlen(name.name);
2379 dir = d_hash_and_lookup(leader, &name);
2381 goto out_put_leader;
2384 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2385 dentry = d_hash_and_lookup(dir, &name);
2387 shrink_dcache_parent(dentry);
2400 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2401 * @task: task that should be flushed.
2403 * When flushing dentries from proc, one needs to flush them from global
2404 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2405 * in. This call is supposed to do all of this job.
2407 * Looks in the dcache for
2409 * /proc/@tgid/task/@pid
2410 * if either directory is present flushes it and all of it'ts children
2413 * It is safe and reasonable to cache /proc entries for a task until
2414 * that task exits. After that they just clog up the dcache with
2415 * useless entries, possibly causing useful dcache entries to be
2416 * flushed instead. This routine is proved to flush those useless
2417 * dcache entries at process exit time.
2419 * NOTE: This routine is just an optimization so it does not guarantee
2420 * that no dcache entries will exist at process exit time it
2421 * just makes it very unlikely that any will persist.
2424 void proc_flush_task(struct task_struct *task)
2427 struct pid *pid, *tgid = NULL;
2430 pid = task_pid(task);
2431 if (thread_group_leader(task))
2432 tgid = task_tgid(task);
2434 for (i = 0; i <= pid->level; i++) {
2435 upid = &pid->numbers[i];
2436 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2437 tgid ? tgid->numbers[i].nr : 0);
2440 upid = &pid->numbers[pid->level];
2442 pid_ns_release_proc(upid->ns);
2445 static struct dentry *proc_pid_instantiate(struct inode *dir,
2446 struct dentry * dentry,
2447 struct task_struct *task, const void *ptr)
2449 struct dentry *error = ERR_PTR(-ENOENT);
2450 struct inode *inode;
2452 inode = proc_pid_make_inode(dir->i_sb, task);
2456 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2457 inode->i_op = &proc_tgid_base_inode_operations;
2458 inode->i_fop = &proc_tgid_base_operations;
2459 inode->i_flags|=S_IMMUTABLE;
2461 #ifdef CONFIG_SECURITY
2462 inode->i_nlink += 1;
2465 dentry->d_op = &pid_dentry_operations;
2467 d_add(dentry, inode);
2468 /* Close the race of the process dying before we return the dentry */
2469 if (pid_revalidate(dentry, NULL))
2475 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2477 struct dentry *result = ERR_PTR(-ENOENT);
2478 struct task_struct *task;
2480 struct pid_namespace *ns;
2482 result = proc_base_lookup(dir, dentry);
2483 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2486 tgid = name_to_int(dentry);
2490 ns = dentry->d_sb->s_fs_info;
2492 task = find_task_by_pid_ns(tgid, ns);
2494 get_task_struct(task);
2499 result = proc_pid_instantiate(dir, dentry, task, NULL);
2500 put_task_struct(task);
2506 * Find the first task with tgid >= tgid
2511 struct task_struct *task;
2513 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2518 put_task_struct(iter.task);
2522 pid = find_ge_pid(iter.tgid, ns);
2524 iter.tgid = pid_nr_ns(pid, ns);
2525 iter.task = pid_task(pid, PIDTYPE_PID);
2526 /* What we to know is if the pid we have find is the
2527 * pid of a thread_group_leader. Testing for task
2528 * being a thread_group_leader is the obvious thing
2529 * todo but there is a window when it fails, due to
2530 * the pid transfer logic in de_thread.
2532 * So we perform the straight forward test of seeing
2533 * if the pid we have found is the pid of a thread
2534 * group leader, and don't worry if the task we have
2535 * found doesn't happen to be a thread group leader.
2536 * As we don't care in the case of readdir.
2538 if (!iter.task || !has_group_leader_pid(iter.task)) {
2542 get_task_struct(iter.task);
2548 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2550 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2551 struct tgid_iter iter)
2553 char name[PROC_NUMBUF];
2554 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2555 return proc_fill_cache(filp, dirent, filldir, name, len,
2556 proc_pid_instantiate, iter.task, NULL);
2559 /* for the /proc/ directory itself, after non-process stuff has been done */
2560 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2562 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2563 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2564 struct tgid_iter iter;
2565 struct pid_namespace *ns;
2570 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2571 const struct pid_entry *p = &proc_base_stuff[nr];
2572 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2576 ns = filp->f_dentry->d_sb->s_fs_info;
2578 iter.tgid = filp->f_pos - TGID_OFFSET;
2579 for (iter = next_tgid(ns, iter);
2581 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2582 filp->f_pos = iter.tgid + TGID_OFFSET;
2583 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2584 put_task_struct(iter.task);
2588 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2590 put_task_struct(reaper);
2598 static const struct pid_entry tid_base_stuff[] = {
2599 DIR("fd", S_IRUSR|S_IXUSR, fd),
2600 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2601 REG("environ", S_IRUSR, environ),
2602 INF("auxv", S_IRUSR, pid_auxv),
2603 INF("status", S_IRUGO, pid_status),
2604 INF("limits", S_IRUSR, pid_limits),
2605 #ifdef CONFIG_SCHED_DEBUG
2606 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2608 INF("cmdline", S_IRUGO, pid_cmdline),
2609 INF("stat", S_IRUGO, tid_stat),
2610 INF("statm", S_IRUGO, pid_statm),
2611 REG("maps", S_IRUGO, maps),
2613 REG("numa_maps", S_IRUGO, numa_maps),
2615 REG("mem", S_IRUSR|S_IWUSR, mem),
2619 REG("mounts", S_IRUGO, mounts),
2621 REG("clear_refs", S_IWUSR, clear_refs),
2622 REG("smaps", S_IRUGO, smaps),
2624 #ifdef CONFIG_SECURITY
2625 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2627 #ifdef CONFIG_KALLSYMS
2628 INF("wchan", S_IRUGO, pid_wchan),
2630 #ifdef CONFIG_SCHEDSTATS
2631 INF("schedstat", S_IRUGO, pid_schedstat),
2633 #ifdef CONFIG_LATENCYTOP
2634 REG("latency", S_IRUGO, lstats),
2636 #ifdef CONFIG_PROC_PID_CPUSET
2637 REG("cpuset", S_IRUGO, cpuset),
2639 #ifdef CONFIG_CGROUPS
2640 REG("cgroup", S_IRUGO, cgroup),
2642 INF("oom_score", S_IRUGO, oom_score),
2643 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2644 #ifdef CONFIG_AUDITSYSCALL
2645 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2647 #ifdef CONFIG_FAULT_INJECTION
2648 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2652 static int proc_tid_base_readdir(struct file * filp,
2653 void * dirent, filldir_t filldir)
2655 return proc_pident_readdir(filp,dirent,filldir,
2656 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2659 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2660 return proc_pident_lookup(dir, dentry,
2661 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2664 static const struct file_operations proc_tid_base_operations = {
2665 .read = generic_read_dir,
2666 .readdir = proc_tid_base_readdir,
2669 static const struct inode_operations proc_tid_base_inode_operations = {
2670 .lookup = proc_tid_base_lookup,
2671 .getattr = pid_getattr,
2672 .setattr = proc_setattr,
2675 static struct dentry *proc_task_instantiate(struct inode *dir,
2676 struct dentry *dentry, struct task_struct *task, const void *ptr)
2678 struct dentry *error = ERR_PTR(-ENOENT);
2679 struct inode *inode;
2680 inode = proc_pid_make_inode(dir->i_sb, task);
2684 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2685 inode->i_op = &proc_tid_base_inode_operations;
2686 inode->i_fop = &proc_tid_base_operations;
2687 inode->i_flags|=S_IMMUTABLE;
2689 #ifdef CONFIG_SECURITY
2690 inode->i_nlink += 1;
2693 dentry->d_op = &pid_dentry_operations;
2695 d_add(dentry, inode);
2696 /* Close the race of the process dying before we return the dentry */
2697 if (pid_revalidate(dentry, NULL))
2703 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2705 struct dentry *result = ERR_PTR(-ENOENT);
2706 struct task_struct *task;
2707 struct task_struct *leader = get_proc_task(dir);
2709 struct pid_namespace *ns;
2714 tid = name_to_int(dentry);
2718 ns = dentry->d_sb->s_fs_info;
2720 task = find_task_by_pid_ns(tid, ns);
2722 get_task_struct(task);
2726 if (!same_thread_group(leader, task))
2729 result = proc_task_instantiate(dir, dentry, task, NULL);
2731 put_task_struct(task);
2733 put_task_struct(leader);
2739 * Find the first tid of a thread group to return to user space.
2741 * Usually this is just the thread group leader, but if the users
2742 * buffer was too small or there was a seek into the middle of the
2743 * directory we have more work todo.
2745 * In the case of a short read we start with find_task_by_pid.
2747 * In the case of a seek we start with the leader and walk nr
2750 static struct task_struct *first_tid(struct task_struct *leader,
2751 int tid, int nr, struct pid_namespace *ns)
2753 struct task_struct *pos;
2756 /* Attempt to start with the pid of a thread */
2757 if (tid && (nr > 0)) {
2758 pos = find_task_by_pid_ns(tid, ns);
2759 if (pos && (pos->group_leader == leader))
2763 /* If nr exceeds the number of threads there is nothing todo */
2765 if (nr && nr >= get_nr_threads(leader))
2768 /* If we haven't found our starting place yet start
2769 * with the leader and walk nr threads forward.
2771 for (pos = leader; nr > 0; --nr) {
2772 pos = next_thread(pos);
2773 if (pos == leader) {
2779 get_task_struct(pos);
2786 * Find the next thread in the thread list.
2787 * Return NULL if there is an error or no next thread.
2789 * The reference to the input task_struct is released.
2791 static struct task_struct *next_tid(struct task_struct *start)
2793 struct task_struct *pos = NULL;
2795 if (pid_alive(start)) {
2796 pos = next_thread(start);
2797 if (thread_group_leader(pos))
2800 get_task_struct(pos);
2803 put_task_struct(start);
2807 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2808 struct task_struct *task, int tid)
2810 char name[PROC_NUMBUF];
2811 int len = snprintf(name, sizeof(name), "%d", tid);
2812 return proc_fill_cache(filp, dirent, filldir, name, len,
2813 proc_task_instantiate, task, NULL);
2816 /* for the /proc/TGID/task/ directories */
2817 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2819 struct dentry *dentry = filp->f_path.dentry;
2820 struct inode *inode = dentry->d_inode;
2821 struct task_struct *leader = NULL;
2822 struct task_struct *task;
2823 int retval = -ENOENT;
2826 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2827 struct pid_namespace *ns;
2829 task = get_proc_task(inode);
2833 if (pid_alive(task)) {
2834 leader = task->group_leader;
2835 get_task_struct(leader);
2838 put_task_struct(task);
2846 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2851 ino = parent_ino(dentry);
2852 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2858 /* f_version caches the tgid value that the last readdir call couldn't
2859 * return. lseek aka telldir automagically resets f_version to 0.
2861 ns = filp->f_dentry->d_sb->s_fs_info;
2862 tid = (int)filp->f_version;
2863 filp->f_version = 0;
2864 for (task = first_tid(leader, tid, pos - 2, ns);
2866 task = next_tid(task), pos++) {
2867 tid = task_pid_nr_ns(task, ns);
2868 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2869 /* returning this tgid failed, save it as the first
2870 * pid for the next readir call */
2871 filp->f_version = (u64)tid;
2872 put_task_struct(task);
2878 put_task_struct(leader);
2883 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2885 struct inode *inode = dentry->d_inode;
2886 struct task_struct *p = get_proc_task(inode);
2887 generic_fillattr(inode, stat);
2891 stat->nlink += get_nr_threads(p);
2899 static const struct inode_operations proc_task_inode_operations = {
2900 .lookup = proc_task_lookup,
2901 .getattr = proc_task_getattr,
2902 .setattr = proc_setattr,
2905 static const struct file_operations proc_task_operations = {
2906 .read = generic_read_dir,
2907 .readdir = proc_task_readdir,