4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
86 #include <linux/flex_array.h>
87 #ifdef CONFIG_HARDWALL
88 #include <asm/hardwall.h>
90 #include <trace/events/oom.h>
94 * Implementing inode permission operations in /proc is almost
95 * certainly an error. Permission checks need to happen during
96 * each system call not at open time. The reason is that most of
97 * what we wish to check for permissions in /proc varies at runtime.
99 * The classic example of a problem is opening file descriptors
100 * in /proc for a task before it execs a suid executable.
107 const struct inode_operations *iop;
108 const struct file_operations *fop;
112 #define NOD(NAME, MODE, IOP, FOP, OP) { \
114 .len = sizeof(NAME) - 1, \
121 #define DIR(NAME, MODE, iops, fops) \
122 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
123 #define LNK(NAME, get_link) \
124 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
125 &proc_pid_link_inode_operations, NULL, \
126 { .proc_get_link = get_link } )
127 #define REG(NAME, MODE, fops) \
128 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
129 #define INF(NAME, MODE, read) \
130 NOD(NAME, (S_IFREG|(MODE)), \
131 NULL, &proc_info_file_operations, \
132 { .proc_read = read } )
133 #define ONE(NAME, MODE, show) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
138 static int proc_fd_permission(struct inode *inode, int mask);
141 * Count the number of hardlinks for the pid_entry table, excluding the .
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
159 static int get_task_root(struct task_struct *task, struct path *root)
161 int result = -ENOENT;
165 get_fs_root(task->fs, root);
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
174 struct task_struct *task = get_proc_task(dentry->d_inode);
175 int result = -ENOENT;
180 get_fs_pwd(task->fs, path);
184 put_task_struct(task);
189 static int proc_root_link(struct dentry *dentry, struct path *path)
191 struct task_struct *task = get_proc_task(dentry->d_inode);
192 int result = -ENOENT;
195 result = get_task_root(task, path);
196 put_task_struct(task);
201 static struct mm_struct *__check_mem_permission(struct task_struct *task)
203 struct mm_struct *mm;
205 mm = get_task_mm(task);
207 return ERR_PTR(-EINVAL);
210 * A task can always look at itself, in case it chooses
211 * to use system calls instead of load instructions.
217 * If current is actively ptrace'ing, and would also be
218 * permitted to freshly attach with ptrace now, permit it.
220 if (task_is_stopped_or_traced(task)) {
223 match = (ptrace_parent(task) == current);
225 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
230 * No one else is allowed.
233 return ERR_PTR(-EPERM);
237 * If current may access user memory in @task return a reference to the
238 * corresponding mm, otherwise ERR_PTR.
240 static struct mm_struct *check_mem_permission(struct task_struct *task)
242 struct mm_struct *mm;
246 * Avoid racing if task exec's as we might get a new mm but validate
247 * against old credentials.
249 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
253 mm = __check_mem_permission(task);
254 mutex_unlock(&task->signal->cred_guard_mutex);
259 struct mm_struct *mm_for_maps(struct task_struct *task)
261 struct mm_struct *mm;
264 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
268 mm = get_task_mm(task);
269 if (mm && mm != current->mm &&
270 !ptrace_may_access(task, PTRACE_MODE_READ)) {
272 mm = ERR_PTR(-EACCES);
274 mutex_unlock(&task->signal->cred_guard_mutex);
279 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
283 struct mm_struct *mm = get_task_mm(task);
287 goto out_mm; /* Shh! No looking before we're done */
289 len = mm->arg_end - mm->arg_start;
294 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
296 // If the nul at the end of args has been overwritten, then
297 // assume application is using setproctitle(3).
298 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
299 len = strnlen(buffer, res);
303 len = mm->env_end - mm->env_start;
304 if (len > PAGE_SIZE - res)
305 len = PAGE_SIZE - res;
306 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
307 res = strnlen(buffer, res);
316 static int proc_pid_auxv(struct task_struct *task, char *buffer)
318 struct mm_struct *mm = mm_for_maps(task);
319 int res = PTR_ERR(mm);
320 if (mm && !IS_ERR(mm)) {
321 unsigned int nwords = 0;
324 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
325 res = nwords * sizeof(mm->saved_auxv[0]);
328 memcpy(buffer, mm->saved_auxv, res);
335 #ifdef CONFIG_KALLSYMS
337 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
338 * Returns the resolved symbol. If that fails, simply return the address.
340 static int proc_pid_wchan(struct task_struct *task, char *buffer)
343 char symname[KSYM_NAME_LEN];
345 wchan = get_wchan(task);
347 if (lookup_symbol_name(wchan, symname) < 0)
348 if (!ptrace_may_access(task, PTRACE_MODE_READ))
351 return sprintf(buffer, "%lu", wchan);
353 return sprintf(buffer, "%s", symname);
355 #endif /* CONFIG_KALLSYMS */
357 static int lock_trace(struct task_struct *task)
359 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
362 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
363 mutex_unlock(&task->signal->cred_guard_mutex);
369 static void unlock_trace(struct task_struct *task)
371 mutex_unlock(&task->signal->cred_guard_mutex);
374 #ifdef CONFIG_STACKTRACE
376 #define MAX_STACK_TRACE_DEPTH 64
378 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
379 struct pid *pid, struct task_struct *task)
381 struct stack_trace trace;
382 unsigned long *entries;
386 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
390 trace.nr_entries = 0;
391 trace.max_entries = MAX_STACK_TRACE_DEPTH;
392 trace.entries = entries;
395 err = lock_trace(task);
397 save_stack_trace_tsk(task, &trace);
399 for (i = 0; i < trace.nr_entries; i++) {
400 seq_printf(m, "[<%pK>] %pS\n",
401 (void *)entries[i], (void *)entries[i]);
411 #ifdef CONFIG_SCHEDSTATS
413 * Provides /proc/PID/schedstat
415 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
417 return sprintf(buffer, "%llu %llu %lu\n",
418 (unsigned long long)task->se.sum_exec_runtime,
419 (unsigned long long)task->sched_info.run_delay,
420 task->sched_info.pcount);
424 #ifdef CONFIG_LATENCYTOP
425 static int lstats_show_proc(struct seq_file *m, void *v)
428 struct inode *inode = m->private;
429 struct task_struct *task = get_proc_task(inode);
433 seq_puts(m, "Latency Top version : v0.1\n");
434 for (i = 0; i < 32; i++) {
435 struct latency_record *lr = &task->latency_record[i];
436 if (lr->backtrace[0]) {
438 seq_printf(m, "%i %li %li",
439 lr->count, lr->time, lr->max);
440 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
441 unsigned long bt = lr->backtrace[q];
446 seq_printf(m, " %ps", (void *)bt);
452 put_task_struct(task);
456 static int lstats_open(struct inode *inode, struct file *file)
458 return single_open(file, lstats_show_proc, inode);
461 static ssize_t lstats_write(struct file *file, const char __user *buf,
462 size_t count, loff_t *offs)
464 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
468 clear_all_latency_tracing(task);
469 put_task_struct(task);
474 static const struct file_operations proc_lstats_operations = {
477 .write = lstats_write,
479 .release = single_release,
484 static int proc_oom_score(struct task_struct *task, char *buffer)
486 unsigned long points = 0;
488 read_lock(&tasklist_lock);
490 points = oom_badness(task, NULL, NULL,
491 totalram_pages + total_swap_pages);
492 read_unlock(&tasklist_lock);
493 return sprintf(buffer, "%lu\n", points);
501 static const struct limit_names lnames[RLIM_NLIMITS] = {
502 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
503 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
504 [RLIMIT_DATA] = {"Max data size", "bytes"},
505 [RLIMIT_STACK] = {"Max stack size", "bytes"},
506 [RLIMIT_CORE] = {"Max core file size", "bytes"},
507 [RLIMIT_RSS] = {"Max resident set", "bytes"},
508 [RLIMIT_NPROC] = {"Max processes", "processes"},
509 [RLIMIT_NOFILE] = {"Max open files", "files"},
510 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
511 [RLIMIT_AS] = {"Max address space", "bytes"},
512 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
513 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
514 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
515 [RLIMIT_NICE] = {"Max nice priority", NULL},
516 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
517 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
520 /* Display limits for a process */
521 static int proc_pid_limits(struct task_struct *task, char *buffer)
526 char *bufptr = buffer;
528 struct rlimit rlim[RLIM_NLIMITS];
530 if (!lock_task_sighand(task, &flags))
532 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
533 unlock_task_sighand(task, &flags);
536 * print the file header
538 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
539 "Limit", "Soft Limit", "Hard Limit", "Units");
541 for (i = 0; i < RLIM_NLIMITS; i++) {
542 if (rlim[i].rlim_cur == RLIM_INFINITY)
543 count += sprintf(&bufptr[count], "%-25s %-20s ",
544 lnames[i].name, "unlimited");
546 count += sprintf(&bufptr[count], "%-25s %-20lu ",
547 lnames[i].name, rlim[i].rlim_cur);
549 if (rlim[i].rlim_max == RLIM_INFINITY)
550 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
552 count += sprintf(&bufptr[count], "%-20lu ",
556 count += sprintf(&bufptr[count], "%-10s\n",
559 count += sprintf(&bufptr[count], "\n");
565 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
566 static int proc_pid_syscall(struct task_struct *task, char *buffer)
569 unsigned long args[6], sp, pc;
570 int res = lock_trace(task);
574 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
575 res = sprintf(buffer, "running\n");
577 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
579 res = sprintf(buffer,
580 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
582 args[0], args[1], args[2], args[3], args[4], args[5],
587 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
589 /************************************************************************/
590 /* Here the fs part begins */
591 /************************************************************************/
593 /* permission checks */
594 static int proc_fd_access_allowed(struct inode *inode)
596 struct task_struct *task;
598 /* Allow access to a task's file descriptors if it is us or we
599 * may use ptrace attach to the process and find out that
602 task = get_proc_task(inode);
604 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
605 put_task_struct(task);
610 int proc_setattr(struct dentry *dentry, struct iattr *attr)
613 struct inode *inode = dentry->d_inode;
615 if (attr->ia_valid & ATTR_MODE)
618 error = inode_change_ok(inode, attr);
622 if ((attr->ia_valid & ATTR_SIZE) &&
623 attr->ia_size != i_size_read(inode)) {
624 error = vmtruncate(inode, attr->ia_size);
629 setattr_copy(inode, attr);
630 mark_inode_dirty(inode);
635 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
636 * or euid/egid (for hide_pid_min=2)?
638 static bool has_pid_permissions(struct pid_namespace *pid,
639 struct task_struct *task,
642 if (pid->hide_pid < hide_pid_min)
644 if (in_group_p(pid->pid_gid))
646 return ptrace_may_access(task, PTRACE_MODE_READ);
650 static int proc_pid_permission(struct inode *inode, int mask)
652 struct pid_namespace *pid = inode->i_sb->s_fs_info;
653 struct task_struct *task;
656 task = get_proc_task(inode);
657 has_perms = has_pid_permissions(pid, task, 1);
658 put_task_struct(task);
661 if (pid->hide_pid == 2) {
663 * Let's make getdents(), stat(), and open()
664 * consistent with each other. If a process
665 * may not stat() a file, it shouldn't be seen
673 return generic_permission(inode, mask);
678 static const struct inode_operations proc_def_inode_operations = {
679 .setattr = proc_setattr,
682 static int mounts_open_common(struct inode *inode, struct file *file,
683 const struct seq_operations *op)
685 struct task_struct *task = get_proc_task(inode);
687 struct mnt_namespace *ns = NULL;
689 struct proc_mounts *p;
694 nsp = task_nsproxy(task);
701 if (ns && get_task_root(task, &root) == 0)
703 put_task_struct(task);
712 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
716 file->private_data = &p->m;
717 ret = seq_open(file, op);
724 p->m.poll_event = ns->event;
738 static int mounts_release(struct inode *inode, struct file *file)
740 struct proc_mounts *p = file->private_data;
743 return seq_release(inode, file);
746 static unsigned mounts_poll(struct file *file, poll_table *wait)
748 struct proc_mounts *p = file->private_data;
749 unsigned res = POLLIN | POLLRDNORM;
751 poll_wait(file, &p->ns->poll, wait);
752 if (mnt_had_events(p))
753 res |= POLLERR | POLLPRI;
758 static int mounts_open(struct inode *inode, struct file *file)
760 return mounts_open_common(inode, file, &mounts_op);
763 static const struct file_operations proc_mounts_operations = {
767 .release = mounts_release,
771 static int mountinfo_open(struct inode *inode, struct file *file)
773 return mounts_open_common(inode, file, &mountinfo_op);
776 static const struct file_operations proc_mountinfo_operations = {
777 .open = mountinfo_open,
780 .release = mounts_release,
784 static int mountstats_open(struct inode *inode, struct file *file)
786 return mounts_open_common(inode, file, &mountstats_op);
789 static const struct file_operations proc_mountstats_operations = {
790 .open = mountstats_open,
793 .release = mounts_release,
796 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
798 static ssize_t proc_info_read(struct file * file, char __user * buf,
799 size_t count, loff_t *ppos)
801 struct inode * inode = file->f_path.dentry->d_inode;
804 struct task_struct *task = get_proc_task(inode);
810 if (count > PROC_BLOCK_SIZE)
811 count = PROC_BLOCK_SIZE;
814 if (!(page = __get_free_page(GFP_TEMPORARY)))
817 length = PROC_I(inode)->op.proc_read(task, (char*)page);
820 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
823 put_task_struct(task);
828 static const struct file_operations proc_info_file_operations = {
829 .read = proc_info_read,
830 .llseek = generic_file_llseek,
833 static int proc_single_show(struct seq_file *m, void *v)
835 struct inode *inode = m->private;
836 struct pid_namespace *ns;
838 struct task_struct *task;
841 ns = inode->i_sb->s_fs_info;
842 pid = proc_pid(inode);
843 task = get_pid_task(pid, PIDTYPE_PID);
847 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
849 put_task_struct(task);
853 static int proc_single_open(struct inode *inode, struct file *filp)
855 return single_open(filp, proc_single_show, inode);
858 static const struct file_operations proc_single_file_operations = {
859 .open = proc_single_open,
862 .release = single_release,
865 static int mem_open(struct inode* inode, struct file* file)
867 file->private_data = (void*)((long)current->self_exec_id);
868 /* OK to pass negative loff_t, we can catch out-of-range */
869 file->f_mode |= FMODE_UNSIGNED_OFFSET;
873 static ssize_t mem_read(struct file * file, char __user * buf,
874 size_t count, loff_t *ppos)
876 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
878 unsigned long src = *ppos;
880 struct mm_struct *mm;
886 page = (char *)__get_free_page(GFP_TEMPORARY);
890 mm = check_mem_permission(task);
897 if (file->private_data != (void*)((long)current->self_exec_id))
903 int this_len, retval;
905 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
906 retval = access_remote_vm(mm, src, page, this_len, 0);
913 if (copy_to_user(buf, page, retval)) {
928 free_page((unsigned long) page);
930 put_task_struct(task);
935 static ssize_t mem_write(struct file * file, const char __user *buf,
936 size_t count, loff_t *ppos)
940 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
941 unsigned long dst = *ppos;
942 struct mm_struct *mm;
949 page = (char *)__get_free_page(GFP_TEMPORARY);
953 mm = check_mem_permission(task);
954 copied = PTR_ERR(mm);
959 if (file->private_data != (void *)((long)current->self_exec_id))
964 int this_len, retval;
966 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
967 if (copy_from_user(page, buf, this_len)) {
971 retval = access_remote_vm(mm, dst, page, this_len, 1);
987 free_page((unsigned long) page);
989 put_task_struct(task);
994 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
998 file->f_pos = offset;
1001 file->f_pos += offset;
1006 force_successful_syscall_return();
1010 static const struct file_operations proc_mem_operations = {
1011 .llseek = mem_lseek,
1017 static ssize_t environ_read(struct file *file, char __user *buf,
1018 size_t count, loff_t *ppos)
1020 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1022 unsigned long src = *ppos;
1024 struct mm_struct *mm;
1030 page = (char *)__get_free_page(GFP_TEMPORARY);
1035 mm = mm_for_maps(task);
1037 if (!mm || IS_ERR(mm))
1042 int this_len, retval, max_len;
1044 this_len = mm->env_end - (mm->env_start + src);
1049 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1050 this_len = (this_len > max_len) ? max_len : this_len;
1052 retval = access_process_vm(task, (mm->env_start + src),
1060 if (copy_to_user(buf, page, retval)) {
1074 free_page((unsigned long) page);
1076 put_task_struct(task);
1081 static const struct file_operations proc_environ_operations = {
1082 .read = environ_read,
1083 .llseek = generic_file_llseek,
1086 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1087 size_t count, loff_t *ppos)
1089 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1090 char buffer[PROC_NUMBUF];
1092 int oom_adjust = OOM_DISABLE;
1093 unsigned long flags;
1098 if (lock_task_sighand(task, &flags)) {
1099 oom_adjust = task->signal->oom_adj;
1100 unlock_task_sighand(task, &flags);
1103 put_task_struct(task);
1105 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1107 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1110 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1111 size_t count, loff_t *ppos)
1113 struct task_struct *task;
1114 char buffer[PROC_NUMBUF];
1116 unsigned long flags;
1119 memset(buffer, 0, sizeof(buffer));
1120 if (count > sizeof(buffer) - 1)
1121 count = sizeof(buffer) - 1;
1122 if (copy_from_user(buffer, buf, count)) {
1127 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1130 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1131 oom_adjust != OOM_DISABLE) {
1136 task = get_proc_task(file->f_path.dentry->d_inode);
1148 if (!lock_task_sighand(task, &flags)) {
1153 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1159 * Warn that /proc/pid/oom_adj is deprecated, see
1160 * Documentation/feature-removal-schedule.txt.
1162 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1163 current->comm, task_pid_nr(current), task_pid_nr(task),
1165 task->signal->oom_adj = oom_adjust;
1167 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1168 * value is always attainable.
1170 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1171 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1173 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1175 trace_oom_score_adj_update(task);
1177 unlock_task_sighand(task, &flags);
1180 put_task_struct(task);
1182 return err < 0 ? err : count;
1185 static const struct file_operations proc_oom_adjust_operations = {
1186 .read = oom_adjust_read,
1187 .write = oom_adjust_write,
1188 .llseek = generic_file_llseek,
1191 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1192 size_t count, loff_t *ppos)
1194 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1195 char buffer[PROC_NUMBUF];
1196 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1197 unsigned long flags;
1202 if (lock_task_sighand(task, &flags)) {
1203 oom_score_adj = task->signal->oom_score_adj;
1204 unlock_task_sighand(task, &flags);
1206 put_task_struct(task);
1207 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1208 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1211 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1212 size_t count, loff_t *ppos)
1214 struct task_struct *task;
1215 char buffer[PROC_NUMBUF];
1216 unsigned long flags;
1220 memset(buffer, 0, sizeof(buffer));
1221 if (count > sizeof(buffer) - 1)
1222 count = sizeof(buffer) - 1;
1223 if (copy_from_user(buffer, buf, count)) {
1228 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1231 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1232 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1237 task = get_proc_task(file->f_path.dentry->d_inode);
1249 if (!lock_task_sighand(task, &flags)) {
1254 if (oom_score_adj < task->signal->oom_score_adj_min &&
1255 !capable(CAP_SYS_RESOURCE)) {
1260 task->signal->oom_score_adj = oom_score_adj;
1261 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1262 task->signal->oom_score_adj_min = oom_score_adj;
1263 trace_oom_score_adj_update(task);
1265 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1266 * always attainable.
1268 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1269 task->signal->oom_adj = OOM_DISABLE;
1271 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1274 unlock_task_sighand(task, &flags);
1277 put_task_struct(task);
1279 return err < 0 ? err : count;
1282 static const struct file_operations proc_oom_score_adj_operations = {
1283 .read = oom_score_adj_read,
1284 .write = oom_score_adj_write,
1285 .llseek = default_llseek,
1288 #ifdef CONFIG_AUDITSYSCALL
1289 #define TMPBUFLEN 21
1290 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1291 size_t count, loff_t *ppos)
1293 struct inode * inode = file->f_path.dentry->d_inode;
1294 struct task_struct *task = get_proc_task(inode);
1296 char tmpbuf[TMPBUFLEN];
1300 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1301 audit_get_loginuid(task));
1302 put_task_struct(task);
1303 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1306 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1307 size_t count, loff_t *ppos)
1309 struct inode * inode = file->f_path.dentry->d_inode;
1314 if (!capable(CAP_AUDIT_CONTROL))
1318 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1324 if (count >= PAGE_SIZE)
1325 count = PAGE_SIZE - 1;
1328 /* No partial writes. */
1331 page = (char*)__get_free_page(GFP_TEMPORARY);
1335 if (copy_from_user(page, buf, count))
1339 loginuid = simple_strtoul(page, &tmp, 10);
1345 length = audit_set_loginuid(current, loginuid);
1346 if (likely(length == 0))
1350 free_page((unsigned long) page);
1354 static const struct file_operations proc_loginuid_operations = {
1355 .read = proc_loginuid_read,
1356 .write = proc_loginuid_write,
1357 .llseek = generic_file_llseek,
1360 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1361 size_t count, loff_t *ppos)
1363 struct inode * inode = file->f_path.dentry->d_inode;
1364 struct task_struct *task = get_proc_task(inode);
1366 char tmpbuf[TMPBUFLEN];
1370 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1371 audit_get_sessionid(task));
1372 put_task_struct(task);
1373 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1376 static const struct file_operations proc_sessionid_operations = {
1377 .read = proc_sessionid_read,
1378 .llseek = generic_file_llseek,
1382 #ifdef CONFIG_FAULT_INJECTION
1383 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1384 size_t count, loff_t *ppos)
1386 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1387 char buffer[PROC_NUMBUF];
1393 make_it_fail = task->make_it_fail;
1394 put_task_struct(task);
1396 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1398 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1401 static ssize_t proc_fault_inject_write(struct file * file,
1402 const char __user * buf, size_t count, loff_t *ppos)
1404 struct task_struct *task;
1405 char buffer[PROC_NUMBUF], *end;
1408 if (!capable(CAP_SYS_RESOURCE))
1410 memset(buffer, 0, sizeof(buffer));
1411 if (count > sizeof(buffer) - 1)
1412 count = sizeof(buffer) - 1;
1413 if (copy_from_user(buffer, buf, count))
1415 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1418 task = get_proc_task(file->f_dentry->d_inode);
1421 task->make_it_fail = make_it_fail;
1422 put_task_struct(task);
1427 static const struct file_operations proc_fault_inject_operations = {
1428 .read = proc_fault_inject_read,
1429 .write = proc_fault_inject_write,
1430 .llseek = generic_file_llseek,
1435 #ifdef CONFIG_SCHED_DEBUG
1437 * Print out various scheduling related per-task fields:
1439 static int sched_show(struct seq_file *m, void *v)
1441 struct inode *inode = m->private;
1442 struct task_struct *p;
1444 p = get_proc_task(inode);
1447 proc_sched_show_task(p, m);
1455 sched_write(struct file *file, const char __user *buf,
1456 size_t count, loff_t *offset)
1458 struct inode *inode = file->f_path.dentry->d_inode;
1459 struct task_struct *p;
1461 p = get_proc_task(inode);
1464 proc_sched_set_task(p);
1471 static int sched_open(struct inode *inode, struct file *filp)
1473 return single_open(filp, sched_show, inode);
1476 static const struct file_operations proc_pid_sched_operations = {
1479 .write = sched_write,
1480 .llseek = seq_lseek,
1481 .release = single_release,
1486 #ifdef CONFIG_SCHED_AUTOGROUP
1488 * Print out autogroup related information:
1490 static int sched_autogroup_show(struct seq_file *m, void *v)
1492 struct inode *inode = m->private;
1493 struct task_struct *p;
1495 p = get_proc_task(inode);
1498 proc_sched_autogroup_show_task(p, m);
1506 sched_autogroup_write(struct file *file, const char __user *buf,
1507 size_t count, loff_t *offset)
1509 struct inode *inode = file->f_path.dentry->d_inode;
1510 struct task_struct *p;
1511 char buffer[PROC_NUMBUF];
1515 memset(buffer, 0, sizeof(buffer));
1516 if (count > sizeof(buffer) - 1)
1517 count = sizeof(buffer) - 1;
1518 if (copy_from_user(buffer, buf, count))
1521 err = kstrtoint(strstrip(buffer), 0, &nice);
1525 p = get_proc_task(inode);
1530 err = proc_sched_autogroup_set_nice(p, &err);
1539 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1543 ret = single_open(filp, sched_autogroup_show, NULL);
1545 struct seq_file *m = filp->private_data;
1552 static const struct file_operations proc_pid_sched_autogroup_operations = {
1553 .open = sched_autogroup_open,
1555 .write = sched_autogroup_write,
1556 .llseek = seq_lseek,
1557 .release = single_release,
1560 #endif /* CONFIG_SCHED_AUTOGROUP */
1562 static ssize_t comm_write(struct file *file, const char __user *buf,
1563 size_t count, loff_t *offset)
1565 struct inode *inode = file->f_path.dentry->d_inode;
1566 struct task_struct *p;
1567 char buffer[TASK_COMM_LEN];
1569 memset(buffer, 0, sizeof(buffer));
1570 if (count > sizeof(buffer) - 1)
1571 count = sizeof(buffer) - 1;
1572 if (copy_from_user(buffer, buf, count))
1575 p = get_proc_task(inode);
1579 if (same_thread_group(current, p))
1580 set_task_comm(p, buffer);
1589 static int comm_show(struct seq_file *m, void *v)
1591 struct inode *inode = m->private;
1592 struct task_struct *p;
1594 p = get_proc_task(inode);
1599 seq_printf(m, "%s\n", p->comm);
1607 static int comm_open(struct inode *inode, struct file *filp)
1609 return single_open(filp, comm_show, inode);
1612 static const struct file_operations proc_pid_set_comm_operations = {
1615 .write = comm_write,
1616 .llseek = seq_lseek,
1617 .release = single_release,
1620 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1622 struct task_struct *task;
1623 struct mm_struct *mm;
1624 struct file *exe_file;
1626 task = get_proc_task(dentry->d_inode);
1629 mm = get_task_mm(task);
1630 put_task_struct(task);
1633 exe_file = get_mm_exe_file(mm);
1636 *exe_path = exe_file->f_path;
1637 path_get(&exe_file->f_path);
1644 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1646 struct inode *inode = dentry->d_inode;
1647 int error = -EACCES;
1649 /* We don't need a base pointer in the /proc filesystem */
1650 path_put(&nd->path);
1652 /* Are we allowed to snoop on the tasks file descriptors? */
1653 if (!proc_fd_access_allowed(inode))
1656 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1658 return ERR_PTR(error);
1661 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1663 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1670 pathname = d_path(path, tmp, PAGE_SIZE);
1671 len = PTR_ERR(pathname);
1672 if (IS_ERR(pathname))
1674 len = tmp + PAGE_SIZE - 1 - pathname;
1678 if (copy_to_user(buffer, pathname, len))
1681 free_page((unsigned long)tmp);
1685 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1687 int error = -EACCES;
1688 struct inode *inode = dentry->d_inode;
1691 /* Are we allowed to snoop on the tasks file descriptors? */
1692 if (!proc_fd_access_allowed(inode))
1695 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1699 error = do_proc_readlink(&path, buffer, buflen);
1705 static const struct inode_operations proc_pid_link_inode_operations = {
1706 .readlink = proc_pid_readlink,
1707 .follow_link = proc_pid_follow_link,
1708 .setattr = proc_setattr,
1712 /* building an inode */
1714 static int task_dumpable(struct task_struct *task)
1717 struct mm_struct *mm;
1722 dumpable = get_dumpable(mm);
1729 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1731 struct inode * inode;
1732 struct proc_inode *ei;
1733 const struct cred *cred;
1735 /* We need a new inode */
1737 inode = new_inode(sb);
1743 inode->i_ino = get_next_ino();
1744 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1745 inode->i_op = &proc_def_inode_operations;
1748 * grab the reference to task.
1750 ei->pid = get_task_pid(task, PIDTYPE_PID);
1754 if (task_dumpable(task)) {
1756 cred = __task_cred(task);
1757 inode->i_uid = cred->euid;
1758 inode->i_gid = cred->egid;
1761 security_task_to_inode(task, inode);
1771 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1773 struct inode *inode = dentry->d_inode;
1774 struct task_struct *task;
1775 const struct cred *cred;
1776 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1778 generic_fillattr(inode, stat);
1783 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1785 if (!has_pid_permissions(pid, task, 2)) {
1788 * This doesn't prevent learning whether PID exists,
1789 * it only makes getattr() consistent with readdir().
1793 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1794 task_dumpable(task)) {
1795 cred = __task_cred(task);
1796 stat->uid = cred->euid;
1797 stat->gid = cred->egid;
1807 * Exceptional case: normally we are not allowed to unhash a busy
1808 * directory. In this case, however, we can do it - no aliasing problems
1809 * due to the way we treat inodes.
1811 * Rewrite the inode's ownerships here because the owning task may have
1812 * performed a setuid(), etc.
1814 * Before the /proc/pid/status file was created the only way to read
1815 * the effective uid of a /process was to stat /proc/pid. Reading
1816 * /proc/pid/status is slow enough that procps and other packages
1817 * kept stating /proc/pid. To keep the rules in /proc simple I have
1818 * made this apply to all per process world readable and executable
1821 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1823 struct inode *inode;
1824 struct task_struct *task;
1825 const struct cred *cred;
1827 if (nd && nd->flags & LOOKUP_RCU)
1830 inode = dentry->d_inode;
1831 task = get_proc_task(inode);
1834 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1835 task_dumpable(task)) {
1837 cred = __task_cred(task);
1838 inode->i_uid = cred->euid;
1839 inode->i_gid = cred->egid;
1845 inode->i_mode &= ~(S_ISUID | S_ISGID);
1846 security_task_to_inode(task, inode);
1847 put_task_struct(task);
1854 static int pid_delete_dentry(const struct dentry * dentry)
1856 /* Is the task we represent dead?
1857 * If so, then don't put the dentry on the lru list,
1858 * kill it immediately.
1860 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1863 const struct dentry_operations pid_dentry_operations =
1865 .d_revalidate = pid_revalidate,
1866 .d_delete = pid_delete_dentry,
1872 * Fill a directory entry.
1874 * If possible create the dcache entry and derive our inode number and
1875 * file type from dcache entry.
1877 * Since all of the proc inode numbers are dynamically generated, the inode
1878 * numbers do not exist until the inode is cache. This means creating the
1879 * the dcache entry in readdir is necessary to keep the inode numbers
1880 * reported by readdir in sync with the inode numbers reported
1883 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1884 const char *name, int len,
1885 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1887 struct dentry *child, *dir = filp->f_path.dentry;
1888 struct inode *inode;
1891 unsigned type = DT_UNKNOWN;
1895 qname.hash = full_name_hash(name, len);
1897 child = d_lookup(dir, &qname);
1900 new = d_alloc(dir, &qname);
1902 child = instantiate(dir->d_inode, new, task, ptr);
1909 if (!child || IS_ERR(child) || !child->d_inode)
1910 goto end_instantiate;
1911 inode = child->d_inode;
1914 type = inode->i_mode >> 12;
1919 ino = find_inode_number(dir, &qname);
1922 return filldir(dirent, name, len, filp->f_pos, ino, type);
1925 static unsigned name_to_int(struct dentry *dentry)
1927 const char *name = dentry->d_name.name;
1928 int len = dentry->d_name.len;
1931 if (len > 1 && *name == '0')
1934 unsigned c = *name++ - '0';
1937 if (n >= (~0U-9)/10)
1947 #define PROC_FDINFO_MAX 64
1949 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1951 struct task_struct *task = get_proc_task(inode);
1952 struct files_struct *files = NULL;
1954 int fd = proc_fd(inode);
1957 files = get_files_struct(task);
1958 put_task_struct(task);
1962 * We are not taking a ref to the file structure, so we must
1965 spin_lock(&files->file_lock);
1966 file = fcheck_files(files, fd);
1968 unsigned int f_flags;
1969 struct fdtable *fdt;
1971 fdt = files_fdtable(files);
1972 f_flags = file->f_flags & ~O_CLOEXEC;
1973 if (FD_ISSET(fd, fdt->close_on_exec))
1974 f_flags |= O_CLOEXEC;
1977 *path = file->f_path;
1978 path_get(&file->f_path);
1981 snprintf(info, PROC_FDINFO_MAX,
1984 (long long) file->f_pos,
1986 spin_unlock(&files->file_lock);
1987 put_files_struct(files);
1990 spin_unlock(&files->file_lock);
1991 put_files_struct(files);
1996 static int proc_fd_link(struct dentry *dentry, struct path *path)
1998 return proc_fd_info(dentry->d_inode, path, NULL);
2001 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
2003 struct inode *inode;
2004 struct task_struct *task;
2006 struct files_struct *files;
2007 const struct cred *cred;
2009 if (nd && nd->flags & LOOKUP_RCU)
2012 inode = dentry->d_inode;
2013 task = get_proc_task(inode);
2014 fd = proc_fd(inode);
2017 files = get_files_struct(task);
2020 if (fcheck_files(files, fd)) {
2022 put_files_struct(files);
2023 if (task_dumpable(task)) {
2025 cred = __task_cred(task);
2026 inode->i_uid = cred->euid;
2027 inode->i_gid = cred->egid;
2033 inode->i_mode &= ~(S_ISUID | S_ISGID);
2034 security_task_to_inode(task, inode);
2035 put_task_struct(task);
2039 put_files_struct(files);
2041 put_task_struct(task);
2047 static const struct dentry_operations tid_fd_dentry_operations =
2049 .d_revalidate = tid_fd_revalidate,
2050 .d_delete = pid_delete_dentry,
2053 static struct dentry *proc_fd_instantiate(struct inode *dir,
2054 struct dentry *dentry, struct task_struct *task, const void *ptr)
2056 unsigned fd = *(const unsigned *)ptr;
2058 struct files_struct *files;
2059 struct inode *inode;
2060 struct proc_inode *ei;
2061 struct dentry *error = ERR_PTR(-ENOENT);
2063 inode = proc_pid_make_inode(dir->i_sb, task);
2068 files = get_files_struct(task);
2071 inode->i_mode = S_IFLNK;
2074 * We are not taking a ref to the file structure, so we must
2077 spin_lock(&files->file_lock);
2078 file = fcheck_files(files, fd);
2081 if (file->f_mode & FMODE_READ)
2082 inode->i_mode |= S_IRUSR | S_IXUSR;
2083 if (file->f_mode & FMODE_WRITE)
2084 inode->i_mode |= S_IWUSR | S_IXUSR;
2085 spin_unlock(&files->file_lock);
2086 put_files_struct(files);
2088 inode->i_op = &proc_pid_link_inode_operations;
2090 ei->op.proc_get_link = proc_fd_link;
2091 d_set_d_op(dentry, &tid_fd_dentry_operations);
2092 d_add(dentry, inode);
2093 /* Close the race of the process dying before we return the dentry */
2094 if (tid_fd_revalidate(dentry, NULL))
2100 spin_unlock(&files->file_lock);
2101 put_files_struct(files);
2107 static struct dentry *proc_lookupfd_common(struct inode *dir,
2108 struct dentry *dentry,
2109 instantiate_t instantiate)
2111 struct task_struct *task = get_proc_task(dir);
2112 unsigned fd = name_to_int(dentry);
2113 struct dentry *result = ERR_PTR(-ENOENT);
2120 result = instantiate(dir, dentry, task, &fd);
2122 put_task_struct(task);
2127 static int proc_readfd_common(struct file * filp, void * dirent,
2128 filldir_t filldir, instantiate_t instantiate)
2130 struct dentry *dentry = filp->f_path.dentry;
2131 struct inode *inode = dentry->d_inode;
2132 struct task_struct *p = get_proc_task(inode);
2133 unsigned int fd, ino;
2135 struct files_struct * files;
2145 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2149 ino = parent_ino(dentry);
2150 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2154 files = get_files_struct(p);
2158 for (fd = filp->f_pos-2;
2159 fd < files_fdtable(files)->max_fds;
2160 fd++, filp->f_pos++) {
2161 char name[PROC_NUMBUF];
2164 if (!fcheck_files(files, fd))
2168 len = snprintf(name, sizeof(name), "%d", fd);
2169 if (proc_fill_cache(filp, dirent, filldir,
2170 name, len, instantiate,
2178 put_files_struct(files);
2186 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2187 struct nameidata *nd)
2189 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2192 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2194 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2197 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2198 size_t len, loff_t *ppos)
2200 char tmp[PROC_FDINFO_MAX];
2201 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2203 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2207 static const struct file_operations proc_fdinfo_file_operations = {
2208 .open = nonseekable_open,
2209 .read = proc_fdinfo_read,
2210 .llseek = no_llseek,
2213 static const struct file_operations proc_fd_operations = {
2214 .read = generic_read_dir,
2215 .readdir = proc_readfd,
2216 .llseek = default_llseek,
2220 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2221 * which represent vma start and end addresses.
2223 static int dname_to_vma_addr(struct dentry *dentry,
2224 unsigned long *start, unsigned long *end)
2226 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2232 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2234 unsigned long vm_start, vm_end;
2235 bool exact_vma_exists = false;
2236 struct mm_struct *mm = NULL;
2237 struct task_struct *task;
2238 const struct cred *cred;
2239 struct inode *inode;
2242 if (nd && nd->flags & LOOKUP_RCU)
2245 if (!capable(CAP_SYS_ADMIN)) {
2250 inode = dentry->d_inode;
2251 task = get_proc_task(inode);
2255 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2258 mm = get_task_mm(task);
2262 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2263 down_read(&mm->mmap_sem);
2264 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2265 up_read(&mm->mmap_sem);
2270 if (exact_vma_exists) {
2271 if (task_dumpable(task)) {
2273 cred = __task_cred(task);
2274 inode->i_uid = cred->euid;
2275 inode->i_gid = cred->egid;
2281 security_task_to_inode(task, inode);
2286 put_task_struct(task);
2295 static const struct dentry_operations tid_map_files_dentry_operations = {
2296 .d_revalidate = map_files_d_revalidate,
2297 .d_delete = pid_delete_dentry,
2300 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2302 unsigned long vm_start, vm_end;
2303 struct vm_area_struct *vma;
2304 struct task_struct *task;
2305 struct mm_struct *mm;
2309 task = get_proc_task(dentry->d_inode);
2313 mm = get_task_mm(task);
2314 put_task_struct(task);
2318 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2322 down_read(&mm->mmap_sem);
2323 vma = find_exact_vma(mm, vm_start, vm_end);
2324 if (vma && vma->vm_file) {
2325 *path = vma->vm_file->f_path;
2329 up_read(&mm->mmap_sem);
2337 struct map_files_info {
2340 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2343 static struct dentry *
2344 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2345 struct task_struct *task, const void *ptr)
2347 const struct file *file = ptr;
2348 struct proc_inode *ei;
2349 struct inode *inode;
2352 return ERR_PTR(-ENOENT);
2354 inode = proc_pid_make_inode(dir->i_sb, task);
2356 return ERR_PTR(-ENOENT);
2359 ei->op.proc_get_link = proc_map_files_get_link;
2361 inode->i_op = &proc_pid_link_inode_operations;
2363 inode->i_mode = S_IFLNK;
2365 if (file->f_mode & FMODE_READ)
2366 inode->i_mode |= S_IRUSR;
2367 if (file->f_mode & FMODE_WRITE)
2368 inode->i_mode |= S_IWUSR;
2370 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2371 d_add(dentry, inode);
2376 static struct dentry *proc_map_files_lookup(struct inode *dir,
2377 struct dentry *dentry, struct nameidata *nd)
2379 unsigned long vm_start, vm_end;
2380 struct vm_area_struct *vma;
2381 struct task_struct *task;
2382 struct dentry *result;
2383 struct mm_struct *mm;
2385 result = ERR_PTR(-EACCES);
2386 if (!capable(CAP_SYS_ADMIN))
2389 result = ERR_PTR(-ENOENT);
2390 task = get_proc_task(dir);
2394 result = ERR_PTR(-EACCES);
2395 if (lock_trace(task))
2398 result = ERR_PTR(-ENOENT);
2399 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2402 mm = get_task_mm(task);
2406 down_read(&mm->mmap_sem);
2407 vma = find_exact_vma(mm, vm_start, vm_end);
2411 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2414 up_read(&mm->mmap_sem);
2419 put_task_struct(task);
2424 static const struct inode_operations proc_map_files_inode_operations = {
2425 .lookup = proc_map_files_lookup,
2426 .permission = proc_fd_permission,
2427 .setattr = proc_setattr,
2431 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2433 struct dentry *dentry = filp->f_path.dentry;
2434 struct inode *inode = dentry->d_inode;
2435 struct vm_area_struct *vma;
2436 struct task_struct *task;
2437 struct mm_struct *mm;
2442 if (!capable(CAP_SYS_ADMIN))
2446 task = get_proc_task(inode);
2451 if (lock_trace(task))
2455 switch (filp->f_pos) {
2458 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2462 ino = parent_ino(dentry);
2463 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2468 unsigned long nr_files, pos, i;
2469 struct flex_array *fa = NULL;
2470 struct map_files_info info;
2471 struct map_files_info *p;
2473 mm = get_task_mm(task);
2476 down_read(&mm->mmap_sem);
2481 * We need two passes here:
2483 * 1) Collect vmas of mapped files with mmap_sem taken
2484 * 2) Release mmap_sem and instantiate entries
2486 * otherwise we get lockdep complained, since filldir()
2487 * routine might require mmap_sem taken in might_fault().
2490 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2491 if (vma->vm_file && ++pos > filp->f_pos)
2496 fa = flex_array_alloc(sizeof(info), nr_files,
2498 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2502 flex_array_free(fa);
2503 up_read(&mm->mmap_sem);
2507 for (i = 0, vma = mm->mmap, pos = 2; vma;
2508 vma = vma->vm_next) {
2511 if (++pos <= filp->f_pos)
2514 get_file(vma->vm_file);
2515 info.file = vma->vm_file;
2516 info.len = snprintf(info.name,
2517 sizeof(info.name), "%lx-%lx",
2518 vma->vm_start, vma->vm_end);
2519 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2523 up_read(&mm->mmap_sem);
2525 for (i = 0; i < nr_files; i++) {
2526 p = flex_array_get(fa, i);
2527 ret = proc_fill_cache(filp, dirent, filldir,
2529 proc_map_files_instantiate,
2536 for (; i < nr_files; i++) {
2538 * In case of error don't forget
2539 * to put rest of file refs.
2541 p = flex_array_get(fa, i);
2545 flex_array_free(fa);
2553 put_task_struct(task);
2558 static const struct file_operations proc_map_files_operations = {
2559 .read = generic_read_dir,
2560 .readdir = proc_map_files_readdir,
2561 .llseek = default_llseek,
2565 * /proc/pid/fd needs a special permission handler so that a process can still
2566 * access /proc/self/fd after it has executed a setuid().
2568 static int proc_fd_permission(struct inode *inode, int mask)
2570 int rv = generic_permission(inode, mask);
2573 if (task_pid(current) == proc_pid(inode))
2579 * proc directories can do almost nothing..
2581 static const struct inode_operations proc_fd_inode_operations = {
2582 .lookup = proc_lookupfd,
2583 .permission = proc_fd_permission,
2584 .setattr = proc_setattr,
2587 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2588 struct dentry *dentry, struct task_struct *task, const void *ptr)
2590 unsigned fd = *(unsigned *)ptr;
2591 struct inode *inode;
2592 struct proc_inode *ei;
2593 struct dentry *error = ERR_PTR(-ENOENT);
2595 inode = proc_pid_make_inode(dir->i_sb, task);
2600 inode->i_mode = S_IFREG | S_IRUSR;
2601 inode->i_fop = &proc_fdinfo_file_operations;
2602 d_set_d_op(dentry, &tid_fd_dentry_operations);
2603 d_add(dentry, inode);
2604 /* Close the race of the process dying before we return the dentry */
2605 if (tid_fd_revalidate(dentry, NULL))
2612 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2613 struct dentry *dentry,
2614 struct nameidata *nd)
2616 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2619 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2621 return proc_readfd_common(filp, dirent, filldir,
2622 proc_fdinfo_instantiate);
2625 static const struct file_operations proc_fdinfo_operations = {
2626 .read = generic_read_dir,
2627 .readdir = proc_readfdinfo,
2628 .llseek = default_llseek,
2632 * proc directories can do almost nothing..
2634 static const struct inode_operations proc_fdinfo_inode_operations = {
2635 .lookup = proc_lookupfdinfo,
2636 .setattr = proc_setattr,
2640 static struct dentry *proc_pident_instantiate(struct inode *dir,
2641 struct dentry *dentry, struct task_struct *task, const void *ptr)
2643 const struct pid_entry *p = ptr;
2644 struct inode *inode;
2645 struct proc_inode *ei;
2646 struct dentry *error = ERR_PTR(-ENOENT);
2648 inode = proc_pid_make_inode(dir->i_sb, task);
2653 inode->i_mode = p->mode;
2654 if (S_ISDIR(inode->i_mode))
2655 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2657 inode->i_op = p->iop;
2659 inode->i_fop = p->fop;
2661 d_set_d_op(dentry, &pid_dentry_operations);
2662 d_add(dentry, inode);
2663 /* Close the race of the process dying before we return the dentry */
2664 if (pid_revalidate(dentry, NULL))
2670 static struct dentry *proc_pident_lookup(struct inode *dir,
2671 struct dentry *dentry,
2672 const struct pid_entry *ents,
2675 struct dentry *error;
2676 struct task_struct *task = get_proc_task(dir);
2677 const struct pid_entry *p, *last;
2679 error = ERR_PTR(-ENOENT);
2685 * Yes, it does not scale. And it should not. Don't add
2686 * new entries into /proc/<tgid>/ without very good reasons.
2688 last = &ents[nents - 1];
2689 for (p = ents; p <= last; p++) {
2690 if (p->len != dentry->d_name.len)
2692 if (!memcmp(dentry->d_name.name, p->name, p->len))
2698 error = proc_pident_instantiate(dir, dentry, task, p);
2700 put_task_struct(task);
2705 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2706 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2708 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2709 proc_pident_instantiate, task, p);
2712 static int proc_pident_readdir(struct file *filp,
2713 void *dirent, filldir_t filldir,
2714 const struct pid_entry *ents, unsigned int nents)
2717 struct dentry *dentry = filp->f_path.dentry;
2718 struct inode *inode = dentry->d_inode;
2719 struct task_struct *task = get_proc_task(inode);
2720 const struct pid_entry *p, *last;
2733 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2739 ino = parent_ino(dentry);
2740 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2752 last = &ents[nents - 1];
2754 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2763 put_task_struct(task);
2768 #ifdef CONFIG_SECURITY
2769 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2770 size_t count, loff_t *ppos)
2772 struct inode * inode = file->f_path.dentry->d_inode;
2775 struct task_struct *task = get_proc_task(inode);
2780 length = security_getprocattr(task,
2781 (char*)file->f_path.dentry->d_name.name,
2783 put_task_struct(task);
2785 length = simple_read_from_buffer(buf, count, ppos, p, length);
2790 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2791 size_t count, loff_t *ppos)
2793 struct inode * inode = file->f_path.dentry->d_inode;
2796 struct task_struct *task = get_proc_task(inode);
2801 if (count > PAGE_SIZE)
2804 /* No partial writes. */
2810 page = (char*)__get_free_page(GFP_TEMPORARY);
2815 if (copy_from_user(page, buf, count))
2818 /* Guard against adverse ptrace interaction */
2819 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2823 length = security_setprocattr(task,
2824 (char*)file->f_path.dentry->d_name.name,
2825 (void*)page, count);
2826 mutex_unlock(&task->signal->cred_guard_mutex);
2828 free_page((unsigned long) page);
2830 put_task_struct(task);
2835 static const struct file_operations proc_pid_attr_operations = {
2836 .read = proc_pid_attr_read,
2837 .write = proc_pid_attr_write,
2838 .llseek = generic_file_llseek,
2841 static const struct pid_entry attr_dir_stuff[] = {
2842 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2843 REG("prev", S_IRUGO, proc_pid_attr_operations),
2844 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2845 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2846 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2847 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2850 static int proc_attr_dir_readdir(struct file * filp,
2851 void * dirent, filldir_t filldir)
2853 return proc_pident_readdir(filp,dirent,filldir,
2854 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2857 static const struct file_operations proc_attr_dir_operations = {
2858 .read = generic_read_dir,
2859 .readdir = proc_attr_dir_readdir,
2860 .llseek = default_llseek,
2863 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2864 struct dentry *dentry, struct nameidata *nd)
2866 return proc_pident_lookup(dir, dentry,
2867 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2870 static const struct inode_operations proc_attr_dir_inode_operations = {
2871 .lookup = proc_attr_dir_lookup,
2872 .getattr = pid_getattr,
2873 .setattr = proc_setattr,
2878 #ifdef CONFIG_ELF_CORE
2879 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2880 size_t count, loff_t *ppos)
2882 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2883 struct mm_struct *mm;
2884 char buffer[PROC_NUMBUF];
2892 mm = get_task_mm(task);
2894 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2895 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2896 MMF_DUMP_FILTER_SHIFT));
2898 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2901 put_task_struct(task);
2906 static ssize_t proc_coredump_filter_write(struct file *file,
2907 const char __user *buf,
2911 struct task_struct *task;
2912 struct mm_struct *mm;
2913 char buffer[PROC_NUMBUF], *end;
2920 memset(buffer, 0, sizeof(buffer));
2921 if (count > sizeof(buffer) - 1)
2922 count = sizeof(buffer) - 1;
2923 if (copy_from_user(buffer, buf, count))
2927 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2930 if (end - buffer == 0)
2934 task = get_proc_task(file->f_dentry->d_inode);
2939 mm = get_task_mm(task);
2943 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2945 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2947 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2952 put_task_struct(task);
2957 static const struct file_operations proc_coredump_filter_operations = {
2958 .read = proc_coredump_filter_read,
2959 .write = proc_coredump_filter_write,
2960 .llseek = generic_file_llseek,
2967 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2970 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2971 pid_t tgid = task_tgid_nr_ns(current, ns);
2972 char tmp[PROC_NUMBUF];
2975 sprintf(tmp, "%d", tgid);
2976 return vfs_readlink(dentry,buffer,buflen,tmp);
2979 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2981 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2982 pid_t tgid = task_tgid_nr_ns(current, ns);
2983 char *name = ERR_PTR(-ENOENT);
2987 name = ERR_PTR(-ENOMEM);
2989 sprintf(name, "%d", tgid);
2991 nd_set_link(nd, name);
2995 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2998 char *s = nd_get_link(nd);
3003 static const struct inode_operations proc_self_inode_operations = {
3004 .readlink = proc_self_readlink,
3005 .follow_link = proc_self_follow_link,
3006 .put_link = proc_self_put_link,
3012 * These are the directory entries in the root directory of /proc
3013 * that properly belong to the /proc filesystem, as they describe
3014 * describe something that is process related.
3016 static const struct pid_entry proc_base_stuff[] = {
3017 NOD("self", S_IFLNK|S_IRWXUGO,
3018 &proc_self_inode_operations, NULL, {}),
3021 static struct dentry *proc_base_instantiate(struct inode *dir,
3022 struct dentry *dentry, struct task_struct *task, const void *ptr)
3024 const struct pid_entry *p = ptr;
3025 struct inode *inode;
3026 struct proc_inode *ei;
3027 struct dentry *error;
3029 /* Allocate the inode */
3030 error = ERR_PTR(-ENOMEM);
3031 inode = new_inode(dir->i_sb);
3035 /* Initialize the inode */
3037 inode->i_ino = get_next_ino();
3038 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3041 * grab the reference to the task.
3043 ei->pid = get_task_pid(task, PIDTYPE_PID);
3047 inode->i_mode = p->mode;
3048 if (S_ISDIR(inode->i_mode))
3049 set_nlink(inode, 2);
3050 if (S_ISLNK(inode->i_mode))
3053 inode->i_op = p->iop;
3055 inode->i_fop = p->fop;
3057 d_add(dentry, inode);
3066 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
3068 struct dentry *error;
3069 struct task_struct *task = get_proc_task(dir);
3070 const struct pid_entry *p, *last;
3072 error = ERR_PTR(-ENOENT);
3077 /* Lookup the directory entry */
3078 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
3079 for (p = proc_base_stuff; p <= last; p++) {
3080 if (p->len != dentry->d_name.len)
3082 if (!memcmp(dentry->d_name.name, p->name, p->len))
3088 error = proc_base_instantiate(dir, dentry, task, p);
3091 put_task_struct(task);
3096 static int proc_base_fill_cache(struct file *filp, void *dirent,
3097 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
3099 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
3100 proc_base_instantiate, task, p);
3103 #ifdef CONFIG_TASK_IO_ACCOUNTING
3104 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
3106 struct task_io_accounting acct = task->ioac;
3107 unsigned long flags;
3110 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
3114 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
3119 if (whole && lock_task_sighand(task, &flags)) {
3120 struct task_struct *t = task;
3122 task_io_accounting_add(&acct, &task->signal->ioac);
3123 while_each_thread(task, t)
3124 task_io_accounting_add(&acct, &t->ioac);
3126 unlock_task_sighand(task, &flags);
3128 result = sprintf(buffer,
3133 "read_bytes: %llu\n"
3134 "write_bytes: %llu\n"
3135 "cancelled_write_bytes: %llu\n",
3136 (unsigned long long)acct.rchar,
3137 (unsigned long long)acct.wchar,
3138 (unsigned long long)acct.syscr,
3139 (unsigned long long)acct.syscw,
3140 (unsigned long long)acct.read_bytes,
3141 (unsigned long long)acct.write_bytes,
3142 (unsigned long long)acct.cancelled_write_bytes);
3144 mutex_unlock(&task->signal->cred_guard_mutex);
3148 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3150 return do_io_accounting(task, buffer, 0);
3153 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3155 return do_io_accounting(task, buffer, 1);
3157 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3159 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3160 struct pid *pid, struct task_struct *task)
3162 int err = lock_trace(task);
3164 seq_printf(m, "%08x\n", task->personality);
3173 static const struct file_operations proc_task_operations;
3174 static const struct inode_operations proc_task_inode_operations;
3176 static const struct pid_entry tgid_base_stuff[] = {
3177 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3178 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3179 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3180 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3181 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3183 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3185 REG("environ", S_IRUSR, proc_environ_operations),
3186 INF("auxv", S_IRUSR, proc_pid_auxv),
3187 ONE("status", S_IRUGO, proc_pid_status),
3188 ONE("personality", S_IRUGO, proc_pid_personality),
3189 INF("limits", S_IRUGO, proc_pid_limits),
3190 #ifdef CONFIG_SCHED_DEBUG
3191 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3193 #ifdef CONFIG_SCHED_AUTOGROUP
3194 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3196 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3197 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3198 INF("syscall", S_IRUGO, proc_pid_syscall),
3200 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3201 ONE("stat", S_IRUGO, proc_tgid_stat),
3202 ONE("statm", S_IRUGO, proc_pid_statm),
3203 REG("maps", S_IRUGO, proc_maps_operations),
3205 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3207 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3208 LNK("cwd", proc_cwd_link),
3209 LNK("root", proc_root_link),
3210 LNK("exe", proc_exe_link),
3211 REG("mounts", S_IRUGO, proc_mounts_operations),
3212 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3213 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3214 #ifdef CONFIG_PROC_PAGE_MONITOR
3215 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3216 REG("smaps", S_IRUGO, proc_smaps_operations),
3217 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3219 #ifdef CONFIG_SECURITY
3220 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3222 #ifdef CONFIG_KALLSYMS
3223 INF("wchan", S_IRUGO, proc_pid_wchan),
3225 #ifdef CONFIG_STACKTRACE
3226 ONE("stack", S_IRUGO, proc_pid_stack),
3228 #ifdef CONFIG_SCHEDSTATS
3229 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3231 #ifdef CONFIG_LATENCYTOP
3232 REG("latency", S_IRUGO, proc_lstats_operations),
3234 #ifdef CONFIG_PROC_PID_CPUSET
3235 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3237 #ifdef CONFIG_CGROUPS
3238 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3240 INF("oom_score", S_IRUGO, proc_oom_score),
3241 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3242 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3243 #ifdef CONFIG_AUDITSYSCALL
3244 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3245 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3247 #ifdef CONFIG_FAULT_INJECTION
3248 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3250 #ifdef CONFIG_ELF_CORE
3251 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3253 #ifdef CONFIG_TASK_IO_ACCOUNTING
3254 INF("io", S_IRUSR, proc_tgid_io_accounting),
3256 #ifdef CONFIG_HARDWALL
3257 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3261 static int proc_tgid_base_readdir(struct file * filp,
3262 void * dirent, filldir_t filldir)
3264 return proc_pident_readdir(filp,dirent,filldir,
3265 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3268 static const struct file_operations proc_tgid_base_operations = {
3269 .read = generic_read_dir,
3270 .readdir = proc_tgid_base_readdir,
3271 .llseek = default_llseek,
3274 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3275 return proc_pident_lookup(dir, dentry,
3276 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3279 static const struct inode_operations proc_tgid_base_inode_operations = {
3280 .lookup = proc_tgid_base_lookup,
3281 .getattr = pid_getattr,
3282 .setattr = proc_setattr,
3283 .permission = proc_pid_permission,
3286 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3288 struct dentry *dentry, *leader, *dir;
3289 char buf[PROC_NUMBUF];
3293 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3294 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3296 shrink_dcache_parent(dentry);
3302 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3303 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3308 name.len = strlen(name.name);
3309 dir = d_hash_and_lookup(leader, &name);
3311 goto out_put_leader;
3314 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3315 dentry = d_hash_and_lookup(dir, &name);
3317 shrink_dcache_parent(dentry);
3330 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3331 * @task: task that should be flushed.
3333 * When flushing dentries from proc, one needs to flush them from global
3334 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3335 * in. This call is supposed to do all of this job.
3337 * Looks in the dcache for
3339 * /proc/@tgid/task/@pid
3340 * if either directory is present flushes it and all of it'ts children
3343 * It is safe and reasonable to cache /proc entries for a task until
3344 * that task exits. After that they just clog up the dcache with
3345 * useless entries, possibly causing useful dcache entries to be
3346 * flushed instead. This routine is proved to flush those useless
3347 * dcache entries at process exit time.
3349 * NOTE: This routine is just an optimization so it does not guarantee
3350 * that no dcache entries will exist at process exit time it
3351 * just makes it very unlikely that any will persist.
3354 void proc_flush_task(struct task_struct *task)
3357 struct pid *pid, *tgid;
3360 pid = task_pid(task);
3361 tgid = task_tgid(task);
3363 for (i = 0; i <= pid->level; i++) {
3364 upid = &pid->numbers[i];
3365 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3366 tgid->numbers[i].nr);
3369 upid = &pid->numbers[pid->level];
3371 pid_ns_release_proc(upid->ns);
3374 static struct dentry *proc_pid_instantiate(struct inode *dir,
3375 struct dentry * dentry,
3376 struct task_struct *task, const void *ptr)
3378 struct dentry *error = ERR_PTR(-ENOENT);
3379 struct inode *inode;
3381 inode = proc_pid_make_inode(dir->i_sb, task);
3385 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3386 inode->i_op = &proc_tgid_base_inode_operations;
3387 inode->i_fop = &proc_tgid_base_operations;
3388 inode->i_flags|=S_IMMUTABLE;
3390 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3391 ARRAY_SIZE(tgid_base_stuff)));
3393 d_set_d_op(dentry, &pid_dentry_operations);
3395 d_add(dentry, inode);
3396 /* Close the race of the process dying before we return the dentry */
3397 if (pid_revalidate(dentry, NULL))
3403 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3405 struct dentry *result;
3406 struct task_struct *task;
3408 struct pid_namespace *ns;
3410 result = proc_base_lookup(dir, dentry);
3411 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3414 tgid = name_to_int(dentry);
3418 ns = dentry->d_sb->s_fs_info;
3420 task = find_task_by_pid_ns(tgid, ns);
3422 get_task_struct(task);
3427 result = proc_pid_instantiate(dir, dentry, task, NULL);
3428 put_task_struct(task);
3434 * Find the first task with tgid >= tgid
3439 struct task_struct *task;
3441 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3446 put_task_struct(iter.task);
3450 pid = find_ge_pid(iter.tgid, ns);
3452 iter.tgid = pid_nr_ns(pid, ns);
3453 iter.task = pid_task(pid, PIDTYPE_PID);
3454 /* What we to know is if the pid we have find is the
3455 * pid of a thread_group_leader. Testing for task
3456 * being a thread_group_leader is the obvious thing
3457 * todo but there is a window when it fails, due to
3458 * the pid transfer logic in de_thread.
3460 * So we perform the straight forward test of seeing
3461 * if the pid we have found is the pid of a thread
3462 * group leader, and don't worry if the task we have
3463 * found doesn't happen to be a thread group leader.
3464 * As we don't care in the case of readdir.
3466 if (!iter.task || !has_group_leader_pid(iter.task)) {
3470 get_task_struct(iter.task);
3476 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3478 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3479 struct tgid_iter iter)
3481 char name[PROC_NUMBUF];
3482 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3483 return proc_fill_cache(filp, dirent, filldir, name, len,
3484 proc_pid_instantiate, iter.task, NULL);
3487 static int fake_filldir(void *buf, const char *name, int namelen,
3488 loff_t offset, u64 ino, unsigned d_type)
3493 /* for the /proc/ directory itself, after non-process stuff has been done */
3494 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3497 struct task_struct *reaper;
3498 struct tgid_iter iter;
3499 struct pid_namespace *ns;
3500 filldir_t __filldir;
3502 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3504 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3506 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3510 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3511 const struct pid_entry *p = &proc_base_stuff[nr];
3512 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3516 ns = filp->f_dentry->d_sb->s_fs_info;
3518 iter.tgid = filp->f_pos - TGID_OFFSET;
3519 for (iter = next_tgid(ns, iter);
3521 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3522 if (has_pid_permissions(ns, iter.task, 2))
3523 __filldir = filldir;
3525 __filldir = fake_filldir;
3527 filp->f_pos = iter.tgid + TGID_OFFSET;
3528 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3529 put_task_struct(iter.task);
3533 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3535 put_task_struct(reaper);
3543 static const struct pid_entry tid_base_stuff[] = {
3544 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3545 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3546 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3547 REG("environ", S_IRUSR, proc_environ_operations),
3548 INF("auxv", S_IRUSR, proc_pid_auxv),
3549 ONE("status", S_IRUGO, proc_pid_status),
3550 ONE("personality", S_IRUGO, proc_pid_personality),
3551 INF("limits", S_IRUGO, proc_pid_limits),
3552 #ifdef CONFIG_SCHED_DEBUG
3553 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3555 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3556 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3557 INF("syscall", S_IRUGO, proc_pid_syscall),
3559 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3560 ONE("stat", S_IRUGO, proc_tid_stat),
3561 ONE("statm", S_IRUGO, proc_pid_statm),
3562 REG("maps", S_IRUGO, proc_maps_operations),
3564 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3566 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3567 LNK("cwd", proc_cwd_link),
3568 LNK("root", proc_root_link),
3569 LNK("exe", proc_exe_link),
3570 REG("mounts", S_IRUGO, proc_mounts_operations),
3571 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3572 #ifdef CONFIG_PROC_PAGE_MONITOR
3573 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3574 REG("smaps", S_IRUGO, proc_smaps_operations),
3575 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3577 #ifdef CONFIG_SECURITY
3578 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3580 #ifdef CONFIG_KALLSYMS
3581 INF("wchan", S_IRUGO, proc_pid_wchan),
3583 #ifdef CONFIG_STACKTRACE
3584 ONE("stack", S_IRUGO, proc_pid_stack),
3586 #ifdef CONFIG_SCHEDSTATS
3587 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3589 #ifdef CONFIG_LATENCYTOP
3590 REG("latency", S_IRUGO, proc_lstats_operations),
3592 #ifdef CONFIG_PROC_PID_CPUSET
3593 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3595 #ifdef CONFIG_CGROUPS
3596 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3598 INF("oom_score", S_IRUGO, proc_oom_score),
3599 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3600 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3601 #ifdef CONFIG_AUDITSYSCALL
3602 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3603 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3605 #ifdef CONFIG_FAULT_INJECTION
3606 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3608 #ifdef CONFIG_TASK_IO_ACCOUNTING
3609 INF("io", S_IRUSR, proc_tid_io_accounting),
3611 #ifdef CONFIG_HARDWALL
3612 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3616 static int proc_tid_base_readdir(struct file * filp,
3617 void * dirent, filldir_t filldir)
3619 return proc_pident_readdir(filp,dirent,filldir,
3620 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3623 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3624 return proc_pident_lookup(dir, dentry,
3625 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3628 static const struct file_operations proc_tid_base_operations = {
3629 .read = generic_read_dir,
3630 .readdir = proc_tid_base_readdir,
3631 .llseek = default_llseek,
3634 static const struct inode_operations proc_tid_base_inode_operations = {
3635 .lookup = proc_tid_base_lookup,
3636 .getattr = pid_getattr,
3637 .setattr = proc_setattr,
3640 static struct dentry *proc_task_instantiate(struct inode *dir,
3641 struct dentry *dentry, struct task_struct *task, const void *ptr)
3643 struct dentry *error = ERR_PTR(-ENOENT);
3644 struct inode *inode;
3645 inode = proc_pid_make_inode(dir->i_sb, task);
3649 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3650 inode->i_op = &proc_tid_base_inode_operations;
3651 inode->i_fop = &proc_tid_base_operations;
3652 inode->i_flags|=S_IMMUTABLE;
3654 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3655 ARRAY_SIZE(tid_base_stuff)));
3657 d_set_d_op(dentry, &pid_dentry_operations);
3659 d_add(dentry, inode);
3660 /* Close the race of the process dying before we return the dentry */
3661 if (pid_revalidate(dentry, NULL))
3667 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3669 struct dentry *result = ERR_PTR(-ENOENT);
3670 struct task_struct *task;
3671 struct task_struct *leader = get_proc_task(dir);
3673 struct pid_namespace *ns;
3678 tid = name_to_int(dentry);
3682 ns = dentry->d_sb->s_fs_info;
3684 task = find_task_by_pid_ns(tid, ns);
3686 get_task_struct(task);
3690 if (!same_thread_group(leader, task))
3693 result = proc_task_instantiate(dir, dentry, task, NULL);
3695 put_task_struct(task);
3697 put_task_struct(leader);
3703 * Find the first tid of a thread group to return to user space.
3705 * Usually this is just the thread group leader, but if the users
3706 * buffer was too small or there was a seek into the middle of the
3707 * directory we have more work todo.
3709 * In the case of a short read we start with find_task_by_pid.
3711 * In the case of a seek we start with the leader and walk nr
3714 static struct task_struct *first_tid(struct task_struct *leader,
3715 int tid, int nr, struct pid_namespace *ns)
3717 struct task_struct *pos;
3720 /* Attempt to start with the pid of a thread */
3721 if (tid && (nr > 0)) {
3722 pos = find_task_by_pid_ns(tid, ns);
3723 if (pos && (pos->group_leader == leader))
3727 /* If nr exceeds the number of threads there is nothing todo */
3729 if (nr && nr >= get_nr_threads(leader))
3732 /* If we haven't found our starting place yet start
3733 * with the leader and walk nr threads forward.
3735 for (pos = leader; nr > 0; --nr) {
3736 pos = next_thread(pos);
3737 if (pos == leader) {
3743 get_task_struct(pos);
3750 * Find the next thread in the thread list.
3751 * Return NULL if there is an error or no next thread.
3753 * The reference to the input task_struct is released.
3755 static struct task_struct *next_tid(struct task_struct *start)
3757 struct task_struct *pos = NULL;
3759 if (pid_alive(start)) {
3760 pos = next_thread(start);
3761 if (thread_group_leader(pos))
3764 get_task_struct(pos);
3767 put_task_struct(start);
3771 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3772 struct task_struct *task, int tid)
3774 char name[PROC_NUMBUF];
3775 int len = snprintf(name, sizeof(name), "%d", tid);
3776 return proc_fill_cache(filp, dirent, filldir, name, len,
3777 proc_task_instantiate, task, NULL);
3780 /* for the /proc/TGID/task/ directories */
3781 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3783 struct dentry *dentry = filp->f_path.dentry;
3784 struct inode *inode = dentry->d_inode;
3785 struct task_struct *leader = NULL;
3786 struct task_struct *task;
3787 int retval = -ENOENT;
3790 struct pid_namespace *ns;
3792 task = get_proc_task(inode);
3796 if (pid_alive(task)) {
3797 leader = task->group_leader;
3798 get_task_struct(leader);
3801 put_task_struct(task);
3806 switch ((unsigned long)filp->f_pos) {
3809 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3814 ino = parent_ino(dentry);
3815 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3821 /* f_version caches the tgid value that the last readdir call couldn't
3822 * return. lseek aka telldir automagically resets f_version to 0.
3824 ns = filp->f_dentry->d_sb->s_fs_info;
3825 tid = (int)filp->f_version;
3826 filp->f_version = 0;
3827 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3829 task = next_tid(task), filp->f_pos++) {
3830 tid = task_pid_nr_ns(task, ns);
3831 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3832 /* returning this tgid failed, save it as the first
3833 * pid for the next readir call */
3834 filp->f_version = (u64)tid;
3835 put_task_struct(task);
3840 put_task_struct(leader);
3845 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3847 struct inode *inode = dentry->d_inode;
3848 struct task_struct *p = get_proc_task(inode);
3849 generic_fillattr(inode, stat);
3852 stat->nlink += get_nr_threads(p);
3859 static const struct inode_operations proc_task_inode_operations = {
3860 .lookup = proc_task_lookup,
3861 .getattr = proc_task_getattr,
3862 .setattr = proc_setattr,
3863 .permission = proc_pid_permission,
3866 static const struct file_operations proc_task_operations = {
3867 .read = generic_read_dir,
3868 .readdir = proc_task_readdir,
3869 .llseek = default_llseek,