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/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
93 #include <trace/events/oom.h>
97 #include "../../lib/kstrtox.h"
100 * Implementing inode permission operations in /proc is almost
101 * certainly an error. Permission checks need to happen during
102 * each system call not at open time. The reason is that most of
103 * what we wish to check for permissions in /proc varies at runtime.
105 * The classic example of a problem is opening file descriptors
106 * in /proc for a task before it execs a suid executable.
113 const struct inode_operations *iop;
114 const struct file_operations *fop;
118 #define NOD(NAME, MODE, IOP, FOP, OP) { \
120 .len = sizeof(NAME) - 1, \
127 #define DIR(NAME, MODE, iops, fops) \
128 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
129 #define LNK(NAME, get_link) \
130 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
131 &proc_pid_link_inode_operations, NULL, \
132 { .proc_get_link = get_link } )
133 #define REG(NAME, MODE, fops) \
134 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
135 #define ONE(NAME, MODE, show) \
136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
138 { .proc_show = show } )
141 * Count the number of hardlinks for the pid_entry table, excluding the .
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
159 static int get_task_root(struct task_struct *task, struct path *root)
161 int result = -ENOENT;
165 get_fs_root(task->fs, root);
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
174 struct task_struct *task = get_proc_task(d_inode(dentry));
175 int result = -ENOENT;
180 get_fs_pwd(task->fs, path);
184 put_task_struct(task);
189 static int proc_root_link(struct dentry *dentry, struct path *path)
191 struct task_struct *task = get_proc_task(d_inode(dentry));
192 int result = -ENOENT;
195 result = get_task_root(task, path);
196 put_task_struct(task);
201 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
202 size_t _count, loff_t *pos)
204 struct task_struct *tsk;
205 struct mm_struct *mm;
207 unsigned long count = _count;
208 unsigned long arg_start, arg_end, env_start, env_end;
209 unsigned long len1, len2, len;
216 tsk = get_proc_task(file_inode(file));
219 mm = get_task_mm(tsk);
220 put_task_struct(tsk);
223 /* Check if process spawned far enough to have cmdline. */
229 page = (char *)__get_free_page(GFP_TEMPORARY);
235 down_read(&mm->mmap_sem);
236 arg_start = mm->arg_start;
237 arg_end = mm->arg_end;
238 env_start = mm->env_start;
239 env_end = mm->env_end;
240 up_read(&mm->mmap_sem);
242 BUG_ON(arg_start > arg_end);
243 BUG_ON(env_start > env_end);
245 len1 = arg_end - arg_start;
246 len2 = env_end - env_start;
254 * Inherently racy -- command line shares address space
255 * with code and data.
257 rv = access_remote_vm(mm, arg_end - 1, &c, 1, FOLL_ANON);
264 /* Command line (set of strings) occupies whole ARGV. */
268 p = arg_start + *pos;
270 while (count > 0 && len > 0) {
274 _count = min3(count, len, PAGE_SIZE);
275 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
281 if (copy_to_user(buf, page, nr_read)) {
294 * Command line (1 string) occupies ARGV and maybe
297 if (len1 + len2 <= *pos)
302 p = arg_start + *pos;
304 while (count > 0 && len > 0) {
305 unsigned int _count, l;
309 _count = min3(count, len, PAGE_SIZE);
310 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
317 * Command line can be shorter than whole ARGV
318 * even if last "marker" byte says it is not.
321 l = strnlen(page, nr_read);
327 if (copy_to_user(buf, page, nr_read)) {
343 * Command line (1 string) occupies ARGV and
347 p = env_start + *pos - len1;
348 len = len1 + len2 - *pos;
353 while (count > 0 && len > 0) {
354 unsigned int _count, l;
358 _count = min3(count, len, PAGE_SIZE);
359 nr_read = access_remote_vm(mm, p, page, _count, FOLL_ANON);
367 l = strnlen(page, nr_read);
373 if (copy_to_user(buf, page, nr_read)) {
392 free_page((unsigned long)page);
400 static const struct file_operations proc_pid_cmdline_ops = {
401 .read = proc_pid_cmdline_read,
402 .llseek = generic_file_llseek,
405 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
406 struct pid *pid, struct task_struct *task)
408 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
409 if (mm && !IS_ERR(mm)) {
410 unsigned int nwords = 0;
413 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
414 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
422 #ifdef CONFIG_KALLSYMS
424 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
425 * Returns the resolved symbol. If that fails, simply return the address.
427 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
428 struct pid *pid, struct task_struct *task)
431 char symname[KSYM_NAME_LEN];
433 wchan = get_wchan(task);
435 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)
436 && !lookup_symbol_name(wchan, symname))
437 seq_printf(m, "%s", symname);
443 #endif /* CONFIG_KALLSYMS */
445 static int lock_trace(struct task_struct *task)
447 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
450 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
451 mutex_unlock(&task->signal->cred_guard_mutex);
457 static void unlock_trace(struct task_struct *task)
459 mutex_unlock(&task->signal->cred_guard_mutex);
462 #ifdef CONFIG_STACKTRACE
464 #define MAX_STACK_TRACE_DEPTH 64
466 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
467 struct pid *pid, struct task_struct *task)
469 struct stack_trace trace;
470 unsigned long *entries;
475 * The ability to racily run the kernel stack unwinder on a running task
476 * and then observe the unwinder output is scary; while it is useful for
477 * debugging kernel issues, it can also allow an attacker to leak kernel
479 * Doing this in a manner that is at least safe from races would require
480 * some work to ensure that the remote task can not be scheduled; and
481 * even then, this would still expose the unwinder as local attack
483 * Therefore, this interface is restricted to root.
485 if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
488 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
492 trace.nr_entries = 0;
493 trace.max_entries = MAX_STACK_TRACE_DEPTH;
494 trace.entries = entries;
497 err = lock_trace(task);
499 save_stack_trace_tsk(task, &trace);
501 for (i = 0; i < trace.nr_entries; i++) {
502 seq_printf(m, "[<%pK>] %pS\n",
503 (void *)entries[i], (void *)entries[i]);
513 #ifdef CONFIG_SCHED_INFO
515 * Provides /proc/PID/schedstat
517 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
518 struct pid *pid, struct task_struct *task)
520 if (unlikely(!sched_info_on()))
521 seq_printf(m, "0 0 0\n");
523 seq_printf(m, "%llu %llu %lu\n",
524 (unsigned long long)task->se.sum_exec_runtime,
525 (unsigned long long)task->sched_info.run_delay,
526 task->sched_info.pcount);
532 #ifdef CONFIG_LATENCYTOP
533 static int lstats_show_proc(struct seq_file *m, void *v)
536 struct inode *inode = m->private;
537 struct task_struct *task = get_proc_task(inode);
541 seq_puts(m, "Latency Top version : v0.1\n");
542 for (i = 0; i < 32; i++) {
543 struct latency_record *lr = &task->latency_record[i];
544 if (lr->backtrace[0]) {
546 seq_printf(m, "%i %li %li",
547 lr->count, lr->time, lr->max);
548 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
549 unsigned long bt = lr->backtrace[q];
554 seq_printf(m, " %ps", (void *)bt);
560 put_task_struct(task);
564 static int lstats_open(struct inode *inode, struct file *file)
566 return single_open(file, lstats_show_proc, inode);
569 static ssize_t lstats_write(struct file *file, const char __user *buf,
570 size_t count, loff_t *offs)
572 struct task_struct *task = get_proc_task(file_inode(file));
576 clear_all_latency_tracing(task);
577 put_task_struct(task);
582 static const struct file_operations proc_lstats_operations = {
585 .write = lstats_write,
587 .release = single_release,
592 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
593 struct pid *pid, struct task_struct *task)
595 unsigned long totalpages = totalram_pages + total_swap_pages;
596 unsigned long points = 0;
598 read_lock(&tasklist_lock);
600 points = oom_badness(task, NULL, NULL, totalpages) *
602 read_unlock(&tasklist_lock);
603 seq_printf(m, "%lu\n", points);
613 static const struct limit_names lnames[RLIM_NLIMITS] = {
614 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
615 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
616 [RLIMIT_DATA] = {"Max data size", "bytes"},
617 [RLIMIT_STACK] = {"Max stack size", "bytes"},
618 [RLIMIT_CORE] = {"Max core file size", "bytes"},
619 [RLIMIT_RSS] = {"Max resident set", "bytes"},
620 [RLIMIT_NPROC] = {"Max processes", "processes"},
621 [RLIMIT_NOFILE] = {"Max open files", "files"},
622 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
623 [RLIMIT_AS] = {"Max address space", "bytes"},
624 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
625 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
626 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
627 [RLIMIT_NICE] = {"Max nice priority", NULL},
628 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
629 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
632 /* Display limits for a process */
633 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
634 struct pid *pid, struct task_struct *task)
639 struct rlimit rlim[RLIM_NLIMITS];
641 if (!lock_task_sighand(task, &flags))
643 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
644 unlock_task_sighand(task, &flags);
647 * print the file header
649 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
650 "Limit", "Soft Limit", "Hard Limit", "Units");
652 for (i = 0; i < RLIM_NLIMITS; i++) {
653 if (rlim[i].rlim_cur == RLIM_INFINITY)
654 seq_printf(m, "%-25s %-20s ",
655 lnames[i].name, "unlimited");
657 seq_printf(m, "%-25s %-20lu ",
658 lnames[i].name, rlim[i].rlim_cur);
660 if (rlim[i].rlim_max == RLIM_INFINITY)
661 seq_printf(m, "%-20s ", "unlimited");
663 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
666 seq_printf(m, "%-10s\n", lnames[i].unit);
674 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
675 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
676 struct pid *pid, struct task_struct *task)
679 unsigned long args[6], sp, pc;
682 res = lock_trace(task);
686 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
687 seq_puts(m, "running\n");
689 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
692 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
694 args[0], args[1], args[2], args[3], args[4], args[5],
700 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
702 /************************************************************************/
703 /* Here the fs part begins */
704 /************************************************************************/
706 /* permission checks */
707 static int proc_fd_access_allowed(struct inode *inode)
709 struct task_struct *task;
711 /* Allow access to a task's file descriptors if it is us or we
712 * may use ptrace attach to the process and find out that
715 task = get_proc_task(inode);
717 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
718 put_task_struct(task);
723 int proc_setattr(struct dentry *dentry, struct iattr *attr)
726 struct inode *inode = d_inode(dentry);
728 if (attr->ia_valid & ATTR_MODE)
731 error = inode_change_ok(inode, attr);
735 setattr_copy(inode, attr);
736 mark_inode_dirty(inode);
741 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
742 * or euid/egid (for hide_pid_min=2)?
744 static bool has_pid_permissions(struct pid_namespace *pid,
745 struct task_struct *task,
748 if (pid->hide_pid < hide_pid_min)
750 if (in_group_p(pid->pid_gid))
752 return ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
756 static int proc_pid_permission(struct inode *inode, int mask)
758 struct pid_namespace *pid = inode->i_sb->s_fs_info;
759 struct task_struct *task;
762 task = get_proc_task(inode);
765 has_perms = has_pid_permissions(pid, task, 1);
766 put_task_struct(task);
769 if (pid->hide_pid == 2) {
771 * Let's make getdents(), stat(), and open()
772 * consistent with each other. If a process
773 * may not stat() a file, it shouldn't be seen
781 return generic_permission(inode, mask);
786 static const struct inode_operations proc_def_inode_operations = {
787 .setattr = proc_setattr,
790 static int proc_single_show(struct seq_file *m, void *v)
792 struct inode *inode = m->private;
793 struct pid_namespace *ns;
795 struct task_struct *task;
798 ns = inode->i_sb->s_fs_info;
799 pid = proc_pid(inode);
800 task = get_pid_task(pid, PIDTYPE_PID);
804 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
806 put_task_struct(task);
810 static int proc_single_open(struct inode *inode, struct file *filp)
812 return single_open(filp, proc_single_show, inode);
815 static const struct file_operations proc_single_file_operations = {
816 .open = proc_single_open,
819 .release = single_release,
823 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
825 struct task_struct *task = get_proc_task(inode);
826 struct mm_struct *mm = ERR_PTR(-ESRCH);
829 mm = mm_access(task, mode | PTRACE_MODE_FSCREDS);
830 put_task_struct(task);
832 if (!IS_ERR_OR_NULL(mm)) {
833 /* ensure this mm_struct can't be freed */
834 atomic_inc(&mm->mm_count);
835 /* but do not pin its memory */
843 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
845 struct mm_struct *mm = proc_mem_open(inode, mode);
850 file->private_data = mm;
854 static int mem_open(struct inode *inode, struct file *file)
856 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
858 /* OK to pass negative loff_t, we can catch out-of-range */
859 file->f_mode |= FMODE_UNSIGNED_OFFSET;
864 static ssize_t mem_rw(struct file *file, char __user *buf,
865 size_t count, loff_t *ppos, int write)
867 struct mm_struct *mm = file->private_data;
868 unsigned long addr = *ppos;
876 page = (char *)__get_free_page(GFP_TEMPORARY);
881 if (!atomic_inc_not_zero(&mm->mm_users))
884 /* Maybe we should limit FOLL_FORCE to actual ptrace users? */
890 size_t this_len = min_t(size_t, count, PAGE_SIZE);
892 if (write && copy_from_user(page, buf, this_len)) {
897 this_len = access_remote_vm(mm, addr, page, this_len, flags);
904 if (!write && copy_to_user(buf, page, this_len)) {
918 free_page((unsigned long) page);
922 static ssize_t mem_read(struct file *file, char __user *buf,
923 size_t count, loff_t *ppos)
925 return mem_rw(file, buf, count, ppos, 0);
928 static ssize_t mem_write(struct file *file, const char __user *buf,
929 size_t count, loff_t *ppos)
931 return mem_rw(file, (char __user*)buf, count, ppos, 1);
934 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
938 file->f_pos = offset;
941 file->f_pos += offset;
946 force_successful_syscall_return();
950 static int mem_release(struct inode *inode, struct file *file)
952 struct mm_struct *mm = file->private_data;
958 static const struct file_operations proc_mem_operations = {
963 .release = mem_release,
966 static int environ_open(struct inode *inode, struct file *file)
968 return __mem_open(inode, file, PTRACE_MODE_READ);
971 static ssize_t environ_read(struct file *file, char __user *buf,
972 size_t count, loff_t *ppos)
975 unsigned long src = *ppos;
977 struct mm_struct *mm = file->private_data;
978 unsigned long env_start, env_end;
980 /* Ensure the process spawned far enough to have an environment. */
981 if (!mm || !mm->env_end)
984 page = (char *)__get_free_page(GFP_TEMPORARY);
989 if (!atomic_inc_not_zero(&mm->mm_users))
992 down_read(&mm->mmap_sem);
993 env_start = mm->env_start;
994 env_end = mm->env_end;
995 up_read(&mm->mmap_sem);
998 size_t this_len, max_len;
1001 if (src >= (env_end - env_start))
1004 this_len = env_end - (env_start + src);
1006 max_len = min_t(size_t, PAGE_SIZE, count);
1007 this_len = min(max_len, this_len);
1009 retval = access_remote_vm(mm, (env_start + src), page, this_len, FOLL_ANON);
1016 if (copy_to_user(buf, page, retval)) {
1030 free_page((unsigned long) page);
1034 static const struct file_operations proc_environ_operations = {
1035 .open = environ_open,
1036 .read = environ_read,
1037 .llseek = generic_file_llseek,
1038 .release = mem_release,
1041 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1044 struct task_struct *task = get_proc_task(file_inode(file));
1045 char buffer[PROC_NUMBUF];
1046 int oom_adj = OOM_ADJUST_MIN;
1048 unsigned long flags;
1052 if (lock_task_sighand(task, &flags)) {
1053 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1054 oom_adj = OOM_ADJUST_MAX;
1056 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1058 unlock_task_sighand(task, &flags);
1060 put_task_struct(task);
1061 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1062 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1066 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1067 * kernels. The effective policy is defined by oom_score_adj, which has a
1068 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1069 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1070 * Processes that become oom disabled via oom_adj will still be oom disabled
1071 * with this implementation.
1073 * oom_adj cannot be removed since existing userspace binaries use it.
1075 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1076 size_t count, loff_t *ppos)
1078 struct task_struct *task;
1079 char buffer[PROC_NUMBUF];
1081 unsigned long flags;
1084 memset(buffer, 0, sizeof(buffer));
1085 if (count > sizeof(buffer) - 1)
1086 count = sizeof(buffer) - 1;
1087 if (copy_from_user(buffer, buf, count)) {
1092 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1095 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1096 oom_adj != OOM_DISABLE) {
1101 task = get_proc_task(file_inode(file));
1113 if (!lock_task_sighand(task, &flags)) {
1119 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1120 * value is always attainable.
1122 if (oom_adj == OOM_ADJUST_MAX)
1123 oom_adj = OOM_SCORE_ADJ_MAX;
1125 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1127 if (oom_adj < task->signal->oom_score_adj &&
1128 !capable(CAP_SYS_RESOURCE)) {
1134 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1135 * /proc/pid/oom_score_adj instead.
1137 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1138 current->comm, task_pid_nr(current), task_pid_nr(task),
1141 task->signal->oom_score_adj = oom_adj;
1142 trace_oom_score_adj_update(task);
1144 unlock_task_sighand(task, &flags);
1147 put_task_struct(task);
1149 return err < 0 ? err : count;
1152 static const struct file_operations proc_oom_adj_operations = {
1153 .read = oom_adj_read,
1154 .write = oom_adj_write,
1155 .llseek = generic_file_llseek,
1158 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1159 size_t count, loff_t *ppos)
1161 struct task_struct *task = get_proc_task(file_inode(file));
1162 char buffer[PROC_NUMBUF];
1163 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1164 unsigned long flags;
1169 if (lock_task_sighand(task, &flags)) {
1170 oom_score_adj = task->signal->oom_score_adj;
1171 unlock_task_sighand(task, &flags);
1173 put_task_struct(task);
1174 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1175 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1178 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1179 size_t count, loff_t *ppos)
1181 struct task_struct *task;
1182 char buffer[PROC_NUMBUF];
1183 unsigned long flags;
1187 memset(buffer, 0, sizeof(buffer));
1188 if (count > sizeof(buffer) - 1)
1189 count = sizeof(buffer) - 1;
1190 if (copy_from_user(buffer, buf, count)) {
1195 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1198 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1199 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1204 task = get_proc_task(file_inode(file));
1216 if (!lock_task_sighand(task, &flags)) {
1221 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1222 !capable(CAP_SYS_RESOURCE)) {
1227 task->signal->oom_score_adj = (short)oom_score_adj;
1228 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1229 task->signal->oom_score_adj_min = (short)oom_score_adj;
1230 trace_oom_score_adj_update(task);
1233 unlock_task_sighand(task, &flags);
1236 put_task_struct(task);
1238 return err < 0 ? err : count;
1241 static const struct file_operations proc_oom_score_adj_operations = {
1242 .read = oom_score_adj_read,
1243 .write = oom_score_adj_write,
1244 .llseek = default_llseek,
1247 #ifdef CONFIG_AUDITSYSCALL
1248 #define TMPBUFLEN 21
1249 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1250 size_t count, loff_t *ppos)
1252 struct inode * inode = file_inode(file);
1253 struct task_struct *task = get_proc_task(inode);
1255 char tmpbuf[TMPBUFLEN];
1259 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1260 from_kuid(file->f_cred->user_ns,
1261 audit_get_loginuid(task)));
1262 put_task_struct(task);
1263 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1266 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1267 size_t count, loff_t *ppos)
1269 struct inode * inode = file_inode(file);
1275 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1282 /* No partial writes. */
1286 rv = kstrtou32_from_user(buf, count, 10, &loginuid);
1290 /* is userspace tring to explicitly UNSET the loginuid? */
1291 if (loginuid == AUDIT_UID_UNSET) {
1292 kloginuid = INVALID_UID;
1294 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1295 if (!uid_valid(kloginuid))
1299 rv = audit_set_loginuid(kloginuid);
1305 static const struct file_operations proc_loginuid_operations = {
1306 .read = proc_loginuid_read,
1307 .write = proc_loginuid_write,
1308 .llseek = generic_file_llseek,
1311 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1312 size_t count, loff_t *ppos)
1314 struct inode * inode = file_inode(file);
1315 struct task_struct *task = get_proc_task(inode);
1317 char tmpbuf[TMPBUFLEN];
1321 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1322 audit_get_sessionid(task));
1323 put_task_struct(task);
1324 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1327 static const struct file_operations proc_sessionid_operations = {
1328 .read = proc_sessionid_read,
1329 .llseek = generic_file_llseek,
1333 #ifdef CONFIG_FAULT_INJECTION
1334 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1335 size_t count, loff_t *ppos)
1337 struct task_struct *task = get_proc_task(file_inode(file));
1338 char buffer[PROC_NUMBUF];
1344 make_it_fail = task->make_it_fail;
1345 put_task_struct(task);
1347 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1349 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1352 static ssize_t proc_fault_inject_write(struct file * file,
1353 const char __user * buf, size_t count, loff_t *ppos)
1355 struct task_struct *task;
1356 char buffer[PROC_NUMBUF];
1360 if (!capable(CAP_SYS_RESOURCE))
1362 memset(buffer, 0, sizeof(buffer));
1363 if (count > sizeof(buffer) - 1)
1364 count = sizeof(buffer) - 1;
1365 if (copy_from_user(buffer, buf, count))
1367 rv = kstrtoint(strstrip(buffer), 0, &make_it_fail);
1370 if (make_it_fail < 0 || make_it_fail > 1)
1373 task = get_proc_task(file_inode(file));
1376 task->make_it_fail = make_it_fail;
1377 put_task_struct(task);
1382 static const struct file_operations proc_fault_inject_operations = {
1383 .read = proc_fault_inject_read,
1384 .write = proc_fault_inject_write,
1385 .llseek = generic_file_llseek,
1390 #ifdef CONFIG_SCHED_DEBUG
1392 * Print out various scheduling related per-task fields:
1394 static int sched_show(struct seq_file *m, void *v)
1396 struct inode *inode = m->private;
1397 struct task_struct *p;
1399 p = get_proc_task(inode);
1402 proc_sched_show_task(p, m);
1410 sched_write(struct file *file, const char __user *buf,
1411 size_t count, loff_t *offset)
1413 struct inode *inode = file_inode(file);
1414 struct task_struct *p;
1416 p = get_proc_task(inode);
1419 proc_sched_set_task(p);
1426 static int sched_open(struct inode *inode, struct file *filp)
1428 return single_open(filp, sched_show, inode);
1431 static const struct file_operations proc_pid_sched_operations = {
1434 .write = sched_write,
1435 .llseek = seq_lseek,
1436 .release = single_release,
1441 #ifdef CONFIG_SCHED_AUTOGROUP
1443 * Print out autogroup related information:
1445 static int sched_autogroup_show(struct seq_file *m, void *v)
1447 struct inode *inode = m->private;
1448 struct task_struct *p;
1450 p = get_proc_task(inode);
1453 proc_sched_autogroup_show_task(p, m);
1461 sched_autogroup_write(struct file *file, const char __user *buf,
1462 size_t count, loff_t *offset)
1464 struct inode *inode = file_inode(file);
1465 struct task_struct *p;
1466 char buffer[PROC_NUMBUF];
1470 memset(buffer, 0, sizeof(buffer));
1471 if (count > sizeof(buffer) - 1)
1472 count = sizeof(buffer) - 1;
1473 if (copy_from_user(buffer, buf, count))
1476 err = kstrtoint(strstrip(buffer), 0, &nice);
1480 p = get_proc_task(inode);
1484 err = proc_sched_autogroup_set_nice(p, nice);
1493 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1497 ret = single_open(filp, sched_autogroup_show, NULL);
1499 struct seq_file *m = filp->private_data;
1506 static const struct file_operations proc_pid_sched_autogroup_operations = {
1507 .open = sched_autogroup_open,
1509 .write = sched_autogroup_write,
1510 .llseek = seq_lseek,
1511 .release = single_release,
1514 #endif /* CONFIG_SCHED_AUTOGROUP */
1516 static ssize_t comm_write(struct file *file, const char __user *buf,
1517 size_t count, loff_t *offset)
1519 struct inode *inode = file_inode(file);
1520 struct task_struct *p;
1521 char buffer[TASK_COMM_LEN];
1522 const size_t maxlen = sizeof(buffer) - 1;
1524 memset(buffer, 0, sizeof(buffer));
1525 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1528 p = get_proc_task(inode);
1532 if (same_thread_group(current, p))
1533 set_task_comm(p, buffer);
1542 static int comm_show(struct seq_file *m, void *v)
1544 struct inode *inode = m->private;
1545 struct task_struct *p;
1547 p = get_proc_task(inode);
1552 seq_printf(m, "%s\n", p->comm);
1560 static int comm_open(struct inode *inode, struct file *filp)
1562 return single_open(filp, comm_show, inode);
1565 static const struct file_operations proc_pid_set_comm_operations = {
1568 .write = comm_write,
1569 .llseek = seq_lseek,
1570 .release = single_release,
1573 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1575 struct task_struct *task;
1576 struct file *exe_file;
1578 task = get_proc_task(d_inode(dentry));
1581 exe_file = get_task_exe_file(task);
1582 put_task_struct(task);
1584 *exe_path = exe_file->f_path;
1585 path_get(&exe_file->f_path);
1592 static const char *proc_pid_follow_link(struct dentry *dentry, void **cookie)
1594 struct inode *inode = d_inode(dentry);
1596 int error = -EACCES;
1598 /* Are we allowed to snoop on the tasks file descriptors? */
1599 if (!proc_fd_access_allowed(inode))
1602 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1606 nd_jump_link(&path);
1609 return ERR_PTR(error);
1612 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1614 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1621 pathname = d_path(path, tmp, PAGE_SIZE);
1622 len = PTR_ERR(pathname);
1623 if (IS_ERR(pathname))
1625 len = tmp + PAGE_SIZE - 1 - pathname;
1629 if (copy_to_user(buffer, pathname, len))
1632 free_page((unsigned long)tmp);
1636 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1638 int error = -EACCES;
1639 struct inode *inode = d_inode(dentry);
1642 /* Are we allowed to snoop on the tasks file descriptors? */
1643 if (!proc_fd_access_allowed(inode))
1646 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1650 error = do_proc_readlink(&path, buffer, buflen);
1656 const struct inode_operations proc_pid_link_inode_operations = {
1657 .readlink = proc_pid_readlink,
1658 .follow_link = proc_pid_follow_link,
1659 .setattr = proc_setattr,
1663 /* building an inode */
1665 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1667 struct inode * inode;
1668 struct proc_inode *ei;
1669 const struct cred *cred;
1671 /* We need a new inode */
1673 inode = new_inode(sb);
1679 inode->i_ino = get_next_ino();
1680 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1681 inode->i_op = &proc_def_inode_operations;
1684 * grab the reference to task.
1686 ei->pid = get_task_pid(task, PIDTYPE_PID);
1690 if (task_dumpable(task)) {
1692 cred = __task_cred(task);
1693 inode->i_uid = cred->euid;
1694 inode->i_gid = cred->egid;
1697 security_task_to_inode(task, inode);
1707 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1709 struct inode *inode = d_inode(dentry);
1710 struct task_struct *task;
1711 const struct cred *cred;
1712 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1714 generic_fillattr(inode, stat);
1717 stat->uid = GLOBAL_ROOT_UID;
1718 stat->gid = GLOBAL_ROOT_GID;
1719 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1721 if (!has_pid_permissions(pid, task, 2)) {
1724 * This doesn't prevent learning whether PID exists,
1725 * it only makes getattr() consistent with readdir().
1729 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1730 task_dumpable(task)) {
1731 cred = __task_cred(task);
1732 stat->uid = cred->euid;
1733 stat->gid = cred->egid;
1743 * Exceptional case: normally we are not allowed to unhash a busy
1744 * directory. In this case, however, we can do it - no aliasing problems
1745 * due to the way we treat inodes.
1747 * Rewrite the inode's ownerships here because the owning task may have
1748 * performed a setuid(), etc.
1750 * Before the /proc/pid/status file was created the only way to read
1751 * the effective uid of a /process was to stat /proc/pid. Reading
1752 * /proc/pid/status is slow enough that procps and other packages
1753 * kept stating /proc/pid. To keep the rules in /proc simple I have
1754 * made this apply to all per process world readable and executable
1757 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1759 struct inode *inode;
1760 struct task_struct *task;
1761 const struct cred *cred;
1763 if (flags & LOOKUP_RCU)
1766 inode = d_inode(dentry);
1767 task = get_proc_task(inode);
1770 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1771 task_dumpable(task)) {
1773 cred = __task_cred(task);
1774 inode->i_uid = cred->euid;
1775 inode->i_gid = cred->egid;
1778 inode->i_uid = GLOBAL_ROOT_UID;
1779 inode->i_gid = GLOBAL_ROOT_GID;
1781 inode->i_mode &= ~(S_ISUID | S_ISGID);
1782 security_task_to_inode(task, inode);
1783 put_task_struct(task);
1789 static inline bool proc_inode_is_dead(struct inode *inode)
1791 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1794 int pid_delete_dentry(const struct dentry *dentry)
1796 /* Is the task we represent dead?
1797 * If so, then don't put the dentry on the lru list,
1798 * kill it immediately.
1800 return proc_inode_is_dead(d_inode(dentry));
1803 const struct dentry_operations pid_dentry_operations =
1805 .d_revalidate = pid_revalidate,
1806 .d_delete = pid_delete_dentry,
1812 * Fill a directory entry.
1814 * If possible create the dcache entry and derive our inode number and
1815 * file type from dcache entry.
1817 * Since all of the proc inode numbers are dynamically generated, the inode
1818 * numbers do not exist until the inode is cache. This means creating the
1819 * the dcache entry in readdir is necessary to keep the inode numbers
1820 * reported by readdir in sync with the inode numbers reported
1823 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1824 const char *name, int len,
1825 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1827 struct dentry *child, *dir = file->f_path.dentry;
1828 struct qstr qname = QSTR_INIT(name, len);
1829 struct inode *inode;
1833 child = d_hash_and_lookup(dir, &qname);
1835 child = d_alloc(dir, &qname);
1837 goto end_instantiate;
1838 if (instantiate(d_inode(dir), child, task, ptr) < 0) {
1840 goto end_instantiate;
1843 inode = d_inode(child);
1845 type = inode->i_mode >> 12;
1847 return dir_emit(ctx, name, len, ino, type);
1850 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1854 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1855 * which represent vma start and end addresses.
1857 static int dname_to_vma_addr(struct dentry *dentry,
1858 unsigned long *start, unsigned long *end)
1860 const char *str = dentry->d_name.name;
1861 unsigned long long sval, eval;
1864 len = _parse_integer(str, 16, &sval);
1865 if (len & KSTRTOX_OVERFLOW)
1867 if (sval != (unsigned long)sval)
1875 len = _parse_integer(str, 16, &eval);
1876 if (len & KSTRTOX_OVERFLOW)
1878 if (eval != (unsigned long)eval)
1891 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1893 unsigned long vm_start, vm_end;
1894 bool exact_vma_exists = false;
1895 struct mm_struct *mm = NULL;
1896 struct task_struct *task;
1897 const struct cred *cred;
1898 struct inode *inode;
1901 if (flags & LOOKUP_RCU)
1904 inode = d_inode(dentry);
1905 task = get_proc_task(inode);
1909 mm = mm_access(task, PTRACE_MODE_READ_FSCREDS);
1910 if (IS_ERR_OR_NULL(mm))
1913 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1914 down_read(&mm->mmap_sem);
1915 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1916 up_read(&mm->mmap_sem);
1921 if (exact_vma_exists) {
1922 if (task_dumpable(task)) {
1924 cred = __task_cred(task);
1925 inode->i_uid = cred->euid;
1926 inode->i_gid = cred->egid;
1929 inode->i_uid = GLOBAL_ROOT_UID;
1930 inode->i_gid = GLOBAL_ROOT_GID;
1932 security_task_to_inode(task, inode);
1937 put_task_struct(task);
1943 static const struct dentry_operations tid_map_files_dentry_operations = {
1944 .d_revalidate = map_files_d_revalidate,
1945 .d_delete = pid_delete_dentry,
1948 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1950 unsigned long vm_start, vm_end;
1951 struct vm_area_struct *vma;
1952 struct task_struct *task;
1953 struct mm_struct *mm;
1957 task = get_proc_task(d_inode(dentry));
1961 mm = get_task_mm(task);
1962 put_task_struct(task);
1966 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1971 down_read(&mm->mmap_sem);
1972 vma = find_exact_vma(mm, vm_start, vm_end);
1973 if (vma && vma->vm_file) {
1974 *path = vma->vm_file->f_path;
1978 up_read(&mm->mmap_sem);
1986 struct map_files_info {
1989 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1993 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1994 * symlinks may be used to bypass permissions on ancestor directories in the
1995 * path to the file in question.
1998 proc_map_files_follow_link(struct dentry *dentry, void **cookie)
2000 if (!capable(CAP_SYS_ADMIN))
2001 return ERR_PTR(-EPERM);
2003 return proc_pid_follow_link(dentry, NULL);
2007 * Identical to proc_pid_link_inode_operations except for follow_link()
2009 static const struct inode_operations proc_map_files_link_inode_operations = {
2010 .readlink = proc_pid_readlink,
2011 .follow_link = proc_map_files_follow_link,
2012 .setattr = proc_setattr,
2016 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2017 struct task_struct *task, const void *ptr)
2019 fmode_t mode = (fmode_t)(unsigned long)ptr;
2020 struct proc_inode *ei;
2021 struct inode *inode;
2023 inode = proc_pid_make_inode(dir->i_sb, task);
2028 ei->op.proc_get_link = proc_map_files_get_link;
2030 inode->i_op = &proc_map_files_link_inode_operations;
2032 inode->i_mode = S_IFLNK;
2034 if (mode & FMODE_READ)
2035 inode->i_mode |= S_IRUSR;
2036 if (mode & FMODE_WRITE)
2037 inode->i_mode |= S_IWUSR;
2039 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2040 d_add(dentry, inode);
2045 static struct dentry *proc_map_files_lookup(struct inode *dir,
2046 struct dentry *dentry, unsigned int flags)
2048 unsigned long vm_start, vm_end;
2049 struct vm_area_struct *vma;
2050 struct task_struct *task;
2052 struct mm_struct *mm;
2055 task = get_proc_task(dir);
2060 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2064 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2067 mm = get_task_mm(task);
2071 down_read(&mm->mmap_sem);
2072 vma = find_exact_vma(mm, vm_start, vm_end);
2077 result = proc_map_files_instantiate(dir, dentry, task,
2078 (void *)(unsigned long)vma->vm_file->f_mode);
2081 up_read(&mm->mmap_sem);
2084 put_task_struct(task);
2086 return ERR_PTR(result);
2089 static const struct inode_operations proc_map_files_inode_operations = {
2090 .lookup = proc_map_files_lookup,
2091 .permission = proc_fd_permission,
2092 .setattr = proc_setattr,
2096 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2098 struct vm_area_struct *vma;
2099 struct task_struct *task;
2100 struct mm_struct *mm;
2101 unsigned long nr_files, pos, i;
2102 struct flex_array *fa = NULL;
2103 struct map_files_info info;
2104 struct map_files_info *p;
2108 task = get_proc_task(file_inode(file));
2113 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
2117 if (!dir_emit_dots(file, ctx))
2120 mm = get_task_mm(task);
2123 down_read(&mm->mmap_sem);
2128 * We need two passes here:
2130 * 1) Collect vmas of mapped files with mmap_sem taken
2131 * 2) Release mmap_sem and instantiate entries
2133 * otherwise we get lockdep complained, since filldir()
2134 * routine might require mmap_sem taken in might_fault().
2137 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2138 if (vma->vm_file && ++pos > ctx->pos)
2143 fa = flex_array_alloc(sizeof(info), nr_files,
2145 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2149 flex_array_free(fa);
2150 up_read(&mm->mmap_sem);
2154 for (i = 0, vma = mm->mmap, pos = 2; vma;
2155 vma = vma->vm_next) {
2158 if (++pos <= ctx->pos)
2161 info.mode = vma->vm_file->f_mode;
2162 info.len = snprintf(info.name,
2163 sizeof(info.name), "%lx-%lx",
2164 vma->vm_start, vma->vm_end);
2165 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2169 up_read(&mm->mmap_sem);
2171 for (i = 0; i < nr_files; i++) {
2172 p = flex_array_get(fa, i);
2173 if (!proc_fill_cache(file, ctx,
2175 proc_map_files_instantiate,
2177 (void *)(unsigned long)p->mode))
2182 flex_array_free(fa);
2186 put_task_struct(task);
2191 static const struct file_operations proc_map_files_operations = {
2192 .read = generic_read_dir,
2193 .iterate = proc_map_files_readdir,
2194 .llseek = default_llseek,
2197 struct timers_private {
2199 struct task_struct *task;
2200 struct sighand_struct *sighand;
2201 struct pid_namespace *ns;
2202 unsigned long flags;
2205 static void *timers_start(struct seq_file *m, loff_t *pos)
2207 struct timers_private *tp = m->private;
2209 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2211 return ERR_PTR(-ESRCH);
2213 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2215 return ERR_PTR(-ESRCH);
2217 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2220 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2222 struct timers_private *tp = m->private;
2223 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2226 static void timers_stop(struct seq_file *m, void *v)
2228 struct timers_private *tp = m->private;
2231 unlock_task_sighand(tp->task, &tp->flags);
2236 put_task_struct(tp->task);
2241 static int show_timer(struct seq_file *m, void *v)
2243 struct k_itimer *timer;
2244 struct timers_private *tp = m->private;
2246 static const char * const nstr[] = {
2247 [SIGEV_SIGNAL] = "signal",
2248 [SIGEV_NONE] = "none",
2249 [SIGEV_THREAD] = "thread",
2252 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2253 notify = timer->it_sigev_notify;
2255 seq_printf(m, "ID: %d\n", timer->it_id);
2256 seq_printf(m, "signal: %d/%p\n",
2257 timer->sigq->info.si_signo,
2258 timer->sigq->info.si_value.sival_ptr);
2259 seq_printf(m, "notify: %s/%s.%d\n",
2260 nstr[notify & ~SIGEV_THREAD_ID],
2261 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2262 pid_nr_ns(timer->it_pid, tp->ns));
2263 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2268 static const struct seq_operations proc_timers_seq_ops = {
2269 .start = timers_start,
2270 .next = timers_next,
2271 .stop = timers_stop,
2275 static int proc_timers_open(struct inode *inode, struct file *file)
2277 struct timers_private *tp;
2279 tp = __seq_open_private(file, &proc_timers_seq_ops,
2280 sizeof(struct timers_private));
2284 tp->pid = proc_pid(inode);
2285 tp->ns = inode->i_sb->s_fs_info;
2289 static const struct file_operations proc_timers_operations = {
2290 .open = proc_timers_open,
2292 .llseek = seq_lseek,
2293 .release = seq_release_private,
2296 static int proc_pident_instantiate(struct inode *dir,
2297 struct dentry *dentry, struct task_struct *task, const void *ptr)
2299 const struct pid_entry *p = ptr;
2300 struct inode *inode;
2301 struct proc_inode *ei;
2303 inode = proc_pid_make_inode(dir->i_sb, task);
2308 inode->i_mode = p->mode;
2309 if (S_ISDIR(inode->i_mode))
2310 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2312 inode->i_op = p->iop;
2314 inode->i_fop = p->fop;
2316 d_set_d_op(dentry, &pid_dentry_operations);
2317 d_add(dentry, inode);
2318 /* Close the race of the process dying before we return the dentry */
2319 if (pid_revalidate(dentry, 0))
2325 static struct dentry *proc_pident_lookup(struct inode *dir,
2326 struct dentry *dentry,
2327 const struct pid_entry *ents,
2331 struct task_struct *task = get_proc_task(dir);
2332 const struct pid_entry *p, *last;
2340 * Yes, it does not scale. And it should not. Don't add
2341 * new entries into /proc/<tgid>/ without very good reasons.
2343 last = &ents[nents - 1];
2344 for (p = ents; p <= last; p++) {
2345 if (p->len != dentry->d_name.len)
2347 if (!memcmp(dentry->d_name.name, p->name, p->len))
2353 error = proc_pident_instantiate(dir, dentry, task, p);
2355 put_task_struct(task);
2357 return ERR_PTR(error);
2360 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2361 const struct pid_entry *ents, unsigned int nents)
2363 struct task_struct *task = get_proc_task(file_inode(file));
2364 const struct pid_entry *p;
2369 if (!dir_emit_dots(file, ctx))
2372 if (ctx->pos >= nents + 2)
2375 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2376 if (!proc_fill_cache(file, ctx, p->name, p->len,
2377 proc_pident_instantiate, task, p))
2382 put_task_struct(task);
2386 #ifdef CONFIG_SECURITY
2387 static int proc_pid_attr_open(struct inode *inode, struct file *file)
2389 file->private_data = NULL;
2390 __mem_open(inode, file, PTRACE_MODE_READ_FSCREDS);
2394 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2395 size_t count, loff_t *ppos)
2397 struct inode * inode = file_inode(file);
2400 struct task_struct *task = get_proc_task(inode);
2405 length = security_getprocattr(task,
2406 (char*)file->f_path.dentry->d_name.name,
2408 put_task_struct(task);
2410 length = simple_read_from_buffer(buf, count, ppos, p, length);
2415 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2416 size_t count, loff_t *ppos)
2418 struct inode * inode = file_inode(file);
2421 struct task_struct *task = get_proc_task(inode);
2423 /* A task may only write when it was the opener. */
2424 if (file->private_data != current->mm)
2430 if (count > PAGE_SIZE)
2433 /* No partial writes. */
2439 page = (char*)__get_free_page(GFP_TEMPORARY);
2444 if (copy_from_user(page, buf, count))
2447 /* Guard against adverse ptrace interaction */
2448 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2452 length = security_setprocattr(task,
2453 (char*)file->f_path.dentry->d_name.name,
2454 (void*)page, count);
2455 mutex_unlock(&task->signal->cred_guard_mutex);
2457 free_page((unsigned long) page);
2459 put_task_struct(task);
2464 static const struct file_operations proc_pid_attr_operations = {
2465 .open = proc_pid_attr_open,
2466 .read = proc_pid_attr_read,
2467 .write = proc_pid_attr_write,
2468 .llseek = generic_file_llseek,
2469 .release = mem_release,
2472 static const struct pid_entry attr_dir_stuff[] = {
2473 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2474 REG("prev", S_IRUGO, proc_pid_attr_operations),
2475 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2476 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2477 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2478 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2481 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2483 return proc_pident_readdir(file, ctx,
2484 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2487 static const struct file_operations proc_attr_dir_operations = {
2488 .read = generic_read_dir,
2489 .iterate = proc_attr_dir_readdir,
2490 .llseek = default_llseek,
2493 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2494 struct dentry *dentry, unsigned int flags)
2496 return proc_pident_lookup(dir, dentry,
2497 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2500 static const struct inode_operations proc_attr_dir_inode_operations = {
2501 .lookup = proc_attr_dir_lookup,
2502 .getattr = pid_getattr,
2503 .setattr = proc_setattr,
2508 #ifdef CONFIG_ELF_CORE
2509 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2510 size_t count, loff_t *ppos)
2512 struct task_struct *task = get_proc_task(file_inode(file));
2513 struct mm_struct *mm;
2514 char buffer[PROC_NUMBUF];
2522 mm = get_task_mm(task);
2524 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2525 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2526 MMF_DUMP_FILTER_SHIFT));
2528 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2531 put_task_struct(task);
2536 static ssize_t proc_coredump_filter_write(struct file *file,
2537 const char __user *buf,
2541 struct task_struct *task;
2542 struct mm_struct *mm;
2548 ret = kstrtouint_from_user(buf, count, 0, &val);
2553 task = get_proc_task(file_inode(file));
2557 mm = get_task_mm(task);
2562 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2564 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2566 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2571 put_task_struct(task);
2578 static const struct file_operations proc_coredump_filter_operations = {
2579 .read = proc_coredump_filter_read,
2580 .write = proc_coredump_filter_write,
2581 .llseek = generic_file_llseek,
2585 #ifdef CONFIG_TASK_IO_ACCOUNTING
2586 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2588 struct task_io_accounting acct = task->ioac;
2589 unsigned long flags;
2592 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2596 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2601 if (whole && lock_task_sighand(task, &flags)) {
2602 struct task_struct *t = task;
2604 task_io_accounting_add(&acct, &task->signal->ioac);
2605 while_each_thread(task, t)
2606 task_io_accounting_add(&acct, &t->ioac);
2608 unlock_task_sighand(task, &flags);
2615 "read_bytes: %llu\n"
2616 "write_bytes: %llu\n"
2617 "cancelled_write_bytes: %llu\n",
2618 (unsigned long long)acct.rchar,
2619 (unsigned long long)acct.wchar,
2620 (unsigned long long)acct.syscr,
2621 (unsigned long long)acct.syscw,
2622 (unsigned long long)acct.read_bytes,
2623 (unsigned long long)acct.write_bytes,
2624 (unsigned long long)acct.cancelled_write_bytes);
2628 mutex_unlock(&task->signal->cred_guard_mutex);
2632 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2633 struct pid *pid, struct task_struct *task)
2635 return do_io_accounting(task, m, 0);
2638 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2639 struct pid *pid, struct task_struct *task)
2641 return do_io_accounting(task, m, 1);
2643 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2645 #ifdef CONFIG_USER_NS
2646 static int proc_id_map_open(struct inode *inode, struct file *file,
2647 const struct seq_operations *seq_ops)
2649 struct user_namespace *ns = NULL;
2650 struct task_struct *task;
2651 struct seq_file *seq;
2654 task = get_proc_task(inode);
2657 ns = get_user_ns(task_cred_xxx(task, user_ns));
2659 put_task_struct(task);
2664 ret = seq_open(file, seq_ops);
2668 seq = file->private_data;
2678 static int proc_id_map_release(struct inode *inode, struct file *file)
2680 struct seq_file *seq = file->private_data;
2681 struct user_namespace *ns = seq->private;
2683 return seq_release(inode, file);
2686 static int proc_uid_map_open(struct inode *inode, struct file *file)
2688 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2691 static int proc_gid_map_open(struct inode *inode, struct file *file)
2693 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2696 static int proc_projid_map_open(struct inode *inode, struct file *file)
2698 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2701 static const struct file_operations proc_uid_map_operations = {
2702 .open = proc_uid_map_open,
2703 .write = proc_uid_map_write,
2705 .llseek = seq_lseek,
2706 .release = proc_id_map_release,
2709 static const struct file_operations proc_gid_map_operations = {
2710 .open = proc_gid_map_open,
2711 .write = proc_gid_map_write,
2713 .llseek = seq_lseek,
2714 .release = proc_id_map_release,
2717 static const struct file_operations proc_projid_map_operations = {
2718 .open = proc_projid_map_open,
2719 .write = proc_projid_map_write,
2721 .llseek = seq_lseek,
2722 .release = proc_id_map_release,
2725 static int proc_setgroups_open(struct inode *inode, struct file *file)
2727 struct user_namespace *ns = NULL;
2728 struct task_struct *task;
2732 task = get_proc_task(inode);
2735 ns = get_user_ns(task_cred_xxx(task, user_ns));
2737 put_task_struct(task);
2742 if (file->f_mode & FMODE_WRITE) {
2744 if (!ns_capable(ns, CAP_SYS_ADMIN))
2748 ret = single_open(file, &proc_setgroups_show, ns);
2759 static int proc_setgroups_release(struct inode *inode, struct file *file)
2761 struct seq_file *seq = file->private_data;
2762 struct user_namespace *ns = seq->private;
2763 int ret = single_release(inode, file);
2768 static const struct file_operations proc_setgroups_operations = {
2769 .open = proc_setgroups_open,
2770 .write = proc_setgroups_write,
2772 .llseek = seq_lseek,
2773 .release = proc_setgroups_release,
2775 #endif /* CONFIG_USER_NS */
2777 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2778 struct pid *pid, struct task_struct *task)
2780 int err = lock_trace(task);
2782 seq_printf(m, "%08x\n", task->personality);
2791 static const struct file_operations proc_task_operations;
2792 static const struct inode_operations proc_task_inode_operations;
2794 static const struct pid_entry tgid_base_stuff[] = {
2795 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2796 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2797 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2798 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2799 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2801 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2803 REG("environ", S_IRUSR, proc_environ_operations),
2804 ONE("auxv", S_IRUSR, proc_pid_auxv),
2805 ONE("status", S_IRUGO, proc_pid_status),
2806 ONE("personality", S_IRUSR, proc_pid_personality),
2807 ONE("limits", S_IRUGO, proc_pid_limits),
2808 #ifdef CONFIG_SCHED_DEBUG
2809 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2811 #ifdef CONFIG_SCHED_AUTOGROUP
2812 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2814 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2815 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2816 ONE("syscall", S_IRUSR, proc_pid_syscall),
2818 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2819 ONE("stat", S_IRUGO, proc_tgid_stat),
2820 ONE("statm", S_IRUGO, proc_pid_statm),
2821 REG("maps", S_IRUGO, proc_pid_maps_operations),
2823 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2825 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2826 LNK("cwd", proc_cwd_link),
2827 LNK("root", proc_root_link),
2828 LNK("exe", proc_exe_link),
2829 REG("mounts", S_IRUGO, proc_mounts_operations),
2830 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2831 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2832 #ifdef CONFIG_PROC_PAGE_MONITOR
2833 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2834 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2835 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2837 #ifdef CONFIG_SECURITY
2838 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2840 #ifdef CONFIG_KALLSYMS
2841 ONE("wchan", S_IRUGO, proc_pid_wchan),
2843 #ifdef CONFIG_STACKTRACE
2844 ONE("stack", S_IRUSR, proc_pid_stack),
2846 #ifdef CONFIG_SCHED_INFO
2847 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2849 #ifdef CONFIG_LATENCYTOP
2850 REG("latency", S_IRUGO, proc_lstats_operations),
2852 #ifdef CONFIG_PROC_PID_CPUSET
2853 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2855 #ifdef CONFIG_CGROUPS
2856 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2858 ONE("oom_score", S_IRUGO, proc_oom_score),
2859 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2860 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2861 #ifdef CONFIG_AUDITSYSCALL
2862 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2863 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2865 #ifdef CONFIG_FAULT_INJECTION
2866 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2868 #ifdef CONFIG_ELF_CORE
2869 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2871 #ifdef CONFIG_TASK_IO_ACCOUNTING
2872 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2874 #ifdef CONFIG_HARDWALL
2875 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2877 #ifdef CONFIG_USER_NS
2878 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2879 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2880 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2881 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2883 #ifdef CONFIG_CHECKPOINT_RESTORE
2884 REG("timers", S_IRUGO, proc_timers_operations),
2888 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2890 return proc_pident_readdir(file, ctx,
2891 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2894 static const struct file_operations proc_tgid_base_operations = {
2895 .read = generic_read_dir,
2896 .iterate = proc_tgid_base_readdir,
2897 .llseek = default_llseek,
2900 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2902 return proc_pident_lookup(dir, dentry,
2903 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2906 static const struct inode_operations proc_tgid_base_inode_operations = {
2907 .lookup = proc_tgid_base_lookup,
2908 .getattr = pid_getattr,
2909 .setattr = proc_setattr,
2910 .permission = proc_pid_permission,
2913 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2915 struct dentry *dentry, *leader, *dir;
2916 char buf[PROC_NUMBUF];
2920 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2921 /* no ->d_hash() rejects on procfs */
2922 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2924 d_invalidate(dentry);
2932 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2933 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2938 name.len = strlen(name.name);
2939 dir = d_hash_and_lookup(leader, &name);
2941 goto out_put_leader;
2944 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2945 dentry = d_hash_and_lookup(dir, &name);
2947 d_invalidate(dentry);
2959 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2960 * @task: task that should be flushed.
2962 * When flushing dentries from proc, one needs to flush them from global
2963 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2964 * in. This call is supposed to do all of this job.
2966 * Looks in the dcache for
2968 * /proc/@tgid/task/@pid
2969 * if either directory is present flushes it and all of it'ts children
2972 * It is safe and reasonable to cache /proc entries for a task until
2973 * that task exits. After that they just clog up the dcache with
2974 * useless entries, possibly causing useful dcache entries to be
2975 * flushed instead. This routine is proved to flush those useless
2976 * dcache entries at process exit time.
2978 * NOTE: This routine is just an optimization so it does not guarantee
2979 * that no dcache entries will exist at process exit time it
2980 * just makes it very unlikely that any will persist.
2983 void proc_flush_task(struct task_struct *task)
2986 struct pid *pid, *tgid;
2989 pid = task_pid(task);
2990 tgid = task_tgid(task);
2992 for (i = 0; i <= pid->level; i++) {
2993 upid = &pid->numbers[i];
2994 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2995 tgid->numbers[i].nr);
2999 static int proc_pid_instantiate(struct inode *dir,
3000 struct dentry * dentry,
3001 struct task_struct *task, const void *ptr)
3003 struct inode *inode;
3005 inode = proc_pid_make_inode(dir->i_sb, task);
3009 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3010 inode->i_op = &proc_tgid_base_inode_operations;
3011 inode->i_fop = &proc_tgid_base_operations;
3012 inode->i_flags|=S_IMMUTABLE;
3014 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3015 ARRAY_SIZE(tgid_base_stuff)));
3017 d_set_d_op(dentry, &pid_dentry_operations);
3019 d_add(dentry, inode);
3020 /* Close the race of the process dying before we return the dentry */
3021 if (pid_revalidate(dentry, 0))
3027 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3029 int result = -ENOENT;
3030 struct task_struct *task;
3032 struct pid_namespace *ns;
3034 tgid = name_to_int(&dentry->d_name);
3038 ns = dentry->d_sb->s_fs_info;
3040 task = find_task_by_pid_ns(tgid, ns);
3042 get_task_struct(task);
3047 result = proc_pid_instantiate(dir, dentry, task, NULL);
3048 put_task_struct(task);
3050 return ERR_PTR(result);
3054 * Find the first task with tgid >= tgid
3059 struct task_struct *task;
3061 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3066 put_task_struct(iter.task);
3070 pid = find_ge_pid(iter.tgid, ns);
3072 iter.tgid = pid_nr_ns(pid, ns);
3073 iter.task = pid_task(pid, PIDTYPE_PID);
3074 /* What we to know is if the pid we have find is the
3075 * pid of a thread_group_leader. Testing for task
3076 * being a thread_group_leader is the obvious thing
3077 * todo but there is a window when it fails, due to
3078 * the pid transfer logic in de_thread.
3080 * So we perform the straight forward test of seeing
3081 * if the pid we have found is the pid of a thread
3082 * group leader, and don't worry if the task we have
3083 * found doesn't happen to be a thread group leader.
3084 * As we don't care in the case of readdir.
3086 if (!iter.task || !has_group_leader_pid(iter.task)) {
3090 get_task_struct(iter.task);
3096 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3098 /* for the /proc/ directory itself, after non-process stuff has been done */
3099 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3101 struct tgid_iter iter;
3102 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3103 loff_t pos = ctx->pos;
3105 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3108 if (pos == TGID_OFFSET - 2) {
3109 struct inode *inode = d_inode(ns->proc_self);
3110 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3112 ctx->pos = pos = pos + 1;
3114 if (pos == TGID_OFFSET - 1) {
3115 struct inode *inode = d_inode(ns->proc_thread_self);
3116 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3118 ctx->pos = pos = pos + 1;
3120 iter.tgid = pos - TGID_OFFSET;
3122 for (iter = next_tgid(ns, iter);
3124 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3125 char name[PROC_NUMBUF];
3129 if (!has_pid_permissions(ns, iter.task, 2))
3132 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3133 ctx->pos = iter.tgid + TGID_OFFSET;
3134 if (!proc_fill_cache(file, ctx, name, len,
3135 proc_pid_instantiate, iter.task, NULL)) {
3136 put_task_struct(iter.task);
3140 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3145 * proc_tid_comm_permission is a special permission function exclusively
3146 * used for the node /proc/<pid>/task/<tid>/comm.
3147 * It bypasses generic permission checks in the case where a task of the same
3148 * task group attempts to access the node.
3149 * The rationale behind this is that glibc and bionic access this node for
3150 * cross thread naming (pthread_set/getname_np(!self)). However, if
3151 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3152 * which locks out the cross thread naming implementation.
3153 * This function makes sure that the node is always accessible for members of
3154 * same thread group.
3156 static int proc_tid_comm_permission(struct inode *inode, int mask)
3158 bool is_same_tgroup;
3159 struct task_struct *task;
3161 task = get_proc_task(inode);
3164 is_same_tgroup = same_thread_group(current, task);
3165 put_task_struct(task);
3167 if (likely(is_same_tgroup && !(mask & MAY_EXEC))) {
3168 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3169 * read or written by the members of the corresponding
3175 return generic_permission(inode, mask);
3178 static const struct inode_operations proc_tid_comm_inode_operations = {
3179 .permission = proc_tid_comm_permission,
3185 static const struct pid_entry tid_base_stuff[] = {
3186 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3187 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3188 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3190 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3192 REG("environ", S_IRUSR, proc_environ_operations),
3193 ONE("auxv", S_IRUSR, proc_pid_auxv),
3194 ONE("status", S_IRUGO, proc_pid_status),
3195 ONE("personality", S_IRUSR, proc_pid_personality),
3196 ONE("limits", S_IRUGO, proc_pid_limits),
3197 #ifdef CONFIG_SCHED_DEBUG
3198 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3200 NOD("comm", S_IFREG|S_IRUGO|S_IWUSR,
3201 &proc_tid_comm_inode_operations,
3202 &proc_pid_set_comm_operations, {}),
3203 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3204 ONE("syscall", S_IRUSR, proc_pid_syscall),
3206 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3207 ONE("stat", S_IRUGO, proc_tid_stat),
3208 ONE("statm", S_IRUGO, proc_pid_statm),
3209 REG("maps", S_IRUGO, proc_tid_maps_operations),
3210 #ifdef CONFIG_PROC_CHILDREN
3211 REG("children", S_IRUGO, proc_tid_children_operations),
3214 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3216 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3217 LNK("cwd", proc_cwd_link),
3218 LNK("root", proc_root_link),
3219 LNK("exe", proc_exe_link),
3220 REG("mounts", S_IRUGO, proc_mounts_operations),
3221 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3222 #ifdef CONFIG_PROC_PAGE_MONITOR
3223 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3224 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3225 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3227 #ifdef CONFIG_SECURITY
3228 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3230 #ifdef CONFIG_KALLSYMS
3231 ONE("wchan", S_IRUGO, proc_pid_wchan),
3233 #ifdef CONFIG_STACKTRACE
3234 ONE("stack", S_IRUSR, proc_pid_stack),
3236 #ifdef CONFIG_SCHED_INFO
3237 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3239 #ifdef CONFIG_LATENCYTOP
3240 REG("latency", S_IRUGO, proc_lstats_operations),
3242 #ifdef CONFIG_PROC_PID_CPUSET
3243 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3245 #ifdef CONFIG_CGROUPS
3246 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3248 ONE("oom_score", S_IRUGO, proc_oom_score),
3249 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3250 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3251 #ifdef CONFIG_AUDITSYSCALL
3252 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3253 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3255 #ifdef CONFIG_FAULT_INJECTION
3256 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3258 #ifdef CONFIG_TASK_IO_ACCOUNTING
3259 ONE("io", S_IRUSR, proc_tid_io_accounting),
3261 #ifdef CONFIG_HARDWALL
3262 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3264 #ifdef CONFIG_USER_NS
3265 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3266 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3267 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3268 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3272 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3274 return proc_pident_readdir(file, ctx,
3275 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3278 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3280 return proc_pident_lookup(dir, dentry,
3281 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3284 static const struct file_operations proc_tid_base_operations = {
3285 .read = generic_read_dir,
3286 .iterate = proc_tid_base_readdir,
3287 .llseek = default_llseek,
3290 static const struct inode_operations proc_tid_base_inode_operations = {
3291 .lookup = proc_tid_base_lookup,
3292 .getattr = pid_getattr,
3293 .setattr = proc_setattr,
3296 static int proc_task_instantiate(struct inode *dir,
3297 struct dentry *dentry, struct task_struct *task, const void *ptr)
3299 struct inode *inode;
3300 inode = proc_pid_make_inode(dir->i_sb, task);
3304 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3305 inode->i_op = &proc_tid_base_inode_operations;
3306 inode->i_fop = &proc_tid_base_operations;
3307 inode->i_flags|=S_IMMUTABLE;
3309 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3310 ARRAY_SIZE(tid_base_stuff)));
3312 d_set_d_op(dentry, &pid_dentry_operations);
3314 d_add(dentry, inode);
3315 /* Close the race of the process dying before we return the dentry */
3316 if (pid_revalidate(dentry, 0))
3322 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3324 int result = -ENOENT;
3325 struct task_struct *task;
3326 struct task_struct *leader = get_proc_task(dir);
3328 struct pid_namespace *ns;
3333 tid = name_to_int(&dentry->d_name);
3337 ns = dentry->d_sb->s_fs_info;
3339 task = find_task_by_pid_ns(tid, ns);
3341 get_task_struct(task);
3345 if (!same_thread_group(leader, task))
3348 result = proc_task_instantiate(dir, dentry, task, NULL);
3350 put_task_struct(task);
3352 put_task_struct(leader);
3354 return ERR_PTR(result);
3358 * Find the first tid of a thread group to return to user space.
3360 * Usually this is just the thread group leader, but if the users
3361 * buffer was too small or there was a seek into the middle of the
3362 * directory we have more work todo.
3364 * In the case of a short read we start with find_task_by_pid.
3366 * In the case of a seek we start with the leader and walk nr
3369 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3370 struct pid_namespace *ns)
3372 struct task_struct *pos, *task;
3373 unsigned long nr = f_pos;
3375 if (nr != f_pos) /* 32bit overflow? */
3379 task = pid_task(pid, PIDTYPE_PID);
3383 /* Attempt to start with the tid of a thread */
3385 pos = find_task_by_pid_ns(tid, ns);
3386 if (pos && same_thread_group(pos, task))
3390 /* If nr exceeds the number of threads there is nothing todo */
3391 if (nr >= get_nr_threads(task))
3394 /* If we haven't found our starting place yet start
3395 * with the leader and walk nr threads forward.
3397 pos = task = task->group_leader;
3401 } while_each_thread(task, pos);
3406 get_task_struct(pos);
3413 * Find the next thread in the thread list.
3414 * Return NULL if there is an error or no next thread.
3416 * The reference to the input task_struct is released.
3418 static struct task_struct *next_tid(struct task_struct *start)
3420 struct task_struct *pos = NULL;
3422 if (pid_alive(start)) {
3423 pos = next_thread(start);
3424 if (thread_group_leader(pos))
3427 get_task_struct(pos);
3430 put_task_struct(start);
3434 /* for the /proc/TGID/task/ directories */
3435 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3437 struct inode *inode = file_inode(file);
3438 struct task_struct *task;
3439 struct pid_namespace *ns;
3442 if (proc_inode_is_dead(inode))
3445 if (!dir_emit_dots(file, ctx))
3448 /* f_version caches the tgid value that the last readdir call couldn't
3449 * return. lseek aka telldir automagically resets f_version to 0.
3451 ns = inode->i_sb->s_fs_info;
3452 tid = (int)file->f_version;
3453 file->f_version = 0;
3454 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3456 task = next_tid(task), ctx->pos++) {
3457 char name[PROC_NUMBUF];
3459 tid = task_pid_nr_ns(task, ns);
3460 len = snprintf(name, sizeof(name), "%d", tid);
3461 if (!proc_fill_cache(file, ctx, name, len,
3462 proc_task_instantiate, task, NULL)) {
3463 /* returning this tgid failed, save it as the first
3464 * pid for the next readir call */
3465 file->f_version = (u64)tid;
3466 put_task_struct(task);
3474 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3476 struct inode *inode = d_inode(dentry);
3477 struct task_struct *p = get_proc_task(inode);
3478 generic_fillattr(inode, stat);
3481 stat->nlink += get_nr_threads(p);
3488 static const struct inode_operations proc_task_inode_operations = {
3489 .lookup = proc_task_lookup,
3490 .getattr = proc_task_getattr,
3491 .setattr = proc_setattr,
3492 .permission = proc_pid_permission,
3495 static const struct file_operations proc_task_operations = {
3496 .read = generic_read_dir,
3497 .iterate = proc_task_readdir,
3498 .llseek = default_llseek,