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 info.file = get_file(vma->vm_file);
2381 info.len = snprintf(info.name,
2382 sizeof(info.name), "%lx-%lx",
2383 vma->vm_start, vma->vm_end);
2384 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2388 up_read(&mm->mmap_sem);
2390 for (i = 0; i < nr_files; i++) {
2391 p = flex_array_get(fa, i);
2392 ret = proc_fill_cache(filp, dirent, filldir,
2394 proc_map_files_instantiate,
2401 for (; i < nr_files; i++) {
2403 * In case of error don't forget
2404 * to put rest of file refs.
2406 p = flex_array_get(fa, i);
2410 flex_array_free(fa);
2418 put_task_struct(task);
2423 static const struct file_operations proc_map_files_operations = {
2424 .read = generic_read_dir,
2425 .readdir = proc_map_files_readdir,
2426 .llseek = default_llseek,
2429 #endif /* CONFIG_CHECKPOINT_RESTORE */
2432 * /proc/pid/fd needs a special permission handler so that a process can still
2433 * access /proc/self/fd after it has executed a setuid().
2435 static int proc_fd_permission(struct inode *inode, int mask)
2437 int rv = generic_permission(inode, mask);
2440 if (task_pid(current) == proc_pid(inode))
2446 * proc directories can do almost nothing..
2448 static const struct inode_operations proc_fd_inode_operations = {
2449 .lookup = proc_lookupfd,
2450 .permission = proc_fd_permission,
2451 .setattr = proc_setattr,
2454 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2455 struct dentry *dentry, struct task_struct *task, const void *ptr)
2457 unsigned fd = *(unsigned *)ptr;
2458 struct inode *inode;
2459 struct proc_inode *ei;
2460 struct dentry *error = ERR_PTR(-ENOENT);
2462 inode = proc_pid_make_inode(dir->i_sb, task);
2467 inode->i_mode = S_IFREG | S_IRUSR;
2468 inode->i_fop = &proc_fdinfo_file_operations;
2469 d_set_d_op(dentry, &tid_fd_dentry_operations);
2470 d_add(dentry, inode);
2471 /* Close the race of the process dying before we return the dentry */
2472 if (tid_fd_revalidate(dentry, NULL))
2479 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2480 struct dentry *dentry,
2481 struct nameidata *nd)
2483 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2486 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2488 return proc_readfd_common(filp, dirent, filldir,
2489 proc_fdinfo_instantiate);
2492 static const struct file_operations proc_fdinfo_operations = {
2493 .read = generic_read_dir,
2494 .readdir = proc_readfdinfo,
2495 .llseek = default_llseek,
2499 * proc directories can do almost nothing..
2501 static const struct inode_operations proc_fdinfo_inode_operations = {
2502 .lookup = proc_lookupfdinfo,
2503 .setattr = proc_setattr,
2507 static struct dentry *proc_pident_instantiate(struct inode *dir,
2508 struct dentry *dentry, struct task_struct *task, const void *ptr)
2510 const struct pid_entry *p = ptr;
2511 struct inode *inode;
2512 struct proc_inode *ei;
2513 struct dentry *error = ERR_PTR(-ENOENT);
2515 inode = proc_pid_make_inode(dir->i_sb, task);
2520 inode->i_mode = p->mode;
2521 if (S_ISDIR(inode->i_mode))
2522 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2524 inode->i_op = p->iop;
2526 inode->i_fop = p->fop;
2528 d_set_d_op(dentry, &pid_dentry_operations);
2529 d_add(dentry, inode);
2530 /* Close the race of the process dying before we return the dentry */
2531 if (pid_revalidate(dentry, NULL))
2537 static struct dentry *proc_pident_lookup(struct inode *dir,
2538 struct dentry *dentry,
2539 const struct pid_entry *ents,
2542 struct dentry *error;
2543 struct task_struct *task = get_proc_task(dir);
2544 const struct pid_entry *p, *last;
2546 error = ERR_PTR(-ENOENT);
2552 * Yes, it does not scale. And it should not. Don't add
2553 * new entries into /proc/<tgid>/ without very good reasons.
2555 last = &ents[nents - 1];
2556 for (p = ents; p <= last; p++) {
2557 if (p->len != dentry->d_name.len)
2559 if (!memcmp(dentry->d_name.name, p->name, p->len))
2565 error = proc_pident_instantiate(dir, dentry, task, p);
2567 put_task_struct(task);
2572 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2573 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2575 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2576 proc_pident_instantiate, task, p);
2579 static int proc_pident_readdir(struct file *filp,
2580 void *dirent, filldir_t filldir,
2581 const struct pid_entry *ents, unsigned int nents)
2584 struct dentry *dentry = filp->f_path.dentry;
2585 struct inode *inode = dentry->d_inode;
2586 struct task_struct *task = get_proc_task(inode);
2587 const struct pid_entry *p, *last;
2600 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2606 ino = parent_ino(dentry);
2607 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2619 last = &ents[nents - 1];
2621 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2630 put_task_struct(task);
2635 #ifdef CONFIG_SECURITY
2636 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2637 size_t count, loff_t *ppos)
2639 struct inode * inode = file->f_path.dentry->d_inode;
2642 struct task_struct *task = get_proc_task(inode);
2647 length = security_getprocattr(task,
2648 (char*)file->f_path.dentry->d_name.name,
2650 put_task_struct(task);
2652 length = simple_read_from_buffer(buf, count, ppos, p, length);
2657 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2658 size_t count, loff_t *ppos)
2660 struct inode * inode = file->f_path.dentry->d_inode;
2663 struct task_struct *task = get_proc_task(inode);
2668 if (count > PAGE_SIZE)
2671 /* No partial writes. */
2677 page = (char*)__get_free_page(GFP_TEMPORARY);
2682 if (copy_from_user(page, buf, count))
2685 /* Guard against adverse ptrace interaction */
2686 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2690 length = security_setprocattr(task,
2691 (char*)file->f_path.dentry->d_name.name,
2692 (void*)page, count);
2693 mutex_unlock(&task->signal->cred_guard_mutex);
2695 free_page((unsigned long) page);
2697 put_task_struct(task);
2702 static const struct file_operations proc_pid_attr_operations = {
2703 .read = proc_pid_attr_read,
2704 .write = proc_pid_attr_write,
2705 .llseek = generic_file_llseek,
2708 static const struct pid_entry attr_dir_stuff[] = {
2709 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2710 REG("prev", S_IRUGO, proc_pid_attr_operations),
2711 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2712 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2713 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2714 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2717 static int proc_attr_dir_readdir(struct file * filp,
2718 void * dirent, filldir_t filldir)
2720 return proc_pident_readdir(filp,dirent,filldir,
2721 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2724 static const struct file_operations proc_attr_dir_operations = {
2725 .read = generic_read_dir,
2726 .readdir = proc_attr_dir_readdir,
2727 .llseek = default_llseek,
2730 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2731 struct dentry *dentry, struct nameidata *nd)
2733 return proc_pident_lookup(dir, dentry,
2734 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2737 static const struct inode_operations proc_attr_dir_inode_operations = {
2738 .lookup = proc_attr_dir_lookup,
2739 .getattr = pid_getattr,
2740 .setattr = proc_setattr,
2745 #ifdef CONFIG_ELF_CORE
2746 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2747 size_t count, loff_t *ppos)
2749 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2750 struct mm_struct *mm;
2751 char buffer[PROC_NUMBUF];
2759 mm = get_task_mm(task);
2761 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2762 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2763 MMF_DUMP_FILTER_SHIFT));
2765 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2768 put_task_struct(task);
2773 static ssize_t proc_coredump_filter_write(struct file *file,
2774 const char __user *buf,
2778 struct task_struct *task;
2779 struct mm_struct *mm;
2780 char buffer[PROC_NUMBUF], *end;
2787 memset(buffer, 0, sizeof(buffer));
2788 if (count > sizeof(buffer) - 1)
2789 count = sizeof(buffer) - 1;
2790 if (copy_from_user(buffer, buf, count))
2794 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2797 if (end - buffer == 0)
2801 task = get_proc_task(file->f_dentry->d_inode);
2806 mm = get_task_mm(task);
2810 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2812 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2814 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2819 put_task_struct(task);
2824 static const struct file_operations proc_coredump_filter_operations = {
2825 .read = proc_coredump_filter_read,
2826 .write = proc_coredump_filter_write,
2827 .llseek = generic_file_llseek,
2834 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2837 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2838 pid_t tgid = task_tgid_nr_ns(current, ns);
2839 char tmp[PROC_NUMBUF];
2842 sprintf(tmp, "%d", tgid);
2843 return vfs_readlink(dentry,buffer,buflen,tmp);
2846 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2848 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2849 pid_t tgid = task_tgid_nr_ns(current, ns);
2850 char *name = ERR_PTR(-ENOENT);
2854 name = ERR_PTR(-ENOMEM);
2856 sprintf(name, "%d", tgid);
2858 nd_set_link(nd, name);
2862 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2865 char *s = nd_get_link(nd);
2870 static const struct inode_operations proc_self_inode_operations = {
2871 .readlink = proc_self_readlink,
2872 .follow_link = proc_self_follow_link,
2873 .put_link = proc_self_put_link,
2879 * These are the directory entries in the root directory of /proc
2880 * that properly belong to the /proc filesystem, as they describe
2881 * describe something that is process related.
2883 static const struct pid_entry proc_base_stuff[] = {
2884 NOD("self", S_IFLNK|S_IRWXUGO,
2885 &proc_self_inode_operations, NULL, {}),
2888 static struct dentry *proc_base_instantiate(struct inode *dir,
2889 struct dentry *dentry, struct task_struct *task, const void *ptr)
2891 const struct pid_entry *p = ptr;
2892 struct inode *inode;
2893 struct proc_inode *ei;
2894 struct dentry *error;
2896 /* Allocate the inode */
2897 error = ERR_PTR(-ENOMEM);
2898 inode = new_inode(dir->i_sb);
2902 /* Initialize the inode */
2904 inode->i_ino = get_next_ino();
2905 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2908 * grab the reference to the task.
2910 ei->pid = get_task_pid(task, PIDTYPE_PID);
2914 inode->i_mode = p->mode;
2915 if (S_ISDIR(inode->i_mode))
2916 set_nlink(inode, 2);
2917 if (S_ISLNK(inode->i_mode))
2920 inode->i_op = p->iop;
2922 inode->i_fop = p->fop;
2924 d_add(dentry, inode);
2933 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2935 struct dentry *error;
2936 struct task_struct *task = get_proc_task(dir);
2937 const struct pid_entry *p, *last;
2939 error = ERR_PTR(-ENOENT);
2944 /* Lookup the directory entry */
2945 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2946 for (p = proc_base_stuff; p <= last; p++) {
2947 if (p->len != dentry->d_name.len)
2949 if (!memcmp(dentry->d_name.name, p->name, p->len))
2955 error = proc_base_instantiate(dir, dentry, task, p);
2958 put_task_struct(task);
2963 static int proc_base_fill_cache(struct file *filp, void *dirent,
2964 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2966 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2967 proc_base_instantiate, task, p);
2970 #ifdef CONFIG_TASK_IO_ACCOUNTING
2971 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2973 struct task_io_accounting acct = task->ioac;
2974 unsigned long flags;
2977 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2981 if (!ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
2986 if (whole && lock_task_sighand(task, &flags)) {
2987 struct task_struct *t = task;
2989 task_io_accounting_add(&acct, &task->signal->ioac);
2990 while_each_thread(task, t)
2991 task_io_accounting_add(&acct, &t->ioac);
2993 unlock_task_sighand(task, &flags);
2995 result = sprintf(buffer,
3000 "read_bytes: %llu\n"
3001 "write_bytes: %llu\n"
3002 "cancelled_write_bytes: %llu\n",
3003 (unsigned long long)acct.rchar,
3004 (unsigned long long)acct.wchar,
3005 (unsigned long long)acct.syscr,
3006 (unsigned long long)acct.syscw,
3007 (unsigned long long)acct.read_bytes,
3008 (unsigned long long)acct.write_bytes,
3009 (unsigned long long)acct.cancelled_write_bytes);
3011 mutex_unlock(&task->signal->cred_guard_mutex);
3015 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
3017 return do_io_accounting(task, buffer, 0);
3020 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
3022 return do_io_accounting(task, buffer, 1);
3024 #endif /* CONFIG_TASK_IO_ACCOUNTING */
3026 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
3027 struct pid *pid, struct task_struct *task)
3029 int err = lock_trace(task);
3031 seq_printf(m, "%08x\n", task->personality);
3040 static const struct file_operations proc_task_operations;
3041 static const struct inode_operations proc_task_inode_operations;
3043 static const struct pid_entry tgid_base_stuff[] = {
3044 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
3045 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3046 #ifdef CONFIG_CHECKPOINT_RESTORE
3047 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
3049 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3050 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3052 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
3054 REG("environ", S_IRUSR, proc_environ_operations),
3055 INF("auxv", S_IRUSR, proc_pid_auxv),
3056 ONE("status", S_IRUGO, proc_pid_status),
3057 ONE("personality", S_IRUGO, proc_pid_personality),
3058 INF("limits", S_IRUGO, proc_pid_limits),
3059 #ifdef CONFIG_SCHED_DEBUG
3060 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3062 #ifdef CONFIG_SCHED_AUTOGROUP
3063 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
3065 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3066 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3067 INF("syscall", S_IRUGO, proc_pid_syscall),
3069 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3070 ONE("stat", S_IRUGO, proc_tgid_stat),
3071 ONE("statm", S_IRUGO, proc_pid_statm),
3072 REG("maps", S_IRUGO, proc_maps_operations),
3073 REG("arm_maps", S_IRUGO, proc_armv7_maps_operations),
3075 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3077 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3078 LNK("cwd", proc_cwd_link),
3079 LNK("root", proc_root_link),
3080 LNK("exe", proc_exe_link),
3081 REG("mounts", S_IRUGO, proc_mounts_operations),
3082 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3083 REG("mountstats", S_IRUSR, proc_mountstats_operations),
3084 #ifdef CONFIG_PROC_PAGE_MONITOR
3085 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3086 REG("smaps", S_IRUGO, proc_smaps_operations),
3087 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3089 #ifdef CONFIG_SECURITY
3090 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3092 #ifdef CONFIG_KALLSYMS
3093 INF("wchan", S_IRUGO, proc_pid_wchan),
3095 #ifdef CONFIG_STACKTRACE
3096 ONE("stack", S_IRUGO, proc_pid_stack),
3098 #ifdef CONFIG_SCHEDSTATS
3099 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3101 #ifdef CONFIG_LATENCYTOP
3102 REG("latency", S_IRUGO, proc_lstats_operations),
3104 #ifdef CONFIG_PROC_PID_CPUSET
3105 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3107 #ifdef CONFIG_CGROUPS
3108 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3110 INF("oom_score", S_IRUGO, proc_oom_score),
3111 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3112 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3113 #ifdef CONFIG_AUDITSYSCALL
3114 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3115 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3117 #ifdef CONFIG_FAULT_INJECTION
3118 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3120 #ifdef CONFIG_ELF_CORE
3121 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3123 #ifdef CONFIG_TASK_IO_ACCOUNTING
3124 INF("io", S_IRUSR, proc_tgid_io_accounting),
3126 #ifdef CONFIG_HARDWALL
3127 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3131 static int proc_tgid_base_readdir(struct file * filp,
3132 void * dirent, filldir_t filldir)
3134 return proc_pident_readdir(filp,dirent,filldir,
3135 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
3138 static const struct file_operations proc_tgid_base_operations = {
3139 .read = generic_read_dir,
3140 .readdir = proc_tgid_base_readdir,
3141 .llseek = default_llseek,
3144 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3145 return proc_pident_lookup(dir, dentry,
3146 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
3149 static const struct inode_operations proc_tgid_base_inode_operations = {
3150 .lookup = proc_tgid_base_lookup,
3151 .getattr = pid_getattr,
3152 .setattr = proc_setattr,
3155 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
3157 struct dentry *dentry, *leader, *dir;
3158 char buf[PROC_NUMBUF];
3162 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3163 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
3165 shrink_dcache_parent(dentry);
3171 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
3172 leader = d_hash_and_lookup(mnt->mnt_root, &name);
3177 name.len = strlen(name.name);
3178 dir = d_hash_and_lookup(leader, &name);
3180 goto out_put_leader;
3183 name.len = snprintf(buf, sizeof(buf), "%d", pid);
3184 dentry = d_hash_and_lookup(dir, &name);
3186 shrink_dcache_parent(dentry);
3199 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3200 * @task: task that should be flushed.
3202 * When flushing dentries from proc, one needs to flush them from global
3203 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3204 * in. This call is supposed to do all of this job.
3206 * Looks in the dcache for
3208 * /proc/@tgid/task/@pid
3209 * if either directory is present flushes it and all of it'ts children
3212 * It is safe and reasonable to cache /proc entries for a task until
3213 * that task exits. After that they just clog up the dcache with
3214 * useless entries, possibly causing useful dcache entries to be
3215 * flushed instead. This routine is proved to flush those useless
3216 * dcache entries at process exit time.
3218 * NOTE: This routine is just an optimization so it does not guarantee
3219 * that no dcache entries will exist at process exit time it
3220 * just makes it very unlikely that any will persist.
3223 void proc_flush_task(struct task_struct *task)
3226 struct pid *pid, *tgid;
3229 pid = task_pid(task);
3230 tgid = task_tgid(task);
3232 for (i = 0; i <= pid->level; i++) {
3233 upid = &pid->numbers[i];
3234 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
3235 tgid->numbers[i].nr);
3238 upid = &pid->numbers[pid->level];
3240 pid_ns_release_proc(upid->ns);
3243 static struct dentry *proc_pid_instantiate(struct inode *dir,
3244 struct dentry * dentry,
3245 struct task_struct *task, const void *ptr)
3247 struct dentry *error = ERR_PTR(-ENOENT);
3248 struct inode *inode;
3250 inode = proc_pid_make_inode(dir->i_sb, task);
3254 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3255 inode->i_op = &proc_tgid_base_inode_operations;
3256 inode->i_fop = &proc_tgid_base_operations;
3257 inode->i_flags|=S_IMMUTABLE;
3259 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
3260 ARRAY_SIZE(tgid_base_stuff)));
3262 d_set_d_op(dentry, &pid_dentry_operations);
3264 d_add(dentry, inode);
3265 /* Close the race of the process dying before we return the dentry */
3266 if (pid_revalidate(dentry, NULL))
3272 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3274 struct dentry *result;
3275 struct task_struct *task;
3277 struct pid_namespace *ns;
3279 result = proc_base_lookup(dir, dentry);
3280 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3283 tgid = name_to_int(dentry);
3287 ns = dentry->d_sb->s_fs_info;
3289 task = find_task_by_pid_ns(tgid, ns);
3291 get_task_struct(task);
3296 result = proc_pid_instantiate(dir, dentry, task, NULL);
3297 put_task_struct(task);
3303 * Find the first task with tgid >= tgid
3308 struct task_struct *task;
3310 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3315 put_task_struct(iter.task);
3319 pid = find_ge_pid(iter.tgid, ns);
3321 iter.tgid = pid_nr_ns(pid, ns);
3322 iter.task = pid_task(pid, PIDTYPE_PID);
3323 /* What we to know is if the pid we have find is the
3324 * pid of a thread_group_leader. Testing for task
3325 * being a thread_group_leader is the obvious thing
3326 * todo but there is a window when it fails, due to
3327 * the pid transfer logic in de_thread.
3329 * So we perform the straight forward test of seeing
3330 * if the pid we have found is the pid of a thread
3331 * group leader, and don't worry if the task we have
3332 * found doesn't happen to be a thread group leader.
3333 * As we don't care in the case of readdir.
3335 if (!iter.task || !has_group_leader_pid(iter.task)) {
3339 get_task_struct(iter.task);
3345 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3347 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3348 struct tgid_iter iter)
3350 char name[PROC_NUMBUF];
3351 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3352 return proc_fill_cache(filp, dirent, filldir, name, len,
3353 proc_pid_instantiate, iter.task, NULL);
3356 /* for the /proc/ directory itself, after non-process stuff has been done */
3357 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3360 struct task_struct *reaper;
3361 struct tgid_iter iter;
3362 struct pid_namespace *ns;
3364 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3366 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3368 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3372 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3373 const struct pid_entry *p = &proc_base_stuff[nr];
3374 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3378 ns = filp->f_dentry->d_sb->s_fs_info;
3380 iter.tgid = filp->f_pos - TGID_OFFSET;
3381 for (iter = next_tgid(ns, iter);
3383 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3384 filp->f_pos = iter.tgid + TGID_OFFSET;
3385 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3386 put_task_struct(iter.task);
3390 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3392 put_task_struct(reaper);
3400 static const struct pid_entry tid_base_stuff[] = {
3401 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3402 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3403 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3404 REG("environ", S_IRUSR, proc_environ_operations),
3405 INF("auxv", S_IRUSR, proc_pid_auxv),
3406 ONE("status", S_IRUGO, proc_pid_status),
3407 ONE("personality", S_IRUGO, proc_pid_personality),
3408 INF("limits", S_IRUGO, proc_pid_limits),
3409 #ifdef CONFIG_SCHED_DEBUG
3410 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3412 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3413 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3414 INF("syscall", S_IRUGO, proc_pid_syscall),
3416 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3417 ONE("stat", S_IRUGO, proc_tid_stat),
3418 ONE("statm", S_IRUGO, proc_pid_statm),
3419 REG("maps", S_IRUGO, proc_maps_operations),
3420 REG("arm_maps", S_IRUGO, proc_armv7_maps_operations),
3422 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3424 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3425 LNK("cwd", proc_cwd_link),
3426 LNK("root", proc_root_link),
3427 LNK("exe", proc_exe_link),
3428 REG("mounts", S_IRUGO, proc_mounts_operations),
3429 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3430 #ifdef CONFIG_PROC_PAGE_MONITOR
3431 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3432 REG("smaps", S_IRUGO, proc_smaps_operations),
3433 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3435 #ifdef CONFIG_SECURITY
3436 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3438 #ifdef CONFIG_KALLSYMS
3439 INF("wchan", S_IRUGO, proc_pid_wchan),
3441 #ifdef CONFIG_STACKTRACE
3442 ONE("stack", S_IRUGO, proc_pid_stack),
3444 #ifdef CONFIG_SCHEDSTATS
3445 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3447 #ifdef CONFIG_LATENCYTOP
3448 REG("latency", S_IRUGO, proc_lstats_operations),
3450 #ifdef CONFIG_PROC_PID_CPUSET
3451 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3453 #ifdef CONFIG_CGROUPS
3454 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3456 INF("oom_score", S_IRUGO, proc_oom_score),
3457 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3458 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3459 #ifdef CONFIG_AUDITSYSCALL
3460 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3461 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3463 #ifdef CONFIG_FAULT_INJECTION
3464 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3466 #ifdef CONFIG_TASK_IO_ACCOUNTING
3467 INF("io", S_IRUSR, proc_tid_io_accounting),
3469 #ifdef CONFIG_HARDWALL
3470 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3474 static int proc_tid_base_readdir(struct file * filp,
3475 void * dirent, filldir_t filldir)
3477 return proc_pident_readdir(filp,dirent,filldir,
3478 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3481 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3482 return proc_pident_lookup(dir, dentry,
3483 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3486 static const struct file_operations proc_tid_base_operations = {
3487 .read = generic_read_dir,
3488 .readdir = proc_tid_base_readdir,
3489 .llseek = default_llseek,
3492 static const struct inode_operations proc_tid_base_inode_operations = {
3493 .lookup = proc_tid_base_lookup,
3494 .getattr = pid_getattr,
3495 .setattr = proc_setattr,
3498 static struct dentry *proc_task_instantiate(struct inode *dir,
3499 struct dentry *dentry, struct task_struct *task, const void *ptr)
3501 struct dentry *error = ERR_PTR(-ENOENT);
3502 struct inode *inode;
3503 inode = proc_pid_make_inode(dir->i_sb, task);
3507 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3508 inode->i_op = &proc_tid_base_inode_operations;
3509 inode->i_fop = &proc_tid_base_operations;
3510 inode->i_flags|=S_IMMUTABLE;
3512 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3513 ARRAY_SIZE(tid_base_stuff)));
3515 d_set_d_op(dentry, &pid_dentry_operations);
3517 d_add(dentry, inode);
3518 /* Close the race of the process dying before we return the dentry */
3519 if (pid_revalidate(dentry, NULL))
3525 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3527 struct dentry *result = ERR_PTR(-ENOENT);
3528 struct task_struct *task;
3529 struct task_struct *leader = get_proc_task(dir);
3531 struct pid_namespace *ns;
3536 tid = name_to_int(dentry);
3540 ns = dentry->d_sb->s_fs_info;
3542 task = find_task_by_pid_ns(tid, ns);
3544 get_task_struct(task);
3548 if (!same_thread_group(leader, task))
3551 result = proc_task_instantiate(dir, dentry, task, NULL);
3553 put_task_struct(task);
3555 put_task_struct(leader);
3561 * Find the first tid of a thread group to return to user space.
3563 * Usually this is just the thread group leader, but if the users
3564 * buffer was too small or there was a seek into the middle of the
3565 * directory we have more work todo.
3567 * In the case of a short read we start with find_task_by_pid.
3569 * In the case of a seek we start with the leader and walk nr
3572 static struct task_struct *first_tid(struct task_struct *leader,
3573 int tid, int nr, struct pid_namespace *ns)
3575 struct task_struct *pos;
3578 /* Attempt to start with the pid of a thread */
3579 if (tid && (nr > 0)) {
3580 pos = find_task_by_pid_ns(tid, ns);
3581 if (pos && (pos->group_leader == leader))
3585 /* If nr exceeds the number of threads there is nothing todo */
3587 if (nr && nr >= get_nr_threads(leader))
3590 /* If we haven't found our starting place yet start
3591 * with the leader and walk nr threads forward.
3593 for (pos = leader; nr > 0; --nr) {
3594 pos = next_thread(pos);
3595 if (pos == leader) {
3601 get_task_struct(pos);
3608 * Find the next thread in the thread list.
3609 * Return NULL if there is an error or no next thread.
3611 * The reference to the input task_struct is released.
3613 static struct task_struct *next_tid(struct task_struct *start)
3615 struct task_struct *pos = NULL;
3617 if (pid_alive(start)) {
3618 pos = next_thread(start);
3619 if (thread_group_leader(pos))
3622 get_task_struct(pos);
3625 put_task_struct(start);
3629 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3630 struct task_struct *task, int tid)
3632 char name[PROC_NUMBUF];
3633 int len = snprintf(name, sizeof(name), "%d", tid);
3634 return proc_fill_cache(filp, dirent, filldir, name, len,
3635 proc_task_instantiate, task, NULL);
3638 /* for the /proc/TGID/task/ directories */
3639 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3641 struct dentry *dentry = filp->f_path.dentry;
3642 struct inode *inode = dentry->d_inode;
3643 struct task_struct *leader = NULL;
3644 struct task_struct *task;
3645 int retval = -ENOENT;
3648 struct pid_namespace *ns;
3650 task = get_proc_task(inode);
3654 if (pid_alive(task)) {
3655 leader = task->group_leader;
3656 get_task_struct(leader);
3659 put_task_struct(task);
3664 switch ((unsigned long)filp->f_pos) {
3667 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3672 ino = parent_ino(dentry);
3673 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3679 /* f_version caches the tgid value that the last readdir call couldn't
3680 * return. lseek aka telldir automagically resets f_version to 0.
3682 ns = filp->f_dentry->d_sb->s_fs_info;
3683 tid = (int)filp->f_version;
3684 filp->f_version = 0;
3685 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3687 task = next_tid(task), filp->f_pos++) {
3688 tid = task_pid_nr_ns(task, ns);
3689 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3690 /* returning this tgid failed, save it as the first
3691 * pid for the next readir call */
3692 filp->f_version = (u64)tid;
3693 put_task_struct(task);
3698 put_task_struct(leader);
3703 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3705 struct inode *inode = dentry->d_inode;
3706 struct task_struct *p = get_proc_task(inode);
3707 generic_fillattr(inode, stat);
3710 stat->nlink += get_nr_threads(p);
3717 static const struct inode_operations proc_task_inode_operations = {
3718 .lookup = proc_task_lookup,
3719 .getattr = proc_task_getattr,
3720 .setattr = proc_setattr,
3723 static const struct file_operations proc_task_operations = {
3724 .read = generic_read_dir,
3725 .readdir = proc_task_readdir,
3726 .llseek = default_llseek,