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 *mm_access(struct task_struct *task, unsigned int mode)
202 struct mm_struct *mm;
205 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
209 mm = get_task_mm(task);
210 if (mm && mm != current->mm &&
211 !ptrace_may_access(task, mode)) {
213 mm = ERR_PTR(-EACCES);
215 mutex_unlock(&task->signal->cred_guard_mutex);
220 struct mm_struct *mm_for_maps(struct task_struct *task)
222 return mm_access(task, PTRACE_MODE_READ_FSCREDS);
225 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
229 struct mm_struct *mm = get_task_mm(task);
233 goto out_mm; /* Shh! No looking before we're done */
235 len = mm->arg_end - mm->arg_start;
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
262 static int proc_pid_auxv(struct task_struct *task, char *buffer)
264 struct mm_struct *mm = mm_for_maps(task);
265 int res = PTR_ERR(mm);
266 if (mm && !IS_ERR(mm)) {
267 unsigned int nwords = 0;
270 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
274 memcpy(buffer, mm->saved_auxv, res);
281 #ifdef CONFIG_KALLSYMS
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
286 static int proc_pid_wchan(struct task_struct *task, char *buffer)
289 char symname[KSYM_NAME_LEN];
291 wchan = get_wchan(task);
293 if (lookup_symbol_name(wchan, symname) < 0)
294 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
297 return sprintf(buffer, "%lu", wchan);
299 return sprintf(buffer, "%s", symname);
301 #endif /* CONFIG_KALLSYMS */
303 static int lock_trace(struct task_struct *task)
305 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
308 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH_FSCREDS)) {
309 mutex_unlock(&task->signal->cred_guard_mutex);
315 static void unlock_trace(struct task_struct *task)
317 mutex_unlock(&task->signal->cred_guard_mutex);
320 #ifdef CONFIG_STACKTRACE
322 #define MAX_STACK_TRACE_DEPTH 64
324 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
325 struct pid *pid, struct task_struct *task)
327 struct stack_trace trace;
328 unsigned long *entries;
332 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
336 trace.nr_entries = 0;
337 trace.max_entries = MAX_STACK_TRACE_DEPTH;
338 trace.entries = entries;
341 err = lock_trace(task);
343 save_stack_trace_tsk(task, &trace);
345 for (i = 0; i < trace.nr_entries; i++) {
346 seq_printf(m, "[<%pK>] %pS\n",
347 (void *)entries[i], (void *)entries[i]);
357 #ifdef CONFIG_SCHEDSTATS
359 * Provides /proc/PID/schedstat
361 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
363 return sprintf(buffer, "%llu %llu %lu\n",
364 (unsigned long long)task->se.sum_exec_runtime,
365 (unsigned long long)task->sched_info.run_delay,
366 task->sched_info.pcount);
370 #ifdef CONFIG_LATENCYTOP
371 static int lstats_show_proc(struct seq_file *m, void *v)
374 struct inode *inode = m->private;
375 struct task_struct *task = get_proc_task(inode);
379 seq_puts(m, "Latency Top version : v0.1\n");
380 for (i = 0; i < 32; i++) {
381 struct latency_record *lr = &task->latency_record[i];
382 if (lr->backtrace[0]) {
384 seq_printf(m, "%i %li %li",
385 lr->count, lr->time, lr->max);
386 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
387 unsigned long bt = lr->backtrace[q];
392 seq_printf(m, " %ps", (void *)bt);
398 put_task_struct(task);
402 static int lstats_open(struct inode *inode, struct file *file)
404 return single_open(file, lstats_show_proc, inode);
407 static ssize_t lstats_write(struct file *file, const char __user *buf,
408 size_t count, loff_t *offs)
410 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
414 clear_all_latency_tracing(task);
415 put_task_struct(task);
420 static const struct file_operations proc_lstats_operations = {
423 .write = lstats_write,
425 .release = single_release,
430 static int proc_oom_score(struct task_struct *task, char *buffer)
432 unsigned long points = 0;
434 read_lock(&tasklist_lock);
436 points = oom_badness(task, NULL, NULL,
437 totalram_pages + total_swap_pages);
438 read_unlock(&tasklist_lock);
439 return sprintf(buffer, "%lu\n", points);
447 static const struct limit_names lnames[RLIM_NLIMITS] = {
448 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
449 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
450 [RLIMIT_DATA] = {"Max data size", "bytes"},
451 [RLIMIT_STACK] = {"Max stack size", "bytes"},
452 [RLIMIT_CORE] = {"Max core file size", "bytes"},
453 [RLIMIT_RSS] = {"Max resident set", "bytes"},
454 [RLIMIT_NPROC] = {"Max processes", "processes"},
455 [RLIMIT_NOFILE] = {"Max open files", "files"},
456 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
457 [RLIMIT_AS] = {"Max address space", "bytes"},
458 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
459 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
460 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
461 [RLIMIT_NICE] = {"Max nice priority", NULL},
462 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
463 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
466 /* Display limits for a process */
467 static int proc_pid_limits(struct task_struct *task, char *buffer)
472 char *bufptr = buffer;
474 struct rlimit rlim[RLIM_NLIMITS];
476 if (!lock_task_sighand(task, &flags))
478 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
479 unlock_task_sighand(task, &flags);
482 * print the file header
484 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
485 "Limit", "Soft Limit", "Hard Limit", "Units");
487 for (i = 0; i < RLIM_NLIMITS; i++) {
488 if (rlim[i].rlim_cur == RLIM_INFINITY)
489 count += sprintf(&bufptr[count], "%-25s %-20s ",
490 lnames[i].name, "unlimited");
492 count += sprintf(&bufptr[count], "%-25s %-20lu ",
493 lnames[i].name, rlim[i].rlim_cur);
495 if (rlim[i].rlim_max == RLIM_INFINITY)
496 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498 count += sprintf(&bufptr[count], "%-20lu ",
502 count += sprintf(&bufptr[count], "%-10s\n",
505 count += sprintf(&bufptr[count], "\n");
511 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
512 static int proc_pid_syscall(struct task_struct *task, char *buffer)
515 unsigned long args[6], sp, pc;
516 int res = lock_trace(task);
520 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
521 res = sprintf(buffer, "running\n");
523 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
525 res = sprintf(buffer,
526 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
528 args[0], args[1], args[2], args[3], args[4], args[5],
533 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
535 /************************************************************************/
536 /* Here the fs part begins */
537 /************************************************************************/
539 /* permission checks */
540 static int proc_fd_access_allowed(struct inode *inode)
542 struct task_struct *task;
544 /* Allow access to a task's file descriptors if it is us or we
545 * may use ptrace attach to the process and find out that
548 task = get_proc_task(inode);
550 allowed = ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS);
551 put_task_struct(task);
556 int proc_setattr(struct dentry *dentry, struct iattr *attr)
559 struct inode *inode = dentry->d_inode;
561 if (attr->ia_valid & ATTR_MODE)
564 error = setattr_prepare(dentry, attr);
568 if ((attr->ia_valid & ATTR_SIZE) &&
569 attr->ia_size != i_size_read(inode)) {
570 error = vmtruncate(inode, attr->ia_size);
575 setattr_copy(inode, attr);
576 mark_inode_dirty(inode);
580 static const struct inode_operations proc_def_inode_operations = {
581 .setattr = proc_setattr,
584 static int mounts_open_common(struct inode *inode, struct file *file,
585 const struct seq_operations *op)
587 struct task_struct *task = get_proc_task(inode);
589 struct mnt_namespace *ns = NULL;
591 struct proc_mounts *p;
596 nsp = task_nsproxy(task);
603 if (ns && get_task_root(task, &root) == 0)
605 put_task_struct(task);
614 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
618 file->private_data = &p->m;
619 ret = seq_open(file, op);
626 p->m.poll_event = ns->event;
640 static int mounts_release(struct inode *inode, struct file *file)
642 struct proc_mounts *p = file->private_data;
645 return seq_release(inode, file);
648 static unsigned mounts_poll(struct file *file, poll_table *wait)
650 struct proc_mounts *p = file->private_data;
651 unsigned res = POLLIN | POLLRDNORM;
653 poll_wait(file, &p->ns->poll, wait);
654 if (mnt_had_events(p))
655 res |= POLLERR | POLLPRI;
660 static int mounts_open(struct inode *inode, struct file *file)
662 return mounts_open_common(inode, file, &mounts_op);
665 static const struct file_operations proc_mounts_operations = {
669 .release = mounts_release,
673 static int mountinfo_open(struct inode *inode, struct file *file)
675 return mounts_open_common(inode, file, &mountinfo_op);
678 static const struct file_operations proc_mountinfo_operations = {
679 .open = mountinfo_open,
682 .release = mounts_release,
686 static int mountstats_open(struct inode *inode, struct file *file)
688 return mounts_open_common(inode, file, &mountstats_op);
691 static const struct file_operations proc_mountstats_operations = {
692 .open = mountstats_open,
695 .release = mounts_release,
698 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
700 static ssize_t proc_info_read(struct file * file, char __user * buf,
701 size_t count, loff_t *ppos)
703 struct inode * inode = file->f_path.dentry->d_inode;
706 struct task_struct *task = get_proc_task(inode);
712 if (count > PROC_BLOCK_SIZE)
713 count = PROC_BLOCK_SIZE;
716 if (!(page = __get_free_page(GFP_TEMPORARY)))
719 length = PROC_I(inode)->op.proc_read(task, (char*)page);
722 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
725 put_task_struct(task);
730 static const struct file_operations proc_info_file_operations = {
731 .read = proc_info_read,
732 .llseek = generic_file_llseek,
735 static int proc_single_show(struct seq_file *m, void *v)
737 struct inode *inode = m->private;
738 struct pid_namespace *ns;
740 struct task_struct *task;
743 ns = inode->i_sb->s_fs_info;
744 pid = proc_pid(inode);
745 task = get_pid_task(pid, PIDTYPE_PID);
749 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
751 put_task_struct(task);
755 static int proc_single_open(struct inode *inode, struct file *filp)
757 return single_open(filp, proc_single_show, inode);
760 static const struct file_operations proc_single_file_operations = {
761 .open = proc_single_open,
764 .release = single_release,
767 static int mem_open(struct inode* inode, struct file* file)
769 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
770 struct mm_struct *mm;
775 mm = mm_access(task, PTRACE_MODE_ATTACH | PTRACE_MODE_FSCREDS);
776 put_task_struct(task);
782 /* ensure this mm_struct can't be freed */
783 atomic_inc(&mm->mm_count);
784 /* but do not pin its memory */
788 /* OK to pass negative loff_t, we can catch out-of-range */
789 file->f_mode |= FMODE_UNSIGNED_OFFSET;
790 file->private_data = mm;
795 static ssize_t mem_rw(struct file *file, char __user *buf,
796 size_t count, loff_t *ppos, int write)
798 struct mm_struct *mm = file->private_data;
799 unsigned long addr = *ppos;
806 page = (char *)__get_free_page(GFP_TEMPORARY);
811 if (!atomic_inc_not_zero(&mm->mm_users))
815 int this_len = min_t(int, count, PAGE_SIZE);
817 if (write && copy_from_user(page, buf, this_len)) {
822 this_len = access_remote_vm(mm, addr, page, this_len, write);
829 if (!write && copy_to_user(buf, page, this_len)) {
843 free_page((unsigned long) page);
847 static ssize_t mem_read(struct file *file, char __user *buf,
848 size_t count, loff_t *ppos)
850 return mem_rw(file, buf, count, ppos, 0);
853 static ssize_t mem_write(struct file *file, const char __user *buf,
854 size_t count, loff_t *ppos)
856 return mem_rw(file, (char __user*)buf, count, ppos, 1);
859 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
863 file->f_pos = offset;
866 file->f_pos += offset;
871 force_successful_syscall_return();
875 static int mem_release(struct inode *inode, struct file *file)
877 struct mm_struct *mm = file->private_data;
883 static const struct file_operations proc_mem_operations = {
888 .release = mem_release,
891 static ssize_t environ_read(struct file *file, char __user *buf,
892 size_t count, loff_t *ppos)
894 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
896 unsigned long src = *ppos;
898 struct mm_struct *mm;
904 page = (char *)__get_free_page(GFP_TEMPORARY);
909 mm = mm_for_maps(task);
911 /* Ensure the process spawned far enough to have an environment. */
912 if (!mm || IS_ERR(mm) || !mm->env_end)
917 int this_len, retval, max_len;
919 this_len = mm->env_end - (mm->env_start + src);
924 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
925 this_len = (this_len > max_len) ? max_len : this_len;
927 retval = access_process_vm(task, (mm->env_start + src),
935 if (copy_to_user(buf, page, retval)) {
949 free_page((unsigned long) page);
951 put_task_struct(task);
956 static const struct file_operations proc_environ_operations = {
957 .read = environ_read,
958 .llseek = generic_file_llseek,
961 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
962 size_t count, loff_t *ppos)
964 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
965 char buffer[PROC_NUMBUF];
967 int oom_adjust = OOM_DISABLE;
973 if (lock_task_sighand(task, &flags)) {
974 oom_adjust = task->signal->oom_adj;
975 unlock_task_sighand(task, &flags);
978 put_task_struct(task);
980 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
982 return simple_read_from_buffer(buf, count, ppos, buffer, len);
985 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
986 size_t count, loff_t *ppos)
988 struct task_struct *task;
989 char buffer[PROC_NUMBUF];
994 memset(buffer, 0, sizeof(buffer));
995 if (count > sizeof(buffer) - 1)
996 count = sizeof(buffer) - 1;
997 if (copy_from_user(buffer, buf, count)) {
1002 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1005 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1006 oom_adjust != OOM_DISABLE) {
1011 task = get_proc_task(file->f_path.dentry->d_inode);
1023 if (!lock_task_sighand(task, &flags)) {
1028 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1034 * Warn that /proc/pid/oom_adj is deprecated, see
1035 * Documentation/feature-removal-schedule.txt.
1037 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1038 current->comm, task_pid_nr(current), task_pid_nr(task),
1040 task->signal->oom_adj = oom_adjust;
1042 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1043 * value is always attainable.
1045 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1046 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1048 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1051 unlock_task_sighand(task, &flags);
1054 put_task_struct(task);
1056 return err < 0 ? err : count;
1059 static const struct file_operations proc_oom_adjust_operations = {
1060 .read = oom_adjust_read,
1061 .write = oom_adjust_write,
1062 .llseek = generic_file_llseek,
1065 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1066 size_t count, loff_t *ppos)
1068 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1069 char buffer[PROC_NUMBUF];
1070 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1071 unsigned long flags;
1076 if (lock_task_sighand(task, &flags)) {
1077 oom_score_adj = task->signal->oom_score_adj;
1078 unlock_task_sighand(task, &flags);
1080 put_task_struct(task);
1081 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1082 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1085 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1086 size_t count, loff_t *ppos)
1088 struct task_struct *task;
1089 char buffer[PROC_NUMBUF];
1090 unsigned long flags;
1094 memset(buffer, 0, sizeof(buffer));
1095 if (count > sizeof(buffer) - 1)
1096 count = sizeof(buffer) - 1;
1097 if (copy_from_user(buffer, buf, count)) {
1102 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1105 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1106 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1111 task = get_proc_task(file->f_path.dentry->d_inode);
1123 if (!lock_task_sighand(task, &flags)) {
1128 if (oom_score_adj < task->signal->oom_score_adj_min &&
1129 !capable(CAP_SYS_RESOURCE)) {
1134 task->signal->oom_score_adj = oom_score_adj;
1135 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1136 task->signal->oom_score_adj_min = oom_score_adj;
1138 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1139 * always attainable.
1141 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1142 task->signal->oom_adj = OOM_DISABLE;
1144 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1147 unlock_task_sighand(task, &flags);
1150 put_task_struct(task);
1152 return err < 0 ? err : count;
1155 static const struct file_operations proc_oom_score_adj_operations = {
1156 .read = oom_score_adj_read,
1157 .write = oom_score_adj_write,
1158 .llseek = default_llseek,
1161 #ifdef CONFIG_AUDITSYSCALL
1162 #define TMPBUFLEN 21
1163 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1164 size_t count, loff_t *ppos)
1166 struct inode * inode = file->f_path.dentry->d_inode;
1167 struct task_struct *task = get_proc_task(inode);
1169 char tmpbuf[TMPBUFLEN];
1173 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1174 audit_get_loginuid(task));
1175 put_task_struct(task);
1176 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1179 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1180 size_t count, loff_t *ppos)
1182 struct inode * inode = file->f_path.dentry->d_inode;
1187 if (!capable(CAP_AUDIT_CONTROL))
1191 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1197 if (count >= PAGE_SIZE)
1198 count = PAGE_SIZE - 1;
1201 /* No partial writes. */
1204 page = (char*)__get_free_page(GFP_TEMPORARY);
1208 if (copy_from_user(page, buf, count))
1212 loginuid = simple_strtoul(page, &tmp, 10);
1218 length = audit_set_loginuid(current, loginuid);
1219 if (likely(length == 0))
1223 free_page((unsigned long) page);
1227 static const struct file_operations proc_loginuid_operations = {
1228 .read = proc_loginuid_read,
1229 .write = proc_loginuid_write,
1230 .llseek = generic_file_llseek,
1233 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1234 size_t count, loff_t *ppos)
1236 struct inode * inode = file->f_path.dentry->d_inode;
1237 struct task_struct *task = get_proc_task(inode);
1239 char tmpbuf[TMPBUFLEN];
1243 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1244 audit_get_sessionid(task));
1245 put_task_struct(task);
1246 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1249 static const struct file_operations proc_sessionid_operations = {
1250 .read = proc_sessionid_read,
1251 .llseek = generic_file_llseek,
1255 #ifdef CONFIG_FAULT_INJECTION
1256 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1257 size_t count, loff_t *ppos)
1259 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1260 char buffer[PROC_NUMBUF];
1266 make_it_fail = task->make_it_fail;
1267 put_task_struct(task);
1269 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1271 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1274 static ssize_t proc_fault_inject_write(struct file * file,
1275 const char __user * buf, size_t count, loff_t *ppos)
1277 struct task_struct *task;
1278 char buffer[PROC_NUMBUF], *end;
1281 if (!capable(CAP_SYS_RESOURCE))
1283 memset(buffer, 0, sizeof(buffer));
1284 if (count > sizeof(buffer) - 1)
1285 count = sizeof(buffer) - 1;
1286 if (copy_from_user(buffer, buf, count))
1288 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1291 task = get_proc_task(file->f_dentry->d_inode);
1294 task->make_it_fail = make_it_fail;
1295 put_task_struct(task);
1300 static const struct file_operations proc_fault_inject_operations = {
1301 .read = proc_fault_inject_read,
1302 .write = proc_fault_inject_write,
1303 .llseek = generic_file_llseek,
1308 #ifdef CONFIG_SCHED_DEBUG
1310 * Print out various scheduling related per-task fields:
1312 static int sched_show(struct seq_file *m, void *v)
1314 struct inode *inode = m->private;
1315 struct task_struct *p;
1317 p = get_proc_task(inode);
1320 proc_sched_show_task(p, m);
1328 sched_write(struct file *file, const char __user *buf,
1329 size_t count, loff_t *offset)
1331 struct inode *inode = file->f_path.dentry->d_inode;
1332 struct task_struct *p;
1334 p = get_proc_task(inode);
1337 proc_sched_set_task(p);
1344 static int sched_open(struct inode *inode, struct file *filp)
1346 return single_open(filp, sched_show, inode);
1349 static const struct file_operations proc_pid_sched_operations = {
1352 .write = sched_write,
1353 .llseek = seq_lseek,
1354 .release = single_release,
1359 #ifdef CONFIG_SCHED_AUTOGROUP
1361 * Print out autogroup related information:
1363 static int sched_autogroup_show(struct seq_file *m, void *v)
1365 struct inode *inode = m->private;
1366 struct task_struct *p;
1368 p = get_proc_task(inode);
1371 proc_sched_autogroup_show_task(p, m);
1379 sched_autogroup_write(struct file *file, const char __user *buf,
1380 size_t count, loff_t *offset)
1382 struct inode *inode = file->f_path.dentry->d_inode;
1383 struct task_struct *p;
1384 char buffer[PROC_NUMBUF];
1388 memset(buffer, 0, sizeof(buffer));
1389 if (count > sizeof(buffer) - 1)
1390 count = sizeof(buffer) - 1;
1391 if (copy_from_user(buffer, buf, count))
1394 err = kstrtoint(strstrip(buffer), 0, &nice);
1398 p = get_proc_task(inode);
1403 err = proc_sched_autogroup_set_nice(p, &err);
1412 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1416 ret = single_open(filp, sched_autogroup_show, NULL);
1418 struct seq_file *m = filp->private_data;
1425 static const struct file_operations proc_pid_sched_autogroup_operations = {
1426 .open = sched_autogroup_open,
1428 .write = sched_autogroup_write,
1429 .llseek = seq_lseek,
1430 .release = single_release,
1433 #endif /* CONFIG_SCHED_AUTOGROUP */
1435 static ssize_t comm_write(struct file *file, const char __user *buf,
1436 size_t count, loff_t *offset)
1438 struct inode *inode = file->f_path.dentry->d_inode;
1439 struct task_struct *p;
1440 char buffer[TASK_COMM_LEN];
1442 memset(buffer, 0, sizeof(buffer));
1443 if (count > sizeof(buffer) - 1)
1444 count = sizeof(buffer) - 1;
1445 if (copy_from_user(buffer, buf, count))
1448 p = get_proc_task(inode);
1452 if (same_thread_group(current, p))
1453 set_task_comm(p, buffer);
1462 static int comm_show(struct seq_file *m, void *v)
1464 struct inode *inode = m->private;
1465 struct task_struct *p;
1467 p = get_proc_task(inode);
1472 seq_printf(m, "%s\n", p->comm);
1480 static int comm_open(struct inode *inode, struct file *filp)
1482 return single_open(filp, comm_show, inode);
1485 static const struct file_operations proc_pid_set_comm_operations = {
1488 .write = comm_write,
1489 .llseek = seq_lseek,
1490 .release = single_release,
1493 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1495 struct task_struct *task;
1496 struct mm_struct *mm;
1497 struct file *exe_file;
1499 task = get_proc_task(dentry->d_inode);
1502 mm = get_task_mm(task);
1503 put_task_struct(task);
1506 exe_file = get_mm_exe_file(mm);
1509 *exe_path = exe_file->f_path;
1510 path_get(&exe_file->f_path);
1517 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1519 struct inode *inode = dentry->d_inode;
1520 int error = -EACCES;
1522 /* We don't need a base pointer in the /proc filesystem */
1523 path_put(&nd->path);
1525 /* Are we allowed to snoop on the tasks file descriptors? */
1526 if (!proc_fd_access_allowed(inode))
1529 error = PROC_I(inode)->op.proc_get_link(dentry, &nd->path);
1531 return ERR_PTR(error);
1534 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1536 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1543 pathname = d_path(path, tmp, PAGE_SIZE);
1544 len = PTR_ERR(pathname);
1545 if (IS_ERR(pathname))
1547 len = tmp + PAGE_SIZE - 1 - pathname;
1551 if (copy_to_user(buffer, pathname, len))
1554 free_page((unsigned long)tmp);
1558 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1560 int error = -EACCES;
1561 struct inode *inode = dentry->d_inode;
1564 /* Are we allowed to snoop on the tasks file descriptors? */
1565 if (!proc_fd_access_allowed(inode))
1568 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1572 error = do_proc_readlink(&path, buffer, buflen);
1578 static const struct inode_operations proc_pid_link_inode_operations = {
1579 .readlink = proc_pid_readlink,
1580 .follow_link = proc_pid_follow_link,
1581 .setattr = proc_setattr,
1585 /* building an inode */
1587 static int task_dumpable(struct task_struct *task)
1590 struct mm_struct *mm;
1595 dumpable = get_dumpable(mm);
1602 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1604 struct inode * inode;
1605 struct proc_inode *ei;
1606 const struct cred *cred;
1608 /* We need a new inode */
1610 inode = new_inode(sb);
1616 inode->i_ino = get_next_ino();
1617 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1618 inode->i_op = &proc_def_inode_operations;
1621 * grab the reference to task.
1623 ei->pid = get_task_pid(task, PIDTYPE_PID);
1627 if (task_dumpable(task)) {
1629 cred = __task_cred(task);
1630 inode->i_uid = cred->euid;
1631 inode->i_gid = cred->egid;
1634 security_task_to_inode(task, inode);
1644 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1646 struct inode *inode = dentry->d_inode;
1647 struct task_struct *task;
1648 const struct cred *cred;
1650 generic_fillattr(inode, stat);
1655 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1657 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1658 task_dumpable(task)) {
1659 cred = __task_cred(task);
1660 stat->uid = cred->euid;
1661 stat->gid = cred->egid;
1671 * Exceptional case: normally we are not allowed to unhash a busy
1672 * directory. In this case, however, we can do it - no aliasing problems
1673 * due to the way we treat inodes.
1675 * Rewrite the inode's ownerships here because the owning task may have
1676 * performed a setuid(), etc.
1678 * Before the /proc/pid/status file was created the only way to read
1679 * the effective uid of a /process was to stat /proc/pid. Reading
1680 * /proc/pid/status is slow enough that procps and other packages
1681 * kept stating /proc/pid. To keep the rules in /proc simple I have
1682 * made this apply to all per process world readable and executable
1685 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1687 struct inode *inode;
1688 struct task_struct *task;
1689 const struct cred *cred;
1691 if (nd && nd->flags & LOOKUP_RCU)
1694 inode = dentry->d_inode;
1695 task = get_proc_task(inode);
1698 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1699 task_dumpable(task)) {
1701 cred = __task_cred(task);
1702 inode->i_uid = cred->euid;
1703 inode->i_gid = cred->egid;
1709 inode->i_mode &= ~(S_ISUID | S_ISGID);
1710 security_task_to_inode(task, inode);
1711 put_task_struct(task);
1718 static int pid_delete_dentry(const struct dentry * dentry)
1720 /* Is the task we represent dead?
1721 * If so, then don't put the dentry on the lru list,
1722 * kill it immediately.
1724 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1727 const struct dentry_operations pid_dentry_operations =
1729 .d_revalidate = pid_revalidate,
1730 .d_delete = pid_delete_dentry,
1736 * Fill a directory entry.
1738 * If possible create the dcache entry and derive our inode number and
1739 * file type from dcache entry.
1741 * Since all of the proc inode numbers are dynamically generated, the inode
1742 * numbers do not exist until the inode is cache. This means creating the
1743 * the dcache entry in readdir is necessary to keep the inode numbers
1744 * reported by readdir in sync with the inode numbers reported
1747 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1748 const char *name, int len,
1749 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1751 struct dentry *child, *dir = filp->f_path.dentry;
1752 struct inode *inode;
1755 unsigned type = DT_UNKNOWN;
1759 qname.hash = full_name_hash(name, len);
1761 child = d_lookup(dir, &qname);
1764 new = d_alloc(dir, &qname);
1766 child = instantiate(dir->d_inode, new, task, ptr);
1773 if (!child || IS_ERR(child) || !child->d_inode)
1774 goto end_instantiate;
1775 inode = child->d_inode;
1778 type = inode->i_mode >> 12;
1783 ino = find_inode_number(dir, &qname);
1786 return filldir(dirent, name, len, filp->f_pos, ino, type);
1789 static unsigned name_to_int(struct dentry *dentry)
1791 const char *name = dentry->d_name.name;
1792 int len = dentry->d_name.len;
1795 if (len > 1 && *name == '0')
1798 unsigned c = *name++ - '0';
1801 if (n >= (~0U-9)/10)
1811 #define PROC_FDINFO_MAX 64
1813 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1815 struct task_struct *task = get_proc_task(inode);
1816 struct files_struct *files = NULL;
1818 int fd = proc_fd(inode);
1821 files = get_files_struct(task);
1822 put_task_struct(task);
1826 * We are not taking a ref to the file structure, so we must
1829 spin_lock(&files->file_lock);
1830 file = fcheck_files(files, fd);
1832 unsigned int f_flags;
1833 struct fdtable *fdt;
1835 fdt = files_fdtable(files);
1836 f_flags = file->f_flags & ~O_CLOEXEC;
1837 if (close_on_exec(fd, fdt))
1838 f_flags |= O_CLOEXEC;
1841 *path = file->f_path;
1842 path_get(&file->f_path);
1845 snprintf(info, PROC_FDINFO_MAX,
1848 (long long) file->f_pos,
1850 spin_unlock(&files->file_lock);
1851 put_files_struct(files);
1854 spin_unlock(&files->file_lock);
1855 put_files_struct(files);
1860 static int proc_fd_link(struct dentry *dentry, struct path *path)
1862 return proc_fd_info(dentry->d_inode, path, NULL);
1865 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1867 struct inode *inode;
1868 struct task_struct *task;
1870 struct files_struct *files;
1871 const struct cred *cred;
1873 if (nd && nd->flags & LOOKUP_RCU)
1876 inode = dentry->d_inode;
1877 task = get_proc_task(inode);
1878 fd = proc_fd(inode);
1881 files = get_files_struct(task);
1884 if (fcheck_files(files, fd)) {
1886 put_files_struct(files);
1887 if (task_dumpable(task)) {
1889 cred = __task_cred(task);
1890 inode->i_uid = cred->euid;
1891 inode->i_gid = cred->egid;
1897 inode->i_mode &= ~(S_ISUID | S_ISGID);
1898 security_task_to_inode(task, inode);
1899 put_task_struct(task);
1903 put_files_struct(files);
1905 put_task_struct(task);
1911 static const struct dentry_operations tid_fd_dentry_operations =
1913 .d_revalidate = tid_fd_revalidate,
1914 .d_delete = pid_delete_dentry,
1917 static struct dentry *proc_fd_instantiate(struct inode *dir,
1918 struct dentry *dentry, struct task_struct *task, const void *ptr)
1920 unsigned fd = *(const unsigned *)ptr;
1922 struct files_struct *files;
1923 struct inode *inode;
1924 struct proc_inode *ei;
1925 struct dentry *error = ERR_PTR(-ENOENT);
1927 inode = proc_pid_make_inode(dir->i_sb, task);
1932 files = get_files_struct(task);
1935 inode->i_mode = S_IFLNK;
1938 * We are not taking a ref to the file structure, so we must
1941 spin_lock(&files->file_lock);
1942 file = fcheck_files(files, fd);
1945 if (file->f_mode & FMODE_READ)
1946 inode->i_mode |= S_IRUSR | S_IXUSR;
1947 if (file->f_mode & FMODE_WRITE)
1948 inode->i_mode |= S_IWUSR | S_IXUSR;
1949 spin_unlock(&files->file_lock);
1950 put_files_struct(files);
1952 inode->i_op = &proc_pid_link_inode_operations;
1954 ei->op.proc_get_link = proc_fd_link;
1955 d_set_d_op(dentry, &tid_fd_dentry_operations);
1956 d_add(dentry, inode);
1957 /* Close the race of the process dying before we return the dentry */
1958 if (tid_fd_revalidate(dentry, NULL))
1964 spin_unlock(&files->file_lock);
1965 put_files_struct(files);
1971 static struct dentry *proc_lookupfd_common(struct inode *dir,
1972 struct dentry *dentry,
1973 instantiate_t instantiate)
1975 struct task_struct *task = get_proc_task(dir);
1976 unsigned fd = name_to_int(dentry);
1977 struct dentry *result = ERR_PTR(-ENOENT);
1984 result = instantiate(dir, dentry, task, &fd);
1986 put_task_struct(task);
1991 static int proc_readfd_common(struct file * filp, void * dirent,
1992 filldir_t filldir, instantiate_t instantiate)
1994 struct dentry *dentry = filp->f_path.dentry;
1995 struct inode *inode = dentry->d_inode;
1996 struct task_struct *p = get_proc_task(inode);
1997 unsigned int fd, ino;
1999 struct files_struct * files;
2009 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2013 ino = parent_ino(dentry);
2014 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2018 files = get_files_struct(p);
2022 for (fd = filp->f_pos-2;
2023 fd < files_fdtable(files)->max_fds;
2024 fd++, filp->f_pos++) {
2025 char name[PROC_NUMBUF];
2028 if (!fcheck_files(files, fd))
2032 len = snprintf(name, sizeof(name), "%d", fd);
2033 if (proc_fill_cache(filp, dirent, filldir,
2034 name, len, instantiate,
2042 put_files_struct(files);
2050 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2051 struct nameidata *nd)
2053 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2056 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2058 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2061 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2062 size_t len, loff_t *ppos)
2064 char tmp[PROC_FDINFO_MAX];
2065 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2067 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2071 static const struct file_operations proc_fdinfo_file_operations = {
2072 .open = nonseekable_open,
2073 .read = proc_fdinfo_read,
2074 .llseek = no_llseek,
2077 static const struct file_operations proc_fd_operations = {
2078 .read = generic_read_dir,
2079 .readdir = proc_readfd,
2080 .llseek = default_llseek,
2083 #ifdef CONFIG_CHECKPOINT_RESTORE
2086 * dname_to_vma_addr - maps a dentry name into two unsigned longs
2087 * which represent vma start and end addresses.
2089 static int dname_to_vma_addr(struct dentry *dentry,
2090 unsigned long *start, unsigned long *end)
2092 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
2098 static int map_files_d_revalidate(struct dentry *dentry, struct nameidata *nd)
2100 unsigned long vm_start, vm_end;
2101 bool exact_vma_exists = false;
2102 struct mm_struct *mm = NULL;
2103 struct task_struct *task;
2104 const struct cred *cred;
2105 struct inode *inode;
2108 if (nd && nd->flags & LOOKUP_RCU)
2111 if (!capable(CAP_SYS_ADMIN)) {
2116 inode = dentry->d_inode;
2117 task = get_proc_task(inode);
2121 if (!ptrace_may_access(task, PTRACE_MODE_READ))
2124 mm = get_task_mm(task);
2128 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
2129 down_read(&mm->mmap_sem);
2130 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
2131 up_read(&mm->mmap_sem);
2136 if (exact_vma_exists) {
2137 if (task_dumpable(task)) {
2139 cred = __task_cred(task);
2140 inode->i_uid = cred->euid;
2141 inode->i_gid = cred->egid;
2147 security_task_to_inode(task, inode);
2152 put_task_struct(task);
2161 static const struct dentry_operations tid_map_files_dentry_operations = {
2162 .d_revalidate = map_files_d_revalidate,
2163 .d_delete = pid_delete_dentry,
2166 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
2168 unsigned long vm_start, vm_end;
2169 struct vm_area_struct *vma;
2170 struct task_struct *task;
2171 struct mm_struct *mm;
2175 task = get_proc_task(dentry->d_inode);
2179 mm = get_task_mm(task);
2180 put_task_struct(task);
2184 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
2188 down_read(&mm->mmap_sem);
2189 vma = find_exact_vma(mm, vm_start, vm_end);
2190 if (vma && vma->vm_file) {
2191 *path = vma->vm_file->f_path;
2195 up_read(&mm->mmap_sem);
2203 struct map_files_info {
2206 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2209 static struct dentry *
2210 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
2211 struct task_struct *task, const void *ptr)
2213 const struct file *file = ptr;
2214 struct proc_inode *ei;
2215 struct inode *inode;
2218 return ERR_PTR(-ENOENT);
2220 inode = proc_pid_make_inode(dir->i_sb, task);
2222 return ERR_PTR(-ENOENT);
2225 ei->op.proc_get_link = proc_map_files_get_link;
2227 inode->i_op = &proc_pid_link_inode_operations;
2229 inode->i_mode = S_IFLNK;
2231 if (file->f_mode & FMODE_READ)
2232 inode->i_mode |= S_IRUSR;
2233 if (file->f_mode & FMODE_WRITE)
2234 inode->i_mode |= S_IWUSR;
2236 d_set_d_op(dentry, &tid_map_files_dentry_operations);
2237 d_add(dentry, inode);
2242 static struct dentry *proc_map_files_lookup(struct inode *dir,
2243 struct dentry *dentry, struct nameidata *nd)
2245 unsigned long vm_start, vm_end;
2246 struct vm_area_struct *vma;
2247 struct task_struct *task;
2248 struct dentry *result;
2249 struct mm_struct *mm;
2251 result = ERR_PTR(-EACCES);
2252 if (!capable(CAP_SYS_ADMIN))
2255 result = ERR_PTR(-ENOENT);
2256 task = get_proc_task(dir);
2260 result = ERR_PTR(-EACCES);
2261 if (lock_trace(task))
2264 result = ERR_PTR(-ENOENT);
2265 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
2268 mm = get_task_mm(task);
2272 down_read(&mm->mmap_sem);
2273 vma = find_exact_vma(mm, vm_start, vm_end);
2277 result = proc_map_files_instantiate(dir, dentry, task, vma->vm_file);
2280 up_read(&mm->mmap_sem);
2285 put_task_struct(task);
2290 static const struct inode_operations proc_map_files_inode_operations = {
2291 .lookup = proc_map_files_lookup,
2292 .permission = proc_fd_permission,
2293 .setattr = proc_setattr,
2297 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
2299 struct dentry *dentry = filp->f_path.dentry;
2300 struct inode *inode = dentry->d_inode;
2301 struct vm_area_struct *vma;
2302 struct task_struct *task;
2303 struct mm_struct *mm;
2308 if (!capable(CAP_SYS_ADMIN))
2312 task = get_proc_task(inode);
2317 if (lock_trace(task))
2321 switch (filp->f_pos) {
2324 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
2328 ino = parent_ino(dentry);
2329 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2334 unsigned long nr_files, pos, i;
2335 struct flex_array *fa = NULL;
2336 struct map_files_info info;
2337 struct map_files_info *p;
2339 mm = get_task_mm(task);
2342 down_read(&mm->mmap_sem);
2347 * We need two passes here:
2349 * 1) Collect vmas of mapped files with mmap_sem taken
2350 * 2) Release mmap_sem and instantiate entries
2352 * otherwise we get lockdep complained, since filldir()
2353 * routine might require mmap_sem taken in might_fault().
2356 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
2357 if (vma->vm_file && ++pos > filp->f_pos)
2362 fa = flex_array_alloc(sizeof(info), nr_files,
2364 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2368 flex_array_free(fa);
2369 up_read(&mm->mmap_sem);
2373 for (i = 0, vma = mm->mmap, pos = 2; vma;
2374 vma = vma->vm_next) {
2377 if (++pos <= filp->f_pos)
2380 get_file(vma->vm_file);
2381 info.file = vma->vm_file;
2382 info.len = snprintf(info.name,
2383 sizeof(info.name), "%lx-%lx",
2384 vma->vm_start, vma->vm_end);
2385 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2389 up_read(&mm->mmap_sem);
2391 for (i = 0; i < nr_files; i++) {
2392 p = flex_array_get(fa, i);
2393 ret = proc_fill_cache(filp, dirent, filldir,
2395 proc_map_files_instantiate,
2402 for (; i < nr_files; i++) {
2404 * In case of error don't forget
2405 * to put rest of file refs.
2407 p = flex_array_get(fa, i);
2411 flex_array_free(fa);
2419 put_task_struct(task);
2424 static const struct file_operations proc_map_files_operations = {
2425 .read = generic_read_dir,
2426 .readdir = proc_map_files_readdir,
2427 .llseek = default_llseek,
2430 #endif /* CONFIG_CHECKPOINT_RESTORE */
2433 * /proc/pid/fd needs a special permission handler so that a process can still
2434 * access /proc/self/fd after it has executed a setuid().
2436 static int proc_fd_permission(struct inode *inode, int mask)
2438 int rv = generic_permission(inode, mask);
2441 if (task_pid(current) == proc_pid(inode))
2447 * proc directories can do almost nothing..
2449 static const struct inode_operations proc_fd_inode_operations = {
2450 .lookup = proc_lookupfd,
2451 .permission = proc_fd_permission,
2452 .setattr = proc_setattr,
2455 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2456 struct dentry *dentry, struct task_struct *task, const void *ptr)
2458 unsigned fd = *(unsigned *)ptr;
2459 struct inode *inode;
2460 struct proc_inode *ei;
2461 struct dentry *error = ERR_PTR(-ENOENT);
2463 inode = proc_pid_make_inode(dir->i_sb, task);
2468 inode->i_mode = S_IFREG | S_IRUSR;
2469 inode->i_fop = &proc_fdinfo_file_operations;
2470 d_set_d_op(dentry, &tid_fd_dentry_operations);
2471 d_add(dentry, inode);
2472 /* Close the race of the process dying before we return the dentry */
2473 if (tid_fd_revalidate(dentry, NULL))
2480 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2481 struct dentry *dentry,
2482 struct nameidata *nd)
2484 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2487 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2489 return proc_readfd_common(filp, dirent, filldir,
2490 proc_fdinfo_instantiate);
2493 static const struct file_operations proc_fdinfo_operations = {
2494 .read = generic_read_dir,
2495 .readdir = proc_readfdinfo,
2496 .llseek = default_llseek,
2500 * proc directories can do almost nothing..
2502 static const struct inode_operations proc_fdinfo_inode_operations = {
2503 .lookup = proc_lookupfdinfo,
2504 .setattr = proc_setattr,
2508 static struct dentry *proc_pident_instantiate(struct inode *dir,
2509 struct dentry *dentry, struct task_struct *task, const void *ptr)
2511 const struct pid_entry *p = ptr;
2512 struct inode *inode;
2513 struct proc_inode *ei;
2514 struct dentry *error = ERR_PTR(-ENOENT);
2516 inode = proc_pid_make_inode(dir->i_sb, task);
2521 inode->i_mode = p->mode;
2522 if (S_ISDIR(inode->i_mode))
2523 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2525 inode->i_op = p->iop;
2527 inode->i_fop = p->fop;
2529 d_set_d_op(dentry, &pid_dentry_operations);
2530 d_add(dentry, inode);
2531 /* Close the race of the process dying before we return the dentry */
2532 if (pid_revalidate(dentry, NULL))
2538 static struct dentry *proc_pident_lookup(struct inode *dir,
2539 struct dentry *dentry,
2540 const struct pid_entry *ents,
2543 struct dentry *error;
2544 struct task_struct *task = get_proc_task(dir);
2545 const struct pid_entry *p, *last;
2547 error = ERR_PTR(-ENOENT);
2553 * Yes, it does not scale. And it should not. Don't add
2554 * new entries into /proc/<tgid>/ without very good reasons.
2556 last = &ents[nents - 1];
2557 for (p = ents; p <= last; p++) {
2558 if (p->len != dentry->d_name.len)
2560 if (!memcmp(dentry->d_name.name, p->name, p->len))
2566 error = proc_pident_instantiate(dir, dentry, task, p);
2568 put_task_struct(task);
2573 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2574 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2576 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2577 proc_pident_instantiate, task, p);
2580 static int proc_pident_readdir(struct file *filp,
2581 void *dirent, filldir_t filldir,
2582 const struct pid_entry *ents, unsigned int nents)
2585 struct dentry *dentry = filp->f_path.dentry;
2586 struct inode *inode = dentry->d_inode;
2587 struct task_struct *task = get_proc_task(inode);
2588 const struct pid_entry *p, *last;
2601 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2607 ino = parent_ino(dentry);
2608 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2620 last = &ents[nents - 1];
2622 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2631 put_task_struct(task);
2636 #ifdef CONFIG_SECURITY
2637 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2638 size_t count, loff_t *ppos)
2640 struct inode * inode = file->f_path.dentry->d_inode;
2643 struct task_struct *task = get_proc_task(inode);
2648 length = security_getprocattr(task,
2649 (char*)file->f_path.dentry->d_name.name,
2651 put_task_struct(task);
2653 length = simple_read_from_buffer(buf, count, ppos, p, length);
2658 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2659 size_t count, loff_t *ppos)
2661 struct inode * inode = file->f_path.dentry->d_inode;
2664 struct task_struct *task = get_proc_task(inode);
2669 if (count > PAGE_SIZE)
2672 /* No partial writes. */
2678 page = (char*)__get_free_page(GFP_TEMPORARY);
2683 if (copy_from_user(page, buf, count))
2686 /* Guard against adverse ptrace interaction */
2687 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2691 length = security_setprocattr(task,
2692 (char*)file->f_path.dentry->d_name.name,
2693 (void*)page, count);
2694 mutex_unlock(&task->signal->cred_guard_mutex);
2696 free_page((unsigned long) page);
2698 put_task_struct(task);
2703 static const struct file_operations proc_pid_attr_operations = {
2704 .read = proc_pid_attr_read,
2705 .write = proc_pid_attr_write,
2706 .llseek = generic_file_llseek,
2709 static const struct pid_entry attr_dir_stuff[] = {
2710 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2711 REG("prev", S_IRUGO, proc_pid_attr_operations),
2712 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2713 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2714 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2715 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2718 static int proc_attr_dir_readdir(struct file * filp,
2719 void * dirent, filldir_t filldir)
2721 return proc_pident_readdir(filp,dirent,filldir,
2722 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2725 static const struct file_operations proc_attr_dir_operations = {
2726 .read = generic_read_dir,
2727 .readdir = proc_attr_dir_readdir,
2728 .llseek = default_llseek,
2731 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2732 struct dentry *dentry, struct nameidata *nd)
2734 return proc_pident_lookup(dir, dentry,
2735 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2738 static const struct inode_operations proc_attr_dir_inode_operations = {
2739 .lookup = proc_attr_dir_lookup,
2740 .getattr = pid_getattr,
2741 .setattr = proc_setattr,
2746 #ifdef CONFIG_ELF_CORE
2747 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2748 size_t count, loff_t *ppos)
2750 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2751 struct mm_struct *mm;
2752 char buffer[PROC_NUMBUF];
2760 mm = get_task_mm(task);
2762 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2763 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2764 MMF_DUMP_FILTER_SHIFT));
2766 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2769 put_task_struct(task);
2774 static ssize_t proc_coredump_filter_write(struct file *file,
2775 const char __user *buf,
2779 struct task_struct *task;
2780 struct mm_struct *mm;
2781 char buffer[PROC_NUMBUF], *end;
2788 memset(buffer, 0, sizeof(buffer));
2789 if (count > sizeof(buffer) - 1)
2790 count = sizeof(buffer) - 1;
2791 if (copy_from_user(buffer, buf, count))
2795 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2798 if (end - buffer == 0)
2802 task = get_proc_task(file->f_dentry->d_inode);
2807 mm = get_task_mm(task);
2811 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2813 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2815 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2820 put_task_struct(task);
2825 static const struct file_operations proc_coredump_filter_operations = {
2826 .read = proc_coredump_filter_read,
2827 .write = proc_coredump_filter_write,
2828 .llseek = generic_file_llseek,
2835 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2838 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2839 pid_t tgid = task_tgid_nr_ns(current, ns);
2840 char tmp[PROC_NUMBUF];
2843 sprintf(tmp, "%d", tgid);
2844 return vfs_readlink(dentry,buffer,buflen,tmp);
2847 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2849 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2850 pid_t tgid = task_tgid_nr_ns(current, ns);
2851 char *name = ERR_PTR(-ENOENT);
2855 name = ERR_PTR(-ENOMEM);
2857 sprintf(name, "%d", tgid);
2859 nd_set_link(nd, name);
2863 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2866 char *s = nd_get_link(nd);
2871 static const struct inode_operations proc_self_inode_operations = {
2872 .readlink = proc_self_readlink,
2873 .follow_link = proc_self_follow_link,
2874 .put_link = proc_self_put_link,
2880 * These are the directory entries in the root directory of /proc
2881 * that properly belong to the /proc filesystem, as they describe
2882 * describe something that is process related.
2884 static const struct pid_entry proc_base_stuff[] = {
2885 NOD("self", S_IFLNK|S_IRWXUGO,
2886 &proc_self_inode_operations, NULL, {}),
2889 static struct dentry *proc_base_instantiate(struct inode *dir,
2890 struct dentry *dentry, struct task_struct *task, const void *ptr)
2892 const struct pid_entry *p = ptr;
2893 struct inode *inode;
2894 struct proc_inode *ei;
2895 struct dentry *error;
2897 /* Allocate the inode */
2898 error = ERR_PTR(-ENOMEM);
2899 inode = new_inode(dir->i_sb);
2903 /* Initialize the inode */
2905 inode->i_ino = get_next_ino();
2906 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2909 * grab the reference to the task.
2911 ei->pid = get_task_pid(task, PIDTYPE_PID);
2915 inode->i_mode = p->mode;
2916 if (S_ISDIR(inode->i_mode))
2917 set_nlink(inode, 2);
2918 if (S_ISLNK(inode->i_mode))
2921 inode->i_op = p->iop;
2923 inode->i_fop = p->fop;
2925 d_add(dentry, inode);
2934 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2936 struct dentry *error;
2937 struct task_struct *task = get_proc_task(dir);
2938 const struct pid_entry *p, *last;
2940 error = ERR_PTR(-ENOENT);
2945 /* Lookup the directory entry */
2946 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2947 for (p = proc_base_stuff; p <= last; p++) {
2948 if (p->len != dentry->d_name.len)
2950 if (!memcmp(dentry->d_name.name, p->name, p->len))
2956 error = proc_base_instantiate(dir, dentry, task, p);
2959 put_task_struct(task);
2964 static int proc_base_fill_cache(struct file *filp, void *dirent,
2965 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2967 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2968 proc_base_instantiate, task, p);
2971 #ifdef CONFIG_TASK_IO_ACCOUNTING
2972 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2974 struct task_io_accounting acct = task->ioac;
2975 unsigned long flags;
2978 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2982 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2987 if (whole && lock_task_sighand(task, &flags)) {
2988 struct task_struct *t = task;
2990 task_io_accounting_add(&acct, &task->signal->ioac);
2991 while_each_thread(task, t)
2992 task_io_accounting_add(&acct, &t->ioac);
2994 unlock_task_sighand(task, &flags);
2996 result = sprintf(buffer,
3001 "read_bytes: %llu\n"
3002 "write_bytes: %llu\n"
3003 "cancelled_write_bytes: %llu\n",
3004 (unsigned long long)acct.rchar,
3005 (unsigned long long)acct.wchar,
3006 (unsigned long long)acct.syscr,
3007 (unsigned long long)acct.syscw,
3008 (unsigned long long)acct.read_bytes,
3009 (unsigned long long)acct.write_bytes,
3010 (unsigned long long)acct.cancelled_write_bytes);
3012 mutex_unlock(&task->signal->cred_guard_mutex);
3016 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3018 return do_io_accounting(task, buffer, 0);
3021 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3023 return do_io_accounting(task, buffer, 1);
3025 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3027 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3028 struct pid *pid, struct task_struct *task)
3030 int err = lock_trace(task);
3032 seq_printf(m, "%08x\n", task->personality);
3041 static const struct file_operations proc_task_operations;
3042 static const struct inode_operations proc_task_inode_operations;
3044 static const struct pid_entry tgid_base_stuff[] = {
3045 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3046 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3047 #ifdef CONFIG_CHECKPOINT_RESTORE
3048 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3050 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3051 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3053 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3055 REG("environ", S_IRUSR, proc_environ_operations),
3056 INF("auxv", S_IRUSR, proc_pid_auxv),
3057 ONE("status", S_IRUGO, proc_pid_status),
3058 ONE("personality", S_IRUGO, proc_pid_personality),
3059 INF("limits", S_IRUGO, proc_pid_limits),
3060 #ifdef CONFIG_SCHED_DEBUG
3061 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3063 #ifdef CONFIG_SCHED_AUTOGROUP
3064 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3066 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3067 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3068 INF("syscall", S_IRUGO, proc_pid_syscall),
3070 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3071 ONE("stat", S_IRUGO, proc_tgid_stat),
3072 ONE("statm", S_IRUGO, proc_pid_statm),
3073 REG("maps", S_IRUGO, proc_maps_operations),
3074 REG("arm_maps", S_IRUGO, proc_armv7_maps_operations),
3076 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3078 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3079 LNK("cwd", proc_cwd_link),
3080 LNK("root", proc_root_link),
3081 LNK("exe", proc_exe_link),
3082 REG("mounts", S_IRUGO, proc_mounts_operations),
3083 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3084 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3085 #ifdef CONFIG_PROC_PAGE_MONITOR
3086 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3087 REG("smaps", S_IRUGO, proc_smaps_operations),
3088 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3090 #ifdef CONFIG_SECURITY
3091 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3093 #ifdef CONFIG_KALLSYMS
3094 INF("wchan", S_IRUGO, proc_pid_wchan),
3096 #ifdef CONFIG_STACKTRACE
3097 ONE("stack", S_IRUGO, proc_pid_stack),
3099 #ifdef CONFIG_SCHEDSTATS
3100 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3102 #ifdef CONFIG_LATENCYTOP
3103 REG("latency", S_IRUGO, proc_lstats_operations),
3105 #ifdef CONFIG_PROC_PID_CPUSET
3106 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3108 #ifdef CONFIG_CGROUPS
3109 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3111 INF("oom_score", S_IRUGO, proc_oom_score),
3112 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3113 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3114 #ifdef CONFIG_AUDITSYSCALL
3115 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3116 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3118 #ifdef CONFIG_FAULT_INJECTION
3119 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3121 #ifdef CONFIG_ELF_CORE
3122 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3124 #ifdef CONFIG_TASK_IO_ACCOUNTING
3125 INF("io", S_IRUSR, proc_tgid_io_accounting),
3127 #ifdef CONFIG_HARDWALL
3128 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3132 static int proc_tgid_base_readdir(struct file * filp,
3133 void * dirent, filldir_t filldir)
3135 return proc_pident_readdir(filp,dirent,filldir,
3136 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3139 static const struct file_operations proc_tgid_base_operations = {
3140 .read = generic_read_dir,
3141 .readdir = proc_tgid_base_readdir,
3142 .llseek = default_llseek,
3145 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3146 return proc_pident_lookup(dir, dentry,
3147 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3150 static const struct inode_operations proc_tgid_base_inode_operations = {
3151 .lookup = proc_tgid_base_lookup,
3152 .getattr = pid_getattr,
3153 .setattr = proc_setattr,
3156 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3158 struct dentry *dentry, *leader, *dir;
3159 char buf[PROC_NUMBUF];
3163 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3164 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3166 shrink_dcache_parent(dentry);
3172 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3173 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3178 name.len = strlen(name.name);
3179 dir = d_hash_and_lookup(leader, &name);
3181 goto out_put_leader;
3184 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3185 dentry = d_hash_and_lookup(dir, &name);
3187 shrink_dcache_parent(dentry);
3200 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3201 * @task: task that should be flushed.
3203 * When flushing dentries from proc, one needs to flush them from global
3204 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3205 * in. This call is supposed to do all of this job.
3207 * Looks in the dcache for
3209 * /proc/@tgid/task/@pid
3210 * if either directory is present flushes it and all of it'ts children
3213 * It is safe and reasonable to cache /proc entries for a task until
3214 * that task exits. After that they just clog up the dcache with
3215 * useless entries, possibly causing useful dcache entries to be
3216 * flushed instead. This routine is proved to flush those useless
3217 * dcache entries at process exit time.
3219 * NOTE: This routine is just an optimization so it does not guarantee
3220 * that no dcache entries will exist at process exit time it
3221 * just makes it very unlikely that any will persist.
3224 void proc_flush_task(struct task_struct *task)
3227 struct pid *pid, *tgid;
3230 pid = task_pid(task);
3231 tgid = task_tgid(task);
3233 for (i = 0; i <= pid->level; i++) {
3234 upid = &pid->numbers[i];
3235 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3236 tgid->numbers[i].nr);
3239 upid = &pid->numbers[pid->level];
3241 pid_ns_release_proc(upid->ns);
3244 static struct dentry *proc_pid_instantiate(struct inode *dir,
3245 struct dentry * dentry,
3246 struct task_struct *task, const void *ptr)
3248 struct dentry *error = ERR_PTR(-ENOENT);
3249 struct inode *inode;
3251 inode = proc_pid_make_inode(dir->i_sb, task);
3255 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3256 inode->i_op = &proc_tgid_base_inode_operations;
3257 inode->i_fop = &proc_tgid_base_operations;
3258 inode->i_flags|=S_IMMUTABLE;
3260 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3261 ARRAY_SIZE(tgid_base_stuff)));
3263 d_set_d_op(dentry, &pid_dentry_operations);
3265 d_add(dentry, inode);
3266 /* Close the race of the process dying before we return the dentry */
3267 if (pid_revalidate(dentry, NULL))
3273 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3275 struct dentry *result;
3276 struct task_struct *task;
3278 struct pid_namespace *ns;
3280 result = proc_base_lookup(dir, dentry);
3281 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3284 tgid = name_to_int(dentry);
3288 ns = dentry->d_sb->s_fs_info;
3290 task = find_task_by_pid_ns(tgid, ns);
3292 get_task_struct(task);
3297 result = proc_pid_instantiate(dir, dentry, task, NULL);
3298 put_task_struct(task);
3304 * Find the first task with tgid >= tgid
3309 struct task_struct *task;
3311 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3316 put_task_struct(iter.task);
3320 pid = find_ge_pid(iter.tgid, ns);
3322 iter.tgid = pid_nr_ns(pid, ns);
3323 iter.task = pid_task(pid, PIDTYPE_PID);
3324 /* What we to know is if the pid we have find is the
3325 * pid of a thread_group_leader. Testing for task
3326 * being a thread_group_leader is the obvious thing
3327 * todo but there is a window when it fails, due to
3328 * the pid transfer logic in de_thread.
3330 * So we perform the straight forward test of seeing
3331 * if the pid we have found is the pid of a thread
3332 * group leader, and don't worry if the task we have
3333 * found doesn't happen to be a thread group leader.
3334 * As we don't care in the case of readdir.
3336 if (!iter.task || !has_group_leader_pid(iter.task)) {
3340 get_task_struct(iter.task);
3346 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3348 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3349 struct tgid_iter iter)
3351 char name[PROC_NUMBUF];
3352 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3353 return proc_fill_cache(filp, dirent, filldir, name, len,
3354 proc_pid_instantiate, iter.task, NULL);
3357 /* for the /proc/ directory itself, after non-process stuff has been done */
3358 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3361 struct task_struct *reaper;
3362 struct tgid_iter iter;
3363 struct pid_namespace *ns;
3365 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3367 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3369 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3373 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3374 const struct pid_entry *p = &proc_base_stuff[nr];
3375 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3379 ns = filp->f_dentry->d_sb->s_fs_info;
3381 iter.tgid = filp->f_pos - TGID_OFFSET;
3382 for (iter = next_tgid(ns, iter);
3384 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3385 filp->f_pos = iter.tgid + TGID_OFFSET;
3386 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3387 put_task_struct(iter.task);
3391 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3393 put_task_struct(reaper);
3401 static const struct pid_entry tid_base_stuff[] = {
3402 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3403 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3404 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3405 REG("environ", S_IRUSR, proc_environ_operations),
3406 INF("auxv", S_IRUSR, proc_pid_auxv),
3407 ONE("status", S_IRUGO, proc_pid_status),
3408 ONE("personality", S_IRUGO, proc_pid_personality),
3409 INF("limits", S_IRUGO, proc_pid_limits),
3410 #ifdef CONFIG_SCHED_DEBUG
3411 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3413 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3414 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3415 INF("syscall", S_IRUGO, proc_pid_syscall),
3417 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3418 ONE("stat", S_IRUGO, proc_tid_stat),
3419 ONE("statm", S_IRUGO, proc_pid_statm),
3420 REG("maps", S_IRUGO, proc_maps_operations),
3421 REG("arm_maps", S_IRUGO, proc_armv7_maps_operations),
3423 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3425 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3426 LNK("cwd", proc_cwd_link),
3427 LNK("root", proc_root_link),
3428 LNK("exe", proc_exe_link),
3429 REG("mounts", S_IRUGO, proc_mounts_operations),
3430 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3431 #ifdef CONFIG_PROC_PAGE_MONITOR
3432 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3433 REG("smaps", S_IRUGO, proc_smaps_operations),
3434 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3436 #ifdef CONFIG_SECURITY
3437 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3439 #ifdef CONFIG_KALLSYMS
3440 INF("wchan", S_IRUGO, proc_pid_wchan),
3442 #ifdef CONFIG_STACKTRACE
3443 ONE("stack", S_IRUGO, proc_pid_stack),
3445 #ifdef CONFIG_SCHEDSTATS
3446 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3448 #ifdef CONFIG_LATENCYTOP
3449 REG("latency", S_IRUGO, proc_lstats_operations),
3451 #ifdef CONFIG_PROC_PID_CPUSET
3452 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3454 #ifdef CONFIG_CGROUPS
3455 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3457 INF("oom_score", S_IRUGO, proc_oom_score),
3458 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3459 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3460 #ifdef CONFIG_AUDITSYSCALL
3461 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3462 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3464 #ifdef CONFIG_FAULT_INJECTION
3465 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3467 #ifdef CONFIG_TASK_IO_ACCOUNTING
3468 INF("io", S_IRUSR, proc_tid_io_accounting),
3470 #ifdef CONFIG_HARDWALL
3471 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3475 static int proc_tid_base_readdir(struct file * filp,
3476 void * dirent, filldir_t filldir)
3478 return proc_pident_readdir(filp,dirent,filldir,
3479 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3482 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3483 return proc_pident_lookup(dir, dentry,
3484 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3487 static const struct file_operations proc_tid_base_operations = {
3488 .read = generic_read_dir,
3489 .readdir = proc_tid_base_readdir,
3490 .llseek = default_llseek,
3493 static const struct inode_operations proc_tid_base_inode_operations = {
3494 .lookup = proc_tid_base_lookup,
3495 .getattr = pid_getattr,
3496 .setattr = proc_setattr,
3499 static struct dentry *proc_task_instantiate(struct inode *dir,
3500 struct dentry *dentry, struct task_struct *task, const void *ptr)
3502 struct dentry *error = ERR_PTR(-ENOENT);
3503 struct inode *inode;
3504 inode = proc_pid_make_inode(dir->i_sb, task);
3508 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3509 inode->i_op = &proc_tid_base_inode_operations;
3510 inode->i_fop = &proc_tid_base_operations;
3511 inode->i_flags|=S_IMMUTABLE;
3513 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3514 ARRAY_SIZE(tid_base_stuff)));
3516 d_set_d_op(dentry, &pid_dentry_operations);
3518 d_add(dentry, inode);
3519 /* Close the race of the process dying before we return the dentry */
3520 if (pid_revalidate(dentry, NULL))
3526 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3528 struct dentry *result = ERR_PTR(-ENOENT);
3529 struct task_struct *task;
3530 struct task_struct *leader = get_proc_task(dir);
3532 struct pid_namespace *ns;
3537 tid = name_to_int(dentry);
3541 ns = dentry->d_sb->s_fs_info;
3543 task = find_task_by_pid_ns(tid, ns);
3545 get_task_struct(task);
3549 if (!same_thread_group(leader, task))
3552 result = proc_task_instantiate(dir, dentry, task, NULL);
3554 put_task_struct(task);
3556 put_task_struct(leader);
3562 * Find the first tid of a thread group to return to user space.
3564 * Usually this is just the thread group leader, but if the users
3565 * buffer was too small or there was a seek into the middle of the
3566 * directory we have more work todo.
3568 * In the case of a short read we start with find_task_by_pid.
3570 * In the case of a seek we start with the leader and walk nr
3573 static struct task_struct *first_tid(struct task_struct *leader,
3574 int tid, int nr, struct pid_namespace *ns)
3576 struct task_struct *pos;
3579 /* Attempt to start with the pid of a thread */
3580 if (tid && (nr > 0)) {
3581 pos = find_task_by_pid_ns(tid, ns);
3582 if (pos && (pos->group_leader == leader))
3586 /* If nr exceeds the number of threads there is nothing todo */
3588 if (nr && nr >= get_nr_threads(leader))
3591 /* If we haven't found our starting place yet start
3592 * with the leader and walk nr threads forward.
3594 for (pos = leader; nr > 0; --nr) {
3595 pos = next_thread(pos);
3596 if (pos == leader) {
3602 get_task_struct(pos);
3609 * Find the next thread in the thread list.
3610 * Return NULL if there is an error or no next thread.
3612 * The reference to the input task_struct is released.
3614 static struct task_struct *next_tid(struct task_struct *start)
3616 struct task_struct *pos = NULL;
3618 if (pid_alive(start)) {
3619 pos = next_thread(start);
3620 if (thread_group_leader(pos))
3623 get_task_struct(pos);
3626 put_task_struct(start);
3630 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3631 struct task_struct *task, int tid)
3633 char name[PROC_NUMBUF];
3634 int len = snprintf(name, sizeof(name), "%d", tid);
3635 return proc_fill_cache(filp, dirent, filldir, name, len,
3636 proc_task_instantiate, task, NULL);
3639 /* for the /proc/TGID/task/ directories */
3640 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3642 struct dentry *dentry = filp->f_path.dentry;
3643 struct inode *inode = dentry->d_inode;
3644 struct task_struct *leader = NULL;
3645 struct task_struct *task;
3646 int retval = -ENOENT;
3649 struct pid_namespace *ns;
3651 task = get_proc_task(inode);
3655 if (pid_alive(task)) {
3656 leader = task->group_leader;
3657 get_task_struct(leader);
3660 put_task_struct(task);
3665 switch ((unsigned long)filp->f_pos) {
3668 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3673 ino = parent_ino(dentry);
3674 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3680 /* f_version caches the tgid value that the last readdir call couldn't
3681 * return. lseek aka telldir automagically resets f_version to 0.
3683 ns = filp->f_dentry->d_sb->s_fs_info;
3684 tid = (int)filp->f_version;
3685 filp->f_version = 0;
3686 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3688 task = next_tid(task), filp->f_pos++) {
3689 tid = task_pid_nr_ns(task, ns);
3690 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3691 /* returning this tgid failed, save it as the first
3692 * pid for the next readir call */
3693 filp->f_version = (u64)tid;
3694 put_task_struct(task);
3699 put_task_struct(leader);
3704 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3706 struct inode *inode = dentry->d_inode;
3707 struct task_struct *p = get_proc_task(inode);
3708 generic_fillattr(inode, stat);
3711 stat->nlink += get_nr_threads(p);
3718 static const struct inode_operations proc_task_inode_operations = {
3719 .lookup = proc_task_lookup,
3720 .getattr = proc_task_getattr,
3721 .setattr = proc_setattr,
3724 static const struct file_operations proc_task_operations = {
3725 .read = generic_read_dir,
3726 .readdir = proc_task_readdir,
3727 .llseek = default_llseek,