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);
199 * Return zero if current may access user memory in @task, -error if not.
201 static int check_mem_permission(struct task_struct *task)
204 * A task can always look at itself, in case it chooses
205 * to use system calls instead of load instructions.
211 * If current is actively ptrace'ing, and would also be
212 * permitted to freshly attach with ptrace now, permit it.
214 if (task->parent == current && (task->ptrace & PT_PTRACED) &&
215 task_is_stopped_or_traced(task) &&
216 ptrace_may_attach(task))
220 * Noone else is allowed.
225 struct mm_struct *mm_for_maps(struct task_struct *task)
227 struct mm_struct *mm = get_task_mm(task);
230 down_read(&mm->mmap_sem);
234 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
240 up_read(&mm->mmap_sem);
245 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
249 struct mm_struct *mm = get_task_mm(task);
253 goto out_mm; /* Shh! No looking before we're done */
255 len = mm->arg_end - mm->arg_start;
260 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
262 // If the nul at the end of args has been overwritten, then
263 // assume application is using setproctitle(3).
264 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
265 len = strnlen(buffer, res);
269 len = mm->env_end - mm->env_start;
270 if (len > PAGE_SIZE - res)
271 len = PAGE_SIZE - res;
272 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
273 res = strnlen(buffer, res);
282 static int proc_pid_auxv(struct task_struct *task, char *buffer)
285 struct mm_struct *mm = get_task_mm(task);
287 unsigned int nwords = 0;
290 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
291 res = nwords * sizeof(mm->saved_auxv[0]);
294 memcpy(buffer, mm->saved_auxv, res);
301 #ifdef CONFIG_KALLSYMS
303 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
304 * Returns the resolved symbol. If that fails, simply return the address.
306 static int proc_pid_wchan(struct task_struct *task, char *buffer)
309 char symname[KSYM_NAME_LEN];
311 wchan = get_wchan(task);
313 if (lookup_symbol_name(wchan, symname) < 0)
314 return sprintf(buffer, "%lu", wchan);
316 return sprintf(buffer, "%s", symname);
318 #endif /* CONFIG_KALLSYMS */
320 #ifdef CONFIG_SCHEDSTATS
322 * Provides /proc/PID/schedstat
324 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
326 return sprintf(buffer, "%llu %llu %lu\n",
327 task->sched_info.cpu_time,
328 task->sched_info.run_delay,
329 task->sched_info.pcount);
333 #ifdef CONFIG_LATENCYTOP
334 static int lstats_show_proc(struct seq_file *m, void *v)
337 struct inode *inode = m->private;
338 struct task_struct *task = get_proc_task(inode);
342 seq_puts(m, "Latency Top version : v0.1\n");
343 for (i = 0; i < 32; i++) {
344 if (task->latency_record[i].backtrace[0]) {
346 seq_printf(m, "%i %li %li ",
347 task->latency_record[i].count,
348 task->latency_record[i].time,
349 task->latency_record[i].max);
350 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
351 char sym[KSYM_NAME_LEN];
353 if (!task->latency_record[i].backtrace[q])
355 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
357 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
358 c = strchr(sym, '+');
361 seq_printf(m, "%s ", sym);
367 put_task_struct(task);
371 static int lstats_open(struct inode *inode, struct file *file)
373 return single_open(file, lstats_show_proc, inode);
376 static ssize_t lstats_write(struct file *file, const char __user *buf,
377 size_t count, loff_t *offs)
379 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
383 clear_all_latency_tracing(task);
384 put_task_struct(task);
389 static const struct file_operations proc_lstats_operations = {
392 .write = lstats_write,
394 .release = single_release,
399 /* The badness from the OOM killer */
400 unsigned long badness(struct task_struct *p, unsigned long uptime);
401 static int proc_oom_score(struct task_struct *task, char *buffer)
403 unsigned long points;
404 struct timespec uptime;
406 do_posix_clock_monotonic_gettime(&uptime);
407 read_lock(&tasklist_lock);
408 points = badness(task, uptime.tv_sec);
409 read_unlock(&tasklist_lock);
410 return sprintf(buffer, "%lu\n", points);
418 static const struct limit_names lnames[RLIM_NLIMITS] = {
419 [RLIMIT_CPU] = {"Max cpu time", "ms"},
420 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
421 [RLIMIT_DATA] = {"Max data size", "bytes"},
422 [RLIMIT_STACK] = {"Max stack size", "bytes"},
423 [RLIMIT_CORE] = {"Max core file size", "bytes"},
424 [RLIMIT_RSS] = {"Max resident set", "bytes"},
425 [RLIMIT_NPROC] = {"Max processes", "processes"},
426 [RLIMIT_NOFILE] = {"Max open files", "files"},
427 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
428 [RLIMIT_AS] = {"Max address space", "bytes"},
429 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
430 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
431 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
432 [RLIMIT_NICE] = {"Max nice priority", NULL},
433 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
434 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
437 /* Display limits for a process */
438 static int proc_pid_limits(struct task_struct *task, char *buffer)
443 char *bufptr = buffer;
445 struct rlimit rlim[RLIM_NLIMITS];
448 if (!lock_task_sighand(task,&flags)) {
452 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
453 unlock_task_sighand(task, &flags);
457 * print the file header
459 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
460 "Limit", "Soft Limit", "Hard Limit", "Units");
462 for (i = 0; i < RLIM_NLIMITS; i++) {
463 if (rlim[i].rlim_cur == RLIM_INFINITY)
464 count += sprintf(&bufptr[count], "%-25s %-20s ",
465 lnames[i].name, "unlimited");
467 count += sprintf(&bufptr[count], "%-25s %-20lu ",
468 lnames[i].name, rlim[i].rlim_cur);
470 if (rlim[i].rlim_max == RLIM_INFINITY)
471 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
473 count += sprintf(&bufptr[count], "%-20lu ",
477 count += sprintf(&bufptr[count], "%-10s\n",
480 count += sprintf(&bufptr[count], "\n");
486 /************************************************************************/
487 /* Here the fs part begins */
488 /************************************************************************/
490 /* permission checks */
491 static int proc_fd_access_allowed(struct inode *inode)
493 struct task_struct *task;
495 /* Allow access to a task's file descriptors if it is us or we
496 * may use ptrace attach to the process and find out that
499 task = get_proc_task(inode);
501 allowed = ptrace_may_attach(task);
502 put_task_struct(task);
507 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
510 struct inode *inode = dentry->d_inode;
512 if (attr->ia_valid & ATTR_MODE)
515 error = inode_change_ok(inode, attr);
517 error = inode_setattr(inode, attr);
521 static const struct inode_operations proc_def_inode_operations = {
522 .setattr = proc_setattr,
525 static int mounts_open_common(struct inode *inode, struct file *file,
526 const struct seq_operations *op)
528 struct task_struct *task = get_proc_task(inode);
530 struct mnt_namespace *ns = NULL;
531 struct fs_struct *fs = NULL;
533 struct proc_mounts *p;
538 nsp = task_nsproxy(task);
546 fs = get_fs_struct(task);
547 put_task_struct(task);
555 read_lock(&fs->lock);
558 read_unlock(&fs->lock);
562 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
566 file->private_data = &p->m;
567 ret = seq_open(file, op);
574 p->event = ns->event;
588 static int mounts_release(struct inode *inode, struct file *file)
590 struct proc_mounts *p = file->private_data;
593 return seq_release(inode, file);
596 static unsigned mounts_poll(struct file *file, poll_table *wait)
598 struct proc_mounts *p = file->private_data;
599 struct mnt_namespace *ns = p->ns;
602 poll_wait(file, &ns->poll, wait);
604 spin_lock(&vfsmount_lock);
605 if (p->event != ns->event) {
606 p->event = ns->event;
609 spin_unlock(&vfsmount_lock);
614 static int mounts_open(struct inode *inode, struct file *file)
616 return mounts_open_common(inode, file, &mounts_op);
619 static const struct file_operations proc_mounts_operations = {
623 .release = mounts_release,
627 static int mountinfo_open(struct inode *inode, struct file *file)
629 return mounts_open_common(inode, file, &mountinfo_op);
632 static const struct file_operations proc_mountinfo_operations = {
633 .open = mountinfo_open,
636 .release = mounts_release,
640 static int mountstats_open(struct inode *inode, struct file *file)
642 return mounts_open_common(inode, file, &mountstats_op);
645 static const struct file_operations proc_mountstats_operations = {
646 .open = mountstats_open,
649 .release = mounts_release,
652 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
654 static ssize_t proc_info_read(struct file * file, char __user * buf,
655 size_t count, loff_t *ppos)
657 struct inode * inode = file->f_path.dentry->d_inode;
660 struct task_struct *task = get_proc_task(inode);
666 if (count > PROC_BLOCK_SIZE)
667 count = PROC_BLOCK_SIZE;
670 if (!(page = __get_free_page(GFP_TEMPORARY)))
673 length = PROC_I(inode)->op.proc_read(task, (char*)page);
676 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
679 put_task_struct(task);
684 static const struct file_operations proc_info_file_operations = {
685 .read = proc_info_read,
688 static int proc_single_show(struct seq_file *m, void *v)
690 struct inode *inode = m->private;
691 struct pid_namespace *ns;
693 struct task_struct *task;
696 ns = inode->i_sb->s_fs_info;
697 pid = proc_pid(inode);
698 task = get_pid_task(pid, PIDTYPE_PID);
702 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
704 put_task_struct(task);
708 static int proc_single_open(struct inode *inode, struct file *filp)
711 ret = single_open(filp, proc_single_show, NULL);
713 struct seq_file *m = filp->private_data;
720 static const struct file_operations proc_single_file_operations = {
721 .open = proc_single_open,
724 .release = single_release,
727 static int mem_open(struct inode* inode, struct file* file)
729 file->private_data = (void*)((long)current->self_exec_id);
733 static ssize_t mem_read(struct file * file, char __user * buf,
734 size_t count, loff_t *ppos)
736 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
738 unsigned long src = *ppos;
740 struct mm_struct *mm;
745 if (check_mem_permission(task))
749 page = (char *)__get_free_page(GFP_TEMPORARY);
755 mm = get_task_mm(task);
761 if (file->private_data != (void*)((long)current->self_exec_id))
767 int this_len, retval;
769 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
770 retval = access_process_vm(task, src, page, this_len, 0);
771 if (!retval || check_mem_permission(task)) {
777 if (copy_to_user(buf, page, retval)) {
792 free_page((unsigned long) page);
794 put_task_struct(task);
799 #define mem_write NULL
802 /* This is a security hazard */
803 static ssize_t mem_write(struct file * file, const char __user *buf,
804 size_t count, loff_t *ppos)
808 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
809 unsigned long dst = *ppos;
815 if (check_mem_permission(task))
819 page = (char *)__get_free_page(GFP_TEMPORARY);
825 int this_len, retval;
827 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
828 if (copy_from_user(page, buf, this_len)) {
832 retval = access_process_vm(task, dst, page, this_len, 1);
844 free_page((unsigned long) page);
846 put_task_struct(task);
852 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
856 file->f_pos = offset;
859 file->f_pos += offset;
864 force_successful_syscall_return();
868 static const struct file_operations proc_mem_operations = {
875 static ssize_t environ_read(struct file *file, char __user *buf,
876 size_t count, loff_t *ppos)
878 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
880 unsigned long src = *ppos;
882 struct mm_struct *mm;
887 if (!ptrace_may_attach(task))
891 page = (char *)__get_free_page(GFP_TEMPORARY);
897 mm = get_task_mm(task);
902 int this_len, retval, max_len;
904 this_len = mm->env_end - (mm->env_start + src);
909 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
910 this_len = (this_len > max_len) ? max_len : this_len;
912 retval = access_process_vm(task, (mm->env_start + src),
920 if (copy_to_user(buf, page, retval)) {
934 free_page((unsigned long) page);
936 put_task_struct(task);
941 static const struct file_operations proc_environ_operations = {
942 .read = environ_read,
945 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
946 size_t count, loff_t *ppos)
948 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
949 char buffer[PROC_NUMBUF];
955 oom_adjust = task->oomkilladj;
956 put_task_struct(task);
958 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
960 return simple_read_from_buffer(buf, count, ppos, buffer, len);
963 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
964 size_t count, loff_t *ppos)
966 struct task_struct *task;
967 char buffer[PROC_NUMBUF], *end;
970 memset(buffer, 0, sizeof(buffer));
971 if (count > sizeof(buffer) - 1)
972 count = sizeof(buffer) - 1;
973 if (copy_from_user(buffer, buf, count))
975 oom_adjust = simple_strtol(buffer, &end, 0);
976 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
977 oom_adjust != OOM_DISABLE)
981 task = get_proc_task(file->f_path.dentry->d_inode);
984 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
985 put_task_struct(task);
988 task->oomkilladj = oom_adjust;
989 put_task_struct(task);
990 if (end - buffer == 0)
995 static const struct file_operations proc_oom_adjust_operations = {
996 .read = oom_adjust_read,
997 .write = oom_adjust_write,
1000 #ifdef CONFIG_AUDITSYSCALL
1001 #define TMPBUFLEN 21
1002 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1003 size_t count, loff_t *ppos)
1005 struct inode * inode = file->f_path.dentry->d_inode;
1006 struct task_struct *task = get_proc_task(inode);
1008 char tmpbuf[TMPBUFLEN];
1012 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1013 audit_get_loginuid(task));
1014 put_task_struct(task);
1015 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1018 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1019 size_t count, loff_t *ppos)
1021 struct inode * inode = file->f_path.dentry->d_inode;
1026 if (!capable(CAP_AUDIT_CONTROL))
1029 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1032 if (count >= PAGE_SIZE)
1033 count = PAGE_SIZE - 1;
1036 /* No partial writes. */
1039 page = (char*)__get_free_page(GFP_TEMPORARY);
1043 if (copy_from_user(page, buf, count))
1047 loginuid = simple_strtoul(page, &tmp, 10);
1053 length = audit_set_loginuid(current, loginuid);
1054 if (likely(length == 0))
1058 free_page((unsigned long) page);
1062 static const struct file_operations proc_loginuid_operations = {
1063 .read = proc_loginuid_read,
1064 .write = proc_loginuid_write,
1067 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1068 size_t count, loff_t *ppos)
1070 struct inode * inode = file->f_path.dentry->d_inode;
1071 struct task_struct *task = get_proc_task(inode);
1073 char tmpbuf[TMPBUFLEN];
1077 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1078 audit_get_sessionid(task));
1079 put_task_struct(task);
1080 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1083 static const struct file_operations proc_sessionid_operations = {
1084 .read = proc_sessionid_read,
1088 #ifdef CONFIG_FAULT_INJECTION
1089 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1090 size_t count, loff_t *ppos)
1092 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1093 char buffer[PROC_NUMBUF];
1099 make_it_fail = task->make_it_fail;
1100 put_task_struct(task);
1102 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1104 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1107 static ssize_t proc_fault_inject_write(struct file * file,
1108 const char __user * buf, size_t count, loff_t *ppos)
1110 struct task_struct *task;
1111 char buffer[PROC_NUMBUF], *end;
1114 if (!capable(CAP_SYS_RESOURCE))
1116 memset(buffer, 0, sizeof(buffer));
1117 if (count > sizeof(buffer) - 1)
1118 count = sizeof(buffer) - 1;
1119 if (copy_from_user(buffer, buf, count))
1121 make_it_fail = simple_strtol(buffer, &end, 0);
1124 task = get_proc_task(file->f_dentry->d_inode);
1127 task->make_it_fail = make_it_fail;
1128 put_task_struct(task);
1129 if (end - buffer == 0)
1131 return end - buffer;
1134 static const struct file_operations proc_fault_inject_operations = {
1135 .read = proc_fault_inject_read,
1136 .write = proc_fault_inject_write,
1141 #ifdef CONFIG_SCHED_DEBUG
1143 * Print out various scheduling related per-task fields:
1145 static int sched_show(struct seq_file *m, void *v)
1147 struct inode *inode = m->private;
1148 struct task_struct *p;
1152 p = get_proc_task(inode);
1155 proc_sched_show_task(p, m);
1163 sched_write(struct file *file, const char __user *buf,
1164 size_t count, loff_t *offset)
1166 struct inode *inode = file->f_path.dentry->d_inode;
1167 struct task_struct *p;
1171 p = get_proc_task(inode);
1174 proc_sched_set_task(p);
1181 static int sched_open(struct inode *inode, struct file *filp)
1185 ret = single_open(filp, sched_show, NULL);
1187 struct seq_file *m = filp->private_data;
1194 static const struct file_operations proc_pid_sched_operations = {
1197 .write = sched_write,
1198 .llseek = seq_lseek,
1199 .release = single_release,
1205 * We added or removed a vma mapping the executable. The vmas are only mapped
1206 * during exec and are not mapped with the mmap system call.
1207 * Callers must hold down_write() on the mm's mmap_sem for these
1209 void added_exe_file_vma(struct mm_struct *mm)
1211 mm->num_exe_file_vmas++;
1214 void removed_exe_file_vma(struct mm_struct *mm)
1216 mm->num_exe_file_vmas--;
1217 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1219 mm->exe_file = NULL;
1224 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1227 get_file(new_exe_file);
1230 mm->exe_file = new_exe_file;
1231 mm->num_exe_file_vmas = 0;
1234 struct file *get_mm_exe_file(struct mm_struct *mm)
1236 struct file *exe_file;
1238 /* We need mmap_sem to protect against races with removal of
1239 * VM_EXECUTABLE vmas */
1240 down_read(&mm->mmap_sem);
1241 exe_file = mm->exe_file;
1244 up_read(&mm->mmap_sem);
1248 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1250 /* It's safe to write the exe_file pointer without exe_file_lock because
1251 * this is called during fork when the task is not yet in /proc */
1252 newmm->exe_file = get_mm_exe_file(oldmm);
1255 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1257 struct task_struct *task;
1258 struct mm_struct *mm;
1259 struct file *exe_file;
1261 task = get_proc_task(inode);
1264 mm = get_task_mm(task);
1265 put_task_struct(task);
1268 exe_file = get_mm_exe_file(mm);
1271 *exe_path = exe_file->f_path;
1272 path_get(&exe_file->f_path);
1279 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1281 struct inode *inode = dentry->d_inode;
1282 int error = -EACCES;
1284 /* We don't need a base pointer in the /proc filesystem */
1285 path_put(&nd->path);
1287 /* Are we allowed to snoop on the tasks file descriptors? */
1288 if (!proc_fd_access_allowed(inode))
1291 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1292 nd->last_type = LAST_BIND;
1294 return ERR_PTR(error);
1297 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1299 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1306 pathname = d_path(path, tmp, PAGE_SIZE);
1307 len = PTR_ERR(pathname);
1308 if (IS_ERR(pathname))
1310 len = tmp + PAGE_SIZE - 1 - pathname;
1314 if (copy_to_user(buffer, pathname, len))
1317 free_page((unsigned long)tmp);
1321 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1323 int error = -EACCES;
1324 struct inode *inode = dentry->d_inode;
1327 /* Are we allowed to snoop on the tasks file descriptors? */
1328 if (!proc_fd_access_allowed(inode))
1331 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1335 error = do_proc_readlink(&path, buffer, buflen);
1341 static const struct inode_operations proc_pid_link_inode_operations = {
1342 .readlink = proc_pid_readlink,
1343 .follow_link = proc_pid_follow_link,
1344 .setattr = proc_setattr,
1348 /* building an inode */
1350 static int task_dumpable(struct task_struct *task)
1353 struct mm_struct *mm;
1358 dumpable = get_dumpable(mm);
1366 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1368 struct inode * inode;
1369 struct proc_inode *ei;
1371 /* We need a new inode */
1373 inode = new_inode(sb);
1379 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1380 inode->i_op = &proc_def_inode_operations;
1383 * grab the reference to task.
1385 ei->pid = get_task_pid(task, PIDTYPE_PID);
1391 if (task_dumpable(task)) {
1392 inode->i_uid = task->euid;
1393 inode->i_gid = task->egid;
1395 security_task_to_inode(task, inode);
1405 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1407 struct inode *inode = dentry->d_inode;
1408 struct task_struct *task;
1409 generic_fillattr(inode, stat);
1414 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1416 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1417 task_dumpable(task)) {
1418 stat->uid = task->euid;
1419 stat->gid = task->egid;
1429 * Exceptional case: normally we are not allowed to unhash a busy
1430 * directory. In this case, however, we can do it - no aliasing problems
1431 * due to the way we treat inodes.
1433 * Rewrite the inode's ownerships here because the owning task may have
1434 * performed a setuid(), etc.
1436 * Before the /proc/pid/status file was created the only way to read
1437 * the effective uid of a /process was to stat /proc/pid. Reading
1438 * /proc/pid/status is slow enough that procps and other packages
1439 * kept stating /proc/pid. To keep the rules in /proc simple I have
1440 * made this apply to all per process world readable and executable
1443 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1445 struct inode *inode = dentry->d_inode;
1446 struct task_struct *task = get_proc_task(inode);
1448 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1449 task_dumpable(task)) {
1450 inode->i_uid = task->euid;
1451 inode->i_gid = task->egid;
1456 inode->i_mode &= ~(S_ISUID | S_ISGID);
1457 security_task_to_inode(task, inode);
1458 put_task_struct(task);
1465 static int pid_delete_dentry(struct dentry * dentry)
1467 /* Is the task we represent dead?
1468 * If so, then don't put the dentry on the lru list,
1469 * kill it immediately.
1471 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1474 static struct dentry_operations pid_dentry_operations =
1476 .d_revalidate = pid_revalidate,
1477 .d_delete = pid_delete_dentry,
1482 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1483 struct task_struct *, const void *);
1486 * Fill a directory entry.
1488 * If possible create the dcache entry and derive our inode number and
1489 * file type from dcache entry.
1491 * Since all of the proc inode numbers are dynamically generated, the inode
1492 * numbers do not exist until the inode is cache. This means creating the
1493 * the dcache entry in readdir is necessary to keep the inode numbers
1494 * reported by readdir in sync with the inode numbers reported
1497 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1498 char *name, int len,
1499 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1501 struct dentry *child, *dir = filp->f_path.dentry;
1502 struct inode *inode;
1505 unsigned type = DT_UNKNOWN;
1509 qname.hash = full_name_hash(name, len);
1511 child = d_lookup(dir, &qname);
1514 new = d_alloc(dir, &qname);
1516 child = instantiate(dir->d_inode, new, task, ptr);
1523 if (!child || IS_ERR(child) || !child->d_inode)
1524 goto end_instantiate;
1525 inode = child->d_inode;
1528 type = inode->i_mode >> 12;
1533 ino = find_inode_number(dir, &qname);
1536 return filldir(dirent, name, len, filp->f_pos, ino, type);
1539 static unsigned name_to_int(struct dentry *dentry)
1541 const char *name = dentry->d_name.name;
1542 int len = dentry->d_name.len;
1545 if (len > 1 && *name == '0')
1548 unsigned c = *name++ - '0';
1551 if (n >= (~0U-9)/10)
1561 #define PROC_FDINFO_MAX 64
1563 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1565 struct task_struct *task = get_proc_task(inode);
1566 struct files_struct *files = NULL;
1568 int fd = proc_fd(inode);
1571 files = get_files_struct(task);
1572 put_task_struct(task);
1576 * We are not taking a ref to the file structure, so we must
1579 spin_lock(&files->file_lock);
1580 file = fcheck_files(files, fd);
1583 *path = file->f_path;
1584 path_get(&file->f_path);
1587 snprintf(info, PROC_FDINFO_MAX,
1590 (long long) file->f_pos,
1592 spin_unlock(&files->file_lock);
1593 put_files_struct(files);
1596 spin_unlock(&files->file_lock);
1597 put_files_struct(files);
1602 static int proc_fd_link(struct inode *inode, struct path *path)
1604 return proc_fd_info(inode, path, NULL);
1607 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1609 struct inode *inode = dentry->d_inode;
1610 struct task_struct *task = get_proc_task(inode);
1611 int fd = proc_fd(inode);
1612 struct files_struct *files;
1615 files = get_files_struct(task);
1618 if (fcheck_files(files, fd)) {
1620 put_files_struct(files);
1621 if (task_dumpable(task)) {
1622 inode->i_uid = task->euid;
1623 inode->i_gid = task->egid;
1628 inode->i_mode &= ~(S_ISUID | S_ISGID);
1629 security_task_to_inode(task, inode);
1630 put_task_struct(task);
1634 put_files_struct(files);
1636 put_task_struct(task);
1642 static struct dentry_operations tid_fd_dentry_operations =
1644 .d_revalidate = tid_fd_revalidate,
1645 .d_delete = pid_delete_dentry,
1648 static struct dentry *proc_fd_instantiate(struct inode *dir,
1649 struct dentry *dentry, struct task_struct *task, const void *ptr)
1651 unsigned fd = *(const unsigned *)ptr;
1653 struct files_struct *files;
1654 struct inode *inode;
1655 struct proc_inode *ei;
1656 struct dentry *error = ERR_PTR(-ENOENT);
1658 inode = proc_pid_make_inode(dir->i_sb, task);
1663 files = get_files_struct(task);
1666 inode->i_mode = S_IFLNK;
1669 * We are not taking a ref to the file structure, so we must
1672 spin_lock(&files->file_lock);
1673 file = fcheck_files(files, fd);
1676 if (file->f_mode & 1)
1677 inode->i_mode |= S_IRUSR | S_IXUSR;
1678 if (file->f_mode & 2)
1679 inode->i_mode |= S_IWUSR | S_IXUSR;
1680 spin_unlock(&files->file_lock);
1681 put_files_struct(files);
1683 inode->i_op = &proc_pid_link_inode_operations;
1685 ei->op.proc_get_link = proc_fd_link;
1686 dentry->d_op = &tid_fd_dentry_operations;
1687 d_add(dentry, inode);
1688 /* Close the race of the process dying before we return the dentry */
1689 if (tid_fd_revalidate(dentry, NULL))
1695 spin_unlock(&files->file_lock);
1696 put_files_struct(files);
1702 static struct dentry *proc_lookupfd_common(struct inode *dir,
1703 struct dentry *dentry,
1704 instantiate_t instantiate)
1706 struct task_struct *task = get_proc_task(dir);
1707 unsigned fd = name_to_int(dentry);
1708 struct dentry *result = ERR_PTR(-ENOENT);
1715 result = instantiate(dir, dentry, task, &fd);
1717 put_task_struct(task);
1722 static int proc_readfd_common(struct file * filp, void * dirent,
1723 filldir_t filldir, instantiate_t instantiate)
1725 struct dentry *dentry = filp->f_path.dentry;
1726 struct inode *inode = dentry->d_inode;
1727 struct task_struct *p = get_proc_task(inode);
1728 unsigned int fd, ino;
1730 struct files_struct * files;
1740 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1744 ino = parent_ino(dentry);
1745 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1749 files = get_files_struct(p);
1753 for (fd = filp->f_pos-2;
1754 fd < files_fdtable(files)->max_fds;
1755 fd++, filp->f_pos++) {
1756 char name[PROC_NUMBUF];
1759 if (!fcheck_files(files, fd))
1763 len = snprintf(name, sizeof(name), "%d", fd);
1764 if (proc_fill_cache(filp, dirent, filldir,
1765 name, len, instantiate,
1773 put_files_struct(files);
1781 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1782 struct nameidata *nd)
1784 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1787 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1789 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1792 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1793 size_t len, loff_t *ppos)
1795 char tmp[PROC_FDINFO_MAX];
1796 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1798 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1802 static const struct file_operations proc_fdinfo_file_operations = {
1803 .open = nonseekable_open,
1804 .read = proc_fdinfo_read,
1807 static const struct file_operations proc_fd_operations = {
1808 .read = generic_read_dir,
1809 .readdir = proc_readfd,
1813 * /proc/pid/fd needs a special permission handler so that a process can still
1814 * access /proc/self/fd after it has executed a setuid().
1816 static int proc_fd_permission(struct inode *inode, int mask,
1817 struct nameidata *nd)
1821 rv = generic_permission(inode, mask, NULL);
1824 if (task_pid(current) == proc_pid(inode))
1830 * proc directories can do almost nothing..
1832 static const struct inode_operations proc_fd_inode_operations = {
1833 .lookup = proc_lookupfd,
1834 .permission = proc_fd_permission,
1835 .setattr = proc_setattr,
1838 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1839 struct dentry *dentry, struct task_struct *task, const void *ptr)
1841 unsigned fd = *(unsigned *)ptr;
1842 struct inode *inode;
1843 struct proc_inode *ei;
1844 struct dentry *error = ERR_PTR(-ENOENT);
1846 inode = proc_pid_make_inode(dir->i_sb, task);
1851 inode->i_mode = S_IFREG | S_IRUSR;
1852 inode->i_fop = &proc_fdinfo_file_operations;
1853 dentry->d_op = &tid_fd_dentry_operations;
1854 d_add(dentry, inode);
1855 /* Close the race of the process dying before we return the dentry */
1856 if (tid_fd_revalidate(dentry, NULL))
1863 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1864 struct dentry *dentry,
1865 struct nameidata *nd)
1867 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1870 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1872 return proc_readfd_common(filp, dirent, filldir,
1873 proc_fdinfo_instantiate);
1876 static const struct file_operations proc_fdinfo_operations = {
1877 .read = generic_read_dir,
1878 .readdir = proc_readfdinfo,
1882 * proc directories can do almost nothing..
1884 static const struct inode_operations proc_fdinfo_inode_operations = {
1885 .lookup = proc_lookupfdinfo,
1886 .setattr = proc_setattr,
1890 static struct dentry *proc_pident_instantiate(struct inode *dir,
1891 struct dentry *dentry, struct task_struct *task, const void *ptr)
1893 const struct pid_entry *p = ptr;
1894 struct inode *inode;
1895 struct proc_inode *ei;
1896 struct dentry *error = ERR_PTR(-EINVAL);
1898 inode = proc_pid_make_inode(dir->i_sb, task);
1903 inode->i_mode = p->mode;
1904 if (S_ISDIR(inode->i_mode))
1905 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1907 inode->i_op = p->iop;
1909 inode->i_fop = p->fop;
1911 dentry->d_op = &pid_dentry_operations;
1912 d_add(dentry, inode);
1913 /* Close the race of the process dying before we return the dentry */
1914 if (pid_revalidate(dentry, NULL))
1920 static struct dentry *proc_pident_lookup(struct inode *dir,
1921 struct dentry *dentry,
1922 const struct pid_entry *ents,
1925 struct inode *inode;
1926 struct dentry *error;
1927 struct task_struct *task = get_proc_task(dir);
1928 const struct pid_entry *p, *last;
1930 error = ERR_PTR(-ENOENT);
1937 * Yes, it does not scale. And it should not. Don't add
1938 * new entries into /proc/<tgid>/ without very good reasons.
1940 last = &ents[nents - 1];
1941 for (p = ents; p <= last; p++) {
1942 if (p->len != dentry->d_name.len)
1944 if (!memcmp(dentry->d_name.name, p->name, p->len))
1950 error = proc_pident_instantiate(dir, dentry, task, p);
1952 put_task_struct(task);
1957 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1958 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1960 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1961 proc_pident_instantiate, task, p);
1964 static int proc_pident_readdir(struct file *filp,
1965 void *dirent, filldir_t filldir,
1966 const struct pid_entry *ents, unsigned int nents)
1969 struct dentry *dentry = filp->f_path.dentry;
1970 struct inode *inode = dentry->d_inode;
1971 struct task_struct *task = get_proc_task(inode);
1972 const struct pid_entry *p, *last;
1985 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1991 ino = parent_ino(dentry);
1992 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2004 last = &ents[nents - 1];
2006 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2015 put_task_struct(task);
2020 #ifdef CONFIG_SECURITY
2021 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2022 size_t count, loff_t *ppos)
2024 struct inode * inode = file->f_path.dentry->d_inode;
2027 struct task_struct *task = get_proc_task(inode);
2032 length = security_getprocattr(task,
2033 (char*)file->f_path.dentry->d_name.name,
2035 put_task_struct(task);
2037 length = simple_read_from_buffer(buf, count, ppos, p, length);
2042 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2043 size_t count, loff_t *ppos)
2045 struct inode * inode = file->f_path.dentry->d_inode;
2048 struct task_struct *task = get_proc_task(inode);
2053 if (count > PAGE_SIZE)
2056 /* No partial writes. */
2062 page = (char*)__get_free_page(GFP_TEMPORARY);
2067 if (copy_from_user(page, buf, count))
2070 length = security_setprocattr(task,
2071 (char*)file->f_path.dentry->d_name.name,
2072 (void*)page, count);
2074 free_page((unsigned long) page);
2076 put_task_struct(task);
2081 static const struct file_operations proc_pid_attr_operations = {
2082 .read = proc_pid_attr_read,
2083 .write = proc_pid_attr_write,
2086 static const struct pid_entry attr_dir_stuff[] = {
2087 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2088 REG("prev", S_IRUGO, pid_attr),
2089 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2090 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2091 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2092 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
2095 static int proc_attr_dir_readdir(struct file * filp,
2096 void * dirent, filldir_t filldir)
2098 return proc_pident_readdir(filp,dirent,filldir,
2099 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2102 static const struct file_operations proc_attr_dir_operations = {
2103 .read = generic_read_dir,
2104 .readdir = proc_attr_dir_readdir,
2107 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2108 struct dentry *dentry, struct nameidata *nd)
2110 return proc_pident_lookup(dir, dentry,
2111 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2114 static const struct inode_operations proc_attr_dir_inode_operations = {
2115 .lookup = proc_attr_dir_lookup,
2116 .getattr = pid_getattr,
2117 .setattr = proc_setattr,
2122 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2123 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2124 size_t count, loff_t *ppos)
2126 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2127 struct mm_struct *mm;
2128 char buffer[PROC_NUMBUF];
2136 mm = get_task_mm(task);
2138 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2139 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2140 MMF_DUMP_FILTER_SHIFT));
2142 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2145 put_task_struct(task);
2150 static ssize_t proc_coredump_filter_write(struct file *file,
2151 const char __user *buf,
2155 struct task_struct *task;
2156 struct mm_struct *mm;
2157 char buffer[PROC_NUMBUF], *end;
2164 memset(buffer, 0, sizeof(buffer));
2165 if (count > sizeof(buffer) - 1)
2166 count = sizeof(buffer) - 1;
2167 if (copy_from_user(buffer, buf, count))
2171 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2174 if (end - buffer == 0)
2178 task = get_proc_task(file->f_dentry->d_inode);
2183 mm = get_task_mm(task);
2187 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2189 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2191 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2196 put_task_struct(task);
2201 static const struct file_operations proc_coredump_filter_operations = {
2202 .read = proc_coredump_filter_read,
2203 .write = proc_coredump_filter_write,
2210 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2213 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2214 pid_t tgid = task_tgid_nr_ns(current, ns);
2215 char tmp[PROC_NUMBUF];
2218 sprintf(tmp, "%d", tgid);
2219 return vfs_readlink(dentry,buffer,buflen,tmp);
2222 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2224 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2225 pid_t tgid = task_tgid_nr_ns(current, ns);
2226 char tmp[PROC_NUMBUF];
2228 return ERR_PTR(-ENOENT);
2229 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2230 return ERR_PTR(vfs_follow_link(nd,tmp));
2233 static const struct inode_operations proc_self_inode_operations = {
2234 .readlink = proc_self_readlink,
2235 .follow_link = proc_self_follow_link,
2241 * These are the directory entries in the root directory of /proc
2242 * that properly belong to the /proc filesystem, as they describe
2243 * describe something that is process related.
2245 static const struct pid_entry proc_base_stuff[] = {
2246 NOD("self", S_IFLNK|S_IRWXUGO,
2247 &proc_self_inode_operations, NULL, {}),
2251 * Exceptional case: normally we are not allowed to unhash a busy
2252 * directory. In this case, however, we can do it - no aliasing problems
2253 * due to the way we treat inodes.
2255 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2257 struct inode *inode = dentry->d_inode;
2258 struct task_struct *task = get_proc_task(inode);
2260 put_task_struct(task);
2267 static struct dentry_operations proc_base_dentry_operations =
2269 .d_revalidate = proc_base_revalidate,
2270 .d_delete = pid_delete_dentry,
2273 static struct dentry *proc_base_instantiate(struct inode *dir,
2274 struct dentry *dentry, struct task_struct *task, const void *ptr)
2276 const struct pid_entry *p = ptr;
2277 struct inode *inode;
2278 struct proc_inode *ei;
2279 struct dentry *error = ERR_PTR(-EINVAL);
2281 /* Allocate the inode */
2282 error = ERR_PTR(-ENOMEM);
2283 inode = new_inode(dir->i_sb);
2287 /* Initialize the inode */
2289 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2292 * grab the reference to the task.
2294 ei->pid = get_task_pid(task, PIDTYPE_PID);
2300 inode->i_mode = p->mode;
2301 if (S_ISDIR(inode->i_mode))
2303 if (S_ISLNK(inode->i_mode))
2306 inode->i_op = p->iop;
2308 inode->i_fop = p->fop;
2310 dentry->d_op = &proc_base_dentry_operations;
2311 d_add(dentry, inode);
2320 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2322 struct dentry *error;
2323 struct task_struct *task = get_proc_task(dir);
2324 const struct pid_entry *p, *last;
2326 error = ERR_PTR(-ENOENT);
2331 /* Lookup the directory entry */
2332 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2333 for (p = proc_base_stuff; p <= last; p++) {
2334 if (p->len != dentry->d_name.len)
2336 if (!memcmp(dentry->d_name.name, p->name, p->len))
2342 error = proc_base_instantiate(dir, dentry, task, p);
2345 put_task_struct(task);
2350 static int proc_base_fill_cache(struct file *filp, void *dirent,
2351 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2353 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2354 proc_base_instantiate, task, p);
2357 #ifdef CONFIG_TASK_IO_ACCOUNTING
2358 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2360 return sprintf(buffer,
2361 #ifdef CONFIG_TASK_XACCT
2367 "read_bytes: %llu\n"
2368 "write_bytes: %llu\n"
2369 "cancelled_write_bytes: %llu\n",
2370 #ifdef CONFIG_TASK_XACCT
2371 (unsigned long long)task->rchar,
2372 (unsigned long long)task->wchar,
2373 (unsigned long long)task->syscr,
2374 (unsigned long long)task->syscw,
2376 (unsigned long long)task->ioac.read_bytes,
2377 (unsigned long long)task->ioac.write_bytes,
2378 (unsigned long long)task->ioac.cancelled_write_bytes);
2385 static const struct file_operations proc_task_operations;
2386 static const struct inode_operations proc_task_inode_operations;
2388 static const struct pid_entry tgid_base_stuff[] = {
2389 DIR("task", S_IRUGO|S_IXUGO, task),
2390 DIR("fd", S_IRUSR|S_IXUSR, fd),
2391 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2393 DIR("net", S_IRUGO|S_IXUGO, net),
2395 REG("environ", S_IRUSR, environ),
2396 INF("auxv", S_IRUSR, pid_auxv),
2397 ONE("status", S_IRUGO, pid_status),
2398 INF("limits", S_IRUSR, pid_limits),
2399 #ifdef CONFIG_SCHED_DEBUG
2400 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2402 INF("cmdline", S_IRUGO, pid_cmdline),
2403 ONE("stat", S_IRUGO, tgid_stat),
2404 ONE("statm", S_IRUGO, pid_statm),
2405 REG("maps", S_IRUGO, maps),
2407 REG("numa_maps", S_IRUGO, numa_maps),
2409 REG("mem", S_IRUSR|S_IWUSR, mem),
2413 REG("mounts", S_IRUGO, mounts),
2414 REG("mountinfo", S_IRUGO, mountinfo),
2415 REG("mountstats", S_IRUSR, mountstats),
2416 #ifdef CONFIG_PROC_PAGE_MONITOR
2417 REG("clear_refs", S_IWUSR, clear_refs),
2418 REG("smaps", S_IRUGO, smaps),
2419 REG("pagemap", S_IRUSR, pagemap),
2421 #ifdef CONFIG_SECURITY
2422 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2424 #ifdef CONFIG_KALLSYMS
2425 INF("wchan", S_IRUGO, pid_wchan),
2427 #ifdef CONFIG_SCHEDSTATS
2428 INF("schedstat", S_IRUGO, pid_schedstat),
2430 #ifdef CONFIG_LATENCYTOP
2431 REG("latency", S_IRUGO, lstats),
2433 #ifdef CONFIG_PROC_PID_CPUSET
2434 REG("cpuset", S_IRUGO, cpuset),
2436 #ifdef CONFIG_CGROUPS
2437 REG("cgroup", S_IRUGO, cgroup),
2439 INF("oom_score", S_IRUGO, oom_score),
2440 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2441 #ifdef CONFIG_AUDITSYSCALL
2442 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2443 REG("sessionid", S_IRUSR, sessionid),
2445 #ifdef CONFIG_FAULT_INJECTION
2446 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2448 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2449 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2451 #ifdef CONFIG_TASK_IO_ACCOUNTING
2452 INF("io", S_IRUGO, pid_io_accounting),
2456 static int proc_tgid_base_readdir(struct file * filp,
2457 void * dirent, filldir_t filldir)
2459 return proc_pident_readdir(filp,dirent,filldir,
2460 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2463 static const struct file_operations proc_tgid_base_operations = {
2464 .read = generic_read_dir,
2465 .readdir = proc_tgid_base_readdir,
2468 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2469 return proc_pident_lookup(dir, dentry,
2470 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2473 static const struct inode_operations proc_tgid_base_inode_operations = {
2474 .lookup = proc_tgid_base_lookup,
2475 .getattr = pid_getattr,
2476 .setattr = proc_setattr,
2479 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2481 struct dentry *dentry, *leader, *dir;
2482 char buf[PROC_NUMBUF];
2486 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2487 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2489 if (!(current->flags & PF_EXITING))
2490 shrink_dcache_parent(dentry);
2499 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2500 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2505 name.len = strlen(name.name);
2506 dir = d_hash_and_lookup(leader, &name);
2508 goto out_put_leader;
2511 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2512 dentry = d_hash_and_lookup(dir, &name);
2514 shrink_dcache_parent(dentry);
2527 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2528 * @task: task that should be flushed.
2530 * When flushing dentries from proc, one needs to flush them from global
2531 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2532 * in. This call is supposed to do all of this job.
2534 * Looks in the dcache for
2536 * /proc/@tgid/task/@pid
2537 * if either directory is present flushes it and all of it'ts children
2540 * It is safe and reasonable to cache /proc entries for a task until
2541 * that task exits. After that they just clog up the dcache with
2542 * useless entries, possibly causing useful dcache entries to be
2543 * flushed instead. This routine is proved to flush those useless
2544 * dcache entries at process exit time.
2546 * NOTE: This routine is just an optimization so it does not guarantee
2547 * that no dcache entries will exist at process exit time it
2548 * just makes it very unlikely that any will persist.
2551 void proc_flush_task(struct task_struct *task)
2554 struct pid *pid, *tgid = NULL;
2557 pid = task_pid(task);
2558 if (thread_group_leader(task))
2559 tgid = task_tgid(task);
2561 for (i = 0; i <= pid->level; i++) {
2562 upid = &pid->numbers[i];
2563 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2564 tgid ? tgid->numbers[i].nr : 0);
2567 upid = &pid->numbers[pid->level];
2569 pid_ns_release_proc(upid->ns);
2572 static struct dentry *proc_pid_instantiate(struct inode *dir,
2573 struct dentry * dentry,
2574 struct task_struct *task, const void *ptr)
2576 struct dentry *error = ERR_PTR(-ENOENT);
2577 struct inode *inode;
2579 inode = proc_pid_make_inode(dir->i_sb, task);
2583 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2584 inode->i_op = &proc_tgid_base_inode_operations;
2585 inode->i_fop = &proc_tgid_base_operations;
2586 inode->i_flags|=S_IMMUTABLE;
2588 #ifdef CONFIG_SECURITY
2589 inode->i_nlink += 1;
2592 dentry->d_op = &pid_dentry_operations;
2594 d_add(dentry, inode);
2595 /* Close the race of the process dying before we return the dentry */
2596 if (pid_revalidate(dentry, NULL))
2602 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2604 struct dentry *result = ERR_PTR(-ENOENT);
2605 struct task_struct *task;
2607 struct pid_namespace *ns;
2609 result = proc_base_lookup(dir, dentry);
2610 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2613 tgid = name_to_int(dentry);
2617 ns = dentry->d_sb->s_fs_info;
2619 task = find_task_by_pid_ns(tgid, ns);
2621 get_task_struct(task);
2626 result = proc_pid_instantiate(dir, dentry, task, NULL);
2627 put_task_struct(task);
2633 * Find the first task with tgid >= tgid
2638 struct task_struct *task;
2640 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2645 put_task_struct(iter.task);
2649 pid = find_ge_pid(iter.tgid, ns);
2651 iter.tgid = pid_nr_ns(pid, ns);
2652 iter.task = pid_task(pid, PIDTYPE_PID);
2653 /* What we to know is if the pid we have find is the
2654 * pid of a thread_group_leader. Testing for task
2655 * being a thread_group_leader is the obvious thing
2656 * todo but there is a window when it fails, due to
2657 * the pid transfer logic in de_thread.
2659 * So we perform the straight forward test of seeing
2660 * if the pid we have found is the pid of a thread
2661 * group leader, and don't worry if the task we have
2662 * found doesn't happen to be a thread group leader.
2663 * As we don't care in the case of readdir.
2665 if (!iter.task || !has_group_leader_pid(iter.task)) {
2669 get_task_struct(iter.task);
2675 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2677 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2678 struct tgid_iter iter)
2680 char name[PROC_NUMBUF];
2681 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2682 return proc_fill_cache(filp, dirent, filldir, name, len,
2683 proc_pid_instantiate, iter.task, NULL);
2686 /* for the /proc/ directory itself, after non-process stuff has been done */
2687 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2689 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2690 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2691 struct tgid_iter iter;
2692 struct pid_namespace *ns;
2697 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2698 const struct pid_entry *p = &proc_base_stuff[nr];
2699 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2703 ns = filp->f_dentry->d_sb->s_fs_info;
2705 iter.tgid = filp->f_pos - TGID_OFFSET;
2706 for (iter = next_tgid(ns, iter);
2708 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2709 filp->f_pos = iter.tgid + TGID_OFFSET;
2710 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2711 put_task_struct(iter.task);
2715 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2717 put_task_struct(reaper);
2725 static const struct pid_entry tid_base_stuff[] = {
2726 DIR("fd", S_IRUSR|S_IXUSR, fd),
2727 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2728 REG("environ", S_IRUSR, environ),
2729 INF("auxv", S_IRUSR, pid_auxv),
2730 ONE("status", S_IRUGO, pid_status),
2731 INF("limits", S_IRUSR, pid_limits),
2732 #ifdef CONFIG_SCHED_DEBUG
2733 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2735 INF("cmdline", S_IRUGO, pid_cmdline),
2736 ONE("stat", S_IRUGO, tid_stat),
2737 ONE("statm", S_IRUGO, pid_statm),
2738 REG("maps", S_IRUGO, maps),
2740 REG("numa_maps", S_IRUGO, numa_maps),
2742 REG("mem", S_IRUSR|S_IWUSR, mem),
2746 REG("mounts", S_IRUGO, mounts),
2747 REG("mountinfo", S_IRUGO, mountinfo),
2748 #ifdef CONFIG_PROC_PAGE_MONITOR
2749 REG("clear_refs", S_IWUSR, clear_refs),
2750 REG("smaps", S_IRUGO, smaps),
2751 REG("pagemap", S_IRUSR, pagemap),
2753 #ifdef CONFIG_SECURITY
2754 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2756 #ifdef CONFIG_KALLSYMS
2757 INF("wchan", S_IRUGO, pid_wchan),
2759 #ifdef CONFIG_SCHEDSTATS
2760 INF("schedstat", S_IRUGO, pid_schedstat),
2762 #ifdef CONFIG_LATENCYTOP
2763 REG("latency", S_IRUGO, lstats),
2765 #ifdef CONFIG_PROC_PID_CPUSET
2766 REG("cpuset", S_IRUGO, cpuset),
2768 #ifdef CONFIG_CGROUPS
2769 REG("cgroup", S_IRUGO, cgroup),
2771 INF("oom_score", S_IRUGO, oom_score),
2772 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2773 #ifdef CONFIG_AUDITSYSCALL
2774 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2775 REG("sessionid", S_IRUSR, sessionid),
2777 #ifdef CONFIG_FAULT_INJECTION
2778 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2782 static int proc_tid_base_readdir(struct file * filp,
2783 void * dirent, filldir_t filldir)
2785 return proc_pident_readdir(filp,dirent,filldir,
2786 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2789 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2790 return proc_pident_lookup(dir, dentry,
2791 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2794 static const struct file_operations proc_tid_base_operations = {
2795 .read = generic_read_dir,
2796 .readdir = proc_tid_base_readdir,
2799 static const struct inode_operations proc_tid_base_inode_operations = {
2800 .lookup = proc_tid_base_lookup,
2801 .getattr = pid_getattr,
2802 .setattr = proc_setattr,
2805 static struct dentry *proc_task_instantiate(struct inode *dir,
2806 struct dentry *dentry, struct task_struct *task, const void *ptr)
2808 struct dentry *error = ERR_PTR(-ENOENT);
2809 struct inode *inode;
2810 inode = proc_pid_make_inode(dir->i_sb, task);
2814 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2815 inode->i_op = &proc_tid_base_inode_operations;
2816 inode->i_fop = &proc_tid_base_operations;
2817 inode->i_flags|=S_IMMUTABLE;
2819 #ifdef CONFIG_SECURITY
2820 inode->i_nlink += 1;
2823 dentry->d_op = &pid_dentry_operations;
2825 d_add(dentry, inode);
2826 /* Close the race of the process dying before we return the dentry */
2827 if (pid_revalidate(dentry, NULL))
2833 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2835 struct dentry *result = ERR_PTR(-ENOENT);
2836 struct task_struct *task;
2837 struct task_struct *leader = get_proc_task(dir);
2839 struct pid_namespace *ns;
2844 tid = name_to_int(dentry);
2848 ns = dentry->d_sb->s_fs_info;
2850 task = find_task_by_pid_ns(tid, ns);
2852 get_task_struct(task);
2856 if (!same_thread_group(leader, task))
2859 result = proc_task_instantiate(dir, dentry, task, NULL);
2861 put_task_struct(task);
2863 put_task_struct(leader);
2869 * Find the first tid of a thread group to return to user space.
2871 * Usually this is just the thread group leader, but if the users
2872 * buffer was too small or there was a seek into the middle of the
2873 * directory we have more work todo.
2875 * In the case of a short read we start with find_task_by_pid.
2877 * In the case of a seek we start with the leader and walk nr
2880 static struct task_struct *first_tid(struct task_struct *leader,
2881 int tid, int nr, struct pid_namespace *ns)
2883 struct task_struct *pos;
2886 /* Attempt to start with the pid of a thread */
2887 if (tid && (nr > 0)) {
2888 pos = find_task_by_pid_ns(tid, ns);
2889 if (pos && (pos->group_leader == leader))
2893 /* If nr exceeds the number of threads there is nothing todo */
2895 if (nr && nr >= get_nr_threads(leader))
2898 /* If we haven't found our starting place yet start
2899 * with the leader and walk nr threads forward.
2901 for (pos = leader; nr > 0; --nr) {
2902 pos = next_thread(pos);
2903 if (pos == leader) {
2909 get_task_struct(pos);
2916 * Find the next thread in the thread list.
2917 * Return NULL if there is an error or no next thread.
2919 * The reference to the input task_struct is released.
2921 static struct task_struct *next_tid(struct task_struct *start)
2923 struct task_struct *pos = NULL;
2925 if (pid_alive(start)) {
2926 pos = next_thread(start);
2927 if (thread_group_leader(pos))
2930 get_task_struct(pos);
2933 put_task_struct(start);
2937 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2938 struct task_struct *task, int tid)
2940 char name[PROC_NUMBUF];
2941 int len = snprintf(name, sizeof(name), "%d", tid);
2942 return proc_fill_cache(filp, dirent, filldir, name, len,
2943 proc_task_instantiate, task, NULL);
2946 /* for the /proc/TGID/task/ directories */
2947 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2949 struct dentry *dentry = filp->f_path.dentry;
2950 struct inode *inode = dentry->d_inode;
2951 struct task_struct *leader = NULL;
2952 struct task_struct *task;
2953 int retval = -ENOENT;
2956 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2957 struct pid_namespace *ns;
2959 task = get_proc_task(inode);
2963 if (pid_alive(task)) {
2964 leader = task->group_leader;
2965 get_task_struct(leader);
2968 put_task_struct(task);
2976 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2981 ino = parent_ino(dentry);
2982 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2988 /* f_version caches the tgid value that the last readdir call couldn't
2989 * return. lseek aka telldir automagically resets f_version to 0.
2991 ns = filp->f_dentry->d_sb->s_fs_info;
2992 tid = (int)filp->f_version;
2993 filp->f_version = 0;
2994 for (task = first_tid(leader, tid, pos - 2, ns);
2996 task = next_tid(task), pos++) {
2997 tid = task_pid_nr_ns(task, ns);
2998 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2999 /* returning this tgid failed, save it as the first
3000 * pid for the next readir call */
3001 filp->f_version = (u64)tid;
3002 put_task_struct(task);
3008 put_task_struct(leader);
3013 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3015 struct inode *inode = dentry->d_inode;
3016 struct task_struct *p = get_proc_task(inode);
3017 generic_fillattr(inode, stat);
3021 stat->nlink += get_nr_threads(p);
3029 static const struct inode_operations proc_task_inode_operations = {
3030 .lookup = proc_task_lookup,
3031 .getattr = proc_task_getattr,
3032 .setattr = proc_setattr,
3035 static const struct file_operations proc_task_operations = {
3036 .read = generic_read_dir,
3037 .readdir = proc_task_readdir,