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(dentry->d_inode);
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(dentry->d_inode);
190 int result = -ENOENT;
193 result = get_task_root(task, path);
194 put_task_struct(task);
199 static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
200 struct pid *pid, struct task_struct *task)
203 * Rely on struct seq_operations::show() being called once
204 * per internal buffer allocation. See single_open(), traverse().
206 BUG_ON(m->size < PAGE_SIZE);
207 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
211 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
212 struct pid *pid, struct task_struct *task)
214 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
215 if (mm && !IS_ERR(mm)) {
216 unsigned int nwords = 0;
219 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
220 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
228 #ifdef CONFIG_KALLSYMS
230 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
231 * Returns the resolved symbol. If that fails, simply return the address.
233 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
234 struct pid *pid, struct task_struct *task)
237 char symname[KSYM_NAME_LEN];
239 wchan = get_wchan(task);
241 if (lookup_symbol_name(wchan, symname) < 0)
242 if (!ptrace_may_access(task, PTRACE_MODE_READ))
245 return seq_printf(m, "%lu", wchan);
247 return seq_printf(m, "%s", symname);
249 #endif /* CONFIG_KALLSYMS */
251 static int lock_trace(struct task_struct *task)
253 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
256 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
257 mutex_unlock(&task->signal->cred_guard_mutex);
263 static void unlock_trace(struct task_struct *task)
265 mutex_unlock(&task->signal->cred_guard_mutex);
268 #ifdef CONFIG_STACKTRACE
270 #define MAX_STACK_TRACE_DEPTH 64
272 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
273 struct pid *pid, struct task_struct *task)
275 struct stack_trace trace;
276 unsigned long *entries;
280 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
284 trace.nr_entries = 0;
285 trace.max_entries = MAX_STACK_TRACE_DEPTH;
286 trace.entries = entries;
289 err = lock_trace(task);
291 save_stack_trace_tsk(task, &trace);
293 for (i = 0; i < trace.nr_entries; i++) {
294 seq_printf(m, "[<%pK>] %pS\n",
295 (void *)entries[i], (void *)entries[i]);
305 #ifdef CONFIG_SCHEDSTATS
307 * Provides /proc/PID/schedstat
309 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
310 struct pid *pid, struct task_struct *task)
312 return seq_printf(m, "%llu %llu %lu\n",
313 (unsigned long long)task->se.sum_exec_runtime,
314 (unsigned long long)task->sched_info.run_delay,
315 task->sched_info.pcount);
319 #ifdef CONFIG_LATENCYTOP
320 static int lstats_show_proc(struct seq_file *m, void *v)
323 struct inode *inode = m->private;
324 struct task_struct *task = get_proc_task(inode);
328 seq_puts(m, "Latency Top version : v0.1\n");
329 for (i = 0; i < 32; i++) {
330 struct latency_record *lr = &task->latency_record[i];
331 if (lr->backtrace[0]) {
333 seq_printf(m, "%i %li %li",
334 lr->count, lr->time, lr->max);
335 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
336 unsigned long bt = lr->backtrace[q];
341 seq_printf(m, " %ps", (void *)bt);
347 put_task_struct(task);
351 static int lstats_open(struct inode *inode, struct file *file)
353 return single_open(file, lstats_show_proc, inode);
356 static ssize_t lstats_write(struct file *file, const char __user *buf,
357 size_t count, loff_t *offs)
359 struct task_struct *task = get_proc_task(file_inode(file));
363 clear_all_latency_tracing(task);
364 put_task_struct(task);
369 static const struct file_operations proc_lstats_operations = {
372 .write = lstats_write,
374 .release = single_release,
379 #ifdef CONFIG_CGROUPS
380 static int cgroup_open(struct inode *inode, struct file *file)
382 struct pid *pid = PROC_I(inode)->pid;
383 return single_open(file, proc_cgroup_show, pid);
386 static const struct file_operations proc_cgroup_operations = {
390 .release = single_release,
394 #ifdef CONFIG_PROC_PID_CPUSET
396 static int cpuset_open(struct inode *inode, struct file *file)
398 struct pid *pid = PROC_I(inode)->pid;
399 return single_open(file, proc_cpuset_show, pid);
402 static const struct file_operations proc_cpuset_operations = {
406 .release = single_release,
410 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
411 struct pid *pid, struct task_struct *task)
413 unsigned long totalpages = totalram_pages + total_swap_pages;
414 unsigned long points = 0;
416 read_lock(&tasklist_lock);
418 points = oom_badness(task, NULL, NULL, totalpages) *
420 read_unlock(&tasklist_lock);
421 return seq_printf(m, "%lu\n", points);
429 static const struct limit_names lnames[RLIM_NLIMITS] = {
430 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
431 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
432 [RLIMIT_DATA] = {"Max data size", "bytes"},
433 [RLIMIT_STACK] = {"Max stack size", "bytes"},
434 [RLIMIT_CORE] = {"Max core file size", "bytes"},
435 [RLIMIT_RSS] = {"Max resident set", "bytes"},
436 [RLIMIT_NPROC] = {"Max processes", "processes"},
437 [RLIMIT_NOFILE] = {"Max open files", "files"},
438 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
439 [RLIMIT_AS] = {"Max address space", "bytes"},
440 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
441 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
442 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
443 [RLIMIT_NICE] = {"Max nice priority", NULL},
444 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
445 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
448 /* Display limits for a process */
449 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
450 struct pid *pid, struct task_struct *task)
455 struct rlimit rlim[RLIM_NLIMITS];
457 if (!lock_task_sighand(task, &flags))
459 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
460 unlock_task_sighand(task, &flags);
463 * print the file header
465 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
466 "Limit", "Soft Limit", "Hard Limit", "Units");
468 for (i = 0; i < RLIM_NLIMITS; i++) {
469 if (rlim[i].rlim_cur == RLIM_INFINITY)
470 seq_printf(m, "%-25s %-20s ",
471 lnames[i].name, "unlimited");
473 seq_printf(m, "%-25s %-20lu ",
474 lnames[i].name, rlim[i].rlim_cur);
476 if (rlim[i].rlim_max == RLIM_INFINITY)
477 seq_printf(m, "%-20s ", "unlimited");
479 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
482 seq_printf(m, "%-10s\n", lnames[i].unit);
490 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
491 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
492 struct pid *pid, struct task_struct *task)
495 unsigned long args[6], sp, pc;
496 int res = lock_trace(task);
500 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
501 seq_puts(m, "running\n");
503 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
506 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
508 args[0], args[1], args[2], args[3], args[4], args[5],
513 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
515 /************************************************************************/
516 /* Here the fs part begins */
517 /************************************************************************/
519 /* permission checks */
520 static int proc_fd_access_allowed(struct inode *inode)
522 struct task_struct *task;
524 /* Allow access to a task's file descriptors if it is us or we
525 * may use ptrace attach to the process and find out that
528 task = get_proc_task(inode);
530 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
531 put_task_struct(task);
536 int proc_setattr(struct dentry *dentry, struct iattr *attr)
539 struct inode *inode = dentry->d_inode;
541 if (attr->ia_valid & ATTR_MODE)
544 error = inode_change_ok(inode, attr);
548 setattr_copy(inode, attr);
549 mark_inode_dirty(inode);
554 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
555 * or euid/egid (for hide_pid_min=2)?
557 static bool has_pid_permissions(struct pid_namespace *pid,
558 struct task_struct *task,
561 if (pid->hide_pid < hide_pid_min)
563 if (in_group_p(pid->pid_gid))
565 return ptrace_may_access(task, PTRACE_MODE_READ);
569 static int proc_pid_permission(struct inode *inode, int mask)
571 struct pid_namespace *pid = inode->i_sb->s_fs_info;
572 struct task_struct *task;
575 task = get_proc_task(inode);
578 has_perms = has_pid_permissions(pid, task, 1);
579 put_task_struct(task);
582 if (pid->hide_pid == 2) {
584 * Let's make getdents(), stat(), and open()
585 * consistent with each other. If a process
586 * may not stat() a file, it shouldn't be seen
594 return generic_permission(inode, mask);
599 static const struct inode_operations proc_def_inode_operations = {
600 .setattr = proc_setattr,
603 static int proc_single_show(struct seq_file *m, void *v)
605 struct inode *inode = m->private;
606 struct pid_namespace *ns;
608 struct task_struct *task;
611 ns = inode->i_sb->s_fs_info;
612 pid = proc_pid(inode);
613 task = get_pid_task(pid, PIDTYPE_PID);
617 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
619 put_task_struct(task);
623 static int proc_single_open(struct inode *inode, struct file *filp)
625 return single_open(filp, proc_single_show, inode);
628 static const struct file_operations proc_single_file_operations = {
629 .open = proc_single_open,
632 .release = single_release,
636 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
638 struct task_struct *task = get_proc_task(inode);
639 struct mm_struct *mm = ERR_PTR(-ESRCH);
642 mm = mm_access(task, mode);
643 put_task_struct(task);
645 if (!IS_ERR_OR_NULL(mm)) {
646 /* ensure this mm_struct can't be freed */
647 atomic_inc(&mm->mm_count);
648 /* but do not pin its memory */
656 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
658 struct mm_struct *mm = proc_mem_open(inode, mode);
663 file->private_data = mm;
667 static int mem_open(struct inode *inode, struct file *file)
669 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
671 /* OK to pass negative loff_t, we can catch out-of-range */
672 file->f_mode |= FMODE_UNSIGNED_OFFSET;
677 static ssize_t mem_rw(struct file *file, char __user *buf,
678 size_t count, loff_t *ppos, int write)
680 struct mm_struct *mm = file->private_data;
681 unsigned long addr = *ppos;
688 page = (char *)__get_free_page(GFP_TEMPORARY);
693 if (!atomic_inc_not_zero(&mm->mm_users))
697 int this_len = min_t(int, count, PAGE_SIZE);
699 if (write && copy_from_user(page, buf, this_len)) {
704 this_len = access_remote_vm(mm, addr, page, this_len, write);
711 if (!write && copy_to_user(buf, page, this_len)) {
725 free_page((unsigned long) page);
729 static ssize_t mem_read(struct file *file, char __user *buf,
730 size_t count, loff_t *ppos)
732 return mem_rw(file, buf, count, ppos, 0);
735 static ssize_t mem_write(struct file *file, const char __user *buf,
736 size_t count, loff_t *ppos)
738 return mem_rw(file, (char __user*)buf, count, ppos, 1);
741 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
745 file->f_pos = offset;
748 file->f_pos += offset;
753 force_successful_syscall_return();
757 static int mem_release(struct inode *inode, struct file *file)
759 struct mm_struct *mm = file->private_data;
765 static const struct file_operations proc_mem_operations = {
770 .release = mem_release,
773 static int environ_open(struct inode *inode, struct file *file)
775 return __mem_open(inode, file, PTRACE_MODE_READ);
778 static ssize_t environ_read(struct file *file, char __user *buf,
779 size_t count, loff_t *ppos)
782 unsigned long src = *ppos;
784 struct mm_struct *mm = file->private_data;
789 page = (char *)__get_free_page(GFP_TEMPORARY);
794 if (!atomic_inc_not_zero(&mm->mm_users))
797 size_t this_len, max_len;
800 if (src >= (mm->env_end - mm->env_start))
803 this_len = mm->env_end - (mm->env_start + src);
805 max_len = min_t(size_t, PAGE_SIZE, count);
806 this_len = min(max_len, this_len);
808 retval = access_remote_vm(mm, (mm->env_start + src),
816 if (copy_to_user(buf, page, retval)) {
830 free_page((unsigned long) page);
834 static const struct file_operations proc_environ_operations = {
835 .open = environ_open,
836 .read = environ_read,
837 .llseek = generic_file_llseek,
838 .release = mem_release,
841 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
844 struct task_struct *task = get_proc_task(file_inode(file));
845 char buffer[PROC_NUMBUF];
846 int oom_adj = OOM_ADJUST_MIN;
852 if (lock_task_sighand(task, &flags)) {
853 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
854 oom_adj = OOM_ADJUST_MAX;
856 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
858 unlock_task_sighand(task, &flags);
860 put_task_struct(task);
861 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
862 return simple_read_from_buffer(buf, count, ppos, buffer, len);
865 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
866 size_t count, loff_t *ppos)
868 struct task_struct *task;
869 char buffer[PROC_NUMBUF];
874 memset(buffer, 0, sizeof(buffer));
875 if (count > sizeof(buffer) - 1)
876 count = sizeof(buffer) - 1;
877 if (copy_from_user(buffer, buf, count)) {
882 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
885 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
886 oom_adj != OOM_DISABLE) {
891 task = get_proc_task(file_inode(file));
903 if (!lock_task_sighand(task, &flags)) {
909 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
910 * value is always attainable.
912 if (oom_adj == OOM_ADJUST_MAX)
913 oom_adj = OOM_SCORE_ADJ_MAX;
915 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
917 if (oom_adj < task->signal->oom_score_adj &&
918 !capable(CAP_SYS_RESOURCE)) {
924 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
925 * /proc/pid/oom_score_adj instead.
927 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
928 current->comm, task_pid_nr(current), task_pid_nr(task),
931 task->signal->oom_score_adj = oom_adj;
932 trace_oom_score_adj_update(task);
934 unlock_task_sighand(task, &flags);
937 put_task_struct(task);
939 return err < 0 ? err : count;
942 static const struct file_operations proc_oom_adj_operations = {
943 .read = oom_adj_read,
944 .write = oom_adj_write,
945 .llseek = generic_file_llseek,
948 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
949 size_t count, loff_t *ppos)
951 struct task_struct *task = get_proc_task(file_inode(file));
952 char buffer[PROC_NUMBUF];
953 short oom_score_adj = OOM_SCORE_ADJ_MIN;
959 if (lock_task_sighand(task, &flags)) {
960 oom_score_adj = task->signal->oom_score_adj;
961 unlock_task_sighand(task, &flags);
963 put_task_struct(task);
964 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
965 return simple_read_from_buffer(buf, count, ppos, buffer, len);
968 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
969 size_t count, loff_t *ppos)
971 struct task_struct *task;
972 char buffer[PROC_NUMBUF];
977 memset(buffer, 0, sizeof(buffer));
978 if (count > sizeof(buffer) - 1)
979 count = sizeof(buffer) - 1;
980 if (copy_from_user(buffer, buf, count)) {
985 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
988 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
989 oom_score_adj > OOM_SCORE_ADJ_MAX) {
994 task = get_proc_task(file_inode(file));
1006 if (!lock_task_sighand(task, &flags)) {
1011 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1012 !capable(CAP_SYS_RESOURCE)) {
1017 task->signal->oom_score_adj = (short)oom_score_adj;
1018 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1019 task->signal->oom_score_adj_min = (short)oom_score_adj;
1020 trace_oom_score_adj_update(task);
1023 unlock_task_sighand(task, &flags);
1026 put_task_struct(task);
1028 return err < 0 ? err : count;
1031 static const struct file_operations proc_oom_score_adj_operations = {
1032 .read = oom_score_adj_read,
1033 .write = oom_score_adj_write,
1034 .llseek = default_llseek,
1037 #ifdef CONFIG_AUDITSYSCALL
1038 #define TMPBUFLEN 21
1039 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1040 size_t count, loff_t *ppos)
1042 struct inode * inode = file_inode(file);
1043 struct task_struct *task = get_proc_task(inode);
1045 char tmpbuf[TMPBUFLEN];
1049 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1050 from_kuid(file->f_cred->user_ns,
1051 audit_get_loginuid(task)));
1052 put_task_struct(task);
1053 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1056 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1057 size_t count, loff_t *ppos)
1059 struct inode * inode = file_inode(file);
1066 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1072 if (count >= PAGE_SIZE)
1073 count = PAGE_SIZE - 1;
1076 /* No partial writes. */
1079 page = (char*)__get_free_page(GFP_TEMPORARY);
1083 if (copy_from_user(page, buf, count))
1087 loginuid = simple_strtoul(page, &tmp, 10);
1094 /* is userspace tring to explicitly UNSET the loginuid? */
1095 if (loginuid == AUDIT_UID_UNSET) {
1096 kloginuid = INVALID_UID;
1098 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1099 if (!uid_valid(kloginuid)) {
1105 length = audit_set_loginuid(kloginuid);
1106 if (likely(length == 0))
1110 free_page((unsigned long) page);
1114 static const struct file_operations proc_loginuid_operations = {
1115 .read = proc_loginuid_read,
1116 .write = proc_loginuid_write,
1117 .llseek = generic_file_llseek,
1120 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1121 size_t count, loff_t *ppos)
1123 struct inode * inode = file_inode(file);
1124 struct task_struct *task = get_proc_task(inode);
1126 char tmpbuf[TMPBUFLEN];
1130 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1131 audit_get_sessionid(task));
1132 put_task_struct(task);
1133 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1136 static const struct file_operations proc_sessionid_operations = {
1137 .read = proc_sessionid_read,
1138 .llseek = generic_file_llseek,
1142 #ifdef CONFIG_FAULT_INJECTION
1143 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1144 size_t count, loff_t *ppos)
1146 struct task_struct *task = get_proc_task(file_inode(file));
1147 char buffer[PROC_NUMBUF];
1153 make_it_fail = task->make_it_fail;
1154 put_task_struct(task);
1156 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1158 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1161 static ssize_t proc_fault_inject_write(struct file * file,
1162 const char __user * buf, size_t count, loff_t *ppos)
1164 struct task_struct *task;
1165 char buffer[PROC_NUMBUF], *end;
1168 if (!capable(CAP_SYS_RESOURCE))
1170 memset(buffer, 0, sizeof(buffer));
1171 if (count > sizeof(buffer) - 1)
1172 count = sizeof(buffer) - 1;
1173 if (copy_from_user(buffer, buf, count))
1175 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1178 if (make_it_fail < 0 || make_it_fail > 1)
1181 task = get_proc_task(file_inode(file));
1184 task->make_it_fail = make_it_fail;
1185 put_task_struct(task);
1190 static const struct file_operations proc_fault_inject_operations = {
1191 .read = proc_fault_inject_read,
1192 .write = proc_fault_inject_write,
1193 .llseek = generic_file_llseek,
1198 #ifdef CONFIG_SCHED_DEBUG
1200 * Print out various scheduling related per-task fields:
1202 static int sched_show(struct seq_file *m, void *v)
1204 struct inode *inode = m->private;
1205 struct task_struct *p;
1207 p = get_proc_task(inode);
1210 proc_sched_show_task(p, m);
1218 sched_write(struct file *file, const char __user *buf,
1219 size_t count, loff_t *offset)
1221 struct inode *inode = file_inode(file);
1222 struct task_struct *p;
1224 p = get_proc_task(inode);
1227 proc_sched_set_task(p);
1234 static int sched_open(struct inode *inode, struct file *filp)
1236 return single_open(filp, sched_show, inode);
1239 static const struct file_operations proc_pid_sched_operations = {
1242 .write = sched_write,
1243 .llseek = seq_lseek,
1244 .release = single_release,
1249 #ifdef CONFIG_SCHED_AUTOGROUP
1251 * Print out autogroup related information:
1253 static int sched_autogroup_show(struct seq_file *m, void *v)
1255 struct inode *inode = m->private;
1256 struct task_struct *p;
1258 p = get_proc_task(inode);
1261 proc_sched_autogroup_show_task(p, m);
1269 sched_autogroup_write(struct file *file, const char __user *buf,
1270 size_t count, loff_t *offset)
1272 struct inode *inode = file_inode(file);
1273 struct task_struct *p;
1274 char buffer[PROC_NUMBUF];
1278 memset(buffer, 0, sizeof(buffer));
1279 if (count > sizeof(buffer) - 1)
1280 count = sizeof(buffer) - 1;
1281 if (copy_from_user(buffer, buf, count))
1284 err = kstrtoint(strstrip(buffer), 0, &nice);
1288 p = get_proc_task(inode);
1292 err = proc_sched_autogroup_set_nice(p, nice);
1301 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1305 ret = single_open(filp, sched_autogroup_show, NULL);
1307 struct seq_file *m = filp->private_data;
1314 static const struct file_operations proc_pid_sched_autogroup_operations = {
1315 .open = sched_autogroup_open,
1317 .write = sched_autogroup_write,
1318 .llseek = seq_lseek,
1319 .release = single_release,
1322 #endif /* CONFIG_SCHED_AUTOGROUP */
1324 static ssize_t comm_write(struct file *file, const char __user *buf,
1325 size_t count, loff_t *offset)
1327 struct inode *inode = file_inode(file);
1328 struct task_struct *p;
1329 char buffer[TASK_COMM_LEN];
1330 const size_t maxlen = sizeof(buffer) - 1;
1332 memset(buffer, 0, sizeof(buffer));
1333 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1336 p = get_proc_task(inode);
1340 if (same_thread_group(current, p))
1341 set_task_comm(p, buffer);
1350 static int comm_show(struct seq_file *m, void *v)
1352 struct inode *inode = m->private;
1353 struct task_struct *p;
1355 p = get_proc_task(inode);
1360 seq_printf(m, "%s\n", p->comm);
1368 static int comm_open(struct inode *inode, struct file *filp)
1370 return single_open(filp, comm_show, inode);
1373 static const struct file_operations proc_pid_set_comm_operations = {
1376 .write = comm_write,
1377 .llseek = seq_lseek,
1378 .release = single_release,
1381 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1383 struct task_struct *task;
1384 struct mm_struct *mm;
1385 struct file *exe_file;
1387 task = get_proc_task(dentry->d_inode);
1390 mm = get_task_mm(task);
1391 put_task_struct(task);
1394 exe_file = get_mm_exe_file(mm);
1397 *exe_path = exe_file->f_path;
1398 path_get(&exe_file->f_path);
1405 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1407 struct inode *inode = dentry->d_inode;
1409 int error = -EACCES;
1411 /* Are we allowed to snoop on the tasks file descriptors? */
1412 if (!proc_fd_access_allowed(inode))
1415 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1419 nd_jump_link(nd, &path);
1422 return ERR_PTR(error);
1425 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1427 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1434 pathname = d_path(path, tmp, PAGE_SIZE);
1435 len = PTR_ERR(pathname);
1436 if (IS_ERR(pathname))
1438 len = tmp + PAGE_SIZE - 1 - pathname;
1442 if (copy_to_user(buffer, pathname, len))
1445 free_page((unsigned long)tmp);
1449 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1451 int error = -EACCES;
1452 struct inode *inode = dentry->d_inode;
1455 /* Are we allowed to snoop on the tasks file descriptors? */
1456 if (!proc_fd_access_allowed(inode))
1459 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1463 error = do_proc_readlink(&path, buffer, buflen);
1469 const struct inode_operations proc_pid_link_inode_operations = {
1470 .readlink = proc_pid_readlink,
1471 .follow_link = proc_pid_follow_link,
1472 .setattr = proc_setattr,
1476 /* building an inode */
1478 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1480 struct inode * inode;
1481 struct proc_inode *ei;
1482 const struct cred *cred;
1484 /* We need a new inode */
1486 inode = new_inode(sb);
1492 inode->i_ino = get_next_ino();
1493 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1494 inode->i_op = &proc_def_inode_operations;
1497 * grab the reference to task.
1499 ei->pid = get_task_pid(task, PIDTYPE_PID);
1503 if (task_dumpable(task)) {
1505 cred = __task_cred(task);
1506 inode->i_uid = cred->euid;
1507 inode->i_gid = cred->egid;
1510 security_task_to_inode(task, inode);
1520 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1522 struct inode *inode = dentry->d_inode;
1523 struct task_struct *task;
1524 const struct cred *cred;
1525 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1527 generic_fillattr(inode, stat);
1530 stat->uid = GLOBAL_ROOT_UID;
1531 stat->gid = GLOBAL_ROOT_GID;
1532 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1534 if (!has_pid_permissions(pid, task, 2)) {
1537 * This doesn't prevent learning whether PID exists,
1538 * it only makes getattr() consistent with readdir().
1542 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1543 task_dumpable(task)) {
1544 cred = __task_cred(task);
1545 stat->uid = cred->euid;
1546 stat->gid = cred->egid;
1556 * Exceptional case: normally we are not allowed to unhash a busy
1557 * directory. In this case, however, we can do it - no aliasing problems
1558 * due to the way we treat inodes.
1560 * Rewrite the inode's ownerships here because the owning task may have
1561 * performed a setuid(), etc.
1563 * Before the /proc/pid/status file was created the only way to read
1564 * the effective uid of a /process was to stat /proc/pid. Reading
1565 * /proc/pid/status is slow enough that procps and other packages
1566 * kept stating /proc/pid. To keep the rules in /proc simple I have
1567 * made this apply to all per process world readable and executable
1570 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1572 struct inode *inode;
1573 struct task_struct *task;
1574 const struct cred *cred;
1576 if (flags & LOOKUP_RCU)
1579 inode = dentry->d_inode;
1580 task = get_proc_task(inode);
1583 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1584 task_dumpable(task)) {
1586 cred = __task_cred(task);
1587 inode->i_uid = cred->euid;
1588 inode->i_gid = cred->egid;
1591 inode->i_uid = GLOBAL_ROOT_UID;
1592 inode->i_gid = GLOBAL_ROOT_GID;
1594 inode->i_mode &= ~(S_ISUID | S_ISGID);
1595 security_task_to_inode(task, inode);
1596 put_task_struct(task);
1603 static inline bool proc_inode_is_dead(struct inode *inode)
1605 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1608 int pid_delete_dentry(const struct dentry *dentry)
1610 /* Is the task we represent dead?
1611 * If so, then don't put the dentry on the lru list,
1612 * kill it immediately.
1614 return proc_inode_is_dead(dentry->d_inode);
1617 const struct dentry_operations pid_dentry_operations =
1619 .d_revalidate = pid_revalidate,
1620 .d_delete = pid_delete_dentry,
1626 * Fill a directory entry.
1628 * If possible create the dcache entry and derive our inode number and
1629 * file type from dcache entry.
1631 * Since all of the proc inode numbers are dynamically generated, the inode
1632 * numbers do not exist until the inode is cache. This means creating the
1633 * the dcache entry in readdir is necessary to keep the inode numbers
1634 * reported by readdir in sync with the inode numbers reported
1637 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1638 const char *name, int len,
1639 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1641 struct dentry *child, *dir = file->f_path.dentry;
1642 struct qstr qname = QSTR_INIT(name, len);
1643 struct inode *inode;
1647 child = d_hash_and_lookup(dir, &qname);
1649 child = d_alloc(dir, &qname);
1651 goto end_instantiate;
1652 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1654 goto end_instantiate;
1657 inode = child->d_inode;
1659 type = inode->i_mode >> 12;
1661 return dir_emit(ctx, name, len, ino, type);
1664 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1667 #ifdef CONFIG_CHECKPOINT_RESTORE
1670 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1671 * which represent vma start and end addresses.
1673 static int dname_to_vma_addr(struct dentry *dentry,
1674 unsigned long *start, unsigned long *end)
1676 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1682 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1684 unsigned long vm_start, vm_end;
1685 bool exact_vma_exists = false;
1686 struct mm_struct *mm = NULL;
1687 struct task_struct *task;
1688 const struct cred *cred;
1689 struct inode *inode;
1692 if (flags & LOOKUP_RCU)
1695 if (!capable(CAP_SYS_ADMIN)) {
1700 inode = dentry->d_inode;
1701 task = get_proc_task(inode);
1705 mm = mm_access(task, PTRACE_MODE_READ);
1706 if (IS_ERR_OR_NULL(mm))
1709 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1710 down_read(&mm->mmap_sem);
1711 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1712 up_read(&mm->mmap_sem);
1717 if (exact_vma_exists) {
1718 if (task_dumpable(task)) {
1720 cred = __task_cred(task);
1721 inode->i_uid = cred->euid;
1722 inode->i_gid = cred->egid;
1725 inode->i_uid = GLOBAL_ROOT_UID;
1726 inode->i_gid = GLOBAL_ROOT_GID;
1728 security_task_to_inode(task, inode);
1733 put_task_struct(task);
1742 static const struct dentry_operations tid_map_files_dentry_operations = {
1743 .d_revalidate = map_files_d_revalidate,
1744 .d_delete = pid_delete_dentry,
1747 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1749 unsigned long vm_start, vm_end;
1750 struct vm_area_struct *vma;
1751 struct task_struct *task;
1752 struct mm_struct *mm;
1756 task = get_proc_task(dentry->d_inode);
1760 mm = get_task_mm(task);
1761 put_task_struct(task);
1765 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1770 down_read(&mm->mmap_sem);
1771 vma = find_exact_vma(mm, vm_start, vm_end);
1772 if (vma && vma->vm_file) {
1773 *path = vma->vm_file->f_path;
1777 up_read(&mm->mmap_sem);
1785 struct map_files_info {
1788 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1792 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1793 struct task_struct *task, const void *ptr)
1795 fmode_t mode = (fmode_t)(unsigned long)ptr;
1796 struct proc_inode *ei;
1797 struct inode *inode;
1799 inode = proc_pid_make_inode(dir->i_sb, task);
1804 ei->op.proc_get_link = proc_map_files_get_link;
1806 inode->i_op = &proc_pid_link_inode_operations;
1808 inode->i_mode = S_IFLNK;
1810 if (mode & FMODE_READ)
1811 inode->i_mode |= S_IRUSR;
1812 if (mode & FMODE_WRITE)
1813 inode->i_mode |= S_IWUSR;
1815 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1816 d_add(dentry, inode);
1821 static struct dentry *proc_map_files_lookup(struct inode *dir,
1822 struct dentry *dentry, unsigned int flags)
1824 unsigned long vm_start, vm_end;
1825 struct vm_area_struct *vma;
1826 struct task_struct *task;
1828 struct mm_struct *mm;
1831 if (!capable(CAP_SYS_ADMIN))
1835 task = get_proc_task(dir);
1840 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1844 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1847 mm = get_task_mm(task);
1851 down_read(&mm->mmap_sem);
1852 vma = find_exact_vma(mm, vm_start, vm_end);
1857 result = proc_map_files_instantiate(dir, dentry, task,
1858 (void *)(unsigned long)vma->vm_file->f_mode);
1861 up_read(&mm->mmap_sem);
1864 put_task_struct(task);
1866 return ERR_PTR(result);
1869 static const struct inode_operations proc_map_files_inode_operations = {
1870 .lookup = proc_map_files_lookup,
1871 .permission = proc_fd_permission,
1872 .setattr = proc_setattr,
1876 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1878 struct vm_area_struct *vma;
1879 struct task_struct *task;
1880 struct mm_struct *mm;
1881 unsigned long nr_files, pos, i;
1882 struct flex_array *fa = NULL;
1883 struct map_files_info info;
1884 struct map_files_info *p;
1888 if (!capable(CAP_SYS_ADMIN))
1892 task = get_proc_task(file_inode(file));
1897 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1901 if (!dir_emit_dots(file, ctx))
1904 mm = get_task_mm(task);
1907 down_read(&mm->mmap_sem);
1912 * We need two passes here:
1914 * 1) Collect vmas of mapped files with mmap_sem taken
1915 * 2) Release mmap_sem and instantiate entries
1917 * otherwise we get lockdep complained, since filldir()
1918 * routine might require mmap_sem taken in might_fault().
1921 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1922 if (vma->vm_file && ++pos > ctx->pos)
1927 fa = flex_array_alloc(sizeof(info), nr_files,
1929 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1933 flex_array_free(fa);
1934 up_read(&mm->mmap_sem);
1938 for (i = 0, vma = mm->mmap, pos = 2; vma;
1939 vma = vma->vm_next) {
1942 if (++pos <= ctx->pos)
1945 info.mode = vma->vm_file->f_mode;
1946 info.len = snprintf(info.name,
1947 sizeof(info.name), "%lx-%lx",
1948 vma->vm_start, vma->vm_end);
1949 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1953 up_read(&mm->mmap_sem);
1955 for (i = 0; i < nr_files; i++) {
1956 p = flex_array_get(fa, i);
1957 if (!proc_fill_cache(file, ctx,
1959 proc_map_files_instantiate,
1961 (void *)(unsigned long)p->mode))
1966 flex_array_free(fa);
1970 put_task_struct(task);
1975 static const struct file_operations proc_map_files_operations = {
1976 .read = generic_read_dir,
1977 .iterate = proc_map_files_readdir,
1978 .llseek = default_llseek,
1981 struct timers_private {
1983 struct task_struct *task;
1984 struct sighand_struct *sighand;
1985 struct pid_namespace *ns;
1986 unsigned long flags;
1989 static void *timers_start(struct seq_file *m, loff_t *pos)
1991 struct timers_private *tp = m->private;
1993 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
1995 return ERR_PTR(-ESRCH);
1997 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
1999 return ERR_PTR(-ESRCH);
2001 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2004 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2006 struct timers_private *tp = m->private;
2007 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2010 static void timers_stop(struct seq_file *m, void *v)
2012 struct timers_private *tp = m->private;
2015 unlock_task_sighand(tp->task, &tp->flags);
2020 put_task_struct(tp->task);
2025 static int show_timer(struct seq_file *m, void *v)
2027 struct k_itimer *timer;
2028 struct timers_private *tp = m->private;
2030 static const char * const nstr[] = {
2031 [SIGEV_SIGNAL] = "signal",
2032 [SIGEV_NONE] = "none",
2033 [SIGEV_THREAD] = "thread",
2036 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2037 notify = timer->it_sigev_notify;
2039 seq_printf(m, "ID: %d\n", timer->it_id);
2040 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2041 timer->sigq->info.si_value.sival_ptr);
2042 seq_printf(m, "notify: %s/%s.%d\n",
2043 nstr[notify & ~SIGEV_THREAD_ID],
2044 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2045 pid_nr_ns(timer->it_pid, tp->ns));
2046 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2051 static const struct seq_operations proc_timers_seq_ops = {
2052 .start = timers_start,
2053 .next = timers_next,
2054 .stop = timers_stop,
2058 static int proc_timers_open(struct inode *inode, struct file *file)
2060 struct timers_private *tp;
2062 tp = __seq_open_private(file, &proc_timers_seq_ops,
2063 sizeof(struct timers_private));
2067 tp->pid = proc_pid(inode);
2068 tp->ns = inode->i_sb->s_fs_info;
2072 static const struct file_operations proc_timers_operations = {
2073 .open = proc_timers_open,
2075 .llseek = seq_lseek,
2076 .release = seq_release_private,
2078 #endif /* CONFIG_CHECKPOINT_RESTORE */
2080 static int proc_pident_instantiate(struct inode *dir,
2081 struct dentry *dentry, struct task_struct *task, const void *ptr)
2083 const struct pid_entry *p = ptr;
2084 struct inode *inode;
2085 struct proc_inode *ei;
2087 inode = proc_pid_make_inode(dir->i_sb, task);
2092 inode->i_mode = p->mode;
2093 if (S_ISDIR(inode->i_mode))
2094 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2096 inode->i_op = p->iop;
2098 inode->i_fop = p->fop;
2100 d_set_d_op(dentry, &pid_dentry_operations);
2101 d_add(dentry, inode);
2102 /* Close the race of the process dying before we return the dentry */
2103 if (pid_revalidate(dentry, 0))
2109 static struct dentry *proc_pident_lookup(struct inode *dir,
2110 struct dentry *dentry,
2111 const struct pid_entry *ents,
2115 struct task_struct *task = get_proc_task(dir);
2116 const struct pid_entry *p, *last;
2124 * Yes, it does not scale. And it should not. Don't add
2125 * new entries into /proc/<tgid>/ without very good reasons.
2127 last = &ents[nents - 1];
2128 for (p = ents; p <= last; p++) {
2129 if (p->len != dentry->d_name.len)
2131 if (!memcmp(dentry->d_name.name, p->name, p->len))
2137 error = proc_pident_instantiate(dir, dentry, task, p);
2139 put_task_struct(task);
2141 return ERR_PTR(error);
2144 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2145 const struct pid_entry *ents, unsigned int nents)
2147 struct task_struct *task = get_proc_task(file_inode(file));
2148 const struct pid_entry *p;
2153 if (!dir_emit_dots(file, ctx))
2156 if (ctx->pos >= nents + 2)
2159 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2160 if (!proc_fill_cache(file, ctx, p->name, p->len,
2161 proc_pident_instantiate, task, p))
2166 put_task_struct(task);
2170 #ifdef CONFIG_SECURITY
2171 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2172 size_t count, loff_t *ppos)
2174 struct inode * inode = file_inode(file);
2177 struct task_struct *task = get_proc_task(inode);
2182 length = security_getprocattr(task,
2183 (char*)file->f_path.dentry->d_name.name,
2185 put_task_struct(task);
2187 length = simple_read_from_buffer(buf, count, ppos, p, length);
2192 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2193 size_t count, loff_t *ppos)
2195 struct inode * inode = file_inode(file);
2198 struct task_struct *task = get_proc_task(inode);
2203 if (count > PAGE_SIZE)
2206 /* No partial writes. */
2212 page = (char*)__get_free_page(GFP_TEMPORARY);
2217 if (copy_from_user(page, buf, count))
2220 /* Guard against adverse ptrace interaction */
2221 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2225 length = security_setprocattr(task,
2226 (char*)file->f_path.dentry->d_name.name,
2227 (void*)page, count);
2228 mutex_unlock(&task->signal->cred_guard_mutex);
2230 free_page((unsigned long) page);
2232 put_task_struct(task);
2237 static const struct file_operations proc_pid_attr_operations = {
2238 .read = proc_pid_attr_read,
2239 .write = proc_pid_attr_write,
2240 .llseek = generic_file_llseek,
2243 static const struct pid_entry attr_dir_stuff[] = {
2244 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2245 REG("prev", S_IRUGO, proc_pid_attr_operations),
2246 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2247 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2248 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2249 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2252 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2254 return proc_pident_readdir(file, ctx,
2255 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2258 static const struct file_operations proc_attr_dir_operations = {
2259 .read = generic_read_dir,
2260 .iterate = proc_attr_dir_readdir,
2261 .llseek = default_llseek,
2264 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2265 struct dentry *dentry, unsigned int flags)
2267 return proc_pident_lookup(dir, dentry,
2268 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2271 static const struct inode_operations proc_attr_dir_inode_operations = {
2272 .lookup = proc_attr_dir_lookup,
2273 .getattr = pid_getattr,
2274 .setattr = proc_setattr,
2279 #ifdef CONFIG_ELF_CORE
2280 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2281 size_t count, loff_t *ppos)
2283 struct task_struct *task = get_proc_task(file_inode(file));
2284 struct mm_struct *mm;
2285 char buffer[PROC_NUMBUF];
2293 mm = get_task_mm(task);
2295 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2296 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2297 MMF_DUMP_FILTER_SHIFT));
2299 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2302 put_task_struct(task);
2307 static ssize_t proc_coredump_filter_write(struct file *file,
2308 const char __user *buf,
2312 struct task_struct *task;
2313 struct mm_struct *mm;
2314 char buffer[PROC_NUMBUF], *end;
2321 memset(buffer, 0, sizeof(buffer));
2322 if (count > sizeof(buffer) - 1)
2323 count = sizeof(buffer) - 1;
2324 if (copy_from_user(buffer, buf, count))
2328 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2331 if (end - buffer == 0)
2335 task = get_proc_task(file_inode(file));
2340 mm = get_task_mm(task);
2344 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2346 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2348 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2353 put_task_struct(task);
2358 static const struct file_operations proc_coredump_filter_operations = {
2359 .read = proc_coredump_filter_read,
2360 .write = proc_coredump_filter_write,
2361 .llseek = generic_file_llseek,
2365 #ifdef CONFIG_TASK_IO_ACCOUNTING
2366 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2368 struct task_io_accounting acct = task->ioac;
2369 unsigned long flags;
2372 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2376 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2381 if (whole && lock_task_sighand(task, &flags)) {
2382 struct task_struct *t = task;
2384 task_io_accounting_add(&acct, &task->signal->ioac);
2385 while_each_thread(task, t)
2386 task_io_accounting_add(&acct, &t->ioac);
2388 unlock_task_sighand(task, &flags);
2390 result = seq_printf(m,
2395 "read_bytes: %llu\n"
2396 "write_bytes: %llu\n"
2397 "cancelled_write_bytes: %llu\n",
2398 (unsigned long long)acct.rchar,
2399 (unsigned long long)acct.wchar,
2400 (unsigned long long)acct.syscr,
2401 (unsigned long long)acct.syscw,
2402 (unsigned long long)acct.read_bytes,
2403 (unsigned long long)acct.write_bytes,
2404 (unsigned long long)acct.cancelled_write_bytes);
2406 mutex_unlock(&task->signal->cred_guard_mutex);
2410 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2411 struct pid *pid, struct task_struct *task)
2413 return do_io_accounting(task, m, 0);
2416 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2417 struct pid *pid, struct task_struct *task)
2419 return do_io_accounting(task, m, 1);
2421 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2423 #ifdef CONFIG_USER_NS
2424 static int proc_id_map_open(struct inode *inode, struct file *file,
2425 const struct seq_operations *seq_ops)
2427 struct user_namespace *ns = NULL;
2428 struct task_struct *task;
2429 struct seq_file *seq;
2432 task = get_proc_task(inode);
2435 ns = get_user_ns(task_cred_xxx(task, user_ns));
2437 put_task_struct(task);
2442 ret = seq_open(file, seq_ops);
2446 seq = file->private_data;
2456 static int proc_id_map_release(struct inode *inode, struct file *file)
2458 struct seq_file *seq = file->private_data;
2459 struct user_namespace *ns = seq->private;
2461 return seq_release(inode, file);
2464 static int proc_uid_map_open(struct inode *inode, struct file *file)
2466 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2469 static int proc_gid_map_open(struct inode *inode, struct file *file)
2471 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2474 static int proc_projid_map_open(struct inode *inode, struct file *file)
2476 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2479 static const struct file_operations proc_uid_map_operations = {
2480 .open = proc_uid_map_open,
2481 .write = proc_uid_map_write,
2483 .llseek = seq_lseek,
2484 .release = proc_id_map_release,
2487 static const struct file_operations proc_gid_map_operations = {
2488 .open = proc_gid_map_open,
2489 .write = proc_gid_map_write,
2491 .llseek = seq_lseek,
2492 .release = proc_id_map_release,
2495 static const struct file_operations proc_projid_map_operations = {
2496 .open = proc_projid_map_open,
2497 .write = proc_projid_map_write,
2499 .llseek = seq_lseek,
2500 .release = proc_id_map_release,
2502 #endif /* CONFIG_USER_NS */
2504 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2505 struct pid *pid, struct task_struct *task)
2507 int err = lock_trace(task);
2509 seq_printf(m, "%08x\n", task->personality);
2518 static const struct file_operations proc_task_operations;
2519 static const struct inode_operations proc_task_inode_operations;
2521 static const struct pid_entry tgid_base_stuff[] = {
2522 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2523 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2524 #ifdef CONFIG_CHECKPOINT_RESTORE
2525 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2527 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2528 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2530 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2532 REG("environ", S_IRUSR, proc_environ_operations),
2533 ONE("auxv", S_IRUSR, proc_pid_auxv),
2534 ONE("status", S_IRUGO, proc_pid_status),
2535 ONE("personality", S_IRUSR, proc_pid_personality),
2536 ONE("limits", S_IRUGO, proc_pid_limits),
2537 #ifdef CONFIG_SCHED_DEBUG
2538 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2540 #ifdef CONFIG_SCHED_AUTOGROUP
2541 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2543 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2544 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2545 ONE("syscall", S_IRUSR, proc_pid_syscall),
2547 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2548 ONE("stat", S_IRUGO, proc_tgid_stat),
2549 ONE("statm", S_IRUGO, proc_pid_statm),
2550 REG("maps", S_IRUGO, proc_pid_maps_operations),
2552 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2554 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2555 LNK("cwd", proc_cwd_link),
2556 LNK("root", proc_root_link),
2557 LNK("exe", proc_exe_link),
2558 REG("mounts", S_IRUGO, proc_mounts_operations),
2559 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2560 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2561 #ifdef CONFIG_PROC_PAGE_MONITOR
2562 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2563 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2564 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2566 #ifdef CONFIG_SECURITY
2567 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2569 #ifdef CONFIG_KALLSYMS
2570 ONE("wchan", S_IRUGO, proc_pid_wchan),
2572 #ifdef CONFIG_STACKTRACE
2573 ONE("stack", S_IRUSR, proc_pid_stack),
2575 #ifdef CONFIG_SCHEDSTATS
2576 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2578 #ifdef CONFIG_LATENCYTOP
2579 REG("latency", S_IRUGO, proc_lstats_operations),
2581 #ifdef CONFIG_PROC_PID_CPUSET
2582 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2584 #ifdef CONFIG_CGROUPS
2585 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2587 ONE("oom_score", S_IRUGO, proc_oom_score),
2588 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2589 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2590 #ifdef CONFIG_AUDITSYSCALL
2591 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2592 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2594 #ifdef CONFIG_FAULT_INJECTION
2595 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2597 #ifdef CONFIG_ELF_CORE
2598 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2600 #ifdef CONFIG_TASK_IO_ACCOUNTING
2601 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2603 #ifdef CONFIG_HARDWALL
2604 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2606 #ifdef CONFIG_USER_NS
2607 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2608 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2609 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2611 #ifdef CONFIG_CHECKPOINT_RESTORE
2612 REG("timers", S_IRUGO, proc_timers_operations),
2616 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2618 return proc_pident_readdir(file, ctx,
2619 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2622 static const struct file_operations proc_tgid_base_operations = {
2623 .read = generic_read_dir,
2624 .iterate = proc_tgid_base_readdir,
2625 .llseek = default_llseek,
2628 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2630 return proc_pident_lookup(dir, dentry,
2631 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2634 static const struct inode_operations proc_tgid_base_inode_operations = {
2635 .lookup = proc_tgid_base_lookup,
2636 .getattr = pid_getattr,
2637 .setattr = proc_setattr,
2638 .permission = proc_pid_permission,
2641 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2643 struct dentry *dentry, *leader, *dir;
2644 char buf[PROC_NUMBUF];
2648 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2649 /* no ->d_hash() rejects on procfs */
2650 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2652 shrink_dcache_parent(dentry);
2658 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2659 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2664 name.len = strlen(name.name);
2665 dir = d_hash_and_lookup(leader, &name);
2667 goto out_put_leader;
2670 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2671 dentry = d_hash_and_lookup(dir, &name);
2673 shrink_dcache_parent(dentry);
2686 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2687 * @task: task that should be flushed.
2689 * When flushing dentries from proc, one needs to flush them from global
2690 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2691 * in. This call is supposed to do all of this job.
2693 * Looks in the dcache for
2695 * /proc/@tgid/task/@pid
2696 * if either directory is present flushes it and all of it'ts children
2699 * It is safe and reasonable to cache /proc entries for a task until
2700 * that task exits. After that they just clog up the dcache with
2701 * useless entries, possibly causing useful dcache entries to be
2702 * flushed instead. This routine is proved to flush those useless
2703 * dcache entries at process exit time.
2705 * NOTE: This routine is just an optimization so it does not guarantee
2706 * that no dcache entries will exist at process exit time it
2707 * just makes it very unlikely that any will persist.
2710 void proc_flush_task(struct task_struct *task)
2713 struct pid *pid, *tgid;
2716 pid = task_pid(task);
2717 tgid = task_tgid(task);
2719 for (i = 0; i <= pid->level; i++) {
2720 upid = &pid->numbers[i];
2721 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2722 tgid->numbers[i].nr);
2726 static int proc_pid_instantiate(struct inode *dir,
2727 struct dentry * dentry,
2728 struct task_struct *task, const void *ptr)
2730 struct inode *inode;
2732 inode = proc_pid_make_inode(dir->i_sb, task);
2736 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2737 inode->i_op = &proc_tgid_base_inode_operations;
2738 inode->i_fop = &proc_tgid_base_operations;
2739 inode->i_flags|=S_IMMUTABLE;
2741 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2742 ARRAY_SIZE(tgid_base_stuff)));
2744 d_set_d_op(dentry, &pid_dentry_operations);
2746 d_add(dentry, inode);
2747 /* Close the race of the process dying before we return the dentry */
2748 if (pid_revalidate(dentry, 0))
2754 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2756 int result = -ENOENT;
2757 struct task_struct *task;
2759 struct pid_namespace *ns;
2761 tgid = name_to_int(&dentry->d_name);
2765 ns = dentry->d_sb->s_fs_info;
2767 task = find_task_by_pid_ns(tgid, ns);
2769 get_task_struct(task);
2774 result = proc_pid_instantiate(dir, dentry, task, NULL);
2775 put_task_struct(task);
2777 return ERR_PTR(result);
2781 * Find the first task with tgid >= tgid
2786 struct task_struct *task;
2788 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2793 put_task_struct(iter.task);
2797 pid = find_ge_pid(iter.tgid, ns);
2799 iter.tgid = pid_nr_ns(pid, ns);
2800 iter.task = pid_task(pid, PIDTYPE_PID);
2801 /* What we to know is if the pid we have find is the
2802 * pid of a thread_group_leader. Testing for task
2803 * being a thread_group_leader is the obvious thing
2804 * todo but there is a window when it fails, due to
2805 * the pid transfer logic in de_thread.
2807 * So we perform the straight forward test of seeing
2808 * if the pid we have found is the pid of a thread
2809 * group leader, and don't worry if the task we have
2810 * found doesn't happen to be a thread group leader.
2811 * As we don't care in the case of readdir.
2813 if (!iter.task || !has_group_leader_pid(iter.task)) {
2817 get_task_struct(iter.task);
2823 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
2825 /* for the /proc/ directory itself, after non-process stuff has been done */
2826 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2828 struct tgid_iter iter;
2829 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2830 loff_t pos = ctx->pos;
2832 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2835 if (pos == TGID_OFFSET - 2) {
2836 struct inode *inode = ns->proc_self->d_inode;
2837 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2839 ctx->pos = pos = pos + 1;
2841 if (pos == TGID_OFFSET - 1) {
2842 struct inode *inode = ns->proc_thread_self->d_inode;
2843 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
2845 ctx->pos = pos = pos + 1;
2847 iter.tgid = pos - TGID_OFFSET;
2849 for (iter = next_tgid(ns, iter);
2851 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2852 char name[PROC_NUMBUF];
2854 if (!has_pid_permissions(ns, iter.task, 2))
2857 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2858 ctx->pos = iter.tgid + TGID_OFFSET;
2859 if (!proc_fill_cache(file, ctx, name, len,
2860 proc_pid_instantiate, iter.task, NULL)) {
2861 put_task_struct(iter.task);
2865 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2872 static const struct pid_entry tid_base_stuff[] = {
2873 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2874 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2875 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2877 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2879 REG("environ", S_IRUSR, proc_environ_operations),
2880 ONE("auxv", S_IRUSR, proc_pid_auxv),
2881 ONE("status", S_IRUGO, proc_pid_status),
2882 ONE("personality", S_IRUSR, proc_pid_personality),
2883 ONE("limits", S_IRUGO, proc_pid_limits),
2884 #ifdef CONFIG_SCHED_DEBUG
2885 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2887 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2888 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2889 ONE("syscall", S_IRUSR, proc_pid_syscall),
2891 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2892 ONE("stat", S_IRUGO, proc_tid_stat),
2893 ONE("statm", S_IRUGO, proc_pid_statm),
2894 REG("maps", S_IRUGO, proc_tid_maps_operations),
2895 #ifdef CONFIG_CHECKPOINT_RESTORE
2896 REG("children", S_IRUGO, proc_tid_children_operations),
2899 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2901 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2902 LNK("cwd", proc_cwd_link),
2903 LNK("root", proc_root_link),
2904 LNK("exe", proc_exe_link),
2905 REG("mounts", S_IRUGO, proc_mounts_operations),
2906 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2907 #ifdef CONFIG_PROC_PAGE_MONITOR
2908 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2909 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2910 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2912 #ifdef CONFIG_SECURITY
2913 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2915 #ifdef CONFIG_KALLSYMS
2916 ONE("wchan", S_IRUGO, proc_pid_wchan),
2918 #ifdef CONFIG_STACKTRACE
2919 ONE("stack", S_IRUSR, proc_pid_stack),
2921 #ifdef CONFIG_SCHEDSTATS
2922 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2924 #ifdef CONFIG_LATENCYTOP
2925 REG("latency", S_IRUGO, proc_lstats_operations),
2927 #ifdef CONFIG_PROC_PID_CPUSET
2928 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2930 #ifdef CONFIG_CGROUPS
2931 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2933 ONE("oom_score", S_IRUGO, proc_oom_score),
2934 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2935 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2936 #ifdef CONFIG_AUDITSYSCALL
2937 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2938 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2940 #ifdef CONFIG_FAULT_INJECTION
2941 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2943 #ifdef CONFIG_TASK_IO_ACCOUNTING
2944 ONE("io", S_IRUSR, proc_tid_io_accounting),
2946 #ifdef CONFIG_HARDWALL
2947 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2949 #ifdef CONFIG_USER_NS
2950 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2951 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2952 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2956 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2958 return proc_pident_readdir(file, ctx,
2959 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2962 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2964 return proc_pident_lookup(dir, dentry,
2965 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2968 static const struct file_operations proc_tid_base_operations = {
2969 .read = generic_read_dir,
2970 .iterate = proc_tid_base_readdir,
2971 .llseek = default_llseek,
2974 static const struct inode_operations proc_tid_base_inode_operations = {
2975 .lookup = proc_tid_base_lookup,
2976 .getattr = pid_getattr,
2977 .setattr = proc_setattr,
2980 static int proc_task_instantiate(struct inode *dir,
2981 struct dentry *dentry, struct task_struct *task, const void *ptr)
2983 struct inode *inode;
2984 inode = proc_pid_make_inode(dir->i_sb, task);
2988 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2989 inode->i_op = &proc_tid_base_inode_operations;
2990 inode->i_fop = &proc_tid_base_operations;
2991 inode->i_flags|=S_IMMUTABLE;
2993 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2994 ARRAY_SIZE(tid_base_stuff)));
2996 d_set_d_op(dentry, &pid_dentry_operations);
2998 d_add(dentry, inode);
2999 /* Close the race of the process dying before we return the dentry */
3000 if (pid_revalidate(dentry, 0))
3006 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3008 int result = -ENOENT;
3009 struct task_struct *task;
3010 struct task_struct *leader = get_proc_task(dir);
3012 struct pid_namespace *ns;
3017 tid = name_to_int(&dentry->d_name);
3021 ns = dentry->d_sb->s_fs_info;
3023 task = find_task_by_pid_ns(tid, ns);
3025 get_task_struct(task);
3029 if (!same_thread_group(leader, task))
3032 result = proc_task_instantiate(dir, dentry, task, NULL);
3034 put_task_struct(task);
3036 put_task_struct(leader);
3038 return ERR_PTR(result);
3042 * Find the first tid of a thread group to return to user space.
3044 * Usually this is just the thread group leader, but if the users
3045 * buffer was too small or there was a seek into the middle of the
3046 * directory we have more work todo.
3048 * In the case of a short read we start with find_task_by_pid.
3050 * In the case of a seek we start with the leader and walk nr
3053 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3054 struct pid_namespace *ns)
3056 struct task_struct *pos, *task;
3057 unsigned long nr = f_pos;
3059 if (nr != f_pos) /* 32bit overflow? */
3063 task = pid_task(pid, PIDTYPE_PID);
3067 /* Attempt to start with the tid of a thread */
3069 pos = find_task_by_pid_ns(tid, ns);
3070 if (pos && same_thread_group(pos, task))
3074 /* If nr exceeds the number of threads there is nothing todo */
3075 if (nr >= get_nr_threads(task))
3078 /* If we haven't found our starting place yet start
3079 * with the leader and walk nr threads forward.
3081 pos = task = task->group_leader;
3085 } while_each_thread(task, pos);
3090 get_task_struct(pos);
3097 * Find the next thread in the thread list.
3098 * Return NULL if there is an error or no next thread.
3100 * The reference to the input task_struct is released.
3102 static struct task_struct *next_tid(struct task_struct *start)
3104 struct task_struct *pos = NULL;
3106 if (pid_alive(start)) {
3107 pos = next_thread(start);
3108 if (thread_group_leader(pos))
3111 get_task_struct(pos);
3114 put_task_struct(start);
3118 /* for the /proc/TGID/task/ directories */
3119 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3121 struct inode *inode = file_inode(file);
3122 struct task_struct *task;
3123 struct pid_namespace *ns;
3126 if (proc_inode_is_dead(inode))
3129 if (!dir_emit_dots(file, ctx))
3132 /* f_version caches the tgid value that the last readdir call couldn't
3133 * return. lseek aka telldir automagically resets f_version to 0.
3135 ns = file->f_dentry->d_sb->s_fs_info;
3136 tid = (int)file->f_version;
3137 file->f_version = 0;
3138 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3140 task = next_tid(task), ctx->pos++) {
3141 char name[PROC_NUMBUF];
3143 tid = task_pid_nr_ns(task, ns);
3144 len = snprintf(name, sizeof(name), "%d", tid);
3145 if (!proc_fill_cache(file, ctx, name, len,
3146 proc_task_instantiate, task, NULL)) {
3147 /* returning this tgid failed, save it as the first
3148 * pid for the next readir call */
3149 file->f_version = (u64)tid;
3150 put_task_struct(task);
3158 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3160 struct inode *inode = dentry->d_inode;
3161 struct task_struct *p = get_proc_task(inode);
3162 generic_fillattr(inode, stat);
3165 stat->nlink += get_nr_threads(p);
3172 static const struct inode_operations proc_task_inode_operations = {
3173 .lookup = proc_task_lookup,
3174 .getattr = proc_task_getattr,
3175 .setattr = proc_setattr,
3176 .permission = proc_pid_permission,
3179 static const struct file_operations proc_task_operations = {
3180 .read = generic_read_dir,
3181 .iterate = proc_task_readdir,
3182 .llseek = default_llseek,