thinkpad-acpi: documentation cleanup
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / proc / base.c
blob74e83e7b1c9b07d4fe2780ff780d8c0dc756c567
1 /*
2 * linux/fs/proc/base.c
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.
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
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.
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
38 * ChangeLog:
39 * 10-Mar-2005
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>
65 #include <linux/mm.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 "internal.h"
85 /* NOTE:
86 * Implementing inode permission operations in /proc is almost
87 * certainly an error. Permission checks need to happen during
88 * each system call not at open time. The reason is that most of
89 * what we wish to check for permissions in /proc varies at runtime.
91 * The classic example of a problem is opening file descriptors
92 * in /proc for a task before it execs a suid executable.
95 struct pid_entry {
96 char *name;
97 int len;
98 mode_t mode;
99 const struct inode_operations *iop;
100 const struct file_operations *fop;
101 union proc_op op;
104 #define NOD(NAME, MODE, IOP, FOP, OP) { \
105 .name = (NAME), \
106 .len = sizeof(NAME) - 1, \
107 .mode = MODE, \
108 .iop = IOP, \
109 .fop = FOP, \
110 .op = OP, \
113 #define DIR(NAME, MODE, iops, fops) \
114 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
115 #define LNK(NAME, get_link) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = get_link } )
119 #define REG(NAME, MODE, fops) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
121 #define INF(NAME, MODE, read) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = read } )
125 #define ONE(NAME, MODE, show) \
126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_single_file_operations, \
128 { .proc_show = show } )
131 * Count the number of hardlinks for the pid_entry table, excluding the .
132 * and .. links.
134 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
135 unsigned int n)
137 unsigned int i;
138 unsigned int count;
140 count = 0;
141 for (i = 0; i < n; ++i) {
142 if (S_ISDIR(entries[i].mode))
143 ++count;
146 return count;
149 static int get_fs_path(struct task_struct *task, struct path *path, bool root)
151 struct fs_struct *fs;
152 int result = -ENOENT;
154 task_lock(task);
155 fs = task->fs;
156 if (fs) {
157 read_lock(&fs->lock);
158 *path = root ? fs->root : fs->pwd;
159 path_get(path);
160 read_unlock(&fs->lock);
161 result = 0;
163 task_unlock(task);
164 return result;
167 static int get_nr_threads(struct task_struct *tsk)
169 unsigned long flags;
170 int count = 0;
172 if (lock_task_sighand(tsk, &flags)) {
173 count = atomic_read(&tsk->signal->count);
174 unlock_task_sighand(tsk, &flags);
176 return count;
179 static int proc_cwd_link(struct inode *inode, struct path *path)
181 struct task_struct *task = get_proc_task(inode);
182 int result = -ENOENT;
184 if (task) {
185 result = get_fs_path(task, path, 0);
186 put_task_struct(task);
188 return result;
191 static int proc_root_link(struct inode *inode, struct path *path)
193 struct task_struct *task = get_proc_task(inode);
194 int result = -ENOENT;
196 if (task) {
197 result = get_fs_path(task, path, 1);
198 put_task_struct(task);
200 return result;
204 * Return zero if current may access user memory in @task, -error if not.
206 static int check_mem_permission(struct task_struct *task)
209 * A task can always look at itself, in case it chooses
210 * to use system calls instead of load instructions.
212 if (task == current)
213 return 0;
216 * If current is actively ptrace'ing, and would also be
217 * permitted to freshly attach with ptrace now, permit it.
219 if (task_is_stopped_or_traced(task)) {
220 int match;
221 rcu_read_lock();
222 match = (tracehook_tracer_task(task) == current);
223 rcu_read_unlock();
224 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
225 return 0;
229 * Noone else is allowed.
231 return -EPERM;
234 struct mm_struct *mm_for_maps(struct task_struct *task)
236 struct mm_struct *mm = get_task_mm(task);
237 if (!mm)
238 return NULL;
239 down_read(&mm->mmap_sem);
240 task_lock(task);
241 if (task->mm != mm)
242 goto out;
243 if (task->mm != current->mm &&
244 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
245 goto out;
246 task_unlock(task);
247 return mm;
248 out:
249 task_unlock(task);
250 up_read(&mm->mmap_sem);
251 mmput(mm);
252 return NULL;
255 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
257 int res = 0;
258 unsigned int len;
259 struct mm_struct *mm = get_task_mm(task);
260 if (!mm)
261 goto out;
262 if (!mm->arg_end)
263 goto out_mm; /* Shh! No looking before we're done */
265 len = mm->arg_end - mm->arg_start;
267 if (len > PAGE_SIZE)
268 len = PAGE_SIZE;
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);
276 if (len < res) {
277 res = len;
278 } else {
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);
286 out_mm:
287 mmput(mm);
288 out:
289 return res;
292 static int proc_pid_auxv(struct task_struct *task, char *buffer)
294 int res = 0;
295 struct mm_struct *mm = get_task_mm(task);
296 if (mm) {
297 unsigned int nwords = 0;
298 do {
299 nwords += 2;
300 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
301 res = nwords * sizeof(mm->saved_auxv[0]);
302 if (res > PAGE_SIZE)
303 res = PAGE_SIZE;
304 memcpy(buffer, mm->saved_auxv, res);
305 mmput(mm);
307 return 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)
318 unsigned long wchan;
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))
325 return 0;
326 else
327 return sprintf(buffer, "%lu", wchan);
328 else
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;
342 int i;
344 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
345 if (!entries)
346 return -ENOMEM;
348 trace.nr_entries = 0;
349 trace.max_entries = MAX_STACK_TRACE_DEPTH;
350 trace.entries = entries;
351 trace.skip = 0;
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]);
358 kfree(entries);
360 return 0;
362 #endif
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);
375 #endif
377 #ifdef CONFIG_LATENCYTOP
378 static int lstats_show_proc(struct seq_file *m, void *v)
380 int i;
381 struct inode *inode = m->private;
382 struct task_struct *task = get_proc_task(inode);
384 if (!task)
385 return -ESRCH;
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]) {
389 int q;
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];
396 char *c;
397 if (!task->latency_record[i].backtrace[q])
398 break;
399 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
400 break;
401 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
402 c = strchr(sym, '+');
403 if (c)
404 *c = 0;
405 seq_printf(m, "%s ", sym);
407 seq_printf(m, "\n");
411 put_task_struct(task);
412 return 0;
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);
425 if (!task)
426 return -ESRCH;
427 clear_all_latency_tracing(task);
428 put_task_struct(task);
430 return count;
433 static const struct file_operations proc_lstats_operations = {
434 .open = lstats_open,
435 .read = seq_read,
436 .write = lstats_write,
437 .llseek = seq_lseek,
438 .release = single_release,
441 #endif
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, uptime.tv_sec);
453 read_unlock(&tasklist_lock);
454 return sprintf(buffer, "%lu\n", points);
457 struct limit_names {
458 char *name;
459 char *unit;
462 static const struct limit_names lnames[RLIM_NLIMITS] = {
463 [RLIMIT_CPU] = {"Max cpu time", "ms"},
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)
484 unsigned int i;
485 int count = 0;
486 unsigned long flags;
487 char *bufptr = buffer;
489 struct rlimit rlim[RLIM_NLIMITS];
491 if (!lock_task_sighand(task, &flags))
492 return 0;
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");
506 else
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");
512 else
513 count += sprintf(&bufptr[count], "%-20lu ",
514 rlim[i].rlim_max);
516 if (lnames[i].unit)
517 count += sprintf(&bufptr[count], "%-10s\n",
518 lnames[i].unit);
519 else
520 count += sprintf(&bufptr[count], "\n");
523 return count;
526 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
527 static int proc_pid_syscall(struct task_struct *task, char *buffer)
529 long nr;
530 unsigned long args[6], sp, pc;
532 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
533 return sprintf(buffer, "running\n");
535 if (nr < 0)
536 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
538 return sprintf(buffer,
539 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
541 args[0], args[1], args[2], args[3], args[4], args[5],
542 sp, pc);
544 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
546 /************************************************************************/
547 /* Here the fs part begins */
548 /************************************************************************/
550 /* permission checks */
551 static int proc_fd_access_allowed(struct inode *inode)
553 struct task_struct *task;
554 int allowed = 0;
555 /* Allow access to a task's file descriptors if it is us or we
556 * may use ptrace attach to the process and find out that
557 * information.
559 task = get_proc_task(inode);
560 if (task) {
561 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
562 put_task_struct(task);
564 return allowed;
567 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
569 int error;
570 struct inode *inode = dentry->d_inode;
572 if (attr->ia_valid & ATTR_MODE)
573 return -EPERM;
575 error = inode_change_ok(inode, attr);
576 if (!error)
577 error = inode_setattr(inode, attr);
578 return error;
581 static const struct inode_operations proc_def_inode_operations = {
582 .setattr = proc_setattr,
585 static int mounts_open_common(struct inode *inode, struct file *file,
586 const struct seq_operations *op)
588 struct task_struct *task = get_proc_task(inode);
589 struct nsproxy *nsp;
590 struct mnt_namespace *ns = NULL;
591 struct path root;
592 struct proc_mounts *p;
593 int ret = -EINVAL;
595 if (task) {
596 rcu_read_lock();
597 nsp = task_nsproxy(task);
598 if (nsp) {
599 ns = nsp->mnt_ns;
600 if (ns)
601 get_mnt_ns(ns);
603 rcu_read_unlock();
604 if (ns && get_fs_path(task, &root, 1) == 0)
605 ret = 0;
606 put_task_struct(task);
609 if (!ns)
610 goto err;
611 if (ret)
612 goto err_put_ns;
614 ret = -ENOMEM;
615 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
616 if (!p)
617 goto err_put_path;
619 file->private_data = &p->m;
620 ret = seq_open(file, op);
621 if (ret)
622 goto err_free;
624 p->m.private = p;
625 p->ns = ns;
626 p->root = root;
627 p->event = ns->event;
629 return 0;
631 err_free:
632 kfree(p);
633 err_put_path:
634 path_put(&root);
635 err_put_ns:
636 put_mnt_ns(ns);
637 err:
638 return ret;
641 static int mounts_release(struct inode *inode, struct file *file)
643 struct proc_mounts *p = file->private_data;
644 path_put(&p->root);
645 put_mnt_ns(p->ns);
646 return seq_release(inode, file);
649 static unsigned mounts_poll(struct file *file, poll_table *wait)
651 struct proc_mounts *p = file->private_data;
652 struct mnt_namespace *ns = p->ns;
653 unsigned res = 0;
655 poll_wait(file, &ns->poll, wait);
657 spin_lock(&vfsmount_lock);
658 if (p->event != ns->event) {
659 p->event = ns->event;
660 res = POLLERR;
662 spin_unlock(&vfsmount_lock);
664 return res;
667 static int mounts_open(struct inode *inode, struct file *file)
669 return mounts_open_common(inode, file, &mounts_op);
672 static const struct file_operations proc_mounts_operations = {
673 .open = mounts_open,
674 .read = seq_read,
675 .llseek = seq_lseek,
676 .release = mounts_release,
677 .poll = mounts_poll,
680 static int mountinfo_open(struct inode *inode, struct file *file)
682 return mounts_open_common(inode, file, &mountinfo_op);
685 static const struct file_operations proc_mountinfo_operations = {
686 .open = mountinfo_open,
687 .read = seq_read,
688 .llseek = seq_lseek,
689 .release = mounts_release,
690 .poll = mounts_poll,
693 static int mountstats_open(struct inode *inode, struct file *file)
695 return mounts_open_common(inode, file, &mountstats_op);
698 static const struct file_operations proc_mountstats_operations = {
699 .open = mountstats_open,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = mounts_release,
705 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
707 static ssize_t proc_info_read(struct file * file, char __user * buf,
708 size_t count, loff_t *ppos)
710 struct inode * inode = file->f_path.dentry->d_inode;
711 unsigned long page;
712 ssize_t length;
713 struct task_struct *task = get_proc_task(inode);
715 length = -ESRCH;
716 if (!task)
717 goto out_no_task;
719 if (count > PROC_BLOCK_SIZE)
720 count = PROC_BLOCK_SIZE;
722 length = -ENOMEM;
723 if (!(page = __get_free_page(GFP_TEMPORARY)))
724 goto out;
726 length = PROC_I(inode)->op.proc_read(task, (char*)page);
728 if (length >= 0)
729 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
730 free_page(page);
731 out:
732 put_task_struct(task);
733 out_no_task:
734 return length;
737 static const struct file_operations proc_info_file_operations = {
738 .read = proc_info_read,
741 static int proc_single_show(struct seq_file *m, void *v)
743 struct inode *inode = m->private;
744 struct pid_namespace *ns;
745 struct pid *pid;
746 struct task_struct *task;
747 int ret;
749 ns = inode->i_sb->s_fs_info;
750 pid = proc_pid(inode);
751 task = get_pid_task(pid, PIDTYPE_PID);
752 if (!task)
753 return -ESRCH;
755 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
757 put_task_struct(task);
758 return ret;
761 static int proc_single_open(struct inode *inode, struct file *filp)
763 int ret;
764 ret = single_open(filp, proc_single_show, NULL);
765 if (!ret) {
766 struct seq_file *m = filp->private_data;
768 m->private = inode;
770 return ret;
773 static const struct file_operations proc_single_file_operations = {
774 .open = proc_single_open,
775 .read = seq_read,
776 .llseek = seq_lseek,
777 .release = single_release,
780 static int mem_open(struct inode* inode, struct file* file)
782 file->private_data = (void*)((long)current->self_exec_id);
783 return 0;
786 static ssize_t mem_read(struct file * file, char __user * buf,
787 size_t count, loff_t *ppos)
789 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
790 char *page;
791 unsigned long src = *ppos;
792 int ret = -ESRCH;
793 struct mm_struct *mm;
795 if (!task)
796 goto out_no_task;
798 if (check_mem_permission(task))
799 goto out;
801 ret = -ENOMEM;
802 page = (char *)__get_free_page(GFP_TEMPORARY);
803 if (!page)
804 goto out;
806 ret = 0;
808 mm = get_task_mm(task);
809 if (!mm)
810 goto out_free;
812 ret = -EIO;
814 if (file->private_data != (void*)((long)current->self_exec_id))
815 goto out_put;
817 ret = 0;
819 while (count > 0) {
820 int this_len, retval;
822 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
823 retval = access_process_vm(task, src, page, this_len, 0);
824 if (!retval || check_mem_permission(task)) {
825 if (!ret)
826 ret = -EIO;
827 break;
830 if (copy_to_user(buf, page, retval)) {
831 ret = -EFAULT;
832 break;
835 ret += retval;
836 src += retval;
837 buf += retval;
838 count -= retval;
840 *ppos = src;
842 out_put:
843 mmput(mm);
844 out_free:
845 free_page((unsigned long) page);
846 out:
847 put_task_struct(task);
848 out_no_task:
849 return ret;
852 #define mem_write NULL
854 #ifndef mem_write
855 /* This is a security hazard */
856 static ssize_t mem_write(struct file * file, const char __user *buf,
857 size_t count, loff_t *ppos)
859 int copied;
860 char *page;
861 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
862 unsigned long dst = *ppos;
864 copied = -ESRCH;
865 if (!task)
866 goto out_no_task;
868 if (check_mem_permission(task))
869 goto out;
871 copied = -ENOMEM;
872 page = (char *)__get_free_page(GFP_TEMPORARY);
873 if (!page)
874 goto out;
876 copied = 0;
877 while (count > 0) {
878 int this_len, retval;
880 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
881 if (copy_from_user(page, buf, this_len)) {
882 copied = -EFAULT;
883 break;
885 retval = access_process_vm(task, dst, page, this_len, 1);
886 if (!retval) {
887 if (!copied)
888 copied = -EIO;
889 break;
891 copied += retval;
892 buf += retval;
893 dst += retval;
894 count -= retval;
896 *ppos = dst;
897 free_page((unsigned long) page);
898 out:
899 put_task_struct(task);
900 out_no_task:
901 return copied;
903 #endif
905 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
907 switch (orig) {
908 case 0:
909 file->f_pos = offset;
910 break;
911 case 1:
912 file->f_pos += offset;
913 break;
914 default:
915 return -EINVAL;
917 force_successful_syscall_return();
918 return file->f_pos;
921 static const struct file_operations proc_mem_operations = {
922 .llseek = mem_lseek,
923 .read = mem_read,
924 .write = mem_write,
925 .open = mem_open,
928 static ssize_t environ_read(struct file *file, char __user *buf,
929 size_t count, loff_t *ppos)
931 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
932 char *page;
933 unsigned long src = *ppos;
934 int ret = -ESRCH;
935 struct mm_struct *mm;
937 if (!task)
938 goto out_no_task;
940 if (!ptrace_may_access(task, PTRACE_MODE_READ))
941 goto out;
943 ret = -ENOMEM;
944 page = (char *)__get_free_page(GFP_TEMPORARY);
945 if (!page)
946 goto out;
948 ret = 0;
950 mm = get_task_mm(task);
951 if (!mm)
952 goto out_free;
954 while (count > 0) {
955 int this_len, retval, max_len;
957 this_len = mm->env_end - (mm->env_start + src);
959 if (this_len <= 0)
960 break;
962 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
963 this_len = (this_len > max_len) ? max_len : this_len;
965 retval = access_process_vm(task, (mm->env_start + src),
966 page, this_len, 0);
968 if (retval <= 0) {
969 ret = retval;
970 break;
973 if (copy_to_user(buf, page, retval)) {
974 ret = -EFAULT;
975 break;
978 ret += retval;
979 src += retval;
980 buf += retval;
981 count -= retval;
983 *ppos = src;
985 mmput(mm);
986 out_free:
987 free_page((unsigned long) page);
988 out:
989 put_task_struct(task);
990 out_no_task:
991 return ret;
994 static const struct file_operations proc_environ_operations = {
995 .read = environ_read,
998 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
999 size_t count, loff_t *ppos)
1001 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1002 char buffer[PROC_NUMBUF];
1003 size_t len;
1004 int oom_adjust;
1006 if (!task)
1007 return -ESRCH;
1008 oom_adjust = task->oomkilladj;
1009 put_task_struct(task);
1011 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1013 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1016 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1017 size_t count, loff_t *ppos)
1019 struct task_struct *task;
1020 char buffer[PROC_NUMBUF], *end;
1021 int oom_adjust;
1023 memset(buffer, 0, sizeof(buffer));
1024 if (count > sizeof(buffer) - 1)
1025 count = sizeof(buffer) - 1;
1026 if (copy_from_user(buffer, buf, count))
1027 return -EFAULT;
1028 oom_adjust = simple_strtol(buffer, &end, 0);
1029 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1030 oom_adjust != OOM_DISABLE)
1031 return -EINVAL;
1032 if (*end == '\n')
1033 end++;
1034 task = get_proc_task(file->f_path.dentry->d_inode);
1035 if (!task)
1036 return -ESRCH;
1037 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1038 put_task_struct(task);
1039 return -EACCES;
1041 task->oomkilladj = oom_adjust;
1042 put_task_struct(task);
1043 if (end - buffer == 0)
1044 return -EIO;
1045 return end - buffer;
1048 static const struct file_operations proc_oom_adjust_operations = {
1049 .read = oom_adjust_read,
1050 .write = oom_adjust_write,
1053 #ifdef CONFIG_AUDITSYSCALL
1054 #define TMPBUFLEN 21
1055 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1056 size_t count, loff_t *ppos)
1058 struct inode * inode = file->f_path.dentry->d_inode;
1059 struct task_struct *task = get_proc_task(inode);
1060 ssize_t length;
1061 char tmpbuf[TMPBUFLEN];
1063 if (!task)
1064 return -ESRCH;
1065 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1066 audit_get_loginuid(task));
1067 put_task_struct(task);
1068 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1071 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1072 size_t count, loff_t *ppos)
1074 struct inode * inode = file->f_path.dentry->d_inode;
1075 char *page, *tmp;
1076 ssize_t length;
1077 uid_t loginuid;
1079 if (!capable(CAP_AUDIT_CONTROL))
1080 return -EPERM;
1082 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1083 return -EPERM;
1085 if (count >= PAGE_SIZE)
1086 count = PAGE_SIZE - 1;
1088 if (*ppos != 0) {
1089 /* No partial writes. */
1090 return -EINVAL;
1092 page = (char*)__get_free_page(GFP_TEMPORARY);
1093 if (!page)
1094 return -ENOMEM;
1095 length = -EFAULT;
1096 if (copy_from_user(page, buf, count))
1097 goto out_free_page;
1099 page[count] = '\0';
1100 loginuid = simple_strtoul(page, &tmp, 10);
1101 if (tmp == page) {
1102 length = -EINVAL;
1103 goto out_free_page;
1106 length = audit_set_loginuid(current, loginuid);
1107 if (likely(length == 0))
1108 length = count;
1110 out_free_page:
1111 free_page((unsigned long) page);
1112 return length;
1115 static const struct file_operations proc_loginuid_operations = {
1116 .read = proc_loginuid_read,
1117 .write = proc_loginuid_write,
1120 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1121 size_t count, loff_t *ppos)
1123 struct inode * inode = file->f_path.dentry->d_inode;
1124 struct task_struct *task = get_proc_task(inode);
1125 ssize_t length;
1126 char tmpbuf[TMPBUFLEN];
1128 if (!task)
1129 return -ESRCH;
1130 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1131 audit_get_sessionid(task));
1132 put_task_struct(task);
1133 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1136 static const struct file_operations proc_sessionid_operations = {
1137 .read = proc_sessionid_read,
1139 #endif
1141 #ifdef CONFIG_FAULT_INJECTION
1142 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1143 size_t count, loff_t *ppos)
1145 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1146 char buffer[PROC_NUMBUF];
1147 size_t len;
1148 int make_it_fail;
1150 if (!task)
1151 return -ESRCH;
1152 make_it_fail = task->make_it_fail;
1153 put_task_struct(task);
1155 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1157 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1160 static ssize_t proc_fault_inject_write(struct file * file,
1161 const char __user * buf, size_t count, loff_t *ppos)
1163 struct task_struct *task;
1164 char buffer[PROC_NUMBUF], *end;
1165 int make_it_fail;
1167 if (!capable(CAP_SYS_RESOURCE))
1168 return -EPERM;
1169 memset(buffer, 0, sizeof(buffer));
1170 if (count > sizeof(buffer) - 1)
1171 count = sizeof(buffer) - 1;
1172 if (copy_from_user(buffer, buf, count))
1173 return -EFAULT;
1174 make_it_fail = simple_strtol(buffer, &end, 0);
1175 if (*end == '\n')
1176 end++;
1177 task = get_proc_task(file->f_dentry->d_inode);
1178 if (!task)
1179 return -ESRCH;
1180 task->make_it_fail = make_it_fail;
1181 put_task_struct(task);
1182 if (end - buffer == 0)
1183 return -EIO;
1184 return end - buffer;
1187 static const struct file_operations proc_fault_inject_operations = {
1188 .read = proc_fault_inject_read,
1189 .write = proc_fault_inject_write,
1191 #endif
1194 #ifdef CONFIG_SCHED_DEBUG
1196 * Print out various scheduling related per-task fields:
1198 static int sched_show(struct seq_file *m, void *v)
1200 struct inode *inode = m->private;
1201 struct task_struct *p;
1203 p = get_proc_task(inode);
1204 if (!p)
1205 return -ESRCH;
1206 proc_sched_show_task(p, m);
1208 put_task_struct(p);
1210 return 0;
1213 static ssize_t
1214 sched_write(struct file *file, const char __user *buf,
1215 size_t count, loff_t *offset)
1217 struct inode *inode = file->f_path.dentry->d_inode;
1218 struct task_struct *p;
1220 p = get_proc_task(inode);
1221 if (!p)
1222 return -ESRCH;
1223 proc_sched_set_task(p);
1225 put_task_struct(p);
1227 return count;
1230 static int sched_open(struct inode *inode, struct file *filp)
1232 int ret;
1234 ret = single_open(filp, sched_show, NULL);
1235 if (!ret) {
1236 struct seq_file *m = filp->private_data;
1238 m->private = inode;
1240 return ret;
1243 static const struct file_operations proc_pid_sched_operations = {
1244 .open = sched_open,
1245 .read = seq_read,
1246 .write = sched_write,
1247 .llseek = seq_lseek,
1248 .release = single_release,
1251 #endif
1254 * We added or removed a vma mapping the executable. The vmas are only mapped
1255 * during exec and are not mapped with the mmap system call.
1256 * Callers must hold down_write() on the mm's mmap_sem for these
1258 void added_exe_file_vma(struct mm_struct *mm)
1260 mm->num_exe_file_vmas++;
1263 void removed_exe_file_vma(struct mm_struct *mm)
1265 mm->num_exe_file_vmas--;
1266 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1267 fput(mm->exe_file);
1268 mm->exe_file = NULL;
1273 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1275 if (new_exe_file)
1276 get_file(new_exe_file);
1277 if (mm->exe_file)
1278 fput(mm->exe_file);
1279 mm->exe_file = new_exe_file;
1280 mm->num_exe_file_vmas = 0;
1283 struct file *get_mm_exe_file(struct mm_struct *mm)
1285 struct file *exe_file;
1287 /* We need mmap_sem to protect against races with removal of
1288 * VM_EXECUTABLE vmas */
1289 down_read(&mm->mmap_sem);
1290 exe_file = mm->exe_file;
1291 if (exe_file)
1292 get_file(exe_file);
1293 up_read(&mm->mmap_sem);
1294 return exe_file;
1297 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1299 /* It's safe to write the exe_file pointer without exe_file_lock because
1300 * this is called during fork when the task is not yet in /proc */
1301 newmm->exe_file = get_mm_exe_file(oldmm);
1304 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1306 struct task_struct *task;
1307 struct mm_struct *mm;
1308 struct file *exe_file;
1310 task = get_proc_task(inode);
1311 if (!task)
1312 return -ENOENT;
1313 mm = get_task_mm(task);
1314 put_task_struct(task);
1315 if (!mm)
1316 return -ENOENT;
1317 exe_file = get_mm_exe_file(mm);
1318 mmput(mm);
1319 if (exe_file) {
1320 *exe_path = exe_file->f_path;
1321 path_get(&exe_file->f_path);
1322 fput(exe_file);
1323 return 0;
1324 } else
1325 return -ENOENT;
1328 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1330 struct inode *inode = dentry->d_inode;
1331 int error = -EACCES;
1333 /* We don't need a base pointer in the /proc filesystem */
1334 path_put(&nd->path);
1336 /* Are we allowed to snoop on the tasks file descriptors? */
1337 if (!proc_fd_access_allowed(inode))
1338 goto out;
1340 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1341 nd->last_type = LAST_BIND;
1342 out:
1343 return ERR_PTR(error);
1346 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1348 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1349 char *pathname;
1350 int len;
1352 if (!tmp)
1353 return -ENOMEM;
1355 pathname = d_path(path, tmp, PAGE_SIZE);
1356 len = PTR_ERR(pathname);
1357 if (IS_ERR(pathname))
1358 goto out;
1359 len = tmp + PAGE_SIZE - 1 - pathname;
1361 if (len > buflen)
1362 len = buflen;
1363 if (copy_to_user(buffer, pathname, len))
1364 len = -EFAULT;
1365 out:
1366 free_page((unsigned long)tmp);
1367 return len;
1370 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1372 int error = -EACCES;
1373 struct inode *inode = dentry->d_inode;
1374 struct path path;
1376 /* Are we allowed to snoop on the tasks file descriptors? */
1377 if (!proc_fd_access_allowed(inode))
1378 goto out;
1380 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1381 if (error)
1382 goto out;
1384 error = do_proc_readlink(&path, buffer, buflen);
1385 path_put(&path);
1386 out:
1387 return error;
1390 static const struct inode_operations proc_pid_link_inode_operations = {
1391 .readlink = proc_pid_readlink,
1392 .follow_link = proc_pid_follow_link,
1393 .setattr = proc_setattr,
1397 /* building an inode */
1399 static int task_dumpable(struct task_struct *task)
1401 int dumpable = 0;
1402 struct mm_struct *mm;
1404 task_lock(task);
1405 mm = task->mm;
1406 if (mm)
1407 dumpable = get_dumpable(mm);
1408 task_unlock(task);
1409 if(dumpable == 1)
1410 return 1;
1411 return 0;
1415 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1417 struct inode * inode;
1418 struct proc_inode *ei;
1419 const struct cred *cred;
1421 /* We need a new inode */
1423 inode = new_inode(sb);
1424 if (!inode)
1425 goto out;
1427 /* Common stuff */
1428 ei = PROC_I(inode);
1429 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1430 inode->i_op = &proc_def_inode_operations;
1433 * grab the reference to task.
1435 ei->pid = get_task_pid(task, PIDTYPE_PID);
1436 if (!ei->pid)
1437 goto out_unlock;
1439 if (task_dumpable(task)) {
1440 rcu_read_lock();
1441 cred = __task_cred(task);
1442 inode->i_uid = cred->euid;
1443 inode->i_gid = cred->egid;
1444 rcu_read_unlock();
1446 security_task_to_inode(task, inode);
1448 out:
1449 return inode;
1451 out_unlock:
1452 iput(inode);
1453 return NULL;
1456 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1458 struct inode *inode = dentry->d_inode;
1459 struct task_struct *task;
1460 const struct cred *cred;
1462 generic_fillattr(inode, stat);
1464 rcu_read_lock();
1465 stat->uid = 0;
1466 stat->gid = 0;
1467 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1468 if (task) {
1469 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1470 task_dumpable(task)) {
1471 cred = __task_cred(task);
1472 stat->uid = cred->euid;
1473 stat->gid = cred->egid;
1476 rcu_read_unlock();
1477 return 0;
1480 /* dentry stuff */
1483 * Exceptional case: normally we are not allowed to unhash a busy
1484 * directory. In this case, however, we can do it - no aliasing problems
1485 * due to the way we treat inodes.
1487 * Rewrite the inode's ownerships here because the owning task may have
1488 * performed a setuid(), etc.
1490 * Before the /proc/pid/status file was created the only way to read
1491 * the effective uid of a /process was to stat /proc/pid. Reading
1492 * /proc/pid/status is slow enough that procps and other packages
1493 * kept stating /proc/pid. To keep the rules in /proc simple I have
1494 * made this apply to all per process world readable and executable
1495 * directories.
1497 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1499 struct inode *inode = dentry->d_inode;
1500 struct task_struct *task = get_proc_task(inode);
1501 const struct cred *cred;
1503 if (task) {
1504 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1505 task_dumpable(task)) {
1506 rcu_read_lock();
1507 cred = __task_cred(task);
1508 inode->i_uid = cred->euid;
1509 inode->i_gid = cred->egid;
1510 rcu_read_unlock();
1511 } else {
1512 inode->i_uid = 0;
1513 inode->i_gid = 0;
1515 inode->i_mode &= ~(S_ISUID | S_ISGID);
1516 security_task_to_inode(task, inode);
1517 put_task_struct(task);
1518 return 1;
1520 d_drop(dentry);
1521 return 0;
1524 static int pid_delete_dentry(struct dentry * dentry)
1526 /* Is the task we represent dead?
1527 * If so, then don't put the dentry on the lru list,
1528 * kill it immediately.
1530 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1533 static struct dentry_operations pid_dentry_operations =
1535 .d_revalidate = pid_revalidate,
1536 .d_delete = pid_delete_dentry,
1539 /* Lookups */
1541 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1542 struct task_struct *, const void *);
1545 * Fill a directory entry.
1547 * If possible create the dcache entry and derive our inode number and
1548 * file type from dcache entry.
1550 * Since all of the proc inode numbers are dynamically generated, the inode
1551 * numbers do not exist until the inode is cache. This means creating the
1552 * the dcache entry in readdir is necessary to keep the inode numbers
1553 * reported by readdir in sync with the inode numbers reported
1554 * by stat.
1556 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1557 char *name, int len,
1558 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1560 struct dentry *child, *dir = filp->f_path.dentry;
1561 struct inode *inode;
1562 struct qstr qname;
1563 ino_t ino = 0;
1564 unsigned type = DT_UNKNOWN;
1566 qname.name = name;
1567 qname.len = len;
1568 qname.hash = full_name_hash(name, len);
1570 child = d_lookup(dir, &qname);
1571 if (!child) {
1572 struct dentry *new;
1573 new = d_alloc(dir, &qname);
1574 if (new) {
1575 child = instantiate(dir->d_inode, new, task, ptr);
1576 if (child)
1577 dput(new);
1578 else
1579 child = new;
1582 if (!child || IS_ERR(child) || !child->d_inode)
1583 goto end_instantiate;
1584 inode = child->d_inode;
1585 if (inode) {
1586 ino = inode->i_ino;
1587 type = inode->i_mode >> 12;
1589 dput(child);
1590 end_instantiate:
1591 if (!ino)
1592 ino = find_inode_number(dir, &qname);
1593 if (!ino)
1594 ino = 1;
1595 return filldir(dirent, name, len, filp->f_pos, ino, type);
1598 static unsigned name_to_int(struct dentry *dentry)
1600 const char *name = dentry->d_name.name;
1601 int len = dentry->d_name.len;
1602 unsigned n = 0;
1604 if (len > 1 && *name == '0')
1605 goto out;
1606 while (len-- > 0) {
1607 unsigned c = *name++ - '0';
1608 if (c > 9)
1609 goto out;
1610 if (n >= (~0U-9)/10)
1611 goto out;
1612 n *= 10;
1613 n += c;
1615 return n;
1616 out:
1617 return ~0U;
1620 #define PROC_FDINFO_MAX 64
1622 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1624 struct task_struct *task = get_proc_task(inode);
1625 struct files_struct *files = NULL;
1626 struct file *file;
1627 int fd = proc_fd(inode);
1629 if (task) {
1630 files = get_files_struct(task);
1631 put_task_struct(task);
1633 if (files) {
1635 * We are not taking a ref to the file structure, so we must
1636 * hold ->file_lock.
1638 spin_lock(&files->file_lock);
1639 file = fcheck_files(files, fd);
1640 if (file) {
1641 if (path) {
1642 *path = file->f_path;
1643 path_get(&file->f_path);
1645 if (info)
1646 snprintf(info, PROC_FDINFO_MAX,
1647 "pos:\t%lli\n"
1648 "flags:\t0%o\n",
1649 (long long) file->f_pos,
1650 file->f_flags);
1651 spin_unlock(&files->file_lock);
1652 put_files_struct(files);
1653 return 0;
1655 spin_unlock(&files->file_lock);
1656 put_files_struct(files);
1658 return -ENOENT;
1661 static int proc_fd_link(struct inode *inode, struct path *path)
1663 return proc_fd_info(inode, path, NULL);
1666 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1668 struct inode *inode = dentry->d_inode;
1669 struct task_struct *task = get_proc_task(inode);
1670 int fd = proc_fd(inode);
1671 struct files_struct *files;
1672 const struct cred *cred;
1674 if (task) {
1675 files = get_files_struct(task);
1676 if (files) {
1677 rcu_read_lock();
1678 if (fcheck_files(files, fd)) {
1679 rcu_read_unlock();
1680 put_files_struct(files);
1681 if (task_dumpable(task)) {
1682 rcu_read_lock();
1683 cred = __task_cred(task);
1684 inode->i_uid = cred->euid;
1685 inode->i_gid = cred->egid;
1686 rcu_read_unlock();
1687 } else {
1688 inode->i_uid = 0;
1689 inode->i_gid = 0;
1691 inode->i_mode &= ~(S_ISUID | S_ISGID);
1692 security_task_to_inode(task, inode);
1693 put_task_struct(task);
1694 return 1;
1696 rcu_read_unlock();
1697 put_files_struct(files);
1699 put_task_struct(task);
1701 d_drop(dentry);
1702 return 0;
1705 static struct dentry_operations tid_fd_dentry_operations =
1707 .d_revalidate = tid_fd_revalidate,
1708 .d_delete = pid_delete_dentry,
1711 static struct dentry *proc_fd_instantiate(struct inode *dir,
1712 struct dentry *dentry, struct task_struct *task, const void *ptr)
1714 unsigned fd = *(const unsigned *)ptr;
1715 struct file *file;
1716 struct files_struct *files;
1717 struct inode *inode;
1718 struct proc_inode *ei;
1719 struct dentry *error = ERR_PTR(-ENOENT);
1721 inode = proc_pid_make_inode(dir->i_sb, task);
1722 if (!inode)
1723 goto out;
1724 ei = PROC_I(inode);
1725 ei->fd = fd;
1726 files = get_files_struct(task);
1727 if (!files)
1728 goto out_iput;
1729 inode->i_mode = S_IFLNK;
1732 * We are not taking a ref to the file structure, so we must
1733 * hold ->file_lock.
1735 spin_lock(&files->file_lock);
1736 file = fcheck_files(files, fd);
1737 if (!file)
1738 goto out_unlock;
1739 if (file->f_mode & FMODE_READ)
1740 inode->i_mode |= S_IRUSR | S_IXUSR;
1741 if (file->f_mode & FMODE_WRITE)
1742 inode->i_mode |= S_IWUSR | S_IXUSR;
1743 spin_unlock(&files->file_lock);
1744 put_files_struct(files);
1746 inode->i_op = &proc_pid_link_inode_operations;
1747 inode->i_size = 64;
1748 ei->op.proc_get_link = proc_fd_link;
1749 dentry->d_op = &tid_fd_dentry_operations;
1750 d_add(dentry, inode);
1751 /* Close the race of the process dying before we return the dentry */
1752 if (tid_fd_revalidate(dentry, NULL))
1753 error = NULL;
1755 out:
1756 return error;
1757 out_unlock:
1758 spin_unlock(&files->file_lock);
1759 put_files_struct(files);
1760 out_iput:
1761 iput(inode);
1762 goto out;
1765 static struct dentry *proc_lookupfd_common(struct inode *dir,
1766 struct dentry *dentry,
1767 instantiate_t instantiate)
1769 struct task_struct *task = get_proc_task(dir);
1770 unsigned fd = name_to_int(dentry);
1771 struct dentry *result = ERR_PTR(-ENOENT);
1773 if (!task)
1774 goto out_no_task;
1775 if (fd == ~0U)
1776 goto out;
1778 result = instantiate(dir, dentry, task, &fd);
1779 out:
1780 put_task_struct(task);
1781 out_no_task:
1782 return result;
1785 static int proc_readfd_common(struct file * filp, void * dirent,
1786 filldir_t filldir, instantiate_t instantiate)
1788 struct dentry *dentry = filp->f_path.dentry;
1789 struct inode *inode = dentry->d_inode;
1790 struct task_struct *p = get_proc_task(inode);
1791 unsigned int fd, ino;
1792 int retval;
1793 struct files_struct * files;
1795 retval = -ENOENT;
1796 if (!p)
1797 goto out_no_task;
1798 retval = 0;
1800 fd = filp->f_pos;
1801 switch (fd) {
1802 case 0:
1803 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1804 goto out;
1805 filp->f_pos++;
1806 case 1:
1807 ino = parent_ino(dentry);
1808 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1809 goto out;
1810 filp->f_pos++;
1811 default:
1812 files = get_files_struct(p);
1813 if (!files)
1814 goto out;
1815 rcu_read_lock();
1816 for (fd = filp->f_pos-2;
1817 fd < files_fdtable(files)->max_fds;
1818 fd++, filp->f_pos++) {
1819 char name[PROC_NUMBUF];
1820 int len;
1822 if (!fcheck_files(files, fd))
1823 continue;
1824 rcu_read_unlock();
1826 len = snprintf(name, sizeof(name), "%d", fd);
1827 if (proc_fill_cache(filp, dirent, filldir,
1828 name, len, instantiate,
1829 p, &fd) < 0) {
1830 rcu_read_lock();
1831 break;
1833 rcu_read_lock();
1835 rcu_read_unlock();
1836 put_files_struct(files);
1838 out:
1839 put_task_struct(p);
1840 out_no_task:
1841 return retval;
1844 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1845 struct nameidata *nd)
1847 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1850 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1852 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1855 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1856 size_t len, loff_t *ppos)
1858 char tmp[PROC_FDINFO_MAX];
1859 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1860 if (!err)
1861 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1862 return err;
1865 static const struct file_operations proc_fdinfo_file_operations = {
1866 .open = nonseekable_open,
1867 .read = proc_fdinfo_read,
1870 static const struct file_operations proc_fd_operations = {
1871 .read = generic_read_dir,
1872 .readdir = proc_readfd,
1876 * /proc/pid/fd needs a special permission handler so that a process can still
1877 * access /proc/self/fd after it has executed a setuid().
1879 static int proc_fd_permission(struct inode *inode, int mask)
1881 int rv;
1883 rv = generic_permission(inode, mask, NULL);
1884 if (rv == 0)
1885 return 0;
1886 if (task_pid(current) == proc_pid(inode))
1887 rv = 0;
1888 return rv;
1892 * proc directories can do almost nothing..
1894 static const struct inode_operations proc_fd_inode_operations = {
1895 .lookup = proc_lookupfd,
1896 .permission = proc_fd_permission,
1897 .setattr = proc_setattr,
1900 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1901 struct dentry *dentry, struct task_struct *task, const void *ptr)
1903 unsigned fd = *(unsigned *)ptr;
1904 struct inode *inode;
1905 struct proc_inode *ei;
1906 struct dentry *error = ERR_PTR(-ENOENT);
1908 inode = proc_pid_make_inode(dir->i_sb, task);
1909 if (!inode)
1910 goto out;
1911 ei = PROC_I(inode);
1912 ei->fd = fd;
1913 inode->i_mode = S_IFREG | S_IRUSR;
1914 inode->i_fop = &proc_fdinfo_file_operations;
1915 dentry->d_op = &tid_fd_dentry_operations;
1916 d_add(dentry, inode);
1917 /* Close the race of the process dying before we return the dentry */
1918 if (tid_fd_revalidate(dentry, NULL))
1919 error = NULL;
1921 out:
1922 return error;
1925 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1926 struct dentry *dentry,
1927 struct nameidata *nd)
1929 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1932 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1934 return proc_readfd_common(filp, dirent, filldir,
1935 proc_fdinfo_instantiate);
1938 static const struct file_operations proc_fdinfo_operations = {
1939 .read = generic_read_dir,
1940 .readdir = proc_readfdinfo,
1944 * proc directories can do almost nothing..
1946 static const struct inode_operations proc_fdinfo_inode_operations = {
1947 .lookup = proc_lookupfdinfo,
1948 .setattr = proc_setattr,
1952 static struct dentry *proc_pident_instantiate(struct inode *dir,
1953 struct dentry *dentry, struct task_struct *task, const void *ptr)
1955 const struct pid_entry *p = ptr;
1956 struct inode *inode;
1957 struct proc_inode *ei;
1958 struct dentry *error = ERR_PTR(-EINVAL);
1960 inode = proc_pid_make_inode(dir->i_sb, task);
1961 if (!inode)
1962 goto out;
1964 ei = PROC_I(inode);
1965 inode->i_mode = p->mode;
1966 if (S_ISDIR(inode->i_mode))
1967 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1968 if (p->iop)
1969 inode->i_op = p->iop;
1970 if (p->fop)
1971 inode->i_fop = p->fop;
1972 ei->op = p->op;
1973 dentry->d_op = &pid_dentry_operations;
1974 d_add(dentry, inode);
1975 /* Close the race of the process dying before we return the dentry */
1976 if (pid_revalidate(dentry, NULL))
1977 error = NULL;
1978 out:
1979 return error;
1982 static struct dentry *proc_pident_lookup(struct inode *dir,
1983 struct dentry *dentry,
1984 const struct pid_entry *ents,
1985 unsigned int nents)
1987 struct dentry *error;
1988 struct task_struct *task = get_proc_task(dir);
1989 const struct pid_entry *p, *last;
1991 error = ERR_PTR(-ENOENT);
1993 if (!task)
1994 goto out_no_task;
1997 * Yes, it does not scale. And it should not. Don't add
1998 * new entries into /proc/<tgid>/ without very good reasons.
2000 last = &ents[nents - 1];
2001 for (p = ents; p <= last; p++) {
2002 if (p->len != dentry->d_name.len)
2003 continue;
2004 if (!memcmp(dentry->d_name.name, p->name, p->len))
2005 break;
2007 if (p > last)
2008 goto out;
2010 error = proc_pident_instantiate(dir, dentry, task, p);
2011 out:
2012 put_task_struct(task);
2013 out_no_task:
2014 return error;
2017 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2018 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2020 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2021 proc_pident_instantiate, task, p);
2024 static int proc_pident_readdir(struct file *filp,
2025 void *dirent, filldir_t filldir,
2026 const struct pid_entry *ents, unsigned int nents)
2028 int i;
2029 struct dentry *dentry = filp->f_path.dentry;
2030 struct inode *inode = dentry->d_inode;
2031 struct task_struct *task = get_proc_task(inode);
2032 const struct pid_entry *p, *last;
2033 ino_t ino;
2034 int ret;
2036 ret = -ENOENT;
2037 if (!task)
2038 goto out_no_task;
2040 ret = 0;
2041 i = filp->f_pos;
2042 switch (i) {
2043 case 0:
2044 ino = inode->i_ino;
2045 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2046 goto out;
2047 i++;
2048 filp->f_pos++;
2049 /* fall through */
2050 case 1:
2051 ino = parent_ino(dentry);
2052 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2053 goto out;
2054 i++;
2055 filp->f_pos++;
2056 /* fall through */
2057 default:
2058 i -= 2;
2059 if (i >= nents) {
2060 ret = 1;
2061 goto out;
2063 p = ents + i;
2064 last = &ents[nents - 1];
2065 while (p <= last) {
2066 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2067 goto out;
2068 filp->f_pos++;
2069 p++;
2073 ret = 1;
2074 out:
2075 put_task_struct(task);
2076 out_no_task:
2077 return ret;
2080 #ifdef CONFIG_SECURITY
2081 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2082 size_t count, loff_t *ppos)
2084 struct inode * inode = file->f_path.dentry->d_inode;
2085 char *p = NULL;
2086 ssize_t length;
2087 struct task_struct *task = get_proc_task(inode);
2089 if (!task)
2090 return -ESRCH;
2092 length = security_getprocattr(task,
2093 (char*)file->f_path.dentry->d_name.name,
2094 &p);
2095 put_task_struct(task);
2096 if (length > 0)
2097 length = simple_read_from_buffer(buf, count, ppos, p, length);
2098 kfree(p);
2099 return length;
2102 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2103 size_t count, loff_t *ppos)
2105 struct inode * inode = file->f_path.dentry->d_inode;
2106 char *page;
2107 ssize_t length;
2108 struct task_struct *task = get_proc_task(inode);
2110 length = -ESRCH;
2111 if (!task)
2112 goto out_no_task;
2113 if (count > PAGE_SIZE)
2114 count = PAGE_SIZE;
2116 /* No partial writes. */
2117 length = -EINVAL;
2118 if (*ppos != 0)
2119 goto out;
2121 length = -ENOMEM;
2122 page = (char*)__get_free_page(GFP_TEMPORARY);
2123 if (!page)
2124 goto out;
2126 length = -EFAULT;
2127 if (copy_from_user(page, buf, count))
2128 goto out_free;
2130 length = security_setprocattr(task,
2131 (char*)file->f_path.dentry->d_name.name,
2132 (void*)page, count);
2133 out_free:
2134 free_page((unsigned long) page);
2135 out:
2136 put_task_struct(task);
2137 out_no_task:
2138 return length;
2141 static const struct file_operations proc_pid_attr_operations = {
2142 .read = proc_pid_attr_read,
2143 .write = proc_pid_attr_write,
2146 static const struct pid_entry attr_dir_stuff[] = {
2147 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2148 REG("prev", S_IRUGO, proc_pid_attr_operations),
2149 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2150 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2151 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2152 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2155 static int proc_attr_dir_readdir(struct file * filp,
2156 void * dirent, filldir_t filldir)
2158 return proc_pident_readdir(filp,dirent,filldir,
2159 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2162 static const struct file_operations proc_attr_dir_operations = {
2163 .read = generic_read_dir,
2164 .readdir = proc_attr_dir_readdir,
2167 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2168 struct dentry *dentry, struct nameidata *nd)
2170 return proc_pident_lookup(dir, dentry,
2171 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2174 static const struct inode_operations proc_attr_dir_inode_operations = {
2175 .lookup = proc_attr_dir_lookup,
2176 .getattr = pid_getattr,
2177 .setattr = proc_setattr,
2180 #endif
2182 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2183 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2184 size_t count, loff_t *ppos)
2186 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2187 struct mm_struct *mm;
2188 char buffer[PROC_NUMBUF];
2189 size_t len;
2190 int ret;
2192 if (!task)
2193 return -ESRCH;
2195 ret = 0;
2196 mm = get_task_mm(task);
2197 if (mm) {
2198 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2199 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2200 MMF_DUMP_FILTER_SHIFT));
2201 mmput(mm);
2202 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2205 put_task_struct(task);
2207 return ret;
2210 static ssize_t proc_coredump_filter_write(struct file *file,
2211 const char __user *buf,
2212 size_t count,
2213 loff_t *ppos)
2215 struct task_struct *task;
2216 struct mm_struct *mm;
2217 char buffer[PROC_NUMBUF], *end;
2218 unsigned int val;
2219 int ret;
2220 int i;
2221 unsigned long mask;
2223 ret = -EFAULT;
2224 memset(buffer, 0, sizeof(buffer));
2225 if (count > sizeof(buffer) - 1)
2226 count = sizeof(buffer) - 1;
2227 if (copy_from_user(buffer, buf, count))
2228 goto out_no_task;
2230 ret = -EINVAL;
2231 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2232 if (*end == '\n')
2233 end++;
2234 if (end - buffer == 0)
2235 goto out_no_task;
2237 ret = -ESRCH;
2238 task = get_proc_task(file->f_dentry->d_inode);
2239 if (!task)
2240 goto out_no_task;
2242 ret = end - buffer;
2243 mm = get_task_mm(task);
2244 if (!mm)
2245 goto out_no_mm;
2247 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2248 if (val & mask)
2249 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2250 else
2251 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2254 mmput(mm);
2255 out_no_mm:
2256 put_task_struct(task);
2257 out_no_task:
2258 return ret;
2261 static const struct file_operations proc_coredump_filter_operations = {
2262 .read = proc_coredump_filter_read,
2263 .write = proc_coredump_filter_write,
2265 #endif
2268 * /proc/self:
2270 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2271 int buflen)
2273 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2274 pid_t tgid = task_tgid_nr_ns(current, ns);
2275 char tmp[PROC_NUMBUF];
2276 if (!tgid)
2277 return -ENOENT;
2278 sprintf(tmp, "%d", tgid);
2279 return vfs_readlink(dentry,buffer,buflen,tmp);
2282 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2284 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2285 pid_t tgid = task_tgid_nr_ns(current, ns);
2286 char tmp[PROC_NUMBUF];
2287 if (!tgid)
2288 return ERR_PTR(-ENOENT);
2289 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2290 return ERR_PTR(vfs_follow_link(nd,tmp));
2293 static const struct inode_operations proc_self_inode_operations = {
2294 .readlink = proc_self_readlink,
2295 .follow_link = proc_self_follow_link,
2299 * proc base
2301 * These are the directory entries in the root directory of /proc
2302 * that properly belong to the /proc filesystem, as they describe
2303 * describe something that is process related.
2305 static const struct pid_entry proc_base_stuff[] = {
2306 NOD("self", S_IFLNK|S_IRWXUGO,
2307 &proc_self_inode_operations, NULL, {}),
2311 * Exceptional case: normally we are not allowed to unhash a busy
2312 * directory. In this case, however, we can do it - no aliasing problems
2313 * due to the way we treat inodes.
2315 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2317 struct inode *inode = dentry->d_inode;
2318 struct task_struct *task = get_proc_task(inode);
2319 if (task) {
2320 put_task_struct(task);
2321 return 1;
2323 d_drop(dentry);
2324 return 0;
2327 static struct dentry_operations proc_base_dentry_operations =
2329 .d_revalidate = proc_base_revalidate,
2330 .d_delete = pid_delete_dentry,
2333 static struct dentry *proc_base_instantiate(struct inode *dir,
2334 struct dentry *dentry, struct task_struct *task, const void *ptr)
2336 const struct pid_entry *p = ptr;
2337 struct inode *inode;
2338 struct proc_inode *ei;
2339 struct dentry *error = ERR_PTR(-EINVAL);
2341 /* Allocate the inode */
2342 error = ERR_PTR(-ENOMEM);
2343 inode = new_inode(dir->i_sb);
2344 if (!inode)
2345 goto out;
2347 /* Initialize the inode */
2348 ei = PROC_I(inode);
2349 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2352 * grab the reference to the task.
2354 ei->pid = get_task_pid(task, PIDTYPE_PID);
2355 if (!ei->pid)
2356 goto out_iput;
2358 inode->i_mode = p->mode;
2359 if (S_ISDIR(inode->i_mode))
2360 inode->i_nlink = 2;
2361 if (S_ISLNK(inode->i_mode))
2362 inode->i_size = 64;
2363 if (p->iop)
2364 inode->i_op = p->iop;
2365 if (p->fop)
2366 inode->i_fop = p->fop;
2367 ei->op = p->op;
2368 dentry->d_op = &proc_base_dentry_operations;
2369 d_add(dentry, inode);
2370 error = NULL;
2371 out:
2372 return error;
2373 out_iput:
2374 iput(inode);
2375 goto out;
2378 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2380 struct dentry *error;
2381 struct task_struct *task = get_proc_task(dir);
2382 const struct pid_entry *p, *last;
2384 error = ERR_PTR(-ENOENT);
2386 if (!task)
2387 goto out_no_task;
2389 /* Lookup the directory entry */
2390 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2391 for (p = proc_base_stuff; p <= last; p++) {
2392 if (p->len != dentry->d_name.len)
2393 continue;
2394 if (!memcmp(dentry->d_name.name, p->name, p->len))
2395 break;
2397 if (p > last)
2398 goto out;
2400 error = proc_base_instantiate(dir, dentry, task, p);
2402 out:
2403 put_task_struct(task);
2404 out_no_task:
2405 return error;
2408 static int proc_base_fill_cache(struct file *filp, void *dirent,
2409 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2411 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2412 proc_base_instantiate, task, p);
2415 #ifdef CONFIG_TASK_IO_ACCOUNTING
2416 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2418 struct task_io_accounting acct = task->ioac;
2419 unsigned long flags;
2421 if (whole && lock_task_sighand(task, &flags)) {
2422 struct task_struct *t = task;
2424 task_io_accounting_add(&acct, &task->signal->ioac);
2425 while_each_thread(task, t)
2426 task_io_accounting_add(&acct, &t->ioac);
2428 unlock_task_sighand(task, &flags);
2430 return sprintf(buffer,
2431 "rchar: %llu\n"
2432 "wchar: %llu\n"
2433 "syscr: %llu\n"
2434 "syscw: %llu\n"
2435 "read_bytes: %llu\n"
2436 "write_bytes: %llu\n"
2437 "cancelled_write_bytes: %llu\n",
2438 (unsigned long long)acct.rchar,
2439 (unsigned long long)acct.wchar,
2440 (unsigned long long)acct.syscr,
2441 (unsigned long long)acct.syscw,
2442 (unsigned long long)acct.read_bytes,
2443 (unsigned long long)acct.write_bytes,
2444 (unsigned long long)acct.cancelled_write_bytes);
2447 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2449 return do_io_accounting(task, buffer, 0);
2452 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2454 return do_io_accounting(task, buffer, 1);
2456 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2458 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2459 struct pid *pid, struct task_struct *task)
2461 seq_printf(m, "%08x\n", task->personality);
2462 return 0;
2466 * Thread groups
2468 static const struct file_operations proc_task_operations;
2469 static const struct inode_operations proc_task_inode_operations;
2471 static const struct pid_entry tgid_base_stuff[] = {
2472 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2473 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2474 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2475 #ifdef CONFIG_NET
2476 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2477 #endif
2478 REG("environ", S_IRUSR, proc_environ_operations),
2479 INF("auxv", S_IRUSR, proc_pid_auxv),
2480 ONE("status", S_IRUGO, proc_pid_status),
2481 ONE("personality", S_IRUSR, proc_pid_personality),
2482 INF("limits", S_IRUSR, proc_pid_limits),
2483 #ifdef CONFIG_SCHED_DEBUG
2484 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2485 #endif
2486 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2487 INF("syscall", S_IRUSR, proc_pid_syscall),
2488 #endif
2489 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2490 ONE("stat", S_IRUGO, proc_tgid_stat),
2491 ONE("statm", S_IRUGO, proc_pid_statm),
2492 REG("maps", S_IRUGO, proc_maps_operations),
2493 #ifdef CONFIG_NUMA
2494 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2495 #endif
2496 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2497 LNK("cwd", proc_cwd_link),
2498 LNK("root", proc_root_link),
2499 LNK("exe", proc_exe_link),
2500 REG("mounts", S_IRUGO, proc_mounts_operations),
2501 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2502 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2503 #ifdef CONFIG_PROC_PAGE_MONITOR
2504 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2505 REG("smaps", S_IRUGO, proc_smaps_operations),
2506 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2507 #endif
2508 #ifdef CONFIG_SECURITY
2509 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2510 #endif
2511 #ifdef CONFIG_KALLSYMS
2512 INF("wchan", S_IRUGO, proc_pid_wchan),
2513 #endif
2514 #ifdef CONFIG_STACKTRACE
2515 ONE("stack", S_IRUSR, proc_pid_stack),
2516 #endif
2517 #ifdef CONFIG_SCHEDSTATS
2518 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2519 #endif
2520 #ifdef CONFIG_LATENCYTOP
2521 REG("latency", S_IRUGO, proc_lstats_operations),
2522 #endif
2523 #ifdef CONFIG_PROC_PID_CPUSET
2524 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2525 #endif
2526 #ifdef CONFIG_CGROUPS
2527 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2528 #endif
2529 INF("oom_score", S_IRUGO, proc_oom_score),
2530 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2531 #ifdef CONFIG_AUDITSYSCALL
2532 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2533 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2534 #endif
2535 #ifdef CONFIG_FAULT_INJECTION
2536 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2537 #endif
2538 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2539 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2540 #endif
2541 #ifdef CONFIG_TASK_IO_ACCOUNTING
2542 INF("io", S_IRUGO, proc_tgid_io_accounting),
2543 #endif
2546 static int proc_tgid_base_readdir(struct file * filp,
2547 void * dirent, filldir_t filldir)
2549 return proc_pident_readdir(filp,dirent,filldir,
2550 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2553 static const struct file_operations proc_tgid_base_operations = {
2554 .read = generic_read_dir,
2555 .readdir = proc_tgid_base_readdir,
2558 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2559 return proc_pident_lookup(dir, dentry,
2560 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2563 static const struct inode_operations proc_tgid_base_inode_operations = {
2564 .lookup = proc_tgid_base_lookup,
2565 .getattr = pid_getattr,
2566 .setattr = proc_setattr,
2569 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2571 struct dentry *dentry, *leader, *dir;
2572 char buf[PROC_NUMBUF];
2573 struct qstr name;
2575 name.name = buf;
2576 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2577 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2578 if (dentry) {
2579 if (!(current->flags & PF_EXITING))
2580 shrink_dcache_parent(dentry);
2581 d_drop(dentry);
2582 dput(dentry);
2585 if (tgid == 0)
2586 goto out;
2588 name.name = buf;
2589 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2590 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2591 if (!leader)
2592 goto out;
2594 name.name = "task";
2595 name.len = strlen(name.name);
2596 dir = d_hash_and_lookup(leader, &name);
2597 if (!dir)
2598 goto out_put_leader;
2600 name.name = buf;
2601 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2602 dentry = d_hash_and_lookup(dir, &name);
2603 if (dentry) {
2604 shrink_dcache_parent(dentry);
2605 d_drop(dentry);
2606 dput(dentry);
2609 dput(dir);
2610 out_put_leader:
2611 dput(leader);
2612 out:
2613 return;
2617 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2618 * @task: task that should be flushed.
2620 * When flushing dentries from proc, one needs to flush them from global
2621 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2622 * in. This call is supposed to do all of this job.
2624 * Looks in the dcache for
2625 * /proc/@pid
2626 * /proc/@tgid/task/@pid
2627 * if either directory is present flushes it and all of it'ts children
2628 * from the dcache.
2630 * It is safe and reasonable to cache /proc entries for a task until
2631 * that task exits. After that they just clog up the dcache with
2632 * useless entries, possibly causing useful dcache entries to be
2633 * flushed instead. This routine is proved to flush those useless
2634 * dcache entries at process exit time.
2636 * NOTE: This routine is just an optimization so it does not guarantee
2637 * that no dcache entries will exist at process exit time it
2638 * just makes it very unlikely that any will persist.
2641 void proc_flush_task(struct task_struct *task)
2643 int i;
2644 struct pid *pid, *tgid = NULL;
2645 struct upid *upid;
2647 pid = task_pid(task);
2648 if (thread_group_leader(task))
2649 tgid = task_tgid(task);
2651 for (i = 0; i <= pid->level; i++) {
2652 upid = &pid->numbers[i];
2653 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2654 tgid ? tgid->numbers[i].nr : 0);
2657 upid = &pid->numbers[pid->level];
2658 if (upid->nr == 1)
2659 pid_ns_release_proc(upid->ns);
2662 static struct dentry *proc_pid_instantiate(struct inode *dir,
2663 struct dentry * dentry,
2664 struct task_struct *task, const void *ptr)
2666 struct dentry *error = ERR_PTR(-ENOENT);
2667 struct inode *inode;
2669 inode = proc_pid_make_inode(dir->i_sb, task);
2670 if (!inode)
2671 goto out;
2673 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2674 inode->i_op = &proc_tgid_base_inode_operations;
2675 inode->i_fop = &proc_tgid_base_operations;
2676 inode->i_flags|=S_IMMUTABLE;
2678 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2679 ARRAY_SIZE(tgid_base_stuff));
2681 dentry->d_op = &pid_dentry_operations;
2683 d_add(dentry, inode);
2684 /* Close the race of the process dying before we return the dentry */
2685 if (pid_revalidate(dentry, NULL))
2686 error = NULL;
2687 out:
2688 return error;
2691 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2693 struct dentry *result = ERR_PTR(-ENOENT);
2694 struct task_struct *task;
2695 unsigned tgid;
2696 struct pid_namespace *ns;
2698 result = proc_base_lookup(dir, dentry);
2699 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2700 goto out;
2702 tgid = name_to_int(dentry);
2703 if (tgid == ~0U)
2704 goto out;
2706 ns = dentry->d_sb->s_fs_info;
2707 rcu_read_lock();
2708 task = find_task_by_pid_ns(tgid, ns);
2709 if (task)
2710 get_task_struct(task);
2711 rcu_read_unlock();
2712 if (!task)
2713 goto out;
2715 result = proc_pid_instantiate(dir, dentry, task, NULL);
2716 put_task_struct(task);
2717 out:
2718 return result;
2722 * Find the first task with tgid >= tgid
2725 struct tgid_iter {
2726 unsigned int tgid;
2727 struct task_struct *task;
2729 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2731 struct pid *pid;
2733 if (iter.task)
2734 put_task_struct(iter.task);
2735 rcu_read_lock();
2736 retry:
2737 iter.task = NULL;
2738 pid = find_ge_pid(iter.tgid, ns);
2739 if (pid) {
2740 iter.tgid = pid_nr_ns(pid, ns);
2741 iter.task = pid_task(pid, PIDTYPE_PID);
2742 /* What we to know is if the pid we have find is the
2743 * pid of a thread_group_leader. Testing for task
2744 * being a thread_group_leader is the obvious thing
2745 * todo but there is a window when it fails, due to
2746 * the pid transfer logic in de_thread.
2748 * So we perform the straight forward test of seeing
2749 * if the pid we have found is the pid of a thread
2750 * group leader, and don't worry if the task we have
2751 * found doesn't happen to be a thread group leader.
2752 * As we don't care in the case of readdir.
2754 if (!iter.task || !has_group_leader_pid(iter.task)) {
2755 iter.tgid += 1;
2756 goto retry;
2758 get_task_struct(iter.task);
2760 rcu_read_unlock();
2761 return iter;
2764 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2766 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2767 struct tgid_iter iter)
2769 char name[PROC_NUMBUF];
2770 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2771 return proc_fill_cache(filp, dirent, filldir, name, len,
2772 proc_pid_instantiate, iter.task, NULL);
2775 /* for the /proc/ directory itself, after non-process stuff has been done */
2776 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2778 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2779 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2780 struct tgid_iter iter;
2781 struct pid_namespace *ns;
2783 if (!reaper)
2784 goto out_no_task;
2786 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2787 const struct pid_entry *p = &proc_base_stuff[nr];
2788 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2789 goto out;
2792 ns = filp->f_dentry->d_sb->s_fs_info;
2793 iter.task = NULL;
2794 iter.tgid = filp->f_pos - TGID_OFFSET;
2795 for (iter = next_tgid(ns, iter);
2796 iter.task;
2797 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2798 filp->f_pos = iter.tgid + TGID_OFFSET;
2799 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2800 put_task_struct(iter.task);
2801 goto out;
2804 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2805 out:
2806 put_task_struct(reaper);
2807 out_no_task:
2808 return 0;
2812 * Tasks
2814 static const struct pid_entry tid_base_stuff[] = {
2815 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2816 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2817 REG("environ", S_IRUSR, proc_environ_operations),
2818 INF("auxv", S_IRUSR, proc_pid_auxv),
2819 ONE("status", S_IRUGO, proc_pid_status),
2820 ONE("personality", S_IRUSR, proc_pid_personality),
2821 INF("limits", S_IRUSR, proc_pid_limits),
2822 #ifdef CONFIG_SCHED_DEBUG
2823 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2824 #endif
2825 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2826 INF("syscall", S_IRUSR, proc_pid_syscall),
2827 #endif
2828 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2829 ONE("stat", S_IRUGO, proc_tid_stat),
2830 ONE("statm", S_IRUGO, proc_pid_statm),
2831 REG("maps", S_IRUGO, proc_maps_operations),
2832 #ifdef CONFIG_NUMA
2833 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2834 #endif
2835 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2836 LNK("cwd", proc_cwd_link),
2837 LNK("root", proc_root_link),
2838 LNK("exe", proc_exe_link),
2839 REG("mounts", S_IRUGO, proc_mounts_operations),
2840 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2841 #ifdef CONFIG_PROC_PAGE_MONITOR
2842 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2843 REG("smaps", S_IRUGO, proc_smaps_operations),
2844 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2845 #endif
2846 #ifdef CONFIG_SECURITY
2847 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2848 #endif
2849 #ifdef CONFIG_KALLSYMS
2850 INF("wchan", S_IRUGO, proc_pid_wchan),
2851 #endif
2852 #ifdef CONFIG_STACKTRACE
2853 ONE("stack", S_IRUSR, proc_pid_stack),
2854 #endif
2855 #ifdef CONFIG_SCHEDSTATS
2856 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2857 #endif
2858 #ifdef CONFIG_LATENCYTOP
2859 REG("latency", S_IRUGO, proc_lstats_operations),
2860 #endif
2861 #ifdef CONFIG_PROC_PID_CPUSET
2862 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2863 #endif
2864 #ifdef CONFIG_CGROUPS
2865 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2866 #endif
2867 INF("oom_score", S_IRUGO, proc_oom_score),
2868 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2869 #ifdef CONFIG_AUDITSYSCALL
2870 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2871 REG("sessionid", S_IRUSR, proc_sessionid_operations),
2872 #endif
2873 #ifdef CONFIG_FAULT_INJECTION
2874 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2875 #endif
2876 #ifdef CONFIG_TASK_IO_ACCOUNTING
2877 INF("io", S_IRUGO, proc_tid_io_accounting),
2878 #endif
2881 static int proc_tid_base_readdir(struct file * filp,
2882 void * dirent, filldir_t filldir)
2884 return proc_pident_readdir(filp,dirent,filldir,
2885 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2888 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2889 return proc_pident_lookup(dir, dentry,
2890 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2893 static const struct file_operations proc_tid_base_operations = {
2894 .read = generic_read_dir,
2895 .readdir = proc_tid_base_readdir,
2898 static const struct inode_operations proc_tid_base_inode_operations = {
2899 .lookup = proc_tid_base_lookup,
2900 .getattr = pid_getattr,
2901 .setattr = proc_setattr,
2904 static struct dentry *proc_task_instantiate(struct inode *dir,
2905 struct dentry *dentry, struct task_struct *task, const void *ptr)
2907 struct dentry *error = ERR_PTR(-ENOENT);
2908 struct inode *inode;
2909 inode = proc_pid_make_inode(dir->i_sb, task);
2911 if (!inode)
2912 goto out;
2913 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2914 inode->i_op = &proc_tid_base_inode_operations;
2915 inode->i_fop = &proc_tid_base_operations;
2916 inode->i_flags|=S_IMMUTABLE;
2918 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2919 ARRAY_SIZE(tid_base_stuff));
2921 dentry->d_op = &pid_dentry_operations;
2923 d_add(dentry, inode);
2924 /* Close the race of the process dying before we return the dentry */
2925 if (pid_revalidate(dentry, NULL))
2926 error = NULL;
2927 out:
2928 return error;
2931 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2933 struct dentry *result = ERR_PTR(-ENOENT);
2934 struct task_struct *task;
2935 struct task_struct *leader = get_proc_task(dir);
2936 unsigned tid;
2937 struct pid_namespace *ns;
2939 if (!leader)
2940 goto out_no_task;
2942 tid = name_to_int(dentry);
2943 if (tid == ~0U)
2944 goto out;
2946 ns = dentry->d_sb->s_fs_info;
2947 rcu_read_lock();
2948 task = find_task_by_pid_ns(tid, ns);
2949 if (task)
2950 get_task_struct(task);
2951 rcu_read_unlock();
2952 if (!task)
2953 goto out;
2954 if (!same_thread_group(leader, task))
2955 goto out_drop_task;
2957 result = proc_task_instantiate(dir, dentry, task, NULL);
2958 out_drop_task:
2959 put_task_struct(task);
2960 out:
2961 put_task_struct(leader);
2962 out_no_task:
2963 return result;
2967 * Find the first tid of a thread group to return to user space.
2969 * Usually this is just the thread group leader, but if the users
2970 * buffer was too small or there was a seek into the middle of the
2971 * directory we have more work todo.
2973 * In the case of a short read we start with find_task_by_pid.
2975 * In the case of a seek we start with the leader and walk nr
2976 * threads past it.
2978 static struct task_struct *first_tid(struct task_struct *leader,
2979 int tid, int nr, struct pid_namespace *ns)
2981 struct task_struct *pos;
2983 rcu_read_lock();
2984 /* Attempt to start with the pid of a thread */
2985 if (tid && (nr > 0)) {
2986 pos = find_task_by_pid_ns(tid, ns);
2987 if (pos && (pos->group_leader == leader))
2988 goto found;
2991 /* If nr exceeds the number of threads there is nothing todo */
2992 pos = NULL;
2993 if (nr && nr >= get_nr_threads(leader))
2994 goto out;
2996 /* If we haven't found our starting place yet start
2997 * with the leader and walk nr threads forward.
2999 for (pos = leader; nr > 0; --nr) {
3000 pos = next_thread(pos);
3001 if (pos == leader) {
3002 pos = NULL;
3003 goto out;
3006 found:
3007 get_task_struct(pos);
3008 out:
3009 rcu_read_unlock();
3010 return pos;
3014 * Find the next thread in the thread list.
3015 * Return NULL if there is an error or no next thread.
3017 * The reference to the input task_struct is released.
3019 static struct task_struct *next_tid(struct task_struct *start)
3021 struct task_struct *pos = NULL;
3022 rcu_read_lock();
3023 if (pid_alive(start)) {
3024 pos = next_thread(start);
3025 if (thread_group_leader(pos))
3026 pos = NULL;
3027 else
3028 get_task_struct(pos);
3030 rcu_read_unlock();
3031 put_task_struct(start);
3032 return pos;
3035 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3036 struct task_struct *task, int tid)
3038 char name[PROC_NUMBUF];
3039 int len = snprintf(name, sizeof(name), "%d", tid);
3040 return proc_fill_cache(filp, dirent, filldir, name, len,
3041 proc_task_instantiate, task, NULL);
3044 /* for the /proc/TGID/task/ directories */
3045 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3047 struct dentry *dentry = filp->f_path.dentry;
3048 struct inode *inode = dentry->d_inode;
3049 struct task_struct *leader = NULL;
3050 struct task_struct *task;
3051 int retval = -ENOENT;
3052 ino_t ino;
3053 int tid;
3054 struct pid_namespace *ns;
3056 task = get_proc_task(inode);
3057 if (!task)
3058 goto out_no_task;
3059 rcu_read_lock();
3060 if (pid_alive(task)) {
3061 leader = task->group_leader;
3062 get_task_struct(leader);
3064 rcu_read_unlock();
3065 put_task_struct(task);
3066 if (!leader)
3067 goto out_no_task;
3068 retval = 0;
3070 switch ((unsigned long)filp->f_pos) {
3071 case 0:
3072 ino = inode->i_ino;
3073 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3074 goto out;
3075 filp->f_pos++;
3076 /* fall through */
3077 case 1:
3078 ino = parent_ino(dentry);
3079 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3080 goto out;
3081 filp->f_pos++;
3082 /* fall through */
3085 /* f_version caches the tgid value that the last readdir call couldn't
3086 * return. lseek aka telldir automagically resets f_version to 0.
3088 ns = filp->f_dentry->d_sb->s_fs_info;
3089 tid = (int)filp->f_version;
3090 filp->f_version = 0;
3091 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3092 task;
3093 task = next_tid(task), filp->f_pos++) {
3094 tid = task_pid_nr_ns(task, ns);
3095 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3096 /* returning this tgid failed, save it as the first
3097 * pid for the next readir call */
3098 filp->f_version = (u64)tid;
3099 put_task_struct(task);
3100 break;
3103 out:
3104 put_task_struct(leader);
3105 out_no_task:
3106 return retval;
3109 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3111 struct inode *inode = dentry->d_inode;
3112 struct task_struct *p = get_proc_task(inode);
3113 generic_fillattr(inode, stat);
3115 if (p) {
3116 stat->nlink += get_nr_threads(p);
3117 put_task_struct(p);
3120 return 0;
3123 static const struct inode_operations proc_task_inode_operations = {
3124 .lookup = proc_task_lookup,
3125 .getattr = proc_task_getattr,
3126 .setattr = proc_setattr,
3129 static const struct file_operations proc_task_operations = {
3130 .read = generic_read_dir,
3131 .readdir = proc_task_readdir,