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/user_namespace.h>
85 #include <linux/fs_struct.h>
86 #include <linux/slab.h>
87 #include <linux/flex_array.h>
88 #ifdef CONFIG_HARDWALL
89 #include <asm/hardwall.h>
91 #include <trace/events/oom.h>
95 * Implementing inode permission operations in /proc is almost
96 * certainly an error. Permission checks need to happen during
97 * each system call not at open time. The reason is that most of
98 * what we wish to check for permissions in /proc varies at runtime.
100 * The classic example of a problem is opening file descriptors
101 * in /proc for a task before it execs a suid executable.
108 const struct inode_operations *iop;
109 const struct file_operations *fop;
113 #define NOD(NAME, MODE, IOP, FOP, OP) { \
115 .len = sizeof(NAME) - 1, \
122 #define DIR(NAME, MODE, iops, fops) \
123 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
124 #define LNK(NAME, get_link) \
125 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
126 &proc_pid_link_inode_operations, NULL, \
127 { .proc_get_link = get_link } )
128 #define REG(NAME, MODE, fops) \
129 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
130 #define INF(NAME, MODE, read) \
131 NOD(NAME, (S_IFREG|(MODE)), \
132 NULL, &proc_info_file_operations, \
133 { .proc_read = read } )
134 #define ONE(NAME, MODE, show) \
135 NOD(NAME, (S_IFREG|(MODE)), \
136 NULL, &proc_single_file_operations, \
137 { .proc_show = show } )
139 static int proc_fd_permission(struct inode *inode, int mask);
142 * Count the number of hardlinks for the pid_entry table, excluding the .
145 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
160 static int get_task_root(struct task_struct *task, struct path *root)
162 int result = -ENOENT;
166 get_fs_root(task->fs, root);
173 static int proc_cwd_link(struct dentry *dentry, struct path *path)
175 struct task_struct *task = get_proc_task(dentry->d_inode);
176 int result = -ENOENT;
181 get_fs_pwd(task->fs, path);
185 put_task_struct(task);
190 static int proc_root_link(struct dentry *dentry, struct path *path)
192 struct task_struct *task = get_proc_task(dentry->d_inode);
193 int result = -ENOENT;
196 result = get_task_root(task, path);
197 put_task_struct(task);
202 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
206 struct mm_struct *mm = get_task_mm(task);
210 goto out_mm; /* Shh! No looking before we're done */
212 len = mm->arg_end - mm->arg_start;
217 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
219 // If the nul at the end of args has been overwritten, then
220 // assume application is using setproctitle(3).
221 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
222 len = strnlen(buffer, res);
226 len = mm->env_end - mm->env_start;
227 if (len > PAGE_SIZE - res)
228 len = PAGE_SIZE - res;
229 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
230 res = strnlen(buffer, res);
239 static int proc_pid_auxv(struct task_struct *task, char *buffer)
241 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
242 int res = PTR_ERR(mm);
243 if (mm && !IS_ERR(mm)) {
244 unsigned int nwords = 0;
247 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
248 res = nwords * sizeof(mm->saved_auxv[0]);
251 memcpy(buffer, mm->saved_auxv, res);
258 #ifdef CONFIG_KALLSYMS
260 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
261 * Returns the resolved symbol. If that fails, simply return the address.
263 static int proc_pid_wchan(struct task_struct *task, char *buffer)
266 char symname[KSYM_NAME_LEN];
268 wchan = get_wchan(task);
270 if (lookup_symbol_name(wchan, symname) < 0)
271 if (!ptrace_may_access(task, PTRACE_MODE_READ))
274 return sprintf(buffer, "%lu", wchan);
276 return sprintf(buffer, "%s", symname);
278 #endif /* CONFIG_KALLSYMS */
280 static int lock_trace(struct task_struct *task)
282 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
285 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
286 mutex_unlock(&task->signal->cred_guard_mutex);
292 static void unlock_trace(struct task_struct *task)
294 mutex_unlock(&task->signal->cred_guard_mutex);
297 #ifdef CONFIG_STACKTRACE
299 #define MAX_STACK_TRACE_DEPTH 64
301 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
302 struct pid *pid, struct task_struct *task)
304 struct stack_trace trace;
305 unsigned long *entries;
309 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
313 trace.nr_entries = 0;
314 trace.max_entries = MAX_STACK_TRACE_DEPTH;
315 trace.entries = entries;
318 err = lock_trace(task);
320 save_stack_trace_tsk(task, &trace);
322 for (i = 0; i < trace.nr_entries; i++) {
323 seq_printf(m, "[<%pK>] %pS\n",
324 (void *)entries[i], (void *)entries[i]);
334 #ifdef CONFIG_SCHEDSTATS
336 * Provides /proc/PID/schedstat
338 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
340 return sprintf(buffer, "%llu %llu %lu\n",
341 (unsigned long long)task->se.sum_exec_runtime,
342 (unsigned long long)task->sched_info.run_delay,
343 task->sched_info.pcount);
347 #ifdef CONFIG_LATENCYTOP
348 static int lstats_show_proc(struct seq_file *m, void *v)
351 struct inode *inode = m->private;
352 struct task_struct *task = get_proc_task(inode);
356 seq_puts(m, "Latency Top version : v0.1\n");
357 for (i = 0; i < 32; i++) {
358 struct latency_record *lr = &task->latency_record[i];
359 if (lr->backtrace[0]) {
361 seq_printf(m, "%i %li %li",
362 lr->count, lr->time, lr->max);
363 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
364 unsigned long bt = lr->backtrace[q];
369 seq_printf(m, " %ps", (void *)bt);
375 put_task_struct(task);
379 static int lstats_open(struct inode *inode, struct file *file)
381 return single_open(file, lstats_show_proc, inode);
384 static ssize_t lstats_write(struct file *file, const char __user *buf,
385 size_t count, loff_t *offs)
387 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
391 clear_all_latency_tracing(task);
392 put_task_struct(task);
397 static const struct file_operations proc_lstats_operations = {
400 .write = lstats_write,
402 .release = single_release,
407 static int proc_oom_score(struct task_struct *task, char *buffer)
409 unsigned long totalpages = totalram_pages + total_swap_pages;
410 unsigned long points = 0;
412 read_lock(&tasklist_lock);
414 points = oom_badness(task, NULL, NULL, totalpages) *
416 read_unlock(&tasklist_lock);
417 return sprintf(buffer, "%lu\n", points);
425 static const struct limit_names lnames[RLIM_NLIMITS] = {
426 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
427 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
428 [RLIMIT_DATA] = {"Max data size", "bytes"},
429 [RLIMIT_STACK] = {"Max stack size", "bytes"},
430 [RLIMIT_CORE] = {"Max core file size", "bytes"},
431 [RLIMIT_RSS] = {"Max resident set", "bytes"},
432 [RLIMIT_NPROC] = {"Max processes", "processes"},
433 [RLIMIT_NOFILE] = {"Max open files", "files"},
434 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
435 [RLIMIT_AS] = {"Max address space", "bytes"},
436 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
437 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
438 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
439 [RLIMIT_NICE] = {"Max nice priority", NULL},
440 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
441 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
444 /* Display limits for a process */
445 static int proc_pid_limits(struct task_struct *task, char *buffer)
450 char *bufptr = buffer;
452 struct rlimit rlim[RLIM_NLIMITS];
454 if (!lock_task_sighand(task, &flags))
456 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
457 unlock_task_sighand(task, &flags);
460 * print the file header
462 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
463 "Limit", "Soft Limit", "Hard Limit", "Units");
465 for (i = 0; i < RLIM_NLIMITS; i++) {
466 if (rlim[i].rlim_cur == RLIM_INFINITY)
467 count += sprintf(&bufptr[count], "%-25s %-20s ",
468 lnames[i].name, "unlimited");
470 count += sprintf(&bufptr[count], "%-25s %-20lu ",
471 lnames[i].name, rlim[i].rlim_cur);
473 if (rlim[i].rlim_max == RLIM_INFINITY)
474 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
476 count += sprintf(&bufptr[count], "%-20lu ",
480 count += sprintf(&bufptr[count], "%-10s\n",
483 count += sprintf(&bufptr[count], "\n");
489 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
490 static int proc_pid_syscall(struct task_struct *task, char *buffer)
493 unsigned long args[6], sp, pc;
494 int res = lock_trace(task);
498 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
499 res = sprintf(buffer, "running\n");
501 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
503 res = sprintf(buffer,
504 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
506 args[0], args[1], args[2], args[3], args[4], args[5],
511 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
513 /************************************************************************/
514 /* Here the fs part begins */
515 /************************************************************************/
517 /* permission checks */
518 static int proc_fd_access_allowed(struct inode *inode)
520 struct task_struct *task;
522 /* Allow access to a task's file descriptors if it is us or we
523 * may use ptrace attach to the process and find out that
526 task = get_proc_task(inode);
528 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
529 put_task_struct(task);
534 int proc_setattr(struct dentry *dentry, struct iattr *attr)
537 struct inode *inode = dentry->d_inode;
539 if (attr->ia_valid & ATTR_MODE)
542 error = inode_change_ok(inode, attr);
546 if ((attr->ia_valid & ATTR_SIZE) &&
547 attr->ia_size != i_size_read(inode)) {
548 error = vmtruncate(inode, attr->ia_size);
553 setattr_copy(inode, attr);
554 mark_inode_dirty(inode);
559 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
560 * or euid/egid (for hide_pid_min=2)?
562 static bool has_pid_permissions(struct pid_namespace *pid,
563 struct task_struct *task,
566 if (pid->hide_pid < hide_pid_min)
568 if (in_group_p(pid->pid_gid))
570 return ptrace_may_access(task, PTRACE_MODE_READ);
574 static int proc_pid_permission(struct inode *inode, int mask)
576 struct pid_namespace *pid = inode->i_sb->s_fs_info;
577 struct task_struct *task;
580 task = get_proc_task(inode);
583 has_perms = has_pid_permissions(pid, task, 1);
584 put_task_struct(task);
587 if (pid->hide_pid == 2) {
589 * Let's make getdents(), stat(), and open()
590 * consistent with each other. If a process
591 * may not stat() a file, it shouldn't be seen
599 return generic_permission(inode, mask);
604 static const struct inode_operations proc_def_inode_operations = {
605 .setattr = proc_setattr,
608 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
610 static ssize_t proc_info_read(struct file * file, char __user * buf,
611 size_t count, loff_t *ppos)
613 struct inode * inode = file->f_path.dentry->d_inode;
616 struct task_struct *task = get_proc_task(inode);
622 if (count > PROC_BLOCK_SIZE)
623 count = PROC_BLOCK_SIZE;
626 if (!(page = __get_free_page(GFP_TEMPORARY)))
629 length = PROC_I(inode)->op.proc_read(task, (char*)page);
632 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
635 put_task_struct(task);
640 static const struct file_operations proc_info_file_operations = {
641 .read = proc_info_read,
642 .llseek = generic_file_llseek,
645 static int proc_single_show(struct seq_file *m, void *v)
647 struct inode *inode = m->private;
648 struct pid_namespace *ns;
650 struct task_struct *task;
653 ns = inode->i_sb->s_fs_info;
654 pid = proc_pid(inode);
655 task = get_pid_task(pid, PIDTYPE_PID);
659 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
661 put_task_struct(task);
665 static int proc_single_open(struct inode *inode, struct file *filp)
667 return single_open(filp, proc_single_show, inode);
670 static const struct file_operations proc_single_file_operations = {
671 .open = proc_single_open,
674 .release = single_release,
677 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
679 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
680 struct mm_struct *mm;
685 mm = mm_access(task, mode);
686 put_task_struct(task);
692 /* ensure this mm_struct can't be freed */
693 atomic_inc(&mm->mm_count);
694 /* but do not pin its memory */
698 file->private_data = mm;
703 static int mem_open(struct inode *inode, struct file *file)
705 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
707 /* OK to pass negative loff_t, we can catch out-of-range */
708 file->f_mode |= FMODE_UNSIGNED_OFFSET;
713 static ssize_t mem_rw(struct file *file, char __user *buf,
714 size_t count, loff_t *ppos, int write)
716 struct mm_struct *mm = file->private_data;
717 unsigned long addr = *ppos;
724 page = (char *)__get_free_page(GFP_TEMPORARY);
729 if (!atomic_inc_not_zero(&mm->mm_users))
733 int this_len = min_t(int, count, PAGE_SIZE);
735 if (write && copy_from_user(page, buf, this_len)) {
740 this_len = access_remote_vm(mm, addr, page, this_len, write);
747 if (!write && copy_to_user(buf, page, this_len)) {
761 free_page((unsigned long) page);
765 static ssize_t mem_read(struct file *file, char __user *buf,
766 size_t count, loff_t *ppos)
768 return mem_rw(file, buf, count, ppos, 0);
771 static ssize_t mem_write(struct file *file, const char __user *buf,
772 size_t count, loff_t *ppos)
774 return mem_rw(file, (char __user*)buf, count, ppos, 1);
777 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
781 file->f_pos = offset;
784 file->f_pos += offset;
789 force_successful_syscall_return();
793 static int mem_release(struct inode *inode, struct file *file)
795 struct mm_struct *mm = file->private_data;
801 static const struct file_operations proc_mem_operations = {
806 .release = mem_release,
809 static int environ_open(struct inode *inode, struct file *file)
811 return __mem_open(inode, file, PTRACE_MODE_READ);
814 static ssize_t environ_read(struct file *file, char __user *buf,
815 size_t count, loff_t *ppos)
818 unsigned long src = *ppos;
820 struct mm_struct *mm = file->private_data;
825 page = (char *)__get_free_page(GFP_TEMPORARY);
830 if (!atomic_inc_not_zero(&mm->mm_users))
833 size_t this_len, max_len;
836 if (src >= (mm->env_end - mm->env_start))
839 this_len = mm->env_end - (mm->env_start + src);
841 max_len = min_t(size_t, PAGE_SIZE, count);
842 this_len = min(max_len, this_len);
844 retval = access_remote_vm(mm, (mm->env_start + src),
852 if (copy_to_user(buf, page, retval)) {
866 free_page((unsigned long) page);
870 static const struct file_operations proc_environ_operations = {
871 .open = environ_open,
872 .read = environ_read,
873 .llseek = generic_file_llseek,
874 .release = mem_release,
877 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
878 size_t count, loff_t *ppos)
880 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
881 char buffer[PROC_NUMBUF];
882 int oom_score_adj = OOM_SCORE_ADJ_MIN;
888 if (lock_task_sighand(task, &flags)) {
889 oom_score_adj = task->signal->oom_score_adj;
890 unlock_task_sighand(task, &flags);
892 put_task_struct(task);
893 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
894 return simple_read_from_buffer(buf, count, ppos, buffer, len);
897 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
898 size_t count, loff_t *ppos)
900 struct task_struct *task;
901 char buffer[PROC_NUMBUF];
906 memset(buffer, 0, sizeof(buffer));
907 if (count > sizeof(buffer) - 1)
908 count = sizeof(buffer) - 1;
909 if (copy_from_user(buffer, buf, count)) {
914 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
917 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
918 oom_score_adj > OOM_SCORE_ADJ_MAX) {
923 task = get_proc_task(file->f_path.dentry->d_inode);
935 if (!lock_task_sighand(task, &flags)) {
940 if (oom_score_adj < task->signal->oom_score_adj_min &&
941 !capable(CAP_SYS_RESOURCE)) {
946 task->signal->oom_score_adj = oom_score_adj;
947 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
948 task->signal->oom_score_adj_min = oom_score_adj;
949 trace_oom_score_adj_update(task);
952 unlock_task_sighand(task, &flags);
955 put_task_struct(task);
957 return err < 0 ? err : count;
960 static const struct file_operations proc_oom_score_adj_operations = {
961 .read = oom_score_adj_read,
962 .write = oom_score_adj_write,
963 .llseek = default_llseek,
966 #ifdef CONFIG_AUDITSYSCALL
968 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
969 size_t count, loff_t *ppos)
971 struct inode * inode = file->f_path.dentry->d_inode;
972 struct task_struct *task = get_proc_task(inode);
974 char tmpbuf[TMPBUFLEN];
978 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
979 audit_get_loginuid(task));
980 put_task_struct(task);
981 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
984 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
985 size_t count, loff_t *ppos)
987 struct inode * inode = file->f_path.dentry->d_inode;
993 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
999 if (count >= PAGE_SIZE)
1000 count = PAGE_SIZE - 1;
1003 /* No partial writes. */
1006 page = (char*)__get_free_page(GFP_TEMPORARY);
1010 if (copy_from_user(page, buf, count))
1014 loginuid = simple_strtoul(page, &tmp, 10);
1020 length = audit_set_loginuid(loginuid);
1021 if (likely(length == 0))
1025 free_page((unsigned long) page);
1029 static const struct file_operations proc_loginuid_operations = {
1030 .read = proc_loginuid_read,
1031 .write = proc_loginuid_write,
1032 .llseek = generic_file_llseek,
1035 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1036 size_t count, loff_t *ppos)
1038 struct inode * inode = file->f_path.dentry->d_inode;
1039 struct task_struct *task = get_proc_task(inode);
1041 char tmpbuf[TMPBUFLEN];
1045 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1046 audit_get_sessionid(task));
1047 put_task_struct(task);
1048 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1051 static const struct file_operations proc_sessionid_operations = {
1052 .read = proc_sessionid_read,
1053 .llseek = generic_file_llseek,
1057 #ifdef CONFIG_FAULT_INJECTION
1058 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1059 size_t count, loff_t *ppos)
1061 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1062 char buffer[PROC_NUMBUF];
1068 make_it_fail = task->make_it_fail;
1069 put_task_struct(task);
1071 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1073 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1076 static ssize_t proc_fault_inject_write(struct file * file,
1077 const char __user * buf, size_t count, loff_t *ppos)
1079 struct task_struct *task;
1080 char buffer[PROC_NUMBUF], *end;
1083 if (!capable(CAP_SYS_RESOURCE))
1085 memset(buffer, 0, sizeof(buffer));
1086 if (count > sizeof(buffer) - 1)
1087 count = sizeof(buffer) - 1;
1088 if (copy_from_user(buffer, buf, count))
1090 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1093 task = get_proc_task(file->f_dentry->d_inode);
1096 task->make_it_fail = make_it_fail;
1097 put_task_struct(task);
1102 static const struct file_operations proc_fault_inject_operations = {
1103 .read = proc_fault_inject_read,
1104 .write = proc_fault_inject_write,
1105 .llseek = generic_file_llseek,
1110 #ifdef CONFIG_SCHED_DEBUG
1112 * Print out various scheduling related per-task fields:
1114 static int sched_show(struct seq_file *m, void *v)
1116 struct inode *inode = m->private;
1117 struct task_struct *p;
1119 p = get_proc_task(inode);
1122 proc_sched_show_task(p, m);
1130 sched_write(struct file *file, const char __user *buf,
1131 size_t count, loff_t *offset)
1133 struct inode *inode = file->f_path.dentry->d_inode;
1134 struct task_struct *p;
1136 p = get_proc_task(inode);
1139 proc_sched_set_task(p);
1146 static int sched_open(struct inode *inode, struct file *filp)
1148 return single_open(filp, sched_show, inode);
1151 static const struct file_operations proc_pid_sched_operations = {
1154 .write = sched_write,
1155 .llseek = seq_lseek,
1156 .release = single_release,
1161 #ifdef CONFIG_SCHED_AUTOGROUP
1163 * Print out autogroup related information:
1165 static int sched_autogroup_show(struct seq_file *m, void *v)
1167 struct inode *inode = m->private;
1168 struct task_struct *p;
1170 p = get_proc_task(inode);
1173 proc_sched_autogroup_show_task(p, m);
1181 sched_autogroup_write(struct file *file, const char __user *buf,
1182 size_t count, loff_t *offset)
1184 struct inode *inode = file->f_path.dentry->d_inode;
1185 struct task_struct *p;
1186 char buffer[PROC_NUMBUF];
1190 memset(buffer, 0, sizeof(buffer));
1191 if (count > sizeof(buffer) - 1)
1192 count = sizeof(buffer) - 1;
1193 if (copy_from_user(buffer, buf, count))
1196 err = kstrtoint(strstrip(buffer), 0, &nice);
1200 p = get_proc_task(inode);
1204 err = proc_sched_autogroup_set_nice(p, nice);
1213 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1217 ret = single_open(filp, sched_autogroup_show, NULL);
1219 struct seq_file *m = filp->private_data;
1226 static const struct file_operations proc_pid_sched_autogroup_operations = {
1227 .open = sched_autogroup_open,
1229 .write = sched_autogroup_write,
1230 .llseek = seq_lseek,
1231 .release = single_release,
1234 #endif /* CONFIG_SCHED_AUTOGROUP */
1236 static ssize_t comm_write(struct file *file, const char __user *buf,
1237 size_t count, loff_t *offset)
1239 struct inode *inode = file->f_path.dentry->d_inode;
1240 struct task_struct *p;
1241 char buffer[TASK_COMM_LEN];
1243 memset(buffer, 0, sizeof(buffer));
1244 if (count > sizeof(buffer) - 1)
1245 count = sizeof(buffer) - 1;
1246 if (copy_from_user(buffer, buf, count))
1249 p = get_proc_task(inode);
1253 if (same_thread_group(current, p))
1254 set_task_comm(p, buffer);
1263 static int comm_show(struct seq_file *m, void *v)
1265 struct inode *inode = m->private;
1266 struct task_struct *p;
1268 p = get_proc_task(inode);
1273 seq_printf(m, "%s\n", p->comm);
1281 static int comm_open(struct inode *inode, struct file *filp)
1283 return single_open(filp, comm_show, inode);
1286 static const struct file_operations proc_pid_set_comm_operations = {
1289 .write = comm_write,
1290 .llseek = seq_lseek,
1291 .release = single_release,
1294 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1296 struct task_struct *task;
1297 struct mm_struct *mm;
1298 struct file *exe_file;
1300 task = get_proc_task(dentry->d_inode);
1303 mm = get_task_mm(task);
1304 put_task_struct(task);
1307 exe_file = get_mm_exe_file(mm);
1310 *exe_path = exe_file->f_path;
1311 path_get(&exe_file->f_path);
1318 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1320 struct inode *inode = dentry->d_inode;
1322 int error = -EACCES;
1324 /* Are we allowed to snoop on the tasks file descriptors? */
1325 if (!proc_fd_access_allowed(inode))
1328 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1332 nd_jump_link(nd, &path);
1335 return ERR_PTR(error);
1338 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1340 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1347 pathname = d_path(path, tmp, PAGE_SIZE);
1348 len = PTR_ERR(pathname);
1349 if (IS_ERR(pathname))
1351 len = tmp + PAGE_SIZE - 1 - pathname;
1355 if (copy_to_user(buffer, pathname, len))
1358 free_page((unsigned long)tmp);
1362 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1364 int error = -EACCES;
1365 struct inode *inode = dentry->d_inode;
1368 /* Are we allowed to snoop on the tasks file descriptors? */
1369 if (!proc_fd_access_allowed(inode))
1372 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1376 error = do_proc_readlink(&path, buffer, buflen);
1382 static const struct inode_operations proc_pid_link_inode_operations = {
1383 .readlink = proc_pid_readlink,
1384 .follow_link = proc_pid_follow_link,
1385 .setattr = proc_setattr,
1389 /* building an inode */
1391 static int task_dumpable(struct task_struct *task)
1394 struct mm_struct *mm;
1399 dumpable = get_dumpable(mm);
1406 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1408 struct inode * inode;
1409 struct proc_inode *ei;
1410 const struct cred *cred;
1412 /* We need a new inode */
1414 inode = new_inode(sb);
1420 inode->i_ino = get_next_ino();
1421 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1422 inode->i_op = &proc_def_inode_operations;
1425 * grab the reference to task.
1427 ei->pid = get_task_pid(task, PIDTYPE_PID);
1431 if (task_dumpable(task)) {
1433 cred = __task_cred(task);
1434 inode->i_uid = cred->euid;
1435 inode->i_gid = cred->egid;
1438 security_task_to_inode(task, inode);
1448 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1450 struct inode *inode = dentry->d_inode;
1451 struct task_struct *task;
1452 const struct cred *cred;
1453 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1455 generic_fillattr(inode, stat);
1458 stat->uid = GLOBAL_ROOT_UID;
1459 stat->gid = GLOBAL_ROOT_GID;
1460 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1462 if (!has_pid_permissions(pid, task, 2)) {
1465 * This doesn't prevent learning whether PID exists,
1466 * it only makes getattr() consistent with readdir().
1470 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1471 task_dumpable(task)) {
1472 cred = __task_cred(task);
1473 stat->uid = cred->euid;
1474 stat->gid = cred->egid;
1484 * Exceptional case: normally we are not allowed to unhash a busy
1485 * directory. In this case, however, we can do it - no aliasing problems
1486 * due to the way we treat inodes.
1488 * Rewrite the inode's ownerships here because the owning task may have
1489 * performed a setuid(), etc.
1491 * Before the /proc/pid/status file was created the only way to read
1492 * the effective uid of a /process was to stat /proc/pid. Reading
1493 * /proc/pid/status is slow enough that procps and other packages
1494 * kept stating /proc/pid. To keep the rules in /proc simple I have
1495 * made this apply to all per process world readable and executable
1498 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1500 struct inode *inode;
1501 struct task_struct *task;
1502 const struct cred *cred;
1504 if (flags & LOOKUP_RCU)
1507 inode = dentry->d_inode;
1508 task = get_proc_task(inode);
1511 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1512 task_dumpable(task)) {
1514 cred = __task_cred(task);
1515 inode->i_uid = cred->euid;
1516 inode->i_gid = cred->egid;
1519 inode->i_uid = GLOBAL_ROOT_UID;
1520 inode->i_gid = GLOBAL_ROOT_GID;
1522 inode->i_mode &= ~(S_ISUID | S_ISGID);
1523 security_task_to_inode(task, inode);
1524 put_task_struct(task);
1531 static int pid_delete_dentry(const struct dentry * dentry)
1533 /* Is the task we represent dead?
1534 * If so, then don't put the dentry on the lru list,
1535 * kill it immediately.
1537 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1540 const struct dentry_operations pid_dentry_operations =
1542 .d_revalidate = pid_revalidate,
1543 .d_delete = pid_delete_dentry,
1549 * Fill a directory entry.
1551 * If possible create the dcache entry and derive our inode number and
1552 * file type from dcache entry.
1554 * Since all of the proc inode numbers are dynamically generated, the inode
1555 * numbers do not exist until the inode is cache. This means creating the
1556 * the dcache entry in readdir is necessary to keep the inode numbers
1557 * reported by readdir in sync with the inode numbers reported
1560 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1561 const char *name, int len,
1562 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1564 struct dentry *child, *dir = filp->f_path.dentry;
1565 struct inode *inode;
1568 unsigned type = DT_UNKNOWN;
1572 qname.hash = full_name_hash(name, len);
1574 child = d_lookup(dir, &qname);
1577 new = d_alloc(dir, &qname);
1579 child = instantiate(dir->d_inode, new, task, ptr);
1586 if (!child || IS_ERR(child) || !child->d_inode)
1587 goto end_instantiate;
1588 inode = child->d_inode;
1591 type = inode->i_mode >> 12;
1596 ino = find_inode_number(dir, &qname);
1599 return filldir(dirent, name, len, filp->f_pos, ino, type);
1602 static unsigned name_to_int(struct dentry *dentry)
1604 const char *name = dentry->d_name.name;
1605 int len = dentry->d_name.len;
1608 if (len > 1 && *name == '0')
1611 unsigned c = *name++ - '0';
1614 if (n >= (~0U-9)/10)
1624 #define PROC_FDINFO_MAX 64
1626 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1628 struct task_struct *task = get_proc_task(inode);
1629 struct files_struct *files = NULL;
1631 int fd = proc_fd(inode);
1634 files = get_files_struct(task);
1635 put_task_struct(task);
1639 * We are not taking a ref to the file structure, so we must
1642 spin_lock(&files->file_lock);
1643 file = fcheck_files(files, fd);
1645 unsigned int f_flags;
1646 struct fdtable *fdt;
1648 fdt = files_fdtable(files);
1649 f_flags = file->f_flags & ~O_CLOEXEC;
1650 if (close_on_exec(fd, fdt))
1651 f_flags |= O_CLOEXEC;
1654 *path = file->f_path;
1655 path_get(&file->f_path);
1658 snprintf(info, PROC_FDINFO_MAX,
1661 (long long) file->f_pos,
1663 spin_unlock(&files->file_lock);
1664 put_files_struct(files);
1667 spin_unlock(&files->file_lock);
1668 put_files_struct(files);
1673 static int proc_fd_link(struct dentry *dentry, struct path *path)
1675 return proc_fd_info(dentry->d_inode, path, NULL);
1678 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
1680 struct inode *inode;
1681 struct task_struct *task;
1683 struct files_struct *files;
1684 const struct cred *cred;
1686 if (flags & LOOKUP_RCU)
1689 inode = dentry->d_inode;
1690 task = get_proc_task(inode);
1691 fd = proc_fd(inode);
1694 files = get_files_struct(task);
1698 file = fcheck_files(files, fd);
1700 unsigned f_mode = file->f_mode;
1703 put_files_struct(files);
1705 if (task_dumpable(task)) {
1707 cred = __task_cred(task);
1708 inode->i_uid = cred->euid;
1709 inode->i_gid = cred->egid;
1712 inode->i_uid = GLOBAL_ROOT_UID;
1713 inode->i_gid = GLOBAL_ROOT_GID;
1716 if (S_ISLNK(inode->i_mode)) {
1717 unsigned i_mode = S_IFLNK;
1718 if (f_mode & FMODE_READ)
1719 i_mode |= S_IRUSR | S_IXUSR;
1720 if (f_mode & FMODE_WRITE)
1721 i_mode |= S_IWUSR | S_IXUSR;
1722 inode->i_mode = i_mode;
1725 security_task_to_inode(task, inode);
1726 put_task_struct(task);
1730 put_files_struct(files);
1732 put_task_struct(task);
1738 static const struct dentry_operations tid_fd_dentry_operations =
1740 .d_revalidate = tid_fd_revalidate,
1741 .d_delete = pid_delete_dentry,
1744 static struct dentry *proc_fd_instantiate(struct inode *dir,
1745 struct dentry *dentry, struct task_struct *task, const void *ptr)
1747 unsigned fd = (unsigned long)ptr;
1748 struct inode *inode;
1749 struct proc_inode *ei;
1750 struct dentry *error = ERR_PTR(-ENOENT);
1752 inode = proc_pid_make_inode(dir->i_sb, task);
1758 inode->i_mode = S_IFLNK;
1759 inode->i_op = &proc_pid_link_inode_operations;
1761 ei->op.proc_get_link = proc_fd_link;
1762 d_set_d_op(dentry, &tid_fd_dentry_operations);
1763 d_add(dentry, inode);
1764 /* Close the race of the process dying before we return the dentry */
1765 if (tid_fd_revalidate(dentry, 0))
1772 static struct dentry *proc_lookupfd_common(struct inode *dir,
1773 struct dentry *dentry,
1774 instantiate_t instantiate)
1776 struct task_struct *task = get_proc_task(dir);
1777 unsigned fd = name_to_int(dentry);
1778 struct dentry *result = ERR_PTR(-ENOENT);
1785 result = instantiate(dir, dentry, task, (void *)(unsigned long)fd);
1787 put_task_struct(task);
1792 static int proc_readfd_common(struct file * filp, void * dirent,
1793 filldir_t filldir, instantiate_t instantiate)
1795 struct dentry *dentry = filp->f_path.dentry;
1796 struct inode *inode = dentry->d_inode;
1797 struct task_struct *p = get_proc_task(inode);
1798 unsigned int fd, ino;
1800 struct files_struct * files;
1810 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1814 ino = parent_ino(dentry);
1815 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1819 files = get_files_struct(p);
1823 for (fd = filp->f_pos-2;
1824 fd < files_fdtable(files)->max_fds;
1825 fd++, filp->f_pos++) {
1826 char name[PROC_NUMBUF];
1830 if (!fcheck_files(files, fd))
1834 len = snprintf(name, sizeof(name), "%d", fd);
1835 rv = proc_fill_cache(filp, dirent, filldir,
1836 name, len, instantiate, p,
1837 (void *)(unsigned long)fd);
1844 put_files_struct(files);
1852 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1855 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1858 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1860 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1863 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1864 size_t len, loff_t *ppos)
1866 char tmp[PROC_FDINFO_MAX];
1867 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1869 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1873 static const struct file_operations proc_fdinfo_file_operations = {
1874 .open = nonseekable_open,
1875 .read = proc_fdinfo_read,
1876 .llseek = no_llseek,
1879 static const struct file_operations proc_fd_operations = {
1880 .read = generic_read_dir,
1881 .readdir = proc_readfd,
1882 .llseek = default_llseek,
1885 #ifdef CONFIG_CHECKPOINT_RESTORE
1888 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1889 * which represent vma start and end addresses.
1891 static int dname_to_vma_addr(struct dentry *dentry,
1892 unsigned long *start, unsigned long *end)
1894 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1900 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1902 unsigned long vm_start, vm_end;
1903 bool exact_vma_exists = false;
1904 struct mm_struct *mm = NULL;
1905 struct task_struct *task;
1906 const struct cred *cred;
1907 struct inode *inode;
1910 if (flags & LOOKUP_RCU)
1913 if (!capable(CAP_SYS_ADMIN)) {
1918 inode = dentry->d_inode;
1919 task = get_proc_task(inode);
1923 mm = mm_access(task, PTRACE_MODE_READ);
1924 if (IS_ERR_OR_NULL(mm))
1927 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1928 down_read(&mm->mmap_sem);
1929 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1930 up_read(&mm->mmap_sem);
1935 if (exact_vma_exists) {
1936 if (task_dumpable(task)) {
1938 cred = __task_cred(task);
1939 inode->i_uid = cred->euid;
1940 inode->i_gid = cred->egid;
1943 inode->i_uid = GLOBAL_ROOT_UID;
1944 inode->i_gid = GLOBAL_ROOT_GID;
1946 security_task_to_inode(task, inode);
1951 put_task_struct(task);
1960 static const struct dentry_operations tid_map_files_dentry_operations = {
1961 .d_revalidate = map_files_d_revalidate,
1962 .d_delete = pid_delete_dentry,
1965 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1967 unsigned long vm_start, vm_end;
1968 struct vm_area_struct *vma;
1969 struct task_struct *task;
1970 struct mm_struct *mm;
1974 task = get_proc_task(dentry->d_inode);
1978 mm = get_task_mm(task);
1979 put_task_struct(task);
1983 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1987 down_read(&mm->mmap_sem);
1988 vma = find_exact_vma(mm, vm_start, vm_end);
1989 if (vma && vma->vm_file) {
1990 *path = vma->vm_file->f_path;
1994 up_read(&mm->mmap_sem);
2002 struct map_files_info {
2005 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2008 static struct dentry *
2009 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2010 struct task_struct *task, const void *ptr)
2012 const struct file *file = ptr;
2013 struct proc_inode *ei;
2014 struct inode *inode;
2017 return ERR_PTR(-ENOENT);
2019 inode = proc_pid_make_inode(dir->i_sb, task);
2021 return ERR_PTR(-ENOENT);
2024 ei->op.proc_get_link = proc_map_files_get_link;
2026 inode->i_op = &proc_pid_link_inode_operations;
2028 inode->i_mode = S_IFLNK;
2030 if (file->f_mode & FMODE_READ)
2031 inode->i_mode |= S_IRUSR;
2032 if (file->f_mode & FMODE_WRITE)
2033 inode->i_mode |= S_IWUSR;
2035 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2036 d_add(dentry, inode);
2041 static struct dentry *proc_map_files_lookup(struct inode *dir,
2042 struct dentry *dentry, unsigned int flags)
2044 unsigned long vm_start, vm_end;
2045 struct vm_area_struct *vma;
2046 struct task_struct *task;
2047 struct dentry *result;
2048 struct mm_struct *mm;
2050 result = ERR_PTR(-EACCES);
2051 if (!capable(CAP_SYS_ADMIN))
2054 result = ERR_PTR(-ENOENT);
2055 task = get_proc_task(dir);
2059 result = ERR_PTR(-EACCES);
2060 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2063 result = ERR_PTR(-ENOENT);
2064 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2067 mm = get_task_mm(task);
2071 down_read(&mm->mmap_sem);
2072 vma = find_exact_vma(mm, vm_start, vm_end);
2076 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2079 up_read(&mm->mmap_sem);
2082 put_task_struct(task);
2087 static const struct inode_operations proc_map_files_inode_operations = {
2088 .lookup = proc_map_files_lookup,
2089 .permission = proc_fd_permission,
2090 .setattr = proc_setattr,
2094 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2096 struct dentry *dentry = filp->f_path.dentry;
2097 struct inode *inode = dentry->d_inode;
2098 struct vm_area_struct *vma;
2099 struct task_struct *task;
2100 struct mm_struct *mm;
2105 if (!capable(CAP_SYS_ADMIN))
2109 task = get_proc_task(inode);
2114 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2118 switch (filp->f_pos) {
2121 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2125 ino = parent_ino(dentry);
2126 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2131 unsigned long nr_files, pos, i;
2132 struct flex_array *fa = NULL;
2133 struct map_files_info info;
2134 struct map_files_info *p;
2136 mm = get_task_mm(task);
2139 down_read(&mm->mmap_sem);
2144 * We need two passes here:
2146 * 1) Collect vmas of mapped files with mmap_sem taken
2147 * 2) Release mmap_sem and instantiate entries
2149 * otherwise we get lockdep complained, since filldir()
2150 * routine might require mmap_sem taken in might_fault().
2153 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2154 if (vma->vm_file && ++pos > filp->f_pos)
2159 fa = flex_array_alloc(sizeof(info), nr_files,
2161 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2165 flex_array_free(fa);
2166 up_read(&mm->mmap_sem);
2170 for (i = 0, vma = mm->mmap, pos = 2; vma;
2171 vma = vma->vm_next) {
2174 if (++pos <= filp->f_pos)
2177 get_file(vma->vm_file);
2178 info.file = vma->vm_file;
2179 info.len = snprintf(info.name,
2180 sizeof(info.name), "%lx-%lx",
2181 vma->vm_start, vma->vm_end);
2182 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2186 up_read(&mm->mmap_sem);
2188 for (i = 0; i < nr_files; i++) {
2189 p = flex_array_get(fa, i);
2190 ret = proc_fill_cache(filp, dirent, filldir,
2192 proc_map_files_instantiate,
2199 for (; i < nr_files; i++) {
2201 * In case of error don't forget
2202 * to put rest of file refs.
2204 p = flex_array_get(fa, i);
2208 flex_array_free(fa);
2214 put_task_struct(task);
2219 static const struct file_operations proc_map_files_operations = {
2220 .read = generic_read_dir,
2221 .readdir = proc_map_files_readdir,
2222 .llseek = default_llseek,
2225 #endif /* CONFIG_CHECKPOINT_RESTORE */
2228 * /proc/pid/fd needs a special permission handler so that a process can still
2229 * access /proc/self/fd after it has executed a setuid().
2231 static int proc_fd_permission(struct inode *inode, int mask)
2233 int rv = generic_permission(inode, mask);
2236 if (task_pid(current) == proc_pid(inode))
2242 * proc directories can do almost nothing..
2244 static const struct inode_operations proc_fd_inode_operations = {
2245 .lookup = proc_lookupfd,
2246 .permission = proc_fd_permission,
2247 .setattr = proc_setattr,
2250 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2251 struct dentry *dentry, struct task_struct *task, const void *ptr)
2253 unsigned fd = (unsigned long)ptr;
2254 struct inode *inode;
2255 struct proc_inode *ei;
2256 struct dentry *error = ERR_PTR(-ENOENT);
2258 inode = proc_pid_make_inode(dir->i_sb, task);
2263 inode->i_mode = S_IFREG | S_IRUSR;
2264 inode->i_fop = &proc_fdinfo_file_operations;
2265 d_set_d_op(dentry, &tid_fd_dentry_operations);
2266 d_add(dentry, inode);
2267 /* Close the race of the process dying before we return the dentry */
2268 if (tid_fd_revalidate(dentry, 0))
2275 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2276 struct dentry *dentry,
2279 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2282 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2284 return proc_readfd_common(filp, dirent, filldir,
2285 proc_fdinfo_instantiate);
2288 static const struct file_operations proc_fdinfo_operations = {
2289 .read = generic_read_dir,
2290 .readdir = proc_readfdinfo,
2291 .llseek = default_llseek,
2295 * proc directories can do almost nothing..
2297 static const struct inode_operations proc_fdinfo_inode_operations = {
2298 .lookup = proc_lookupfdinfo,
2299 .setattr = proc_setattr,
2303 static struct dentry *proc_pident_instantiate(struct inode *dir,
2304 struct dentry *dentry, struct task_struct *task, const void *ptr)
2306 const struct pid_entry *p = ptr;
2307 struct inode *inode;
2308 struct proc_inode *ei;
2309 struct dentry *error = ERR_PTR(-ENOENT);
2311 inode = proc_pid_make_inode(dir->i_sb, task);
2316 inode->i_mode = p->mode;
2317 if (S_ISDIR(inode->i_mode))
2318 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2320 inode->i_op = p->iop;
2322 inode->i_fop = p->fop;
2324 d_set_d_op(dentry, &pid_dentry_operations);
2325 d_add(dentry, inode);
2326 /* Close the race of the process dying before we return the dentry */
2327 if (pid_revalidate(dentry, 0))
2333 static struct dentry *proc_pident_lookup(struct inode *dir,
2334 struct dentry *dentry,
2335 const struct pid_entry *ents,
2338 struct dentry *error;
2339 struct task_struct *task = get_proc_task(dir);
2340 const struct pid_entry *p, *last;
2342 error = ERR_PTR(-ENOENT);
2348 * Yes, it does not scale. And it should not. Don't add
2349 * new entries into /proc/<tgid>/ without very good reasons.
2351 last = &ents[nents - 1];
2352 for (p = ents; p <= last; p++) {
2353 if (p->len != dentry->d_name.len)
2355 if (!memcmp(dentry->d_name.name, p->name, p->len))
2361 error = proc_pident_instantiate(dir, dentry, task, p);
2363 put_task_struct(task);
2368 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2369 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2371 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2372 proc_pident_instantiate, task, p);
2375 static int proc_pident_readdir(struct file *filp,
2376 void *dirent, filldir_t filldir,
2377 const struct pid_entry *ents, unsigned int nents)
2380 struct dentry *dentry = filp->f_path.dentry;
2381 struct inode *inode = dentry->d_inode;
2382 struct task_struct *task = get_proc_task(inode);
2383 const struct pid_entry *p, *last;
2396 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2402 ino = parent_ino(dentry);
2403 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2415 last = &ents[nents - 1];
2417 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2426 put_task_struct(task);
2431 #ifdef CONFIG_SECURITY
2432 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2433 size_t count, loff_t *ppos)
2435 struct inode * inode = file->f_path.dentry->d_inode;
2438 struct task_struct *task = get_proc_task(inode);
2443 length = security_getprocattr(task,
2444 (char*)file->f_path.dentry->d_name.name,
2446 put_task_struct(task);
2448 length = simple_read_from_buffer(buf, count, ppos, p, length);
2453 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2454 size_t count, loff_t *ppos)
2456 struct inode * inode = file->f_path.dentry->d_inode;
2459 struct task_struct *task = get_proc_task(inode);
2464 if (count > PAGE_SIZE)
2467 /* No partial writes. */
2473 page = (char*)__get_free_page(GFP_TEMPORARY);
2478 if (copy_from_user(page, buf, count))
2481 /* Guard against adverse ptrace interaction */
2482 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2486 length = security_setprocattr(task,
2487 (char*)file->f_path.dentry->d_name.name,
2488 (void*)page, count);
2489 mutex_unlock(&task->signal->cred_guard_mutex);
2491 free_page((unsigned long) page);
2493 put_task_struct(task);
2498 static const struct file_operations proc_pid_attr_operations = {
2499 .read = proc_pid_attr_read,
2500 .write = proc_pid_attr_write,
2501 .llseek = generic_file_llseek,
2504 static const struct pid_entry attr_dir_stuff[] = {
2505 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2506 REG("prev", S_IRUGO, proc_pid_attr_operations),
2507 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2508 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2509 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2510 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2513 static int proc_attr_dir_readdir(struct file * filp,
2514 void * dirent, filldir_t filldir)
2516 return proc_pident_readdir(filp,dirent,filldir,
2517 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2520 static const struct file_operations proc_attr_dir_operations = {
2521 .read = generic_read_dir,
2522 .readdir = proc_attr_dir_readdir,
2523 .llseek = default_llseek,
2526 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2527 struct dentry *dentry, unsigned int flags)
2529 return proc_pident_lookup(dir, dentry,
2530 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2533 static const struct inode_operations proc_attr_dir_inode_operations = {
2534 .lookup = proc_attr_dir_lookup,
2535 .getattr = pid_getattr,
2536 .setattr = proc_setattr,
2541 #ifdef CONFIG_ELF_CORE
2542 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2543 size_t count, loff_t *ppos)
2545 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2546 struct mm_struct *mm;
2547 char buffer[PROC_NUMBUF];
2555 mm = get_task_mm(task);
2557 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2558 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2559 MMF_DUMP_FILTER_SHIFT));
2561 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2564 put_task_struct(task);
2569 static ssize_t proc_coredump_filter_write(struct file *file,
2570 const char __user *buf,
2574 struct task_struct *task;
2575 struct mm_struct *mm;
2576 char buffer[PROC_NUMBUF], *end;
2583 memset(buffer, 0, sizeof(buffer));
2584 if (count > sizeof(buffer) - 1)
2585 count = sizeof(buffer) - 1;
2586 if (copy_from_user(buffer, buf, count))
2590 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2593 if (end - buffer == 0)
2597 task = get_proc_task(file->f_dentry->d_inode);
2602 mm = get_task_mm(task);
2606 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2608 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2610 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2615 put_task_struct(task);
2620 static const struct file_operations proc_coredump_filter_operations = {
2621 .read = proc_coredump_filter_read,
2622 .write = proc_coredump_filter_write,
2623 .llseek = generic_file_llseek,
2630 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2633 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2634 pid_t tgid = task_tgid_nr_ns(current, ns);
2635 char tmp[PROC_NUMBUF];
2638 sprintf(tmp, "%d", tgid);
2639 return vfs_readlink(dentry,buffer,buflen,tmp);
2642 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2644 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2645 pid_t tgid = task_tgid_nr_ns(current, ns);
2646 char *name = ERR_PTR(-ENOENT);
2650 name = ERR_PTR(-ENOMEM);
2652 sprintf(name, "%d", tgid);
2654 nd_set_link(nd, name);
2658 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2661 char *s = nd_get_link(nd);
2666 static const struct inode_operations proc_self_inode_operations = {
2667 .readlink = proc_self_readlink,
2668 .follow_link = proc_self_follow_link,
2669 .put_link = proc_self_put_link,
2675 * These are the directory entries in the root directory of /proc
2676 * that properly belong to the /proc filesystem, as they describe
2677 * describe something that is process related.
2679 static const struct pid_entry proc_base_stuff[] = {
2680 NOD("self", S_IFLNK|S_IRWXUGO,
2681 &proc_self_inode_operations, NULL, {}),
2684 static struct dentry *proc_base_instantiate(struct inode *dir,
2685 struct dentry *dentry, struct task_struct *task, const void *ptr)
2687 const struct pid_entry *p = ptr;
2688 struct inode *inode;
2689 struct proc_inode *ei;
2690 struct dentry *error;
2692 /* Allocate the inode */
2693 error = ERR_PTR(-ENOMEM);
2694 inode = new_inode(dir->i_sb);
2698 /* Initialize the inode */
2700 inode->i_ino = get_next_ino();
2701 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2704 * grab the reference to the task.
2706 ei->pid = get_task_pid(task, PIDTYPE_PID);
2710 inode->i_mode = p->mode;
2711 if (S_ISDIR(inode->i_mode))
2712 set_nlink(inode, 2);
2713 if (S_ISLNK(inode->i_mode))
2716 inode->i_op = p->iop;
2718 inode->i_fop = p->fop;
2720 d_add(dentry, inode);
2729 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2731 struct dentry *error;
2732 struct task_struct *task = get_proc_task(dir);
2733 const struct pid_entry *p, *last;
2735 error = ERR_PTR(-ENOENT);
2740 /* Lookup the directory entry */
2741 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2742 for (p = proc_base_stuff; p <= last; p++) {
2743 if (p->len != dentry->d_name.len)
2745 if (!memcmp(dentry->d_name.name, p->name, p->len))
2751 error = proc_base_instantiate(dir, dentry, task, p);
2754 put_task_struct(task);
2759 static int proc_base_fill_cache(struct file *filp, void *dirent,
2760 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2762 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2763 proc_base_instantiate, task, p);
2766 #ifdef CONFIG_TASK_IO_ACCOUNTING
2767 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2769 struct task_io_accounting acct = task->ioac;
2770 unsigned long flags;
2773 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2777 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2782 if (whole && lock_task_sighand(task, &flags)) {
2783 struct task_struct *t = task;
2785 task_io_accounting_add(&acct, &task->signal->ioac);
2786 while_each_thread(task, t)
2787 task_io_accounting_add(&acct, &t->ioac);
2789 unlock_task_sighand(task, &flags);
2791 result = sprintf(buffer,
2796 "read_bytes: %llu\n"
2797 "write_bytes: %llu\n"
2798 "cancelled_write_bytes: %llu\n",
2799 (unsigned long long)acct.rchar,
2800 (unsigned long long)acct.wchar,
2801 (unsigned long long)acct.syscr,
2802 (unsigned long long)acct.syscw,
2803 (unsigned long long)acct.read_bytes,
2804 (unsigned long long)acct.write_bytes,
2805 (unsigned long long)acct.cancelled_write_bytes);
2807 mutex_unlock(&task->signal->cred_guard_mutex);
2811 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2813 return do_io_accounting(task, buffer, 0);
2816 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2818 return do_io_accounting(task, buffer, 1);
2820 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2822 #ifdef CONFIG_USER_NS
2823 static int proc_id_map_open(struct inode *inode, struct file *file,
2824 struct seq_operations *seq_ops)
2826 struct user_namespace *ns = NULL;
2827 struct task_struct *task;
2828 struct seq_file *seq;
2831 task = get_proc_task(inode);
2834 ns = get_user_ns(task_cred_xxx(task, user_ns));
2836 put_task_struct(task);
2841 ret = seq_open(file, seq_ops);
2845 seq = file->private_data;
2855 static int proc_id_map_release(struct inode *inode, struct file *file)
2857 struct seq_file *seq = file->private_data;
2858 struct user_namespace *ns = seq->private;
2860 return seq_release(inode, file);
2863 static int proc_uid_map_open(struct inode *inode, struct file *file)
2865 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2868 static int proc_gid_map_open(struct inode *inode, struct file *file)
2870 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2873 static const struct file_operations proc_uid_map_operations = {
2874 .open = proc_uid_map_open,
2875 .write = proc_uid_map_write,
2877 .llseek = seq_lseek,
2878 .release = proc_id_map_release,
2881 static const struct file_operations proc_gid_map_operations = {
2882 .open = proc_gid_map_open,
2883 .write = proc_gid_map_write,
2885 .llseek = seq_lseek,
2886 .release = proc_id_map_release,
2888 #endif /* CONFIG_USER_NS */
2890 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2891 struct pid *pid, struct task_struct *task)
2893 int err = lock_trace(task);
2895 seq_printf(m, "%08x\n", task->personality);
2904 static const struct file_operations proc_task_operations;
2905 static const struct inode_operations proc_task_inode_operations;
2907 static const struct pid_entry tgid_base_stuff[] = {
2908 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2909 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2910 #ifdef CONFIG_CHECKPOINT_RESTORE
2911 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2913 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2914 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2916 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2918 REG("environ", S_IRUSR, proc_environ_operations),
2919 INF("auxv", S_IRUSR, proc_pid_auxv),
2920 ONE("status", S_IRUGO, proc_pid_status),
2921 ONE("personality", S_IRUGO, proc_pid_personality),
2922 INF("limits", S_IRUGO, proc_pid_limits),
2923 #ifdef CONFIG_SCHED_DEBUG
2924 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2926 #ifdef CONFIG_SCHED_AUTOGROUP
2927 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2929 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2930 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2931 INF("syscall", S_IRUGO, proc_pid_syscall),
2933 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2934 ONE("stat", S_IRUGO, proc_tgid_stat),
2935 ONE("statm", S_IRUGO, proc_pid_statm),
2936 REG("maps", S_IRUGO, proc_pid_maps_operations),
2938 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2940 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2941 LNK("cwd", proc_cwd_link),
2942 LNK("root", proc_root_link),
2943 LNK("exe", proc_exe_link),
2944 REG("mounts", S_IRUGO, proc_mounts_operations),
2945 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2946 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2947 #ifdef CONFIG_PROC_PAGE_MONITOR
2948 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2949 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2950 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2952 #ifdef CONFIG_SECURITY
2953 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2955 #ifdef CONFIG_KALLSYMS
2956 INF("wchan", S_IRUGO, proc_pid_wchan),
2958 #ifdef CONFIG_STACKTRACE
2959 ONE("stack", S_IRUGO, proc_pid_stack),
2961 #ifdef CONFIG_SCHEDSTATS
2962 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2964 #ifdef CONFIG_LATENCYTOP
2965 REG("latency", S_IRUGO, proc_lstats_operations),
2967 #ifdef CONFIG_PROC_PID_CPUSET
2968 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2970 #ifdef CONFIG_CGROUPS
2971 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2973 INF("oom_score", S_IRUGO, proc_oom_score),
2974 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2975 #ifdef CONFIG_AUDITSYSCALL
2976 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2977 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2979 #ifdef CONFIG_FAULT_INJECTION
2980 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2982 #ifdef CONFIG_ELF_CORE
2983 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2985 #ifdef CONFIG_TASK_IO_ACCOUNTING
2986 INF("io", S_IRUSR, proc_tgid_io_accounting),
2988 #ifdef CONFIG_HARDWALL
2989 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2991 #ifdef CONFIG_USER_NS
2992 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2993 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2997 static int proc_tgid_base_readdir(struct file * filp,
2998 void * dirent, filldir_t filldir)
3000 return proc_pident_readdir(filp,dirent,filldir,
3001 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3004 static const struct file_operations proc_tgid_base_operations = {
3005 .read = generic_read_dir,
3006 .readdir = proc_tgid_base_readdir,
3007 .llseek = default_llseek,
3010 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3012 return proc_pident_lookup(dir, dentry,
3013 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3016 static const struct inode_operations proc_tgid_base_inode_operations = {
3017 .lookup = proc_tgid_base_lookup,
3018 .getattr = pid_getattr,
3019 .setattr = proc_setattr,
3020 .permission = proc_pid_permission,
3023 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3025 struct dentry *dentry, *leader, *dir;
3026 char buf[PROC_NUMBUF];
3030 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3031 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3033 shrink_dcache_parent(dentry);
3039 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3040 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3045 name.len = strlen(name.name);
3046 dir = d_hash_and_lookup(leader, &name);
3048 goto out_put_leader;
3051 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3052 dentry = d_hash_and_lookup(dir, &name);
3054 shrink_dcache_parent(dentry);
3067 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3068 * @task: task that should be flushed.
3070 * When flushing dentries from proc, one needs to flush them from global
3071 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3072 * in. This call is supposed to do all of this job.
3074 * Looks in the dcache for
3076 * /proc/@tgid/task/@pid
3077 * if either directory is present flushes it and all of it'ts children
3080 * It is safe and reasonable to cache /proc entries for a task until
3081 * that task exits. After that they just clog up the dcache with
3082 * useless entries, possibly causing useful dcache entries to be
3083 * flushed instead. This routine is proved to flush those useless
3084 * dcache entries at process exit time.
3086 * NOTE: This routine is just an optimization so it does not guarantee
3087 * that no dcache entries will exist at process exit time it
3088 * just makes it very unlikely that any will persist.
3091 void proc_flush_task(struct task_struct *task)
3094 struct pid *pid, *tgid;
3097 pid = task_pid(task);
3098 tgid = task_tgid(task);
3100 for (i = 0; i <= pid->level; i++) {
3101 upid = &pid->numbers[i];
3102 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3103 tgid->numbers[i].nr);
3106 upid = &pid->numbers[pid->level];
3108 pid_ns_release_proc(upid->ns);
3111 static struct dentry *proc_pid_instantiate(struct inode *dir,
3112 struct dentry * dentry,
3113 struct task_struct *task, const void *ptr)
3115 struct dentry *error = ERR_PTR(-ENOENT);
3116 struct inode *inode;
3118 inode = proc_pid_make_inode(dir->i_sb, task);
3122 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3123 inode->i_op = &proc_tgid_base_inode_operations;
3124 inode->i_fop = &proc_tgid_base_operations;
3125 inode->i_flags|=S_IMMUTABLE;
3127 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3128 ARRAY_SIZE(tgid_base_stuff)));
3130 d_set_d_op(dentry, &pid_dentry_operations);
3132 d_add(dentry, inode);
3133 /* Close the race of the process dying before we return the dentry */
3134 if (pid_revalidate(dentry, 0))
3140 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3142 struct dentry *result;
3143 struct task_struct *task;
3145 struct pid_namespace *ns;
3147 result = proc_base_lookup(dir, dentry);
3148 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3151 tgid = name_to_int(dentry);
3155 ns = dentry->d_sb->s_fs_info;
3157 task = find_task_by_pid_ns(tgid, ns);
3159 get_task_struct(task);
3164 result = proc_pid_instantiate(dir, dentry, task, NULL);
3165 put_task_struct(task);
3171 * Find the first task with tgid >= tgid
3176 struct task_struct *task;
3178 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3183 put_task_struct(iter.task);
3187 pid = find_ge_pid(iter.tgid, ns);
3189 iter.tgid = pid_nr_ns(pid, ns);
3190 iter.task = pid_task(pid, PIDTYPE_PID);
3191 /* What we to know is if the pid we have find is the
3192 * pid of a thread_group_leader. Testing for task
3193 * being a thread_group_leader is the obvious thing
3194 * todo but there is a window when it fails, due to
3195 * the pid transfer logic in de_thread.
3197 * So we perform the straight forward test of seeing
3198 * if the pid we have found is the pid of a thread
3199 * group leader, and don't worry if the task we have
3200 * found doesn't happen to be a thread group leader.
3201 * As we don't care in the case of readdir.
3203 if (!iter.task || !has_group_leader_pid(iter.task)) {
3207 get_task_struct(iter.task);
3213 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3215 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3216 struct tgid_iter iter)
3218 char name[PROC_NUMBUF];
3219 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3220 return proc_fill_cache(filp, dirent, filldir, name, len,
3221 proc_pid_instantiate, iter.task, NULL);
3224 static int fake_filldir(void *buf, const char *name, int namelen,
3225 loff_t offset, u64 ino, unsigned d_type)
3230 /* for the /proc/ directory itself, after non-process stuff has been done */
3231 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3234 struct task_struct *reaper;
3235 struct tgid_iter iter;
3236 struct pid_namespace *ns;
3237 filldir_t __filldir;
3239 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3241 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3243 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3247 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3248 const struct pid_entry *p = &proc_base_stuff[nr];
3249 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3253 ns = filp->f_dentry->d_sb->s_fs_info;
3255 iter.tgid = filp->f_pos - TGID_OFFSET;
3256 for (iter = next_tgid(ns, iter);
3258 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3259 if (has_pid_permissions(ns, iter.task, 2))
3260 __filldir = filldir;
3262 __filldir = fake_filldir;
3264 filp->f_pos = iter.tgid + TGID_OFFSET;
3265 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
3266 put_task_struct(iter.task);
3270 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3272 put_task_struct(reaper);
3280 static const struct pid_entry tid_base_stuff[] = {
3281 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3282 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3283 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3284 REG("environ", S_IRUSR, proc_environ_operations),
3285 INF("auxv", S_IRUSR, proc_pid_auxv),
3286 ONE("status", S_IRUGO, proc_pid_status),
3287 ONE("personality", S_IRUGO, proc_pid_personality),
3288 INF("limits", S_IRUGO, proc_pid_limits),
3289 #ifdef CONFIG_SCHED_DEBUG
3290 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3292 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3293 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3294 INF("syscall", S_IRUGO, proc_pid_syscall),
3296 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3297 ONE("stat", S_IRUGO, proc_tid_stat),
3298 ONE("statm", S_IRUGO, proc_pid_statm),
3299 REG("maps", S_IRUGO, proc_tid_maps_operations),
3300 #ifdef CONFIG_CHECKPOINT_RESTORE
3301 REG("children", S_IRUGO, proc_tid_children_operations),
3304 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
3306 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3307 LNK("cwd", proc_cwd_link),
3308 LNK("root", proc_root_link),
3309 LNK("exe", proc_exe_link),
3310 REG("mounts", S_IRUGO, proc_mounts_operations),
3311 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3312 #ifdef CONFIG_PROC_PAGE_MONITOR
3313 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3314 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
3315 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3317 #ifdef CONFIG_SECURITY
3318 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3320 #ifdef CONFIG_KALLSYMS
3321 INF("wchan", S_IRUGO, proc_pid_wchan),
3323 #ifdef CONFIG_STACKTRACE
3324 ONE("stack", S_IRUGO, proc_pid_stack),
3326 #ifdef CONFIG_SCHEDSTATS
3327 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3329 #ifdef CONFIG_LATENCYTOP
3330 REG("latency", S_IRUGO, proc_lstats_operations),
3332 #ifdef CONFIG_PROC_PID_CPUSET
3333 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3335 #ifdef CONFIG_CGROUPS
3336 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3338 INF("oom_score", S_IRUGO, proc_oom_score),
3339 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3340 #ifdef CONFIG_AUDITSYSCALL
3341 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3342 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3344 #ifdef CONFIG_FAULT_INJECTION
3345 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3347 #ifdef CONFIG_TASK_IO_ACCOUNTING
3348 INF("io", S_IRUSR, proc_tid_io_accounting),
3350 #ifdef CONFIG_HARDWALL
3351 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3353 #ifdef CONFIG_USER_NS
3354 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3355 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3359 static int proc_tid_base_readdir(struct file * filp,
3360 void * dirent, filldir_t filldir)
3362 return proc_pident_readdir(filp,dirent,filldir,
3363 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3366 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3368 return proc_pident_lookup(dir, dentry,
3369 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3372 static const struct file_operations proc_tid_base_operations = {
3373 .read = generic_read_dir,
3374 .readdir = proc_tid_base_readdir,
3375 .llseek = default_llseek,
3378 static const struct inode_operations proc_tid_base_inode_operations = {
3379 .lookup = proc_tid_base_lookup,
3380 .getattr = pid_getattr,
3381 .setattr = proc_setattr,
3384 static struct dentry *proc_task_instantiate(struct inode *dir,
3385 struct dentry *dentry, struct task_struct *task, const void *ptr)
3387 struct dentry *error = ERR_PTR(-ENOENT);
3388 struct inode *inode;
3389 inode = proc_pid_make_inode(dir->i_sb, task);
3393 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3394 inode->i_op = &proc_tid_base_inode_operations;
3395 inode->i_fop = &proc_tid_base_operations;
3396 inode->i_flags|=S_IMMUTABLE;
3398 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3399 ARRAY_SIZE(tid_base_stuff)));
3401 d_set_d_op(dentry, &pid_dentry_operations);
3403 d_add(dentry, inode);
3404 /* Close the race of the process dying before we return the dentry */
3405 if (pid_revalidate(dentry, 0))
3411 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3413 struct dentry *result = ERR_PTR(-ENOENT);
3414 struct task_struct *task;
3415 struct task_struct *leader = get_proc_task(dir);
3417 struct pid_namespace *ns;
3422 tid = name_to_int(dentry);
3426 ns = dentry->d_sb->s_fs_info;
3428 task = find_task_by_pid_ns(tid, ns);
3430 get_task_struct(task);
3434 if (!same_thread_group(leader, task))
3437 result = proc_task_instantiate(dir, dentry, task, NULL);
3439 put_task_struct(task);
3441 put_task_struct(leader);
3447 * Find the first tid of a thread group to return to user space.
3449 * Usually this is just the thread group leader, but if the users
3450 * buffer was too small or there was a seek into the middle of the
3451 * directory we have more work todo.
3453 * In the case of a short read we start with find_task_by_pid.
3455 * In the case of a seek we start with the leader and walk nr
3458 static struct task_struct *first_tid(struct task_struct *leader,
3459 int tid, int nr, struct pid_namespace *ns)
3461 struct task_struct *pos;
3464 /* Attempt to start with the pid of a thread */
3465 if (tid && (nr > 0)) {
3466 pos = find_task_by_pid_ns(tid, ns);
3467 if (pos && (pos->group_leader == leader))
3471 /* If nr exceeds the number of threads there is nothing todo */
3473 if (nr && nr >= get_nr_threads(leader))
3476 /* If we haven't found our starting place yet start
3477 * with the leader and walk nr threads forward.
3479 for (pos = leader; nr > 0; --nr) {
3480 pos = next_thread(pos);
3481 if (pos == leader) {
3487 get_task_struct(pos);
3494 * Find the next thread in the thread list.
3495 * Return NULL if there is an error or no next thread.
3497 * The reference to the input task_struct is released.
3499 static struct task_struct *next_tid(struct task_struct *start)
3501 struct task_struct *pos = NULL;
3503 if (pid_alive(start)) {
3504 pos = next_thread(start);
3505 if (thread_group_leader(pos))
3508 get_task_struct(pos);
3511 put_task_struct(start);
3515 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3516 struct task_struct *task, int tid)
3518 char name[PROC_NUMBUF];
3519 int len = snprintf(name, sizeof(name), "%d", tid);
3520 return proc_fill_cache(filp, dirent, filldir, name, len,
3521 proc_task_instantiate, task, NULL);
3524 /* for the /proc/TGID/task/ directories */
3525 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3527 struct dentry *dentry = filp->f_path.dentry;
3528 struct inode *inode = dentry->d_inode;
3529 struct task_struct *leader = NULL;
3530 struct task_struct *task;
3531 int retval = -ENOENT;
3534 struct pid_namespace *ns;
3536 task = get_proc_task(inode);
3540 if (pid_alive(task)) {
3541 leader = task->group_leader;
3542 get_task_struct(leader);
3545 put_task_struct(task);
3550 switch ((unsigned long)filp->f_pos) {
3553 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3558 ino = parent_ino(dentry);
3559 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3565 /* f_version caches the tgid value that the last readdir call couldn't
3566 * return. lseek aka telldir automagically resets f_version to 0.
3568 ns = filp->f_dentry->d_sb->s_fs_info;
3569 tid = (int)filp->f_version;
3570 filp->f_version = 0;
3571 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3573 task = next_tid(task), filp->f_pos++) {
3574 tid = task_pid_nr_ns(task, ns);
3575 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3576 /* returning this tgid failed, save it as the first
3577 * pid for the next readir call */
3578 filp->f_version = (u64)tid;
3579 put_task_struct(task);
3584 put_task_struct(leader);
3589 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3591 struct inode *inode = dentry->d_inode;
3592 struct task_struct *p = get_proc_task(inode);
3593 generic_fillattr(inode, stat);
3596 stat->nlink += get_nr_threads(p);
3603 static const struct inode_operations proc_task_inode_operations = {
3604 .lookup = proc_task_lookup,
3605 .getattr = proc_task_getattr,
3606 .setattr = proc_setattr,
3607 .permission = proc_pid_permission,
3610 static const struct file_operations proc_task_operations = {
3611 .read = generic_read_dir,
3612 .readdir = proc_task_readdir,
3613 .llseek = default_llseek,