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 /* Ensure the process spawned far enough to have an environment. */
909 if (!mm || IS_ERR(mm) || !mm->env_end)
914 int this_len, retval, max_len;
916 this_len = mm->env_end - (mm->env_start + src);
921 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
922 this_len = (this_len > max_len) ? max_len : this_len;
924 retval = access_process_vm(task, (mm->env_start + src),
932 if (copy_to_user(buf, page, retval)) {
946 free_page((unsigned long) page);
948 put_task_struct(task);
953 static const struct file_operations proc_environ_operations = {
954 .read = environ_read,
955 .llseek = generic_file_llseek,
958 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
959 size_t count, loff_t *ppos)
961 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
962 char buffer[PROC_NUMBUF];
964 int oom_adjust = OOM_DISABLE;
970 if (lock_task_sighand(task, &flags)) {
971 oom_adjust = task->signal->oom_adj;
972 unlock_task_sighand(task, &flags);
975 put_task_struct(task);
977 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
979 return simple_read_from_buffer(buf, count, ppos, buffer, len);
982 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
983 size_t count, loff_t *ppos)
985 struct task_struct *task;
986 char buffer[PROC_NUMBUF];
991 memset(buffer, 0, sizeof(buffer));
992 if (count > sizeof(buffer) - 1)
993 count = sizeof(buffer) - 1;
994 if (copy_from_user(buffer, buf, count)) {
999 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1002 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1003 oom_adjust != OOM_DISABLE) {
1008 task = get_proc_task(file->f_path.dentry->d_inode);
1020 if (!lock_task_sighand(task, &flags)) {
1025 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1031 * Warn that /proc/pid/oom_adj is deprecated, see
1032 * Documentation/feature-removal-schedule.txt.
1034 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1035 current->comm, task_pid_nr(current), task_pid_nr(task),
1037 task->signal->oom_adj = oom_adjust;
1039 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1040 * value is always attainable.
1042 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1043 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1045 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1048 unlock_task_sighand(task, &flags);
1051 put_task_struct(task);
1053 return err < 0 ? err : count;
1056 static const struct file_operations proc_oom_adjust_operations = {
1057 .read = oom_adjust_read,
1058 .write = oom_adjust_write,
1059 .llseek = generic_file_llseek,
1062 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1063 size_t count, loff_t *ppos)
1065 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1066 char buffer[PROC_NUMBUF];
1067 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1068 unsigned long flags;
1073 if (lock_task_sighand(task, &flags)) {
1074 oom_score_adj = task->signal->oom_score_adj;
1075 unlock_task_sighand(task, &flags);
1077 put_task_struct(task);
1078 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1079 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1082 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1083 size_t count, loff_t *ppos)
1085 struct task_struct *task;
1086 char buffer[PROC_NUMBUF];
1087 unsigned long flags;
1091 memset(buffer, 0, sizeof(buffer));
1092 if (count > sizeof(buffer) - 1)
1093 count = sizeof(buffer) - 1;
1094 if (copy_from_user(buffer, buf, count)) {
1099 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1102 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1103 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1108 task = get_proc_task(file->f_path.dentry->d_inode);
1120 if (!lock_task_sighand(task, &flags)) {
1125 if (oom_score_adj < task->signal->oom_score_adj_min &&
1126 !capable(CAP_SYS_RESOURCE)) {
1131 task->signal->oom_score_adj = oom_score_adj;
1132 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1133 task->signal->oom_score_adj_min = oom_score_adj;
1135 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1136 * always attainable.
1138 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1139 task->signal->oom_adj = OOM_DISABLE;
1141 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1144 unlock_task_sighand(task, &flags);
1147 put_task_struct(task);
1149 return err < 0 ? err : count;
1152 static const struct file_operations proc_oom_score_adj_operations = {
1153 .read = oom_score_adj_read,
1154 .write = oom_score_adj_write,
1155 .llseek = default_llseek,
1158 #ifdef CONFIG_AUDITSYSCALL
1159 #define TMPBUFLEN 21
1160 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1161 size_t count, loff_t *ppos)
1163 struct inode * inode = file->f_path.dentry->d_inode;
1164 struct task_struct *task = get_proc_task(inode);
1166 char tmpbuf[TMPBUFLEN];
1170 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1171 audit_get_loginuid(task));
1172 put_task_struct(task);
1173 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1176 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1177 size_t count, loff_t *ppos)
1179 struct inode * inode = file->f_path.dentry->d_inode;
1184 if (!capable(CAP_AUDIT_CONTROL))
1188 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1194 if (count >= PAGE_SIZE)
1195 count = PAGE_SIZE - 1;
1198 /* No partial writes. */
1201 page = (char*)__get_free_page(GFP_TEMPORARY);
1205 if (copy_from_user(page, buf, count))
1209 loginuid = simple_strtoul(page, &tmp, 10);
1215 length = audit_set_loginuid(current, loginuid);
1216 if (likely(length == 0))
1220 free_page((unsigned long) page);
1224 static const struct file_operations proc_loginuid_operations = {
1225 .read = proc_loginuid_read,
1226 .write = proc_loginuid_write,
1227 .llseek = generic_file_llseek,
1230 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1231 size_t count, loff_t *ppos)
1233 struct inode * inode = file->f_path.dentry->d_inode;
1234 struct task_struct *task = get_proc_task(inode);
1236 char tmpbuf[TMPBUFLEN];
1240 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1241 audit_get_sessionid(task));
1242 put_task_struct(task);
1243 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1246 static const struct file_operations proc_sessionid_operations = {
1247 .read = proc_sessionid_read,
1248 .llseek = generic_file_llseek,
1252 #ifdef CONFIG_FAULT_INJECTION
1253 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1254 size_t count, loff_t *ppos)
1256 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1257 char buffer[PROC_NUMBUF];
1263 make_it_fail = task->make_it_fail;
1264 put_task_struct(task);
1266 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1268 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1271 static ssize_t proc_fault_inject_write(struct file * file,
1272 const char __user * buf, size_t count, loff_t *ppos)
1274 struct task_struct *task;
1275 char buffer[PROC_NUMBUF], *end;
1278 if (!capable(CAP_SYS_RESOURCE))
1280 memset(buffer, 0, sizeof(buffer));
1281 if (count > sizeof(buffer) - 1)
1282 count = sizeof(buffer) - 1;
1283 if (copy_from_user(buffer, buf, count))
1285 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1288 task = get_proc_task(file->f_dentry->d_inode);
1291 task->make_it_fail = make_it_fail;
1292 put_task_struct(task);
1297 static const struct file_operations proc_fault_inject_operations = {
1298 .read = proc_fault_inject_read,
1299 .write = proc_fault_inject_write,
1300 .llseek = generic_file_llseek,
1305 #ifdef CONFIG_SCHED_DEBUG
1307 * Print out various scheduling related per-task fields:
1309 static int sched_show(struct seq_file *m, void *v)
1311 struct inode *inode = m->private;
1312 struct task_struct *p;
1314 p = get_proc_task(inode);
1317 proc_sched_show_task(p, m);
1325 sched_write(struct file *file, const char __user *buf,
1326 size_t count, loff_t *offset)
1328 struct inode *inode = file->f_path.dentry->d_inode;
1329 struct task_struct *p;
1331 p = get_proc_task(inode);
1334 proc_sched_set_task(p);
1341 static int sched_open(struct inode *inode, struct file *filp)
1343 return single_open(filp, sched_show, inode);
1346 static const struct file_operations proc_pid_sched_operations = {
1349 .write = sched_write,
1350 .llseek = seq_lseek,
1351 .release = single_release,
1356 #ifdef CONFIG_SCHED_AUTOGROUP
1358 * Print out autogroup related information:
1360 static int sched_autogroup_show(struct seq_file *m, void *v)
1362 struct inode *inode = m->private;
1363 struct task_struct *p;
1365 p = get_proc_task(inode);
1368 proc_sched_autogroup_show_task(p, m);
1376 sched_autogroup_write(struct file *file, const char __user *buf,
1377 size_t count, loff_t *offset)
1379 struct inode *inode = file->f_path.dentry->d_inode;
1380 struct task_struct *p;
1381 char buffer[PROC_NUMBUF];
1385 memset(buffer, 0, sizeof(buffer));
1386 if (count > sizeof(buffer) - 1)
1387 count = sizeof(buffer) - 1;
1388 if (copy_from_user(buffer, buf, count))
1391 err = kstrtoint(strstrip(buffer), 0, &nice);
1395 p = get_proc_task(inode);
1400 err = proc_sched_autogroup_set_nice(p, &err);
1409 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1413 ret = single_open(filp, sched_autogroup_show, NULL);
1415 struct seq_file *m = filp->private_data;
1422 static const struct file_operations proc_pid_sched_autogroup_operations = {
1423 .open = sched_autogroup_open,
1425 .write = sched_autogroup_write,
1426 .llseek = seq_lseek,
1427 .release = single_release,
1430 #endif /* CONFIG_SCHED_AUTOGROUP */
1432 static ssize_t comm_write(struct file *file, const char __user *buf,
1433 size_t count, loff_t *offset)
1435 struct inode *inode = file->f_path.dentry->d_inode;
1436 struct task_struct *p;
1437 char buffer[TASK_COMM_LEN];
1439 memset(buffer, 0, sizeof(buffer));
1440 if (count > sizeof(buffer) - 1)
1441 count = sizeof(buffer) - 1;
1442 if (copy_from_user(buffer, buf, count))
1445 p = get_proc_task(inode);
1449 if (same_thread_group(current, p))
1450 set_task_comm(p, buffer);
1459 static int comm_show(struct seq_file *m, void *v)
1461 struct inode *inode = m->private;
1462 struct task_struct *p;
1464 p = get_proc_task(inode);
1469 seq_printf(m, "%s\n", p->comm);
1477 static int comm_open(struct inode *inode, struct file *filp)
1479 return single_open(filp, comm_show, inode);
1482 static const struct file_operations proc_pid_set_comm_operations = {
1485 .write = comm_write,
1486 .llseek = seq_lseek,
1487 .release = single_release,
1490 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1492 struct task_struct *task;
1493 struct mm_struct *mm;
1494 struct file *exe_file;
1496 task = get_proc_task(inode);
1499 mm = get_task_mm(task);
1500 put_task_struct(task);
1503 exe_file = get_mm_exe_file(mm);
1506 *exe_path = exe_file->f_path;
1507 path_get(&exe_file->f_path);
1514 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1516 struct inode *inode = dentry->d_inode;
1517 int error = -EACCES;
1519 /* We don't need a base pointer in the /proc filesystem */
1520 path_put(&nd->path);
1522 /* Are we allowed to snoop on the tasks file descriptors? */
1523 if (!proc_fd_access_allowed(inode))
1526 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1528 return ERR_PTR(error);
1531 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1533 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1540 pathname = d_path(path, tmp, PAGE_SIZE);
1541 len = PTR_ERR(pathname);
1542 if (IS_ERR(pathname))
1544 len = tmp + PAGE_SIZE - 1 - pathname;
1548 if (copy_to_user(buffer, pathname, len))
1551 free_page((unsigned long)tmp);
1555 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1557 int error = -EACCES;
1558 struct inode *inode = dentry->d_inode;
1561 /* Are we allowed to snoop on the tasks file descriptors? */
1562 if (!proc_fd_access_allowed(inode))
1565 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1569 error = do_proc_readlink(&path, buffer, buflen);
1575 static const struct inode_operations proc_pid_link_inode_operations = {
1576 .readlink = proc_pid_readlink,
1577 .follow_link = proc_pid_follow_link,
1578 .setattr = proc_setattr,
1582 /* building an inode */
1584 static int task_dumpable(struct task_struct *task)
1587 struct mm_struct *mm;
1592 dumpable = get_dumpable(mm);
1599 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1601 struct inode * inode;
1602 struct proc_inode *ei;
1603 const struct cred *cred;
1605 /* We need a new inode */
1607 inode = new_inode(sb);
1613 inode->i_ino = get_next_ino();
1614 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1615 inode->i_op = &proc_def_inode_operations;
1618 * grab the reference to task.
1620 ei->pid = get_task_pid(task, PIDTYPE_PID);
1624 if (task_dumpable(task)) {
1626 cred = __task_cred(task);
1627 inode->i_uid = cred->euid;
1628 inode->i_gid = cred->egid;
1631 security_task_to_inode(task, inode);
1641 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1643 struct inode *inode = dentry->d_inode;
1644 struct task_struct *task;
1645 const struct cred *cred;
1647 generic_fillattr(inode, stat);
1652 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1654 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1655 task_dumpable(task)) {
1656 cred = __task_cred(task);
1657 stat->uid = cred->euid;
1658 stat->gid = cred->egid;
1668 * Exceptional case: normally we are not allowed to unhash a busy
1669 * directory. In this case, however, we can do it - no aliasing problems
1670 * due to the way we treat inodes.
1672 * Rewrite the inode's ownerships here because the owning task may have
1673 * performed a setuid(), etc.
1675 * Before the /proc/pid/status file was created the only way to read
1676 * the effective uid of a /process was to stat /proc/pid. Reading
1677 * /proc/pid/status is slow enough that procps and other packages
1678 * kept stating /proc/pid. To keep the rules in /proc simple I have
1679 * made this apply to all per process world readable and executable
1682 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1684 struct inode *inode;
1685 struct task_struct *task;
1686 const struct cred *cred;
1688 if (nd && nd->flags & LOOKUP_RCU)
1691 inode = dentry->d_inode;
1692 task = get_proc_task(inode);
1695 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1696 task_dumpable(task)) {
1698 cred = __task_cred(task);
1699 inode->i_uid = cred->euid;
1700 inode->i_gid = cred->egid;
1706 inode->i_mode &= ~(S_ISUID | S_ISGID);
1707 security_task_to_inode(task, inode);
1708 put_task_struct(task);
1715 static int pid_delete_dentry(const struct dentry * dentry)
1717 /* Is the task we represent dead?
1718 * If so, then don't put the dentry on the lru list,
1719 * kill it immediately.
1721 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1724 const struct dentry_operations pid_dentry_operations =
1726 .d_revalidate = pid_revalidate,
1727 .d_delete = pid_delete_dentry,
1733 * Fill a directory entry.
1735 * If possible create the dcache entry and derive our inode number and
1736 * file type from dcache entry.
1738 * Since all of the proc inode numbers are dynamically generated, the inode
1739 * numbers do not exist until the inode is cache. This means creating the
1740 * the dcache entry in readdir is necessary to keep the inode numbers
1741 * reported by readdir in sync with the inode numbers reported
1744 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1745 const char *name, int len,
1746 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1748 struct dentry *child, *dir = filp->f_path.dentry;
1749 struct inode *inode;
1752 unsigned type = DT_UNKNOWN;
1756 qname.hash = full_name_hash(name, len);
1758 child = d_lookup(dir, &qname);
1761 new = d_alloc(dir, &qname);
1763 child = instantiate(dir->d_inode, new, task, ptr);
1770 if (!child || IS_ERR(child) || !child->d_inode)
1771 goto end_instantiate;
1772 inode = child->d_inode;
1775 type = inode->i_mode >> 12;
1780 ino = find_inode_number(dir, &qname);
1783 return filldir(dirent, name, len, filp->f_pos, ino, type);
1786 static unsigned name_to_int(struct dentry *dentry)
1788 const char *name = dentry->d_name.name;
1789 int len = dentry->d_name.len;
1792 if (len > 1 && *name == '0')
1795 unsigned c = *name++ - '0';
1798 if (n >= (~0U-9)/10)
1808 #define PROC_FDINFO_MAX 64
1810 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1812 struct task_struct *task = get_proc_task(inode);
1813 struct files_struct *files = NULL;
1815 int fd = proc_fd(inode);
1818 files = get_files_struct(task);
1819 put_task_struct(task);
1823 * We are not taking a ref to the file structure, so we must
1826 spin_lock(&files->file_lock);
1827 file = fcheck_files(files, fd);
1829 unsigned int f_flags;
1830 struct fdtable *fdt;
1832 fdt = files_fdtable(files);
1833 f_flags = file->f_flags & ~O_CLOEXEC;
1834 if (FD_ISSET(fd, fdt->close_on_exec))
1835 f_flags |= O_CLOEXEC;
1838 *path = file->f_path;
1839 path_get(&file->f_path);
1842 snprintf(info, PROC_FDINFO_MAX,
1845 (long long) file->f_pos,
1847 spin_unlock(&files->file_lock);
1848 put_files_struct(files);
1851 spin_unlock(&files->file_lock);
1852 put_files_struct(files);
1857 static int proc_fd_link(struct inode *inode, struct path *path)
1859 return proc_fd_info(inode, path, NULL);
1862 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1864 struct inode *inode;
1865 struct task_struct *task;
1867 struct files_struct *files;
1868 const struct cred *cred;
1870 if (nd && nd->flags & LOOKUP_RCU)
1873 inode = dentry->d_inode;
1874 task = get_proc_task(inode);
1875 fd = proc_fd(inode);
1878 files = get_files_struct(task);
1881 if (fcheck_files(files, fd)) {
1883 put_files_struct(files);
1884 if (task_dumpable(task)) {
1886 cred = __task_cred(task);
1887 inode->i_uid = cred->euid;
1888 inode->i_gid = cred->egid;
1894 inode->i_mode &= ~(S_ISUID | S_ISGID);
1895 security_task_to_inode(task, inode);
1896 put_task_struct(task);
1900 put_files_struct(files);
1902 put_task_struct(task);
1908 static const struct dentry_operations tid_fd_dentry_operations =
1910 .d_revalidate = tid_fd_revalidate,
1911 .d_delete = pid_delete_dentry,
1914 static struct dentry *proc_fd_instantiate(struct inode *dir,
1915 struct dentry *dentry, struct task_struct *task, const void *ptr)
1917 unsigned fd = *(const unsigned *)ptr;
1919 struct files_struct *files;
1920 struct inode *inode;
1921 struct proc_inode *ei;
1922 struct dentry *error = ERR_PTR(-ENOENT);
1924 inode = proc_pid_make_inode(dir->i_sb, task);
1929 files = get_files_struct(task);
1932 inode->i_mode = S_IFLNK;
1935 * We are not taking a ref to the file structure, so we must
1938 spin_lock(&files->file_lock);
1939 file = fcheck_files(files, fd);
1942 if (file->f_mode & FMODE_READ)
1943 inode->i_mode |= S_IRUSR | S_IXUSR;
1944 if (file->f_mode & FMODE_WRITE)
1945 inode->i_mode |= S_IWUSR | S_IXUSR;
1946 spin_unlock(&files->file_lock);
1947 put_files_struct(files);
1949 inode->i_op = &proc_pid_link_inode_operations;
1951 ei->op.proc_get_link = proc_fd_link;
1952 d_set_d_op(dentry, &tid_fd_dentry_operations);
1953 d_add(dentry, inode);
1954 /* Close the race of the process dying before we return the dentry */
1955 if (tid_fd_revalidate(dentry, NULL))
1961 spin_unlock(&files->file_lock);
1962 put_files_struct(files);
1968 static struct dentry *proc_lookupfd_common(struct inode *dir,
1969 struct dentry *dentry,
1970 instantiate_t instantiate)
1972 struct task_struct *task = get_proc_task(dir);
1973 unsigned fd = name_to_int(dentry);
1974 struct dentry *result = ERR_PTR(-ENOENT);
1981 result = instantiate(dir, dentry, task, &fd);
1983 put_task_struct(task);
1988 static int proc_readfd_common(struct file * filp, void * dirent,
1989 filldir_t filldir, instantiate_t instantiate)
1991 struct dentry *dentry = filp->f_path.dentry;
1992 struct inode *inode = dentry->d_inode;
1993 struct task_struct *p = get_proc_task(inode);
1994 unsigned int fd, ino;
1996 struct files_struct * files;
2006 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2010 ino = parent_ino(dentry);
2011 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2015 files = get_files_struct(p);
2019 for (fd = filp->f_pos-2;
2020 fd < files_fdtable(files)->max_fds;
2021 fd++, filp->f_pos++) {
2022 char name[PROC_NUMBUF];
2025 if (!fcheck_files(files, fd))
2029 len = snprintf(name, sizeof(name), "%d", fd);
2030 if (proc_fill_cache(filp, dirent, filldir,
2031 name, len, instantiate,
2039 put_files_struct(files);
2047 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2048 struct nameidata *nd)
2050 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2053 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2055 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2058 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2059 size_t len, loff_t *ppos)
2061 char tmp[PROC_FDINFO_MAX];
2062 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2064 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2068 static const struct file_operations proc_fdinfo_file_operations = {
2069 .open = nonseekable_open,
2070 .read = proc_fdinfo_read,
2071 .llseek = no_llseek,
2074 static const struct file_operations proc_fd_operations = {
2075 .read = generic_read_dir,
2076 .readdir = proc_readfd,
2077 .llseek = default_llseek,
2081 * /proc/pid/fd needs a special permission handler so that a process can still
2082 * access /proc/self/fd after it has executed a setuid().
2084 static int proc_fd_permission(struct inode *inode, int mask)
2086 int rv = generic_permission(inode, mask);
2089 if (task_pid(current) == proc_pid(inode))
2095 * proc directories can do almost nothing..
2097 static const struct inode_operations proc_fd_inode_operations = {
2098 .lookup = proc_lookupfd,
2099 .permission = proc_fd_permission,
2100 .setattr = proc_setattr,
2103 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2104 struct dentry *dentry, struct task_struct *task, const void *ptr)
2106 unsigned fd = *(unsigned *)ptr;
2107 struct inode *inode;
2108 struct proc_inode *ei;
2109 struct dentry *error = ERR_PTR(-ENOENT);
2111 inode = proc_pid_make_inode(dir->i_sb, task);
2116 inode->i_mode = S_IFREG | S_IRUSR;
2117 inode->i_fop = &proc_fdinfo_file_operations;
2118 d_set_d_op(dentry, &tid_fd_dentry_operations);
2119 d_add(dentry, inode);
2120 /* Close the race of the process dying before we return the dentry */
2121 if (tid_fd_revalidate(dentry, NULL))
2128 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2129 struct dentry *dentry,
2130 struct nameidata *nd)
2132 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2135 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2137 return proc_readfd_common(filp, dirent, filldir,
2138 proc_fdinfo_instantiate);
2141 static const struct file_operations proc_fdinfo_operations = {
2142 .read = generic_read_dir,
2143 .readdir = proc_readfdinfo,
2144 .llseek = default_llseek,
2148 * proc directories can do almost nothing..
2150 static const struct inode_operations proc_fdinfo_inode_operations = {
2151 .lookup = proc_lookupfdinfo,
2152 .setattr = proc_setattr,
2156 static struct dentry *proc_pident_instantiate(struct inode *dir,
2157 struct dentry *dentry, struct task_struct *task, const void *ptr)
2159 const struct pid_entry *p = ptr;
2160 struct inode *inode;
2161 struct proc_inode *ei;
2162 struct dentry *error = ERR_PTR(-ENOENT);
2164 inode = proc_pid_make_inode(dir->i_sb, task);
2169 inode->i_mode = p->mode;
2170 if (S_ISDIR(inode->i_mode))
2171 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2173 inode->i_op = p->iop;
2175 inode->i_fop = p->fop;
2177 d_set_d_op(dentry, &pid_dentry_operations);
2178 d_add(dentry, inode);
2179 /* Close the race of the process dying before we return the dentry */
2180 if (pid_revalidate(dentry, NULL))
2186 static struct dentry *proc_pident_lookup(struct inode *dir,
2187 struct dentry *dentry,
2188 const struct pid_entry *ents,
2191 struct dentry *error;
2192 struct task_struct *task = get_proc_task(dir);
2193 const struct pid_entry *p, *last;
2195 error = ERR_PTR(-ENOENT);
2201 * Yes, it does not scale. And it should not. Don't add
2202 * new entries into /proc/<tgid>/ without very good reasons.
2204 last = &ents[nents - 1];
2205 for (p = ents; p <= last; p++) {
2206 if (p->len != dentry->d_name.len)
2208 if (!memcmp(dentry->d_name.name, p->name, p->len))
2214 error = proc_pident_instantiate(dir, dentry, task, p);
2216 put_task_struct(task);
2221 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2222 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2224 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2225 proc_pident_instantiate, task, p);
2228 static int proc_pident_readdir(struct file *filp,
2229 void *dirent, filldir_t filldir,
2230 const struct pid_entry *ents, unsigned int nents)
2233 struct dentry *dentry = filp->f_path.dentry;
2234 struct inode *inode = dentry->d_inode;
2235 struct task_struct *task = get_proc_task(inode);
2236 const struct pid_entry *p, *last;
2249 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2255 ino = parent_ino(dentry);
2256 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2268 last = &ents[nents - 1];
2270 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2279 put_task_struct(task);
2284 #ifdef CONFIG_SECURITY
2285 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2286 size_t count, loff_t *ppos)
2288 struct inode * inode = file->f_path.dentry->d_inode;
2291 struct task_struct *task = get_proc_task(inode);
2296 length = security_getprocattr(task,
2297 (char*)file->f_path.dentry->d_name.name,
2299 put_task_struct(task);
2301 length = simple_read_from_buffer(buf, count, ppos, p, length);
2306 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2307 size_t count, loff_t *ppos)
2309 struct inode * inode = file->f_path.dentry->d_inode;
2312 struct task_struct *task = get_proc_task(inode);
2317 if (count > PAGE_SIZE)
2320 /* No partial writes. */
2326 page = (char*)__get_free_page(GFP_TEMPORARY);
2331 if (copy_from_user(page, buf, count))
2334 /* Guard against adverse ptrace interaction */
2335 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2339 length = security_setprocattr(task,
2340 (char*)file->f_path.dentry->d_name.name,
2341 (void*)page, count);
2342 mutex_unlock(&task->signal->cred_guard_mutex);
2344 free_page((unsigned long) page);
2346 put_task_struct(task);
2351 static const struct file_operations proc_pid_attr_operations = {
2352 .read = proc_pid_attr_read,
2353 .write = proc_pid_attr_write,
2354 .llseek = generic_file_llseek,
2357 static const struct pid_entry attr_dir_stuff[] = {
2358 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2359 REG("prev", S_IRUGO, proc_pid_attr_operations),
2360 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2361 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2362 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2363 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2366 static int proc_attr_dir_readdir(struct file * filp,
2367 void * dirent, filldir_t filldir)
2369 return proc_pident_readdir(filp,dirent,filldir,
2370 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2373 static const struct file_operations proc_attr_dir_operations = {
2374 .read = generic_read_dir,
2375 .readdir = proc_attr_dir_readdir,
2376 .llseek = default_llseek,
2379 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2380 struct dentry *dentry, struct nameidata *nd)
2382 return proc_pident_lookup(dir, dentry,
2383 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2386 static const struct inode_operations proc_attr_dir_inode_operations = {
2387 .lookup = proc_attr_dir_lookup,
2388 .getattr = pid_getattr,
2389 .setattr = proc_setattr,
2394 #ifdef CONFIG_ELF_CORE
2395 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2396 size_t count, loff_t *ppos)
2398 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2399 struct mm_struct *mm;
2400 char buffer[PROC_NUMBUF];
2408 mm = get_task_mm(task);
2410 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2411 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2412 MMF_DUMP_FILTER_SHIFT));
2414 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2417 put_task_struct(task);
2422 static ssize_t proc_coredump_filter_write(struct file *file,
2423 const char __user *buf,
2427 struct task_struct *task;
2428 struct mm_struct *mm;
2429 char buffer[PROC_NUMBUF], *end;
2436 memset(buffer, 0, sizeof(buffer));
2437 if (count > sizeof(buffer) - 1)
2438 count = sizeof(buffer) - 1;
2439 if (copy_from_user(buffer, buf, count))
2443 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2446 if (end - buffer == 0)
2450 task = get_proc_task(file->f_dentry->d_inode);
2455 mm = get_task_mm(task);
2459 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2461 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2463 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2468 put_task_struct(task);
2473 static const struct file_operations proc_coredump_filter_operations = {
2474 .read = proc_coredump_filter_read,
2475 .write = proc_coredump_filter_write,
2476 .llseek = generic_file_llseek,
2483 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2486 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2487 pid_t tgid = task_tgid_nr_ns(current, ns);
2488 char tmp[PROC_NUMBUF];
2491 sprintf(tmp, "%d", tgid);
2492 return vfs_readlink(dentry,buffer,buflen,tmp);
2495 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2497 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2498 pid_t tgid = task_tgid_nr_ns(current, ns);
2499 char *name = ERR_PTR(-ENOENT);
2503 name = ERR_PTR(-ENOMEM);
2505 sprintf(name, "%d", tgid);
2507 nd_set_link(nd, name);
2511 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2514 char *s = nd_get_link(nd);
2519 static const struct inode_operations proc_self_inode_operations = {
2520 .readlink = proc_self_readlink,
2521 .follow_link = proc_self_follow_link,
2522 .put_link = proc_self_put_link,
2528 * These are the directory entries in the root directory of /proc
2529 * that properly belong to the /proc filesystem, as they describe
2530 * describe something that is process related.
2532 static const struct pid_entry proc_base_stuff[] = {
2533 NOD("self", S_IFLNK|S_IRWXUGO,
2534 &proc_self_inode_operations, NULL, {}),
2537 static struct dentry *proc_base_instantiate(struct inode *dir,
2538 struct dentry *dentry, struct task_struct *task, const void *ptr)
2540 const struct pid_entry *p = ptr;
2541 struct inode *inode;
2542 struct proc_inode *ei;
2543 struct dentry *error;
2545 /* Allocate the inode */
2546 error = ERR_PTR(-ENOMEM);
2547 inode = new_inode(dir->i_sb);
2551 /* Initialize the inode */
2553 inode->i_ino = get_next_ino();
2554 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2557 * grab the reference to the task.
2559 ei->pid = get_task_pid(task, PIDTYPE_PID);
2563 inode->i_mode = p->mode;
2564 if (S_ISDIR(inode->i_mode))
2565 set_nlink(inode, 2);
2566 if (S_ISLNK(inode->i_mode))
2569 inode->i_op = p->iop;
2571 inode->i_fop = p->fop;
2573 d_add(dentry, inode);
2582 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2584 struct dentry *error;
2585 struct task_struct *task = get_proc_task(dir);
2586 const struct pid_entry *p, *last;
2588 error = ERR_PTR(-ENOENT);
2593 /* Lookup the directory entry */
2594 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2595 for (p = proc_base_stuff; p <= last; p++) {
2596 if (p->len != dentry->d_name.len)
2598 if (!memcmp(dentry->d_name.name, p->name, p->len))
2604 error = proc_base_instantiate(dir, dentry, task, p);
2607 put_task_struct(task);
2612 static int proc_base_fill_cache(struct file *filp, void *dirent,
2613 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2615 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2616 proc_base_instantiate, task, p);
2619 #ifdef CONFIG_TASK_IO_ACCOUNTING
2620 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2622 struct task_io_accounting acct = task->ioac;
2623 unsigned long flags;
2626 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2630 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2635 if (whole && lock_task_sighand(task, &flags)) {
2636 struct task_struct *t = task;
2638 task_io_accounting_add(&acct, &task->signal->ioac);
2639 while_each_thread(task, t)
2640 task_io_accounting_add(&acct, &t->ioac);
2642 unlock_task_sighand(task, &flags);
2644 result = sprintf(buffer,
2649 "read_bytes: %llu\n"
2650 "write_bytes: %llu\n"
2651 "cancelled_write_bytes: %llu\n",
2652 (unsigned long long)acct.rchar,
2653 (unsigned long long)acct.wchar,
2654 (unsigned long long)acct.syscr,
2655 (unsigned long long)acct.syscw,
2656 (unsigned long long)acct.read_bytes,
2657 (unsigned long long)acct.write_bytes,
2658 (unsigned long long)acct.cancelled_write_bytes);
2660 mutex_unlock(&task->signal->cred_guard_mutex);
2664 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2666 return do_io_accounting(task, buffer, 0);
2669 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2671 return do_io_accounting(task, buffer, 1);
2673 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2675 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2676 struct pid *pid, struct task_struct *task)
2678 int err = lock_trace(task);
2680 seq_printf(m, "%08x\n", task->personality);
2689 static const struct file_operations proc_task_operations;
2690 static const struct inode_operations proc_task_inode_operations;
2692 static const struct pid_entry tgid_base_stuff[] = {
2693 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2694 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2695 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2696 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2698 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2700 REG("environ", S_IRUSR, proc_environ_operations),
2701 INF("auxv", S_IRUSR, proc_pid_auxv),
2702 ONE("status", S_IRUGO, proc_pid_status),
2703 ONE("personality", S_IRUGO, proc_pid_personality),
2704 INF("limits", S_IRUGO, proc_pid_limits),
2705 #ifdef CONFIG_SCHED_DEBUG
2706 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2708 #ifdef CONFIG_SCHED_AUTOGROUP
2709 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2711 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2712 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2713 INF("syscall", S_IRUGO, proc_pid_syscall),
2715 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2716 ONE("stat", S_IRUGO, proc_tgid_stat),
2717 ONE("statm", S_IRUGO, proc_pid_statm),
2718 REG("maps", S_IRUGO, proc_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),
3066 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3068 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3069 LNK("cwd", proc_cwd_link),
3070 LNK("root", proc_root_link),
3071 LNK("exe", proc_exe_link),
3072 REG("mounts", S_IRUGO, proc_mounts_operations),
3073 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3074 #ifdef CONFIG_PROC_PAGE_MONITOR
3075 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3076 REG("smaps", S_IRUGO, proc_smaps_operations),
3077 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3079 #ifdef CONFIG_SECURITY
3080 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3082 #ifdef CONFIG_KALLSYMS
3083 INF("wchan", S_IRUGO, proc_pid_wchan),
3085 #ifdef CONFIG_STACKTRACE
3086 ONE("stack", S_IRUGO, proc_pid_stack),
3088 #ifdef CONFIG_SCHEDSTATS
3089 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3091 #ifdef CONFIG_LATENCYTOP
3092 REG("latency", S_IRUGO, proc_lstats_operations),
3094 #ifdef CONFIG_PROC_PID_CPUSET
3095 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3097 #ifdef CONFIG_CGROUPS
3098 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3100 INF("oom_score", S_IRUGO, proc_oom_score),
3101 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3102 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3103 #ifdef CONFIG_AUDITSYSCALL
3104 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3105 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3107 #ifdef CONFIG_FAULT_INJECTION
3108 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3110 #ifdef CONFIG_TASK_IO_ACCOUNTING
3111 INF("io", S_IRUSR, proc_tid_io_accounting),
3113 #ifdef CONFIG_HARDWALL
3114 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3118 static int proc_tid_base_readdir(struct file * filp,
3119 void * dirent, filldir_t filldir)
3121 return proc_pident_readdir(filp,dirent,filldir,
3122 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3125 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3126 return proc_pident_lookup(dir, dentry,
3127 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3130 static const struct file_operations proc_tid_base_operations = {
3131 .read = generic_read_dir,
3132 .readdir = proc_tid_base_readdir,
3133 .llseek = default_llseek,
3136 static const struct inode_operations proc_tid_base_inode_operations = {
3137 .lookup = proc_tid_base_lookup,
3138 .getattr = pid_getattr,
3139 .setattr = proc_setattr,
3142 static struct dentry *proc_task_instantiate(struct inode *dir,
3143 struct dentry *dentry, struct task_struct *task, const void *ptr)
3145 struct dentry *error = ERR_PTR(-ENOENT);
3146 struct inode *inode;
3147 inode = proc_pid_make_inode(dir->i_sb, task);
3151 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3152 inode->i_op = &proc_tid_base_inode_operations;
3153 inode->i_fop = &proc_tid_base_operations;
3154 inode->i_flags|=S_IMMUTABLE;
3156 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3157 ARRAY_SIZE(tid_base_stuff)));
3159 d_set_d_op(dentry, &pid_dentry_operations);
3161 d_add(dentry, inode);
3162 /* Close the race of the process dying before we return the dentry */
3163 if (pid_revalidate(dentry, NULL))
3169 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3171 struct dentry *result = ERR_PTR(-ENOENT);
3172 struct task_struct *task;
3173 struct task_struct *leader = get_proc_task(dir);
3175 struct pid_namespace *ns;
3180 tid = name_to_int(dentry);
3184 ns = dentry->d_sb->s_fs_info;
3186 task = find_task_by_pid_ns(tid, ns);
3188 get_task_struct(task);
3192 if (!same_thread_group(leader, task))
3195 result = proc_task_instantiate(dir, dentry, task, NULL);
3197 put_task_struct(task);
3199 put_task_struct(leader);
3205 * Find the first tid of a thread group to return to user space.
3207 * Usually this is just the thread group leader, but if the users
3208 * buffer was too small or there was a seek into the middle of the
3209 * directory we have more work todo.
3211 * In the case of a short read we start with find_task_by_pid.
3213 * In the case of a seek we start with the leader and walk nr
3216 static struct task_struct *first_tid(struct task_struct *leader,
3217 int tid, int nr, struct pid_namespace *ns)
3219 struct task_struct *pos;
3222 /* Attempt to start with the pid of a thread */
3223 if (tid && (nr > 0)) {
3224 pos = find_task_by_pid_ns(tid, ns);
3225 if (pos && (pos->group_leader == leader))
3229 /* If nr exceeds the number of threads there is nothing todo */
3231 if (nr && nr >= get_nr_threads(leader))
3234 /* If we haven't found our starting place yet start
3235 * with the leader and walk nr threads forward.
3237 for (pos = leader; nr > 0; --nr) {
3238 pos = next_thread(pos);
3239 if (pos == leader) {
3245 get_task_struct(pos);
3252 * Find the next thread in the thread list.
3253 * Return NULL if there is an error or no next thread.
3255 * The reference to the input task_struct is released.
3257 static struct task_struct *next_tid(struct task_struct *start)
3259 struct task_struct *pos = NULL;
3261 if (pid_alive(start)) {
3262 pos = next_thread(start);
3263 if (thread_group_leader(pos))
3266 get_task_struct(pos);
3269 put_task_struct(start);
3273 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3274 struct task_struct *task, int tid)
3276 char name[PROC_NUMBUF];
3277 int len = snprintf(name, sizeof(name), "%d", tid);
3278 return proc_fill_cache(filp, dirent, filldir, name, len,
3279 proc_task_instantiate, task, NULL);
3282 /* for the /proc/TGID/task/ directories */
3283 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3285 struct dentry *dentry = filp->f_path.dentry;
3286 struct inode *inode = dentry->d_inode;
3287 struct task_struct *leader = NULL;
3288 struct task_struct *task;
3289 int retval = -ENOENT;
3292 struct pid_namespace *ns;
3294 task = get_proc_task(inode);
3298 if (pid_alive(task)) {
3299 leader = task->group_leader;
3300 get_task_struct(leader);
3303 put_task_struct(task);
3308 switch ((unsigned long)filp->f_pos) {
3311 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3316 ino = parent_ino(dentry);
3317 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3323 /* f_version caches the tgid value that the last readdir call couldn't
3324 * return. lseek aka telldir automagically resets f_version to 0.
3326 ns = filp->f_dentry->d_sb->s_fs_info;
3327 tid = (int)filp->f_version;
3328 filp->f_version = 0;
3329 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3331 task = next_tid(task), filp->f_pos++) {
3332 tid = task_pid_nr_ns(task, ns);
3333 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3334 /* returning this tgid failed, save it as the first
3335 * pid for the next readir call */
3336 filp->f_version = (u64)tid;
3337 put_task_struct(task);
3342 put_task_struct(leader);
3347 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3349 struct inode *inode = dentry->d_inode;
3350 struct task_struct *p = get_proc_task(inode);
3351 generic_fillattr(inode, stat);
3354 stat->nlink += get_nr_threads(p);
3361 static const struct inode_operations proc_task_inode_operations = {
3362 .lookup = proc_task_lookup,
3363 .getattr = proc_task_getattr,
3364 .setattr = proc_setattr,
3367 static const struct file_operations proc_task_operations = {
3368 .read = generic_read_dir,
3369 .readdir = proc_task_readdir,
3370 .llseek = default_llseek,