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/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
89 * Implementing inode permission operations in /proc is almost
90 * certainly an error. Permission checks need to happen during
91 * each system call not at open time. The reason is that most of
92 * what we wish to check for permissions in /proc varies at runtime.
94 * The classic example of a problem is opening file descriptors
95 * in /proc for a task before it execs a suid executable.
102 const struct inode_operations *iop;
103 const struct file_operations *fop;
107 #define NOD(NAME, MODE, IOP, FOP, OP) { \
109 .len = sizeof(NAME) - 1, \
116 #define DIR(NAME, MODE, iops, fops) \
117 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118 #define LNK(NAME, get_link) \
119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
120 &proc_pid_link_inode_operations, NULL, \
121 { .proc_get_link = get_link } )
122 #define REG(NAME, MODE, fops) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124 #define INF(NAME, MODE, read) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_info_file_operations, \
127 { .proc_read = read } )
128 #define ONE(NAME, MODE, show) \
129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_single_file_operations, \
131 { .proc_show = show } )
134 * Count the number of hardlinks for the pid_entry table, excluding the .
137 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
144 for (i = 0; i < n; ++i) {
145 if (S_ISDIR(entries[i].mode))
152 static int get_task_root(struct task_struct *task, struct path *root)
154 int result = -ENOENT;
158 get_fs_root(task->fs, root);
165 static int proc_cwd_link(struct inode *inode, struct path *path)
167 struct task_struct *task = get_proc_task(inode);
168 int result = -ENOENT;
173 get_fs_pwd(task->fs, path);
177 put_task_struct(task);
182 static int proc_root_link(struct inode *inode, struct path *path)
184 struct task_struct *task = get_proc_task(inode);
185 int result = -ENOENT;
188 result = get_task_root(task, path);
189 put_task_struct(task);
195 * Return zero if current may access user memory in @task, -error if not.
197 static int check_mem_permission(struct task_struct *task)
200 * A task can always look at itself, in case it chooses
201 * to use system calls instead of load instructions.
207 * If current is actively ptrace'ing, and would also be
208 * permitted to freshly attach with ptrace now, permit it.
210 if (task_is_stopped_or_traced(task)) {
213 match = (tracehook_tracer_task(task) == current);
215 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
220 * Noone else is allowed.
225 struct mm_struct *mm_for_maps(struct task_struct *task)
227 struct mm_struct *mm;
229 if (mutex_lock_killable(&task->signal->cred_guard_mutex))
232 mm = get_task_mm(task);
233 if (mm && mm != current->mm &&
234 !ptrace_may_access(task, PTRACE_MODE_READ)) {
238 mutex_unlock(&task->signal->cred_guard_mutex);
243 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
247 struct mm_struct *mm = get_task_mm(task);
251 goto out_mm; /* Shh! No looking before we're done */
253 len = mm->arg_end - mm->arg_start;
258 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
260 // If the nul at the end of args has been overwritten, then
261 // assume application is using setproctitle(3).
262 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
263 len = strnlen(buffer, res);
267 len = mm->env_end - mm->env_start;
268 if (len > PAGE_SIZE - res)
269 len = PAGE_SIZE - res;
270 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
271 res = strnlen(buffer, res);
280 static int proc_pid_auxv(struct task_struct *task, char *buffer)
283 struct mm_struct *mm = get_task_mm(task);
285 unsigned int nwords = 0;
288 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
289 res = nwords * sizeof(mm->saved_auxv[0]);
292 memcpy(buffer, mm->saved_auxv, res);
299 #ifdef CONFIG_KALLSYMS
301 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
302 * Returns the resolved symbol. If that fails, simply return the address.
304 static int proc_pid_wchan(struct task_struct *task, char *buffer)
307 char symname[KSYM_NAME_LEN];
309 wchan = get_wchan(task);
311 if (lookup_symbol_name(wchan, symname) < 0)
312 if (!ptrace_may_access(task, PTRACE_MODE_READ))
315 return sprintf(buffer, "%lu", wchan);
317 return sprintf(buffer, "%s", symname);
319 #endif /* CONFIG_KALLSYMS */
321 #ifdef CONFIG_STACKTRACE
323 #define MAX_STACK_TRACE_DEPTH 64
325 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
326 struct pid *pid, struct task_struct *task)
328 struct stack_trace trace;
329 unsigned long *entries;
332 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
336 trace.nr_entries = 0;
337 trace.max_entries = MAX_STACK_TRACE_DEPTH;
338 trace.entries = entries;
340 save_stack_trace_tsk(task, &trace);
342 for (i = 0; i < trace.nr_entries; i++) {
343 seq_printf(m, "[<%p>] %pS\n",
344 (void *)entries[i], (void *)entries[i]);
352 #ifdef CONFIG_SCHEDSTATS
354 * Provides /proc/PID/schedstat
356 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
358 return sprintf(buffer, "%llu %llu %lu\n",
359 (unsigned long long)task->se.sum_exec_runtime,
360 (unsigned long long)task->sched_info.run_delay,
361 task->sched_info.pcount);
365 #ifdef CONFIG_LATENCYTOP
366 static int lstats_show_proc(struct seq_file *m, void *v)
369 struct inode *inode = m->private;
370 struct task_struct *task = get_proc_task(inode);
374 seq_puts(m, "Latency Top version : v0.1\n");
375 for (i = 0; i < 32; i++) {
376 struct latency_record *lr = &task->latency_record[i];
377 if (lr->backtrace[0]) {
379 seq_printf(m, "%i %li %li",
380 lr->count, lr->time, lr->max);
381 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
382 unsigned long bt = lr->backtrace[q];
387 seq_printf(m, " %ps", (void *)bt);
393 put_task_struct(task);
397 static int lstats_open(struct inode *inode, struct file *file)
399 return single_open(file, lstats_show_proc, inode);
402 static ssize_t lstats_write(struct file *file, const char __user *buf,
403 size_t count, loff_t *offs)
405 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
409 clear_all_latency_tracing(task);
410 put_task_struct(task);
415 static const struct file_operations proc_lstats_operations = {
418 .write = lstats_write,
420 .release = single_release,
425 static int proc_oom_score(struct task_struct *task, char *buffer)
427 unsigned long points = 0;
429 read_lock(&tasklist_lock);
431 points = oom_badness(task, NULL, NULL,
432 totalram_pages + total_swap_pages);
433 read_unlock(&tasklist_lock);
434 return sprintf(buffer, "%lu\n", points);
442 static const struct limit_names lnames[RLIM_NLIMITS] = {
443 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
444 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
445 [RLIMIT_DATA] = {"Max data size", "bytes"},
446 [RLIMIT_STACK] = {"Max stack size", "bytes"},
447 [RLIMIT_CORE] = {"Max core file size", "bytes"},
448 [RLIMIT_RSS] = {"Max resident set", "bytes"},
449 [RLIMIT_NPROC] = {"Max processes", "processes"},
450 [RLIMIT_NOFILE] = {"Max open files", "files"},
451 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
452 [RLIMIT_AS] = {"Max address space", "bytes"},
453 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
454 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
455 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
456 [RLIMIT_NICE] = {"Max nice priority", NULL},
457 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
458 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
461 /* Display limits for a process */
462 static int proc_pid_limits(struct task_struct *task, char *buffer)
467 char *bufptr = buffer;
469 struct rlimit rlim[RLIM_NLIMITS];
471 if (!lock_task_sighand(task, &flags))
473 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
474 unlock_task_sighand(task, &flags);
477 * print the file header
479 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
480 "Limit", "Soft Limit", "Hard Limit", "Units");
482 for (i = 0; i < RLIM_NLIMITS; i++) {
483 if (rlim[i].rlim_cur == RLIM_INFINITY)
484 count += sprintf(&bufptr[count], "%-25s %-20s ",
485 lnames[i].name, "unlimited");
487 count += sprintf(&bufptr[count], "%-25s %-20lu ",
488 lnames[i].name, rlim[i].rlim_cur);
490 if (rlim[i].rlim_max == RLIM_INFINITY)
491 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
493 count += sprintf(&bufptr[count], "%-20lu ",
497 count += sprintf(&bufptr[count], "%-10s\n",
500 count += sprintf(&bufptr[count], "\n");
506 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
507 static int proc_pid_syscall(struct task_struct *task, char *buffer)
510 unsigned long args[6], sp, pc;
512 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
513 return sprintf(buffer, "running\n");
516 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
518 return sprintf(buffer,
519 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
521 args[0], args[1], args[2], args[3], args[4], args[5],
524 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
526 /************************************************************************/
527 /* Here the fs part begins */
528 /************************************************************************/
530 /* permission checks */
531 static int proc_fd_access_allowed(struct inode *inode)
533 struct task_struct *task;
535 /* Allow access to a task's file descriptors if it is us or we
536 * may use ptrace attach to the process and find out that
539 task = get_proc_task(inode);
541 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
542 put_task_struct(task);
547 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
550 struct inode *inode = dentry->d_inode;
552 if (attr->ia_valid & ATTR_MODE)
555 error = inode_change_ok(inode, attr);
559 if ((attr->ia_valid & ATTR_SIZE) &&
560 attr->ia_size != i_size_read(inode)) {
561 error = vmtruncate(inode, attr->ia_size);
566 setattr_copy(inode, attr);
567 mark_inode_dirty(inode);
571 static const struct inode_operations proc_def_inode_operations = {
572 .setattr = proc_setattr,
575 static int mounts_open_common(struct inode *inode, struct file *file,
576 const struct seq_operations *op)
578 struct task_struct *task = get_proc_task(inode);
580 struct mnt_namespace *ns = NULL;
582 struct proc_mounts *p;
587 nsp = task_nsproxy(task);
594 if (ns && get_task_root(task, &root) == 0)
596 put_task_struct(task);
605 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
609 file->private_data = &p->m;
610 ret = seq_open(file, op);
617 p->event = ns->event;
631 static int mounts_release(struct inode *inode, struct file *file)
633 struct proc_mounts *p = file->private_data;
636 return seq_release(inode, file);
639 static unsigned mounts_poll(struct file *file, poll_table *wait)
641 struct proc_mounts *p = file->private_data;
642 unsigned res = POLLIN | POLLRDNORM;
644 poll_wait(file, &p->ns->poll, wait);
645 if (mnt_had_events(p))
646 res |= POLLERR | POLLPRI;
651 static int mounts_open(struct inode *inode, struct file *file)
653 return mounts_open_common(inode, file, &mounts_op);
656 static const struct file_operations proc_mounts_operations = {
660 .release = mounts_release,
664 static int mountinfo_open(struct inode *inode, struct file *file)
666 return mounts_open_common(inode, file, &mountinfo_op);
669 static const struct file_operations proc_mountinfo_operations = {
670 .open = mountinfo_open,
673 .release = mounts_release,
677 static int mountstats_open(struct inode *inode, struct file *file)
679 return mounts_open_common(inode, file, &mountstats_op);
682 static const struct file_operations proc_mountstats_operations = {
683 .open = mountstats_open,
686 .release = mounts_release,
689 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
691 static ssize_t proc_info_read(struct file * file, char __user * buf,
692 size_t count, loff_t *ppos)
694 struct inode * inode = file->f_path.dentry->d_inode;
697 struct task_struct *task = get_proc_task(inode);
703 if (count > PROC_BLOCK_SIZE)
704 count = PROC_BLOCK_SIZE;
707 if (!(page = __get_free_page(GFP_TEMPORARY)))
710 length = PROC_I(inode)->op.proc_read(task, (char*)page);
713 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
716 put_task_struct(task);
721 static const struct file_operations proc_info_file_operations = {
722 .read = proc_info_read,
723 .llseek = generic_file_llseek,
726 static int proc_single_show(struct seq_file *m, void *v)
728 struct inode *inode = m->private;
729 struct pid_namespace *ns;
731 struct task_struct *task;
734 ns = inode->i_sb->s_fs_info;
735 pid = proc_pid(inode);
736 task = get_pid_task(pid, PIDTYPE_PID);
740 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
742 put_task_struct(task);
746 static int proc_single_open(struct inode *inode, struct file *filp)
748 return single_open(filp, proc_single_show, inode);
751 static const struct file_operations proc_single_file_operations = {
752 .open = proc_single_open,
755 .release = single_release,
758 static int mem_open(struct inode* inode, struct file* file)
760 file->private_data = (void*)((long)current->self_exec_id);
761 /* OK to pass negative loff_t, we can catch out-of-range */
762 file->f_mode |= FMODE_UNSIGNED_OFFSET;
766 static ssize_t mem_read(struct file * file, char __user * buf,
767 size_t count, loff_t *ppos)
769 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
771 unsigned long src = *ppos;
773 struct mm_struct *mm;
778 if (check_mem_permission(task))
782 page = (char *)__get_free_page(GFP_TEMPORARY);
788 mm = get_task_mm(task);
794 if (file->private_data != (void*)((long)current->self_exec_id))
800 int this_len, retval;
802 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
803 retval = access_process_vm(task, src, page, this_len, 0);
804 if (!retval || check_mem_permission(task)) {
810 if (copy_to_user(buf, page, retval)) {
825 free_page((unsigned long) page);
827 put_task_struct(task);
832 #define mem_write NULL
835 /* This is a security hazard */
836 static ssize_t mem_write(struct file * file, const char __user *buf,
837 size_t count, loff_t *ppos)
841 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
842 unsigned long dst = *ppos;
848 if (check_mem_permission(task))
852 page = (char *)__get_free_page(GFP_TEMPORARY);
858 int this_len, retval;
860 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
861 if (copy_from_user(page, buf, this_len)) {
865 retval = access_process_vm(task, dst, page, this_len, 1);
877 free_page((unsigned long) page);
879 put_task_struct(task);
885 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
889 file->f_pos = offset;
892 file->f_pos += offset;
897 force_successful_syscall_return();
901 static const struct file_operations proc_mem_operations = {
908 static ssize_t environ_read(struct file *file, char __user *buf,
909 size_t count, loff_t *ppos)
911 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
913 unsigned long src = *ppos;
915 struct mm_struct *mm;
920 if (!ptrace_may_access(task, PTRACE_MODE_READ))
924 page = (char *)__get_free_page(GFP_TEMPORARY);
930 mm = get_task_mm(task);
935 int this_len, retval, max_len;
937 this_len = mm->env_end - (mm->env_start + src);
942 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
943 this_len = (this_len > max_len) ? max_len : this_len;
945 retval = access_process_vm(task, (mm->env_start + src),
953 if (copy_to_user(buf, page, retval)) {
967 free_page((unsigned long) page);
969 put_task_struct(task);
974 static const struct file_operations proc_environ_operations = {
975 .read = environ_read,
976 .llseek = generic_file_llseek,
979 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
980 size_t count, loff_t *ppos)
982 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
983 char buffer[PROC_NUMBUF];
985 int oom_adjust = OOM_DISABLE;
991 if (lock_task_sighand(task, &flags)) {
992 oom_adjust = task->signal->oom_adj;
993 unlock_task_sighand(task, &flags);
996 put_task_struct(task);
998 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1000 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1003 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1004 size_t count, loff_t *ppos)
1006 struct task_struct *task;
1007 char buffer[PROC_NUMBUF];
1009 unsigned long flags;
1012 memset(buffer, 0, sizeof(buffer));
1013 if (count > sizeof(buffer) - 1)
1014 count = sizeof(buffer) - 1;
1015 if (copy_from_user(buffer, buf, count)) {
1020 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1023 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1024 oom_adjust != OOM_DISABLE) {
1029 task = get_proc_task(file->f_path.dentry->d_inode);
1041 if (!lock_task_sighand(task, &flags)) {
1046 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1051 if (oom_adjust != task->signal->oom_adj) {
1052 if (oom_adjust == OOM_DISABLE)
1053 atomic_inc(&task->mm->oom_disable_count);
1054 if (task->signal->oom_adj == OOM_DISABLE)
1055 atomic_dec(&task->mm->oom_disable_count);
1059 * Warn that /proc/pid/oom_adj is deprecated, see
1060 * Documentation/feature-removal-schedule.txt.
1062 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1063 "please use /proc/%d/oom_score_adj instead.\n",
1064 current->comm, task_pid_nr(current),
1065 task_pid_nr(task), task_pid_nr(task));
1066 task->signal->oom_adj = oom_adjust;
1068 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1069 * value is always attainable.
1071 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1072 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1074 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1077 unlock_task_sighand(task, &flags);
1080 put_task_struct(task);
1082 return err < 0 ? err : count;
1085 static const struct file_operations proc_oom_adjust_operations = {
1086 .read = oom_adjust_read,
1087 .write = oom_adjust_write,
1088 .llseek = generic_file_llseek,
1091 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1092 size_t count, loff_t *ppos)
1094 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1095 char buffer[PROC_NUMBUF];
1096 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1097 unsigned long flags;
1102 if (lock_task_sighand(task, &flags)) {
1103 oom_score_adj = task->signal->oom_score_adj;
1104 unlock_task_sighand(task, &flags);
1106 put_task_struct(task);
1107 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1108 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1111 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1112 size_t count, loff_t *ppos)
1114 struct task_struct *task;
1115 char buffer[PROC_NUMBUF];
1116 unsigned long flags;
1120 memset(buffer, 0, sizeof(buffer));
1121 if (count > sizeof(buffer) - 1)
1122 count = sizeof(buffer) - 1;
1123 if (copy_from_user(buffer, buf, count)) {
1128 err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1131 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1132 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1137 task = get_proc_task(file->f_path.dentry->d_inode);
1149 if (!lock_task_sighand(task, &flags)) {
1154 if (oom_score_adj < task->signal->oom_score_adj &&
1155 !capable(CAP_SYS_RESOURCE)) {
1160 if (oom_score_adj != task->signal->oom_score_adj) {
1161 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1162 atomic_inc(&task->mm->oom_disable_count);
1163 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1164 atomic_dec(&task->mm->oom_disable_count);
1166 task->signal->oom_score_adj = oom_score_adj;
1168 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1169 * always attainable.
1171 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1172 task->signal->oom_adj = OOM_DISABLE;
1174 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1177 unlock_task_sighand(task, &flags);
1180 put_task_struct(task);
1182 return err < 0 ? err : count;
1185 static const struct file_operations proc_oom_score_adj_operations = {
1186 .read = oom_score_adj_read,
1187 .write = oom_score_adj_write,
1188 .llseek = default_llseek,
1191 #ifdef CONFIG_AUDITSYSCALL
1192 #define TMPBUFLEN 21
1193 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1194 size_t count, loff_t *ppos)
1196 struct inode * inode = file->f_path.dentry->d_inode;
1197 struct task_struct *task = get_proc_task(inode);
1199 char tmpbuf[TMPBUFLEN];
1203 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1204 audit_get_loginuid(task));
1205 put_task_struct(task);
1206 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1209 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1210 size_t count, loff_t *ppos)
1212 struct inode * inode = file->f_path.dentry->d_inode;
1217 if (!capable(CAP_AUDIT_CONTROL))
1221 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1227 if (count >= PAGE_SIZE)
1228 count = PAGE_SIZE - 1;
1231 /* No partial writes. */
1234 page = (char*)__get_free_page(GFP_TEMPORARY);
1238 if (copy_from_user(page, buf, count))
1242 loginuid = simple_strtoul(page, &tmp, 10);
1248 length = audit_set_loginuid(current, loginuid);
1249 if (likely(length == 0))
1253 free_page((unsigned long) page);
1257 static const struct file_operations proc_loginuid_operations = {
1258 .read = proc_loginuid_read,
1259 .write = proc_loginuid_write,
1260 .llseek = generic_file_llseek,
1263 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1264 size_t count, loff_t *ppos)
1266 struct inode * inode = file->f_path.dentry->d_inode;
1267 struct task_struct *task = get_proc_task(inode);
1269 char tmpbuf[TMPBUFLEN];
1273 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1274 audit_get_sessionid(task));
1275 put_task_struct(task);
1276 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1279 static const struct file_operations proc_sessionid_operations = {
1280 .read = proc_sessionid_read,
1281 .llseek = generic_file_llseek,
1285 #ifdef CONFIG_FAULT_INJECTION
1286 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1287 size_t count, loff_t *ppos)
1289 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1290 char buffer[PROC_NUMBUF];
1296 make_it_fail = task->make_it_fail;
1297 put_task_struct(task);
1299 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1301 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1304 static ssize_t proc_fault_inject_write(struct file * file,
1305 const char __user * buf, size_t count, loff_t *ppos)
1307 struct task_struct *task;
1308 char buffer[PROC_NUMBUF], *end;
1311 if (!capable(CAP_SYS_RESOURCE))
1313 memset(buffer, 0, sizeof(buffer));
1314 if (count > sizeof(buffer) - 1)
1315 count = sizeof(buffer) - 1;
1316 if (copy_from_user(buffer, buf, count))
1318 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1321 task = get_proc_task(file->f_dentry->d_inode);
1324 task->make_it_fail = make_it_fail;
1325 put_task_struct(task);
1330 static const struct file_operations proc_fault_inject_operations = {
1331 .read = proc_fault_inject_read,
1332 .write = proc_fault_inject_write,
1333 .llseek = generic_file_llseek,
1338 #ifdef CONFIG_SCHED_DEBUG
1340 * Print out various scheduling related per-task fields:
1342 static int sched_show(struct seq_file *m, void *v)
1344 struct inode *inode = m->private;
1345 struct task_struct *p;
1347 p = get_proc_task(inode);
1350 proc_sched_show_task(p, m);
1358 sched_write(struct file *file, const char __user *buf,
1359 size_t count, loff_t *offset)
1361 struct inode *inode = file->f_path.dentry->d_inode;
1362 struct task_struct *p;
1364 p = get_proc_task(inode);
1367 proc_sched_set_task(p);
1374 static int sched_open(struct inode *inode, struct file *filp)
1376 return single_open(filp, sched_show, inode);
1379 static const struct file_operations proc_pid_sched_operations = {
1382 .write = sched_write,
1383 .llseek = seq_lseek,
1384 .release = single_release,
1389 #ifdef CONFIG_SCHED_AUTOGROUP
1391 * Print out autogroup related information:
1393 static int sched_autogroup_show(struct seq_file *m, void *v)
1395 struct inode *inode = m->private;
1396 struct task_struct *p;
1398 p = get_proc_task(inode);
1401 proc_sched_autogroup_show_task(p, m);
1409 sched_autogroup_write(struct file *file, const char __user *buf,
1410 size_t count, loff_t *offset)
1412 struct inode *inode = file->f_path.dentry->d_inode;
1413 struct task_struct *p;
1414 char buffer[PROC_NUMBUF];
1418 memset(buffer, 0, sizeof(buffer));
1419 if (count > sizeof(buffer) - 1)
1420 count = sizeof(buffer) - 1;
1421 if (copy_from_user(buffer, buf, count))
1424 err = strict_strtol(strstrip(buffer), 0, &nice);
1428 p = get_proc_task(inode);
1433 err = proc_sched_autogroup_set_nice(p, &err);
1442 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1446 ret = single_open(filp, sched_autogroup_show, NULL);
1448 struct seq_file *m = filp->private_data;
1455 static const struct file_operations proc_pid_sched_autogroup_operations = {
1456 .open = sched_autogroup_open,
1458 .write = sched_autogroup_write,
1459 .llseek = seq_lseek,
1460 .release = single_release,
1463 #endif /* CONFIG_SCHED_AUTOGROUP */
1465 static ssize_t comm_write(struct file *file, const char __user *buf,
1466 size_t count, loff_t *offset)
1468 struct inode *inode = file->f_path.dentry->d_inode;
1469 struct task_struct *p;
1470 char buffer[TASK_COMM_LEN];
1472 memset(buffer, 0, sizeof(buffer));
1473 if (count > sizeof(buffer) - 1)
1474 count = sizeof(buffer) - 1;
1475 if (copy_from_user(buffer, buf, count))
1478 p = get_proc_task(inode);
1482 if (same_thread_group(current, p))
1483 set_task_comm(p, buffer);
1492 static int comm_show(struct seq_file *m, void *v)
1494 struct inode *inode = m->private;
1495 struct task_struct *p;
1497 p = get_proc_task(inode);
1502 seq_printf(m, "%s\n", p->comm);
1510 static int comm_open(struct inode *inode, struct file *filp)
1512 return single_open(filp, comm_show, inode);
1515 static const struct file_operations proc_pid_set_comm_operations = {
1518 .write = comm_write,
1519 .llseek = seq_lseek,
1520 .release = single_release,
1524 * We added or removed a vma mapping the executable. The vmas are only mapped
1525 * during exec and are not mapped with the mmap system call.
1526 * Callers must hold down_write() on the mm's mmap_sem for these
1528 void added_exe_file_vma(struct mm_struct *mm)
1530 mm->num_exe_file_vmas++;
1533 void removed_exe_file_vma(struct mm_struct *mm)
1535 mm->num_exe_file_vmas--;
1536 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1538 mm->exe_file = NULL;
1543 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1546 get_file(new_exe_file);
1549 mm->exe_file = new_exe_file;
1550 mm->num_exe_file_vmas = 0;
1553 struct file *get_mm_exe_file(struct mm_struct *mm)
1555 struct file *exe_file;
1557 /* We need mmap_sem to protect against races with removal of
1558 * VM_EXECUTABLE vmas */
1559 down_read(&mm->mmap_sem);
1560 exe_file = mm->exe_file;
1563 up_read(&mm->mmap_sem);
1567 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1569 /* It's safe to write the exe_file pointer without exe_file_lock because
1570 * this is called during fork when the task is not yet in /proc */
1571 newmm->exe_file = get_mm_exe_file(oldmm);
1574 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1576 struct task_struct *task;
1577 struct mm_struct *mm;
1578 struct file *exe_file;
1580 task = get_proc_task(inode);
1583 mm = get_task_mm(task);
1584 put_task_struct(task);
1587 exe_file = get_mm_exe_file(mm);
1590 *exe_path = exe_file->f_path;
1591 path_get(&exe_file->f_path);
1598 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1600 struct inode *inode = dentry->d_inode;
1601 int error = -EACCES;
1603 /* We don't need a base pointer in the /proc filesystem */
1604 path_put(&nd->path);
1606 /* Are we allowed to snoop on the tasks file descriptors? */
1607 if (!proc_fd_access_allowed(inode))
1610 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1612 return ERR_PTR(error);
1615 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1617 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1624 pathname = d_path(path, tmp, PAGE_SIZE);
1625 len = PTR_ERR(pathname);
1626 if (IS_ERR(pathname))
1628 len = tmp + PAGE_SIZE - 1 - pathname;
1632 if (copy_to_user(buffer, pathname, len))
1635 free_page((unsigned long)tmp);
1639 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1641 int error = -EACCES;
1642 struct inode *inode = dentry->d_inode;
1645 /* Are we allowed to snoop on the tasks file descriptors? */
1646 if (!proc_fd_access_allowed(inode))
1649 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1653 error = do_proc_readlink(&path, buffer, buflen);
1659 static const struct inode_operations proc_pid_link_inode_operations = {
1660 .readlink = proc_pid_readlink,
1661 .follow_link = proc_pid_follow_link,
1662 .setattr = proc_setattr,
1666 /* building an inode */
1668 static int task_dumpable(struct task_struct *task)
1671 struct mm_struct *mm;
1676 dumpable = get_dumpable(mm);
1684 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1686 struct inode * inode;
1687 struct proc_inode *ei;
1688 const struct cred *cred;
1690 /* We need a new inode */
1692 inode = new_inode(sb);
1698 inode->i_ino = get_next_ino();
1699 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1700 inode->i_op = &proc_def_inode_operations;
1703 * grab the reference to task.
1705 ei->pid = get_task_pid(task, PIDTYPE_PID);
1709 if (task_dumpable(task)) {
1711 cred = __task_cred(task);
1712 inode->i_uid = cred->euid;
1713 inode->i_gid = cred->egid;
1716 security_task_to_inode(task, inode);
1726 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1728 struct inode *inode = dentry->d_inode;
1729 struct task_struct *task;
1730 const struct cred *cred;
1732 generic_fillattr(inode, stat);
1737 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1739 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1740 task_dumpable(task)) {
1741 cred = __task_cred(task);
1742 stat->uid = cred->euid;
1743 stat->gid = cred->egid;
1753 * Exceptional case: normally we are not allowed to unhash a busy
1754 * directory. In this case, however, we can do it - no aliasing problems
1755 * due to the way we treat inodes.
1757 * Rewrite the inode's ownerships here because the owning task may have
1758 * performed a setuid(), etc.
1760 * Before the /proc/pid/status file was created the only way to read
1761 * the effective uid of a /process was to stat /proc/pid. Reading
1762 * /proc/pid/status is slow enough that procps and other packages
1763 * kept stating /proc/pid. To keep the rules in /proc simple I have
1764 * made this apply to all per process world readable and executable
1767 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1769 struct inode *inode;
1770 struct task_struct *task;
1771 const struct cred *cred;
1773 if (nd && nd->flags & LOOKUP_RCU)
1776 inode = dentry->d_inode;
1777 task = get_proc_task(inode);
1780 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1781 task_dumpable(task)) {
1783 cred = __task_cred(task);
1784 inode->i_uid = cred->euid;
1785 inode->i_gid = cred->egid;
1791 inode->i_mode &= ~(S_ISUID | S_ISGID);
1792 security_task_to_inode(task, inode);
1793 put_task_struct(task);
1800 static int pid_delete_dentry(const struct dentry * dentry)
1802 /* Is the task we represent dead?
1803 * If so, then don't put the dentry on the lru list,
1804 * kill it immediately.
1806 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1809 static const struct dentry_operations pid_dentry_operations =
1811 .d_revalidate = pid_revalidate,
1812 .d_delete = pid_delete_dentry,
1817 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1818 struct task_struct *, const void *);
1821 * Fill a directory entry.
1823 * If possible create the dcache entry and derive our inode number and
1824 * file type from dcache entry.
1826 * Since all of the proc inode numbers are dynamically generated, the inode
1827 * numbers do not exist until the inode is cache. This means creating the
1828 * the dcache entry in readdir is necessary to keep the inode numbers
1829 * reported by readdir in sync with the inode numbers reported
1832 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1833 char *name, int len,
1834 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1836 struct dentry *child, *dir = filp->f_path.dentry;
1837 struct inode *inode;
1840 unsigned type = DT_UNKNOWN;
1844 qname.hash = full_name_hash(name, len);
1846 child = d_lookup(dir, &qname);
1849 new = d_alloc(dir, &qname);
1851 child = instantiate(dir->d_inode, new, task, ptr);
1858 if (!child || IS_ERR(child) || !child->d_inode)
1859 goto end_instantiate;
1860 inode = child->d_inode;
1863 type = inode->i_mode >> 12;
1868 ino = find_inode_number(dir, &qname);
1871 return filldir(dirent, name, len, filp->f_pos, ino, type);
1874 static unsigned name_to_int(struct dentry *dentry)
1876 const char *name = dentry->d_name.name;
1877 int len = dentry->d_name.len;
1880 if (len > 1 && *name == '0')
1883 unsigned c = *name++ - '0';
1886 if (n >= (~0U-9)/10)
1896 #define PROC_FDINFO_MAX 64
1898 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1900 struct task_struct *task = get_proc_task(inode);
1901 struct files_struct *files = NULL;
1903 int fd = proc_fd(inode);
1906 files = get_files_struct(task);
1907 put_task_struct(task);
1911 * We are not taking a ref to the file structure, so we must
1914 spin_lock(&files->file_lock);
1915 file = fcheck_files(files, fd);
1918 *path = file->f_path;
1919 path_get(&file->f_path);
1922 snprintf(info, PROC_FDINFO_MAX,
1925 (long long) file->f_pos,
1927 spin_unlock(&files->file_lock);
1928 put_files_struct(files);
1931 spin_unlock(&files->file_lock);
1932 put_files_struct(files);
1937 static int proc_fd_link(struct inode *inode, struct path *path)
1939 return proc_fd_info(inode, path, NULL);
1942 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1944 struct inode *inode;
1945 struct task_struct *task;
1947 struct files_struct *files;
1948 const struct cred *cred;
1950 if (nd && nd->flags & LOOKUP_RCU)
1953 inode = dentry->d_inode;
1954 task = get_proc_task(inode);
1955 fd = proc_fd(inode);
1958 files = get_files_struct(task);
1961 if (fcheck_files(files, fd)) {
1963 put_files_struct(files);
1964 if (task_dumpable(task)) {
1966 cred = __task_cred(task);
1967 inode->i_uid = cred->euid;
1968 inode->i_gid = cred->egid;
1974 inode->i_mode &= ~(S_ISUID | S_ISGID);
1975 security_task_to_inode(task, inode);
1976 put_task_struct(task);
1980 put_files_struct(files);
1982 put_task_struct(task);
1988 static const struct dentry_operations tid_fd_dentry_operations =
1990 .d_revalidate = tid_fd_revalidate,
1991 .d_delete = pid_delete_dentry,
1994 static struct dentry *proc_fd_instantiate(struct inode *dir,
1995 struct dentry *dentry, struct task_struct *task, const void *ptr)
1997 unsigned fd = *(const unsigned *)ptr;
1999 struct files_struct *files;
2000 struct inode *inode;
2001 struct proc_inode *ei;
2002 struct dentry *error = ERR_PTR(-ENOENT);
2004 inode = proc_pid_make_inode(dir->i_sb, task);
2009 files = get_files_struct(task);
2012 inode->i_mode = S_IFLNK;
2015 * We are not taking a ref to the file structure, so we must
2018 spin_lock(&files->file_lock);
2019 file = fcheck_files(files, fd);
2022 if (file->f_mode & FMODE_READ)
2023 inode->i_mode |= S_IRUSR | S_IXUSR;
2024 if (file->f_mode & FMODE_WRITE)
2025 inode->i_mode |= S_IWUSR | S_IXUSR;
2026 spin_unlock(&files->file_lock);
2027 put_files_struct(files);
2029 inode->i_op = &proc_pid_link_inode_operations;
2031 ei->op.proc_get_link = proc_fd_link;
2032 d_set_d_op(dentry, &tid_fd_dentry_operations);
2033 d_add(dentry, inode);
2034 /* Close the race of the process dying before we return the dentry */
2035 if (tid_fd_revalidate(dentry, NULL))
2041 spin_unlock(&files->file_lock);
2042 put_files_struct(files);
2048 static struct dentry *proc_lookupfd_common(struct inode *dir,
2049 struct dentry *dentry,
2050 instantiate_t instantiate)
2052 struct task_struct *task = get_proc_task(dir);
2053 unsigned fd = name_to_int(dentry);
2054 struct dentry *result = ERR_PTR(-ENOENT);
2061 result = instantiate(dir, dentry, task, &fd);
2063 put_task_struct(task);
2068 static int proc_readfd_common(struct file * filp, void * dirent,
2069 filldir_t filldir, instantiate_t instantiate)
2071 struct dentry *dentry = filp->f_path.dentry;
2072 struct inode *inode = dentry->d_inode;
2073 struct task_struct *p = get_proc_task(inode);
2074 unsigned int fd, ino;
2076 struct files_struct * files;
2086 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2090 ino = parent_ino(dentry);
2091 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2095 files = get_files_struct(p);
2099 for (fd = filp->f_pos-2;
2100 fd < files_fdtable(files)->max_fds;
2101 fd++, filp->f_pos++) {
2102 char name[PROC_NUMBUF];
2105 if (!fcheck_files(files, fd))
2109 len = snprintf(name, sizeof(name), "%d", fd);
2110 if (proc_fill_cache(filp, dirent, filldir,
2111 name, len, instantiate,
2119 put_files_struct(files);
2127 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2128 struct nameidata *nd)
2130 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2133 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2135 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2138 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2139 size_t len, loff_t *ppos)
2141 char tmp[PROC_FDINFO_MAX];
2142 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2144 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2148 static const struct file_operations proc_fdinfo_file_operations = {
2149 .open = nonseekable_open,
2150 .read = proc_fdinfo_read,
2151 .llseek = no_llseek,
2154 static const struct file_operations proc_fd_operations = {
2155 .read = generic_read_dir,
2156 .readdir = proc_readfd,
2157 .llseek = default_llseek,
2161 * /proc/pid/fd needs a special permission handler so that a process can still
2162 * access /proc/self/fd after it has executed a setuid().
2164 static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
2168 if (flags & IPERM_FLAG_RCU)
2170 rv = generic_permission(inode, mask, flags, NULL);
2173 if (task_pid(current) == proc_pid(inode))
2179 * proc directories can do almost nothing..
2181 static const struct inode_operations proc_fd_inode_operations = {
2182 .lookup = proc_lookupfd,
2183 .permission = proc_fd_permission,
2184 .setattr = proc_setattr,
2187 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2188 struct dentry *dentry, struct task_struct *task, const void *ptr)
2190 unsigned fd = *(unsigned *)ptr;
2191 struct inode *inode;
2192 struct proc_inode *ei;
2193 struct dentry *error = ERR_PTR(-ENOENT);
2195 inode = proc_pid_make_inode(dir->i_sb, task);
2200 inode->i_mode = S_IFREG | S_IRUSR;
2201 inode->i_fop = &proc_fdinfo_file_operations;
2202 d_set_d_op(dentry, &tid_fd_dentry_operations);
2203 d_add(dentry, inode);
2204 /* Close the race of the process dying before we return the dentry */
2205 if (tid_fd_revalidate(dentry, NULL))
2212 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2213 struct dentry *dentry,
2214 struct nameidata *nd)
2216 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2219 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2221 return proc_readfd_common(filp, dirent, filldir,
2222 proc_fdinfo_instantiate);
2225 static const struct file_operations proc_fdinfo_operations = {
2226 .read = generic_read_dir,
2227 .readdir = proc_readfdinfo,
2228 .llseek = default_llseek,
2232 * proc directories can do almost nothing..
2234 static const struct inode_operations proc_fdinfo_inode_operations = {
2235 .lookup = proc_lookupfdinfo,
2236 .setattr = proc_setattr,
2240 static struct dentry *proc_pident_instantiate(struct inode *dir,
2241 struct dentry *dentry, struct task_struct *task, const void *ptr)
2243 const struct pid_entry *p = ptr;
2244 struct inode *inode;
2245 struct proc_inode *ei;
2246 struct dentry *error = ERR_PTR(-ENOENT);
2248 inode = proc_pid_make_inode(dir->i_sb, task);
2253 inode->i_mode = p->mode;
2254 if (S_ISDIR(inode->i_mode))
2255 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2257 inode->i_op = p->iop;
2259 inode->i_fop = p->fop;
2261 d_set_d_op(dentry, &pid_dentry_operations);
2262 d_add(dentry, inode);
2263 /* Close the race of the process dying before we return the dentry */
2264 if (pid_revalidate(dentry, NULL))
2270 static struct dentry *proc_pident_lookup(struct inode *dir,
2271 struct dentry *dentry,
2272 const struct pid_entry *ents,
2275 struct dentry *error;
2276 struct task_struct *task = get_proc_task(dir);
2277 const struct pid_entry *p, *last;
2279 error = ERR_PTR(-ENOENT);
2285 * Yes, it does not scale. And it should not. Don't add
2286 * new entries into /proc/<tgid>/ without very good reasons.
2288 last = &ents[nents - 1];
2289 for (p = ents; p <= last; p++) {
2290 if (p->len != dentry->d_name.len)
2292 if (!memcmp(dentry->d_name.name, p->name, p->len))
2298 error = proc_pident_instantiate(dir, dentry, task, p);
2300 put_task_struct(task);
2305 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2306 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2308 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2309 proc_pident_instantiate, task, p);
2312 static int proc_pident_readdir(struct file *filp,
2313 void *dirent, filldir_t filldir,
2314 const struct pid_entry *ents, unsigned int nents)
2317 struct dentry *dentry = filp->f_path.dentry;
2318 struct inode *inode = dentry->d_inode;
2319 struct task_struct *task = get_proc_task(inode);
2320 const struct pid_entry *p, *last;
2333 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2339 ino = parent_ino(dentry);
2340 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2352 last = &ents[nents - 1];
2354 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2363 put_task_struct(task);
2368 #ifdef CONFIG_SECURITY
2369 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2370 size_t count, loff_t *ppos)
2372 struct inode * inode = file->f_path.dentry->d_inode;
2375 struct task_struct *task = get_proc_task(inode);
2380 length = security_getprocattr(task,
2381 (char*)file->f_path.dentry->d_name.name,
2383 put_task_struct(task);
2385 length = simple_read_from_buffer(buf, count, ppos, p, length);
2390 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2391 size_t count, loff_t *ppos)
2393 struct inode * inode = file->f_path.dentry->d_inode;
2396 struct task_struct *task = get_proc_task(inode);
2401 if (count > PAGE_SIZE)
2404 /* No partial writes. */
2410 page = (char*)__get_free_page(GFP_TEMPORARY);
2415 if (copy_from_user(page, buf, count))
2418 /* Guard against adverse ptrace interaction */
2419 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2423 length = security_setprocattr(task,
2424 (char*)file->f_path.dentry->d_name.name,
2425 (void*)page, count);
2426 mutex_unlock(&task->signal->cred_guard_mutex);
2428 free_page((unsigned long) page);
2430 put_task_struct(task);
2435 static const struct file_operations proc_pid_attr_operations = {
2436 .read = proc_pid_attr_read,
2437 .write = proc_pid_attr_write,
2438 .llseek = generic_file_llseek,
2441 static const struct pid_entry attr_dir_stuff[] = {
2442 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2443 REG("prev", S_IRUGO, proc_pid_attr_operations),
2444 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2445 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2446 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2447 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2450 static int proc_attr_dir_readdir(struct file * filp,
2451 void * dirent, filldir_t filldir)
2453 return proc_pident_readdir(filp,dirent,filldir,
2454 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2457 static const struct file_operations proc_attr_dir_operations = {
2458 .read = generic_read_dir,
2459 .readdir = proc_attr_dir_readdir,
2460 .llseek = default_llseek,
2463 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2464 struct dentry *dentry, struct nameidata *nd)
2466 return proc_pident_lookup(dir, dentry,
2467 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2470 static const struct inode_operations proc_attr_dir_inode_operations = {
2471 .lookup = proc_attr_dir_lookup,
2472 .getattr = pid_getattr,
2473 .setattr = proc_setattr,
2478 #ifdef CONFIG_ELF_CORE
2479 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2480 size_t count, loff_t *ppos)
2482 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2483 struct mm_struct *mm;
2484 char buffer[PROC_NUMBUF];
2492 mm = get_task_mm(task);
2494 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2495 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2496 MMF_DUMP_FILTER_SHIFT));
2498 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2501 put_task_struct(task);
2506 static ssize_t proc_coredump_filter_write(struct file *file,
2507 const char __user *buf,
2511 struct task_struct *task;
2512 struct mm_struct *mm;
2513 char buffer[PROC_NUMBUF], *end;
2520 memset(buffer, 0, sizeof(buffer));
2521 if (count > sizeof(buffer) - 1)
2522 count = sizeof(buffer) - 1;
2523 if (copy_from_user(buffer, buf, count))
2527 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2530 if (end - buffer == 0)
2534 task = get_proc_task(file->f_dentry->d_inode);
2539 mm = get_task_mm(task);
2543 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2545 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2547 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2552 put_task_struct(task);
2557 static const struct file_operations proc_coredump_filter_operations = {
2558 .read = proc_coredump_filter_read,
2559 .write = proc_coredump_filter_write,
2560 .llseek = generic_file_llseek,
2567 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2570 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2571 pid_t tgid = task_tgid_nr_ns(current, ns);
2572 char tmp[PROC_NUMBUF];
2575 sprintf(tmp, "%d", tgid);
2576 return vfs_readlink(dentry,buffer,buflen,tmp);
2579 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2581 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2582 pid_t tgid = task_tgid_nr_ns(current, ns);
2583 char *name = ERR_PTR(-ENOENT);
2587 name = ERR_PTR(-ENOMEM);
2589 sprintf(name, "%d", tgid);
2591 nd_set_link(nd, name);
2595 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2598 char *s = nd_get_link(nd);
2603 static const struct inode_operations proc_self_inode_operations = {
2604 .readlink = proc_self_readlink,
2605 .follow_link = proc_self_follow_link,
2606 .put_link = proc_self_put_link,
2612 * These are the directory entries in the root directory of /proc
2613 * that properly belong to the /proc filesystem, as they describe
2614 * describe something that is process related.
2616 static const struct pid_entry proc_base_stuff[] = {
2617 NOD("self", S_IFLNK|S_IRWXUGO,
2618 &proc_self_inode_operations, NULL, {}),
2622 * Exceptional case: normally we are not allowed to unhash a busy
2623 * directory. In this case, however, we can do it - no aliasing problems
2624 * due to the way we treat inodes.
2626 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2628 struct inode *inode;
2629 struct task_struct *task;
2631 if (nd->flags & LOOKUP_RCU)
2634 inode = dentry->d_inode;
2635 task = get_proc_task(inode);
2637 put_task_struct(task);
2644 static const struct dentry_operations proc_base_dentry_operations =
2646 .d_revalidate = proc_base_revalidate,
2647 .d_delete = pid_delete_dentry,
2650 static struct dentry *proc_base_instantiate(struct inode *dir,
2651 struct dentry *dentry, struct task_struct *task, const void *ptr)
2653 const struct pid_entry *p = ptr;
2654 struct inode *inode;
2655 struct proc_inode *ei;
2656 struct dentry *error;
2658 /* Allocate the inode */
2659 error = ERR_PTR(-ENOMEM);
2660 inode = new_inode(dir->i_sb);
2664 /* Initialize the inode */
2666 inode->i_ino = get_next_ino();
2667 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2670 * grab the reference to the task.
2672 ei->pid = get_task_pid(task, PIDTYPE_PID);
2676 inode->i_mode = p->mode;
2677 if (S_ISDIR(inode->i_mode))
2679 if (S_ISLNK(inode->i_mode))
2682 inode->i_op = p->iop;
2684 inode->i_fop = p->fop;
2686 d_set_d_op(dentry, &proc_base_dentry_operations);
2687 d_add(dentry, inode);
2696 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2698 struct dentry *error;
2699 struct task_struct *task = get_proc_task(dir);
2700 const struct pid_entry *p, *last;
2702 error = ERR_PTR(-ENOENT);
2707 /* Lookup the directory entry */
2708 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2709 for (p = proc_base_stuff; p <= last; p++) {
2710 if (p->len != dentry->d_name.len)
2712 if (!memcmp(dentry->d_name.name, p->name, p->len))
2718 error = proc_base_instantiate(dir, dentry, task, p);
2721 put_task_struct(task);
2726 static int proc_base_fill_cache(struct file *filp, void *dirent,
2727 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2729 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2730 proc_base_instantiate, task, p);
2733 #ifdef CONFIG_TASK_IO_ACCOUNTING
2734 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2736 struct task_io_accounting acct = task->ioac;
2737 unsigned long flags;
2739 if (whole && lock_task_sighand(task, &flags)) {
2740 struct task_struct *t = task;
2742 task_io_accounting_add(&acct, &task->signal->ioac);
2743 while_each_thread(task, t)
2744 task_io_accounting_add(&acct, &t->ioac);
2746 unlock_task_sighand(task, &flags);
2748 return sprintf(buffer,
2753 "read_bytes: %llu\n"
2754 "write_bytes: %llu\n"
2755 "cancelled_write_bytes: %llu\n",
2756 (unsigned long long)acct.rchar,
2757 (unsigned long long)acct.wchar,
2758 (unsigned long long)acct.syscr,
2759 (unsigned long long)acct.syscw,
2760 (unsigned long long)acct.read_bytes,
2761 (unsigned long long)acct.write_bytes,
2762 (unsigned long long)acct.cancelled_write_bytes);
2765 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2767 return do_io_accounting(task, buffer, 0);
2770 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2772 return do_io_accounting(task, buffer, 1);
2774 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2776 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2777 struct pid *pid, struct task_struct *task)
2779 seq_printf(m, "%08x\n", task->personality);
2786 static const struct file_operations proc_task_operations;
2787 static const struct inode_operations proc_task_inode_operations;
2789 static const struct pid_entry tgid_base_stuff[] = {
2790 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2791 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2792 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2794 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2796 REG("environ", S_IRUSR, proc_environ_operations),
2797 INF("auxv", S_IRUSR, proc_pid_auxv),
2798 ONE("status", S_IRUGO, proc_pid_status),
2799 ONE("personality", S_IRUSR, proc_pid_personality),
2800 INF("limits", S_IRUGO, proc_pid_limits),
2801 #ifdef CONFIG_SCHED_DEBUG
2802 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2804 #ifdef CONFIG_SCHED_AUTOGROUP
2805 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2807 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2808 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2809 INF("syscall", S_IRUSR, proc_pid_syscall),
2811 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2812 ONE("stat", S_IRUGO, proc_tgid_stat),
2813 ONE("statm", S_IRUGO, proc_pid_statm),
2814 REG("maps", S_IRUGO, proc_maps_operations),
2816 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2818 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2819 LNK("cwd", proc_cwd_link),
2820 LNK("root", proc_root_link),
2821 LNK("exe", proc_exe_link),
2822 REG("mounts", S_IRUGO, proc_mounts_operations),
2823 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2824 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2825 #ifdef CONFIG_PROC_PAGE_MONITOR
2826 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2827 REG("smaps", S_IRUGO, proc_smaps_operations),
2828 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2830 #ifdef CONFIG_SECURITY
2831 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2833 #ifdef CONFIG_KALLSYMS
2834 INF("wchan", S_IRUGO, proc_pid_wchan),
2836 #ifdef CONFIG_STACKTRACE
2837 ONE("stack", S_IRUSR, proc_pid_stack),
2839 #ifdef CONFIG_SCHEDSTATS
2840 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2842 #ifdef CONFIG_LATENCYTOP
2843 REG("latency", S_IRUGO, proc_lstats_operations),
2845 #ifdef CONFIG_PROC_PID_CPUSET
2846 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2848 #ifdef CONFIG_CGROUPS
2849 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2851 INF("oom_score", S_IRUGO, proc_oom_score),
2852 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2853 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2854 #ifdef CONFIG_AUDITSYSCALL
2855 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2856 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2858 #ifdef CONFIG_FAULT_INJECTION
2859 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2861 #ifdef CONFIG_ELF_CORE
2862 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2864 #ifdef CONFIG_TASK_IO_ACCOUNTING
2865 INF("io", S_IRUGO, proc_tgid_io_accounting),
2869 static int proc_tgid_base_readdir(struct file * filp,
2870 void * dirent, filldir_t filldir)
2872 return proc_pident_readdir(filp,dirent,filldir,
2873 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2876 static const struct file_operations proc_tgid_base_operations = {
2877 .read = generic_read_dir,
2878 .readdir = proc_tgid_base_readdir,
2879 .llseek = default_llseek,
2882 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2883 return proc_pident_lookup(dir, dentry,
2884 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2887 static const struct inode_operations proc_tgid_base_inode_operations = {
2888 .lookup = proc_tgid_base_lookup,
2889 .getattr = pid_getattr,
2890 .setattr = proc_setattr,
2893 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2895 struct dentry *dentry, *leader, *dir;
2896 char buf[PROC_NUMBUF];
2900 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2901 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2903 shrink_dcache_parent(dentry);
2909 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2910 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2915 name.len = strlen(name.name);
2916 dir = d_hash_and_lookup(leader, &name);
2918 goto out_put_leader;
2921 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2922 dentry = d_hash_and_lookup(dir, &name);
2924 shrink_dcache_parent(dentry);
2937 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2938 * @task: task that should be flushed.
2940 * When flushing dentries from proc, one needs to flush them from global
2941 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2942 * in. This call is supposed to do all of this job.
2944 * Looks in the dcache for
2946 * /proc/@tgid/task/@pid
2947 * if either directory is present flushes it and all of it'ts children
2950 * It is safe and reasonable to cache /proc entries for a task until
2951 * that task exits. After that they just clog up the dcache with
2952 * useless entries, possibly causing useful dcache entries to be
2953 * flushed instead. This routine is proved to flush those useless
2954 * dcache entries at process exit time.
2956 * NOTE: This routine is just an optimization so it does not guarantee
2957 * that no dcache entries will exist at process exit time it
2958 * just makes it very unlikely that any will persist.
2961 void proc_flush_task(struct task_struct *task)
2964 struct pid *pid, *tgid;
2967 pid = task_pid(task);
2968 tgid = task_tgid(task);
2970 for (i = 0; i <= pid->level; i++) {
2971 upid = &pid->numbers[i];
2972 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2973 tgid->numbers[i].nr);
2976 upid = &pid->numbers[pid->level];
2978 pid_ns_release_proc(upid->ns);
2981 static struct dentry *proc_pid_instantiate(struct inode *dir,
2982 struct dentry * dentry,
2983 struct task_struct *task, const void *ptr)
2985 struct dentry *error = ERR_PTR(-ENOENT);
2986 struct inode *inode;
2988 inode = proc_pid_make_inode(dir->i_sb, task);
2992 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2993 inode->i_op = &proc_tgid_base_inode_operations;
2994 inode->i_fop = &proc_tgid_base_operations;
2995 inode->i_flags|=S_IMMUTABLE;
2997 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2998 ARRAY_SIZE(tgid_base_stuff));
3000 d_set_d_op(dentry, &pid_dentry_operations);
3002 d_add(dentry, inode);
3003 /* Close the race of the process dying before we return the dentry */
3004 if (pid_revalidate(dentry, NULL))
3010 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3012 struct dentry *result;
3013 struct task_struct *task;
3015 struct pid_namespace *ns;
3017 result = proc_base_lookup(dir, dentry);
3018 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3021 tgid = name_to_int(dentry);
3025 ns = dentry->d_sb->s_fs_info;
3027 task = find_task_by_pid_ns(tgid, ns);
3029 get_task_struct(task);
3034 result = proc_pid_instantiate(dir, dentry, task, NULL);
3035 put_task_struct(task);
3041 * Find the first task with tgid >= tgid
3046 struct task_struct *task;
3048 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3053 put_task_struct(iter.task);
3057 pid = find_ge_pid(iter.tgid, ns);
3059 iter.tgid = pid_nr_ns(pid, ns);
3060 iter.task = pid_task(pid, PIDTYPE_PID);
3061 /* What we to know is if the pid we have find is the
3062 * pid of a thread_group_leader. Testing for task
3063 * being a thread_group_leader is the obvious thing
3064 * todo but there is a window when it fails, due to
3065 * the pid transfer logic in de_thread.
3067 * So we perform the straight forward test of seeing
3068 * if the pid we have found is the pid of a thread
3069 * group leader, and don't worry if the task we have
3070 * found doesn't happen to be a thread group leader.
3071 * As we don't care in the case of readdir.
3073 if (!iter.task || !has_group_leader_pid(iter.task)) {
3077 get_task_struct(iter.task);
3083 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3085 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3086 struct tgid_iter iter)
3088 char name[PROC_NUMBUF];
3089 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3090 return proc_fill_cache(filp, dirent, filldir, name, len,
3091 proc_pid_instantiate, iter.task, NULL);
3094 /* for the /proc/ directory itself, after non-process stuff has been done */
3095 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3097 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3098 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
3099 struct tgid_iter iter;
3100 struct pid_namespace *ns;
3105 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3106 const struct pid_entry *p = &proc_base_stuff[nr];
3107 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3111 ns = filp->f_dentry->d_sb->s_fs_info;
3113 iter.tgid = filp->f_pos - TGID_OFFSET;
3114 for (iter = next_tgid(ns, iter);
3116 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3117 filp->f_pos = iter.tgid + TGID_OFFSET;
3118 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3119 put_task_struct(iter.task);
3123 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3125 put_task_struct(reaper);
3133 static const struct pid_entry tid_base_stuff[] = {
3134 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3135 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3136 REG("environ", S_IRUSR, proc_environ_operations),
3137 INF("auxv", S_IRUSR, proc_pid_auxv),
3138 ONE("status", S_IRUGO, proc_pid_status),
3139 ONE("personality", S_IRUSR, proc_pid_personality),
3140 INF("limits", S_IRUGO, proc_pid_limits),
3141 #ifdef CONFIG_SCHED_DEBUG
3142 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3144 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3145 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3146 INF("syscall", S_IRUSR, proc_pid_syscall),
3148 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3149 ONE("stat", S_IRUGO, proc_tid_stat),
3150 ONE("statm", S_IRUGO, proc_pid_statm),
3151 REG("maps", S_IRUGO, proc_maps_operations),
3153 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3155 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3156 LNK("cwd", proc_cwd_link),
3157 LNK("root", proc_root_link),
3158 LNK("exe", proc_exe_link),
3159 REG("mounts", S_IRUGO, proc_mounts_operations),
3160 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3161 #ifdef CONFIG_PROC_PAGE_MONITOR
3162 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3163 REG("smaps", S_IRUGO, proc_smaps_operations),
3164 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3166 #ifdef CONFIG_SECURITY
3167 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3169 #ifdef CONFIG_KALLSYMS
3170 INF("wchan", S_IRUGO, proc_pid_wchan),
3172 #ifdef CONFIG_STACKTRACE
3173 ONE("stack", S_IRUSR, proc_pid_stack),
3175 #ifdef CONFIG_SCHEDSTATS
3176 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3178 #ifdef CONFIG_LATENCYTOP
3179 REG("latency", S_IRUGO, proc_lstats_operations),
3181 #ifdef CONFIG_PROC_PID_CPUSET
3182 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3184 #ifdef CONFIG_CGROUPS
3185 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3187 INF("oom_score", S_IRUGO, proc_oom_score),
3188 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3189 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3190 #ifdef CONFIG_AUDITSYSCALL
3191 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3192 REG("sessionid", S_IRUSR, proc_sessionid_operations),
3194 #ifdef CONFIG_FAULT_INJECTION
3195 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3197 #ifdef CONFIG_TASK_IO_ACCOUNTING
3198 INF("io", S_IRUGO, proc_tid_io_accounting),
3202 static int proc_tid_base_readdir(struct file * filp,
3203 void * dirent, filldir_t filldir)
3205 return proc_pident_readdir(filp,dirent,filldir,
3206 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3209 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3210 return proc_pident_lookup(dir, dentry,
3211 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3214 static const struct file_operations proc_tid_base_operations = {
3215 .read = generic_read_dir,
3216 .readdir = proc_tid_base_readdir,
3217 .llseek = default_llseek,
3220 static const struct inode_operations proc_tid_base_inode_operations = {
3221 .lookup = proc_tid_base_lookup,
3222 .getattr = pid_getattr,
3223 .setattr = proc_setattr,
3226 static struct dentry *proc_task_instantiate(struct inode *dir,
3227 struct dentry *dentry, struct task_struct *task, const void *ptr)
3229 struct dentry *error = ERR_PTR(-ENOENT);
3230 struct inode *inode;
3231 inode = proc_pid_make_inode(dir->i_sb, task);
3235 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3236 inode->i_op = &proc_tid_base_inode_operations;
3237 inode->i_fop = &proc_tid_base_operations;
3238 inode->i_flags|=S_IMMUTABLE;
3240 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3241 ARRAY_SIZE(tid_base_stuff));
3243 d_set_d_op(dentry, &pid_dentry_operations);
3245 d_add(dentry, inode);
3246 /* Close the race of the process dying before we return the dentry */
3247 if (pid_revalidate(dentry, NULL))
3253 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3255 struct dentry *result = ERR_PTR(-ENOENT);
3256 struct task_struct *task;
3257 struct task_struct *leader = get_proc_task(dir);
3259 struct pid_namespace *ns;
3264 tid = name_to_int(dentry);
3268 ns = dentry->d_sb->s_fs_info;
3270 task = find_task_by_pid_ns(tid, ns);
3272 get_task_struct(task);
3276 if (!same_thread_group(leader, task))
3279 result = proc_task_instantiate(dir, dentry, task, NULL);
3281 put_task_struct(task);
3283 put_task_struct(leader);
3289 * Find the first tid of a thread group to return to user space.
3291 * Usually this is just the thread group leader, but if the users
3292 * buffer was too small or there was a seek into the middle of the
3293 * directory we have more work todo.
3295 * In the case of a short read we start with find_task_by_pid.
3297 * In the case of a seek we start with the leader and walk nr
3300 static struct task_struct *first_tid(struct task_struct *leader,
3301 int tid, int nr, struct pid_namespace *ns)
3303 struct task_struct *pos;
3306 /* Attempt to start with the pid of a thread */
3307 if (tid && (nr > 0)) {
3308 pos = find_task_by_pid_ns(tid, ns);
3309 if (pos && (pos->group_leader == leader))
3313 /* If nr exceeds the number of threads there is nothing todo */
3315 if (nr && nr >= get_nr_threads(leader))
3318 /* If we haven't found our starting place yet start
3319 * with the leader and walk nr threads forward.
3321 for (pos = leader; nr > 0; --nr) {
3322 pos = next_thread(pos);
3323 if (pos == leader) {
3329 get_task_struct(pos);
3336 * Find the next thread in the thread list.
3337 * Return NULL if there is an error or no next thread.
3339 * The reference to the input task_struct is released.
3341 static struct task_struct *next_tid(struct task_struct *start)
3343 struct task_struct *pos = NULL;
3345 if (pid_alive(start)) {
3346 pos = next_thread(start);
3347 if (thread_group_leader(pos))
3350 get_task_struct(pos);
3353 put_task_struct(start);
3357 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3358 struct task_struct *task, int tid)
3360 char name[PROC_NUMBUF];
3361 int len = snprintf(name, sizeof(name), "%d", tid);
3362 return proc_fill_cache(filp, dirent, filldir, name, len,
3363 proc_task_instantiate, task, NULL);
3366 /* for the /proc/TGID/task/ directories */
3367 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3369 struct dentry *dentry = filp->f_path.dentry;
3370 struct inode *inode = dentry->d_inode;
3371 struct task_struct *leader = NULL;
3372 struct task_struct *task;
3373 int retval = -ENOENT;
3376 struct pid_namespace *ns;
3378 task = get_proc_task(inode);
3382 if (pid_alive(task)) {
3383 leader = task->group_leader;
3384 get_task_struct(leader);
3387 put_task_struct(task);
3392 switch ((unsigned long)filp->f_pos) {
3395 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3400 ino = parent_ino(dentry);
3401 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3407 /* f_version caches the tgid value that the last readdir call couldn't
3408 * return. lseek aka telldir automagically resets f_version to 0.
3410 ns = filp->f_dentry->d_sb->s_fs_info;
3411 tid = (int)filp->f_version;
3412 filp->f_version = 0;
3413 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3415 task = next_tid(task), filp->f_pos++) {
3416 tid = task_pid_nr_ns(task, ns);
3417 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3418 /* returning this tgid failed, save it as the first
3419 * pid for the next readir call */
3420 filp->f_version = (u64)tid;
3421 put_task_struct(task);
3426 put_task_struct(leader);
3431 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3433 struct inode *inode = dentry->d_inode;
3434 struct task_struct *p = get_proc_task(inode);
3435 generic_fillattr(inode, stat);
3438 stat->nlink += get_nr_threads(p);
3445 static const struct inode_operations proc_task_inode_operations = {
3446 .lookup = proc_task_lookup,
3447 .getattr = proc_task_getattr,
3448 .setattr = proc_setattr,
3451 static const struct file_operations proc_task_operations = {
3452 .read = generic_read_dir,
3453 .readdir = proc_task_readdir,
3454 .llseek = default_llseek,