4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
86 #ifdef CONFIG_HARDWALL
87 #include <asm/hardwall.h>
92 * Implementing inode permission operations in /proc is almost
93 * certainly an error. Permission checks need to happen during
94 * each system call not at open time. The reason is that most of
95 * what we wish to check for permissions in /proc varies at runtime.
97 * The classic example of a problem is opening file descriptors
98 * in /proc for a task before it execs a suid executable.
105 const struct inode_operations *iop;
106 const struct file_operations *fop;
110 #define NOD(NAME, MODE, IOP, FOP, OP) { \
112 .len = sizeof(NAME) - 1, \
119 #define DIR(NAME, MODE, iops, fops) \
120 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
121 #define LNK(NAME, get_link) \
122 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
123 &proc_pid_link_inode_operations, NULL, \
124 { .proc_get_link = get_link } )
125 #define REG(NAME, MODE, fops) \
126 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
127 #define INF(NAME, MODE, read) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_info_file_operations, \
130 { .proc_read = read } )
131 #define ONE(NAME, MODE, show) \
132 NOD(NAME, (S_IFREG|(MODE)), \
133 NULL, &proc_single_file_operations, \
134 { .proc_show = show } )
137 * Count the number of hardlinks for the pid_entry table, excluding the .
140 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 for (i = 0; i < n; ++i) {
148 if (S_ISDIR(entries[i].mode))
155 static int get_task_root(struct task_struct *task, struct path *root)
157 int result = -ENOENT;
161 get_fs_root(task->fs, root);
168 static int proc_cwd_link(struct inode *inode, struct path *path)
170 struct task_struct *task = get_proc_task(inode);
171 int result = -ENOENT;
176 get_fs_pwd(task->fs, path);
180 put_task_struct(task);
185 static int proc_root_link(struct inode *inode, struct path *path)
187 struct task_struct *task = get_proc_task(inode);
188 int result = -ENOENT;
191 result = get_task_root(task, path);
192 put_task_struct(task);
197 static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
199 struct mm_struct *mm;
202 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
206 mm = get_task_mm(task);
207 if (mm && mm != current->mm &&
208 !ptrace_may_access(task, mode)) {
210 mm = ERR_PTR(-EACCES);
212 mutex_unlock(&task->signal->cred_guard_mutex);
217 struct mm_struct *mm_for_maps(struct task_struct *task)
219 return mm_access(task, PTRACE_MODE_READ);
222 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
226 struct mm_struct *mm = get_task_mm(task);
230 goto out_mm; /* Shh! No looking before we're done */
232 len = mm->arg_end - mm->arg_start;
237 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
239 // If the nul at the end of args has been overwritten, then
240 // assume application is using setproctitle(3).
241 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
242 len = strnlen(buffer, res);
246 len = mm->env_end - mm->env_start;
247 if (len > PAGE_SIZE - res)
248 len = PAGE_SIZE - res;
249 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
250 res = strnlen(buffer, res);
259 static int proc_pid_auxv(struct task_struct *task, char *buffer)
261 struct mm_struct *mm = mm_for_maps(task);
262 int res = PTR_ERR(mm);
263 if (mm && !IS_ERR(mm)) {
264 unsigned int nwords = 0;
267 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
268 res = nwords * sizeof(mm->saved_auxv[0]);
271 memcpy(buffer, mm->saved_auxv, res);
278 #ifdef CONFIG_KALLSYMS
280 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
281 * Returns the resolved symbol. If that fails, simply return the address.
283 static int proc_pid_wchan(struct task_struct *task, char *buffer)
286 char symname[KSYM_NAME_LEN];
288 wchan = get_wchan(task);
290 if (lookup_symbol_name(wchan, symname) < 0)
291 if (!ptrace_may_access(task, PTRACE_MODE_READ))
294 return sprintf(buffer, "%lu", wchan);
296 return sprintf(buffer, "%s", symname);
298 #endif /* CONFIG_KALLSYMS */
300 static int lock_trace(struct task_struct *task)
302 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
305 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
306 mutex_unlock(&task->signal->cred_guard_mutex);
312 static void unlock_trace(struct task_struct *task)
314 mutex_unlock(&task->signal->cred_guard_mutex);
317 #ifdef CONFIG_STACKTRACE
319 #define MAX_STACK_TRACE_DEPTH 64
321 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
322 struct pid *pid, struct task_struct *task)
324 struct stack_trace trace;
325 unsigned long *entries;
329 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
333 trace.nr_entries = 0;
334 trace.max_entries = MAX_STACK_TRACE_DEPTH;
335 trace.entries = entries;
338 err = lock_trace(task);
340 save_stack_trace_tsk(task, &trace);
342 for (i = 0; i < trace.nr_entries; i++) {
343 seq_printf(m, "[<%pK>] %pS\n",
344 (void *)entries[i], (void *)entries[i]);
354 #ifdef CONFIG_SCHEDSTATS
356 * Provides /proc/PID/schedstat
358 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
360 return sprintf(buffer, "%llu %llu %lu\n",
361 (unsigned long long)task->se.sum_exec_runtime,
362 (unsigned long long)task->sched_info.run_delay,
363 task->sched_info.pcount);
367 #ifdef CONFIG_LATENCYTOP
368 static int lstats_show_proc(struct seq_file *m, void *v)
371 struct inode *inode = m->private;
372 struct task_struct *task = get_proc_task(inode);
376 seq_puts(m, "Latency Top version : v0.1\n");
377 for (i = 0; i < 32; i++) {
378 struct latency_record *lr = &task->latency_record[i];
379 if (lr->backtrace[0]) {
381 seq_printf(m, "%i %li %li",
382 lr->count, lr->time, lr->max);
383 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
384 unsigned long bt = lr->backtrace[q];
389 seq_printf(m, " %ps", (void *)bt);
395 put_task_struct(task);
399 static int lstats_open(struct inode *inode, struct file *file)
401 return single_open(file, lstats_show_proc, inode);
404 static ssize_t lstats_write(struct file *file, const char __user *buf,
405 size_t count, loff_t *offs)
407 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
411 clear_all_latency_tracing(task);
412 put_task_struct(task);
417 static const struct file_operations proc_lstats_operations = {
420 .write = lstats_write,
422 .release = single_release,
427 static int proc_oom_score(struct task_struct *task, char *buffer)
429 unsigned long points = 0;
431 read_lock(&tasklist_lock);
433 points = oom_badness(task, NULL, NULL,
434 totalram_pages + total_swap_pages);
435 read_unlock(&tasklist_lock);
436 return sprintf(buffer, "%lu\n", points);
444 static const struct limit_names lnames[RLIM_NLIMITS] = {
445 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
446 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
447 [RLIMIT_DATA] = {"Max data size", "bytes"},
448 [RLIMIT_STACK] = {"Max stack size", "bytes"},
449 [RLIMIT_CORE] = {"Max core file size", "bytes"},
450 [RLIMIT_RSS] = {"Max resident set", "bytes"},
451 [RLIMIT_NPROC] = {"Max processes", "processes"},
452 [RLIMIT_NOFILE] = {"Max open files", "files"},
453 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
454 [RLIMIT_AS] = {"Max address space", "bytes"},
455 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
456 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
457 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
458 [RLIMIT_NICE] = {"Max nice priority", NULL},
459 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
460 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
463 /* Display limits for a process */
464 static int proc_pid_limits(struct task_struct *task, char *buffer)
469 char *bufptr = buffer;
471 struct rlimit rlim[RLIM_NLIMITS];
473 if (!lock_task_sighand(task, &flags))
475 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
476 unlock_task_sighand(task, &flags);
479 * print the file header
481 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
482 "Limit", "Soft Limit", "Hard Limit", "Units");
484 for (i = 0; i < RLIM_NLIMITS; i++) {
485 if (rlim[i].rlim_cur == RLIM_INFINITY)
486 count += sprintf(&bufptr[count], "%-25s %-20s ",
487 lnames[i].name, "unlimited");
489 count += sprintf(&bufptr[count], "%-25s %-20lu ",
490 lnames[i].name, rlim[i].rlim_cur);
492 if (rlim[i].rlim_max == RLIM_INFINITY)
493 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
495 count += sprintf(&bufptr[count], "%-20lu ",
499 count += sprintf(&bufptr[count], "%-10s\n",
502 count += sprintf(&bufptr[count], "\n");
508 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
509 static int proc_pid_syscall(struct task_struct *task, char *buffer)
512 unsigned long args[6], sp, pc;
513 int res = lock_trace(task);
517 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
518 res = sprintf(buffer, "running\n");
520 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
522 res = sprintf(buffer,
523 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
525 args[0], args[1], args[2], args[3], args[4], args[5],
530 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
532 /************************************************************************/
533 /* Here the fs part begins */
534 /************************************************************************/
536 /* permission checks */
537 static int proc_fd_access_allowed(struct inode *inode)
539 struct task_struct *task;
541 /* Allow access to a task's file descriptors if it is us or we
542 * may use ptrace attach to the process and find out that
545 task = get_proc_task(inode);
547 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
548 put_task_struct(task);
553 int proc_setattr(struct dentry *dentry, struct iattr *attr)
556 struct inode *inode = dentry->d_inode;
558 if (attr->ia_valid & ATTR_MODE)
561 error = inode_change_ok(inode, attr);
565 if ((attr->ia_valid & ATTR_SIZE) &&
566 attr->ia_size != i_size_read(inode)) {
567 error = vmtruncate(inode, attr->ia_size);
572 setattr_copy(inode, attr);
573 mark_inode_dirty(inode);
577 static const struct inode_operations proc_def_inode_operations = {
578 .setattr = proc_setattr,
581 static int mounts_open_common(struct inode *inode, struct file *file,
582 const struct seq_operations *op)
584 struct task_struct *task = get_proc_task(inode);
586 struct mnt_namespace *ns = NULL;
588 struct proc_mounts *p;
593 nsp = task_nsproxy(task);
600 if (ns && get_task_root(task, &root) == 0)
602 put_task_struct(task);
611 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
615 file->private_data = &p->m;
616 ret = seq_open(file, op);
623 p->m.poll_event = ns->event;
637 static int mounts_release(struct inode *inode, struct file *file)
639 struct proc_mounts *p = file->private_data;
642 return seq_release(inode, file);
645 static unsigned mounts_poll(struct file *file, poll_table *wait)
647 struct proc_mounts *p = file->private_data;
648 unsigned res = POLLIN | POLLRDNORM;
650 poll_wait(file, &p->ns->poll, wait);
651 if (mnt_had_events(p))
652 res |= POLLERR | POLLPRI;
657 static int mounts_open(struct inode *inode, struct file *file)
659 return mounts_open_common(inode, file, &mounts_op);
662 static const struct file_operations proc_mounts_operations = {
666 .release = mounts_release,
670 static int mountinfo_open(struct inode *inode, struct file *file)
672 return mounts_open_common(inode, file, &mountinfo_op);
675 static const struct file_operations proc_mountinfo_operations = {
676 .open = mountinfo_open,
679 .release = mounts_release,
683 static int mountstats_open(struct inode *inode, struct file *file)
685 return mounts_open_common(inode, file, &mountstats_op);
688 static const struct file_operations proc_mountstats_operations = {
689 .open = mountstats_open,
692 .release = mounts_release,
695 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
697 static ssize_t proc_info_read(struct file * file, char __user * buf,
698 size_t count, loff_t *ppos)
700 struct inode * inode = file->f_path.dentry->d_inode;
703 struct task_struct *task = get_proc_task(inode);
709 if (count > PROC_BLOCK_SIZE)
710 count = PROC_BLOCK_SIZE;
713 if (!(page = __get_free_page(GFP_TEMPORARY)))
716 length = PROC_I(inode)->op.proc_read(task, (char*)page);
719 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
722 put_task_struct(task);
727 static const struct file_operations proc_info_file_operations = {
728 .read = proc_info_read,
729 .llseek = generic_file_llseek,
732 static int proc_single_show(struct seq_file *m, void *v)
734 struct inode *inode = m->private;
735 struct pid_namespace *ns;
737 struct task_struct *task;
740 ns = inode->i_sb->s_fs_info;
741 pid = proc_pid(inode);
742 task = get_pid_task(pid, PIDTYPE_PID);
746 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
748 put_task_struct(task);
752 static int proc_single_open(struct inode *inode, struct file *filp)
754 return single_open(filp, proc_single_show, inode);
757 static const struct file_operations proc_single_file_operations = {
758 .open = proc_single_open,
761 .release = single_release,
764 static int mem_open(struct inode* inode, struct file* file)
766 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
767 struct mm_struct *mm;
772 mm = mm_access(task, PTRACE_MODE_ATTACH);
773 put_task_struct(task);
779 /* ensure this mm_struct can't be freed */
780 atomic_inc(&mm->mm_count);
781 /* but do not pin its memory */
785 /* OK to pass negative loff_t, we can catch out-of-range */
786 file->f_mode |= FMODE_UNSIGNED_OFFSET;
787 file->private_data = mm;
792 static ssize_t mem_rw(struct file *file, char __user *buf,
793 size_t count, loff_t *ppos, int write)
795 struct mm_struct *mm = file->private_data;
796 unsigned long addr = *ppos;
803 page = (char *)__get_free_page(GFP_TEMPORARY);
808 if (!atomic_inc_not_zero(&mm->mm_users))
812 int this_len = min_t(int, count, PAGE_SIZE);
814 if (write && copy_from_user(page, buf, this_len)) {
819 this_len = access_remote_vm(mm, addr, page, this_len, write);
826 if (!write && copy_to_user(buf, page, this_len)) {
840 free_page((unsigned long) page);
844 static ssize_t mem_read(struct file *file, char __user *buf,
845 size_t count, loff_t *ppos)
847 return mem_rw(file, buf, count, ppos, 0);
850 static ssize_t mem_write(struct file *file, const char __user *buf,
851 size_t count, loff_t *ppos)
853 return mem_rw(file, (char __user*)buf, count, ppos, 1);
856 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
860 file->f_pos = offset;
863 file->f_pos += offset;
868 force_successful_syscall_return();
872 static int mem_release(struct inode *inode, struct file *file)
874 struct mm_struct *mm = file->private_data;
880 static const struct file_operations proc_mem_operations = {
885 .release = mem_release,
888 static ssize_t environ_read(struct file *file, char __user *buf,
889 size_t count, loff_t *ppos)
891 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
893 unsigned long src = *ppos;
895 struct mm_struct *mm;
901 page = (char *)__get_free_page(GFP_TEMPORARY);
906 mm = mm_for_maps(task);
908 if (!mm || IS_ERR(mm))
913 int this_len, retval, max_len;
915 this_len = mm->env_end - (mm->env_start + src);
920 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
921 this_len = (this_len > max_len) ? max_len : this_len;
923 retval = access_process_vm(task, (mm->env_start + src),
931 if (copy_to_user(buf, page, retval)) {
945 free_page((unsigned long) page);
947 put_task_struct(task);
952 static const struct file_operations proc_environ_operations = {
953 .read = environ_read,
954 .llseek = generic_file_llseek,
957 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
958 size_t count, loff_t *ppos)
960 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
961 char buffer[PROC_NUMBUF];
963 int oom_adjust = OOM_DISABLE;
969 if (lock_task_sighand(task, &flags)) {
970 oom_adjust = task->signal->oom_adj;
971 unlock_task_sighand(task, &flags);
974 put_task_struct(task);
976 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
978 return simple_read_from_buffer(buf, count, ppos, buffer, len);
981 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
982 size_t count, loff_t *ppos)
984 struct task_struct *task;
985 char buffer[PROC_NUMBUF];
990 memset(buffer, 0, sizeof(buffer));
991 if (count > sizeof(buffer) - 1)
992 count = sizeof(buffer) - 1;
993 if (copy_from_user(buffer, buf, count)) {
998 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1001 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1002 oom_adjust != OOM_DISABLE) {
1007 task = get_proc_task(file->f_path.dentry->d_inode);
1019 if (!lock_task_sighand(task, &flags)) {
1024 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1030 * Warn that /proc/pid/oom_adj is deprecated, see
1031 * Documentation/feature-removal-schedule.txt.
1033 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1034 current->comm, task_pid_nr(current), task_pid_nr(task),
1036 task->signal->oom_adj = oom_adjust;
1038 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1039 * value is always attainable.
1041 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1042 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1044 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
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_adjust_operations = {
1056 .read = oom_adjust_read,
1057 .write = oom_adjust_write,
1058 .llseek = generic_file_llseek,
1061 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1062 size_t count, loff_t *ppos)
1064 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1065 char buffer[PROC_NUMBUF];
1066 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1067 unsigned long flags;
1072 if (lock_task_sighand(task, &flags)) {
1073 oom_score_adj = task->signal->oom_score_adj;
1074 unlock_task_sighand(task, &flags);
1076 put_task_struct(task);
1077 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1078 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1081 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1082 size_t count, loff_t *ppos)
1084 struct task_struct *task;
1085 char buffer[PROC_NUMBUF];
1086 unsigned long flags;
1090 memset(buffer, 0, sizeof(buffer));
1091 if (count > sizeof(buffer) - 1)
1092 count = sizeof(buffer) - 1;
1093 if (copy_from_user(buffer, buf, count)) {
1098 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1101 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1102 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1107 task = get_proc_task(file->f_path.dentry->d_inode);
1119 if (!lock_task_sighand(task, &flags)) {
1124 if (oom_score_adj < task->signal->oom_score_adj_min &&
1125 !capable(CAP_SYS_RESOURCE)) {
1130 task->signal->oom_score_adj = oom_score_adj;
1131 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1132 task->signal->oom_score_adj_min = oom_score_adj;
1134 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1135 * always attainable.
1137 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1138 task->signal->oom_adj = OOM_DISABLE;
1140 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1143 unlock_task_sighand(task, &flags);
1146 put_task_struct(task);
1148 return err < 0 ? err : count;
1151 static const struct file_operations proc_oom_score_adj_operations = {
1152 .read = oom_score_adj_read,
1153 .write = oom_score_adj_write,
1154 .llseek = default_llseek,
1157 #ifdef CONFIG_AUDITSYSCALL
1158 #define TMPBUFLEN 21
1159 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1160 size_t count, loff_t *ppos)
1162 struct inode * inode = file->f_path.dentry->d_inode;
1163 struct task_struct *task = get_proc_task(inode);
1165 char tmpbuf[TMPBUFLEN];
1169 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1170 audit_get_loginuid(task));
1171 put_task_struct(task);
1172 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1175 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1176 size_t count, loff_t *ppos)
1178 struct inode * inode = file->f_path.dentry->d_inode;
1183 if (!capable(CAP_AUDIT_CONTROL))
1187 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1193 if (count >= PAGE_SIZE)
1194 count = PAGE_SIZE - 1;
1197 /* No partial writes. */
1200 page = (char*)__get_free_page(GFP_TEMPORARY);
1204 if (copy_from_user(page, buf, count))
1208 loginuid = simple_strtoul(page, &tmp, 10);
1214 length = audit_set_loginuid(current, loginuid);
1215 if (likely(length == 0))
1219 free_page((unsigned long) page);
1223 static const struct file_operations proc_loginuid_operations = {
1224 .read = proc_loginuid_read,
1225 .write = proc_loginuid_write,
1226 .llseek = generic_file_llseek,
1229 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1230 size_t count, loff_t *ppos)
1232 struct inode * inode = file->f_path.dentry->d_inode;
1233 struct task_struct *task = get_proc_task(inode);
1235 char tmpbuf[TMPBUFLEN];
1239 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1240 audit_get_sessionid(task));
1241 put_task_struct(task);
1242 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1245 static const struct file_operations proc_sessionid_operations = {
1246 .read = proc_sessionid_read,
1247 .llseek = generic_file_llseek,
1251 #ifdef CONFIG_FAULT_INJECTION
1252 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1253 size_t count, loff_t *ppos)
1255 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1256 char buffer[PROC_NUMBUF];
1262 make_it_fail = task->make_it_fail;
1263 put_task_struct(task);
1265 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1267 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1270 static ssize_t proc_fault_inject_write(struct file * file,
1271 const char __user * buf, size_t count, loff_t *ppos)
1273 struct task_struct *task;
1274 char buffer[PROC_NUMBUF], *end;
1277 if (!capable(CAP_SYS_RESOURCE))
1279 memset(buffer, 0, sizeof(buffer));
1280 if (count > sizeof(buffer) - 1)
1281 count = sizeof(buffer) - 1;
1282 if (copy_from_user(buffer, buf, count))
1284 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1287 task = get_proc_task(file->f_dentry->d_inode);
1290 task->make_it_fail = make_it_fail;
1291 put_task_struct(task);
1296 static const struct file_operations proc_fault_inject_operations = {
1297 .read = proc_fault_inject_read,
1298 .write = proc_fault_inject_write,
1299 .llseek = generic_file_llseek,
1304 #ifdef CONFIG_SCHED_DEBUG
1306 * Print out various scheduling related per-task fields:
1308 static int sched_show(struct seq_file *m, void *v)
1310 struct inode *inode = m->private;
1311 struct task_struct *p;
1313 p = get_proc_task(inode);
1316 proc_sched_show_task(p, m);
1324 sched_write(struct file *file, const char __user *buf,
1325 size_t count, loff_t *offset)
1327 struct inode *inode = file->f_path.dentry->d_inode;
1328 struct task_struct *p;
1330 p = get_proc_task(inode);
1333 proc_sched_set_task(p);
1340 static int sched_open(struct inode *inode, struct file *filp)
1342 return single_open(filp, sched_show, inode);
1345 static const struct file_operations proc_pid_sched_operations = {
1348 .write = sched_write,
1349 .llseek = seq_lseek,
1350 .release = single_release,
1355 #ifdef CONFIG_SCHED_AUTOGROUP
1357 * Print out autogroup related information:
1359 static int sched_autogroup_show(struct seq_file *m, void *v)
1361 struct inode *inode = m->private;
1362 struct task_struct *p;
1364 p = get_proc_task(inode);
1367 proc_sched_autogroup_show_task(p, m);
1375 sched_autogroup_write(struct file *file, const char __user *buf,
1376 size_t count, loff_t *offset)
1378 struct inode *inode = file->f_path.dentry->d_inode;
1379 struct task_struct *p;
1380 char buffer[PROC_NUMBUF];
1384 memset(buffer, 0, sizeof(buffer));
1385 if (count > sizeof(buffer) - 1)
1386 count = sizeof(buffer) - 1;
1387 if (copy_from_user(buffer, buf, count))
1390 err = kstrtoint(strstrip(buffer), 0, &nice);
1394 p = get_proc_task(inode);
1399 err = proc_sched_autogroup_set_nice(p, &err);
1408 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1412 ret = single_open(filp, sched_autogroup_show, NULL);
1414 struct seq_file *m = filp->private_data;
1421 static const struct file_operations proc_pid_sched_autogroup_operations = {
1422 .open = sched_autogroup_open,
1424 .write = sched_autogroup_write,
1425 .llseek = seq_lseek,
1426 .release = single_release,
1429 #endif /* CONFIG_SCHED_AUTOGROUP */
1431 static ssize_t comm_write(struct file *file, const char __user *buf,
1432 size_t count, loff_t *offset)
1434 struct inode *inode = file->f_path.dentry->d_inode;
1435 struct task_struct *p;
1436 char buffer[TASK_COMM_LEN];
1438 memset(buffer, 0, sizeof(buffer));
1439 if (count > sizeof(buffer) - 1)
1440 count = sizeof(buffer) - 1;
1441 if (copy_from_user(buffer, buf, count))
1444 p = get_proc_task(inode);
1448 if (same_thread_group(current, p))
1449 set_task_comm(p, buffer);
1458 static int comm_show(struct seq_file *m, void *v)
1460 struct inode *inode = m->private;
1461 struct task_struct *p;
1463 p = get_proc_task(inode);
1468 seq_printf(m, "%s\n", p->comm);
1476 static int comm_open(struct inode *inode, struct file *filp)
1478 return single_open(filp, comm_show, inode);
1481 static const struct file_operations proc_pid_set_comm_operations = {
1484 .write = comm_write,
1485 .llseek = seq_lseek,
1486 .release = single_release,
1489 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1491 struct task_struct *task;
1492 struct mm_struct *mm;
1493 struct file *exe_file;
1495 task = get_proc_task(inode);
1498 mm = get_task_mm(task);
1499 put_task_struct(task);
1502 exe_file = get_mm_exe_file(mm);
1505 *exe_path = exe_file->f_path;
1506 path_get(&exe_file->f_path);
1513 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1515 struct inode *inode = dentry->d_inode;
1516 int error = -EACCES;
1518 /* We don't need a base pointer in the /proc filesystem */
1519 path_put(&nd->path);
1521 /* Are we allowed to snoop on the tasks file descriptors? */
1522 if (!proc_fd_access_allowed(inode))
1525 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1527 return ERR_PTR(error);
1530 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1532 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1539 pathname = d_path(path, tmp, PAGE_SIZE);
1540 len = PTR_ERR(pathname);
1541 if (IS_ERR(pathname))
1543 len = tmp + PAGE_SIZE - 1 - pathname;
1547 if (copy_to_user(buffer, pathname, len))
1550 free_page((unsigned long)tmp);
1554 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1556 int error = -EACCES;
1557 struct inode *inode = dentry->d_inode;
1560 /* Are we allowed to snoop on the tasks file descriptors? */
1561 if (!proc_fd_access_allowed(inode))
1564 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1568 error = do_proc_readlink(&path, buffer, buflen);
1574 static const struct inode_operations proc_pid_link_inode_operations = {
1575 .readlink = proc_pid_readlink,
1576 .follow_link = proc_pid_follow_link,
1577 .setattr = proc_setattr,
1581 /* building an inode */
1583 static int task_dumpable(struct task_struct *task)
1586 struct mm_struct *mm;
1591 dumpable = get_dumpable(mm);
1598 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1600 struct inode * inode;
1601 struct proc_inode *ei;
1602 const struct cred *cred;
1604 /* We need a new inode */
1606 inode = new_inode(sb);
1612 inode->i_ino = get_next_ino();
1613 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1614 inode->i_op = &proc_def_inode_operations;
1617 * grab the reference to task.
1619 ei->pid = get_task_pid(task, PIDTYPE_PID);
1623 if (task_dumpable(task)) {
1625 cred = __task_cred(task);
1626 inode->i_uid = cred->euid;
1627 inode->i_gid = cred->egid;
1630 security_task_to_inode(task, inode);
1640 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1642 struct inode *inode = dentry->d_inode;
1643 struct task_struct *task;
1644 const struct cred *cred;
1646 generic_fillattr(inode, stat);
1651 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1653 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1654 task_dumpable(task)) {
1655 cred = __task_cred(task);
1656 stat->uid = cred->euid;
1657 stat->gid = cred->egid;
1667 * Exceptional case: normally we are not allowed to unhash a busy
1668 * directory. In this case, however, we can do it - no aliasing problems
1669 * due to the way we treat inodes.
1671 * Rewrite the inode's ownerships here because the owning task may have
1672 * performed a setuid(), etc.
1674 * Before the /proc/pid/status file was created the only way to read
1675 * the effective uid of a /process was to stat /proc/pid. Reading
1676 * /proc/pid/status is slow enough that procps and other packages
1677 * kept stating /proc/pid. To keep the rules in /proc simple I have
1678 * made this apply to all per process world readable and executable
1681 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1683 struct inode *inode;
1684 struct task_struct *task;
1685 const struct cred *cred;
1687 if (nd && nd->flags & LOOKUP_RCU)
1690 inode = dentry->d_inode;
1691 task = get_proc_task(inode);
1694 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1695 task_dumpable(task)) {
1697 cred = __task_cred(task);
1698 inode->i_uid = cred->euid;
1699 inode->i_gid = cred->egid;
1705 inode->i_mode &= ~(S_ISUID | S_ISGID);
1706 security_task_to_inode(task, inode);
1707 put_task_struct(task);
1714 static int pid_delete_dentry(const struct dentry * dentry)
1716 /* Is the task we represent dead?
1717 * If so, then don't put the dentry on the lru list,
1718 * kill it immediately.
1720 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1723 const struct dentry_operations pid_dentry_operations =
1725 .d_revalidate = pid_revalidate,
1726 .d_delete = pid_delete_dentry,
1732 * Fill a directory entry.
1734 * If possible create the dcache entry and derive our inode number and
1735 * file type from dcache entry.
1737 * Since all of the proc inode numbers are dynamically generated, the inode
1738 * numbers do not exist until the inode is cache. This means creating the
1739 * the dcache entry in readdir is necessary to keep the inode numbers
1740 * reported by readdir in sync with the inode numbers reported
1743 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1744 const char *name, int len,
1745 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1747 struct dentry *child, *dir = filp->f_path.dentry;
1748 struct inode *inode;
1751 unsigned type = DT_UNKNOWN;
1755 qname.hash = full_name_hash(name, len);
1757 child = d_lookup(dir, &qname);
1760 new = d_alloc(dir, &qname);
1762 child = instantiate(dir->d_inode, new, task, ptr);
1769 if (!child || IS_ERR(child) || !child->d_inode)
1770 goto end_instantiate;
1771 inode = child->d_inode;
1774 type = inode->i_mode >> 12;
1779 ino = find_inode_number(dir, &qname);
1782 return filldir(dirent, name, len, filp->f_pos, ino, type);
1785 static unsigned name_to_int(struct dentry *dentry)
1787 const char *name = dentry->d_name.name;
1788 int len = dentry->d_name.len;
1791 if (len > 1 && *name == '0')
1794 unsigned c = *name++ - '0';
1797 if (n >= (~0U-9)/10)
1807 #define PROC_FDINFO_MAX 64
1809 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1811 struct task_struct *task = get_proc_task(inode);
1812 struct files_struct *files = NULL;
1814 int fd = proc_fd(inode);
1817 files = get_files_struct(task);
1818 put_task_struct(task);
1822 * We are not taking a ref to the file structure, so we must
1825 spin_lock(&files->file_lock);
1826 file = fcheck_files(files, fd);
1828 unsigned int f_flags;
1829 struct fdtable *fdt;
1831 fdt = files_fdtable(files);
1832 f_flags = file->f_flags & ~O_CLOEXEC;
1833 if (close_on_exec(fd, fdt))
1834 f_flags |= O_CLOEXEC;
1837 *path = file->f_path;
1838 path_get(&file->f_path);
1841 snprintf(info, PROC_FDINFO_MAX,
1844 (long long) file->f_pos,
1846 spin_unlock(&files->file_lock);
1847 put_files_struct(files);
1850 spin_unlock(&files->file_lock);
1851 put_files_struct(files);
1856 static int proc_fd_link(struct inode *inode, struct path *path)
1858 return proc_fd_info(inode, path, NULL);
1861 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1863 struct inode *inode;
1864 struct task_struct *task;
1866 struct files_struct *files;
1867 const struct cred *cred;
1869 if (nd && nd->flags & LOOKUP_RCU)
1872 inode = dentry->d_inode;
1873 task = get_proc_task(inode);
1874 fd = proc_fd(inode);
1877 files = get_files_struct(task);
1880 if (fcheck_files(files, fd)) {
1882 put_files_struct(files);
1883 if (task_dumpable(task)) {
1885 cred = __task_cred(task);
1886 inode->i_uid = cred->euid;
1887 inode->i_gid = cred->egid;
1893 inode->i_mode &= ~(S_ISUID | S_ISGID);
1894 security_task_to_inode(task, inode);
1895 put_task_struct(task);
1899 put_files_struct(files);
1901 put_task_struct(task);
1907 static const struct dentry_operations tid_fd_dentry_operations =
1909 .d_revalidate = tid_fd_revalidate,
1910 .d_delete = pid_delete_dentry,
1913 static struct dentry *proc_fd_instantiate(struct inode *dir,
1914 struct dentry *dentry, struct task_struct *task, const void *ptr)
1916 unsigned fd = *(const unsigned *)ptr;
1918 struct files_struct *files;
1919 struct inode *inode;
1920 struct proc_inode *ei;
1921 struct dentry *error = ERR_PTR(-ENOENT);
1923 inode = proc_pid_make_inode(dir->i_sb, task);
1928 files = get_files_struct(task);
1931 inode->i_mode = S_IFLNK;
1934 * We are not taking a ref to the file structure, so we must
1937 spin_lock(&files->file_lock);
1938 file = fcheck_files(files, fd);
1941 if (file->f_mode & FMODE_READ)
1942 inode->i_mode |= S_IRUSR | S_IXUSR;
1943 if (file->f_mode & FMODE_WRITE)
1944 inode->i_mode |= S_IWUSR | S_IXUSR;
1945 spin_unlock(&files->file_lock);
1946 put_files_struct(files);
1948 inode->i_op = &proc_pid_link_inode_operations;
1950 ei->op.proc_get_link = proc_fd_link;
1951 d_set_d_op(dentry, &tid_fd_dentry_operations);
1952 d_add(dentry, inode);
1953 /* Close the race of the process dying before we return the dentry */
1954 if (tid_fd_revalidate(dentry, NULL))
1960 spin_unlock(&files->file_lock);
1961 put_files_struct(files);
1967 static struct dentry *proc_lookupfd_common(struct inode *dir,
1968 struct dentry *dentry,
1969 instantiate_t instantiate)
1971 struct task_struct *task = get_proc_task(dir);
1972 unsigned fd = name_to_int(dentry);
1973 struct dentry *result = ERR_PTR(-ENOENT);
1980 result = instantiate(dir, dentry, task, &fd);
1982 put_task_struct(task);
1987 static int proc_readfd_common(struct file * filp, void * dirent,
1988 filldir_t filldir, instantiate_t instantiate)
1990 struct dentry *dentry = filp->f_path.dentry;
1991 struct inode *inode = dentry->d_inode;
1992 struct task_struct *p = get_proc_task(inode);
1993 unsigned int fd, ino;
1995 struct files_struct * files;
2005 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2009 ino = parent_ino(dentry);
2010 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2014 files = get_files_struct(p);
2018 for (fd = filp->f_pos-2;
2019 fd < files_fdtable(files)->max_fds;
2020 fd++, filp->f_pos++) {
2021 char name[PROC_NUMBUF];
2024 if (!fcheck_files(files, fd))
2028 len = snprintf(name, sizeof(name), "%d", fd);
2029 if (proc_fill_cache(filp, dirent, filldir,
2030 name, len, instantiate,
2038 put_files_struct(files);
2046 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2047 struct nameidata *nd)
2049 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2052 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2054 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2057 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2058 size_t len, loff_t *ppos)
2060 char tmp[PROC_FDINFO_MAX];
2061 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2063 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2067 static const struct file_operations proc_fdinfo_file_operations = {
2068 .open = nonseekable_open,
2069 .read = proc_fdinfo_read,
2070 .llseek = no_llseek,
2073 static const struct file_operations proc_fd_operations = {
2074 .read = generic_read_dir,
2075 .readdir = proc_readfd,
2076 .llseek = default_llseek,
2080 * /proc/pid/fd needs a special permission handler so that a process can still
2081 * access /proc/self/fd after it has executed a setuid().
2083 static int proc_fd_permission(struct inode *inode, int mask)
2085 int rv = generic_permission(inode, mask);
2088 if (task_pid(current) == proc_pid(inode))
2094 * proc directories can do almost nothing..
2096 static const struct inode_operations proc_fd_inode_operations = {
2097 .lookup = proc_lookupfd,
2098 .permission = proc_fd_permission,
2099 .setattr = proc_setattr,
2102 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2103 struct dentry *dentry, struct task_struct *task, const void *ptr)
2105 unsigned fd = *(unsigned *)ptr;
2106 struct inode *inode;
2107 struct proc_inode *ei;
2108 struct dentry *error = ERR_PTR(-ENOENT);
2110 inode = proc_pid_make_inode(dir->i_sb, task);
2115 inode->i_mode = S_IFREG | S_IRUSR;
2116 inode->i_fop = &proc_fdinfo_file_operations;
2117 d_set_d_op(dentry, &tid_fd_dentry_operations);
2118 d_add(dentry, inode);
2119 /* Close the race of the process dying before we return the dentry */
2120 if (tid_fd_revalidate(dentry, NULL))
2127 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2128 struct dentry *dentry,
2129 struct nameidata *nd)
2131 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2134 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2136 return proc_readfd_common(filp, dirent, filldir,
2137 proc_fdinfo_instantiate);
2140 static const struct file_operations proc_fdinfo_operations = {
2141 .read = generic_read_dir,
2142 .readdir = proc_readfdinfo,
2143 .llseek = default_llseek,
2147 * proc directories can do almost nothing..
2149 static const struct inode_operations proc_fdinfo_inode_operations = {
2150 .lookup = proc_lookupfdinfo,
2151 .setattr = proc_setattr,
2155 static struct dentry *proc_pident_instantiate(struct inode *dir,
2156 struct dentry *dentry, struct task_struct *task, const void *ptr)
2158 const struct pid_entry *p = ptr;
2159 struct inode *inode;
2160 struct proc_inode *ei;
2161 struct dentry *error = ERR_PTR(-ENOENT);
2163 inode = proc_pid_make_inode(dir->i_sb, task);
2168 inode->i_mode = p->mode;
2169 if (S_ISDIR(inode->i_mode))
2170 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2172 inode->i_op = p->iop;
2174 inode->i_fop = p->fop;
2176 d_set_d_op(dentry, &pid_dentry_operations);
2177 d_add(dentry, inode);
2178 /* Close the race of the process dying before we return the dentry */
2179 if (pid_revalidate(dentry, NULL))
2185 static struct dentry *proc_pident_lookup(struct inode *dir,
2186 struct dentry *dentry,
2187 const struct pid_entry *ents,
2190 struct dentry *error;
2191 struct task_struct *task = get_proc_task(dir);
2192 const struct pid_entry *p, *last;
2194 error = ERR_PTR(-ENOENT);
2200 * Yes, it does not scale. And it should not. Don't add
2201 * new entries into /proc/<tgid>/ without very good reasons.
2203 last = &ents[nents - 1];
2204 for (p = ents; p <= last; p++) {
2205 if (p->len != dentry->d_name.len)
2207 if (!memcmp(dentry->d_name.name, p->name, p->len))
2213 error = proc_pident_instantiate(dir, dentry, task, p);
2215 put_task_struct(task);
2220 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2221 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2223 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2224 proc_pident_instantiate, task, p);
2227 static int proc_pident_readdir(struct file *filp,
2228 void *dirent, filldir_t filldir,
2229 const struct pid_entry *ents, unsigned int nents)
2232 struct dentry *dentry = filp->f_path.dentry;
2233 struct inode *inode = dentry->d_inode;
2234 struct task_struct *task = get_proc_task(inode);
2235 const struct pid_entry *p, *last;
2248 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2254 ino = parent_ino(dentry);
2255 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2267 last = &ents[nents - 1];
2269 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2278 put_task_struct(task);
2283 #ifdef CONFIG_SECURITY
2284 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2285 size_t count, loff_t *ppos)
2287 struct inode * inode = file->f_path.dentry->d_inode;
2290 struct task_struct *task = get_proc_task(inode);
2295 length = security_getprocattr(task,
2296 (char*)file->f_path.dentry->d_name.name,
2298 put_task_struct(task);
2300 length = simple_read_from_buffer(buf, count, ppos, p, length);
2305 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2306 size_t count, loff_t *ppos)
2308 struct inode * inode = file->f_path.dentry->d_inode;
2311 struct task_struct *task = get_proc_task(inode);
2316 if (count > PAGE_SIZE)
2319 /* No partial writes. */
2325 page = (char*)__get_free_page(GFP_TEMPORARY);
2330 if (copy_from_user(page, buf, count))
2333 /* Guard against adverse ptrace interaction */
2334 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2338 length = security_setprocattr(task,
2339 (char*)file->f_path.dentry->d_name.name,
2340 (void*)page, count);
2341 mutex_unlock(&task->signal->cred_guard_mutex);
2343 free_page((unsigned long) page);
2345 put_task_struct(task);
2350 static const struct file_operations proc_pid_attr_operations = {
2351 .read = proc_pid_attr_read,
2352 .write = proc_pid_attr_write,
2353 .llseek = generic_file_llseek,
2356 static const struct pid_entry attr_dir_stuff[] = {
2357 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2358 REG("prev", S_IRUGO, proc_pid_attr_operations),
2359 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2360 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2361 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2362 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2365 static int proc_attr_dir_readdir(struct file * filp,
2366 void * dirent, filldir_t filldir)
2368 return proc_pident_readdir(filp,dirent,filldir,
2369 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2372 static const struct file_operations proc_attr_dir_operations = {
2373 .read = generic_read_dir,
2374 .readdir = proc_attr_dir_readdir,
2375 .llseek = default_llseek,
2378 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2379 struct dentry *dentry, struct nameidata *nd)
2381 return proc_pident_lookup(dir, dentry,
2382 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2385 static const struct inode_operations proc_attr_dir_inode_operations = {
2386 .lookup = proc_attr_dir_lookup,
2387 .getattr = pid_getattr,
2388 .setattr = proc_setattr,
2393 #ifdef CONFIG_ELF_CORE
2394 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2395 size_t count, loff_t *ppos)
2397 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2398 struct mm_struct *mm;
2399 char buffer[PROC_NUMBUF];
2407 mm = get_task_mm(task);
2409 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2410 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2411 MMF_DUMP_FILTER_SHIFT));
2413 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2416 put_task_struct(task);
2421 static ssize_t proc_coredump_filter_write(struct file *file,
2422 const char __user *buf,
2426 struct task_struct *task;
2427 struct mm_struct *mm;
2428 char buffer[PROC_NUMBUF], *end;
2435 memset(buffer, 0, sizeof(buffer));
2436 if (count > sizeof(buffer) - 1)
2437 count = sizeof(buffer) - 1;
2438 if (copy_from_user(buffer, buf, count))
2442 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2445 if (end - buffer == 0)
2449 task = get_proc_task(file->f_dentry->d_inode);
2454 mm = get_task_mm(task);
2458 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2460 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2462 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2467 put_task_struct(task);
2472 static const struct file_operations proc_coredump_filter_operations = {
2473 .read = proc_coredump_filter_read,
2474 .write = proc_coredump_filter_write,
2475 .llseek = generic_file_llseek,
2482 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2485 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2486 pid_t tgid = task_tgid_nr_ns(current, ns);
2487 char tmp[PROC_NUMBUF];
2490 sprintf(tmp, "%d", tgid);
2491 return vfs_readlink(dentry,buffer,buflen,tmp);
2494 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2496 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2497 pid_t tgid = task_tgid_nr_ns(current, ns);
2498 char *name = ERR_PTR(-ENOENT);
2502 name = ERR_PTR(-ENOMEM);
2504 sprintf(name, "%d", tgid);
2506 nd_set_link(nd, name);
2510 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2513 char *s = nd_get_link(nd);
2518 static const struct inode_operations proc_self_inode_operations = {
2519 .readlink = proc_self_readlink,
2520 .follow_link = proc_self_follow_link,
2521 .put_link = proc_self_put_link,
2527 * These are the directory entries in the root directory of /proc
2528 * that properly belong to the /proc filesystem, as they describe
2529 * describe something that is process related.
2531 static const struct pid_entry proc_base_stuff[] = {
2532 NOD("self", S_IFLNK|S_IRWXUGO,
2533 &proc_self_inode_operations, NULL, {}),
2536 static struct dentry *proc_base_instantiate(struct inode *dir,
2537 struct dentry *dentry, struct task_struct *task, const void *ptr)
2539 const struct pid_entry *p = ptr;
2540 struct inode *inode;
2541 struct proc_inode *ei;
2542 struct dentry *error;
2544 /* Allocate the inode */
2545 error = ERR_PTR(-ENOMEM);
2546 inode = new_inode(dir->i_sb);
2550 /* Initialize the inode */
2552 inode->i_ino = get_next_ino();
2553 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2556 * grab the reference to the task.
2558 ei->pid = get_task_pid(task, PIDTYPE_PID);
2562 inode->i_mode = p->mode;
2563 if (S_ISDIR(inode->i_mode))
2564 set_nlink(inode, 2);
2565 if (S_ISLNK(inode->i_mode))
2568 inode->i_op = p->iop;
2570 inode->i_fop = p->fop;
2572 d_add(dentry, inode);
2581 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2583 struct dentry *error;
2584 struct task_struct *task = get_proc_task(dir);
2585 const struct pid_entry *p, *last;
2587 error = ERR_PTR(-ENOENT);
2592 /* Lookup the directory entry */
2593 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2594 for (p = proc_base_stuff; p <= last; p++) {
2595 if (p->len != dentry->d_name.len)
2597 if (!memcmp(dentry->d_name.name, p->name, p->len))
2603 error = proc_base_instantiate(dir, dentry, task, p);
2606 put_task_struct(task);
2611 static int proc_base_fill_cache(struct file *filp, void *dirent,
2612 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2614 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2615 proc_base_instantiate, task, p);
2618 #ifdef CONFIG_TASK_IO_ACCOUNTING
2619 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2621 struct task_io_accounting acct = task->ioac;
2622 unsigned long flags;
2625 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2629 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2634 if (whole && lock_task_sighand(task, &flags)) {
2635 struct task_struct *t = task;
2637 task_io_accounting_add(&acct, &task->signal->ioac);
2638 while_each_thread(task, t)
2639 task_io_accounting_add(&acct, &t->ioac);
2641 unlock_task_sighand(task, &flags);
2643 result = sprintf(buffer,
2648 "read_bytes: %llu\n"
2649 "write_bytes: %llu\n"
2650 "cancelled_write_bytes: %llu\n",
2651 (unsigned long long)acct.rchar,
2652 (unsigned long long)acct.wchar,
2653 (unsigned long long)acct.syscr,
2654 (unsigned long long)acct.syscw,
2655 (unsigned long long)acct.read_bytes,
2656 (unsigned long long)acct.write_bytes,
2657 (unsigned long long)acct.cancelled_write_bytes);
2659 mutex_unlock(&task->signal->cred_guard_mutex);
2663 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2665 return do_io_accounting(task, buffer, 0);
2668 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2670 return do_io_accounting(task, buffer, 1);
2672 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2674 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2675 struct pid *pid, struct task_struct *task)
2677 int err = lock_trace(task);
2679 seq_printf(m, "%08x\n", task->personality);
2688 static const struct file_operations proc_task_operations;
2689 static const struct inode_operations proc_task_inode_operations;
2691 static const struct pid_entry tgid_base_stuff[] = {
2692 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2693 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2694 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2695 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2697 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2699 REG("environ", S_IRUSR, proc_environ_operations),
2700 INF("auxv", S_IRUSR, proc_pid_auxv),
2701 ONE("status", S_IRUGO, proc_pid_status),
2702 ONE("personality", S_IRUGO, proc_pid_personality),
2703 INF("limits", S_IRUGO, proc_pid_limits),
2704 #ifdef CONFIG_SCHED_DEBUG
2705 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2707 #ifdef CONFIG_SCHED_AUTOGROUP
2708 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2710 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2711 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2712 INF("syscall", S_IRUGO, proc_pid_syscall),
2714 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2715 ONE("stat", S_IRUGO, proc_tgid_stat),
2716 ONE("statm", S_IRUGO, proc_pid_statm),
2717 REG("maps", S_IRUGO, proc_maps_operations),
2718 REG("arm_maps", S_IRUGO, proc_armv7_maps_operations),
2720 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2722 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2723 LNK("cwd", proc_cwd_link),
2724 LNK("root", proc_root_link),
2725 LNK("exe", proc_exe_link),
2726 REG("mounts", S_IRUGO, proc_mounts_operations),
2727 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2728 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2729 #ifdef CONFIG_PROC_PAGE_MONITOR
2730 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2731 REG("smaps", S_IRUGO, proc_smaps_operations),
2732 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2734 #ifdef CONFIG_SECURITY
2735 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2737 #ifdef CONFIG_KALLSYMS
2738 INF("wchan", S_IRUGO, proc_pid_wchan),
2740 #ifdef CONFIG_STACKTRACE
2741 ONE("stack", S_IRUGO, proc_pid_stack),
2743 #ifdef CONFIG_SCHEDSTATS
2744 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2746 #ifdef CONFIG_LATENCYTOP
2747 REG("latency", S_IRUGO, proc_lstats_operations),
2749 #ifdef CONFIG_PROC_PID_CPUSET
2750 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2752 #ifdef CONFIG_CGROUPS
2753 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2755 INF("oom_score", S_IRUGO, proc_oom_score),
2756 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2757 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2758 #ifdef CONFIG_AUDITSYSCALL
2759 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2760 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2762 #ifdef CONFIG_FAULT_INJECTION
2763 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2765 #ifdef CONFIG_ELF_CORE
2766 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2768 #ifdef CONFIG_TASK_IO_ACCOUNTING
2769 INF("io", S_IRUSR, proc_tgid_io_accounting),
2771 #ifdef CONFIG_HARDWALL
2772 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2776 static int proc_tgid_base_readdir(struct file * filp,
2777 void * dirent, filldir_t filldir)
2779 return proc_pident_readdir(filp,dirent,filldir,
2780 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2783 static const struct file_operations proc_tgid_base_operations = {
2784 .read = generic_read_dir,
2785 .readdir = proc_tgid_base_readdir,
2786 .llseek = default_llseek,
2789 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2790 return proc_pident_lookup(dir, dentry,
2791 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2794 static const struct inode_operations proc_tgid_base_inode_operations = {
2795 .lookup = proc_tgid_base_lookup,
2796 .getattr = pid_getattr,
2797 .setattr = proc_setattr,
2800 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2802 struct dentry *dentry, *leader, *dir;
2803 char buf[PROC_NUMBUF];
2807 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2808 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2810 shrink_dcache_parent(dentry);
2816 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2817 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2822 name.len = strlen(name.name);
2823 dir = d_hash_and_lookup(leader, &name);
2825 goto out_put_leader;
2828 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2829 dentry = d_hash_and_lookup(dir, &name);
2831 shrink_dcache_parent(dentry);
2844 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2845 * @task: task that should be flushed.
2847 * When flushing dentries from proc, one needs to flush them from global
2848 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2849 * in. This call is supposed to do all of this job.
2851 * Looks in the dcache for
2853 * /proc/@tgid/task/@pid
2854 * if either directory is present flushes it and all of it'ts children
2857 * It is safe and reasonable to cache /proc entries for a task until
2858 * that task exits. After that they just clog up the dcache with
2859 * useless entries, possibly causing useful dcache entries to be
2860 * flushed instead. This routine is proved to flush those useless
2861 * dcache entries at process exit time.
2863 * NOTE: This routine is just an optimization so it does not guarantee
2864 * that no dcache entries will exist at process exit time it
2865 * just makes it very unlikely that any will persist.
2868 void proc_flush_task(struct task_struct *task)
2871 struct pid *pid, *tgid;
2874 pid = task_pid(task);
2875 tgid = task_tgid(task);
2877 for (i = 0; i <= pid->level; i++) {
2878 upid = &pid->numbers[i];
2879 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2880 tgid->numbers[i].nr);
2883 upid = &pid->numbers[pid->level];
2885 pid_ns_release_proc(upid->ns);
2888 static struct dentry *proc_pid_instantiate(struct inode *dir,
2889 struct dentry * dentry,
2890 struct task_struct *task, const void *ptr)
2892 struct dentry *error = ERR_PTR(-ENOENT);
2893 struct inode *inode;
2895 inode = proc_pid_make_inode(dir->i_sb, task);
2899 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2900 inode->i_op = &proc_tgid_base_inode_operations;
2901 inode->i_fop = &proc_tgid_base_operations;
2902 inode->i_flags|=S_IMMUTABLE;
2904 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2905 ARRAY_SIZE(tgid_base_stuff)));
2907 d_set_d_op(dentry, &pid_dentry_operations);
2909 d_add(dentry, inode);
2910 /* Close the race of the process dying before we return the dentry */
2911 if (pid_revalidate(dentry, NULL))
2917 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2919 struct dentry *result;
2920 struct task_struct *task;
2922 struct pid_namespace *ns;
2924 result = proc_base_lookup(dir, dentry);
2925 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2928 tgid = name_to_int(dentry);
2932 ns = dentry->d_sb->s_fs_info;
2934 task = find_task_by_pid_ns(tgid, ns);
2936 get_task_struct(task);
2941 result = proc_pid_instantiate(dir, dentry, task, NULL);
2942 put_task_struct(task);
2948 * Find the first task with tgid >= tgid
2953 struct task_struct *task;
2955 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2960 put_task_struct(iter.task);
2964 pid = find_ge_pid(iter.tgid, ns);
2966 iter.tgid = pid_nr_ns(pid, ns);
2967 iter.task = pid_task(pid, PIDTYPE_PID);
2968 /* What we to know is if the pid we have find is the
2969 * pid of a thread_group_leader. Testing for task
2970 * being a thread_group_leader is the obvious thing
2971 * todo but there is a window when it fails, due to
2972 * the pid transfer logic in de_thread.
2974 * So we perform the straight forward test of seeing
2975 * if the pid we have found is the pid of a thread
2976 * group leader, and don't worry if the task we have
2977 * found doesn't happen to be a thread group leader.
2978 * As we don't care in the case of readdir.
2980 if (!iter.task || !has_group_leader_pid(iter.task)) {
2984 get_task_struct(iter.task);
2990 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2992 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2993 struct tgid_iter iter)
2995 char name[PROC_NUMBUF];
2996 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2997 return proc_fill_cache(filp, dirent, filldir, name, len,
2998 proc_pid_instantiate, iter.task, NULL);
3001 /* for the /proc/ directory itself, after non-process stuff has been done */
3002 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3005 struct task_struct *reaper;
3006 struct tgid_iter iter;
3007 struct pid_namespace *ns;
3009 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3011 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3013 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3017 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3018 const struct pid_entry *p = &proc_base_stuff[nr];
3019 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3023 ns = filp->f_dentry->d_sb->s_fs_info;
3025 iter.tgid = filp->f_pos - TGID_OFFSET;
3026 for (iter = next_tgid(ns, iter);
3028 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3029 filp->f_pos = iter.tgid + TGID_OFFSET;
3030 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3031 put_task_struct(iter.task);
3035 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3037 put_task_struct(reaper);
3045 static const struct pid_entry tid_base_stuff[] = {
3046 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3047 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3048 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3049 REG("environ", S_IRUSR, proc_environ_operations),
3050 INF("auxv", S_IRUSR, proc_pid_auxv),
3051 ONE("status", S_IRUGO, proc_pid_status),
3052 ONE("personality", S_IRUGO, proc_pid_personality),
3053 INF("limits", S_IRUGO, proc_pid_limits),
3054 #ifdef CONFIG_SCHED_DEBUG
3055 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3057 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3058 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3059 INF("syscall", S_IRUGO, proc_pid_syscall),
3061 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3062 ONE("stat", S_IRUGO, proc_tid_stat),
3063 ONE("statm", S_IRUGO, proc_pid_statm),
3064 REG("maps", S_IRUGO, proc_maps_operations),
3065 REG("arm_maps", S_IRUGO, proc_armv7_maps_operations),
3067 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3069 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3070 LNK("cwd", proc_cwd_link),
3071 LNK("root", proc_root_link),
3072 LNK("exe", proc_exe_link),
3073 REG("mounts", S_IRUGO, proc_mounts_operations),
3074 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3075 #ifdef CONFIG_PROC_PAGE_MONITOR
3076 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3077 REG("smaps", S_IRUGO, proc_smaps_operations),
3078 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3080 #ifdef CONFIG_SECURITY
3081 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3083 #ifdef CONFIG_KALLSYMS
3084 INF("wchan", S_IRUGO, proc_pid_wchan),
3086 #ifdef CONFIG_STACKTRACE
3087 ONE("stack", S_IRUGO, proc_pid_stack),
3089 #ifdef CONFIG_SCHEDSTATS
3090 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3092 #ifdef CONFIG_LATENCYTOP
3093 REG("latency", S_IRUGO, proc_lstats_operations),
3095 #ifdef CONFIG_PROC_PID_CPUSET
3096 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3098 #ifdef CONFIG_CGROUPS
3099 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3101 INF("oom_score", S_IRUGO, proc_oom_score),
3102 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3103 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3104 #ifdef CONFIG_AUDITSYSCALL
3105 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3106 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3108 #ifdef CONFIG_FAULT_INJECTION
3109 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3111 #ifdef CONFIG_TASK_IO_ACCOUNTING
3112 INF("io", S_IRUSR, proc_tid_io_accounting),
3114 #ifdef CONFIG_HARDWALL
3115 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3119 static int proc_tid_base_readdir(struct file * filp,
3120 void * dirent, filldir_t filldir)
3122 return proc_pident_readdir(filp,dirent,filldir,
3123 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3126 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3127 return proc_pident_lookup(dir, dentry,
3128 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3131 static const struct file_operations proc_tid_base_operations = {
3132 .read = generic_read_dir,
3133 .readdir = proc_tid_base_readdir,
3134 .llseek = default_llseek,
3137 static const struct inode_operations proc_tid_base_inode_operations = {
3138 .lookup = proc_tid_base_lookup,
3139 .getattr = pid_getattr,
3140 .setattr = proc_setattr,
3143 static struct dentry *proc_task_instantiate(struct inode *dir,
3144 struct dentry *dentry, struct task_struct *task, const void *ptr)
3146 struct dentry *error = ERR_PTR(-ENOENT);
3147 struct inode *inode;
3148 inode = proc_pid_make_inode(dir->i_sb, task);
3152 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3153 inode->i_op = &proc_tid_base_inode_operations;
3154 inode->i_fop = &proc_tid_base_operations;
3155 inode->i_flags|=S_IMMUTABLE;
3157 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3158 ARRAY_SIZE(tid_base_stuff)));
3160 d_set_d_op(dentry, &pid_dentry_operations);
3162 d_add(dentry, inode);
3163 /* Close the race of the process dying before we return the dentry */
3164 if (pid_revalidate(dentry, NULL))
3170 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3172 struct dentry *result = ERR_PTR(-ENOENT);
3173 struct task_struct *task;
3174 struct task_struct *leader = get_proc_task(dir);
3176 struct pid_namespace *ns;
3181 tid = name_to_int(dentry);
3185 ns = dentry->d_sb->s_fs_info;
3187 task = find_task_by_pid_ns(tid, ns);
3189 get_task_struct(task);
3193 if (!same_thread_group(leader, task))
3196 result = proc_task_instantiate(dir, dentry, task, NULL);
3198 put_task_struct(task);
3200 put_task_struct(leader);
3206 * Find the first tid of a thread group to return to user space.
3208 * Usually this is just the thread group leader, but if the users
3209 * buffer was too small or there was a seek into the middle of the
3210 * directory we have more work todo.
3212 * In the case of a short read we start with find_task_by_pid.
3214 * In the case of a seek we start with the leader and walk nr
3217 static struct task_struct *first_tid(struct task_struct *leader,
3218 int tid, int nr, struct pid_namespace *ns)
3220 struct task_struct *pos;
3223 /* Attempt to start with the pid of a thread */
3224 if (tid && (nr > 0)) {
3225 pos = find_task_by_pid_ns(tid, ns);
3226 if (pos && (pos->group_leader == leader))
3230 /* If nr exceeds the number of threads there is nothing todo */
3232 if (nr && nr >= get_nr_threads(leader))
3235 /* If we haven't found our starting place yet start
3236 * with the leader and walk nr threads forward.
3238 for (pos = leader; nr > 0; --nr) {
3239 pos = next_thread(pos);
3240 if (pos == leader) {
3246 get_task_struct(pos);
3253 * Find the next thread in the thread list.
3254 * Return NULL if there is an error or no next thread.
3256 * The reference to the input task_struct is released.
3258 static struct task_struct *next_tid(struct task_struct *start)
3260 struct task_struct *pos = NULL;
3262 if (pid_alive(start)) {
3263 pos = next_thread(start);
3264 if (thread_group_leader(pos))
3267 get_task_struct(pos);
3270 put_task_struct(start);
3274 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3275 struct task_struct *task, int tid)
3277 char name[PROC_NUMBUF];
3278 int len = snprintf(name, sizeof(name), "%d", tid);
3279 return proc_fill_cache(filp, dirent, filldir, name, len,
3280 proc_task_instantiate, task, NULL);
3283 /* for the /proc/TGID/task/ directories */
3284 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3286 struct dentry *dentry = filp->f_path.dentry;
3287 struct inode *inode = dentry->d_inode;
3288 struct task_struct *leader = NULL;
3289 struct task_struct *task;
3290 int retval = -ENOENT;
3293 struct pid_namespace *ns;
3295 task = get_proc_task(inode);
3299 if (pid_alive(task)) {
3300 leader = task->group_leader;
3301 get_task_struct(leader);
3304 put_task_struct(task);
3309 switch ((unsigned long)filp->f_pos) {
3312 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3317 ino = parent_ino(dentry);
3318 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3324 /* f_version caches the tgid value that the last readdir call couldn't
3325 * return. lseek aka telldir automagically resets f_version to 0.
3327 ns = filp->f_dentry->d_sb->s_fs_info;
3328 tid = (int)filp->f_version;
3329 filp->f_version = 0;
3330 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3332 task = next_tid(task), filp->f_pos++) {
3333 tid = task_pid_nr_ns(task, ns);
3334 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3335 /* returning this tgid failed, save it as the first
3336 * pid for the next readir call */
3337 filp->f_version = (u64)tid;
3338 put_task_struct(task);
3343 put_task_struct(leader);
3348 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3350 struct inode *inode = dentry->d_inode;
3351 struct task_struct *p = get_proc_task(inode);
3352 generic_fillattr(inode, stat);
3355 stat->nlink += get_nr_threads(p);
3362 static const struct inode_operations proc_task_inode_operations = {
3363 .lookup = proc_task_lookup,
3364 .getattr = proc_task_getattr,
3365 .setattr = proc_setattr,
3368 static const struct file_operations proc_task_operations = {
3369 .read = generic_read_dir,
3370 .readdir = proc_task_readdir,
3371 .llseek = default_llseek,