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)
205 return get_cmdline(task, buffer, PAGE_SIZE);
208 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
209 struct pid *pid, struct task_struct *task)
211 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
212 if (mm && !IS_ERR(mm)) {
213 unsigned int nwords = 0;
216 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
217 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
225 #ifdef CONFIG_KALLSYMS
227 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
228 * Returns the resolved symbol. If that fails, simply return the address.
230 static int proc_pid_wchan(struct task_struct *task, char *buffer)
233 char symname[KSYM_NAME_LEN];
235 wchan = get_wchan(task);
237 if (lookup_symbol_name(wchan, symname) < 0)
238 if (!ptrace_may_access(task, PTRACE_MODE_READ))
241 return sprintf(buffer, "%lu", wchan);
243 return sprintf(buffer, "%s", symname);
245 #endif /* CONFIG_KALLSYMS */
247 static int lock_trace(struct task_struct *task)
249 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
252 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
253 mutex_unlock(&task->signal->cred_guard_mutex);
259 static void unlock_trace(struct task_struct *task)
261 mutex_unlock(&task->signal->cred_guard_mutex);
264 #ifdef CONFIG_STACKTRACE
266 #define MAX_STACK_TRACE_DEPTH 64
268 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
269 struct pid *pid, struct task_struct *task)
271 struct stack_trace trace;
272 unsigned long *entries;
276 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
280 trace.nr_entries = 0;
281 trace.max_entries = MAX_STACK_TRACE_DEPTH;
282 trace.entries = entries;
285 err = lock_trace(task);
287 save_stack_trace_tsk(task, &trace);
289 for (i = 0; i < trace.nr_entries; i++) {
290 seq_printf(m, "[<%pK>] %pS\n",
291 (void *)entries[i], (void *)entries[i]);
301 #ifdef CONFIG_SCHEDSTATS
303 * Provides /proc/PID/schedstat
305 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
307 return sprintf(buffer, "%llu %llu %lu\n",
308 (unsigned long long)task->se.sum_exec_runtime,
309 (unsigned long long)task->sched_info.run_delay,
310 task->sched_info.pcount);
314 #ifdef CONFIG_LATENCYTOP
315 static int lstats_show_proc(struct seq_file *m, void *v)
318 struct inode *inode = m->private;
319 struct task_struct *task = get_proc_task(inode);
323 seq_puts(m, "Latency Top version : v0.1\n");
324 for (i = 0; i < 32; i++) {
325 struct latency_record *lr = &task->latency_record[i];
326 if (lr->backtrace[0]) {
328 seq_printf(m, "%i %li %li",
329 lr->count, lr->time, lr->max);
330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
331 unsigned long bt = lr->backtrace[q];
336 seq_printf(m, " %ps", (void *)bt);
342 put_task_struct(task);
346 static int lstats_open(struct inode *inode, struct file *file)
348 return single_open(file, lstats_show_proc, inode);
351 static ssize_t lstats_write(struct file *file, const char __user *buf,
352 size_t count, loff_t *offs)
354 struct task_struct *task = get_proc_task(file_inode(file));
358 clear_all_latency_tracing(task);
359 put_task_struct(task);
364 static const struct file_operations proc_lstats_operations = {
367 .write = lstats_write,
369 .release = single_release,
374 #ifdef CONFIG_CGROUPS
375 static int cgroup_open(struct inode *inode, struct file *file)
377 struct pid *pid = PROC_I(inode)->pid;
378 return single_open(file, proc_cgroup_show, pid);
381 static const struct file_operations proc_cgroup_operations = {
385 .release = single_release,
389 #ifdef CONFIG_PROC_PID_CPUSET
391 static int cpuset_open(struct inode *inode, struct file *file)
393 struct pid *pid = PROC_I(inode)->pid;
394 return single_open(file, proc_cpuset_show, pid);
397 static const struct file_operations proc_cpuset_operations = {
401 .release = single_release,
405 static int proc_oom_score(struct task_struct *task, char *buffer)
407 unsigned long totalpages = totalram_pages + total_swap_pages;
408 unsigned long points = 0;
410 read_lock(&tasklist_lock);
412 points = oom_badness(task, NULL, NULL, totalpages) *
414 read_unlock(&tasklist_lock);
415 return sprintf(buffer, "%lu\n", points);
423 static const struct limit_names lnames[RLIM_NLIMITS] = {
424 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
425 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
426 [RLIMIT_DATA] = {"Max data size", "bytes"},
427 [RLIMIT_STACK] = {"Max stack size", "bytes"},
428 [RLIMIT_CORE] = {"Max core file size", "bytes"},
429 [RLIMIT_RSS] = {"Max resident set", "bytes"},
430 [RLIMIT_NPROC] = {"Max processes", "processes"},
431 [RLIMIT_NOFILE] = {"Max open files", "files"},
432 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
433 [RLIMIT_AS] = {"Max address space", "bytes"},
434 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
435 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
436 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
437 [RLIMIT_NICE] = {"Max nice priority", NULL},
438 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
439 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
442 /* Display limits for a process */
443 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
444 struct pid *pid, struct task_struct *task)
449 struct rlimit rlim[RLIM_NLIMITS];
451 if (!lock_task_sighand(task, &flags))
453 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454 unlock_task_sighand(task, &flags);
457 * print the file header
459 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
460 "Limit", "Soft Limit", "Hard Limit", "Units");
462 for (i = 0; i < RLIM_NLIMITS; i++) {
463 if (rlim[i].rlim_cur == RLIM_INFINITY)
464 seq_printf(m, "%-25s %-20s ",
465 lnames[i].name, "unlimited");
467 seq_printf(m, "%-25s %-20lu ",
468 lnames[i].name, rlim[i].rlim_cur);
470 if (rlim[i].rlim_max == RLIM_INFINITY)
471 seq_printf(m, "%-20s ", "unlimited");
473 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
476 seq_printf(m, "%-10s\n", lnames[i].unit);
484 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
485 static int proc_pid_syscall(struct task_struct *task, char *buffer)
488 unsigned long args[6], sp, pc;
489 int res = lock_trace(task);
493 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
494 res = sprintf(buffer, "running\n");
496 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
498 res = sprintf(buffer,
499 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
501 args[0], args[1], args[2], args[3], args[4], args[5],
506 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
508 /************************************************************************/
509 /* Here the fs part begins */
510 /************************************************************************/
512 /* permission checks */
513 static int proc_fd_access_allowed(struct inode *inode)
515 struct task_struct *task;
517 /* Allow access to a task's file descriptors if it is us or we
518 * may use ptrace attach to the process and find out that
521 task = get_proc_task(inode);
523 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
524 put_task_struct(task);
529 int proc_setattr(struct dentry *dentry, struct iattr *attr)
532 struct inode *inode = dentry->d_inode;
534 if (attr->ia_valid & ATTR_MODE)
537 error = inode_change_ok(inode, attr);
541 setattr_copy(inode, attr);
542 mark_inode_dirty(inode);
547 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
548 * or euid/egid (for hide_pid_min=2)?
550 static bool has_pid_permissions(struct pid_namespace *pid,
551 struct task_struct *task,
554 if (pid->hide_pid < hide_pid_min)
556 if (in_group_p(pid->pid_gid))
558 return ptrace_may_access(task, PTRACE_MODE_READ);
562 static int proc_pid_permission(struct inode *inode, int mask)
564 struct pid_namespace *pid = inode->i_sb->s_fs_info;
565 struct task_struct *task;
568 task = get_proc_task(inode);
571 has_perms = has_pid_permissions(pid, task, 1);
572 put_task_struct(task);
575 if (pid->hide_pid == 2) {
577 * Let's make getdents(), stat(), and open()
578 * consistent with each other. If a process
579 * may not stat() a file, it shouldn't be seen
587 return generic_permission(inode, mask);
592 static const struct inode_operations proc_def_inode_operations = {
593 .setattr = proc_setattr,
596 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
598 static ssize_t proc_info_read(struct file * file, char __user * buf,
599 size_t count, loff_t *ppos)
601 struct inode * inode = file_inode(file);
604 struct task_struct *task = get_proc_task(inode);
610 if (count > PROC_BLOCK_SIZE)
611 count = PROC_BLOCK_SIZE;
614 if (!(page = __get_free_page(GFP_TEMPORARY)))
617 length = PROC_I(inode)->op.proc_read(task, (char*)page);
620 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
623 put_task_struct(task);
628 static const struct file_operations proc_info_file_operations = {
629 .read = proc_info_read,
630 .llseek = generic_file_llseek,
633 static int proc_single_show(struct seq_file *m, void *v)
635 struct inode *inode = m->private;
636 struct pid_namespace *ns;
638 struct task_struct *task;
641 ns = inode->i_sb->s_fs_info;
642 pid = proc_pid(inode);
643 task = get_pid_task(pid, PIDTYPE_PID);
647 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
649 put_task_struct(task);
653 static int proc_single_open(struct inode *inode, struct file *filp)
655 return single_open(filp, proc_single_show, inode);
658 static const struct file_operations proc_single_file_operations = {
659 .open = proc_single_open,
662 .release = single_release,
665 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
667 struct task_struct *task = get_proc_task(file_inode(file));
668 struct mm_struct *mm;
673 mm = mm_access(task, mode);
674 put_task_struct(task);
680 /* ensure this mm_struct can't be freed */
681 atomic_inc(&mm->mm_count);
682 /* but do not pin its memory */
686 file->private_data = mm;
691 static int mem_open(struct inode *inode, struct file *file)
693 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
695 /* OK to pass negative loff_t, we can catch out-of-range */
696 file->f_mode |= FMODE_UNSIGNED_OFFSET;
701 static ssize_t mem_rw(struct file *file, char __user *buf,
702 size_t count, loff_t *ppos, int write)
704 struct mm_struct *mm = file->private_data;
705 unsigned long addr = *ppos;
712 page = (char *)__get_free_page(GFP_TEMPORARY);
717 if (!atomic_inc_not_zero(&mm->mm_users))
721 int this_len = min_t(int, count, PAGE_SIZE);
723 if (write && copy_from_user(page, buf, this_len)) {
728 this_len = access_remote_vm(mm, addr, page, this_len, write);
735 if (!write && copy_to_user(buf, page, this_len)) {
749 free_page((unsigned long) page);
753 static ssize_t mem_read(struct file *file, char __user *buf,
754 size_t count, loff_t *ppos)
756 return mem_rw(file, buf, count, ppos, 0);
759 static ssize_t mem_write(struct file *file, const char __user *buf,
760 size_t count, loff_t *ppos)
762 return mem_rw(file, (char __user*)buf, count, ppos, 1);
765 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
769 file->f_pos = offset;
772 file->f_pos += offset;
777 force_successful_syscall_return();
781 static int mem_release(struct inode *inode, struct file *file)
783 struct mm_struct *mm = file->private_data;
789 static const struct file_operations proc_mem_operations = {
794 .release = mem_release,
797 static int environ_open(struct inode *inode, struct file *file)
799 return __mem_open(inode, file, PTRACE_MODE_READ);
802 static ssize_t environ_read(struct file *file, char __user *buf,
803 size_t count, loff_t *ppos)
806 unsigned long src = *ppos;
808 struct mm_struct *mm = file->private_data;
813 page = (char *)__get_free_page(GFP_TEMPORARY);
818 if (!atomic_inc_not_zero(&mm->mm_users))
821 size_t this_len, max_len;
824 if (src >= (mm->env_end - mm->env_start))
827 this_len = mm->env_end - (mm->env_start + src);
829 max_len = min_t(size_t, PAGE_SIZE, count);
830 this_len = min(max_len, this_len);
832 retval = access_remote_vm(mm, (mm->env_start + src),
840 if (copy_to_user(buf, page, retval)) {
854 free_page((unsigned long) page);
858 static const struct file_operations proc_environ_operations = {
859 .open = environ_open,
860 .read = environ_read,
861 .llseek = generic_file_llseek,
862 .release = mem_release,
865 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
868 struct task_struct *task = get_proc_task(file_inode(file));
869 char buffer[PROC_NUMBUF];
870 int oom_adj = OOM_ADJUST_MIN;
876 if (lock_task_sighand(task, &flags)) {
877 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
878 oom_adj = OOM_ADJUST_MAX;
880 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
882 unlock_task_sighand(task, &flags);
884 put_task_struct(task);
885 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
886 return simple_read_from_buffer(buf, count, ppos, buffer, len);
889 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
890 size_t count, loff_t *ppos)
892 struct task_struct *task;
893 char buffer[PROC_NUMBUF];
898 memset(buffer, 0, sizeof(buffer));
899 if (count > sizeof(buffer) - 1)
900 count = sizeof(buffer) - 1;
901 if (copy_from_user(buffer, buf, count)) {
906 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
909 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
910 oom_adj != OOM_DISABLE) {
915 task = get_proc_task(file_inode(file));
927 if (!lock_task_sighand(task, &flags)) {
933 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
934 * value is always attainable.
936 if (oom_adj == OOM_ADJUST_MAX)
937 oom_adj = OOM_SCORE_ADJ_MAX;
939 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
941 if (oom_adj < task->signal->oom_score_adj &&
942 !capable(CAP_SYS_RESOURCE)) {
948 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
949 * /proc/pid/oom_score_adj instead.
951 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
952 current->comm, task_pid_nr(current), task_pid_nr(task),
955 task->signal->oom_score_adj = oom_adj;
956 trace_oom_score_adj_update(task);
958 unlock_task_sighand(task, &flags);
961 put_task_struct(task);
963 return err < 0 ? err : count;
966 static const struct file_operations proc_oom_adj_operations = {
967 .read = oom_adj_read,
968 .write = oom_adj_write,
969 .llseek = generic_file_llseek,
972 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
973 size_t count, loff_t *ppos)
975 struct task_struct *task = get_proc_task(file_inode(file));
976 char buffer[PROC_NUMBUF];
977 short oom_score_adj = OOM_SCORE_ADJ_MIN;
983 if (lock_task_sighand(task, &flags)) {
984 oom_score_adj = task->signal->oom_score_adj;
985 unlock_task_sighand(task, &flags);
987 put_task_struct(task);
988 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
989 return simple_read_from_buffer(buf, count, ppos, buffer, len);
992 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
993 size_t count, loff_t *ppos)
995 struct task_struct *task;
996 char buffer[PROC_NUMBUF];
1001 memset(buffer, 0, sizeof(buffer));
1002 if (count > sizeof(buffer) - 1)
1003 count = sizeof(buffer) - 1;
1004 if (copy_from_user(buffer, buf, count)) {
1009 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1012 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1013 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1018 task = get_proc_task(file_inode(file));
1030 if (!lock_task_sighand(task, &flags)) {
1035 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1036 !capable(CAP_SYS_RESOURCE)) {
1041 task->signal->oom_score_adj = (short)oom_score_adj;
1042 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1043 task->signal->oom_score_adj_min = (short)oom_score_adj;
1044 trace_oom_score_adj_update(task);
1047 unlock_task_sighand(task, &flags);
1050 put_task_struct(task);
1052 return err < 0 ? err : count;
1055 static const struct file_operations proc_oom_score_adj_operations = {
1056 .read = oom_score_adj_read,
1057 .write = oom_score_adj_write,
1058 .llseek = default_llseek,
1061 #ifdef CONFIG_AUDITSYSCALL
1062 #define TMPBUFLEN 21
1063 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1064 size_t count, loff_t *ppos)
1066 struct inode * inode = file_inode(file);
1067 struct task_struct *task = get_proc_task(inode);
1069 char tmpbuf[TMPBUFLEN];
1073 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1074 from_kuid(file->f_cred->user_ns,
1075 audit_get_loginuid(task)));
1076 put_task_struct(task);
1077 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1080 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1081 size_t count, loff_t *ppos)
1083 struct inode * inode = file_inode(file);
1090 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1096 if (count >= PAGE_SIZE)
1097 count = PAGE_SIZE - 1;
1100 /* No partial writes. */
1103 page = (char*)__get_free_page(GFP_TEMPORARY);
1107 if (copy_from_user(page, buf, count))
1111 loginuid = simple_strtoul(page, &tmp, 10);
1118 /* is userspace tring to explicitly UNSET the loginuid? */
1119 if (loginuid == AUDIT_UID_UNSET) {
1120 kloginuid = INVALID_UID;
1122 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1123 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 if (make_it_fail < 0 || make_it_fail > 1)
1205 task = get_proc_task(file_inode(file));
1208 task->make_it_fail = make_it_fail;
1209 put_task_struct(task);
1214 static const struct file_operations proc_fault_inject_operations = {
1215 .read = proc_fault_inject_read,
1216 .write = proc_fault_inject_write,
1217 .llseek = generic_file_llseek,
1222 #ifdef CONFIG_SCHED_DEBUG
1224 * Print out various scheduling related per-task fields:
1226 static int sched_show(struct seq_file *m, void *v)
1228 struct inode *inode = m->private;
1229 struct task_struct *p;
1231 p = get_proc_task(inode);
1234 proc_sched_show_task(p, m);
1242 sched_write(struct file *file, const char __user *buf,
1243 size_t count, loff_t *offset)
1245 struct inode *inode = file_inode(file);
1246 struct task_struct *p;
1248 p = get_proc_task(inode);
1251 proc_sched_set_task(p);
1258 static int sched_open(struct inode *inode, struct file *filp)
1260 return single_open(filp, sched_show, inode);
1263 static const struct file_operations proc_pid_sched_operations = {
1266 .write = sched_write,
1267 .llseek = seq_lseek,
1268 .release = single_release,
1273 #ifdef CONFIG_SCHED_AUTOGROUP
1275 * Print out autogroup related information:
1277 static int sched_autogroup_show(struct seq_file *m, void *v)
1279 struct inode *inode = m->private;
1280 struct task_struct *p;
1282 p = get_proc_task(inode);
1285 proc_sched_autogroup_show_task(p, m);
1293 sched_autogroup_write(struct file *file, const char __user *buf,
1294 size_t count, loff_t *offset)
1296 struct inode *inode = file_inode(file);
1297 struct task_struct *p;
1298 char buffer[PROC_NUMBUF];
1302 memset(buffer, 0, sizeof(buffer));
1303 if (count > sizeof(buffer) - 1)
1304 count = sizeof(buffer) - 1;
1305 if (copy_from_user(buffer, buf, count))
1308 err = kstrtoint(strstrip(buffer), 0, &nice);
1312 p = get_proc_task(inode);
1316 err = proc_sched_autogroup_set_nice(p, nice);
1325 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1329 ret = single_open(filp, sched_autogroup_show, NULL);
1331 struct seq_file *m = filp->private_data;
1338 static const struct file_operations proc_pid_sched_autogroup_operations = {
1339 .open = sched_autogroup_open,
1341 .write = sched_autogroup_write,
1342 .llseek = seq_lseek,
1343 .release = single_release,
1346 #endif /* CONFIG_SCHED_AUTOGROUP */
1348 static ssize_t comm_write(struct file *file, const char __user *buf,
1349 size_t count, loff_t *offset)
1351 struct inode *inode = file_inode(file);
1352 struct task_struct *p;
1353 char buffer[TASK_COMM_LEN];
1354 const size_t maxlen = sizeof(buffer) - 1;
1356 memset(buffer, 0, sizeof(buffer));
1357 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1360 p = get_proc_task(inode);
1364 if (same_thread_group(current, p))
1365 set_task_comm(p, buffer);
1374 static int comm_show(struct seq_file *m, void *v)
1376 struct inode *inode = m->private;
1377 struct task_struct *p;
1379 p = get_proc_task(inode);
1384 seq_printf(m, "%s\n", p->comm);
1392 static int comm_open(struct inode *inode, struct file *filp)
1394 return single_open(filp, comm_show, inode);
1397 static const struct file_operations proc_pid_set_comm_operations = {
1400 .write = comm_write,
1401 .llseek = seq_lseek,
1402 .release = single_release,
1405 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1407 struct task_struct *task;
1408 struct mm_struct *mm;
1409 struct file *exe_file;
1411 task = get_proc_task(dentry->d_inode);
1414 mm = get_task_mm(task);
1415 put_task_struct(task);
1418 exe_file = get_mm_exe_file(mm);
1421 *exe_path = exe_file->f_path;
1422 path_get(&exe_file->f_path);
1429 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1431 struct inode *inode = dentry->d_inode;
1433 int error = -EACCES;
1435 /* Are we allowed to snoop on the tasks file descriptors? */
1436 if (!proc_fd_access_allowed(inode))
1439 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1443 nd_jump_link(nd, &path);
1446 return ERR_PTR(error);
1449 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1451 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1458 pathname = d_path(path, tmp, PAGE_SIZE);
1459 len = PTR_ERR(pathname);
1460 if (IS_ERR(pathname))
1462 len = tmp + PAGE_SIZE - 1 - pathname;
1466 if (copy_to_user(buffer, pathname, len))
1469 free_page((unsigned long)tmp);
1473 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1475 int error = -EACCES;
1476 struct inode *inode = dentry->d_inode;
1479 /* Are we allowed to snoop on the tasks file descriptors? */
1480 if (!proc_fd_access_allowed(inode))
1483 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1487 error = do_proc_readlink(&path, buffer, buflen);
1493 const struct inode_operations proc_pid_link_inode_operations = {
1494 .readlink = proc_pid_readlink,
1495 .follow_link = proc_pid_follow_link,
1496 .setattr = proc_setattr,
1500 /* building an inode */
1502 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1504 struct inode * inode;
1505 struct proc_inode *ei;
1506 const struct cred *cred;
1508 /* We need a new inode */
1510 inode = new_inode(sb);
1516 inode->i_ino = get_next_ino();
1517 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1518 inode->i_op = &proc_def_inode_operations;
1521 * grab the reference to task.
1523 ei->pid = get_task_pid(task, PIDTYPE_PID);
1527 if (task_dumpable(task)) {
1529 cred = __task_cred(task);
1530 inode->i_uid = cred->euid;
1531 inode->i_gid = cred->egid;
1534 security_task_to_inode(task, inode);
1544 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1546 struct inode *inode = dentry->d_inode;
1547 struct task_struct *task;
1548 const struct cred *cred;
1549 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1551 generic_fillattr(inode, stat);
1554 stat->uid = GLOBAL_ROOT_UID;
1555 stat->gid = GLOBAL_ROOT_GID;
1556 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1558 if (!has_pid_permissions(pid, task, 2)) {
1561 * This doesn't prevent learning whether PID exists,
1562 * it only makes getattr() consistent with readdir().
1566 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1567 task_dumpable(task)) {
1568 cred = __task_cred(task);
1569 stat->uid = cred->euid;
1570 stat->gid = cred->egid;
1580 * Exceptional case: normally we are not allowed to unhash a busy
1581 * directory. In this case, however, we can do it - no aliasing problems
1582 * due to the way we treat inodes.
1584 * Rewrite the inode's ownerships here because the owning task may have
1585 * performed a setuid(), etc.
1587 * Before the /proc/pid/status file was created the only way to read
1588 * the effective uid of a /process was to stat /proc/pid. Reading
1589 * /proc/pid/status is slow enough that procps and other packages
1590 * kept stating /proc/pid. To keep the rules in /proc simple I have
1591 * made this apply to all per process world readable and executable
1594 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1596 struct inode *inode;
1597 struct task_struct *task;
1598 const struct cred *cred;
1600 if (flags & LOOKUP_RCU)
1603 inode = dentry->d_inode;
1604 task = get_proc_task(inode);
1607 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1608 task_dumpable(task)) {
1610 cred = __task_cred(task);
1611 inode->i_uid = cred->euid;
1612 inode->i_gid = cred->egid;
1615 inode->i_uid = GLOBAL_ROOT_UID;
1616 inode->i_gid = GLOBAL_ROOT_GID;
1618 inode->i_mode &= ~(S_ISUID | S_ISGID);
1619 security_task_to_inode(task, inode);
1620 put_task_struct(task);
1627 static inline bool proc_inode_is_dead(struct inode *inode)
1629 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1632 int pid_delete_dentry(const struct dentry *dentry)
1634 /* Is the task we represent dead?
1635 * If so, then don't put the dentry on the lru list,
1636 * kill it immediately.
1638 return proc_inode_is_dead(dentry->d_inode);
1641 const struct dentry_operations pid_dentry_operations =
1643 .d_revalidate = pid_revalidate,
1644 .d_delete = pid_delete_dentry,
1650 * Fill a directory entry.
1652 * If possible create the dcache entry and derive our inode number and
1653 * file type from dcache entry.
1655 * Since all of the proc inode numbers are dynamically generated, the inode
1656 * numbers do not exist until the inode is cache. This means creating the
1657 * the dcache entry in readdir is necessary to keep the inode numbers
1658 * reported by readdir in sync with the inode numbers reported
1661 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1662 const char *name, int len,
1663 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1665 struct dentry *child, *dir = file->f_path.dentry;
1666 struct qstr qname = QSTR_INIT(name, len);
1667 struct inode *inode;
1671 child = d_hash_and_lookup(dir, &qname);
1673 child = d_alloc(dir, &qname);
1675 goto end_instantiate;
1676 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1678 goto end_instantiate;
1681 inode = child->d_inode;
1683 type = inode->i_mode >> 12;
1685 return dir_emit(ctx, name, len, ino, type);
1688 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1691 #ifdef CONFIG_CHECKPOINT_RESTORE
1694 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1695 * which represent vma start and end addresses.
1697 static int dname_to_vma_addr(struct dentry *dentry,
1698 unsigned long *start, unsigned long *end)
1700 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1706 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1708 unsigned long vm_start, vm_end;
1709 bool exact_vma_exists = false;
1710 struct mm_struct *mm = NULL;
1711 struct task_struct *task;
1712 const struct cred *cred;
1713 struct inode *inode;
1716 if (flags & LOOKUP_RCU)
1719 if (!capable(CAP_SYS_ADMIN)) {
1724 inode = dentry->d_inode;
1725 task = get_proc_task(inode);
1729 mm = mm_access(task, PTRACE_MODE_READ);
1730 if (IS_ERR_OR_NULL(mm))
1733 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1734 down_read(&mm->mmap_sem);
1735 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1736 up_read(&mm->mmap_sem);
1741 if (exact_vma_exists) {
1742 if (task_dumpable(task)) {
1744 cred = __task_cred(task);
1745 inode->i_uid = cred->euid;
1746 inode->i_gid = cred->egid;
1749 inode->i_uid = GLOBAL_ROOT_UID;
1750 inode->i_gid = GLOBAL_ROOT_GID;
1752 security_task_to_inode(task, inode);
1757 put_task_struct(task);
1766 static const struct dentry_operations tid_map_files_dentry_operations = {
1767 .d_revalidate = map_files_d_revalidate,
1768 .d_delete = pid_delete_dentry,
1771 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1773 unsigned long vm_start, vm_end;
1774 struct vm_area_struct *vma;
1775 struct task_struct *task;
1776 struct mm_struct *mm;
1780 task = get_proc_task(dentry->d_inode);
1784 mm = get_task_mm(task);
1785 put_task_struct(task);
1789 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1794 down_read(&mm->mmap_sem);
1795 vma = find_exact_vma(mm, vm_start, vm_end);
1796 if (vma && vma->vm_file) {
1797 *path = vma->vm_file->f_path;
1801 up_read(&mm->mmap_sem);
1809 struct map_files_info {
1812 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1816 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1817 struct task_struct *task, const void *ptr)
1819 fmode_t mode = (fmode_t)(unsigned long)ptr;
1820 struct proc_inode *ei;
1821 struct inode *inode;
1823 inode = proc_pid_make_inode(dir->i_sb, task);
1828 ei->op.proc_get_link = proc_map_files_get_link;
1830 inode->i_op = &proc_pid_link_inode_operations;
1832 inode->i_mode = S_IFLNK;
1834 if (mode & FMODE_READ)
1835 inode->i_mode |= S_IRUSR;
1836 if (mode & FMODE_WRITE)
1837 inode->i_mode |= S_IWUSR;
1839 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1840 d_add(dentry, inode);
1845 static struct dentry *proc_map_files_lookup(struct inode *dir,
1846 struct dentry *dentry, unsigned int flags)
1848 unsigned long vm_start, vm_end;
1849 struct vm_area_struct *vma;
1850 struct task_struct *task;
1852 struct mm_struct *mm;
1855 if (!capable(CAP_SYS_ADMIN))
1859 task = get_proc_task(dir);
1864 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1868 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1871 mm = get_task_mm(task);
1875 down_read(&mm->mmap_sem);
1876 vma = find_exact_vma(mm, vm_start, vm_end);
1881 result = proc_map_files_instantiate(dir, dentry, task,
1882 (void *)(unsigned long)vma->vm_file->f_mode);
1885 up_read(&mm->mmap_sem);
1888 put_task_struct(task);
1890 return ERR_PTR(result);
1893 static const struct inode_operations proc_map_files_inode_operations = {
1894 .lookup = proc_map_files_lookup,
1895 .permission = proc_fd_permission,
1896 .setattr = proc_setattr,
1900 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1902 struct vm_area_struct *vma;
1903 struct task_struct *task;
1904 struct mm_struct *mm;
1905 unsigned long nr_files, pos, i;
1906 struct flex_array *fa = NULL;
1907 struct map_files_info info;
1908 struct map_files_info *p;
1912 if (!capable(CAP_SYS_ADMIN))
1916 task = get_proc_task(file_inode(file));
1921 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1925 if (!dir_emit_dots(file, ctx))
1928 mm = get_task_mm(task);
1931 down_read(&mm->mmap_sem);
1936 * We need two passes here:
1938 * 1) Collect vmas of mapped files with mmap_sem taken
1939 * 2) Release mmap_sem and instantiate entries
1941 * otherwise we get lockdep complained, since filldir()
1942 * routine might require mmap_sem taken in might_fault().
1945 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1946 if (vma->vm_file && ++pos > ctx->pos)
1951 fa = flex_array_alloc(sizeof(info), nr_files,
1953 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1957 flex_array_free(fa);
1958 up_read(&mm->mmap_sem);
1962 for (i = 0, vma = mm->mmap, pos = 2; vma;
1963 vma = vma->vm_next) {
1966 if (++pos <= ctx->pos)
1969 info.mode = vma->vm_file->f_mode;
1970 info.len = snprintf(info.name,
1971 sizeof(info.name), "%lx-%lx",
1972 vma->vm_start, vma->vm_end);
1973 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1977 up_read(&mm->mmap_sem);
1979 for (i = 0; i < nr_files; i++) {
1980 p = flex_array_get(fa, i);
1981 if (!proc_fill_cache(file, ctx,
1983 proc_map_files_instantiate,
1985 (void *)(unsigned long)p->mode))
1990 flex_array_free(fa);
1994 put_task_struct(task);
1999 static const struct file_operations proc_map_files_operations = {
2000 .read = generic_read_dir,
2001 .iterate = proc_map_files_readdir,
2002 .llseek = default_llseek,
2005 struct timers_private {
2007 struct task_struct *task;
2008 struct sighand_struct *sighand;
2009 struct pid_namespace *ns;
2010 unsigned long flags;
2013 static void *timers_start(struct seq_file *m, loff_t *pos)
2015 struct timers_private *tp = m->private;
2017 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2019 return ERR_PTR(-ESRCH);
2021 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2023 return ERR_PTR(-ESRCH);
2025 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2028 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2030 struct timers_private *tp = m->private;
2031 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2034 static void timers_stop(struct seq_file *m, void *v)
2036 struct timers_private *tp = m->private;
2039 unlock_task_sighand(tp->task, &tp->flags);
2044 put_task_struct(tp->task);
2049 static int show_timer(struct seq_file *m, void *v)
2051 struct k_itimer *timer;
2052 struct timers_private *tp = m->private;
2054 static const char * const nstr[] = {
2055 [SIGEV_SIGNAL] = "signal",
2056 [SIGEV_NONE] = "none",
2057 [SIGEV_THREAD] = "thread",
2060 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2061 notify = timer->it_sigev_notify;
2063 seq_printf(m, "ID: %d\n", timer->it_id);
2064 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2065 timer->sigq->info.si_value.sival_ptr);
2066 seq_printf(m, "notify: %s/%s.%d\n",
2067 nstr[notify & ~SIGEV_THREAD_ID],
2068 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2069 pid_nr_ns(timer->it_pid, tp->ns));
2070 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2075 static const struct seq_operations proc_timers_seq_ops = {
2076 .start = timers_start,
2077 .next = timers_next,
2078 .stop = timers_stop,
2082 static int proc_timers_open(struct inode *inode, struct file *file)
2084 struct timers_private *tp;
2086 tp = __seq_open_private(file, &proc_timers_seq_ops,
2087 sizeof(struct timers_private));
2091 tp->pid = proc_pid(inode);
2092 tp->ns = inode->i_sb->s_fs_info;
2096 static const struct file_operations proc_timers_operations = {
2097 .open = proc_timers_open,
2099 .llseek = seq_lseek,
2100 .release = seq_release_private,
2102 #endif /* CONFIG_CHECKPOINT_RESTORE */
2104 static int proc_pident_instantiate(struct inode *dir,
2105 struct dentry *dentry, struct task_struct *task, const void *ptr)
2107 const struct pid_entry *p = ptr;
2108 struct inode *inode;
2109 struct proc_inode *ei;
2111 inode = proc_pid_make_inode(dir->i_sb, task);
2116 inode->i_mode = p->mode;
2117 if (S_ISDIR(inode->i_mode))
2118 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2120 inode->i_op = p->iop;
2122 inode->i_fop = p->fop;
2124 d_set_d_op(dentry, &pid_dentry_operations);
2125 d_add(dentry, inode);
2126 /* Close the race of the process dying before we return the dentry */
2127 if (pid_revalidate(dentry, 0))
2133 static struct dentry *proc_pident_lookup(struct inode *dir,
2134 struct dentry *dentry,
2135 const struct pid_entry *ents,
2139 struct task_struct *task = get_proc_task(dir);
2140 const struct pid_entry *p, *last;
2148 * Yes, it does not scale. And it should not. Don't add
2149 * new entries into /proc/<tgid>/ without very good reasons.
2151 last = &ents[nents - 1];
2152 for (p = ents; p <= last; p++) {
2153 if (p->len != dentry->d_name.len)
2155 if (!memcmp(dentry->d_name.name, p->name, p->len))
2161 error = proc_pident_instantiate(dir, dentry, task, p);
2163 put_task_struct(task);
2165 return ERR_PTR(error);
2168 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2169 const struct pid_entry *ents, unsigned int nents)
2171 struct task_struct *task = get_proc_task(file_inode(file));
2172 const struct pid_entry *p;
2177 if (!dir_emit_dots(file, ctx))
2180 if (ctx->pos >= nents + 2)
2183 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2184 if (!proc_fill_cache(file, ctx, p->name, p->len,
2185 proc_pident_instantiate, task, p))
2190 put_task_struct(task);
2194 #ifdef CONFIG_SECURITY
2195 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2196 size_t count, loff_t *ppos)
2198 struct inode * inode = file_inode(file);
2201 struct task_struct *task = get_proc_task(inode);
2206 length = security_getprocattr(task,
2207 (char*)file->f_path.dentry->d_name.name,
2209 put_task_struct(task);
2211 length = simple_read_from_buffer(buf, count, ppos, p, length);
2216 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2217 size_t count, loff_t *ppos)
2219 struct inode * inode = file_inode(file);
2222 struct task_struct *task = get_proc_task(inode);
2227 if (count > PAGE_SIZE)
2230 /* No partial writes. */
2236 page = (char*)__get_free_page(GFP_TEMPORARY);
2241 if (copy_from_user(page, buf, count))
2244 /* Guard against adverse ptrace interaction */
2245 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2249 length = security_setprocattr(task,
2250 (char*)file->f_path.dentry->d_name.name,
2251 (void*)page, count);
2252 mutex_unlock(&task->signal->cred_guard_mutex);
2254 free_page((unsigned long) page);
2256 put_task_struct(task);
2261 static const struct file_operations proc_pid_attr_operations = {
2262 .read = proc_pid_attr_read,
2263 .write = proc_pid_attr_write,
2264 .llseek = generic_file_llseek,
2267 static const struct pid_entry attr_dir_stuff[] = {
2268 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2269 REG("prev", S_IRUGO, proc_pid_attr_operations),
2270 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2271 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2272 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2273 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2276 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2278 return proc_pident_readdir(file, ctx,
2279 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2282 static const struct file_operations proc_attr_dir_operations = {
2283 .read = generic_read_dir,
2284 .iterate = proc_attr_dir_readdir,
2285 .llseek = default_llseek,
2288 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2289 struct dentry *dentry, unsigned int flags)
2291 return proc_pident_lookup(dir, dentry,
2292 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2295 static const struct inode_operations proc_attr_dir_inode_operations = {
2296 .lookup = proc_attr_dir_lookup,
2297 .getattr = pid_getattr,
2298 .setattr = proc_setattr,
2303 #ifdef CONFIG_ELF_CORE
2304 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2305 size_t count, loff_t *ppos)
2307 struct task_struct *task = get_proc_task(file_inode(file));
2308 struct mm_struct *mm;
2309 char buffer[PROC_NUMBUF];
2317 mm = get_task_mm(task);
2319 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2320 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2321 MMF_DUMP_FILTER_SHIFT));
2323 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2326 put_task_struct(task);
2331 static ssize_t proc_coredump_filter_write(struct file *file,
2332 const char __user *buf,
2336 struct task_struct *task;
2337 struct mm_struct *mm;
2338 char buffer[PROC_NUMBUF], *end;
2345 memset(buffer, 0, sizeof(buffer));
2346 if (count > sizeof(buffer) - 1)
2347 count = sizeof(buffer) - 1;
2348 if (copy_from_user(buffer, buf, count))
2352 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2355 if (end - buffer == 0)
2359 task = get_proc_task(file_inode(file));
2364 mm = get_task_mm(task);
2368 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2370 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2372 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2377 put_task_struct(task);
2382 static const struct file_operations proc_coredump_filter_operations = {
2383 .read = proc_coredump_filter_read,
2384 .write = proc_coredump_filter_write,
2385 .llseek = generic_file_llseek,
2389 #ifdef CONFIG_TASK_IO_ACCOUNTING
2390 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2392 struct task_io_accounting acct = task->ioac;
2393 unsigned long flags;
2396 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2400 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2405 if (whole && lock_task_sighand(task, &flags)) {
2406 struct task_struct *t = task;
2408 task_io_accounting_add(&acct, &task->signal->ioac);
2409 while_each_thread(task, t)
2410 task_io_accounting_add(&acct, &t->ioac);
2412 unlock_task_sighand(task, &flags);
2414 result = sprintf(buffer,
2419 "read_bytes: %llu\n"
2420 "write_bytes: %llu\n"
2421 "cancelled_write_bytes: %llu\n",
2422 (unsigned long long)acct.rchar,
2423 (unsigned long long)acct.wchar,
2424 (unsigned long long)acct.syscr,
2425 (unsigned long long)acct.syscw,
2426 (unsigned long long)acct.read_bytes,
2427 (unsigned long long)acct.write_bytes,
2428 (unsigned long long)acct.cancelled_write_bytes);
2430 mutex_unlock(&task->signal->cred_guard_mutex);
2434 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2436 return do_io_accounting(task, buffer, 0);
2439 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2441 return do_io_accounting(task, buffer, 1);
2443 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2445 #ifdef CONFIG_USER_NS
2446 static int proc_id_map_open(struct inode *inode, struct file *file,
2447 const struct seq_operations *seq_ops)
2449 struct user_namespace *ns = NULL;
2450 struct task_struct *task;
2451 struct seq_file *seq;
2454 task = get_proc_task(inode);
2457 ns = get_user_ns(task_cred_xxx(task, user_ns));
2459 put_task_struct(task);
2464 ret = seq_open(file, seq_ops);
2468 seq = file->private_data;
2478 static int proc_id_map_release(struct inode *inode, struct file *file)
2480 struct seq_file *seq = file->private_data;
2481 struct user_namespace *ns = seq->private;
2483 return seq_release(inode, file);
2486 static int proc_uid_map_open(struct inode *inode, struct file *file)
2488 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2491 static int proc_gid_map_open(struct inode *inode, struct file *file)
2493 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2496 static int proc_projid_map_open(struct inode *inode, struct file *file)
2498 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2501 static const struct file_operations proc_uid_map_operations = {
2502 .open = proc_uid_map_open,
2503 .write = proc_uid_map_write,
2505 .llseek = seq_lseek,
2506 .release = proc_id_map_release,
2509 static const struct file_operations proc_gid_map_operations = {
2510 .open = proc_gid_map_open,
2511 .write = proc_gid_map_write,
2513 .llseek = seq_lseek,
2514 .release = proc_id_map_release,
2517 static const struct file_operations proc_projid_map_operations = {
2518 .open = proc_projid_map_open,
2519 .write = proc_projid_map_write,
2521 .llseek = seq_lseek,
2522 .release = proc_id_map_release,
2524 #endif /* CONFIG_USER_NS */
2526 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2527 struct pid *pid, struct task_struct *task)
2529 int err = lock_trace(task);
2531 seq_printf(m, "%08x\n", task->personality);
2540 static const struct file_operations proc_task_operations;
2541 static const struct inode_operations proc_task_inode_operations;
2543 static const struct pid_entry tgid_base_stuff[] = {
2544 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2545 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2546 #ifdef CONFIG_CHECKPOINT_RESTORE
2547 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2549 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2550 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2552 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2554 REG("environ", S_IRUSR, proc_environ_operations),
2555 ONE("auxv", S_IRUSR, proc_pid_auxv),
2556 ONE("status", S_IRUGO, proc_pid_status),
2557 ONE("personality", S_IRUSR, proc_pid_personality),
2558 ONE("limits", S_IRUGO, proc_pid_limits),
2559 #ifdef CONFIG_SCHED_DEBUG
2560 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2562 #ifdef CONFIG_SCHED_AUTOGROUP
2563 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2565 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2566 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2567 INF("syscall", S_IRUSR, proc_pid_syscall),
2569 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2570 ONE("stat", S_IRUGO, proc_tgid_stat),
2571 ONE("statm", S_IRUGO, proc_pid_statm),
2572 REG("maps", S_IRUGO, proc_pid_maps_operations),
2574 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2576 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2577 LNK("cwd", proc_cwd_link),
2578 LNK("root", proc_root_link),
2579 LNK("exe", proc_exe_link),
2580 REG("mounts", S_IRUGO, proc_mounts_operations),
2581 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2582 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2583 #ifdef CONFIG_PROC_PAGE_MONITOR
2584 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2585 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2586 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2588 #ifdef CONFIG_SECURITY
2589 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2591 #ifdef CONFIG_KALLSYMS
2592 INF("wchan", S_IRUGO, proc_pid_wchan),
2594 #ifdef CONFIG_STACKTRACE
2595 ONE("stack", S_IRUSR, proc_pid_stack),
2597 #ifdef CONFIG_SCHEDSTATS
2598 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2600 #ifdef CONFIG_LATENCYTOP
2601 REG("latency", S_IRUGO, proc_lstats_operations),
2603 #ifdef CONFIG_PROC_PID_CPUSET
2604 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2606 #ifdef CONFIG_CGROUPS
2607 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2609 INF("oom_score", S_IRUGO, proc_oom_score),
2610 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2611 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2612 #ifdef CONFIG_AUDITSYSCALL
2613 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2614 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2616 #ifdef CONFIG_FAULT_INJECTION
2617 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2619 #ifdef CONFIG_ELF_CORE
2620 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2622 #ifdef CONFIG_TASK_IO_ACCOUNTING
2623 INF("io", S_IRUSR, proc_tgid_io_accounting),
2625 #ifdef CONFIG_HARDWALL
2626 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2628 #ifdef CONFIG_USER_NS
2629 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2630 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2631 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2633 #ifdef CONFIG_CHECKPOINT_RESTORE
2634 REG("timers", S_IRUGO, proc_timers_operations),
2638 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2640 return proc_pident_readdir(file, ctx,
2641 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2644 static const struct file_operations proc_tgid_base_operations = {
2645 .read = generic_read_dir,
2646 .iterate = proc_tgid_base_readdir,
2647 .llseek = default_llseek,
2650 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2652 return proc_pident_lookup(dir, dentry,
2653 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2656 static const struct inode_operations proc_tgid_base_inode_operations = {
2657 .lookup = proc_tgid_base_lookup,
2658 .getattr = pid_getattr,
2659 .setattr = proc_setattr,
2660 .permission = proc_pid_permission,
2663 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2665 struct dentry *dentry, *leader, *dir;
2666 char buf[PROC_NUMBUF];
2670 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2671 /* no ->d_hash() rejects on procfs */
2672 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2674 shrink_dcache_parent(dentry);
2680 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2681 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2686 name.len = strlen(name.name);
2687 dir = d_hash_and_lookup(leader, &name);
2689 goto out_put_leader;
2692 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2693 dentry = d_hash_and_lookup(dir, &name);
2695 shrink_dcache_parent(dentry);
2708 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2709 * @task: task that should be flushed.
2711 * When flushing dentries from proc, one needs to flush them from global
2712 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2713 * in. This call is supposed to do all of this job.
2715 * Looks in the dcache for
2717 * /proc/@tgid/task/@pid
2718 * if either directory is present flushes it and all of it'ts children
2721 * It is safe and reasonable to cache /proc entries for a task until
2722 * that task exits. After that they just clog up the dcache with
2723 * useless entries, possibly causing useful dcache entries to be
2724 * flushed instead. This routine is proved to flush those useless
2725 * dcache entries at process exit time.
2727 * NOTE: This routine is just an optimization so it does not guarantee
2728 * that no dcache entries will exist at process exit time it
2729 * just makes it very unlikely that any will persist.
2732 void proc_flush_task(struct task_struct *task)
2735 struct pid *pid, *tgid;
2738 pid = task_pid(task);
2739 tgid = task_tgid(task);
2741 for (i = 0; i <= pid->level; i++) {
2742 upid = &pid->numbers[i];
2743 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2744 tgid->numbers[i].nr);
2748 static int proc_pid_instantiate(struct inode *dir,
2749 struct dentry * dentry,
2750 struct task_struct *task, const void *ptr)
2752 struct inode *inode;
2754 inode = proc_pid_make_inode(dir->i_sb, task);
2758 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2759 inode->i_op = &proc_tgid_base_inode_operations;
2760 inode->i_fop = &proc_tgid_base_operations;
2761 inode->i_flags|=S_IMMUTABLE;
2763 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2764 ARRAY_SIZE(tgid_base_stuff)));
2766 d_set_d_op(dentry, &pid_dentry_operations);
2768 d_add(dentry, inode);
2769 /* Close the race of the process dying before we return the dentry */
2770 if (pid_revalidate(dentry, 0))
2776 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2778 int result = -ENOENT;
2779 struct task_struct *task;
2781 struct pid_namespace *ns;
2783 tgid = name_to_int(&dentry->d_name);
2787 ns = dentry->d_sb->s_fs_info;
2789 task = find_task_by_pid_ns(tgid, ns);
2791 get_task_struct(task);
2796 result = proc_pid_instantiate(dir, dentry, task, NULL);
2797 put_task_struct(task);
2799 return ERR_PTR(result);
2803 * Find the first task with tgid >= tgid
2808 struct task_struct *task;
2810 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2815 put_task_struct(iter.task);
2819 pid = find_ge_pid(iter.tgid, ns);
2821 iter.tgid = pid_nr_ns(pid, ns);
2822 iter.task = pid_task(pid, PIDTYPE_PID);
2823 /* What we to know is if the pid we have find is the
2824 * pid of a thread_group_leader. Testing for task
2825 * being a thread_group_leader is the obvious thing
2826 * todo but there is a window when it fails, due to
2827 * the pid transfer logic in de_thread.
2829 * So we perform the straight forward test of seeing
2830 * if the pid we have found is the pid of a thread
2831 * group leader, and don't worry if the task we have
2832 * found doesn't happen to be a thread group leader.
2833 * As we don't care in the case of readdir.
2835 if (!iter.task || !has_group_leader_pid(iter.task)) {
2839 get_task_struct(iter.task);
2845 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2847 /* for the /proc/ directory itself, after non-process stuff has been done */
2848 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2850 struct tgid_iter iter;
2851 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2852 loff_t pos = ctx->pos;
2854 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2857 if (pos == TGID_OFFSET - 1) {
2858 struct inode *inode = ns->proc_self->d_inode;
2859 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2863 iter.tgid = pos - TGID_OFFSET;
2866 for (iter = next_tgid(ns, iter);
2868 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2869 char name[PROC_NUMBUF];
2871 if (!has_pid_permissions(ns, iter.task, 2))
2874 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2875 ctx->pos = iter.tgid + TGID_OFFSET;
2876 if (!proc_fill_cache(file, ctx, name, len,
2877 proc_pid_instantiate, iter.task, NULL)) {
2878 put_task_struct(iter.task);
2882 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2889 static const struct pid_entry tid_base_stuff[] = {
2890 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2891 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2892 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2893 REG("environ", S_IRUSR, proc_environ_operations),
2894 ONE("auxv", S_IRUSR, proc_pid_auxv),
2895 ONE("status", S_IRUGO, proc_pid_status),
2896 ONE("personality", S_IRUSR, proc_pid_personality),
2897 ONE("limits", S_IRUGO, proc_pid_limits),
2898 #ifdef CONFIG_SCHED_DEBUG
2899 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2901 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2902 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2903 INF("syscall", S_IRUSR, proc_pid_syscall),
2905 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2906 ONE("stat", S_IRUGO, proc_tid_stat),
2907 ONE("statm", S_IRUGO, proc_pid_statm),
2908 REG("maps", S_IRUGO, proc_tid_maps_operations),
2909 #ifdef CONFIG_CHECKPOINT_RESTORE
2910 REG("children", S_IRUGO, proc_tid_children_operations),
2913 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2915 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2916 LNK("cwd", proc_cwd_link),
2917 LNK("root", proc_root_link),
2918 LNK("exe", proc_exe_link),
2919 REG("mounts", S_IRUGO, proc_mounts_operations),
2920 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2921 #ifdef CONFIG_PROC_PAGE_MONITOR
2922 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2923 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2924 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2926 #ifdef CONFIG_SECURITY
2927 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2929 #ifdef CONFIG_KALLSYMS
2930 INF("wchan", S_IRUGO, proc_pid_wchan),
2932 #ifdef CONFIG_STACKTRACE
2933 ONE("stack", S_IRUSR, proc_pid_stack),
2935 #ifdef CONFIG_SCHEDSTATS
2936 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2938 #ifdef CONFIG_LATENCYTOP
2939 REG("latency", S_IRUGO, proc_lstats_operations),
2941 #ifdef CONFIG_PROC_PID_CPUSET
2942 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2944 #ifdef CONFIG_CGROUPS
2945 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2947 INF("oom_score", S_IRUGO, proc_oom_score),
2948 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2949 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2950 #ifdef CONFIG_AUDITSYSCALL
2951 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2952 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2954 #ifdef CONFIG_FAULT_INJECTION
2955 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2957 #ifdef CONFIG_TASK_IO_ACCOUNTING
2958 INF("io", S_IRUSR, proc_tid_io_accounting),
2960 #ifdef CONFIG_HARDWALL
2961 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2963 #ifdef CONFIG_USER_NS
2964 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2965 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2966 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2970 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2972 return proc_pident_readdir(file, ctx,
2973 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2976 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2978 return proc_pident_lookup(dir, dentry,
2979 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2982 static const struct file_operations proc_tid_base_operations = {
2983 .read = generic_read_dir,
2984 .iterate = proc_tid_base_readdir,
2985 .llseek = default_llseek,
2988 static const struct inode_operations proc_tid_base_inode_operations = {
2989 .lookup = proc_tid_base_lookup,
2990 .getattr = pid_getattr,
2991 .setattr = proc_setattr,
2994 static int proc_task_instantiate(struct inode *dir,
2995 struct dentry *dentry, struct task_struct *task, const void *ptr)
2997 struct inode *inode;
2998 inode = proc_pid_make_inode(dir->i_sb, task);
3002 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3003 inode->i_op = &proc_tid_base_inode_operations;
3004 inode->i_fop = &proc_tid_base_operations;
3005 inode->i_flags|=S_IMMUTABLE;
3007 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3008 ARRAY_SIZE(tid_base_stuff)));
3010 d_set_d_op(dentry, &pid_dentry_operations);
3012 d_add(dentry, inode);
3013 /* Close the race of the process dying before we return the dentry */
3014 if (pid_revalidate(dentry, 0))
3020 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3022 int result = -ENOENT;
3023 struct task_struct *task;
3024 struct task_struct *leader = get_proc_task(dir);
3026 struct pid_namespace *ns;
3031 tid = name_to_int(&dentry->d_name);
3035 ns = dentry->d_sb->s_fs_info;
3037 task = find_task_by_pid_ns(tid, ns);
3039 get_task_struct(task);
3043 if (!same_thread_group(leader, task))
3046 result = proc_task_instantiate(dir, dentry, task, NULL);
3048 put_task_struct(task);
3050 put_task_struct(leader);
3052 return ERR_PTR(result);
3056 * Find the first tid of a thread group to return to user space.
3058 * Usually this is just the thread group leader, but if the users
3059 * buffer was too small or there was a seek into the middle of the
3060 * directory we have more work todo.
3062 * In the case of a short read we start with find_task_by_pid.
3064 * In the case of a seek we start with the leader and walk nr
3067 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3068 struct pid_namespace *ns)
3070 struct task_struct *pos, *task;
3071 unsigned long nr = f_pos;
3073 if (nr != f_pos) /* 32bit overflow? */
3077 task = pid_task(pid, PIDTYPE_PID);
3081 /* Attempt to start with the tid of a thread */
3083 pos = find_task_by_pid_ns(tid, ns);
3084 if (pos && same_thread_group(pos, task))
3088 /* If nr exceeds the number of threads there is nothing todo */
3089 if (nr >= get_nr_threads(task))
3092 /* If we haven't found our starting place yet start
3093 * with the leader and walk nr threads forward.
3095 pos = task = task->group_leader;
3099 } while_each_thread(task, pos);
3104 get_task_struct(pos);
3111 * Find the next thread in the thread list.
3112 * Return NULL if there is an error or no next thread.
3114 * The reference to the input task_struct is released.
3116 static struct task_struct *next_tid(struct task_struct *start)
3118 struct task_struct *pos = NULL;
3120 if (pid_alive(start)) {
3121 pos = next_thread(start);
3122 if (thread_group_leader(pos))
3125 get_task_struct(pos);
3128 put_task_struct(start);
3132 /* for the /proc/TGID/task/ directories */
3133 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3135 struct inode *inode = file_inode(file);
3136 struct task_struct *task;
3137 struct pid_namespace *ns;
3140 if (proc_inode_is_dead(inode))
3143 if (!dir_emit_dots(file, ctx))
3146 /* f_version caches the tgid value that the last readdir call couldn't
3147 * return. lseek aka telldir automagically resets f_version to 0.
3149 ns = file->f_dentry->d_sb->s_fs_info;
3150 tid = (int)file->f_version;
3151 file->f_version = 0;
3152 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3154 task = next_tid(task), ctx->pos++) {
3155 char name[PROC_NUMBUF];
3157 tid = task_pid_nr_ns(task, ns);
3158 len = snprintf(name, sizeof(name), "%d", tid);
3159 if (!proc_fill_cache(file, ctx, name, len,
3160 proc_task_instantiate, task, NULL)) {
3161 /* returning this tgid failed, save it as the first
3162 * pid for the next readir call */
3163 file->f_version = (u64)tid;
3164 put_task_struct(task);
3172 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3174 struct inode *inode = dentry->d_inode;
3175 struct task_struct *p = get_proc_task(inode);
3176 generic_fillattr(inode, stat);
3179 stat->nlink += get_nr_threads(p);
3186 static const struct inode_operations proc_task_inode_operations = {
3187 .lookup = proc_task_lookup,
3188 .getattr = proc_task_getattr,
3189 .setattr = proc_setattr,
3190 .permission = proc_pid_permission,
3193 static const struct file_operations proc_task_operations = {
3194 .read = generic_read_dir,
3195 .iterate = proc_task_readdir,
3196 .llseek = default_llseek,