4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
64 #include <linux/rcupdate.h>
65 #include <linux/kallsyms.h>
66 #include <linux/resource.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/cgroup.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include <linux/elf.h>
78 #include <linux/pid_namespace.h>
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
95 const struct inode_operations *iop;
96 const struct file_operations *fop;
100 #define NOD(NAME, MODE, IOP, FOP, OP) { \
102 .len = sizeof(NAME) - 1, \
109 #define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 #define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
117 #define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
119 &proc_##OTYPE##_operations, {})
120 #define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
124 #define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &proc_##OTYPE } )
130 EXPORT_SYMBOL(maps_protect);
132 static struct fs_struct *get_fs_struct(struct task_struct *task)
134 struct fs_struct *fs;
138 atomic_inc(&fs->count);
143 static int get_nr_threads(struct task_struct *tsk)
145 /* Must be called with the rcu_read_lock held */
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
156 static int proc_cwd_link(struct inode *inode, struct path *path)
158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
160 int result = -ENOENT;
163 fs = get_fs_struct(task);
164 put_task_struct(task);
167 read_lock(&fs->lock);
170 read_unlock(&fs->lock);
177 static int proc_root_link(struct inode *inode, struct path *path)
179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
181 int result = -ENOENT;
184 fs = get_fs_struct(task);
185 put_task_struct(task);
188 read_lock(&fs->lock);
191 read_unlock(&fs->lock);
198 #define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
202 (task_is_stopped_or_traced(task)) && \
203 security_ptrace(current,task) == 0))
205 struct mm_struct *mm_for_maps(struct task_struct *task)
207 struct mm_struct *mm = get_task_mm(task);
210 down_read(&mm->mmap_sem);
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
220 up_read(&mm->mmap_sem);
225 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
229 struct mm_struct *mm = get_task_mm(task);
233 goto out_mm; /* Shh! No looking before we're done */
235 len = mm->arg_end - mm->arg_start;
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
262 static int proc_pid_auxv(struct task_struct *task, char *buffer)
265 struct mm_struct *mm = get_task_mm(task);
267 unsigned int nwords = 0;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
274 memcpy(buffer, mm->saved_auxv, res);
281 #ifdef CONFIG_KALLSYMS
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
286 static int proc_pid_wchan(struct task_struct *task, char *buffer)
289 char symname[KSYM_NAME_LEN];
291 wchan = get_wchan(task);
293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
296 return sprintf(buffer, "%s", symname);
298 #endif /* CONFIG_KALLSYMS */
300 #ifdef CONFIG_SCHEDSTATS
302 * Provides /proc/PID/schedstat
304 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
306 return sprintf(buffer, "%llu %llu %lu\n",
307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
309 task->sched_info.pcount);
313 #ifdef CONFIG_LATENCYTOP
314 static int lstats_show_proc(struct seq_file *m, void *v)
317 struct inode *inode = m->private;
318 struct task_struct *task = get_proc_task(inode);
322 seq_puts(m, "Latency Top version : v0.1\n");
323 for (i = 0; i < 32; i++) {
324 if (task->latency_record[i].backtrace[0]) {
326 seq_printf(m, "%i %li %li ",
327 task->latency_record[i].count,
328 task->latency_record[i].time,
329 task->latency_record[i].max);
330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
331 char sym[KSYM_NAME_LEN];
333 if (!task->latency_record[i].backtrace[q])
335 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
337 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
338 c = strchr(sym, '+');
341 seq_printf(m, "%s ", sym);
347 put_task_struct(task);
351 static int lstats_open(struct inode *inode, struct file *file)
353 return single_open(file, lstats_show_proc, inode);
356 static ssize_t lstats_write(struct file *file, const char __user *buf,
357 size_t count, loff_t *offs)
359 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
363 clear_all_latency_tracing(task);
364 put_task_struct(task);
369 static const struct file_operations proc_lstats_operations = {
372 .write = lstats_write,
374 .release = single_release,
379 /* The badness from the OOM killer */
380 unsigned long badness(struct task_struct *p, unsigned long uptime);
381 static int proc_oom_score(struct task_struct *task, char *buffer)
383 unsigned long points;
384 struct timespec uptime;
386 do_posix_clock_monotonic_gettime(&uptime);
387 read_lock(&tasklist_lock);
388 points = badness(task, uptime.tv_sec);
389 read_unlock(&tasklist_lock);
390 return sprintf(buffer, "%lu\n", points);
398 static const struct limit_names lnames[RLIM_NLIMITS] = {
399 [RLIMIT_CPU] = {"Max cpu time", "ms"},
400 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
401 [RLIMIT_DATA] = {"Max data size", "bytes"},
402 [RLIMIT_STACK] = {"Max stack size", "bytes"},
403 [RLIMIT_CORE] = {"Max core file size", "bytes"},
404 [RLIMIT_RSS] = {"Max resident set", "bytes"},
405 [RLIMIT_NPROC] = {"Max processes", "processes"},
406 [RLIMIT_NOFILE] = {"Max open files", "files"},
407 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
408 [RLIMIT_AS] = {"Max address space", "bytes"},
409 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
410 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
411 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
412 [RLIMIT_NICE] = {"Max nice priority", NULL},
413 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
414 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
417 /* Display limits for a process */
418 static int proc_pid_limits(struct task_struct *task, char *buffer)
423 char *bufptr = buffer;
425 struct rlimit rlim[RLIM_NLIMITS];
428 if (!lock_task_sighand(task,&flags)) {
432 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
433 unlock_task_sighand(task, &flags);
437 * print the file header
439 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
440 "Limit", "Soft Limit", "Hard Limit", "Units");
442 for (i = 0; i < RLIM_NLIMITS; i++) {
443 if (rlim[i].rlim_cur == RLIM_INFINITY)
444 count += sprintf(&bufptr[count], "%-25s %-20s ",
445 lnames[i].name, "unlimited");
447 count += sprintf(&bufptr[count], "%-25s %-20lu ",
448 lnames[i].name, rlim[i].rlim_cur);
450 if (rlim[i].rlim_max == RLIM_INFINITY)
451 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
453 count += sprintf(&bufptr[count], "%-20lu ",
457 count += sprintf(&bufptr[count], "%-10s\n",
460 count += sprintf(&bufptr[count], "\n");
466 /************************************************************************/
467 /* Here the fs part begins */
468 /************************************************************************/
470 /* permission checks */
471 static int proc_fd_access_allowed(struct inode *inode)
473 struct task_struct *task;
475 /* Allow access to a task's file descriptors if it is us or we
476 * may use ptrace attach to the process and find out that
479 task = get_proc_task(inode);
481 allowed = ptrace_may_attach(task);
482 put_task_struct(task);
487 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
490 struct inode *inode = dentry->d_inode;
492 if (attr->ia_valid & ATTR_MODE)
495 error = inode_change_ok(inode, attr);
497 error = inode_setattr(inode, attr);
501 static const struct inode_operations proc_def_inode_operations = {
502 .setattr = proc_setattr,
505 static int mounts_open_common(struct inode *inode, struct file *file,
506 const struct seq_operations *op)
508 struct task_struct *task = get_proc_task(inode);
510 struct mnt_namespace *ns = NULL;
511 struct fs_struct *fs = NULL;
513 struct proc_mounts *p;
518 nsp = task_nsproxy(task);
526 fs = get_fs_struct(task);
527 put_task_struct(task);
535 read_lock(&fs->lock);
538 read_unlock(&fs->lock);
542 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
546 file->private_data = &p->m;
547 ret = seq_open(file, op);
554 p->event = ns->event;
568 static int mounts_release(struct inode *inode, struct file *file)
570 struct proc_mounts *p = file->private_data;
573 return seq_release(inode, file);
576 static unsigned mounts_poll(struct file *file, poll_table *wait)
578 struct proc_mounts *p = file->private_data;
579 struct mnt_namespace *ns = p->ns;
582 poll_wait(file, &ns->poll, wait);
584 spin_lock(&vfsmount_lock);
585 if (p->event != ns->event) {
586 p->event = ns->event;
589 spin_unlock(&vfsmount_lock);
594 static int mounts_open(struct inode *inode, struct file *file)
596 return mounts_open_common(inode, file, &mounts_op);
599 static const struct file_operations proc_mounts_operations = {
603 .release = mounts_release,
607 static int mountinfo_open(struct inode *inode, struct file *file)
609 return mounts_open_common(inode, file, &mountinfo_op);
612 static const struct file_operations proc_mountinfo_operations = {
613 .open = mountinfo_open,
616 .release = mounts_release,
620 static int mountstats_open(struct inode *inode, struct file *file)
622 return mounts_open_common(inode, file, &mountstats_op);
625 static const struct file_operations proc_mountstats_operations = {
626 .open = mountstats_open,
629 .release = mounts_release,
632 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
634 static ssize_t proc_info_read(struct file * file, char __user * buf,
635 size_t count, loff_t *ppos)
637 struct inode * inode = file->f_path.dentry->d_inode;
640 struct task_struct *task = get_proc_task(inode);
646 if (count > PROC_BLOCK_SIZE)
647 count = PROC_BLOCK_SIZE;
650 if (!(page = __get_free_page(GFP_TEMPORARY)))
653 length = PROC_I(inode)->op.proc_read(task, (char*)page);
656 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
659 put_task_struct(task);
664 static const struct file_operations proc_info_file_operations = {
665 .read = proc_info_read,
668 static int proc_single_show(struct seq_file *m, void *v)
670 struct inode *inode = m->private;
671 struct pid_namespace *ns;
673 struct task_struct *task;
676 ns = inode->i_sb->s_fs_info;
677 pid = proc_pid(inode);
678 task = get_pid_task(pid, PIDTYPE_PID);
682 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
684 put_task_struct(task);
688 static int proc_single_open(struct inode *inode, struct file *filp)
691 ret = single_open(filp, proc_single_show, NULL);
693 struct seq_file *m = filp->private_data;
700 static const struct file_operations proc_single_file_operations = {
701 .open = proc_single_open,
704 .release = single_release,
707 static int mem_open(struct inode* inode, struct file* file)
709 file->private_data = (void*)((long)current->self_exec_id);
713 static ssize_t mem_read(struct file * file, char __user * buf,
714 size_t count, loff_t *ppos)
716 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
718 unsigned long src = *ppos;
720 struct mm_struct *mm;
725 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
729 page = (char *)__get_free_page(GFP_TEMPORARY);
735 mm = get_task_mm(task);
741 if (file->private_data != (void*)((long)current->self_exec_id))
747 int this_len, retval;
749 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
750 retval = access_process_vm(task, src, page, this_len, 0);
751 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
757 if (copy_to_user(buf, page, retval)) {
772 free_page((unsigned long) page);
774 put_task_struct(task);
779 #define mem_write NULL
782 /* This is a security hazard */
783 static ssize_t mem_write(struct file * file, const char __user *buf,
784 size_t count, loff_t *ppos)
788 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
789 unsigned long dst = *ppos;
795 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
799 page = (char *)__get_free_page(GFP_TEMPORARY);
805 int this_len, retval;
807 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
808 if (copy_from_user(page, buf, this_len)) {
812 retval = access_process_vm(task, dst, page, this_len, 1);
824 free_page((unsigned long) page);
826 put_task_struct(task);
832 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
836 file->f_pos = offset;
839 file->f_pos += offset;
844 force_successful_syscall_return();
848 static const struct file_operations proc_mem_operations = {
855 static ssize_t environ_read(struct file *file, char __user *buf,
856 size_t count, loff_t *ppos)
858 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
860 unsigned long src = *ppos;
862 struct mm_struct *mm;
867 if (!ptrace_may_attach(task))
871 page = (char *)__get_free_page(GFP_TEMPORARY);
877 mm = get_task_mm(task);
882 int this_len, retval, max_len;
884 this_len = mm->env_end - (mm->env_start + src);
889 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
890 this_len = (this_len > max_len) ? max_len : this_len;
892 retval = access_process_vm(task, (mm->env_start + src),
900 if (copy_to_user(buf, page, retval)) {
914 free_page((unsigned long) page);
916 put_task_struct(task);
921 static const struct file_operations proc_environ_operations = {
922 .read = environ_read,
925 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
928 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
929 char buffer[PROC_NUMBUF];
935 oom_adjust = task->oomkilladj;
936 put_task_struct(task);
938 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
940 return simple_read_from_buffer(buf, count, ppos, buffer, len);
943 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
944 size_t count, loff_t *ppos)
946 struct task_struct *task;
947 char buffer[PROC_NUMBUF], *end;
950 memset(buffer, 0, sizeof(buffer));
951 if (count > sizeof(buffer) - 1)
952 count = sizeof(buffer) - 1;
953 if (copy_from_user(buffer, buf, count))
955 oom_adjust = simple_strtol(buffer, &end, 0);
956 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
957 oom_adjust != OOM_DISABLE)
961 task = get_proc_task(file->f_path.dentry->d_inode);
964 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
965 put_task_struct(task);
968 task->oomkilladj = oom_adjust;
969 put_task_struct(task);
970 if (end - buffer == 0)
975 static const struct file_operations proc_oom_adjust_operations = {
976 .read = oom_adjust_read,
977 .write = oom_adjust_write,
980 #ifdef CONFIG_AUDITSYSCALL
982 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
983 size_t count, loff_t *ppos)
985 struct inode * inode = file->f_path.dentry->d_inode;
986 struct task_struct *task = get_proc_task(inode);
988 char tmpbuf[TMPBUFLEN];
992 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
993 audit_get_loginuid(task));
994 put_task_struct(task);
995 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
998 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
999 size_t count, loff_t *ppos)
1001 struct inode * inode = file->f_path.dentry->d_inode;
1006 if (!capable(CAP_AUDIT_CONTROL))
1009 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1012 if (count >= PAGE_SIZE)
1013 count = PAGE_SIZE - 1;
1016 /* No partial writes. */
1019 page = (char*)__get_free_page(GFP_TEMPORARY);
1023 if (copy_from_user(page, buf, count))
1027 loginuid = simple_strtoul(page, &tmp, 10);
1033 length = audit_set_loginuid(current, loginuid);
1034 if (likely(length == 0))
1038 free_page((unsigned long) page);
1042 static const struct file_operations proc_loginuid_operations = {
1043 .read = proc_loginuid_read,
1044 .write = proc_loginuid_write,
1047 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1048 size_t count, loff_t *ppos)
1050 struct inode * inode = file->f_path.dentry->d_inode;
1051 struct task_struct *task = get_proc_task(inode);
1053 char tmpbuf[TMPBUFLEN];
1057 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1058 audit_get_sessionid(task));
1059 put_task_struct(task);
1060 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1063 static const struct file_operations proc_sessionid_operations = {
1064 .read = proc_sessionid_read,
1068 #ifdef CONFIG_FAULT_INJECTION
1069 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1070 size_t count, loff_t *ppos)
1072 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1073 char buffer[PROC_NUMBUF];
1079 make_it_fail = task->make_it_fail;
1080 put_task_struct(task);
1082 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1084 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1087 static ssize_t proc_fault_inject_write(struct file * file,
1088 const char __user * buf, size_t count, loff_t *ppos)
1090 struct task_struct *task;
1091 char buffer[PROC_NUMBUF], *end;
1094 if (!capable(CAP_SYS_RESOURCE))
1096 memset(buffer, 0, sizeof(buffer));
1097 if (count > sizeof(buffer) - 1)
1098 count = sizeof(buffer) - 1;
1099 if (copy_from_user(buffer, buf, count))
1101 make_it_fail = simple_strtol(buffer, &end, 0);
1104 task = get_proc_task(file->f_dentry->d_inode);
1107 task->make_it_fail = make_it_fail;
1108 put_task_struct(task);
1109 if (end - buffer == 0)
1111 return end - buffer;
1114 static const struct file_operations proc_fault_inject_operations = {
1115 .read = proc_fault_inject_read,
1116 .write = proc_fault_inject_write,
1121 #ifdef CONFIG_SCHED_DEBUG
1123 * Print out various scheduling related per-task fields:
1125 static int sched_show(struct seq_file *m, void *v)
1127 struct inode *inode = m->private;
1128 struct task_struct *p;
1132 p = get_proc_task(inode);
1135 proc_sched_show_task(p, m);
1143 sched_write(struct file *file, const char __user *buf,
1144 size_t count, loff_t *offset)
1146 struct inode *inode = file->f_path.dentry->d_inode;
1147 struct task_struct *p;
1151 p = get_proc_task(inode);
1154 proc_sched_set_task(p);
1161 static int sched_open(struct inode *inode, struct file *filp)
1165 ret = single_open(filp, sched_show, NULL);
1167 struct seq_file *m = filp->private_data;
1174 static const struct file_operations proc_pid_sched_operations = {
1177 .write = sched_write,
1178 .llseek = seq_lseek,
1179 .release = single_release,
1185 * We added or removed a vma mapping the executable. The vmas are only mapped
1186 * during exec and are not mapped with the mmap system call.
1187 * Callers must hold down_write() on the mm's mmap_sem for these
1189 void added_exe_file_vma(struct mm_struct *mm)
1191 mm->num_exe_file_vmas++;
1194 void removed_exe_file_vma(struct mm_struct *mm)
1196 mm->num_exe_file_vmas--;
1197 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1199 mm->exe_file = NULL;
1204 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1207 get_file(new_exe_file);
1210 mm->exe_file = new_exe_file;
1211 mm->num_exe_file_vmas = 0;
1214 struct file *get_mm_exe_file(struct mm_struct *mm)
1216 struct file *exe_file;
1218 /* We need mmap_sem to protect against races with removal of
1219 * VM_EXECUTABLE vmas */
1220 down_read(&mm->mmap_sem);
1221 exe_file = mm->exe_file;
1224 up_read(&mm->mmap_sem);
1228 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1230 /* It's safe to write the exe_file pointer without exe_file_lock because
1231 * this is called during fork when the task is not yet in /proc */
1232 newmm->exe_file = get_mm_exe_file(oldmm);
1235 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1237 struct task_struct *task;
1238 struct mm_struct *mm;
1239 struct file *exe_file;
1241 task = get_proc_task(inode);
1244 mm = get_task_mm(task);
1245 put_task_struct(task);
1248 exe_file = get_mm_exe_file(mm);
1251 *exe_path = exe_file->f_path;
1252 path_get(&exe_file->f_path);
1259 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1261 struct inode *inode = dentry->d_inode;
1262 int error = -EACCES;
1264 /* We don't need a base pointer in the /proc filesystem */
1265 path_put(&nd->path);
1267 /* Are we allowed to snoop on the tasks file descriptors? */
1268 if (!proc_fd_access_allowed(inode))
1271 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1272 nd->last_type = LAST_BIND;
1274 return ERR_PTR(error);
1277 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1279 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1286 pathname = d_path(path, tmp, PAGE_SIZE);
1287 len = PTR_ERR(pathname);
1288 if (IS_ERR(pathname))
1290 len = tmp + PAGE_SIZE - 1 - pathname;
1294 if (copy_to_user(buffer, pathname, len))
1297 free_page((unsigned long)tmp);
1301 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1303 int error = -EACCES;
1304 struct inode *inode = dentry->d_inode;
1307 /* Are we allowed to snoop on the tasks file descriptors? */
1308 if (!proc_fd_access_allowed(inode))
1311 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1315 error = do_proc_readlink(&path, buffer, buflen);
1321 static const struct inode_operations proc_pid_link_inode_operations = {
1322 .readlink = proc_pid_readlink,
1323 .follow_link = proc_pid_follow_link,
1324 .setattr = proc_setattr,
1328 /* building an inode */
1330 static int task_dumpable(struct task_struct *task)
1333 struct mm_struct *mm;
1338 dumpable = get_dumpable(mm);
1346 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1348 struct inode * inode;
1349 struct proc_inode *ei;
1351 /* We need a new inode */
1353 inode = new_inode(sb);
1359 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1360 inode->i_op = &proc_def_inode_operations;
1363 * grab the reference to task.
1365 ei->pid = get_task_pid(task, PIDTYPE_PID);
1371 if (task_dumpable(task)) {
1372 inode->i_uid = task->euid;
1373 inode->i_gid = task->egid;
1375 security_task_to_inode(task, inode);
1385 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1387 struct inode *inode = dentry->d_inode;
1388 struct task_struct *task;
1389 generic_fillattr(inode, stat);
1394 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1396 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1397 task_dumpable(task)) {
1398 stat->uid = task->euid;
1399 stat->gid = task->egid;
1409 * Exceptional case: normally we are not allowed to unhash a busy
1410 * directory. In this case, however, we can do it - no aliasing problems
1411 * due to the way we treat inodes.
1413 * Rewrite the inode's ownerships here because the owning task may have
1414 * performed a setuid(), etc.
1416 * Before the /proc/pid/status file was created the only way to read
1417 * the effective uid of a /process was to stat /proc/pid. Reading
1418 * /proc/pid/status is slow enough that procps and other packages
1419 * kept stating /proc/pid. To keep the rules in /proc simple I have
1420 * made this apply to all per process world readable and executable
1423 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1425 struct inode *inode = dentry->d_inode;
1426 struct task_struct *task = get_proc_task(inode);
1428 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1429 task_dumpable(task)) {
1430 inode->i_uid = task->euid;
1431 inode->i_gid = task->egid;
1436 inode->i_mode &= ~(S_ISUID | S_ISGID);
1437 security_task_to_inode(task, inode);
1438 put_task_struct(task);
1445 static int pid_delete_dentry(struct dentry * dentry)
1447 /* Is the task we represent dead?
1448 * If so, then don't put the dentry on the lru list,
1449 * kill it immediately.
1451 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1454 static struct dentry_operations pid_dentry_operations =
1456 .d_revalidate = pid_revalidate,
1457 .d_delete = pid_delete_dentry,
1462 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1463 struct task_struct *, const void *);
1466 * Fill a directory entry.
1468 * If possible create the dcache entry and derive our inode number and
1469 * file type from dcache entry.
1471 * Since all of the proc inode numbers are dynamically generated, the inode
1472 * numbers do not exist until the inode is cache. This means creating the
1473 * the dcache entry in readdir is necessary to keep the inode numbers
1474 * reported by readdir in sync with the inode numbers reported
1477 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1478 char *name, int len,
1479 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1481 struct dentry *child, *dir = filp->f_path.dentry;
1482 struct inode *inode;
1485 unsigned type = DT_UNKNOWN;
1489 qname.hash = full_name_hash(name, len);
1491 child = d_lookup(dir, &qname);
1494 new = d_alloc(dir, &qname);
1496 child = instantiate(dir->d_inode, new, task, ptr);
1503 if (!child || IS_ERR(child) || !child->d_inode)
1504 goto end_instantiate;
1505 inode = child->d_inode;
1508 type = inode->i_mode >> 12;
1513 ino = find_inode_number(dir, &qname);
1516 return filldir(dirent, name, len, filp->f_pos, ino, type);
1519 static unsigned name_to_int(struct dentry *dentry)
1521 const char *name = dentry->d_name.name;
1522 int len = dentry->d_name.len;
1525 if (len > 1 && *name == '0')
1528 unsigned c = *name++ - '0';
1531 if (n >= (~0U-9)/10)
1541 #define PROC_FDINFO_MAX 64
1543 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1545 struct task_struct *task = get_proc_task(inode);
1546 struct files_struct *files = NULL;
1548 int fd = proc_fd(inode);
1551 files = get_files_struct(task);
1552 put_task_struct(task);
1556 * We are not taking a ref to the file structure, so we must
1559 spin_lock(&files->file_lock);
1560 file = fcheck_files(files, fd);
1563 *path = file->f_path;
1564 path_get(&file->f_path);
1567 snprintf(info, PROC_FDINFO_MAX,
1570 (long long) file->f_pos,
1572 spin_unlock(&files->file_lock);
1573 put_files_struct(files);
1576 spin_unlock(&files->file_lock);
1577 put_files_struct(files);
1582 static int proc_fd_link(struct inode *inode, struct path *path)
1584 return proc_fd_info(inode, path, NULL);
1587 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1589 struct inode *inode = dentry->d_inode;
1590 struct task_struct *task = get_proc_task(inode);
1591 int fd = proc_fd(inode);
1592 struct files_struct *files;
1595 files = get_files_struct(task);
1598 if (fcheck_files(files, fd)) {
1600 put_files_struct(files);
1601 if (task_dumpable(task)) {
1602 inode->i_uid = task->euid;
1603 inode->i_gid = task->egid;
1608 inode->i_mode &= ~(S_ISUID | S_ISGID);
1609 security_task_to_inode(task, inode);
1610 put_task_struct(task);
1614 put_files_struct(files);
1616 put_task_struct(task);
1622 static struct dentry_operations tid_fd_dentry_operations =
1624 .d_revalidate = tid_fd_revalidate,
1625 .d_delete = pid_delete_dentry,
1628 static struct dentry *proc_fd_instantiate(struct inode *dir,
1629 struct dentry *dentry, struct task_struct *task, const void *ptr)
1631 unsigned fd = *(const unsigned *)ptr;
1633 struct files_struct *files;
1634 struct inode *inode;
1635 struct proc_inode *ei;
1636 struct dentry *error = ERR_PTR(-ENOENT);
1638 inode = proc_pid_make_inode(dir->i_sb, task);
1643 files = get_files_struct(task);
1646 inode->i_mode = S_IFLNK;
1649 * We are not taking a ref to the file structure, so we must
1652 spin_lock(&files->file_lock);
1653 file = fcheck_files(files, fd);
1656 if (file->f_mode & 1)
1657 inode->i_mode |= S_IRUSR | S_IXUSR;
1658 if (file->f_mode & 2)
1659 inode->i_mode |= S_IWUSR | S_IXUSR;
1660 spin_unlock(&files->file_lock);
1661 put_files_struct(files);
1663 inode->i_op = &proc_pid_link_inode_operations;
1665 ei->op.proc_get_link = proc_fd_link;
1666 dentry->d_op = &tid_fd_dentry_operations;
1667 d_add(dentry, inode);
1668 /* Close the race of the process dying before we return the dentry */
1669 if (tid_fd_revalidate(dentry, NULL))
1675 spin_unlock(&files->file_lock);
1676 put_files_struct(files);
1682 static struct dentry *proc_lookupfd_common(struct inode *dir,
1683 struct dentry *dentry,
1684 instantiate_t instantiate)
1686 struct task_struct *task = get_proc_task(dir);
1687 unsigned fd = name_to_int(dentry);
1688 struct dentry *result = ERR_PTR(-ENOENT);
1695 result = instantiate(dir, dentry, task, &fd);
1697 put_task_struct(task);
1702 static int proc_readfd_common(struct file * filp, void * dirent,
1703 filldir_t filldir, instantiate_t instantiate)
1705 struct dentry *dentry = filp->f_path.dentry;
1706 struct inode *inode = dentry->d_inode;
1707 struct task_struct *p = get_proc_task(inode);
1708 unsigned int fd, ino;
1710 struct files_struct * files;
1720 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1724 ino = parent_ino(dentry);
1725 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1729 files = get_files_struct(p);
1733 for (fd = filp->f_pos-2;
1734 fd < files_fdtable(files)->max_fds;
1735 fd++, filp->f_pos++) {
1736 char name[PROC_NUMBUF];
1739 if (!fcheck_files(files, fd))
1743 len = snprintf(name, sizeof(name), "%d", fd);
1744 if (proc_fill_cache(filp, dirent, filldir,
1745 name, len, instantiate,
1753 put_files_struct(files);
1761 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1762 struct nameidata *nd)
1764 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1767 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1769 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1772 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1773 size_t len, loff_t *ppos)
1775 char tmp[PROC_FDINFO_MAX];
1776 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1778 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1782 static const struct file_operations proc_fdinfo_file_operations = {
1783 .open = nonseekable_open,
1784 .read = proc_fdinfo_read,
1787 static const struct file_operations proc_fd_operations = {
1788 .read = generic_read_dir,
1789 .readdir = proc_readfd,
1793 * /proc/pid/fd needs a special permission handler so that a process can still
1794 * access /proc/self/fd after it has executed a setuid().
1796 static int proc_fd_permission(struct inode *inode, int mask,
1797 struct nameidata *nd)
1801 rv = generic_permission(inode, mask, NULL);
1804 if (task_pid(current) == proc_pid(inode))
1810 * proc directories can do almost nothing..
1812 static const struct inode_operations proc_fd_inode_operations = {
1813 .lookup = proc_lookupfd,
1814 .permission = proc_fd_permission,
1815 .setattr = proc_setattr,
1818 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1819 struct dentry *dentry, struct task_struct *task, const void *ptr)
1821 unsigned fd = *(unsigned *)ptr;
1822 struct inode *inode;
1823 struct proc_inode *ei;
1824 struct dentry *error = ERR_PTR(-ENOENT);
1826 inode = proc_pid_make_inode(dir->i_sb, task);
1831 inode->i_mode = S_IFREG | S_IRUSR;
1832 inode->i_fop = &proc_fdinfo_file_operations;
1833 dentry->d_op = &tid_fd_dentry_operations;
1834 d_add(dentry, inode);
1835 /* Close the race of the process dying before we return the dentry */
1836 if (tid_fd_revalidate(dentry, NULL))
1843 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1844 struct dentry *dentry,
1845 struct nameidata *nd)
1847 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1850 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1852 return proc_readfd_common(filp, dirent, filldir,
1853 proc_fdinfo_instantiate);
1856 static const struct file_operations proc_fdinfo_operations = {
1857 .read = generic_read_dir,
1858 .readdir = proc_readfdinfo,
1862 * proc directories can do almost nothing..
1864 static const struct inode_operations proc_fdinfo_inode_operations = {
1865 .lookup = proc_lookupfdinfo,
1866 .setattr = proc_setattr,
1870 static struct dentry *proc_pident_instantiate(struct inode *dir,
1871 struct dentry *dentry, struct task_struct *task, const void *ptr)
1873 const struct pid_entry *p = ptr;
1874 struct inode *inode;
1875 struct proc_inode *ei;
1876 struct dentry *error = ERR_PTR(-EINVAL);
1878 inode = proc_pid_make_inode(dir->i_sb, task);
1883 inode->i_mode = p->mode;
1884 if (S_ISDIR(inode->i_mode))
1885 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1887 inode->i_op = p->iop;
1889 inode->i_fop = p->fop;
1891 dentry->d_op = &pid_dentry_operations;
1892 d_add(dentry, inode);
1893 /* Close the race of the process dying before we return the dentry */
1894 if (pid_revalidate(dentry, NULL))
1900 static struct dentry *proc_pident_lookup(struct inode *dir,
1901 struct dentry *dentry,
1902 const struct pid_entry *ents,
1905 struct inode *inode;
1906 struct dentry *error;
1907 struct task_struct *task = get_proc_task(dir);
1908 const struct pid_entry *p, *last;
1910 error = ERR_PTR(-ENOENT);
1917 * Yes, it does not scale. And it should not. Don't add
1918 * new entries into /proc/<tgid>/ without very good reasons.
1920 last = &ents[nents - 1];
1921 for (p = ents; p <= last; p++) {
1922 if (p->len != dentry->d_name.len)
1924 if (!memcmp(dentry->d_name.name, p->name, p->len))
1930 error = proc_pident_instantiate(dir, dentry, task, p);
1932 put_task_struct(task);
1937 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1938 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1940 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1941 proc_pident_instantiate, task, p);
1944 static int proc_pident_readdir(struct file *filp,
1945 void *dirent, filldir_t filldir,
1946 const struct pid_entry *ents, unsigned int nents)
1949 struct dentry *dentry = filp->f_path.dentry;
1950 struct inode *inode = dentry->d_inode;
1951 struct task_struct *task = get_proc_task(inode);
1952 const struct pid_entry *p, *last;
1965 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1971 ino = parent_ino(dentry);
1972 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1984 last = &ents[nents - 1];
1986 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1995 put_task_struct(task);
2000 #ifdef CONFIG_SECURITY
2001 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2002 size_t count, loff_t *ppos)
2004 struct inode * inode = file->f_path.dentry->d_inode;
2007 struct task_struct *task = get_proc_task(inode);
2012 length = security_getprocattr(task,
2013 (char*)file->f_path.dentry->d_name.name,
2015 put_task_struct(task);
2017 length = simple_read_from_buffer(buf, count, ppos, p, length);
2022 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2023 size_t count, loff_t *ppos)
2025 struct inode * inode = file->f_path.dentry->d_inode;
2028 struct task_struct *task = get_proc_task(inode);
2033 if (count > PAGE_SIZE)
2036 /* No partial writes. */
2042 page = (char*)__get_free_page(GFP_TEMPORARY);
2047 if (copy_from_user(page, buf, count))
2050 length = security_setprocattr(task,
2051 (char*)file->f_path.dentry->d_name.name,
2052 (void*)page, count);
2054 free_page((unsigned long) page);
2056 put_task_struct(task);
2061 static const struct file_operations proc_pid_attr_operations = {
2062 .read = proc_pid_attr_read,
2063 .write = proc_pid_attr_write,
2066 static const struct pid_entry attr_dir_stuff[] = {
2067 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2068 REG("prev", S_IRUGO, pid_attr),
2069 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2070 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2071 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2072 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
2075 static int proc_attr_dir_readdir(struct file * filp,
2076 void * dirent, filldir_t filldir)
2078 return proc_pident_readdir(filp,dirent,filldir,
2079 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2082 static const struct file_operations proc_attr_dir_operations = {
2083 .read = generic_read_dir,
2084 .readdir = proc_attr_dir_readdir,
2087 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2088 struct dentry *dentry, struct nameidata *nd)
2090 return proc_pident_lookup(dir, dentry,
2091 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2094 static const struct inode_operations proc_attr_dir_inode_operations = {
2095 .lookup = proc_attr_dir_lookup,
2096 .getattr = pid_getattr,
2097 .setattr = proc_setattr,
2102 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2103 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2104 size_t count, loff_t *ppos)
2106 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2107 struct mm_struct *mm;
2108 char buffer[PROC_NUMBUF];
2116 mm = get_task_mm(task);
2118 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2119 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2120 MMF_DUMP_FILTER_SHIFT));
2122 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2125 put_task_struct(task);
2130 static ssize_t proc_coredump_filter_write(struct file *file,
2131 const char __user *buf,
2135 struct task_struct *task;
2136 struct mm_struct *mm;
2137 char buffer[PROC_NUMBUF], *end;
2144 memset(buffer, 0, sizeof(buffer));
2145 if (count > sizeof(buffer) - 1)
2146 count = sizeof(buffer) - 1;
2147 if (copy_from_user(buffer, buf, count))
2151 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2154 if (end - buffer == 0)
2158 task = get_proc_task(file->f_dentry->d_inode);
2163 mm = get_task_mm(task);
2167 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2169 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2171 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2176 put_task_struct(task);
2181 static const struct file_operations proc_coredump_filter_operations = {
2182 .read = proc_coredump_filter_read,
2183 .write = proc_coredump_filter_write,
2190 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2193 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2194 pid_t tgid = task_tgid_nr_ns(current, ns);
2195 char tmp[PROC_NUMBUF];
2198 sprintf(tmp, "%d", tgid);
2199 return vfs_readlink(dentry,buffer,buflen,tmp);
2202 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2204 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2205 pid_t tgid = task_tgid_nr_ns(current, ns);
2206 char tmp[PROC_NUMBUF];
2208 return ERR_PTR(-ENOENT);
2209 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2210 return ERR_PTR(vfs_follow_link(nd,tmp));
2213 static const struct inode_operations proc_self_inode_operations = {
2214 .readlink = proc_self_readlink,
2215 .follow_link = proc_self_follow_link,
2221 * These are the directory entries in the root directory of /proc
2222 * that properly belong to the /proc filesystem, as they describe
2223 * describe something that is process related.
2225 static const struct pid_entry proc_base_stuff[] = {
2226 NOD("self", S_IFLNK|S_IRWXUGO,
2227 &proc_self_inode_operations, NULL, {}),
2231 * Exceptional case: normally we are not allowed to unhash a busy
2232 * directory. In this case, however, we can do it - no aliasing problems
2233 * due to the way we treat inodes.
2235 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2237 struct inode *inode = dentry->d_inode;
2238 struct task_struct *task = get_proc_task(inode);
2240 put_task_struct(task);
2247 static struct dentry_operations proc_base_dentry_operations =
2249 .d_revalidate = proc_base_revalidate,
2250 .d_delete = pid_delete_dentry,
2253 static struct dentry *proc_base_instantiate(struct inode *dir,
2254 struct dentry *dentry, struct task_struct *task, const void *ptr)
2256 const struct pid_entry *p = ptr;
2257 struct inode *inode;
2258 struct proc_inode *ei;
2259 struct dentry *error = ERR_PTR(-EINVAL);
2261 /* Allocate the inode */
2262 error = ERR_PTR(-ENOMEM);
2263 inode = new_inode(dir->i_sb);
2267 /* Initialize the inode */
2269 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2272 * grab the reference to the task.
2274 ei->pid = get_task_pid(task, PIDTYPE_PID);
2280 inode->i_mode = p->mode;
2281 if (S_ISDIR(inode->i_mode))
2283 if (S_ISLNK(inode->i_mode))
2286 inode->i_op = p->iop;
2288 inode->i_fop = p->fop;
2290 dentry->d_op = &proc_base_dentry_operations;
2291 d_add(dentry, inode);
2300 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2302 struct dentry *error;
2303 struct task_struct *task = get_proc_task(dir);
2304 const struct pid_entry *p, *last;
2306 error = ERR_PTR(-ENOENT);
2311 /* Lookup the directory entry */
2312 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2313 for (p = proc_base_stuff; p <= last; p++) {
2314 if (p->len != dentry->d_name.len)
2316 if (!memcmp(dentry->d_name.name, p->name, p->len))
2322 error = proc_base_instantiate(dir, dentry, task, p);
2325 put_task_struct(task);
2330 static int proc_base_fill_cache(struct file *filp, void *dirent,
2331 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2333 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2334 proc_base_instantiate, task, p);
2337 #ifdef CONFIG_TASK_IO_ACCOUNTING
2338 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2340 return sprintf(buffer,
2341 #ifdef CONFIG_TASK_XACCT
2347 "read_bytes: %llu\n"
2348 "write_bytes: %llu\n"
2349 "cancelled_write_bytes: %llu\n",
2350 #ifdef CONFIG_TASK_XACCT
2351 (unsigned long long)task->rchar,
2352 (unsigned long long)task->wchar,
2353 (unsigned long long)task->syscr,
2354 (unsigned long long)task->syscw,
2356 (unsigned long long)task->ioac.read_bytes,
2357 (unsigned long long)task->ioac.write_bytes,
2358 (unsigned long long)task->ioac.cancelled_write_bytes);
2365 static const struct file_operations proc_task_operations;
2366 static const struct inode_operations proc_task_inode_operations;
2368 static const struct pid_entry tgid_base_stuff[] = {
2369 DIR("task", S_IRUGO|S_IXUGO, task),
2370 DIR("fd", S_IRUSR|S_IXUSR, fd),
2371 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2373 DIR("net", S_IRUGO|S_IXUGO, net),
2375 REG("environ", S_IRUSR, environ),
2376 INF("auxv", S_IRUSR, pid_auxv),
2377 ONE("status", S_IRUGO, pid_status),
2378 INF("limits", S_IRUSR, pid_limits),
2379 #ifdef CONFIG_SCHED_DEBUG
2380 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2382 INF("cmdline", S_IRUGO, pid_cmdline),
2383 ONE("stat", S_IRUGO, tgid_stat),
2384 ONE("statm", S_IRUGO, pid_statm),
2385 REG("maps", S_IRUGO, maps),
2387 REG("numa_maps", S_IRUGO, numa_maps),
2389 REG("mem", S_IRUSR|S_IWUSR, mem),
2393 REG("mounts", S_IRUGO, mounts),
2394 REG("mountinfo", S_IRUGO, mountinfo),
2395 REG("mountstats", S_IRUSR, mountstats),
2396 #ifdef CONFIG_PROC_PAGE_MONITOR
2397 REG("clear_refs", S_IWUSR, clear_refs),
2398 REG("smaps", S_IRUGO, smaps),
2399 REG("pagemap", S_IRUSR, pagemap),
2401 #ifdef CONFIG_SECURITY
2402 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2404 #ifdef CONFIG_KALLSYMS
2405 INF("wchan", S_IRUGO, pid_wchan),
2407 #ifdef CONFIG_SCHEDSTATS
2408 INF("schedstat", S_IRUGO, pid_schedstat),
2410 #ifdef CONFIG_LATENCYTOP
2411 REG("latency", S_IRUGO, lstats),
2413 #ifdef CONFIG_PROC_PID_CPUSET
2414 REG("cpuset", S_IRUGO, cpuset),
2416 #ifdef CONFIG_CGROUPS
2417 REG("cgroup", S_IRUGO, cgroup),
2419 INF("oom_score", S_IRUGO, oom_score),
2420 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2421 #ifdef CONFIG_AUDITSYSCALL
2422 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2423 REG("sessionid", S_IRUSR, sessionid),
2425 #ifdef CONFIG_FAULT_INJECTION
2426 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2428 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2429 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2431 #ifdef CONFIG_TASK_IO_ACCOUNTING
2432 INF("io", S_IRUGO, pid_io_accounting),
2436 static int proc_tgid_base_readdir(struct file * filp,
2437 void * dirent, filldir_t filldir)
2439 return proc_pident_readdir(filp,dirent,filldir,
2440 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2443 static const struct file_operations proc_tgid_base_operations = {
2444 .read = generic_read_dir,
2445 .readdir = proc_tgid_base_readdir,
2448 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2449 return proc_pident_lookup(dir, dentry,
2450 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2453 static const struct inode_operations proc_tgid_base_inode_operations = {
2454 .lookup = proc_tgid_base_lookup,
2455 .getattr = pid_getattr,
2456 .setattr = proc_setattr,
2459 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2461 struct dentry *dentry, *leader, *dir;
2462 char buf[PROC_NUMBUF];
2466 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2467 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2469 if (!(current->flags & PF_EXITING))
2470 shrink_dcache_parent(dentry);
2479 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2480 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2485 name.len = strlen(name.name);
2486 dir = d_hash_and_lookup(leader, &name);
2488 goto out_put_leader;
2491 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2492 dentry = d_hash_and_lookup(dir, &name);
2494 shrink_dcache_parent(dentry);
2507 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2508 * @task: task that should be flushed.
2510 * When flushing dentries from proc, one needs to flush them from global
2511 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2512 * in. This call is supposed to do all of this job.
2514 * Looks in the dcache for
2516 * /proc/@tgid/task/@pid
2517 * if either directory is present flushes it and all of it'ts children
2520 * It is safe and reasonable to cache /proc entries for a task until
2521 * that task exits. After that they just clog up the dcache with
2522 * useless entries, possibly causing useful dcache entries to be
2523 * flushed instead. This routine is proved to flush those useless
2524 * dcache entries at process exit time.
2526 * NOTE: This routine is just an optimization so it does not guarantee
2527 * that no dcache entries will exist at process exit time it
2528 * just makes it very unlikely that any will persist.
2531 void proc_flush_task(struct task_struct *task)
2534 struct pid *pid, *tgid = NULL;
2537 pid = task_pid(task);
2538 if (thread_group_leader(task))
2539 tgid = task_tgid(task);
2541 for (i = 0; i <= pid->level; i++) {
2542 upid = &pid->numbers[i];
2543 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2544 tgid ? tgid->numbers[i].nr : 0);
2547 upid = &pid->numbers[pid->level];
2549 pid_ns_release_proc(upid->ns);
2552 static struct dentry *proc_pid_instantiate(struct inode *dir,
2553 struct dentry * dentry,
2554 struct task_struct *task, const void *ptr)
2556 struct dentry *error = ERR_PTR(-ENOENT);
2557 struct inode *inode;
2559 inode = proc_pid_make_inode(dir->i_sb, task);
2563 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2564 inode->i_op = &proc_tgid_base_inode_operations;
2565 inode->i_fop = &proc_tgid_base_operations;
2566 inode->i_flags|=S_IMMUTABLE;
2568 #ifdef CONFIG_SECURITY
2569 inode->i_nlink += 1;
2572 dentry->d_op = &pid_dentry_operations;
2574 d_add(dentry, inode);
2575 /* Close the race of the process dying before we return the dentry */
2576 if (pid_revalidate(dentry, NULL))
2582 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2584 struct dentry *result = ERR_PTR(-ENOENT);
2585 struct task_struct *task;
2587 struct pid_namespace *ns;
2589 result = proc_base_lookup(dir, dentry);
2590 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2593 tgid = name_to_int(dentry);
2597 ns = dentry->d_sb->s_fs_info;
2599 task = find_task_by_pid_ns(tgid, ns);
2601 get_task_struct(task);
2606 result = proc_pid_instantiate(dir, dentry, task, NULL);
2607 put_task_struct(task);
2613 * Find the first task with tgid >= tgid
2618 struct task_struct *task;
2620 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2625 put_task_struct(iter.task);
2629 pid = find_ge_pid(iter.tgid, ns);
2631 iter.tgid = pid_nr_ns(pid, ns);
2632 iter.task = pid_task(pid, PIDTYPE_PID);
2633 /* What we to know is if the pid we have find is the
2634 * pid of a thread_group_leader. Testing for task
2635 * being a thread_group_leader is the obvious thing
2636 * todo but there is a window when it fails, due to
2637 * the pid transfer logic in de_thread.
2639 * So we perform the straight forward test of seeing
2640 * if the pid we have found is the pid of a thread
2641 * group leader, and don't worry if the task we have
2642 * found doesn't happen to be a thread group leader.
2643 * As we don't care in the case of readdir.
2645 if (!iter.task || !has_group_leader_pid(iter.task)) {
2649 get_task_struct(iter.task);
2655 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2657 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2658 struct tgid_iter iter)
2660 char name[PROC_NUMBUF];
2661 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2662 return proc_fill_cache(filp, dirent, filldir, name, len,
2663 proc_pid_instantiate, iter.task, NULL);
2666 /* for the /proc/ directory itself, after non-process stuff has been done */
2667 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2669 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2670 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2671 struct tgid_iter iter;
2672 struct pid_namespace *ns;
2677 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2678 const struct pid_entry *p = &proc_base_stuff[nr];
2679 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2683 ns = filp->f_dentry->d_sb->s_fs_info;
2685 iter.tgid = filp->f_pos - TGID_OFFSET;
2686 for (iter = next_tgid(ns, iter);
2688 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2689 filp->f_pos = iter.tgid + TGID_OFFSET;
2690 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2691 put_task_struct(iter.task);
2695 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2697 put_task_struct(reaper);
2705 static const struct pid_entry tid_base_stuff[] = {
2706 DIR("fd", S_IRUSR|S_IXUSR, fd),
2707 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2708 REG("environ", S_IRUSR, environ),
2709 INF("auxv", S_IRUSR, pid_auxv),
2710 ONE("status", S_IRUGO, pid_status),
2711 INF("limits", S_IRUSR, pid_limits),
2712 #ifdef CONFIG_SCHED_DEBUG
2713 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2715 INF("cmdline", S_IRUGO, pid_cmdline),
2716 ONE("stat", S_IRUGO, tid_stat),
2717 ONE("statm", S_IRUGO, pid_statm),
2718 REG("maps", S_IRUGO, maps),
2720 REG("numa_maps", S_IRUGO, numa_maps),
2722 REG("mem", S_IRUSR|S_IWUSR, mem),
2726 REG("mounts", S_IRUGO, mounts),
2727 REG("mountinfo", S_IRUGO, mountinfo),
2728 #ifdef CONFIG_PROC_PAGE_MONITOR
2729 REG("clear_refs", S_IWUSR, clear_refs),
2730 REG("smaps", S_IRUGO, smaps),
2731 REG("pagemap", S_IRUSR, pagemap),
2733 #ifdef CONFIG_SECURITY
2734 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2736 #ifdef CONFIG_KALLSYMS
2737 INF("wchan", S_IRUGO, pid_wchan),
2739 #ifdef CONFIG_SCHEDSTATS
2740 INF("schedstat", S_IRUGO, pid_schedstat),
2742 #ifdef CONFIG_LATENCYTOP
2743 REG("latency", S_IRUGO, lstats),
2745 #ifdef CONFIG_PROC_PID_CPUSET
2746 REG("cpuset", S_IRUGO, cpuset),
2748 #ifdef CONFIG_CGROUPS
2749 REG("cgroup", S_IRUGO, cgroup),
2751 INF("oom_score", S_IRUGO, oom_score),
2752 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2753 #ifdef CONFIG_AUDITSYSCALL
2754 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2755 REG("sessionid", S_IRUSR, sessionid),
2757 #ifdef CONFIG_FAULT_INJECTION
2758 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2762 static int proc_tid_base_readdir(struct file * filp,
2763 void * dirent, filldir_t filldir)
2765 return proc_pident_readdir(filp,dirent,filldir,
2766 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2769 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2770 return proc_pident_lookup(dir, dentry,
2771 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2774 static const struct file_operations proc_tid_base_operations = {
2775 .read = generic_read_dir,
2776 .readdir = proc_tid_base_readdir,
2779 static const struct inode_operations proc_tid_base_inode_operations = {
2780 .lookup = proc_tid_base_lookup,
2781 .getattr = pid_getattr,
2782 .setattr = proc_setattr,
2785 static struct dentry *proc_task_instantiate(struct inode *dir,
2786 struct dentry *dentry, struct task_struct *task, const void *ptr)
2788 struct dentry *error = ERR_PTR(-ENOENT);
2789 struct inode *inode;
2790 inode = proc_pid_make_inode(dir->i_sb, task);
2794 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2795 inode->i_op = &proc_tid_base_inode_operations;
2796 inode->i_fop = &proc_tid_base_operations;
2797 inode->i_flags|=S_IMMUTABLE;
2799 #ifdef CONFIG_SECURITY
2800 inode->i_nlink += 1;
2803 dentry->d_op = &pid_dentry_operations;
2805 d_add(dentry, inode);
2806 /* Close the race of the process dying before we return the dentry */
2807 if (pid_revalidate(dentry, NULL))
2813 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2815 struct dentry *result = ERR_PTR(-ENOENT);
2816 struct task_struct *task;
2817 struct task_struct *leader = get_proc_task(dir);
2819 struct pid_namespace *ns;
2824 tid = name_to_int(dentry);
2828 ns = dentry->d_sb->s_fs_info;
2830 task = find_task_by_pid_ns(tid, ns);
2832 get_task_struct(task);
2836 if (!same_thread_group(leader, task))
2839 result = proc_task_instantiate(dir, dentry, task, NULL);
2841 put_task_struct(task);
2843 put_task_struct(leader);
2849 * Find the first tid of a thread group to return to user space.
2851 * Usually this is just the thread group leader, but if the users
2852 * buffer was too small or there was a seek into the middle of the
2853 * directory we have more work todo.
2855 * In the case of a short read we start with find_task_by_pid.
2857 * In the case of a seek we start with the leader and walk nr
2860 static struct task_struct *first_tid(struct task_struct *leader,
2861 int tid, int nr, struct pid_namespace *ns)
2863 struct task_struct *pos;
2866 /* Attempt to start with the pid of a thread */
2867 if (tid && (nr > 0)) {
2868 pos = find_task_by_pid_ns(tid, ns);
2869 if (pos && (pos->group_leader == leader))
2873 /* If nr exceeds the number of threads there is nothing todo */
2875 if (nr && nr >= get_nr_threads(leader))
2878 /* If we haven't found our starting place yet start
2879 * with the leader and walk nr threads forward.
2881 for (pos = leader; nr > 0; --nr) {
2882 pos = next_thread(pos);
2883 if (pos == leader) {
2889 get_task_struct(pos);
2896 * Find the next thread in the thread list.
2897 * Return NULL if there is an error or no next thread.
2899 * The reference to the input task_struct is released.
2901 static struct task_struct *next_tid(struct task_struct *start)
2903 struct task_struct *pos = NULL;
2905 if (pid_alive(start)) {
2906 pos = next_thread(start);
2907 if (thread_group_leader(pos))
2910 get_task_struct(pos);
2913 put_task_struct(start);
2917 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2918 struct task_struct *task, int tid)
2920 char name[PROC_NUMBUF];
2921 int len = snprintf(name, sizeof(name), "%d", tid);
2922 return proc_fill_cache(filp, dirent, filldir, name, len,
2923 proc_task_instantiate, task, NULL);
2926 /* for the /proc/TGID/task/ directories */
2927 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2929 struct dentry *dentry = filp->f_path.dentry;
2930 struct inode *inode = dentry->d_inode;
2931 struct task_struct *leader = NULL;
2932 struct task_struct *task;
2933 int retval = -ENOENT;
2936 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2937 struct pid_namespace *ns;
2939 task = get_proc_task(inode);
2943 if (pid_alive(task)) {
2944 leader = task->group_leader;
2945 get_task_struct(leader);
2948 put_task_struct(task);
2956 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2961 ino = parent_ino(dentry);
2962 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2968 /* f_version caches the tgid value that the last readdir call couldn't
2969 * return. lseek aka telldir automagically resets f_version to 0.
2971 ns = filp->f_dentry->d_sb->s_fs_info;
2972 tid = (int)filp->f_version;
2973 filp->f_version = 0;
2974 for (task = first_tid(leader, tid, pos - 2, ns);
2976 task = next_tid(task), pos++) {
2977 tid = task_pid_nr_ns(task, ns);
2978 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2979 /* returning this tgid failed, save it as the first
2980 * pid for the next readir call */
2981 filp->f_version = (u64)tid;
2982 put_task_struct(task);
2988 put_task_struct(leader);
2993 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2995 struct inode *inode = dentry->d_inode;
2996 struct task_struct *p = get_proc_task(inode);
2997 generic_fillattr(inode, stat);
3001 stat->nlink += get_nr_threads(p);
3009 static const struct inode_operations proc_task_inode_operations = {
3010 .lookup = proc_task_lookup,
3011 .getattr = proc_task_getattr,
3012 .setattr = proc_setattr,
3015 static const struct file_operations proc_task_operations = {
3016 .read = generic_read_dir,
3017 .readdir = proc_task_readdir,