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/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/stacktrace.h>
69 #include <linux/resource.h>
70 #include <linux/module.h>
71 #include <linux/mount.h>
72 #include <linux/security.h>
73 #include <linux/ptrace.h>
74 #include <linux/tracehook.h>
75 #include <linux/cgroup.h>
76 #include <linux/cpuset.h>
77 #include <linux/audit.h>
78 #include <linux/poll.h>
79 #include <linux/nsproxy.h>
80 #include <linux/oom.h>
81 #include <linux/elf.h>
82 #include <linux/pid_namespace.h>
83 #include <linux/fs_struct.h>
84 #include <linux/slab.h>
88 * Implementing inode permission operations in /proc is almost
89 * certainly an error. Permission checks need to happen during
90 * each system call not at open time. The reason is that most of
91 * what we wish to check for permissions in /proc varies at runtime.
93 * The classic example of a problem is opening file descriptors
94 * in /proc for a task before it execs a suid executable.
101 const struct inode_operations *iop;
102 const struct file_operations *fop;
106 #define NOD(NAME, MODE, IOP, FOP, OP) { \
108 .len = sizeof(NAME) - 1, \
115 #define DIR(NAME, MODE, iops, fops) \
116 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
117 #define LNK(NAME, get_link) \
118 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
119 &proc_pid_link_inode_operations, NULL, \
120 { .proc_get_link = get_link } )
121 #define REG(NAME, MODE, fops) \
122 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
123 #define INF(NAME, MODE, read) \
124 NOD(NAME, (S_IFREG|(MODE)), \
125 NULL, &proc_info_file_operations, \
126 { .proc_read = read } )
127 #define ONE(NAME, MODE, show) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
130 { .proc_show = show } )
133 * Count the number of hardlinks for the pid_entry table, excluding the .
136 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
143 for (i = 0; i < n; ++i) {
144 if (S_ISDIR(entries[i].mode))
151 static int get_fs_path(struct task_struct *task, struct path *path, bool root)
153 struct fs_struct *fs;
154 int result = -ENOENT;
159 read_lock(&fs->lock);
160 *path = root ? fs->root : fs->pwd;
162 read_unlock(&fs->lock);
169 static int proc_cwd_link(struct inode *inode, struct path *path)
171 struct task_struct *task = get_proc_task(inode);
172 int result = -ENOENT;
175 result = get_fs_path(task, path, 0);
176 put_task_struct(task);
181 static int proc_root_link(struct inode *inode, struct path *path)
183 struct task_struct *task = get_proc_task(inode);
184 int result = -ENOENT;
187 result = get_fs_path(task, path, 1);
188 put_task_struct(task);
194 * Return zero if current may access user memory in @task, -error if not.
196 static int check_mem_permission(struct task_struct *task)
199 * A task can always look at itself, in case it chooses
200 * to use system calls instead of load instructions.
206 * If current is actively ptrace'ing, and would also be
207 * permitted to freshly attach with ptrace now, permit it.
209 if (task_is_stopped_or_traced(task)) {
212 match = (tracehook_tracer_task(task) == current);
214 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
219 * Noone else is allowed.
224 struct mm_struct *mm_for_maps(struct task_struct *task)
226 struct mm_struct *mm;
228 if (mutex_lock_killable(&task->cred_guard_mutex))
231 mm = get_task_mm(task);
232 if (mm && mm != current->mm &&
233 !ptrace_may_access(task, PTRACE_MODE_READ)) {
237 mutex_unlock(&task->cred_guard_mutex);
242 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
246 struct mm_struct *mm = get_task_mm(task);
250 goto out_mm; /* Shh! No looking before we're done */
252 len = mm->arg_end - mm->arg_start;
257 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
259 // If the nul at the end of args has been overwritten, then
260 // assume application is using setproctitle(3).
261 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
262 len = strnlen(buffer, res);
266 len = mm->env_end - mm->env_start;
267 if (len > PAGE_SIZE - res)
268 len = PAGE_SIZE - res;
269 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
270 res = strnlen(buffer, res);
279 static int proc_pid_auxv(struct task_struct *task, char *buffer)
282 struct mm_struct *mm = get_task_mm(task);
284 unsigned int nwords = 0;
287 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
288 res = nwords * sizeof(mm->saved_auxv[0]);
291 memcpy(buffer, mm->saved_auxv, res);
298 #ifdef CONFIG_KALLSYMS
300 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
301 * Returns the resolved symbol. If that fails, simply return the address.
303 static int proc_pid_wchan(struct task_struct *task, char *buffer)
306 char symname[KSYM_NAME_LEN];
308 wchan = get_wchan(task);
310 if (lookup_symbol_name(wchan, symname) < 0)
311 if (!ptrace_may_access(task, PTRACE_MODE_READ))
314 return sprintf(buffer, "%lu", wchan);
316 return sprintf(buffer, "%s", symname);
318 #endif /* CONFIG_KALLSYMS */
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;
331 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
335 trace.nr_entries = 0;
336 trace.max_entries = MAX_STACK_TRACE_DEPTH;
337 trace.entries = entries;
339 save_stack_trace_tsk(task, &trace);
341 for (i = 0; i < trace.nr_entries; i++) {
342 seq_printf(m, "[<%p>] %pS\n",
343 (void *)entries[i], (void *)entries[i]);
351 #ifdef CONFIG_SCHEDSTATS
353 * Provides /proc/PID/schedstat
355 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
357 return sprintf(buffer, "%llu %llu %lu\n",
358 (unsigned long long)task->se.sum_exec_runtime,
359 (unsigned long long)task->sched_info.run_delay,
360 task->sched_info.pcount);
364 #ifdef CONFIG_LATENCYTOP
365 static int lstats_show_proc(struct seq_file *m, void *v)
368 struct inode *inode = m->private;
369 struct task_struct *task = get_proc_task(inode);
373 seq_puts(m, "Latency Top version : v0.1\n");
374 for (i = 0; i < 32; i++) {
375 if (task->latency_record[i].backtrace[0]) {
377 seq_printf(m, "%i %li %li ",
378 task->latency_record[i].count,
379 task->latency_record[i].time,
380 task->latency_record[i].max);
381 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
382 char sym[KSYM_SYMBOL_LEN];
384 if (!task->latency_record[i].backtrace[q])
386 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
388 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
389 c = strchr(sym, '+');
392 seq_printf(m, "%s ", sym);
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 /* The badness from the OOM killer */
431 unsigned long badness(struct task_struct *p, unsigned long uptime);
432 static int proc_oom_score(struct task_struct *task, char *buffer)
434 unsigned long points = 0;
435 struct timespec uptime;
437 do_posix_clock_monotonic_gettime(&uptime);
438 read_lock(&tasklist_lock);
440 points = badness(task, uptime.tv_sec);
441 read_unlock(&tasklist_lock);
442 return sprintf(buffer, "%lu\n", points);
450 static const struct limit_names lnames[RLIM_NLIMITS] = {
451 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
452 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
453 [RLIMIT_DATA] = {"Max data size", "bytes"},
454 [RLIMIT_STACK] = {"Max stack size", "bytes"},
455 [RLIMIT_CORE] = {"Max core file size", "bytes"},
456 [RLIMIT_RSS] = {"Max resident set", "bytes"},
457 [RLIMIT_NPROC] = {"Max processes", "processes"},
458 [RLIMIT_NOFILE] = {"Max open files", "files"},
459 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
460 [RLIMIT_AS] = {"Max address space", "bytes"},
461 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
462 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
463 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
464 [RLIMIT_NICE] = {"Max nice priority", NULL},
465 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
466 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
469 /* Display limits for a process */
470 static ssize_t limits_read(struct file *file, char __user *buf, size_t rcount,
473 struct rlimit rlim[RLIM_NLIMITS];
474 struct task_struct *task;
480 task = get_proc_task(file->f_path.dentry->d_inode);
483 if (!lock_task_sighand(task, &flags)) {
484 put_task_struct(task);
487 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
488 unlock_task_sighand(task, &flags);
489 put_task_struct(task);
491 bufptr = (char *)__get_free_page(GFP_TEMPORARY);
496 * print the file header
498 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
499 "Limit", "Soft Limit", "Hard Limit", "Units");
501 for (i = 0; i < RLIM_NLIMITS; i++) {
502 if (rlim[i].rlim_cur == RLIM_INFINITY)
503 count += sprintf(&bufptr[count], "%-25s %-20s ",
504 lnames[i].name, "unlimited");
506 count += sprintf(&bufptr[count], "%-25s %-20lu ",
507 lnames[i].name, rlim[i].rlim_cur);
509 if (rlim[i].rlim_max == RLIM_INFINITY)
510 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
512 count += sprintf(&bufptr[count], "%-20lu ",
516 count += sprintf(&bufptr[count], "%-10s\n",
519 count += sprintf(&bufptr[count], "\n");
522 count = simple_read_from_buffer(buf, rcount, ppos, bufptr, count);
524 free_page((unsigned long)bufptr);
529 static ssize_t limits_write(struct file *file, const char __user *buf,
530 size_t count, loff_t *ppos)
532 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
533 char str[32 + 1 + 16 + 1 + 16 + 1], *delim, *next;
534 struct rlimit new_rlimit;
542 if (copy_from_user(str, buf, min(count, sizeof(str) - 1))) {
547 str[min(count, sizeof(str) - 1)] = 0;
549 delim = strchr(str, '=');
554 *delim++ = 0; /* for easy 'str' usage */
555 new_rlimit.rlim_cur = simple_strtoul(delim, &next, 0);
557 if (strncmp(delim, "unlimited:", 10)) {
561 new_rlimit.rlim_cur = RLIM_INFINITY;
562 next = delim + 9; /* move to ':' */
565 new_rlimit.rlim_max = simple_strtoul(delim, &next, 0);
567 if (strcmp(delim, "unlimited")) {
571 new_rlimit.rlim_max = RLIM_INFINITY;
574 for (i = 0; i < RLIM_NLIMITS; i++)
575 if (!strcmp(str, lnames[i].name))
577 if (i >= RLIM_NLIMITS) {
582 ret = do_setrlimit(task, i, &new_rlimit);
587 put_task_struct(task);
592 static const struct file_operations proc_pid_limits_operations = {
594 .write = limits_write,
597 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
598 static int proc_pid_syscall(struct task_struct *task, char *buffer)
601 unsigned long args[6], sp, pc;
603 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
604 return sprintf(buffer, "running\n");
607 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
609 return sprintf(buffer,
610 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
612 args[0], args[1], args[2], args[3], args[4], args[5],
615 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
617 /************************************************************************/
618 /* Here the fs part begins */
619 /************************************************************************/
621 /* permission checks */
622 static int proc_fd_access_allowed(struct inode *inode)
624 struct task_struct *task;
626 /* Allow access to a task's file descriptors if it is us or we
627 * may use ptrace attach to the process and find out that
630 task = get_proc_task(inode);
632 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
633 put_task_struct(task);
638 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
641 struct inode *inode = dentry->d_inode;
643 if (attr->ia_valid & ATTR_MODE)
646 error = inode_change_ok(inode, attr);
648 error = inode_setattr(inode, attr);
652 static const struct inode_operations proc_def_inode_operations = {
653 .setattr = proc_setattr,
656 static int mounts_open_common(struct inode *inode, struct file *file,
657 const struct seq_operations *op)
659 struct task_struct *task = get_proc_task(inode);
661 struct mnt_namespace *ns = NULL;
663 struct proc_mounts *p;
668 nsp = task_nsproxy(task);
675 if (ns && get_fs_path(task, &root, 1) == 0)
677 put_task_struct(task);
686 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
690 file->private_data = &p->m;
691 ret = seq_open(file, op);
698 p->event = ns->event;
712 static int mounts_release(struct inode *inode, struct file *file)
714 struct proc_mounts *p = file->private_data;
717 return seq_release(inode, file);
720 static unsigned mounts_poll(struct file *file, poll_table *wait)
722 struct proc_mounts *p = file->private_data;
723 unsigned res = POLLIN | POLLRDNORM;
725 poll_wait(file, &p->ns->poll, wait);
726 if (mnt_had_events(p))
727 res |= POLLERR | POLLPRI;
732 static int mounts_open(struct inode *inode, struct file *file)
734 return mounts_open_common(inode, file, &mounts_op);
737 static const struct file_operations proc_mounts_operations = {
741 .release = mounts_release,
745 static int mountinfo_open(struct inode *inode, struct file *file)
747 return mounts_open_common(inode, file, &mountinfo_op);
750 static const struct file_operations proc_mountinfo_operations = {
751 .open = mountinfo_open,
754 .release = mounts_release,
758 static int mountstats_open(struct inode *inode, struct file *file)
760 return mounts_open_common(inode, file, &mountstats_op);
763 static const struct file_operations proc_mountstats_operations = {
764 .open = mountstats_open,
767 .release = mounts_release,
770 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
772 static ssize_t proc_info_read(struct file * file, char __user * buf,
773 size_t count, loff_t *ppos)
775 struct inode * inode = file->f_path.dentry->d_inode;
778 struct task_struct *task = get_proc_task(inode);
784 if (count > PROC_BLOCK_SIZE)
785 count = PROC_BLOCK_SIZE;
788 if (!(page = __get_free_page(GFP_TEMPORARY)))
791 length = PROC_I(inode)->op.proc_read(task, (char*)page);
794 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
797 put_task_struct(task);
802 static const struct file_operations proc_info_file_operations = {
803 .read = proc_info_read,
804 .llseek = generic_file_llseek,
807 static int proc_single_show(struct seq_file *m, void *v)
809 struct inode *inode = m->private;
810 struct pid_namespace *ns;
812 struct task_struct *task;
815 ns = inode->i_sb->s_fs_info;
816 pid = proc_pid(inode);
817 task = get_pid_task(pid, PIDTYPE_PID);
821 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
823 put_task_struct(task);
827 static int proc_single_open(struct inode *inode, struct file *filp)
830 ret = single_open(filp, proc_single_show, NULL);
832 struct seq_file *m = filp->private_data;
839 static const struct file_operations proc_single_file_operations = {
840 .open = proc_single_open,
843 .release = single_release,
846 static int mem_open(struct inode* inode, struct file* file)
848 file->private_data = (void*)((long)current->self_exec_id);
852 static ssize_t mem_read(struct file * file, char __user * buf,
853 size_t count, loff_t *ppos)
855 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
857 unsigned long src = *ppos;
859 struct mm_struct *mm;
864 if (check_mem_permission(task))
868 page = (char *)__get_free_page(GFP_TEMPORARY);
874 mm = get_task_mm(task);
880 if (file->private_data != (void*)((long)current->self_exec_id))
886 int this_len, retval;
888 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
889 retval = access_process_vm(task, src, page, this_len, 0);
890 if (!retval || check_mem_permission(task)) {
896 if (copy_to_user(buf, page, retval)) {
911 free_page((unsigned long) page);
913 put_task_struct(task);
918 #define mem_write NULL
921 /* This is a security hazard */
922 static ssize_t mem_write(struct file * file, const char __user *buf,
923 size_t count, loff_t *ppos)
927 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
928 unsigned long dst = *ppos;
934 if (check_mem_permission(task))
938 page = (char *)__get_free_page(GFP_TEMPORARY);
944 int this_len, retval;
946 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
947 if (copy_from_user(page, buf, this_len)) {
951 retval = access_process_vm(task, dst, page, this_len, 1);
963 free_page((unsigned long) page);
965 put_task_struct(task);
971 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
975 file->f_pos = offset;
978 file->f_pos += offset;
983 force_successful_syscall_return();
987 static const struct file_operations proc_mem_operations = {
994 static ssize_t environ_read(struct file *file, char __user *buf,
995 size_t count, loff_t *ppos)
997 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
999 unsigned long src = *ppos;
1001 struct mm_struct *mm;
1006 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1010 page = (char *)__get_free_page(GFP_TEMPORARY);
1016 mm = get_task_mm(task);
1021 int this_len, retval, max_len;
1023 this_len = mm->env_end - (mm->env_start + src);
1028 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1029 this_len = (this_len > max_len) ? max_len : this_len;
1031 retval = access_process_vm(task, (mm->env_start + src),
1039 if (copy_to_user(buf, page, retval)) {
1053 free_page((unsigned long) page);
1055 put_task_struct(task);
1060 static const struct file_operations proc_environ_operations = {
1061 .read = environ_read,
1062 .llseek = generic_file_llseek,
1065 static ssize_t oom_adjust_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];
1071 int oom_adjust = OOM_DISABLE;
1072 unsigned long flags;
1077 if (lock_task_sighand(task, &flags)) {
1078 oom_adjust = task->signal->oom_adj;
1079 unlock_task_sighand(task, &flags);
1082 put_task_struct(task);
1084 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1086 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1089 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1090 size_t count, loff_t *ppos)
1092 struct task_struct *task;
1093 char buffer[PROC_NUMBUF];
1095 unsigned long flags;
1098 memset(buffer, 0, sizeof(buffer));
1099 if (count > sizeof(buffer) - 1)
1100 count = sizeof(buffer) - 1;
1101 if (copy_from_user(buffer, buf, count))
1104 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1107 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1108 oom_adjust != OOM_DISABLE)
1111 task = get_proc_task(file->f_path.dentry->d_inode);
1114 if (!lock_task_sighand(task, &flags)) {
1115 put_task_struct(task);
1119 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1120 unlock_task_sighand(task, &flags);
1121 put_task_struct(task);
1125 task->signal->oom_adj = oom_adjust;
1127 unlock_task_sighand(task, &flags);
1128 put_task_struct(task);
1133 static const struct file_operations proc_oom_adjust_operations = {
1134 .read = oom_adjust_read,
1135 .write = oom_adjust_write,
1136 .llseek = generic_file_llseek,
1139 #ifdef CONFIG_AUDITSYSCALL
1140 #define TMPBUFLEN 21
1141 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1142 size_t count, loff_t *ppos)
1144 struct inode * inode = file->f_path.dentry->d_inode;
1145 struct task_struct *task = get_proc_task(inode);
1147 char tmpbuf[TMPBUFLEN];
1151 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1152 audit_get_loginuid(task));
1153 put_task_struct(task);
1154 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1157 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1158 size_t count, loff_t *ppos)
1160 struct inode * inode = file->f_path.dentry->d_inode;
1165 if (!capable(CAP_AUDIT_CONTROL))
1169 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1175 if (count >= PAGE_SIZE)
1176 count = PAGE_SIZE - 1;
1179 /* No partial writes. */
1182 page = (char*)__get_free_page(GFP_TEMPORARY);
1186 if (copy_from_user(page, buf, count))
1190 loginuid = simple_strtoul(page, &tmp, 10);
1196 length = audit_set_loginuid(current, loginuid);
1197 if (likely(length == 0))
1201 free_page((unsigned long) page);
1205 static const struct file_operations proc_loginuid_operations = {
1206 .read = proc_loginuid_read,
1207 .write = proc_loginuid_write,
1208 .llseek = generic_file_llseek,
1211 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1212 size_t count, loff_t *ppos)
1214 struct inode * inode = file->f_path.dentry->d_inode;
1215 struct task_struct *task = get_proc_task(inode);
1217 char tmpbuf[TMPBUFLEN];
1221 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1222 audit_get_sessionid(task));
1223 put_task_struct(task);
1224 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1227 static const struct file_operations proc_sessionid_operations = {
1228 .read = proc_sessionid_read,
1229 .llseek = generic_file_llseek,
1233 #ifdef CONFIG_FAULT_INJECTION
1234 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1235 size_t count, loff_t *ppos)
1237 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1238 char buffer[PROC_NUMBUF];
1244 make_it_fail = task->make_it_fail;
1245 put_task_struct(task);
1247 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1249 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1252 static ssize_t proc_fault_inject_write(struct file * file,
1253 const char __user * buf, size_t count, loff_t *ppos)
1255 struct task_struct *task;
1256 char buffer[PROC_NUMBUF], *end;
1259 if (!capable(CAP_SYS_RESOURCE))
1261 memset(buffer, 0, sizeof(buffer));
1262 if (count > sizeof(buffer) - 1)
1263 count = sizeof(buffer) - 1;
1264 if (copy_from_user(buffer, buf, count))
1266 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1269 task = get_proc_task(file->f_dentry->d_inode);
1272 task->make_it_fail = make_it_fail;
1273 put_task_struct(task);
1278 static const struct file_operations proc_fault_inject_operations = {
1279 .read = proc_fault_inject_read,
1280 .write = proc_fault_inject_write,
1281 .llseek = generic_file_llseek,
1286 #ifdef CONFIG_SCHED_DEBUG
1288 * Print out various scheduling related per-task fields:
1290 static int sched_show(struct seq_file *m, void *v)
1292 struct inode *inode = m->private;
1293 struct task_struct *p;
1295 p = get_proc_task(inode);
1298 proc_sched_show_task(p, m);
1306 sched_write(struct file *file, const char __user *buf,
1307 size_t count, loff_t *offset)
1309 struct inode *inode = file->f_path.dentry->d_inode;
1310 struct task_struct *p;
1312 p = get_proc_task(inode);
1315 proc_sched_set_task(p);
1322 static int sched_open(struct inode *inode, struct file *filp)
1326 ret = single_open(filp, sched_show, NULL);
1328 struct seq_file *m = filp->private_data;
1335 static const struct file_operations proc_pid_sched_operations = {
1338 .write = sched_write,
1339 .llseek = seq_lseek,
1340 .release = single_release,
1345 static ssize_t comm_write(struct file *file, const char __user *buf,
1346 size_t count, loff_t *offset)
1348 struct inode *inode = file->f_path.dentry->d_inode;
1349 struct task_struct *p;
1350 char buffer[TASK_COMM_LEN];
1352 memset(buffer, 0, sizeof(buffer));
1353 if (count > sizeof(buffer) - 1)
1354 count = sizeof(buffer) - 1;
1355 if (copy_from_user(buffer, buf, count))
1358 p = get_proc_task(inode);
1362 if (same_thread_group(current, p))
1363 set_task_comm(p, buffer);
1372 static int comm_show(struct seq_file *m, void *v)
1374 struct inode *inode = m->private;
1375 struct task_struct *p;
1377 p = get_proc_task(inode);
1382 seq_printf(m, "%s\n", p->comm);
1390 static int comm_open(struct inode *inode, struct file *filp)
1394 ret = single_open(filp, comm_show, NULL);
1396 struct seq_file *m = filp->private_data;
1403 static const struct file_operations proc_pid_set_comm_operations = {
1406 .write = comm_write,
1407 .llseek = seq_lseek,
1408 .release = single_release,
1412 * We added or removed a vma mapping the executable. The vmas are only mapped
1413 * during exec and are not mapped with the mmap system call.
1414 * Callers must hold down_write() on the mm's mmap_sem for these
1416 void added_exe_file_vma(struct mm_struct *mm)
1418 mm->num_exe_file_vmas++;
1421 void removed_exe_file_vma(struct mm_struct *mm)
1423 mm->num_exe_file_vmas--;
1424 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1426 mm->exe_file = NULL;
1431 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1434 get_file(new_exe_file);
1437 mm->exe_file = new_exe_file;
1438 mm->num_exe_file_vmas = 0;
1441 struct file *get_mm_exe_file(struct mm_struct *mm)
1443 struct file *exe_file;
1445 /* We need mmap_sem to protect against races with removal of
1446 * VM_EXECUTABLE vmas */
1447 down_read(&mm->mmap_sem);
1448 exe_file = mm->exe_file;
1451 up_read(&mm->mmap_sem);
1455 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1457 /* It's safe to write the exe_file pointer without exe_file_lock because
1458 * this is called during fork when the task is not yet in /proc */
1459 newmm->exe_file = get_mm_exe_file(oldmm);
1462 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1464 struct task_struct *task;
1465 struct mm_struct *mm;
1466 struct file *exe_file;
1468 task = get_proc_task(inode);
1471 mm = get_task_mm(task);
1472 put_task_struct(task);
1475 exe_file = get_mm_exe_file(mm);
1478 *exe_path = exe_file->f_path;
1479 path_get(&exe_file->f_path);
1486 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1488 struct inode *inode = dentry->d_inode;
1489 int error = -EACCES;
1491 /* We don't need a base pointer in the /proc filesystem */
1492 path_put(&nd->path);
1494 /* Are we allowed to snoop on the tasks file descriptors? */
1495 if (!proc_fd_access_allowed(inode))
1498 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1500 return ERR_PTR(error);
1503 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1505 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1512 pathname = d_path(path, tmp, PAGE_SIZE);
1513 len = PTR_ERR(pathname);
1514 if (IS_ERR(pathname))
1516 len = tmp + PAGE_SIZE - 1 - pathname;
1520 if (copy_to_user(buffer, pathname, len))
1523 free_page((unsigned long)tmp);
1527 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1529 int error = -EACCES;
1530 struct inode *inode = dentry->d_inode;
1533 /* Are we allowed to snoop on the tasks file descriptors? */
1534 if (!proc_fd_access_allowed(inode))
1537 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1541 error = do_proc_readlink(&path, buffer, buflen);
1547 static const struct inode_operations proc_pid_link_inode_operations = {
1548 .readlink = proc_pid_readlink,
1549 .follow_link = proc_pid_follow_link,
1550 .setattr = proc_setattr,
1554 /* building an inode */
1556 static int task_dumpable(struct task_struct *task)
1559 struct mm_struct *mm;
1564 dumpable = get_dumpable(mm);
1572 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1574 struct inode * inode;
1575 struct proc_inode *ei;
1576 const struct cred *cred;
1578 /* We need a new inode */
1580 inode = new_inode(sb);
1586 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1587 inode->i_op = &proc_def_inode_operations;
1590 * grab the reference to task.
1592 ei->pid = get_task_pid(task, PIDTYPE_PID);
1596 if (task_dumpable(task)) {
1598 cred = __task_cred(task);
1599 inode->i_uid = cred->euid;
1600 inode->i_gid = cred->egid;
1603 security_task_to_inode(task, inode);
1613 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1615 struct inode *inode = dentry->d_inode;
1616 struct task_struct *task;
1617 const struct cred *cred;
1619 generic_fillattr(inode, stat);
1624 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1626 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1627 task_dumpable(task)) {
1628 cred = __task_cred(task);
1629 stat->uid = cred->euid;
1630 stat->gid = cred->egid;
1640 * Exceptional case: normally we are not allowed to unhash a busy
1641 * directory. In this case, however, we can do it - no aliasing problems
1642 * due to the way we treat inodes.
1644 * Rewrite the inode's ownerships here because the owning task may have
1645 * performed a setuid(), etc.
1647 * Before the /proc/pid/status file was created the only way to read
1648 * the effective uid of a /process was to stat /proc/pid. Reading
1649 * /proc/pid/status is slow enough that procps and other packages
1650 * kept stating /proc/pid. To keep the rules in /proc simple I have
1651 * made this apply to all per process world readable and executable
1654 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1656 struct inode *inode = dentry->d_inode;
1657 struct task_struct *task = get_proc_task(inode);
1658 const struct cred *cred;
1661 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1662 task_dumpable(task)) {
1664 cred = __task_cred(task);
1665 inode->i_uid = cred->euid;
1666 inode->i_gid = cred->egid;
1672 inode->i_mode &= ~(S_ISUID | S_ISGID);
1673 security_task_to_inode(task, inode);
1674 put_task_struct(task);
1681 static int pid_delete_dentry(struct dentry * dentry)
1683 /* Is the task we represent dead?
1684 * If so, then don't put the dentry on the lru list,
1685 * kill it immediately.
1687 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1690 static const struct dentry_operations pid_dentry_operations =
1692 .d_revalidate = pid_revalidate,
1693 .d_delete = pid_delete_dentry,
1698 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1699 struct task_struct *, const void *);
1702 * Fill a directory entry.
1704 * If possible create the dcache entry and derive our inode number and
1705 * file type from dcache entry.
1707 * Since all of the proc inode numbers are dynamically generated, the inode
1708 * numbers do not exist until the inode is cache. This means creating the
1709 * the dcache entry in readdir is necessary to keep the inode numbers
1710 * reported by readdir in sync with the inode numbers reported
1713 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1714 char *name, int len,
1715 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1717 struct dentry *child, *dir = filp->f_path.dentry;
1718 struct inode *inode;
1721 unsigned type = DT_UNKNOWN;
1725 qname.hash = full_name_hash(name, len);
1727 child = d_lookup(dir, &qname);
1730 new = d_alloc(dir, &qname);
1732 child = instantiate(dir->d_inode, new, task, ptr);
1739 if (!child || IS_ERR(child) || !child->d_inode)
1740 goto end_instantiate;
1741 inode = child->d_inode;
1744 type = inode->i_mode >> 12;
1749 ino = find_inode_number(dir, &qname);
1752 return filldir(dirent, name, len, filp->f_pos, ino, type);
1755 static unsigned name_to_int(struct dentry *dentry)
1757 const char *name = dentry->d_name.name;
1758 int len = dentry->d_name.len;
1761 if (len > 1 && *name == '0')
1764 unsigned c = *name++ - '0';
1767 if (n >= (~0U-9)/10)
1777 #define PROC_FDINFO_MAX 64
1779 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1781 struct task_struct *task = get_proc_task(inode);
1782 struct files_struct *files = NULL;
1784 int fd = proc_fd(inode);
1787 files = get_files_struct(task);
1788 put_task_struct(task);
1792 * We are not taking a ref to the file structure, so we must
1795 spin_lock(&files->file_lock);
1796 file = fcheck_files(files, fd);
1799 *path = file->f_path;
1800 path_get(&file->f_path);
1803 snprintf(info, PROC_FDINFO_MAX,
1806 (long long) file->f_pos,
1808 spin_unlock(&files->file_lock);
1809 put_files_struct(files);
1812 spin_unlock(&files->file_lock);
1813 put_files_struct(files);
1818 static int proc_fd_link(struct inode *inode, struct path *path)
1820 return proc_fd_info(inode, path, NULL);
1823 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1825 struct inode *inode = dentry->d_inode;
1826 struct task_struct *task = get_proc_task(inode);
1827 int fd = proc_fd(inode);
1828 struct files_struct *files;
1829 const struct cred *cred;
1832 files = get_files_struct(task);
1835 if (fcheck_files(files, fd)) {
1837 put_files_struct(files);
1838 if (task_dumpable(task)) {
1840 cred = __task_cred(task);
1841 inode->i_uid = cred->euid;
1842 inode->i_gid = cred->egid;
1848 inode->i_mode &= ~(S_ISUID | S_ISGID);
1849 security_task_to_inode(task, inode);
1850 put_task_struct(task);
1854 put_files_struct(files);
1856 put_task_struct(task);
1862 static const struct dentry_operations tid_fd_dentry_operations =
1864 .d_revalidate = tid_fd_revalidate,
1865 .d_delete = pid_delete_dentry,
1868 static struct dentry *proc_fd_instantiate(struct inode *dir,
1869 struct dentry *dentry, struct task_struct *task, const void *ptr)
1871 unsigned fd = *(const unsigned *)ptr;
1873 struct files_struct *files;
1874 struct inode *inode;
1875 struct proc_inode *ei;
1876 struct dentry *error = ERR_PTR(-ENOENT);
1878 inode = proc_pid_make_inode(dir->i_sb, task);
1883 files = get_files_struct(task);
1886 inode->i_mode = S_IFLNK;
1889 * We are not taking a ref to the file structure, so we must
1892 spin_lock(&files->file_lock);
1893 file = fcheck_files(files, fd);
1896 if (file->f_mode & FMODE_READ)
1897 inode->i_mode |= S_IRUSR | S_IXUSR;
1898 if (file->f_mode & FMODE_WRITE)
1899 inode->i_mode |= S_IWUSR | S_IXUSR;
1900 spin_unlock(&files->file_lock);
1901 put_files_struct(files);
1903 inode->i_op = &proc_pid_link_inode_operations;
1905 ei->op.proc_get_link = proc_fd_link;
1906 dentry->d_op = &tid_fd_dentry_operations;
1907 d_add(dentry, inode);
1908 /* Close the race of the process dying before we return the dentry */
1909 if (tid_fd_revalidate(dentry, NULL))
1915 spin_unlock(&files->file_lock);
1916 put_files_struct(files);
1922 static struct dentry *proc_lookupfd_common(struct inode *dir,
1923 struct dentry *dentry,
1924 instantiate_t instantiate)
1926 struct task_struct *task = get_proc_task(dir);
1927 unsigned fd = name_to_int(dentry);
1928 struct dentry *result = ERR_PTR(-ENOENT);
1935 result = instantiate(dir, dentry, task, &fd);
1937 put_task_struct(task);
1942 static int proc_readfd_common(struct file * filp, void * dirent,
1943 filldir_t filldir, instantiate_t instantiate)
1945 struct dentry *dentry = filp->f_path.dentry;
1946 struct inode *inode = dentry->d_inode;
1947 struct task_struct *p = get_proc_task(inode);
1948 unsigned int fd, ino;
1950 struct files_struct * files;
1960 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1964 ino = parent_ino(dentry);
1965 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1969 files = get_files_struct(p);
1973 for (fd = filp->f_pos-2;
1974 fd < files_fdtable(files)->max_fds;
1975 fd++, filp->f_pos++) {
1976 char name[PROC_NUMBUF];
1979 if (!fcheck_files(files, fd))
1983 len = snprintf(name, sizeof(name), "%d", fd);
1984 if (proc_fill_cache(filp, dirent, filldir,
1985 name, len, instantiate,
1993 put_files_struct(files);
2001 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2002 struct nameidata *nd)
2004 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2007 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2009 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2012 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2013 size_t len, loff_t *ppos)
2015 char tmp[PROC_FDINFO_MAX];
2016 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2018 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2022 static const struct file_operations proc_fdinfo_file_operations = {
2023 .open = nonseekable_open,
2024 .read = proc_fdinfo_read,
2027 static const struct file_operations proc_fd_operations = {
2028 .read = generic_read_dir,
2029 .readdir = proc_readfd,
2033 * /proc/pid/fd needs a special permission handler so that a process can still
2034 * access /proc/self/fd after it has executed a setuid().
2036 static int proc_fd_permission(struct inode *inode, int mask)
2040 rv = generic_permission(inode, mask, NULL);
2043 if (task_pid(current) == proc_pid(inode))
2049 * proc directories can do almost nothing..
2051 static const struct inode_operations proc_fd_inode_operations = {
2052 .lookup = proc_lookupfd,
2053 .permission = proc_fd_permission,
2054 .setattr = proc_setattr,
2057 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2058 struct dentry *dentry, struct task_struct *task, const void *ptr)
2060 unsigned fd = *(unsigned *)ptr;
2061 struct inode *inode;
2062 struct proc_inode *ei;
2063 struct dentry *error = ERR_PTR(-ENOENT);
2065 inode = proc_pid_make_inode(dir->i_sb, task);
2070 inode->i_mode = S_IFREG | S_IRUSR;
2071 inode->i_fop = &proc_fdinfo_file_operations;
2072 dentry->d_op = &tid_fd_dentry_operations;
2073 d_add(dentry, inode);
2074 /* Close the race of the process dying before we return the dentry */
2075 if (tid_fd_revalidate(dentry, NULL))
2082 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2083 struct dentry *dentry,
2084 struct nameidata *nd)
2086 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2089 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2091 return proc_readfd_common(filp, dirent, filldir,
2092 proc_fdinfo_instantiate);
2095 static const struct file_operations proc_fdinfo_operations = {
2096 .read = generic_read_dir,
2097 .readdir = proc_readfdinfo,
2101 * proc directories can do almost nothing..
2103 static const struct inode_operations proc_fdinfo_inode_operations = {
2104 .lookup = proc_lookupfdinfo,
2105 .setattr = proc_setattr,
2109 static struct dentry *proc_pident_instantiate(struct inode *dir,
2110 struct dentry *dentry, struct task_struct *task, const void *ptr)
2112 const struct pid_entry *p = ptr;
2113 struct inode *inode;
2114 struct proc_inode *ei;
2115 struct dentry *error = ERR_PTR(-ENOENT);
2117 inode = proc_pid_make_inode(dir->i_sb, task);
2122 inode->i_mode = p->mode;
2123 if (S_ISDIR(inode->i_mode))
2124 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2126 inode->i_op = p->iop;
2128 inode->i_fop = p->fop;
2130 dentry->d_op = &pid_dentry_operations;
2131 d_add(dentry, inode);
2132 /* Close the race of the process dying before we return the dentry */
2133 if (pid_revalidate(dentry, NULL))
2139 static struct dentry *proc_pident_lookup(struct inode *dir,
2140 struct dentry *dentry,
2141 const struct pid_entry *ents,
2144 struct dentry *error;
2145 struct task_struct *task = get_proc_task(dir);
2146 const struct pid_entry *p, *last;
2148 error = ERR_PTR(-ENOENT);
2154 * Yes, it does not scale. And it should not. Don't add
2155 * new entries into /proc/<tgid>/ without very good reasons.
2157 last = &ents[nents - 1];
2158 for (p = ents; p <= last; p++) {
2159 if (p->len != dentry->d_name.len)
2161 if (!memcmp(dentry->d_name.name, p->name, p->len))
2167 error = proc_pident_instantiate(dir, dentry, task, p);
2169 put_task_struct(task);
2174 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2175 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2177 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2178 proc_pident_instantiate, task, p);
2181 static int proc_pident_readdir(struct file *filp,
2182 void *dirent, filldir_t filldir,
2183 const struct pid_entry *ents, unsigned int nents)
2186 struct dentry *dentry = filp->f_path.dentry;
2187 struct inode *inode = dentry->d_inode;
2188 struct task_struct *task = get_proc_task(inode);
2189 const struct pid_entry *p, *last;
2202 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2208 ino = parent_ino(dentry);
2209 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2221 last = &ents[nents - 1];
2223 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2232 put_task_struct(task);
2237 #ifdef CONFIG_SECURITY
2238 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2239 size_t count, loff_t *ppos)
2241 struct inode * inode = file->f_path.dentry->d_inode;
2244 struct task_struct *task = get_proc_task(inode);
2249 length = security_getprocattr(task,
2250 (char*)file->f_path.dentry->d_name.name,
2252 put_task_struct(task);
2254 length = simple_read_from_buffer(buf, count, ppos, p, length);
2259 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2260 size_t count, loff_t *ppos)
2262 struct inode * inode = file->f_path.dentry->d_inode;
2265 struct task_struct *task = get_proc_task(inode);
2270 if (count > PAGE_SIZE)
2273 /* No partial writes. */
2279 page = (char*)__get_free_page(GFP_TEMPORARY);
2284 if (copy_from_user(page, buf, count))
2287 /* Guard against adverse ptrace interaction */
2288 length = mutex_lock_interruptible(&task->cred_guard_mutex);
2292 length = security_setprocattr(task,
2293 (char*)file->f_path.dentry->d_name.name,
2294 (void*)page, count);
2295 mutex_unlock(&task->cred_guard_mutex);
2297 free_page((unsigned long) page);
2299 put_task_struct(task);
2304 static const struct file_operations proc_pid_attr_operations = {
2305 .read = proc_pid_attr_read,
2306 .write = proc_pid_attr_write,
2307 .llseek = generic_file_llseek,
2310 static const struct pid_entry attr_dir_stuff[] = {
2311 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2312 REG("prev", S_IRUGO, proc_pid_attr_operations),
2313 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2314 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2315 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2316 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2319 static int proc_attr_dir_readdir(struct file * filp,
2320 void * dirent, filldir_t filldir)
2322 return proc_pident_readdir(filp,dirent,filldir,
2323 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2326 static const struct file_operations proc_attr_dir_operations = {
2327 .read = generic_read_dir,
2328 .readdir = proc_attr_dir_readdir,
2331 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2332 struct dentry *dentry, struct nameidata *nd)
2334 return proc_pident_lookup(dir, dentry,
2335 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2338 static const struct inode_operations proc_attr_dir_inode_operations = {
2339 .lookup = proc_attr_dir_lookup,
2340 .getattr = pid_getattr,
2341 .setattr = proc_setattr,
2346 #ifdef CONFIG_ELF_CORE
2347 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2348 size_t count, loff_t *ppos)
2350 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2351 struct mm_struct *mm;
2352 char buffer[PROC_NUMBUF];
2360 mm = get_task_mm(task);
2362 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2363 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2364 MMF_DUMP_FILTER_SHIFT));
2366 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2369 put_task_struct(task);
2374 static ssize_t proc_coredump_filter_write(struct file *file,
2375 const char __user *buf,
2379 struct task_struct *task;
2380 struct mm_struct *mm;
2381 char buffer[PROC_NUMBUF], *end;
2388 memset(buffer, 0, sizeof(buffer));
2389 if (count > sizeof(buffer) - 1)
2390 count = sizeof(buffer) - 1;
2391 if (copy_from_user(buffer, buf, count))
2395 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2398 if (end - buffer == 0)
2402 task = get_proc_task(file->f_dentry->d_inode);
2407 mm = get_task_mm(task);
2411 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2413 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2415 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2420 put_task_struct(task);
2425 static const struct file_operations proc_coredump_filter_operations = {
2426 .read = proc_coredump_filter_read,
2427 .write = proc_coredump_filter_write,
2428 .llseek = generic_file_llseek,
2435 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2438 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2439 pid_t tgid = task_tgid_nr_ns(current, ns);
2440 char tmp[PROC_NUMBUF];
2443 sprintf(tmp, "%d", tgid);
2444 return vfs_readlink(dentry,buffer,buflen,tmp);
2447 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2449 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2450 pid_t tgid = task_tgid_nr_ns(current, ns);
2451 char *name = ERR_PTR(-ENOENT);
2455 name = ERR_PTR(-ENOMEM);
2457 sprintf(name, "%d", tgid);
2459 nd_set_link(nd, name);
2463 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2466 char *s = nd_get_link(nd);
2471 static const struct inode_operations proc_self_inode_operations = {
2472 .readlink = proc_self_readlink,
2473 .follow_link = proc_self_follow_link,
2474 .put_link = proc_self_put_link,
2480 * These are the directory entries in the root directory of /proc
2481 * that properly belong to the /proc filesystem, as they describe
2482 * describe something that is process related.
2484 static const struct pid_entry proc_base_stuff[] = {
2485 NOD("self", S_IFLNK|S_IRWXUGO,
2486 &proc_self_inode_operations, NULL, {}),
2490 * Exceptional case: normally we are not allowed to unhash a busy
2491 * directory. In this case, however, we can do it - no aliasing problems
2492 * due to the way we treat inodes.
2494 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2496 struct inode *inode = dentry->d_inode;
2497 struct task_struct *task = get_proc_task(inode);
2499 put_task_struct(task);
2506 static const struct dentry_operations proc_base_dentry_operations =
2508 .d_revalidate = proc_base_revalidate,
2509 .d_delete = pid_delete_dentry,
2512 static struct dentry *proc_base_instantiate(struct inode *dir,
2513 struct dentry *dentry, struct task_struct *task, const void *ptr)
2515 const struct pid_entry *p = ptr;
2516 struct inode *inode;
2517 struct proc_inode *ei;
2518 struct dentry *error;
2520 /* Allocate the inode */
2521 error = ERR_PTR(-ENOMEM);
2522 inode = new_inode(dir->i_sb);
2526 /* Initialize the inode */
2528 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2531 * grab the reference to the task.
2533 ei->pid = get_task_pid(task, PIDTYPE_PID);
2537 inode->i_mode = p->mode;
2538 if (S_ISDIR(inode->i_mode))
2540 if (S_ISLNK(inode->i_mode))
2543 inode->i_op = p->iop;
2545 inode->i_fop = p->fop;
2547 dentry->d_op = &proc_base_dentry_operations;
2548 d_add(dentry, inode);
2557 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2559 struct dentry *error;
2560 struct task_struct *task = get_proc_task(dir);
2561 const struct pid_entry *p, *last;
2563 error = ERR_PTR(-ENOENT);
2568 /* Lookup the directory entry */
2569 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2570 for (p = proc_base_stuff; p <= last; p++) {
2571 if (p->len != dentry->d_name.len)
2573 if (!memcmp(dentry->d_name.name, p->name, p->len))
2579 error = proc_base_instantiate(dir, dentry, task, p);
2582 put_task_struct(task);
2587 static int proc_base_fill_cache(struct file *filp, void *dirent,
2588 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2590 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2591 proc_base_instantiate, task, p);
2594 #ifdef CONFIG_TASK_IO_ACCOUNTING
2595 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2597 struct task_io_accounting acct = task->ioac;
2598 unsigned long flags;
2600 if (whole && lock_task_sighand(task, &flags)) {
2601 struct task_struct *t = task;
2603 task_io_accounting_add(&acct, &task->signal->ioac);
2604 while_each_thread(task, t)
2605 task_io_accounting_add(&acct, &t->ioac);
2607 unlock_task_sighand(task, &flags);
2609 return sprintf(buffer,
2614 "read_bytes: %llu\n"
2615 "write_bytes: %llu\n"
2616 "cancelled_write_bytes: %llu\n",
2617 (unsigned long long)acct.rchar,
2618 (unsigned long long)acct.wchar,
2619 (unsigned long long)acct.syscr,
2620 (unsigned long long)acct.syscw,
2621 (unsigned long long)acct.read_bytes,
2622 (unsigned long long)acct.write_bytes,
2623 (unsigned long long)acct.cancelled_write_bytes);
2626 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2628 return do_io_accounting(task, buffer, 0);
2631 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2633 return do_io_accounting(task, buffer, 1);
2635 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2637 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2638 struct pid *pid, struct task_struct *task)
2640 seq_printf(m, "%08x\n", task->personality);
2647 static const struct file_operations proc_task_operations;
2648 static const struct inode_operations proc_task_inode_operations;
2650 static const struct pid_entry tgid_base_stuff[] = {
2651 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2652 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2653 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2655 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2657 REG("environ", S_IRUSR, proc_environ_operations),
2658 INF("auxv", S_IRUSR, proc_pid_auxv),
2659 ONE("status", S_IRUGO, proc_pid_status),
2660 ONE("personality", S_IRUSR, proc_pid_personality),
2661 REG("limits", S_IRUSR|S_IWUSR, proc_pid_limits_operations),
2662 #ifdef CONFIG_SCHED_DEBUG
2663 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2665 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2666 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2667 INF("syscall", S_IRUSR, proc_pid_syscall),
2669 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2670 ONE("stat", S_IRUGO, proc_tgid_stat),
2671 ONE("statm", S_IRUGO, proc_pid_statm),
2672 REG("maps", S_IRUGO, proc_maps_operations),
2674 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2676 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2677 LNK("cwd", proc_cwd_link),
2678 LNK("root", proc_root_link),
2679 LNK("exe", proc_exe_link),
2680 REG("mounts", S_IRUGO, proc_mounts_operations),
2681 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2682 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2683 #ifdef CONFIG_PROC_PAGE_MONITOR
2684 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2685 REG("smaps", S_IRUGO, proc_smaps_operations),
2686 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2688 #ifdef CONFIG_SECURITY
2689 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2691 #ifdef CONFIG_KALLSYMS
2692 INF("wchan", S_IRUGO, proc_pid_wchan),
2694 #ifdef CONFIG_STACKTRACE
2695 ONE("stack", S_IRUSR, proc_pid_stack),
2697 #ifdef CONFIG_SCHEDSTATS
2698 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2700 #ifdef CONFIG_LATENCYTOP
2701 REG("latency", S_IRUGO, proc_lstats_operations),
2703 #ifdef CONFIG_PROC_PID_CPUSET
2704 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2706 #ifdef CONFIG_CGROUPS
2707 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2709 INF("oom_score", S_IRUGO, proc_oom_score),
2710 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2711 #ifdef CONFIG_AUDITSYSCALL
2712 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2713 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2715 #ifdef CONFIG_FAULT_INJECTION
2716 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2718 #ifdef CONFIG_ELF_CORE
2719 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2721 #ifdef CONFIG_TASK_IO_ACCOUNTING
2722 INF("io", S_IRUGO, proc_tgid_io_accounting),
2726 static int proc_tgid_base_readdir(struct file * filp,
2727 void * dirent, filldir_t filldir)
2729 return proc_pident_readdir(filp,dirent,filldir,
2730 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2733 static const struct file_operations proc_tgid_base_operations = {
2734 .read = generic_read_dir,
2735 .readdir = proc_tgid_base_readdir,
2738 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2739 return proc_pident_lookup(dir, dentry,
2740 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2743 static const struct inode_operations proc_tgid_base_inode_operations = {
2744 .lookup = proc_tgid_base_lookup,
2745 .getattr = pid_getattr,
2746 .setattr = proc_setattr,
2749 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2751 struct dentry *dentry, *leader, *dir;
2752 char buf[PROC_NUMBUF];
2756 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2757 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2759 shrink_dcache_parent(dentry);
2765 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2766 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2771 name.len = strlen(name.name);
2772 dir = d_hash_and_lookup(leader, &name);
2774 goto out_put_leader;
2777 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2778 dentry = d_hash_and_lookup(dir, &name);
2780 shrink_dcache_parent(dentry);
2793 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2794 * @task: task that should be flushed.
2796 * When flushing dentries from proc, one needs to flush them from global
2797 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2798 * in. This call is supposed to do all of this job.
2800 * Looks in the dcache for
2802 * /proc/@tgid/task/@pid
2803 * if either directory is present flushes it and all of it'ts children
2806 * It is safe and reasonable to cache /proc entries for a task until
2807 * that task exits. After that they just clog up the dcache with
2808 * useless entries, possibly causing useful dcache entries to be
2809 * flushed instead. This routine is proved to flush those useless
2810 * dcache entries at process exit time.
2812 * NOTE: This routine is just an optimization so it does not guarantee
2813 * that no dcache entries will exist at process exit time it
2814 * just makes it very unlikely that any will persist.
2817 void proc_flush_task(struct task_struct *task)
2820 struct pid *pid, *tgid;
2823 pid = task_pid(task);
2824 tgid = task_tgid(task);
2826 for (i = 0; i <= pid->level; i++) {
2827 upid = &pid->numbers[i];
2828 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2829 tgid->numbers[i].nr);
2832 upid = &pid->numbers[pid->level];
2834 pid_ns_release_proc(upid->ns);
2837 static struct dentry *proc_pid_instantiate(struct inode *dir,
2838 struct dentry * dentry,
2839 struct task_struct *task, const void *ptr)
2841 struct dentry *error = ERR_PTR(-ENOENT);
2842 struct inode *inode;
2844 inode = proc_pid_make_inode(dir->i_sb, task);
2848 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2849 inode->i_op = &proc_tgid_base_inode_operations;
2850 inode->i_fop = &proc_tgid_base_operations;
2851 inode->i_flags|=S_IMMUTABLE;
2853 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2854 ARRAY_SIZE(tgid_base_stuff));
2856 dentry->d_op = &pid_dentry_operations;
2858 d_add(dentry, inode);
2859 /* Close the race of the process dying before we return the dentry */
2860 if (pid_revalidate(dentry, NULL))
2866 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2868 struct dentry *result;
2869 struct task_struct *task;
2871 struct pid_namespace *ns;
2873 result = proc_base_lookup(dir, dentry);
2874 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2877 tgid = name_to_int(dentry);
2881 ns = dentry->d_sb->s_fs_info;
2883 task = find_task_by_pid_ns(tgid, ns);
2885 get_task_struct(task);
2890 result = proc_pid_instantiate(dir, dentry, task, NULL);
2891 put_task_struct(task);
2897 * Find the first task with tgid >= tgid
2902 struct task_struct *task;
2904 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2909 put_task_struct(iter.task);
2913 pid = find_ge_pid(iter.tgid, ns);
2915 iter.tgid = pid_nr_ns(pid, ns);
2916 iter.task = pid_task(pid, PIDTYPE_PID);
2917 /* What we to know is if the pid we have find is the
2918 * pid of a thread_group_leader. Testing for task
2919 * being a thread_group_leader is the obvious thing
2920 * todo but there is a window when it fails, due to
2921 * the pid transfer logic in de_thread.
2923 * So we perform the straight forward test of seeing
2924 * if the pid we have found is the pid of a thread
2925 * group leader, and don't worry if the task we have
2926 * found doesn't happen to be a thread group leader.
2927 * As we don't care in the case of readdir.
2929 if (!iter.task || !has_group_leader_pid(iter.task)) {
2933 get_task_struct(iter.task);
2939 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2941 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2942 struct tgid_iter iter)
2944 char name[PROC_NUMBUF];
2945 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2946 return proc_fill_cache(filp, dirent, filldir, name, len,
2947 proc_pid_instantiate, iter.task, NULL);
2950 /* for the /proc/ directory itself, after non-process stuff has been done */
2951 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2953 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2954 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2955 struct tgid_iter iter;
2956 struct pid_namespace *ns;
2961 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2962 const struct pid_entry *p = &proc_base_stuff[nr];
2963 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2967 ns = filp->f_dentry->d_sb->s_fs_info;
2969 iter.tgid = filp->f_pos - TGID_OFFSET;
2970 for (iter = next_tgid(ns, iter);
2972 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2973 filp->f_pos = iter.tgid + TGID_OFFSET;
2974 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2975 put_task_struct(iter.task);
2979 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2981 put_task_struct(reaper);
2989 static const struct pid_entry tid_base_stuff[] = {
2990 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2991 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2992 REG("environ", S_IRUSR, proc_environ_operations),
2993 INF("auxv", S_IRUSR, proc_pid_auxv),
2994 ONE("status", S_IRUGO, proc_pid_status),
2995 ONE("personality", S_IRUSR, proc_pid_personality),
2996 REG("limits", S_IRUSR|S_IWUSR, proc_pid_limits_operations),
2997 #ifdef CONFIG_SCHED_DEBUG
2998 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3000 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3001 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3002 INF("syscall", S_IRUSR, proc_pid_syscall),
3004 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3005 ONE("stat", S_IRUGO, proc_tid_stat),
3006 ONE("statm", S_IRUGO, proc_pid_statm),
3007 REG("maps", S_IRUGO, proc_maps_operations),
3009 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3011 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3012 LNK("cwd", proc_cwd_link),
3013 LNK("root", proc_root_link),
3014 LNK("exe", proc_exe_link),
3015 REG("mounts", S_IRUGO, proc_mounts_operations),
3016 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3017 #ifdef CONFIG_PROC_PAGE_MONITOR
3018 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3019 REG("smaps", S_IRUGO, proc_smaps_operations),
3020 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3022 #ifdef CONFIG_SECURITY
3023 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3025 #ifdef CONFIG_KALLSYMS
3026 INF("wchan", S_IRUGO, proc_pid_wchan),
3028 #ifdef CONFIG_STACKTRACE
3029 ONE("stack", S_IRUSR, proc_pid_stack),
3031 #ifdef CONFIG_SCHEDSTATS
3032 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3034 #ifdef CONFIG_LATENCYTOP
3035 REG("latency", S_IRUGO, proc_lstats_operations),
3037 #ifdef CONFIG_PROC_PID_CPUSET
3038 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3040 #ifdef CONFIG_CGROUPS
3041 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3043 INF("oom_score", S_IRUGO, proc_oom_score),
3044 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3045 #ifdef CONFIG_AUDITSYSCALL
3046 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3047 REG("sessionid", S_IRUSR, proc_sessionid_operations),
3049 #ifdef CONFIG_FAULT_INJECTION
3050 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3052 #ifdef CONFIG_TASK_IO_ACCOUNTING
3053 INF("io", S_IRUGO, proc_tid_io_accounting),
3057 static int proc_tid_base_readdir(struct file * filp,
3058 void * dirent, filldir_t filldir)
3060 return proc_pident_readdir(filp,dirent,filldir,
3061 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3064 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3065 return proc_pident_lookup(dir, dentry,
3066 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3069 static const struct file_operations proc_tid_base_operations = {
3070 .read = generic_read_dir,
3071 .readdir = proc_tid_base_readdir,
3074 static const struct inode_operations proc_tid_base_inode_operations = {
3075 .lookup = proc_tid_base_lookup,
3076 .getattr = pid_getattr,
3077 .setattr = proc_setattr,
3080 static struct dentry *proc_task_instantiate(struct inode *dir,
3081 struct dentry *dentry, struct task_struct *task, const void *ptr)
3083 struct dentry *error = ERR_PTR(-ENOENT);
3084 struct inode *inode;
3085 inode = proc_pid_make_inode(dir->i_sb, task);
3089 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3090 inode->i_op = &proc_tid_base_inode_operations;
3091 inode->i_fop = &proc_tid_base_operations;
3092 inode->i_flags|=S_IMMUTABLE;
3094 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3095 ARRAY_SIZE(tid_base_stuff));
3097 dentry->d_op = &pid_dentry_operations;
3099 d_add(dentry, inode);
3100 /* Close the race of the process dying before we return the dentry */
3101 if (pid_revalidate(dentry, NULL))
3107 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3109 struct dentry *result = ERR_PTR(-ENOENT);
3110 struct task_struct *task;
3111 struct task_struct *leader = get_proc_task(dir);
3113 struct pid_namespace *ns;
3118 tid = name_to_int(dentry);
3122 ns = dentry->d_sb->s_fs_info;
3124 task = find_task_by_pid_ns(tid, ns);
3126 get_task_struct(task);
3130 if (!same_thread_group(leader, task))
3133 result = proc_task_instantiate(dir, dentry, task, NULL);
3135 put_task_struct(task);
3137 put_task_struct(leader);
3143 * Find the first tid of a thread group to return to user space.
3145 * Usually this is just the thread group leader, but if the users
3146 * buffer was too small or there was a seek into the middle of the
3147 * directory we have more work todo.
3149 * In the case of a short read we start with find_task_by_pid.
3151 * In the case of a seek we start with the leader and walk nr
3154 static struct task_struct *first_tid(struct task_struct *leader,
3155 int tid, int nr, struct pid_namespace *ns)
3157 struct task_struct *pos;
3160 /* Attempt to start with the pid of a thread */
3161 if (tid && (nr > 0)) {
3162 pos = find_task_by_pid_ns(tid, ns);
3163 if (pos && (pos->group_leader == leader))
3167 /* If nr exceeds the number of threads there is nothing todo */
3169 if (nr && nr >= get_nr_threads(leader))
3172 /* If we haven't found our starting place yet start
3173 * with the leader and walk nr threads forward.
3175 for (pos = leader; nr > 0; --nr) {
3176 pos = next_thread(pos);
3177 if (pos == leader) {
3183 get_task_struct(pos);
3190 * Find the next thread in the thread list.
3191 * Return NULL if there is an error or no next thread.
3193 * The reference to the input task_struct is released.
3195 static struct task_struct *next_tid(struct task_struct *start)
3197 struct task_struct *pos = NULL;
3199 if (pid_alive(start)) {
3200 pos = next_thread(start);
3201 if (thread_group_leader(pos))
3204 get_task_struct(pos);
3207 put_task_struct(start);
3211 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3212 struct task_struct *task, int tid)
3214 char name[PROC_NUMBUF];
3215 int len = snprintf(name, sizeof(name), "%d", tid);
3216 return proc_fill_cache(filp, dirent, filldir, name, len,
3217 proc_task_instantiate, task, NULL);
3220 /* for the /proc/TGID/task/ directories */
3221 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3223 struct dentry *dentry = filp->f_path.dentry;
3224 struct inode *inode = dentry->d_inode;
3225 struct task_struct *leader = NULL;
3226 struct task_struct *task;
3227 int retval = -ENOENT;
3230 struct pid_namespace *ns;
3232 task = get_proc_task(inode);
3236 if (pid_alive(task)) {
3237 leader = task->group_leader;
3238 get_task_struct(leader);
3241 put_task_struct(task);
3246 switch ((unsigned long)filp->f_pos) {
3249 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3254 ino = parent_ino(dentry);
3255 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3261 /* f_version caches the tgid value that the last readdir call couldn't
3262 * return. lseek aka telldir automagically resets f_version to 0.
3264 ns = filp->f_dentry->d_sb->s_fs_info;
3265 tid = (int)filp->f_version;
3266 filp->f_version = 0;
3267 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3269 task = next_tid(task), filp->f_pos++) {
3270 tid = task_pid_nr_ns(task, ns);
3271 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3272 /* returning this tgid failed, save it as the first
3273 * pid for the next readir call */
3274 filp->f_version = (u64)tid;
3275 put_task_struct(task);
3280 put_task_struct(leader);
3285 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3287 struct inode *inode = dentry->d_inode;
3288 struct task_struct *p = get_proc_task(inode);
3289 generic_fillattr(inode, stat);
3292 stat->nlink += get_nr_threads(p);
3299 static const struct inode_operations proc_task_inode_operations = {
3300 .lookup = proc_task_lookup,
3301 .getattr = proc_task_getattr,
3302 .setattr = proc_setattr,
3305 static const struct file_operations proc_task_operations = {
3306 .read = generic_read_dir,
3307 .readdir = proc_task_readdir,