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>
87 * Implementing inode permission operations in /proc is almost
88 * certainly an error. Permission checks need to happen during
89 * each system call not at open time. The reason is that most of
90 * what we wish to check for permissions in /proc varies at runtime.
92 * The classic example of a problem is opening file descriptors
93 * in /proc for a task before it execs a suid executable.
100 const struct inode_operations *iop;
101 const struct file_operations *fop;
105 #define NOD(NAME, MODE, IOP, FOP, OP) { \
107 .len = sizeof(NAME) - 1, \
114 #define DIR(NAME, MODE, iops, fops) \
115 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
116 #define LNK(NAME, get_link) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = get_link } )
120 #define REG(NAME, MODE, fops) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
122 #define INF(NAME, MODE, read) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = read } )
126 #define ONE(NAME, MODE, show) \
127 NOD(NAME, (S_IFREG|(MODE)), \
128 NULL, &proc_single_file_operations, \
129 { .proc_show = show } )
132 * Count the number of hardlinks for the pid_entry table, excluding the .
135 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
142 for (i = 0; i < n; ++i) {
143 if (S_ISDIR(entries[i].mode))
150 static int get_fs_path(struct task_struct *task, struct path *path, bool root)
152 struct fs_struct *fs;
153 int result = -ENOENT;
158 read_lock(&fs->lock);
159 *path = root ? fs->root : fs->pwd;
161 read_unlock(&fs->lock);
168 static int get_nr_threads(struct task_struct *tsk)
173 if (lock_task_sighand(tsk, &flags)) {
174 count = atomic_read(&tsk->signal->count);
175 unlock_task_sighand(tsk, &flags);
180 static int proc_cwd_link(struct inode *inode, struct path *path)
182 struct task_struct *task = get_proc_task(inode);
183 int result = -ENOENT;
186 result = get_fs_path(task, path, 0);
187 put_task_struct(task);
192 static int proc_root_link(struct inode *inode, struct path *path)
194 struct task_struct *task = get_proc_task(inode);
195 int result = -ENOENT;
198 result = get_fs_path(task, path, 1);
199 put_task_struct(task);
205 * Return zero if current may access user memory in @task, -error if not.
207 static int check_mem_permission(struct task_struct *task)
210 * A task can always look at itself, in case it chooses
211 * to use system calls instead of load instructions.
217 * If current is actively ptrace'ing, and would also be
218 * permitted to freshly attach with ptrace now, permit it.
220 if (task_is_stopped_or_traced(task)) {
223 match = (tracehook_tracer_task(task) == current);
225 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
230 * Noone else is allowed.
235 struct mm_struct *mm_for_maps(struct task_struct *task)
237 struct mm_struct *mm;
239 if (mutex_lock_killable(&task->cred_guard_mutex))
242 mm = get_task_mm(task);
243 if (mm && mm != current->mm &&
244 !ptrace_may_access(task, PTRACE_MODE_READ)) {
248 mutex_unlock(&task->cred_guard_mutex);
253 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
257 struct mm_struct *mm = get_task_mm(task);
261 goto out_mm; /* Shh! No looking before we're done */
263 len = mm->arg_end - mm->arg_start;
268 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
270 // If the nul at the end of args has been overwritten, then
271 // assume application is using setproctitle(3).
272 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
273 len = strnlen(buffer, res);
277 len = mm->env_end - mm->env_start;
278 if (len > PAGE_SIZE - res)
279 len = PAGE_SIZE - res;
280 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
281 res = strnlen(buffer, res);
290 static int proc_pid_auxv(struct task_struct *task, char *buffer)
293 struct mm_struct *mm = get_task_mm(task);
295 unsigned int nwords = 0;
298 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
299 res = nwords * sizeof(mm->saved_auxv[0]);
302 memcpy(buffer, mm->saved_auxv, res);
309 #ifdef CONFIG_KALLSYMS
311 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
312 * Returns the resolved symbol. If that fails, simply return the address.
314 static int proc_pid_wchan(struct task_struct *task, char *buffer)
317 char symname[KSYM_NAME_LEN];
319 wchan = get_wchan(task);
321 if (lookup_symbol_name(wchan, symname) < 0)
322 if (!ptrace_may_access(task, PTRACE_MODE_READ))
325 return sprintf(buffer, "%lu", wchan);
327 return sprintf(buffer, "%s", symname);
329 #endif /* CONFIG_KALLSYMS */
331 #ifdef CONFIG_STACKTRACE
333 #define MAX_STACK_TRACE_DEPTH 64
335 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
336 struct pid *pid, struct task_struct *task)
338 struct stack_trace trace;
339 unsigned long *entries;
342 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
346 trace.nr_entries = 0;
347 trace.max_entries = MAX_STACK_TRACE_DEPTH;
348 trace.entries = entries;
350 save_stack_trace_tsk(task, &trace);
352 for (i = 0; i < trace.nr_entries; i++) {
353 seq_printf(m, "[<%p>] %pS\n",
354 (void *)entries[i], (void *)entries[i]);
362 #ifdef CONFIG_SCHEDSTATS
364 * Provides /proc/PID/schedstat
366 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
368 return sprintf(buffer, "%llu %llu %lu\n",
369 (unsigned long long)task->se.sum_exec_runtime,
370 (unsigned long long)task->sched_info.run_delay,
371 task->sched_info.pcount);
375 #ifdef CONFIG_LATENCYTOP
376 static int lstats_show_proc(struct seq_file *m, void *v)
379 struct inode *inode = m->private;
380 struct task_struct *task = get_proc_task(inode);
384 seq_puts(m, "Latency Top version : v0.1\n");
385 for (i = 0; i < 32; i++) {
386 if (task->latency_record[i].backtrace[0]) {
388 seq_printf(m, "%i %li %li ",
389 task->latency_record[i].count,
390 task->latency_record[i].time,
391 task->latency_record[i].max);
392 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
393 char sym[KSYM_SYMBOL_LEN];
395 if (!task->latency_record[i].backtrace[q])
397 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
399 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
400 c = strchr(sym, '+');
403 seq_printf(m, "%s ", sym);
409 put_task_struct(task);
413 static int lstats_open(struct inode *inode, struct file *file)
415 return single_open(file, lstats_show_proc, inode);
418 static ssize_t lstats_write(struct file *file, const char __user *buf,
419 size_t count, loff_t *offs)
421 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
425 clear_all_latency_tracing(task);
426 put_task_struct(task);
431 static const struct file_operations proc_lstats_operations = {
434 .write = lstats_write,
436 .release = single_release,
441 /* The badness from the OOM killer */
442 unsigned long badness(struct task_struct *p, unsigned long uptime);
443 static int proc_oom_score(struct task_struct *task, char *buffer)
445 unsigned long points;
446 struct timespec uptime;
448 do_posix_clock_monotonic_gettime(&uptime);
449 read_lock(&tasklist_lock);
450 points = badness(task->group_leader, uptime.tv_sec);
451 read_unlock(&tasklist_lock);
452 return sprintf(buffer, "%lu\n", points);
460 static const struct limit_names lnames[RLIM_NLIMITS] = {
461 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
462 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
463 [RLIMIT_DATA] = {"Max data size", "bytes"},
464 [RLIMIT_STACK] = {"Max stack size", "bytes"},
465 [RLIMIT_CORE] = {"Max core file size", "bytes"},
466 [RLIMIT_RSS] = {"Max resident set", "bytes"},
467 [RLIMIT_NPROC] = {"Max processes", "processes"},
468 [RLIMIT_NOFILE] = {"Max open files", "files"},
469 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
470 [RLIMIT_AS] = {"Max address space", "bytes"},
471 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
472 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
473 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
474 [RLIMIT_NICE] = {"Max nice priority", NULL},
475 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
476 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
479 /* Display limits for a process */
480 static ssize_t limits_read(struct file *file, char __user *buf, size_t rcount,
483 struct rlimit rlim[RLIM_NLIMITS];
484 struct task_struct *task;
490 task = get_proc_task(file->f_path.dentry->d_inode);
493 if (!lock_task_sighand(task, &flags)) {
494 put_task_struct(task);
497 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
498 unlock_task_sighand(task, &flags);
499 put_task_struct(task);
501 bufptr = (char *)__get_free_page(GFP_TEMPORARY);
506 * print the file header
508 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
509 "Limit", "Soft Limit", "Hard Limit", "Units");
511 for (i = 0; i < RLIM_NLIMITS; i++) {
512 if (rlim[i].rlim_cur == RLIM_INFINITY)
513 count += sprintf(&bufptr[count], "%-25s %-20s ",
514 lnames[i].name, "unlimited");
516 count += sprintf(&bufptr[count], "%-25s %-20lu ",
517 lnames[i].name, rlim[i].rlim_cur);
519 if (rlim[i].rlim_max == RLIM_INFINITY)
520 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
522 count += sprintf(&bufptr[count], "%-20lu ",
526 count += sprintf(&bufptr[count], "%-10s\n",
529 count += sprintf(&bufptr[count], "\n");
532 count = simple_read_from_buffer(buf, rcount, ppos, bufptr, count);
534 free_page((unsigned long)bufptr);
539 static ssize_t limits_write(struct file *file, const char __user *buf,
540 size_t count, loff_t *ppos)
542 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
543 char str[32 + 1 + 16 + 1 + 16 + 1], *delim, *next;
544 struct rlimit new_rlimit;
552 if (copy_from_user(str, buf, min(count, sizeof(str) - 1))) {
557 str[min(count, sizeof(str) - 1)] = 0;
559 delim = strchr(str, '=');
564 *delim++ = 0; /* for easy 'str' usage */
565 new_rlimit.rlim_cur = simple_strtoul(delim, &next, 0);
567 if (strncmp(delim, "unlimited:", 10)) {
571 new_rlimit.rlim_cur = RLIM_INFINITY;
572 next = delim + 9; /* move to ':' */
575 new_rlimit.rlim_max = simple_strtoul(delim, &next, 0);
577 if (strcmp(delim, "unlimited")) {
581 new_rlimit.rlim_max = RLIM_INFINITY;
584 for (i = 0; i < RLIM_NLIMITS; i++)
585 if (!strcmp(str, lnames[i].name))
587 if (i >= RLIM_NLIMITS) {
592 ret = do_setrlimit(task, i, &new_rlimit);
597 put_task_struct(task);
602 static const struct file_operations proc_pid_limits_operations = {
604 .write = limits_write,
607 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
608 static int proc_pid_syscall(struct task_struct *task, char *buffer)
611 unsigned long args[6], sp, pc;
613 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
614 return sprintf(buffer, "running\n");
617 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
619 return sprintf(buffer,
620 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
622 args[0], args[1], args[2], args[3], args[4], args[5],
625 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
627 /************************************************************************/
628 /* Here the fs part begins */
629 /************************************************************************/
631 /* permission checks */
632 static int proc_fd_access_allowed(struct inode *inode)
634 struct task_struct *task;
636 /* Allow access to a task's file descriptors if it is us or we
637 * may use ptrace attach to the process and find out that
640 task = get_proc_task(inode);
642 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
643 put_task_struct(task);
648 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
651 struct inode *inode = dentry->d_inode;
653 if (attr->ia_valid & ATTR_MODE)
656 error = inode_change_ok(inode, attr);
658 error = inode_setattr(inode, attr);
662 static const struct inode_operations proc_def_inode_operations = {
663 .setattr = proc_setattr,
666 static int mounts_open_common(struct inode *inode, struct file *file,
667 const struct seq_operations *op)
669 struct task_struct *task = get_proc_task(inode);
671 struct mnt_namespace *ns = NULL;
673 struct proc_mounts *p;
678 nsp = task_nsproxy(task);
685 if (ns && get_fs_path(task, &root, 1) == 0)
687 put_task_struct(task);
696 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
700 file->private_data = &p->m;
701 ret = seq_open(file, op);
708 p->event = ns->event;
722 static int mounts_release(struct inode *inode, struct file *file)
724 struct proc_mounts *p = file->private_data;
727 return seq_release(inode, file);
730 static unsigned mounts_poll(struct file *file, poll_table *wait)
732 struct proc_mounts *p = file->private_data;
733 struct mnt_namespace *ns = p->ns;
734 unsigned res = POLLIN | POLLRDNORM;
736 poll_wait(file, &ns->poll, wait);
738 spin_lock(&vfsmount_lock);
739 if (p->event != ns->event) {
740 p->event = ns->event;
741 res |= POLLERR | POLLPRI;
743 spin_unlock(&vfsmount_lock);
748 static int mounts_open(struct inode *inode, struct file *file)
750 return mounts_open_common(inode, file, &mounts_op);
753 static const struct file_operations proc_mounts_operations = {
757 .release = mounts_release,
761 static int mountinfo_open(struct inode *inode, struct file *file)
763 return mounts_open_common(inode, file, &mountinfo_op);
766 static const struct file_operations proc_mountinfo_operations = {
767 .open = mountinfo_open,
770 .release = mounts_release,
774 static int mountstats_open(struct inode *inode, struct file *file)
776 return mounts_open_common(inode, file, &mountstats_op);
779 static const struct file_operations proc_mountstats_operations = {
780 .open = mountstats_open,
783 .release = mounts_release,
786 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
788 static ssize_t proc_info_read(struct file * file, char __user * buf,
789 size_t count, loff_t *ppos)
791 struct inode * inode = file->f_path.dentry->d_inode;
794 struct task_struct *task = get_proc_task(inode);
800 if (count > PROC_BLOCK_SIZE)
801 count = PROC_BLOCK_SIZE;
804 if (!(page = __get_free_page(GFP_TEMPORARY)))
807 length = PROC_I(inode)->op.proc_read(task, (char*)page);
810 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
813 put_task_struct(task);
818 static const struct file_operations proc_info_file_operations = {
819 .read = proc_info_read,
822 static int proc_single_show(struct seq_file *m, void *v)
824 struct inode *inode = m->private;
825 struct pid_namespace *ns;
827 struct task_struct *task;
830 ns = inode->i_sb->s_fs_info;
831 pid = proc_pid(inode);
832 task = get_pid_task(pid, PIDTYPE_PID);
836 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
838 put_task_struct(task);
842 static int proc_single_open(struct inode *inode, struct file *filp)
845 ret = single_open(filp, proc_single_show, NULL);
847 struct seq_file *m = filp->private_data;
854 static const struct file_operations proc_single_file_operations = {
855 .open = proc_single_open,
858 .release = single_release,
861 static int mem_open(struct inode* inode, struct file* file)
863 file->private_data = (void*)((long)current->self_exec_id);
867 static ssize_t mem_read(struct file * file, char __user * buf,
868 size_t count, loff_t *ppos)
870 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
872 unsigned long src = *ppos;
874 struct mm_struct *mm;
879 if (check_mem_permission(task))
883 page = (char *)__get_free_page(GFP_TEMPORARY);
889 mm = get_task_mm(task);
895 if (file->private_data != (void*)((long)current->self_exec_id))
901 int this_len, retval;
903 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
904 retval = access_process_vm(task, src, page, this_len, 0);
905 if (!retval || check_mem_permission(task)) {
911 if (copy_to_user(buf, page, retval)) {
926 free_page((unsigned long) page);
928 put_task_struct(task);
933 #define mem_write NULL
936 /* This is a security hazard */
937 static ssize_t mem_write(struct file * file, const char __user *buf,
938 size_t count, loff_t *ppos)
942 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
943 unsigned long dst = *ppos;
949 if (check_mem_permission(task))
953 page = (char *)__get_free_page(GFP_TEMPORARY);
959 int this_len, retval;
961 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
962 if (copy_from_user(page, buf, this_len)) {
966 retval = access_process_vm(task, dst, page, this_len, 1);
978 free_page((unsigned long) page);
980 put_task_struct(task);
986 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
990 file->f_pos = offset;
993 file->f_pos += offset;
998 force_successful_syscall_return();
1002 static const struct file_operations proc_mem_operations = {
1003 .llseek = mem_lseek,
1009 static ssize_t environ_read(struct file *file, char __user *buf,
1010 size_t count, loff_t *ppos)
1012 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1014 unsigned long src = *ppos;
1016 struct mm_struct *mm;
1021 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1025 page = (char *)__get_free_page(GFP_TEMPORARY);
1031 mm = get_task_mm(task);
1036 int this_len, retval, max_len;
1038 this_len = mm->env_end - (mm->env_start + src);
1043 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1044 this_len = (this_len > max_len) ? max_len : this_len;
1046 retval = access_process_vm(task, (mm->env_start + src),
1054 if (copy_to_user(buf, page, retval)) {
1068 free_page((unsigned long) page);
1070 put_task_struct(task);
1075 static const struct file_operations proc_environ_operations = {
1076 .read = environ_read,
1079 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1080 size_t count, loff_t *ppos)
1082 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1083 char buffer[PROC_NUMBUF];
1085 int oom_adjust = OOM_DISABLE;
1086 unsigned long flags;
1091 if (lock_task_sighand(task, &flags)) {
1092 oom_adjust = task->signal->oom_adj;
1093 unlock_task_sighand(task, &flags);
1096 put_task_struct(task);
1098 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1100 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1103 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1104 size_t count, loff_t *ppos)
1106 struct task_struct *task;
1107 char buffer[PROC_NUMBUF];
1109 unsigned long flags;
1112 memset(buffer, 0, sizeof(buffer));
1113 if (count > sizeof(buffer) - 1)
1114 count = sizeof(buffer) - 1;
1115 if (copy_from_user(buffer, buf, count))
1118 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1121 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1122 oom_adjust != OOM_DISABLE)
1125 task = get_proc_task(file->f_path.dentry->d_inode);
1128 if (!lock_task_sighand(task, &flags)) {
1129 put_task_struct(task);
1133 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1134 unlock_task_sighand(task, &flags);
1135 put_task_struct(task);
1139 task->signal->oom_adj = oom_adjust;
1141 unlock_task_sighand(task, &flags);
1142 put_task_struct(task);
1147 static const struct file_operations proc_oom_adjust_operations = {
1148 .read = oom_adjust_read,
1149 .write = oom_adjust_write,
1152 #ifdef CONFIG_AUDITSYSCALL
1153 #define TMPBUFLEN 21
1154 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1155 size_t count, loff_t *ppos)
1157 struct inode * inode = file->f_path.dentry->d_inode;
1158 struct task_struct *task = get_proc_task(inode);
1160 char tmpbuf[TMPBUFLEN];
1164 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1165 audit_get_loginuid(task));
1166 put_task_struct(task);
1167 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1170 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1171 size_t count, loff_t *ppos)
1173 struct inode * inode = file->f_path.dentry->d_inode;
1178 if (!capable(CAP_AUDIT_CONTROL))
1181 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1184 if (count >= PAGE_SIZE)
1185 count = PAGE_SIZE - 1;
1188 /* No partial writes. */
1191 page = (char*)__get_free_page(GFP_TEMPORARY);
1195 if (copy_from_user(page, buf, count))
1199 loginuid = simple_strtoul(page, &tmp, 10);
1205 length = audit_set_loginuid(current, loginuid);
1206 if (likely(length == 0))
1210 free_page((unsigned long) page);
1214 static const struct file_operations proc_loginuid_operations = {
1215 .read = proc_loginuid_read,
1216 .write = proc_loginuid_write,
1219 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1220 size_t count, loff_t *ppos)
1222 struct inode * inode = file->f_path.dentry->d_inode;
1223 struct task_struct *task = get_proc_task(inode);
1225 char tmpbuf[TMPBUFLEN];
1229 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1230 audit_get_sessionid(task));
1231 put_task_struct(task);
1232 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1235 static const struct file_operations proc_sessionid_operations = {
1236 .read = proc_sessionid_read,
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,
1292 #ifdef CONFIG_SCHED_DEBUG
1294 * Print out various scheduling related per-task fields:
1296 static int sched_show(struct seq_file *m, void *v)
1298 struct inode *inode = m->private;
1299 struct task_struct *p;
1301 p = get_proc_task(inode);
1304 proc_sched_show_task(p, m);
1312 sched_write(struct file *file, const char __user *buf,
1313 size_t count, loff_t *offset)
1315 struct inode *inode = file->f_path.dentry->d_inode;
1316 struct task_struct *p;
1318 p = get_proc_task(inode);
1321 proc_sched_set_task(p);
1328 static int sched_open(struct inode *inode, struct file *filp)
1332 ret = single_open(filp, sched_show, NULL);
1334 struct seq_file *m = filp->private_data;
1341 static const struct file_operations proc_pid_sched_operations = {
1344 .write = sched_write,
1345 .llseek = seq_lseek,
1346 .release = single_release,
1351 static ssize_t comm_write(struct file *file, const char __user *buf,
1352 size_t count, loff_t *offset)
1354 struct inode *inode = file->f_path.dentry->d_inode;
1355 struct task_struct *p;
1356 char buffer[TASK_COMM_LEN];
1358 memset(buffer, 0, sizeof(buffer));
1359 if (count > sizeof(buffer) - 1)
1360 count = sizeof(buffer) - 1;
1361 if (copy_from_user(buffer, buf, count))
1364 p = get_proc_task(inode);
1368 if (same_thread_group(current, p))
1369 set_task_comm(p, buffer);
1378 static int comm_show(struct seq_file *m, void *v)
1380 struct inode *inode = m->private;
1381 struct task_struct *p;
1383 p = get_proc_task(inode);
1388 seq_printf(m, "%s\n", p->comm);
1396 static int comm_open(struct inode *inode, struct file *filp)
1400 ret = single_open(filp, comm_show, NULL);
1402 struct seq_file *m = filp->private_data;
1409 static const struct file_operations proc_pid_set_comm_operations = {
1412 .write = comm_write,
1413 .llseek = seq_lseek,
1414 .release = single_release,
1418 * We added or removed a vma mapping the executable. The vmas are only mapped
1419 * during exec and are not mapped with the mmap system call.
1420 * Callers must hold down_write() on the mm's mmap_sem for these
1422 void added_exe_file_vma(struct mm_struct *mm)
1424 mm->num_exe_file_vmas++;
1427 void removed_exe_file_vma(struct mm_struct *mm)
1429 mm->num_exe_file_vmas--;
1430 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1432 mm->exe_file = NULL;
1437 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1440 get_file(new_exe_file);
1443 mm->exe_file = new_exe_file;
1444 mm->num_exe_file_vmas = 0;
1447 struct file *get_mm_exe_file(struct mm_struct *mm)
1449 struct file *exe_file;
1451 /* We need mmap_sem to protect against races with removal of
1452 * VM_EXECUTABLE vmas */
1453 down_read(&mm->mmap_sem);
1454 exe_file = mm->exe_file;
1457 up_read(&mm->mmap_sem);
1461 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1463 /* It's safe to write the exe_file pointer without exe_file_lock because
1464 * this is called during fork when the task is not yet in /proc */
1465 newmm->exe_file = get_mm_exe_file(oldmm);
1468 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1470 struct task_struct *task;
1471 struct mm_struct *mm;
1472 struct file *exe_file;
1474 task = get_proc_task(inode);
1477 mm = get_task_mm(task);
1478 put_task_struct(task);
1481 exe_file = get_mm_exe_file(mm);
1484 *exe_path = exe_file->f_path;
1485 path_get(&exe_file->f_path);
1492 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1494 struct inode *inode = dentry->d_inode;
1495 int error = -EACCES;
1497 /* We don't need a base pointer in the /proc filesystem */
1498 path_put(&nd->path);
1500 /* Are we allowed to snoop on the tasks file descriptors? */
1501 if (!proc_fd_access_allowed(inode))
1504 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1506 return ERR_PTR(error);
1509 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1511 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1518 pathname = d_path(path, tmp, PAGE_SIZE);
1519 len = PTR_ERR(pathname);
1520 if (IS_ERR(pathname))
1522 len = tmp + PAGE_SIZE - 1 - pathname;
1526 if (copy_to_user(buffer, pathname, len))
1529 free_page((unsigned long)tmp);
1533 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1535 int error = -EACCES;
1536 struct inode *inode = dentry->d_inode;
1539 /* Are we allowed to snoop on the tasks file descriptors? */
1540 if (!proc_fd_access_allowed(inode))
1543 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1547 error = do_proc_readlink(&path, buffer, buflen);
1553 static const struct inode_operations proc_pid_link_inode_operations = {
1554 .readlink = proc_pid_readlink,
1555 .follow_link = proc_pid_follow_link,
1556 .setattr = proc_setattr,
1560 /* building an inode */
1562 static int task_dumpable(struct task_struct *task)
1565 struct mm_struct *mm;
1570 dumpable = get_dumpable(mm);
1578 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1580 struct inode * inode;
1581 struct proc_inode *ei;
1582 const struct cred *cred;
1584 /* We need a new inode */
1586 inode = new_inode(sb);
1592 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1593 inode->i_op = &proc_def_inode_operations;
1596 * grab the reference to task.
1598 ei->pid = get_task_pid(task, PIDTYPE_PID);
1602 if (task_dumpable(task)) {
1604 cred = __task_cred(task);
1605 inode->i_uid = cred->euid;
1606 inode->i_gid = cred->egid;
1609 security_task_to_inode(task, inode);
1619 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1621 struct inode *inode = dentry->d_inode;
1622 struct task_struct *task;
1623 const struct cred *cred;
1625 generic_fillattr(inode, stat);
1630 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1632 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1633 task_dumpable(task)) {
1634 cred = __task_cred(task);
1635 stat->uid = cred->euid;
1636 stat->gid = cred->egid;
1646 * Exceptional case: normally we are not allowed to unhash a busy
1647 * directory. In this case, however, we can do it - no aliasing problems
1648 * due to the way we treat inodes.
1650 * Rewrite the inode's ownerships here because the owning task may have
1651 * performed a setuid(), etc.
1653 * Before the /proc/pid/status file was created the only way to read
1654 * the effective uid of a /process was to stat /proc/pid. Reading
1655 * /proc/pid/status is slow enough that procps and other packages
1656 * kept stating /proc/pid. To keep the rules in /proc simple I have
1657 * made this apply to all per process world readable and executable
1660 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1662 struct inode *inode = dentry->d_inode;
1663 struct task_struct *task = get_proc_task(inode);
1664 const struct cred *cred;
1667 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1668 task_dumpable(task)) {
1670 cred = __task_cred(task);
1671 inode->i_uid = cred->euid;
1672 inode->i_gid = cred->egid;
1678 inode->i_mode &= ~(S_ISUID | S_ISGID);
1679 security_task_to_inode(task, inode);
1680 put_task_struct(task);
1687 static int pid_delete_dentry(struct dentry * dentry)
1689 /* Is the task we represent dead?
1690 * If so, then don't put the dentry on the lru list,
1691 * kill it immediately.
1693 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1696 static const struct dentry_operations pid_dentry_operations =
1698 .d_revalidate = pid_revalidate,
1699 .d_delete = pid_delete_dentry,
1704 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1705 struct task_struct *, const void *);
1708 * Fill a directory entry.
1710 * If possible create the dcache entry and derive our inode number and
1711 * file type from dcache entry.
1713 * Since all of the proc inode numbers are dynamically generated, the inode
1714 * numbers do not exist until the inode is cache. This means creating the
1715 * the dcache entry in readdir is necessary to keep the inode numbers
1716 * reported by readdir in sync with the inode numbers reported
1719 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1720 char *name, int len,
1721 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1723 struct dentry *child, *dir = filp->f_path.dentry;
1724 struct inode *inode;
1727 unsigned type = DT_UNKNOWN;
1731 qname.hash = full_name_hash(name, len);
1733 child = d_lookup(dir, &qname);
1736 new = d_alloc(dir, &qname);
1738 child = instantiate(dir->d_inode, new, task, ptr);
1745 if (!child || IS_ERR(child) || !child->d_inode)
1746 goto end_instantiate;
1747 inode = child->d_inode;
1750 type = inode->i_mode >> 12;
1755 ino = find_inode_number(dir, &qname);
1758 return filldir(dirent, name, len, filp->f_pos, ino, type);
1761 static unsigned name_to_int(struct dentry *dentry)
1763 const char *name = dentry->d_name.name;
1764 int len = dentry->d_name.len;
1767 if (len > 1 && *name == '0')
1770 unsigned c = *name++ - '0';
1773 if (n >= (~0U-9)/10)
1783 #define PROC_FDINFO_MAX 64
1785 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1787 struct task_struct *task = get_proc_task(inode);
1788 struct files_struct *files = NULL;
1790 int fd = proc_fd(inode);
1793 files = get_files_struct(task);
1794 put_task_struct(task);
1798 * We are not taking a ref to the file structure, so we must
1801 spin_lock(&files->file_lock);
1802 file = fcheck_files(files, fd);
1805 *path = file->f_path;
1806 path_get(&file->f_path);
1809 snprintf(info, PROC_FDINFO_MAX,
1812 (long long) file->f_pos,
1814 spin_unlock(&files->file_lock);
1815 put_files_struct(files);
1818 spin_unlock(&files->file_lock);
1819 put_files_struct(files);
1824 static int proc_fd_link(struct inode *inode, struct path *path)
1826 return proc_fd_info(inode, path, NULL);
1829 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1831 struct inode *inode = dentry->d_inode;
1832 struct task_struct *task = get_proc_task(inode);
1833 int fd = proc_fd(inode);
1834 struct files_struct *files;
1835 const struct cred *cred;
1838 files = get_files_struct(task);
1841 if (fcheck_files(files, fd)) {
1843 put_files_struct(files);
1844 if (task_dumpable(task)) {
1846 cred = __task_cred(task);
1847 inode->i_uid = cred->euid;
1848 inode->i_gid = cred->egid;
1854 inode->i_mode &= ~(S_ISUID | S_ISGID);
1855 security_task_to_inode(task, inode);
1856 put_task_struct(task);
1860 put_files_struct(files);
1862 put_task_struct(task);
1868 static const struct dentry_operations tid_fd_dentry_operations =
1870 .d_revalidate = tid_fd_revalidate,
1871 .d_delete = pid_delete_dentry,
1874 static struct dentry *proc_fd_instantiate(struct inode *dir,
1875 struct dentry *dentry, struct task_struct *task, const void *ptr)
1877 unsigned fd = *(const unsigned *)ptr;
1879 struct files_struct *files;
1880 struct inode *inode;
1881 struct proc_inode *ei;
1882 struct dentry *error = ERR_PTR(-ENOENT);
1884 inode = proc_pid_make_inode(dir->i_sb, task);
1889 files = get_files_struct(task);
1892 inode->i_mode = S_IFLNK;
1895 * We are not taking a ref to the file structure, so we must
1898 spin_lock(&files->file_lock);
1899 file = fcheck_files(files, fd);
1902 if (file->f_mode & FMODE_READ)
1903 inode->i_mode |= S_IRUSR | S_IXUSR;
1904 if (file->f_mode & FMODE_WRITE)
1905 inode->i_mode |= S_IWUSR | S_IXUSR;
1906 spin_unlock(&files->file_lock);
1907 put_files_struct(files);
1909 inode->i_op = &proc_pid_link_inode_operations;
1911 ei->op.proc_get_link = proc_fd_link;
1912 dentry->d_op = &tid_fd_dentry_operations;
1913 d_add(dentry, inode);
1914 /* Close the race of the process dying before we return the dentry */
1915 if (tid_fd_revalidate(dentry, NULL))
1921 spin_unlock(&files->file_lock);
1922 put_files_struct(files);
1928 static struct dentry *proc_lookupfd_common(struct inode *dir,
1929 struct dentry *dentry,
1930 instantiate_t instantiate)
1932 struct task_struct *task = get_proc_task(dir);
1933 unsigned fd = name_to_int(dentry);
1934 struct dentry *result = ERR_PTR(-ENOENT);
1941 result = instantiate(dir, dentry, task, &fd);
1943 put_task_struct(task);
1948 static int proc_readfd_common(struct file * filp, void * dirent,
1949 filldir_t filldir, instantiate_t instantiate)
1951 struct dentry *dentry = filp->f_path.dentry;
1952 struct inode *inode = dentry->d_inode;
1953 struct task_struct *p = get_proc_task(inode);
1954 unsigned int fd, ino;
1956 struct files_struct * files;
1966 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1970 ino = parent_ino(dentry);
1971 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1975 files = get_files_struct(p);
1979 for (fd = filp->f_pos-2;
1980 fd < files_fdtable(files)->max_fds;
1981 fd++, filp->f_pos++) {
1982 char name[PROC_NUMBUF];
1985 if (!fcheck_files(files, fd))
1989 len = snprintf(name, sizeof(name), "%d", fd);
1990 if (proc_fill_cache(filp, dirent, filldir,
1991 name, len, instantiate,
1999 put_files_struct(files);
2007 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2008 struct nameidata *nd)
2010 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2013 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2015 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2018 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2019 size_t len, loff_t *ppos)
2021 char tmp[PROC_FDINFO_MAX];
2022 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2024 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2028 static const struct file_operations proc_fdinfo_file_operations = {
2029 .open = nonseekable_open,
2030 .read = proc_fdinfo_read,
2033 static const struct file_operations proc_fd_operations = {
2034 .read = generic_read_dir,
2035 .readdir = proc_readfd,
2039 * /proc/pid/fd needs a special permission handler so that a process can still
2040 * access /proc/self/fd after it has executed a setuid().
2042 static int proc_fd_permission(struct inode *inode, int mask)
2046 rv = generic_permission(inode, mask, NULL);
2049 if (task_pid(current) == proc_pid(inode))
2055 * proc directories can do almost nothing..
2057 static const struct inode_operations proc_fd_inode_operations = {
2058 .lookup = proc_lookupfd,
2059 .permission = proc_fd_permission,
2060 .setattr = proc_setattr,
2063 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2064 struct dentry *dentry, struct task_struct *task, const void *ptr)
2066 unsigned fd = *(unsigned *)ptr;
2067 struct inode *inode;
2068 struct proc_inode *ei;
2069 struct dentry *error = ERR_PTR(-ENOENT);
2071 inode = proc_pid_make_inode(dir->i_sb, task);
2076 inode->i_mode = S_IFREG | S_IRUSR;
2077 inode->i_fop = &proc_fdinfo_file_operations;
2078 dentry->d_op = &tid_fd_dentry_operations;
2079 d_add(dentry, inode);
2080 /* Close the race of the process dying before we return the dentry */
2081 if (tid_fd_revalidate(dentry, NULL))
2088 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2089 struct dentry *dentry,
2090 struct nameidata *nd)
2092 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2095 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2097 return proc_readfd_common(filp, dirent, filldir,
2098 proc_fdinfo_instantiate);
2101 static const struct file_operations proc_fdinfo_operations = {
2102 .read = generic_read_dir,
2103 .readdir = proc_readfdinfo,
2107 * proc directories can do almost nothing..
2109 static const struct inode_operations proc_fdinfo_inode_operations = {
2110 .lookup = proc_lookupfdinfo,
2111 .setattr = proc_setattr,
2115 static struct dentry *proc_pident_instantiate(struct inode *dir,
2116 struct dentry *dentry, struct task_struct *task, const void *ptr)
2118 const struct pid_entry *p = ptr;
2119 struct inode *inode;
2120 struct proc_inode *ei;
2121 struct dentry *error = ERR_PTR(-ENOENT);
2123 inode = proc_pid_make_inode(dir->i_sb, task);
2128 inode->i_mode = p->mode;
2129 if (S_ISDIR(inode->i_mode))
2130 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2132 inode->i_op = p->iop;
2134 inode->i_fop = p->fop;
2136 dentry->d_op = &pid_dentry_operations;
2137 d_add(dentry, inode);
2138 /* Close the race of the process dying before we return the dentry */
2139 if (pid_revalidate(dentry, NULL))
2145 static struct dentry *proc_pident_lookup(struct inode *dir,
2146 struct dentry *dentry,
2147 const struct pid_entry *ents,
2150 struct dentry *error;
2151 struct task_struct *task = get_proc_task(dir);
2152 const struct pid_entry *p, *last;
2154 error = ERR_PTR(-ENOENT);
2160 * Yes, it does not scale. And it should not. Don't add
2161 * new entries into /proc/<tgid>/ without very good reasons.
2163 last = &ents[nents - 1];
2164 for (p = ents; p <= last; p++) {
2165 if (p->len != dentry->d_name.len)
2167 if (!memcmp(dentry->d_name.name, p->name, p->len))
2173 error = proc_pident_instantiate(dir, dentry, task, p);
2175 put_task_struct(task);
2180 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2181 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2183 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2184 proc_pident_instantiate, task, p);
2187 static int proc_pident_readdir(struct file *filp,
2188 void *dirent, filldir_t filldir,
2189 const struct pid_entry *ents, unsigned int nents)
2192 struct dentry *dentry = filp->f_path.dentry;
2193 struct inode *inode = dentry->d_inode;
2194 struct task_struct *task = get_proc_task(inode);
2195 const struct pid_entry *p, *last;
2208 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2214 ino = parent_ino(dentry);
2215 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2227 last = &ents[nents - 1];
2229 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2238 put_task_struct(task);
2243 #ifdef CONFIG_SECURITY
2244 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2245 size_t count, loff_t *ppos)
2247 struct inode * inode = file->f_path.dentry->d_inode;
2250 struct task_struct *task = get_proc_task(inode);
2255 length = security_getprocattr(task,
2256 (char*)file->f_path.dentry->d_name.name,
2258 put_task_struct(task);
2260 length = simple_read_from_buffer(buf, count, ppos, p, length);
2265 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2266 size_t count, loff_t *ppos)
2268 struct inode * inode = file->f_path.dentry->d_inode;
2271 struct task_struct *task = get_proc_task(inode);
2276 if (count > PAGE_SIZE)
2279 /* No partial writes. */
2285 page = (char*)__get_free_page(GFP_TEMPORARY);
2290 if (copy_from_user(page, buf, count))
2293 /* Guard against adverse ptrace interaction */
2294 length = mutex_lock_interruptible(&task->cred_guard_mutex);
2298 length = security_setprocattr(task,
2299 (char*)file->f_path.dentry->d_name.name,
2300 (void*)page, count);
2301 mutex_unlock(&task->cred_guard_mutex);
2303 free_page((unsigned long) page);
2305 put_task_struct(task);
2310 static const struct file_operations proc_pid_attr_operations = {
2311 .read = proc_pid_attr_read,
2312 .write = proc_pid_attr_write,
2315 static const struct pid_entry attr_dir_stuff[] = {
2316 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2317 REG("prev", S_IRUGO, proc_pid_attr_operations),
2318 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2319 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2320 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2321 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2324 static int proc_attr_dir_readdir(struct file * filp,
2325 void * dirent, filldir_t filldir)
2327 return proc_pident_readdir(filp,dirent,filldir,
2328 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2331 static const struct file_operations proc_attr_dir_operations = {
2332 .read = generic_read_dir,
2333 .readdir = proc_attr_dir_readdir,
2336 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2337 struct dentry *dentry, struct nameidata *nd)
2339 return proc_pident_lookup(dir, dentry,
2340 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2343 static const struct inode_operations proc_attr_dir_inode_operations = {
2344 .lookup = proc_attr_dir_lookup,
2345 .getattr = pid_getattr,
2346 .setattr = proc_setattr,
2351 #ifdef CONFIG_ELF_CORE
2352 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2353 size_t count, loff_t *ppos)
2355 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2356 struct mm_struct *mm;
2357 char buffer[PROC_NUMBUF];
2365 mm = get_task_mm(task);
2367 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2368 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2369 MMF_DUMP_FILTER_SHIFT));
2371 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2374 put_task_struct(task);
2379 static ssize_t proc_coredump_filter_write(struct file *file,
2380 const char __user *buf,
2384 struct task_struct *task;
2385 struct mm_struct *mm;
2386 char buffer[PROC_NUMBUF], *end;
2393 memset(buffer, 0, sizeof(buffer));
2394 if (count > sizeof(buffer) - 1)
2395 count = sizeof(buffer) - 1;
2396 if (copy_from_user(buffer, buf, count))
2400 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2403 if (end - buffer == 0)
2407 task = get_proc_task(file->f_dentry->d_inode);
2412 mm = get_task_mm(task);
2416 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2418 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2420 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2425 put_task_struct(task);
2430 static const struct file_operations proc_coredump_filter_operations = {
2431 .read = proc_coredump_filter_read,
2432 .write = proc_coredump_filter_write,
2439 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2442 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2443 pid_t tgid = task_tgid_nr_ns(current, ns);
2444 char tmp[PROC_NUMBUF];
2447 sprintf(tmp, "%d", tgid);
2448 return vfs_readlink(dentry,buffer,buflen,tmp);
2451 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2453 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2454 pid_t tgid = task_tgid_nr_ns(current, ns);
2455 char tmp[PROC_NUMBUF];
2457 return ERR_PTR(-ENOENT);
2458 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2459 return ERR_PTR(vfs_follow_link(nd,tmp));
2462 static const struct inode_operations proc_self_inode_operations = {
2463 .readlink = proc_self_readlink,
2464 .follow_link = proc_self_follow_link,
2470 * These are the directory entries in the root directory of /proc
2471 * that properly belong to the /proc filesystem, as they describe
2472 * describe something that is process related.
2474 static const struct pid_entry proc_base_stuff[] = {
2475 NOD("self", S_IFLNK|S_IRWXUGO,
2476 &proc_self_inode_operations, NULL, {}),
2480 * Exceptional case: normally we are not allowed to unhash a busy
2481 * directory. In this case, however, we can do it - no aliasing problems
2482 * due to the way we treat inodes.
2484 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2486 struct inode *inode = dentry->d_inode;
2487 struct task_struct *task = get_proc_task(inode);
2489 put_task_struct(task);
2496 static const struct dentry_operations proc_base_dentry_operations =
2498 .d_revalidate = proc_base_revalidate,
2499 .d_delete = pid_delete_dentry,
2502 static struct dentry *proc_base_instantiate(struct inode *dir,
2503 struct dentry *dentry, struct task_struct *task, const void *ptr)
2505 const struct pid_entry *p = ptr;
2506 struct inode *inode;
2507 struct proc_inode *ei;
2508 struct dentry *error = ERR_PTR(-EINVAL);
2510 /* Allocate the inode */
2511 error = ERR_PTR(-ENOMEM);
2512 inode = new_inode(dir->i_sb);
2516 /* Initialize the inode */
2518 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2521 * grab the reference to the task.
2523 ei->pid = get_task_pid(task, PIDTYPE_PID);
2527 inode->i_mode = p->mode;
2528 if (S_ISDIR(inode->i_mode))
2530 if (S_ISLNK(inode->i_mode))
2533 inode->i_op = p->iop;
2535 inode->i_fop = p->fop;
2537 dentry->d_op = &proc_base_dentry_operations;
2538 d_add(dentry, inode);
2547 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2549 struct dentry *error;
2550 struct task_struct *task = get_proc_task(dir);
2551 const struct pid_entry *p, *last;
2553 error = ERR_PTR(-ENOENT);
2558 /* Lookup the directory entry */
2559 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2560 for (p = proc_base_stuff; p <= last; p++) {
2561 if (p->len != dentry->d_name.len)
2563 if (!memcmp(dentry->d_name.name, p->name, p->len))
2569 error = proc_base_instantiate(dir, dentry, task, p);
2572 put_task_struct(task);
2577 static int proc_base_fill_cache(struct file *filp, void *dirent,
2578 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2580 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2581 proc_base_instantiate, task, p);
2584 #ifdef CONFIG_TASK_IO_ACCOUNTING
2585 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2587 struct task_io_accounting acct = task->ioac;
2588 unsigned long flags;
2590 if (whole && lock_task_sighand(task, &flags)) {
2591 struct task_struct *t = task;
2593 task_io_accounting_add(&acct, &task->signal->ioac);
2594 while_each_thread(task, t)
2595 task_io_accounting_add(&acct, &t->ioac);
2597 unlock_task_sighand(task, &flags);
2599 return sprintf(buffer,
2604 "read_bytes: %llu\n"
2605 "write_bytes: %llu\n"
2606 "cancelled_write_bytes: %llu\n",
2607 (unsigned long long)acct.rchar,
2608 (unsigned long long)acct.wchar,
2609 (unsigned long long)acct.syscr,
2610 (unsigned long long)acct.syscw,
2611 (unsigned long long)acct.read_bytes,
2612 (unsigned long long)acct.write_bytes,
2613 (unsigned long long)acct.cancelled_write_bytes);
2616 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2618 return do_io_accounting(task, buffer, 0);
2621 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2623 return do_io_accounting(task, buffer, 1);
2625 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2627 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2628 struct pid *pid, struct task_struct *task)
2630 seq_printf(m, "%08x\n", task->personality);
2637 static const struct file_operations proc_task_operations;
2638 static const struct inode_operations proc_task_inode_operations;
2640 static const struct pid_entry tgid_base_stuff[] = {
2641 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2642 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2643 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2645 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2647 REG("environ", S_IRUSR, proc_environ_operations),
2648 INF("auxv", S_IRUSR, proc_pid_auxv),
2649 ONE("status", S_IRUGO, proc_pid_status),
2650 ONE("personality", S_IRUSR, proc_pid_personality),
2651 REG("limits", S_IRUSR|S_IWUSR, proc_pid_limits_operations),
2652 #ifdef CONFIG_SCHED_DEBUG
2653 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2655 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2656 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2657 INF("syscall", S_IRUSR, proc_pid_syscall),
2659 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2660 ONE("stat", S_IRUGO, proc_tgid_stat),
2661 ONE("statm", S_IRUGO, proc_pid_statm),
2662 REG("maps", S_IRUGO, proc_maps_operations),
2664 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2666 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2667 LNK("cwd", proc_cwd_link),
2668 LNK("root", proc_root_link),
2669 LNK("exe", proc_exe_link),
2670 REG("mounts", S_IRUGO, proc_mounts_operations),
2671 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2672 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2673 #ifdef CONFIG_PROC_PAGE_MONITOR
2674 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2675 REG("smaps", S_IRUGO, proc_smaps_operations),
2676 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2678 #ifdef CONFIG_SECURITY
2679 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2681 #ifdef CONFIG_KALLSYMS
2682 INF("wchan", S_IRUGO, proc_pid_wchan),
2684 #ifdef CONFIG_STACKTRACE
2685 ONE("stack", S_IRUSR, proc_pid_stack),
2687 #ifdef CONFIG_SCHEDSTATS
2688 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2690 #ifdef CONFIG_LATENCYTOP
2691 REG("latency", S_IRUGO, proc_lstats_operations),
2693 #ifdef CONFIG_PROC_PID_CPUSET
2694 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2696 #ifdef CONFIG_CGROUPS
2697 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2699 INF("oom_score", S_IRUGO, proc_oom_score),
2700 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2701 #ifdef CONFIG_AUDITSYSCALL
2702 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2703 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2705 #ifdef CONFIG_FAULT_INJECTION
2706 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2708 #ifdef CONFIG_ELF_CORE
2709 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2711 #ifdef CONFIG_TASK_IO_ACCOUNTING
2712 INF("io", S_IRUGO, proc_tgid_io_accounting),
2716 static int proc_tgid_base_readdir(struct file * filp,
2717 void * dirent, filldir_t filldir)
2719 return proc_pident_readdir(filp,dirent,filldir,
2720 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2723 static const struct file_operations proc_tgid_base_operations = {
2724 .read = generic_read_dir,
2725 .readdir = proc_tgid_base_readdir,
2728 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2729 return proc_pident_lookup(dir, dentry,
2730 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2733 static const struct inode_operations proc_tgid_base_inode_operations = {
2734 .lookup = proc_tgid_base_lookup,
2735 .getattr = pid_getattr,
2736 .setattr = proc_setattr,
2739 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2741 struct dentry *dentry, *leader, *dir;
2742 char buf[PROC_NUMBUF];
2746 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2747 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2749 shrink_dcache_parent(dentry);
2755 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2756 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2761 name.len = strlen(name.name);
2762 dir = d_hash_and_lookup(leader, &name);
2764 goto out_put_leader;
2767 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2768 dentry = d_hash_and_lookup(dir, &name);
2770 shrink_dcache_parent(dentry);
2783 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2784 * @task: task that should be flushed.
2786 * When flushing dentries from proc, one needs to flush them from global
2787 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2788 * in. This call is supposed to do all of this job.
2790 * Looks in the dcache for
2792 * /proc/@tgid/task/@pid
2793 * if either directory is present flushes it and all of it'ts children
2796 * It is safe and reasonable to cache /proc entries for a task until
2797 * that task exits. After that they just clog up the dcache with
2798 * useless entries, possibly causing useful dcache entries to be
2799 * flushed instead. This routine is proved to flush those useless
2800 * dcache entries at process exit time.
2802 * NOTE: This routine is just an optimization so it does not guarantee
2803 * that no dcache entries will exist at process exit time it
2804 * just makes it very unlikely that any will persist.
2807 void proc_flush_task(struct task_struct *task)
2810 struct pid *pid, *tgid;
2813 pid = task_pid(task);
2814 tgid = task_tgid(task);
2816 for (i = 0; i <= pid->level; i++) {
2817 upid = &pid->numbers[i];
2818 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2819 tgid->numbers[i].nr);
2822 upid = &pid->numbers[pid->level];
2824 pid_ns_release_proc(upid->ns);
2827 static struct dentry *proc_pid_instantiate(struct inode *dir,
2828 struct dentry * dentry,
2829 struct task_struct *task, const void *ptr)
2831 struct dentry *error = ERR_PTR(-ENOENT);
2832 struct inode *inode;
2834 inode = proc_pid_make_inode(dir->i_sb, task);
2838 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2839 inode->i_op = &proc_tgid_base_inode_operations;
2840 inode->i_fop = &proc_tgid_base_operations;
2841 inode->i_flags|=S_IMMUTABLE;
2843 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2844 ARRAY_SIZE(tgid_base_stuff));
2846 dentry->d_op = &pid_dentry_operations;
2848 d_add(dentry, inode);
2849 /* Close the race of the process dying before we return the dentry */
2850 if (pid_revalidate(dentry, NULL))
2856 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2858 struct dentry *result = ERR_PTR(-ENOENT);
2859 struct task_struct *task;
2861 struct pid_namespace *ns;
2863 result = proc_base_lookup(dir, dentry);
2864 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2867 tgid = name_to_int(dentry);
2871 ns = dentry->d_sb->s_fs_info;
2873 task = find_task_by_pid_ns(tgid, ns);
2875 get_task_struct(task);
2880 result = proc_pid_instantiate(dir, dentry, task, NULL);
2881 put_task_struct(task);
2887 * Find the first task with tgid >= tgid
2892 struct task_struct *task;
2894 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2899 put_task_struct(iter.task);
2903 pid = find_ge_pid(iter.tgid, ns);
2905 iter.tgid = pid_nr_ns(pid, ns);
2906 iter.task = pid_task(pid, PIDTYPE_PID);
2907 /* What we to know is if the pid we have find is the
2908 * pid of a thread_group_leader. Testing for task
2909 * being a thread_group_leader is the obvious thing
2910 * todo but there is a window when it fails, due to
2911 * the pid transfer logic in de_thread.
2913 * So we perform the straight forward test of seeing
2914 * if the pid we have found is the pid of a thread
2915 * group leader, and don't worry if the task we have
2916 * found doesn't happen to be a thread group leader.
2917 * As we don't care in the case of readdir.
2919 if (!iter.task || !has_group_leader_pid(iter.task)) {
2923 get_task_struct(iter.task);
2929 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2931 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2932 struct tgid_iter iter)
2934 char name[PROC_NUMBUF];
2935 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2936 return proc_fill_cache(filp, dirent, filldir, name, len,
2937 proc_pid_instantiate, iter.task, NULL);
2940 /* for the /proc/ directory itself, after non-process stuff has been done */
2941 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2943 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2944 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2945 struct tgid_iter iter;
2946 struct pid_namespace *ns;
2951 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2952 const struct pid_entry *p = &proc_base_stuff[nr];
2953 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2957 ns = filp->f_dentry->d_sb->s_fs_info;
2959 iter.tgid = filp->f_pos - TGID_OFFSET;
2960 for (iter = next_tgid(ns, iter);
2962 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2963 filp->f_pos = iter.tgid + TGID_OFFSET;
2964 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2965 put_task_struct(iter.task);
2969 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2971 put_task_struct(reaper);
2979 static const struct pid_entry tid_base_stuff[] = {
2980 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2981 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2982 REG("environ", S_IRUSR, proc_environ_operations),
2983 INF("auxv", S_IRUSR, proc_pid_auxv),
2984 ONE("status", S_IRUGO, proc_pid_status),
2985 ONE("personality", S_IRUSR, proc_pid_personality),
2986 REG("limits", S_IRUSR|S_IWUSR, proc_pid_limits_operations),
2987 #ifdef CONFIG_SCHED_DEBUG
2988 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2990 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2991 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2992 INF("syscall", S_IRUSR, proc_pid_syscall),
2994 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2995 ONE("stat", S_IRUGO, proc_tid_stat),
2996 ONE("statm", S_IRUGO, proc_pid_statm),
2997 REG("maps", S_IRUGO, proc_maps_operations),
2999 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3001 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3002 LNK("cwd", proc_cwd_link),
3003 LNK("root", proc_root_link),
3004 LNK("exe", proc_exe_link),
3005 REG("mounts", S_IRUGO, proc_mounts_operations),
3006 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3007 #ifdef CONFIG_PROC_PAGE_MONITOR
3008 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3009 REG("smaps", S_IRUGO, proc_smaps_operations),
3010 REG("pagemap", S_IRUSR, proc_pagemap_operations),
3012 #ifdef CONFIG_SECURITY
3013 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3015 #ifdef CONFIG_KALLSYMS
3016 INF("wchan", S_IRUGO, proc_pid_wchan),
3018 #ifdef CONFIG_STACKTRACE
3019 ONE("stack", S_IRUSR, proc_pid_stack),
3021 #ifdef CONFIG_SCHEDSTATS
3022 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3024 #ifdef CONFIG_LATENCYTOP
3025 REG("latency", S_IRUGO, proc_lstats_operations),
3027 #ifdef CONFIG_PROC_PID_CPUSET
3028 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3030 #ifdef CONFIG_CGROUPS
3031 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3033 INF("oom_score", S_IRUGO, proc_oom_score),
3034 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3035 #ifdef CONFIG_AUDITSYSCALL
3036 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3037 REG("sessionid", S_IRUSR, proc_sessionid_operations),
3039 #ifdef CONFIG_FAULT_INJECTION
3040 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3042 #ifdef CONFIG_TASK_IO_ACCOUNTING
3043 INF("io", S_IRUGO, proc_tid_io_accounting),
3047 static int proc_tid_base_readdir(struct file * filp,
3048 void * dirent, filldir_t filldir)
3050 return proc_pident_readdir(filp,dirent,filldir,
3051 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3054 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3055 return proc_pident_lookup(dir, dentry,
3056 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3059 static const struct file_operations proc_tid_base_operations = {
3060 .read = generic_read_dir,
3061 .readdir = proc_tid_base_readdir,
3064 static const struct inode_operations proc_tid_base_inode_operations = {
3065 .lookup = proc_tid_base_lookup,
3066 .getattr = pid_getattr,
3067 .setattr = proc_setattr,
3070 static struct dentry *proc_task_instantiate(struct inode *dir,
3071 struct dentry *dentry, struct task_struct *task, const void *ptr)
3073 struct dentry *error = ERR_PTR(-ENOENT);
3074 struct inode *inode;
3075 inode = proc_pid_make_inode(dir->i_sb, task);
3079 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3080 inode->i_op = &proc_tid_base_inode_operations;
3081 inode->i_fop = &proc_tid_base_operations;
3082 inode->i_flags|=S_IMMUTABLE;
3084 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3085 ARRAY_SIZE(tid_base_stuff));
3087 dentry->d_op = &pid_dentry_operations;
3089 d_add(dentry, inode);
3090 /* Close the race of the process dying before we return the dentry */
3091 if (pid_revalidate(dentry, NULL))
3097 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3099 struct dentry *result = ERR_PTR(-ENOENT);
3100 struct task_struct *task;
3101 struct task_struct *leader = get_proc_task(dir);
3103 struct pid_namespace *ns;
3108 tid = name_to_int(dentry);
3112 ns = dentry->d_sb->s_fs_info;
3114 task = find_task_by_pid_ns(tid, ns);
3116 get_task_struct(task);
3120 if (!same_thread_group(leader, task))
3123 result = proc_task_instantiate(dir, dentry, task, NULL);
3125 put_task_struct(task);
3127 put_task_struct(leader);
3133 * Find the first tid of a thread group to return to user space.
3135 * Usually this is just the thread group leader, but if the users
3136 * buffer was too small or there was a seek into the middle of the
3137 * directory we have more work todo.
3139 * In the case of a short read we start with find_task_by_pid.
3141 * In the case of a seek we start with the leader and walk nr
3144 static struct task_struct *first_tid(struct task_struct *leader,
3145 int tid, int nr, struct pid_namespace *ns)
3147 struct task_struct *pos;
3150 /* Attempt to start with the pid of a thread */
3151 if (tid && (nr > 0)) {
3152 pos = find_task_by_pid_ns(tid, ns);
3153 if (pos && (pos->group_leader == leader))
3157 /* If nr exceeds the number of threads there is nothing todo */
3159 if (nr && nr >= get_nr_threads(leader))
3162 /* If we haven't found our starting place yet start
3163 * with the leader and walk nr threads forward.
3165 for (pos = leader; nr > 0; --nr) {
3166 pos = next_thread(pos);
3167 if (pos == leader) {
3173 get_task_struct(pos);
3180 * Find the next thread in the thread list.
3181 * Return NULL if there is an error or no next thread.
3183 * The reference to the input task_struct is released.
3185 static struct task_struct *next_tid(struct task_struct *start)
3187 struct task_struct *pos = NULL;
3189 if (pid_alive(start)) {
3190 pos = next_thread(start);
3191 if (thread_group_leader(pos))
3194 get_task_struct(pos);
3197 put_task_struct(start);
3201 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3202 struct task_struct *task, int tid)
3204 char name[PROC_NUMBUF];
3205 int len = snprintf(name, sizeof(name), "%d", tid);
3206 return proc_fill_cache(filp, dirent, filldir, name, len,
3207 proc_task_instantiate, task, NULL);
3210 /* for the /proc/TGID/task/ directories */
3211 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3213 struct dentry *dentry = filp->f_path.dentry;
3214 struct inode *inode = dentry->d_inode;
3215 struct task_struct *leader = NULL;
3216 struct task_struct *task;
3217 int retval = -ENOENT;
3220 struct pid_namespace *ns;
3222 task = get_proc_task(inode);
3226 if (pid_alive(task)) {
3227 leader = task->group_leader;
3228 get_task_struct(leader);
3231 put_task_struct(task);
3236 switch ((unsigned long)filp->f_pos) {
3239 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3244 ino = parent_ino(dentry);
3245 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3251 /* f_version caches the tgid value that the last readdir call couldn't
3252 * return. lseek aka telldir automagically resets f_version to 0.
3254 ns = filp->f_dentry->d_sb->s_fs_info;
3255 tid = (int)filp->f_version;
3256 filp->f_version = 0;
3257 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3259 task = next_tid(task), filp->f_pos++) {
3260 tid = task_pid_nr_ns(task, ns);
3261 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3262 /* returning this tgid failed, save it as the first
3263 * pid for the next readir call */
3264 filp->f_version = (u64)tid;
3265 put_task_struct(task);
3270 put_task_struct(leader);
3275 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3277 struct inode *inode = dentry->d_inode;
3278 struct task_struct *p = get_proc_task(inode);
3279 generic_fillattr(inode, stat);
3282 stat->nlink += get_nr_threads(p);
3289 static const struct inode_operations proc_task_inode_operations = {
3290 .lookup = proc_task_lookup,
3291 .getattr = proc_task_getattr,
3292 .setattr = proc_setattr,
3295 static const struct file_operations proc_task_operations = {
3296 .read = generic_read_dir,
3297 .readdir = proc_task_readdir,