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 struct fs_struct *get_fs_struct(struct task_struct *task)
151 struct fs_struct *fs;
155 atomic_inc(&fs->count);
160 static int get_nr_threads(struct task_struct *tsk)
165 if (lock_task_sighand(tsk, &flags)) {
166 count = atomic_read(&tsk->signal->count);
167 unlock_task_sighand(tsk, &flags);
172 static int proc_cwd_link(struct inode *inode, struct path *path)
174 struct task_struct *task = get_proc_task(inode);
175 struct fs_struct *fs = NULL;
176 int result = -ENOENT;
179 fs = get_fs_struct(task);
180 put_task_struct(task);
183 read_lock(&fs->lock);
186 read_unlock(&fs->lock);
193 static int proc_root_link(struct inode *inode, struct path *path)
195 struct task_struct *task = get_proc_task(inode);
196 struct fs_struct *fs = NULL;
197 int result = -ENOENT;
200 fs = get_fs_struct(task);
201 put_task_struct(task);
204 read_lock(&fs->lock);
207 read_unlock(&fs->lock);
215 * Return zero if current may access user memory in @task, -error if not.
217 static int check_mem_permission(struct task_struct *task)
220 * A task can always look at itself, in case it chooses
221 * to use system calls instead of load instructions.
227 * If current is actively ptrace'ing, and would also be
228 * permitted to freshly attach with ptrace now, permit it.
230 if (task_is_stopped_or_traced(task)) {
233 match = (tracehook_tracer_task(task) == current);
235 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
240 * Noone else is allowed.
245 struct mm_struct *mm_for_maps(struct task_struct *task)
247 struct mm_struct *mm = get_task_mm(task);
250 down_read(&mm->mmap_sem);
254 if (task->mm != current->mm &&
255 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
261 up_read(&mm->mmap_sem);
266 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
270 struct mm_struct *mm = get_task_mm(task);
274 goto out_mm; /* Shh! No looking before we're done */
276 len = mm->arg_end - mm->arg_start;
281 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
283 // If the nul at the end of args has been overwritten, then
284 // assume application is using setproctitle(3).
285 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
286 len = strnlen(buffer, res);
290 len = mm->env_end - mm->env_start;
291 if (len > PAGE_SIZE - res)
292 len = PAGE_SIZE - res;
293 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
294 res = strnlen(buffer, res);
303 static int proc_pid_auxv(struct task_struct *task, char *buffer)
306 struct mm_struct *mm = get_task_mm(task);
308 unsigned int nwords = 0;
311 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
312 res = nwords * sizeof(mm->saved_auxv[0]);
315 memcpy(buffer, mm->saved_auxv, res);
322 #ifdef CONFIG_KALLSYMS
324 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
325 * Returns the resolved symbol. If that fails, simply return the address.
327 static int proc_pid_wchan(struct task_struct *task, char *buffer)
330 char symname[KSYM_NAME_LEN];
332 wchan = get_wchan(task);
334 if (lookup_symbol_name(wchan, symname) < 0)
335 return sprintf(buffer, "%lu", wchan);
337 return sprintf(buffer, "%s", symname);
339 #endif /* CONFIG_KALLSYMS */
341 #ifdef CONFIG_STACKTRACE
343 #define MAX_STACK_TRACE_DEPTH 64
345 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
346 struct pid *pid, struct task_struct *task)
348 struct stack_trace trace;
349 unsigned long *entries;
352 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
356 trace.nr_entries = 0;
357 trace.max_entries = MAX_STACK_TRACE_DEPTH;
358 trace.entries = entries;
360 save_stack_trace_tsk(task, &trace);
362 for (i = 0; i < trace.nr_entries; i++) {
363 seq_printf(m, "[<%p>] %pS\n",
364 (void *)entries[i], (void *)entries[i]);
372 #ifdef CONFIG_SCHEDSTATS
374 * Provides /proc/PID/schedstat
376 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
378 return sprintf(buffer, "%llu %llu %lu\n",
379 (unsigned long long)task->se.sum_exec_runtime,
380 (unsigned long long)task->sched_info.run_delay,
381 task->sched_info.pcount);
385 #ifdef CONFIG_LATENCYTOP
386 static int lstats_show_proc(struct seq_file *m, void *v)
389 struct inode *inode = m->private;
390 struct task_struct *task = get_proc_task(inode);
394 seq_puts(m, "Latency Top version : v0.1\n");
395 for (i = 0; i < 32; i++) {
396 if (task->latency_record[i].backtrace[0]) {
398 seq_printf(m, "%i %li %li ",
399 task->latency_record[i].count,
400 task->latency_record[i].time,
401 task->latency_record[i].max);
402 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
403 char sym[KSYM_SYMBOL_LEN];
405 if (!task->latency_record[i].backtrace[q])
407 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
409 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
410 c = strchr(sym, '+');
413 seq_printf(m, "%s ", sym);
419 put_task_struct(task);
423 static int lstats_open(struct inode *inode, struct file *file)
425 return single_open(file, lstats_show_proc, inode);
428 static ssize_t lstats_write(struct file *file, const char __user *buf,
429 size_t count, loff_t *offs)
431 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
435 clear_all_latency_tracing(task);
436 put_task_struct(task);
441 static const struct file_operations proc_lstats_operations = {
444 .write = lstats_write,
446 .release = single_release,
451 /* The badness from the OOM killer */
452 unsigned long badness(struct task_struct *p, unsigned long uptime);
453 static int proc_oom_score(struct task_struct *task, char *buffer)
455 unsigned long points;
456 struct timespec uptime;
458 do_posix_clock_monotonic_gettime(&uptime);
459 read_lock(&tasklist_lock);
460 points = badness(task, uptime.tv_sec);
461 read_unlock(&tasklist_lock);
462 return sprintf(buffer, "%lu\n", points);
470 static const struct limit_names lnames[RLIM_NLIMITS] = {
471 [RLIMIT_CPU] = {"Max cpu time", "ms"},
472 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
473 [RLIMIT_DATA] = {"Max data size", "bytes"},
474 [RLIMIT_STACK] = {"Max stack size", "bytes"},
475 [RLIMIT_CORE] = {"Max core file size", "bytes"},
476 [RLIMIT_RSS] = {"Max resident set", "bytes"},
477 [RLIMIT_NPROC] = {"Max processes", "processes"},
478 [RLIMIT_NOFILE] = {"Max open files", "files"},
479 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
480 [RLIMIT_AS] = {"Max address space", "bytes"},
481 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
482 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
483 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
484 [RLIMIT_NICE] = {"Max nice priority", NULL},
485 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
486 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
489 /* Display limits for a process */
490 static int proc_pid_limits(struct task_struct *task, char *buffer)
495 char *bufptr = buffer;
497 struct rlimit rlim[RLIM_NLIMITS];
499 if (!lock_task_sighand(task, &flags))
501 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
502 unlock_task_sighand(task, &flags);
505 * print the file header
507 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
508 "Limit", "Soft Limit", "Hard Limit", "Units");
510 for (i = 0; i < RLIM_NLIMITS; i++) {
511 if (rlim[i].rlim_cur == RLIM_INFINITY)
512 count += sprintf(&bufptr[count], "%-25s %-20s ",
513 lnames[i].name, "unlimited");
515 count += sprintf(&bufptr[count], "%-25s %-20lu ",
516 lnames[i].name, rlim[i].rlim_cur);
518 if (rlim[i].rlim_max == RLIM_INFINITY)
519 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
521 count += sprintf(&bufptr[count], "%-20lu ",
525 count += sprintf(&bufptr[count], "%-10s\n",
528 count += sprintf(&bufptr[count], "\n");
534 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
535 static int proc_pid_syscall(struct task_struct *task, char *buffer)
538 unsigned long args[6], sp, pc;
540 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
541 return sprintf(buffer, "running\n");
544 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
546 return sprintf(buffer,
547 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
549 args[0], args[1], args[2], args[3], args[4], args[5],
552 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
554 /************************************************************************/
555 /* Here the fs part begins */
556 /************************************************************************/
558 /* permission checks */
559 static int proc_fd_access_allowed(struct inode *inode)
561 struct task_struct *task;
563 /* Allow access to a task's file descriptors if it is us or we
564 * may use ptrace attach to the process and find out that
567 task = get_proc_task(inode);
569 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
570 put_task_struct(task);
575 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
578 struct inode *inode = dentry->d_inode;
580 if (attr->ia_valid & ATTR_MODE)
583 error = inode_change_ok(inode, attr);
585 error = inode_setattr(inode, attr);
589 static const struct inode_operations proc_def_inode_operations = {
590 .setattr = proc_setattr,
593 static int mounts_open_common(struct inode *inode, struct file *file,
594 const struct seq_operations *op)
596 struct task_struct *task = get_proc_task(inode);
598 struct mnt_namespace *ns = NULL;
599 struct fs_struct *fs = NULL;
601 struct proc_mounts *p;
606 nsp = task_nsproxy(task);
614 fs = get_fs_struct(task);
615 put_task_struct(task);
623 read_lock(&fs->lock);
626 read_unlock(&fs->lock);
630 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
634 file->private_data = &p->m;
635 ret = seq_open(file, op);
642 p->event = ns->event;
656 static int mounts_release(struct inode *inode, struct file *file)
658 struct proc_mounts *p = file->private_data;
661 return seq_release(inode, file);
664 static unsigned mounts_poll(struct file *file, poll_table *wait)
666 struct proc_mounts *p = file->private_data;
667 struct mnt_namespace *ns = p->ns;
670 poll_wait(file, &ns->poll, wait);
672 spin_lock(&vfsmount_lock);
673 if (p->event != ns->event) {
674 p->event = ns->event;
677 spin_unlock(&vfsmount_lock);
682 static int mounts_open(struct inode *inode, struct file *file)
684 return mounts_open_common(inode, file, &mounts_op);
687 static const struct file_operations proc_mounts_operations = {
691 .release = mounts_release,
695 static int mountinfo_open(struct inode *inode, struct file *file)
697 return mounts_open_common(inode, file, &mountinfo_op);
700 static const struct file_operations proc_mountinfo_operations = {
701 .open = mountinfo_open,
704 .release = mounts_release,
708 static int mountstats_open(struct inode *inode, struct file *file)
710 return mounts_open_common(inode, file, &mountstats_op);
713 static const struct file_operations proc_mountstats_operations = {
714 .open = mountstats_open,
717 .release = mounts_release,
720 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
722 static ssize_t proc_info_read(struct file * file, char __user * buf,
723 size_t count, loff_t *ppos)
725 struct inode * inode = file->f_path.dentry->d_inode;
728 struct task_struct *task = get_proc_task(inode);
734 if (count > PROC_BLOCK_SIZE)
735 count = PROC_BLOCK_SIZE;
738 if (!(page = __get_free_page(GFP_TEMPORARY)))
741 length = PROC_I(inode)->op.proc_read(task, (char*)page);
744 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
747 put_task_struct(task);
752 static const struct file_operations proc_info_file_operations = {
753 .read = proc_info_read,
756 static int proc_single_show(struct seq_file *m, void *v)
758 struct inode *inode = m->private;
759 struct pid_namespace *ns;
761 struct task_struct *task;
764 ns = inode->i_sb->s_fs_info;
765 pid = proc_pid(inode);
766 task = get_pid_task(pid, PIDTYPE_PID);
770 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
772 put_task_struct(task);
776 static int proc_single_open(struct inode *inode, struct file *filp)
779 ret = single_open(filp, proc_single_show, NULL);
781 struct seq_file *m = filp->private_data;
788 static const struct file_operations proc_single_file_operations = {
789 .open = proc_single_open,
792 .release = single_release,
795 static int mem_open(struct inode* inode, struct file* file)
797 file->private_data = (void*)((long)current->self_exec_id);
801 static ssize_t mem_read(struct file * file, char __user * buf,
802 size_t count, loff_t *ppos)
804 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
806 unsigned long src = *ppos;
808 struct mm_struct *mm;
813 if (check_mem_permission(task))
817 page = (char *)__get_free_page(GFP_TEMPORARY);
823 mm = get_task_mm(task);
829 if (file->private_data != (void*)((long)current->self_exec_id))
835 int this_len, retval;
837 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
838 retval = access_process_vm(task, src, page, this_len, 0);
839 if (!retval || check_mem_permission(task)) {
845 if (copy_to_user(buf, page, retval)) {
860 free_page((unsigned long) page);
862 put_task_struct(task);
867 #define mem_write NULL
870 /* This is a security hazard */
871 static ssize_t mem_write(struct file * file, const char __user *buf,
872 size_t count, loff_t *ppos)
876 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
877 unsigned long dst = *ppos;
883 if (check_mem_permission(task))
887 page = (char *)__get_free_page(GFP_TEMPORARY);
893 int this_len, retval;
895 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
896 if (copy_from_user(page, buf, this_len)) {
900 retval = access_process_vm(task, dst, page, this_len, 1);
912 free_page((unsigned long) page);
914 put_task_struct(task);
920 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
924 file->f_pos = offset;
927 file->f_pos += offset;
932 force_successful_syscall_return();
936 static const struct file_operations proc_mem_operations = {
943 static ssize_t environ_read(struct file *file, char __user *buf,
944 size_t count, loff_t *ppos)
946 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
948 unsigned long src = *ppos;
950 struct mm_struct *mm;
955 if (!ptrace_may_access(task, PTRACE_MODE_READ))
959 page = (char *)__get_free_page(GFP_TEMPORARY);
965 mm = get_task_mm(task);
970 int this_len, retval, max_len;
972 this_len = mm->env_end - (mm->env_start + src);
977 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
978 this_len = (this_len > max_len) ? max_len : this_len;
980 retval = access_process_vm(task, (mm->env_start + src),
988 if (copy_to_user(buf, page, retval)) {
1002 free_page((unsigned long) page);
1004 put_task_struct(task);
1009 static const struct file_operations proc_environ_operations = {
1010 .read = environ_read,
1013 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1014 size_t count, loff_t *ppos)
1016 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1017 char buffer[PROC_NUMBUF];
1023 oom_adjust = task->oomkilladj;
1024 put_task_struct(task);
1026 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1028 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1031 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1032 size_t count, loff_t *ppos)
1034 struct task_struct *task;
1035 char buffer[PROC_NUMBUF], *end;
1038 memset(buffer, 0, sizeof(buffer));
1039 if (count > sizeof(buffer) - 1)
1040 count = sizeof(buffer) - 1;
1041 if (copy_from_user(buffer, buf, count))
1043 oom_adjust = simple_strtol(buffer, &end, 0);
1044 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1045 oom_adjust != OOM_DISABLE)
1049 task = get_proc_task(file->f_path.dentry->d_inode);
1052 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1053 put_task_struct(task);
1056 task->oomkilladj = oom_adjust;
1057 put_task_struct(task);
1058 if (end - buffer == 0)
1060 return end - buffer;
1063 static const struct file_operations proc_oom_adjust_operations = {
1064 .read = oom_adjust_read,
1065 .write = oom_adjust_write,
1068 #ifdef CONFIG_AUDITSYSCALL
1069 #define TMPBUFLEN 21
1070 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1071 size_t count, loff_t *ppos)
1073 struct inode * inode = file->f_path.dentry->d_inode;
1074 struct task_struct *task = get_proc_task(inode);
1076 char tmpbuf[TMPBUFLEN];
1080 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1081 audit_get_loginuid(task));
1082 put_task_struct(task);
1083 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1086 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1087 size_t count, loff_t *ppos)
1089 struct inode * inode = file->f_path.dentry->d_inode;
1094 if (!capable(CAP_AUDIT_CONTROL))
1097 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1100 if (count >= PAGE_SIZE)
1101 count = PAGE_SIZE - 1;
1104 /* No partial writes. */
1107 page = (char*)__get_free_page(GFP_TEMPORARY);
1111 if (copy_from_user(page, buf, count))
1115 loginuid = simple_strtoul(page, &tmp, 10);
1121 length = audit_set_loginuid(current, loginuid);
1122 if (likely(length == 0))
1126 free_page((unsigned long) page);
1130 static const struct file_operations proc_loginuid_operations = {
1131 .read = proc_loginuid_read,
1132 .write = proc_loginuid_write,
1135 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1136 size_t count, loff_t *ppos)
1138 struct inode * inode = file->f_path.dentry->d_inode;
1139 struct task_struct *task = get_proc_task(inode);
1141 char tmpbuf[TMPBUFLEN];
1145 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1146 audit_get_sessionid(task));
1147 put_task_struct(task);
1148 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1151 static const struct file_operations proc_sessionid_operations = {
1152 .read = proc_sessionid_read,
1156 #ifdef CONFIG_FAULT_INJECTION
1157 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1158 size_t count, loff_t *ppos)
1160 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1161 char buffer[PROC_NUMBUF];
1167 make_it_fail = task->make_it_fail;
1168 put_task_struct(task);
1170 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1172 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1175 static ssize_t proc_fault_inject_write(struct file * file,
1176 const char __user * buf, size_t count, loff_t *ppos)
1178 struct task_struct *task;
1179 char buffer[PROC_NUMBUF], *end;
1182 if (!capable(CAP_SYS_RESOURCE))
1184 memset(buffer, 0, sizeof(buffer));
1185 if (count > sizeof(buffer) - 1)
1186 count = sizeof(buffer) - 1;
1187 if (copy_from_user(buffer, buf, count))
1189 make_it_fail = simple_strtol(buffer, &end, 0);
1192 task = get_proc_task(file->f_dentry->d_inode);
1195 task->make_it_fail = make_it_fail;
1196 put_task_struct(task);
1197 if (end - buffer == 0)
1199 return end - buffer;
1202 static const struct file_operations proc_fault_inject_operations = {
1203 .read = proc_fault_inject_read,
1204 .write = proc_fault_inject_write,
1209 #ifdef CONFIG_SCHED_DEBUG
1211 * Print out various scheduling related per-task fields:
1213 static int sched_show(struct seq_file *m, void *v)
1215 struct inode *inode = m->private;
1216 struct task_struct *p;
1218 p = get_proc_task(inode);
1221 proc_sched_show_task(p, m);
1229 sched_write(struct file *file, const char __user *buf,
1230 size_t count, loff_t *offset)
1232 struct inode *inode = file->f_path.dentry->d_inode;
1233 struct task_struct *p;
1235 p = get_proc_task(inode);
1238 proc_sched_set_task(p);
1245 static int sched_open(struct inode *inode, struct file *filp)
1249 ret = single_open(filp, sched_show, NULL);
1251 struct seq_file *m = filp->private_data;
1258 static const struct file_operations proc_pid_sched_operations = {
1261 .write = sched_write,
1262 .llseek = seq_lseek,
1263 .release = single_release,
1269 * We added or removed a vma mapping the executable. The vmas are only mapped
1270 * during exec and are not mapped with the mmap system call.
1271 * Callers must hold down_write() on the mm's mmap_sem for these
1273 void added_exe_file_vma(struct mm_struct *mm)
1275 mm->num_exe_file_vmas++;
1278 void removed_exe_file_vma(struct mm_struct *mm)
1280 mm->num_exe_file_vmas--;
1281 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1283 mm->exe_file = NULL;
1288 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1291 get_file(new_exe_file);
1294 mm->exe_file = new_exe_file;
1295 mm->num_exe_file_vmas = 0;
1298 struct file *get_mm_exe_file(struct mm_struct *mm)
1300 struct file *exe_file;
1302 /* We need mmap_sem to protect against races with removal of
1303 * VM_EXECUTABLE vmas */
1304 down_read(&mm->mmap_sem);
1305 exe_file = mm->exe_file;
1308 up_read(&mm->mmap_sem);
1312 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1314 /* It's safe to write the exe_file pointer without exe_file_lock because
1315 * this is called during fork when the task is not yet in /proc */
1316 newmm->exe_file = get_mm_exe_file(oldmm);
1319 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1321 struct task_struct *task;
1322 struct mm_struct *mm;
1323 struct file *exe_file;
1325 task = get_proc_task(inode);
1328 mm = get_task_mm(task);
1329 put_task_struct(task);
1332 exe_file = get_mm_exe_file(mm);
1335 *exe_path = exe_file->f_path;
1336 path_get(&exe_file->f_path);
1343 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1345 struct inode *inode = dentry->d_inode;
1346 int error = -EACCES;
1348 /* We don't need a base pointer in the /proc filesystem */
1349 path_put(&nd->path);
1351 /* Are we allowed to snoop on the tasks file descriptors? */
1352 if (!proc_fd_access_allowed(inode))
1355 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1356 nd->last_type = LAST_BIND;
1358 return ERR_PTR(error);
1361 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1363 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1370 pathname = d_path(path, tmp, PAGE_SIZE);
1371 len = PTR_ERR(pathname);
1372 if (IS_ERR(pathname))
1374 len = tmp + PAGE_SIZE - 1 - pathname;
1378 if (copy_to_user(buffer, pathname, len))
1381 free_page((unsigned long)tmp);
1385 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1387 int error = -EACCES;
1388 struct inode *inode = dentry->d_inode;
1391 /* Are we allowed to snoop on the tasks file descriptors? */
1392 if (!proc_fd_access_allowed(inode))
1395 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1399 error = do_proc_readlink(&path, buffer, buflen);
1405 static const struct inode_operations proc_pid_link_inode_operations = {
1406 .readlink = proc_pid_readlink,
1407 .follow_link = proc_pid_follow_link,
1408 .setattr = proc_setattr,
1412 /* building an inode */
1414 static int task_dumpable(struct task_struct *task)
1417 struct mm_struct *mm;
1422 dumpable = get_dumpable(mm);
1430 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1432 struct inode * inode;
1433 struct proc_inode *ei;
1434 const struct cred *cred;
1436 /* We need a new inode */
1438 inode = new_inode(sb);
1444 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1445 inode->i_op = &proc_def_inode_operations;
1448 * grab the reference to task.
1450 ei->pid = get_task_pid(task, PIDTYPE_PID);
1454 if (task_dumpable(task)) {
1456 cred = __task_cred(task);
1457 inode->i_uid = cred->euid;
1458 inode->i_gid = cred->egid;
1461 security_task_to_inode(task, inode);
1471 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1473 struct inode *inode = dentry->d_inode;
1474 struct task_struct *task;
1475 const struct cred *cred;
1477 generic_fillattr(inode, stat);
1482 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1484 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1485 task_dumpable(task)) {
1486 cred = __task_cred(task);
1487 stat->uid = cred->euid;
1488 stat->gid = cred->egid;
1498 * Exceptional case: normally we are not allowed to unhash a busy
1499 * directory. In this case, however, we can do it - no aliasing problems
1500 * due to the way we treat inodes.
1502 * Rewrite the inode's ownerships here because the owning task may have
1503 * performed a setuid(), etc.
1505 * Before the /proc/pid/status file was created the only way to read
1506 * the effective uid of a /process was to stat /proc/pid. Reading
1507 * /proc/pid/status is slow enough that procps and other packages
1508 * kept stating /proc/pid. To keep the rules in /proc simple I have
1509 * made this apply to all per process world readable and executable
1512 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1514 struct inode *inode = dentry->d_inode;
1515 struct task_struct *task = get_proc_task(inode);
1516 const struct cred *cred;
1519 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1520 task_dumpable(task)) {
1522 cred = __task_cred(task);
1523 inode->i_uid = cred->euid;
1524 inode->i_gid = cred->egid;
1530 inode->i_mode &= ~(S_ISUID | S_ISGID);
1531 security_task_to_inode(task, inode);
1532 put_task_struct(task);
1539 static int pid_delete_dentry(struct dentry * dentry)
1541 /* Is the task we represent dead?
1542 * If so, then don't put the dentry on the lru list,
1543 * kill it immediately.
1545 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1548 static struct dentry_operations pid_dentry_operations =
1550 .d_revalidate = pid_revalidate,
1551 .d_delete = pid_delete_dentry,
1556 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1557 struct task_struct *, const void *);
1560 * Fill a directory entry.
1562 * If possible create the dcache entry and derive our inode number and
1563 * file type from dcache entry.
1565 * Since all of the proc inode numbers are dynamically generated, the inode
1566 * numbers do not exist until the inode is cache. This means creating the
1567 * the dcache entry in readdir is necessary to keep the inode numbers
1568 * reported by readdir in sync with the inode numbers reported
1571 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1572 char *name, int len,
1573 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1575 struct dentry *child, *dir = filp->f_path.dentry;
1576 struct inode *inode;
1579 unsigned type = DT_UNKNOWN;
1583 qname.hash = full_name_hash(name, len);
1585 child = d_lookup(dir, &qname);
1588 new = d_alloc(dir, &qname);
1590 child = instantiate(dir->d_inode, new, task, ptr);
1597 if (!child || IS_ERR(child) || !child->d_inode)
1598 goto end_instantiate;
1599 inode = child->d_inode;
1602 type = inode->i_mode >> 12;
1607 ino = find_inode_number(dir, &qname);
1610 return filldir(dirent, name, len, filp->f_pos, ino, type);
1613 static unsigned name_to_int(struct dentry *dentry)
1615 const char *name = dentry->d_name.name;
1616 int len = dentry->d_name.len;
1619 if (len > 1 && *name == '0')
1622 unsigned c = *name++ - '0';
1625 if (n >= (~0U-9)/10)
1635 #define PROC_FDINFO_MAX 64
1637 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1639 struct task_struct *task = get_proc_task(inode);
1640 struct files_struct *files = NULL;
1642 int fd = proc_fd(inode);
1645 files = get_files_struct(task);
1646 put_task_struct(task);
1650 * We are not taking a ref to the file structure, so we must
1653 spin_lock(&files->file_lock);
1654 file = fcheck_files(files, fd);
1657 *path = file->f_path;
1658 path_get(&file->f_path);
1661 snprintf(info, PROC_FDINFO_MAX,
1664 (long long) file->f_pos,
1666 spin_unlock(&files->file_lock);
1667 put_files_struct(files);
1670 spin_unlock(&files->file_lock);
1671 put_files_struct(files);
1676 static int proc_fd_link(struct inode *inode, struct path *path)
1678 return proc_fd_info(inode, path, NULL);
1681 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1683 struct inode *inode = dentry->d_inode;
1684 struct task_struct *task = get_proc_task(inode);
1685 int fd = proc_fd(inode);
1686 struct files_struct *files;
1687 const struct cred *cred;
1690 files = get_files_struct(task);
1693 if (fcheck_files(files, fd)) {
1695 put_files_struct(files);
1696 if (task_dumpable(task)) {
1698 cred = __task_cred(task);
1699 inode->i_uid = cred->euid;
1700 inode->i_gid = cred->egid;
1706 inode->i_mode &= ~(S_ISUID | S_ISGID);
1707 security_task_to_inode(task, inode);
1708 put_task_struct(task);
1712 put_files_struct(files);
1714 put_task_struct(task);
1720 static struct dentry_operations tid_fd_dentry_operations =
1722 .d_revalidate = tid_fd_revalidate,
1723 .d_delete = pid_delete_dentry,
1726 static struct dentry *proc_fd_instantiate(struct inode *dir,
1727 struct dentry *dentry, struct task_struct *task, const void *ptr)
1729 unsigned fd = *(const unsigned *)ptr;
1731 struct files_struct *files;
1732 struct inode *inode;
1733 struct proc_inode *ei;
1734 struct dentry *error = ERR_PTR(-ENOENT);
1736 inode = proc_pid_make_inode(dir->i_sb, task);
1741 files = get_files_struct(task);
1744 inode->i_mode = S_IFLNK;
1747 * We are not taking a ref to the file structure, so we must
1750 spin_lock(&files->file_lock);
1751 file = fcheck_files(files, fd);
1754 if (file->f_mode & FMODE_READ)
1755 inode->i_mode |= S_IRUSR | S_IXUSR;
1756 if (file->f_mode & FMODE_WRITE)
1757 inode->i_mode |= S_IWUSR | S_IXUSR;
1758 spin_unlock(&files->file_lock);
1759 put_files_struct(files);
1761 inode->i_op = &proc_pid_link_inode_operations;
1763 ei->op.proc_get_link = proc_fd_link;
1764 dentry->d_op = &tid_fd_dentry_operations;
1765 d_add(dentry, inode);
1766 /* Close the race of the process dying before we return the dentry */
1767 if (tid_fd_revalidate(dentry, NULL))
1773 spin_unlock(&files->file_lock);
1774 put_files_struct(files);
1780 static struct dentry *proc_lookupfd_common(struct inode *dir,
1781 struct dentry *dentry,
1782 instantiate_t instantiate)
1784 struct task_struct *task = get_proc_task(dir);
1785 unsigned fd = name_to_int(dentry);
1786 struct dentry *result = ERR_PTR(-ENOENT);
1793 result = instantiate(dir, dentry, task, &fd);
1795 put_task_struct(task);
1800 static int proc_readfd_common(struct file * filp, void * dirent,
1801 filldir_t filldir, instantiate_t instantiate)
1803 struct dentry *dentry = filp->f_path.dentry;
1804 struct inode *inode = dentry->d_inode;
1805 struct task_struct *p = get_proc_task(inode);
1806 unsigned int fd, ino;
1808 struct files_struct * files;
1818 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1822 ino = parent_ino(dentry);
1823 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1827 files = get_files_struct(p);
1831 for (fd = filp->f_pos-2;
1832 fd < files_fdtable(files)->max_fds;
1833 fd++, filp->f_pos++) {
1834 char name[PROC_NUMBUF];
1837 if (!fcheck_files(files, fd))
1841 len = snprintf(name, sizeof(name), "%d", fd);
1842 if (proc_fill_cache(filp, dirent, filldir,
1843 name, len, instantiate,
1851 put_files_struct(files);
1859 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1860 struct nameidata *nd)
1862 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1865 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1867 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1870 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1871 size_t len, loff_t *ppos)
1873 char tmp[PROC_FDINFO_MAX];
1874 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1876 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1880 static const struct file_operations proc_fdinfo_file_operations = {
1881 .open = nonseekable_open,
1882 .read = proc_fdinfo_read,
1885 static const struct file_operations proc_fd_operations = {
1886 .read = generic_read_dir,
1887 .readdir = proc_readfd,
1891 * /proc/pid/fd needs a special permission handler so that a process can still
1892 * access /proc/self/fd after it has executed a setuid().
1894 static int proc_fd_permission(struct inode *inode, int mask)
1898 rv = generic_permission(inode, mask, NULL);
1901 if (task_pid(current) == proc_pid(inode))
1907 * proc directories can do almost nothing..
1909 static const struct inode_operations proc_fd_inode_operations = {
1910 .lookup = proc_lookupfd,
1911 .permission = proc_fd_permission,
1912 .setattr = proc_setattr,
1915 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1916 struct dentry *dentry, struct task_struct *task, const void *ptr)
1918 unsigned fd = *(unsigned *)ptr;
1919 struct inode *inode;
1920 struct proc_inode *ei;
1921 struct dentry *error = ERR_PTR(-ENOENT);
1923 inode = proc_pid_make_inode(dir->i_sb, task);
1928 inode->i_mode = S_IFREG | S_IRUSR;
1929 inode->i_fop = &proc_fdinfo_file_operations;
1930 dentry->d_op = &tid_fd_dentry_operations;
1931 d_add(dentry, inode);
1932 /* Close the race of the process dying before we return the dentry */
1933 if (tid_fd_revalidate(dentry, NULL))
1940 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1941 struct dentry *dentry,
1942 struct nameidata *nd)
1944 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1947 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1949 return proc_readfd_common(filp, dirent, filldir,
1950 proc_fdinfo_instantiate);
1953 static const struct file_operations proc_fdinfo_operations = {
1954 .read = generic_read_dir,
1955 .readdir = proc_readfdinfo,
1959 * proc directories can do almost nothing..
1961 static const struct inode_operations proc_fdinfo_inode_operations = {
1962 .lookup = proc_lookupfdinfo,
1963 .setattr = proc_setattr,
1967 static struct dentry *proc_pident_instantiate(struct inode *dir,
1968 struct dentry *dentry, struct task_struct *task, const void *ptr)
1970 const struct pid_entry *p = ptr;
1971 struct inode *inode;
1972 struct proc_inode *ei;
1973 struct dentry *error = ERR_PTR(-EINVAL);
1975 inode = proc_pid_make_inode(dir->i_sb, task);
1980 inode->i_mode = p->mode;
1981 if (S_ISDIR(inode->i_mode))
1982 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1984 inode->i_op = p->iop;
1986 inode->i_fop = p->fop;
1988 dentry->d_op = &pid_dentry_operations;
1989 d_add(dentry, inode);
1990 /* Close the race of the process dying before we return the dentry */
1991 if (pid_revalidate(dentry, NULL))
1997 static struct dentry *proc_pident_lookup(struct inode *dir,
1998 struct dentry *dentry,
1999 const struct pid_entry *ents,
2002 struct dentry *error;
2003 struct task_struct *task = get_proc_task(dir);
2004 const struct pid_entry *p, *last;
2006 error = ERR_PTR(-ENOENT);
2012 * Yes, it does not scale. And it should not. Don't add
2013 * new entries into /proc/<tgid>/ without very good reasons.
2015 last = &ents[nents - 1];
2016 for (p = ents; p <= last; p++) {
2017 if (p->len != dentry->d_name.len)
2019 if (!memcmp(dentry->d_name.name, p->name, p->len))
2025 error = proc_pident_instantiate(dir, dentry, task, p);
2027 put_task_struct(task);
2032 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2033 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2035 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2036 proc_pident_instantiate, task, p);
2039 static int proc_pident_readdir(struct file *filp,
2040 void *dirent, filldir_t filldir,
2041 const struct pid_entry *ents, unsigned int nents)
2044 struct dentry *dentry = filp->f_path.dentry;
2045 struct inode *inode = dentry->d_inode;
2046 struct task_struct *task = get_proc_task(inode);
2047 const struct pid_entry *p, *last;
2060 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2066 ino = parent_ino(dentry);
2067 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2079 last = &ents[nents - 1];
2081 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2090 put_task_struct(task);
2095 #ifdef CONFIG_SECURITY
2096 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2097 size_t count, loff_t *ppos)
2099 struct inode * inode = file->f_path.dentry->d_inode;
2102 struct task_struct *task = get_proc_task(inode);
2107 length = security_getprocattr(task,
2108 (char*)file->f_path.dentry->d_name.name,
2110 put_task_struct(task);
2112 length = simple_read_from_buffer(buf, count, ppos, p, length);
2117 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2118 size_t count, loff_t *ppos)
2120 struct inode * inode = file->f_path.dentry->d_inode;
2123 struct task_struct *task = get_proc_task(inode);
2128 if (count > PAGE_SIZE)
2131 /* No partial writes. */
2137 page = (char*)__get_free_page(GFP_TEMPORARY);
2142 if (copy_from_user(page, buf, count))
2145 length = security_setprocattr(task,
2146 (char*)file->f_path.dentry->d_name.name,
2147 (void*)page, count);
2149 free_page((unsigned long) page);
2151 put_task_struct(task);
2156 static const struct file_operations proc_pid_attr_operations = {
2157 .read = proc_pid_attr_read,
2158 .write = proc_pid_attr_write,
2161 static const struct pid_entry attr_dir_stuff[] = {
2162 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2163 REG("prev", S_IRUGO, proc_pid_attr_operations),
2164 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2165 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2166 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2167 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2170 static int proc_attr_dir_readdir(struct file * filp,
2171 void * dirent, filldir_t filldir)
2173 return proc_pident_readdir(filp,dirent,filldir,
2174 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2177 static const struct file_operations proc_attr_dir_operations = {
2178 .read = generic_read_dir,
2179 .readdir = proc_attr_dir_readdir,
2182 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2183 struct dentry *dentry, struct nameidata *nd)
2185 return proc_pident_lookup(dir, dentry,
2186 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2189 static const struct inode_operations proc_attr_dir_inode_operations = {
2190 .lookup = proc_attr_dir_lookup,
2191 .getattr = pid_getattr,
2192 .setattr = proc_setattr,
2197 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2198 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2199 size_t count, loff_t *ppos)
2201 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2202 struct mm_struct *mm;
2203 char buffer[PROC_NUMBUF];
2211 mm = get_task_mm(task);
2213 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2214 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2215 MMF_DUMP_FILTER_SHIFT));
2217 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2220 put_task_struct(task);
2225 static ssize_t proc_coredump_filter_write(struct file *file,
2226 const char __user *buf,
2230 struct task_struct *task;
2231 struct mm_struct *mm;
2232 char buffer[PROC_NUMBUF], *end;
2239 memset(buffer, 0, sizeof(buffer));
2240 if (count > sizeof(buffer) - 1)
2241 count = sizeof(buffer) - 1;
2242 if (copy_from_user(buffer, buf, count))
2246 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2249 if (end - buffer == 0)
2253 task = get_proc_task(file->f_dentry->d_inode);
2258 mm = get_task_mm(task);
2262 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2264 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2266 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2271 put_task_struct(task);
2276 static const struct file_operations proc_coredump_filter_operations = {
2277 .read = proc_coredump_filter_read,
2278 .write = proc_coredump_filter_write,
2285 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2288 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2289 pid_t tgid = task_tgid_nr_ns(current, ns);
2290 char tmp[PROC_NUMBUF];
2293 sprintf(tmp, "%d", tgid);
2294 return vfs_readlink(dentry,buffer,buflen,tmp);
2297 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2299 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2300 pid_t tgid = task_tgid_nr_ns(current, ns);
2301 char tmp[PROC_NUMBUF];
2303 return ERR_PTR(-ENOENT);
2304 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2305 return ERR_PTR(vfs_follow_link(nd,tmp));
2308 static const struct inode_operations proc_self_inode_operations = {
2309 .readlink = proc_self_readlink,
2310 .follow_link = proc_self_follow_link,
2316 * These are the directory entries in the root directory of /proc
2317 * that properly belong to the /proc filesystem, as they describe
2318 * describe something that is process related.
2320 static const struct pid_entry proc_base_stuff[] = {
2321 NOD("self", S_IFLNK|S_IRWXUGO,
2322 &proc_self_inode_operations, NULL, {}),
2326 * Exceptional case: normally we are not allowed to unhash a busy
2327 * directory. In this case, however, we can do it - no aliasing problems
2328 * due to the way we treat inodes.
2330 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2332 struct inode *inode = dentry->d_inode;
2333 struct task_struct *task = get_proc_task(inode);
2335 put_task_struct(task);
2342 static struct dentry_operations proc_base_dentry_operations =
2344 .d_revalidate = proc_base_revalidate,
2345 .d_delete = pid_delete_dentry,
2348 static struct dentry *proc_base_instantiate(struct inode *dir,
2349 struct dentry *dentry, struct task_struct *task, const void *ptr)
2351 const struct pid_entry *p = ptr;
2352 struct inode *inode;
2353 struct proc_inode *ei;
2354 struct dentry *error = ERR_PTR(-EINVAL);
2356 /* Allocate the inode */
2357 error = ERR_PTR(-ENOMEM);
2358 inode = new_inode(dir->i_sb);
2362 /* Initialize the inode */
2364 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2367 * grab the reference to the task.
2369 ei->pid = get_task_pid(task, PIDTYPE_PID);
2373 inode->i_mode = p->mode;
2374 if (S_ISDIR(inode->i_mode))
2376 if (S_ISLNK(inode->i_mode))
2379 inode->i_op = p->iop;
2381 inode->i_fop = p->fop;
2383 dentry->d_op = &proc_base_dentry_operations;
2384 d_add(dentry, inode);
2393 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2395 struct dentry *error;
2396 struct task_struct *task = get_proc_task(dir);
2397 const struct pid_entry *p, *last;
2399 error = ERR_PTR(-ENOENT);
2404 /* Lookup the directory entry */
2405 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2406 for (p = proc_base_stuff; p <= last; p++) {
2407 if (p->len != dentry->d_name.len)
2409 if (!memcmp(dentry->d_name.name, p->name, p->len))
2415 error = proc_base_instantiate(dir, dentry, task, p);
2418 put_task_struct(task);
2423 static int proc_base_fill_cache(struct file *filp, void *dirent,
2424 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2426 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2427 proc_base_instantiate, task, p);
2430 #ifdef CONFIG_TASK_IO_ACCOUNTING
2431 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2433 struct task_io_accounting acct = task->ioac;
2434 unsigned long flags;
2436 if (whole && lock_task_sighand(task, &flags)) {
2437 struct task_struct *t = task;
2439 task_io_accounting_add(&acct, &task->signal->ioac);
2440 while_each_thread(task, t)
2441 task_io_accounting_add(&acct, &t->ioac);
2443 unlock_task_sighand(task, &flags);
2445 return sprintf(buffer,
2450 "read_bytes: %llu\n"
2451 "write_bytes: %llu\n"
2452 "cancelled_write_bytes: %llu\n",
2453 (unsigned long long)acct.rchar,
2454 (unsigned long long)acct.wchar,
2455 (unsigned long long)acct.syscr,
2456 (unsigned long long)acct.syscw,
2457 (unsigned long long)acct.read_bytes,
2458 (unsigned long long)acct.write_bytes,
2459 (unsigned long long)acct.cancelled_write_bytes);
2462 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2464 return do_io_accounting(task, buffer, 0);
2467 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2469 return do_io_accounting(task, buffer, 1);
2471 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2473 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2474 struct pid *pid, struct task_struct *task)
2476 seq_printf(m, "%08x\n", task->personality);
2483 static const struct file_operations proc_task_operations;
2484 static const struct inode_operations proc_task_inode_operations;
2486 static const struct pid_entry tgid_base_stuff[] = {
2487 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2488 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2489 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2491 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2493 REG("environ", S_IRUSR, proc_environ_operations),
2494 INF("auxv", S_IRUSR, proc_pid_auxv),
2495 ONE("status", S_IRUGO, proc_pid_status),
2496 ONE("personality", S_IRUSR, proc_pid_personality),
2497 INF("limits", S_IRUSR, proc_pid_limits),
2498 #ifdef CONFIG_SCHED_DEBUG
2499 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2501 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2502 INF("syscall", S_IRUSR, proc_pid_syscall),
2504 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2505 ONE("stat", S_IRUGO, proc_tgid_stat),
2506 ONE("statm", S_IRUGO, proc_pid_statm),
2507 REG("maps", S_IRUGO, proc_maps_operations),
2509 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2511 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2512 LNK("cwd", proc_cwd_link),
2513 LNK("root", proc_root_link),
2514 LNK("exe", proc_exe_link),
2515 REG("mounts", S_IRUGO, proc_mounts_operations),
2516 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2517 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2518 #ifdef CONFIG_PROC_PAGE_MONITOR
2519 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2520 REG("smaps", S_IRUGO, proc_smaps_operations),
2521 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2523 #ifdef CONFIG_SECURITY
2524 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2526 #ifdef CONFIG_KALLSYMS
2527 INF("wchan", S_IRUGO, proc_pid_wchan),
2529 #ifdef CONFIG_STACKTRACE
2530 ONE("stack", S_IRUSR, proc_pid_stack),
2532 #ifdef CONFIG_SCHEDSTATS
2533 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2535 #ifdef CONFIG_LATENCYTOP
2536 REG("latency", S_IRUGO, proc_lstats_operations),
2538 #ifdef CONFIG_PROC_PID_CPUSET
2539 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2541 #ifdef CONFIG_CGROUPS
2542 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2544 INF("oom_score", S_IRUGO, proc_oom_score),
2545 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2546 #ifdef CONFIG_AUDITSYSCALL
2547 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2548 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2550 #ifdef CONFIG_FAULT_INJECTION
2551 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2553 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2554 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2556 #ifdef CONFIG_TASK_IO_ACCOUNTING
2557 INF("io", S_IRUGO, proc_tgid_io_accounting),
2561 static int proc_tgid_base_readdir(struct file * filp,
2562 void * dirent, filldir_t filldir)
2564 return proc_pident_readdir(filp,dirent,filldir,
2565 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2568 static const struct file_operations proc_tgid_base_operations = {
2569 .read = generic_read_dir,
2570 .readdir = proc_tgid_base_readdir,
2573 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2574 return proc_pident_lookup(dir, dentry,
2575 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2578 static const struct inode_operations proc_tgid_base_inode_operations = {
2579 .lookup = proc_tgid_base_lookup,
2580 .getattr = pid_getattr,
2581 .setattr = proc_setattr,
2584 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2586 struct dentry *dentry, *leader, *dir;
2587 char buf[PROC_NUMBUF];
2591 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2592 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2594 if (!(current->flags & PF_EXITING))
2595 shrink_dcache_parent(dentry);
2604 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2605 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2610 name.len = strlen(name.name);
2611 dir = d_hash_and_lookup(leader, &name);
2613 goto out_put_leader;
2616 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2617 dentry = d_hash_and_lookup(dir, &name);
2619 shrink_dcache_parent(dentry);
2632 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2633 * @task: task that should be flushed.
2635 * When flushing dentries from proc, one needs to flush them from global
2636 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2637 * in. This call is supposed to do all of this job.
2639 * Looks in the dcache for
2641 * /proc/@tgid/task/@pid
2642 * if either directory is present flushes it and all of it'ts children
2645 * It is safe and reasonable to cache /proc entries for a task until
2646 * that task exits. After that they just clog up the dcache with
2647 * useless entries, possibly causing useful dcache entries to be
2648 * flushed instead. This routine is proved to flush those useless
2649 * dcache entries at process exit time.
2651 * NOTE: This routine is just an optimization so it does not guarantee
2652 * that no dcache entries will exist at process exit time it
2653 * just makes it very unlikely that any will persist.
2656 void proc_flush_task(struct task_struct *task)
2659 struct pid *pid, *tgid = NULL;
2662 pid = task_pid(task);
2663 if (thread_group_leader(task))
2664 tgid = task_tgid(task);
2666 for (i = 0; i <= pid->level; i++) {
2667 upid = &pid->numbers[i];
2668 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2669 tgid ? tgid->numbers[i].nr : 0);
2672 upid = &pid->numbers[pid->level];
2674 pid_ns_release_proc(upid->ns);
2677 static struct dentry *proc_pid_instantiate(struct inode *dir,
2678 struct dentry * dentry,
2679 struct task_struct *task, const void *ptr)
2681 struct dentry *error = ERR_PTR(-ENOENT);
2682 struct inode *inode;
2684 inode = proc_pid_make_inode(dir->i_sb, task);
2688 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2689 inode->i_op = &proc_tgid_base_inode_operations;
2690 inode->i_fop = &proc_tgid_base_operations;
2691 inode->i_flags|=S_IMMUTABLE;
2693 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2694 ARRAY_SIZE(tgid_base_stuff));
2696 dentry->d_op = &pid_dentry_operations;
2698 d_add(dentry, inode);
2699 /* Close the race of the process dying before we return the dentry */
2700 if (pid_revalidate(dentry, NULL))
2706 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2708 struct dentry *result = ERR_PTR(-ENOENT);
2709 struct task_struct *task;
2711 struct pid_namespace *ns;
2713 result = proc_base_lookup(dir, dentry);
2714 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2717 tgid = name_to_int(dentry);
2721 ns = dentry->d_sb->s_fs_info;
2723 task = find_task_by_pid_ns(tgid, ns);
2725 get_task_struct(task);
2730 result = proc_pid_instantiate(dir, dentry, task, NULL);
2731 put_task_struct(task);
2737 * Find the first task with tgid >= tgid
2742 struct task_struct *task;
2744 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2749 put_task_struct(iter.task);
2753 pid = find_ge_pid(iter.tgid, ns);
2755 iter.tgid = pid_nr_ns(pid, ns);
2756 iter.task = pid_task(pid, PIDTYPE_PID);
2757 /* What we to know is if the pid we have find is the
2758 * pid of a thread_group_leader. Testing for task
2759 * being a thread_group_leader is the obvious thing
2760 * todo but there is a window when it fails, due to
2761 * the pid transfer logic in de_thread.
2763 * So we perform the straight forward test of seeing
2764 * if the pid we have found is the pid of a thread
2765 * group leader, and don't worry if the task we have
2766 * found doesn't happen to be a thread group leader.
2767 * As we don't care in the case of readdir.
2769 if (!iter.task || !has_group_leader_pid(iter.task)) {
2773 get_task_struct(iter.task);
2779 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2781 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2782 struct tgid_iter iter)
2784 char name[PROC_NUMBUF];
2785 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2786 return proc_fill_cache(filp, dirent, filldir, name, len,
2787 proc_pid_instantiate, iter.task, NULL);
2790 /* for the /proc/ directory itself, after non-process stuff has been done */
2791 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2793 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2794 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2795 struct tgid_iter iter;
2796 struct pid_namespace *ns;
2801 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2802 const struct pid_entry *p = &proc_base_stuff[nr];
2803 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2807 ns = filp->f_dentry->d_sb->s_fs_info;
2809 iter.tgid = filp->f_pos - TGID_OFFSET;
2810 for (iter = next_tgid(ns, iter);
2812 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2813 filp->f_pos = iter.tgid + TGID_OFFSET;
2814 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2815 put_task_struct(iter.task);
2819 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2821 put_task_struct(reaper);
2829 static const struct pid_entry tid_base_stuff[] = {
2830 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2831 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2832 REG("environ", S_IRUSR, proc_environ_operations),
2833 INF("auxv", S_IRUSR, proc_pid_auxv),
2834 ONE("status", S_IRUGO, proc_pid_status),
2835 ONE("personality", S_IRUSR, proc_pid_personality),
2836 INF("limits", S_IRUSR, proc_pid_limits),
2837 #ifdef CONFIG_SCHED_DEBUG
2838 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2840 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2841 INF("syscall", S_IRUSR, proc_pid_syscall),
2843 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2844 ONE("stat", S_IRUGO, proc_tid_stat),
2845 ONE("statm", S_IRUGO, proc_pid_statm),
2846 REG("maps", S_IRUGO, proc_maps_operations),
2848 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2850 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2851 LNK("cwd", proc_cwd_link),
2852 LNK("root", proc_root_link),
2853 LNK("exe", proc_exe_link),
2854 REG("mounts", S_IRUGO, proc_mounts_operations),
2855 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2856 #ifdef CONFIG_PROC_PAGE_MONITOR
2857 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2858 REG("smaps", S_IRUGO, proc_smaps_operations),
2859 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2861 #ifdef CONFIG_SECURITY
2862 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2864 #ifdef CONFIG_KALLSYMS
2865 INF("wchan", S_IRUGO, proc_pid_wchan),
2867 #ifdef CONFIG_STACKTRACE
2868 ONE("stack", S_IRUSR, proc_pid_stack),
2870 #ifdef CONFIG_SCHEDSTATS
2871 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2873 #ifdef CONFIG_LATENCYTOP
2874 REG("latency", S_IRUGO, proc_lstats_operations),
2876 #ifdef CONFIG_PROC_PID_CPUSET
2877 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2879 #ifdef CONFIG_CGROUPS
2880 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2882 INF("oom_score", S_IRUGO, proc_oom_score),
2883 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2884 #ifdef CONFIG_AUDITSYSCALL
2885 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2886 REG("sessionid", S_IRUSR, proc_sessionid_operations),
2888 #ifdef CONFIG_FAULT_INJECTION
2889 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2891 #ifdef CONFIG_TASK_IO_ACCOUNTING
2892 INF("io", S_IRUGO, proc_tid_io_accounting),
2896 static int proc_tid_base_readdir(struct file * filp,
2897 void * dirent, filldir_t filldir)
2899 return proc_pident_readdir(filp,dirent,filldir,
2900 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2903 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2904 return proc_pident_lookup(dir, dentry,
2905 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2908 static const struct file_operations proc_tid_base_operations = {
2909 .read = generic_read_dir,
2910 .readdir = proc_tid_base_readdir,
2913 static const struct inode_operations proc_tid_base_inode_operations = {
2914 .lookup = proc_tid_base_lookup,
2915 .getattr = pid_getattr,
2916 .setattr = proc_setattr,
2919 static struct dentry *proc_task_instantiate(struct inode *dir,
2920 struct dentry *dentry, struct task_struct *task, const void *ptr)
2922 struct dentry *error = ERR_PTR(-ENOENT);
2923 struct inode *inode;
2924 inode = proc_pid_make_inode(dir->i_sb, task);
2928 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2929 inode->i_op = &proc_tid_base_inode_operations;
2930 inode->i_fop = &proc_tid_base_operations;
2931 inode->i_flags|=S_IMMUTABLE;
2933 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2934 ARRAY_SIZE(tid_base_stuff));
2936 dentry->d_op = &pid_dentry_operations;
2938 d_add(dentry, inode);
2939 /* Close the race of the process dying before we return the dentry */
2940 if (pid_revalidate(dentry, NULL))
2946 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2948 struct dentry *result = ERR_PTR(-ENOENT);
2949 struct task_struct *task;
2950 struct task_struct *leader = get_proc_task(dir);
2952 struct pid_namespace *ns;
2957 tid = name_to_int(dentry);
2961 ns = dentry->d_sb->s_fs_info;
2963 task = find_task_by_pid_ns(tid, ns);
2965 get_task_struct(task);
2969 if (!same_thread_group(leader, task))
2972 result = proc_task_instantiate(dir, dentry, task, NULL);
2974 put_task_struct(task);
2976 put_task_struct(leader);
2982 * Find the first tid of a thread group to return to user space.
2984 * Usually this is just the thread group leader, but if the users
2985 * buffer was too small or there was a seek into the middle of the
2986 * directory we have more work todo.
2988 * In the case of a short read we start with find_task_by_pid.
2990 * In the case of a seek we start with the leader and walk nr
2993 static struct task_struct *first_tid(struct task_struct *leader,
2994 int tid, int nr, struct pid_namespace *ns)
2996 struct task_struct *pos;
2999 /* Attempt to start with the pid of a thread */
3000 if (tid && (nr > 0)) {
3001 pos = find_task_by_pid_ns(tid, ns);
3002 if (pos && (pos->group_leader == leader))
3006 /* If nr exceeds the number of threads there is nothing todo */
3008 if (nr && nr >= get_nr_threads(leader))
3011 /* If we haven't found our starting place yet start
3012 * with the leader and walk nr threads forward.
3014 for (pos = leader; nr > 0; --nr) {
3015 pos = next_thread(pos);
3016 if (pos == leader) {
3022 get_task_struct(pos);
3029 * Find the next thread in the thread list.
3030 * Return NULL if there is an error or no next thread.
3032 * The reference to the input task_struct is released.
3034 static struct task_struct *next_tid(struct task_struct *start)
3036 struct task_struct *pos = NULL;
3038 if (pid_alive(start)) {
3039 pos = next_thread(start);
3040 if (thread_group_leader(pos))
3043 get_task_struct(pos);
3046 put_task_struct(start);
3050 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3051 struct task_struct *task, int tid)
3053 char name[PROC_NUMBUF];
3054 int len = snprintf(name, sizeof(name), "%d", tid);
3055 return proc_fill_cache(filp, dirent, filldir, name, len,
3056 proc_task_instantiate, task, NULL);
3059 /* for the /proc/TGID/task/ directories */
3060 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3062 struct dentry *dentry = filp->f_path.dentry;
3063 struct inode *inode = dentry->d_inode;
3064 struct task_struct *leader = NULL;
3065 struct task_struct *task;
3066 int retval = -ENOENT;
3069 struct pid_namespace *ns;
3071 task = get_proc_task(inode);
3075 if (pid_alive(task)) {
3076 leader = task->group_leader;
3077 get_task_struct(leader);
3080 put_task_struct(task);
3085 switch ((unsigned long)filp->f_pos) {
3088 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3093 ino = parent_ino(dentry);
3094 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3100 /* f_version caches the tgid value that the last readdir call couldn't
3101 * return. lseek aka telldir automagically resets f_version to 0.
3103 ns = filp->f_dentry->d_sb->s_fs_info;
3104 tid = (int)filp->f_version;
3105 filp->f_version = 0;
3106 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3108 task = next_tid(task), filp->f_pos++) {
3109 tid = task_pid_nr_ns(task, ns);
3110 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3111 /* returning this tgid failed, save it as the first
3112 * pid for the next readir call */
3113 filp->f_version = (u64)tid;
3114 put_task_struct(task);
3119 put_task_struct(leader);
3124 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3126 struct inode *inode = dentry->d_inode;
3127 struct task_struct *p = get_proc_task(inode);
3128 generic_fillattr(inode, stat);
3131 stat->nlink += get_nr_threads(p);
3138 static const struct inode_operations proc_task_inode_operations = {
3139 .lookup = proc_task_lookup,
3140 .getattr = proc_task_getattr,
3141 .setattr = proc_setattr,
3144 static const struct file_operations proc_task_operations = {
3145 .read = generic_read_dir,
3146 .readdir = proc_task_readdir,