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 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_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},
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 proc_single_show(struct seq_file *m, void *v)
667 struct inode *inode = m->private;
668 struct pid_namespace *ns;
670 struct task_struct *task;
673 ns = inode->i_sb->s_fs_info;
674 pid = proc_pid(inode);
675 task = get_pid_task(pid, PIDTYPE_PID);
679 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
681 put_task_struct(task);
685 static int proc_single_open(struct inode *inode, struct file *filp)
688 ret = single_open(filp, proc_single_show, NULL);
690 struct seq_file *m = filp->private_data;
697 static const struct file_operations proc_single_file_operations = {
698 .open = proc_single_open,
701 .release = single_release,
704 static int mem_open(struct inode* inode, struct file* file)
706 file->private_data = (void*)((long)current->self_exec_id);
710 static ssize_t mem_read(struct file * file, char __user * buf,
711 size_t count, loff_t *ppos)
713 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
715 unsigned long src = *ppos;
717 struct mm_struct *mm;
722 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
726 page = (char *)__get_free_page(GFP_TEMPORARY);
732 mm = get_task_mm(task);
738 if (file->private_data != (void*)((long)current->self_exec_id))
744 int this_len, retval;
746 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
747 retval = access_process_vm(task, src, page, this_len, 0);
748 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
754 if (copy_to_user(buf, page, retval)) {
769 free_page((unsigned long) page);
771 put_task_struct(task);
776 #define mem_write NULL
779 /* This is a security hazard */
780 static ssize_t mem_write(struct file * file, const char __user *buf,
781 size_t count, loff_t *ppos)
785 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
786 unsigned long dst = *ppos;
792 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
796 page = (char *)__get_free_page(GFP_TEMPORARY);
802 int this_len, retval;
804 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
805 if (copy_from_user(page, buf, this_len)) {
809 retval = access_process_vm(task, dst, page, this_len, 1);
821 free_page((unsigned long) page);
823 put_task_struct(task);
829 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
833 file->f_pos = offset;
836 file->f_pos += offset;
841 force_successful_syscall_return();
845 static const struct file_operations proc_mem_operations = {
852 static ssize_t environ_read(struct file *file, char __user *buf,
853 size_t count, loff_t *ppos)
855 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
857 unsigned long src = *ppos;
859 struct mm_struct *mm;
864 if (!ptrace_may_attach(task))
868 page = (char *)__get_free_page(GFP_TEMPORARY);
874 mm = get_task_mm(task);
879 int this_len, retval, max_len;
881 this_len = mm->env_end - (mm->env_start + src);
886 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
887 this_len = (this_len > max_len) ? max_len : this_len;
889 retval = access_process_vm(task, (mm->env_start + src),
897 if (copy_to_user(buf, page, retval)) {
911 free_page((unsigned long) page);
913 put_task_struct(task);
918 static const struct file_operations proc_environ_operations = {
919 .read = environ_read,
922 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
923 size_t count, loff_t *ppos)
925 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
926 char buffer[PROC_NUMBUF];
932 oom_adjust = task->oomkilladj;
933 put_task_struct(task);
935 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
937 return simple_read_from_buffer(buf, count, ppos, buffer, len);
940 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
941 size_t count, loff_t *ppos)
943 struct task_struct *task;
944 char buffer[PROC_NUMBUF], *end;
947 memset(buffer, 0, sizeof(buffer));
948 if (count > sizeof(buffer) - 1)
949 count = sizeof(buffer) - 1;
950 if (copy_from_user(buffer, buf, count))
952 oom_adjust = simple_strtol(buffer, &end, 0);
953 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
954 oom_adjust != OOM_DISABLE)
958 task = get_proc_task(file->f_path.dentry->d_inode);
961 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
962 put_task_struct(task);
965 task->oomkilladj = oom_adjust;
966 put_task_struct(task);
967 if (end - buffer == 0)
972 static const struct file_operations proc_oom_adjust_operations = {
973 .read = oom_adjust_read,
974 .write = oom_adjust_write,
977 #ifdef CONFIG_AUDITSYSCALL
979 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
980 size_t count, loff_t *ppos)
982 struct inode * inode = file->f_path.dentry->d_inode;
983 struct task_struct *task = get_proc_task(inode);
985 char tmpbuf[TMPBUFLEN];
989 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
990 audit_get_loginuid(task));
991 put_task_struct(task);
992 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
995 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
996 size_t count, loff_t *ppos)
998 struct inode * inode = file->f_path.dentry->d_inode;
1003 if (!capable(CAP_AUDIT_CONTROL))
1006 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1009 if (count >= PAGE_SIZE)
1010 count = PAGE_SIZE - 1;
1013 /* No partial writes. */
1016 page = (char*)__get_free_page(GFP_TEMPORARY);
1020 if (copy_from_user(page, buf, count))
1024 loginuid = simple_strtoul(page, &tmp, 10);
1030 length = audit_set_loginuid(current, loginuid);
1031 if (likely(length == 0))
1035 free_page((unsigned long) page);
1039 static const struct file_operations proc_loginuid_operations = {
1040 .read = proc_loginuid_read,
1041 .write = proc_loginuid_write,
1045 #ifdef CONFIG_FAULT_INJECTION
1046 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1047 size_t count, loff_t *ppos)
1049 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1050 char buffer[PROC_NUMBUF];
1056 make_it_fail = task->make_it_fail;
1057 put_task_struct(task);
1059 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1061 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1064 static ssize_t proc_fault_inject_write(struct file * file,
1065 const char __user * buf, size_t count, loff_t *ppos)
1067 struct task_struct *task;
1068 char buffer[PROC_NUMBUF], *end;
1071 if (!capable(CAP_SYS_RESOURCE))
1073 memset(buffer, 0, sizeof(buffer));
1074 if (count > sizeof(buffer) - 1)
1075 count = sizeof(buffer) - 1;
1076 if (copy_from_user(buffer, buf, count))
1078 make_it_fail = simple_strtol(buffer, &end, 0);
1081 task = get_proc_task(file->f_dentry->d_inode);
1084 task->make_it_fail = make_it_fail;
1085 put_task_struct(task);
1086 if (end - buffer == 0)
1088 return end - buffer;
1091 static const struct file_operations proc_fault_inject_operations = {
1092 .read = proc_fault_inject_read,
1093 .write = proc_fault_inject_write,
1098 #ifdef CONFIG_SCHED_DEBUG
1100 * Print out various scheduling related per-task fields:
1102 static int sched_show(struct seq_file *m, void *v)
1104 struct inode *inode = m->private;
1105 struct task_struct *p;
1109 p = get_proc_task(inode);
1112 proc_sched_show_task(p, m);
1120 sched_write(struct file *file, const char __user *buf,
1121 size_t count, loff_t *offset)
1123 struct inode *inode = file->f_path.dentry->d_inode;
1124 struct task_struct *p;
1128 p = get_proc_task(inode);
1131 proc_sched_set_task(p);
1138 static int sched_open(struct inode *inode, struct file *filp)
1142 ret = single_open(filp, sched_show, NULL);
1144 struct seq_file *m = filp->private_data;
1151 static const struct file_operations proc_pid_sched_operations = {
1154 .write = sched_write,
1155 .llseek = seq_lseek,
1156 .release = single_release,
1161 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1163 struct inode *inode = dentry->d_inode;
1164 int error = -EACCES;
1166 /* We don't need a base pointer in the /proc filesystem */
1169 /* Are we allowed to snoop on the tasks file descriptors? */
1170 if (!proc_fd_access_allowed(inode))
1173 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1174 nd->last_type = LAST_BIND;
1176 return ERR_PTR(error);
1179 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1180 char __user *buffer, int buflen)
1182 struct inode * inode;
1183 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1190 inode = dentry->d_inode;
1191 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1192 len = PTR_ERR(path);
1195 len = tmp + PAGE_SIZE - 1 - path;
1199 if (copy_to_user(buffer, path, len))
1202 free_page((unsigned long)tmp);
1206 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1208 int error = -EACCES;
1209 struct inode *inode = dentry->d_inode;
1211 struct vfsmount *mnt = NULL;
1213 /* Are we allowed to snoop on the tasks file descriptors? */
1214 if (!proc_fd_access_allowed(inode))
1217 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1221 error = do_proc_readlink(de, mnt, buffer, buflen);
1228 static const struct inode_operations proc_pid_link_inode_operations = {
1229 .readlink = proc_pid_readlink,
1230 .follow_link = proc_pid_follow_link,
1231 .setattr = proc_setattr,
1235 /* building an inode */
1237 static int task_dumpable(struct task_struct *task)
1240 struct mm_struct *mm;
1245 dumpable = get_dumpable(mm);
1253 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1255 struct inode * inode;
1256 struct proc_inode *ei;
1258 /* We need a new inode */
1260 inode = new_inode(sb);
1266 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1267 inode->i_op = &proc_def_inode_operations;
1270 * grab the reference to task.
1272 ei->pid = get_task_pid(task, PIDTYPE_PID);
1278 if (task_dumpable(task)) {
1279 inode->i_uid = task->euid;
1280 inode->i_gid = task->egid;
1282 security_task_to_inode(task, inode);
1292 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1294 struct inode *inode = dentry->d_inode;
1295 struct task_struct *task;
1296 generic_fillattr(inode, stat);
1301 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1303 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1304 task_dumpable(task)) {
1305 stat->uid = task->euid;
1306 stat->gid = task->egid;
1316 * Exceptional case: normally we are not allowed to unhash a busy
1317 * directory. In this case, however, we can do it - no aliasing problems
1318 * due to the way we treat inodes.
1320 * Rewrite the inode's ownerships here because the owning task may have
1321 * performed a setuid(), etc.
1323 * Before the /proc/pid/status file was created the only way to read
1324 * the effective uid of a /process was to stat /proc/pid. Reading
1325 * /proc/pid/status is slow enough that procps and other packages
1326 * kept stating /proc/pid. To keep the rules in /proc simple I have
1327 * made this apply to all per process world readable and executable
1330 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1332 struct inode *inode = dentry->d_inode;
1333 struct task_struct *task = get_proc_task(inode);
1335 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1336 task_dumpable(task)) {
1337 inode->i_uid = task->euid;
1338 inode->i_gid = task->egid;
1343 inode->i_mode &= ~(S_ISUID | S_ISGID);
1344 security_task_to_inode(task, inode);
1345 put_task_struct(task);
1352 static int pid_delete_dentry(struct dentry * dentry)
1354 /* Is the task we represent dead?
1355 * If so, then don't put the dentry on the lru list,
1356 * kill it immediately.
1358 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1361 static struct dentry_operations pid_dentry_operations =
1363 .d_revalidate = pid_revalidate,
1364 .d_delete = pid_delete_dentry,
1369 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1370 struct task_struct *, const void *);
1373 * Fill a directory entry.
1375 * If possible create the dcache entry and derive our inode number and
1376 * file type from dcache entry.
1378 * Since all of the proc inode numbers are dynamically generated, the inode
1379 * numbers do not exist until the inode is cache. This means creating the
1380 * the dcache entry in readdir is necessary to keep the inode numbers
1381 * reported by readdir in sync with the inode numbers reported
1384 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1385 char *name, int len,
1386 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1388 struct dentry *child, *dir = filp->f_path.dentry;
1389 struct inode *inode;
1392 unsigned type = DT_UNKNOWN;
1396 qname.hash = full_name_hash(name, len);
1398 child = d_lookup(dir, &qname);
1401 new = d_alloc(dir, &qname);
1403 child = instantiate(dir->d_inode, new, task, ptr);
1410 if (!child || IS_ERR(child) || !child->d_inode)
1411 goto end_instantiate;
1412 inode = child->d_inode;
1415 type = inode->i_mode >> 12;
1420 ino = find_inode_number(dir, &qname);
1423 return filldir(dirent, name, len, filp->f_pos, ino, type);
1426 static unsigned name_to_int(struct dentry *dentry)
1428 const char *name = dentry->d_name.name;
1429 int len = dentry->d_name.len;
1432 if (len > 1 && *name == '0')
1435 unsigned c = *name++ - '0';
1438 if (n >= (~0U-9)/10)
1448 #define PROC_FDINFO_MAX 64
1450 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1451 struct vfsmount **mnt, char *info)
1453 struct task_struct *task = get_proc_task(inode);
1454 struct files_struct *files = NULL;
1456 int fd = proc_fd(inode);
1459 files = get_files_struct(task);
1460 put_task_struct(task);
1464 * We are not taking a ref to the file structure, so we must
1467 spin_lock(&files->file_lock);
1468 file = fcheck_files(files, fd);
1471 *mnt = mntget(file->f_path.mnt);
1473 *dentry = dget(file->f_path.dentry);
1475 snprintf(info, PROC_FDINFO_MAX,
1478 (long long) file->f_pos,
1480 spin_unlock(&files->file_lock);
1481 put_files_struct(files);
1484 spin_unlock(&files->file_lock);
1485 put_files_struct(files);
1490 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1491 struct vfsmount **mnt)
1493 return proc_fd_info(inode, dentry, mnt, NULL);
1496 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1498 struct inode *inode = dentry->d_inode;
1499 struct task_struct *task = get_proc_task(inode);
1500 int fd = proc_fd(inode);
1501 struct files_struct *files;
1504 files = get_files_struct(task);
1507 if (fcheck_files(files, fd)) {
1509 put_files_struct(files);
1510 if (task_dumpable(task)) {
1511 inode->i_uid = task->euid;
1512 inode->i_gid = task->egid;
1517 inode->i_mode &= ~(S_ISUID | S_ISGID);
1518 security_task_to_inode(task, inode);
1519 put_task_struct(task);
1523 put_files_struct(files);
1525 put_task_struct(task);
1531 static struct dentry_operations tid_fd_dentry_operations =
1533 .d_revalidate = tid_fd_revalidate,
1534 .d_delete = pid_delete_dentry,
1537 static struct dentry *proc_fd_instantiate(struct inode *dir,
1538 struct dentry *dentry, struct task_struct *task, const void *ptr)
1540 unsigned fd = *(const unsigned *)ptr;
1542 struct files_struct *files;
1543 struct inode *inode;
1544 struct proc_inode *ei;
1545 struct dentry *error = ERR_PTR(-ENOENT);
1547 inode = proc_pid_make_inode(dir->i_sb, task);
1552 files = get_files_struct(task);
1555 inode->i_mode = S_IFLNK;
1558 * We are not taking a ref to the file structure, so we must
1561 spin_lock(&files->file_lock);
1562 file = fcheck_files(files, fd);
1565 if (file->f_mode & 1)
1566 inode->i_mode |= S_IRUSR | S_IXUSR;
1567 if (file->f_mode & 2)
1568 inode->i_mode |= S_IWUSR | S_IXUSR;
1569 spin_unlock(&files->file_lock);
1570 put_files_struct(files);
1572 inode->i_op = &proc_pid_link_inode_operations;
1574 ei->op.proc_get_link = proc_fd_link;
1575 dentry->d_op = &tid_fd_dentry_operations;
1576 d_add(dentry, inode);
1577 /* Close the race of the process dying before we return the dentry */
1578 if (tid_fd_revalidate(dentry, NULL))
1584 spin_unlock(&files->file_lock);
1585 put_files_struct(files);
1591 static struct dentry *proc_lookupfd_common(struct inode *dir,
1592 struct dentry *dentry,
1593 instantiate_t instantiate)
1595 struct task_struct *task = get_proc_task(dir);
1596 unsigned fd = name_to_int(dentry);
1597 struct dentry *result = ERR_PTR(-ENOENT);
1604 result = instantiate(dir, dentry, task, &fd);
1606 put_task_struct(task);
1611 static int proc_readfd_common(struct file * filp, void * dirent,
1612 filldir_t filldir, instantiate_t instantiate)
1614 struct dentry *dentry = filp->f_path.dentry;
1615 struct inode *inode = dentry->d_inode;
1616 struct task_struct *p = get_proc_task(inode);
1617 unsigned int fd, ino;
1619 struct files_struct * files;
1620 struct fdtable *fdt;
1630 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1634 ino = parent_ino(dentry);
1635 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1639 files = get_files_struct(p);
1643 fdt = files_fdtable(files);
1644 for (fd = filp->f_pos-2;
1646 fd++, filp->f_pos++) {
1647 char name[PROC_NUMBUF];
1650 if (!fcheck_files(files, fd))
1654 len = snprintf(name, sizeof(name), "%d", fd);
1655 if (proc_fill_cache(filp, dirent, filldir,
1656 name, len, instantiate,
1664 put_files_struct(files);
1672 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1673 struct nameidata *nd)
1675 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1678 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1680 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1683 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1684 size_t len, loff_t *ppos)
1686 char tmp[PROC_FDINFO_MAX];
1687 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1689 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1693 static const struct file_operations proc_fdinfo_file_operations = {
1694 .open = nonseekable_open,
1695 .read = proc_fdinfo_read,
1698 static const struct file_operations proc_fd_operations = {
1699 .read = generic_read_dir,
1700 .readdir = proc_readfd,
1704 * /proc/pid/fd needs a special permission handler so that a process can still
1705 * access /proc/self/fd after it has executed a setuid().
1707 static int proc_fd_permission(struct inode *inode, int mask,
1708 struct nameidata *nd)
1712 rv = generic_permission(inode, mask, NULL);
1715 if (task_pid(current) == proc_pid(inode))
1721 * proc directories can do almost nothing..
1723 static const struct inode_operations proc_fd_inode_operations = {
1724 .lookup = proc_lookupfd,
1725 .permission = proc_fd_permission,
1726 .setattr = proc_setattr,
1729 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1730 struct dentry *dentry, struct task_struct *task, const void *ptr)
1732 unsigned fd = *(unsigned *)ptr;
1733 struct inode *inode;
1734 struct proc_inode *ei;
1735 struct dentry *error = ERR_PTR(-ENOENT);
1737 inode = proc_pid_make_inode(dir->i_sb, task);
1742 inode->i_mode = S_IFREG | S_IRUSR;
1743 inode->i_fop = &proc_fdinfo_file_operations;
1744 dentry->d_op = &tid_fd_dentry_operations;
1745 d_add(dentry, inode);
1746 /* Close the race of the process dying before we return the dentry */
1747 if (tid_fd_revalidate(dentry, NULL))
1754 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1755 struct dentry *dentry,
1756 struct nameidata *nd)
1758 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1761 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1763 return proc_readfd_common(filp, dirent, filldir,
1764 proc_fdinfo_instantiate);
1767 static const struct file_operations proc_fdinfo_operations = {
1768 .read = generic_read_dir,
1769 .readdir = proc_readfdinfo,
1773 * proc directories can do almost nothing..
1775 static const struct inode_operations proc_fdinfo_inode_operations = {
1776 .lookup = proc_lookupfdinfo,
1777 .setattr = proc_setattr,
1781 static struct dentry *proc_pident_instantiate(struct inode *dir,
1782 struct dentry *dentry, struct task_struct *task, const void *ptr)
1784 const struct pid_entry *p = ptr;
1785 struct inode *inode;
1786 struct proc_inode *ei;
1787 struct dentry *error = ERR_PTR(-EINVAL);
1789 inode = proc_pid_make_inode(dir->i_sb, task);
1794 inode->i_mode = p->mode;
1795 if (S_ISDIR(inode->i_mode))
1796 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1798 inode->i_op = p->iop;
1800 inode->i_fop = p->fop;
1802 dentry->d_op = &pid_dentry_operations;
1803 d_add(dentry, inode);
1804 /* Close the race of the process dying before we return the dentry */
1805 if (pid_revalidate(dentry, NULL))
1811 static struct dentry *proc_pident_lookup(struct inode *dir,
1812 struct dentry *dentry,
1813 const struct pid_entry *ents,
1816 struct inode *inode;
1817 struct dentry *error;
1818 struct task_struct *task = get_proc_task(dir);
1819 const struct pid_entry *p, *last;
1821 error = ERR_PTR(-ENOENT);
1828 * Yes, it does not scale. And it should not. Don't add
1829 * new entries into /proc/<tgid>/ without very good reasons.
1831 last = &ents[nents - 1];
1832 for (p = ents; p <= last; p++) {
1833 if (p->len != dentry->d_name.len)
1835 if (!memcmp(dentry->d_name.name, p->name, p->len))
1841 error = proc_pident_instantiate(dir, dentry, task, p);
1843 put_task_struct(task);
1848 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1849 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1851 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1852 proc_pident_instantiate, task, p);
1855 static int proc_pident_readdir(struct file *filp,
1856 void *dirent, filldir_t filldir,
1857 const struct pid_entry *ents, unsigned int nents)
1860 struct dentry *dentry = filp->f_path.dentry;
1861 struct inode *inode = dentry->d_inode;
1862 struct task_struct *task = get_proc_task(inode);
1863 const struct pid_entry *p, *last;
1876 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1882 ino = parent_ino(dentry);
1883 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1895 last = &ents[nents - 1];
1897 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1906 put_task_struct(task);
1911 #ifdef CONFIG_SECURITY
1912 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1913 size_t count, loff_t *ppos)
1915 struct inode * inode = file->f_path.dentry->d_inode;
1918 struct task_struct *task = get_proc_task(inode);
1923 length = security_getprocattr(task,
1924 (char*)file->f_path.dentry->d_name.name,
1926 put_task_struct(task);
1928 length = simple_read_from_buffer(buf, count, ppos, p, length);
1933 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1934 size_t count, loff_t *ppos)
1936 struct inode * inode = file->f_path.dentry->d_inode;
1939 struct task_struct *task = get_proc_task(inode);
1944 if (count > PAGE_SIZE)
1947 /* No partial writes. */
1953 page = (char*)__get_free_page(GFP_TEMPORARY);
1958 if (copy_from_user(page, buf, count))
1961 length = security_setprocattr(task,
1962 (char*)file->f_path.dentry->d_name.name,
1963 (void*)page, count);
1965 free_page((unsigned long) page);
1967 put_task_struct(task);
1972 static const struct file_operations proc_pid_attr_operations = {
1973 .read = proc_pid_attr_read,
1974 .write = proc_pid_attr_write,
1977 static const struct pid_entry attr_dir_stuff[] = {
1978 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1979 REG("prev", S_IRUGO, pid_attr),
1980 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1981 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1982 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1983 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1986 static int proc_attr_dir_readdir(struct file * filp,
1987 void * dirent, filldir_t filldir)
1989 return proc_pident_readdir(filp,dirent,filldir,
1990 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1993 static const struct file_operations proc_attr_dir_operations = {
1994 .read = generic_read_dir,
1995 .readdir = proc_attr_dir_readdir,
1998 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1999 struct dentry *dentry, struct nameidata *nd)
2001 return proc_pident_lookup(dir, dentry,
2002 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2005 static const struct inode_operations proc_attr_dir_inode_operations = {
2006 .lookup = proc_attr_dir_lookup,
2007 .getattr = pid_getattr,
2008 .setattr = proc_setattr,
2013 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2014 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2015 size_t count, loff_t *ppos)
2017 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2018 struct mm_struct *mm;
2019 char buffer[PROC_NUMBUF];
2027 mm = get_task_mm(task);
2029 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2030 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2031 MMF_DUMP_FILTER_SHIFT));
2033 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2036 put_task_struct(task);
2041 static ssize_t proc_coredump_filter_write(struct file *file,
2042 const char __user *buf,
2046 struct task_struct *task;
2047 struct mm_struct *mm;
2048 char buffer[PROC_NUMBUF], *end;
2055 memset(buffer, 0, sizeof(buffer));
2056 if (count > sizeof(buffer) - 1)
2057 count = sizeof(buffer) - 1;
2058 if (copy_from_user(buffer, buf, count))
2062 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2065 if (end - buffer == 0)
2069 task = get_proc_task(file->f_dentry->d_inode);
2074 mm = get_task_mm(task);
2078 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2080 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2082 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2087 put_task_struct(task);
2092 static const struct file_operations proc_coredump_filter_operations = {
2093 .read = proc_coredump_filter_read,
2094 .write = proc_coredump_filter_write,
2101 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2104 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2105 pid_t tgid = task_tgid_nr_ns(current, ns);
2106 char tmp[PROC_NUMBUF];
2109 sprintf(tmp, "%d", tgid);
2110 return vfs_readlink(dentry,buffer,buflen,tmp);
2113 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2115 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2116 pid_t tgid = task_tgid_nr_ns(current, ns);
2117 char tmp[PROC_NUMBUF];
2119 return ERR_PTR(-ENOENT);
2120 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2121 return ERR_PTR(vfs_follow_link(nd,tmp));
2124 static const struct inode_operations proc_self_inode_operations = {
2125 .readlink = proc_self_readlink,
2126 .follow_link = proc_self_follow_link,
2132 * These are the directory entries in the root directory of /proc
2133 * that properly belong to the /proc filesystem, as they describe
2134 * describe something that is process related.
2136 static const struct pid_entry proc_base_stuff[] = {
2137 NOD("self", S_IFLNK|S_IRWXUGO,
2138 &proc_self_inode_operations, NULL, {}),
2142 * Exceptional case: normally we are not allowed to unhash a busy
2143 * directory. In this case, however, we can do it - no aliasing problems
2144 * due to the way we treat inodes.
2146 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2148 struct inode *inode = dentry->d_inode;
2149 struct task_struct *task = get_proc_task(inode);
2151 put_task_struct(task);
2158 static struct dentry_operations proc_base_dentry_operations =
2160 .d_revalidate = proc_base_revalidate,
2161 .d_delete = pid_delete_dentry,
2164 static struct dentry *proc_base_instantiate(struct inode *dir,
2165 struct dentry *dentry, struct task_struct *task, const void *ptr)
2167 const struct pid_entry *p = ptr;
2168 struct inode *inode;
2169 struct proc_inode *ei;
2170 struct dentry *error = ERR_PTR(-EINVAL);
2172 /* Allocate the inode */
2173 error = ERR_PTR(-ENOMEM);
2174 inode = new_inode(dir->i_sb);
2178 /* Initialize the inode */
2180 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2183 * grab the reference to the task.
2185 ei->pid = get_task_pid(task, PIDTYPE_PID);
2191 inode->i_mode = p->mode;
2192 if (S_ISDIR(inode->i_mode))
2194 if (S_ISLNK(inode->i_mode))
2197 inode->i_op = p->iop;
2199 inode->i_fop = p->fop;
2201 dentry->d_op = &proc_base_dentry_operations;
2202 d_add(dentry, inode);
2211 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2213 struct dentry *error;
2214 struct task_struct *task = get_proc_task(dir);
2215 const struct pid_entry *p, *last;
2217 error = ERR_PTR(-ENOENT);
2222 /* Lookup the directory entry */
2223 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2224 for (p = proc_base_stuff; p <= last; p++) {
2225 if (p->len != dentry->d_name.len)
2227 if (!memcmp(dentry->d_name.name, p->name, p->len))
2233 error = proc_base_instantiate(dir, dentry, task, p);
2236 put_task_struct(task);
2241 static int proc_base_fill_cache(struct file *filp, void *dirent,
2242 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2244 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2245 proc_base_instantiate, task, p);
2248 #ifdef CONFIG_TASK_IO_ACCOUNTING
2249 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2251 return sprintf(buffer,
2252 #ifdef CONFIG_TASK_XACCT
2258 "read_bytes: %llu\n"
2259 "write_bytes: %llu\n"
2260 "cancelled_write_bytes: %llu\n",
2261 #ifdef CONFIG_TASK_XACCT
2262 (unsigned long long)task->rchar,
2263 (unsigned long long)task->wchar,
2264 (unsigned long long)task->syscr,
2265 (unsigned long long)task->syscw,
2267 (unsigned long long)task->ioac.read_bytes,
2268 (unsigned long long)task->ioac.write_bytes,
2269 (unsigned long long)task->ioac.cancelled_write_bytes);
2276 static const struct file_operations proc_task_operations;
2277 static const struct inode_operations proc_task_inode_operations;
2279 static const struct pid_entry tgid_base_stuff[] = {
2280 DIR("task", S_IRUGO|S_IXUGO, task),
2281 DIR("fd", S_IRUSR|S_IXUSR, fd),
2282 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2283 REG("environ", S_IRUSR, environ),
2284 INF("auxv", S_IRUSR, pid_auxv),
2285 ONE("status", S_IRUGO, pid_status),
2286 INF("limits", S_IRUSR, pid_limits),
2287 #ifdef CONFIG_SCHED_DEBUG
2288 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2290 INF("cmdline", S_IRUGO, pid_cmdline),
2291 ONE("stat", S_IRUGO, tgid_stat),
2292 ONE("statm", S_IRUGO, pid_statm),
2293 REG("maps", S_IRUGO, maps),
2295 REG("numa_maps", S_IRUGO, numa_maps),
2297 REG("mem", S_IRUSR|S_IWUSR, mem),
2301 REG("mounts", S_IRUGO, mounts),
2302 REG("mountstats", S_IRUSR, mountstats),
2303 #ifdef CONFIG_PROC_PAGE_MONITOR
2304 REG("clear_refs", S_IWUSR, clear_refs),
2305 REG("smaps", S_IRUGO, smaps),
2306 REG("pagemap", S_IRUSR, pagemap),
2308 #ifdef CONFIG_SECURITY
2309 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2311 #ifdef CONFIG_KALLSYMS
2312 INF("wchan", S_IRUGO, pid_wchan),
2314 #ifdef CONFIG_SCHEDSTATS
2315 INF("schedstat", S_IRUGO, pid_schedstat),
2317 #ifdef CONFIG_LATENCYTOP
2318 REG("latency", S_IRUGO, lstats),
2320 #ifdef CONFIG_PROC_PID_CPUSET
2321 REG("cpuset", S_IRUGO, cpuset),
2323 #ifdef CONFIG_CGROUPS
2324 REG("cgroup", S_IRUGO, cgroup),
2326 INF("oom_score", S_IRUGO, oom_score),
2327 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2328 #ifdef CONFIG_AUDITSYSCALL
2329 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2331 #ifdef CONFIG_FAULT_INJECTION
2332 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2334 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2335 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2337 #ifdef CONFIG_TASK_IO_ACCOUNTING
2338 INF("io", S_IRUGO, pid_io_accounting),
2342 static int proc_tgid_base_readdir(struct file * filp,
2343 void * dirent, filldir_t filldir)
2345 return proc_pident_readdir(filp,dirent,filldir,
2346 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2349 static const struct file_operations proc_tgid_base_operations = {
2350 .read = generic_read_dir,
2351 .readdir = proc_tgid_base_readdir,
2354 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2355 return proc_pident_lookup(dir, dentry,
2356 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2359 static const struct inode_operations proc_tgid_base_inode_operations = {
2360 .lookup = proc_tgid_base_lookup,
2361 .getattr = pid_getattr,
2362 .setattr = proc_setattr,
2365 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2367 struct dentry *dentry, *leader, *dir;
2368 char buf[PROC_NUMBUF];
2372 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2373 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2375 if (!(current->flags & PF_EXITING))
2376 shrink_dcache_parent(dentry);
2385 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2386 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2391 name.len = strlen(name.name);
2392 dir = d_hash_and_lookup(leader, &name);
2394 goto out_put_leader;
2397 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2398 dentry = d_hash_and_lookup(dir, &name);
2400 shrink_dcache_parent(dentry);
2413 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2414 * @task: task that should be flushed.
2416 * When flushing dentries from proc, one needs to flush them from global
2417 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2418 * in. This call is supposed to do all of this job.
2420 * Looks in the dcache for
2422 * /proc/@tgid/task/@pid
2423 * if either directory is present flushes it and all of it'ts children
2426 * It is safe and reasonable to cache /proc entries for a task until
2427 * that task exits. After that they just clog up the dcache with
2428 * useless entries, possibly causing useful dcache entries to be
2429 * flushed instead. This routine is proved to flush those useless
2430 * dcache entries at process exit time.
2432 * NOTE: This routine is just an optimization so it does not guarantee
2433 * that no dcache entries will exist at process exit time it
2434 * just makes it very unlikely that any will persist.
2437 void proc_flush_task(struct task_struct *task)
2440 struct pid *pid, *tgid = NULL;
2443 pid = task_pid(task);
2444 if (thread_group_leader(task))
2445 tgid = task_tgid(task);
2447 for (i = 0; i <= pid->level; i++) {
2448 upid = &pid->numbers[i];
2449 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2450 tgid ? tgid->numbers[i].nr : 0);
2453 upid = &pid->numbers[pid->level];
2455 pid_ns_release_proc(upid->ns);
2458 static struct dentry *proc_pid_instantiate(struct inode *dir,
2459 struct dentry * dentry,
2460 struct task_struct *task, const void *ptr)
2462 struct dentry *error = ERR_PTR(-ENOENT);
2463 struct inode *inode;
2465 inode = proc_pid_make_inode(dir->i_sb, task);
2469 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2470 inode->i_op = &proc_tgid_base_inode_operations;
2471 inode->i_fop = &proc_tgid_base_operations;
2472 inode->i_flags|=S_IMMUTABLE;
2474 #ifdef CONFIG_SECURITY
2475 inode->i_nlink += 1;
2478 dentry->d_op = &pid_dentry_operations;
2480 d_add(dentry, inode);
2481 /* Close the race of the process dying before we return the dentry */
2482 if (pid_revalidate(dentry, NULL))
2488 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2490 struct dentry *result = ERR_PTR(-ENOENT);
2491 struct task_struct *task;
2493 struct pid_namespace *ns;
2495 result = proc_base_lookup(dir, dentry);
2496 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2499 tgid = name_to_int(dentry);
2503 ns = dentry->d_sb->s_fs_info;
2505 task = find_task_by_pid_ns(tgid, ns);
2507 get_task_struct(task);
2512 result = proc_pid_instantiate(dir, dentry, task, NULL);
2513 put_task_struct(task);
2519 * Find the first task with tgid >= tgid
2524 struct task_struct *task;
2526 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2531 put_task_struct(iter.task);
2535 pid = find_ge_pid(iter.tgid, ns);
2537 iter.tgid = pid_nr_ns(pid, ns);
2538 iter.task = pid_task(pid, PIDTYPE_PID);
2539 /* What we to know is if the pid we have find is the
2540 * pid of a thread_group_leader. Testing for task
2541 * being a thread_group_leader is the obvious thing
2542 * todo but there is a window when it fails, due to
2543 * the pid transfer logic in de_thread.
2545 * So we perform the straight forward test of seeing
2546 * if the pid we have found is the pid of a thread
2547 * group leader, and don't worry if the task we have
2548 * found doesn't happen to be a thread group leader.
2549 * As we don't care in the case of readdir.
2551 if (!iter.task || !has_group_leader_pid(iter.task)) {
2555 get_task_struct(iter.task);
2561 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2563 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2564 struct tgid_iter iter)
2566 char name[PROC_NUMBUF];
2567 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2568 return proc_fill_cache(filp, dirent, filldir, name, len,
2569 proc_pid_instantiate, iter.task, NULL);
2572 /* for the /proc/ directory itself, after non-process stuff has been done */
2573 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2575 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2576 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2577 struct tgid_iter iter;
2578 struct pid_namespace *ns;
2583 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2584 const struct pid_entry *p = &proc_base_stuff[nr];
2585 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2589 ns = filp->f_dentry->d_sb->s_fs_info;
2591 iter.tgid = filp->f_pos - TGID_OFFSET;
2592 for (iter = next_tgid(ns, iter);
2594 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2595 filp->f_pos = iter.tgid + TGID_OFFSET;
2596 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2597 put_task_struct(iter.task);
2601 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2603 put_task_struct(reaper);
2611 static const struct pid_entry tid_base_stuff[] = {
2612 DIR("fd", S_IRUSR|S_IXUSR, fd),
2613 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2614 REG("environ", S_IRUSR, environ),
2615 INF("auxv", S_IRUSR, pid_auxv),
2616 ONE("status", S_IRUGO, pid_status),
2617 INF("limits", S_IRUSR, pid_limits),
2618 #ifdef CONFIG_SCHED_DEBUG
2619 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2621 INF("cmdline", S_IRUGO, pid_cmdline),
2622 ONE("stat", S_IRUGO, tid_stat),
2623 ONE("statm", S_IRUGO, pid_statm),
2624 REG("maps", S_IRUGO, maps),
2626 REG("numa_maps", S_IRUGO, numa_maps),
2628 REG("mem", S_IRUSR|S_IWUSR, mem),
2632 REG("mounts", S_IRUGO, mounts),
2633 #ifdef CONFIG_PROC_PAGE_MONITOR
2634 REG("clear_refs", S_IWUSR, clear_refs),
2635 REG("smaps", S_IRUGO, smaps),
2636 REG("pagemap", S_IRUSR, pagemap),
2638 #ifdef CONFIG_SECURITY
2639 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2641 #ifdef CONFIG_KALLSYMS
2642 INF("wchan", S_IRUGO, pid_wchan),
2644 #ifdef CONFIG_SCHEDSTATS
2645 INF("schedstat", S_IRUGO, pid_schedstat),
2647 #ifdef CONFIG_LATENCYTOP
2648 REG("latency", S_IRUGO, lstats),
2650 #ifdef CONFIG_PROC_PID_CPUSET
2651 REG("cpuset", S_IRUGO, cpuset),
2653 #ifdef CONFIG_CGROUPS
2654 REG("cgroup", S_IRUGO, cgroup),
2656 INF("oom_score", S_IRUGO, oom_score),
2657 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2658 #ifdef CONFIG_AUDITSYSCALL
2659 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2661 #ifdef CONFIG_FAULT_INJECTION
2662 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2666 static int proc_tid_base_readdir(struct file * filp,
2667 void * dirent, filldir_t filldir)
2669 return proc_pident_readdir(filp,dirent,filldir,
2670 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2673 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2674 return proc_pident_lookup(dir, dentry,
2675 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2678 static const struct file_operations proc_tid_base_operations = {
2679 .read = generic_read_dir,
2680 .readdir = proc_tid_base_readdir,
2683 static const struct inode_operations proc_tid_base_inode_operations = {
2684 .lookup = proc_tid_base_lookup,
2685 .getattr = pid_getattr,
2686 .setattr = proc_setattr,
2689 static struct dentry *proc_task_instantiate(struct inode *dir,
2690 struct dentry *dentry, struct task_struct *task, const void *ptr)
2692 struct dentry *error = ERR_PTR(-ENOENT);
2693 struct inode *inode;
2694 inode = proc_pid_make_inode(dir->i_sb, task);
2698 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2699 inode->i_op = &proc_tid_base_inode_operations;
2700 inode->i_fop = &proc_tid_base_operations;
2701 inode->i_flags|=S_IMMUTABLE;
2703 #ifdef CONFIG_SECURITY
2704 inode->i_nlink += 1;
2707 dentry->d_op = &pid_dentry_operations;
2709 d_add(dentry, inode);
2710 /* Close the race of the process dying before we return the dentry */
2711 if (pid_revalidate(dentry, NULL))
2717 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2719 struct dentry *result = ERR_PTR(-ENOENT);
2720 struct task_struct *task;
2721 struct task_struct *leader = get_proc_task(dir);
2723 struct pid_namespace *ns;
2728 tid = name_to_int(dentry);
2732 ns = dentry->d_sb->s_fs_info;
2734 task = find_task_by_pid_ns(tid, ns);
2736 get_task_struct(task);
2740 if (!same_thread_group(leader, task))
2743 result = proc_task_instantiate(dir, dentry, task, NULL);
2745 put_task_struct(task);
2747 put_task_struct(leader);
2753 * Find the first tid of a thread group to return to user space.
2755 * Usually this is just the thread group leader, but if the users
2756 * buffer was too small or there was a seek into the middle of the
2757 * directory we have more work todo.
2759 * In the case of a short read we start with find_task_by_pid.
2761 * In the case of a seek we start with the leader and walk nr
2764 static struct task_struct *first_tid(struct task_struct *leader,
2765 int tid, int nr, struct pid_namespace *ns)
2767 struct task_struct *pos;
2770 /* Attempt to start with the pid of a thread */
2771 if (tid && (nr > 0)) {
2772 pos = find_task_by_pid_ns(tid, ns);
2773 if (pos && (pos->group_leader == leader))
2777 /* If nr exceeds the number of threads there is nothing todo */
2779 if (nr && nr >= get_nr_threads(leader))
2782 /* If we haven't found our starting place yet start
2783 * with the leader and walk nr threads forward.
2785 for (pos = leader; nr > 0; --nr) {
2786 pos = next_thread(pos);
2787 if (pos == leader) {
2793 get_task_struct(pos);
2800 * Find the next thread in the thread list.
2801 * Return NULL if there is an error or no next thread.
2803 * The reference to the input task_struct is released.
2805 static struct task_struct *next_tid(struct task_struct *start)
2807 struct task_struct *pos = NULL;
2809 if (pid_alive(start)) {
2810 pos = next_thread(start);
2811 if (thread_group_leader(pos))
2814 get_task_struct(pos);
2817 put_task_struct(start);
2821 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2822 struct task_struct *task, int tid)
2824 char name[PROC_NUMBUF];
2825 int len = snprintf(name, sizeof(name), "%d", tid);
2826 return proc_fill_cache(filp, dirent, filldir, name, len,
2827 proc_task_instantiate, task, NULL);
2830 /* for the /proc/TGID/task/ directories */
2831 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2833 struct dentry *dentry = filp->f_path.dentry;
2834 struct inode *inode = dentry->d_inode;
2835 struct task_struct *leader = NULL;
2836 struct task_struct *task;
2837 int retval = -ENOENT;
2840 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2841 struct pid_namespace *ns;
2843 task = get_proc_task(inode);
2847 if (pid_alive(task)) {
2848 leader = task->group_leader;
2849 get_task_struct(leader);
2852 put_task_struct(task);
2860 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2865 ino = parent_ino(dentry);
2866 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2872 /* f_version caches the tgid value that the last readdir call couldn't
2873 * return. lseek aka telldir automagically resets f_version to 0.
2875 ns = filp->f_dentry->d_sb->s_fs_info;
2876 tid = (int)filp->f_version;
2877 filp->f_version = 0;
2878 for (task = first_tid(leader, tid, pos - 2, ns);
2880 task = next_tid(task), pos++) {
2881 tid = task_pid_nr_ns(task, ns);
2882 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2883 /* returning this tgid failed, save it as the first
2884 * pid for the next readir call */
2885 filp->f_version = (u64)tid;
2886 put_task_struct(task);
2892 put_task_struct(leader);
2897 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2899 struct inode *inode = dentry->d_inode;
2900 struct task_struct *p = get_proc_task(inode);
2901 generic_fillattr(inode, stat);
2905 stat->nlink += get_nr_threads(p);
2913 static const struct inode_operations proc_task_inode_operations = {
2914 .lookup = proc_task_lookup,
2915 .getattr = proc_task_getattr,
2916 .setattr = proc_setattr,
2919 static const struct file_operations proc_task_operations = {
2920 .read = generic_read_dir,
2921 .readdir = proc_task_readdir,