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>
89 * Implementing inode permission operations in /proc is almost
90 * certainly an error. Permission checks need to happen during
91 * each system call not at open time. The reason is that most of
92 * what we wish to check for permissions in /proc varies at runtime.
94 * The classic example of a problem is opening file descriptors
95 * in /proc for a task before it execs a suid executable.
102 const struct inode_operations *iop;
103 const struct file_operations *fop;
107 #define NOD(NAME, MODE, IOP, FOP, OP) { \
109 .len = sizeof(NAME) - 1, \
116 #define DIR(NAME, MODE, iops, fops) \
117 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118 #define LNK(NAME, get_link) \
119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
120 &proc_pid_link_inode_operations, NULL, \
121 { .proc_get_link = get_link } )
122 #define REG(NAME, MODE, fops) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124 #define INF(NAME, MODE, read) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_info_file_operations, \
127 { .proc_read = read } )
128 #define ONE(NAME, MODE, show) \
129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_single_file_operations, \
131 { .proc_show = show } )
134 * Count the number of hardlinks for the pid_entry table, excluding the .
137 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
144 for (i = 0; i < n; ++i) {
145 if (S_ISDIR(entries[i].mode))
152 static int get_fs_path(struct task_struct *task, struct path *path, bool root)
154 struct fs_struct *fs;
155 int result = -ENOENT;
160 read_lock(&fs->lock);
161 *path = root ? fs->root : fs->pwd;
163 read_unlock(&fs->lock);
170 static int proc_cwd_link(struct inode *inode, struct path *path)
172 struct task_struct *task = get_proc_task(inode);
173 int result = -ENOENT;
176 result = get_fs_path(task, path, 0);
177 put_task_struct(task);
182 static int proc_root_link(struct inode *inode, struct path *path)
184 struct task_struct *task = get_proc_task(inode);
185 int result = -ENOENT;
188 result = get_fs_path(task, path, 1);
189 put_task_struct(task);
195 * Return zero if current may access user memory in @task, -error if not.
197 static int check_mem_permission(struct task_struct *task)
200 * A task can always look at itself, in case it chooses
201 * to use system calls instead of load instructions.
207 * If current is actively ptrace'ing, and would also be
208 * permitted to freshly attach with ptrace now, permit it.
210 if (task_is_stopped_or_traced(task)) {
213 match = (tracehook_tracer_task(task) == current);
215 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
220 * Noone else is allowed.
225 struct mm_struct *mm_for_maps(struct task_struct *task)
227 struct mm_struct *mm;
229 if (mutex_lock_killable(&task->cred_guard_mutex))
232 mm = get_task_mm(task);
233 if (mm && mm != current->mm &&
234 !ptrace_may_access(task, PTRACE_MODE_READ)) {
238 mutex_unlock(&task->cred_guard_mutex);
243 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
247 struct mm_struct *mm = get_task_mm(task);
251 goto out_mm; /* Shh! No looking before we're done */
253 len = mm->arg_end - mm->arg_start;
258 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
260 // If the nul at the end of args has been overwritten, then
261 // assume application is using setproctitle(3).
262 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
263 len = strnlen(buffer, res);
267 len = mm->env_end - mm->env_start;
268 if (len > PAGE_SIZE - res)
269 len = PAGE_SIZE - res;
270 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
271 res = strnlen(buffer, res);
280 static int proc_pid_auxv(struct task_struct *task, char *buffer)
283 struct mm_struct *mm = get_task_mm(task);
285 unsigned int nwords = 0;
288 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
289 res = nwords * sizeof(mm->saved_auxv[0]);
292 memcpy(buffer, mm->saved_auxv, res);
299 #ifdef CONFIG_KALLSYMS
301 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
302 * Returns the resolved symbol. If that fails, simply return the address.
304 static int proc_pid_wchan(struct task_struct *task, char *buffer)
307 char symname[KSYM_NAME_LEN];
309 wchan = get_wchan(task);
311 if (lookup_symbol_name(wchan, symname) < 0)
312 if (!ptrace_may_access(task, PTRACE_MODE_READ))
315 return sprintf(buffer, "%lu", wchan);
317 return sprintf(buffer, "%s", symname);
319 #endif /* CONFIG_KALLSYMS */
321 #ifdef CONFIG_STACKTRACE
323 #define MAX_STACK_TRACE_DEPTH 64
325 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
326 struct pid *pid, struct task_struct *task)
328 struct stack_trace trace;
329 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;
340 save_stack_trace_tsk(task, &trace);
342 for (i = 0; i < trace.nr_entries; i++) {
343 seq_printf(m, "[<%p>] %pS\n",
344 (void *)entries[i], (void *)entries[i]);
352 #ifdef CONFIG_SCHEDSTATS
354 * Provides /proc/PID/schedstat
356 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
358 return sprintf(buffer, "%llu %llu %lu\n",
359 (unsigned long long)task->se.sum_exec_runtime,
360 (unsigned long long)task->sched_info.run_delay,
361 task->sched_info.pcount);
365 #ifdef CONFIG_LATENCYTOP
366 static int lstats_show_proc(struct seq_file *m, void *v)
369 struct inode *inode = m->private;
370 struct task_struct *task = get_proc_task(inode);
374 seq_puts(m, "Latency Top version : v0.1\n");
375 for (i = 0; i < 32; i++) {
376 if (task->latency_record[i].backtrace[0]) {
378 seq_printf(m, "%i %li %li ",
379 task->latency_record[i].count,
380 task->latency_record[i].time,
381 task->latency_record[i].max);
382 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
383 char sym[KSYM_SYMBOL_LEN];
385 if (!task->latency_record[i].backtrace[q])
387 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
389 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
390 c = strchr(sym, '+');
393 seq_printf(m, "%s ", sym);
399 put_task_struct(task);
403 static int lstats_open(struct inode *inode, struct file *file)
405 return single_open(file, lstats_show_proc, inode);
408 static ssize_t lstats_write(struct file *file, const char __user *buf,
409 size_t count, loff_t *offs)
411 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
415 clear_all_latency_tracing(task);
416 put_task_struct(task);
421 static const struct file_operations proc_lstats_operations = {
424 .write = lstats_write,
426 .release = single_release,
431 static int proc_oom_score(struct task_struct *task, char *buffer)
433 unsigned long points = 0;
435 read_lock(&tasklist_lock);
437 points = oom_badness(task, NULL, NULL,
438 totalram_pages + total_swap_pages);
439 read_unlock(&tasklist_lock);
440 return sprintf(buffer, "%lu\n", points);
448 static const struct limit_names lnames[RLIM_NLIMITS] = {
449 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
450 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
451 [RLIMIT_DATA] = {"Max data size", "bytes"},
452 [RLIMIT_STACK] = {"Max stack size", "bytes"},
453 [RLIMIT_CORE] = {"Max core file size", "bytes"},
454 [RLIMIT_RSS] = {"Max resident set", "bytes"},
455 [RLIMIT_NPROC] = {"Max processes", "processes"},
456 [RLIMIT_NOFILE] = {"Max open files", "files"},
457 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
458 [RLIMIT_AS] = {"Max address space", "bytes"},
459 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
460 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
461 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
462 [RLIMIT_NICE] = {"Max nice priority", NULL},
463 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
464 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
467 /* Display limits for a process */
468 static int proc_pid_limits(struct task_struct *task, char *buffer)
473 char *bufptr = buffer;
475 struct rlimit rlim[RLIM_NLIMITS];
477 if (!lock_task_sighand(task, &flags))
479 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480 unlock_task_sighand(task, &flags);
483 * print the file header
485 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
488 for (i = 0; i < RLIM_NLIMITS; i++) {
489 if (rlim[i].rlim_cur == RLIM_INFINITY)
490 count += sprintf(&bufptr[count], "%-25s %-20s ",
491 lnames[i].name, "unlimited");
493 count += sprintf(&bufptr[count], "%-25s %-20lu ",
494 lnames[i].name, rlim[i].rlim_cur);
496 if (rlim[i].rlim_max == RLIM_INFINITY)
497 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
499 count += sprintf(&bufptr[count], "%-20lu ",
503 count += sprintf(&bufptr[count], "%-10s\n",
506 count += sprintf(&bufptr[count], "\n");
512 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513 static int proc_pid_syscall(struct task_struct *task, char *buffer)
516 unsigned long args[6], sp, pc;
518 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
519 return sprintf(buffer, "running\n");
522 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
524 return sprintf(buffer,
525 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
527 args[0], args[1], args[2], args[3], args[4], args[5],
530 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
532 /************************************************************************/
533 /* Here the fs part begins */
534 /************************************************************************/
536 /* permission checks */
537 static int proc_fd_access_allowed(struct inode *inode)
539 struct task_struct *task;
541 /* Allow access to a task's file descriptors if it is us or we
542 * may use ptrace attach to the process and find out that
545 task = get_proc_task(inode);
547 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
548 put_task_struct(task);
553 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
556 struct inode *inode = dentry->d_inode;
558 if (attr->ia_valid & ATTR_MODE)
561 error = inode_change_ok(inode, attr);
563 error = inode_setattr(inode, attr);
567 static const struct inode_operations proc_def_inode_operations = {
568 .setattr = proc_setattr,
571 static int mounts_open_common(struct inode *inode, struct file *file,
572 const struct seq_operations *op)
574 struct task_struct *task = get_proc_task(inode);
576 struct mnt_namespace *ns = NULL;
578 struct proc_mounts *p;
583 nsp = task_nsproxy(task);
590 if (ns && get_fs_path(task, &root, 1) == 0)
592 put_task_struct(task);
601 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
605 file->private_data = &p->m;
606 ret = seq_open(file, op);
613 p->event = ns->event;
627 static int mounts_release(struct inode *inode, struct file *file)
629 struct proc_mounts *p = file->private_data;
632 return seq_release(inode, file);
635 static unsigned mounts_poll(struct file *file, poll_table *wait)
637 struct proc_mounts *p = file->private_data;
638 unsigned res = POLLIN | POLLRDNORM;
640 poll_wait(file, &p->ns->poll, wait);
641 if (mnt_had_events(p))
642 res |= POLLERR | POLLPRI;
647 static int mounts_open(struct inode *inode, struct file *file)
649 return mounts_open_common(inode, file, &mounts_op);
652 static const struct file_operations proc_mounts_operations = {
656 .release = mounts_release,
660 static int mountinfo_open(struct inode *inode, struct file *file)
662 return mounts_open_common(inode, file, &mountinfo_op);
665 static const struct file_operations proc_mountinfo_operations = {
666 .open = mountinfo_open,
669 .release = mounts_release,
673 static int mountstats_open(struct inode *inode, struct file *file)
675 return mounts_open_common(inode, file, &mountstats_op);
678 static const struct file_operations proc_mountstats_operations = {
679 .open = mountstats_open,
682 .release = mounts_release,
685 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
687 static ssize_t proc_info_read(struct file * file, char __user * buf,
688 size_t count, loff_t *ppos)
690 struct inode * inode = file->f_path.dentry->d_inode;
693 struct task_struct *task = get_proc_task(inode);
699 if (count > PROC_BLOCK_SIZE)
700 count = PROC_BLOCK_SIZE;
703 if (!(page = __get_free_page(GFP_TEMPORARY)))
706 length = PROC_I(inode)->op.proc_read(task, (char*)page);
709 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
712 put_task_struct(task);
717 static const struct file_operations proc_info_file_operations = {
718 .read = proc_info_read,
719 .llseek = generic_file_llseek,
722 static int proc_single_show(struct seq_file *m, void *v)
724 struct inode *inode = m->private;
725 struct pid_namespace *ns;
727 struct task_struct *task;
730 ns = inode->i_sb->s_fs_info;
731 pid = proc_pid(inode);
732 task = get_pid_task(pid, PIDTYPE_PID);
736 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
738 put_task_struct(task);
742 static int proc_single_open(struct inode *inode, struct file *filp)
745 ret = single_open(filp, proc_single_show, NULL);
747 struct seq_file *m = filp->private_data;
754 static const struct file_operations proc_single_file_operations = {
755 .open = proc_single_open,
758 .release = single_release,
761 static int mem_open(struct inode* inode, struct file* file)
763 file->private_data = (void*)((long)current->self_exec_id);
767 static ssize_t mem_read(struct file * file, char __user * buf,
768 size_t count, loff_t *ppos)
770 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
772 unsigned long src = *ppos;
774 struct mm_struct *mm;
779 if (check_mem_permission(task))
783 page = (char *)__get_free_page(GFP_TEMPORARY);
789 mm = get_task_mm(task);
795 if (file->private_data != (void*)((long)current->self_exec_id))
801 int this_len, retval;
803 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
804 retval = access_process_vm(task, src, page, this_len, 0);
805 if (!retval || check_mem_permission(task)) {
811 if (copy_to_user(buf, page, retval)) {
826 free_page((unsigned long) page);
828 put_task_struct(task);
833 #define mem_write NULL
836 /* This is a security hazard */
837 static ssize_t mem_write(struct file * file, const char __user *buf,
838 size_t count, loff_t *ppos)
842 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
843 unsigned long dst = *ppos;
849 if (check_mem_permission(task))
853 page = (char *)__get_free_page(GFP_TEMPORARY);
859 int this_len, retval;
861 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
862 if (copy_from_user(page, buf, this_len)) {
866 retval = access_process_vm(task, dst, page, this_len, 1);
878 free_page((unsigned long) page);
880 put_task_struct(task);
886 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
890 file->f_pos = offset;
893 file->f_pos += offset;
898 force_successful_syscall_return();
902 static const struct file_operations proc_mem_operations = {
909 static ssize_t environ_read(struct file *file, char __user *buf,
910 size_t count, loff_t *ppos)
912 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
914 unsigned long src = *ppos;
916 struct mm_struct *mm;
921 if (!ptrace_may_access(task, PTRACE_MODE_READ))
925 page = (char *)__get_free_page(GFP_TEMPORARY);
931 mm = get_task_mm(task);
936 int this_len, retval, max_len;
938 this_len = mm->env_end - (mm->env_start + src);
943 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
944 this_len = (this_len > max_len) ? max_len : this_len;
946 retval = access_process_vm(task, (mm->env_start + src),
954 if (copy_to_user(buf, page, retval)) {
968 free_page((unsigned long) page);
970 put_task_struct(task);
975 static const struct file_operations proc_environ_operations = {
976 .read = environ_read,
977 .llseek = generic_file_llseek,
980 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
981 size_t count, loff_t *ppos)
983 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
984 char buffer[PROC_NUMBUF];
986 int oom_adjust = OOM_DISABLE;
992 if (lock_task_sighand(task, &flags)) {
993 oom_adjust = task->signal->oom_adj;
994 unlock_task_sighand(task, &flags);
997 put_task_struct(task);
999 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1001 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1004 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1005 size_t count, loff_t *ppos)
1007 struct task_struct *task;
1008 char buffer[PROC_NUMBUF];
1010 unsigned long flags;
1013 memset(buffer, 0, sizeof(buffer));
1014 if (count > sizeof(buffer) - 1)
1015 count = sizeof(buffer) - 1;
1016 if (copy_from_user(buffer, buf, count))
1019 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1022 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1023 oom_adjust != OOM_DISABLE)
1026 task = get_proc_task(file->f_path.dentry->d_inode);
1029 if (!lock_task_sighand(task, &flags)) {
1030 put_task_struct(task);
1034 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1035 unlock_task_sighand(task, &flags);
1036 put_task_struct(task);
1041 * Warn that /proc/pid/oom_adj is deprecated, see
1042 * Documentation/feature-removal-schedule.txt.
1044 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1045 "please use /proc/%d/oom_score_adj instead.\n",
1046 current->comm, task_pid_nr(current),
1047 task_pid_nr(task), task_pid_nr(task));
1048 task->signal->oom_adj = oom_adjust;
1050 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1051 * value is always attainable.
1053 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1054 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1056 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1058 unlock_task_sighand(task, &flags);
1059 put_task_struct(task);
1064 static const struct file_operations proc_oom_adjust_operations = {
1065 .read = oom_adjust_read,
1066 .write = oom_adjust_write,
1067 .llseek = generic_file_llseek,
1070 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1071 size_t count, loff_t *ppos)
1073 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1074 char buffer[PROC_NUMBUF];
1075 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1076 unsigned long flags;
1081 if (lock_task_sighand(task, &flags)) {
1082 oom_score_adj = task->signal->oom_score_adj;
1083 unlock_task_sighand(task, &flags);
1085 put_task_struct(task);
1086 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1087 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1090 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1091 size_t count, loff_t *ppos)
1093 struct task_struct *task;
1094 char buffer[PROC_NUMBUF];
1095 unsigned long flags;
1099 memset(buffer, 0, sizeof(buffer));
1100 if (count > sizeof(buffer) - 1)
1101 count = sizeof(buffer) - 1;
1102 if (copy_from_user(buffer, buf, count))
1105 err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1108 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1109 oom_score_adj > OOM_SCORE_ADJ_MAX)
1112 task = get_proc_task(file->f_path.dentry->d_inode);
1115 if (!lock_task_sighand(task, &flags)) {
1116 put_task_struct(task);
1119 if (oom_score_adj < task->signal->oom_score_adj &&
1120 !capable(CAP_SYS_RESOURCE)) {
1121 unlock_task_sighand(task, &flags);
1122 put_task_struct(task);
1126 task->signal->oom_score_adj = oom_score_adj;
1128 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1129 * always attainable.
1131 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1132 task->signal->oom_adj = OOM_DISABLE;
1134 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1136 unlock_task_sighand(task, &flags);
1137 put_task_struct(task);
1141 static const struct file_operations proc_oom_score_adj_operations = {
1142 .read = oom_score_adj_read,
1143 .write = oom_score_adj_write,
1146 #ifdef CONFIG_AUDITSYSCALL
1147 #define TMPBUFLEN 21
1148 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1149 size_t count, loff_t *ppos)
1151 struct inode * inode = file->f_path.dentry->d_inode;
1152 struct task_struct *task = get_proc_task(inode);
1154 char tmpbuf[TMPBUFLEN];
1158 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1159 audit_get_loginuid(task));
1160 put_task_struct(task);
1161 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1164 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1165 size_t count, loff_t *ppos)
1167 struct inode * inode = file->f_path.dentry->d_inode;
1172 if (!capable(CAP_AUDIT_CONTROL))
1176 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1182 if (count >= PAGE_SIZE)
1183 count = PAGE_SIZE - 1;
1186 /* No partial writes. */
1189 page = (char*)__get_free_page(GFP_TEMPORARY);
1193 if (copy_from_user(page, buf, count))
1197 loginuid = simple_strtoul(page, &tmp, 10);
1203 length = audit_set_loginuid(current, loginuid);
1204 if (likely(length == 0))
1208 free_page((unsigned long) page);
1212 static const struct file_operations proc_loginuid_operations = {
1213 .read = proc_loginuid_read,
1214 .write = proc_loginuid_write,
1215 .llseek = generic_file_llseek,
1218 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1219 size_t count, loff_t *ppos)
1221 struct inode * inode = file->f_path.dentry->d_inode;
1222 struct task_struct *task = get_proc_task(inode);
1224 char tmpbuf[TMPBUFLEN];
1228 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1229 audit_get_sessionid(task));
1230 put_task_struct(task);
1231 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1234 static const struct file_operations proc_sessionid_operations = {
1235 .read = proc_sessionid_read,
1236 .llseek = generic_file_llseek,
1240 #ifdef CONFIG_FAULT_INJECTION
1241 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1242 size_t count, loff_t *ppos)
1244 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1245 char buffer[PROC_NUMBUF];
1251 make_it_fail = task->make_it_fail;
1252 put_task_struct(task);
1254 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1256 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1259 static ssize_t proc_fault_inject_write(struct file * file,
1260 const char __user * buf, size_t count, loff_t *ppos)
1262 struct task_struct *task;
1263 char buffer[PROC_NUMBUF], *end;
1266 if (!capable(CAP_SYS_RESOURCE))
1268 memset(buffer, 0, sizeof(buffer));
1269 if (count > sizeof(buffer) - 1)
1270 count = sizeof(buffer) - 1;
1271 if (copy_from_user(buffer, buf, count))
1273 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1276 task = get_proc_task(file->f_dentry->d_inode);
1279 task->make_it_fail = make_it_fail;
1280 put_task_struct(task);
1285 static const struct file_operations proc_fault_inject_operations = {
1286 .read = proc_fault_inject_read,
1287 .write = proc_fault_inject_write,
1288 .llseek = generic_file_llseek,
1293 #ifdef CONFIG_SCHED_DEBUG
1295 * Print out various scheduling related per-task fields:
1297 static int sched_show(struct seq_file *m, void *v)
1299 struct inode *inode = m->private;
1300 struct task_struct *p;
1302 p = get_proc_task(inode);
1305 proc_sched_show_task(p, m);
1313 sched_write(struct file *file, const char __user *buf,
1314 size_t count, loff_t *offset)
1316 struct inode *inode = file->f_path.dentry->d_inode;
1317 struct task_struct *p;
1319 p = get_proc_task(inode);
1322 proc_sched_set_task(p);
1329 static int sched_open(struct inode *inode, struct file *filp)
1333 ret = single_open(filp, sched_show, NULL);
1335 struct seq_file *m = filp->private_data;
1342 static const struct file_operations proc_pid_sched_operations = {
1345 .write = sched_write,
1346 .llseek = seq_lseek,
1347 .release = single_release,
1352 static ssize_t comm_write(struct file *file, const char __user *buf,
1353 size_t count, loff_t *offset)
1355 struct inode *inode = file->f_path.dentry->d_inode;
1356 struct task_struct *p;
1357 char buffer[TASK_COMM_LEN];
1359 memset(buffer, 0, sizeof(buffer));
1360 if (count > sizeof(buffer) - 1)
1361 count = sizeof(buffer) - 1;
1362 if (copy_from_user(buffer, buf, count))
1365 p = get_proc_task(inode);
1369 if (same_thread_group(current, p))
1370 set_task_comm(p, buffer);
1379 static int comm_show(struct seq_file *m, void *v)
1381 struct inode *inode = m->private;
1382 struct task_struct *p;
1384 p = get_proc_task(inode);
1389 seq_printf(m, "%s\n", p->comm);
1397 static int comm_open(struct inode *inode, struct file *filp)
1401 ret = single_open(filp, comm_show, NULL);
1403 struct seq_file *m = filp->private_data;
1410 static const struct file_operations proc_pid_set_comm_operations = {
1413 .write = comm_write,
1414 .llseek = seq_lseek,
1415 .release = single_release,
1419 * We added or removed a vma mapping the executable. The vmas are only mapped
1420 * during exec and are not mapped with the mmap system call.
1421 * Callers must hold down_write() on the mm's mmap_sem for these
1423 void added_exe_file_vma(struct mm_struct *mm)
1425 mm->num_exe_file_vmas++;
1428 void removed_exe_file_vma(struct mm_struct *mm)
1430 mm->num_exe_file_vmas--;
1431 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1433 mm->exe_file = NULL;
1438 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1441 get_file(new_exe_file);
1444 mm->exe_file = new_exe_file;
1445 mm->num_exe_file_vmas = 0;
1448 struct file *get_mm_exe_file(struct mm_struct *mm)
1450 struct file *exe_file;
1452 /* We need mmap_sem to protect against races with removal of
1453 * VM_EXECUTABLE vmas */
1454 down_read(&mm->mmap_sem);
1455 exe_file = mm->exe_file;
1458 up_read(&mm->mmap_sem);
1462 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1464 /* It's safe to write the exe_file pointer without exe_file_lock because
1465 * this is called during fork when the task is not yet in /proc */
1466 newmm->exe_file = get_mm_exe_file(oldmm);
1469 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1471 struct task_struct *task;
1472 struct mm_struct *mm;
1473 struct file *exe_file;
1475 task = get_proc_task(inode);
1478 mm = get_task_mm(task);
1479 put_task_struct(task);
1482 exe_file = get_mm_exe_file(mm);
1485 *exe_path = exe_file->f_path;
1486 path_get(&exe_file->f_path);
1493 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1495 struct inode *inode = dentry->d_inode;
1496 int error = -EACCES;
1498 /* We don't need a base pointer in the /proc filesystem */
1499 path_put(&nd->path);
1501 /* Are we allowed to snoop on the tasks file descriptors? */
1502 if (!proc_fd_access_allowed(inode))
1505 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1507 return ERR_PTR(error);
1510 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1512 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1519 pathname = d_path(path, tmp, PAGE_SIZE);
1520 len = PTR_ERR(pathname);
1521 if (IS_ERR(pathname))
1523 len = tmp + PAGE_SIZE - 1 - pathname;
1527 if (copy_to_user(buffer, pathname, len))
1530 free_page((unsigned long)tmp);
1534 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1536 int error = -EACCES;
1537 struct inode *inode = dentry->d_inode;
1540 /* Are we allowed to snoop on the tasks file descriptors? */
1541 if (!proc_fd_access_allowed(inode))
1544 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1548 error = do_proc_readlink(&path, buffer, buflen);
1554 static const struct inode_operations proc_pid_link_inode_operations = {
1555 .readlink = proc_pid_readlink,
1556 .follow_link = proc_pid_follow_link,
1557 .setattr = proc_setattr,
1561 /* building an inode */
1563 static int task_dumpable(struct task_struct *task)
1566 struct mm_struct *mm;
1571 dumpable = get_dumpable(mm);
1579 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1581 struct inode * inode;
1582 struct proc_inode *ei;
1583 const struct cred *cred;
1585 /* We need a new inode */
1587 inode = new_inode(sb);
1593 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1594 inode->i_op = &proc_def_inode_operations;
1597 * grab the reference to task.
1599 ei->pid = get_task_pid(task, PIDTYPE_PID);
1603 if (task_dumpable(task)) {
1605 cred = __task_cred(task);
1606 inode->i_uid = cred->euid;
1607 inode->i_gid = cred->egid;
1610 security_task_to_inode(task, inode);
1620 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1622 struct inode *inode = dentry->d_inode;
1623 struct task_struct *task;
1624 const struct cred *cred;
1626 generic_fillattr(inode, stat);
1631 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1633 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1634 task_dumpable(task)) {
1635 cred = __task_cred(task);
1636 stat->uid = cred->euid;
1637 stat->gid = cred->egid;
1647 * Exceptional case: normally we are not allowed to unhash a busy
1648 * directory. In this case, however, we can do it - no aliasing problems
1649 * due to the way we treat inodes.
1651 * Rewrite the inode's ownerships here because the owning task may have
1652 * performed a setuid(), etc.
1654 * Before the /proc/pid/status file was created the only way to read
1655 * the effective uid of a /process was to stat /proc/pid. Reading
1656 * /proc/pid/status is slow enough that procps and other packages
1657 * kept stating /proc/pid. To keep the rules in /proc simple I have
1658 * made this apply to all per process world readable and executable
1661 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1663 struct inode *inode = dentry->d_inode;
1664 struct task_struct *task = get_proc_task(inode);
1665 const struct cred *cred;
1668 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1669 task_dumpable(task)) {
1671 cred = __task_cred(task);
1672 inode->i_uid = cred->euid;
1673 inode->i_gid = cred->egid;
1679 inode->i_mode &= ~(S_ISUID | S_ISGID);
1680 security_task_to_inode(task, inode);
1681 put_task_struct(task);
1688 static int pid_delete_dentry(struct dentry * dentry)
1690 /* Is the task we represent dead?
1691 * If so, then don't put the dentry on the lru list,
1692 * kill it immediately.
1694 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1697 static const struct dentry_operations pid_dentry_operations =
1699 .d_revalidate = pid_revalidate,
1700 .d_delete = pid_delete_dentry,
1705 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1706 struct task_struct *, const void *);
1709 * Fill a directory entry.
1711 * If possible create the dcache entry and derive our inode number and
1712 * file type from dcache entry.
1714 * Since all of the proc inode numbers are dynamically generated, the inode
1715 * numbers do not exist until the inode is cache. This means creating the
1716 * the dcache entry in readdir is necessary to keep the inode numbers
1717 * reported by readdir in sync with the inode numbers reported
1720 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1721 char *name, int len,
1722 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1724 struct dentry *child, *dir = filp->f_path.dentry;
1725 struct inode *inode;
1728 unsigned type = DT_UNKNOWN;
1732 qname.hash = full_name_hash(name, len);
1734 child = d_lookup(dir, &qname);
1737 new = d_alloc(dir, &qname);
1739 child = instantiate(dir->d_inode, new, task, ptr);
1746 if (!child || IS_ERR(child) || !child->d_inode)
1747 goto end_instantiate;
1748 inode = child->d_inode;
1751 type = inode->i_mode >> 12;
1756 ino = find_inode_number(dir, &qname);
1759 return filldir(dirent, name, len, filp->f_pos, ino, type);
1762 static unsigned name_to_int(struct dentry *dentry)
1764 const char *name = dentry->d_name.name;
1765 int len = dentry->d_name.len;
1768 if (len > 1 && *name == '0')
1771 unsigned c = *name++ - '0';
1774 if (n >= (~0U-9)/10)
1784 #define PROC_FDINFO_MAX 64
1786 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1788 struct task_struct *task = get_proc_task(inode);
1789 struct files_struct *files = NULL;
1791 int fd = proc_fd(inode);
1794 files = get_files_struct(task);
1795 put_task_struct(task);
1799 * We are not taking a ref to the file structure, so we must
1802 spin_lock(&files->file_lock);
1803 file = fcheck_files(files, fd);
1806 *path = file->f_path;
1807 path_get(&file->f_path);
1810 snprintf(info, PROC_FDINFO_MAX,
1813 (long long) file->f_pos,
1815 spin_unlock(&files->file_lock);
1816 put_files_struct(files);
1819 spin_unlock(&files->file_lock);
1820 put_files_struct(files);
1825 static int proc_fd_link(struct inode *inode, struct path *path)
1827 return proc_fd_info(inode, path, NULL);
1830 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1832 struct inode *inode = dentry->d_inode;
1833 struct task_struct *task = get_proc_task(inode);
1834 int fd = proc_fd(inode);
1835 struct files_struct *files;
1836 const struct cred *cred;
1839 files = get_files_struct(task);
1842 if (fcheck_files(files, fd)) {
1844 put_files_struct(files);
1845 if (task_dumpable(task)) {
1847 cred = __task_cred(task);
1848 inode->i_uid = cred->euid;
1849 inode->i_gid = cred->egid;
1855 inode->i_mode &= ~(S_ISUID | S_ISGID);
1856 security_task_to_inode(task, inode);
1857 put_task_struct(task);
1861 put_files_struct(files);
1863 put_task_struct(task);
1869 static const struct dentry_operations tid_fd_dentry_operations =
1871 .d_revalidate = tid_fd_revalidate,
1872 .d_delete = pid_delete_dentry,
1875 static struct dentry *proc_fd_instantiate(struct inode *dir,
1876 struct dentry *dentry, struct task_struct *task, const void *ptr)
1878 unsigned fd = *(const unsigned *)ptr;
1880 struct files_struct *files;
1881 struct inode *inode;
1882 struct proc_inode *ei;
1883 struct dentry *error = ERR_PTR(-ENOENT);
1885 inode = proc_pid_make_inode(dir->i_sb, task);
1890 files = get_files_struct(task);
1893 inode->i_mode = S_IFLNK;
1896 * We are not taking a ref to the file structure, so we must
1899 spin_lock(&files->file_lock);
1900 file = fcheck_files(files, fd);
1903 if (file->f_mode & FMODE_READ)
1904 inode->i_mode |= S_IRUSR | S_IXUSR;
1905 if (file->f_mode & FMODE_WRITE)
1906 inode->i_mode |= S_IWUSR | S_IXUSR;
1907 spin_unlock(&files->file_lock);
1908 put_files_struct(files);
1910 inode->i_op = &proc_pid_link_inode_operations;
1912 ei->op.proc_get_link = proc_fd_link;
1913 dentry->d_op = &tid_fd_dentry_operations;
1914 d_add(dentry, inode);
1915 /* Close the race of the process dying before we return the dentry */
1916 if (tid_fd_revalidate(dentry, NULL))
1922 spin_unlock(&files->file_lock);
1923 put_files_struct(files);
1929 static struct dentry *proc_lookupfd_common(struct inode *dir,
1930 struct dentry *dentry,
1931 instantiate_t instantiate)
1933 struct task_struct *task = get_proc_task(dir);
1934 unsigned fd = name_to_int(dentry);
1935 struct dentry *result = ERR_PTR(-ENOENT);
1942 result = instantiate(dir, dentry, task, &fd);
1944 put_task_struct(task);
1949 static int proc_readfd_common(struct file * filp, void * dirent,
1950 filldir_t filldir, instantiate_t instantiate)
1952 struct dentry *dentry = filp->f_path.dentry;
1953 struct inode *inode = dentry->d_inode;
1954 struct task_struct *p = get_proc_task(inode);
1955 unsigned int fd, ino;
1957 struct files_struct * files;
1967 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1971 ino = parent_ino(dentry);
1972 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1976 files = get_files_struct(p);
1980 for (fd = filp->f_pos-2;
1981 fd < files_fdtable(files)->max_fds;
1982 fd++, filp->f_pos++) {
1983 char name[PROC_NUMBUF];
1986 if (!fcheck_files(files, fd))
1990 len = snprintf(name, sizeof(name), "%d", fd);
1991 if (proc_fill_cache(filp, dirent, filldir,
1992 name, len, instantiate,
2000 put_files_struct(files);
2008 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2009 struct nameidata *nd)
2011 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2014 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2016 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2019 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2020 size_t len, loff_t *ppos)
2022 char tmp[PROC_FDINFO_MAX];
2023 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2025 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2029 static const struct file_operations proc_fdinfo_file_operations = {
2030 .open = nonseekable_open,
2031 .read = proc_fdinfo_read,
2034 static const struct file_operations proc_fd_operations = {
2035 .read = generic_read_dir,
2036 .readdir = proc_readfd,
2040 * /proc/pid/fd needs a special permission handler so that a process can still
2041 * access /proc/self/fd after it has executed a setuid().
2043 static int proc_fd_permission(struct inode *inode, int mask)
2047 rv = generic_permission(inode, mask, NULL);
2050 if (task_pid(current) == proc_pid(inode))
2056 * proc directories can do almost nothing..
2058 static const struct inode_operations proc_fd_inode_operations = {
2059 .lookup = proc_lookupfd,
2060 .permission = proc_fd_permission,
2061 .setattr = proc_setattr,
2064 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2065 struct dentry *dentry, struct task_struct *task, const void *ptr)
2067 unsigned fd = *(unsigned *)ptr;
2068 struct inode *inode;
2069 struct proc_inode *ei;
2070 struct dentry *error = ERR_PTR(-ENOENT);
2072 inode = proc_pid_make_inode(dir->i_sb, task);
2077 inode->i_mode = S_IFREG | S_IRUSR;
2078 inode->i_fop = &proc_fdinfo_file_operations;
2079 dentry->d_op = &tid_fd_dentry_operations;
2080 d_add(dentry, inode);
2081 /* Close the race of the process dying before we return the dentry */
2082 if (tid_fd_revalidate(dentry, NULL))
2089 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2090 struct dentry *dentry,
2091 struct nameidata *nd)
2093 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2096 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2098 return proc_readfd_common(filp, dirent, filldir,
2099 proc_fdinfo_instantiate);
2102 static const struct file_operations proc_fdinfo_operations = {
2103 .read = generic_read_dir,
2104 .readdir = proc_readfdinfo,
2108 * proc directories can do almost nothing..
2110 static const struct inode_operations proc_fdinfo_inode_operations = {
2111 .lookup = proc_lookupfdinfo,
2112 .setattr = proc_setattr,
2116 static struct dentry *proc_pident_instantiate(struct inode *dir,
2117 struct dentry *dentry, struct task_struct *task, const void *ptr)
2119 const struct pid_entry *p = ptr;
2120 struct inode *inode;
2121 struct proc_inode *ei;
2122 struct dentry *error = ERR_PTR(-ENOENT);
2124 inode = proc_pid_make_inode(dir->i_sb, task);
2129 inode->i_mode = p->mode;
2130 if (S_ISDIR(inode->i_mode))
2131 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2133 inode->i_op = p->iop;
2135 inode->i_fop = p->fop;
2137 dentry->d_op = &pid_dentry_operations;
2138 d_add(dentry, inode);
2139 /* Close the race of the process dying before we return the dentry */
2140 if (pid_revalidate(dentry, NULL))
2146 static struct dentry *proc_pident_lookup(struct inode *dir,
2147 struct dentry *dentry,
2148 const struct pid_entry *ents,
2151 struct dentry *error;
2152 struct task_struct *task = get_proc_task(dir);
2153 const struct pid_entry *p, *last;
2155 error = ERR_PTR(-ENOENT);
2161 * Yes, it does not scale. And it should not. Don't add
2162 * new entries into /proc/<tgid>/ without very good reasons.
2164 last = &ents[nents - 1];
2165 for (p = ents; p <= last; p++) {
2166 if (p->len != dentry->d_name.len)
2168 if (!memcmp(dentry->d_name.name, p->name, p->len))
2174 error = proc_pident_instantiate(dir, dentry, task, p);
2176 put_task_struct(task);
2181 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2182 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2184 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2185 proc_pident_instantiate, task, p);
2188 static int proc_pident_readdir(struct file *filp,
2189 void *dirent, filldir_t filldir,
2190 const struct pid_entry *ents, unsigned int nents)
2193 struct dentry *dentry = filp->f_path.dentry;
2194 struct inode *inode = dentry->d_inode;
2195 struct task_struct *task = get_proc_task(inode);
2196 const struct pid_entry *p, *last;
2209 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2215 ino = parent_ino(dentry);
2216 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2228 last = &ents[nents - 1];
2230 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2239 put_task_struct(task);
2244 #ifdef CONFIG_SECURITY
2245 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2246 size_t count, loff_t *ppos)
2248 struct inode * inode = file->f_path.dentry->d_inode;
2251 struct task_struct *task = get_proc_task(inode);
2256 length = security_getprocattr(task,
2257 (char*)file->f_path.dentry->d_name.name,
2259 put_task_struct(task);
2261 length = simple_read_from_buffer(buf, count, ppos, p, length);
2266 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2267 size_t count, loff_t *ppos)
2269 struct inode * inode = file->f_path.dentry->d_inode;
2272 struct task_struct *task = get_proc_task(inode);
2277 if (count > PAGE_SIZE)
2280 /* No partial writes. */
2286 page = (char*)__get_free_page(GFP_TEMPORARY);
2291 if (copy_from_user(page, buf, count))
2294 /* Guard against adverse ptrace interaction */
2295 length = mutex_lock_interruptible(&task->cred_guard_mutex);
2299 length = security_setprocattr(task,
2300 (char*)file->f_path.dentry->d_name.name,
2301 (void*)page, count);
2302 mutex_unlock(&task->cred_guard_mutex);
2304 free_page((unsigned long) page);
2306 put_task_struct(task);
2311 static const struct file_operations proc_pid_attr_operations = {
2312 .read = proc_pid_attr_read,
2313 .write = proc_pid_attr_write,
2314 .llseek = generic_file_llseek,
2317 static const struct pid_entry attr_dir_stuff[] = {
2318 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2319 REG("prev", S_IRUGO, proc_pid_attr_operations),
2320 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2321 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2322 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2323 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2326 static int proc_attr_dir_readdir(struct file * filp,
2327 void * dirent, filldir_t filldir)
2329 return proc_pident_readdir(filp,dirent,filldir,
2330 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2333 static const struct file_operations proc_attr_dir_operations = {
2334 .read = generic_read_dir,
2335 .readdir = proc_attr_dir_readdir,
2338 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2339 struct dentry *dentry, struct nameidata *nd)
2341 return proc_pident_lookup(dir, dentry,
2342 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2345 static const struct inode_operations proc_attr_dir_inode_operations = {
2346 .lookup = proc_attr_dir_lookup,
2347 .getattr = pid_getattr,
2348 .setattr = proc_setattr,
2353 #ifdef CONFIG_ELF_CORE
2354 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2355 size_t count, loff_t *ppos)
2357 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2358 struct mm_struct *mm;
2359 char buffer[PROC_NUMBUF];
2367 mm = get_task_mm(task);
2369 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2370 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2371 MMF_DUMP_FILTER_SHIFT));
2373 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2376 put_task_struct(task);
2381 static ssize_t proc_coredump_filter_write(struct file *file,
2382 const char __user *buf,
2386 struct task_struct *task;
2387 struct mm_struct *mm;
2388 char buffer[PROC_NUMBUF], *end;
2395 memset(buffer, 0, sizeof(buffer));
2396 if (count > sizeof(buffer) - 1)
2397 count = sizeof(buffer) - 1;
2398 if (copy_from_user(buffer, buf, count))
2402 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2405 if (end - buffer == 0)
2409 task = get_proc_task(file->f_dentry->d_inode);
2414 mm = get_task_mm(task);
2418 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2420 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2422 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2427 put_task_struct(task);
2432 static const struct file_operations proc_coredump_filter_operations = {
2433 .read = proc_coredump_filter_read,
2434 .write = proc_coredump_filter_write,
2435 .llseek = generic_file_llseek,
2442 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2445 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2446 pid_t tgid = task_tgid_nr_ns(current, ns);
2447 char tmp[PROC_NUMBUF];
2450 sprintf(tmp, "%d", tgid);
2451 return vfs_readlink(dentry,buffer,buflen,tmp);
2454 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2456 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2457 pid_t tgid = task_tgid_nr_ns(current, ns);
2458 char *name = ERR_PTR(-ENOENT);
2462 name = ERR_PTR(-ENOMEM);
2464 sprintf(name, "%d", tgid);
2466 nd_set_link(nd, name);
2470 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2473 char *s = nd_get_link(nd);
2478 static const struct inode_operations proc_self_inode_operations = {
2479 .readlink = proc_self_readlink,
2480 .follow_link = proc_self_follow_link,
2481 .put_link = proc_self_put_link,
2487 * These are the directory entries in the root directory of /proc
2488 * that properly belong to the /proc filesystem, as they describe
2489 * describe something that is process related.
2491 static const struct pid_entry proc_base_stuff[] = {
2492 NOD("self", S_IFLNK|S_IRWXUGO,
2493 &proc_self_inode_operations, NULL, {}),
2497 * Exceptional case: normally we are not allowed to unhash a busy
2498 * directory. In this case, however, we can do it - no aliasing problems
2499 * due to the way we treat inodes.
2501 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2503 struct inode *inode = dentry->d_inode;
2504 struct task_struct *task = get_proc_task(inode);
2506 put_task_struct(task);
2513 static const struct dentry_operations proc_base_dentry_operations =
2515 .d_revalidate = proc_base_revalidate,
2516 .d_delete = pid_delete_dentry,
2519 static struct dentry *proc_base_instantiate(struct inode *dir,
2520 struct dentry *dentry, struct task_struct *task, const void *ptr)
2522 const struct pid_entry *p = ptr;
2523 struct inode *inode;
2524 struct proc_inode *ei;
2525 struct dentry *error;
2527 /* Allocate the inode */
2528 error = ERR_PTR(-ENOMEM);
2529 inode = new_inode(dir->i_sb);
2533 /* Initialize the inode */
2535 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2538 * grab the reference to the task.
2540 ei->pid = get_task_pid(task, PIDTYPE_PID);
2544 inode->i_mode = p->mode;
2545 if (S_ISDIR(inode->i_mode))
2547 if (S_ISLNK(inode->i_mode))
2550 inode->i_op = p->iop;
2552 inode->i_fop = p->fop;
2554 dentry->d_op = &proc_base_dentry_operations;
2555 d_add(dentry, inode);
2564 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2566 struct dentry *error;
2567 struct task_struct *task = get_proc_task(dir);
2568 const struct pid_entry *p, *last;
2570 error = ERR_PTR(-ENOENT);
2575 /* Lookup the directory entry */
2576 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2577 for (p = proc_base_stuff; p <= last; p++) {
2578 if (p->len != dentry->d_name.len)
2580 if (!memcmp(dentry->d_name.name, p->name, p->len))
2586 error = proc_base_instantiate(dir, dentry, task, p);
2589 put_task_struct(task);
2594 static int proc_base_fill_cache(struct file *filp, void *dirent,
2595 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2597 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2598 proc_base_instantiate, task, p);
2601 #ifdef CONFIG_TASK_IO_ACCOUNTING
2602 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2604 struct task_io_accounting acct = task->ioac;
2605 unsigned long flags;
2607 if (whole && lock_task_sighand(task, &flags)) {
2608 struct task_struct *t = task;
2610 task_io_accounting_add(&acct, &task->signal->ioac);
2611 while_each_thread(task, t)
2612 task_io_accounting_add(&acct, &t->ioac);
2614 unlock_task_sighand(task, &flags);
2616 return sprintf(buffer,
2621 "read_bytes: %llu\n"
2622 "write_bytes: %llu\n"
2623 "cancelled_write_bytes: %llu\n",
2624 (unsigned long long)acct.rchar,
2625 (unsigned long long)acct.wchar,
2626 (unsigned long long)acct.syscr,
2627 (unsigned long long)acct.syscw,
2628 (unsigned long long)acct.read_bytes,
2629 (unsigned long long)acct.write_bytes,
2630 (unsigned long long)acct.cancelled_write_bytes);
2633 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2635 return do_io_accounting(task, buffer, 0);
2638 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2640 return do_io_accounting(task, buffer, 1);
2642 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2644 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2645 struct pid *pid, struct task_struct *task)
2647 seq_printf(m, "%08x\n", task->personality);
2654 static const struct file_operations proc_task_operations;
2655 static const struct inode_operations proc_task_inode_operations;
2657 static const struct pid_entry tgid_base_stuff[] = {
2658 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2659 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2660 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2662 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2664 REG("environ", S_IRUSR, proc_environ_operations),
2665 INF("auxv", S_IRUSR, proc_pid_auxv),
2666 ONE("status", S_IRUGO, proc_pid_status),
2667 ONE("personality", S_IRUSR, proc_pid_personality),
2668 INF("limits", S_IRUSR, proc_pid_limits),
2669 #ifdef CONFIG_SCHED_DEBUG
2670 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2672 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2673 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2674 INF("syscall", S_IRUSR, proc_pid_syscall),
2676 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2677 ONE("stat", S_IRUGO, proc_tgid_stat),
2678 ONE("statm", S_IRUGO, proc_pid_statm),
2679 REG("maps", S_IRUGO, proc_maps_operations),
2681 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2683 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2684 LNK("cwd", proc_cwd_link),
2685 LNK("root", proc_root_link),
2686 LNK("exe", proc_exe_link),
2687 REG("mounts", S_IRUGO, proc_mounts_operations),
2688 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2689 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2690 #ifdef CONFIG_PROC_PAGE_MONITOR
2691 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2692 REG("smaps", S_IRUGO, proc_smaps_operations),
2693 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2695 #ifdef CONFIG_SECURITY
2696 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2698 #ifdef CONFIG_KALLSYMS
2699 INF("wchan", S_IRUGO, proc_pid_wchan),
2701 #ifdef CONFIG_STACKTRACE
2702 ONE("stack", S_IRUSR, proc_pid_stack),
2704 #ifdef CONFIG_SCHEDSTATS
2705 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2707 #ifdef CONFIG_LATENCYTOP
2708 REG("latency", S_IRUGO, proc_lstats_operations),
2710 #ifdef CONFIG_PROC_PID_CPUSET
2711 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2713 #ifdef CONFIG_CGROUPS
2714 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2716 INF("oom_score", S_IRUGO, proc_oom_score),
2717 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2718 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2719 #ifdef CONFIG_AUDITSYSCALL
2720 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2721 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2723 #ifdef CONFIG_FAULT_INJECTION
2724 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2726 #ifdef CONFIG_ELF_CORE
2727 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2729 #ifdef CONFIG_TASK_IO_ACCOUNTING
2730 INF("io", S_IRUGO, proc_tgid_io_accounting),
2734 static int proc_tgid_base_readdir(struct file * filp,
2735 void * dirent, filldir_t filldir)
2737 return proc_pident_readdir(filp,dirent,filldir,
2738 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2741 static const struct file_operations proc_tgid_base_operations = {
2742 .read = generic_read_dir,
2743 .readdir = proc_tgid_base_readdir,
2746 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2747 return proc_pident_lookup(dir, dentry,
2748 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2751 static const struct inode_operations proc_tgid_base_inode_operations = {
2752 .lookup = proc_tgid_base_lookup,
2753 .getattr = pid_getattr,
2754 .setattr = proc_setattr,
2757 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2759 struct dentry *dentry, *leader, *dir;
2760 char buf[PROC_NUMBUF];
2764 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2765 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2767 shrink_dcache_parent(dentry);
2773 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2774 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2779 name.len = strlen(name.name);
2780 dir = d_hash_and_lookup(leader, &name);
2782 goto out_put_leader;
2785 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2786 dentry = d_hash_and_lookup(dir, &name);
2788 shrink_dcache_parent(dentry);
2801 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2802 * @task: task that should be flushed.
2804 * When flushing dentries from proc, one needs to flush them from global
2805 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2806 * in. This call is supposed to do all of this job.
2808 * Looks in the dcache for
2810 * /proc/@tgid/task/@pid
2811 * if either directory is present flushes it and all of it'ts children
2814 * It is safe and reasonable to cache /proc entries for a task until
2815 * that task exits. After that they just clog up the dcache with
2816 * useless entries, possibly causing useful dcache entries to be
2817 * flushed instead. This routine is proved to flush those useless
2818 * dcache entries at process exit time.
2820 * NOTE: This routine is just an optimization so it does not guarantee
2821 * that no dcache entries will exist at process exit time it
2822 * just makes it very unlikely that any will persist.
2825 void proc_flush_task(struct task_struct *task)
2828 struct pid *pid, *tgid;
2831 pid = task_pid(task);
2832 tgid = task_tgid(task);
2834 for (i = 0; i <= pid->level; i++) {
2835 upid = &pid->numbers[i];
2836 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2837 tgid->numbers[i].nr);
2840 upid = &pid->numbers[pid->level];
2842 pid_ns_release_proc(upid->ns);
2845 static struct dentry *proc_pid_instantiate(struct inode *dir,
2846 struct dentry * dentry,
2847 struct task_struct *task, const void *ptr)
2849 struct dentry *error = ERR_PTR(-ENOENT);
2850 struct inode *inode;
2852 inode = proc_pid_make_inode(dir->i_sb, task);
2856 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2857 inode->i_op = &proc_tgid_base_inode_operations;
2858 inode->i_fop = &proc_tgid_base_operations;
2859 inode->i_flags|=S_IMMUTABLE;
2861 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2862 ARRAY_SIZE(tgid_base_stuff));
2864 dentry->d_op = &pid_dentry_operations;
2866 d_add(dentry, inode);
2867 /* Close the race of the process dying before we return the dentry */
2868 if (pid_revalidate(dentry, NULL))
2874 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2876 struct dentry *result;
2877 struct task_struct *task;
2879 struct pid_namespace *ns;
2881 result = proc_base_lookup(dir, dentry);
2882 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2885 tgid = name_to_int(dentry);
2889 ns = dentry->d_sb->s_fs_info;
2891 task = find_task_by_pid_ns(tgid, ns);
2893 get_task_struct(task);
2898 result = proc_pid_instantiate(dir, dentry, task, NULL);
2899 put_task_struct(task);
2905 * Find the first task with tgid >= tgid
2910 struct task_struct *task;
2912 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2917 put_task_struct(iter.task);
2921 pid = find_ge_pid(iter.tgid, ns);
2923 iter.tgid = pid_nr_ns(pid, ns);
2924 iter.task = pid_task(pid, PIDTYPE_PID);
2925 /* What we to know is if the pid we have find is the
2926 * pid of a thread_group_leader. Testing for task
2927 * being a thread_group_leader is the obvious thing
2928 * todo but there is a window when it fails, due to
2929 * the pid transfer logic in de_thread.
2931 * So we perform the straight forward test of seeing
2932 * if the pid we have found is the pid of a thread
2933 * group leader, and don't worry if the task we have
2934 * found doesn't happen to be a thread group leader.
2935 * As we don't care in the case of readdir.
2937 if (!iter.task || !has_group_leader_pid(iter.task)) {
2941 get_task_struct(iter.task);
2947 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2949 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2950 struct tgid_iter iter)
2952 char name[PROC_NUMBUF];
2953 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2954 return proc_fill_cache(filp, dirent, filldir, name, len,
2955 proc_pid_instantiate, iter.task, NULL);
2958 /* for the /proc/ directory itself, after non-process stuff has been done */
2959 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2961 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2962 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2963 struct tgid_iter iter;
2964 struct pid_namespace *ns;
2969 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2970 const struct pid_entry *p = &proc_base_stuff[nr];
2971 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2975 ns = filp->f_dentry->d_sb->s_fs_info;
2977 iter.tgid = filp->f_pos - TGID_OFFSET;
2978 for (iter = next_tgid(ns, iter);
2980 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2981 filp->f_pos = iter.tgid + TGID_OFFSET;
2982 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2983 put_task_struct(iter.task);
2987 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2989 put_task_struct(reaper);
2997 static const struct pid_entry tid_base_stuff[] = {
2998 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2999 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3000 REG("environ", S_IRUSR, proc_environ_operations),
3001 INF("auxv", S_IRUSR, proc_pid_auxv),
3002 ONE("status", S_IRUGO, proc_pid_status),
3003 ONE("personality", S_IRUSR, proc_pid_personality),
3004 INF("limits", S_IRUSR, proc_pid_limits),
3005 #ifdef CONFIG_SCHED_DEBUG
3006 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3008 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3009 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3010 INF("syscall", S_IRUSR, proc_pid_syscall),
3012 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3013 ONE("stat", S_IRUGO, proc_tid_stat),
3014 ONE("statm", S_IRUGO, proc_pid_statm),
3015 REG("maps", S_IRUGO, proc_maps_operations),
3017 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3019 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3020 LNK("cwd", proc_cwd_link),
3021 LNK("root", proc_root_link),
3022 LNK("exe", proc_exe_link),
3023 REG("mounts", S_IRUGO, proc_mounts_operations),
3024 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3025 #ifdef CONFIG_PROC_PAGE_MONITOR
3026 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3027 REG("smaps", S_IRUGO, proc_smaps_operations),
3028 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3030 #ifdef CONFIG_SECURITY
3031 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3033 #ifdef CONFIG_KALLSYMS
3034 INF("wchan", S_IRUGO, proc_pid_wchan),
3036 #ifdef CONFIG_STACKTRACE
3037 ONE("stack", S_IRUSR, proc_pid_stack),
3039 #ifdef CONFIG_SCHEDSTATS
3040 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3042 #ifdef CONFIG_LATENCYTOP
3043 REG("latency", S_IRUGO, proc_lstats_operations),
3045 #ifdef CONFIG_PROC_PID_CPUSET
3046 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3048 #ifdef CONFIG_CGROUPS
3049 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3051 INF("oom_score", S_IRUGO, proc_oom_score),
3052 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3053 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3054 #ifdef CONFIG_AUDITSYSCALL
3055 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3056 REG("sessionid", S_IRUSR, proc_sessionid_operations),
3058 #ifdef CONFIG_FAULT_INJECTION
3059 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3061 #ifdef CONFIG_TASK_IO_ACCOUNTING
3062 INF("io", S_IRUGO, proc_tid_io_accounting),
3066 static int proc_tid_base_readdir(struct file * filp,
3067 void * dirent, filldir_t filldir)
3069 return proc_pident_readdir(filp,dirent,filldir,
3070 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3073 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3074 return proc_pident_lookup(dir, dentry,
3075 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3078 static const struct file_operations proc_tid_base_operations = {
3079 .read = generic_read_dir,
3080 .readdir = proc_tid_base_readdir,
3083 static const struct inode_operations proc_tid_base_inode_operations = {
3084 .lookup = proc_tid_base_lookup,
3085 .getattr = pid_getattr,
3086 .setattr = proc_setattr,
3089 static struct dentry *proc_task_instantiate(struct inode *dir,
3090 struct dentry *dentry, struct task_struct *task, const void *ptr)
3092 struct dentry *error = ERR_PTR(-ENOENT);
3093 struct inode *inode;
3094 inode = proc_pid_make_inode(dir->i_sb, task);
3098 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3099 inode->i_op = &proc_tid_base_inode_operations;
3100 inode->i_fop = &proc_tid_base_operations;
3101 inode->i_flags|=S_IMMUTABLE;
3103 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3104 ARRAY_SIZE(tid_base_stuff));
3106 dentry->d_op = &pid_dentry_operations;
3108 d_add(dentry, inode);
3109 /* Close the race of the process dying before we return the dentry */
3110 if (pid_revalidate(dentry, NULL))
3116 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3118 struct dentry *result = ERR_PTR(-ENOENT);
3119 struct task_struct *task;
3120 struct task_struct *leader = get_proc_task(dir);
3122 struct pid_namespace *ns;
3127 tid = name_to_int(dentry);
3131 ns = dentry->d_sb->s_fs_info;
3133 task = find_task_by_pid_ns(tid, ns);
3135 get_task_struct(task);
3139 if (!same_thread_group(leader, task))
3142 result = proc_task_instantiate(dir, dentry, task, NULL);
3144 put_task_struct(task);
3146 put_task_struct(leader);
3152 * Find the first tid of a thread group to return to user space.
3154 * Usually this is just the thread group leader, but if the users
3155 * buffer was too small or there was a seek into the middle of the
3156 * directory we have more work todo.
3158 * In the case of a short read we start with find_task_by_pid.
3160 * In the case of a seek we start with the leader and walk nr
3163 static struct task_struct *first_tid(struct task_struct *leader,
3164 int tid, int nr, struct pid_namespace *ns)
3166 struct task_struct *pos;
3169 /* Attempt to start with the pid of a thread */
3170 if (tid && (nr > 0)) {
3171 pos = find_task_by_pid_ns(tid, ns);
3172 if (pos && (pos->group_leader == leader))
3176 /* If nr exceeds the number of threads there is nothing todo */
3178 if (nr && nr >= get_nr_threads(leader))
3181 /* If we haven't found our starting place yet start
3182 * with the leader and walk nr threads forward.
3184 for (pos = leader; nr > 0; --nr) {
3185 pos = next_thread(pos);
3186 if (pos == leader) {
3192 get_task_struct(pos);
3199 * Find the next thread in the thread list.
3200 * Return NULL if there is an error or no next thread.
3202 * The reference to the input task_struct is released.
3204 static struct task_struct *next_tid(struct task_struct *start)
3206 struct task_struct *pos = NULL;
3208 if (pid_alive(start)) {
3209 pos = next_thread(start);
3210 if (thread_group_leader(pos))
3213 get_task_struct(pos);
3216 put_task_struct(start);
3220 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3221 struct task_struct *task, int tid)
3223 char name[PROC_NUMBUF];
3224 int len = snprintf(name, sizeof(name), "%d", tid);
3225 return proc_fill_cache(filp, dirent, filldir, name, len,
3226 proc_task_instantiate, task, NULL);
3229 /* for the /proc/TGID/task/ directories */
3230 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3232 struct dentry *dentry = filp->f_path.dentry;
3233 struct inode *inode = dentry->d_inode;
3234 struct task_struct *leader = NULL;
3235 struct task_struct *task;
3236 int retval = -ENOENT;
3239 struct pid_namespace *ns;
3241 task = get_proc_task(inode);
3245 if (pid_alive(task)) {
3246 leader = task->group_leader;
3247 get_task_struct(leader);
3250 put_task_struct(task);
3255 switch ((unsigned long)filp->f_pos) {
3258 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3263 ino = parent_ino(dentry);
3264 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3270 /* f_version caches the tgid value that the last readdir call couldn't
3271 * return. lseek aka telldir automagically resets f_version to 0.
3273 ns = filp->f_dentry->d_sb->s_fs_info;
3274 tid = (int)filp->f_version;
3275 filp->f_version = 0;
3276 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3278 task = next_tid(task), filp->f_pos++) {
3279 tid = task_pid_nr_ns(task, ns);
3280 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3281 /* returning this tgid failed, save it as the first
3282 * pid for the next readir call */
3283 filp->f_version = (u64)tid;
3284 put_task_struct(task);
3289 put_task_struct(leader);
3294 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3296 struct inode *inode = dentry->d_inode;
3297 struct task_struct *p = get_proc_task(inode);
3298 generic_fillattr(inode, stat);
3301 stat->nlink += get_nr_threads(p);
3308 static const struct inode_operations proc_task_inode_operations = {
3309 .lookup = proc_task_lookup,
3310 .getattr = proc_task_getattr,
3311 .setattr = proc_setattr,
3314 static const struct file_operations proc_task_operations = {
3315 .read = generic_read_dir,
3316 .readdir = proc_task_readdir,