4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
93 #include <trace/events/oom.h>
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
111 const struct inode_operations *iop;
112 const struct file_operations *fop;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
118 .len = sizeof(NAME) - 1, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
143 * Count the number of hardlinks for the pid_entry table, excluding the .
146 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
161 static int get_task_root(struct task_struct *task, struct path *root)
163 int result = -ENOENT;
167 get_fs_root(task->fs, root);
174 static int proc_cwd_link(struct dentry *dentry, struct path *path)
176 struct task_struct *task = get_proc_task(dentry->d_inode);
177 int result = -ENOENT;
182 get_fs_pwd(task->fs, path);
186 put_task_struct(task);
191 static int proc_root_link(struct dentry *dentry, struct path *path)
193 struct task_struct *task = get_proc_task(dentry->d_inode);
194 int result = -ENOENT;
197 result = get_task_root(task, path);
198 put_task_struct(task);
203 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
207 struct mm_struct *mm = get_task_mm(task);
211 goto out_mm; /* Shh! No looking before we're done */
213 len = mm->arg_end - mm->arg_start;
218 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
223 len = strnlen(buffer, res);
227 len = mm->env_end - mm->env_start;
228 if (len > PAGE_SIZE - res)
229 len = PAGE_SIZE - res;
230 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
231 res = strnlen(buffer, res);
240 static int proc_pid_auxv(struct task_struct *task, char *buffer)
242 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
243 int res = PTR_ERR(mm);
244 if (mm && !IS_ERR(mm)) {
245 unsigned int nwords = 0;
248 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
249 res = nwords * sizeof(mm->saved_auxv[0]);
252 memcpy(buffer, mm->saved_auxv, res);
259 #ifdef CONFIG_KALLSYMS
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
264 static int proc_pid_wchan(struct task_struct *task, char *buffer)
267 char symname[KSYM_NAME_LEN];
269 wchan = get_wchan(task);
271 if (lookup_symbol_name(wchan, symname) < 0)
272 if (!ptrace_may_access(task, PTRACE_MODE_READ))
275 return sprintf(buffer, "%lu", wchan);
277 return sprintf(buffer, "%s", symname);
279 #endif /* CONFIG_KALLSYMS */
281 static int lock_trace(struct task_struct *task)
283 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
286 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
287 mutex_unlock(&task->signal->cred_guard_mutex);
293 static void unlock_trace(struct task_struct *task)
295 mutex_unlock(&task->signal->cred_guard_mutex);
298 #ifdef CONFIG_STACKTRACE
300 #define MAX_STACK_TRACE_DEPTH 64
302 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
303 struct pid *pid, struct task_struct *task)
305 struct stack_trace trace;
306 unsigned long *entries;
310 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
314 trace.nr_entries = 0;
315 trace.max_entries = MAX_STACK_TRACE_DEPTH;
316 trace.entries = entries;
319 err = lock_trace(task);
321 save_stack_trace_tsk(task, &trace);
323 for (i = 0; i < trace.nr_entries; i++) {
324 seq_printf(m, "[<%pK>] %pS\n",
325 (void *)entries[i], (void *)entries[i]);
335 #ifdef CONFIG_SCHEDSTATS
337 * Provides /proc/PID/schedstat
339 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
341 return sprintf(buffer, "%llu %llu %lu\n",
342 (unsigned long long)task->se.sum_exec_runtime,
343 (unsigned long long)task->sched_info.run_delay,
344 task->sched_info.pcount);
348 #ifdef CONFIG_LATENCYTOP
349 static int lstats_show_proc(struct seq_file *m, void *v)
352 struct inode *inode = m->private;
353 struct task_struct *task = get_proc_task(inode);
357 seq_puts(m, "Latency Top version : v0.1\n");
358 for (i = 0; i < 32; i++) {
359 struct latency_record *lr = &task->latency_record[i];
360 if (lr->backtrace[0]) {
362 seq_printf(m, "%i %li %li",
363 lr->count, lr->time, lr->max);
364 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
365 unsigned long bt = lr->backtrace[q];
370 seq_printf(m, " %ps", (void *)bt);
376 put_task_struct(task);
380 static int lstats_open(struct inode *inode, struct file *file)
382 return single_open(file, lstats_show_proc, inode);
385 static ssize_t lstats_write(struct file *file, const char __user *buf,
386 size_t count, loff_t *offs)
388 struct task_struct *task = get_proc_task(file_inode(file));
392 clear_all_latency_tracing(task);
393 put_task_struct(task);
398 static const struct file_operations proc_lstats_operations = {
401 .write = lstats_write,
403 .release = single_release,
408 static int proc_oom_score(struct task_struct *task, char *buffer)
410 unsigned long totalpages = totalram_pages + total_swap_pages;
411 unsigned long points = 0;
413 read_lock(&tasklist_lock);
415 points = oom_badness(task, NULL, NULL, totalpages) *
417 read_unlock(&tasklist_lock);
418 return sprintf(buffer, "%lu\n", points);
426 static const struct limit_names lnames[RLIM_NLIMITS] = {
427 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
428 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
429 [RLIMIT_DATA] = {"Max data size", "bytes"},
430 [RLIMIT_STACK] = {"Max stack size", "bytes"},
431 [RLIMIT_CORE] = {"Max core file size", "bytes"},
432 [RLIMIT_RSS] = {"Max resident set", "bytes"},
433 [RLIMIT_NPROC] = {"Max processes", "processes"},
434 [RLIMIT_NOFILE] = {"Max open files", "files"},
435 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
436 [RLIMIT_AS] = {"Max address space", "bytes"},
437 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
438 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
439 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
440 [RLIMIT_NICE] = {"Max nice priority", NULL},
441 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
442 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
445 /* Display limits for a process */
446 static int proc_pid_limits(struct task_struct *task, char *buffer)
451 char *bufptr = buffer;
453 struct rlimit rlim[RLIM_NLIMITS];
455 if (!lock_task_sighand(task, &flags))
457 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
458 unlock_task_sighand(task, &flags);
461 * print the file header
463 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
464 "Limit", "Soft Limit", "Hard Limit", "Units");
466 for (i = 0; i < RLIM_NLIMITS; i++) {
467 if (rlim[i].rlim_cur == RLIM_INFINITY)
468 count += sprintf(&bufptr[count], "%-25s %-20s ",
469 lnames[i].name, "unlimited");
471 count += sprintf(&bufptr[count], "%-25s %-20lu ",
472 lnames[i].name, rlim[i].rlim_cur);
474 if (rlim[i].rlim_max == RLIM_INFINITY)
475 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
477 count += sprintf(&bufptr[count], "%-20lu ",
481 count += sprintf(&bufptr[count], "%-10s\n",
484 count += sprintf(&bufptr[count], "\n");
490 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
491 static int proc_pid_syscall(struct task_struct *task, char *buffer)
494 unsigned long args[6], sp, pc;
495 int res = lock_trace(task);
499 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
500 res = sprintf(buffer, "running\n");
502 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
504 res = sprintf(buffer,
505 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
507 args[0], args[1], args[2], args[3], args[4], args[5],
512 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
514 /************************************************************************/
515 /* Here the fs part begins */
516 /************************************************************************/
518 /* permission checks */
519 static int proc_fd_access_allowed(struct inode *inode)
521 struct task_struct *task;
523 /* Allow access to a task's file descriptors if it is us or we
524 * may use ptrace attach to the process and find out that
527 task = get_proc_task(inode);
529 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
530 put_task_struct(task);
535 int proc_setattr(struct dentry *dentry, struct iattr *attr)
538 struct inode *inode = dentry->d_inode;
540 if (attr->ia_valid & ATTR_MODE)
543 error = inode_change_ok(inode, attr);
547 setattr_copy(inode, attr);
548 mark_inode_dirty(inode);
553 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
554 * or euid/egid (for hide_pid_min=2)?
556 static bool has_pid_permissions(struct pid_namespace *pid,
557 struct task_struct *task,
560 if (pid->hide_pid < hide_pid_min)
562 if (in_group_p(pid->pid_gid))
564 return ptrace_may_access(task, PTRACE_MODE_READ);
568 static int proc_pid_permission(struct inode *inode, int mask)
570 struct pid_namespace *pid = inode->i_sb->s_fs_info;
571 struct task_struct *task;
574 task = get_proc_task(inode);
577 has_perms = has_pid_permissions(pid, task, 1);
578 put_task_struct(task);
581 if (pid->hide_pid == 2) {
583 * Let's make getdents(), stat(), and open()
584 * consistent with each other. If a process
585 * may not stat() a file, it shouldn't be seen
593 return generic_permission(inode, mask);
598 static const struct inode_operations proc_def_inode_operations = {
599 .setattr = proc_setattr,
602 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
604 static ssize_t proc_info_read(struct file * file, char __user * buf,
605 size_t count, loff_t *ppos)
607 struct inode * inode = file_inode(file);
610 struct task_struct *task = get_proc_task(inode);
616 if (count > PROC_BLOCK_SIZE)
617 count = PROC_BLOCK_SIZE;
620 if (!(page = __get_free_page(GFP_TEMPORARY)))
623 length = PROC_I(inode)->op.proc_read(task, (char*)page);
626 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
629 put_task_struct(task);
634 static const struct file_operations proc_info_file_operations = {
635 .read = proc_info_read,
636 .llseek = generic_file_llseek,
639 static int proc_single_show(struct seq_file *m, void *v)
641 struct inode *inode = m->private;
642 struct pid_namespace *ns;
644 struct task_struct *task;
647 ns = inode->i_sb->s_fs_info;
648 pid = proc_pid(inode);
649 task = get_pid_task(pid, PIDTYPE_PID);
653 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
655 put_task_struct(task);
659 static int proc_single_open(struct inode *inode, struct file *filp)
661 return single_open(filp, proc_single_show, inode);
664 static const struct file_operations proc_single_file_operations = {
665 .open = proc_single_open,
668 .release = single_release,
671 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
673 struct task_struct *task = get_proc_task(file_inode(file));
674 struct mm_struct *mm;
679 mm = mm_access(task, mode);
680 put_task_struct(task);
686 /* ensure this mm_struct can't be freed */
687 atomic_inc(&mm->mm_count);
688 /* but do not pin its memory */
692 file->private_data = mm;
697 static int mem_open(struct inode *inode, struct file *file)
699 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
701 /* OK to pass negative loff_t, we can catch out-of-range */
702 file->f_mode |= FMODE_UNSIGNED_OFFSET;
707 static ssize_t mem_rw(struct file *file, char __user *buf,
708 size_t count, loff_t *ppos, int write)
710 struct mm_struct *mm = file->private_data;
711 unsigned long addr = *ppos;
718 page = (char *)__get_free_page(GFP_TEMPORARY);
723 if (!atomic_inc_not_zero(&mm->mm_users))
727 int this_len = min_t(int, count, PAGE_SIZE);
729 if (write && copy_from_user(page, buf, this_len)) {
734 this_len = access_remote_vm(mm, addr, page, this_len, write);
741 if (!write && copy_to_user(buf, page, this_len)) {
755 free_page((unsigned long) page);
759 static ssize_t mem_read(struct file *file, char __user *buf,
760 size_t count, loff_t *ppos)
762 return mem_rw(file, buf, count, ppos, 0);
765 static ssize_t mem_write(struct file *file, const char __user *buf,
766 size_t count, loff_t *ppos)
768 return mem_rw(file, (char __user*)buf, count, ppos, 1);
771 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
775 file->f_pos = offset;
778 file->f_pos += offset;
783 force_successful_syscall_return();
787 static int mem_release(struct inode *inode, struct file *file)
789 struct mm_struct *mm = file->private_data;
795 static const struct file_operations proc_mem_operations = {
800 .release = mem_release,
803 static int environ_open(struct inode *inode, struct file *file)
805 return __mem_open(inode, file, PTRACE_MODE_READ);
808 static ssize_t environ_read(struct file *file, char __user *buf,
809 size_t count, loff_t *ppos)
812 unsigned long src = *ppos;
814 struct mm_struct *mm = file->private_data;
819 page = (char *)__get_free_page(GFP_TEMPORARY);
824 if (!atomic_inc_not_zero(&mm->mm_users))
827 size_t this_len, max_len;
830 if (src >= (mm->env_end - mm->env_start))
833 this_len = mm->env_end - (mm->env_start + src);
835 max_len = min_t(size_t, PAGE_SIZE, count);
836 this_len = min(max_len, this_len);
838 retval = access_remote_vm(mm, (mm->env_start + src),
846 if (copy_to_user(buf, page, retval)) {
860 free_page((unsigned long) page);
864 static const struct file_operations proc_environ_operations = {
865 .open = environ_open,
866 .read = environ_read,
867 .llseek = generic_file_llseek,
868 .release = mem_release,
871 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
874 struct task_struct *task = get_proc_task(file_inode(file));
875 char buffer[PROC_NUMBUF];
876 int oom_adj = OOM_ADJUST_MIN;
882 if (lock_task_sighand(task, &flags)) {
883 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
884 oom_adj = OOM_ADJUST_MAX;
886 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
888 unlock_task_sighand(task, &flags);
890 put_task_struct(task);
891 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
892 return simple_read_from_buffer(buf, count, ppos, buffer, len);
895 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
896 size_t count, loff_t *ppos)
898 struct task_struct *task;
899 char buffer[PROC_NUMBUF];
904 memset(buffer, 0, sizeof(buffer));
905 if (count > sizeof(buffer) - 1)
906 count = sizeof(buffer) - 1;
907 if (copy_from_user(buffer, buf, count)) {
912 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
915 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
916 oom_adj != OOM_DISABLE) {
921 task = get_proc_task(file_inode(file));
933 if (!lock_task_sighand(task, &flags)) {
939 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
940 * value is always attainable.
942 if (oom_adj == OOM_ADJUST_MAX)
943 oom_adj = OOM_SCORE_ADJ_MAX;
945 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
947 if (oom_adj < task->signal->oom_score_adj &&
948 !capable(CAP_SYS_RESOURCE)) {
954 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
955 * /proc/pid/oom_score_adj instead.
957 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
958 current->comm, task_pid_nr(current), task_pid_nr(task),
961 task->signal->oom_score_adj = oom_adj;
962 trace_oom_score_adj_update(task);
964 unlock_task_sighand(task, &flags);
967 put_task_struct(task);
969 return err < 0 ? err : count;
972 static const struct file_operations proc_oom_adj_operations = {
973 .read = oom_adj_read,
974 .write = oom_adj_write,
975 .llseek = generic_file_llseek,
978 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
979 size_t count, loff_t *ppos)
981 struct task_struct *task = get_proc_task(file_inode(file));
982 char buffer[PROC_NUMBUF];
983 short oom_score_adj = OOM_SCORE_ADJ_MIN;
989 if (lock_task_sighand(task, &flags)) {
990 oom_score_adj = task->signal->oom_score_adj;
991 unlock_task_sighand(task, &flags);
993 put_task_struct(task);
994 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
995 return simple_read_from_buffer(buf, count, ppos, buffer, len);
998 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
999 size_t count, loff_t *ppos)
1001 struct task_struct *task;
1002 char buffer[PROC_NUMBUF];
1003 unsigned long flags;
1007 memset(buffer, 0, sizeof(buffer));
1008 if (count > sizeof(buffer) - 1)
1009 count = sizeof(buffer) - 1;
1010 if (copy_from_user(buffer, buf, count)) {
1015 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1018 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1019 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1024 task = get_proc_task(file_inode(file));
1036 if (!lock_task_sighand(task, &flags)) {
1041 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1042 !capable(CAP_SYS_RESOURCE)) {
1047 task->signal->oom_score_adj = (short)oom_score_adj;
1048 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1049 task->signal->oom_score_adj_min = (short)oom_score_adj;
1050 trace_oom_score_adj_update(task);
1053 unlock_task_sighand(task, &flags);
1056 put_task_struct(task);
1058 return err < 0 ? err : count;
1061 static const struct file_operations proc_oom_score_adj_operations = {
1062 .read = oom_score_adj_read,
1063 .write = oom_score_adj_write,
1064 .llseek = default_llseek,
1067 #ifdef CONFIG_AUDITSYSCALL
1068 #define TMPBUFLEN 21
1069 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1070 size_t count, loff_t *ppos)
1072 struct inode * inode = file_inode(file);
1073 struct task_struct *task = get_proc_task(inode);
1075 char tmpbuf[TMPBUFLEN];
1079 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1080 from_kuid(file->f_cred->user_ns,
1081 audit_get_loginuid(task)));
1082 put_task_struct(task);
1083 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1086 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1087 size_t count, loff_t *ppos)
1089 struct inode * inode = file_inode(file);
1096 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1102 if (count >= PAGE_SIZE)
1103 count = PAGE_SIZE - 1;
1106 /* No partial writes. */
1109 page = (char*)__get_free_page(GFP_TEMPORARY);
1113 if (copy_from_user(page, buf, count))
1117 loginuid = simple_strtoul(page, &tmp, 10);
1123 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1124 if (!uid_valid(kloginuid)) {
1129 length = audit_set_loginuid(kloginuid);
1130 if (likely(length == 0))
1134 free_page((unsigned long) page);
1138 static const struct file_operations proc_loginuid_operations = {
1139 .read = proc_loginuid_read,
1140 .write = proc_loginuid_write,
1141 .llseek = generic_file_llseek,
1144 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1145 size_t count, loff_t *ppos)
1147 struct inode * inode = file_inode(file);
1148 struct task_struct *task = get_proc_task(inode);
1150 char tmpbuf[TMPBUFLEN];
1154 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1155 audit_get_sessionid(task));
1156 put_task_struct(task);
1157 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1160 static const struct file_operations proc_sessionid_operations = {
1161 .read = proc_sessionid_read,
1162 .llseek = generic_file_llseek,
1166 #ifdef CONFIG_FAULT_INJECTION
1167 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1168 size_t count, loff_t *ppos)
1170 struct task_struct *task = get_proc_task(file_inode(file));
1171 char buffer[PROC_NUMBUF];
1177 make_it_fail = task->make_it_fail;
1178 put_task_struct(task);
1180 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1182 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1185 static ssize_t proc_fault_inject_write(struct file * file,
1186 const char __user * buf, size_t count, loff_t *ppos)
1188 struct task_struct *task;
1189 char buffer[PROC_NUMBUF], *end;
1192 if (!capable(CAP_SYS_RESOURCE))
1194 memset(buffer, 0, sizeof(buffer));
1195 if (count > sizeof(buffer) - 1)
1196 count = sizeof(buffer) - 1;
1197 if (copy_from_user(buffer, buf, count))
1199 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1202 task = get_proc_task(file_inode(file));
1205 task->make_it_fail = make_it_fail;
1206 put_task_struct(task);
1211 static const struct file_operations proc_fault_inject_operations = {
1212 .read = proc_fault_inject_read,
1213 .write = proc_fault_inject_write,
1214 .llseek = generic_file_llseek,
1219 #ifdef CONFIG_SCHED_DEBUG
1221 * Print out various scheduling related per-task fields:
1223 static int sched_show(struct seq_file *m, void *v)
1225 struct inode *inode = m->private;
1226 struct task_struct *p;
1228 p = get_proc_task(inode);
1231 proc_sched_show_task(p, m);
1239 sched_write(struct file *file, const char __user *buf,
1240 size_t count, loff_t *offset)
1242 struct inode *inode = file_inode(file);
1243 struct task_struct *p;
1245 p = get_proc_task(inode);
1248 proc_sched_set_task(p);
1255 static int sched_open(struct inode *inode, struct file *filp)
1257 return single_open(filp, sched_show, inode);
1260 static const struct file_operations proc_pid_sched_operations = {
1263 .write = sched_write,
1264 .llseek = seq_lseek,
1265 .release = single_release,
1270 #ifdef CONFIG_SCHED_AUTOGROUP
1272 * Print out autogroup related information:
1274 static int sched_autogroup_show(struct seq_file *m, void *v)
1276 struct inode *inode = m->private;
1277 struct task_struct *p;
1279 p = get_proc_task(inode);
1282 proc_sched_autogroup_show_task(p, m);
1290 sched_autogroup_write(struct file *file, const char __user *buf,
1291 size_t count, loff_t *offset)
1293 struct inode *inode = file_inode(file);
1294 struct task_struct *p;
1295 char buffer[PROC_NUMBUF];
1299 memset(buffer, 0, sizeof(buffer));
1300 if (count > sizeof(buffer) - 1)
1301 count = sizeof(buffer) - 1;
1302 if (copy_from_user(buffer, buf, count))
1305 err = kstrtoint(strstrip(buffer), 0, &nice);
1309 p = get_proc_task(inode);
1313 err = proc_sched_autogroup_set_nice(p, nice);
1322 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1326 ret = single_open(filp, sched_autogroup_show, NULL);
1328 struct seq_file *m = filp->private_data;
1335 static const struct file_operations proc_pid_sched_autogroup_operations = {
1336 .open = sched_autogroup_open,
1338 .write = sched_autogroup_write,
1339 .llseek = seq_lseek,
1340 .release = single_release,
1343 #endif /* CONFIG_SCHED_AUTOGROUP */
1345 static ssize_t comm_write(struct file *file, const char __user *buf,
1346 size_t count, loff_t *offset)
1348 struct inode *inode = file_inode(file);
1349 struct task_struct *p;
1350 char buffer[TASK_COMM_LEN];
1352 memset(buffer, 0, sizeof(buffer));
1353 if (count > sizeof(buffer) - 1)
1354 count = sizeof(buffer) - 1;
1355 if (copy_from_user(buffer, buf, count))
1358 p = get_proc_task(inode);
1362 if (same_thread_group(current, p))
1363 set_task_comm(p, buffer);
1372 static int comm_show(struct seq_file *m, void *v)
1374 struct inode *inode = m->private;
1375 struct task_struct *p;
1377 p = get_proc_task(inode);
1382 seq_printf(m, "%s\n", p->comm);
1390 static int comm_open(struct inode *inode, struct file *filp)
1392 return single_open(filp, comm_show, inode);
1395 static const struct file_operations proc_pid_set_comm_operations = {
1398 .write = comm_write,
1399 .llseek = seq_lseek,
1400 .release = single_release,
1403 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1405 struct task_struct *task;
1406 struct mm_struct *mm;
1407 struct file *exe_file;
1409 task = get_proc_task(dentry->d_inode);
1412 mm = get_task_mm(task);
1413 put_task_struct(task);
1416 exe_file = get_mm_exe_file(mm);
1419 *exe_path = exe_file->f_path;
1420 path_get(&exe_file->f_path);
1427 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1429 struct inode *inode = dentry->d_inode;
1431 int error = -EACCES;
1433 /* Are we allowed to snoop on the tasks file descriptors? */
1434 if (!proc_fd_access_allowed(inode))
1437 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1441 nd_jump_link(nd, &path);
1444 return ERR_PTR(error);
1447 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1449 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1456 pathname = d_path(path, tmp, PAGE_SIZE);
1457 len = PTR_ERR(pathname);
1458 if (IS_ERR(pathname))
1460 len = tmp + PAGE_SIZE - 1 - pathname;
1464 if (copy_to_user(buffer, pathname, len))
1467 free_page((unsigned long)tmp);
1471 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1473 int error = -EACCES;
1474 struct inode *inode = dentry->d_inode;
1477 /* Are we allowed to snoop on the tasks file descriptors? */
1478 if (!proc_fd_access_allowed(inode))
1481 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1485 error = do_proc_readlink(&path, buffer, buflen);
1491 const struct inode_operations proc_pid_link_inode_operations = {
1492 .readlink = proc_pid_readlink,
1493 .follow_link = proc_pid_follow_link,
1494 .setattr = proc_setattr,
1498 /* building an inode */
1500 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1502 struct inode * inode;
1503 struct proc_inode *ei;
1504 const struct cred *cred;
1506 /* We need a new inode */
1508 inode = new_inode(sb);
1514 inode->i_ino = get_next_ino();
1515 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1516 inode->i_op = &proc_def_inode_operations;
1519 * grab the reference to task.
1521 ei->pid = get_task_pid(task, PIDTYPE_PID);
1525 if (task_dumpable(task)) {
1527 cred = __task_cred(task);
1528 inode->i_uid = cred->euid;
1529 inode->i_gid = cred->egid;
1532 security_task_to_inode(task, inode);
1542 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1544 struct inode *inode = dentry->d_inode;
1545 struct task_struct *task;
1546 const struct cred *cred;
1547 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1549 generic_fillattr(inode, stat);
1552 stat->uid = GLOBAL_ROOT_UID;
1553 stat->gid = GLOBAL_ROOT_GID;
1554 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1556 if (!has_pid_permissions(pid, task, 2)) {
1559 * This doesn't prevent learning whether PID exists,
1560 * it only makes getattr() consistent with readdir().
1564 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1565 task_dumpable(task)) {
1566 cred = __task_cred(task);
1567 stat->uid = cred->euid;
1568 stat->gid = cred->egid;
1578 * Exceptional case: normally we are not allowed to unhash a busy
1579 * directory. In this case, however, we can do it - no aliasing problems
1580 * due to the way we treat inodes.
1582 * Rewrite the inode's ownerships here because the owning task may have
1583 * performed a setuid(), etc.
1585 * Before the /proc/pid/status file was created the only way to read
1586 * the effective uid of a /process was to stat /proc/pid. Reading
1587 * /proc/pid/status is slow enough that procps and other packages
1588 * kept stating /proc/pid. To keep the rules in /proc simple I have
1589 * made this apply to all per process world readable and executable
1592 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1594 struct inode *inode;
1595 struct task_struct *task;
1596 const struct cred *cred;
1598 if (flags & LOOKUP_RCU)
1601 inode = dentry->d_inode;
1602 task = get_proc_task(inode);
1605 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1606 task_dumpable(task)) {
1608 cred = __task_cred(task);
1609 inode->i_uid = cred->euid;
1610 inode->i_gid = cred->egid;
1613 inode->i_uid = GLOBAL_ROOT_UID;
1614 inode->i_gid = GLOBAL_ROOT_GID;
1616 inode->i_mode &= ~(S_ISUID | S_ISGID);
1617 security_task_to_inode(task, inode);
1618 put_task_struct(task);
1625 const struct dentry_operations pid_dentry_operations =
1627 .d_revalidate = pid_revalidate,
1628 .d_delete = pid_delete_dentry,
1634 * Fill a directory entry.
1636 * If possible create the dcache entry and derive our inode number and
1637 * file type from dcache entry.
1639 * Since all of the proc inode numbers are dynamically generated, the inode
1640 * numbers do not exist until the inode is cache. This means creating the
1641 * the dcache entry in readdir is necessary to keep the inode numbers
1642 * reported by readdir in sync with the inode numbers reported
1645 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1646 const char *name, int len,
1647 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1649 struct dentry *child, *dir = filp->f_path.dentry;
1650 struct inode *inode;
1653 unsigned type = DT_UNKNOWN;
1657 qname.hash = full_name_hash(name, len);
1659 child = d_lookup(dir, &qname);
1662 new = d_alloc(dir, &qname);
1664 child = instantiate(dir->d_inode, new, task, ptr);
1671 if (!child || IS_ERR(child) || !child->d_inode)
1672 goto end_instantiate;
1673 inode = child->d_inode;
1676 type = inode->i_mode >> 12;
1681 ino = find_inode_number(dir, &qname);
1684 return filldir(dirent, name, len, filp->f_pos, ino, type);
1687 #ifdef CONFIG_CHECKPOINT_RESTORE
1690 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1691 * which represent vma start and end addresses.
1693 static int dname_to_vma_addr(struct dentry *dentry,
1694 unsigned long *start, unsigned long *end)
1696 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1702 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1704 unsigned long vm_start, vm_end;
1705 bool exact_vma_exists = false;
1706 struct mm_struct *mm = NULL;
1707 struct task_struct *task;
1708 const struct cred *cred;
1709 struct inode *inode;
1712 if (flags & LOOKUP_RCU)
1715 if (!capable(CAP_SYS_ADMIN)) {
1720 inode = dentry->d_inode;
1721 task = get_proc_task(inode);
1725 mm = mm_access(task, PTRACE_MODE_READ);
1726 if (IS_ERR_OR_NULL(mm))
1729 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1730 down_read(&mm->mmap_sem);
1731 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1732 up_read(&mm->mmap_sem);
1737 if (exact_vma_exists) {
1738 if (task_dumpable(task)) {
1740 cred = __task_cred(task);
1741 inode->i_uid = cred->euid;
1742 inode->i_gid = cred->egid;
1745 inode->i_uid = GLOBAL_ROOT_UID;
1746 inode->i_gid = GLOBAL_ROOT_GID;
1748 security_task_to_inode(task, inode);
1753 put_task_struct(task);
1762 static const struct dentry_operations tid_map_files_dentry_operations = {
1763 .d_revalidate = map_files_d_revalidate,
1764 .d_delete = pid_delete_dentry,
1767 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1769 unsigned long vm_start, vm_end;
1770 struct vm_area_struct *vma;
1771 struct task_struct *task;
1772 struct mm_struct *mm;
1776 task = get_proc_task(dentry->d_inode);
1780 mm = get_task_mm(task);
1781 put_task_struct(task);
1785 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1789 down_read(&mm->mmap_sem);
1790 vma = find_exact_vma(mm, vm_start, vm_end);
1791 if (vma && vma->vm_file) {
1792 *path = vma->vm_file->f_path;
1796 up_read(&mm->mmap_sem);
1804 struct map_files_info {
1807 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1810 static struct dentry *
1811 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1812 struct task_struct *task, const void *ptr)
1814 fmode_t mode = (fmode_t)(unsigned long)ptr;
1815 struct proc_inode *ei;
1816 struct inode *inode;
1818 inode = proc_pid_make_inode(dir->i_sb, task);
1820 return ERR_PTR(-ENOENT);
1823 ei->op.proc_get_link = proc_map_files_get_link;
1825 inode->i_op = &proc_pid_link_inode_operations;
1827 inode->i_mode = S_IFLNK;
1829 if (mode & FMODE_READ)
1830 inode->i_mode |= S_IRUSR;
1831 if (mode & FMODE_WRITE)
1832 inode->i_mode |= S_IWUSR;
1834 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1835 d_add(dentry, inode);
1840 static struct dentry *proc_map_files_lookup(struct inode *dir,
1841 struct dentry *dentry, unsigned int flags)
1843 unsigned long vm_start, vm_end;
1844 struct vm_area_struct *vma;
1845 struct task_struct *task;
1846 struct dentry *result;
1847 struct mm_struct *mm;
1849 result = ERR_PTR(-EPERM);
1850 if (!capable(CAP_SYS_ADMIN))
1853 result = ERR_PTR(-ENOENT);
1854 task = get_proc_task(dir);
1858 result = ERR_PTR(-EACCES);
1859 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1862 result = ERR_PTR(-ENOENT);
1863 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1866 mm = get_task_mm(task);
1870 down_read(&mm->mmap_sem);
1871 vma = find_exact_vma(mm, vm_start, vm_end);
1876 result = proc_map_files_instantiate(dir, dentry, task,
1877 (void *)(unsigned long)vma->vm_file->f_mode);
1880 up_read(&mm->mmap_sem);
1883 put_task_struct(task);
1888 static const struct inode_operations proc_map_files_inode_operations = {
1889 .lookup = proc_map_files_lookup,
1890 .permission = proc_fd_permission,
1891 .setattr = proc_setattr,
1895 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1897 struct dentry *dentry = filp->f_path.dentry;
1898 struct inode *inode = dentry->d_inode;
1899 struct vm_area_struct *vma;
1900 struct task_struct *task;
1901 struct mm_struct *mm;
1906 if (!capable(CAP_SYS_ADMIN))
1910 task = get_proc_task(inode);
1915 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1919 switch (filp->f_pos) {
1922 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
1926 ino = parent_ino(dentry);
1927 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1932 unsigned long nr_files, pos, i;
1933 struct flex_array *fa = NULL;
1934 struct map_files_info info;
1935 struct map_files_info *p;
1937 mm = get_task_mm(task);
1940 down_read(&mm->mmap_sem);
1945 * We need two passes here:
1947 * 1) Collect vmas of mapped files with mmap_sem taken
1948 * 2) Release mmap_sem and instantiate entries
1950 * otherwise we get lockdep complained, since filldir()
1951 * routine might require mmap_sem taken in might_fault().
1954 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1955 if (vma->vm_file && ++pos > filp->f_pos)
1960 fa = flex_array_alloc(sizeof(info), nr_files,
1962 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1966 flex_array_free(fa);
1967 up_read(&mm->mmap_sem);
1971 for (i = 0, vma = mm->mmap, pos = 2; vma;
1972 vma = vma->vm_next) {
1975 if (++pos <= filp->f_pos)
1978 info.mode = vma->vm_file->f_mode;
1979 info.len = snprintf(info.name,
1980 sizeof(info.name), "%lx-%lx",
1981 vma->vm_start, vma->vm_end);
1982 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1986 up_read(&mm->mmap_sem);
1988 for (i = 0; i < nr_files; i++) {
1989 p = flex_array_get(fa, i);
1990 ret = proc_fill_cache(filp, dirent, filldir,
1992 proc_map_files_instantiate,
1994 (void *)(unsigned long)p->mode);
2000 flex_array_free(fa);
2006 put_task_struct(task);
2011 static const struct file_operations proc_map_files_operations = {
2012 .read = generic_read_dir,
2013 .readdir = proc_map_files_readdir,
2014 .llseek = default_llseek,
2017 struct timers_private {
2019 struct task_struct *task;
2020 struct sighand_struct *sighand;
2021 struct pid_namespace *ns;
2022 unsigned long flags;
2025 static void *timers_start(struct seq_file *m, loff_t *pos)
2027 struct timers_private *tp = m->private;
2029 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2031 return ERR_PTR(-ESRCH);
2033 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2035 return ERR_PTR(-ESRCH);
2037 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2040 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2042 struct timers_private *tp = m->private;
2043 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2046 static void timers_stop(struct seq_file *m, void *v)
2048 struct timers_private *tp = m->private;
2051 unlock_task_sighand(tp->task, &tp->flags);
2056 put_task_struct(tp->task);
2061 static int show_timer(struct seq_file *m, void *v)
2063 struct k_itimer *timer;
2064 struct timers_private *tp = m->private;
2066 static char *nstr[] = {
2067 [SIGEV_SIGNAL] = "signal",
2068 [SIGEV_NONE] = "none",
2069 [SIGEV_THREAD] = "thread",
2072 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2073 notify = timer->it_sigev_notify;
2075 seq_printf(m, "ID: %d\n", timer->it_id);
2076 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2077 timer->sigq->info.si_value.sival_ptr);
2078 seq_printf(m, "notify: %s/%s.%d\n",
2079 nstr[notify & ~SIGEV_THREAD_ID],
2080 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2081 pid_nr_ns(timer->it_pid, tp->ns));
2086 static const struct seq_operations proc_timers_seq_ops = {
2087 .start = timers_start,
2088 .next = timers_next,
2089 .stop = timers_stop,
2093 static int proc_timers_open(struct inode *inode, struct file *file)
2095 struct timers_private *tp;
2097 tp = __seq_open_private(file, &proc_timers_seq_ops,
2098 sizeof(struct timers_private));
2102 tp->pid = proc_pid(inode);
2103 tp->ns = inode->i_sb->s_fs_info;
2107 static const struct file_operations proc_timers_operations = {
2108 .open = proc_timers_open,
2110 .llseek = seq_lseek,
2111 .release = seq_release_private,
2113 #endif /* CONFIG_CHECKPOINT_RESTORE */
2115 static struct dentry *proc_pident_instantiate(struct inode *dir,
2116 struct dentry *dentry, struct task_struct *task, const void *ptr)
2118 const struct pid_entry *p = ptr;
2119 struct inode *inode;
2120 struct proc_inode *ei;
2121 struct dentry *error = ERR_PTR(-ENOENT);
2123 inode = proc_pid_make_inode(dir->i_sb, task);
2128 inode->i_mode = p->mode;
2129 if (S_ISDIR(inode->i_mode))
2130 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2132 inode->i_op = p->iop;
2134 inode->i_fop = p->fop;
2136 d_set_d_op(dentry, &pid_dentry_operations);
2137 d_add(dentry, inode);
2138 /* Close the race of the process dying before we return the dentry */
2139 if (pid_revalidate(dentry, 0))
2145 static struct dentry *proc_pident_lookup(struct inode *dir,
2146 struct dentry *dentry,
2147 const struct pid_entry *ents,
2150 struct dentry *error;
2151 struct task_struct *task = get_proc_task(dir);
2152 const struct pid_entry *p, *last;
2154 error = ERR_PTR(-ENOENT);
2160 * Yes, it does not scale. And it should not. Don't add
2161 * new entries into /proc/<tgid>/ without very good reasons.
2163 last = &ents[nents - 1];
2164 for (p = ents; p <= last; p++) {
2165 if (p->len != dentry->d_name.len)
2167 if (!memcmp(dentry->d_name.name, p->name, p->len))
2173 error = proc_pident_instantiate(dir, dentry, task, p);
2175 put_task_struct(task);
2180 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2181 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2183 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2184 proc_pident_instantiate, task, p);
2187 static int proc_pident_readdir(struct file *filp,
2188 void *dirent, filldir_t filldir,
2189 const struct pid_entry *ents, unsigned int nents)
2192 struct dentry *dentry = filp->f_path.dentry;
2193 struct inode *inode = dentry->d_inode;
2194 struct task_struct *task = get_proc_task(inode);
2195 const struct pid_entry *p, *last;
2208 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2214 ino = parent_ino(dentry);
2215 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2227 last = &ents[nents - 1];
2229 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2238 put_task_struct(task);
2243 #ifdef CONFIG_SECURITY
2244 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2245 size_t count, loff_t *ppos)
2247 struct inode * inode = file_inode(file);
2250 struct task_struct *task = get_proc_task(inode);
2255 length = security_getprocattr(task,
2256 (char*)file->f_path.dentry->d_name.name,
2258 put_task_struct(task);
2260 length = simple_read_from_buffer(buf, count, ppos, p, length);
2265 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2266 size_t count, loff_t *ppos)
2268 struct inode * inode = file_inode(file);
2271 struct task_struct *task = get_proc_task(inode);
2276 if (count > PAGE_SIZE)
2279 /* No partial writes. */
2285 page = (char*)__get_free_page(GFP_TEMPORARY);
2290 if (copy_from_user(page, buf, count))
2293 /* Guard against adverse ptrace interaction */
2294 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2298 length = security_setprocattr(task,
2299 (char*)file->f_path.dentry->d_name.name,
2300 (void*)page, count);
2301 mutex_unlock(&task->signal->cred_guard_mutex);
2303 free_page((unsigned long) page);
2305 put_task_struct(task);
2310 static const struct file_operations proc_pid_attr_operations = {
2311 .read = proc_pid_attr_read,
2312 .write = proc_pid_attr_write,
2313 .llseek = generic_file_llseek,
2316 static const struct pid_entry attr_dir_stuff[] = {
2317 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2318 REG("prev", S_IRUGO, proc_pid_attr_operations),
2319 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2320 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2321 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2322 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2325 static int proc_attr_dir_readdir(struct file * filp,
2326 void * dirent, filldir_t filldir)
2328 return proc_pident_readdir(filp,dirent,filldir,
2329 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2332 static const struct file_operations proc_attr_dir_operations = {
2333 .read = generic_read_dir,
2334 .readdir = proc_attr_dir_readdir,
2335 .llseek = default_llseek,
2338 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2339 struct dentry *dentry, unsigned int flags)
2341 return proc_pident_lookup(dir, dentry,
2342 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2345 static const struct inode_operations proc_attr_dir_inode_operations = {
2346 .lookup = proc_attr_dir_lookup,
2347 .getattr = pid_getattr,
2348 .setattr = proc_setattr,
2353 #ifdef CONFIG_ELF_CORE
2354 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2355 size_t count, loff_t *ppos)
2357 struct task_struct *task = get_proc_task(file_inode(file));
2358 struct mm_struct *mm;
2359 char buffer[PROC_NUMBUF];
2367 mm = get_task_mm(task);
2369 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2370 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2371 MMF_DUMP_FILTER_SHIFT));
2373 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2376 put_task_struct(task);
2381 static ssize_t proc_coredump_filter_write(struct file *file,
2382 const char __user *buf,
2386 struct task_struct *task;
2387 struct mm_struct *mm;
2388 char buffer[PROC_NUMBUF], *end;
2395 memset(buffer, 0, sizeof(buffer));
2396 if (count > sizeof(buffer) - 1)
2397 count = sizeof(buffer) - 1;
2398 if (copy_from_user(buffer, buf, count))
2402 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2405 if (end - buffer == 0)
2409 task = get_proc_task(file_inode(file));
2414 mm = get_task_mm(task);
2418 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2420 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2422 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2427 put_task_struct(task);
2432 static const struct file_operations proc_coredump_filter_operations = {
2433 .read = proc_coredump_filter_read,
2434 .write = proc_coredump_filter_write,
2435 .llseek = generic_file_llseek,
2439 #ifdef CONFIG_TASK_IO_ACCOUNTING
2440 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2442 struct task_io_accounting acct = task->ioac;
2443 unsigned long flags;
2446 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2450 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2455 if (whole && lock_task_sighand(task, &flags)) {
2456 struct task_struct *t = task;
2458 task_io_accounting_add(&acct, &task->signal->ioac);
2459 while_each_thread(task, t)
2460 task_io_accounting_add(&acct, &t->ioac);
2462 unlock_task_sighand(task, &flags);
2464 result = sprintf(buffer,
2469 "read_bytes: %llu\n"
2470 "write_bytes: %llu\n"
2471 "cancelled_write_bytes: %llu\n",
2472 (unsigned long long)acct.rchar,
2473 (unsigned long long)acct.wchar,
2474 (unsigned long long)acct.syscr,
2475 (unsigned long long)acct.syscw,
2476 (unsigned long long)acct.read_bytes,
2477 (unsigned long long)acct.write_bytes,
2478 (unsigned long long)acct.cancelled_write_bytes);
2480 mutex_unlock(&task->signal->cred_guard_mutex);
2484 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2486 return do_io_accounting(task, buffer, 0);
2489 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2491 return do_io_accounting(task, buffer, 1);
2493 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2495 #ifdef CONFIG_USER_NS
2496 static int proc_id_map_open(struct inode *inode, struct file *file,
2497 struct seq_operations *seq_ops)
2499 struct user_namespace *ns = NULL;
2500 struct task_struct *task;
2501 struct seq_file *seq;
2504 task = get_proc_task(inode);
2507 ns = get_user_ns(task_cred_xxx(task, user_ns));
2509 put_task_struct(task);
2514 ret = seq_open(file, seq_ops);
2518 seq = file->private_data;
2528 static int proc_id_map_release(struct inode *inode, struct file *file)
2530 struct seq_file *seq = file->private_data;
2531 struct user_namespace *ns = seq->private;
2533 return seq_release(inode, file);
2536 static int proc_uid_map_open(struct inode *inode, struct file *file)
2538 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2541 static int proc_gid_map_open(struct inode *inode, struct file *file)
2543 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2546 static int proc_projid_map_open(struct inode *inode, struct file *file)
2548 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2551 static const struct file_operations proc_uid_map_operations = {
2552 .open = proc_uid_map_open,
2553 .write = proc_uid_map_write,
2555 .llseek = seq_lseek,
2556 .release = proc_id_map_release,
2559 static const struct file_operations proc_gid_map_operations = {
2560 .open = proc_gid_map_open,
2561 .write = proc_gid_map_write,
2563 .llseek = seq_lseek,
2564 .release = proc_id_map_release,
2567 static const struct file_operations proc_projid_map_operations = {
2568 .open = proc_projid_map_open,
2569 .write = proc_projid_map_write,
2571 .llseek = seq_lseek,
2572 .release = proc_id_map_release,
2574 #endif /* CONFIG_USER_NS */
2576 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2577 struct pid *pid, struct task_struct *task)
2579 int err = lock_trace(task);
2581 seq_printf(m, "%08x\n", task->personality);
2590 static const struct file_operations proc_task_operations;
2591 static const struct inode_operations proc_task_inode_operations;
2593 static const struct pid_entry tgid_base_stuff[] = {
2594 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2595 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2596 #ifdef CONFIG_CHECKPOINT_RESTORE
2597 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2599 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2600 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2602 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2604 REG("environ", S_IRUSR, proc_environ_operations),
2605 INF("auxv", S_IRUSR, proc_pid_auxv),
2606 ONE("status", S_IRUGO, proc_pid_status),
2607 ONE("personality", S_IRUGO, proc_pid_personality),
2608 INF("limits", S_IRUGO, proc_pid_limits),
2609 #ifdef CONFIG_SCHED_DEBUG
2610 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2612 #ifdef CONFIG_SCHED_AUTOGROUP
2613 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2615 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2616 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2617 INF("syscall", S_IRUGO, proc_pid_syscall),
2619 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2620 ONE("stat", S_IRUGO, proc_tgid_stat),
2621 ONE("statm", S_IRUGO, proc_pid_statm),
2622 REG("maps", S_IRUGO, proc_pid_maps_operations),
2624 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2626 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2627 LNK("cwd", proc_cwd_link),
2628 LNK("root", proc_root_link),
2629 LNK("exe", proc_exe_link),
2630 REG("mounts", S_IRUGO, proc_mounts_operations),
2631 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2632 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2633 #ifdef CONFIG_PROC_PAGE_MONITOR
2634 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2635 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2636 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2638 #ifdef CONFIG_SECURITY
2639 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2641 #ifdef CONFIG_KALLSYMS
2642 INF("wchan", S_IRUGO, proc_pid_wchan),
2644 #ifdef CONFIG_STACKTRACE
2645 ONE("stack", S_IRUGO, proc_pid_stack),
2647 #ifdef CONFIG_SCHEDSTATS
2648 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2650 #ifdef CONFIG_LATENCYTOP
2651 REG("latency", S_IRUGO, proc_lstats_operations),
2653 #ifdef CONFIG_PROC_PID_CPUSET
2654 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2656 #ifdef CONFIG_CGROUPS
2657 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2659 INF("oom_score", S_IRUGO, proc_oom_score),
2660 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2661 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2662 #ifdef CONFIG_AUDITSYSCALL
2663 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2664 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2666 #ifdef CONFIG_FAULT_INJECTION
2667 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2669 #ifdef CONFIG_ELF_CORE
2670 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2672 #ifdef CONFIG_TASK_IO_ACCOUNTING
2673 INF("io", S_IRUSR, proc_tgid_io_accounting),
2675 #ifdef CONFIG_HARDWALL
2676 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2678 #ifdef CONFIG_USER_NS
2679 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2680 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2681 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2683 #ifdef CONFIG_CHECKPOINT_RESTORE
2684 REG("timers", S_IRUGO, proc_timers_operations),
2688 static int proc_tgid_base_readdir(struct file * filp,
2689 void * dirent, filldir_t filldir)
2691 return proc_pident_readdir(filp,dirent,filldir,
2692 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2695 static const struct file_operations proc_tgid_base_operations = {
2696 .read = generic_read_dir,
2697 .readdir = proc_tgid_base_readdir,
2698 .llseek = default_llseek,
2701 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2703 return proc_pident_lookup(dir, dentry,
2704 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2707 static const struct inode_operations proc_tgid_base_inode_operations = {
2708 .lookup = proc_tgid_base_lookup,
2709 .getattr = pid_getattr,
2710 .setattr = proc_setattr,
2711 .permission = proc_pid_permission,
2714 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2716 struct dentry *dentry, *leader, *dir;
2717 char buf[PROC_NUMBUF];
2721 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2722 /* no ->d_hash() rejects on procfs */
2723 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2725 shrink_dcache_parent(dentry);
2731 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2732 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2737 name.len = strlen(name.name);
2738 dir = d_hash_and_lookup(leader, &name);
2740 goto out_put_leader;
2743 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2744 dentry = d_hash_and_lookup(dir, &name);
2746 shrink_dcache_parent(dentry);
2759 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2760 * @task: task that should be flushed.
2762 * When flushing dentries from proc, one needs to flush them from global
2763 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2764 * in. This call is supposed to do all of this job.
2766 * Looks in the dcache for
2768 * /proc/@tgid/task/@pid
2769 * if either directory is present flushes it and all of it'ts children
2772 * It is safe and reasonable to cache /proc entries for a task until
2773 * that task exits. After that they just clog up the dcache with
2774 * useless entries, possibly causing useful dcache entries to be
2775 * flushed instead. This routine is proved to flush those useless
2776 * dcache entries at process exit time.
2778 * NOTE: This routine is just an optimization so it does not guarantee
2779 * that no dcache entries will exist at process exit time it
2780 * just makes it very unlikely that any will persist.
2783 void proc_flush_task(struct task_struct *task)
2786 struct pid *pid, *tgid;
2789 pid = task_pid(task);
2790 tgid = task_tgid(task);
2792 for (i = 0; i <= pid->level; i++) {
2793 upid = &pid->numbers[i];
2794 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2795 tgid->numbers[i].nr);
2799 static struct dentry *proc_pid_instantiate(struct inode *dir,
2800 struct dentry * dentry,
2801 struct task_struct *task, const void *ptr)
2803 struct dentry *error = ERR_PTR(-ENOENT);
2804 struct inode *inode;
2806 inode = proc_pid_make_inode(dir->i_sb, task);
2810 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2811 inode->i_op = &proc_tgid_base_inode_operations;
2812 inode->i_fop = &proc_tgid_base_operations;
2813 inode->i_flags|=S_IMMUTABLE;
2815 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2816 ARRAY_SIZE(tgid_base_stuff)));
2818 d_set_d_op(dentry, &pid_dentry_operations);
2820 d_add(dentry, inode);
2821 /* Close the race of the process dying before we return the dentry */
2822 if (pid_revalidate(dentry, 0))
2828 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2830 struct dentry *result = NULL;
2831 struct task_struct *task;
2833 struct pid_namespace *ns;
2835 tgid = name_to_int(dentry);
2839 ns = dentry->d_sb->s_fs_info;
2841 task = find_task_by_pid_ns(tgid, ns);
2843 get_task_struct(task);
2848 result = proc_pid_instantiate(dir, dentry, task, NULL);
2849 put_task_struct(task);
2855 * Find the first task with tgid >= tgid
2860 struct task_struct *task;
2862 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2867 put_task_struct(iter.task);
2871 pid = find_ge_pid(iter.tgid, ns);
2873 iter.tgid = pid_nr_ns(pid, ns);
2874 iter.task = pid_task(pid, PIDTYPE_PID);
2875 /* What we to know is if the pid we have find is the
2876 * pid of a thread_group_leader. Testing for task
2877 * being a thread_group_leader is the obvious thing
2878 * todo but there is a window when it fails, due to
2879 * the pid transfer logic in de_thread.
2881 * So we perform the straight forward test of seeing
2882 * if the pid we have found is the pid of a thread
2883 * group leader, and don't worry if the task we have
2884 * found doesn't happen to be a thread group leader.
2885 * As we don't care in the case of readdir.
2887 if (!iter.task || !has_group_leader_pid(iter.task)) {
2891 get_task_struct(iter.task);
2897 #define TGID_OFFSET (FIRST_PROCESS_ENTRY)
2899 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2900 struct tgid_iter iter)
2902 char name[PROC_NUMBUF];
2903 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2904 return proc_fill_cache(filp, dirent, filldir, name, len,
2905 proc_pid_instantiate, iter.task, NULL);
2908 static int fake_filldir(void *buf, const char *name, int namelen,
2909 loff_t offset, u64 ino, unsigned d_type)
2914 /* for the /proc/ directory itself, after non-process stuff has been done */
2915 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2917 struct tgid_iter iter;
2918 struct pid_namespace *ns;
2919 filldir_t __filldir;
2921 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
2924 ns = filp->f_dentry->d_sb->s_fs_info;
2926 iter.tgid = filp->f_pos - TGID_OFFSET;
2927 for (iter = next_tgid(ns, iter);
2929 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2930 if (has_pid_permissions(ns, iter.task, 2))
2931 __filldir = filldir;
2933 __filldir = fake_filldir;
2935 filp->f_pos = iter.tgid + TGID_OFFSET;
2936 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
2937 put_task_struct(iter.task);
2941 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2949 static const struct pid_entry tid_base_stuff[] = {
2950 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2951 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2952 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2953 REG("environ", S_IRUSR, proc_environ_operations),
2954 INF("auxv", S_IRUSR, proc_pid_auxv),
2955 ONE("status", S_IRUGO, proc_pid_status),
2956 ONE("personality", S_IRUGO, proc_pid_personality),
2957 INF("limits", S_IRUGO, proc_pid_limits),
2958 #ifdef CONFIG_SCHED_DEBUG
2959 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2961 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2962 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2963 INF("syscall", S_IRUGO, proc_pid_syscall),
2965 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2966 ONE("stat", S_IRUGO, proc_tid_stat),
2967 ONE("statm", S_IRUGO, proc_pid_statm),
2968 REG("maps", S_IRUGO, proc_tid_maps_operations),
2969 #ifdef CONFIG_CHECKPOINT_RESTORE
2970 REG("children", S_IRUGO, proc_tid_children_operations),
2973 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2975 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2976 LNK("cwd", proc_cwd_link),
2977 LNK("root", proc_root_link),
2978 LNK("exe", proc_exe_link),
2979 REG("mounts", S_IRUGO, proc_mounts_operations),
2980 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2981 #ifdef CONFIG_PROC_PAGE_MONITOR
2982 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2983 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2984 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2986 #ifdef CONFIG_SECURITY
2987 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2989 #ifdef CONFIG_KALLSYMS
2990 INF("wchan", S_IRUGO, proc_pid_wchan),
2992 #ifdef CONFIG_STACKTRACE
2993 ONE("stack", S_IRUGO, proc_pid_stack),
2995 #ifdef CONFIG_SCHEDSTATS
2996 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2998 #ifdef CONFIG_LATENCYTOP
2999 REG("latency", S_IRUGO, proc_lstats_operations),
3001 #ifdef CONFIG_PROC_PID_CPUSET
3002 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3004 #ifdef CONFIG_CGROUPS
3005 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3007 INF("oom_score", S_IRUGO, proc_oom_score),
3008 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
3009 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3010 #ifdef CONFIG_AUDITSYSCALL
3011 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3012 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3014 #ifdef CONFIG_FAULT_INJECTION
3015 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3017 #ifdef CONFIG_TASK_IO_ACCOUNTING
3018 INF("io", S_IRUSR, proc_tid_io_accounting),
3020 #ifdef CONFIG_HARDWALL
3021 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3023 #ifdef CONFIG_USER_NS
3024 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3025 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
3026 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
3030 static int proc_tid_base_readdir(struct file * filp,
3031 void * dirent, filldir_t filldir)
3033 return proc_pident_readdir(filp,dirent,filldir,
3034 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3037 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3039 return proc_pident_lookup(dir, dentry,
3040 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3043 static const struct file_operations proc_tid_base_operations = {
3044 .read = generic_read_dir,
3045 .readdir = proc_tid_base_readdir,
3046 .llseek = default_llseek,
3049 static const struct inode_operations proc_tid_base_inode_operations = {
3050 .lookup = proc_tid_base_lookup,
3051 .getattr = pid_getattr,
3052 .setattr = proc_setattr,
3055 static struct dentry *proc_task_instantiate(struct inode *dir,
3056 struct dentry *dentry, struct task_struct *task, const void *ptr)
3058 struct dentry *error = ERR_PTR(-ENOENT);
3059 struct inode *inode;
3060 inode = proc_pid_make_inode(dir->i_sb, task);
3064 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3065 inode->i_op = &proc_tid_base_inode_operations;
3066 inode->i_fop = &proc_tid_base_operations;
3067 inode->i_flags|=S_IMMUTABLE;
3069 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3070 ARRAY_SIZE(tid_base_stuff)));
3072 d_set_d_op(dentry, &pid_dentry_operations);
3074 d_add(dentry, inode);
3075 /* Close the race of the process dying before we return the dentry */
3076 if (pid_revalidate(dentry, 0))
3082 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3084 struct dentry *result = ERR_PTR(-ENOENT);
3085 struct task_struct *task;
3086 struct task_struct *leader = get_proc_task(dir);
3088 struct pid_namespace *ns;
3093 tid = name_to_int(dentry);
3097 ns = dentry->d_sb->s_fs_info;
3099 task = find_task_by_pid_ns(tid, ns);
3101 get_task_struct(task);
3105 if (!same_thread_group(leader, task))
3108 result = proc_task_instantiate(dir, dentry, task, NULL);
3110 put_task_struct(task);
3112 put_task_struct(leader);
3118 * Find the first tid of a thread group to return to user space.
3120 * Usually this is just the thread group leader, but if the users
3121 * buffer was too small or there was a seek into the middle of the
3122 * directory we have more work todo.
3124 * In the case of a short read we start with find_task_by_pid.
3126 * In the case of a seek we start with the leader and walk nr
3129 static struct task_struct *first_tid(struct task_struct *leader,
3130 int tid, int nr, struct pid_namespace *ns)
3132 struct task_struct *pos;
3135 /* Attempt to start with the pid of a thread */
3136 if (tid && (nr > 0)) {
3137 pos = find_task_by_pid_ns(tid, ns);
3138 if (pos && (pos->group_leader == leader))
3142 /* If nr exceeds the number of threads there is nothing todo */
3144 if (nr && nr >= get_nr_threads(leader))
3147 /* If we haven't found our starting place yet start
3148 * with the leader and walk nr threads forward.
3150 for (pos = leader; nr > 0; --nr) {
3151 pos = next_thread(pos);
3152 if (pos == leader) {
3158 get_task_struct(pos);
3165 * Find the next thread in the thread list.
3166 * Return NULL if there is an error or no next thread.
3168 * The reference to the input task_struct is released.
3170 static struct task_struct *next_tid(struct task_struct *start)
3172 struct task_struct *pos = NULL;
3174 if (pid_alive(start)) {
3175 pos = next_thread(start);
3176 if (thread_group_leader(pos))
3179 get_task_struct(pos);
3182 put_task_struct(start);
3186 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3187 struct task_struct *task, int tid)
3189 char name[PROC_NUMBUF];
3190 int len = snprintf(name, sizeof(name), "%d", tid);
3191 return proc_fill_cache(filp, dirent, filldir, name, len,
3192 proc_task_instantiate, task, NULL);
3195 /* for the /proc/TGID/task/ directories */
3196 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3198 struct dentry *dentry = filp->f_path.dentry;
3199 struct inode *inode = dentry->d_inode;
3200 struct task_struct *leader = NULL;
3201 struct task_struct *task;
3202 int retval = -ENOENT;
3205 struct pid_namespace *ns;
3207 task = get_proc_task(inode);
3211 if (pid_alive(task)) {
3212 leader = task->group_leader;
3213 get_task_struct(leader);
3216 put_task_struct(task);
3221 switch ((unsigned long)filp->f_pos) {
3224 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3229 ino = parent_ino(dentry);
3230 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3236 /* f_version caches the tgid value that the last readdir call couldn't
3237 * return. lseek aka telldir automagically resets f_version to 0.
3239 ns = filp->f_dentry->d_sb->s_fs_info;
3240 tid = (int)filp->f_version;
3241 filp->f_version = 0;
3242 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3244 task = next_tid(task), filp->f_pos++) {
3245 tid = task_pid_nr_ns(task, ns);
3246 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3247 /* returning this tgid failed, save it as the first
3248 * pid for the next readir call */
3249 filp->f_version = (u64)tid;
3250 put_task_struct(task);
3255 put_task_struct(leader);
3260 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3262 struct inode *inode = dentry->d_inode;
3263 struct task_struct *p = get_proc_task(inode);
3264 generic_fillattr(inode, stat);
3267 stat->nlink += get_nr_threads(p);
3274 static const struct inode_operations proc_task_inode_operations = {
3275 .lookup = proc_task_lookup,
3276 .getattr = proc_task_getattr,
3277 .setattr = proc_setattr,
3278 .permission = proc_pid_permission,
3281 static const struct file_operations proc_task_operations = {
3282 .read = generic_read_dir,
3283 .readdir = proc_task_readdir,
3284 .llseek = default_llseek,