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/fdtable.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/mnt_namespace.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/resource.h>
68 #include <linux/module.h>
69 #include <linux/mount.h>
70 #include <linux/security.h>
71 #include <linux/ptrace.h>
72 #include <linux/cgroup.h>
73 #include <linux/cpuset.h>
74 #include <linux/audit.h>
75 #include <linux/poll.h>
76 #include <linux/nsproxy.h>
77 #include <linux/oom.h>
78 #include <linux/elf.h>
79 #include <linux/pid_namespace.h>
83 * Implementing inode permission operations in /proc is almost
84 * certainly an error. Permission checks need to happen during
85 * each system call not at open time. The reason is that most of
86 * what we wish to check for permissions in /proc varies at runtime.
88 * The classic example of a problem is opening file descriptors
89 * in /proc for a task before it execs a suid executable.
96 const struct inode_operations *iop;
97 const struct file_operations *fop;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
103 .len = sizeof(NAME) - 1, \
110 #define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 #define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
118 #define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
120 &proc_##OTYPE##_operations, {})
121 #define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
125 #define ONE(NAME, MODE, OTYPE) \
126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_single_file_operations, \
128 { .proc_show = &proc_##OTYPE } )
131 EXPORT_SYMBOL(maps_protect);
133 static struct fs_struct *get_fs_struct(struct task_struct *task)
135 struct fs_struct *fs;
139 atomic_inc(&fs->count);
144 static int get_nr_threads(struct task_struct *tsk)
146 /* Must be called with the rcu_read_lock held */
150 if (lock_task_sighand(tsk, &flags)) {
151 count = atomic_read(&tsk->signal->count);
152 unlock_task_sighand(tsk, &flags);
157 static int proc_cwd_link(struct inode *inode, struct path *path)
159 struct task_struct *task = get_proc_task(inode);
160 struct fs_struct *fs = NULL;
161 int result = -ENOENT;
164 fs = get_fs_struct(task);
165 put_task_struct(task);
168 read_lock(&fs->lock);
171 read_unlock(&fs->lock);
178 static int proc_root_link(struct inode *inode, struct path *path)
180 struct task_struct *task = get_proc_task(inode);
181 struct fs_struct *fs = NULL;
182 int result = -ENOENT;
185 fs = get_fs_struct(task);
186 put_task_struct(task);
189 read_lock(&fs->lock);
192 read_unlock(&fs->lock);
200 * Return zero if current may access user memory in @task, -error if not.
202 static int check_mem_permission(struct task_struct *task)
205 * A task can always look at itself, in case it chooses
206 * to use system calls instead of load instructions.
212 * If current is actively ptrace'ing, and would also be
213 * permitted to freshly attach with ptrace now, permit it.
215 if (task->parent == current && (task->ptrace & PT_PTRACED) &&
216 task_is_stopped_or_traced(task) &&
217 ptrace_may_attach(task))
221 * Noone else is allowed.
226 struct mm_struct *mm_for_maps(struct task_struct *task)
228 struct mm_struct *mm = get_task_mm(task);
231 down_read(&mm->mmap_sem);
235 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
241 up_read(&mm->mmap_sem);
246 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
250 struct mm_struct *mm = get_task_mm(task);
254 goto out_mm; /* Shh! No looking before we're done */
256 len = mm->arg_end - mm->arg_start;
261 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
263 // If the nul at the end of args has been overwritten, then
264 // assume application is using setproctitle(3).
265 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
266 len = strnlen(buffer, res);
270 len = mm->env_end - mm->env_start;
271 if (len > PAGE_SIZE - res)
272 len = PAGE_SIZE - res;
273 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
274 res = strnlen(buffer, res);
283 static int proc_pid_auxv(struct task_struct *task, char *buffer)
286 struct mm_struct *mm = get_task_mm(task);
288 unsigned int nwords = 0;
291 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
292 res = nwords * sizeof(mm->saved_auxv[0]);
295 memcpy(buffer, mm->saved_auxv, res);
302 #ifdef CONFIG_KALLSYMS
304 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
305 * Returns the resolved symbol. If that fails, simply return the address.
307 static int proc_pid_wchan(struct task_struct *task, char *buffer)
310 char symname[KSYM_NAME_LEN];
312 wchan = get_wchan(task);
314 if (lookup_symbol_name(wchan, symname) < 0)
315 return sprintf(buffer, "%lu", wchan);
317 return sprintf(buffer, "%s", symname);
319 #endif /* CONFIG_KALLSYMS */
321 #ifdef CONFIG_SCHEDSTATS
323 * Provides /proc/PID/schedstat
325 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
327 return sprintf(buffer, "%llu %llu %lu\n",
328 task->sched_info.cpu_time,
329 task->sched_info.run_delay,
330 task->sched_info.pcount);
334 #ifdef CONFIG_LATENCYTOP
335 static int lstats_show_proc(struct seq_file *m, void *v)
338 struct inode *inode = m->private;
339 struct task_struct *task = get_proc_task(inode);
343 seq_puts(m, "Latency Top version : v0.1\n");
344 for (i = 0; i < 32; i++) {
345 if (task->latency_record[i].backtrace[0]) {
347 seq_printf(m, "%i %li %li ",
348 task->latency_record[i].count,
349 task->latency_record[i].time,
350 task->latency_record[i].max);
351 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
352 char sym[KSYM_NAME_LEN];
354 if (!task->latency_record[i].backtrace[q])
356 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
358 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
359 c = strchr(sym, '+');
362 seq_printf(m, "%s ", sym);
368 put_task_struct(task);
372 static int lstats_open(struct inode *inode, struct file *file)
374 return single_open(file, lstats_show_proc, inode);
377 static ssize_t lstats_write(struct file *file, const char __user *buf,
378 size_t count, loff_t *offs)
380 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
384 clear_all_latency_tracing(task);
385 put_task_struct(task);
390 static const struct file_operations proc_lstats_operations = {
393 .write = lstats_write,
395 .release = single_release,
400 /* The badness from the OOM killer */
401 unsigned long badness(struct task_struct *p, unsigned long uptime);
402 static int proc_oom_score(struct task_struct *task, char *buffer)
404 unsigned long points;
405 struct timespec uptime;
407 do_posix_clock_monotonic_gettime(&uptime);
408 read_lock(&tasklist_lock);
409 points = badness(task, uptime.tv_sec);
410 read_unlock(&tasklist_lock);
411 return sprintf(buffer, "%lu\n", points);
419 static const struct limit_names lnames[RLIM_NLIMITS] = {
420 [RLIMIT_CPU] = {"Max cpu time", "ms"},
421 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
422 [RLIMIT_DATA] = {"Max data size", "bytes"},
423 [RLIMIT_STACK] = {"Max stack size", "bytes"},
424 [RLIMIT_CORE] = {"Max core file size", "bytes"},
425 [RLIMIT_RSS] = {"Max resident set", "bytes"},
426 [RLIMIT_NPROC] = {"Max processes", "processes"},
427 [RLIMIT_NOFILE] = {"Max open files", "files"},
428 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
429 [RLIMIT_AS] = {"Max address space", "bytes"},
430 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
431 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
432 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
433 [RLIMIT_NICE] = {"Max nice priority", NULL},
434 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
435 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
438 /* Display limits for a process */
439 static int proc_pid_limits(struct task_struct *task, char *buffer)
444 char *bufptr = buffer;
446 struct rlimit rlim[RLIM_NLIMITS];
449 if (!lock_task_sighand(task,&flags)) {
453 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454 unlock_task_sighand(task, &flags);
458 * print the file header
460 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
461 "Limit", "Soft Limit", "Hard Limit", "Units");
463 for (i = 0; i < RLIM_NLIMITS; i++) {
464 if (rlim[i].rlim_cur == RLIM_INFINITY)
465 count += sprintf(&bufptr[count], "%-25s %-20s ",
466 lnames[i].name, "unlimited");
468 count += sprintf(&bufptr[count], "%-25s %-20lu ",
469 lnames[i].name, rlim[i].rlim_cur);
471 if (rlim[i].rlim_max == RLIM_INFINITY)
472 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
474 count += sprintf(&bufptr[count], "%-20lu ",
478 count += sprintf(&bufptr[count], "%-10s\n",
481 count += sprintf(&bufptr[count], "\n");
487 /************************************************************************/
488 /* Here the fs part begins */
489 /************************************************************************/
491 /* permission checks */
492 static int proc_fd_access_allowed(struct inode *inode)
494 struct task_struct *task;
496 /* Allow access to a task's file descriptors if it is us or we
497 * may use ptrace attach to the process and find out that
500 task = get_proc_task(inode);
502 allowed = ptrace_may_attach(task);
503 put_task_struct(task);
508 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
511 struct inode *inode = dentry->d_inode;
513 if (attr->ia_valid & ATTR_MODE)
516 error = inode_change_ok(inode, attr);
518 error = inode_setattr(inode, attr);
522 static const struct inode_operations proc_def_inode_operations = {
523 .setattr = proc_setattr,
526 static int mounts_open_common(struct inode *inode, struct file *file,
527 const struct seq_operations *op)
529 struct task_struct *task = get_proc_task(inode);
531 struct mnt_namespace *ns = NULL;
532 struct fs_struct *fs = NULL;
534 struct proc_mounts *p;
539 nsp = task_nsproxy(task);
547 fs = get_fs_struct(task);
548 put_task_struct(task);
556 read_lock(&fs->lock);
559 read_unlock(&fs->lock);
563 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
567 file->private_data = &p->m;
568 ret = seq_open(file, op);
575 p->event = ns->event;
589 static int mounts_release(struct inode *inode, struct file *file)
591 struct proc_mounts *p = file->private_data;
594 return seq_release(inode, file);
597 static unsigned mounts_poll(struct file *file, poll_table *wait)
599 struct proc_mounts *p = file->private_data;
600 struct mnt_namespace *ns = p->ns;
603 poll_wait(file, &ns->poll, wait);
605 spin_lock(&vfsmount_lock);
606 if (p->event != ns->event) {
607 p->event = ns->event;
610 spin_unlock(&vfsmount_lock);
615 static int mounts_open(struct inode *inode, struct file *file)
617 return mounts_open_common(inode, file, &mounts_op);
620 static const struct file_operations proc_mounts_operations = {
624 .release = mounts_release,
628 static int mountinfo_open(struct inode *inode, struct file *file)
630 return mounts_open_common(inode, file, &mountinfo_op);
633 static const struct file_operations proc_mountinfo_operations = {
634 .open = mountinfo_open,
637 .release = mounts_release,
641 static int mountstats_open(struct inode *inode, struct file *file)
643 return mounts_open_common(inode, file, &mountstats_op);
646 static const struct file_operations proc_mountstats_operations = {
647 .open = mountstats_open,
650 .release = mounts_release,
653 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
655 static ssize_t proc_info_read(struct file * file, char __user * buf,
656 size_t count, loff_t *ppos)
658 struct inode * inode = file->f_path.dentry->d_inode;
661 struct task_struct *task = get_proc_task(inode);
667 if (count > PROC_BLOCK_SIZE)
668 count = PROC_BLOCK_SIZE;
671 if (!(page = __get_free_page(GFP_TEMPORARY)))
674 length = PROC_I(inode)->op.proc_read(task, (char*)page);
677 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
680 put_task_struct(task);
685 static const struct file_operations proc_info_file_operations = {
686 .read = proc_info_read,
689 static int proc_single_show(struct seq_file *m, void *v)
691 struct inode *inode = m->private;
692 struct pid_namespace *ns;
694 struct task_struct *task;
697 ns = inode->i_sb->s_fs_info;
698 pid = proc_pid(inode);
699 task = get_pid_task(pid, PIDTYPE_PID);
703 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
705 put_task_struct(task);
709 static int proc_single_open(struct inode *inode, struct file *filp)
712 ret = single_open(filp, proc_single_show, NULL);
714 struct seq_file *m = filp->private_data;
721 static const struct file_operations proc_single_file_operations = {
722 .open = proc_single_open,
725 .release = single_release,
728 static int mem_open(struct inode* inode, struct file* file)
730 file->private_data = (void*)((long)current->self_exec_id);
734 static ssize_t mem_read(struct file * file, char __user * buf,
735 size_t count, loff_t *ppos)
737 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
739 unsigned long src = *ppos;
741 struct mm_struct *mm;
746 if (check_mem_permission(task))
750 page = (char *)__get_free_page(GFP_TEMPORARY);
756 mm = get_task_mm(task);
762 if (file->private_data != (void*)((long)current->self_exec_id))
768 int this_len, retval;
770 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
771 retval = access_process_vm(task, src, page, this_len, 0);
772 if (!retval || check_mem_permission(task)) {
778 if (copy_to_user(buf, page, retval)) {
793 free_page((unsigned long) page);
795 put_task_struct(task);
800 #define mem_write NULL
803 /* This is a security hazard */
804 static ssize_t mem_write(struct file * file, const char __user *buf,
805 size_t count, loff_t *ppos)
809 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
810 unsigned long dst = *ppos;
816 if (check_mem_permission(task))
820 page = (char *)__get_free_page(GFP_TEMPORARY);
826 int this_len, retval;
828 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
829 if (copy_from_user(page, buf, this_len)) {
833 retval = access_process_vm(task, dst, page, this_len, 1);
845 free_page((unsigned long) page);
847 put_task_struct(task);
853 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
857 file->f_pos = offset;
860 file->f_pos += offset;
865 force_successful_syscall_return();
869 static const struct file_operations proc_mem_operations = {
876 static ssize_t environ_read(struct file *file, char __user *buf,
877 size_t count, loff_t *ppos)
879 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
881 unsigned long src = *ppos;
883 struct mm_struct *mm;
888 if (!ptrace_may_attach(task))
892 page = (char *)__get_free_page(GFP_TEMPORARY);
898 mm = get_task_mm(task);
903 int this_len, retval, max_len;
905 this_len = mm->env_end - (mm->env_start + src);
910 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
911 this_len = (this_len > max_len) ? max_len : this_len;
913 retval = access_process_vm(task, (mm->env_start + src),
921 if (copy_to_user(buf, page, retval)) {
935 free_page((unsigned long) page);
937 put_task_struct(task);
942 static const struct file_operations proc_environ_operations = {
943 .read = environ_read,
946 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
947 size_t count, loff_t *ppos)
949 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
950 char buffer[PROC_NUMBUF];
956 oom_adjust = task->oomkilladj;
957 put_task_struct(task);
959 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
961 return simple_read_from_buffer(buf, count, ppos, buffer, len);
964 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
965 size_t count, loff_t *ppos)
967 struct task_struct *task;
968 char buffer[PROC_NUMBUF], *end;
971 memset(buffer, 0, sizeof(buffer));
972 if (count > sizeof(buffer) - 1)
973 count = sizeof(buffer) - 1;
974 if (copy_from_user(buffer, buf, count))
976 oom_adjust = simple_strtol(buffer, &end, 0);
977 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
978 oom_adjust != OOM_DISABLE)
982 task = get_proc_task(file->f_path.dentry->d_inode);
985 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
986 put_task_struct(task);
989 task->oomkilladj = oom_adjust;
990 put_task_struct(task);
991 if (end - buffer == 0)
996 static const struct file_operations proc_oom_adjust_operations = {
997 .read = oom_adjust_read,
998 .write = oom_adjust_write,
1001 #ifdef CONFIG_AUDITSYSCALL
1002 #define TMPBUFLEN 21
1003 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1004 size_t count, loff_t *ppos)
1006 struct inode * inode = file->f_path.dentry->d_inode;
1007 struct task_struct *task = get_proc_task(inode);
1009 char tmpbuf[TMPBUFLEN];
1013 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1014 audit_get_loginuid(task));
1015 put_task_struct(task);
1016 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1019 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1020 size_t count, loff_t *ppos)
1022 struct inode * inode = file->f_path.dentry->d_inode;
1027 if (!capable(CAP_AUDIT_CONTROL))
1030 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1033 if (count >= PAGE_SIZE)
1034 count = PAGE_SIZE - 1;
1037 /* No partial writes. */
1040 page = (char*)__get_free_page(GFP_TEMPORARY);
1044 if (copy_from_user(page, buf, count))
1048 loginuid = simple_strtoul(page, &tmp, 10);
1054 length = audit_set_loginuid(current, loginuid);
1055 if (likely(length == 0))
1059 free_page((unsigned long) page);
1063 static const struct file_operations proc_loginuid_operations = {
1064 .read = proc_loginuid_read,
1065 .write = proc_loginuid_write,
1068 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1069 size_t count, loff_t *ppos)
1071 struct inode * inode = file->f_path.dentry->d_inode;
1072 struct task_struct *task = get_proc_task(inode);
1074 char tmpbuf[TMPBUFLEN];
1078 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1079 audit_get_sessionid(task));
1080 put_task_struct(task);
1081 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1084 static const struct file_operations proc_sessionid_operations = {
1085 .read = proc_sessionid_read,
1089 #ifdef CONFIG_FAULT_INJECTION
1090 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1091 size_t count, loff_t *ppos)
1093 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1094 char buffer[PROC_NUMBUF];
1100 make_it_fail = task->make_it_fail;
1101 put_task_struct(task);
1103 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1105 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1108 static ssize_t proc_fault_inject_write(struct file * file,
1109 const char __user * buf, size_t count, loff_t *ppos)
1111 struct task_struct *task;
1112 char buffer[PROC_NUMBUF], *end;
1115 if (!capable(CAP_SYS_RESOURCE))
1117 memset(buffer, 0, sizeof(buffer));
1118 if (count > sizeof(buffer) - 1)
1119 count = sizeof(buffer) - 1;
1120 if (copy_from_user(buffer, buf, count))
1122 make_it_fail = simple_strtol(buffer, &end, 0);
1125 task = get_proc_task(file->f_dentry->d_inode);
1128 task->make_it_fail = make_it_fail;
1129 put_task_struct(task);
1130 if (end - buffer == 0)
1132 return end - buffer;
1135 static const struct file_operations proc_fault_inject_operations = {
1136 .read = proc_fault_inject_read,
1137 .write = proc_fault_inject_write,
1142 #ifdef CONFIG_SCHED_DEBUG
1144 * Print out various scheduling related per-task fields:
1146 static int sched_show(struct seq_file *m, void *v)
1148 struct inode *inode = m->private;
1149 struct task_struct *p;
1153 p = get_proc_task(inode);
1156 proc_sched_show_task(p, m);
1164 sched_write(struct file *file, const char __user *buf,
1165 size_t count, loff_t *offset)
1167 struct inode *inode = file->f_path.dentry->d_inode;
1168 struct task_struct *p;
1172 p = get_proc_task(inode);
1175 proc_sched_set_task(p);
1182 static int sched_open(struct inode *inode, struct file *filp)
1186 ret = single_open(filp, sched_show, NULL);
1188 struct seq_file *m = filp->private_data;
1195 static const struct file_operations proc_pid_sched_operations = {
1198 .write = sched_write,
1199 .llseek = seq_lseek,
1200 .release = single_release,
1206 * We added or removed a vma mapping the executable. The vmas are only mapped
1207 * during exec and are not mapped with the mmap system call.
1208 * Callers must hold down_write() on the mm's mmap_sem for these
1210 void added_exe_file_vma(struct mm_struct *mm)
1212 mm->num_exe_file_vmas++;
1215 void removed_exe_file_vma(struct mm_struct *mm)
1217 mm->num_exe_file_vmas--;
1218 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1220 mm->exe_file = NULL;
1225 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1228 get_file(new_exe_file);
1231 mm->exe_file = new_exe_file;
1232 mm->num_exe_file_vmas = 0;
1235 struct file *get_mm_exe_file(struct mm_struct *mm)
1237 struct file *exe_file;
1239 /* We need mmap_sem to protect against races with removal of
1240 * VM_EXECUTABLE vmas */
1241 down_read(&mm->mmap_sem);
1242 exe_file = mm->exe_file;
1245 up_read(&mm->mmap_sem);
1249 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1251 /* It's safe to write the exe_file pointer without exe_file_lock because
1252 * this is called during fork when the task is not yet in /proc */
1253 newmm->exe_file = get_mm_exe_file(oldmm);
1256 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1258 struct task_struct *task;
1259 struct mm_struct *mm;
1260 struct file *exe_file;
1262 task = get_proc_task(inode);
1265 mm = get_task_mm(task);
1266 put_task_struct(task);
1269 exe_file = get_mm_exe_file(mm);
1272 *exe_path = exe_file->f_path;
1273 path_get(&exe_file->f_path);
1280 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1282 struct inode *inode = dentry->d_inode;
1283 int error = -EACCES;
1285 /* We don't need a base pointer in the /proc filesystem */
1286 path_put(&nd->path);
1288 /* Are we allowed to snoop on the tasks file descriptors? */
1289 if (!proc_fd_access_allowed(inode))
1292 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1293 nd->last_type = LAST_BIND;
1295 return ERR_PTR(error);
1298 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1300 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1307 pathname = d_path(path, tmp, PAGE_SIZE);
1308 len = PTR_ERR(pathname);
1309 if (IS_ERR(pathname))
1311 len = tmp + PAGE_SIZE - 1 - pathname;
1315 if (copy_to_user(buffer, pathname, len))
1318 free_page((unsigned long)tmp);
1322 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1324 int error = -EACCES;
1325 struct inode *inode = dentry->d_inode;
1328 /* Are we allowed to snoop on the tasks file descriptors? */
1329 if (!proc_fd_access_allowed(inode))
1332 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1336 error = do_proc_readlink(&path, buffer, buflen);
1342 static const struct inode_operations proc_pid_link_inode_operations = {
1343 .readlink = proc_pid_readlink,
1344 .follow_link = proc_pid_follow_link,
1345 .setattr = proc_setattr,
1349 /* building an inode */
1351 static int task_dumpable(struct task_struct *task)
1354 struct mm_struct *mm;
1359 dumpable = get_dumpable(mm);
1367 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1369 struct inode * inode;
1370 struct proc_inode *ei;
1372 /* We need a new inode */
1374 inode = new_inode(sb);
1380 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1381 inode->i_op = &proc_def_inode_operations;
1384 * grab the reference to task.
1386 ei->pid = get_task_pid(task, PIDTYPE_PID);
1392 if (task_dumpable(task)) {
1393 inode->i_uid = task->euid;
1394 inode->i_gid = task->egid;
1396 security_task_to_inode(task, inode);
1406 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1408 struct inode *inode = dentry->d_inode;
1409 struct task_struct *task;
1410 generic_fillattr(inode, stat);
1415 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1417 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1418 task_dumpable(task)) {
1419 stat->uid = task->euid;
1420 stat->gid = task->egid;
1430 * Exceptional case: normally we are not allowed to unhash a busy
1431 * directory. In this case, however, we can do it - no aliasing problems
1432 * due to the way we treat inodes.
1434 * Rewrite the inode's ownerships here because the owning task may have
1435 * performed a setuid(), etc.
1437 * Before the /proc/pid/status file was created the only way to read
1438 * the effective uid of a /process was to stat /proc/pid. Reading
1439 * /proc/pid/status is slow enough that procps and other packages
1440 * kept stating /proc/pid. To keep the rules in /proc simple I have
1441 * made this apply to all per process world readable and executable
1444 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1446 struct inode *inode = dentry->d_inode;
1447 struct task_struct *task = get_proc_task(inode);
1449 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1450 task_dumpable(task)) {
1451 inode->i_uid = task->euid;
1452 inode->i_gid = task->egid;
1457 inode->i_mode &= ~(S_ISUID | S_ISGID);
1458 security_task_to_inode(task, inode);
1459 put_task_struct(task);
1466 static int pid_delete_dentry(struct dentry * dentry)
1468 /* Is the task we represent dead?
1469 * If so, then don't put the dentry on the lru list,
1470 * kill it immediately.
1472 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1475 static struct dentry_operations pid_dentry_operations =
1477 .d_revalidate = pid_revalidate,
1478 .d_delete = pid_delete_dentry,
1483 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1484 struct task_struct *, const void *);
1487 * Fill a directory entry.
1489 * If possible create the dcache entry and derive our inode number and
1490 * file type from dcache entry.
1492 * Since all of the proc inode numbers are dynamically generated, the inode
1493 * numbers do not exist until the inode is cache. This means creating the
1494 * the dcache entry in readdir is necessary to keep the inode numbers
1495 * reported by readdir in sync with the inode numbers reported
1498 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1499 char *name, int len,
1500 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1502 struct dentry *child, *dir = filp->f_path.dentry;
1503 struct inode *inode;
1506 unsigned type = DT_UNKNOWN;
1510 qname.hash = full_name_hash(name, len);
1512 child = d_lookup(dir, &qname);
1515 new = d_alloc(dir, &qname);
1517 child = instantiate(dir->d_inode, new, task, ptr);
1524 if (!child || IS_ERR(child) || !child->d_inode)
1525 goto end_instantiate;
1526 inode = child->d_inode;
1529 type = inode->i_mode >> 12;
1534 ino = find_inode_number(dir, &qname);
1537 return filldir(dirent, name, len, filp->f_pos, ino, type);
1540 static unsigned name_to_int(struct dentry *dentry)
1542 const char *name = dentry->d_name.name;
1543 int len = dentry->d_name.len;
1546 if (len > 1 && *name == '0')
1549 unsigned c = *name++ - '0';
1552 if (n >= (~0U-9)/10)
1562 #define PROC_FDINFO_MAX 64
1564 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1566 struct task_struct *task = get_proc_task(inode);
1567 struct files_struct *files = NULL;
1569 int fd = proc_fd(inode);
1572 files = get_files_struct(task);
1573 put_task_struct(task);
1577 * We are not taking a ref to the file structure, so we must
1580 spin_lock(&files->file_lock);
1581 file = fcheck_files(files, fd);
1584 *path = file->f_path;
1585 path_get(&file->f_path);
1588 snprintf(info, PROC_FDINFO_MAX,
1591 (long long) file->f_pos,
1593 spin_unlock(&files->file_lock);
1594 put_files_struct(files);
1597 spin_unlock(&files->file_lock);
1598 put_files_struct(files);
1603 static int proc_fd_link(struct inode *inode, struct path *path)
1605 return proc_fd_info(inode, path, NULL);
1608 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1610 struct inode *inode = dentry->d_inode;
1611 struct task_struct *task = get_proc_task(inode);
1612 int fd = proc_fd(inode);
1613 struct files_struct *files;
1616 files = get_files_struct(task);
1619 if (fcheck_files(files, fd)) {
1621 put_files_struct(files);
1622 if (task_dumpable(task)) {
1623 inode->i_uid = task->euid;
1624 inode->i_gid = task->egid;
1629 inode->i_mode &= ~(S_ISUID | S_ISGID);
1630 security_task_to_inode(task, inode);
1631 put_task_struct(task);
1635 put_files_struct(files);
1637 put_task_struct(task);
1643 static struct dentry_operations tid_fd_dentry_operations =
1645 .d_revalidate = tid_fd_revalidate,
1646 .d_delete = pid_delete_dentry,
1649 static struct dentry *proc_fd_instantiate(struct inode *dir,
1650 struct dentry *dentry, struct task_struct *task, const void *ptr)
1652 unsigned fd = *(const unsigned *)ptr;
1654 struct files_struct *files;
1655 struct inode *inode;
1656 struct proc_inode *ei;
1657 struct dentry *error = ERR_PTR(-ENOENT);
1659 inode = proc_pid_make_inode(dir->i_sb, task);
1664 files = get_files_struct(task);
1667 inode->i_mode = S_IFLNK;
1670 * We are not taking a ref to the file structure, so we must
1673 spin_lock(&files->file_lock);
1674 file = fcheck_files(files, fd);
1677 if (file->f_mode & 1)
1678 inode->i_mode |= S_IRUSR | S_IXUSR;
1679 if (file->f_mode & 2)
1680 inode->i_mode |= S_IWUSR | S_IXUSR;
1681 spin_unlock(&files->file_lock);
1682 put_files_struct(files);
1684 inode->i_op = &proc_pid_link_inode_operations;
1686 ei->op.proc_get_link = proc_fd_link;
1687 dentry->d_op = &tid_fd_dentry_operations;
1688 d_add(dentry, inode);
1689 /* Close the race of the process dying before we return the dentry */
1690 if (tid_fd_revalidate(dentry, NULL))
1696 spin_unlock(&files->file_lock);
1697 put_files_struct(files);
1703 static struct dentry *proc_lookupfd_common(struct inode *dir,
1704 struct dentry *dentry,
1705 instantiate_t instantiate)
1707 struct task_struct *task = get_proc_task(dir);
1708 unsigned fd = name_to_int(dentry);
1709 struct dentry *result = ERR_PTR(-ENOENT);
1716 result = instantiate(dir, dentry, task, &fd);
1718 put_task_struct(task);
1723 static int proc_readfd_common(struct file * filp, void * dirent,
1724 filldir_t filldir, instantiate_t instantiate)
1726 struct dentry *dentry = filp->f_path.dentry;
1727 struct inode *inode = dentry->d_inode;
1728 struct task_struct *p = get_proc_task(inode);
1729 unsigned int fd, ino;
1731 struct files_struct * files;
1741 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1745 ino = parent_ino(dentry);
1746 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1750 files = get_files_struct(p);
1754 for (fd = filp->f_pos-2;
1755 fd < files_fdtable(files)->max_fds;
1756 fd++, filp->f_pos++) {
1757 char name[PROC_NUMBUF];
1760 if (!fcheck_files(files, fd))
1764 len = snprintf(name, sizeof(name), "%d", fd);
1765 if (proc_fill_cache(filp, dirent, filldir,
1766 name, len, instantiate,
1774 put_files_struct(files);
1782 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1783 struct nameidata *nd)
1785 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1788 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1790 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1793 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1794 size_t len, loff_t *ppos)
1796 char tmp[PROC_FDINFO_MAX];
1797 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1799 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1803 static const struct file_operations proc_fdinfo_file_operations = {
1804 .open = nonseekable_open,
1805 .read = proc_fdinfo_read,
1808 static const struct file_operations proc_fd_operations = {
1809 .read = generic_read_dir,
1810 .readdir = proc_readfd,
1814 * /proc/pid/fd needs a special permission handler so that a process can still
1815 * access /proc/self/fd after it has executed a setuid().
1817 static int proc_fd_permission(struct inode *inode, int mask,
1818 struct nameidata *nd)
1822 rv = generic_permission(inode, mask, NULL);
1825 if (task_pid(current) == proc_pid(inode))
1831 * proc directories can do almost nothing..
1833 static const struct inode_operations proc_fd_inode_operations = {
1834 .lookup = proc_lookupfd,
1835 .permission = proc_fd_permission,
1836 .setattr = proc_setattr,
1839 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1840 struct dentry *dentry, struct task_struct *task, const void *ptr)
1842 unsigned fd = *(unsigned *)ptr;
1843 struct inode *inode;
1844 struct proc_inode *ei;
1845 struct dentry *error = ERR_PTR(-ENOENT);
1847 inode = proc_pid_make_inode(dir->i_sb, task);
1852 inode->i_mode = S_IFREG | S_IRUSR;
1853 inode->i_fop = &proc_fdinfo_file_operations;
1854 dentry->d_op = &tid_fd_dentry_operations;
1855 d_add(dentry, inode);
1856 /* Close the race of the process dying before we return the dentry */
1857 if (tid_fd_revalidate(dentry, NULL))
1864 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1865 struct dentry *dentry,
1866 struct nameidata *nd)
1868 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1871 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1873 return proc_readfd_common(filp, dirent, filldir,
1874 proc_fdinfo_instantiate);
1877 static const struct file_operations proc_fdinfo_operations = {
1878 .read = generic_read_dir,
1879 .readdir = proc_readfdinfo,
1883 * proc directories can do almost nothing..
1885 static const struct inode_operations proc_fdinfo_inode_operations = {
1886 .lookup = proc_lookupfdinfo,
1887 .setattr = proc_setattr,
1891 static struct dentry *proc_pident_instantiate(struct inode *dir,
1892 struct dentry *dentry, struct task_struct *task, const void *ptr)
1894 const struct pid_entry *p = ptr;
1895 struct inode *inode;
1896 struct proc_inode *ei;
1897 struct dentry *error = ERR_PTR(-EINVAL);
1899 inode = proc_pid_make_inode(dir->i_sb, task);
1904 inode->i_mode = p->mode;
1905 if (S_ISDIR(inode->i_mode))
1906 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1908 inode->i_op = p->iop;
1910 inode->i_fop = p->fop;
1912 dentry->d_op = &pid_dentry_operations;
1913 d_add(dentry, inode);
1914 /* Close the race of the process dying before we return the dentry */
1915 if (pid_revalidate(dentry, NULL))
1921 static struct dentry *proc_pident_lookup(struct inode *dir,
1922 struct dentry *dentry,
1923 const struct pid_entry *ents,
1926 struct inode *inode;
1927 struct dentry *error;
1928 struct task_struct *task = get_proc_task(dir);
1929 const struct pid_entry *p, *last;
1931 error = ERR_PTR(-ENOENT);
1938 * Yes, it does not scale. And it should not. Don't add
1939 * new entries into /proc/<tgid>/ without very good reasons.
1941 last = &ents[nents - 1];
1942 for (p = ents; p <= last; p++) {
1943 if (p->len != dentry->d_name.len)
1945 if (!memcmp(dentry->d_name.name, p->name, p->len))
1951 error = proc_pident_instantiate(dir, dentry, task, p);
1953 put_task_struct(task);
1958 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1959 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1961 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1962 proc_pident_instantiate, task, p);
1965 static int proc_pident_readdir(struct file *filp,
1966 void *dirent, filldir_t filldir,
1967 const struct pid_entry *ents, unsigned int nents)
1970 struct dentry *dentry = filp->f_path.dentry;
1971 struct inode *inode = dentry->d_inode;
1972 struct task_struct *task = get_proc_task(inode);
1973 const struct pid_entry *p, *last;
1986 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1992 ino = parent_ino(dentry);
1993 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2005 last = &ents[nents - 1];
2007 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2016 put_task_struct(task);
2021 #ifdef CONFIG_SECURITY
2022 static ssize_t proc_pid_attr_read(struct file * file, 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 length = security_getprocattr(task,
2034 (char*)file->f_path.dentry->d_name.name,
2036 put_task_struct(task);
2038 length = simple_read_from_buffer(buf, count, ppos, p, length);
2043 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2044 size_t count, loff_t *ppos)
2046 struct inode * inode = file->f_path.dentry->d_inode;
2049 struct task_struct *task = get_proc_task(inode);
2054 if (count > PAGE_SIZE)
2057 /* No partial writes. */
2063 page = (char*)__get_free_page(GFP_TEMPORARY);
2068 if (copy_from_user(page, buf, count))
2071 length = security_setprocattr(task,
2072 (char*)file->f_path.dentry->d_name.name,
2073 (void*)page, count);
2075 free_page((unsigned long) page);
2077 put_task_struct(task);
2082 static const struct file_operations proc_pid_attr_operations = {
2083 .read = proc_pid_attr_read,
2084 .write = proc_pid_attr_write,
2087 static const struct pid_entry attr_dir_stuff[] = {
2088 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2089 REG("prev", S_IRUGO, pid_attr),
2090 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2091 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2092 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2093 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
2096 static int proc_attr_dir_readdir(struct file * filp,
2097 void * dirent, filldir_t filldir)
2099 return proc_pident_readdir(filp,dirent,filldir,
2100 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2103 static const struct file_operations proc_attr_dir_operations = {
2104 .read = generic_read_dir,
2105 .readdir = proc_attr_dir_readdir,
2108 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2109 struct dentry *dentry, struct nameidata *nd)
2111 return proc_pident_lookup(dir, dentry,
2112 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2115 static const struct inode_operations proc_attr_dir_inode_operations = {
2116 .lookup = proc_attr_dir_lookup,
2117 .getattr = pid_getattr,
2118 .setattr = proc_setattr,
2123 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2124 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2125 size_t count, loff_t *ppos)
2127 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2128 struct mm_struct *mm;
2129 char buffer[PROC_NUMBUF];
2137 mm = get_task_mm(task);
2139 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2140 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2141 MMF_DUMP_FILTER_SHIFT));
2143 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2146 put_task_struct(task);
2151 static ssize_t proc_coredump_filter_write(struct file *file,
2152 const char __user *buf,
2156 struct task_struct *task;
2157 struct mm_struct *mm;
2158 char buffer[PROC_NUMBUF], *end;
2165 memset(buffer, 0, sizeof(buffer));
2166 if (count > sizeof(buffer) - 1)
2167 count = sizeof(buffer) - 1;
2168 if (copy_from_user(buffer, buf, count))
2172 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2175 if (end - buffer == 0)
2179 task = get_proc_task(file->f_dentry->d_inode);
2184 mm = get_task_mm(task);
2188 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2190 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2192 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2197 put_task_struct(task);
2202 static const struct file_operations proc_coredump_filter_operations = {
2203 .read = proc_coredump_filter_read,
2204 .write = proc_coredump_filter_write,
2211 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2214 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2215 pid_t tgid = task_tgid_nr_ns(current, ns);
2216 char tmp[PROC_NUMBUF];
2219 sprintf(tmp, "%d", tgid);
2220 return vfs_readlink(dentry,buffer,buflen,tmp);
2223 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2225 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2226 pid_t tgid = task_tgid_nr_ns(current, ns);
2227 char tmp[PROC_NUMBUF];
2229 return ERR_PTR(-ENOENT);
2230 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2231 return ERR_PTR(vfs_follow_link(nd,tmp));
2234 static const struct inode_operations proc_self_inode_operations = {
2235 .readlink = proc_self_readlink,
2236 .follow_link = proc_self_follow_link,
2242 * These are the directory entries in the root directory of /proc
2243 * that properly belong to the /proc filesystem, as they describe
2244 * describe something that is process related.
2246 static const struct pid_entry proc_base_stuff[] = {
2247 NOD("self", S_IFLNK|S_IRWXUGO,
2248 &proc_self_inode_operations, NULL, {}),
2252 * Exceptional case: normally we are not allowed to unhash a busy
2253 * directory. In this case, however, we can do it - no aliasing problems
2254 * due to the way we treat inodes.
2256 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2258 struct inode *inode = dentry->d_inode;
2259 struct task_struct *task = get_proc_task(inode);
2261 put_task_struct(task);
2268 static struct dentry_operations proc_base_dentry_operations =
2270 .d_revalidate = proc_base_revalidate,
2271 .d_delete = pid_delete_dentry,
2274 static struct dentry *proc_base_instantiate(struct inode *dir,
2275 struct dentry *dentry, struct task_struct *task, const void *ptr)
2277 const struct pid_entry *p = ptr;
2278 struct inode *inode;
2279 struct proc_inode *ei;
2280 struct dentry *error = ERR_PTR(-EINVAL);
2282 /* Allocate the inode */
2283 error = ERR_PTR(-ENOMEM);
2284 inode = new_inode(dir->i_sb);
2288 /* Initialize the inode */
2290 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2293 * grab the reference to the task.
2295 ei->pid = get_task_pid(task, PIDTYPE_PID);
2301 inode->i_mode = p->mode;
2302 if (S_ISDIR(inode->i_mode))
2304 if (S_ISLNK(inode->i_mode))
2307 inode->i_op = p->iop;
2309 inode->i_fop = p->fop;
2311 dentry->d_op = &proc_base_dentry_operations;
2312 d_add(dentry, inode);
2321 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2323 struct dentry *error;
2324 struct task_struct *task = get_proc_task(dir);
2325 const struct pid_entry *p, *last;
2327 error = ERR_PTR(-ENOENT);
2332 /* Lookup the directory entry */
2333 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2334 for (p = proc_base_stuff; p <= last; p++) {
2335 if (p->len != dentry->d_name.len)
2337 if (!memcmp(dentry->d_name.name, p->name, p->len))
2343 error = proc_base_instantiate(dir, dentry, task, p);
2346 put_task_struct(task);
2351 static int proc_base_fill_cache(struct file *filp, void *dirent,
2352 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2354 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2355 proc_base_instantiate, task, p);
2358 #ifdef CONFIG_TASK_IO_ACCOUNTING
2359 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2361 return sprintf(buffer,
2362 #ifdef CONFIG_TASK_XACCT
2368 "read_bytes: %llu\n"
2369 "write_bytes: %llu\n"
2370 "cancelled_write_bytes: %llu\n",
2371 #ifdef CONFIG_TASK_XACCT
2372 (unsigned long long)task->rchar,
2373 (unsigned long long)task->wchar,
2374 (unsigned long long)task->syscr,
2375 (unsigned long long)task->syscw,
2377 (unsigned long long)task->ioac.read_bytes,
2378 (unsigned long long)task->ioac.write_bytes,
2379 (unsigned long long)task->ioac.cancelled_write_bytes);
2386 static const struct file_operations proc_task_operations;
2387 static const struct inode_operations proc_task_inode_operations;
2389 static const struct pid_entry tgid_base_stuff[] = {
2390 DIR("task", S_IRUGO|S_IXUGO, task),
2391 DIR("fd", S_IRUSR|S_IXUSR, fd),
2392 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2394 DIR("net", S_IRUGO|S_IXUGO, net),
2396 REG("environ", S_IRUSR, environ),
2397 INF("auxv", S_IRUSR, pid_auxv),
2398 ONE("status", S_IRUGO, pid_status),
2399 INF("limits", S_IRUSR, pid_limits),
2400 #ifdef CONFIG_SCHED_DEBUG
2401 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2403 INF("cmdline", S_IRUGO, pid_cmdline),
2404 ONE("stat", S_IRUGO, tgid_stat),
2405 ONE("statm", S_IRUGO, pid_statm),
2406 REG("maps", S_IRUGO, maps),
2408 REG("numa_maps", S_IRUGO, numa_maps),
2410 REG("mem", S_IRUSR|S_IWUSR, mem),
2414 REG("mounts", S_IRUGO, mounts),
2415 REG("mountinfo", S_IRUGO, mountinfo),
2416 REG("mountstats", S_IRUSR, mountstats),
2417 #ifdef CONFIG_PROC_PAGE_MONITOR
2418 REG("clear_refs", S_IWUSR, clear_refs),
2419 REG("smaps", S_IRUGO, smaps),
2420 REG("pagemap", S_IRUSR, pagemap),
2422 #ifdef CONFIG_SECURITY
2423 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2425 #ifdef CONFIG_KALLSYMS
2426 INF("wchan", S_IRUGO, pid_wchan),
2428 #ifdef CONFIG_SCHEDSTATS
2429 INF("schedstat", S_IRUGO, pid_schedstat),
2431 #ifdef CONFIG_LATENCYTOP
2432 REG("latency", S_IRUGO, lstats),
2434 #ifdef CONFIG_PROC_PID_CPUSET
2435 REG("cpuset", S_IRUGO, cpuset),
2437 #ifdef CONFIG_CGROUPS
2438 REG("cgroup", S_IRUGO, cgroup),
2440 INF("oom_score", S_IRUGO, oom_score),
2441 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2442 #ifdef CONFIG_AUDITSYSCALL
2443 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2444 REG("sessionid", S_IRUGO, sessionid),
2446 #ifdef CONFIG_FAULT_INJECTION
2447 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2449 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2450 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2452 #ifdef CONFIG_TASK_IO_ACCOUNTING
2453 INF("io", S_IRUGO, pid_io_accounting),
2457 static int proc_tgid_base_readdir(struct file * filp,
2458 void * dirent, filldir_t filldir)
2460 return proc_pident_readdir(filp,dirent,filldir,
2461 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2464 static const struct file_operations proc_tgid_base_operations = {
2465 .read = generic_read_dir,
2466 .readdir = proc_tgid_base_readdir,
2469 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2470 return proc_pident_lookup(dir, dentry,
2471 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2474 static const struct inode_operations proc_tgid_base_inode_operations = {
2475 .lookup = proc_tgid_base_lookup,
2476 .getattr = pid_getattr,
2477 .setattr = proc_setattr,
2480 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2482 struct dentry *dentry, *leader, *dir;
2483 char buf[PROC_NUMBUF];
2487 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2488 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2490 if (!(current->flags & PF_EXITING))
2491 shrink_dcache_parent(dentry);
2500 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2501 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2506 name.len = strlen(name.name);
2507 dir = d_hash_and_lookup(leader, &name);
2509 goto out_put_leader;
2512 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2513 dentry = d_hash_and_lookup(dir, &name);
2515 shrink_dcache_parent(dentry);
2528 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2529 * @task: task that should be flushed.
2531 * When flushing dentries from proc, one needs to flush them from global
2532 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2533 * in. This call is supposed to do all of this job.
2535 * Looks in the dcache for
2537 * /proc/@tgid/task/@pid
2538 * if either directory is present flushes it and all of it'ts children
2541 * It is safe and reasonable to cache /proc entries for a task until
2542 * that task exits. After that they just clog up the dcache with
2543 * useless entries, possibly causing useful dcache entries to be
2544 * flushed instead. This routine is proved to flush those useless
2545 * dcache entries at process exit time.
2547 * NOTE: This routine is just an optimization so it does not guarantee
2548 * that no dcache entries will exist at process exit time it
2549 * just makes it very unlikely that any will persist.
2552 void proc_flush_task(struct task_struct *task)
2555 struct pid *pid, *tgid = NULL;
2558 pid = task_pid(task);
2559 if (thread_group_leader(task))
2560 tgid = task_tgid(task);
2562 for (i = 0; i <= pid->level; i++) {
2563 upid = &pid->numbers[i];
2564 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2565 tgid ? tgid->numbers[i].nr : 0);
2568 upid = &pid->numbers[pid->level];
2570 pid_ns_release_proc(upid->ns);
2573 static struct dentry *proc_pid_instantiate(struct inode *dir,
2574 struct dentry * dentry,
2575 struct task_struct *task, const void *ptr)
2577 struct dentry *error = ERR_PTR(-ENOENT);
2578 struct inode *inode;
2580 inode = proc_pid_make_inode(dir->i_sb, task);
2584 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2585 inode->i_op = &proc_tgid_base_inode_operations;
2586 inode->i_fop = &proc_tgid_base_operations;
2587 inode->i_flags|=S_IMMUTABLE;
2589 #ifdef CONFIG_SECURITY
2590 inode->i_nlink += 1;
2593 dentry->d_op = &pid_dentry_operations;
2595 d_add(dentry, inode);
2596 /* Close the race of the process dying before we return the dentry */
2597 if (pid_revalidate(dentry, NULL))
2603 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2605 struct dentry *result = ERR_PTR(-ENOENT);
2606 struct task_struct *task;
2608 struct pid_namespace *ns;
2610 result = proc_base_lookup(dir, dentry);
2611 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2614 tgid = name_to_int(dentry);
2618 ns = dentry->d_sb->s_fs_info;
2620 task = find_task_by_pid_ns(tgid, ns);
2622 get_task_struct(task);
2627 result = proc_pid_instantiate(dir, dentry, task, NULL);
2628 put_task_struct(task);
2634 * Find the first task with tgid >= tgid
2639 struct task_struct *task;
2641 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2646 put_task_struct(iter.task);
2650 pid = find_ge_pid(iter.tgid, ns);
2652 iter.tgid = pid_nr_ns(pid, ns);
2653 iter.task = pid_task(pid, PIDTYPE_PID);
2654 /* What we to know is if the pid we have find is the
2655 * pid of a thread_group_leader. Testing for task
2656 * being a thread_group_leader is the obvious thing
2657 * todo but there is a window when it fails, due to
2658 * the pid transfer logic in de_thread.
2660 * So we perform the straight forward test of seeing
2661 * if the pid we have found is the pid of a thread
2662 * group leader, and don't worry if the task we have
2663 * found doesn't happen to be a thread group leader.
2664 * As we don't care in the case of readdir.
2666 if (!iter.task || !has_group_leader_pid(iter.task)) {
2670 get_task_struct(iter.task);
2676 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2678 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2679 struct tgid_iter iter)
2681 char name[PROC_NUMBUF];
2682 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2683 return proc_fill_cache(filp, dirent, filldir, name, len,
2684 proc_pid_instantiate, iter.task, NULL);
2687 /* for the /proc/ directory itself, after non-process stuff has been done */
2688 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2690 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2691 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2692 struct tgid_iter iter;
2693 struct pid_namespace *ns;
2698 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2699 const struct pid_entry *p = &proc_base_stuff[nr];
2700 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2704 ns = filp->f_dentry->d_sb->s_fs_info;
2706 iter.tgid = filp->f_pos - TGID_OFFSET;
2707 for (iter = next_tgid(ns, iter);
2709 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2710 filp->f_pos = iter.tgid + TGID_OFFSET;
2711 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2712 put_task_struct(iter.task);
2716 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2718 put_task_struct(reaper);
2726 static const struct pid_entry tid_base_stuff[] = {
2727 DIR("fd", S_IRUSR|S_IXUSR, fd),
2728 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2729 REG("environ", S_IRUSR, environ),
2730 INF("auxv", S_IRUSR, pid_auxv),
2731 ONE("status", S_IRUGO, pid_status),
2732 INF("limits", S_IRUSR, pid_limits),
2733 #ifdef CONFIG_SCHED_DEBUG
2734 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2736 INF("cmdline", S_IRUGO, pid_cmdline),
2737 ONE("stat", S_IRUGO, tid_stat),
2738 ONE("statm", S_IRUGO, pid_statm),
2739 REG("maps", S_IRUGO, maps),
2741 REG("numa_maps", S_IRUGO, numa_maps),
2743 REG("mem", S_IRUSR|S_IWUSR, mem),
2747 REG("mounts", S_IRUGO, mounts),
2748 REG("mountinfo", S_IRUGO, mountinfo),
2749 #ifdef CONFIG_PROC_PAGE_MONITOR
2750 REG("clear_refs", S_IWUSR, clear_refs),
2751 REG("smaps", S_IRUGO, smaps),
2752 REG("pagemap", S_IRUSR, pagemap),
2754 #ifdef CONFIG_SECURITY
2755 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2757 #ifdef CONFIG_KALLSYMS
2758 INF("wchan", S_IRUGO, pid_wchan),
2760 #ifdef CONFIG_SCHEDSTATS
2761 INF("schedstat", S_IRUGO, pid_schedstat),
2763 #ifdef CONFIG_LATENCYTOP
2764 REG("latency", S_IRUGO, lstats),
2766 #ifdef CONFIG_PROC_PID_CPUSET
2767 REG("cpuset", S_IRUGO, cpuset),
2769 #ifdef CONFIG_CGROUPS
2770 REG("cgroup", S_IRUGO, cgroup),
2772 INF("oom_score", S_IRUGO, oom_score),
2773 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2774 #ifdef CONFIG_AUDITSYSCALL
2775 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2776 REG("sessionid", S_IRUSR, sessionid),
2778 #ifdef CONFIG_FAULT_INJECTION
2779 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2783 static int proc_tid_base_readdir(struct file * filp,
2784 void * dirent, filldir_t filldir)
2786 return proc_pident_readdir(filp,dirent,filldir,
2787 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2790 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2791 return proc_pident_lookup(dir, dentry,
2792 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2795 static const struct file_operations proc_tid_base_operations = {
2796 .read = generic_read_dir,
2797 .readdir = proc_tid_base_readdir,
2800 static const struct inode_operations proc_tid_base_inode_operations = {
2801 .lookup = proc_tid_base_lookup,
2802 .getattr = pid_getattr,
2803 .setattr = proc_setattr,
2806 static struct dentry *proc_task_instantiate(struct inode *dir,
2807 struct dentry *dentry, struct task_struct *task, const void *ptr)
2809 struct dentry *error = ERR_PTR(-ENOENT);
2810 struct inode *inode;
2811 inode = proc_pid_make_inode(dir->i_sb, task);
2815 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2816 inode->i_op = &proc_tid_base_inode_operations;
2817 inode->i_fop = &proc_tid_base_operations;
2818 inode->i_flags|=S_IMMUTABLE;
2820 #ifdef CONFIG_SECURITY
2821 inode->i_nlink += 1;
2824 dentry->d_op = &pid_dentry_operations;
2826 d_add(dentry, inode);
2827 /* Close the race of the process dying before we return the dentry */
2828 if (pid_revalidate(dentry, NULL))
2834 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2836 struct dentry *result = ERR_PTR(-ENOENT);
2837 struct task_struct *task;
2838 struct task_struct *leader = get_proc_task(dir);
2840 struct pid_namespace *ns;
2845 tid = name_to_int(dentry);
2849 ns = dentry->d_sb->s_fs_info;
2851 task = find_task_by_pid_ns(tid, ns);
2853 get_task_struct(task);
2857 if (!same_thread_group(leader, task))
2860 result = proc_task_instantiate(dir, dentry, task, NULL);
2862 put_task_struct(task);
2864 put_task_struct(leader);
2870 * Find the first tid of a thread group to return to user space.
2872 * Usually this is just the thread group leader, but if the users
2873 * buffer was too small or there was a seek into the middle of the
2874 * directory we have more work todo.
2876 * In the case of a short read we start with find_task_by_pid.
2878 * In the case of a seek we start with the leader and walk nr
2881 static struct task_struct *first_tid(struct task_struct *leader,
2882 int tid, int nr, struct pid_namespace *ns)
2884 struct task_struct *pos;
2887 /* Attempt to start with the pid of a thread */
2888 if (tid && (nr > 0)) {
2889 pos = find_task_by_pid_ns(tid, ns);
2890 if (pos && (pos->group_leader == leader))
2894 /* If nr exceeds the number of threads there is nothing todo */
2896 if (nr && nr >= get_nr_threads(leader))
2899 /* If we haven't found our starting place yet start
2900 * with the leader and walk nr threads forward.
2902 for (pos = leader; nr > 0; --nr) {
2903 pos = next_thread(pos);
2904 if (pos == leader) {
2910 get_task_struct(pos);
2917 * Find the next thread in the thread list.
2918 * Return NULL if there is an error or no next thread.
2920 * The reference to the input task_struct is released.
2922 static struct task_struct *next_tid(struct task_struct *start)
2924 struct task_struct *pos = NULL;
2926 if (pid_alive(start)) {
2927 pos = next_thread(start);
2928 if (thread_group_leader(pos))
2931 get_task_struct(pos);
2934 put_task_struct(start);
2938 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2939 struct task_struct *task, int tid)
2941 char name[PROC_NUMBUF];
2942 int len = snprintf(name, sizeof(name), "%d", tid);
2943 return proc_fill_cache(filp, dirent, filldir, name, len,
2944 proc_task_instantiate, task, NULL);
2947 /* for the /proc/TGID/task/ directories */
2948 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2950 struct dentry *dentry = filp->f_path.dentry;
2951 struct inode *inode = dentry->d_inode;
2952 struct task_struct *leader = NULL;
2953 struct task_struct *task;
2954 int retval = -ENOENT;
2957 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2958 struct pid_namespace *ns;
2960 task = get_proc_task(inode);
2964 if (pid_alive(task)) {
2965 leader = task->group_leader;
2966 get_task_struct(leader);
2969 put_task_struct(task);
2977 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2982 ino = parent_ino(dentry);
2983 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2989 /* f_version caches the tgid value that the last readdir call couldn't
2990 * return. lseek aka telldir automagically resets f_version to 0.
2992 ns = filp->f_dentry->d_sb->s_fs_info;
2993 tid = (int)filp->f_version;
2994 filp->f_version = 0;
2995 for (task = first_tid(leader, tid, pos - 2, ns);
2997 task = next_tid(task), pos++) {
2998 tid = task_pid_nr_ns(task, ns);
2999 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3000 /* returning this tgid failed, save it as the first
3001 * pid for the next readir call */
3002 filp->f_version = (u64)tid;
3003 put_task_struct(task);
3009 put_task_struct(leader);
3014 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3016 struct inode *inode = dentry->d_inode;
3017 struct task_struct *p = get_proc_task(inode);
3018 generic_fillattr(inode, stat);
3022 stat->nlink += get_nr_threads(p);
3030 static const struct inode_operations proc_task_inode_operations = {
3031 .lookup = proc_task_lookup,
3032 .getattr = proc_task_getattr,
3033 .setattr = proc_setattr,
3036 static const struct file_operations proc_task_operations = {
3037 .read = generic_read_dir,
3038 .readdir = proc_task_readdir,