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/resource.h>
69 #include <linux/module.h>
70 #include <linux/mount.h>
71 #include <linux/security.h>
72 #include <linux/ptrace.h>
73 #include <linux/tracehook.h>
74 #include <linux/cgroup.h>
75 #include <linux/cpuset.h>
76 #include <linux/audit.h>
77 #include <linux/poll.h>
78 #include <linux/nsproxy.h>
79 #include <linux/oom.h>
80 #include <linux/elf.h>
81 #include <linux/pid_namespace.h>
85 * Implementing inode permission operations in /proc is almost
86 * certainly an error. Permission checks need to happen during
87 * each system call not at open time. The reason is that most of
88 * what we wish to check for permissions in /proc varies at runtime.
90 * The classic example of a problem is opening file descriptors
91 * in /proc for a task before it execs a suid executable.
98 const struct inode_operations *iop;
99 const struct file_operations *fop;
103 #define NOD(NAME, MODE, IOP, FOP, OP) { \
105 .len = sizeof(NAME) - 1, \
112 #define DIR(NAME, MODE, iops, fops) \
113 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
114 #define LNK(NAME, get_link) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = get_link } )
118 #define REG(NAME, MODE, fops) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
120 #define INF(NAME, MODE, read) \
121 NOD(NAME, (S_IFREG|(MODE)), \
122 NULL, &proc_info_file_operations, \
123 { .proc_read = read } )
124 #define ONE(NAME, MODE, show) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = show } )
130 * Count the number of hardlinks for the pid_entry table, excluding the .
133 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
140 for (i = 0; i < n; ++i) {
141 if (S_ISDIR(entries[i].mode))
148 static struct fs_struct *get_fs_struct(struct task_struct *task)
150 struct fs_struct *fs;
154 atomic_inc(&fs->count);
159 static int get_nr_threads(struct task_struct *tsk)
164 if (lock_task_sighand(tsk, &flags)) {
165 count = atomic_read(&tsk->signal->count);
166 unlock_task_sighand(tsk, &flags);
171 static int proc_cwd_link(struct inode *inode, struct path *path)
173 struct task_struct *task = get_proc_task(inode);
174 struct fs_struct *fs = NULL;
175 int result = -ENOENT;
178 fs = get_fs_struct(task);
179 put_task_struct(task);
182 read_lock(&fs->lock);
185 read_unlock(&fs->lock);
192 static int proc_root_link(struct inode *inode, struct path *path)
194 struct task_struct *task = get_proc_task(inode);
195 struct fs_struct *fs = NULL;
196 int result = -ENOENT;
199 fs = get_fs_struct(task);
200 put_task_struct(task);
203 read_lock(&fs->lock);
206 read_unlock(&fs->lock);
214 * Return zero if current may access user memory in @task, -error if not.
216 static int check_mem_permission(struct task_struct *task)
219 * A task can always look at itself, in case it chooses
220 * to use system calls instead of load instructions.
226 * If current is actively ptrace'ing, and would also be
227 * permitted to freshly attach with ptrace now, permit it.
229 if (task_is_stopped_or_traced(task)) {
232 match = (tracehook_tracer_task(task) == current);
234 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
239 * Noone else is allowed.
244 struct mm_struct *mm_for_maps(struct task_struct *task)
246 struct mm_struct *mm = get_task_mm(task);
249 down_read(&mm->mmap_sem);
253 if (task->mm != current->mm &&
254 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
260 up_read(&mm->mmap_sem);
265 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
269 struct mm_struct *mm = get_task_mm(task);
273 goto out_mm; /* Shh! No looking before we're done */
275 len = mm->arg_end - mm->arg_start;
280 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
282 // If the nul at the end of args has been overwritten, then
283 // assume application is using setproctitle(3).
284 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
285 len = strnlen(buffer, res);
289 len = mm->env_end - mm->env_start;
290 if (len > PAGE_SIZE - res)
291 len = PAGE_SIZE - res;
292 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
293 res = strnlen(buffer, res);
302 static int proc_pid_auxv(struct task_struct *task, char *buffer)
305 struct mm_struct *mm = get_task_mm(task);
307 unsigned int nwords = 0;
310 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
311 res = nwords * sizeof(mm->saved_auxv[0]);
314 memcpy(buffer, mm->saved_auxv, res);
321 #ifdef CONFIG_KALLSYMS
323 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
324 * Returns the resolved symbol. If that fails, simply return the address.
326 static int proc_pid_wchan(struct task_struct *task, char *buffer)
329 char symname[KSYM_NAME_LEN];
331 wchan = get_wchan(task);
333 if (lookup_symbol_name(wchan, symname) < 0)
334 return sprintf(buffer, "%lu", wchan);
336 return sprintf(buffer, "%s", symname);
338 #endif /* CONFIG_KALLSYMS */
340 #ifdef CONFIG_SCHEDSTATS
342 * Provides /proc/PID/schedstat
344 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
346 return sprintf(buffer, "%llu %llu %lu\n",
347 (unsigned long long)task->se.sum_exec_runtime,
348 (unsigned long long)task->sched_info.run_delay,
349 task->sched_info.pcount);
353 #ifdef CONFIG_LATENCYTOP
354 static int lstats_show_proc(struct seq_file *m, void *v)
357 struct inode *inode = m->private;
358 struct task_struct *task = get_proc_task(inode);
362 seq_puts(m, "Latency Top version : v0.1\n");
363 for (i = 0; i < 32; i++) {
364 if (task->latency_record[i].backtrace[0]) {
366 seq_printf(m, "%i %li %li ",
367 task->latency_record[i].count,
368 task->latency_record[i].time,
369 task->latency_record[i].max);
370 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
371 char sym[KSYM_SYMBOL_LEN];
373 if (!task->latency_record[i].backtrace[q])
375 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
377 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
378 c = strchr(sym, '+');
381 seq_printf(m, "%s ", sym);
387 put_task_struct(task);
391 static int lstats_open(struct inode *inode, struct file *file)
393 return single_open(file, lstats_show_proc, inode);
396 static ssize_t lstats_write(struct file *file, const char __user *buf,
397 size_t count, loff_t *offs)
399 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
403 clear_all_latency_tracing(task);
404 put_task_struct(task);
409 static const struct file_operations proc_lstats_operations = {
412 .write = lstats_write,
414 .release = single_release,
419 /* The badness from the OOM killer */
420 unsigned long badness(struct task_struct *p, unsigned long uptime);
421 static int proc_oom_score(struct task_struct *task, char *buffer)
423 unsigned long points;
424 struct timespec uptime;
426 do_posix_clock_monotonic_gettime(&uptime);
427 read_lock(&tasklist_lock);
428 points = badness(task, uptime.tv_sec);
429 read_unlock(&tasklist_lock);
430 return sprintf(buffer, "%lu\n", points);
438 static const struct limit_names lnames[RLIM_NLIMITS] = {
439 [RLIMIT_CPU] = {"Max cpu time", "ms"},
440 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
441 [RLIMIT_DATA] = {"Max data size", "bytes"},
442 [RLIMIT_STACK] = {"Max stack size", "bytes"},
443 [RLIMIT_CORE] = {"Max core file size", "bytes"},
444 [RLIMIT_RSS] = {"Max resident set", "bytes"},
445 [RLIMIT_NPROC] = {"Max processes", "processes"},
446 [RLIMIT_NOFILE] = {"Max open files", "files"},
447 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
448 [RLIMIT_AS] = {"Max address space", "bytes"},
449 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
450 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
451 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
452 [RLIMIT_NICE] = {"Max nice priority", NULL},
453 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
454 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
457 /* Display limits for a process */
458 static int proc_pid_limits(struct task_struct *task, char *buffer)
463 char *bufptr = buffer;
465 struct rlimit rlim[RLIM_NLIMITS];
467 if (!lock_task_sighand(task, &flags))
469 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
470 unlock_task_sighand(task, &flags);
473 * print the file header
475 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
476 "Limit", "Soft Limit", "Hard Limit", "Units");
478 for (i = 0; i < RLIM_NLIMITS; i++) {
479 if (rlim[i].rlim_cur == RLIM_INFINITY)
480 count += sprintf(&bufptr[count], "%-25s %-20s ",
481 lnames[i].name, "unlimited");
483 count += sprintf(&bufptr[count], "%-25s %-20lu ",
484 lnames[i].name, rlim[i].rlim_cur);
486 if (rlim[i].rlim_max == RLIM_INFINITY)
487 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
489 count += sprintf(&bufptr[count], "%-20lu ",
493 count += sprintf(&bufptr[count], "%-10s\n",
496 count += sprintf(&bufptr[count], "\n");
502 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
503 static int proc_pid_syscall(struct task_struct *task, char *buffer)
506 unsigned long args[6], sp, pc;
508 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
509 return sprintf(buffer, "running\n");
512 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
514 return sprintf(buffer,
515 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
517 args[0], args[1], args[2], args[3], args[4], args[5],
520 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
522 /************************************************************************/
523 /* Here the fs part begins */
524 /************************************************************************/
526 /* permission checks */
527 static int proc_fd_access_allowed(struct inode *inode)
529 struct task_struct *task;
531 /* Allow access to a task's file descriptors if it is us or we
532 * may use ptrace attach to the process and find out that
535 task = get_proc_task(inode);
537 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
538 put_task_struct(task);
543 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
546 struct inode *inode = dentry->d_inode;
548 if (attr->ia_valid & ATTR_MODE)
551 error = inode_change_ok(inode, attr);
553 error = inode_setattr(inode, attr);
557 static const struct inode_operations proc_def_inode_operations = {
558 .setattr = proc_setattr,
561 static int mounts_open_common(struct inode *inode, struct file *file,
562 const struct seq_operations *op)
564 struct task_struct *task = get_proc_task(inode);
566 struct mnt_namespace *ns = NULL;
567 struct fs_struct *fs = NULL;
569 struct proc_mounts *p;
574 nsp = task_nsproxy(task);
582 fs = get_fs_struct(task);
583 put_task_struct(task);
591 read_lock(&fs->lock);
594 read_unlock(&fs->lock);
598 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
602 file->private_data = &p->m;
603 ret = seq_open(file, op);
610 p->event = ns->event;
624 static int mounts_release(struct inode *inode, struct file *file)
626 struct proc_mounts *p = file->private_data;
629 return seq_release(inode, file);
632 static unsigned mounts_poll(struct file *file, poll_table *wait)
634 struct proc_mounts *p = file->private_data;
635 struct mnt_namespace *ns = p->ns;
638 poll_wait(file, &ns->poll, wait);
640 spin_lock(&vfsmount_lock);
641 if (p->event != ns->event) {
642 p->event = ns->event;
645 spin_unlock(&vfsmount_lock);
650 static int mounts_open(struct inode *inode, struct file *file)
652 return mounts_open_common(inode, file, &mounts_op);
655 static const struct file_operations proc_mounts_operations = {
659 .release = mounts_release,
663 static int mountinfo_open(struct inode *inode, struct file *file)
665 return mounts_open_common(inode, file, &mountinfo_op);
668 static const struct file_operations proc_mountinfo_operations = {
669 .open = mountinfo_open,
672 .release = mounts_release,
676 static int mountstats_open(struct inode *inode, struct file *file)
678 return mounts_open_common(inode, file, &mountstats_op);
681 static const struct file_operations proc_mountstats_operations = {
682 .open = mountstats_open,
685 .release = mounts_release,
688 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
690 static ssize_t proc_info_read(struct file * file, char __user * buf,
691 size_t count, loff_t *ppos)
693 struct inode * inode = file->f_path.dentry->d_inode;
696 struct task_struct *task = get_proc_task(inode);
702 if (count > PROC_BLOCK_SIZE)
703 count = PROC_BLOCK_SIZE;
706 if (!(page = __get_free_page(GFP_TEMPORARY)))
709 length = PROC_I(inode)->op.proc_read(task, (char*)page);
712 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
715 put_task_struct(task);
720 static const struct file_operations proc_info_file_operations = {
721 .read = proc_info_read,
724 static int proc_single_show(struct seq_file *m, void *v)
726 struct inode *inode = m->private;
727 struct pid_namespace *ns;
729 struct task_struct *task;
732 ns = inode->i_sb->s_fs_info;
733 pid = proc_pid(inode);
734 task = get_pid_task(pid, PIDTYPE_PID);
738 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
740 put_task_struct(task);
744 static int proc_single_open(struct inode *inode, struct file *filp)
747 ret = single_open(filp, proc_single_show, NULL);
749 struct seq_file *m = filp->private_data;
756 static const struct file_operations proc_single_file_operations = {
757 .open = proc_single_open,
760 .release = single_release,
763 static int mem_open(struct inode* inode, struct file* file)
765 file->private_data = (void*)((long)current->self_exec_id);
769 static ssize_t mem_read(struct file * file, char __user * buf,
770 size_t count, loff_t *ppos)
772 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
774 unsigned long src = *ppos;
776 struct mm_struct *mm;
781 if (check_mem_permission(task))
785 page = (char *)__get_free_page(GFP_TEMPORARY);
791 mm = get_task_mm(task);
797 if (file->private_data != (void*)((long)current->self_exec_id))
803 int this_len, retval;
805 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
806 retval = access_process_vm(task, src, page, this_len, 0);
807 if (!retval || check_mem_permission(task)) {
813 if (copy_to_user(buf, page, retval)) {
828 free_page((unsigned long) page);
830 put_task_struct(task);
835 #define mem_write NULL
838 /* This is a security hazard */
839 static ssize_t mem_write(struct file * file, const char __user *buf,
840 size_t count, loff_t *ppos)
844 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
845 unsigned long dst = *ppos;
851 if (check_mem_permission(task))
855 page = (char *)__get_free_page(GFP_TEMPORARY);
861 int this_len, retval;
863 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
864 if (copy_from_user(page, buf, this_len)) {
868 retval = access_process_vm(task, dst, page, this_len, 1);
880 free_page((unsigned long) page);
882 put_task_struct(task);
888 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
892 file->f_pos = offset;
895 file->f_pos += offset;
900 force_successful_syscall_return();
904 static const struct file_operations proc_mem_operations = {
911 static ssize_t environ_read(struct file *file, char __user *buf,
912 size_t count, loff_t *ppos)
914 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
916 unsigned long src = *ppos;
918 struct mm_struct *mm;
923 if (!ptrace_may_access(task, PTRACE_MODE_READ))
927 page = (char *)__get_free_page(GFP_TEMPORARY);
933 mm = get_task_mm(task);
938 int this_len, retval, max_len;
940 this_len = mm->env_end - (mm->env_start + src);
945 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
946 this_len = (this_len > max_len) ? max_len : this_len;
948 retval = access_process_vm(task, (mm->env_start + src),
956 if (copy_to_user(buf, page, retval)) {
970 free_page((unsigned long) page);
972 put_task_struct(task);
977 static const struct file_operations proc_environ_operations = {
978 .read = environ_read,
981 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
982 size_t count, loff_t *ppos)
984 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
985 char buffer[PROC_NUMBUF];
991 oom_adjust = task->oomkilladj;
992 put_task_struct(task);
994 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
996 return simple_read_from_buffer(buf, count, ppos, buffer, len);
999 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1000 size_t count, loff_t *ppos)
1002 struct task_struct *task;
1003 char buffer[PROC_NUMBUF], *end;
1006 memset(buffer, 0, sizeof(buffer));
1007 if (count > sizeof(buffer) - 1)
1008 count = sizeof(buffer) - 1;
1009 if (copy_from_user(buffer, buf, count))
1011 oom_adjust = simple_strtol(buffer, &end, 0);
1012 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1013 oom_adjust != OOM_DISABLE)
1017 task = get_proc_task(file->f_path.dentry->d_inode);
1020 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1021 put_task_struct(task);
1024 task->oomkilladj = oom_adjust;
1025 put_task_struct(task);
1026 if (end - buffer == 0)
1028 return end - buffer;
1031 static const struct file_operations proc_oom_adjust_operations = {
1032 .read = oom_adjust_read,
1033 .write = oom_adjust_write,
1036 #ifdef CONFIG_AUDITSYSCALL
1037 #define TMPBUFLEN 21
1038 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1039 size_t count, loff_t *ppos)
1041 struct inode * inode = file->f_path.dentry->d_inode;
1042 struct task_struct *task = get_proc_task(inode);
1044 char tmpbuf[TMPBUFLEN];
1048 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1049 audit_get_loginuid(task));
1050 put_task_struct(task);
1051 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1054 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1055 size_t count, loff_t *ppos)
1057 struct inode * inode = file->f_path.dentry->d_inode;
1062 if (!capable(CAP_AUDIT_CONTROL))
1065 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1068 if (count >= PAGE_SIZE)
1069 count = PAGE_SIZE - 1;
1072 /* No partial writes. */
1075 page = (char*)__get_free_page(GFP_TEMPORARY);
1079 if (copy_from_user(page, buf, count))
1083 loginuid = simple_strtoul(page, &tmp, 10);
1089 length = audit_set_loginuid(current, loginuid);
1090 if (likely(length == 0))
1094 free_page((unsigned long) page);
1098 static const struct file_operations proc_loginuid_operations = {
1099 .read = proc_loginuid_read,
1100 .write = proc_loginuid_write,
1103 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1104 size_t count, loff_t *ppos)
1106 struct inode * inode = file->f_path.dentry->d_inode;
1107 struct task_struct *task = get_proc_task(inode);
1109 char tmpbuf[TMPBUFLEN];
1113 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1114 audit_get_sessionid(task));
1115 put_task_struct(task);
1116 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1119 static const struct file_operations proc_sessionid_operations = {
1120 .read = proc_sessionid_read,
1124 #ifdef CONFIG_FAULT_INJECTION
1125 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1126 size_t count, loff_t *ppos)
1128 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1129 char buffer[PROC_NUMBUF];
1135 make_it_fail = task->make_it_fail;
1136 put_task_struct(task);
1138 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1140 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1143 static ssize_t proc_fault_inject_write(struct file * file,
1144 const char __user * buf, size_t count, loff_t *ppos)
1146 struct task_struct *task;
1147 char buffer[PROC_NUMBUF], *end;
1150 if (!capable(CAP_SYS_RESOURCE))
1152 memset(buffer, 0, sizeof(buffer));
1153 if (count > sizeof(buffer) - 1)
1154 count = sizeof(buffer) - 1;
1155 if (copy_from_user(buffer, buf, count))
1157 make_it_fail = simple_strtol(buffer, &end, 0);
1160 task = get_proc_task(file->f_dentry->d_inode);
1163 task->make_it_fail = make_it_fail;
1164 put_task_struct(task);
1165 if (end - buffer == 0)
1167 return end - buffer;
1170 static const struct file_operations proc_fault_inject_operations = {
1171 .read = proc_fault_inject_read,
1172 .write = proc_fault_inject_write,
1177 #ifdef CONFIG_SCHED_DEBUG
1179 * Print out various scheduling related per-task fields:
1181 static int sched_show(struct seq_file *m, void *v)
1183 struct inode *inode = m->private;
1184 struct task_struct *p;
1186 p = get_proc_task(inode);
1189 proc_sched_show_task(p, m);
1197 sched_write(struct file *file, const char __user *buf,
1198 size_t count, loff_t *offset)
1200 struct inode *inode = file->f_path.dentry->d_inode;
1201 struct task_struct *p;
1203 p = get_proc_task(inode);
1206 proc_sched_set_task(p);
1213 static int sched_open(struct inode *inode, struct file *filp)
1217 ret = single_open(filp, sched_show, NULL);
1219 struct seq_file *m = filp->private_data;
1226 static const struct file_operations proc_pid_sched_operations = {
1229 .write = sched_write,
1230 .llseek = seq_lseek,
1231 .release = single_release,
1237 * We added or removed a vma mapping the executable. The vmas are only mapped
1238 * during exec and are not mapped with the mmap system call.
1239 * Callers must hold down_write() on the mm's mmap_sem for these
1241 void added_exe_file_vma(struct mm_struct *mm)
1243 mm->num_exe_file_vmas++;
1246 void removed_exe_file_vma(struct mm_struct *mm)
1248 mm->num_exe_file_vmas--;
1249 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1251 mm->exe_file = NULL;
1256 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1259 get_file(new_exe_file);
1262 mm->exe_file = new_exe_file;
1263 mm->num_exe_file_vmas = 0;
1266 struct file *get_mm_exe_file(struct mm_struct *mm)
1268 struct file *exe_file;
1270 /* We need mmap_sem to protect against races with removal of
1271 * VM_EXECUTABLE vmas */
1272 down_read(&mm->mmap_sem);
1273 exe_file = mm->exe_file;
1276 up_read(&mm->mmap_sem);
1280 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1282 /* It's safe to write the exe_file pointer without exe_file_lock because
1283 * this is called during fork when the task is not yet in /proc */
1284 newmm->exe_file = get_mm_exe_file(oldmm);
1287 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1289 struct task_struct *task;
1290 struct mm_struct *mm;
1291 struct file *exe_file;
1293 task = get_proc_task(inode);
1296 mm = get_task_mm(task);
1297 put_task_struct(task);
1300 exe_file = get_mm_exe_file(mm);
1303 *exe_path = exe_file->f_path;
1304 path_get(&exe_file->f_path);
1311 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1313 struct inode *inode = dentry->d_inode;
1314 int error = -EACCES;
1316 /* We don't need a base pointer in the /proc filesystem */
1317 path_put(&nd->path);
1319 /* Are we allowed to snoop on the tasks file descriptors? */
1320 if (!proc_fd_access_allowed(inode))
1323 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1324 nd->last_type = LAST_BIND;
1326 return ERR_PTR(error);
1329 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1331 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1338 pathname = d_path(path, tmp, PAGE_SIZE);
1339 len = PTR_ERR(pathname);
1340 if (IS_ERR(pathname))
1342 len = tmp + PAGE_SIZE - 1 - pathname;
1346 if (copy_to_user(buffer, pathname, len))
1349 free_page((unsigned long)tmp);
1353 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1355 int error = -EACCES;
1356 struct inode *inode = dentry->d_inode;
1359 /* Are we allowed to snoop on the tasks file descriptors? */
1360 if (!proc_fd_access_allowed(inode))
1363 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1367 error = do_proc_readlink(&path, buffer, buflen);
1373 static const struct inode_operations proc_pid_link_inode_operations = {
1374 .readlink = proc_pid_readlink,
1375 .follow_link = proc_pid_follow_link,
1376 .setattr = proc_setattr,
1380 /* building an inode */
1382 static int task_dumpable(struct task_struct *task)
1385 struct mm_struct *mm;
1390 dumpable = get_dumpable(mm);
1398 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1400 struct inode * inode;
1401 struct proc_inode *ei;
1402 const struct cred *cred;
1404 /* We need a new inode */
1406 inode = new_inode(sb);
1412 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1413 inode->i_op = &proc_def_inode_operations;
1416 * grab the reference to task.
1418 ei->pid = get_task_pid(task, PIDTYPE_PID);
1424 if (task_dumpable(task)) {
1426 cred = __task_cred(task);
1427 inode->i_uid = cred->euid;
1428 inode->i_gid = cred->egid;
1431 security_task_to_inode(task, inode);
1441 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1443 struct inode *inode = dentry->d_inode;
1444 struct task_struct *task;
1445 const struct cred *cred;
1447 generic_fillattr(inode, stat);
1452 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1454 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1455 task_dumpable(task)) {
1456 cred = __task_cred(task);
1457 stat->uid = cred->euid;
1458 stat->gid = cred->egid;
1468 * Exceptional case: normally we are not allowed to unhash a busy
1469 * directory. In this case, however, we can do it - no aliasing problems
1470 * due to the way we treat inodes.
1472 * Rewrite the inode's ownerships here because the owning task may have
1473 * performed a setuid(), etc.
1475 * Before the /proc/pid/status file was created the only way to read
1476 * the effective uid of a /process was to stat /proc/pid. Reading
1477 * /proc/pid/status is slow enough that procps and other packages
1478 * kept stating /proc/pid. To keep the rules in /proc simple I have
1479 * made this apply to all per process world readable and executable
1482 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1484 struct inode *inode = dentry->d_inode;
1485 struct task_struct *task = get_proc_task(inode);
1486 const struct cred *cred;
1489 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1490 task_dumpable(task)) {
1492 cred = __task_cred(task);
1493 inode->i_uid = cred->euid;
1494 inode->i_gid = cred->egid;
1500 inode->i_mode &= ~(S_ISUID | S_ISGID);
1501 security_task_to_inode(task, inode);
1502 put_task_struct(task);
1509 static int pid_delete_dentry(struct dentry * dentry)
1511 /* Is the task we represent dead?
1512 * If so, then don't put the dentry on the lru list,
1513 * kill it immediately.
1515 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1518 static struct dentry_operations pid_dentry_operations =
1520 .d_revalidate = pid_revalidate,
1521 .d_delete = pid_delete_dentry,
1526 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1527 struct task_struct *, const void *);
1530 * Fill a directory entry.
1532 * If possible create the dcache entry and derive our inode number and
1533 * file type from dcache entry.
1535 * Since all of the proc inode numbers are dynamically generated, the inode
1536 * numbers do not exist until the inode is cache. This means creating the
1537 * the dcache entry in readdir is necessary to keep the inode numbers
1538 * reported by readdir in sync with the inode numbers reported
1541 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1542 char *name, int len,
1543 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1545 struct dentry *child, *dir = filp->f_path.dentry;
1546 struct inode *inode;
1549 unsigned type = DT_UNKNOWN;
1553 qname.hash = full_name_hash(name, len);
1555 child = d_lookup(dir, &qname);
1558 new = d_alloc(dir, &qname);
1560 child = instantiate(dir->d_inode, new, task, ptr);
1567 if (!child || IS_ERR(child) || !child->d_inode)
1568 goto end_instantiate;
1569 inode = child->d_inode;
1572 type = inode->i_mode >> 12;
1577 ino = find_inode_number(dir, &qname);
1580 return filldir(dirent, name, len, filp->f_pos, ino, type);
1583 static unsigned name_to_int(struct dentry *dentry)
1585 const char *name = dentry->d_name.name;
1586 int len = dentry->d_name.len;
1589 if (len > 1 && *name == '0')
1592 unsigned c = *name++ - '0';
1595 if (n >= (~0U-9)/10)
1605 #define PROC_FDINFO_MAX 64
1607 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1609 struct task_struct *task = get_proc_task(inode);
1610 struct files_struct *files = NULL;
1612 int fd = proc_fd(inode);
1615 files = get_files_struct(task);
1616 put_task_struct(task);
1620 * We are not taking a ref to the file structure, so we must
1623 spin_lock(&files->file_lock);
1624 file = fcheck_files(files, fd);
1627 *path = file->f_path;
1628 path_get(&file->f_path);
1631 snprintf(info, PROC_FDINFO_MAX,
1634 (long long) file->f_pos,
1636 spin_unlock(&files->file_lock);
1637 put_files_struct(files);
1640 spin_unlock(&files->file_lock);
1641 put_files_struct(files);
1646 static int proc_fd_link(struct inode *inode, struct path *path)
1648 return proc_fd_info(inode, path, NULL);
1651 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1653 struct inode *inode = dentry->d_inode;
1654 struct task_struct *task = get_proc_task(inode);
1655 int fd = proc_fd(inode);
1656 struct files_struct *files;
1657 const struct cred *cred;
1660 files = get_files_struct(task);
1663 if (fcheck_files(files, fd)) {
1665 put_files_struct(files);
1666 if (task_dumpable(task)) {
1668 cred = __task_cred(task);
1669 inode->i_uid = cred->euid;
1670 inode->i_gid = cred->egid;
1676 inode->i_mode &= ~(S_ISUID | S_ISGID);
1677 security_task_to_inode(task, inode);
1678 put_task_struct(task);
1682 put_files_struct(files);
1684 put_task_struct(task);
1690 static struct dentry_operations tid_fd_dentry_operations =
1692 .d_revalidate = tid_fd_revalidate,
1693 .d_delete = pid_delete_dentry,
1696 static struct dentry *proc_fd_instantiate(struct inode *dir,
1697 struct dentry *dentry, struct task_struct *task, const void *ptr)
1699 unsigned fd = *(const unsigned *)ptr;
1701 struct files_struct *files;
1702 struct inode *inode;
1703 struct proc_inode *ei;
1704 struct dentry *error = ERR_PTR(-ENOENT);
1706 inode = proc_pid_make_inode(dir->i_sb, task);
1711 files = get_files_struct(task);
1714 inode->i_mode = S_IFLNK;
1717 * We are not taking a ref to the file structure, so we must
1720 spin_lock(&files->file_lock);
1721 file = fcheck_files(files, fd);
1724 if (file->f_mode & FMODE_READ)
1725 inode->i_mode |= S_IRUSR | S_IXUSR;
1726 if (file->f_mode & FMODE_WRITE)
1727 inode->i_mode |= S_IWUSR | S_IXUSR;
1728 spin_unlock(&files->file_lock);
1729 put_files_struct(files);
1731 inode->i_op = &proc_pid_link_inode_operations;
1733 ei->op.proc_get_link = proc_fd_link;
1734 dentry->d_op = &tid_fd_dentry_operations;
1735 d_add(dentry, inode);
1736 /* Close the race of the process dying before we return the dentry */
1737 if (tid_fd_revalidate(dentry, NULL))
1743 spin_unlock(&files->file_lock);
1744 put_files_struct(files);
1750 static struct dentry *proc_lookupfd_common(struct inode *dir,
1751 struct dentry *dentry,
1752 instantiate_t instantiate)
1754 struct task_struct *task = get_proc_task(dir);
1755 unsigned fd = name_to_int(dentry);
1756 struct dentry *result = ERR_PTR(-ENOENT);
1763 result = instantiate(dir, dentry, task, &fd);
1765 put_task_struct(task);
1770 static int proc_readfd_common(struct file * filp, void * dirent,
1771 filldir_t filldir, instantiate_t instantiate)
1773 struct dentry *dentry = filp->f_path.dentry;
1774 struct inode *inode = dentry->d_inode;
1775 struct task_struct *p = get_proc_task(inode);
1776 unsigned int fd, ino;
1778 struct files_struct * files;
1788 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1792 ino = parent_ino(dentry);
1793 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1797 files = get_files_struct(p);
1801 for (fd = filp->f_pos-2;
1802 fd < files_fdtable(files)->max_fds;
1803 fd++, filp->f_pos++) {
1804 char name[PROC_NUMBUF];
1807 if (!fcheck_files(files, fd))
1811 len = snprintf(name, sizeof(name), "%d", fd);
1812 if (proc_fill_cache(filp, dirent, filldir,
1813 name, len, instantiate,
1821 put_files_struct(files);
1829 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1830 struct nameidata *nd)
1832 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1835 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1837 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1840 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1841 size_t len, loff_t *ppos)
1843 char tmp[PROC_FDINFO_MAX];
1844 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1846 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1850 static const struct file_operations proc_fdinfo_file_operations = {
1851 .open = nonseekable_open,
1852 .read = proc_fdinfo_read,
1855 static const struct file_operations proc_fd_operations = {
1856 .read = generic_read_dir,
1857 .readdir = proc_readfd,
1861 * /proc/pid/fd needs a special permission handler so that a process can still
1862 * access /proc/self/fd after it has executed a setuid().
1864 static int proc_fd_permission(struct inode *inode, int mask)
1868 rv = generic_permission(inode, mask, NULL);
1871 if (task_pid(current) == proc_pid(inode))
1877 * proc directories can do almost nothing..
1879 static const struct inode_operations proc_fd_inode_operations = {
1880 .lookup = proc_lookupfd,
1881 .permission = proc_fd_permission,
1882 .setattr = proc_setattr,
1885 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1886 struct dentry *dentry, struct task_struct *task, const void *ptr)
1888 unsigned fd = *(unsigned *)ptr;
1889 struct inode *inode;
1890 struct proc_inode *ei;
1891 struct dentry *error = ERR_PTR(-ENOENT);
1893 inode = proc_pid_make_inode(dir->i_sb, task);
1898 inode->i_mode = S_IFREG | S_IRUSR;
1899 inode->i_fop = &proc_fdinfo_file_operations;
1900 dentry->d_op = &tid_fd_dentry_operations;
1901 d_add(dentry, inode);
1902 /* Close the race of the process dying before we return the dentry */
1903 if (tid_fd_revalidate(dentry, NULL))
1910 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1911 struct dentry *dentry,
1912 struct nameidata *nd)
1914 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1917 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1919 return proc_readfd_common(filp, dirent, filldir,
1920 proc_fdinfo_instantiate);
1923 static const struct file_operations proc_fdinfo_operations = {
1924 .read = generic_read_dir,
1925 .readdir = proc_readfdinfo,
1929 * proc directories can do almost nothing..
1931 static const struct inode_operations proc_fdinfo_inode_operations = {
1932 .lookup = proc_lookupfdinfo,
1933 .setattr = proc_setattr,
1937 static struct dentry *proc_pident_instantiate(struct inode *dir,
1938 struct dentry *dentry, struct task_struct *task, const void *ptr)
1940 const struct pid_entry *p = ptr;
1941 struct inode *inode;
1942 struct proc_inode *ei;
1943 struct dentry *error = ERR_PTR(-EINVAL);
1945 inode = proc_pid_make_inode(dir->i_sb, task);
1950 inode->i_mode = p->mode;
1951 if (S_ISDIR(inode->i_mode))
1952 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1954 inode->i_op = p->iop;
1956 inode->i_fop = p->fop;
1958 dentry->d_op = &pid_dentry_operations;
1959 d_add(dentry, inode);
1960 /* Close the race of the process dying before we return the dentry */
1961 if (pid_revalidate(dentry, NULL))
1967 static struct dentry *proc_pident_lookup(struct inode *dir,
1968 struct dentry *dentry,
1969 const struct pid_entry *ents,
1972 struct inode *inode;
1973 struct dentry *error;
1974 struct task_struct *task = get_proc_task(dir);
1975 const struct pid_entry *p, *last;
1977 error = ERR_PTR(-ENOENT);
1984 * Yes, it does not scale. And it should not. Don't add
1985 * new entries into /proc/<tgid>/ without very good reasons.
1987 last = &ents[nents - 1];
1988 for (p = ents; p <= last; p++) {
1989 if (p->len != dentry->d_name.len)
1991 if (!memcmp(dentry->d_name.name, p->name, p->len))
1997 error = proc_pident_instantiate(dir, dentry, task, p);
1999 put_task_struct(task);
2004 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2005 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2007 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2008 proc_pident_instantiate, task, p);
2011 static int proc_pident_readdir(struct file *filp,
2012 void *dirent, filldir_t filldir,
2013 const struct pid_entry *ents, unsigned int nents)
2016 struct dentry *dentry = filp->f_path.dentry;
2017 struct inode *inode = dentry->d_inode;
2018 struct task_struct *task = get_proc_task(inode);
2019 const struct pid_entry *p, *last;
2032 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2038 ino = parent_ino(dentry);
2039 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2051 last = &ents[nents - 1];
2053 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2062 put_task_struct(task);
2067 #ifdef CONFIG_SECURITY
2068 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2069 size_t count, loff_t *ppos)
2071 struct inode * inode = file->f_path.dentry->d_inode;
2074 struct task_struct *task = get_proc_task(inode);
2079 length = security_getprocattr(task,
2080 (char*)file->f_path.dentry->d_name.name,
2082 put_task_struct(task);
2084 length = simple_read_from_buffer(buf, count, ppos, p, length);
2089 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2090 size_t count, loff_t *ppos)
2092 struct inode * inode = file->f_path.dentry->d_inode;
2095 struct task_struct *task = get_proc_task(inode);
2100 if (count > PAGE_SIZE)
2103 /* No partial writes. */
2109 page = (char*)__get_free_page(GFP_TEMPORARY);
2114 if (copy_from_user(page, buf, count))
2117 length = security_setprocattr(task,
2118 (char*)file->f_path.dentry->d_name.name,
2119 (void*)page, count);
2121 free_page((unsigned long) page);
2123 put_task_struct(task);
2128 static const struct file_operations proc_pid_attr_operations = {
2129 .read = proc_pid_attr_read,
2130 .write = proc_pid_attr_write,
2133 static const struct pid_entry attr_dir_stuff[] = {
2134 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2135 REG("prev", S_IRUGO, proc_pid_attr_operations),
2136 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2137 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2138 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2139 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2142 static int proc_attr_dir_readdir(struct file * filp,
2143 void * dirent, filldir_t filldir)
2145 return proc_pident_readdir(filp,dirent,filldir,
2146 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2149 static const struct file_operations proc_attr_dir_operations = {
2150 .read = generic_read_dir,
2151 .readdir = proc_attr_dir_readdir,
2154 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2155 struct dentry *dentry, struct nameidata *nd)
2157 return proc_pident_lookup(dir, dentry,
2158 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2161 static const struct inode_operations proc_attr_dir_inode_operations = {
2162 .lookup = proc_attr_dir_lookup,
2163 .getattr = pid_getattr,
2164 .setattr = proc_setattr,
2169 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2170 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2171 size_t count, loff_t *ppos)
2173 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2174 struct mm_struct *mm;
2175 char buffer[PROC_NUMBUF];
2183 mm = get_task_mm(task);
2185 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2186 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2187 MMF_DUMP_FILTER_SHIFT));
2189 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2192 put_task_struct(task);
2197 static ssize_t proc_coredump_filter_write(struct file *file,
2198 const char __user *buf,
2202 struct task_struct *task;
2203 struct mm_struct *mm;
2204 char buffer[PROC_NUMBUF], *end;
2211 memset(buffer, 0, sizeof(buffer));
2212 if (count > sizeof(buffer) - 1)
2213 count = sizeof(buffer) - 1;
2214 if (copy_from_user(buffer, buf, count))
2218 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2221 if (end - buffer == 0)
2225 task = get_proc_task(file->f_dentry->d_inode);
2230 mm = get_task_mm(task);
2234 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2236 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2238 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2243 put_task_struct(task);
2248 static const struct file_operations proc_coredump_filter_operations = {
2249 .read = proc_coredump_filter_read,
2250 .write = proc_coredump_filter_write,
2257 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2260 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2261 pid_t tgid = task_tgid_nr_ns(current, ns);
2262 char tmp[PROC_NUMBUF];
2265 sprintf(tmp, "%d", tgid);
2266 return vfs_readlink(dentry,buffer,buflen,tmp);
2269 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2271 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2272 pid_t tgid = task_tgid_nr_ns(current, ns);
2273 char tmp[PROC_NUMBUF];
2275 return ERR_PTR(-ENOENT);
2276 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2277 return ERR_PTR(vfs_follow_link(nd,tmp));
2280 static const struct inode_operations proc_self_inode_operations = {
2281 .readlink = proc_self_readlink,
2282 .follow_link = proc_self_follow_link,
2288 * These are the directory entries in the root directory of /proc
2289 * that properly belong to the /proc filesystem, as they describe
2290 * describe something that is process related.
2292 static const struct pid_entry proc_base_stuff[] = {
2293 NOD("self", S_IFLNK|S_IRWXUGO,
2294 &proc_self_inode_operations, NULL, {}),
2298 * Exceptional case: normally we are not allowed to unhash a busy
2299 * directory. In this case, however, we can do it - no aliasing problems
2300 * due to the way we treat inodes.
2302 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2304 struct inode *inode = dentry->d_inode;
2305 struct task_struct *task = get_proc_task(inode);
2307 put_task_struct(task);
2314 static struct dentry_operations proc_base_dentry_operations =
2316 .d_revalidate = proc_base_revalidate,
2317 .d_delete = pid_delete_dentry,
2320 static struct dentry *proc_base_instantiate(struct inode *dir,
2321 struct dentry *dentry, struct task_struct *task, const void *ptr)
2323 const struct pid_entry *p = ptr;
2324 struct inode *inode;
2325 struct proc_inode *ei;
2326 struct dentry *error = ERR_PTR(-EINVAL);
2328 /* Allocate the inode */
2329 error = ERR_PTR(-ENOMEM);
2330 inode = new_inode(dir->i_sb);
2334 /* Initialize the inode */
2336 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2339 * grab the reference to the task.
2341 ei->pid = get_task_pid(task, PIDTYPE_PID);
2347 inode->i_mode = p->mode;
2348 if (S_ISDIR(inode->i_mode))
2350 if (S_ISLNK(inode->i_mode))
2353 inode->i_op = p->iop;
2355 inode->i_fop = p->fop;
2357 dentry->d_op = &proc_base_dentry_operations;
2358 d_add(dentry, inode);
2367 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2369 struct dentry *error;
2370 struct task_struct *task = get_proc_task(dir);
2371 const struct pid_entry *p, *last;
2373 error = ERR_PTR(-ENOENT);
2378 /* Lookup the directory entry */
2379 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2380 for (p = proc_base_stuff; p <= last; p++) {
2381 if (p->len != dentry->d_name.len)
2383 if (!memcmp(dentry->d_name.name, p->name, p->len))
2389 error = proc_base_instantiate(dir, dentry, task, p);
2392 put_task_struct(task);
2397 static int proc_base_fill_cache(struct file *filp, void *dirent,
2398 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2400 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2401 proc_base_instantiate, task, p);
2404 #ifdef CONFIG_TASK_IO_ACCOUNTING
2405 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2407 struct task_io_accounting acct = task->ioac;
2408 unsigned long flags;
2410 if (whole && lock_task_sighand(task, &flags)) {
2411 struct task_struct *t = task;
2413 task_io_accounting_add(&acct, &task->signal->ioac);
2414 while_each_thread(task, t)
2415 task_io_accounting_add(&acct, &t->ioac);
2417 unlock_task_sighand(task, &flags);
2419 return sprintf(buffer,
2424 "read_bytes: %llu\n"
2425 "write_bytes: %llu\n"
2426 "cancelled_write_bytes: %llu\n",
2427 (unsigned long long)acct.rchar,
2428 (unsigned long long)acct.wchar,
2429 (unsigned long long)acct.syscr,
2430 (unsigned long long)acct.syscw,
2431 (unsigned long long)acct.read_bytes,
2432 (unsigned long long)acct.write_bytes,
2433 (unsigned long long)acct.cancelled_write_bytes);
2436 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2438 return do_io_accounting(task, buffer, 0);
2441 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2443 return do_io_accounting(task, buffer, 1);
2445 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2447 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2448 struct pid *pid, struct task_struct *task)
2450 seq_printf(m, "%08x\n", task->personality);
2457 static const struct file_operations proc_task_operations;
2458 static const struct inode_operations proc_task_inode_operations;
2460 static const struct pid_entry tgid_base_stuff[] = {
2461 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2462 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2463 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2465 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2467 REG("environ", S_IRUSR, proc_environ_operations),
2468 INF("auxv", S_IRUSR, proc_pid_auxv),
2469 ONE("status", S_IRUGO, proc_pid_status),
2470 ONE("personality", S_IRUSR, proc_pid_personality),
2471 INF("limits", S_IRUSR, proc_pid_limits),
2472 #ifdef CONFIG_SCHED_DEBUG
2473 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2475 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2476 INF("syscall", S_IRUSR, proc_pid_syscall),
2478 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2479 ONE("stat", S_IRUGO, proc_tgid_stat),
2480 ONE("statm", S_IRUGO, proc_pid_statm),
2481 REG("maps", S_IRUGO, proc_maps_operations),
2483 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2485 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2486 LNK("cwd", proc_cwd_link),
2487 LNK("root", proc_root_link),
2488 LNK("exe", proc_exe_link),
2489 REG("mounts", S_IRUGO, proc_mounts_operations),
2490 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2491 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2492 #ifdef CONFIG_PROC_PAGE_MONITOR
2493 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2494 REG("smaps", S_IRUGO, proc_smaps_operations),
2495 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2497 #ifdef CONFIG_SECURITY
2498 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2500 #ifdef CONFIG_KALLSYMS
2501 INF("wchan", S_IRUGO, proc_pid_wchan),
2503 #ifdef CONFIG_SCHEDSTATS
2504 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2506 #ifdef CONFIG_LATENCYTOP
2507 REG("latency", S_IRUGO, proc_lstats_operations),
2509 #ifdef CONFIG_PROC_PID_CPUSET
2510 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2512 #ifdef CONFIG_CGROUPS
2513 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2515 INF("oom_score", S_IRUGO, proc_oom_score),
2516 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2517 #ifdef CONFIG_AUDITSYSCALL
2518 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2519 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2521 #ifdef CONFIG_FAULT_INJECTION
2522 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2524 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2525 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2527 #ifdef CONFIG_TASK_IO_ACCOUNTING
2528 INF("io", S_IRUGO, proc_tgid_io_accounting),
2532 static int proc_tgid_base_readdir(struct file * filp,
2533 void * dirent, filldir_t filldir)
2535 return proc_pident_readdir(filp,dirent,filldir,
2536 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2539 static const struct file_operations proc_tgid_base_operations = {
2540 .read = generic_read_dir,
2541 .readdir = proc_tgid_base_readdir,
2544 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2545 return proc_pident_lookup(dir, dentry,
2546 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2549 static const struct inode_operations proc_tgid_base_inode_operations = {
2550 .lookup = proc_tgid_base_lookup,
2551 .getattr = pid_getattr,
2552 .setattr = proc_setattr,
2555 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2557 struct dentry *dentry, *leader, *dir;
2558 char buf[PROC_NUMBUF];
2562 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2563 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2565 if (!(current->flags & PF_EXITING))
2566 shrink_dcache_parent(dentry);
2575 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2576 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2581 name.len = strlen(name.name);
2582 dir = d_hash_and_lookup(leader, &name);
2584 goto out_put_leader;
2587 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2588 dentry = d_hash_and_lookup(dir, &name);
2590 shrink_dcache_parent(dentry);
2603 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2604 * @task: task that should be flushed.
2606 * When flushing dentries from proc, one needs to flush them from global
2607 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2608 * in. This call is supposed to do all of this job.
2610 * Looks in the dcache for
2612 * /proc/@tgid/task/@pid
2613 * if either directory is present flushes it and all of it'ts children
2616 * It is safe and reasonable to cache /proc entries for a task until
2617 * that task exits. After that they just clog up the dcache with
2618 * useless entries, possibly causing useful dcache entries to be
2619 * flushed instead. This routine is proved to flush those useless
2620 * dcache entries at process exit time.
2622 * NOTE: This routine is just an optimization so it does not guarantee
2623 * that no dcache entries will exist at process exit time it
2624 * just makes it very unlikely that any will persist.
2627 void proc_flush_task(struct task_struct *task)
2630 struct pid *pid, *tgid = NULL;
2633 pid = task_pid(task);
2634 if (thread_group_leader(task))
2635 tgid = task_tgid(task);
2637 for (i = 0; i <= pid->level; i++) {
2638 upid = &pid->numbers[i];
2639 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2640 tgid ? tgid->numbers[i].nr : 0);
2643 upid = &pid->numbers[pid->level];
2645 pid_ns_release_proc(upid->ns);
2648 static struct dentry *proc_pid_instantiate(struct inode *dir,
2649 struct dentry * dentry,
2650 struct task_struct *task, const void *ptr)
2652 struct dentry *error = ERR_PTR(-ENOENT);
2653 struct inode *inode;
2655 inode = proc_pid_make_inode(dir->i_sb, task);
2659 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2660 inode->i_op = &proc_tgid_base_inode_operations;
2661 inode->i_fop = &proc_tgid_base_operations;
2662 inode->i_flags|=S_IMMUTABLE;
2664 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2665 ARRAY_SIZE(tgid_base_stuff));
2667 dentry->d_op = &pid_dentry_operations;
2669 d_add(dentry, inode);
2670 /* Close the race of the process dying before we return the dentry */
2671 if (pid_revalidate(dentry, NULL))
2677 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2679 struct dentry *result = ERR_PTR(-ENOENT);
2680 struct task_struct *task;
2682 struct pid_namespace *ns;
2684 result = proc_base_lookup(dir, dentry);
2685 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2688 tgid = name_to_int(dentry);
2692 ns = dentry->d_sb->s_fs_info;
2694 task = find_task_by_pid_ns(tgid, ns);
2696 get_task_struct(task);
2701 result = proc_pid_instantiate(dir, dentry, task, NULL);
2702 put_task_struct(task);
2708 * Find the first task with tgid >= tgid
2713 struct task_struct *task;
2715 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2720 put_task_struct(iter.task);
2724 pid = find_ge_pid(iter.tgid, ns);
2726 iter.tgid = pid_nr_ns(pid, ns);
2727 iter.task = pid_task(pid, PIDTYPE_PID);
2728 /* What we to know is if the pid we have find is the
2729 * pid of a thread_group_leader. Testing for task
2730 * being a thread_group_leader is the obvious thing
2731 * todo but there is a window when it fails, due to
2732 * the pid transfer logic in de_thread.
2734 * So we perform the straight forward test of seeing
2735 * if the pid we have found is the pid of a thread
2736 * group leader, and don't worry if the task we have
2737 * found doesn't happen to be a thread group leader.
2738 * As we don't care in the case of readdir.
2740 if (!iter.task || !has_group_leader_pid(iter.task)) {
2744 get_task_struct(iter.task);
2750 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2752 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2753 struct tgid_iter iter)
2755 char name[PROC_NUMBUF];
2756 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2757 return proc_fill_cache(filp, dirent, filldir, name, len,
2758 proc_pid_instantiate, iter.task, NULL);
2761 /* for the /proc/ directory itself, after non-process stuff has been done */
2762 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2764 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2765 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2766 struct tgid_iter iter;
2767 struct pid_namespace *ns;
2772 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2773 const struct pid_entry *p = &proc_base_stuff[nr];
2774 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2778 ns = filp->f_dentry->d_sb->s_fs_info;
2780 iter.tgid = filp->f_pos - TGID_OFFSET;
2781 for (iter = next_tgid(ns, iter);
2783 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2784 filp->f_pos = iter.tgid + TGID_OFFSET;
2785 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2786 put_task_struct(iter.task);
2790 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2792 put_task_struct(reaper);
2800 static const struct pid_entry tid_base_stuff[] = {
2801 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2802 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2803 REG("environ", S_IRUSR, proc_environ_operations),
2804 INF("auxv", S_IRUSR, proc_pid_auxv),
2805 ONE("status", S_IRUGO, proc_pid_status),
2806 ONE("personality", S_IRUSR, proc_pid_personality),
2807 INF("limits", S_IRUSR, proc_pid_limits),
2808 #ifdef CONFIG_SCHED_DEBUG
2809 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2811 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2812 INF("syscall", S_IRUSR, proc_pid_syscall),
2814 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2815 ONE("stat", S_IRUGO, proc_tid_stat),
2816 ONE("statm", S_IRUGO, proc_pid_statm),
2817 REG("maps", S_IRUGO, proc_maps_operations),
2819 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2821 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2822 LNK("cwd", proc_cwd_link),
2823 LNK("root", proc_root_link),
2824 LNK("exe", proc_exe_link),
2825 REG("mounts", S_IRUGO, proc_mounts_operations),
2826 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2827 #ifdef CONFIG_PROC_PAGE_MONITOR
2828 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2829 REG("smaps", S_IRUGO, proc_smaps_operations),
2830 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2832 #ifdef CONFIG_SECURITY
2833 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2835 #ifdef CONFIG_KALLSYMS
2836 INF("wchan", S_IRUGO, proc_pid_wchan),
2838 #ifdef CONFIG_SCHEDSTATS
2839 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2841 #ifdef CONFIG_LATENCYTOP
2842 REG("latency", S_IRUGO, proc_lstats_operations),
2844 #ifdef CONFIG_PROC_PID_CPUSET
2845 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2847 #ifdef CONFIG_CGROUPS
2848 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2850 INF("oom_score", S_IRUGO, proc_oom_score),
2851 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2852 #ifdef CONFIG_AUDITSYSCALL
2853 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2854 REG("sessionid", S_IRUSR, proc_sessionid_operations),
2856 #ifdef CONFIG_FAULT_INJECTION
2857 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2859 #ifdef CONFIG_TASK_IO_ACCOUNTING
2860 INF("io", S_IRUGO, proc_tid_io_accounting),
2864 static int proc_tid_base_readdir(struct file * filp,
2865 void * dirent, filldir_t filldir)
2867 return proc_pident_readdir(filp,dirent,filldir,
2868 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2871 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2872 return proc_pident_lookup(dir, dentry,
2873 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2876 static const struct file_operations proc_tid_base_operations = {
2877 .read = generic_read_dir,
2878 .readdir = proc_tid_base_readdir,
2881 static const struct inode_operations proc_tid_base_inode_operations = {
2882 .lookup = proc_tid_base_lookup,
2883 .getattr = pid_getattr,
2884 .setattr = proc_setattr,
2887 static struct dentry *proc_task_instantiate(struct inode *dir,
2888 struct dentry *dentry, struct task_struct *task, const void *ptr)
2890 struct dentry *error = ERR_PTR(-ENOENT);
2891 struct inode *inode;
2892 inode = proc_pid_make_inode(dir->i_sb, task);
2896 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2897 inode->i_op = &proc_tid_base_inode_operations;
2898 inode->i_fop = &proc_tid_base_operations;
2899 inode->i_flags|=S_IMMUTABLE;
2901 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2902 ARRAY_SIZE(tid_base_stuff));
2904 dentry->d_op = &pid_dentry_operations;
2906 d_add(dentry, inode);
2907 /* Close the race of the process dying before we return the dentry */
2908 if (pid_revalidate(dentry, NULL))
2914 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2916 struct dentry *result = ERR_PTR(-ENOENT);
2917 struct task_struct *task;
2918 struct task_struct *leader = get_proc_task(dir);
2920 struct pid_namespace *ns;
2925 tid = name_to_int(dentry);
2929 ns = dentry->d_sb->s_fs_info;
2931 task = find_task_by_pid_ns(tid, ns);
2933 get_task_struct(task);
2937 if (!same_thread_group(leader, task))
2940 result = proc_task_instantiate(dir, dentry, task, NULL);
2942 put_task_struct(task);
2944 put_task_struct(leader);
2950 * Find the first tid of a thread group to return to user space.
2952 * Usually this is just the thread group leader, but if the users
2953 * buffer was too small or there was a seek into the middle of the
2954 * directory we have more work todo.
2956 * In the case of a short read we start with find_task_by_pid.
2958 * In the case of a seek we start with the leader and walk nr
2961 static struct task_struct *first_tid(struct task_struct *leader,
2962 int tid, int nr, struct pid_namespace *ns)
2964 struct task_struct *pos;
2967 /* Attempt to start with the pid of a thread */
2968 if (tid && (nr > 0)) {
2969 pos = find_task_by_pid_ns(tid, ns);
2970 if (pos && (pos->group_leader == leader))
2974 /* If nr exceeds the number of threads there is nothing todo */
2976 if (nr && nr >= get_nr_threads(leader))
2979 /* If we haven't found our starting place yet start
2980 * with the leader and walk nr threads forward.
2982 for (pos = leader; nr > 0; --nr) {
2983 pos = next_thread(pos);
2984 if (pos == leader) {
2990 get_task_struct(pos);
2997 * Find the next thread in the thread list.
2998 * Return NULL if there is an error or no next thread.
3000 * The reference to the input task_struct is released.
3002 static struct task_struct *next_tid(struct task_struct *start)
3004 struct task_struct *pos = NULL;
3006 if (pid_alive(start)) {
3007 pos = next_thread(start);
3008 if (thread_group_leader(pos))
3011 get_task_struct(pos);
3014 put_task_struct(start);
3018 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3019 struct task_struct *task, int tid)
3021 char name[PROC_NUMBUF];
3022 int len = snprintf(name, sizeof(name), "%d", tid);
3023 return proc_fill_cache(filp, dirent, filldir, name, len,
3024 proc_task_instantiate, task, NULL);
3027 /* for the /proc/TGID/task/ directories */
3028 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3030 struct dentry *dentry = filp->f_path.dentry;
3031 struct inode *inode = dentry->d_inode;
3032 struct task_struct *leader = NULL;
3033 struct task_struct *task;
3034 int retval = -ENOENT;
3037 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
3038 struct pid_namespace *ns;
3040 task = get_proc_task(inode);
3044 if (pid_alive(task)) {
3045 leader = task->group_leader;
3046 get_task_struct(leader);
3049 put_task_struct(task);
3057 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3062 ino = parent_ino(dentry);
3063 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3069 /* f_version caches the tgid value that the last readdir call couldn't
3070 * return. lseek aka telldir automagically resets f_version to 0.
3072 ns = filp->f_dentry->d_sb->s_fs_info;
3073 tid = (int)filp->f_version;
3074 filp->f_version = 0;
3075 for (task = first_tid(leader, tid, pos - 2, ns);
3077 task = next_tid(task), pos++) {
3078 tid = task_pid_nr_ns(task, ns);
3079 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3080 /* returning this tgid failed, save it as the first
3081 * pid for the next readir call */
3082 filp->f_version = (u64)tid;
3083 put_task_struct(task);
3089 put_task_struct(leader);
3094 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3096 struct inode *inode = dentry->d_inode;
3097 struct task_struct *p = get_proc_task(inode);
3098 generic_fillattr(inode, stat);
3101 stat->nlink += get_nr_threads(p);
3108 static const struct inode_operations proc_task_inode_operations = {
3109 .lookup = proc_task_lookup,
3110 .getattr = proc_task_getattr,
3111 .setattr = proc_setattr,
3114 static const struct file_operations proc_task_operations = {
3115 .read = generic_read_dir,
3116 .readdir = proc_task_readdir,