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 INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
142 /* ANDROID is for special files in /proc. */
143 #define ANDROID(NAME, MODE, OTYPE) \
144 NOD(NAME, (S_IFREG|(MODE)), \
145 &proc_##OTYPE##_inode_operations, \
146 &proc_##OTYPE##_operations, {})
149 * Count the number of hardlinks for the pid_entry table, excluding the .
152 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
159 for (i = 0; i < n; ++i) {
160 if (S_ISDIR(entries[i].mode))
167 static int get_task_root(struct task_struct *task, struct path *root)
169 int result = -ENOENT;
173 get_fs_root(task->fs, root);
180 static int proc_cwd_link(struct dentry *dentry, struct path *path)
182 struct task_struct *task = get_proc_task(dentry->d_inode);
183 int result = -ENOENT;
188 get_fs_pwd(task->fs, path);
192 put_task_struct(task);
197 static int proc_root_link(struct dentry *dentry, struct path *path)
199 struct task_struct *task = get_proc_task(dentry->d_inode);
200 int result = -ENOENT;
203 result = get_task_root(task, path);
204 put_task_struct(task);
209 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
213 struct mm_struct *mm = get_task_mm(task);
217 goto out_mm; /* Shh! No looking before we're done */
219 len = mm->arg_end - mm->arg_start;
224 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
226 // If the nul at the end of args has been overwritten, then
227 // assume application is using setproctitle(3).
228 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
229 len = strnlen(buffer, res);
233 len = mm->env_end - mm->env_start;
234 if (len > PAGE_SIZE - res)
235 len = PAGE_SIZE - res;
236 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
237 res = strnlen(buffer, res);
246 static int proc_pid_auxv(struct task_struct *task, char *buffer)
248 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
249 int res = PTR_ERR(mm);
250 if (mm && !IS_ERR(mm)) {
251 unsigned int nwords = 0;
254 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
255 res = nwords * sizeof(mm->saved_auxv[0]);
258 memcpy(buffer, mm->saved_auxv, res);
265 #ifdef CONFIG_KALLSYMS
267 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
268 * Returns the resolved symbol. If that fails, simply return the address.
270 static int proc_pid_wchan(struct task_struct *task, char *buffer)
273 char symname[KSYM_NAME_LEN];
275 wchan = get_wchan(task);
277 if (lookup_symbol_name(wchan, symname) < 0)
278 if (!ptrace_may_access(task, PTRACE_MODE_READ))
281 return sprintf(buffer, "%lu", wchan);
283 return sprintf(buffer, "%s", symname);
285 #endif /* CONFIG_KALLSYMS */
287 static int lock_trace(struct task_struct *task)
289 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
292 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
293 mutex_unlock(&task->signal->cred_guard_mutex);
299 static void unlock_trace(struct task_struct *task)
301 mutex_unlock(&task->signal->cred_guard_mutex);
304 #ifdef CONFIG_STACKTRACE
306 #define MAX_STACK_TRACE_DEPTH 64
308 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
309 struct pid *pid, struct task_struct *task)
311 struct stack_trace trace;
312 unsigned long *entries;
316 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
320 trace.nr_entries = 0;
321 trace.max_entries = MAX_STACK_TRACE_DEPTH;
322 trace.entries = entries;
325 err = lock_trace(task);
327 save_stack_trace_tsk(task, &trace);
329 for (i = 0; i < trace.nr_entries; i++) {
330 seq_printf(m, "[<%pK>] %pS\n",
331 (void *)entries[i], (void *)entries[i]);
341 #ifdef CONFIG_SCHEDSTATS
343 * Provides /proc/PID/schedstat
345 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
347 return sprintf(buffer, "%llu %llu %lu\n",
348 (unsigned long long)task->se.sum_exec_runtime,
349 (unsigned long long)task->sched_info.run_delay,
350 task->sched_info.pcount);
354 #ifdef CONFIG_LATENCYTOP
355 static int lstats_show_proc(struct seq_file *m, void *v)
358 struct inode *inode = m->private;
359 struct task_struct *task = get_proc_task(inode);
363 seq_puts(m, "Latency Top version : v0.1\n");
364 for (i = 0; i < 32; i++) {
365 struct latency_record *lr = &task->latency_record[i];
366 if (lr->backtrace[0]) {
368 seq_printf(m, "%i %li %li",
369 lr->count, lr->time, lr->max);
370 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
371 unsigned long bt = lr->backtrace[q];
376 seq_printf(m, " %ps", (void *)bt);
382 put_task_struct(task);
386 static int lstats_open(struct inode *inode, struct file *file)
388 return single_open(file, lstats_show_proc, inode);
391 static ssize_t lstats_write(struct file *file, const char __user *buf,
392 size_t count, loff_t *offs)
394 struct task_struct *task = get_proc_task(file_inode(file));
398 clear_all_latency_tracing(task);
399 put_task_struct(task);
404 static const struct file_operations proc_lstats_operations = {
407 .write = lstats_write,
409 .release = single_release,
414 #ifdef CONFIG_CGROUPS
415 static int cgroup_open(struct inode *inode, struct file *file)
417 struct pid *pid = PROC_I(inode)->pid;
418 return single_open(file, proc_cgroup_show, pid);
421 static const struct file_operations proc_cgroup_operations = {
425 .release = single_release,
429 #ifdef CONFIG_PROC_PID_CPUSET
431 static int cpuset_open(struct inode *inode, struct file *file)
433 struct pid *pid = PROC_I(inode)->pid;
434 return single_open(file, proc_cpuset_show, pid);
437 static const struct file_operations proc_cpuset_operations = {
441 .release = single_release,
445 static int proc_oom_score(struct task_struct *task, char *buffer)
447 unsigned long totalpages = totalram_pages + total_swap_pages;
448 unsigned long points = 0;
450 read_lock(&tasklist_lock);
452 points = oom_badness(task, NULL, NULL, totalpages) *
454 read_unlock(&tasklist_lock);
455 return sprintf(buffer, "%lu\n", points);
463 static const struct limit_names lnames[RLIM_NLIMITS] = {
464 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
465 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
466 [RLIMIT_DATA] = {"Max data size", "bytes"},
467 [RLIMIT_STACK] = {"Max stack size", "bytes"},
468 [RLIMIT_CORE] = {"Max core file size", "bytes"},
469 [RLIMIT_RSS] = {"Max resident set", "bytes"},
470 [RLIMIT_NPROC] = {"Max processes", "processes"},
471 [RLIMIT_NOFILE] = {"Max open files", "files"},
472 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
473 [RLIMIT_AS] = {"Max address space", "bytes"},
474 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
475 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
476 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
477 [RLIMIT_NICE] = {"Max nice priority", NULL},
478 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
479 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
482 /* Display limits for a process */
483 static int proc_pid_limits(struct task_struct *task, char *buffer)
488 char *bufptr = buffer;
490 struct rlimit rlim[RLIM_NLIMITS];
492 if (!lock_task_sighand(task, &flags))
494 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
495 unlock_task_sighand(task, &flags);
498 * print the file header
500 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
501 "Limit", "Soft Limit", "Hard Limit", "Units");
503 for (i = 0; i < RLIM_NLIMITS; i++) {
504 if (rlim[i].rlim_cur == RLIM_INFINITY)
505 count += sprintf(&bufptr[count], "%-25s %-20s ",
506 lnames[i].name, "unlimited");
508 count += sprintf(&bufptr[count], "%-25s %-20lu ",
509 lnames[i].name, rlim[i].rlim_cur);
511 if (rlim[i].rlim_max == RLIM_INFINITY)
512 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
514 count += sprintf(&bufptr[count], "%-20lu ",
518 count += sprintf(&bufptr[count], "%-10s\n",
521 count += sprintf(&bufptr[count], "\n");
527 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
528 static int proc_pid_syscall(struct task_struct *task, char *buffer)
531 unsigned long args[6], sp, pc;
532 int res = lock_trace(task);
536 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
537 res = sprintf(buffer, "running\n");
539 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
541 res = sprintf(buffer,
542 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
544 args[0], args[1], args[2], args[3], args[4], args[5],
549 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
551 /************************************************************************/
552 /* Here the fs part begins */
553 /************************************************************************/
555 /* permission checks */
556 static int proc_fd_access_allowed(struct inode *inode)
558 struct task_struct *task;
560 /* Allow access to a task's file descriptors if it is us or we
561 * may use ptrace attach to the process and find out that
564 task = get_proc_task(inode);
566 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
567 put_task_struct(task);
572 int proc_setattr(struct dentry *dentry, struct iattr *attr)
575 struct inode *inode = dentry->d_inode;
577 if (attr->ia_valid & ATTR_MODE)
580 error = inode_change_ok(inode, attr);
584 setattr_copy(inode, attr);
585 mark_inode_dirty(inode);
590 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
591 * or euid/egid (for hide_pid_min=2)?
593 static bool has_pid_permissions(struct pid_namespace *pid,
594 struct task_struct *task,
597 if (pid->hide_pid < hide_pid_min)
599 if (in_group_p(pid->pid_gid))
601 return ptrace_may_access(task, PTRACE_MODE_READ);
605 static int proc_pid_permission(struct inode *inode, int mask)
607 struct pid_namespace *pid = inode->i_sb->s_fs_info;
608 struct task_struct *task;
611 task = get_proc_task(inode);
614 has_perms = has_pid_permissions(pid, task, 1);
615 put_task_struct(task);
618 if (pid->hide_pid == 2) {
620 * Let's make getdents(), stat(), and open()
621 * consistent with each other. If a process
622 * may not stat() a file, it shouldn't be seen
630 return generic_permission(inode, mask);
635 static const struct inode_operations proc_def_inode_operations = {
636 .setattr = proc_setattr,
639 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
641 static ssize_t proc_info_read(struct file * file, char __user * buf,
642 size_t count, loff_t *ppos)
644 struct inode * inode = file_inode(file);
647 struct task_struct *task = get_proc_task(inode);
653 if (count > PROC_BLOCK_SIZE)
654 count = PROC_BLOCK_SIZE;
657 if (!(page = __get_free_page(GFP_TEMPORARY)))
660 length = PROC_I(inode)->op.proc_read(task, (char*)page);
663 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
666 put_task_struct(task);
671 static const struct file_operations proc_info_file_operations = {
672 .read = proc_info_read,
673 .llseek = generic_file_llseek,
676 static int proc_single_show(struct seq_file *m, void *v)
678 struct inode *inode = m->private;
679 struct pid_namespace *ns;
681 struct task_struct *task;
684 ns = inode->i_sb->s_fs_info;
685 pid = proc_pid(inode);
686 task = get_pid_task(pid, PIDTYPE_PID);
690 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
692 put_task_struct(task);
696 static int proc_single_open(struct inode *inode, struct file *filp)
698 return single_open(filp, proc_single_show, inode);
701 static const struct file_operations proc_single_file_operations = {
702 .open = proc_single_open,
705 .release = single_release,
708 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
710 struct task_struct *task = get_proc_task(file_inode(file));
711 struct mm_struct *mm;
716 mm = mm_access(task, mode);
717 put_task_struct(task);
723 /* ensure this mm_struct can't be freed */
724 atomic_inc(&mm->mm_count);
725 /* but do not pin its memory */
729 file->private_data = mm;
734 static int mem_open(struct inode *inode, struct file *file)
736 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
738 /* OK to pass negative loff_t, we can catch out-of-range */
739 file->f_mode |= FMODE_UNSIGNED_OFFSET;
744 static ssize_t mem_rw(struct file *file, char __user *buf,
745 size_t count, loff_t *ppos, int write)
747 struct mm_struct *mm = file->private_data;
748 unsigned long addr = *ppos;
755 page = (char *)__get_free_page(GFP_TEMPORARY);
760 if (!atomic_inc_not_zero(&mm->mm_users))
764 int this_len = min_t(int, count, PAGE_SIZE);
766 if (write && copy_from_user(page, buf, this_len)) {
771 this_len = access_remote_vm(mm, addr, page, this_len, write);
778 if (!write && copy_to_user(buf, page, this_len)) {
792 free_page((unsigned long) page);
796 static ssize_t mem_read(struct file *file, char __user *buf,
797 size_t count, loff_t *ppos)
799 return mem_rw(file, buf, count, ppos, 0);
802 static ssize_t mem_write(struct file *file, const char __user *buf,
803 size_t count, loff_t *ppos)
805 return mem_rw(file, (char __user*)buf, count, ppos, 1);
808 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
812 file->f_pos = offset;
815 file->f_pos += offset;
820 force_successful_syscall_return();
824 static int mem_release(struct inode *inode, struct file *file)
826 struct mm_struct *mm = file->private_data;
832 static const struct file_operations proc_mem_operations = {
837 .release = mem_release,
840 static int environ_open(struct inode *inode, struct file *file)
842 return __mem_open(inode, file, PTRACE_MODE_READ);
845 static ssize_t environ_read(struct file *file, char __user *buf,
846 size_t count, loff_t *ppos)
849 unsigned long src = *ppos;
851 struct mm_struct *mm = file->private_data;
856 page = (char *)__get_free_page(GFP_TEMPORARY);
861 if (!atomic_inc_not_zero(&mm->mm_users))
864 size_t this_len, max_len;
867 if (src >= (mm->env_end - mm->env_start))
870 this_len = mm->env_end - (mm->env_start + src);
872 max_len = min_t(size_t, PAGE_SIZE, count);
873 this_len = min(max_len, this_len);
875 retval = access_remote_vm(mm, (mm->env_start + src),
883 if (copy_to_user(buf, page, retval)) {
897 free_page((unsigned long) page);
901 static const struct file_operations proc_environ_operations = {
902 .open = environ_open,
903 .read = environ_read,
904 .llseek = generic_file_llseek,
905 .release = mem_release,
908 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
911 struct task_struct *task = get_proc_task(file_inode(file));
912 char buffer[PROC_NUMBUF];
913 int oom_adj = OOM_ADJUST_MIN;
919 if (lock_task_sighand(task, &flags)) {
920 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
921 oom_adj = OOM_ADJUST_MAX;
923 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
925 unlock_task_sighand(task, &flags);
927 put_task_struct(task);
928 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
929 return simple_read_from_buffer(buf, count, ppos, buffer, len);
932 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
933 size_t count, loff_t *ppos)
935 struct task_struct *task;
936 char buffer[PROC_NUMBUF];
941 memset(buffer, 0, sizeof(buffer));
942 if (count > sizeof(buffer) - 1)
943 count = sizeof(buffer) - 1;
944 if (copy_from_user(buffer, buf, count)) {
949 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
952 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
953 oom_adj != OOM_DISABLE) {
958 task = get_proc_task(file_inode(file));
970 if (!lock_task_sighand(task, &flags)) {
976 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
977 * value is always attainable.
979 if (oom_adj == OOM_ADJUST_MAX)
980 oom_adj = OOM_SCORE_ADJ_MAX;
982 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
984 if (oom_adj < task->signal->oom_score_adj &&
985 !capable(CAP_SYS_RESOURCE)) {
991 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
992 * /proc/pid/oom_score_adj instead.
994 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
995 current->comm, task_pid_nr(current), task_pid_nr(task),
998 task->signal->oom_score_adj = oom_adj;
999 trace_oom_score_adj_update(task);
1001 unlock_task_sighand(task, &flags);
1004 put_task_struct(task);
1006 return err < 0 ? err : count;
1009 static int oom_adjust_permission(struct inode *inode, int mask)
1012 struct task_struct *p;
1014 p = get_proc_task(inode);
1021 * System Server (uid == 1000) is granted access to oom_adj of all
1022 * android applications (uid > 10000) as and services (uid >= 1000)
1024 if (p && (current_fsuid() == 1000) && (uid >= 1000)) {
1025 if (inode->i_mode >> 6 & mask) {
1030 /* Fall back to default. */
1031 return generic_permission(inode, mask);
1034 static const struct inode_operations proc_oom_adj_inode_operations = {
1035 .permission = oom_adjust_permission,
1038 static const struct file_operations proc_oom_adj_operations = {
1039 .read = oom_adj_read,
1040 .write = oom_adj_write,
1041 .llseek = generic_file_llseek,
1044 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1045 size_t count, loff_t *ppos)
1047 struct task_struct *task = get_proc_task(file_inode(file));
1048 char buffer[PROC_NUMBUF];
1049 short oom_score_adj = OOM_SCORE_ADJ_MIN;
1050 unsigned long flags;
1055 if (lock_task_sighand(task, &flags)) {
1056 oom_score_adj = task->signal->oom_score_adj;
1057 unlock_task_sighand(task, &flags);
1059 put_task_struct(task);
1060 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1061 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1064 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1065 size_t count, loff_t *ppos)
1067 struct task_struct *task;
1068 char buffer[PROC_NUMBUF];
1069 unsigned long flags;
1073 memset(buffer, 0, sizeof(buffer));
1074 if (count > sizeof(buffer) - 1)
1075 count = sizeof(buffer) - 1;
1076 if (copy_from_user(buffer, buf, count)) {
1081 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1084 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1085 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1090 task = get_proc_task(file_inode(file));
1102 if (!lock_task_sighand(task, &flags)) {
1107 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1108 !capable(CAP_SYS_RESOURCE)) {
1113 task->signal->oom_score_adj = (short)oom_score_adj;
1114 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1115 task->signal->oom_score_adj_min = (short)oom_score_adj;
1116 trace_oom_score_adj_update(task);
1119 unlock_task_sighand(task, &flags);
1122 put_task_struct(task);
1124 return err < 0 ? err : count;
1127 static const struct file_operations proc_oom_score_adj_operations = {
1128 .read = oom_score_adj_read,
1129 .write = oom_score_adj_write,
1130 .llseek = default_llseek,
1133 #ifdef CONFIG_AUDITSYSCALL
1134 #define TMPBUFLEN 21
1135 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1136 size_t count, loff_t *ppos)
1138 struct inode * inode = file_inode(file);
1139 struct task_struct *task = get_proc_task(inode);
1141 char tmpbuf[TMPBUFLEN];
1145 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1146 from_kuid(file->f_cred->user_ns,
1147 audit_get_loginuid(task)));
1148 put_task_struct(task);
1149 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1152 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1153 size_t count, loff_t *ppos)
1155 struct inode * inode = file_inode(file);
1162 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1168 if (count >= PAGE_SIZE)
1169 count = PAGE_SIZE - 1;
1172 /* No partial writes. */
1175 page = (char*)__get_free_page(GFP_TEMPORARY);
1179 if (copy_from_user(page, buf, count))
1183 loginuid = simple_strtoul(page, &tmp, 10);
1189 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1190 if (!uid_valid(kloginuid)) {
1195 length = audit_set_loginuid(kloginuid);
1196 if (likely(length == 0))
1200 free_page((unsigned long) page);
1204 static const struct file_operations proc_loginuid_operations = {
1205 .read = proc_loginuid_read,
1206 .write = proc_loginuid_write,
1207 .llseek = generic_file_llseek,
1210 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1211 size_t count, loff_t *ppos)
1213 struct inode * inode = file_inode(file);
1214 struct task_struct *task = get_proc_task(inode);
1216 char tmpbuf[TMPBUFLEN];
1220 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1221 audit_get_sessionid(task));
1222 put_task_struct(task);
1223 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1226 static const struct file_operations proc_sessionid_operations = {
1227 .read = proc_sessionid_read,
1228 .llseek = generic_file_llseek,
1232 #ifdef CONFIG_FAULT_INJECTION
1233 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1234 size_t count, loff_t *ppos)
1236 struct task_struct *task = get_proc_task(file_inode(file));
1237 char buffer[PROC_NUMBUF];
1243 make_it_fail = task->make_it_fail;
1244 put_task_struct(task);
1246 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1248 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1251 static ssize_t proc_fault_inject_write(struct file * file,
1252 const char __user * buf, size_t count, loff_t *ppos)
1254 struct task_struct *task;
1255 char buffer[PROC_NUMBUF], *end;
1258 if (!capable(CAP_SYS_RESOURCE))
1260 memset(buffer, 0, sizeof(buffer));
1261 if (count > sizeof(buffer) - 1)
1262 count = sizeof(buffer) - 1;
1263 if (copy_from_user(buffer, buf, count))
1265 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1268 task = get_proc_task(file_inode(file));
1271 task->make_it_fail = make_it_fail;
1272 put_task_struct(task);
1277 static const struct file_operations proc_fault_inject_operations = {
1278 .read = proc_fault_inject_read,
1279 .write = proc_fault_inject_write,
1280 .llseek = generic_file_llseek,
1285 #ifdef CONFIG_SCHED_DEBUG
1287 * Print out various scheduling related per-task fields:
1289 static int sched_show(struct seq_file *m, void *v)
1291 struct inode *inode = m->private;
1292 struct task_struct *p;
1294 p = get_proc_task(inode);
1297 proc_sched_show_task(p, m);
1305 sched_write(struct file *file, const char __user *buf,
1306 size_t count, loff_t *offset)
1308 struct inode *inode = file_inode(file);
1309 struct task_struct *p;
1311 p = get_proc_task(inode);
1314 proc_sched_set_task(p);
1321 static int sched_open(struct inode *inode, struct file *filp)
1323 return single_open(filp, sched_show, inode);
1326 static const struct file_operations proc_pid_sched_operations = {
1329 .write = sched_write,
1330 .llseek = seq_lseek,
1331 .release = single_release,
1336 #ifdef CONFIG_SCHED_AUTOGROUP
1338 * Print out autogroup related information:
1340 static int sched_autogroup_show(struct seq_file *m, void *v)
1342 struct inode *inode = m->private;
1343 struct task_struct *p;
1345 p = get_proc_task(inode);
1348 proc_sched_autogroup_show_task(p, m);
1356 sched_autogroup_write(struct file *file, const char __user *buf,
1357 size_t count, loff_t *offset)
1359 struct inode *inode = file_inode(file);
1360 struct task_struct *p;
1361 char buffer[PROC_NUMBUF];
1365 memset(buffer, 0, sizeof(buffer));
1366 if (count > sizeof(buffer) - 1)
1367 count = sizeof(buffer) - 1;
1368 if (copy_from_user(buffer, buf, count))
1371 err = kstrtoint(strstrip(buffer), 0, &nice);
1375 p = get_proc_task(inode);
1379 err = proc_sched_autogroup_set_nice(p, nice);
1388 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1392 ret = single_open(filp, sched_autogroup_show, NULL);
1394 struct seq_file *m = filp->private_data;
1401 static const struct file_operations proc_pid_sched_autogroup_operations = {
1402 .open = sched_autogroup_open,
1404 .write = sched_autogroup_write,
1405 .llseek = seq_lseek,
1406 .release = single_release,
1409 #endif /* CONFIG_SCHED_AUTOGROUP */
1411 static ssize_t comm_write(struct file *file, const char __user *buf,
1412 size_t count, loff_t *offset)
1414 struct inode *inode = file_inode(file);
1415 struct task_struct *p;
1416 char buffer[TASK_COMM_LEN];
1417 const size_t maxlen = sizeof(buffer) - 1;
1419 memset(buffer, 0, sizeof(buffer));
1420 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1423 p = get_proc_task(inode);
1427 if (same_thread_group(current, p))
1428 set_task_comm(p, buffer);
1437 static int comm_show(struct seq_file *m, void *v)
1439 struct inode *inode = m->private;
1440 struct task_struct *p;
1442 p = get_proc_task(inode);
1447 seq_printf(m, "%s\n", p->comm);
1455 static int comm_open(struct inode *inode, struct file *filp)
1457 return single_open(filp, comm_show, inode);
1460 static const struct file_operations proc_pid_set_comm_operations = {
1463 .write = comm_write,
1464 .llseek = seq_lseek,
1465 .release = single_release,
1468 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1470 struct task_struct *task;
1471 struct mm_struct *mm;
1472 struct file *exe_file;
1474 task = get_proc_task(dentry->d_inode);
1477 mm = get_task_mm(task);
1478 put_task_struct(task);
1481 exe_file = get_mm_exe_file(mm);
1484 *exe_path = exe_file->f_path;
1485 path_get(&exe_file->f_path);
1492 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1494 struct inode *inode = dentry->d_inode;
1496 int error = -EACCES;
1498 /* Are we allowed to snoop on the tasks file descriptors? */
1499 if (!proc_fd_access_allowed(inode))
1502 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1506 nd_jump_link(nd, &path);
1509 return ERR_PTR(error);
1512 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1514 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1521 pathname = d_path(path, tmp, PAGE_SIZE);
1522 len = PTR_ERR(pathname);
1523 if (IS_ERR(pathname))
1525 len = tmp + PAGE_SIZE - 1 - pathname;
1529 if (copy_to_user(buffer, pathname, len))
1532 free_page((unsigned long)tmp);
1536 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1538 int error = -EACCES;
1539 struct inode *inode = dentry->d_inode;
1542 /* Are we allowed to snoop on the tasks file descriptors? */
1543 if (!proc_fd_access_allowed(inode))
1546 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1550 error = do_proc_readlink(&path, buffer, buflen);
1556 const struct inode_operations proc_pid_link_inode_operations = {
1557 .readlink = proc_pid_readlink,
1558 .follow_link = proc_pid_follow_link,
1559 .setattr = proc_setattr,
1563 /* building an inode */
1565 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1567 struct inode * inode;
1568 struct proc_inode *ei;
1569 const struct cred *cred;
1571 /* We need a new inode */
1573 inode = new_inode(sb);
1579 inode->i_ino = get_next_ino();
1580 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1581 inode->i_op = &proc_def_inode_operations;
1584 * grab the reference to task.
1586 ei->pid = get_task_pid(task, PIDTYPE_PID);
1590 if (task_dumpable(task)) {
1592 cred = __task_cred(task);
1593 inode->i_uid = cred->euid;
1594 inode->i_gid = cred->egid;
1597 security_task_to_inode(task, inode);
1607 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1609 struct inode *inode = dentry->d_inode;
1610 struct task_struct *task;
1611 const struct cred *cred;
1612 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1614 generic_fillattr(inode, stat);
1617 stat->uid = GLOBAL_ROOT_UID;
1618 stat->gid = GLOBAL_ROOT_GID;
1619 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1621 if (!has_pid_permissions(pid, task, 2)) {
1624 * This doesn't prevent learning whether PID exists,
1625 * it only makes getattr() consistent with readdir().
1629 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1630 task_dumpable(task)) {
1631 cred = __task_cred(task);
1632 stat->uid = cred->euid;
1633 stat->gid = cred->egid;
1643 * Exceptional case: normally we are not allowed to unhash a busy
1644 * directory. In this case, however, we can do it - no aliasing problems
1645 * due to the way we treat inodes.
1647 * Rewrite the inode's ownerships here because the owning task may have
1648 * performed a setuid(), etc.
1650 * Before the /proc/pid/status file was created the only way to read
1651 * the effective uid of a /process was to stat /proc/pid. Reading
1652 * /proc/pid/status is slow enough that procps and other packages
1653 * kept stating /proc/pid. To keep the rules in /proc simple I have
1654 * made this apply to all per process world readable and executable
1657 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1659 struct inode *inode;
1660 struct task_struct *task;
1661 const struct cred *cred;
1663 if (flags & LOOKUP_RCU)
1666 inode = dentry->d_inode;
1667 task = get_proc_task(inode);
1670 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1671 task_dumpable(task)) {
1673 cred = __task_cred(task);
1674 inode->i_uid = cred->euid;
1675 inode->i_gid = cred->egid;
1678 inode->i_uid = GLOBAL_ROOT_UID;
1679 inode->i_gid = GLOBAL_ROOT_GID;
1681 inode->i_mode &= ~(S_ISUID | S_ISGID);
1682 security_task_to_inode(task, inode);
1683 put_task_struct(task);
1690 int pid_delete_dentry(const struct dentry *dentry)
1692 /* Is the task we represent dead?
1693 * If so, then don't put the dentry on the lru list,
1694 * kill it immediately.
1696 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1699 const struct dentry_operations pid_dentry_operations =
1701 .d_revalidate = pid_revalidate,
1702 .d_delete = pid_delete_dentry,
1708 * Fill a directory entry.
1710 * If possible create the dcache entry and derive our inode number and
1711 * file type from dcache entry.
1713 * Since all of the proc inode numbers are dynamically generated, the inode
1714 * numbers do not exist until the inode is cache. This means creating the
1715 * the dcache entry in readdir is necessary to keep the inode numbers
1716 * reported by readdir in sync with the inode numbers reported
1719 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1720 const char *name, int len,
1721 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1723 struct dentry *child, *dir = filp->f_path.dentry;
1724 struct inode *inode;
1727 unsigned type = DT_UNKNOWN;
1731 qname.hash = full_name_hash(name, len);
1733 child = d_lookup(dir, &qname);
1736 new = d_alloc(dir, &qname);
1738 child = instantiate(dir->d_inode, new, task, ptr);
1745 if (!child || IS_ERR(child) || !child->d_inode)
1746 goto end_instantiate;
1747 inode = child->d_inode;
1750 type = inode->i_mode >> 12;
1755 ino = find_inode_number(dir, &qname);
1758 return filldir(dirent, name, len, filp->f_pos, ino, type);
1761 #ifdef CONFIG_CHECKPOINT_RESTORE
1764 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1765 * which represent vma start and end addresses.
1767 static int dname_to_vma_addr(struct dentry *dentry,
1768 unsigned long *start, unsigned long *end)
1770 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1776 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1778 unsigned long vm_start, vm_end;
1779 bool exact_vma_exists = false;
1780 struct mm_struct *mm = NULL;
1781 struct task_struct *task;
1782 const struct cred *cred;
1783 struct inode *inode;
1786 if (flags & LOOKUP_RCU)
1789 if (!capable(CAP_SYS_ADMIN)) {
1794 inode = dentry->d_inode;
1795 task = get_proc_task(inode);
1799 mm = mm_access(task, PTRACE_MODE_READ);
1800 if (IS_ERR_OR_NULL(mm))
1803 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1804 down_read(&mm->mmap_sem);
1805 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1806 up_read(&mm->mmap_sem);
1811 if (exact_vma_exists) {
1812 if (task_dumpable(task)) {
1814 cred = __task_cred(task);
1815 inode->i_uid = cred->euid;
1816 inode->i_gid = cred->egid;
1819 inode->i_uid = GLOBAL_ROOT_UID;
1820 inode->i_gid = GLOBAL_ROOT_GID;
1822 security_task_to_inode(task, inode);
1827 put_task_struct(task);
1836 static const struct dentry_operations tid_map_files_dentry_operations = {
1837 .d_revalidate = map_files_d_revalidate,
1838 .d_delete = pid_delete_dentry,
1841 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1843 unsigned long vm_start, vm_end;
1844 struct vm_area_struct *vma;
1845 struct task_struct *task;
1846 struct mm_struct *mm;
1850 task = get_proc_task(dentry->d_inode);
1854 mm = get_task_mm(task);
1855 put_task_struct(task);
1859 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1864 down_read(&mm->mmap_sem);
1865 vma = find_exact_vma(mm, vm_start, vm_end);
1866 if (vma && vma->vm_file) {
1867 *path = vma->vm_file->f_path;
1871 up_read(&mm->mmap_sem);
1879 struct map_files_info {
1882 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1885 static struct dentry *
1886 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1887 struct task_struct *task, const void *ptr)
1889 fmode_t mode = (fmode_t)(unsigned long)ptr;
1890 struct proc_inode *ei;
1891 struct inode *inode;
1893 inode = proc_pid_make_inode(dir->i_sb, task);
1895 return ERR_PTR(-ENOENT);
1898 ei->op.proc_get_link = proc_map_files_get_link;
1900 inode->i_op = &proc_pid_link_inode_operations;
1902 inode->i_mode = S_IFLNK;
1904 if (mode & FMODE_READ)
1905 inode->i_mode |= S_IRUSR;
1906 if (mode & FMODE_WRITE)
1907 inode->i_mode |= S_IWUSR;
1909 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1910 d_add(dentry, inode);
1915 static struct dentry *proc_map_files_lookup(struct inode *dir,
1916 struct dentry *dentry, unsigned int flags)
1918 unsigned long vm_start, vm_end;
1919 struct vm_area_struct *vma;
1920 struct task_struct *task;
1921 struct dentry *result;
1922 struct mm_struct *mm;
1924 result = ERR_PTR(-EPERM);
1925 if (!capable(CAP_SYS_ADMIN))
1928 result = ERR_PTR(-ENOENT);
1929 task = get_proc_task(dir);
1933 result = ERR_PTR(-EACCES);
1934 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1937 result = ERR_PTR(-ENOENT);
1938 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1941 mm = get_task_mm(task);
1945 down_read(&mm->mmap_sem);
1946 vma = find_exact_vma(mm, vm_start, vm_end);
1951 result = proc_map_files_instantiate(dir, dentry, task,
1952 (void *)(unsigned long)vma->vm_file->f_mode);
1955 up_read(&mm->mmap_sem);
1958 put_task_struct(task);
1963 static const struct inode_operations proc_map_files_inode_operations = {
1964 .lookup = proc_map_files_lookup,
1965 .permission = proc_fd_permission,
1966 .setattr = proc_setattr,
1970 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1972 struct dentry *dentry = filp->f_path.dentry;
1973 struct inode *inode = dentry->d_inode;
1974 struct vm_area_struct *vma;
1975 struct task_struct *task;
1976 struct mm_struct *mm;
1981 if (!capable(CAP_SYS_ADMIN))
1985 task = get_proc_task(inode);
1990 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1994 switch (filp->f_pos) {
1997 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2001 ino = parent_ino(dentry);
2002 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2007 unsigned long nr_files, pos, i;
2008 struct flex_array *fa = NULL;
2009 struct map_files_info info;
2010 struct map_files_info *p;
2012 mm = get_task_mm(task);
2015 down_read(&mm->mmap_sem);
2020 * We need two passes here:
2022 * 1) Collect vmas of mapped files with mmap_sem taken
2023 * 2) Release mmap_sem and instantiate entries
2025 * otherwise we get lockdep complained, since filldir()
2026 * routine might require mmap_sem taken in might_fault().
2029 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2030 if (vma->vm_file && ++pos > filp->f_pos)
2035 fa = flex_array_alloc(sizeof(info), nr_files,
2037 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2041 flex_array_free(fa);
2042 up_read(&mm->mmap_sem);
2046 for (i = 0, vma = mm->mmap, pos = 2; vma;
2047 vma = vma->vm_next) {
2050 if (++pos <= filp->f_pos)
2053 info.mode = vma->vm_file->f_mode;
2054 info.len = snprintf(info.name,
2055 sizeof(info.name), "%lx-%lx",
2056 vma->vm_start, vma->vm_end);
2057 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2061 up_read(&mm->mmap_sem);
2063 for (i = 0; i < nr_files; i++) {
2064 p = flex_array_get(fa, i);
2065 ret = proc_fill_cache(filp, dirent, filldir,
2067 proc_map_files_instantiate,
2069 (void *)(unsigned long)p->mode);
2075 flex_array_free(fa);
2081 put_task_struct(task);
2086 static const struct file_operations proc_map_files_operations = {
2087 .read = generic_read_dir,
2088 .readdir = proc_map_files_readdir,
2089 .llseek = default_llseek,
2092 struct timers_private {
2094 struct task_struct *task;
2095 struct sighand_struct *sighand;
2096 struct pid_namespace *ns;
2097 unsigned long flags;
2100 static void *timers_start(struct seq_file *m, loff_t *pos)
2102 struct timers_private *tp = m->private;
2104 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2106 return ERR_PTR(-ESRCH);
2108 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2110 return ERR_PTR(-ESRCH);
2112 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2115 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2117 struct timers_private *tp = m->private;
2118 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2121 static void timers_stop(struct seq_file *m, void *v)
2123 struct timers_private *tp = m->private;
2126 unlock_task_sighand(tp->task, &tp->flags);
2131 put_task_struct(tp->task);
2136 static int show_timer(struct seq_file *m, void *v)
2138 struct k_itimer *timer;
2139 struct timers_private *tp = m->private;
2141 static char *nstr[] = {
2142 [SIGEV_SIGNAL] = "signal",
2143 [SIGEV_NONE] = "none",
2144 [SIGEV_THREAD] = "thread",
2147 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2148 notify = timer->it_sigev_notify;
2150 seq_printf(m, "ID: %d\n", timer->it_id);
2151 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2152 timer->sigq->info.si_value.sival_ptr);
2153 seq_printf(m, "notify: %s/%s.%d\n",
2154 nstr[notify & ~SIGEV_THREAD_ID],
2155 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2156 pid_nr_ns(timer->it_pid, tp->ns));
2157 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2162 static const struct seq_operations proc_timers_seq_ops = {
2163 .start = timers_start,
2164 .next = timers_next,
2165 .stop = timers_stop,
2169 static int proc_timers_open(struct inode *inode, struct file *file)
2171 struct timers_private *tp;
2173 tp = __seq_open_private(file, &proc_timers_seq_ops,
2174 sizeof(struct timers_private));
2178 tp->pid = proc_pid(inode);
2179 tp->ns = inode->i_sb->s_fs_info;
2183 static const struct file_operations proc_timers_operations = {
2184 .open = proc_timers_open,
2186 .llseek = seq_lseek,
2187 .release = seq_release_private,
2189 #endif /* CONFIG_CHECKPOINT_RESTORE */
2191 static struct dentry *proc_pident_instantiate(struct inode *dir,
2192 struct dentry *dentry, struct task_struct *task, const void *ptr)
2194 const struct pid_entry *p = ptr;
2195 struct inode *inode;
2196 struct proc_inode *ei;
2197 struct dentry *error = ERR_PTR(-ENOENT);
2199 inode = proc_pid_make_inode(dir->i_sb, task);
2204 inode->i_mode = p->mode;
2205 if (S_ISDIR(inode->i_mode))
2206 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2208 inode->i_op = p->iop;
2210 inode->i_fop = p->fop;
2212 d_set_d_op(dentry, &pid_dentry_operations);
2213 d_add(dentry, inode);
2214 /* Close the race of the process dying before we return the dentry */
2215 if (pid_revalidate(dentry, 0))
2221 static struct dentry *proc_pident_lookup(struct inode *dir,
2222 struct dentry *dentry,
2223 const struct pid_entry *ents,
2226 struct dentry *error;
2227 struct task_struct *task = get_proc_task(dir);
2228 const struct pid_entry *p, *last;
2230 error = ERR_PTR(-ENOENT);
2236 * Yes, it does not scale. And it should not. Don't add
2237 * new entries into /proc/<tgid>/ without very good reasons.
2239 last = &ents[nents - 1];
2240 for (p = ents; p <= last; p++) {
2241 if (p->len != dentry->d_name.len)
2243 if (!memcmp(dentry->d_name.name, p->name, p->len))
2249 error = proc_pident_instantiate(dir, dentry, task, p);
2251 put_task_struct(task);
2256 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2257 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2259 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2260 proc_pident_instantiate, task, p);
2263 static int proc_pident_readdir(struct file *filp,
2264 void *dirent, filldir_t filldir,
2265 const struct pid_entry *ents, unsigned int nents)
2268 struct dentry *dentry = filp->f_path.dentry;
2269 struct inode *inode = dentry->d_inode;
2270 struct task_struct *task = get_proc_task(inode);
2271 const struct pid_entry *p, *last;
2284 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2290 ino = parent_ino(dentry);
2291 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2303 last = &ents[nents - 1];
2305 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2314 put_task_struct(task);
2319 #ifdef CONFIG_SECURITY
2320 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2321 size_t count, loff_t *ppos)
2323 struct inode * inode = file_inode(file);
2326 struct task_struct *task = get_proc_task(inode);
2331 length = security_getprocattr(task,
2332 (char*)file->f_path.dentry->d_name.name,
2334 put_task_struct(task);
2336 length = simple_read_from_buffer(buf, count, ppos, p, length);
2341 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2342 size_t count, loff_t *ppos)
2344 struct inode * inode = file_inode(file);
2347 struct task_struct *task = get_proc_task(inode);
2352 if (count > PAGE_SIZE)
2355 /* No partial writes. */
2361 page = (char*)__get_free_page(GFP_TEMPORARY);
2366 if (copy_from_user(page, buf, count))
2369 /* Guard against adverse ptrace interaction */
2370 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2374 length = security_setprocattr(task,
2375 (char*)file->f_path.dentry->d_name.name,
2376 (void*)page, count);
2377 mutex_unlock(&task->signal->cred_guard_mutex);
2379 free_page((unsigned long) page);
2381 put_task_struct(task);
2386 static const struct file_operations proc_pid_attr_operations = {
2387 .read = proc_pid_attr_read,
2388 .write = proc_pid_attr_write,
2389 .llseek = generic_file_llseek,
2392 static const struct pid_entry attr_dir_stuff[] = {
2393 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2394 REG("prev", S_IRUGO, proc_pid_attr_operations),
2395 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2396 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2397 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2398 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2401 static int proc_attr_dir_readdir(struct file * filp,
2402 void * dirent, filldir_t filldir)
2404 return proc_pident_readdir(filp,dirent,filldir,
2405 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2408 static const struct file_operations proc_attr_dir_operations = {
2409 .read = generic_read_dir,
2410 .readdir = proc_attr_dir_readdir,
2411 .llseek = default_llseek,
2414 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2415 struct dentry *dentry, unsigned int flags)
2417 return proc_pident_lookup(dir, dentry,
2418 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2421 static const struct inode_operations proc_attr_dir_inode_operations = {
2422 .lookup = proc_attr_dir_lookup,
2423 .getattr = pid_getattr,
2424 .setattr = proc_setattr,
2429 #ifdef CONFIG_ELF_CORE
2430 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2431 size_t count, loff_t *ppos)
2433 struct task_struct *task = get_proc_task(file_inode(file));
2434 struct mm_struct *mm;
2435 char buffer[PROC_NUMBUF];
2443 mm = get_task_mm(task);
2445 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2446 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2447 MMF_DUMP_FILTER_SHIFT));
2449 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2452 put_task_struct(task);
2457 static ssize_t proc_coredump_filter_write(struct file *file,
2458 const char __user *buf,
2462 struct task_struct *task;
2463 struct mm_struct *mm;
2464 char buffer[PROC_NUMBUF], *end;
2471 memset(buffer, 0, sizeof(buffer));
2472 if (count > sizeof(buffer) - 1)
2473 count = sizeof(buffer) - 1;
2474 if (copy_from_user(buffer, buf, count))
2478 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2481 if (end - buffer == 0)
2485 task = get_proc_task(file_inode(file));
2490 mm = get_task_mm(task);
2494 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2496 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2498 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2503 put_task_struct(task);
2508 static const struct file_operations proc_coredump_filter_operations = {
2509 .read = proc_coredump_filter_read,
2510 .write = proc_coredump_filter_write,
2511 .llseek = generic_file_llseek,
2515 #ifdef CONFIG_TASK_IO_ACCOUNTING
2516 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2518 struct task_io_accounting acct = task->ioac;
2519 unsigned long flags;
2522 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2526 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2531 if (whole && lock_task_sighand(task, &flags)) {
2532 struct task_struct *t = task;
2534 task_io_accounting_add(&acct, &task->signal->ioac);
2535 while_each_thread(task, t)
2536 task_io_accounting_add(&acct, &t->ioac);
2538 unlock_task_sighand(task, &flags);
2540 result = sprintf(buffer,
2545 "read_bytes: %llu\n"
2546 "write_bytes: %llu\n"
2547 "cancelled_write_bytes: %llu\n",
2548 (unsigned long long)acct.rchar,
2549 (unsigned long long)acct.wchar,
2550 (unsigned long long)acct.syscr,
2551 (unsigned long long)acct.syscw,
2552 (unsigned long long)acct.read_bytes,
2553 (unsigned long long)acct.write_bytes,
2554 (unsigned long long)acct.cancelled_write_bytes);
2556 mutex_unlock(&task->signal->cred_guard_mutex);
2560 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2562 return do_io_accounting(task, buffer, 0);
2565 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2567 return do_io_accounting(task, buffer, 1);
2569 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2571 #ifdef CONFIG_USER_NS
2572 static int proc_id_map_open(struct inode *inode, struct file *file,
2573 struct seq_operations *seq_ops)
2575 struct user_namespace *ns = NULL;
2576 struct task_struct *task;
2577 struct seq_file *seq;
2580 task = get_proc_task(inode);
2583 ns = get_user_ns(task_cred_xxx(task, user_ns));
2585 put_task_struct(task);
2590 ret = seq_open(file, seq_ops);
2594 seq = file->private_data;
2604 static int proc_id_map_release(struct inode *inode, struct file *file)
2606 struct seq_file *seq = file->private_data;
2607 struct user_namespace *ns = seq->private;
2609 return seq_release(inode, file);
2612 static int proc_uid_map_open(struct inode *inode, struct file *file)
2614 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2617 static int proc_gid_map_open(struct inode *inode, struct file *file)
2619 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2622 static int proc_projid_map_open(struct inode *inode, struct file *file)
2624 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2627 static const struct file_operations proc_uid_map_operations = {
2628 .open = proc_uid_map_open,
2629 .write = proc_uid_map_write,
2631 .llseek = seq_lseek,
2632 .release = proc_id_map_release,
2635 static const struct file_operations proc_gid_map_operations = {
2636 .open = proc_gid_map_open,
2637 .write = proc_gid_map_write,
2639 .llseek = seq_lseek,
2640 .release = proc_id_map_release,
2643 static const struct file_operations proc_projid_map_operations = {
2644 .open = proc_projid_map_open,
2645 .write = proc_projid_map_write,
2647 .llseek = seq_lseek,
2648 .release = proc_id_map_release,
2651 static int proc_setgroups_open(struct inode *inode, struct file *file)
2653 struct user_namespace *ns = NULL;
2654 struct task_struct *task;
2658 task = get_proc_task(inode);
2661 ns = get_user_ns(task_cred_xxx(task, user_ns));
2663 put_task_struct(task);
2668 if (file->f_mode & FMODE_WRITE) {
2670 if (!ns_capable(ns, CAP_SYS_ADMIN))
2674 ret = single_open(file, &proc_setgroups_show, ns);
2685 static int proc_setgroups_release(struct inode *inode, struct file *file)
2687 struct seq_file *seq = file->private_data;
2688 struct user_namespace *ns = seq->private;
2689 int ret = single_release(inode, file);
2694 static const struct file_operations proc_setgroups_operations = {
2695 .open = proc_setgroups_open,
2696 .write = proc_setgroups_write,
2698 .llseek = seq_lseek,
2699 .release = proc_setgroups_release,
2701 #endif /* CONFIG_USER_NS */
2703 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2704 struct pid *pid, struct task_struct *task)
2706 int err = lock_trace(task);
2708 seq_printf(m, "%08x\n", task->personality);
2717 static const struct file_operations proc_task_operations;
2718 static const struct inode_operations proc_task_inode_operations;
2720 static const struct pid_entry tgid_base_stuff[] = {
2721 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2722 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2723 #ifdef CONFIG_CHECKPOINT_RESTORE
2724 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2726 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2727 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2729 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2731 REG("environ", S_IRUSR, proc_environ_operations),
2732 INF("auxv", S_IRUSR, proc_pid_auxv),
2733 ONE("status", S_IRUGO, proc_pid_status),
2734 ONE("personality", S_IRUGO, proc_pid_personality),
2735 INF("limits", S_IRUGO, proc_pid_limits),
2736 #ifdef CONFIG_SCHED_DEBUG
2737 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2739 #ifdef CONFIG_SCHED_AUTOGROUP
2740 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2742 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2743 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2744 INF("syscall", S_IRUGO, proc_pid_syscall),
2746 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2747 ONE("stat", S_IRUGO, proc_tgid_stat),
2748 ONE("statm", S_IRUGO, proc_pid_statm),
2749 REG("maps", S_IRUGO, proc_pid_maps_operations),
2751 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2753 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2754 LNK("cwd", proc_cwd_link),
2755 LNK("root", proc_root_link),
2756 LNK("exe", proc_exe_link),
2757 REG("mounts", S_IRUGO, proc_mounts_operations),
2758 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2759 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2760 #ifdef CONFIG_PROC_PAGE_MONITOR
2761 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2762 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2763 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2765 #ifdef CONFIG_SECURITY
2766 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2768 #ifdef CONFIG_KALLSYMS
2769 INF("wchan", S_IRUGO, proc_pid_wchan),
2771 #ifdef CONFIG_STACKTRACE
2772 ONE("stack", S_IRUGO, proc_pid_stack),
2774 #ifdef CONFIG_SCHEDSTATS
2775 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2777 #ifdef CONFIG_LATENCYTOP
2778 REG("latency", S_IRUGO, proc_lstats_operations),
2780 #ifdef CONFIG_PROC_PID_CPUSET
2781 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2783 #ifdef CONFIG_CGROUPS
2784 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2786 INF("oom_score", S_IRUGO, proc_oom_score),
2787 ANDROID("oom_adj", S_IRUGO|S_IWUSR, oom_adj),
2788 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2789 #ifdef CONFIG_AUDITSYSCALL
2790 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2791 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2793 #ifdef CONFIG_FAULT_INJECTION
2794 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2796 #ifdef CONFIG_ELF_CORE
2797 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2799 #ifdef CONFIG_TASK_IO_ACCOUNTING
2800 INF("io", S_IRUSR, proc_tgid_io_accounting),
2802 #ifdef CONFIG_HARDWALL
2803 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2805 #ifdef CONFIG_USER_NS
2806 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2807 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2808 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2809 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2811 #ifdef CONFIG_CHECKPOINT_RESTORE
2812 REG("timers", S_IRUGO, proc_timers_operations),
2816 static int proc_tgid_base_readdir(struct file * filp,
2817 void * dirent, filldir_t filldir)
2819 return proc_pident_readdir(filp,dirent,filldir,
2820 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2823 static const struct file_operations proc_tgid_base_operations = {
2824 .read = generic_read_dir,
2825 .readdir = proc_tgid_base_readdir,
2826 .llseek = default_llseek,
2829 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2831 return proc_pident_lookup(dir, dentry,
2832 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2835 static const struct inode_operations proc_tgid_base_inode_operations = {
2836 .lookup = proc_tgid_base_lookup,
2837 .getattr = pid_getattr,
2838 .setattr = proc_setattr,
2839 .permission = proc_pid_permission,
2842 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2844 struct dentry *dentry, *leader, *dir;
2845 char buf[PROC_NUMBUF];
2849 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2850 /* no ->d_hash() rejects on procfs */
2851 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2853 shrink_dcache_parent(dentry);
2859 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2860 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2865 name.len = strlen(name.name);
2866 dir = d_hash_and_lookup(leader, &name);
2868 goto out_put_leader;
2871 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2872 dentry = d_hash_and_lookup(dir, &name);
2874 shrink_dcache_parent(dentry);
2887 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2888 * @task: task that should be flushed.
2890 * When flushing dentries from proc, one needs to flush them from global
2891 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2892 * in. This call is supposed to do all of this job.
2894 * Looks in the dcache for
2896 * /proc/@tgid/task/@pid
2897 * if either directory is present flushes it and all of it'ts children
2900 * It is safe and reasonable to cache /proc entries for a task until
2901 * that task exits. After that they just clog up the dcache with
2902 * useless entries, possibly causing useful dcache entries to be
2903 * flushed instead. This routine is proved to flush those useless
2904 * dcache entries at process exit time.
2906 * NOTE: This routine is just an optimization so it does not guarantee
2907 * that no dcache entries will exist at process exit time it
2908 * just makes it very unlikely that any will persist.
2911 void proc_flush_task(struct task_struct *task)
2914 struct pid *pid, *tgid;
2917 pid = task_pid(task);
2918 tgid = task_tgid(task);
2920 for (i = 0; i <= pid->level; i++) {
2921 upid = &pid->numbers[i];
2922 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2923 tgid->numbers[i].nr);
2927 static struct dentry *proc_pid_instantiate(struct inode *dir,
2928 struct dentry * dentry,
2929 struct task_struct *task, const void *ptr)
2931 struct dentry *error = ERR_PTR(-ENOENT);
2932 struct inode *inode;
2934 inode = proc_pid_make_inode(dir->i_sb, task);
2938 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2939 inode->i_op = &proc_tgid_base_inode_operations;
2940 inode->i_fop = &proc_tgid_base_operations;
2941 inode->i_flags|=S_IMMUTABLE;
2943 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2944 ARRAY_SIZE(tgid_base_stuff)));
2946 d_set_d_op(dentry, &pid_dentry_operations);
2948 d_add(dentry, inode);
2949 /* Close the race of the process dying before we return the dentry */
2950 if (pid_revalidate(dentry, 0))
2956 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2958 struct dentry *result = NULL;
2959 struct task_struct *task;
2961 struct pid_namespace *ns;
2963 tgid = name_to_int(dentry);
2967 ns = dentry->d_sb->s_fs_info;
2969 task = find_task_by_pid_ns(tgid, ns);
2971 get_task_struct(task);
2976 result = proc_pid_instantiate(dir, dentry, task, NULL);
2977 put_task_struct(task);
2983 * Find the first task with tgid >= tgid
2988 struct task_struct *task;
2990 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2995 put_task_struct(iter.task);
2999 pid = find_ge_pid(iter.tgid, ns);
3001 iter.tgid = pid_nr_ns(pid, ns);
3002 iter.task = pid_task(pid, PIDTYPE_PID);
3003 /* What we to know is if the pid we have find is the
3004 * pid of a thread_group_leader. Testing for task
3005 * being a thread_group_leader is the obvious thing
3006 * todo but there is a window when it fails, due to
3007 * the pid transfer logic in de_thread.
3009 * So we perform the straight forward test of seeing
3010 * if the pid we have found is the pid of a thread
3011 * group leader, and don't worry if the task we have
3012 * found doesn't happen to be a thread group leader.
3013 * As we don't care in the case of readdir.
3015 if (!iter.task || !has_group_leader_pid(iter.task)) {
3019 get_task_struct(iter.task);
3025 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
3027 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3028 struct tgid_iter iter)
3030 char name[PROC_NUMBUF];
3031 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3032 return proc_fill_cache(filp, dirent, filldir, name, len,
3033 proc_pid_instantiate, iter.task, NULL);
3036 static int fake_filldir(void *buf, const char *name, int namelen,
3037 loff_t offset, u64 ino, unsigned d_type)
3042 /* for the /proc/ directory itself, after non-process stuff has been done */
3043 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3045 struct tgid_iter iter;
3046 struct pid_namespace *ns;
3047 filldir_t __filldir;
3048 loff_t pos = filp->f_pos;
3050 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
3053 if (pos == TGID_OFFSET - 1) {
3054 if (proc_fill_cache(filp, dirent, filldir, "self", 4,
3055 NULL, NULL, NULL) < 0)
3059 iter.tgid = pos - TGID_OFFSET;
3062 ns = filp->f_dentry->d_sb->s_fs_info;
3063 for (iter = next_tgid(ns, iter);
3065 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3066 if (has_pid_permissions(ns, iter.task, 2))
3067 __filldir = filldir;
3069 __filldir = fake_filldir;
3071 filp->f_pos = iter.tgid + TGID_OFFSET;
3072 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3073 put_task_struct(iter.task);
3077 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3085 static const struct pid_entry tid_base_stuff[] = {
3086 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3087 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3088 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3089 REG("environ", S_IRUSR, proc_environ_operations),
3090 INF("auxv", S_IRUSR, proc_pid_auxv),
3091 ONE("status", S_IRUGO, proc_pid_status),
3092 ONE("personality", S_IRUGO, proc_pid_personality),
3093 INF("limits", S_IRUGO, proc_pid_limits),
3094 #ifdef CONFIG_SCHED_DEBUG
3095 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3097 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3098 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3099 INF("syscall", S_IRUGO, proc_pid_syscall),
3101 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3102 ONE("stat", S_IRUGO, proc_tid_stat),
3103 ONE("statm", S_IRUGO, proc_pid_statm),
3104 REG("maps", S_IRUGO, proc_tid_maps_operations),
3105 #ifdef CONFIG_CHECKPOINT_RESTORE
3106 REG("children", S_IRUGO, proc_tid_children_operations),
3109 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3111 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3112 LNK("cwd", proc_cwd_link),
3113 LNK("root", proc_root_link),
3114 LNK("exe", proc_exe_link),
3115 REG("mounts", S_IRUGO, proc_mounts_operations),
3116 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3117 #ifdef CONFIG_PROC_PAGE_MONITOR
3118 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3119 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3120 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3122 #ifdef CONFIG_SECURITY
3123 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3125 #ifdef CONFIG_KALLSYMS
3126 INF("wchan", S_IRUGO, proc_pid_wchan),
3128 #ifdef CONFIG_STACKTRACE
3129 ONE("stack", S_IRUGO, proc_pid_stack),
3131 #ifdef CONFIG_SCHEDSTATS
3132 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3134 #ifdef CONFIG_LATENCYTOP
3135 REG("latency", S_IRUGO, proc_lstats_operations),
3137 #ifdef CONFIG_PROC_PID_CPUSET
3138 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3140 #ifdef CONFIG_CGROUPS
3141 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3143 INF("oom_score", S_IRUGO, proc_oom_score),
3144 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3145 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3146 #ifdef CONFIG_AUDITSYSCALL
3147 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3148 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3150 #ifdef CONFIG_FAULT_INJECTION
3151 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3153 #ifdef CONFIG_TASK_IO_ACCOUNTING
3154 INF("io", S_IRUSR, proc_tid_io_accounting),
3156 #ifdef CONFIG_HARDWALL
3157 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3159 #ifdef CONFIG_USER_NS
3160 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3161 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3162 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3163 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
3167 static int proc_tid_base_readdir(struct file * filp,
3168 void * dirent, filldir_t filldir)
3170 return proc_pident_readdir(filp,dirent,filldir,
3171 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3174 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3176 return proc_pident_lookup(dir, dentry,
3177 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3180 static const struct file_operations proc_tid_base_operations = {
3181 .read = generic_read_dir,
3182 .readdir = proc_tid_base_readdir,
3183 .llseek = default_llseek,
3186 static const struct inode_operations proc_tid_base_inode_operations = {
3187 .lookup = proc_tid_base_lookup,
3188 .getattr = pid_getattr,
3189 .setattr = proc_setattr,
3192 static struct dentry *proc_task_instantiate(struct inode *dir,
3193 struct dentry *dentry, struct task_struct *task, const void *ptr)
3195 struct dentry *error = ERR_PTR(-ENOENT);
3196 struct inode *inode;
3197 inode = proc_pid_make_inode(dir->i_sb, task);
3201 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3202 inode->i_op = &proc_tid_base_inode_operations;
3203 inode->i_fop = &proc_tid_base_operations;
3204 inode->i_flags|=S_IMMUTABLE;
3206 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3207 ARRAY_SIZE(tid_base_stuff)));
3209 d_set_d_op(dentry, &pid_dentry_operations);
3211 d_add(dentry, inode);
3212 /* Close the race of the process dying before we return the dentry */
3213 if (pid_revalidate(dentry, 0))
3219 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3221 struct dentry *result = ERR_PTR(-ENOENT);
3222 struct task_struct *task;
3223 struct task_struct *leader = get_proc_task(dir);
3225 struct pid_namespace *ns;
3230 tid = name_to_int(dentry);
3234 ns = dentry->d_sb->s_fs_info;
3236 task = find_task_by_pid_ns(tid, ns);
3238 get_task_struct(task);
3242 if (!same_thread_group(leader, task))
3245 result = proc_task_instantiate(dir, dentry, task, NULL);
3247 put_task_struct(task);
3249 put_task_struct(leader);
3255 * Find the first tid of a thread group to return to user space.
3257 * Usually this is just the thread group leader, but if the users
3258 * buffer was too small or there was a seek into the middle of the
3259 * directory we have more work todo.
3261 * In the case of a short read we start with find_task_by_pid.
3263 * In the case of a seek we start with the leader and walk nr
3266 static struct task_struct *first_tid(struct task_struct *leader,
3267 int tid, int nr, struct pid_namespace *ns)
3269 struct task_struct *pos;
3272 /* Attempt to start with the pid of a thread */
3273 if (tid && (nr > 0)) {
3274 pos = find_task_by_pid_ns(tid, ns);
3275 if (pos && (pos->group_leader == leader))
3279 /* If nr exceeds the number of threads there is nothing todo */
3281 if (nr && nr >= get_nr_threads(leader))
3284 /* If we haven't found our starting place yet start
3285 * with the leader and walk nr threads forward.
3287 for (pos = leader; nr > 0; --nr) {
3288 pos = next_thread(pos);
3289 if (pos == leader) {
3295 get_task_struct(pos);
3302 * Find the next thread in the thread list.
3303 * Return NULL if there is an error or no next thread.
3305 * The reference to the input task_struct is released.
3307 static struct task_struct *next_tid(struct task_struct *start)
3309 struct task_struct *pos = NULL;
3311 if (pid_alive(start)) {
3312 pos = next_thread(start);
3313 if (thread_group_leader(pos))
3316 get_task_struct(pos);
3319 put_task_struct(start);
3323 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3324 struct task_struct *task, int tid)
3326 char name[PROC_NUMBUF];
3327 int len = snprintf(name, sizeof(name), "%d", tid);
3328 return proc_fill_cache(filp, dirent, filldir, name, len,
3329 proc_task_instantiate, task, NULL);
3332 /* for the /proc/TGID/task/ directories */
3333 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3335 struct dentry *dentry = filp->f_path.dentry;
3336 struct inode *inode = dentry->d_inode;
3337 struct task_struct *leader = NULL;
3338 struct task_struct *task;
3339 int retval = -ENOENT;
3342 struct pid_namespace *ns;
3344 task = get_proc_task(inode);
3348 if (pid_alive(task)) {
3349 leader = task->group_leader;
3350 get_task_struct(leader);
3353 put_task_struct(task);
3358 switch ((unsigned long)filp->f_pos) {
3361 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3366 ino = parent_ino(dentry);
3367 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3373 /* f_version caches the tgid value that the last readdir call couldn't
3374 * return. lseek aka telldir automagically resets f_version to 0.
3376 ns = filp->f_dentry->d_sb->s_fs_info;
3377 tid = (int)filp->f_version;
3378 filp->f_version = 0;
3379 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3381 task = next_tid(task), filp->f_pos++) {
3382 tid = task_pid_nr_ns(task, ns);
3383 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3384 /* returning this tgid failed, save it as the first
3385 * pid for the next readir call */
3386 filp->f_version = (u64)tid;
3387 put_task_struct(task);
3392 put_task_struct(leader);
3397 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3399 struct inode *inode = dentry->d_inode;
3400 struct task_struct *p = get_proc_task(inode);
3401 generic_fillattr(inode, stat);
3404 stat->nlink += get_nr_threads(p);
3411 static const struct inode_operations proc_task_inode_operations = {
3412 .lookup = proc_task_lookup,
3413 .getattr = proc_task_getattr,
3414 .setattr = proc_setattr,
3415 .permission = proc_pid_permission,
3418 static const struct file_operations proc_task_operations = {
3419 .read = generic_read_dir,
3420 .readdir = proc_task_readdir,
3421 .llseek = default_llseek,