4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/stacktrace.h>
69 #include <linux/resource.h>
70 #include <linux/module.h>
71 #include <linux/mount.h>
72 #include <linux/security.h>
73 #include <linux/ptrace.h>
74 #include <linux/tracehook.h>
75 #include <linux/cgroup.h>
76 #include <linux/cpuset.h>
77 #include <linux/audit.h>
78 #include <linux/poll.h>
79 #include <linux/nsproxy.h>
80 #include <linux/oom.h>
81 #include <linux/elf.h>
82 #include <linux/pid_namespace.h>
83 #include <linux/fs_struct.h>
87 * Implementing inode permission operations in /proc is almost
88 * certainly an error. Permission checks need to happen during
89 * each system call not at open time. The reason is that most of
90 * what we wish to check for permissions in /proc varies at runtime.
92 * The classic example of a problem is opening file descriptors
93 * in /proc for a task before it execs a suid executable.
100 const struct inode_operations *iop;
101 const struct file_operations *fop;
105 #define NOD(NAME, MODE, IOP, FOP, OP) { \
107 .len = sizeof(NAME) - 1, \
114 #define DIR(NAME, MODE, iops, fops) \
115 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
116 #define LNK(NAME, get_link) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = get_link } )
120 #define REG(NAME, MODE, fops) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
122 #define INF(NAME, MODE, read) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = read } )
126 #define ONE(NAME, MODE, show) \
127 NOD(NAME, (S_IFREG|(MODE)), \
128 NULL, &proc_single_file_operations, \
129 { .proc_show = show } )
131 static ssize_t proc_info_read(struct file * file, char __user * buf,
132 size_t count, loff_t *ppos);
134 * Count the number of hardlinks for the pid_entry table, excluding the .
137 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
144 for (i = 0; i < n; ++i) {
145 if (S_ISDIR(entries[i].mode))
152 static int get_fs_path(struct task_struct *task, struct path *path, bool root)
154 struct fs_struct *fs;
155 int result = -ENOENT;
160 read_lock(&fs->lock);
161 *path = root ? fs->root : fs->pwd;
163 read_unlock(&fs->lock);
170 static int get_nr_threads(struct task_struct *tsk)
175 if (lock_task_sighand(tsk, &flags)) {
176 count = atomic_read(&tsk->signal->count);
177 unlock_task_sighand(tsk, &flags);
182 static int proc_cwd_link(struct inode *inode, struct path *path)
184 struct task_struct *task = get_proc_task(inode);
185 int result = -ENOENT;
188 result = get_fs_path(task, path, 0);
189 put_task_struct(task);
194 static int proc_root_link(struct inode *inode, struct path *path)
196 struct task_struct *task = get_proc_task(inode);
197 int result = -ENOENT;
200 result = get_fs_path(task, path, 1);
201 put_task_struct(task);
207 * Return zero if current may access user memory in @task, -error if not.
209 static int check_mem_permission(struct task_struct *task)
212 * A task can always look at itself, in case it chooses
213 * to use system calls instead of load instructions.
219 * If current is actively ptrace'ing, and would also be
220 * permitted to freshly attach with ptrace now, permit it.
222 if (task_is_stopped_or_traced(task)) {
225 match = (tracehook_tracer_task(task) == current);
227 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
232 * Noone else is allowed.
237 struct mm_struct *mm_for_maps(struct task_struct *task)
239 struct mm_struct *mm;
241 if (mutex_lock_killable(&task->cred_guard_mutex))
244 mm = get_task_mm(task);
245 if (mm && mm != current->mm &&
246 !ptrace_may_access(task, PTRACE_MODE_READ)) {
250 mutex_unlock(&task->cred_guard_mutex);
255 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
259 struct mm_struct *mm = get_task_mm(task);
263 goto out_mm; /* Shh! No looking before we're done */
265 len = mm->arg_end - mm->arg_start;
270 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
272 // If the nul at the end of args has been overwritten, then
273 // assume application is using setproctitle(3).
274 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
275 len = strnlen(buffer, res);
279 len = mm->env_end - mm->env_start;
280 if (len > PAGE_SIZE - res)
281 len = PAGE_SIZE - res;
282 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
283 res = strnlen(buffer, res);
292 static int proc_pid_auxv(struct task_struct *task, char *buffer)
295 struct mm_struct *mm = get_task_mm(task);
297 unsigned int nwords = 0;
300 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
301 res = nwords * sizeof(mm->saved_auxv[0]);
304 memcpy(buffer, mm->saved_auxv, res);
311 #ifdef CONFIG_KALLSYMS
313 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
314 * Returns the resolved symbol. If that fails, simply return the address.
316 static int proc_pid_wchan(struct task_struct *task, char *buffer)
319 char symname[KSYM_NAME_LEN];
321 wchan = get_wchan(task);
323 if (lookup_symbol_name(wchan, symname) < 0)
324 if (!ptrace_may_access(task, PTRACE_MODE_READ))
327 return sprintf(buffer, "%lu", wchan);
329 return sprintf(buffer, "%s", symname);
331 #endif /* CONFIG_KALLSYMS */
333 #ifdef CONFIG_STACKTRACE
335 #define MAX_STACK_TRACE_DEPTH 64
337 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
338 struct pid *pid, struct task_struct *task)
340 struct stack_trace trace;
341 unsigned long *entries;
344 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
348 trace.nr_entries = 0;
349 trace.max_entries = MAX_STACK_TRACE_DEPTH;
350 trace.entries = entries;
352 save_stack_trace_tsk(task, &trace);
354 for (i = 0; i < trace.nr_entries; i++) {
355 seq_printf(m, "[<%p>] %pS\n",
356 (void *)entries[i], (void *)entries[i]);
364 #ifdef CONFIG_SCHEDSTATS
366 * Provides /proc/PID/schedstat
368 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
370 return sprintf(buffer, "%llu %llu %lu\n",
371 (unsigned long long)task->se.sum_exec_runtime,
372 (unsigned long long)task->sched_info.run_delay,
373 task->sched_info.pcount);
377 #ifdef CONFIG_LATENCYTOP
378 static int lstats_show_proc(struct seq_file *m, void *v)
381 struct inode *inode = m->private;
382 struct task_struct *task = get_proc_task(inode);
386 seq_puts(m, "Latency Top version : v0.1\n");
387 for (i = 0; i < 32; i++) {
388 if (task->latency_record[i].backtrace[0]) {
390 seq_printf(m, "%i %li %li ",
391 task->latency_record[i].count,
392 task->latency_record[i].time,
393 task->latency_record[i].max);
394 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
395 char sym[KSYM_SYMBOL_LEN];
397 if (!task->latency_record[i].backtrace[q])
399 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
401 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
402 c = strchr(sym, '+');
405 seq_printf(m, "%s ", sym);
411 put_task_struct(task);
415 static int lstats_open(struct inode *inode, struct file *file)
417 return single_open(file, lstats_show_proc, inode);
420 static ssize_t lstats_write(struct file *file, const char __user *buf,
421 size_t count, loff_t *offs)
423 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
427 clear_all_latency_tracing(task);
428 put_task_struct(task);
433 static const struct file_operations proc_lstats_operations = {
436 .write = lstats_write,
438 .release = single_release,
443 /* The badness from the OOM killer */
444 unsigned long badness(struct task_struct *p, unsigned long uptime);
445 static int proc_oom_score(struct task_struct *task, char *buffer)
447 unsigned long points;
448 struct timespec uptime;
450 do_posix_clock_monotonic_gettime(&uptime);
451 read_lock(&tasklist_lock);
452 points = badness(task->group_leader, uptime.tv_sec);
453 read_unlock(&tasklist_lock);
454 return sprintf(buffer, "%lu\n", points);
462 static const struct limit_names lnames[RLIM_NLIMITS] = {
463 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
464 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
465 [RLIMIT_DATA] = {"Max data size", "bytes"},
466 [RLIMIT_STACK] = {"Max stack size", "bytes"},
467 [RLIMIT_CORE] = {"Max core file size", "bytes"},
468 [RLIMIT_RSS] = {"Max resident set", "bytes"},
469 [RLIMIT_NPROC] = {"Max processes", "processes"},
470 [RLIMIT_NOFILE] = {"Max open files", "files"},
471 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
472 [RLIMIT_AS] = {"Max address space", "bytes"},
473 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
474 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
475 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
476 [RLIMIT_NICE] = {"Max nice priority", NULL},
477 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
478 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
481 /* Display limits for a process */
482 static int proc_pid_limits(struct task_struct *task, char *buffer)
487 char *bufptr = buffer;
489 struct rlimit rlim[RLIM_NLIMITS];
491 if (!lock_task_sighand(task, &flags))
493 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
494 unlock_task_sighand(task, &flags);
497 * print the file header
499 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
500 "Limit", "Soft Limit", "Hard Limit", "Units");
502 for (i = 0; i < RLIM_NLIMITS; i++) {
503 if (rlim[i].rlim_cur == RLIM_INFINITY)
504 count += sprintf(&bufptr[count], "%-25s %-20s ",
505 lnames[i].name, "unlimited");
507 count += sprintf(&bufptr[count], "%-25s %-20lu ",
508 lnames[i].name, rlim[i].rlim_cur);
510 if (rlim[i].rlim_max == RLIM_INFINITY)
511 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
513 count += sprintf(&bufptr[count], "%-20lu ",
517 count += sprintf(&bufptr[count], "%-10s\n",
520 count += sprintf(&bufptr[count], "\n");
526 static ssize_t limits_write(struct file *file, const char __user *buf,
527 size_t count, loff_t *ppos)
529 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
530 char str[32 + 1 + 16 + 1 + 16 + 1], *delim, *next;
531 struct rlimit new_rlimit;
539 if (copy_from_user(str, buf, min(count, sizeof(str) - 1))) {
544 str[min(count, sizeof(str) - 1)] = 0;
546 delim = strchr(str, '=');
551 *delim++ = 0; /* for easy 'str' usage */
552 new_rlimit.rlim_cur = simple_strtoul(delim, &next, 0);
554 if (strncmp(delim, "unlimited:", 10)) {
558 new_rlimit.rlim_cur = RLIM_INFINITY;
559 next = delim + 9; /* move to ':' */
562 new_rlimit.rlim_max = simple_strtoul(delim, &next, 0);
564 if (strcmp(delim, "unlimited")) {
568 new_rlimit.rlim_max = RLIM_INFINITY;
571 for (i = 0; i < RLIM_NLIMITS; i++)
572 if (!strcmp(str, lnames[i].name))
574 if (i >= RLIM_NLIMITS) {
579 ret = do_setrlimit(task, i, &new_rlimit);
584 put_task_struct(task);
589 static const struct file_operations proc_pid_limits_operations = {
590 .read = proc_info_read,
591 .write = limits_write,
594 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
595 static int proc_pid_syscall(struct task_struct *task, char *buffer)
598 unsigned long args[6], sp, pc;
600 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
601 return sprintf(buffer, "running\n");
604 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
606 return sprintf(buffer,
607 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
609 args[0], args[1], args[2], args[3], args[4], args[5],
612 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
614 /************************************************************************/
615 /* Here the fs part begins */
616 /************************************************************************/
618 /* permission checks */
619 static int proc_fd_access_allowed(struct inode *inode)
621 struct task_struct *task;
623 /* Allow access to a task's file descriptors if it is us or we
624 * may use ptrace attach to the process and find out that
627 task = get_proc_task(inode);
629 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
630 put_task_struct(task);
635 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
638 struct inode *inode = dentry->d_inode;
640 if (attr->ia_valid & ATTR_MODE)
643 error = inode_change_ok(inode, attr);
645 error = inode_setattr(inode, attr);
649 static const struct inode_operations proc_def_inode_operations = {
650 .setattr = proc_setattr,
653 static int mounts_open_common(struct inode *inode, struct file *file,
654 const struct seq_operations *op)
656 struct task_struct *task = get_proc_task(inode);
658 struct mnt_namespace *ns = NULL;
660 struct proc_mounts *p;
665 nsp = task_nsproxy(task);
672 if (ns && get_fs_path(task, &root, 1) == 0)
674 put_task_struct(task);
683 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
687 file->private_data = &p->m;
688 ret = seq_open(file, op);
695 p->event = ns->event;
709 static int mounts_release(struct inode *inode, struct file *file)
711 struct proc_mounts *p = file->private_data;
714 return seq_release(inode, file);
717 static unsigned mounts_poll(struct file *file, poll_table *wait)
719 struct proc_mounts *p = file->private_data;
720 struct mnt_namespace *ns = p->ns;
721 unsigned res = POLLIN | POLLRDNORM;
723 poll_wait(file, &ns->poll, wait);
725 spin_lock(&vfsmount_lock);
726 if (p->event != ns->event) {
727 p->event = ns->event;
728 res |= POLLERR | POLLPRI;
730 spin_unlock(&vfsmount_lock);
735 static int mounts_open(struct inode *inode, struct file *file)
737 return mounts_open_common(inode, file, &mounts_op);
740 static const struct file_operations proc_mounts_operations = {
744 .release = mounts_release,
748 static int mountinfo_open(struct inode *inode, struct file *file)
750 return mounts_open_common(inode, file, &mountinfo_op);
753 static const struct file_operations proc_mountinfo_operations = {
754 .open = mountinfo_open,
757 .release = mounts_release,
761 static int mountstats_open(struct inode *inode, struct file *file)
763 return mounts_open_common(inode, file, &mountstats_op);
766 static const struct file_operations proc_mountstats_operations = {
767 .open = mountstats_open,
770 .release = mounts_release,
773 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
775 static ssize_t proc_info_read(struct file * file, char __user * buf,
776 size_t count, loff_t *ppos)
778 struct inode * inode = file->f_path.dentry->d_inode;
781 struct task_struct *task = get_proc_task(inode);
787 if (count > PROC_BLOCK_SIZE)
788 count = PROC_BLOCK_SIZE;
791 if (!(page = __get_free_page(GFP_TEMPORARY)))
794 length = PROC_I(inode)->op.proc_read(task, (char*)page);
797 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
800 put_task_struct(task);
805 static const struct file_operations proc_info_file_operations = {
806 .read = proc_info_read,
809 static int proc_single_show(struct seq_file *m, void *v)
811 struct inode *inode = m->private;
812 struct pid_namespace *ns;
814 struct task_struct *task;
817 ns = inode->i_sb->s_fs_info;
818 pid = proc_pid(inode);
819 task = get_pid_task(pid, PIDTYPE_PID);
823 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
825 put_task_struct(task);
829 static int proc_single_open(struct inode *inode, struct file *filp)
832 ret = single_open(filp, proc_single_show, NULL);
834 struct seq_file *m = filp->private_data;
841 static const struct file_operations proc_single_file_operations = {
842 .open = proc_single_open,
845 .release = single_release,
848 static int mem_open(struct inode* inode, struct file* file)
850 file->private_data = (void*)((long)current->self_exec_id);
854 static ssize_t mem_read(struct file * file, char __user * buf,
855 size_t count, loff_t *ppos)
857 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
859 unsigned long src = *ppos;
861 struct mm_struct *mm;
866 if (check_mem_permission(task))
870 page = (char *)__get_free_page(GFP_TEMPORARY);
876 mm = get_task_mm(task);
882 if (file->private_data != (void*)((long)current->self_exec_id))
888 int this_len, retval;
890 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
891 retval = access_process_vm(task, src, page, this_len, 0);
892 if (!retval || check_mem_permission(task)) {
898 if (copy_to_user(buf, page, retval)) {
913 free_page((unsigned long) page);
915 put_task_struct(task);
920 #define mem_write NULL
923 /* This is a security hazard */
924 static ssize_t mem_write(struct file * file, const char __user *buf,
925 size_t count, loff_t *ppos)
929 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
930 unsigned long dst = *ppos;
936 if (check_mem_permission(task))
940 page = (char *)__get_free_page(GFP_TEMPORARY);
946 int this_len, retval;
948 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
949 if (copy_from_user(page, buf, this_len)) {
953 retval = access_process_vm(task, dst, page, this_len, 1);
965 free_page((unsigned long) page);
967 put_task_struct(task);
973 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
977 file->f_pos = offset;
980 file->f_pos += offset;
985 force_successful_syscall_return();
989 static const struct file_operations proc_mem_operations = {
996 static ssize_t environ_read(struct file *file, char __user *buf,
997 size_t count, loff_t *ppos)
999 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1001 unsigned long src = *ppos;
1003 struct mm_struct *mm;
1008 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1012 page = (char *)__get_free_page(GFP_TEMPORARY);
1018 mm = get_task_mm(task);
1023 int this_len, retval, max_len;
1025 this_len = mm->env_end - (mm->env_start + src);
1030 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
1031 this_len = (this_len > max_len) ? max_len : this_len;
1033 retval = access_process_vm(task, (mm->env_start + src),
1041 if (copy_to_user(buf, page, retval)) {
1055 free_page((unsigned long) page);
1057 put_task_struct(task);
1062 static const struct file_operations proc_environ_operations = {
1063 .read = environ_read,
1066 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1067 size_t count, loff_t *ppos)
1069 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1070 char buffer[PROC_NUMBUF];
1072 int oom_adjust = OOM_DISABLE;
1073 unsigned long flags;
1078 if (lock_task_sighand(task, &flags)) {
1079 oom_adjust = task->signal->oom_adj;
1080 unlock_task_sighand(task, &flags);
1083 put_task_struct(task);
1085 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1087 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1090 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1091 size_t count, loff_t *ppos)
1093 struct task_struct *task;
1094 char buffer[PROC_NUMBUF];
1096 unsigned long flags;
1099 memset(buffer, 0, sizeof(buffer));
1100 if (count > sizeof(buffer) - 1)
1101 count = sizeof(buffer) - 1;
1102 if (copy_from_user(buffer, buf, count))
1105 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1108 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1109 oom_adjust != OOM_DISABLE)
1112 task = get_proc_task(file->f_path.dentry->d_inode);
1115 if (!lock_task_sighand(task, &flags)) {
1116 put_task_struct(task);
1120 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1121 unlock_task_sighand(task, &flags);
1122 put_task_struct(task);
1126 task->signal->oom_adj = oom_adjust;
1128 unlock_task_sighand(task, &flags);
1129 put_task_struct(task);
1134 static const struct file_operations proc_oom_adjust_operations = {
1135 .read = oom_adjust_read,
1136 .write = oom_adjust_write,
1139 #ifdef CONFIG_AUDITSYSCALL
1140 #define TMPBUFLEN 21
1141 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1142 size_t count, loff_t *ppos)
1144 struct inode * inode = file->f_path.dentry->d_inode;
1145 struct task_struct *task = get_proc_task(inode);
1147 char tmpbuf[TMPBUFLEN];
1151 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1152 audit_get_loginuid(task));
1153 put_task_struct(task);
1154 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1157 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1158 size_t count, loff_t *ppos)
1160 struct inode * inode = file->f_path.dentry->d_inode;
1165 if (!capable(CAP_AUDIT_CONTROL))
1168 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1171 if (count >= PAGE_SIZE)
1172 count = PAGE_SIZE - 1;
1175 /* No partial writes. */
1178 page = (char*)__get_free_page(GFP_TEMPORARY);
1182 if (copy_from_user(page, buf, count))
1186 loginuid = simple_strtoul(page, &tmp, 10);
1192 length = audit_set_loginuid(current, loginuid);
1193 if (likely(length == 0))
1197 free_page((unsigned long) page);
1201 static const struct file_operations proc_loginuid_operations = {
1202 .read = proc_loginuid_read,
1203 .write = proc_loginuid_write,
1206 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1207 size_t count, loff_t *ppos)
1209 struct inode * inode = file->f_path.dentry->d_inode;
1210 struct task_struct *task = get_proc_task(inode);
1212 char tmpbuf[TMPBUFLEN];
1216 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1217 audit_get_sessionid(task));
1218 put_task_struct(task);
1219 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1222 static const struct file_operations proc_sessionid_operations = {
1223 .read = proc_sessionid_read,
1227 #ifdef CONFIG_FAULT_INJECTION
1228 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1229 size_t count, loff_t *ppos)
1231 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1232 char buffer[PROC_NUMBUF];
1238 make_it_fail = task->make_it_fail;
1239 put_task_struct(task);
1241 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1243 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1246 static ssize_t proc_fault_inject_write(struct file * file,
1247 const char __user * buf, size_t count, loff_t *ppos)
1249 struct task_struct *task;
1250 char buffer[PROC_NUMBUF], *end;
1253 if (!capable(CAP_SYS_RESOURCE))
1255 memset(buffer, 0, sizeof(buffer));
1256 if (count > sizeof(buffer) - 1)
1257 count = sizeof(buffer) - 1;
1258 if (copy_from_user(buffer, buf, count))
1260 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1263 task = get_proc_task(file->f_dentry->d_inode);
1266 task->make_it_fail = make_it_fail;
1267 put_task_struct(task);
1272 static const struct file_operations proc_fault_inject_operations = {
1273 .read = proc_fault_inject_read,
1274 .write = proc_fault_inject_write,
1279 #ifdef CONFIG_SCHED_DEBUG
1281 * Print out various scheduling related per-task fields:
1283 static int sched_show(struct seq_file *m, void *v)
1285 struct inode *inode = m->private;
1286 struct task_struct *p;
1288 p = get_proc_task(inode);
1291 proc_sched_show_task(p, m);
1299 sched_write(struct file *file, const char __user *buf,
1300 size_t count, loff_t *offset)
1302 struct inode *inode = file->f_path.dentry->d_inode;
1303 struct task_struct *p;
1305 p = get_proc_task(inode);
1308 proc_sched_set_task(p);
1315 static int sched_open(struct inode *inode, struct file *filp)
1319 ret = single_open(filp, sched_show, NULL);
1321 struct seq_file *m = filp->private_data;
1328 static const struct file_operations proc_pid_sched_operations = {
1331 .write = sched_write,
1332 .llseek = seq_lseek,
1333 .release = single_release,
1339 * We added or removed a vma mapping the executable. The vmas are only mapped
1340 * during exec and are not mapped with the mmap system call.
1341 * Callers must hold down_write() on the mm's mmap_sem for these
1343 void added_exe_file_vma(struct mm_struct *mm)
1345 mm->num_exe_file_vmas++;
1348 void removed_exe_file_vma(struct mm_struct *mm)
1350 mm->num_exe_file_vmas--;
1351 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1353 mm->exe_file = NULL;
1358 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1361 get_file(new_exe_file);
1364 mm->exe_file = new_exe_file;
1365 mm->num_exe_file_vmas = 0;
1368 struct file *get_mm_exe_file(struct mm_struct *mm)
1370 struct file *exe_file;
1372 /* We need mmap_sem to protect against races with removal of
1373 * VM_EXECUTABLE vmas */
1374 down_read(&mm->mmap_sem);
1375 exe_file = mm->exe_file;
1378 up_read(&mm->mmap_sem);
1382 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1384 /* It's safe to write the exe_file pointer without exe_file_lock because
1385 * this is called during fork when the task is not yet in /proc */
1386 newmm->exe_file = get_mm_exe_file(oldmm);
1389 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1391 struct task_struct *task;
1392 struct mm_struct *mm;
1393 struct file *exe_file;
1395 task = get_proc_task(inode);
1398 mm = get_task_mm(task);
1399 put_task_struct(task);
1402 exe_file = get_mm_exe_file(mm);
1405 *exe_path = exe_file->f_path;
1406 path_get(&exe_file->f_path);
1413 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1415 struct inode *inode = dentry->d_inode;
1416 int error = -EACCES;
1418 /* We don't need a base pointer in the /proc filesystem */
1419 path_put(&nd->path);
1421 /* Are we allowed to snoop on the tasks file descriptors? */
1422 if (!proc_fd_access_allowed(inode))
1425 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1426 nd->last_type = LAST_BIND;
1428 return ERR_PTR(error);
1431 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1433 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1440 pathname = d_path(path, tmp, PAGE_SIZE);
1441 len = PTR_ERR(pathname);
1442 if (IS_ERR(pathname))
1444 len = tmp + PAGE_SIZE - 1 - pathname;
1448 if (copy_to_user(buffer, pathname, len))
1451 free_page((unsigned long)tmp);
1455 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1457 int error = -EACCES;
1458 struct inode *inode = dentry->d_inode;
1461 /* Are we allowed to snoop on the tasks file descriptors? */
1462 if (!proc_fd_access_allowed(inode))
1465 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1469 error = do_proc_readlink(&path, buffer, buflen);
1475 static const struct inode_operations proc_pid_link_inode_operations = {
1476 .readlink = proc_pid_readlink,
1477 .follow_link = proc_pid_follow_link,
1478 .setattr = proc_setattr,
1482 /* building an inode */
1484 static int task_dumpable(struct task_struct *task)
1487 struct mm_struct *mm;
1492 dumpable = get_dumpable(mm);
1500 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1502 struct inode * inode;
1503 struct proc_inode *ei;
1504 const struct cred *cred;
1506 /* We need a new inode */
1508 inode = new_inode(sb);
1514 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1515 inode->i_op = &proc_def_inode_operations;
1518 * grab the reference to task.
1520 ei->pid = get_task_pid(task, PIDTYPE_PID);
1524 if (task_dumpable(task)) {
1526 cred = __task_cred(task);
1527 inode->i_uid = cred->euid;
1528 inode->i_gid = cred->egid;
1531 security_task_to_inode(task, inode);
1541 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1543 struct inode *inode = dentry->d_inode;
1544 struct task_struct *task;
1545 const struct cred *cred;
1547 generic_fillattr(inode, stat);
1552 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1554 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1555 task_dumpable(task)) {
1556 cred = __task_cred(task);
1557 stat->uid = cred->euid;
1558 stat->gid = cred->egid;
1568 * Exceptional case: normally we are not allowed to unhash a busy
1569 * directory. In this case, however, we can do it - no aliasing problems
1570 * due to the way we treat inodes.
1572 * Rewrite the inode's ownerships here because the owning task may have
1573 * performed a setuid(), etc.
1575 * Before the /proc/pid/status file was created the only way to read
1576 * the effective uid of a /process was to stat /proc/pid. Reading
1577 * /proc/pid/status is slow enough that procps and other packages
1578 * kept stating /proc/pid. To keep the rules in /proc simple I have
1579 * made this apply to all per process world readable and executable
1582 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1584 struct inode *inode = dentry->d_inode;
1585 struct task_struct *task = get_proc_task(inode);
1586 const struct cred *cred;
1589 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1590 task_dumpable(task)) {
1592 cred = __task_cred(task);
1593 inode->i_uid = cred->euid;
1594 inode->i_gid = cred->egid;
1600 inode->i_mode &= ~(S_ISUID | S_ISGID);
1601 security_task_to_inode(task, inode);
1602 put_task_struct(task);
1609 static int pid_delete_dentry(struct dentry * dentry)
1611 /* Is the task we represent dead?
1612 * If so, then don't put the dentry on the lru list,
1613 * kill it immediately.
1615 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1618 static const struct dentry_operations pid_dentry_operations =
1620 .d_revalidate = pid_revalidate,
1621 .d_delete = pid_delete_dentry,
1626 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1627 struct task_struct *, const void *);
1630 * Fill a directory entry.
1632 * If possible create the dcache entry and derive our inode number and
1633 * file type from dcache entry.
1635 * Since all of the proc inode numbers are dynamically generated, the inode
1636 * numbers do not exist until the inode is cache. This means creating the
1637 * the dcache entry in readdir is necessary to keep the inode numbers
1638 * reported by readdir in sync with the inode numbers reported
1641 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1642 char *name, int len,
1643 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1645 struct dentry *child, *dir = filp->f_path.dentry;
1646 struct inode *inode;
1649 unsigned type = DT_UNKNOWN;
1653 qname.hash = full_name_hash(name, len);
1655 child = d_lookup(dir, &qname);
1658 new = d_alloc(dir, &qname);
1660 child = instantiate(dir->d_inode, new, task, ptr);
1667 if (!child || IS_ERR(child) || !child->d_inode)
1668 goto end_instantiate;
1669 inode = child->d_inode;
1672 type = inode->i_mode >> 12;
1677 ino = find_inode_number(dir, &qname);
1680 return filldir(dirent, name, len, filp->f_pos, ino, type);
1683 static unsigned name_to_int(struct dentry *dentry)
1685 const char *name = dentry->d_name.name;
1686 int len = dentry->d_name.len;
1689 if (len > 1 && *name == '0')
1692 unsigned c = *name++ - '0';
1695 if (n >= (~0U-9)/10)
1705 #define PROC_FDINFO_MAX 64
1707 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1709 struct task_struct *task = get_proc_task(inode);
1710 struct files_struct *files = NULL;
1712 int fd = proc_fd(inode);
1715 files = get_files_struct(task);
1716 put_task_struct(task);
1720 * We are not taking a ref to the file structure, so we must
1723 spin_lock(&files->file_lock);
1724 file = fcheck_files(files, fd);
1727 *path = file->f_path;
1728 path_get(&file->f_path);
1731 snprintf(info, PROC_FDINFO_MAX,
1734 (long long) file->f_pos,
1736 spin_unlock(&files->file_lock);
1737 put_files_struct(files);
1740 spin_unlock(&files->file_lock);
1741 put_files_struct(files);
1746 static int proc_fd_link(struct inode *inode, struct path *path)
1748 return proc_fd_info(inode, path, NULL);
1751 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1753 struct inode *inode = dentry->d_inode;
1754 struct task_struct *task = get_proc_task(inode);
1755 int fd = proc_fd(inode);
1756 struct files_struct *files;
1757 const struct cred *cred;
1760 files = get_files_struct(task);
1763 if (fcheck_files(files, fd)) {
1765 put_files_struct(files);
1766 if (task_dumpable(task)) {
1768 cred = __task_cred(task);
1769 inode->i_uid = cred->euid;
1770 inode->i_gid = cred->egid;
1776 inode->i_mode &= ~(S_ISUID | S_ISGID);
1777 security_task_to_inode(task, inode);
1778 put_task_struct(task);
1782 put_files_struct(files);
1784 put_task_struct(task);
1790 static const struct dentry_operations tid_fd_dentry_operations =
1792 .d_revalidate = tid_fd_revalidate,
1793 .d_delete = pid_delete_dentry,
1796 static struct dentry *proc_fd_instantiate(struct inode *dir,
1797 struct dentry *dentry, struct task_struct *task, const void *ptr)
1799 unsigned fd = *(const unsigned *)ptr;
1801 struct files_struct *files;
1802 struct inode *inode;
1803 struct proc_inode *ei;
1804 struct dentry *error = ERR_PTR(-ENOENT);
1806 inode = proc_pid_make_inode(dir->i_sb, task);
1811 files = get_files_struct(task);
1814 inode->i_mode = S_IFLNK;
1817 * We are not taking a ref to the file structure, so we must
1820 spin_lock(&files->file_lock);
1821 file = fcheck_files(files, fd);
1824 if (file->f_mode & FMODE_READ)
1825 inode->i_mode |= S_IRUSR | S_IXUSR;
1826 if (file->f_mode & FMODE_WRITE)
1827 inode->i_mode |= S_IWUSR | S_IXUSR;
1828 spin_unlock(&files->file_lock);
1829 put_files_struct(files);
1831 inode->i_op = &proc_pid_link_inode_operations;
1833 ei->op.proc_get_link = proc_fd_link;
1834 dentry->d_op = &tid_fd_dentry_operations;
1835 d_add(dentry, inode);
1836 /* Close the race of the process dying before we return the dentry */
1837 if (tid_fd_revalidate(dentry, NULL))
1843 spin_unlock(&files->file_lock);
1844 put_files_struct(files);
1850 static struct dentry *proc_lookupfd_common(struct inode *dir,
1851 struct dentry *dentry,
1852 instantiate_t instantiate)
1854 struct task_struct *task = get_proc_task(dir);
1855 unsigned fd = name_to_int(dentry);
1856 struct dentry *result = ERR_PTR(-ENOENT);
1863 result = instantiate(dir, dentry, task, &fd);
1865 put_task_struct(task);
1870 static int proc_readfd_common(struct file * filp, void * dirent,
1871 filldir_t filldir, instantiate_t instantiate)
1873 struct dentry *dentry = filp->f_path.dentry;
1874 struct inode *inode = dentry->d_inode;
1875 struct task_struct *p = get_proc_task(inode);
1876 unsigned int fd, ino;
1878 struct files_struct * files;
1888 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1892 ino = parent_ino(dentry);
1893 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1897 files = get_files_struct(p);
1901 for (fd = filp->f_pos-2;
1902 fd < files_fdtable(files)->max_fds;
1903 fd++, filp->f_pos++) {
1904 char name[PROC_NUMBUF];
1907 if (!fcheck_files(files, fd))
1911 len = snprintf(name, sizeof(name), "%d", fd);
1912 if (proc_fill_cache(filp, dirent, filldir,
1913 name, len, instantiate,
1921 put_files_struct(files);
1929 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1930 struct nameidata *nd)
1932 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1935 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1937 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1940 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1941 size_t len, loff_t *ppos)
1943 char tmp[PROC_FDINFO_MAX];
1944 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1946 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1950 static const struct file_operations proc_fdinfo_file_operations = {
1951 .open = nonseekable_open,
1952 .read = proc_fdinfo_read,
1955 static const struct file_operations proc_fd_operations = {
1956 .read = generic_read_dir,
1957 .readdir = proc_readfd,
1961 * /proc/pid/fd needs a special permission handler so that a process can still
1962 * access /proc/self/fd after it has executed a setuid().
1964 static int proc_fd_permission(struct inode *inode, int mask)
1968 rv = generic_permission(inode, mask, NULL);
1971 if (task_pid(current) == proc_pid(inode))
1977 * proc directories can do almost nothing..
1979 static const struct inode_operations proc_fd_inode_operations = {
1980 .lookup = proc_lookupfd,
1981 .permission = proc_fd_permission,
1982 .setattr = proc_setattr,
1985 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1986 struct dentry *dentry, struct task_struct *task, const void *ptr)
1988 unsigned fd = *(unsigned *)ptr;
1989 struct inode *inode;
1990 struct proc_inode *ei;
1991 struct dentry *error = ERR_PTR(-ENOENT);
1993 inode = proc_pid_make_inode(dir->i_sb, task);
1998 inode->i_mode = S_IFREG | S_IRUSR;
1999 inode->i_fop = &proc_fdinfo_file_operations;
2000 dentry->d_op = &tid_fd_dentry_operations;
2001 d_add(dentry, inode);
2002 /* Close the race of the process dying before we return the dentry */
2003 if (tid_fd_revalidate(dentry, NULL))
2010 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2011 struct dentry *dentry,
2012 struct nameidata *nd)
2014 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2017 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2019 return proc_readfd_common(filp, dirent, filldir,
2020 proc_fdinfo_instantiate);
2023 static const struct file_operations proc_fdinfo_operations = {
2024 .read = generic_read_dir,
2025 .readdir = proc_readfdinfo,
2029 * proc directories can do almost nothing..
2031 static const struct inode_operations proc_fdinfo_inode_operations = {
2032 .lookup = proc_lookupfdinfo,
2033 .setattr = proc_setattr,
2037 static struct dentry *proc_pident_instantiate(struct inode *dir,
2038 struct dentry *dentry, struct task_struct *task, const void *ptr)
2040 const struct pid_entry *p = ptr;
2041 struct inode *inode;
2042 struct proc_inode *ei;
2043 struct dentry *error = ERR_PTR(-ENOENT);
2045 inode = proc_pid_make_inode(dir->i_sb, task);
2050 inode->i_mode = p->mode;
2051 if (S_ISDIR(inode->i_mode))
2052 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2054 inode->i_op = p->iop;
2056 inode->i_fop = p->fop;
2058 dentry->d_op = &pid_dentry_operations;
2059 d_add(dentry, inode);
2060 /* Close the race of the process dying before we return the dentry */
2061 if (pid_revalidate(dentry, NULL))
2067 static struct dentry *proc_pident_lookup(struct inode *dir,
2068 struct dentry *dentry,
2069 const struct pid_entry *ents,
2072 struct dentry *error;
2073 struct task_struct *task = get_proc_task(dir);
2074 const struct pid_entry *p, *last;
2076 error = ERR_PTR(-ENOENT);
2082 * Yes, it does not scale. And it should not. Don't add
2083 * new entries into /proc/<tgid>/ without very good reasons.
2085 last = &ents[nents - 1];
2086 for (p = ents; p <= last; p++) {
2087 if (p->len != dentry->d_name.len)
2089 if (!memcmp(dentry->d_name.name, p->name, p->len))
2095 error = proc_pident_instantiate(dir, dentry, task, p);
2097 put_task_struct(task);
2102 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2103 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2105 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2106 proc_pident_instantiate, task, p);
2109 static int proc_pident_readdir(struct file *filp,
2110 void *dirent, filldir_t filldir,
2111 const struct pid_entry *ents, unsigned int nents)
2114 struct dentry *dentry = filp->f_path.dentry;
2115 struct inode *inode = dentry->d_inode;
2116 struct task_struct *task = get_proc_task(inode);
2117 const struct pid_entry *p, *last;
2130 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2136 ino = parent_ino(dentry);
2137 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2149 last = &ents[nents - 1];
2151 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2160 put_task_struct(task);
2165 #ifdef CONFIG_SECURITY
2166 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2167 size_t count, loff_t *ppos)
2169 struct inode * inode = file->f_path.dentry->d_inode;
2172 struct task_struct *task = get_proc_task(inode);
2177 length = security_getprocattr(task,
2178 (char*)file->f_path.dentry->d_name.name,
2180 put_task_struct(task);
2182 length = simple_read_from_buffer(buf, count, ppos, p, length);
2187 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2188 size_t count, loff_t *ppos)
2190 struct inode * inode = file->f_path.dentry->d_inode;
2193 struct task_struct *task = get_proc_task(inode);
2198 if (count > PAGE_SIZE)
2201 /* No partial writes. */
2207 page = (char*)__get_free_page(GFP_TEMPORARY);
2212 if (copy_from_user(page, buf, count))
2215 /* Guard against adverse ptrace interaction */
2216 length = mutex_lock_interruptible(&task->cred_guard_mutex);
2220 length = security_setprocattr(task,
2221 (char*)file->f_path.dentry->d_name.name,
2222 (void*)page, count);
2223 mutex_unlock(&task->cred_guard_mutex);
2225 free_page((unsigned long) page);
2227 put_task_struct(task);
2232 static const struct file_operations proc_pid_attr_operations = {
2233 .read = proc_pid_attr_read,
2234 .write = proc_pid_attr_write,
2237 static const struct pid_entry attr_dir_stuff[] = {
2238 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2239 REG("prev", S_IRUGO, proc_pid_attr_operations),
2240 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2241 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2242 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2243 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2246 static int proc_attr_dir_readdir(struct file * filp,
2247 void * dirent, filldir_t filldir)
2249 return proc_pident_readdir(filp,dirent,filldir,
2250 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2253 static const struct file_operations proc_attr_dir_operations = {
2254 .read = generic_read_dir,
2255 .readdir = proc_attr_dir_readdir,
2258 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2259 struct dentry *dentry, struct nameidata *nd)
2261 return proc_pident_lookup(dir, dentry,
2262 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2265 static const struct inode_operations proc_attr_dir_inode_operations = {
2266 .lookup = proc_attr_dir_lookup,
2267 .getattr = pid_getattr,
2268 .setattr = proc_setattr,
2273 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2274 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2275 size_t count, loff_t *ppos)
2277 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2278 struct mm_struct *mm;
2279 char buffer[PROC_NUMBUF];
2287 mm = get_task_mm(task);
2289 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2290 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2291 MMF_DUMP_FILTER_SHIFT));
2293 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2296 put_task_struct(task);
2301 static ssize_t proc_coredump_filter_write(struct file *file,
2302 const char __user *buf,
2306 struct task_struct *task;
2307 struct mm_struct *mm;
2308 char buffer[PROC_NUMBUF], *end;
2315 memset(buffer, 0, sizeof(buffer));
2316 if (count > sizeof(buffer) - 1)
2317 count = sizeof(buffer) - 1;
2318 if (copy_from_user(buffer, buf, count))
2322 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2325 if (end - buffer == 0)
2329 task = get_proc_task(file->f_dentry->d_inode);
2334 mm = get_task_mm(task);
2338 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2340 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2342 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2347 put_task_struct(task);
2352 static const struct file_operations proc_coredump_filter_operations = {
2353 .read = proc_coredump_filter_read,
2354 .write = proc_coredump_filter_write,
2361 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2364 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2365 pid_t tgid = task_tgid_nr_ns(current, ns);
2366 char tmp[PROC_NUMBUF];
2369 sprintf(tmp, "%d", tgid);
2370 return vfs_readlink(dentry,buffer,buflen,tmp);
2373 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2375 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2376 pid_t tgid = task_tgid_nr_ns(current, ns);
2377 char tmp[PROC_NUMBUF];
2379 return ERR_PTR(-ENOENT);
2380 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2381 return ERR_PTR(vfs_follow_link(nd,tmp));
2384 static const struct inode_operations proc_self_inode_operations = {
2385 .readlink = proc_self_readlink,
2386 .follow_link = proc_self_follow_link,
2392 * These are the directory entries in the root directory of /proc
2393 * that properly belong to the /proc filesystem, as they describe
2394 * describe something that is process related.
2396 static const struct pid_entry proc_base_stuff[] = {
2397 NOD("self", S_IFLNK|S_IRWXUGO,
2398 &proc_self_inode_operations, NULL, {}),
2402 * Exceptional case: normally we are not allowed to unhash a busy
2403 * directory. In this case, however, we can do it - no aliasing problems
2404 * due to the way we treat inodes.
2406 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2408 struct inode *inode = dentry->d_inode;
2409 struct task_struct *task = get_proc_task(inode);
2411 put_task_struct(task);
2418 static const struct dentry_operations proc_base_dentry_operations =
2420 .d_revalidate = proc_base_revalidate,
2421 .d_delete = pid_delete_dentry,
2424 static struct dentry *proc_base_instantiate(struct inode *dir,
2425 struct dentry *dentry, struct task_struct *task, const void *ptr)
2427 const struct pid_entry *p = ptr;
2428 struct inode *inode;
2429 struct proc_inode *ei;
2430 struct dentry *error = ERR_PTR(-EINVAL);
2432 /* Allocate the inode */
2433 error = ERR_PTR(-ENOMEM);
2434 inode = new_inode(dir->i_sb);
2438 /* Initialize the inode */
2440 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2443 * grab the reference to the task.
2445 ei->pid = get_task_pid(task, PIDTYPE_PID);
2449 inode->i_mode = p->mode;
2450 if (S_ISDIR(inode->i_mode))
2452 if (S_ISLNK(inode->i_mode))
2455 inode->i_op = p->iop;
2457 inode->i_fop = p->fop;
2459 dentry->d_op = &proc_base_dentry_operations;
2460 d_add(dentry, inode);
2469 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2471 struct dentry *error;
2472 struct task_struct *task = get_proc_task(dir);
2473 const struct pid_entry *p, *last;
2475 error = ERR_PTR(-ENOENT);
2480 /* Lookup the directory entry */
2481 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2482 for (p = proc_base_stuff; p <= last; p++) {
2483 if (p->len != dentry->d_name.len)
2485 if (!memcmp(dentry->d_name.name, p->name, p->len))
2491 error = proc_base_instantiate(dir, dentry, task, p);
2494 put_task_struct(task);
2499 static int proc_base_fill_cache(struct file *filp, void *dirent,
2500 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2502 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2503 proc_base_instantiate, task, p);
2506 #ifdef CONFIG_TASK_IO_ACCOUNTING
2507 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2509 struct task_io_accounting acct = task->ioac;
2510 unsigned long flags;
2512 if (whole && lock_task_sighand(task, &flags)) {
2513 struct task_struct *t = task;
2515 task_io_accounting_add(&acct, &task->signal->ioac);
2516 while_each_thread(task, t)
2517 task_io_accounting_add(&acct, &t->ioac);
2519 unlock_task_sighand(task, &flags);
2521 return sprintf(buffer,
2526 "read_bytes: %llu\n"
2527 "write_bytes: %llu\n"
2528 "cancelled_write_bytes: %llu\n",
2529 (unsigned long long)acct.rchar,
2530 (unsigned long long)acct.wchar,
2531 (unsigned long long)acct.syscr,
2532 (unsigned long long)acct.syscw,
2533 (unsigned long long)acct.read_bytes,
2534 (unsigned long long)acct.write_bytes,
2535 (unsigned long long)acct.cancelled_write_bytes);
2538 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2540 return do_io_accounting(task, buffer, 0);
2543 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2545 return do_io_accounting(task, buffer, 1);
2547 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2549 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2550 struct pid *pid, struct task_struct *task)
2552 seq_printf(m, "%08x\n", task->personality);
2559 static const struct file_operations proc_task_operations;
2560 static const struct inode_operations proc_task_inode_operations;
2562 static const struct pid_entry tgid_base_stuff[] = {
2563 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2564 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2565 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2567 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2569 REG("environ", S_IRUSR, proc_environ_operations),
2570 INF("auxv", S_IRUSR, proc_pid_auxv),
2571 ONE("status", S_IRUGO, proc_pid_status),
2572 ONE("personality", S_IRUSR, proc_pid_personality),
2573 NOD("limits", S_IFREG|S_IRUSR|S_IWUSR, NULL,
2574 &proc_pid_limits_operations,
2575 { .proc_read = proc_pid_limits }),
2576 #ifdef CONFIG_SCHED_DEBUG
2577 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2579 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2580 INF("syscall", S_IRUSR, proc_pid_syscall),
2582 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2583 ONE("stat", S_IRUGO, proc_tgid_stat),
2584 ONE("statm", S_IRUGO, proc_pid_statm),
2585 REG("maps", S_IRUGO, proc_maps_operations),
2587 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2589 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2590 LNK("cwd", proc_cwd_link),
2591 LNK("root", proc_root_link),
2592 LNK("exe", proc_exe_link),
2593 REG("mounts", S_IRUGO, proc_mounts_operations),
2594 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2595 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2596 #ifdef CONFIG_PROC_PAGE_MONITOR
2597 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2598 REG("smaps", S_IRUGO, proc_smaps_operations),
2599 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2601 #ifdef CONFIG_SECURITY
2602 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2604 #ifdef CONFIG_KALLSYMS
2605 INF("wchan", S_IRUGO, proc_pid_wchan),
2607 #ifdef CONFIG_STACKTRACE
2608 ONE("stack", S_IRUSR, proc_pid_stack),
2610 #ifdef CONFIG_SCHEDSTATS
2611 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2613 #ifdef CONFIG_LATENCYTOP
2614 REG("latency", S_IRUGO, proc_lstats_operations),
2616 #ifdef CONFIG_PROC_PID_CPUSET
2617 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2619 #ifdef CONFIG_CGROUPS
2620 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2622 INF("oom_score", S_IRUGO, proc_oom_score),
2623 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2624 #ifdef CONFIG_AUDITSYSCALL
2625 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2626 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2628 #ifdef CONFIG_FAULT_INJECTION
2629 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2631 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2632 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2634 #ifdef CONFIG_TASK_IO_ACCOUNTING
2635 INF("io", S_IRUGO, proc_tgid_io_accounting),
2639 static int proc_tgid_base_readdir(struct file * filp,
2640 void * dirent, filldir_t filldir)
2642 return proc_pident_readdir(filp,dirent,filldir,
2643 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2646 static const struct file_operations proc_tgid_base_operations = {
2647 .read = generic_read_dir,
2648 .readdir = proc_tgid_base_readdir,
2651 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2652 return proc_pident_lookup(dir, dentry,
2653 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2656 static const struct inode_operations proc_tgid_base_inode_operations = {
2657 .lookup = proc_tgid_base_lookup,
2658 .getattr = pid_getattr,
2659 .setattr = proc_setattr,
2662 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2664 struct dentry *dentry, *leader, *dir;
2665 char buf[PROC_NUMBUF];
2669 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2670 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2672 shrink_dcache_parent(dentry);
2678 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2679 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2684 name.len = strlen(name.name);
2685 dir = d_hash_and_lookup(leader, &name);
2687 goto out_put_leader;
2690 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2691 dentry = d_hash_and_lookup(dir, &name);
2693 shrink_dcache_parent(dentry);
2706 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2707 * @task: task that should be flushed.
2709 * When flushing dentries from proc, one needs to flush them from global
2710 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2711 * in. This call is supposed to do all of this job.
2713 * Looks in the dcache for
2715 * /proc/@tgid/task/@pid
2716 * if either directory is present flushes it and all of it'ts children
2719 * It is safe and reasonable to cache /proc entries for a task until
2720 * that task exits. After that they just clog up the dcache with
2721 * useless entries, possibly causing useful dcache entries to be
2722 * flushed instead. This routine is proved to flush those useless
2723 * dcache entries at process exit time.
2725 * NOTE: This routine is just an optimization so it does not guarantee
2726 * that no dcache entries will exist at process exit time it
2727 * just makes it very unlikely that any will persist.
2730 void proc_flush_task(struct task_struct *task)
2733 struct pid *pid, *tgid;
2736 pid = task_pid(task);
2737 tgid = task_tgid(task);
2739 for (i = 0; i <= pid->level; i++) {
2740 upid = &pid->numbers[i];
2741 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2742 tgid->numbers[i].nr);
2745 upid = &pid->numbers[pid->level];
2747 pid_ns_release_proc(upid->ns);
2750 static struct dentry *proc_pid_instantiate(struct inode *dir,
2751 struct dentry * dentry,
2752 struct task_struct *task, const void *ptr)
2754 struct dentry *error = ERR_PTR(-ENOENT);
2755 struct inode *inode;
2757 inode = proc_pid_make_inode(dir->i_sb, task);
2761 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2762 inode->i_op = &proc_tgid_base_inode_operations;
2763 inode->i_fop = &proc_tgid_base_operations;
2764 inode->i_flags|=S_IMMUTABLE;
2766 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2767 ARRAY_SIZE(tgid_base_stuff));
2769 dentry->d_op = &pid_dentry_operations;
2771 d_add(dentry, inode);
2772 /* Close the race of the process dying before we return the dentry */
2773 if (pid_revalidate(dentry, NULL))
2779 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2781 struct dentry *result = ERR_PTR(-ENOENT);
2782 struct task_struct *task;
2784 struct pid_namespace *ns;
2786 result = proc_base_lookup(dir, dentry);
2787 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2790 tgid = name_to_int(dentry);
2794 ns = dentry->d_sb->s_fs_info;
2796 task = find_task_by_pid_ns(tgid, ns);
2798 get_task_struct(task);
2803 result = proc_pid_instantiate(dir, dentry, task, NULL);
2804 put_task_struct(task);
2810 * Find the first task with tgid >= tgid
2815 struct task_struct *task;
2817 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2822 put_task_struct(iter.task);
2826 pid = find_ge_pid(iter.tgid, ns);
2828 iter.tgid = pid_nr_ns(pid, ns);
2829 iter.task = pid_task(pid, PIDTYPE_PID);
2830 /* What we to know is if the pid we have find is the
2831 * pid of a thread_group_leader. Testing for task
2832 * being a thread_group_leader is the obvious thing
2833 * todo but there is a window when it fails, due to
2834 * the pid transfer logic in de_thread.
2836 * So we perform the straight forward test of seeing
2837 * if the pid we have found is the pid of a thread
2838 * group leader, and don't worry if the task we have
2839 * found doesn't happen to be a thread group leader.
2840 * As we don't care in the case of readdir.
2842 if (!iter.task || !has_group_leader_pid(iter.task)) {
2846 get_task_struct(iter.task);
2852 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2854 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2855 struct tgid_iter iter)
2857 char name[PROC_NUMBUF];
2858 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2859 return proc_fill_cache(filp, dirent, filldir, name, len,
2860 proc_pid_instantiate, iter.task, NULL);
2863 /* for the /proc/ directory itself, after non-process stuff has been done */
2864 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2866 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2867 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2868 struct tgid_iter iter;
2869 struct pid_namespace *ns;
2874 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2875 const struct pid_entry *p = &proc_base_stuff[nr];
2876 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2880 ns = filp->f_dentry->d_sb->s_fs_info;
2882 iter.tgid = filp->f_pos - TGID_OFFSET;
2883 for (iter = next_tgid(ns, iter);
2885 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2886 filp->f_pos = iter.tgid + TGID_OFFSET;
2887 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2888 put_task_struct(iter.task);
2892 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2894 put_task_struct(reaper);
2902 static const struct pid_entry tid_base_stuff[] = {
2903 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2904 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2905 REG("environ", S_IRUSR, proc_environ_operations),
2906 INF("auxv", S_IRUSR, proc_pid_auxv),
2907 ONE("status", S_IRUGO, proc_pid_status),
2908 ONE("personality", S_IRUSR, proc_pid_personality),
2909 NOD("limits", S_IFREG|S_IRUSR|S_IWUSR, NULL,
2910 &proc_pid_limits_operations,
2911 { .proc_read = proc_pid_limits }),
2912 #ifdef CONFIG_SCHED_DEBUG
2913 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2915 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2916 INF("syscall", S_IRUSR, proc_pid_syscall),
2918 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2919 ONE("stat", S_IRUGO, proc_tid_stat),
2920 ONE("statm", S_IRUGO, proc_pid_statm),
2921 REG("maps", S_IRUGO, proc_maps_operations),
2923 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2925 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2926 LNK("cwd", proc_cwd_link),
2927 LNK("root", proc_root_link),
2928 LNK("exe", proc_exe_link),
2929 REG("mounts", S_IRUGO, proc_mounts_operations),
2930 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2931 #ifdef CONFIG_PROC_PAGE_MONITOR
2932 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2933 REG("smaps", S_IRUGO, proc_smaps_operations),
2934 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2936 #ifdef CONFIG_SECURITY
2937 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2939 #ifdef CONFIG_KALLSYMS
2940 INF("wchan", S_IRUGO, proc_pid_wchan),
2942 #ifdef CONFIG_STACKTRACE
2943 ONE("stack", S_IRUSR, proc_pid_stack),
2945 #ifdef CONFIG_SCHEDSTATS
2946 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2948 #ifdef CONFIG_LATENCYTOP
2949 REG("latency", S_IRUGO, proc_lstats_operations),
2951 #ifdef CONFIG_PROC_PID_CPUSET
2952 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2954 #ifdef CONFIG_CGROUPS
2955 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2957 INF("oom_score", S_IRUGO, proc_oom_score),
2958 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2959 #ifdef CONFIG_AUDITSYSCALL
2960 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2961 REG("sessionid", S_IRUSR, proc_sessionid_operations),
2963 #ifdef CONFIG_FAULT_INJECTION
2964 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2966 #ifdef CONFIG_TASK_IO_ACCOUNTING
2967 INF("io", S_IRUGO, proc_tid_io_accounting),
2971 static int proc_tid_base_readdir(struct file * filp,
2972 void * dirent, filldir_t filldir)
2974 return proc_pident_readdir(filp,dirent,filldir,
2975 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2978 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2979 return proc_pident_lookup(dir, dentry,
2980 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2983 static const struct file_operations proc_tid_base_operations = {
2984 .read = generic_read_dir,
2985 .readdir = proc_tid_base_readdir,
2988 static const struct inode_operations proc_tid_base_inode_operations = {
2989 .lookup = proc_tid_base_lookup,
2990 .getattr = pid_getattr,
2991 .setattr = proc_setattr,
2994 static struct dentry *proc_task_instantiate(struct inode *dir,
2995 struct dentry *dentry, struct task_struct *task, const void *ptr)
2997 struct dentry *error = ERR_PTR(-ENOENT);
2998 struct inode *inode;
2999 inode = proc_pid_make_inode(dir->i_sb, task);
3003 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3004 inode->i_op = &proc_tid_base_inode_operations;
3005 inode->i_fop = &proc_tid_base_operations;
3006 inode->i_flags|=S_IMMUTABLE;
3008 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3009 ARRAY_SIZE(tid_base_stuff));
3011 dentry->d_op = &pid_dentry_operations;
3013 d_add(dentry, inode);
3014 /* Close the race of the process dying before we return the dentry */
3015 if (pid_revalidate(dentry, NULL))
3021 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3023 struct dentry *result = ERR_PTR(-ENOENT);
3024 struct task_struct *task;
3025 struct task_struct *leader = get_proc_task(dir);
3027 struct pid_namespace *ns;
3032 tid = name_to_int(dentry);
3036 ns = dentry->d_sb->s_fs_info;
3038 task = find_task_by_pid_ns(tid, ns);
3040 get_task_struct(task);
3044 if (!same_thread_group(leader, task))
3047 result = proc_task_instantiate(dir, dentry, task, NULL);
3049 put_task_struct(task);
3051 put_task_struct(leader);
3057 * Find the first tid of a thread group to return to user space.
3059 * Usually this is just the thread group leader, but if the users
3060 * buffer was too small or there was a seek into the middle of the
3061 * directory we have more work todo.
3063 * In the case of a short read we start with find_task_by_pid.
3065 * In the case of a seek we start with the leader and walk nr
3068 static struct task_struct *first_tid(struct task_struct *leader,
3069 int tid, int nr, struct pid_namespace *ns)
3071 struct task_struct *pos;
3074 /* Attempt to start with the pid of a thread */
3075 if (tid && (nr > 0)) {
3076 pos = find_task_by_pid_ns(tid, ns);
3077 if (pos && (pos->group_leader == leader))
3081 /* If nr exceeds the number of threads there is nothing todo */
3083 if (nr && nr >= get_nr_threads(leader))
3086 /* If we haven't found our starting place yet start
3087 * with the leader and walk nr threads forward.
3089 for (pos = leader; nr > 0; --nr) {
3090 pos = next_thread(pos);
3091 if (pos == leader) {
3097 get_task_struct(pos);
3104 * Find the next thread in the thread list.
3105 * Return NULL if there is an error or no next thread.
3107 * The reference to the input task_struct is released.
3109 static struct task_struct *next_tid(struct task_struct *start)
3111 struct task_struct *pos = NULL;
3113 if (pid_alive(start)) {
3114 pos = next_thread(start);
3115 if (thread_group_leader(pos))
3118 get_task_struct(pos);
3121 put_task_struct(start);
3125 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3126 struct task_struct *task, int tid)
3128 char name[PROC_NUMBUF];
3129 int len = snprintf(name, sizeof(name), "%d", tid);
3130 return proc_fill_cache(filp, dirent, filldir, name, len,
3131 proc_task_instantiate, task, NULL);
3134 /* for the /proc/TGID/task/ directories */
3135 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3137 struct dentry *dentry = filp->f_path.dentry;
3138 struct inode *inode = dentry->d_inode;
3139 struct task_struct *leader = NULL;
3140 struct task_struct *task;
3141 int retval = -ENOENT;
3144 struct pid_namespace *ns;
3146 task = get_proc_task(inode);
3150 if (pid_alive(task)) {
3151 leader = task->group_leader;
3152 get_task_struct(leader);
3155 put_task_struct(task);
3160 switch ((unsigned long)filp->f_pos) {
3163 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3168 ino = parent_ino(dentry);
3169 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3175 /* f_version caches the tgid value that the last readdir call couldn't
3176 * return. lseek aka telldir automagically resets f_version to 0.
3178 ns = filp->f_dentry->d_sb->s_fs_info;
3179 tid = (int)filp->f_version;
3180 filp->f_version = 0;
3181 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3183 task = next_tid(task), filp->f_pos++) {
3184 tid = task_pid_nr_ns(task, ns);
3185 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3186 /* returning this tgid failed, save it as the first
3187 * pid for the next readir call */
3188 filp->f_version = (u64)tid;
3189 put_task_struct(task);
3194 put_task_struct(leader);
3199 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3201 struct inode *inode = dentry->d_inode;
3202 struct task_struct *p = get_proc_task(inode);
3203 generic_fillattr(inode, stat);
3206 stat->nlink += get_nr_threads(p);
3213 static const struct inode_operations proc_task_inode_operations = {
3214 .lookup = proc_task_lookup,
3215 .getattr = proc_task_getattr,
3216 .setattr = proc_setattr,
3219 static const struct file_operations proc_task_operations = {
3220 .read = generic_read_dir,
3221 .readdir = proc_task_readdir,