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>
86 * Implementing inode permission operations in /proc is almost
87 * certainly an error. Permission checks need to happen during
88 * each system call not at open time. The reason is that most of
89 * what we wish to check for permissions in /proc varies at runtime.
91 * The classic example of a problem is opening file descriptors
92 * in /proc for a task before it execs a suid executable.
99 const struct inode_operations *iop;
100 const struct file_operations *fop;
104 #define NOD(NAME, MODE, IOP, FOP, OP) { \
106 .len = sizeof(NAME) - 1, \
113 #define DIR(NAME, MODE, iops, fops) \
114 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
115 #define LNK(NAME, get_link) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = get_link } )
119 #define REG(NAME, MODE, fops) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
121 #define INF(NAME, MODE, read) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = read } )
125 #define ONE(NAME, MODE, show) \
126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_single_file_operations, \
128 { .proc_show = show } )
131 * Count the number of hardlinks for the pid_entry table, excluding the .
134 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
141 for (i = 0; i < n; ++i) {
142 if (S_ISDIR(entries[i].mode))
149 static int get_fs_path(struct task_struct *task, struct path *path, bool root)
151 struct fs_struct *fs;
152 int result = -ENOENT;
157 read_lock(&fs->lock);
158 *path = root ? fs->root : fs->pwd;
160 read_unlock(&fs->lock);
167 static int get_nr_threads(struct task_struct *tsk)
172 if (lock_task_sighand(tsk, &flags)) {
173 count = atomic_read(&tsk->signal->count);
174 unlock_task_sighand(tsk, &flags);
179 static int proc_cwd_link(struct inode *inode, struct path *path)
181 struct task_struct *task = get_proc_task(inode);
182 int result = -ENOENT;
185 result = get_fs_path(task, path, 0);
186 put_task_struct(task);
191 static int proc_root_link(struct inode *inode, struct path *path)
193 struct task_struct *task = get_proc_task(inode);
194 int result = -ENOENT;
197 result = get_fs_path(task, path, 1);
198 put_task_struct(task);
204 * Return zero if current may access user memory in @task, -error if not.
206 static int check_mem_permission(struct task_struct *task)
209 * A task can always look at itself, in case it chooses
210 * to use system calls instead of load instructions.
216 * If current is actively ptrace'ing, and would also be
217 * permitted to freshly attach with ptrace now, permit it.
219 if (task_is_stopped_or_traced(task)) {
222 match = (tracehook_tracer_task(task) == current);
224 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
229 * Noone else is allowed.
234 struct mm_struct *mm_for_maps(struct task_struct *task)
236 struct mm_struct *mm = get_task_mm(task);
239 down_read(&mm->mmap_sem);
243 if (task->mm != current->mm &&
244 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
250 up_read(&mm->mmap_sem);
255 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
259 struct mm_struct *mm = get_task_mm(task);
263 goto out_mm; /* Shh! No looking before we're done */
265 len = mm->arg_end - mm->arg_start;
270 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
272 // If the nul at the end of args has been overwritten, then
273 // assume application is using setproctitle(3).
274 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
275 len = strnlen(buffer, res);
279 len = mm->env_end - mm->env_start;
280 if (len > PAGE_SIZE - res)
281 len = PAGE_SIZE - res;
282 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
283 res = strnlen(buffer, res);
292 static int proc_pid_auxv(struct task_struct *task, char *buffer)
295 struct mm_struct *mm = get_task_mm(task);
297 unsigned int nwords = 0;
300 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
301 res = nwords * sizeof(mm->saved_auxv[0]);
304 memcpy(buffer, mm->saved_auxv, res);
311 #ifdef CONFIG_KALLSYMS
313 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
314 * Returns the resolved symbol. If that fails, simply return the address.
316 static int proc_pid_wchan(struct task_struct *task, char *buffer)
319 char symname[KSYM_NAME_LEN];
321 wchan = get_wchan(task);
323 if (lookup_symbol_name(wchan, symname) < 0)
324 return sprintf(buffer, "%lu", wchan);
326 return sprintf(buffer, "%s", symname);
328 #endif /* CONFIG_KALLSYMS */
330 #ifdef CONFIG_STACKTRACE
332 #define MAX_STACK_TRACE_DEPTH 64
334 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
335 struct pid *pid, struct task_struct *task)
337 struct stack_trace trace;
338 unsigned long *entries;
341 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
345 trace.nr_entries = 0;
346 trace.max_entries = MAX_STACK_TRACE_DEPTH;
347 trace.entries = entries;
349 save_stack_trace_tsk(task, &trace);
351 for (i = 0; i < trace.nr_entries; i++) {
352 seq_printf(m, "[<%p>] %pS\n",
353 (void *)entries[i], (void *)entries[i]);
361 #ifdef CONFIG_SCHEDSTATS
363 * Provides /proc/PID/schedstat
365 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
367 return sprintf(buffer, "%llu %llu %lu\n",
368 (unsigned long long)task->se.sum_exec_runtime,
369 (unsigned long long)task->sched_info.run_delay,
370 task->sched_info.pcount);
374 #ifdef CONFIG_LATENCYTOP
375 static int lstats_show_proc(struct seq_file *m, void *v)
378 struct inode *inode = m->private;
379 struct task_struct *task = get_proc_task(inode);
383 seq_puts(m, "Latency Top version : v0.1\n");
384 for (i = 0; i < 32; i++) {
385 if (task->latency_record[i].backtrace[0]) {
387 seq_printf(m, "%i %li %li ",
388 task->latency_record[i].count,
389 task->latency_record[i].time,
390 task->latency_record[i].max);
391 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
392 char sym[KSYM_SYMBOL_LEN];
394 if (!task->latency_record[i].backtrace[q])
396 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
398 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
399 c = strchr(sym, '+');
402 seq_printf(m, "%s ", sym);
408 put_task_struct(task);
412 static int lstats_open(struct inode *inode, struct file *file)
414 return single_open(file, lstats_show_proc, inode);
417 static ssize_t lstats_write(struct file *file, const char __user *buf,
418 size_t count, loff_t *offs)
420 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
424 clear_all_latency_tracing(task);
425 put_task_struct(task);
430 static const struct file_operations proc_lstats_operations = {
433 .write = lstats_write,
435 .release = single_release,
440 /* The badness from the OOM killer */
441 unsigned long badness(struct task_struct *p, unsigned long uptime);
442 static int proc_oom_score(struct task_struct *task, char *buffer)
444 unsigned long points;
445 struct timespec uptime;
447 do_posix_clock_monotonic_gettime(&uptime);
448 read_lock(&tasklist_lock);
449 points = badness(task, uptime.tv_sec);
450 read_unlock(&tasklist_lock);
451 return sprintf(buffer, "%lu\n", points);
459 static const struct limit_names lnames[RLIM_NLIMITS] = {
460 [RLIMIT_CPU] = {"Max cpu time", "ms"},
461 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
462 [RLIMIT_DATA] = {"Max data size", "bytes"},
463 [RLIMIT_STACK] = {"Max stack size", "bytes"},
464 [RLIMIT_CORE] = {"Max core file size", "bytes"},
465 [RLIMIT_RSS] = {"Max resident set", "bytes"},
466 [RLIMIT_NPROC] = {"Max processes", "processes"},
467 [RLIMIT_NOFILE] = {"Max open files", "files"},
468 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
469 [RLIMIT_AS] = {"Max address space", "bytes"},
470 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
471 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
472 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
473 [RLIMIT_NICE] = {"Max nice priority", NULL},
474 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
475 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
478 /* Display limits for a process */
479 static int proc_pid_limits(struct task_struct *task, char *buffer)
484 char *bufptr = buffer;
486 struct rlimit rlim[RLIM_NLIMITS];
488 if (!lock_task_sighand(task, &flags))
490 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
491 unlock_task_sighand(task, &flags);
494 * print the file header
496 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
497 "Limit", "Soft Limit", "Hard Limit", "Units");
499 for (i = 0; i < RLIM_NLIMITS; i++) {
500 if (rlim[i].rlim_cur == RLIM_INFINITY)
501 count += sprintf(&bufptr[count], "%-25s %-20s ",
502 lnames[i].name, "unlimited");
504 count += sprintf(&bufptr[count], "%-25s %-20lu ",
505 lnames[i].name, rlim[i].rlim_cur);
507 if (rlim[i].rlim_max == RLIM_INFINITY)
508 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
510 count += sprintf(&bufptr[count], "%-20lu ",
514 count += sprintf(&bufptr[count], "%-10s\n",
517 count += sprintf(&bufptr[count], "\n");
523 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
524 static int proc_pid_syscall(struct task_struct *task, char *buffer)
527 unsigned long args[6], sp, pc;
529 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
530 return sprintf(buffer, "running\n");
533 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
535 return sprintf(buffer,
536 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
538 args[0], args[1], args[2], args[3], args[4], args[5],
541 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
543 /************************************************************************/
544 /* Here the fs part begins */
545 /************************************************************************/
547 /* permission checks */
548 static int proc_fd_access_allowed(struct inode *inode)
550 struct task_struct *task;
552 /* Allow access to a task's file descriptors if it is us or we
553 * may use ptrace attach to the process and find out that
556 task = get_proc_task(inode);
558 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
559 put_task_struct(task);
564 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
567 struct inode *inode = dentry->d_inode;
569 if (attr->ia_valid & ATTR_MODE)
572 error = inode_change_ok(inode, attr);
574 error = inode_setattr(inode, attr);
578 static const struct inode_operations proc_def_inode_operations = {
579 .setattr = proc_setattr,
582 static int mounts_open_common(struct inode *inode, struct file *file,
583 const struct seq_operations *op)
585 struct task_struct *task = get_proc_task(inode);
587 struct mnt_namespace *ns = NULL;
589 struct proc_mounts *p;
594 nsp = task_nsproxy(task);
601 if (ns && get_fs_path(task, &root, 1) == 0)
603 put_task_struct(task);
612 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
616 file->private_data = &p->m;
617 ret = seq_open(file, op);
624 p->event = ns->event;
638 static int mounts_release(struct inode *inode, struct file *file)
640 struct proc_mounts *p = file->private_data;
643 return seq_release(inode, file);
646 static unsigned mounts_poll(struct file *file, poll_table *wait)
648 struct proc_mounts *p = file->private_data;
649 struct mnt_namespace *ns = p->ns;
652 poll_wait(file, &ns->poll, wait);
654 spin_lock(&vfsmount_lock);
655 if (p->event != ns->event) {
656 p->event = ns->event;
659 spin_unlock(&vfsmount_lock);
664 static int mounts_open(struct inode *inode, struct file *file)
666 return mounts_open_common(inode, file, &mounts_op);
669 static const struct file_operations proc_mounts_operations = {
673 .release = mounts_release,
677 static int mountinfo_open(struct inode *inode, struct file *file)
679 return mounts_open_common(inode, file, &mountinfo_op);
682 static const struct file_operations proc_mountinfo_operations = {
683 .open = mountinfo_open,
686 .release = mounts_release,
690 static int mountstats_open(struct inode *inode, struct file *file)
692 return mounts_open_common(inode, file, &mountstats_op);
695 static const struct file_operations proc_mountstats_operations = {
696 .open = mountstats_open,
699 .release = mounts_release,
702 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
704 static ssize_t proc_info_read(struct file * file, char __user * buf,
705 size_t count, loff_t *ppos)
707 struct inode * inode = file->f_path.dentry->d_inode;
710 struct task_struct *task = get_proc_task(inode);
716 if (count > PROC_BLOCK_SIZE)
717 count = PROC_BLOCK_SIZE;
720 if (!(page = __get_free_page(GFP_TEMPORARY)))
723 length = PROC_I(inode)->op.proc_read(task, (char*)page);
726 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
729 put_task_struct(task);
734 static const struct file_operations proc_info_file_operations = {
735 .read = proc_info_read,
738 static int proc_single_show(struct seq_file *m, void *v)
740 struct inode *inode = m->private;
741 struct pid_namespace *ns;
743 struct task_struct *task;
746 ns = inode->i_sb->s_fs_info;
747 pid = proc_pid(inode);
748 task = get_pid_task(pid, PIDTYPE_PID);
752 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
754 put_task_struct(task);
758 static int proc_single_open(struct inode *inode, struct file *filp)
761 ret = single_open(filp, proc_single_show, NULL);
763 struct seq_file *m = filp->private_data;
770 static const struct file_operations proc_single_file_operations = {
771 .open = proc_single_open,
774 .release = single_release,
777 static int mem_open(struct inode* inode, struct file* file)
779 file->private_data = (void*)((long)current->self_exec_id);
783 static ssize_t mem_read(struct file * file, char __user * buf,
784 size_t count, loff_t *ppos)
786 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
788 unsigned long src = *ppos;
790 struct mm_struct *mm;
795 if (check_mem_permission(task))
799 page = (char *)__get_free_page(GFP_TEMPORARY);
805 mm = get_task_mm(task);
811 if (file->private_data != (void*)((long)current->self_exec_id))
817 int this_len, retval;
819 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
820 retval = access_process_vm(task, src, page, this_len, 0);
821 if (!retval || check_mem_permission(task)) {
827 if (copy_to_user(buf, page, retval)) {
842 free_page((unsigned long) page);
844 put_task_struct(task);
849 #define mem_write NULL
852 /* This is a security hazard */
853 static ssize_t mem_write(struct file * file, const char __user *buf,
854 size_t count, loff_t *ppos)
858 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
859 unsigned long dst = *ppos;
865 if (check_mem_permission(task))
869 page = (char *)__get_free_page(GFP_TEMPORARY);
875 int this_len, retval;
877 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
878 if (copy_from_user(page, buf, this_len)) {
882 retval = access_process_vm(task, dst, page, this_len, 1);
894 free_page((unsigned long) page);
896 put_task_struct(task);
902 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
906 file->f_pos = offset;
909 file->f_pos += offset;
914 force_successful_syscall_return();
918 static const struct file_operations proc_mem_operations = {
925 static ssize_t environ_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
928 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
930 unsigned long src = *ppos;
932 struct mm_struct *mm;
937 if (!ptrace_may_access(task, PTRACE_MODE_READ))
941 page = (char *)__get_free_page(GFP_TEMPORARY);
947 mm = get_task_mm(task);
952 int this_len, retval, max_len;
954 this_len = mm->env_end - (mm->env_start + src);
959 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
960 this_len = (this_len > max_len) ? max_len : this_len;
962 retval = access_process_vm(task, (mm->env_start + src),
970 if (copy_to_user(buf, page, retval)) {
984 free_page((unsigned long) page);
986 put_task_struct(task);
991 static const struct file_operations proc_environ_operations = {
992 .read = environ_read,
995 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
996 size_t count, loff_t *ppos)
998 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
999 char buffer[PROC_NUMBUF];
1005 oom_adjust = task->oomkilladj;
1006 put_task_struct(task);
1008 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1010 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1013 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1014 size_t count, loff_t *ppos)
1016 struct task_struct *task;
1017 char buffer[PROC_NUMBUF], *end;
1020 memset(buffer, 0, sizeof(buffer));
1021 if (count > sizeof(buffer) - 1)
1022 count = sizeof(buffer) - 1;
1023 if (copy_from_user(buffer, buf, count))
1025 oom_adjust = simple_strtol(buffer, &end, 0);
1026 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1027 oom_adjust != OOM_DISABLE)
1031 task = get_proc_task(file->f_path.dentry->d_inode);
1034 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1035 put_task_struct(task);
1038 task->oomkilladj = oom_adjust;
1039 put_task_struct(task);
1040 if (end - buffer == 0)
1042 return end - buffer;
1045 static const struct file_operations proc_oom_adjust_operations = {
1046 .read = oom_adjust_read,
1047 .write = oom_adjust_write,
1050 #ifdef CONFIG_AUDITSYSCALL
1051 #define TMPBUFLEN 21
1052 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1053 size_t count, loff_t *ppos)
1055 struct inode * inode = file->f_path.dentry->d_inode;
1056 struct task_struct *task = get_proc_task(inode);
1058 char tmpbuf[TMPBUFLEN];
1062 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1063 audit_get_loginuid(task));
1064 put_task_struct(task);
1065 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1068 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1069 size_t count, loff_t *ppos)
1071 struct inode * inode = file->f_path.dentry->d_inode;
1076 if (!capable(CAP_AUDIT_CONTROL))
1079 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1082 if (count >= PAGE_SIZE)
1083 count = PAGE_SIZE - 1;
1086 /* No partial writes. */
1089 page = (char*)__get_free_page(GFP_TEMPORARY);
1093 if (copy_from_user(page, buf, count))
1097 loginuid = simple_strtoul(page, &tmp, 10);
1103 length = audit_set_loginuid(current, loginuid);
1104 if (likely(length == 0))
1108 free_page((unsigned long) page);
1112 static const struct file_operations proc_loginuid_operations = {
1113 .read = proc_loginuid_read,
1114 .write = proc_loginuid_write,
1117 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1118 size_t count, loff_t *ppos)
1120 struct inode * inode = file->f_path.dentry->d_inode;
1121 struct task_struct *task = get_proc_task(inode);
1123 char tmpbuf[TMPBUFLEN];
1127 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1128 audit_get_sessionid(task));
1129 put_task_struct(task);
1130 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1133 static const struct file_operations proc_sessionid_operations = {
1134 .read = proc_sessionid_read,
1138 #ifdef CONFIG_FAULT_INJECTION
1139 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1140 size_t count, loff_t *ppos)
1142 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1143 char buffer[PROC_NUMBUF];
1149 make_it_fail = task->make_it_fail;
1150 put_task_struct(task);
1152 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1154 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1157 static ssize_t proc_fault_inject_write(struct file * file,
1158 const char __user * buf, size_t count, loff_t *ppos)
1160 struct task_struct *task;
1161 char buffer[PROC_NUMBUF], *end;
1164 if (!capable(CAP_SYS_RESOURCE))
1166 memset(buffer, 0, sizeof(buffer));
1167 if (count > sizeof(buffer) - 1)
1168 count = sizeof(buffer) - 1;
1169 if (copy_from_user(buffer, buf, count))
1171 make_it_fail = simple_strtol(buffer, &end, 0);
1174 task = get_proc_task(file->f_dentry->d_inode);
1177 task->make_it_fail = make_it_fail;
1178 put_task_struct(task);
1179 if (end - buffer == 0)
1181 return end - buffer;
1184 static const struct file_operations proc_fault_inject_operations = {
1185 .read = proc_fault_inject_read,
1186 .write = proc_fault_inject_write,
1191 #ifdef CONFIG_SCHED_DEBUG
1193 * Print out various scheduling related per-task fields:
1195 static int sched_show(struct seq_file *m, void *v)
1197 struct inode *inode = m->private;
1198 struct task_struct *p;
1200 p = get_proc_task(inode);
1203 proc_sched_show_task(p, m);
1211 sched_write(struct file *file, const char __user *buf,
1212 size_t count, loff_t *offset)
1214 struct inode *inode = file->f_path.dentry->d_inode;
1215 struct task_struct *p;
1217 p = get_proc_task(inode);
1220 proc_sched_set_task(p);
1227 static int sched_open(struct inode *inode, struct file *filp)
1231 ret = single_open(filp, sched_show, NULL);
1233 struct seq_file *m = filp->private_data;
1240 static const struct file_operations proc_pid_sched_operations = {
1243 .write = sched_write,
1244 .llseek = seq_lseek,
1245 .release = single_release,
1251 * We added or removed a vma mapping the executable. The vmas are only mapped
1252 * during exec and are not mapped with the mmap system call.
1253 * Callers must hold down_write() on the mm's mmap_sem for these
1255 void added_exe_file_vma(struct mm_struct *mm)
1257 mm->num_exe_file_vmas++;
1260 void removed_exe_file_vma(struct mm_struct *mm)
1262 mm->num_exe_file_vmas--;
1263 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1265 mm->exe_file = NULL;
1270 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1273 get_file(new_exe_file);
1276 mm->exe_file = new_exe_file;
1277 mm->num_exe_file_vmas = 0;
1280 struct file *get_mm_exe_file(struct mm_struct *mm)
1282 struct file *exe_file;
1284 /* We need mmap_sem to protect against races with removal of
1285 * VM_EXECUTABLE vmas */
1286 down_read(&mm->mmap_sem);
1287 exe_file = mm->exe_file;
1290 up_read(&mm->mmap_sem);
1294 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1296 /* It's safe to write the exe_file pointer without exe_file_lock because
1297 * this is called during fork when the task is not yet in /proc */
1298 newmm->exe_file = get_mm_exe_file(oldmm);
1301 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1303 struct task_struct *task;
1304 struct mm_struct *mm;
1305 struct file *exe_file;
1307 task = get_proc_task(inode);
1310 mm = get_task_mm(task);
1311 put_task_struct(task);
1314 exe_file = get_mm_exe_file(mm);
1317 *exe_path = exe_file->f_path;
1318 path_get(&exe_file->f_path);
1325 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1327 struct inode *inode = dentry->d_inode;
1328 int error = -EACCES;
1330 /* We don't need a base pointer in the /proc filesystem */
1331 path_put(&nd->path);
1333 /* Are we allowed to snoop on the tasks file descriptors? */
1334 if (!proc_fd_access_allowed(inode))
1337 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1338 nd->last_type = LAST_BIND;
1340 return ERR_PTR(error);
1343 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1345 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1352 pathname = d_path(path, tmp, PAGE_SIZE);
1353 len = PTR_ERR(pathname);
1354 if (IS_ERR(pathname))
1356 len = tmp + PAGE_SIZE - 1 - pathname;
1360 if (copy_to_user(buffer, pathname, len))
1363 free_page((unsigned long)tmp);
1367 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1369 int error = -EACCES;
1370 struct inode *inode = dentry->d_inode;
1373 /* Are we allowed to snoop on the tasks file descriptors? */
1374 if (!proc_fd_access_allowed(inode))
1377 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1381 error = do_proc_readlink(&path, buffer, buflen);
1387 static const struct inode_operations proc_pid_link_inode_operations = {
1388 .readlink = proc_pid_readlink,
1389 .follow_link = proc_pid_follow_link,
1390 .setattr = proc_setattr,
1394 /* building an inode */
1396 static int task_dumpable(struct task_struct *task)
1399 struct mm_struct *mm;
1404 dumpable = get_dumpable(mm);
1412 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1414 struct inode * inode;
1415 struct proc_inode *ei;
1416 const struct cred *cred;
1418 /* We need a new inode */
1420 inode = new_inode(sb);
1426 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1427 inode->i_op = &proc_def_inode_operations;
1430 * grab the reference to task.
1432 ei->pid = get_task_pid(task, PIDTYPE_PID);
1436 if (task_dumpable(task)) {
1438 cred = __task_cred(task);
1439 inode->i_uid = cred->euid;
1440 inode->i_gid = cred->egid;
1443 security_task_to_inode(task, inode);
1453 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1455 struct inode *inode = dentry->d_inode;
1456 struct task_struct *task;
1457 const struct cred *cred;
1459 generic_fillattr(inode, stat);
1464 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1466 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1467 task_dumpable(task)) {
1468 cred = __task_cred(task);
1469 stat->uid = cred->euid;
1470 stat->gid = cred->egid;
1480 * Exceptional case: normally we are not allowed to unhash a busy
1481 * directory. In this case, however, we can do it - no aliasing problems
1482 * due to the way we treat inodes.
1484 * Rewrite the inode's ownerships here because the owning task may have
1485 * performed a setuid(), etc.
1487 * Before the /proc/pid/status file was created the only way to read
1488 * the effective uid of a /process was to stat /proc/pid. Reading
1489 * /proc/pid/status is slow enough that procps and other packages
1490 * kept stating /proc/pid. To keep the rules in /proc simple I have
1491 * made this apply to all per process world readable and executable
1494 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1496 struct inode *inode = dentry->d_inode;
1497 struct task_struct *task = get_proc_task(inode);
1498 const struct cred *cred;
1501 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1502 task_dumpable(task)) {
1504 cred = __task_cred(task);
1505 inode->i_uid = cred->euid;
1506 inode->i_gid = cred->egid;
1512 inode->i_mode &= ~(S_ISUID | S_ISGID);
1513 security_task_to_inode(task, inode);
1514 put_task_struct(task);
1521 static int pid_delete_dentry(struct dentry * dentry)
1523 /* Is the task we represent dead?
1524 * If so, then don't put the dentry on the lru list,
1525 * kill it immediately.
1527 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1530 static const struct dentry_operations pid_dentry_operations =
1532 .d_revalidate = pid_revalidate,
1533 .d_delete = pid_delete_dentry,
1538 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1539 struct task_struct *, const void *);
1542 * Fill a directory entry.
1544 * If possible create the dcache entry and derive our inode number and
1545 * file type from dcache entry.
1547 * Since all of the proc inode numbers are dynamically generated, the inode
1548 * numbers do not exist until the inode is cache. This means creating the
1549 * the dcache entry in readdir is necessary to keep the inode numbers
1550 * reported by readdir in sync with the inode numbers reported
1553 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1554 char *name, int len,
1555 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1557 struct dentry *child, *dir = filp->f_path.dentry;
1558 struct inode *inode;
1561 unsigned type = DT_UNKNOWN;
1565 qname.hash = full_name_hash(name, len);
1567 child = d_lookup(dir, &qname);
1570 new = d_alloc(dir, &qname);
1572 child = instantiate(dir->d_inode, new, task, ptr);
1579 if (!child || IS_ERR(child) || !child->d_inode)
1580 goto end_instantiate;
1581 inode = child->d_inode;
1584 type = inode->i_mode >> 12;
1589 ino = find_inode_number(dir, &qname);
1592 return filldir(dirent, name, len, filp->f_pos, ino, type);
1595 static unsigned name_to_int(struct dentry *dentry)
1597 const char *name = dentry->d_name.name;
1598 int len = dentry->d_name.len;
1601 if (len > 1 && *name == '0')
1604 unsigned c = *name++ - '0';
1607 if (n >= (~0U-9)/10)
1617 #define PROC_FDINFO_MAX 64
1619 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1621 struct task_struct *task = get_proc_task(inode);
1622 struct files_struct *files = NULL;
1624 int fd = proc_fd(inode);
1627 files = get_files_struct(task);
1628 put_task_struct(task);
1632 * We are not taking a ref to the file structure, so we must
1635 spin_lock(&files->file_lock);
1636 file = fcheck_files(files, fd);
1639 *path = file->f_path;
1640 path_get(&file->f_path);
1643 snprintf(info, PROC_FDINFO_MAX,
1646 (long long) file->f_pos,
1648 spin_unlock(&files->file_lock);
1649 put_files_struct(files);
1652 spin_unlock(&files->file_lock);
1653 put_files_struct(files);
1658 static int proc_fd_link(struct inode *inode, struct path *path)
1660 return proc_fd_info(inode, path, NULL);
1663 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1665 struct inode *inode = dentry->d_inode;
1666 struct task_struct *task = get_proc_task(inode);
1667 int fd = proc_fd(inode);
1668 struct files_struct *files;
1669 const struct cred *cred;
1672 files = get_files_struct(task);
1675 if (fcheck_files(files, fd)) {
1677 put_files_struct(files);
1678 if (task_dumpable(task)) {
1680 cred = __task_cred(task);
1681 inode->i_uid = cred->euid;
1682 inode->i_gid = cred->egid;
1688 inode->i_mode &= ~(S_ISUID | S_ISGID);
1689 security_task_to_inode(task, inode);
1690 put_task_struct(task);
1694 put_files_struct(files);
1696 put_task_struct(task);
1702 static const struct dentry_operations tid_fd_dentry_operations =
1704 .d_revalidate = tid_fd_revalidate,
1705 .d_delete = pid_delete_dentry,
1708 static struct dentry *proc_fd_instantiate(struct inode *dir,
1709 struct dentry *dentry, struct task_struct *task, const void *ptr)
1711 unsigned fd = *(const unsigned *)ptr;
1713 struct files_struct *files;
1714 struct inode *inode;
1715 struct proc_inode *ei;
1716 struct dentry *error = ERR_PTR(-ENOENT);
1718 inode = proc_pid_make_inode(dir->i_sb, task);
1723 files = get_files_struct(task);
1726 inode->i_mode = S_IFLNK;
1729 * We are not taking a ref to the file structure, so we must
1732 spin_lock(&files->file_lock);
1733 file = fcheck_files(files, fd);
1736 if (file->f_mode & FMODE_READ)
1737 inode->i_mode |= S_IRUSR | S_IXUSR;
1738 if (file->f_mode & FMODE_WRITE)
1739 inode->i_mode |= S_IWUSR | S_IXUSR;
1740 spin_unlock(&files->file_lock);
1741 put_files_struct(files);
1743 inode->i_op = &proc_pid_link_inode_operations;
1745 ei->op.proc_get_link = proc_fd_link;
1746 dentry->d_op = &tid_fd_dentry_operations;
1747 d_add(dentry, inode);
1748 /* Close the race of the process dying before we return the dentry */
1749 if (tid_fd_revalidate(dentry, NULL))
1755 spin_unlock(&files->file_lock);
1756 put_files_struct(files);
1762 static struct dentry *proc_lookupfd_common(struct inode *dir,
1763 struct dentry *dentry,
1764 instantiate_t instantiate)
1766 struct task_struct *task = get_proc_task(dir);
1767 unsigned fd = name_to_int(dentry);
1768 struct dentry *result = ERR_PTR(-ENOENT);
1775 result = instantiate(dir, dentry, task, &fd);
1777 put_task_struct(task);
1782 static int proc_readfd_common(struct file * filp, void * dirent,
1783 filldir_t filldir, instantiate_t instantiate)
1785 struct dentry *dentry = filp->f_path.dentry;
1786 struct inode *inode = dentry->d_inode;
1787 struct task_struct *p = get_proc_task(inode);
1788 unsigned int fd, ino;
1790 struct files_struct * files;
1800 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1804 ino = parent_ino(dentry);
1805 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1809 files = get_files_struct(p);
1813 for (fd = filp->f_pos-2;
1814 fd < files_fdtable(files)->max_fds;
1815 fd++, filp->f_pos++) {
1816 char name[PROC_NUMBUF];
1819 if (!fcheck_files(files, fd))
1823 len = snprintf(name, sizeof(name), "%d", fd);
1824 if (proc_fill_cache(filp, dirent, filldir,
1825 name, len, instantiate,
1833 put_files_struct(files);
1841 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1842 struct nameidata *nd)
1844 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1847 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1849 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1852 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1853 size_t len, loff_t *ppos)
1855 char tmp[PROC_FDINFO_MAX];
1856 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1858 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1862 static const struct file_operations proc_fdinfo_file_operations = {
1863 .open = nonseekable_open,
1864 .read = proc_fdinfo_read,
1867 static const struct file_operations proc_fd_operations = {
1868 .read = generic_read_dir,
1869 .readdir = proc_readfd,
1873 * /proc/pid/fd needs a special permission handler so that a process can still
1874 * access /proc/self/fd after it has executed a setuid().
1876 static int proc_fd_permission(struct inode *inode, int mask)
1880 rv = generic_permission(inode, mask, NULL);
1883 if (task_pid(current) == proc_pid(inode))
1889 * proc directories can do almost nothing..
1891 static const struct inode_operations proc_fd_inode_operations = {
1892 .lookup = proc_lookupfd,
1893 .permission = proc_fd_permission,
1894 .setattr = proc_setattr,
1897 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1898 struct dentry *dentry, struct task_struct *task, const void *ptr)
1900 unsigned fd = *(unsigned *)ptr;
1901 struct inode *inode;
1902 struct proc_inode *ei;
1903 struct dentry *error = ERR_PTR(-ENOENT);
1905 inode = proc_pid_make_inode(dir->i_sb, task);
1910 inode->i_mode = S_IFREG | S_IRUSR;
1911 inode->i_fop = &proc_fdinfo_file_operations;
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))
1922 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1923 struct dentry *dentry,
1924 struct nameidata *nd)
1926 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1929 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1931 return proc_readfd_common(filp, dirent, filldir,
1932 proc_fdinfo_instantiate);
1935 static const struct file_operations proc_fdinfo_operations = {
1936 .read = generic_read_dir,
1937 .readdir = proc_readfdinfo,
1941 * proc directories can do almost nothing..
1943 static const struct inode_operations proc_fdinfo_inode_operations = {
1944 .lookup = proc_lookupfdinfo,
1945 .setattr = proc_setattr,
1949 static struct dentry *proc_pident_instantiate(struct inode *dir,
1950 struct dentry *dentry, struct task_struct *task, const void *ptr)
1952 const struct pid_entry *p = ptr;
1953 struct inode *inode;
1954 struct proc_inode *ei;
1955 struct dentry *error = ERR_PTR(-EINVAL);
1957 inode = proc_pid_make_inode(dir->i_sb, task);
1962 inode->i_mode = p->mode;
1963 if (S_ISDIR(inode->i_mode))
1964 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1966 inode->i_op = p->iop;
1968 inode->i_fop = p->fop;
1970 dentry->d_op = &pid_dentry_operations;
1971 d_add(dentry, inode);
1972 /* Close the race of the process dying before we return the dentry */
1973 if (pid_revalidate(dentry, NULL))
1979 static struct dentry *proc_pident_lookup(struct inode *dir,
1980 struct dentry *dentry,
1981 const struct pid_entry *ents,
1984 struct dentry *error;
1985 struct task_struct *task = get_proc_task(dir);
1986 const struct pid_entry *p, *last;
1988 error = ERR_PTR(-ENOENT);
1994 * Yes, it does not scale. And it should not. Don't add
1995 * new entries into /proc/<tgid>/ without very good reasons.
1997 last = &ents[nents - 1];
1998 for (p = ents; p <= last; p++) {
1999 if (p->len != dentry->d_name.len)
2001 if (!memcmp(dentry->d_name.name, p->name, p->len))
2007 error = proc_pident_instantiate(dir, dentry, task, p);
2009 put_task_struct(task);
2014 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2015 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2017 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2018 proc_pident_instantiate, task, p);
2021 static int proc_pident_readdir(struct file *filp,
2022 void *dirent, filldir_t filldir,
2023 const struct pid_entry *ents, unsigned int nents)
2026 struct dentry *dentry = filp->f_path.dentry;
2027 struct inode *inode = dentry->d_inode;
2028 struct task_struct *task = get_proc_task(inode);
2029 const struct pid_entry *p, *last;
2042 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2048 ino = parent_ino(dentry);
2049 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2061 last = &ents[nents - 1];
2063 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2072 put_task_struct(task);
2077 #ifdef CONFIG_SECURITY
2078 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2079 size_t count, loff_t *ppos)
2081 struct inode * inode = file->f_path.dentry->d_inode;
2084 struct task_struct *task = get_proc_task(inode);
2089 length = security_getprocattr(task,
2090 (char*)file->f_path.dentry->d_name.name,
2092 put_task_struct(task);
2094 length = simple_read_from_buffer(buf, count, ppos, p, length);
2099 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2100 size_t count, loff_t *ppos)
2102 struct inode * inode = file->f_path.dentry->d_inode;
2105 struct task_struct *task = get_proc_task(inode);
2110 if (count > PAGE_SIZE)
2113 /* No partial writes. */
2119 page = (char*)__get_free_page(GFP_TEMPORARY);
2124 if (copy_from_user(page, buf, count))
2127 length = security_setprocattr(task,
2128 (char*)file->f_path.dentry->d_name.name,
2129 (void*)page, count);
2131 free_page((unsigned long) page);
2133 put_task_struct(task);
2138 static const struct file_operations proc_pid_attr_operations = {
2139 .read = proc_pid_attr_read,
2140 .write = proc_pid_attr_write,
2143 static const struct pid_entry attr_dir_stuff[] = {
2144 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2145 REG("prev", S_IRUGO, proc_pid_attr_operations),
2146 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2147 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2148 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2149 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2152 static int proc_attr_dir_readdir(struct file * filp,
2153 void * dirent, filldir_t filldir)
2155 return proc_pident_readdir(filp,dirent,filldir,
2156 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2159 static const struct file_operations proc_attr_dir_operations = {
2160 .read = generic_read_dir,
2161 .readdir = proc_attr_dir_readdir,
2164 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2165 struct dentry *dentry, struct nameidata *nd)
2167 return proc_pident_lookup(dir, dentry,
2168 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2171 static const struct inode_operations proc_attr_dir_inode_operations = {
2172 .lookup = proc_attr_dir_lookup,
2173 .getattr = pid_getattr,
2174 .setattr = proc_setattr,
2179 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2180 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2181 size_t count, loff_t *ppos)
2183 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2184 struct mm_struct *mm;
2185 char buffer[PROC_NUMBUF];
2193 mm = get_task_mm(task);
2195 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2196 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2197 MMF_DUMP_FILTER_SHIFT));
2199 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2202 put_task_struct(task);
2207 static ssize_t proc_coredump_filter_write(struct file *file,
2208 const char __user *buf,
2212 struct task_struct *task;
2213 struct mm_struct *mm;
2214 char buffer[PROC_NUMBUF], *end;
2221 memset(buffer, 0, sizeof(buffer));
2222 if (count > sizeof(buffer) - 1)
2223 count = sizeof(buffer) - 1;
2224 if (copy_from_user(buffer, buf, count))
2228 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2231 if (end - buffer == 0)
2235 task = get_proc_task(file->f_dentry->d_inode);
2240 mm = get_task_mm(task);
2244 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2246 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2248 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2253 put_task_struct(task);
2258 static const struct file_operations proc_coredump_filter_operations = {
2259 .read = proc_coredump_filter_read,
2260 .write = proc_coredump_filter_write,
2267 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2270 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2271 pid_t tgid = task_tgid_nr_ns(current, ns);
2272 char tmp[PROC_NUMBUF];
2275 sprintf(tmp, "%d", tgid);
2276 return vfs_readlink(dentry,buffer,buflen,tmp);
2279 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2281 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2282 pid_t tgid = task_tgid_nr_ns(current, ns);
2283 char tmp[PROC_NUMBUF];
2285 return ERR_PTR(-ENOENT);
2286 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2287 return ERR_PTR(vfs_follow_link(nd,tmp));
2290 static const struct inode_operations proc_self_inode_operations = {
2291 .readlink = proc_self_readlink,
2292 .follow_link = proc_self_follow_link,
2298 * These are the directory entries in the root directory of /proc
2299 * that properly belong to the /proc filesystem, as they describe
2300 * describe something that is process related.
2302 static const struct pid_entry proc_base_stuff[] = {
2303 NOD("self", S_IFLNK|S_IRWXUGO,
2304 &proc_self_inode_operations, NULL, {}),
2308 * Exceptional case: normally we are not allowed to unhash a busy
2309 * directory. In this case, however, we can do it - no aliasing problems
2310 * due to the way we treat inodes.
2312 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2314 struct inode *inode = dentry->d_inode;
2315 struct task_struct *task = get_proc_task(inode);
2317 put_task_struct(task);
2324 static const struct dentry_operations proc_base_dentry_operations =
2326 .d_revalidate = proc_base_revalidate,
2327 .d_delete = pid_delete_dentry,
2330 static struct dentry *proc_base_instantiate(struct inode *dir,
2331 struct dentry *dentry, struct task_struct *task, const void *ptr)
2333 const struct pid_entry *p = ptr;
2334 struct inode *inode;
2335 struct proc_inode *ei;
2336 struct dentry *error = ERR_PTR(-EINVAL);
2338 /* Allocate the inode */
2339 error = ERR_PTR(-ENOMEM);
2340 inode = new_inode(dir->i_sb);
2344 /* Initialize the inode */
2346 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2349 * grab the reference to the task.
2351 ei->pid = get_task_pid(task, PIDTYPE_PID);
2355 inode->i_mode = p->mode;
2356 if (S_ISDIR(inode->i_mode))
2358 if (S_ISLNK(inode->i_mode))
2361 inode->i_op = p->iop;
2363 inode->i_fop = p->fop;
2365 dentry->d_op = &proc_base_dentry_operations;
2366 d_add(dentry, inode);
2375 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2377 struct dentry *error;
2378 struct task_struct *task = get_proc_task(dir);
2379 const struct pid_entry *p, *last;
2381 error = ERR_PTR(-ENOENT);
2386 /* Lookup the directory entry */
2387 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2388 for (p = proc_base_stuff; p <= last; p++) {
2389 if (p->len != dentry->d_name.len)
2391 if (!memcmp(dentry->d_name.name, p->name, p->len))
2397 error = proc_base_instantiate(dir, dentry, task, p);
2400 put_task_struct(task);
2405 static int proc_base_fill_cache(struct file *filp, void *dirent,
2406 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2408 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2409 proc_base_instantiate, task, p);
2412 #ifdef CONFIG_TASK_IO_ACCOUNTING
2413 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2415 struct task_io_accounting acct = task->ioac;
2416 unsigned long flags;
2418 if (whole && lock_task_sighand(task, &flags)) {
2419 struct task_struct *t = task;
2421 task_io_accounting_add(&acct, &task->signal->ioac);
2422 while_each_thread(task, t)
2423 task_io_accounting_add(&acct, &t->ioac);
2425 unlock_task_sighand(task, &flags);
2427 return sprintf(buffer,
2432 "read_bytes: %llu\n"
2433 "write_bytes: %llu\n"
2434 "cancelled_write_bytes: %llu\n",
2435 (unsigned long long)acct.rchar,
2436 (unsigned long long)acct.wchar,
2437 (unsigned long long)acct.syscr,
2438 (unsigned long long)acct.syscw,
2439 (unsigned long long)acct.read_bytes,
2440 (unsigned long long)acct.write_bytes,
2441 (unsigned long long)acct.cancelled_write_bytes);
2444 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2446 return do_io_accounting(task, buffer, 0);
2449 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2451 return do_io_accounting(task, buffer, 1);
2453 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2455 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2456 struct pid *pid, struct task_struct *task)
2458 seq_printf(m, "%08x\n", task->personality);
2465 static const struct file_operations proc_task_operations;
2466 static const struct inode_operations proc_task_inode_operations;
2468 static const struct pid_entry tgid_base_stuff[] = {
2469 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2470 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2471 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2473 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2475 REG("environ", S_IRUSR, proc_environ_operations),
2476 INF("auxv", S_IRUSR, proc_pid_auxv),
2477 ONE("status", S_IRUGO, proc_pid_status),
2478 ONE("personality", S_IRUSR, proc_pid_personality),
2479 INF("limits", S_IRUSR, proc_pid_limits),
2480 #ifdef CONFIG_SCHED_DEBUG
2481 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2483 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2484 INF("syscall", S_IRUSR, proc_pid_syscall),
2486 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2487 ONE("stat", S_IRUGO, proc_tgid_stat),
2488 ONE("statm", S_IRUGO, proc_pid_statm),
2489 REG("maps", S_IRUGO, proc_maps_operations),
2491 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2493 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2494 LNK("cwd", proc_cwd_link),
2495 LNK("root", proc_root_link),
2496 LNK("exe", proc_exe_link),
2497 REG("mounts", S_IRUGO, proc_mounts_operations),
2498 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2499 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2500 #ifdef CONFIG_PROC_PAGE_MONITOR
2501 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2502 REG("smaps", S_IRUGO, proc_smaps_operations),
2503 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2505 #ifdef CONFIG_SECURITY
2506 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2508 #ifdef CONFIG_KALLSYMS
2509 INF("wchan", S_IRUGO, proc_pid_wchan),
2511 #ifdef CONFIG_STACKTRACE
2512 ONE("stack", S_IRUSR, proc_pid_stack),
2514 #ifdef CONFIG_SCHEDSTATS
2515 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2517 #ifdef CONFIG_LATENCYTOP
2518 REG("latency", S_IRUGO, proc_lstats_operations),
2520 #ifdef CONFIG_PROC_PID_CPUSET
2521 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2523 #ifdef CONFIG_CGROUPS
2524 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2526 INF("oom_score", S_IRUGO, proc_oom_score),
2527 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2528 #ifdef CONFIG_AUDITSYSCALL
2529 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2530 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2532 #ifdef CONFIG_FAULT_INJECTION
2533 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2535 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2536 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2538 #ifdef CONFIG_TASK_IO_ACCOUNTING
2539 INF("io", S_IRUGO, proc_tgid_io_accounting),
2543 static int proc_tgid_base_readdir(struct file * filp,
2544 void * dirent, filldir_t filldir)
2546 return proc_pident_readdir(filp,dirent,filldir,
2547 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2550 static const struct file_operations proc_tgid_base_operations = {
2551 .read = generic_read_dir,
2552 .readdir = proc_tgid_base_readdir,
2555 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2556 return proc_pident_lookup(dir, dentry,
2557 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2560 static const struct inode_operations proc_tgid_base_inode_operations = {
2561 .lookup = proc_tgid_base_lookup,
2562 .getattr = pid_getattr,
2563 .setattr = proc_setattr,
2566 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2568 struct dentry *dentry, *leader, *dir;
2569 char buf[PROC_NUMBUF];
2573 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2574 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2576 if (!(current->flags & PF_EXITING))
2577 shrink_dcache_parent(dentry);
2586 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2587 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2592 name.len = strlen(name.name);
2593 dir = d_hash_and_lookup(leader, &name);
2595 goto out_put_leader;
2598 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2599 dentry = d_hash_and_lookup(dir, &name);
2601 shrink_dcache_parent(dentry);
2614 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2615 * @task: task that should be flushed.
2617 * When flushing dentries from proc, one needs to flush them from global
2618 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2619 * in. This call is supposed to do all of this job.
2621 * Looks in the dcache for
2623 * /proc/@tgid/task/@pid
2624 * if either directory is present flushes it and all of it'ts children
2627 * It is safe and reasonable to cache /proc entries for a task until
2628 * that task exits. After that they just clog up the dcache with
2629 * useless entries, possibly causing useful dcache entries to be
2630 * flushed instead. This routine is proved to flush those useless
2631 * dcache entries at process exit time.
2633 * NOTE: This routine is just an optimization so it does not guarantee
2634 * that no dcache entries will exist at process exit time it
2635 * just makes it very unlikely that any will persist.
2638 void proc_flush_task(struct task_struct *task)
2641 struct pid *pid, *tgid = NULL;
2644 pid = task_pid(task);
2645 if (thread_group_leader(task))
2646 tgid = task_tgid(task);
2648 for (i = 0; i <= pid->level; i++) {
2649 upid = &pid->numbers[i];
2650 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2651 tgid ? tgid->numbers[i].nr : 0);
2654 upid = &pid->numbers[pid->level];
2656 pid_ns_release_proc(upid->ns);
2659 static struct dentry *proc_pid_instantiate(struct inode *dir,
2660 struct dentry * dentry,
2661 struct task_struct *task, const void *ptr)
2663 struct dentry *error = ERR_PTR(-ENOENT);
2664 struct inode *inode;
2666 inode = proc_pid_make_inode(dir->i_sb, task);
2670 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2671 inode->i_op = &proc_tgid_base_inode_operations;
2672 inode->i_fop = &proc_tgid_base_operations;
2673 inode->i_flags|=S_IMMUTABLE;
2675 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2676 ARRAY_SIZE(tgid_base_stuff));
2678 dentry->d_op = &pid_dentry_operations;
2680 d_add(dentry, inode);
2681 /* Close the race of the process dying before we return the dentry */
2682 if (pid_revalidate(dentry, NULL))
2688 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2690 struct dentry *result = ERR_PTR(-ENOENT);
2691 struct task_struct *task;
2693 struct pid_namespace *ns;
2695 result = proc_base_lookup(dir, dentry);
2696 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2699 tgid = name_to_int(dentry);
2703 ns = dentry->d_sb->s_fs_info;
2705 task = find_task_by_pid_ns(tgid, ns);
2707 get_task_struct(task);
2712 result = proc_pid_instantiate(dir, dentry, task, NULL);
2713 put_task_struct(task);
2719 * Find the first task with tgid >= tgid
2724 struct task_struct *task;
2726 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2731 put_task_struct(iter.task);
2735 pid = find_ge_pid(iter.tgid, ns);
2737 iter.tgid = pid_nr_ns(pid, ns);
2738 iter.task = pid_task(pid, PIDTYPE_PID);
2739 /* What we to know is if the pid we have find is the
2740 * pid of a thread_group_leader. Testing for task
2741 * being a thread_group_leader is the obvious thing
2742 * todo but there is a window when it fails, due to
2743 * the pid transfer logic in de_thread.
2745 * So we perform the straight forward test of seeing
2746 * if the pid we have found is the pid of a thread
2747 * group leader, and don't worry if the task we have
2748 * found doesn't happen to be a thread group leader.
2749 * As we don't care in the case of readdir.
2751 if (!iter.task || !has_group_leader_pid(iter.task)) {
2755 get_task_struct(iter.task);
2761 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2763 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2764 struct tgid_iter iter)
2766 char name[PROC_NUMBUF];
2767 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2768 return proc_fill_cache(filp, dirent, filldir, name, len,
2769 proc_pid_instantiate, iter.task, NULL);
2772 /* for the /proc/ directory itself, after non-process stuff has been done */
2773 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2775 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2776 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2777 struct tgid_iter iter;
2778 struct pid_namespace *ns;
2783 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2784 const struct pid_entry *p = &proc_base_stuff[nr];
2785 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2789 ns = filp->f_dentry->d_sb->s_fs_info;
2791 iter.tgid = filp->f_pos - TGID_OFFSET;
2792 for (iter = next_tgid(ns, iter);
2794 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2795 filp->f_pos = iter.tgid + TGID_OFFSET;
2796 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2797 put_task_struct(iter.task);
2801 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2803 put_task_struct(reaper);
2811 static const struct pid_entry tid_base_stuff[] = {
2812 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2813 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2814 REG("environ", S_IRUSR, proc_environ_operations),
2815 INF("auxv", S_IRUSR, proc_pid_auxv),
2816 ONE("status", S_IRUGO, proc_pid_status),
2817 ONE("personality", S_IRUSR, proc_pid_personality),
2818 INF("limits", S_IRUSR, proc_pid_limits),
2819 #ifdef CONFIG_SCHED_DEBUG
2820 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2822 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2823 INF("syscall", S_IRUSR, proc_pid_syscall),
2825 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2826 ONE("stat", S_IRUGO, proc_tid_stat),
2827 ONE("statm", S_IRUGO, proc_pid_statm),
2828 REG("maps", S_IRUGO, proc_maps_operations),
2830 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2832 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2833 LNK("cwd", proc_cwd_link),
2834 LNK("root", proc_root_link),
2835 LNK("exe", proc_exe_link),
2836 REG("mounts", S_IRUGO, proc_mounts_operations),
2837 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2838 #ifdef CONFIG_PROC_PAGE_MONITOR
2839 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2840 REG("smaps", S_IRUGO, proc_smaps_operations),
2841 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2843 #ifdef CONFIG_SECURITY
2844 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2846 #ifdef CONFIG_KALLSYMS
2847 INF("wchan", S_IRUGO, proc_pid_wchan),
2849 #ifdef CONFIG_STACKTRACE
2850 ONE("stack", S_IRUSR, proc_pid_stack),
2852 #ifdef CONFIG_SCHEDSTATS
2853 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2855 #ifdef CONFIG_LATENCYTOP
2856 REG("latency", S_IRUGO, proc_lstats_operations),
2858 #ifdef CONFIG_PROC_PID_CPUSET
2859 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2861 #ifdef CONFIG_CGROUPS
2862 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2864 INF("oom_score", S_IRUGO, proc_oom_score),
2865 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2866 #ifdef CONFIG_AUDITSYSCALL
2867 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2868 REG("sessionid", S_IRUSR, proc_sessionid_operations),
2870 #ifdef CONFIG_FAULT_INJECTION
2871 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2873 #ifdef CONFIG_TASK_IO_ACCOUNTING
2874 INF("io", S_IRUGO, proc_tid_io_accounting),
2878 static int proc_tid_base_readdir(struct file * filp,
2879 void * dirent, filldir_t filldir)
2881 return proc_pident_readdir(filp,dirent,filldir,
2882 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2885 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2886 return proc_pident_lookup(dir, dentry,
2887 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2890 static const struct file_operations proc_tid_base_operations = {
2891 .read = generic_read_dir,
2892 .readdir = proc_tid_base_readdir,
2895 static const struct inode_operations proc_tid_base_inode_operations = {
2896 .lookup = proc_tid_base_lookup,
2897 .getattr = pid_getattr,
2898 .setattr = proc_setattr,
2901 static struct dentry *proc_task_instantiate(struct inode *dir,
2902 struct dentry *dentry, struct task_struct *task, const void *ptr)
2904 struct dentry *error = ERR_PTR(-ENOENT);
2905 struct inode *inode;
2906 inode = proc_pid_make_inode(dir->i_sb, task);
2910 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2911 inode->i_op = &proc_tid_base_inode_operations;
2912 inode->i_fop = &proc_tid_base_operations;
2913 inode->i_flags|=S_IMMUTABLE;
2915 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2916 ARRAY_SIZE(tid_base_stuff));
2918 dentry->d_op = &pid_dentry_operations;
2920 d_add(dentry, inode);
2921 /* Close the race of the process dying before we return the dentry */
2922 if (pid_revalidate(dentry, NULL))
2928 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2930 struct dentry *result = ERR_PTR(-ENOENT);
2931 struct task_struct *task;
2932 struct task_struct *leader = get_proc_task(dir);
2934 struct pid_namespace *ns;
2939 tid = name_to_int(dentry);
2943 ns = dentry->d_sb->s_fs_info;
2945 task = find_task_by_pid_ns(tid, ns);
2947 get_task_struct(task);
2951 if (!same_thread_group(leader, task))
2954 result = proc_task_instantiate(dir, dentry, task, NULL);
2956 put_task_struct(task);
2958 put_task_struct(leader);
2964 * Find the first tid of a thread group to return to user space.
2966 * Usually this is just the thread group leader, but if the users
2967 * buffer was too small or there was a seek into the middle of the
2968 * directory we have more work todo.
2970 * In the case of a short read we start with find_task_by_pid.
2972 * In the case of a seek we start with the leader and walk nr
2975 static struct task_struct *first_tid(struct task_struct *leader,
2976 int tid, int nr, struct pid_namespace *ns)
2978 struct task_struct *pos;
2981 /* Attempt to start with the pid of a thread */
2982 if (tid && (nr > 0)) {
2983 pos = find_task_by_pid_ns(tid, ns);
2984 if (pos && (pos->group_leader == leader))
2988 /* If nr exceeds the number of threads there is nothing todo */
2990 if (nr && nr >= get_nr_threads(leader))
2993 /* If we haven't found our starting place yet start
2994 * with the leader and walk nr threads forward.
2996 for (pos = leader; nr > 0; --nr) {
2997 pos = next_thread(pos);
2998 if (pos == leader) {
3004 get_task_struct(pos);
3011 * Find the next thread in the thread list.
3012 * Return NULL if there is an error or no next thread.
3014 * The reference to the input task_struct is released.
3016 static struct task_struct *next_tid(struct task_struct *start)
3018 struct task_struct *pos = NULL;
3020 if (pid_alive(start)) {
3021 pos = next_thread(start);
3022 if (thread_group_leader(pos))
3025 get_task_struct(pos);
3028 put_task_struct(start);
3032 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3033 struct task_struct *task, int tid)
3035 char name[PROC_NUMBUF];
3036 int len = snprintf(name, sizeof(name), "%d", tid);
3037 return proc_fill_cache(filp, dirent, filldir, name, len,
3038 proc_task_instantiate, task, NULL);
3041 /* for the /proc/TGID/task/ directories */
3042 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3044 struct dentry *dentry = filp->f_path.dentry;
3045 struct inode *inode = dentry->d_inode;
3046 struct task_struct *leader = NULL;
3047 struct task_struct *task;
3048 int retval = -ENOENT;
3051 struct pid_namespace *ns;
3053 task = get_proc_task(inode);
3057 if (pid_alive(task)) {
3058 leader = task->group_leader;
3059 get_task_struct(leader);
3062 put_task_struct(task);
3067 switch ((unsigned long)filp->f_pos) {
3070 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3075 ino = parent_ino(dentry);
3076 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3082 /* f_version caches the tgid value that the last readdir call couldn't
3083 * return. lseek aka telldir automagically resets f_version to 0.
3085 ns = filp->f_dentry->d_sb->s_fs_info;
3086 tid = (int)filp->f_version;
3087 filp->f_version = 0;
3088 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3090 task = next_tid(task), filp->f_pos++) {
3091 tid = task_pid_nr_ns(task, ns);
3092 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3093 /* returning this tgid failed, save it as the first
3094 * pid for the next readir call */
3095 filp->f_version = (u64)tid;
3096 put_task_struct(task);
3101 put_task_struct(leader);
3106 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3108 struct inode *inode = dentry->d_inode;
3109 struct task_struct *p = get_proc_task(inode);
3110 generic_fillattr(inode, stat);
3113 stat->nlink += get_nr_threads(p);
3120 static const struct inode_operations proc_task_inode_operations = {
3121 .lookup = proc_task_lookup,
3122 .getattr = proc_task_getattr,
3123 .setattr = proc_setattr,
3126 static const struct file_operations proc_task_operations = {
3127 .read = generic_read_dir,
3128 .readdir = proc_task_readdir,