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 #include <linux/flex_array.h>
87 #ifdef CONFIG_HARDWALL
88 #include <asm/hardwall.h>
93 * Implementing inode permission operations in /proc is almost
94 * certainly an error. Permission checks need to happen during
95 * each system call not at open time. The reason is that most of
96 * what we wish to check for permissions in /proc varies at runtime.
98 * The classic example of a problem is opening file descriptors
99 * in /proc for a task before it execs a suid executable.
106 const struct inode_operations *iop;
107 const struct file_operations *fop;
111 #define NOD(NAME, MODE, IOP, FOP, OP) { \
113 .len = sizeof(NAME) - 1, \
120 #define DIR(NAME, MODE, iops, fops) \
121 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
122 #define LNK(NAME, get_link) \
123 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
124 &proc_pid_link_inode_operations, NULL, \
125 { .proc_get_link = get_link } )
126 #define REG(NAME, MODE, fops) \
127 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
128 #define INF(NAME, MODE, read) \
129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_info_file_operations, \
131 { .proc_read = read } )
132 #define ONE(NAME, MODE, show) \
133 NOD(NAME, (S_IFREG|(MODE)), \
134 NULL, &proc_single_file_operations, \
135 { .proc_show = show } )
137 static int proc_fd_permission(struct inode *inode, int mask);
140 * Count the number of hardlinks for the pid_entry table, excluding the .
143 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
150 for (i = 0; i < n; ++i) {
151 if (S_ISDIR(entries[i].mode))
158 static int get_task_root(struct task_struct *task, struct path *root)
160 int result = -ENOENT;
164 get_fs_root(task->fs, root);
171 static int proc_cwd_link(struct dentry *dentry, struct path *path)
173 struct task_struct *task = get_proc_task(dentry->d_inode);
174 int result = -ENOENT;
179 get_fs_pwd(task->fs, path);
183 put_task_struct(task);
188 static int proc_root_link(struct dentry *dentry, struct path *path)
190 struct task_struct *task = get_proc_task(dentry->d_inode);
191 int result = -ENOENT;
194 result = get_task_root(task, path);
195 put_task_struct(task);
200 static struct mm_struct *__check_mem_permission(struct task_struct *task)
202 struct mm_struct *mm;
204 mm = get_task_mm(task);
206 return ERR_PTR(-EINVAL);
209 * A task can always look at itself, in case it chooses
210 * to use system calls instead of load instructions.
216 * If current is actively ptrace'ing, and would also be
217 * permitted to freshly attach with ptrace now, permit it.
219 if (task_is_stopped_or_traced(task)) {
222 match = (ptrace_parent(task) == current);
224 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
229 * No one else is allowed.
232 return ERR_PTR(-EPERM);
236 * If current may access user memory in @task return a reference to the
237 * corresponding mm, otherwise ERR_PTR.
239 static struct mm_struct *check_mem_permission(struct task_struct *task)
241 struct mm_struct *mm;
245 * Avoid racing if task exec's as we might get a new mm but validate
246 * against old credentials.
248 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
252 mm = __check_mem_permission(task);
253 mutex_unlock(&task->signal->cred_guard_mutex);
258 struct mm_struct *mm_for_maps(struct task_struct *task)
260 struct mm_struct *mm;
263 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
267 mm = get_task_mm(task);
268 if (mm && mm != current->mm &&
269 !ptrace_may_access(task, PTRACE_MODE_READ)) {
271 mm = ERR_PTR(-EACCES);
273 mutex_unlock(&task->signal->cred_guard_mutex);
278 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
282 struct mm_struct *mm = get_task_mm(task);
286 goto out_mm; /* Shh! No looking before we're done */
288 len = mm->arg_end - mm->arg_start;
293 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
295 // If the nul at the end of args has been overwritten, then
296 // assume application is using setproctitle(3).
297 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
298 len = strnlen(buffer, res);
302 len = mm->env_end - mm->env_start;
303 if (len > PAGE_SIZE - res)
304 len = PAGE_SIZE - res;
305 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
306 res = strnlen(buffer, res);
315 static int proc_pid_auxv(struct task_struct *task, char *buffer)
317 struct mm_struct *mm = mm_for_maps(task);
318 int res = PTR_ERR(mm);
319 if (mm && !IS_ERR(mm)) {
320 unsigned int nwords = 0;
323 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
324 res = nwords * sizeof(mm->saved_auxv[0]);
327 memcpy(buffer, mm->saved_auxv, res);
334 #ifdef CONFIG_KALLSYMS
336 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
337 * Returns the resolved symbol. If that fails, simply return the address.
339 static int proc_pid_wchan(struct task_struct *task, char *buffer)
342 char symname[KSYM_NAME_LEN];
344 wchan = get_wchan(task);
346 if (lookup_symbol_name(wchan, symname) < 0)
347 if (!ptrace_may_access(task, PTRACE_MODE_READ))
350 return sprintf(buffer, "%lu", wchan);
352 return sprintf(buffer, "%s", symname);
354 #endif /* CONFIG_KALLSYMS */
356 static int lock_trace(struct task_struct *task)
358 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
361 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
362 mutex_unlock(&task->signal->cred_guard_mutex);
368 static void unlock_trace(struct task_struct *task)
370 mutex_unlock(&task->signal->cred_guard_mutex);
373 #ifdef CONFIG_STACKTRACE
375 #define MAX_STACK_TRACE_DEPTH 64
377 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
378 struct pid *pid, struct task_struct *task)
380 struct stack_trace trace;
381 unsigned long *entries;
385 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
389 trace.nr_entries = 0;
390 trace.max_entries = MAX_STACK_TRACE_DEPTH;
391 trace.entries = entries;
394 err = lock_trace(task);
396 save_stack_trace_tsk(task, &trace);
398 for (i = 0; i < trace.nr_entries; i++) {
399 seq_printf(m, "[<%pK>] %pS\n",
400 (void *)entries[i], (void *)entries[i]);
410 #ifdef CONFIG_SCHEDSTATS
412 * Provides /proc/PID/schedstat
414 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
416 return sprintf(buffer, "%llu %llu %lu\n",
417 (unsigned long long)task->se.sum_exec_runtime,
418 (unsigned long long)task->sched_info.run_delay,
419 task->sched_info.pcount);
423 #ifdef CONFIG_LATENCYTOP
424 static int lstats_show_proc(struct seq_file *m, void *v)
427 struct inode *inode = m->private;
428 struct task_struct *task = get_proc_task(inode);
432 seq_puts(m, "Latency Top version : v0.1\n");
433 for (i = 0; i < 32; i++) {
434 struct latency_record *lr = &task->latency_record[i];
435 if (lr->backtrace[0]) {
437 seq_printf(m, "%i %li %li",
438 lr->count, lr->time, lr->max);
439 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
440 unsigned long bt = lr->backtrace[q];
445 seq_printf(m, " %ps", (void *)bt);
451 put_task_struct(task);
455 static int lstats_open(struct inode *inode, struct file *file)
457 return single_open(file, lstats_show_proc, inode);
460 static ssize_t lstats_write(struct file *file, const char __user *buf,
461 size_t count, loff_t *offs)
463 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
467 clear_all_latency_tracing(task);
468 put_task_struct(task);
473 static const struct file_operations proc_lstats_operations = {
476 .write = lstats_write,
478 .release = single_release,
483 static int proc_oom_score(struct task_struct *task, char *buffer)
485 unsigned long points = 0;
487 read_lock(&tasklist_lock);
489 points = oom_badness(task, NULL, NULL,
490 totalram_pages + total_swap_pages);
491 read_unlock(&tasklist_lock);
492 return sprintf(buffer, "%lu\n", points);
500 static const struct limit_names lnames[RLIM_NLIMITS] = {
501 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
502 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
503 [RLIMIT_DATA] = {"Max data size", "bytes"},
504 [RLIMIT_STACK] = {"Max stack size", "bytes"},
505 [RLIMIT_CORE] = {"Max core file size", "bytes"},
506 [RLIMIT_RSS] = {"Max resident set", "bytes"},
507 [RLIMIT_NPROC] = {"Max processes", "processes"},
508 [RLIMIT_NOFILE] = {"Max open files", "files"},
509 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
510 [RLIMIT_AS] = {"Max address space", "bytes"},
511 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
512 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
513 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
514 [RLIMIT_NICE] = {"Max nice priority", NULL},
515 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
516 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
519 /* Display limits for a process */
520 static int proc_pid_limits(struct task_struct *task, char *buffer)
525 char *bufptr = buffer;
527 struct rlimit rlim[RLIM_NLIMITS];
529 if (!lock_task_sighand(task, &flags))
531 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
532 unlock_task_sighand(task, &flags);
535 * print the file header
537 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
538 "Limit", "Soft Limit", "Hard Limit", "Units");
540 for (i = 0; i < RLIM_NLIMITS; i++) {
541 if (rlim[i].rlim_cur == RLIM_INFINITY)
542 count += sprintf(&bufptr[count], "%-25s %-20s ",
543 lnames[i].name, "unlimited");
545 count += sprintf(&bufptr[count], "%-25s %-20lu ",
546 lnames[i].name, rlim[i].rlim_cur);
548 if (rlim[i].rlim_max == RLIM_INFINITY)
549 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
551 count += sprintf(&bufptr[count], "%-20lu ",
555 count += sprintf(&bufptr[count], "%-10s\n",
558 count += sprintf(&bufptr[count], "\n");
564 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
565 static int proc_pid_syscall(struct task_struct *task, char *buffer)
568 unsigned long args[6], sp, pc;
569 int res = lock_trace(task);
573 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
574 res = sprintf(buffer, "running\n");
576 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
578 res = sprintf(buffer,
579 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
581 args[0], args[1], args[2], args[3], args[4], args[5],
586 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
588 /************************************************************************/
589 /* Here the fs part begins */
590 /************************************************************************/
592 /* permission checks */
593 static int proc_fd_access_allowed(struct inode *inode)
595 struct task_struct *task;
597 /* Allow access to a task's file descriptors if it is us or we
598 * may use ptrace attach to the process and find out that
601 task = get_proc_task(inode);
603 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
604 put_task_struct(task);
609 int proc_setattr(struct dentry *dentry, struct iattr *attr)
612 struct inode *inode = dentry->d_inode;
614 if (attr->ia_valid & ATTR_MODE)
617 error = inode_change_ok(inode, attr);
621 if ((attr->ia_valid & ATTR_SIZE) &&
622 attr->ia_size != i_size_read(inode)) {
623 error = vmtruncate(inode, attr->ia_size);
628 setattr_copy(inode, attr);
629 mark_inode_dirty(inode);
633 static const struct inode_operations proc_def_inode_operations = {
634 .setattr = proc_setattr,
637 static int mounts_open_common(struct inode *inode, struct file *file,
638 const struct seq_operations *op)
640 struct task_struct *task = get_proc_task(inode);
642 struct mnt_namespace *ns = NULL;
644 struct proc_mounts *p;
649 nsp = task_nsproxy(task);
656 if (ns && get_task_root(task, &root) == 0)
658 put_task_struct(task);
667 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
671 file->private_data = &p->m;
672 ret = seq_open(file, op);
679 p->m.poll_event = ns->event;
693 static int mounts_release(struct inode *inode, struct file *file)
695 struct proc_mounts *p = file->private_data;
698 return seq_release(inode, file);
701 static unsigned mounts_poll(struct file *file, poll_table *wait)
703 struct proc_mounts *p = file->private_data;
704 unsigned res = POLLIN | POLLRDNORM;
706 poll_wait(file, &p->ns->poll, wait);
707 if (mnt_had_events(p))
708 res |= POLLERR | POLLPRI;
713 static int mounts_open(struct inode *inode, struct file *file)
715 return mounts_open_common(inode, file, &mounts_op);
718 static const struct file_operations proc_mounts_operations = {
722 .release = mounts_release,
726 static int mountinfo_open(struct inode *inode, struct file *file)
728 return mounts_open_common(inode, file, &mountinfo_op);
731 static const struct file_operations proc_mountinfo_operations = {
732 .open = mountinfo_open,
735 .release = mounts_release,
739 static int mountstats_open(struct inode *inode, struct file *file)
741 return mounts_open_common(inode, file, &mountstats_op);
744 static const struct file_operations proc_mountstats_operations = {
745 .open = mountstats_open,
748 .release = mounts_release,
751 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
753 static ssize_t proc_info_read(struct file * file, char __user * buf,
754 size_t count, loff_t *ppos)
756 struct inode * inode = file->f_path.dentry->d_inode;
759 struct task_struct *task = get_proc_task(inode);
765 if (count > PROC_BLOCK_SIZE)
766 count = PROC_BLOCK_SIZE;
769 if (!(page = __get_free_page(GFP_TEMPORARY)))
772 length = PROC_I(inode)->op.proc_read(task, (char*)page);
775 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
778 put_task_struct(task);
783 static const struct file_operations proc_info_file_operations = {
784 .read = proc_info_read,
785 .llseek = generic_file_llseek,
788 static int proc_single_show(struct seq_file *m, void *v)
790 struct inode *inode = m->private;
791 struct pid_namespace *ns;
793 struct task_struct *task;
796 ns = inode->i_sb->s_fs_info;
797 pid = proc_pid(inode);
798 task = get_pid_task(pid, PIDTYPE_PID);
802 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
804 put_task_struct(task);
808 static int proc_single_open(struct inode *inode, struct file *filp)
810 return single_open(filp, proc_single_show, inode);
813 static const struct file_operations proc_single_file_operations = {
814 .open = proc_single_open,
817 .release = single_release,
820 static int mem_open(struct inode* inode, struct file* file)
822 file->private_data = (void*)((long)current->self_exec_id);
823 /* OK to pass negative loff_t, we can catch out-of-range */
824 file->f_mode |= FMODE_UNSIGNED_OFFSET;
828 static ssize_t mem_read(struct file * file, char __user * buf,
829 size_t count, loff_t *ppos)
831 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
833 unsigned long src = *ppos;
835 struct mm_struct *mm;
841 page = (char *)__get_free_page(GFP_TEMPORARY);
845 mm = check_mem_permission(task);
852 if (file->private_data != (void*)((long)current->self_exec_id))
858 int this_len, retval;
860 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
861 retval = access_remote_vm(mm, src, page, this_len, 0);
868 if (copy_to_user(buf, page, retval)) {
883 free_page((unsigned long) page);
885 put_task_struct(task);
890 static ssize_t mem_write(struct file * file, const char __user *buf,
891 size_t count, loff_t *ppos)
895 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
896 unsigned long dst = *ppos;
897 struct mm_struct *mm;
904 page = (char *)__get_free_page(GFP_TEMPORARY);
908 mm = check_mem_permission(task);
909 copied = PTR_ERR(mm);
914 if (file->private_data != (void *)((long)current->self_exec_id))
919 int this_len, retval;
921 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
922 if (copy_from_user(page, buf, this_len)) {
926 retval = access_remote_vm(mm, dst, page, this_len, 1);
942 free_page((unsigned long) page);
944 put_task_struct(task);
949 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
953 file->f_pos = offset;
956 file->f_pos += offset;
961 force_successful_syscall_return();
965 static const struct file_operations proc_mem_operations = {
972 static ssize_t environ_read(struct file *file, char __user *buf,
973 size_t count, loff_t *ppos)
975 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
977 unsigned long src = *ppos;
979 struct mm_struct *mm;
985 page = (char *)__get_free_page(GFP_TEMPORARY);
990 mm = mm_for_maps(task);
992 if (!mm || IS_ERR(mm))
997 int this_len, retval, max_len;
999 this_len = mm->env_end - (mm->env_start + src);
1004 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1005 this_len = (this_len > max_len) ? max_len : this_len;
1007 retval = access_process_vm(task, (mm->env_start + src),
1015 if (copy_to_user(buf, page, retval)) {
1029 free_page((unsigned long) page);
1031 put_task_struct(task);
1036 static const struct file_operations proc_environ_operations = {
1037 .read = environ_read,
1038 .llseek = generic_file_llseek,
1041 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1042 size_t count, loff_t *ppos)
1044 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1045 char buffer[PROC_NUMBUF];
1047 int oom_adjust = OOM_DISABLE;
1048 unsigned long flags;
1053 if (lock_task_sighand(task, &flags)) {
1054 oom_adjust = task->signal->oom_adj;
1055 unlock_task_sighand(task, &flags);
1058 put_task_struct(task);
1060 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1062 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1065 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1066 size_t count, loff_t *ppos)
1068 struct task_struct *task;
1069 char buffer[PROC_NUMBUF];
1071 unsigned long flags;
1074 memset(buffer, 0, sizeof(buffer));
1075 if (count > sizeof(buffer) - 1)
1076 count = sizeof(buffer) - 1;
1077 if (copy_from_user(buffer, buf, count)) {
1082 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1085 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1086 oom_adjust != OOM_DISABLE) {
1091 task = get_proc_task(file->f_path.dentry->d_inode);
1103 if (!lock_task_sighand(task, &flags)) {
1108 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1114 * Warn that /proc/pid/oom_adj is deprecated, see
1115 * Documentation/feature-removal-schedule.txt.
1117 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1118 current->comm, task_pid_nr(current), task_pid_nr(task),
1120 task->signal->oom_adj = oom_adjust;
1122 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1123 * value is always attainable.
1125 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1126 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1128 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1131 unlock_task_sighand(task, &flags);
1134 put_task_struct(task);
1136 return err < 0 ? err : count;
1139 static const struct file_operations proc_oom_adjust_operations = {
1140 .read = oom_adjust_read,
1141 .write = oom_adjust_write,
1142 .llseek = generic_file_llseek,
1145 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1146 size_t count, loff_t *ppos)
1148 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1149 char buffer[PROC_NUMBUF];
1150 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1151 unsigned long flags;
1156 if (lock_task_sighand(task, &flags)) {
1157 oom_score_adj = task->signal->oom_score_adj;
1158 unlock_task_sighand(task, &flags);
1160 put_task_struct(task);
1161 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1162 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1165 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1166 size_t count, loff_t *ppos)
1168 struct task_struct *task;
1169 char buffer[PROC_NUMBUF];
1170 unsigned long flags;
1174 memset(buffer, 0, sizeof(buffer));
1175 if (count > sizeof(buffer) - 1)
1176 count = sizeof(buffer) - 1;
1177 if (copy_from_user(buffer, buf, count)) {
1182 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1185 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1186 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1191 task = get_proc_task(file->f_path.dentry->d_inode);
1203 if (!lock_task_sighand(task, &flags)) {
1208 if (oom_score_adj < task->signal->oom_score_adj_min &&
1209 !capable(CAP_SYS_RESOURCE)) {
1214 task->signal->oom_score_adj = oom_score_adj;
1215 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1216 task->signal->oom_score_adj_min = oom_score_adj;
1218 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1219 * always attainable.
1221 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1222 task->signal->oom_adj = OOM_DISABLE;
1224 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1227 unlock_task_sighand(task, &flags);
1230 put_task_struct(task);
1232 return err < 0 ? err : count;
1235 static const struct file_operations proc_oom_score_adj_operations = {
1236 .read = oom_score_adj_read,
1237 .write = oom_score_adj_write,
1238 .llseek = default_llseek,
1241 #ifdef CONFIG_AUDITSYSCALL
1242 #define TMPBUFLEN 21
1243 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1244 size_t count, loff_t *ppos)
1246 struct inode * inode = file->f_path.dentry->d_inode;
1247 struct task_struct *task = get_proc_task(inode);
1249 char tmpbuf[TMPBUFLEN];
1253 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1254 audit_get_loginuid(task));
1255 put_task_struct(task);
1256 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1259 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1260 size_t count, loff_t *ppos)
1262 struct inode * inode = file->f_path.dentry->d_inode;
1267 if (!capable(CAP_AUDIT_CONTROL))
1271 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1277 if (count >= PAGE_SIZE)
1278 count = PAGE_SIZE - 1;
1281 /* No partial writes. */
1284 page = (char*)__get_free_page(GFP_TEMPORARY);
1288 if (copy_from_user(page, buf, count))
1292 loginuid = simple_strtoul(page, &tmp, 10);
1298 length = audit_set_loginuid(current, loginuid);
1299 if (likely(length == 0))
1303 free_page((unsigned long) page);
1307 static const struct file_operations proc_loginuid_operations = {
1308 .read = proc_loginuid_read,
1309 .write = proc_loginuid_write,
1310 .llseek = generic_file_llseek,
1313 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1314 size_t count, loff_t *ppos)
1316 struct inode * inode = file->f_path.dentry->d_inode;
1317 struct task_struct *task = get_proc_task(inode);
1319 char tmpbuf[TMPBUFLEN];
1323 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1324 audit_get_sessionid(task));
1325 put_task_struct(task);
1326 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1329 static const struct file_operations proc_sessionid_operations = {
1330 .read = proc_sessionid_read,
1331 .llseek = generic_file_llseek,
1335 #ifdef CONFIG_FAULT_INJECTION
1336 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1337 size_t count, loff_t *ppos)
1339 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1340 char buffer[PROC_NUMBUF];
1346 make_it_fail = task->make_it_fail;
1347 put_task_struct(task);
1349 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1351 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1354 static ssize_t proc_fault_inject_write(struct file * file,
1355 const char __user * buf, size_t count, loff_t *ppos)
1357 struct task_struct *task;
1358 char buffer[PROC_NUMBUF], *end;
1361 if (!capable(CAP_SYS_RESOURCE))
1363 memset(buffer, 0, sizeof(buffer));
1364 if (count > sizeof(buffer) - 1)
1365 count = sizeof(buffer) - 1;
1366 if (copy_from_user(buffer, buf, count))
1368 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1371 task = get_proc_task(file->f_dentry->d_inode);
1374 task->make_it_fail = make_it_fail;
1375 put_task_struct(task);
1380 static const struct file_operations proc_fault_inject_operations = {
1381 .read = proc_fault_inject_read,
1382 .write = proc_fault_inject_write,
1383 .llseek = generic_file_llseek,
1388 #ifdef CONFIG_SCHED_DEBUG
1390 * Print out various scheduling related per-task fields:
1392 static int sched_show(struct seq_file *m, void *v)
1394 struct inode *inode = m->private;
1395 struct task_struct *p;
1397 p = get_proc_task(inode);
1400 proc_sched_show_task(p, m);
1408 sched_write(struct file *file, const char __user *buf,
1409 size_t count, loff_t *offset)
1411 struct inode *inode = file->f_path.dentry->d_inode;
1412 struct task_struct *p;
1414 p = get_proc_task(inode);
1417 proc_sched_set_task(p);
1424 static int sched_open(struct inode *inode, struct file *filp)
1426 return single_open(filp, sched_show, inode);
1429 static const struct file_operations proc_pid_sched_operations = {
1432 .write = sched_write,
1433 .llseek = seq_lseek,
1434 .release = single_release,
1439 #ifdef CONFIG_SCHED_AUTOGROUP
1441 * Print out autogroup related information:
1443 static int sched_autogroup_show(struct seq_file *m, void *v)
1445 struct inode *inode = m->private;
1446 struct task_struct *p;
1448 p = get_proc_task(inode);
1451 proc_sched_autogroup_show_task(p, m);
1459 sched_autogroup_write(struct file *file, const char __user *buf,
1460 size_t count, loff_t *offset)
1462 struct inode *inode = file->f_path.dentry->d_inode;
1463 struct task_struct *p;
1464 char buffer[PROC_NUMBUF];
1468 memset(buffer, 0, sizeof(buffer));
1469 if (count > sizeof(buffer) - 1)
1470 count = sizeof(buffer) - 1;
1471 if (copy_from_user(buffer, buf, count))
1474 err = kstrtoint(strstrip(buffer), 0, &nice);
1478 p = get_proc_task(inode);
1483 err = proc_sched_autogroup_set_nice(p, &err);
1492 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1496 ret = single_open(filp, sched_autogroup_show, NULL);
1498 struct seq_file *m = filp->private_data;
1505 static const struct file_operations proc_pid_sched_autogroup_operations = {
1506 .open = sched_autogroup_open,
1508 .write = sched_autogroup_write,
1509 .llseek = seq_lseek,
1510 .release = single_release,
1513 #endif /* CONFIG_SCHED_AUTOGROUP */
1515 static ssize_t comm_write(struct file *file, const char __user *buf,
1516 size_t count, loff_t *offset)
1518 struct inode *inode = file->f_path.dentry->d_inode;
1519 struct task_struct *p;
1520 char buffer[TASK_COMM_LEN];
1522 memset(buffer, 0, sizeof(buffer));
1523 if (count > sizeof(buffer) - 1)
1524 count = sizeof(buffer) - 1;
1525 if (copy_from_user(buffer, buf, count))
1528 p = get_proc_task(inode);
1532 if (same_thread_group(current, p))
1533 set_task_comm(p, buffer);
1542 static int comm_show(struct seq_file *m, void *v)
1544 struct inode *inode = m->private;
1545 struct task_struct *p;
1547 p = get_proc_task(inode);
1552 seq_printf(m, "%s\n", p->comm);
1560 static int comm_open(struct inode *inode, struct file *filp)
1562 return single_open(filp, comm_show, inode);
1565 static const struct file_operations proc_pid_set_comm_operations = {
1568 .write = comm_write,
1569 .llseek = seq_lseek,
1570 .release = single_release,
1573 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1575 struct task_struct *task;
1576 struct mm_struct *mm;
1577 struct file *exe_file;
1579 task = get_proc_task(dentry->d_inode);
1582 mm = get_task_mm(task);
1583 put_task_struct(task);
1586 exe_file = get_mm_exe_file(mm);
1589 *exe_path = exe_file->f_path;
1590 path_get(&exe_file->f_path);
1597 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1599 struct inode *inode = dentry->d_inode;
1600 int error = -EACCES;
1602 /* We don't need a base pointer in the /proc filesystem */
1603 path_put(&nd->path);
1605 /* Are we allowed to snoop on the tasks file descriptors? */
1606 if (!proc_fd_access_allowed(inode))
1609 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1611 return ERR_PTR(error);
1614 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1616 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1623 pathname = d_path(path, tmp, PAGE_SIZE);
1624 len = PTR_ERR(pathname);
1625 if (IS_ERR(pathname))
1627 len = tmp + PAGE_SIZE - 1 - pathname;
1631 if (copy_to_user(buffer, pathname, len))
1634 free_page((unsigned long)tmp);
1638 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1640 int error = -EACCES;
1641 struct inode *inode = dentry->d_inode;
1644 /* Are we allowed to snoop on the tasks file descriptors? */
1645 if (!proc_fd_access_allowed(inode))
1648 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1652 error = do_proc_readlink(&path, buffer, buflen);
1658 static const struct inode_operations proc_pid_link_inode_operations = {
1659 .readlink = proc_pid_readlink,
1660 .follow_link = proc_pid_follow_link,
1661 .setattr = proc_setattr,
1665 /* building an inode */
1667 static int task_dumpable(struct task_struct *task)
1670 struct mm_struct *mm;
1675 dumpable = get_dumpable(mm);
1682 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1684 struct inode * inode;
1685 struct proc_inode *ei;
1686 const struct cred *cred;
1688 /* We need a new inode */
1690 inode = new_inode(sb);
1696 inode->i_ino = get_next_ino();
1697 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1698 inode->i_op = &proc_def_inode_operations;
1701 * grab the reference to task.
1703 ei->pid = get_task_pid(task, PIDTYPE_PID);
1707 if (task_dumpable(task)) {
1709 cred = __task_cred(task);
1710 inode->i_uid = cred->euid;
1711 inode->i_gid = cred->egid;
1714 security_task_to_inode(task, inode);
1724 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1726 struct inode *inode = dentry->d_inode;
1727 struct task_struct *task;
1728 const struct cred *cred;
1730 generic_fillattr(inode, stat);
1735 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1737 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1738 task_dumpable(task)) {
1739 cred = __task_cred(task);
1740 stat->uid = cred->euid;
1741 stat->gid = cred->egid;
1751 * Exceptional case: normally we are not allowed to unhash a busy
1752 * directory. In this case, however, we can do it - no aliasing problems
1753 * due to the way we treat inodes.
1755 * Rewrite the inode's ownerships here because the owning task may have
1756 * performed a setuid(), etc.
1758 * Before the /proc/pid/status file was created the only way to read
1759 * the effective uid of a /process was to stat /proc/pid. Reading
1760 * /proc/pid/status is slow enough that procps and other packages
1761 * kept stating /proc/pid. To keep the rules in /proc simple I have
1762 * made this apply to all per process world readable and executable
1765 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1767 struct inode *inode;
1768 struct task_struct *task;
1769 const struct cred *cred;
1771 if (nd && nd->flags & LOOKUP_RCU)
1774 inode = dentry->d_inode;
1775 task = get_proc_task(inode);
1778 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1779 task_dumpable(task)) {
1781 cred = __task_cred(task);
1782 inode->i_uid = cred->euid;
1783 inode->i_gid = cred->egid;
1789 inode->i_mode &= ~(S_ISUID | S_ISGID);
1790 security_task_to_inode(task, inode);
1791 put_task_struct(task);
1798 static int pid_delete_dentry(const struct dentry * dentry)
1800 /* Is the task we represent dead?
1801 * If so, then don't put the dentry on the lru list,
1802 * kill it immediately.
1804 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1807 const struct dentry_operations pid_dentry_operations =
1809 .d_revalidate = pid_revalidate,
1810 .d_delete = pid_delete_dentry,
1816 * Fill a directory entry.
1818 * If possible create the dcache entry and derive our inode number and
1819 * file type from dcache entry.
1821 * Since all of the proc inode numbers are dynamically generated, the inode
1822 * numbers do not exist until the inode is cache. This means creating the
1823 * the dcache entry in readdir is necessary to keep the inode numbers
1824 * reported by readdir in sync with the inode numbers reported
1827 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1828 const char *name, int len,
1829 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1831 struct dentry *child, *dir = filp->f_path.dentry;
1832 struct inode *inode;
1835 unsigned type = DT_UNKNOWN;
1839 qname.hash = full_name_hash(name, len);
1841 child = d_lookup(dir, &qname);
1844 new = d_alloc(dir, &qname);
1846 child = instantiate(dir->d_inode, new, task, ptr);
1853 if (!child || IS_ERR(child) || !child->d_inode)
1854 goto end_instantiate;
1855 inode = child->d_inode;
1858 type = inode->i_mode >> 12;
1863 ino = find_inode_number(dir, &qname);
1866 return filldir(dirent, name, len, filp->f_pos, ino, type);
1869 static unsigned name_to_int(struct dentry *dentry)
1871 const char *name = dentry->d_name.name;
1872 int len = dentry->d_name.len;
1875 if (len > 1 && *name == '0')
1878 unsigned c = *name++ - '0';
1881 if (n >= (~0U-9)/10)
1891 #define PROC_FDINFO_MAX 64
1893 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1895 struct task_struct *task = get_proc_task(inode);
1896 struct files_struct *files = NULL;
1898 int fd = proc_fd(inode);
1901 files = get_files_struct(task);
1902 put_task_struct(task);
1906 * We are not taking a ref to the file structure, so we must
1909 spin_lock(&files->file_lock);
1910 file = fcheck_files(files, fd);
1912 unsigned int f_flags;
1913 struct fdtable *fdt;
1915 fdt = files_fdtable(files);
1916 f_flags = file->f_flags & ~O_CLOEXEC;
1917 if (FD_ISSET(fd, fdt->close_on_exec))
1918 f_flags |= O_CLOEXEC;
1921 *path = file->f_path;
1922 path_get(&file->f_path);
1925 snprintf(info, PROC_FDINFO_MAX,
1928 (long long) file->f_pos,
1930 spin_unlock(&files->file_lock);
1931 put_files_struct(files);
1934 spin_unlock(&files->file_lock);
1935 put_files_struct(files);
1940 static int proc_fd_link(struct dentry *dentry, struct path *path)
1942 return proc_fd_info(dentry->d_inode, path, NULL);
1945 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1947 struct inode *inode;
1948 struct task_struct *task;
1950 struct files_struct *files;
1951 const struct cred *cred;
1953 if (nd && nd->flags & LOOKUP_RCU)
1956 inode = dentry->d_inode;
1957 task = get_proc_task(inode);
1958 fd = proc_fd(inode);
1961 files = get_files_struct(task);
1964 if (fcheck_files(files, fd)) {
1966 put_files_struct(files);
1967 if (task_dumpable(task)) {
1969 cred = __task_cred(task);
1970 inode->i_uid = cred->euid;
1971 inode->i_gid = cred->egid;
1977 inode->i_mode &= ~(S_ISUID | S_ISGID);
1978 security_task_to_inode(task, inode);
1979 put_task_struct(task);
1983 put_files_struct(files);
1985 put_task_struct(task);
1991 static const struct dentry_operations tid_fd_dentry_operations =
1993 .d_revalidate = tid_fd_revalidate,
1994 .d_delete = pid_delete_dentry,
1997 static struct dentry *proc_fd_instantiate(struct inode *dir,
1998 struct dentry *dentry, struct task_struct *task, const void *ptr)
2000 unsigned fd = *(const unsigned *)ptr;
2002 struct files_struct *files;
2003 struct inode *inode;
2004 struct proc_inode *ei;
2005 struct dentry *error = ERR_PTR(-ENOENT);
2007 inode = proc_pid_make_inode(dir->i_sb, task);
2012 files = get_files_struct(task);
2015 inode->i_mode = S_IFLNK;
2018 * We are not taking a ref to the file structure, so we must
2021 spin_lock(&files->file_lock);
2022 file = fcheck_files(files, fd);
2025 if (file->f_mode & FMODE_READ)
2026 inode->i_mode |= S_IRUSR | S_IXUSR;
2027 if (file->f_mode & FMODE_WRITE)
2028 inode->i_mode |= S_IWUSR | S_IXUSR;
2029 spin_unlock(&files->file_lock);
2030 put_files_struct(files);
2032 inode->i_op = &proc_pid_link_inode_operations;
2034 ei->op.proc_get_link = proc_fd_link;
2035 d_set_d_op(dentry, &tid_fd_dentry_operations);
2036 d_add(dentry, inode);
2037 /* Close the race of the process dying before we return the dentry */
2038 if (tid_fd_revalidate(dentry, NULL))
2044 spin_unlock(&files->file_lock);
2045 put_files_struct(files);
2051 static struct dentry *proc_lookupfd_common(struct inode *dir,
2052 struct dentry *dentry,
2053 instantiate_t instantiate)
2055 struct task_struct *task = get_proc_task(dir);
2056 unsigned fd = name_to_int(dentry);
2057 struct dentry *result = ERR_PTR(-ENOENT);
2064 result = instantiate(dir, dentry, task, &fd);
2066 put_task_struct(task);
2071 static int proc_readfd_common(struct file * filp, void * dirent,
2072 filldir_t filldir, instantiate_t instantiate)
2074 struct dentry *dentry = filp->f_path.dentry;
2075 struct inode *inode = dentry->d_inode;
2076 struct task_struct *p = get_proc_task(inode);
2077 unsigned int fd, ino;
2079 struct files_struct * files;
2089 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2093 ino = parent_ino(dentry);
2094 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2098 files = get_files_struct(p);
2102 for (fd = filp->f_pos-2;
2103 fd < files_fdtable(files)->max_fds;
2104 fd++, filp->f_pos++) {
2105 char name[PROC_NUMBUF];
2108 if (!fcheck_files(files, fd))
2112 len = snprintf(name, sizeof(name), "%d", fd);
2113 if (proc_fill_cache(filp, dirent, filldir,
2114 name, len, instantiate,
2122 put_files_struct(files);
2130 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2131 struct nameidata *nd)
2133 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2136 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2138 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2141 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2142 size_t len, loff_t *ppos)
2144 char tmp[PROC_FDINFO_MAX];
2145 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2147 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2151 static const struct file_operations proc_fdinfo_file_operations = {
2152 .open = nonseekable_open,
2153 .read = proc_fdinfo_read,
2154 .llseek = no_llseek,
2157 static const struct file_operations proc_fd_operations = {
2158 .read = generic_read_dir,
2159 .readdir = proc_readfd,
2160 .llseek = default_llseek,
2164 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2165 * which represent vma start and end addresses.
2167 static int dname_to_vma_addr(struct dentry *dentry,
2168 unsigned long *start, unsigned long *end)
2170 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2176 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2178 unsigned long vm_start, vm_end;
2179 bool exact_vma_exists = false;
2180 struct mm_struct *mm = NULL;
2181 struct task_struct *task;
2182 const struct cred *cred;
2183 struct inode *inode;
2186 if (nd && nd->flags & LOOKUP_RCU)
2189 if (!capable(CAP_SYS_ADMIN)) {
2194 inode = dentry->d_inode;
2195 task = get_proc_task(inode);
2199 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2202 mm = get_task_mm(task);
2206 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2207 down_read(&mm->mmap_sem);
2208 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2209 up_read(&mm->mmap_sem);
2214 if (exact_vma_exists) {
2215 if (task_dumpable(task)) {
2217 cred = __task_cred(task);
2218 inode->i_uid = cred->euid;
2219 inode->i_gid = cred->egid;
2225 security_task_to_inode(task, inode);
2230 put_task_struct(task);
2239 static const struct dentry_operations tid_map_files_dentry_operations = {
2240 .d_revalidate = map_files_d_revalidate,
2241 .d_delete = pid_delete_dentry,
2244 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2246 unsigned long vm_start, vm_end;
2247 struct vm_area_struct *vma;
2248 struct task_struct *task;
2249 struct mm_struct *mm;
2253 task = get_proc_task(dentry->d_inode);
2257 mm = get_task_mm(task);
2258 put_task_struct(task);
2262 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2266 down_read(&mm->mmap_sem);
2267 vma = find_exact_vma(mm, vm_start, vm_end);
2268 if (vma && vma->vm_file) {
2269 *path = vma->vm_file->f_path;
2273 up_read(&mm->mmap_sem);
2281 struct map_files_info {
2284 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2287 static struct dentry *
2288 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2289 struct task_struct *task, const void *ptr)
2291 const struct file *file = ptr;
2292 struct proc_inode *ei;
2293 struct inode *inode;
2296 return ERR_PTR(-ENOENT);
2298 inode = proc_pid_make_inode(dir->i_sb, task);
2300 return ERR_PTR(-ENOENT);
2303 ei->op.proc_get_link = proc_map_files_get_link;
2305 inode->i_op = &proc_pid_link_inode_operations;
2307 inode->i_mode = S_IFLNK;
2309 if (file->f_mode & FMODE_READ)
2310 inode->i_mode |= S_IRUSR;
2311 if (file->f_mode & FMODE_WRITE)
2312 inode->i_mode |= S_IWUSR;
2314 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2315 d_add(dentry, inode);
2320 static struct dentry *proc_map_files_lookup(struct inode *dir,
2321 struct dentry *dentry, struct nameidata *nd)
2323 unsigned long vm_start, vm_end;
2324 struct vm_area_struct *vma;
2325 struct task_struct *task;
2326 struct dentry *result;
2327 struct mm_struct *mm;
2329 result = ERR_PTR(-EACCES);
2330 if (!capable(CAP_SYS_ADMIN))
2333 result = ERR_PTR(-ENOENT);
2334 task = get_proc_task(dir);
2338 result = ERR_PTR(-EACCES);
2339 if (lock_trace(task))
2342 result = ERR_PTR(-ENOENT);
2343 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2346 mm = get_task_mm(task);
2350 down_read(&mm->mmap_sem);
2351 vma = find_exact_vma(mm, vm_start, vm_end);
2355 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2358 up_read(&mm->mmap_sem);
2363 put_task_struct(task);
2368 static const struct inode_operations proc_map_files_inode_operations = {
2369 .lookup = proc_map_files_lookup,
2370 .permission = proc_fd_permission,
2371 .setattr = proc_setattr,
2375 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2377 struct dentry *dentry = filp->f_path.dentry;
2378 struct inode *inode = dentry->d_inode;
2379 struct vm_area_struct *vma;
2380 struct task_struct *task;
2381 struct mm_struct *mm;
2386 if (!capable(CAP_SYS_ADMIN))
2390 task = get_proc_task(inode);
2395 if (lock_trace(task))
2399 switch (filp->f_pos) {
2402 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2406 ino = parent_ino(dentry);
2407 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2412 unsigned long nr_files, pos, i;
2413 struct flex_array *fa = NULL;
2414 struct map_files_info info;
2415 struct map_files_info *p;
2417 mm = get_task_mm(task);
2420 down_read(&mm->mmap_sem);
2425 * We need two passes here:
2427 * 1) Collect vmas of mapped files with mmap_sem taken
2428 * 2) Release mmap_sem and instantiate entries
2430 * otherwise we get lockdep complained, since filldir()
2431 * routine might require mmap_sem taken in might_fault().
2434 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2435 if (vma->vm_file && ++pos > filp->f_pos)
2440 fa = flex_array_alloc(sizeof(info), nr_files,
2442 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2446 flex_array_free(fa);
2447 up_read(&mm->mmap_sem);
2451 for (i = 0, vma = mm->mmap, pos = 2; vma;
2452 vma = vma->vm_next) {
2455 if (++pos <= filp->f_pos)
2458 get_file(vma->vm_file);
2459 info.file = vma->vm_file;
2460 info.len = snprintf(info.name,
2461 sizeof(info.name), "%lx-%lx",
2462 vma->vm_start, vma->vm_end);
2463 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2467 up_read(&mm->mmap_sem);
2469 for (i = 0; i < nr_files; i++) {
2470 p = flex_array_get(fa, i);
2471 ret = proc_fill_cache(filp, dirent, filldir,
2473 proc_map_files_instantiate,
2480 for (; i < nr_files; i++) {
2482 * In case of error don't forget
2483 * to put rest of file refs.
2485 p = flex_array_get(fa, i);
2489 flex_array_free(fa);
2497 put_task_struct(task);
2502 static const struct file_operations proc_map_files_operations = {
2503 .read = generic_read_dir,
2504 .readdir = proc_map_files_readdir,
2505 .llseek = default_llseek,
2509 * /proc/pid/fd needs a special permission handler so that a process can still
2510 * access /proc/self/fd after it has executed a setuid().
2512 static int proc_fd_permission(struct inode *inode, int mask)
2514 int rv = generic_permission(inode, mask);
2517 if (task_pid(current) == proc_pid(inode))
2523 * proc directories can do almost nothing..
2525 static const struct inode_operations proc_fd_inode_operations = {
2526 .lookup = proc_lookupfd,
2527 .permission = proc_fd_permission,
2528 .setattr = proc_setattr,
2531 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2532 struct dentry *dentry, struct task_struct *task, const void *ptr)
2534 unsigned fd = *(unsigned *)ptr;
2535 struct inode *inode;
2536 struct proc_inode *ei;
2537 struct dentry *error = ERR_PTR(-ENOENT);
2539 inode = proc_pid_make_inode(dir->i_sb, task);
2544 inode->i_mode = S_IFREG | S_IRUSR;
2545 inode->i_fop = &proc_fdinfo_file_operations;
2546 d_set_d_op(dentry, &tid_fd_dentry_operations);
2547 d_add(dentry, inode);
2548 /* Close the race of the process dying before we return the dentry */
2549 if (tid_fd_revalidate(dentry, NULL))
2556 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2557 struct dentry *dentry,
2558 struct nameidata *nd)
2560 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2563 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2565 return proc_readfd_common(filp, dirent, filldir,
2566 proc_fdinfo_instantiate);
2569 static const struct file_operations proc_fdinfo_operations = {
2570 .read = generic_read_dir,
2571 .readdir = proc_readfdinfo,
2572 .llseek = default_llseek,
2576 * proc directories can do almost nothing..
2578 static const struct inode_operations proc_fdinfo_inode_operations = {
2579 .lookup = proc_lookupfdinfo,
2580 .setattr = proc_setattr,
2584 static struct dentry *proc_pident_instantiate(struct inode *dir,
2585 struct dentry *dentry, struct task_struct *task, const void *ptr)
2587 const struct pid_entry *p = ptr;
2588 struct inode *inode;
2589 struct proc_inode *ei;
2590 struct dentry *error = ERR_PTR(-ENOENT);
2592 inode = proc_pid_make_inode(dir->i_sb, task);
2597 inode->i_mode = p->mode;
2598 if (S_ISDIR(inode->i_mode))
2599 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2601 inode->i_op = p->iop;
2603 inode->i_fop = p->fop;
2605 d_set_d_op(dentry, &pid_dentry_operations);
2606 d_add(dentry, inode);
2607 /* Close the race of the process dying before we return the dentry */
2608 if (pid_revalidate(dentry, NULL))
2614 static struct dentry *proc_pident_lookup(struct inode *dir,
2615 struct dentry *dentry,
2616 const struct pid_entry *ents,
2619 struct dentry *error;
2620 struct task_struct *task = get_proc_task(dir);
2621 const struct pid_entry *p, *last;
2623 error = ERR_PTR(-ENOENT);
2629 * Yes, it does not scale. And it should not. Don't add
2630 * new entries into /proc/<tgid>/ without very good reasons.
2632 last = &ents[nents - 1];
2633 for (p = ents; p <= last; p++) {
2634 if (p->len != dentry->d_name.len)
2636 if (!memcmp(dentry->d_name.name, p->name, p->len))
2642 error = proc_pident_instantiate(dir, dentry, task, p);
2644 put_task_struct(task);
2649 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2650 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2652 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2653 proc_pident_instantiate, task, p);
2656 static int proc_pident_readdir(struct file *filp,
2657 void *dirent, filldir_t filldir,
2658 const struct pid_entry *ents, unsigned int nents)
2661 struct dentry *dentry = filp->f_path.dentry;
2662 struct inode *inode = dentry->d_inode;
2663 struct task_struct *task = get_proc_task(inode);
2664 const struct pid_entry *p, *last;
2677 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2683 ino = parent_ino(dentry);
2684 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2696 last = &ents[nents - 1];
2698 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2707 put_task_struct(task);
2712 #ifdef CONFIG_SECURITY
2713 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2714 size_t count, loff_t *ppos)
2716 struct inode * inode = file->f_path.dentry->d_inode;
2719 struct task_struct *task = get_proc_task(inode);
2724 length = security_getprocattr(task,
2725 (char*)file->f_path.dentry->d_name.name,
2727 put_task_struct(task);
2729 length = simple_read_from_buffer(buf, count, ppos, p, length);
2734 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2735 size_t count, loff_t *ppos)
2737 struct inode * inode = file->f_path.dentry->d_inode;
2740 struct task_struct *task = get_proc_task(inode);
2745 if (count > PAGE_SIZE)
2748 /* No partial writes. */
2754 page = (char*)__get_free_page(GFP_TEMPORARY);
2759 if (copy_from_user(page, buf, count))
2762 /* Guard against adverse ptrace interaction */
2763 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2767 length = security_setprocattr(task,
2768 (char*)file->f_path.dentry->d_name.name,
2769 (void*)page, count);
2770 mutex_unlock(&task->signal->cred_guard_mutex);
2772 free_page((unsigned long) page);
2774 put_task_struct(task);
2779 static const struct file_operations proc_pid_attr_operations = {
2780 .read = proc_pid_attr_read,
2781 .write = proc_pid_attr_write,
2782 .llseek = generic_file_llseek,
2785 static const struct pid_entry attr_dir_stuff[] = {
2786 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2787 REG("prev", S_IRUGO, proc_pid_attr_operations),
2788 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2789 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2790 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2791 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2794 static int proc_attr_dir_readdir(struct file * filp,
2795 void * dirent, filldir_t filldir)
2797 return proc_pident_readdir(filp,dirent,filldir,
2798 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2801 static const struct file_operations proc_attr_dir_operations = {
2802 .read = generic_read_dir,
2803 .readdir = proc_attr_dir_readdir,
2804 .llseek = default_llseek,
2807 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2808 struct dentry *dentry, struct nameidata *nd)
2810 return proc_pident_lookup(dir, dentry,
2811 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2814 static const struct inode_operations proc_attr_dir_inode_operations = {
2815 .lookup = proc_attr_dir_lookup,
2816 .getattr = pid_getattr,
2817 .setattr = proc_setattr,
2822 #ifdef CONFIG_ELF_CORE
2823 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2824 size_t count, loff_t *ppos)
2826 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2827 struct mm_struct *mm;
2828 char buffer[PROC_NUMBUF];
2836 mm = get_task_mm(task);
2838 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2839 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2840 MMF_DUMP_FILTER_SHIFT));
2842 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2845 put_task_struct(task);
2850 static ssize_t proc_coredump_filter_write(struct file *file,
2851 const char __user *buf,
2855 struct task_struct *task;
2856 struct mm_struct *mm;
2857 char buffer[PROC_NUMBUF], *end;
2864 memset(buffer, 0, sizeof(buffer));
2865 if (count > sizeof(buffer) - 1)
2866 count = sizeof(buffer) - 1;
2867 if (copy_from_user(buffer, buf, count))
2871 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2874 if (end - buffer == 0)
2878 task = get_proc_task(file->f_dentry->d_inode);
2883 mm = get_task_mm(task);
2887 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2889 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2891 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2896 put_task_struct(task);
2901 static const struct file_operations proc_coredump_filter_operations = {
2902 .read = proc_coredump_filter_read,
2903 .write = proc_coredump_filter_write,
2904 .llseek = generic_file_llseek,
2911 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2914 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2915 pid_t tgid = task_tgid_nr_ns(current, ns);
2916 char tmp[PROC_NUMBUF];
2919 sprintf(tmp, "%d", tgid);
2920 return vfs_readlink(dentry,buffer,buflen,tmp);
2923 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2925 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2926 pid_t tgid = task_tgid_nr_ns(current, ns);
2927 char *name = ERR_PTR(-ENOENT);
2931 name = ERR_PTR(-ENOMEM);
2933 sprintf(name, "%d", tgid);
2935 nd_set_link(nd, name);
2939 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2942 char *s = nd_get_link(nd);
2947 static const struct inode_operations proc_self_inode_operations = {
2948 .readlink = proc_self_readlink,
2949 .follow_link = proc_self_follow_link,
2950 .put_link = proc_self_put_link,
2956 * These are the directory entries in the root directory of /proc
2957 * that properly belong to the /proc filesystem, as they describe
2958 * describe something that is process related.
2960 static const struct pid_entry proc_base_stuff[] = {
2961 NOD("self", S_IFLNK|S_IRWXUGO,
2962 &proc_self_inode_operations, NULL, {}),
2965 static struct dentry *proc_base_instantiate(struct inode *dir,
2966 struct dentry *dentry, struct task_struct *task, const void *ptr)
2968 const struct pid_entry *p = ptr;
2969 struct inode *inode;
2970 struct proc_inode *ei;
2971 struct dentry *error;
2973 /* Allocate the inode */
2974 error = ERR_PTR(-ENOMEM);
2975 inode = new_inode(dir->i_sb);
2979 /* Initialize the inode */
2981 inode->i_ino = get_next_ino();
2982 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2985 * grab the reference to the task.
2987 ei->pid = get_task_pid(task, PIDTYPE_PID);
2991 inode->i_mode = p->mode;
2992 if (S_ISDIR(inode->i_mode))
2993 set_nlink(inode, 2);
2994 if (S_ISLNK(inode->i_mode))
2997 inode->i_op = p->iop;
2999 inode->i_fop = p->fop;
3001 d_add(dentry, inode);
3010 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
3012 struct dentry *error;
3013 struct task_struct *task = get_proc_task(dir);
3014 const struct pid_entry *p, *last;
3016 error = ERR_PTR(-ENOENT);
3021 /* Lookup the directory entry */
3022 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
3023 for (p = proc_base_stuff; p <= last; p++) {
3024 if (p->len != dentry->d_name.len)
3026 if (!memcmp(dentry->d_name.name, p->name, p->len))
3032 error = proc_base_instantiate(dir, dentry, task, p);
3035 put_task_struct(task);
3040 static int proc_base_fill_cache(struct file *filp, void *dirent,
3041 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
3043 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
3044 proc_base_instantiate, task, p);
3047 #ifdef CONFIG_TASK_IO_ACCOUNTING
3048 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
3050 struct task_io_accounting acct = task->ioac;
3051 unsigned long flags;
3054 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
3058 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
3063 if (whole && lock_task_sighand(task, &flags)) {
3064 struct task_struct *t = task;
3066 task_io_accounting_add(&acct, &task->signal->ioac);
3067 while_each_thread(task, t)
3068 task_io_accounting_add(&acct, &t->ioac);
3070 unlock_task_sighand(task, &flags);
3072 result = sprintf(buffer,
3077 "read_bytes: %llu\n"
3078 "write_bytes: %llu\n"
3079 "cancelled_write_bytes: %llu\n",
3080 (unsigned long long)acct.rchar,
3081 (unsigned long long)acct.wchar,
3082 (unsigned long long)acct.syscr,
3083 (unsigned long long)acct.syscw,
3084 (unsigned long long)acct.read_bytes,
3085 (unsigned long long)acct.write_bytes,
3086 (unsigned long long)acct.cancelled_write_bytes);
3088 mutex_unlock(&task->signal->cred_guard_mutex);
3092 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3094 return do_io_accounting(task, buffer, 0);
3097 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3099 return do_io_accounting(task, buffer, 1);
3101 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3103 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3104 struct pid *pid, struct task_struct *task)
3106 int err = lock_trace(task);
3108 seq_printf(m, "%08x\n", task->personality);
3117 static const struct file_operations proc_task_operations;
3118 static const struct inode_operations proc_task_inode_operations;
3120 static const struct pid_entry tgid_base_stuff[] = {
3121 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3122 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3123 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3124 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3125 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3127 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3129 REG("environ", S_IRUSR, proc_environ_operations),
3130 INF("auxv", S_IRUSR, proc_pid_auxv),
3131 ONE("status", S_IRUGO, proc_pid_status),
3132 ONE("personality", S_IRUGO, proc_pid_personality),
3133 INF("limits", S_IRUGO, proc_pid_limits),
3134 #ifdef CONFIG_SCHED_DEBUG
3135 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3137 #ifdef CONFIG_SCHED_AUTOGROUP
3138 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3140 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3141 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3142 INF("syscall", S_IRUGO, proc_pid_syscall),
3144 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3145 ONE("stat", S_IRUGO, proc_tgid_stat),
3146 ONE("statm", S_IRUGO, proc_pid_statm),
3147 REG("maps", S_IRUGO, proc_maps_operations),
3149 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3151 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3152 LNK("cwd", proc_cwd_link),
3153 LNK("root", proc_root_link),
3154 LNK("exe", proc_exe_link),
3155 REG("mounts", S_IRUGO, proc_mounts_operations),
3156 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3157 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3158 #ifdef CONFIG_PROC_PAGE_MONITOR
3159 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3160 REG("smaps", S_IRUGO, proc_smaps_operations),
3161 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3163 #ifdef CONFIG_SECURITY
3164 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3166 #ifdef CONFIG_KALLSYMS
3167 INF("wchan", S_IRUGO, proc_pid_wchan),
3169 #ifdef CONFIG_STACKTRACE
3170 ONE("stack", S_IRUGO, proc_pid_stack),
3172 #ifdef CONFIG_SCHEDSTATS
3173 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3175 #ifdef CONFIG_LATENCYTOP
3176 REG("latency", S_IRUGO, proc_lstats_operations),
3178 #ifdef CONFIG_PROC_PID_CPUSET
3179 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3181 #ifdef CONFIG_CGROUPS
3182 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3184 INF("oom_score", S_IRUGO, proc_oom_score),
3185 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3186 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3187 #ifdef CONFIG_AUDITSYSCALL
3188 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3189 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3191 #ifdef CONFIG_FAULT_INJECTION
3192 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3194 #ifdef CONFIG_ELF_CORE
3195 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3197 #ifdef CONFIG_TASK_IO_ACCOUNTING
3198 INF("io", S_IRUSR, proc_tgid_io_accounting),
3200 #ifdef CONFIG_HARDWALL
3201 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3205 static int proc_tgid_base_readdir(struct file * filp,
3206 void * dirent, filldir_t filldir)
3208 return proc_pident_readdir(filp,dirent,filldir,
3209 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3212 static const struct file_operations proc_tgid_base_operations = {
3213 .read = generic_read_dir,
3214 .readdir = proc_tgid_base_readdir,
3215 .llseek = default_llseek,
3218 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3219 return proc_pident_lookup(dir, dentry,
3220 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3223 static const struct inode_operations proc_tgid_base_inode_operations = {
3224 .lookup = proc_tgid_base_lookup,
3225 .getattr = pid_getattr,
3226 .setattr = proc_setattr,
3229 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3231 struct dentry *dentry, *leader, *dir;
3232 char buf[PROC_NUMBUF];
3236 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3237 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3239 shrink_dcache_parent(dentry);
3245 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3246 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3251 name.len = strlen(name.name);
3252 dir = d_hash_and_lookup(leader, &name);
3254 goto out_put_leader;
3257 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3258 dentry = d_hash_and_lookup(dir, &name);
3260 shrink_dcache_parent(dentry);
3273 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3274 * @task: task that should be flushed.
3276 * When flushing dentries from proc, one needs to flush them from global
3277 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3278 * in. This call is supposed to do all of this job.
3280 * Looks in the dcache for
3282 * /proc/@tgid/task/@pid
3283 * if either directory is present flushes it and all of it'ts children
3286 * It is safe and reasonable to cache /proc entries for a task until
3287 * that task exits. After that they just clog up the dcache with
3288 * useless entries, possibly causing useful dcache entries to be
3289 * flushed instead. This routine is proved to flush those useless
3290 * dcache entries at process exit time.
3292 * NOTE: This routine is just an optimization so it does not guarantee
3293 * that no dcache entries will exist at process exit time it
3294 * just makes it very unlikely that any will persist.
3297 void proc_flush_task(struct task_struct *task)
3300 struct pid *pid, *tgid;
3303 pid = task_pid(task);
3304 tgid = task_tgid(task);
3306 for (i = 0; i <= pid->level; i++) {
3307 upid = &pid->numbers[i];
3308 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3309 tgid->numbers[i].nr);
3312 upid = &pid->numbers[pid->level];
3314 pid_ns_release_proc(upid->ns);
3317 static struct dentry *proc_pid_instantiate(struct inode *dir,
3318 struct dentry * dentry,
3319 struct task_struct *task, const void *ptr)
3321 struct dentry *error = ERR_PTR(-ENOENT);
3322 struct inode *inode;
3324 inode = proc_pid_make_inode(dir->i_sb, task);
3328 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3329 inode->i_op = &proc_tgid_base_inode_operations;
3330 inode->i_fop = &proc_tgid_base_operations;
3331 inode->i_flags|=S_IMMUTABLE;
3333 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3334 ARRAY_SIZE(tgid_base_stuff)));
3336 d_set_d_op(dentry, &pid_dentry_operations);
3338 d_add(dentry, inode);
3339 /* Close the race of the process dying before we return the dentry */
3340 if (pid_revalidate(dentry, NULL))
3346 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3348 struct dentry *result;
3349 struct task_struct *task;
3351 struct pid_namespace *ns;
3353 result = proc_base_lookup(dir, dentry);
3354 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3357 tgid = name_to_int(dentry);
3361 ns = dentry->d_sb->s_fs_info;
3363 task = find_task_by_pid_ns(tgid, ns);
3365 get_task_struct(task);
3370 result = proc_pid_instantiate(dir, dentry, task, NULL);
3371 put_task_struct(task);
3377 * Find the first task with tgid >= tgid
3382 struct task_struct *task;
3384 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3389 put_task_struct(iter.task);
3393 pid = find_ge_pid(iter.tgid, ns);
3395 iter.tgid = pid_nr_ns(pid, ns);
3396 iter.task = pid_task(pid, PIDTYPE_PID);
3397 /* What we to know is if the pid we have find is the
3398 * pid of a thread_group_leader. Testing for task
3399 * being a thread_group_leader is the obvious thing
3400 * todo but there is a window when it fails, due to
3401 * the pid transfer logic in de_thread.
3403 * So we perform the straight forward test of seeing
3404 * if the pid we have found is the pid of a thread
3405 * group leader, and don't worry if the task we have
3406 * found doesn't happen to be a thread group leader.
3407 * As we don't care in the case of readdir.
3409 if (!iter.task || !has_group_leader_pid(iter.task)) {
3413 get_task_struct(iter.task);
3419 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3421 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3422 struct tgid_iter iter)
3424 char name[PROC_NUMBUF];
3425 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3426 return proc_fill_cache(filp, dirent, filldir, name, len,
3427 proc_pid_instantiate, iter.task, NULL);
3430 /* for the /proc/ directory itself, after non-process stuff has been done */
3431 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3434 struct task_struct *reaper;
3435 struct tgid_iter iter;
3436 struct pid_namespace *ns;
3438 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3440 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3442 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3446 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3447 const struct pid_entry *p = &proc_base_stuff[nr];
3448 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3452 ns = filp->f_dentry->d_sb->s_fs_info;
3454 iter.tgid = filp->f_pos - TGID_OFFSET;
3455 for (iter = next_tgid(ns, iter);
3457 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3458 filp->f_pos = iter.tgid + TGID_OFFSET;
3459 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3460 put_task_struct(iter.task);
3464 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3466 put_task_struct(reaper);
3474 static const struct pid_entry tid_base_stuff[] = {
3475 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3476 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3477 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3478 REG("environ", S_IRUSR, proc_environ_operations),
3479 INF("auxv", S_IRUSR, proc_pid_auxv),
3480 ONE("status", S_IRUGO, proc_pid_status),
3481 ONE("personality", S_IRUGO, proc_pid_personality),
3482 INF("limits", S_IRUGO, proc_pid_limits),
3483 #ifdef CONFIG_SCHED_DEBUG
3484 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3486 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3487 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3488 INF("syscall", S_IRUGO, proc_pid_syscall),
3490 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3491 ONE("stat", S_IRUGO, proc_tid_stat),
3492 ONE("statm", S_IRUGO, proc_pid_statm),
3493 REG("maps", S_IRUGO, proc_maps_operations),
3495 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3497 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3498 LNK("cwd", proc_cwd_link),
3499 LNK("root", proc_root_link),
3500 LNK("exe", proc_exe_link),
3501 REG("mounts", S_IRUGO, proc_mounts_operations),
3502 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3503 #ifdef CONFIG_PROC_PAGE_MONITOR
3504 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3505 REG("smaps", S_IRUGO, proc_smaps_operations),
3506 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3508 #ifdef CONFIG_SECURITY
3509 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3511 #ifdef CONFIG_KALLSYMS
3512 INF("wchan", S_IRUGO, proc_pid_wchan),
3514 #ifdef CONFIG_STACKTRACE
3515 ONE("stack", S_IRUGO, proc_pid_stack),
3517 #ifdef CONFIG_SCHEDSTATS
3518 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3520 #ifdef CONFIG_LATENCYTOP
3521 REG("latency", S_IRUGO, proc_lstats_operations),
3523 #ifdef CONFIG_PROC_PID_CPUSET
3524 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3526 #ifdef CONFIG_CGROUPS
3527 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3529 INF("oom_score", S_IRUGO, proc_oom_score),
3530 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3531 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3532 #ifdef CONFIG_AUDITSYSCALL
3533 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3534 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3536 #ifdef CONFIG_FAULT_INJECTION
3537 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3539 #ifdef CONFIG_TASK_IO_ACCOUNTING
3540 INF("io", S_IRUSR, proc_tid_io_accounting),
3542 #ifdef CONFIG_HARDWALL
3543 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3547 static int proc_tid_base_readdir(struct file * filp,
3548 void * dirent, filldir_t filldir)
3550 return proc_pident_readdir(filp,dirent,filldir,
3551 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3554 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3555 return proc_pident_lookup(dir, dentry,
3556 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3559 static const struct file_operations proc_tid_base_operations = {
3560 .read = generic_read_dir,
3561 .readdir = proc_tid_base_readdir,
3562 .llseek = default_llseek,
3565 static const struct inode_operations proc_tid_base_inode_operations = {
3566 .lookup = proc_tid_base_lookup,
3567 .getattr = pid_getattr,
3568 .setattr = proc_setattr,
3571 static struct dentry *proc_task_instantiate(struct inode *dir,
3572 struct dentry *dentry, struct task_struct *task, const void *ptr)
3574 struct dentry *error = ERR_PTR(-ENOENT);
3575 struct inode *inode;
3576 inode = proc_pid_make_inode(dir->i_sb, task);
3580 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3581 inode->i_op = &proc_tid_base_inode_operations;
3582 inode->i_fop = &proc_tid_base_operations;
3583 inode->i_flags|=S_IMMUTABLE;
3585 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3586 ARRAY_SIZE(tid_base_stuff)));
3588 d_set_d_op(dentry, &pid_dentry_operations);
3590 d_add(dentry, inode);
3591 /* Close the race of the process dying before we return the dentry */
3592 if (pid_revalidate(dentry, NULL))
3598 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3600 struct dentry *result = ERR_PTR(-ENOENT);
3601 struct task_struct *task;
3602 struct task_struct *leader = get_proc_task(dir);
3604 struct pid_namespace *ns;
3609 tid = name_to_int(dentry);
3613 ns = dentry->d_sb->s_fs_info;
3615 task = find_task_by_pid_ns(tid, ns);
3617 get_task_struct(task);
3621 if (!same_thread_group(leader, task))
3624 result = proc_task_instantiate(dir, dentry, task, NULL);
3626 put_task_struct(task);
3628 put_task_struct(leader);
3634 * Find the first tid of a thread group to return to user space.
3636 * Usually this is just the thread group leader, but if the users
3637 * buffer was too small or there was a seek into the middle of the
3638 * directory we have more work todo.
3640 * In the case of a short read we start with find_task_by_pid.
3642 * In the case of a seek we start with the leader and walk nr
3645 static struct task_struct *first_tid(struct task_struct *leader,
3646 int tid, int nr, struct pid_namespace *ns)
3648 struct task_struct *pos;
3651 /* Attempt to start with the pid of a thread */
3652 if (tid && (nr > 0)) {
3653 pos = find_task_by_pid_ns(tid, ns);
3654 if (pos && (pos->group_leader == leader))
3658 /* If nr exceeds the number of threads there is nothing todo */
3660 if (nr && nr >= get_nr_threads(leader))
3663 /* If we haven't found our starting place yet start
3664 * with the leader and walk nr threads forward.
3666 for (pos = leader; nr > 0; --nr) {
3667 pos = next_thread(pos);
3668 if (pos == leader) {
3674 get_task_struct(pos);
3681 * Find the next thread in the thread list.
3682 * Return NULL if there is an error or no next thread.
3684 * The reference to the input task_struct is released.
3686 static struct task_struct *next_tid(struct task_struct *start)
3688 struct task_struct *pos = NULL;
3690 if (pid_alive(start)) {
3691 pos = next_thread(start);
3692 if (thread_group_leader(pos))
3695 get_task_struct(pos);
3698 put_task_struct(start);
3702 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3703 struct task_struct *task, int tid)
3705 char name[PROC_NUMBUF];
3706 int len = snprintf(name, sizeof(name), "%d", tid);
3707 return proc_fill_cache(filp, dirent, filldir, name, len,
3708 proc_task_instantiate, task, NULL);
3711 /* for the /proc/TGID/task/ directories */
3712 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3714 struct dentry *dentry = filp->f_path.dentry;
3715 struct inode *inode = dentry->d_inode;
3716 struct task_struct *leader = NULL;
3717 struct task_struct *task;
3718 int retval = -ENOENT;
3721 struct pid_namespace *ns;
3723 task = get_proc_task(inode);
3727 if (pid_alive(task)) {
3728 leader = task->group_leader;
3729 get_task_struct(leader);
3732 put_task_struct(task);
3737 switch ((unsigned long)filp->f_pos) {
3740 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3745 ino = parent_ino(dentry);
3746 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3752 /* f_version caches the tgid value that the last readdir call couldn't
3753 * return. lseek aka telldir automagically resets f_version to 0.
3755 ns = filp->f_dentry->d_sb->s_fs_info;
3756 tid = (int)filp->f_version;
3757 filp->f_version = 0;
3758 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3760 task = next_tid(task), filp->f_pos++) {
3761 tid = task_pid_nr_ns(task, ns);
3762 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3763 /* returning this tgid failed, save it as the first
3764 * pid for the next readir call */
3765 filp->f_version = (u64)tid;
3766 put_task_struct(task);
3771 put_task_struct(leader);
3776 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3778 struct inode *inode = dentry->d_inode;
3779 struct task_struct *p = get_proc_task(inode);
3780 generic_fillattr(inode, stat);
3783 stat->nlink += get_nr_threads(p);
3790 static const struct inode_operations proc_task_inode_operations = {
3791 .lookup = proc_task_lookup,
3792 .getattr = proc_task_getattr,
3793 .setattr = proc_setattr,
3796 static const struct file_operations proc_task_operations = {
3797 .read = generic_read_dir,
3798 .readdir = proc_task_readdir,
3799 .llseek = default_llseek,