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>
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
111 const struct inode_operations *iop;
112 const struct file_operations *fop;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
118 .len = sizeof(NAME) - 1, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define ONE(NAME, MODE, show) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
139 * Count the number of hardlinks for the pid_entry table, excluding the .
142 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
157 static int get_task_root(struct task_struct *task, struct path *root)
159 int result = -ENOENT;
163 get_fs_root(task->fs, root);
170 static int proc_cwd_link(struct dentry *dentry, struct path *path)
172 struct task_struct *task = get_proc_task(d_inode(dentry));
173 int result = -ENOENT;
178 get_fs_pwd(task->fs, path);
182 put_task_struct(task);
187 static int proc_root_link(struct dentry *dentry, struct path *path)
189 struct task_struct *task = get_proc_task(d_inode(dentry));
190 int result = -ENOENT;
193 result = get_task_root(task, path);
194 put_task_struct(task);
199 static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf,
200 size_t _count, loff_t *pos)
202 struct task_struct *tsk;
203 struct mm_struct *mm;
205 unsigned long count = _count;
206 unsigned long arg_start, arg_end, env_start, env_end;
207 unsigned long len1, len2, len;
214 tsk = get_proc_task(file_inode(file));
217 mm = get_task_mm(tsk);
218 put_task_struct(tsk);
221 /* Check if process spawned far enough to have cmdline. */
227 page = (char *)__get_free_page(GFP_TEMPORARY);
233 down_read(&mm->mmap_sem);
234 arg_start = mm->arg_start;
235 arg_end = mm->arg_end;
236 env_start = mm->env_start;
237 env_end = mm->env_end;
238 up_read(&mm->mmap_sem);
240 BUG_ON(arg_start > arg_end);
241 BUG_ON(env_start > env_end);
243 len1 = arg_end - arg_start;
244 len2 = env_end - env_start;
247 * Inherently racy -- command line shares address space
248 * with code and data.
250 rv = access_remote_vm(mm, arg_end - 1, &c, 1, 0);
257 /* Command line (set of strings) occupies whole ARGV. */
261 p = arg_start + *pos;
263 while (count > 0 && len > 0) {
267 _count = min3(count, len, PAGE_SIZE);
268 nr_read = access_remote_vm(mm, p, page, _count, 0);
274 if (copy_to_user(buf, page, nr_read)) {
287 * Command line (1 string) occupies ARGV and maybe
290 if (len1 + len2 <= *pos)
295 p = arg_start + *pos;
297 while (count > 0 && len > 0) {
298 unsigned int _count, l;
302 _count = min3(count, len, PAGE_SIZE);
303 nr_read = access_remote_vm(mm, p, page, _count, 0);
310 * Command line can be shorter than whole ARGV
311 * even if last "marker" byte says it is not.
314 l = strnlen(page, nr_read);
320 if (copy_to_user(buf, page, nr_read)) {
336 * Command line (1 string) occupies ARGV and
340 p = env_start + *pos - len1;
341 len = len1 + len2 - *pos;
346 while (count > 0 && len > 0) {
347 unsigned int _count, l;
351 _count = min3(count, len, PAGE_SIZE);
352 nr_read = access_remote_vm(mm, p, page, _count, 0);
360 l = strnlen(page, nr_read);
366 if (copy_to_user(buf, page, nr_read)) {
385 free_page((unsigned long)page);
393 static const struct file_operations proc_pid_cmdline_ops = {
394 .read = proc_pid_cmdline_read,
395 .llseek = generic_file_llseek,
398 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
399 struct pid *pid, struct task_struct *task)
401 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
402 if (mm && !IS_ERR(mm)) {
403 unsigned int nwords = 0;
406 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
407 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
415 #ifdef CONFIG_KALLSYMS
417 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
418 * Returns the resolved symbol. If that fails, simply return the address.
420 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
421 struct pid *pid, struct task_struct *task)
424 char symname[KSYM_NAME_LEN];
426 wchan = get_wchan(task);
428 if (lookup_symbol_name(wchan, symname) < 0) {
429 if (!ptrace_may_access(task, PTRACE_MODE_READ))
431 seq_printf(m, "%lu", wchan);
433 seq_printf(m, "%s", symname);
438 #endif /* CONFIG_KALLSYMS */
440 static int lock_trace(struct task_struct *task)
442 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
445 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
446 mutex_unlock(&task->signal->cred_guard_mutex);
452 static void unlock_trace(struct task_struct *task)
454 mutex_unlock(&task->signal->cred_guard_mutex);
457 #ifdef CONFIG_STACKTRACE
459 #define MAX_STACK_TRACE_DEPTH 64
461 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
462 struct pid *pid, struct task_struct *task)
464 struct stack_trace trace;
465 unsigned long *entries;
469 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
473 trace.nr_entries = 0;
474 trace.max_entries = MAX_STACK_TRACE_DEPTH;
475 trace.entries = entries;
478 err = lock_trace(task);
480 save_stack_trace_tsk(task, &trace);
482 for (i = 0; i < trace.nr_entries; i++) {
483 seq_printf(m, "[<%pK>] %pS\n",
484 (void *)entries[i], (void *)entries[i]);
494 #ifdef CONFIG_SCHED_INFO
496 * Provides /proc/PID/schedstat
498 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
499 struct pid *pid, struct task_struct *task)
501 if (unlikely(!sched_info_on()))
502 seq_printf(m, "0 0 0\n");
504 seq_printf(m, "%llu %llu %lu\n",
505 (unsigned long long)task->se.sum_exec_runtime,
506 (unsigned long long)task->sched_info.run_delay,
507 task->sched_info.pcount);
513 #ifdef CONFIG_LATENCYTOP
514 static int lstats_show_proc(struct seq_file *m, void *v)
517 struct inode *inode = m->private;
518 struct task_struct *task = get_proc_task(inode);
522 seq_puts(m, "Latency Top version : v0.1\n");
523 for (i = 0; i < 32; i++) {
524 struct latency_record *lr = &task->latency_record[i];
525 if (lr->backtrace[0]) {
527 seq_printf(m, "%i %li %li",
528 lr->count, lr->time, lr->max);
529 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
530 unsigned long bt = lr->backtrace[q];
535 seq_printf(m, " %ps", (void *)bt);
541 put_task_struct(task);
545 static int lstats_open(struct inode *inode, struct file *file)
547 return single_open(file, lstats_show_proc, inode);
550 static ssize_t lstats_write(struct file *file, const char __user *buf,
551 size_t count, loff_t *offs)
553 struct task_struct *task = get_proc_task(file_inode(file));
557 clear_all_latency_tracing(task);
558 put_task_struct(task);
563 static const struct file_operations proc_lstats_operations = {
566 .write = lstats_write,
568 .release = single_release,
573 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
574 struct pid *pid, struct task_struct *task)
576 unsigned long totalpages = totalram_pages + total_swap_pages;
577 unsigned long points = 0;
579 read_lock(&tasklist_lock);
581 points = oom_badness(task, NULL, NULL, totalpages) *
583 read_unlock(&tasklist_lock);
584 seq_printf(m, "%lu\n", points);
594 static const struct limit_names lnames[RLIM_NLIMITS] = {
595 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
596 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
597 [RLIMIT_DATA] = {"Max data size", "bytes"},
598 [RLIMIT_STACK] = {"Max stack size", "bytes"},
599 [RLIMIT_CORE] = {"Max core file size", "bytes"},
600 [RLIMIT_RSS] = {"Max resident set", "bytes"},
601 [RLIMIT_NPROC] = {"Max processes", "processes"},
602 [RLIMIT_NOFILE] = {"Max open files", "files"},
603 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
604 [RLIMIT_AS] = {"Max address space", "bytes"},
605 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
606 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
607 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
608 [RLIMIT_NICE] = {"Max nice priority", NULL},
609 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
610 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
613 /* Display limits for a process */
614 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
615 struct pid *pid, struct task_struct *task)
620 struct rlimit rlim[RLIM_NLIMITS];
622 if (!lock_task_sighand(task, &flags))
624 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
625 unlock_task_sighand(task, &flags);
628 * print the file header
630 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
631 "Limit", "Soft Limit", "Hard Limit", "Units");
633 for (i = 0; i < RLIM_NLIMITS; i++) {
634 if (rlim[i].rlim_cur == RLIM_INFINITY)
635 seq_printf(m, "%-25s %-20s ",
636 lnames[i].name, "unlimited");
638 seq_printf(m, "%-25s %-20lu ",
639 lnames[i].name, rlim[i].rlim_cur);
641 if (rlim[i].rlim_max == RLIM_INFINITY)
642 seq_printf(m, "%-20s ", "unlimited");
644 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
647 seq_printf(m, "%-10s\n", lnames[i].unit);
655 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
656 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
657 struct pid *pid, struct task_struct *task)
660 unsigned long args[6], sp, pc;
663 res = lock_trace(task);
667 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
668 seq_puts(m, "running\n");
670 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
673 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
675 args[0], args[1], args[2], args[3], args[4], args[5],
681 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
683 /************************************************************************/
684 /* Here the fs part begins */
685 /************************************************************************/
687 /* permission checks */
688 static int proc_fd_access_allowed(struct inode *inode)
690 struct task_struct *task;
692 /* Allow access to a task's file descriptors if it is us or we
693 * may use ptrace attach to the process and find out that
696 task = get_proc_task(inode);
698 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
699 put_task_struct(task);
704 int proc_setattr(struct dentry *dentry, struct iattr *attr)
707 struct inode *inode = d_inode(dentry);
709 if (attr->ia_valid & ATTR_MODE)
712 error = inode_change_ok(inode, attr);
716 setattr_copy(inode, attr);
717 mark_inode_dirty(inode);
722 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
723 * or euid/egid (for hide_pid_min=2)?
725 static bool has_pid_permissions(struct pid_namespace *pid,
726 struct task_struct *task,
729 if (pid->hide_pid < hide_pid_min)
731 if (in_group_p(pid->pid_gid))
733 return ptrace_may_access(task, PTRACE_MODE_READ);
737 static int proc_pid_permission(struct inode *inode, int mask)
739 struct pid_namespace *pid = inode->i_sb->s_fs_info;
740 struct task_struct *task;
743 task = get_proc_task(inode);
746 has_perms = has_pid_permissions(pid, task, 1);
747 put_task_struct(task);
750 if (pid->hide_pid == 2) {
752 * Let's make getdents(), stat(), and open()
753 * consistent with each other. If a process
754 * may not stat() a file, it shouldn't be seen
762 return generic_permission(inode, mask);
767 static const struct inode_operations proc_def_inode_operations = {
768 .setattr = proc_setattr,
771 static int proc_single_show(struct seq_file *m, void *v)
773 struct inode *inode = m->private;
774 struct pid_namespace *ns;
776 struct task_struct *task;
779 ns = inode->i_sb->s_fs_info;
780 pid = proc_pid(inode);
781 task = get_pid_task(pid, PIDTYPE_PID);
785 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
787 put_task_struct(task);
791 static int proc_single_open(struct inode *inode, struct file *filp)
793 return single_open(filp, proc_single_show, inode);
796 static const struct file_operations proc_single_file_operations = {
797 .open = proc_single_open,
800 .release = single_release,
804 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
806 struct task_struct *task = get_proc_task(inode);
807 struct mm_struct *mm = ERR_PTR(-ESRCH);
810 mm = mm_access(task, mode);
811 put_task_struct(task);
813 if (!IS_ERR_OR_NULL(mm)) {
814 /* ensure this mm_struct can't be freed */
815 atomic_inc(&mm->mm_count);
816 /* but do not pin its memory */
824 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
826 struct mm_struct *mm = proc_mem_open(inode, mode);
831 file->private_data = mm;
835 static int mem_open(struct inode *inode, struct file *file)
837 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
839 /* OK to pass negative loff_t, we can catch out-of-range */
840 file->f_mode |= FMODE_UNSIGNED_OFFSET;
845 static ssize_t mem_rw(struct file *file, char __user *buf,
846 size_t count, loff_t *ppos, int write)
848 struct mm_struct *mm = file->private_data;
849 unsigned long addr = *ppos;
856 page = (char *)__get_free_page(GFP_TEMPORARY);
861 if (!atomic_inc_not_zero(&mm->mm_users))
865 int this_len = min_t(int, count, PAGE_SIZE);
867 if (write && copy_from_user(page, buf, this_len)) {
872 this_len = access_remote_vm(mm, addr, page, this_len, write);
879 if (!write && copy_to_user(buf, page, this_len)) {
893 free_page((unsigned long) page);
897 static ssize_t mem_read(struct file *file, char __user *buf,
898 size_t count, loff_t *ppos)
900 return mem_rw(file, buf, count, ppos, 0);
903 static ssize_t mem_write(struct file *file, const char __user *buf,
904 size_t count, loff_t *ppos)
906 return mem_rw(file, (char __user*)buf, count, ppos, 1);
909 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
913 file->f_pos = offset;
916 file->f_pos += offset;
921 force_successful_syscall_return();
925 static int mem_release(struct inode *inode, struct file *file)
927 struct mm_struct *mm = file->private_data;
933 static const struct file_operations proc_mem_operations = {
938 .release = mem_release,
941 static int environ_open(struct inode *inode, struct file *file)
943 return __mem_open(inode, file, PTRACE_MODE_READ);
946 static ssize_t environ_read(struct file *file, char __user *buf,
947 size_t count, loff_t *ppos)
950 unsigned long src = *ppos;
952 struct mm_struct *mm = file->private_data;
957 page = (char *)__get_free_page(GFP_TEMPORARY);
962 if (!atomic_inc_not_zero(&mm->mm_users))
965 size_t this_len, max_len;
968 if (src >= (mm->env_end - mm->env_start))
971 this_len = mm->env_end - (mm->env_start + src);
973 max_len = min_t(size_t, PAGE_SIZE, count);
974 this_len = min(max_len, this_len);
976 retval = access_remote_vm(mm, (mm->env_start + src),
984 if (copy_to_user(buf, page, retval)) {
998 free_page((unsigned long) page);
1002 static const struct file_operations proc_environ_operations = {
1003 .open = environ_open,
1004 .read = environ_read,
1005 .llseek = generic_file_llseek,
1006 .release = mem_release,
1009 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
1012 struct task_struct *task = get_proc_task(file_inode(file));
1013 char buffer[PROC_NUMBUF];
1014 int oom_adj = OOM_ADJUST_MIN;
1016 unsigned long flags;
1020 if (lock_task_sighand(task, &flags)) {
1021 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
1022 oom_adj = OOM_ADJUST_MAX;
1024 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
1026 unlock_task_sighand(task, &flags);
1028 put_task_struct(task);
1029 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
1030 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1033 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
1034 size_t count, loff_t *ppos)
1036 struct task_struct *task;
1037 char buffer[PROC_NUMBUF];
1039 unsigned long flags;
1042 memset(buffer, 0, sizeof(buffer));
1043 if (count > sizeof(buffer) - 1)
1044 count = sizeof(buffer) - 1;
1045 if (copy_from_user(buffer, buf, count)) {
1050 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
1053 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
1054 oom_adj != OOM_DISABLE) {
1059 task = get_proc_task(file_inode(file));
1071 if (!lock_task_sighand(task, &flags)) {
1077 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1078 * value is always attainable.
1080 if (oom_adj == OOM_ADJUST_MAX)
1081 oom_adj = OOM_SCORE_ADJ_MAX;
1083 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
1085 if (oom_adj < task->signal->oom_score_adj &&
1086 !capable(CAP_SYS_RESOURCE)) {
1092 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1093 * /proc/pid/oom_score_adj instead.
1095 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1096 current->comm, task_pid_nr(current), task_pid_nr(task),
1099 task->signal->oom_score_adj = oom_adj;
1100 trace_oom_score_adj_update(task);
1102 unlock_task_sighand(task, &flags);
1105 put_task_struct(task);
1107 return err < 0 ? err : count;
1110 static const struct file_operations proc_oom_adj_operations = {
1111 .read = oom_adj_read,
1112 .write = oom_adj_write,
1113 .llseek = generic_file_llseek,
1116 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1117 size_t count, loff_t *ppos)
1119 struct task_struct *task = get_proc_task(file_inode(file));
1120 char buffer[PROC_NUMBUF];
1121 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1122 unsigned long flags;
1127 if (lock_task_sighand(task, &flags)) {
1128 oom_score_adj = task->signal->oom_score_adj;
1129 unlock_task_sighand(task, &flags);
1131 put_task_struct(task);
1132 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1133 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1136 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1137 size_t count, loff_t *ppos)
1139 struct task_struct *task;
1140 char buffer[PROC_NUMBUF];
1141 unsigned long flags;
1145 memset(buffer, 0, sizeof(buffer));
1146 if (count > sizeof(buffer) - 1)
1147 count = sizeof(buffer) - 1;
1148 if (copy_from_user(buffer, buf, count)) {
1153 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1156 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1157 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1162 task = get_proc_task(file_inode(file));
1174 if (!lock_task_sighand(task, &flags)) {
1179 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1180 !capable(CAP_SYS_RESOURCE)) {
1185 task->signal->oom_score_adj = (short)oom_score_adj;
1186 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1187 task->signal->oom_score_adj_min = (short)oom_score_adj;
1188 trace_oom_score_adj_update(task);
1191 unlock_task_sighand(task, &flags);
1194 put_task_struct(task);
1196 return err < 0 ? err : count;
1199 static const struct file_operations proc_oom_score_adj_operations = {
1200 .read = oom_score_adj_read,
1201 .write = oom_score_adj_write,
1202 .llseek = default_llseek,
1205 #ifdef CONFIG_AUDITSYSCALL
1206 #define TMPBUFLEN 21
1207 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1208 size_t count, loff_t *ppos)
1210 struct inode * inode = file_inode(file);
1211 struct task_struct *task = get_proc_task(inode);
1213 char tmpbuf[TMPBUFLEN];
1217 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1218 from_kuid(file->f_cred->user_ns,
1219 audit_get_loginuid(task)));
1220 put_task_struct(task);
1221 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1224 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1225 size_t count, loff_t *ppos)
1227 struct inode * inode = file_inode(file);
1234 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1240 if (count >= PAGE_SIZE)
1241 count = PAGE_SIZE - 1;
1244 /* No partial writes. */
1247 page = (char*)__get_free_page(GFP_TEMPORARY);
1251 if (copy_from_user(page, buf, count))
1255 loginuid = simple_strtoul(page, &tmp, 10);
1262 /* is userspace tring to explicitly UNSET the loginuid? */
1263 if (loginuid == AUDIT_UID_UNSET) {
1264 kloginuid = INVALID_UID;
1266 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1267 if (!uid_valid(kloginuid)) {
1273 length = audit_set_loginuid(kloginuid);
1274 if (likely(length == 0))
1278 free_page((unsigned long) page);
1282 static const struct file_operations proc_loginuid_operations = {
1283 .read = proc_loginuid_read,
1284 .write = proc_loginuid_write,
1285 .llseek = generic_file_llseek,
1288 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1289 size_t count, loff_t *ppos)
1291 struct inode * inode = file_inode(file);
1292 struct task_struct *task = get_proc_task(inode);
1294 char tmpbuf[TMPBUFLEN];
1298 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1299 audit_get_sessionid(task));
1300 put_task_struct(task);
1301 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1304 static const struct file_operations proc_sessionid_operations = {
1305 .read = proc_sessionid_read,
1306 .llseek = generic_file_llseek,
1310 #ifdef CONFIG_FAULT_INJECTION
1311 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1312 size_t count, loff_t *ppos)
1314 struct task_struct *task = get_proc_task(file_inode(file));
1315 char buffer[PROC_NUMBUF];
1321 make_it_fail = task->make_it_fail;
1322 put_task_struct(task);
1324 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1326 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1329 static ssize_t proc_fault_inject_write(struct file * file,
1330 const char __user * buf, size_t count, loff_t *ppos)
1332 struct task_struct *task;
1333 char buffer[PROC_NUMBUF], *end;
1336 if (!capable(CAP_SYS_RESOURCE))
1338 memset(buffer, 0, sizeof(buffer));
1339 if (count > sizeof(buffer) - 1)
1340 count = sizeof(buffer) - 1;
1341 if (copy_from_user(buffer, buf, count))
1343 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1346 if (make_it_fail < 0 || make_it_fail > 1)
1349 task = get_proc_task(file_inode(file));
1352 task->make_it_fail = make_it_fail;
1353 put_task_struct(task);
1358 static const struct file_operations proc_fault_inject_operations = {
1359 .read = proc_fault_inject_read,
1360 .write = proc_fault_inject_write,
1361 .llseek = generic_file_llseek,
1366 #ifdef CONFIG_SCHED_DEBUG
1368 * Print out various scheduling related per-task fields:
1370 static int sched_show(struct seq_file *m, void *v)
1372 struct inode *inode = m->private;
1373 struct task_struct *p;
1375 p = get_proc_task(inode);
1378 proc_sched_show_task(p, m);
1386 sched_write(struct file *file, const char __user *buf,
1387 size_t count, loff_t *offset)
1389 struct inode *inode = file_inode(file);
1390 struct task_struct *p;
1392 p = get_proc_task(inode);
1395 proc_sched_set_task(p);
1402 static int sched_open(struct inode *inode, struct file *filp)
1404 return single_open(filp, sched_show, inode);
1407 static const struct file_operations proc_pid_sched_operations = {
1410 .write = sched_write,
1411 .llseek = seq_lseek,
1412 .release = single_release,
1417 #ifdef CONFIG_SCHED_AUTOGROUP
1419 * Print out autogroup related information:
1421 static int sched_autogroup_show(struct seq_file *m, void *v)
1423 struct inode *inode = m->private;
1424 struct task_struct *p;
1426 p = get_proc_task(inode);
1429 proc_sched_autogroup_show_task(p, m);
1437 sched_autogroup_write(struct file *file, const char __user *buf,
1438 size_t count, loff_t *offset)
1440 struct inode *inode = file_inode(file);
1441 struct task_struct *p;
1442 char buffer[PROC_NUMBUF];
1446 memset(buffer, 0, sizeof(buffer));
1447 if (count > sizeof(buffer) - 1)
1448 count = sizeof(buffer) - 1;
1449 if (copy_from_user(buffer, buf, count))
1452 err = kstrtoint(strstrip(buffer), 0, &nice);
1456 p = get_proc_task(inode);
1460 err = proc_sched_autogroup_set_nice(p, nice);
1469 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1473 ret = single_open(filp, sched_autogroup_show, NULL);
1475 struct seq_file *m = filp->private_data;
1482 static const struct file_operations proc_pid_sched_autogroup_operations = {
1483 .open = sched_autogroup_open,
1485 .write = sched_autogroup_write,
1486 .llseek = seq_lseek,
1487 .release = single_release,
1490 #endif /* CONFIG_SCHED_AUTOGROUP */
1492 static ssize_t comm_write(struct file *file, const char __user *buf,
1493 size_t count, loff_t *offset)
1495 struct inode *inode = file_inode(file);
1496 struct task_struct *p;
1497 char buffer[TASK_COMM_LEN];
1498 const size_t maxlen = sizeof(buffer) - 1;
1500 memset(buffer, 0, sizeof(buffer));
1501 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1504 p = get_proc_task(inode);
1508 if (same_thread_group(current, p))
1509 set_task_comm(p, buffer);
1518 static int comm_show(struct seq_file *m, void *v)
1520 struct inode *inode = m->private;
1521 struct task_struct *p;
1523 p = get_proc_task(inode);
1528 seq_printf(m, "%s\n", p->comm);
1536 static int comm_open(struct inode *inode, struct file *filp)
1538 return single_open(filp, comm_show, inode);
1541 static const struct file_operations proc_pid_set_comm_operations = {
1544 .write = comm_write,
1545 .llseek = seq_lseek,
1546 .release = single_release,
1549 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1551 struct task_struct *task;
1552 struct mm_struct *mm;
1553 struct file *exe_file;
1555 task = get_proc_task(d_inode(dentry));
1558 mm = get_task_mm(task);
1559 put_task_struct(task);
1562 exe_file = get_mm_exe_file(mm);
1565 *exe_path = exe_file->f_path;
1566 path_get(&exe_file->f_path);
1573 static const char *proc_pid_follow_link(struct dentry *dentry, void **cookie)
1575 struct inode *inode = d_inode(dentry);
1577 int error = -EACCES;
1579 /* Are we allowed to snoop on the tasks file descriptors? */
1580 if (!proc_fd_access_allowed(inode))
1583 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1587 nd_jump_link(&path);
1590 return ERR_PTR(error);
1593 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1595 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1602 pathname = d_path(path, tmp, PAGE_SIZE);
1603 len = PTR_ERR(pathname);
1604 if (IS_ERR(pathname))
1606 len = tmp + PAGE_SIZE - 1 - pathname;
1610 if (copy_to_user(buffer, pathname, len))
1613 free_page((unsigned long)tmp);
1617 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1619 int error = -EACCES;
1620 struct inode *inode = d_inode(dentry);
1623 /* Are we allowed to snoop on the tasks file descriptors? */
1624 if (!proc_fd_access_allowed(inode))
1627 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1631 error = do_proc_readlink(&path, buffer, buflen);
1637 const struct inode_operations proc_pid_link_inode_operations = {
1638 .readlink = proc_pid_readlink,
1639 .follow_link = proc_pid_follow_link,
1640 .setattr = proc_setattr,
1644 /* building an inode */
1646 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1648 struct inode * inode;
1649 struct proc_inode *ei;
1650 const struct cred *cred;
1652 /* We need a new inode */
1654 inode = new_inode(sb);
1660 inode->i_ino = get_next_ino();
1661 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1662 inode->i_op = &proc_def_inode_operations;
1665 * grab the reference to task.
1667 ei->pid = get_task_pid(task, PIDTYPE_PID);
1671 if (task_dumpable(task)) {
1673 cred = __task_cred(task);
1674 inode->i_uid = cred->euid;
1675 inode->i_gid = cred->egid;
1678 security_task_to_inode(task, inode);
1688 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1690 struct inode *inode = d_inode(dentry);
1691 struct task_struct *task;
1692 const struct cred *cred;
1693 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1695 generic_fillattr(inode, stat);
1698 stat->uid = GLOBAL_ROOT_UID;
1699 stat->gid = GLOBAL_ROOT_GID;
1700 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1702 if (!has_pid_permissions(pid, task, 2)) {
1705 * This doesn't prevent learning whether PID exists,
1706 * it only makes getattr() consistent with readdir().
1710 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1711 task_dumpable(task)) {
1712 cred = __task_cred(task);
1713 stat->uid = cred->euid;
1714 stat->gid = cred->egid;
1724 * Exceptional case: normally we are not allowed to unhash a busy
1725 * directory. In this case, however, we can do it - no aliasing problems
1726 * due to the way we treat inodes.
1728 * Rewrite the inode's ownerships here because the owning task may have
1729 * performed a setuid(), etc.
1731 * Before the /proc/pid/status file was created the only way to read
1732 * the effective uid of a /process was to stat /proc/pid. Reading
1733 * /proc/pid/status is slow enough that procps and other packages
1734 * kept stating /proc/pid. To keep the rules in /proc simple I have
1735 * made this apply to all per process world readable and executable
1738 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1740 struct inode *inode;
1741 struct task_struct *task;
1742 const struct cred *cred;
1744 if (flags & LOOKUP_RCU)
1747 inode = d_inode(dentry);
1748 task = get_proc_task(inode);
1751 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1752 task_dumpable(task)) {
1754 cred = __task_cred(task);
1755 inode->i_uid = cred->euid;
1756 inode->i_gid = cred->egid;
1759 inode->i_uid = GLOBAL_ROOT_UID;
1760 inode->i_gid = GLOBAL_ROOT_GID;
1762 inode->i_mode &= ~(S_ISUID | S_ISGID);
1763 security_task_to_inode(task, inode);
1764 put_task_struct(task);
1770 static inline bool proc_inode_is_dead(struct inode *inode)
1772 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1775 int pid_delete_dentry(const struct dentry *dentry)
1777 /* Is the task we represent dead?
1778 * If so, then don't put the dentry on the lru list,
1779 * kill it immediately.
1781 return proc_inode_is_dead(d_inode(dentry));
1784 const struct dentry_operations pid_dentry_operations =
1786 .d_revalidate = pid_revalidate,
1787 .d_delete = pid_delete_dentry,
1793 * Fill a directory entry.
1795 * If possible create the dcache entry and derive our inode number and
1796 * file type from dcache entry.
1798 * Since all of the proc inode numbers are dynamically generated, the inode
1799 * numbers do not exist until the inode is cache. This means creating the
1800 * the dcache entry in readdir is necessary to keep the inode numbers
1801 * reported by readdir in sync with the inode numbers reported
1804 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1805 const char *name, int len,
1806 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1808 struct dentry *child, *dir = file->f_path.dentry;
1809 struct qstr qname = QSTR_INIT(name, len);
1810 struct inode *inode;
1814 child = d_hash_and_lookup(dir, &qname);
1816 child = d_alloc(dir, &qname);
1818 goto end_instantiate;
1819 if (instantiate(d_inode(dir), child, task, ptr) < 0) {
1821 goto end_instantiate;
1824 inode = d_inode(child);
1826 type = inode->i_mode >> 12;
1828 return dir_emit(ctx, name, len, ino, type);
1831 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1834 #ifdef CONFIG_CHECKPOINT_RESTORE
1837 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1838 * which represent vma start and end addresses.
1840 static int dname_to_vma_addr(struct dentry *dentry,
1841 unsigned long *start, unsigned long *end)
1843 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1849 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1851 unsigned long vm_start, vm_end;
1852 bool exact_vma_exists = false;
1853 struct mm_struct *mm = NULL;
1854 struct task_struct *task;
1855 const struct cred *cred;
1856 struct inode *inode;
1859 if (flags & LOOKUP_RCU)
1862 if (!capable(CAP_SYS_ADMIN)) {
1867 inode = d_inode(dentry);
1868 task = get_proc_task(inode);
1872 mm = mm_access(task, PTRACE_MODE_READ);
1873 if (IS_ERR_OR_NULL(mm))
1876 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1877 down_read(&mm->mmap_sem);
1878 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1879 up_read(&mm->mmap_sem);
1884 if (exact_vma_exists) {
1885 if (task_dumpable(task)) {
1887 cred = __task_cred(task);
1888 inode->i_uid = cred->euid;
1889 inode->i_gid = cred->egid;
1892 inode->i_uid = GLOBAL_ROOT_UID;
1893 inode->i_gid = GLOBAL_ROOT_GID;
1895 security_task_to_inode(task, inode);
1900 put_task_struct(task);
1906 static const struct dentry_operations tid_map_files_dentry_operations = {
1907 .d_revalidate = map_files_d_revalidate,
1908 .d_delete = pid_delete_dentry,
1911 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1913 unsigned long vm_start, vm_end;
1914 struct vm_area_struct *vma;
1915 struct task_struct *task;
1916 struct mm_struct *mm;
1920 task = get_proc_task(d_inode(dentry));
1924 mm = get_task_mm(task);
1925 put_task_struct(task);
1929 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1934 down_read(&mm->mmap_sem);
1935 vma = find_exact_vma(mm, vm_start, vm_end);
1936 if (vma && vma->vm_file) {
1937 *path = vma->vm_file->f_path;
1941 up_read(&mm->mmap_sem);
1949 struct map_files_info {
1952 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1956 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1957 struct task_struct *task, const void *ptr)
1959 fmode_t mode = (fmode_t)(unsigned long)ptr;
1960 struct proc_inode *ei;
1961 struct inode *inode;
1963 inode = proc_pid_make_inode(dir->i_sb, task);
1968 ei->op.proc_get_link = proc_map_files_get_link;
1970 inode->i_op = &proc_pid_link_inode_operations;
1972 inode->i_mode = S_IFLNK;
1974 if (mode & FMODE_READ)
1975 inode->i_mode |= S_IRUSR;
1976 if (mode & FMODE_WRITE)
1977 inode->i_mode |= S_IWUSR;
1979 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1980 d_add(dentry, inode);
1985 static struct dentry *proc_map_files_lookup(struct inode *dir,
1986 struct dentry *dentry, unsigned int flags)
1988 unsigned long vm_start, vm_end;
1989 struct vm_area_struct *vma;
1990 struct task_struct *task;
1992 struct mm_struct *mm;
1995 if (!capable(CAP_SYS_ADMIN))
1999 task = get_proc_task(dir);
2004 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2008 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2011 mm = get_task_mm(task);
2015 down_read(&mm->mmap_sem);
2016 vma = find_exact_vma(mm, vm_start, vm_end);
2021 result = proc_map_files_instantiate(dir, dentry, task,
2022 (void *)(unsigned long)vma->vm_file->f_mode);
2025 up_read(&mm->mmap_sem);
2028 put_task_struct(task);
2030 return ERR_PTR(result);
2033 static const struct inode_operations proc_map_files_inode_operations = {
2034 .lookup = proc_map_files_lookup,
2035 .permission = proc_fd_permission,
2036 .setattr = proc_setattr,
2040 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
2042 struct vm_area_struct *vma;
2043 struct task_struct *task;
2044 struct mm_struct *mm;
2045 unsigned long nr_files, pos, i;
2046 struct flex_array *fa = NULL;
2047 struct map_files_info info;
2048 struct map_files_info *p;
2052 if (!capable(CAP_SYS_ADMIN))
2056 task = get_proc_task(file_inode(file));
2061 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2065 if (!dir_emit_dots(file, ctx))
2068 mm = get_task_mm(task);
2071 down_read(&mm->mmap_sem);
2076 * We need two passes here:
2078 * 1) Collect vmas of mapped files with mmap_sem taken
2079 * 2) Release mmap_sem and instantiate entries
2081 * otherwise we get lockdep complained, since filldir()
2082 * routine might require mmap_sem taken in might_fault().
2085 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2086 if (vma->vm_file && ++pos > ctx->pos)
2091 fa = flex_array_alloc(sizeof(info), nr_files,
2093 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2097 flex_array_free(fa);
2098 up_read(&mm->mmap_sem);
2102 for (i = 0, vma = mm->mmap, pos = 2; vma;
2103 vma = vma->vm_next) {
2106 if (++pos <= ctx->pos)
2109 info.mode = vma->vm_file->f_mode;
2110 info.len = snprintf(info.name,
2111 sizeof(info.name), "%lx-%lx",
2112 vma->vm_start, vma->vm_end);
2113 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2117 up_read(&mm->mmap_sem);
2119 for (i = 0; i < nr_files; i++) {
2120 p = flex_array_get(fa, i);
2121 if (!proc_fill_cache(file, ctx,
2123 proc_map_files_instantiate,
2125 (void *)(unsigned long)p->mode))
2130 flex_array_free(fa);
2134 put_task_struct(task);
2139 static const struct file_operations proc_map_files_operations = {
2140 .read = generic_read_dir,
2141 .iterate = proc_map_files_readdir,
2142 .llseek = default_llseek,
2145 struct timers_private {
2147 struct task_struct *task;
2148 struct sighand_struct *sighand;
2149 struct pid_namespace *ns;
2150 unsigned long flags;
2153 static void *timers_start(struct seq_file *m, loff_t *pos)
2155 struct timers_private *tp = m->private;
2157 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2159 return ERR_PTR(-ESRCH);
2161 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2163 return ERR_PTR(-ESRCH);
2165 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2168 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2170 struct timers_private *tp = m->private;
2171 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2174 static void timers_stop(struct seq_file *m, void *v)
2176 struct timers_private *tp = m->private;
2179 unlock_task_sighand(tp->task, &tp->flags);
2184 put_task_struct(tp->task);
2189 static int show_timer(struct seq_file *m, void *v)
2191 struct k_itimer *timer;
2192 struct timers_private *tp = m->private;
2194 static const char * const nstr[] = {
2195 [SIGEV_SIGNAL] = "signal",
2196 [SIGEV_NONE] = "none",
2197 [SIGEV_THREAD] = "thread",
2200 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2201 notify = timer->it_sigev_notify;
2203 seq_printf(m, "ID: %d\n", timer->it_id);
2204 seq_printf(m, "signal: %d/%p\n",
2205 timer->sigq->info.si_signo,
2206 timer->sigq->info.si_value.sival_ptr);
2207 seq_printf(m, "notify: %s/%s.%d\n",
2208 nstr[notify & ~SIGEV_THREAD_ID],
2209 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2210 pid_nr_ns(timer->it_pid, tp->ns));
2211 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2216 static const struct seq_operations proc_timers_seq_ops = {
2217 .start = timers_start,
2218 .next = timers_next,
2219 .stop = timers_stop,
2223 static int proc_timers_open(struct inode *inode, struct file *file)
2225 struct timers_private *tp;
2227 tp = __seq_open_private(file, &proc_timers_seq_ops,
2228 sizeof(struct timers_private));
2232 tp->pid = proc_pid(inode);
2233 tp->ns = inode->i_sb->s_fs_info;
2237 static const struct file_operations proc_timers_operations = {
2238 .open = proc_timers_open,
2240 .llseek = seq_lseek,
2241 .release = seq_release_private,
2243 #endif /* CONFIG_CHECKPOINT_RESTORE */
2245 static int proc_pident_instantiate(struct inode *dir,
2246 struct dentry *dentry, struct task_struct *task, const void *ptr)
2248 const struct pid_entry *p = ptr;
2249 struct inode *inode;
2250 struct proc_inode *ei;
2252 inode = proc_pid_make_inode(dir->i_sb, task);
2257 inode->i_mode = p->mode;
2258 if (S_ISDIR(inode->i_mode))
2259 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2261 inode->i_op = p->iop;
2263 inode->i_fop = p->fop;
2265 d_set_d_op(dentry, &pid_dentry_operations);
2266 d_add(dentry, inode);
2267 /* Close the race of the process dying before we return the dentry */
2268 if (pid_revalidate(dentry, 0))
2274 static struct dentry *proc_pident_lookup(struct inode *dir,
2275 struct dentry *dentry,
2276 const struct pid_entry *ents,
2280 struct task_struct *task = get_proc_task(dir);
2281 const struct pid_entry *p, *last;
2289 * Yes, it does not scale. And it should not. Don't add
2290 * new entries into /proc/<tgid>/ without very good reasons.
2292 last = &ents[nents - 1];
2293 for (p = ents; p <= last; p++) {
2294 if (p->len != dentry->d_name.len)
2296 if (!memcmp(dentry->d_name.name, p->name, p->len))
2302 error = proc_pident_instantiate(dir, dentry, task, p);
2304 put_task_struct(task);
2306 return ERR_PTR(error);
2309 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2310 const struct pid_entry *ents, unsigned int nents)
2312 struct task_struct *task = get_proc_task(file_inode(file));
2313 const struct pid_entry *p;
2318 if (!dir_emit_dots(file, ctx))
2321 if (ctx->pos >= nents + 2)
2324 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2325 if (!proc_fill_cache(file, ctx, p->name, p->len,
2326 proc_pident_instantiate, task, p))
2331 put_task_struct(task);
2335 #ifdef CONFIG_SECURITY
2336 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2337 size_t count, loff_t *ppos)
2339 struct inode * inode = file_inode(file);
2342 struct task_struct *task = get_proc_task(inode);
2347 length = security_getprocattr(task,
2348 (char*)file->f_path.dentry->d_name.name,
2350 put_task_struct(task);
2352 length = simple_read_from_buffer(buf, count, ppos, p, length);
2357 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2358 size_t count, loff_t *ppos)
2360 struct inode * inode = file_inode(file);
2363 struct task_struct *task = get_proc_task(inode);
2368 if (count > PAGE_SIZE)
2371 /* No partial writes. */
2377 page = (char*)__get_free_page(GFP_TEMPORARY);
2382 if (copy_from_user(page, buf, count))
2385 /* Guard against adverse ptrace interaction */
2386 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2390 length = security_setprocattr(task,
2391 (char*)file->f_path.dentry->d_name.name,
2392 (void*)page, count);
2393 mutex_unlock(&task->signal->cred_guard_mutex);
2395 free_page((unsigned long) page);
2397 put_task_struct(task);
2402 static const struct file_operations proc_pid_attr_operations = {
2403 .read = proc_pid_attr_read,
2404 .write = proc_pid_attr_write,
2405 .llseek = generic_file_llseek,
2408 static const struct pid_entry attr_dir_stuff[] = {
2409 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2410 REG("prev", S_IRUGO, proc_pid_attr_operations),
2411 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2412 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2413 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2414 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2417 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2419 return proc_pident_readdir(file, ctx,
2420 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2423 static const struct file_operations proc_attr_dir_operations = {
2424 .read = generic_read_dir,
2425 .iterate = proc_attr_dir_readdir,
2426 .llseek = default_llseek,
2429 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2430 struct dentry *dentry, unsigned int flags)
2432 return proc_pident_lookup(dir, dentry,
2433 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2436 static const struct inode_operations proc_attr_dir_inode_operations = {
2437 .lookup = proc_attr_dir_lookup,
2438 .getattr = pid_getattr,
2439 .setattr = proc_setattr,
2444 #ifdef CONFIG_ELF_CORE
2445 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2446 size_t count, loff_t *ppos)
2448 struct task_struct *task = get_proc_task(file_inode(file));
2449 struct mm_struct *mm;
2450 char buffer[PROC_NUMBUF];
2458 mm = get_task_mm(task);
2460 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2461 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2462 MMF_DUMP_FILTER_SHIFT));
2464 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2467 put_task_struct(task);
2472 static ssize_t proc_coredump_filter_write(struct file *file,
2473 const char __user *buf,
2477 struct task_struct *task;
2478 struct mm_struct *mm;
2479 char buffer[PROC_NUMBUF], *end;
2486 memset(buffer, 0, sizeof(buffer));
2487 if (count > sizeof(buffer) - 1)
2488 count = sizeof(buffer) - 1;
2489 if (copy_from_user(buffer, buf, count))
2493 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2496 if (end - buffer == 0)
2500 task = get_proc_task(file_inode(file));
2505 mm = get_task_mm(task);
2509 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2511 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2513 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2518 put_task_struct(task);
2523 static const struct file_operations proc_coredump_filter_operations = {
2524 .read = proc_coredump_filter_read,
2525 .write = proc_coredump_filter_write,
2526 .llseek = generic_file_llseek,
2530 #ifdef CONFIG_TASK_IO_ACCOUNTING
2531 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2533 struct task_io_accounting acct = task->ioac;
2534 unsigned long flags;
2537 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2541 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2546 if (whole && lock_task_sighand(task, &flags)) {
2547 struct task_struct *t = task;
2549 task_io_accounting_add(&acct, &task->signal->ioac);
2550 while_each_thread(task, t)
2551 task_io_accounting_add(&acct, &t->ioac);
2553 unlock_task_sighand(task, &flags);
2560 "read_bytes: %llu\n"
2561 "write_bytes: %llu\n"
2562 "cancelled_write_bytes: %llu\n",
2563 (unsigned long long)acct.rchar,
2564 (unsigned long long)acct.wchar,
2565 (unsigned long long)acct.syscr,
2566 (unsigned long long)acct.syscw,
2567 (unsigned long long)acct.read_bytes,
2568 (unsigned long long)acct.write_bytes,
2569 (unsigned long long)acct.cancelled_write_bytes);
2573 mutex_unlock(&task->signal->cred_guard_mutex);
2577 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2578 struct pid *pid, struct task_struct *task)
2580 return do_io_accounting(task, m, 0);
2583 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2584 struct pid *pid, struct task_struct *task)
2586 return do_io_accounting(task, m, 1);
2588 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2590 #ifdef CONFIG_USER_NS
2591 static int proc_id_map_open(struct inode *inode, struct file *file,
2592 const struct seq_operations *seq_ops)
2594 struct user_namespace *ns = NULL;
2595 struct task_struct *task;
2596 struct seq_file *seq;
2599 task = get_proc_task(inode);
2602 ns = get_user_ns(task_cred_xxx(task, user_ns));
2604 put_task_struct(task);
2609 ret = seq_open(file, seq_ops);
2613 seq = file->private_data;
2623 static int proc_id_map_release(struct inode *inode, struct file *file)
2625 struct seq_file *seq = file->private_data;
2626 struct user_namespace *ns = seq->private;
2628 return seq_release(inode, file);
2631 static int proc_uid_map_open(struct inode *inode, struct file *file)
2633 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2636 static int proc_gid_map_open(struct inode *inode, struct file *file)
2638 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2641 static int proc_projid_map_open(struct inode *inode, struct file *file)
2643 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2646 static const struct file_operations proc_uid_map_operations = {
2647 .open = proc_uid_map_open,
2648 .write = proc_uid_map_write,
2650 .llseek = seq_lseek,
2651 .release = proc_id_map_release,
2654 static const struct file_operations proc_gid_map_operations = {
2655 .open = proc_gid_map_open,
2656 .write = proc_gid_map_write,
2658 .llseek = seq_lseek,
2659 .release = proc_id_map_release,
2662 static const struct file_operations proc_projid_map_operations = {
2663 .open = proc_projid_map_open,
2664 .write = proc_projid_map_write,
2666 .llseek = seq_lseek,
2667 .release = proc_id_map_release,
2670 static int proc_setgroups_open(struct inode *inode, struct file *file)
2672 struct user_namespace *ns = NULL;
2673 struct task_struct *task;
2677 task = get_proc_task(inode);
2680 ns = get_user_ns(task_cred_xxx(task, user_ns));
2682 put_task_struct(task);
2687 if (file->f_mode & FMODE_WRITE) {
2689 if (!ns_capable(ns, CAP_SYS_ADMIN))
2693 ret = single_open(file, &proc_setgroups_show, ns);
2704 static int proc_setgroups_release(struct inode *inode, struct file *file)
2706 struct seq_file *seq = file->private_data;
2707 struct user_namespace *ns = seq->private;
2708 int ret = single_release(inode, file);
2713 static const struct file_operations proc_setgroups_operations = {
2714 .open = proc_setgroups_open,
2715 .write = proc_setgroups_write,
2717 .llseek = seq_lseek,
2718 .release = proc_setgroups_release,
2720 #endif /* CONFIG_USER_NS */
2722 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2723 struct pid *pid, struct task_struct *task)
2725 int err = lock_trace(task);
2727 seq_printf(m, "%08x\n", task->personality);
2736 static const struct file_operations proc_task_operations;
2737 static const struct inode_operations proc_task_inode_operations;
2739 static const struct pid_entry tgid_base_stuff[] = {
2740 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2741 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2742 #ifdef CONFIG_CHECKPOINT_RESTORE
2743 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2745 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2746 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2748 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2750 REG("environ", S_IRUSR, proc_environ_operations),
2751 ONE("auxv", S_IRUSR, proc_pid_auxv),
2752 ONE("status", S_IRUGO, proc_pid_status),
2753 ONE("personality", S_IRUSR, proc_pid_personality),
2754 ONE("limits", S_IRUGO, proc_pid_limits),
2755 #ifdef CONFIG_SCHED_DEBUG
2756 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2758 #ifdef CONFIG_SCHED_AUTOGROUP
2759 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2761 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2762 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2763 ONE("syscall", S_IRUSR, proc_pid_syscall),
2765 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
2766 ONE("stat", S_IRUGO, proc_tgid_stat),
2767 ONE("statm", S_IRUGO, proc_pid_statm),
2768 REG("maps", S_IRUGO, proc_pid_maps_operations),
2770 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2772 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2773 LNK("cwd", proc_cwd_link),
2774 LNK("root", proc_root_link),
2775 LNK("exe", proc_exe_link),
2776 REG("mounts", S_IRUGO, proc_mounts_operations),
2777 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2778 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2779 #ifdef CONFIG_PROC_PAGE_MONITOR
2780 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2781 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2782 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2784 #ifdef CONFIG_SECURITY
2785 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2787 #ifdef CONFIG_KALLSYMS
2788 ONE("wchan", S_IRUGO, proc_pid_wchan),
2790 #ifdef CONFIG_STACKTRACE
2791 ONE("stack", S_IRUSR, proc_pid_stack),
2793 #ifdef CONFIG_SCHED_INFO
2794 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2796 #ifdef CONFIG_LATENCYTOP
2797 REG("latency", S_IRUGO, proc_lstats_operations),
2799 #ifdef CONFIG_PROC_PID_CPUSET
2800 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2802 #ifdef CONFIG_CGROUPS
2803 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2805 ONE("oom_score", S_IRUGO, proc_oom_score),
2806 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2807 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2808 #ifdef CONFIG_AUDITSYSCALL
2809 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2810 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2812 #ifdef CONFIG_FAULT_INJECTION
2813 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2815 #ifdef CONFIG_ELF_CORE
2816 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2818 #ifdef CONFIG_TASK_IO_ACCOUNTING
2819 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2821 #ifdef CONFIG_HARDWALL
2822 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2824 #ifdef CONFIG_USER_NS
2825 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2826 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2827 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2828 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2830 #ifdef CONFIG_CHECKPOINT_RESTORE
2831 REG("timers", S_IRUGO, proc_timers_operations),
2835 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2837 return proc_pident_readdir(file, ctx,
2838 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2841 static const struct file_operations proc_tgid_base_operations = {
2842 .read = generic_read_dir,
2843 .iterate = proc_tgid_base_readdir,
2844 .llseek = default_llseek,
2847 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2849 return proc_pident_lookup(dir, dentry,
2850 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2853 static const struct inode_operations proc_tgid_base_inode_operations = {
2854 .lookup = proc_tgid_base_lookup,
2855 .getattr = pid_getattr,
2856 .setattr = proc_setattr,
2857 .permission = proc_pid_permission,
2860 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2862 struct dentry *dentry, *leader, *dir;
2863 char buf[PROC_NUMBUF];
2867 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2868 /* no ->d_hash() rejects on procfs */
2869 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2871 d_invalidate(dentry);
2879 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2880 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2885 name.len = strlen(name.name);
2886 dir = d_hash_and_lookup(leader, &name);
2888 goto out_put_leader;
2891 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2892 dentry = d_hash_and_lookup(dir, &name);
2894 d_invalidate(dentry);
2906 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2907 * @task: task that should be flushed.
2909 * When flushing dentries from proc, one needs to flush them from global
2910 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2911 * in. This call is supposed to do all of this job.
2913 * Looks in the dcache for
2915 * /proc/@tgid/task/@pid
2916 * if either directory is present flushes it and all of it'ts children
2919 * It is safe and reasonable to cache /proc entries for a task until
2920 * that task exits. After that they just clog up the dcache with
2921 * useless entries, possibly causing useful dcache entries to be
2922 * flushed instead. This routine is proved to flush those useless
2923 * dcache entries at process exit time.
2925 * NOTE: This routine is just an optimization so it does not guarantee
2926 * that no dcache entries will exist at process exit time it
2927 * just makes it very unlikely that any will persist.
2930 void proc_flush_task(struct task_struct *task)
2933 struct pid *pid, *tgid;
2936 pid = task_pid(task);
2937 tgid = task_tgid(task);
2939 for (i = 0; i <= pid->level; i++) {
2940 upid = &pid->numbers[i];
2941 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2942 tgid->numbers[i].nr);
2946 static int proc_pid_instantiate(struct inode *dir,
2947 struct dentry * dentry,
2948 struct task_struct *task, const void *ptr)
2950 struct inode *inode;
2952 inode = proc_pid_make_inode(dir->i_sb, task);
2956 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2957 inode->i_op = &proc_tgid_base_inode_operations;
2958 inode->i_fop = &proc_tgid_base_operations;
2959 inode->i_flags|=S_IMMUTABLE;
2961 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2962 ARRAY_SIZE(tgid_base_stuff)));
2964 d_set_d_op(dentry, &pid_dentry_operations);
2966 d_add(dentry, inode);
2967 /* Close the race of the process dying before we return the dentry */
2968 if (pid_revalidate(dentry, 0))
2974 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2976 int result = -ENOENT;
2977 struct task_struct *task;
2979 struct pid_namespace *ns;
2981 tgid = name_to_int(&dentry->d_name);
2985 ns = dentry->d_sb->s_fs_info;
2987 task = find_task_by_pid_ns(tgid, ns);
2989 get_task_struct(task);
2994 result = proc_pid_instantiate(dir, dentry, task, NULL);
2995 put_task_struct(task);
2997 return ERR_PTR(result);
3001 * Find the first task with tgid >= tgid
3006 struct task_struct *task;
3008 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3013 put_task_struct(iter.task);
3017 pid = find_ge_pid(iter.tgid, ns);
3019 iter.tgid = pid_nr_ns(pid, ns);
3020 iter.task = pid_task(pid, PIDTYPE_PID);
3021 /* What we to know is if the pid we have find is the
3022 * pid of a thread_group_leader. Testing for task
3023 * being a thread_group_leader is the obvious thing
3024 * todo but there is a window when it fails, due to
3025 * the pid transfer logic in de_thread.
3027 * So we perform the straight forward test of seeing
3028 * if the pid we have found is the pid of a thread
3029 * group leader, and don't worry if the task we have
3030 * found doesn't happen to be a thread group leader.
3031 * As we don't care in the case of readdir.
3033 if (!iter.task || !has_group_leader_pid(iter.task)) {
3037 get_task_struct(iter.task);
3043 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3045 /* for the /proc/ directory itself, after non-process stuff has been done */
3046 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
3048 struct tgid_iter iter;
3049 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
3050 loff_t pos = ctx->pos;
3052 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3055 if (pos == TGID_OFFSET - 2) {
3056 struct inode *inode = d_inode(ns->proc_self);
3057 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
3059 ctx->pos = pos = pos + 1;
3061 if (pos == TGID_OFFSET - 1) {
3062 struct inode *inode = d_inode(ns->proc_thread_self);
3063 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
3065 ctx->pos = pos = pos + 1;
3067 iter.tgid = pos - TGID_OFFSET;
3069 for (iter = next_tgid(ns, iter);
3071 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3072 char name[PROC_NUMBUF];
3074 if (!has_pid_permissions(ns, iter.task, 2))
3077 len = snprintf(name, sizeof(name), "%d", iter.tgid);
3078 ctx->pos = iter.tgid + TGID_OFFSET;
3079 if (!proc_fill_cache(file, ctx, name, len,
3080 proc_pid_instantiate, iter.task, NULL)) {
3081 put_task_struct(iter.task);
3085 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
3092 static const struct pid_entry tid_base_stuff[] = {
3093 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3094 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3095 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3097 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3099 REG("environ", S_IRUSR, proc_environ_operations),
3100 ONE("auxv", S_IRUSR, proc_pid_auxv),
3101 ONE("status", S_IRUGO, proc_pid_status),
3102 ONE("personality", S_IRUSR, proc_pid_personality),
3103 ONE("limits", S_IRUGO, proc_pid_limits),
3104 #ifdef CONFIG_SCHED_DEBUG
3105 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3107 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3108 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3109 ONE("syscall", S_IRUSR, proc_pid_syscall),
3111 REG("cmdline", S_IRUGO, proc_pid_cmdline_ops),
3112 ONE("stat", S_IRUGO, proc_tid_stat),
3113 ONE("statm", S_IRUGO, proc_pid_statm),
3114 REG("maps", S_IRUGO, proc_tid_maps_operations),
3115 #ifdef CONFIG_PROC_CHILDREN
3116 REG("children", S_IRUGO, proc_tid_children_operations),
3119 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3121 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3122 LNK("cwd", proc_cwd_link),
3123 LNK("root", proc_root_link),
3124 LNK("exe", proc_exe_link),
3125 REG("mounts", S_IRUGO, proc_mounts_operations),
3126 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3127 #ifdef CONFIG_PROC_PAGE_MONITOR
3128 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3129 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3130 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3132 #ifdef CONFIG_SECURITY
3133 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3135 #ifdef CONFIG_KALLSYMS
3136 ONE("wchan", S_IRUGO, proc_pid_wchan),
3138 #ifdef CONFIG_STACKTRACE
3139 ONE("stack", S_IRUSR, proc_pid_stack),
3141 #ifdef CONFIG_SCHED_INFO
3142 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
3144 #ifdef CONFIG_LATENCYTOP
3145 REG("latency", S_IRUGO, proc_lstats_operations),
3147 #ifdef CONFIG_PROC_PID_CPUSET
3148 ONE("cpuset", S_IRUGO, proc_cpuset_show),
3150 #ifdef CONFIG_CGROUPS
3151 ONE("cgroup", S_IRUGO, proc_cgroup_show),
3153 ONE("oom_score", S_IRUGO, proc_oom_score),
3154 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3155 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3156 #ifdef CONFIG_AUDITSYSCALL
3157 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3158 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3160 #ifdef CONFIG_FAULT_INJECTION
3161 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3163 #ifdef CONFIG_TASK_IO_ACCOUNTING
3164 ONE("io", S_IRUSR, proc_tid_io_accounting),
3166 #ifdef CONFIG_HARDWALL
3167 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
3169 #ifdef CONFIG_USER_NS
3170 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3171 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3172 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3173 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3177 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
3179 return proc_pident_readdir(file, ctx,
3180 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3183 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3185 return proc_pident_lookup(dir, dentry,
3186 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3189 static const struct file_operations proc_tid_base_operations = {
3190 .read = generic_read_dir,
3191 .iterate = proc_tid_base_readdir,
3192 .llseek = default_llseek,
3195 static const struct inode_operations proc_tid_base_inode_operations = {
3196 .lookup = proc_tid_base_lookup,
3197 .getattr = pid_getattr,
3198 .setattr = proc_setattr,
3201 static int proc_task_instantiate(struct inode *dir,
3202 struct dentry *dentry, struct task_struct *task, const void *ptr)
3204 struct inode *inode;
3205 inode = proc_pid_make_inode(dir->i_sb, task);
3209 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3210 inode->i_op = &proc_tid_base_inode_operations;
3211 inode->i_fop = &proc_tid_base_operations;
3212 inode->i_flags|=S_IMMUTABLE;
3214 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3215 ARRAY_SIZE(tid_base_stuff)));
3217 d_set_d_op(dentry, &pid_dentry_operations);
3219 d_add(dentry, inode);
3220 /* Close the race of the process dying before we return the dentry */
3221 if (pid_revalidate(dentry, 0))
3227 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3229 int result = -ENOENT;
3230 struct task_struct *task;
3231 struct task_struct *leader = get_proc_task(dir);
3233 struct pid_namespace *ns;
3238 tid = name_to_int(&dentry->d_name);
3242 ns = dentry->d_sb->s_fs_info;
3244 task = find_task_by_pid_ns(tid, ns);
3246 get_task_struct(task);
3250 if (!same_thread_group(leader, task))
3253 result = proc_task_instantiate(dir, dentry, task, NULL);
3255 put_task_struct(task);
3257 put_task_struct(leader);
3259 return ERR_PTR(result);
3263 * Find the first tid of a thread group to return to user space.
3265 * Usually this is just the thread group leader, but if the users
3266 * buffer was too small or there was a seek into the middle of the
3267 * directory we have more work todo.
3269 * In the case of a short read we start with find_task_by_pid.
3271 * In the case of a seek we start with the leader and walk nr
3274 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3275 struct pid_namespace *ns)
3277 struct task_struct *pos, *task;
3278 unsigned long nr = f_pos;
3280 if (nr != f_pos) /* 32bit overflow? */
3284 task = pid_task(pid, PIDTYPE_PID);
3288 /* Attempt to start with the tid of a thread */
3290 pos = find_task_by_pid_ns(tid, ns);
3291 if (pos && same_thread_group(pos, task))
3295 /* If nr exceeds the number of threads there is nothing todo */
3296 if (nr >= get_nr_threads(task))
3299 /* If we haven't found our starting place yet start
3300 * with the leader and walk nr threads forward.
3302 pos = task = task->group_leader;
3306 } while_each_thread(task, pos);
3311 get_task_struct(pos);
3318 * Find the next thread in the thread list.
3319 * Return NULL if there is an error or no next thread.
3321 * The reference to the input task_struct is released.
3323 static struct task_struct *next_tid(struct task_struct *start)
3325 struct task_struct *pos = NULL;
3327 if (pid_alive(start)) {
3328 pos = next_thread(start);
3329 if (thread_group_leader(pos))
3332 get_task_struct(pos);
3335 put_task_struct(start);
3339 /* for the /proc/TGID/task/ directories */
3340 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3342 struct inode *inode = file_inode(file);
3343 struct task_struct *task;
3344 struct pid_namespace *ns;
3347 if (proc_inode_is_dead(inode))
3350 if (!dir_emit_dots(file, ctx))
3353 /* f_version caches the tgid value that the last readdir call couldn't
3354 * return. lseek aka telldir automagically resets f_version to 0.
3356 ns = inode->i_sb->s_fs_info;
3357 tid = (int)file->f_version;
3358 file->f_version = 0;
3359 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3361 task = next_tid(task), ctx->pos++) {
3362 char name[PROC_NUMBUF];
3364 tid = task_pid_nr_ns(task, ns);
3365 len = snprintf(name, sizeof(name), "%d", tid);
3366 if (!proc_fill_cache(file, ctx, name, len,
3367 proc_task_instantiate, task, NULL)) {
3368 /* returning this tgid failed, save it as the first
3369 * pid for the next readir call */
3370 file->f_version = (u64)tid;
3371 put_task_struct(task);
3379 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3381 struct inode *inode = d_inode(dentry);
3382 struct task_struct *p = get_proc_task(inode);
3383 generic_fillattr(inode, stat);
3386 stat->nlink += get_nr_threads(p);
3393 static const struct inode_operations proc_task_inode_operations = {
3394 .lookup = proc_task_lookup,
3395 .getattr = proc_task_getattr,
3396 .setattr = proc_setattr,
3397 .permission = proc_pid_permission,
3400 static const struct file_operations proc_task_operations = {
3401 .read = generic_read_dir,
3402 .iterate = proc_task_readdir,
3403 .llseek = default_llseek,