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);
1456 if (task_dumpable(task)) {
1458 cred = __task_cred(task);
1459 inode->i_uid = cred->euid;
1460 inode->i_gid = cred->egid;
1463 security_task_to_inode(task, inode);
1473 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1475 struct inode *inode = dentry->d_inode;
1476 struct task_struct *task;
1477 const struct cred *cred;
1479 generic_fillattr(inode, stat);
1484 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1486 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1487 task_dumpable(task)) {
1488 cred = __task_cred(task);
1489 stat->uid = cred->euid;
1490 stat->gid = cred->egid;
1500 * Exceptional case: normally we are not allowed to unhash a busy
1501 * directory. In this case, however, we can do it - no aliasing problems
1502 * due to the way we treat inodes.
1504 * Rewrite the inode's ownerships here because the owning task may have
1505 * performed a setuid(), etc.
1507 * Before the /proc/pid/status file was created the only way to read
1508 * the effective uid of a /process was to stat /proc/pid. Reading
1509 * /proc/pid/status is slow enough that procps and other packages
1510 * kept stating /proc/pid. To keep the rules in /proc simple I have
1511 * made this apply to all per process world readable and executable
1514 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1516 struct inode *inode = dentry->d_inode;
1517 struct task_struct *task = get_proc_task(inode);
1518 const struct cred *cred;
1521 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1522 task_dumpable(task)) {
1524 cred = __task_cred(task);
1525 inode->i_uid = cred->euid;
1526 inode->i_gid = cred->egid;
1532 inode->i_mode &= ~(S_ISUID | S_ISGID);
1533 security_task_to_inode(task, inode);
1534 put_task_struct(task);
1541 static int pid_delete_dentry(struct dentry * dentry)
1543 /* Is the task we represent dead?
1544 * If so, then don't put the dentry on the lru list,
1545 * kill it immediately.
1547 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1550 static struct dentry_operations pid_dentry_operations =
1552 .d_revalidate = pid_revalidate,
1553 .d_delete = pid_delete_dentry,
1558 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1559 struct task_struct *, const void *);
1562 * Fill a directory entry.
1564 * If possible create the dcache entry and derive our inode number and
1565 * file type from dcache entry.
1567 * Since all of the proc inode numbers are dynamically generated, the inode
1568 * numbers do not exist until the inode is cache. This means creating the
1569 * the dcache entry in readdir is necessary to keep the inode numbers
1570 * reported by readdir in sync with the inode numbers reported
1573 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1574 char *name, int len,
1575 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1577 struct dentry *child, *dir = filp->f_path.dentry;
1578 struct inode *inode;
1581 unsigned type = DT_UNKNOWN;
1585 qname.hash = full_name_hash(name, len);
1587 child = d_lookup(dir, &qname);
1590 new = d_alloc(dir, &qname);
1592 child = instantiate(dir->d_inode, new, task, ptr);
1599 if (!child || IS_ERR(child) || !child->d_inode)
1600 goto end_instantiate;
1601 inode = child->d_inode;
1604 type = inode->i_mode >> 12;
1609 ino = find_inode_number(dir, &qname);
1612 return filldir(dirent, name, len, filp->f_pos, ino, type);
1615 static unsigned name_to_int(struct dentry *dentry)
1617 const char *name = dentry->d_name.name;
1618 int len = dentry->d_name.len;
1621 if (len > 1 && *name == '0')
1624 unsigned c = *name++ - '0';
1627 if (n >= (~0U-9)/10)
1637 #define PROC_FDINFO_MAX 64
1639 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1641 struct task_struct *task = get_proc_task(inode);
1642 struct files_struct *files = NULL;
1644 int fd = proc_fd(inode);
1647 files = get_files_struct(task);
1648 put_task_struct(task);
1652 * We are not taking a ref to the file structure, so we must
1655 spin_lock(&files->file_lock);
1656 file = fcheck_files(files, fd);
1659 *path = file->f_path;
1660 path_get(&file->f_path);
1663 snprintf(info, PROC_FDINFO_MAX,
1666 (long long) file->f_pos,
1668 spin_unlock(&files->file_lock);
1669 put_files_struct(files);
1672 spin_unlock(&files->file_lock);
1673 put_files_struct(files);
1678 static int proc_fd_link(struct inode *inode, struct path *path)
1680 return proc_fd_info(inode, path, NULL);
1683 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1685 struct inode *inode = dentry->d_inode;
1686 struct task_struct *task = get_proc_task(inode);
1687 int fd = proc_fd(inode);
1688 struct files_struct *files;
1689 const struct cred *cred;
1692 files = get_files_struct(task);
1695 if (fcheck_files(files, fd)) {
1697 put_files_struct(files);
1698 if (task_dumpable(task)) {
1700 cred = __task_cred(task);
1701 inode->i_uid = cred->euid;
1702 inode->i_gid = cred->egid;
1708 inode->i_mode &= ~(S_ISUID | S_ISGID);
1709 security_task_to_inode(task, inode);
1710 put_task_struct(task);
1714 put_files_struct(files);
1716 put_task_struct(task);
1722 static struct dentry_operations tid_fd_dentry_operations =
1724 .d_revalidate = tid_fd_revalidate,
1725 .d_delete = pid_delete_dentry,
1728 static struct dentry *proc_fd_instantiate(struct inode *dir,
1729 struct dentry *dentry, struct task_struct *task, const void *ptr)
1731 unsigned fd = *(const unsigned *)ptr;
1733 struct files_struct *files;
1734 struct inode *inode;
1735 struct proc_inode *ei;
1736 struct dentry *error = ERR_PTR(-ENOENT);
1738 inode = proc_pid_make_inode(dir->i_sb, task);
1743 files = get_files_struct(task);
1746 inode->i_mode = S_IFLNK;
1749 * We are not taking a ref to the file structure, so we must
1752 spin_lock(&files->file_lock);
1753 file = fcheck_files(files, fd);
1756 if (file->f_mode & FMODE_READ)
1757 inode->i_mode |= S_IRUSR | S_IXUSR;
1758 if (file->f_mode & FMODE_WRITE)
1759 inode->i_mode |= S_IWUSR | S_IXUSR;
1760 spin_unlock(&files->file_lock);
1761 put_files_struct(files);
1763 inode->i_op = &proc_pid_link_inode_operations;
1765 ei->op.proc_get_link = proc_fd_link;
1766 dentry->d_op = &tid_fd_dentry_operations;
1767 d_add(dentry, inode);
1768 /* Close the race of the process dying before we return the dentry */
1769 if (tid_fd_revalidate(dentry, NULL))
1775 spin_unlock(&files->file_lock);
1776 put_files_struct(files);
1782 static struct dentry *proc_lookupfd_common(struct inode *dir,
1783 struct dentry *dentry,
1784 instantiate_t instantiate)
1786 struct task_struct *task = get_proc_task(dir);
1787 unsigned fd = name_to_int(dentry);
1788 struct dentry *result = ERR_PTR(-ENOENT);
1795 result = instantiate(dir, dentry, task, &fd);
1797 put_task_struct(task);
1802 static int proc_readfd_common(struct file * filp, void * dirent,
1803 filldir_t filldir, instantiate_t instantiate)
1805 struct dentry *dentry = filp->f_path.dentry;
1806 struct inode *inode = dentry->d_inode;
1807 struct task_struct *p = get_proc_task(inode);
1808 unsigned int fd, ino;
1810 struct files_struct * files;
1820 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1824 ino = parent_ino(dentry);
1825 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1829 files = get_files_struct(p);
1833 for (fd = filp->f_pos-2;
1834 fd < files_fdtable(files)->max_fds;
1835 fd++, filp->f_pos++) {
1836 char name[PROC_NUMBUF];
1839 if (!fcheck_files(files, fd))
1843 len = snprintf(name, sizeof(name), "%d", fd);
1844 if (proc_fill_cache(filp, dirent, filldir,
1845 name, len, instantiate,
1853 put_files_struct(files);
1861 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1862 struct nameidata *nd)
1864 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1867 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1869 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1872 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1873 size_t len, loff_t *ppos)
1875 char tmp[PROC_FDINFO_MAX];
1876 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1878 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1882 static const struct file_operations proc_fdinfo_file_operations = {
1883 .open = nonseekable_open,
1884 .read = proc_fdinfo_read,
1887 static const struct file_operations proc_fd_operations = {
1888 .read = generic_read_dir,
1889 .readdir = proc_readfd,
1893 * /proc/pid/fd needs a special permission handler so that a process can still
1894 * access /proc/self/fd after it has executed a setuid().
1896 static int proc_fd_permission(struct inode *inode, int mask)
1900 rv = generic_permission(inode, mask, NULL);
1903 if (task_pid(current) == proc_pid(inode))
1909 * proc directories can do almost nothing..
1911 static const struct inode_operations proc_fd_inode_operations = {
1912 .lookup = proc_lookupfd,
1913 .permission = proc_fd_permission,
1914 .setattr = proc_setattr,
1917 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1918 struct dentry *dentry, struct task_struct *task, const void *ptr)
1920 unsigned fd = *(unsigned *)ptr;
1921 struct inode *inode;
1922 struct proc_inode *ei;
1923 struct dentry *error = ERR_PTR(-ENOENT);
1925 inode = proc_pid_make_inode(dir->i_sb, task);
1930 inode->i_mode = S_IFREG | S_IRUSR;
1931 inode->i_fop = &proc_fdinfo_file_operations;
1932 dentry->d_op = &tid_fd_dentry_operations;
1933 d_add(dentry, inode);
1934 /* Close the race of the process dying before we return the dentry */
1935 if (tid_fd_revalidate(dentry, NULL))
1942 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1943 struct dentry *dentry,
1944 struct nameidata *nd)
1946 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1949 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1951 return proc_readfd_common(filp, dirent, filldir,
1952 proc_fdinfo_instantiate);
1955 static const struct file_operations proc_fdinfo_operations = {
1956 .read = generic_read_dir,
1957 .readdir = proc_readfdinfo,
1961 * proc directories can do almost nothing..
1963 static const struct inode_operations proc_fdinfo_inode_operations = {
1964 .lookup = proc_lookupfdinfo,
1965 .setattr = proc_setattr,
1969 static struct dentry *proc_pident_instantiate(struct inode *dir,
1970 struct dentry *dentry, struct task_struct *task, const void *ptr)
1972 const struct pid_entry *p = ptr;
1973 struct inode *inode;
1974 struct proc_inode *ei;
1975 struct dentry *error = ERR_PTR(-EINVAL);
1977 inode = proc_pid_make_inode(dir->i_sb, task);
1982 inode->i_mode = p->mode;
1983 if (S_ISDIR(inode->i_mode))
1984 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1986 inode->i_op = p->iop;
1988 inode->i_fop = p->fop;
1990 dentry->d_op = &pid_dentry_operations;
1991 d_add(dentry, inode);
1992 /* Close the race of the process dying before we return the dentry */
1993 if (pid_revalidate(dentry, NULL))
1999 static struct dentry *proc_pident_lookup(struct inode *dir,
2000 struct dentry *dentry,
2001 const struct pid_entry *ents,
2004 struct dentry *error;
2005 struct task_struct *task = get_proc_task(dir);
2006 const struct pid_entry *p, *last;
2008 error = ERR_PTR(-ENOENT);
2014 * Yes, it does not scale. And it should not. Don't add
2015 * new entries into /proc/<tgid>/ without very good reasons.
2017 last = &ents[nents - 1];
2018 for (p = ents; p <= last; p++) {
2019 if (p->len != dentry->d_name.len)
2021 if (!memcmp(dentry->d_name.name, p->name, p->len))
2027 error = proc_pident_instantiate(dir, dentry, task, p);
2029 put_task_struct(task);
2034 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2035 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2037 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2038 proc_pident_instantiate, task, p);
2041 static int proc_pident_readdir(struct file *filp,
2042 void *dirent, filldir_t filldir,
2043 const struct pid_entry *ents, unsigned int nents)
2046 struct dentry *dentry = filp->f_path.dentry;
2047 struct inode *inode = dentry->d_inode;
2048 struct task_struct *task = get_proc_task(inode);
2049 const struct pid_entry *p, *last;
2062 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2068 ino = parent_ino(dentry);
2069 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2081 last = &ents[nents - 1];
2083 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2092 put_task_struct(task);
2097 #ifdef CONFIG_SECURITY
2098 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2099 size_t count, loff_t *ppos)
2101 struct inode * inode = file->f_path.dentry->d_inode;
2104 struct task_struct *task = get_proc_task(inode);
2109 length = security_getprocattr(task,
2110 (char*)file->f_path.dentry->d_name.name,
2112 put_task_struct(task);
2114 length = simple_read_from_buffer(buf, count, ppos, p, length);
2119 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2120 size_t count, loff_t *ppos)
2122 struct inode * inode = file->f_path.dentry->d_inode;
2125 struct task_struct *task = get_proc_task(inode);
2130 if (count > PAGE_SIZE)
2133 /* No partial writes. */
2139 page = (char*)__get_free_page(GFP_TEMPORARY);
2144 if (copy_from_user(page, buf, count))
2147 length = security_setprocattr(task,
2148 (char*)file->f_path.dentry->d_name.name,
2149 (void*)page, count);
2151 free_page((unsigned long) page);
2153 put_task_struct(task);
2158 static const struct file_operations proc_pid_attr_operations = {
2159 .read = proc_pid_attr_read,
2160 .write = proc_pid_attr_write,
2163 static const struct pid_entry attr_dir_stuff[] = {
2164 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2165 REG("prev", S_IRUGO, proc_pid_attr_operations),
2166 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2167 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2168 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2169 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2172 static int proc_attr_dir_readdir(struct file * filp,
2173 void * dirent, filldir_t filldir)
2175 return proc_pident_readdir(filp,dirent,filldir,
2176 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2179 static const struct file_operations proc_attr_dir_operations = {
2180 .read = generic_read_dir,
2181 .readdir = proc_attr_dir_readdir,
2184 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2185 struct dentry *dentry, struct nameidata *nd)
2187 return proc_pident_lookup(dir, dentry,
2188 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2191 static const struct inode_operations proc_attr_dir_inode_operations = {
2192 .lookup = proc_attr_dir_lookup,
2193 .getattr = pid_getattr,
2194 .setattr = proc_setattr,
2199 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2200 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2201 size_t count, loff_t *ppos)
2203 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2204 struct mm_struct *mm;
2205 char buffer[PROC_NUMBUF];
2213 mm = get_task_mm(task);
2215 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2216 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2217 MMF_DUMP_FILTER_SHIFT));
2219 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2222 put_task_struct(task);
2227 static ssize_t proc_coredump_filter_write(struct file *file,
2228 const char __user *buf,
2232 struct task_struct *task;
2233 struct mm_struct *mm;
2234 char buffer[PROC_NUMBUF], *end;
2241 memset(buffer, 0, sizeof(buffer));
2242 if (count > sizeof(buffer) - 1)
2243 count = sizeof(buffer) - 1;
2244 if (copy_from_user(buffer, buf, count))
2248 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2251 if (end - buffer == 0)
2255 task = get_proc_task(file->f_dentry->d_inode);
2260 mm = get_task_mm(task);
2264 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2266 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2268 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2273 put_task_struct(task);
2278 static const struct file_operations proc_coredump_filter_operations = {
2279 .read = proc_coredump_filter_read,
2280 .write = proc_coredump_filter_write,
2287 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2290 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2291 pid_t tgid = task_tgid_nr_ns(current, ns);
2292 char tmp[PROC_NUMBUF];
2295 sprintf(tmp, "%d", tgid);
2296 return vfs_readlink(dentry,buffer,buflen,tmp);
2299 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2301 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2302 pid_t tgid = task_tgid_nr_ns(current, ns);
2303 char tmp[PROC_NUMBUF];
2305 return ERR_PTR(-ENOENT);
2306 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2307 return ERR_PTR(vfs_follow_link(nd,tmp));
2310 static const struct inode_operations proc_self_inode_operations = {
2311 .readlink = proc_self_readlink,
2312 .follow_link = proc_self_follow_link,
2318 * These are the directory entries in the root directory of /proc
2319 * that properly belong to the /proc filesystem, as they describe
2320 * describe something that is process related.
2322 static const struct pid_entry proc_base_stuff[] = {
2323 NOD("self", S_IFLNK|S_IRWXUGO,
2324 &proc_self_inode_operations, NULL, {}),
2328 * Exceptional case: normally we are not allowed to unhash a busy
2329 * directory. In this case, however, we can do it - no aliasing problems
2330 * due to the way we treat inodes.
2332 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2334 struct inode *inode = dentry->d_inode;
2335 struct task_struct *task = get_proc_task(inode);
2337 put_task_struct(task);
2344 static struct dentry_operations proc_base_dentry_operations =
2346 .d_revalidate = proc_base_revalidate,
2347 .d_delete = pid_delete_dentry,
2350 static struct dentry *proc_base_instantiate(struct inode *dir,
2351 struct dentry *dentry, struct task_struct *task, const void *ptr)
2353 const struct pid_entry *p = ptr;
2354 struct inode *inode;
2355 struct proc_inode *ei;
2356 struct dentry *error = ERR_PTR(-EINVAL);
2358 /* Allocate the inode */
2359 error = ERR_PTR(-ENOMEM);
2360 inode = new_inode(dir->i_sb);
2364 /* Initialize the inode */
2366 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2369 * grab the reference to the task.
2371 ei->pid = get_task_pid(task, PIDTYPE_PID);
2377 inode->i_mode = p->mode;
2378 if (S_ISDIR(inode->i_mode))
2380 if (S_ISLNK(inode->i_mode))
2383 inode->i_op = p->iop;
2385 inode->i_fop = p->fop;
2387 dentry->d_op = &proc_base_dentry_operations;
2388 d_add(dentry, inode);
2397 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2399 struct dentry *error;
2400 struct task_struct *task = get_proc_task(dir);
2401 const struct pid_entry *p, *last;
2403 error = ERR_PTR(-ENOENT);
2408 /* Lookup the directory entry */
2409 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2410 for (p = proc_base_stuff; p <= last; p++) {
2411 if (p->len != dentry->d_name.len)
2413 if (!memcmp(dentry->d_name.name, p->name, p->len))
2419 error = proc_base_instantiate(dir, dentry, task, p);
2422 put_task_struct(task);
2427 static int proc_base_fill_cache(struct file *filp, void *dirent,
2428 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2430 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2431 proc_base_instantiate, task, p);
2434 #ifdef CONFIG_TASK_IO_ACCOUNTING
2435 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2437 struct task_io_accounting acct = task->ioac;
2438 unsigned long flags;
2440 if (whole && lock_task_sighand(task, &flags)) {
2441 struct task_struct *t = task;
2443 task_io_accounting_add(&acct, &task->signal->ioac);
2444 while_each_thread(task, t)
2445 task_io_accounting_add(&acct, &t->ioac);
2447 unlock_task_sighand(task, &flags);
2449 return sprintf(buffer,
2454 "read_bytes: %llu\n"
2455 "write_bytes: %llu\n"
2456 "cancelled_write_bytes: %llu\n",
2457 (unsigned long long)acct.rchar,
2458 (unsigned long long)acct.wchar,
2459 (unsigned long long)acct.syscr,
2460 (unsigned long long)acct.syscw,
2461 (unsigned long long)acct.read_bytes,
2462 (unsigned long long)acct.write_bytes,
2463 (unsigned long long)acct.cancelled_write_bytes);
2466 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2468 return do_io_accounting(task, buffer, 0);
2471 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2473 return do_io_accounting(task, buffer, 1);
2475 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2477 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2478 struct pid *pid, struct task_struct *task)
2480 seq_printf(m, "%08x\n", task->personality);
2487 static const struct file_operations proc_task_operations;
2488 static const struct inode_operations proc_task_inode_operations;
2490 static const struct pid_entry tgid_base_stuff[] = {
2491 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2492 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2493 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2495 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2497 REG("environ", S_IRUSR, proc_environ_operations),
2498 INF("auxv", S_IRUSR, proc_pid_auxv),
2499 ONE("status", S_IRUGO, proc_pid_status),
2500 ONE("personality", S_IRUSR, proc_pid_personality),
2501 INF("limits", S_IRUSR, proc_pid_limits),
2502 #ifdef CONFIG_SCHED_DEBUG
2503 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2505 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2506 INF("syscall", S_IRUSR, proc_pid_syscall),
2508 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2509 ONE("stat", S_IRUGO, proc_tgid_stat),
2510 ONE("statm", S_IRUGO, proc_pid_statm),
2511 REG("maps", S_IRUGO, proc_maps_operations),
2513 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2515 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2516 LNK("cwd", proc_cwd_link),
2517 LNK("root", proc_root_link),
2518 LNK("exe", proc_exe_link),
2519 REG("mounts", S_IRUGO, proc_mounts_operations),
2520 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2521 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2522 #ifdef CONFIG_PROC_PAGE_MONITOR
2523 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2524 REG("smaps", S_IRUGO, proc_smaps_operations),
2525 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2527 #ifdef CONFIG_SECURITY
2528 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2530 #ifdef CONFIG_KALLSYMS
2531 INF("wchan", S_IRUGO, proc_pid_wchan),
2533 #ifdef CONFIG_STACKTRACE
2534 ONE("stack", S_IRUSR, proc_pid_stack),
2536 #ifdef CONFIG_SCHEDSTATS
2537 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2539 #ifdef CONFIG_LATENCYTOP
2540 REG("latency", S_IRUGO, proc_lstats_operations),
2542 #ifdef CONFIG_PROC_PID_CPUSET
2543 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2545 #ifdef CONFIG_CGROUPS
2546 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2548 INF("oom_score", S_IRUGO, proc_oom_score),
2549 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2550 #ifdef CONFIG_AUDITSYSCALL
2551 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2552 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2554 #ifdef CONFIG_FAULT_INJECTION
2555 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2557 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2558 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2560 #ifdef CONFIG_TASK_IO_ACCOUNTING
2561 INF("io", S_IRUGO, proc_tgid_io_accounting),
2565 static int proc_tgid_base_readdir(struct file * filp,
2566 void * dirent, filldir_t filldir)
2568 return proc_pident_readdir(filp,dirent,filldir,
2569 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2572 static const struct file_operations proc_tgid_base_operations = {
2573 .read = generic_read_dir,
2574 .readdir = proc_tgid_base_readdir,
2577 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2578 return proc_pident_lookup(dir, dentry,
2579 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2582 static const struct inode_operations proc_tgid_base_inode_operations = {
2583 .lookup = proc_tgid_base_lookup,
2584 .getattr = pid_getattr,
2585 .setattr = proc_setattr,
2588 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2590 struct dentry *dentry, *leader, *dir;
2591 char buf[PROC_NUMBUF];
2595 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2596 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2598 if (!(current->flags & PF_EXITING))
2599 shrink_dcache_parent(dentry);
2608 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2609 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2614 name.len = strlen(name.name);
2615 dir = d_hash_and_lookup(leader, &name);
2617 goto out_put_leader;
2620 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2621 dentry = d_hash_and_lookup(dir, &name);
2623 shrink_dcache_parent(dentry);
2636 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2637 * @task: task that should be flushed.
2639 * When flushing dentries from proc, one needs to flush them from global
2640 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2641 * in. This call is supposed to do all of this job.
2643 * Looks in the dcache for
2645 * /proc/@tgid/task/@pid
2646 * if either directory is present flushes it and all of it'ts children
2649 * It is safe and reasonable to cache /proc entries for a task until
2650 * that task exits. After that they just clog up the dcache with
2651 * useless entries, possibly causing useful dcache entries to be
2652 * flushed instead. This routine is proved to flush those useless
2653 * dcache entries at process exit time.
2655 * NOTE: This routine is just an optimization so it does not guarantee
2656 * that no dcache entries will exist at process exit time it
2657 * just makes it very unlikely that any will persist.
2660 void proc_flush_task(struct task_struct *task)
2663 struct pid *pid, *tgid = NULL;
2666 pid = task_pid(task);
2667 if (thread_group_leader(task))
2668 tgid = task_tgid(task);
2670 for (i = 0; i <= pid->level; i++) {
2671 upid = &pid->numbers[i];
2672 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2673 tgid ? tgid->numbers[i].nr : 0);
2676 upid = &pid->numbers[pid->level];
2678 pid_ns_release_proc(upid->ns);
2681 static struct dentry *proc_pid_instantiate(struct inode *dir,
2682 struct dentry * dentry,
2683 struct task_struct *task, const void *ptr)
2685 struct dentry *error = ERR_PTR(-ENOENT);
2686 struct inode *inode;
2688 inode = proc_pid_make_inode(dir->i_sb, task);
2692 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2693 inode->i_op = &proc_tgid_base_inode_operations;
2694 inode->i_fop = &proc_tgid_base_operations;
2695 inode->i_flags|=S_IMMUTABLE;
2697 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2698 ARRAY_SIZE(tgid_base_stuff));
2700 dentry->d_op = &pid_dentry_operations;
2702 d_add(dentry, inode);
2703 /* Close the race of the process dying before we return the dentry */
2704 if (pid_revalidate(dentry, NULL))
2710 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2712 struct dentry *result = ERR_PTR(-ENOENT);
2713 struct task_struct *task;
2715 struct pid_namespace *ns;
2717 result = proc_base_lookup(dir, dentry);
2718 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2721 tgid = name_to_int(dentry);
2725 ns = dentry->d_sb->s_fs_info;
2727 task = find_task_by_pid_ns(tgid, ns);
2729 get_task_struct(task);
2734 result = proc_pid_instantiate(dir, dentry, task, NULL);
2735 put_task_struct(task);
2741 * Find the first task with tgid >= tgid
2746 struct task_struct *task;
2748 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2753 put_task_struct(iter.task);
2757 pid = find_ge_pid(iter.tgid, ns);
2759 iter.tgid = pid_nr_ns(pid, ns);
2760 iter.task = pid_task(pid, PIDTYPE_PID);
2761 /* What we to know is if the pid we have find is the
2762 * pid of a thread_group_leader. Testing for task
2763 * being a thread_group_leader is the obvious thing
2764 * todo but there is a window when it fails, due to
2765 * the pid transfer logic in de_thread.
2767 * So we perform the straight forward test of seeing
2768 * if the pid we have found is the pid of a thread
2769 * group leader, and don't worry if the task we have
2770 * found doesn't happen to be a thread group leader.
2771 * As we don't care in the case of readdir.
2773 if (!iter.task || !has_group_leader_pid(iter.task)) {
2777 get_task_struct(iter.task);
2783 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2785 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2786 struct tgid_iter iter)
2788 char name[PROC_NUMBUF];
2789 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2790 return proc_fill_cache(filp, dirent, filldir, name, len,
2791 proc_pid_instantiate, iter.task, NULL);
2794 /* for the /proc/ directory itself, after non-process stuff has been done */
2795 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2797 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2798 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2799 struct tgid_iter iter;
2800 struct pid_namespace *ns;
2805 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2806 const struct pid_entry *p = &proc_base_stuff[nr];
2807 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2811 ns = filp->f_dentry->d_sb->s_fs_info;
2813 iter.tgid = filp->f_pos - TGID_OFFSET;
2814 for (iter = next_tgid(ns, iter);
2816 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2817 filp->f_pos = iter.tgid + TGID_OFFSET;
2818 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2819 put_task_struct(iter.task);
2823 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2825 put_task_struct(reaper);
2833 static const struct pid_entry tid_base_stuff[] = {
2834 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2835 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2836 REG("environ", S_IRUSR, proc_environ_operations),
2837 INF("auxv", S_IRUSR, proc_pid_auxv),
2838 ONE("status", S_IRUGO, proc_pid_status),
2839 ONE("personality", S_IRUSR, proc_pid_personality),
2840 INF("limits", S_IRUSR, proc_pid_limits),
2841 #ifdef CONFIG_SCHED_DEBUG
2842 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2844 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2845 INF("syscall", S_IRUSR, proc_pid_syscall),
2847 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2848 ONE("stat", S_IRUGO, proc_tid_stat),
2849 ONE("statm", S_IRUGO, proc_pid_statm),
2850 REG("maps", S_IRUGO, proc_maps_operations),
2852 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2854 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2855 LNK("cwd", proc_cwd_link),
2856 LNK("root", proc_root_link),
2857 LNK("exe", proc_exe_link),
2858 REG("mounts", S_IRUGO, proc_mounts_operations),
2859 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2860 #ifdef CONFIG_PROC_PAGE_MONITOR
2861 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2862 REG("smaps", S_IRUGO, proc_smaps_operations),
2863 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2865 #ifdef CONFIG_SECURITY
2866 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2868 #ifdef CONFIG_KALLSYMS
2869 INF("wchan", S_IRUGO, proc_pid_wchan),
2871 #ifdef CONFIG_STACKTRACE
2872 ONE("stack", S_IRUSR, proc_pid_stack),
2874 #ifdef CONFIG_SCHEDSTATS
2875 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2877 #ifdef CONFIG_LATENCYTOP
2878 REG("latency", S_IRUGO, proc_lstats_operations),
2880 #ifdef CONFIG_PROC_PID_CPUSET
2881 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2883 #ifdef CONFIG_CGROUPS
2884 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2886 INF("oom_score", S_IRUGO, proc_oom_score),
2887 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2888 #ifdef CONFIG_AUDITSYSCALL
2889 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2890 REG("sessionid", S_IRUSR, proc_sessionid_operations),
2892 #ifdef CONFIG_FAULT_INJECTION
2893 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2895 #ifdef CONFIG_TASK_IO_ACCOUNTING
2896 INF("io", S_IRUGO, proc_tid_io_accounting),
2900 static int proc_tid_base_readdir(struct file * filp,
2901 void * dirent, filldir_t filldir)
2903 return proc_pident_readdir(filp,dirent,filldir,
2904 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2907 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2908 return proc_pident_lookup(dir, dentry,
2909 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2912 static const struct file_operations proc_tid_base_operations = {
2913 .read = generic_read_dir,
2914 .readdir = proc_tid_base_readdir,
2917 static const struct inode_operations proc_tid_base_inode_operations = {
2918 .lookup = proc_tid_base_lookup,
2919 .getattr = pid_getattr,
2920 .setattr = proc_setattr,
2923 static struct dentry *proc_task_instantiate(struct inode *dir,
2924 struct dentry *dentry, struct task_struct *task, const void *ptr)
2926 struct dentry *error = ERR_PTR(-ENOENT);
2927 struct inode *inode;
2928 inode = proc_pid_make_inode(dir->i_sb, task);
2932 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2933 inode->i_op = &proc_tid_base_inode_operations;
2934 inode->i_fop = &proc_tid_base_operations;
2935 inode->i_flags|=S_IMMUTABLE;
2937 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2938 ARRAY_SIZE(tid_base_stuff));
2940 dentry->d_op = &pid_dentry_operations;
2942 d_add(dentry, inode);
2943 /* Close the race of the process dying before we return the dentry */
2944 if (pid_revalidate(dentry, NULL))
2950 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2952 struct dentry *result = ERR_PTR(-ENOENT);
2953 struct task_struct *task;
2954 struct task_struct *leader = get_proc_task(dir);
2956 struct pid_namespace *ns;
2961 tid = name_to_int(dentry);
2965 ns = dentry->d_sb->s_fs_info;
2967 task = find_task_by_pid_ns(tid, ns);
2969 get_task_struct(task);
2973 if (!same_thread_group(leader, task))
2976 result = proc_task_instantiate(dir, dentry, task, NULL);
2978 put_task_struct(task);
2980 put_task_struct(leader);
2986 * Find the first tid of a thread group to return to user space.
2988 * Usually this is just the thread group leader, but if the users
2989 * buffer was too small or there was a seek into the middle of the
2990 * directory we have more work todo.
2992 * In the case of a short read we start with find_task_by_pid.
2994 * In the case of a seek we start with the leader and walk nr
2997 static struct task_struct *first_tid(struct task_struct *leader,
2998 int tid, int nr, struct pid_namespace *ns)
3000 struct task_struct *pos;
3003 /* Attempt to start with the pid of a thread */
3004 if (tid && (nr > 0)) {
3005 pos = find_task_by_pid_ns(tid, ns);
3006 if (pos && (pos->group_leader == leader))
3010 /* If nr exceeds the number of threads there is nothing todo */
3012 if (nr && nr >= get_nr_threads(leader))
3015 /* If we haven't found our starting place yet start
3016 * with the leader and walk nr threads forward.
3018 for (pos = leader; nr > 0; --nr) {
3019 pos = next_thread(pos);
3020 if (pos == leader) {
3026 get_task_struct(pos);
3033 * Find the next thread in the thread list.
3034 * Return NULL if there is an error or no next thread.
3036 * The reference to the input task_struct is released.
3038 static struct task_struct *next_tid(struct task_struct *start)
3040 struct task_struct *pos = NULL;
3042 if (pid_alive(start)) {
3043 pos = next_thread(start);
3044 if (thread_group_leader(pos))
3047 get_task_struct(pos);
3050 put_task_struct(start);
3054 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3055 struct task_struct *task, int tid)
3057 char name[PROC_NUMBUF];
3058 int len = snprintf(name, sizeof(name), "%d", tid);
3059 return proc_fill_cache(filp, dirent, filldir, name, len,
3060 proc_task_instantiate, task, NULL);
3063 /* for the /proc/TGID/task/ directories */
3064 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3066 struct dentry *dentry = filp->f_path.dentry;
3067 struct inode *inode = dentry->d_inode;
3068 struct task_struct *leader = NULL;
3069 struct task_struct *task;
3070 int retval = -ENOENT;
3073 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
3074 struct pid_namespace *ns;
3076 task = get_proc_task(inode);
3080 if (pid_alive(task)) {
3081 leader = task->group_leader;
3082 get_task_struct(leader);
3085 put_task_struct(task);
3093 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3098 ino = parent_ino(dentry);
3099 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3105 /* f_version caches the tgid value that the last readdir call couldn't
3106 * return. lseek aka telldir automagically resets f_version to 0.
3108 ns = filp->f_dentry->d_sb->s_fs_info;
3109 tid = (int)filp->f_version;
3110 filp->f_version = 0;
3111 for (task = first_tid(leader, tid, pos - 2, ns);
3113 task = next_tid(task), pos++) {
3114 tid = task_pid_nr_ns(task, ns);
3115 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3116 /* returning this tgid failed, save it as the first
3117 * pid for the next readir call */
3118 filp->f_version = (u64)tid;
3119 put_task_struct(task);
3125 put_task_struct(leader);
3130 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3132 struct inode *inode = dentry->d_inode;
3133 struct task_struct *p = get_proc_task(inode);
3134 generic_fillattr(inode, stat);
3137 stat->nlink += get_nr_threads(p);
3144 static const struct inode_operations proc_task_inode_operations = {
3145 .lookup = proc_task_lookup,
3146 .getattr = proc_task_getattr,
3147 .setattr = proc_setattr,
3150 static const struct file_operations proc_task_operations = {
3151 .read = generic_read_dir,
3152 .readdir = proc_task_readdir,