proc: hold cred_guard_mutex in check_mem_permission()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / proc / base.c
blob013f116b3223a88a1b648a1857008c9466a612e5
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/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/fs_struct.h>
85 #include <linux/slab.h>
86 #include "internal.h"
88 /* NOTE:
89 * Implementing inode permission operations in /proc is almost
90 * certainly an error. Permission checks need to happen during
91 * each system call not at open time. The reason is that most of
92 * what we wish to check for permissions in /proc varies at runtime.
94 * The classic example of a problem is opening file descriptors
95 * in /proc for a task before it execs a suid executable.
98 struct pid_entry {
99 char *name;
100 int len;
101 mode_t mode;
102 const struct inode_operations *iop;
103 const struct file_operations *fop;
104 union proc_op op;
107 #define NOD(NAME, MODE, IOP, FOP, OP) { \
108 .name = (NAME), \
109 .len = sizeof(NAME) - 1, \
110 .mode = MODE, \
111 .iop = IOP, \
112 .fop = FOP, \
113 .op = OP, \
116 #define DIR(NAME, MODE, iops, fops) \
117 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118 #define LNK(NAME, get_link) \
119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
120 &proc_pid_link_inode_operations, NULL, \
121 { .proc_get_link = get_link } )
122 #define REG(NAME, MODE, fops) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124 #define INF(NAME, MODE, read) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_info_file_operations, \
127 { .proc_read = read } )
128 #define ONE(NAME, MODE, show) \
129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_single_file_operations, \
131 { .proc_show = show } )
134 * Count the number of hardlinks for the pid_entry table, excluding the .
135 * and .. links.
137 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
138 unsigned int n)
140 unsigned int i;
141 unsigned int count;
143 count = 0;
144 for (i = 0; i < n; ++i) {
145 if (S_ISDIR(entries[i].mode))
146 ++count;
149 return count;
152 static int get_task_root(struct task_struct *task, struct path *root)
154 int result = -ENOENT;
156 task_lock(task);
157 if (task->fs) {
158 get_fs_root(task->fs, root);
159 result = 0;
161 task_unlock(task);
162 return result;
165 static int proc_cwd_link(struct inode *inode, struct path *path)
167 struct task_struct *task = get_proc_task(inode);
168 int result = -ENOENT;
170 if (task) {
171 task_lock(task);
172 if (task->fs) {
173 get_fs_pwd(task->fs, path);
174 result = 0;
176 task_unlock(task);
177 put_task_struct(task);
179 return result;
182 static int proc_root_link(struct inode *inode, struct path *path)
184 struct task_struct *task = get_proc_task(inode);
185 int result = -ENOENT;
187 if (task) {
188 result = get_task_root(task, path);
189 put_task_struct(task);
191 return result;
194 static int __check_mem_permission(struct task_struct *task)
197 * A task can always look at itself, in case it chooses
198 * to use system calls instead of load instructions.
200 if (task == current)
201 return 0;
204 * If current is actively ptrace'ing, and would also be
205 * permitted to freshly attach with ptrace now, permit it.
207 if (task_is_stopped_or_traced(task)) {
208 int match;
209 rcu_read_lock();
210 match = (tracehook_tracer_task(task) == current);
211 rcu_read_unlock();
212 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
213 return 0;
217 * Noone else is allowed.
219 return -EPERM;
223 * Return zero if current may access user memory in @task, -error if not.
225 static int check_mem_permission(struct task_struct *task)
227 int err;
230 * Avoid racing if task exec's as we might get a new mm but validate
231 * against old credentials.
233 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
234 if (err)
235 return err;
237 err = __check_mem_permission(task);
238 mutex_unlock(&task->signal->cred_guard_mutex);
240 return err;
243 struct mm_struct *mm_for_maps(struct task_struct *task)
245 struct mm_struct *mm;
246 int err;
248 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
249 if (err)
250 return ERR_PTR(err);
252 mm = get_task_mm(task);
253 if (mm && mm != current->mm &&
254 !ptrace_may_access(task, PTRACE_MODE_READ)) {
255 mmput(mm);
256 mm = ERR_PTR(-EACCES);
258 mutex_unlock(&task->signal->cred_guard_mutex);
260 return mm;
263 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
265 int res = 0;
266 unsigned int len;
267 struct mm_struct *mm = get_task_mm(task);
268 if (!mm)
269 goto out;
270 if (!mm->arg_end)
271 goto out_mm; /* Shh! No looking before we're done */
273 len = mm->arg_end - mm->arg_start;
275 if (len > PAGE_SIZE)
276 len = PAGE_SIZE;
278 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
280 // If the nul at the end of args has been overwritten, then
281 // assume application is using setproctitle(3).
282 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
283 len = strnlen(buffer, res);
284 if (len < res) {
285 res = len;
286 } else {
287 len = mm->env_end - mm->env_start;
288 if (len > PAGE_SIZE - res)
289 len = PAGE_SIZE - res;
290 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
291 res = strnlen(buffer, res);
294 out_mm:
295 mmput(mm);
296 out:
297 return res;
300 static int proc_pid_auxv(struct task_struct *task, char *buffer)
302 struct mm_struct *mm = mm_for_maps(task);
303 int res = PTR_ERR(mm);
304 if (mm && !IS_ERR(mm)) {
305 unsigned int nwords = 0;
306 do {
307 nwords += 2;
308 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
309 res = nwords * sizeof(mm->saved_auxv[0]);
310 if (res > PAGE_SIZE)
311 res = PAGE_SIZE;
312 memcpy(buffer, mm->saved_auxv, res);
313 mmput(mm);
315 return res;
319 #ifdef CONFIG_KALLSYMS
321 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
322 * Returns the resolved symbol. If that fails, simply return the address.
324 static int proc_pid_wchan(struct task_struct *task, char *buffer)
326 unsigned long wchan;
327 char symname[KSYM_NAME_LEN];
329 wchan = get_wchan(task);
331 if (lookup_symbol_name(wchan, symname) < 0)
332 if (!ptrace_may_access(task, PTRACE_MODE_READ))
333 return 0;
334 else
335 return sprintf(buffer, "%lu", wchan);
336 else
337 return sprintf(buffer, "%s", symname);
339 #endif /* CONFIG_KALLSYMS */
341 #ifdef CONFIG_STACKTRACE
343 #define MAX_STACK_TRACE_DEPTH 64
345 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
346 struct pid *pid, struct task_struct *task)
348 struct stack_trace trace;
349 unsigned long *entries;
350 int i;
352 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
353 if (!entries)
354 return -ENOMEM;
356 trace.nr_entries = 0;
357 trace.max_entries = MAX_STACK_TRACE_DEPTH;
358 trace.entries = entries;
359 trace.skip = 0;
360 save_stack_trace_tsk(task, &trace);
362 for (i = 0; i < trace.nr_entries; i++) {
363 seq_printf(m, "[<%p>] %pS\n",
364 (void *)entries[i], (void *)entries[i]);
366 kfree(entries);
368 return 0;
370 #endif
372 #ifdef CONFIG_SCHEDSTATS
374 * Provides /proc/PID/schedstat
376 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
378 return sprintf(buffer, "%llu %llu %lu\n",
379 (unsigned long long)task->se.sum_exec_runtime,
380 (unsigned long long)task->sched_info.run_delay,
381 task->sched_info.pcount);
383 #endif
385 #ifdef CONFIG_LATENCYTOP
386 static int lstats_show_proc(struct seq_file *m, void *v)
388 int i;
389 struct inode *inode = m->private;
390 struct task_struct *task = get_proc_task(inode);
392 if (!task)
393 return -ESRCH;
394 seq_puts(m, "Latency Top version : v0.1\n");
395 for (i = 0; i < 32; i++) {
396 struct latency_record *lr = &task->latency_record[i];
397 if (lr->backtrace[0]) {
398 int q;
399 seq_printf(m, "%i %li %li",
400 lr->count, lr->time, lr->max);
401 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
402 unsigned long bt = lr->backtrace[q];
403 if (!bt)
404 break;
405 if (bt == ULONG_MAX)
406 break;
407 seq_printf(m, " %ps", (void *)bt);
409 seq_putc(m, '\n');
413 put_task_struct(task);
414 return 0;
417 static int lstats_open(struct inode *inode, struct file *file)
419 return single_open(file, lstats_show_proc, inode);
422 static ssize_t lstats_write(struct file *file, const char __user *buf,
423 size_t count, loff_t *offs)
425 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
427 if (!task)
428 return -ESRCH;
429 clear_all_latency_tracing(task);
430 put_task_struct(task);
432 return count;
435 static const struct file_operations proc_lstats_operations = {
436 .open = lstats_open,
437 .read = seq_read,
438 .write = lstats_write,
439 .llseek = seq_lseek,
440 .release = single_release,
443 #endif
445 static int proc_oom_score(struct task_struct *task, char *buffer)
447 unsigned long points = 0;
449 read_lock(&tasklist_lock);
450 if (pid_alive(task))
451 points = oom_badness(task, NULL, NULL,
452 totalram_pages + total_swap_pages);
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", "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)
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 return error;
579 if ((attr->ia_valid & ATTR_SIZE) &&
580 attr->ia_size != i_size_read(inode)) {
581 error = vmtruncate(inode, attr->ia_size);
582 if (error)
583 return error;
586 setattr_copy(inode, attr);
587 mark_inode_dirty(inode);
588 return 0;
591 static const struct inode_operations proc_def_inode_operations = {
592 .setattr = proc_setattr,
595 static int mounts_open_common(struct inode *inode, struct file *file,
596 const struct seq_operations *op)
598 struct task_struct *task = get_proc_task(inode);
599 struct nsproxy *nsp;
600 struct mnt_namespace *ns = NULL;
601 struct path root;
602 struct proc_mounts *p;
603 int ret = -EINVAL;
605 if (task) {
606 rcu_read_lock();
607 nsp = task_nsproxy(task);
608 if (nsp) {
609 ns = nsp->mnt_ns;
610 if (ns)
611 get_mnt_ns(ns);
613 rcu_read_unlock();
614 if (ns && get_task_root(task, &root) == 0)
615 ret = 0;
616 put_task_struct(task);
619 if (!ns)
620 goto err;
621 if (ret)
622 goto err_put_ns;
624 ret = -ENOMEM;
625 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
626 if (!p)
627 goto err_put_path;
629 file->private_data = &p->m;
630 ret = seq_open(file, op);
631 if (ret)
632 goto err_free;
634 p->m.private = p;
635 p->ns = ns;
636 p->root = root;
637 p->event = ns->event;
639 return 0;
641 err_free:
642 kfree(p);
643 err_put_path:
644 path_put(&root);
645 err_put_ns:
646 put_mnt_ns(ns);
647 err:
648 return ret;
651 static int mounts_release(struct inode *inode, struct file *file)
653 struct proc_mounts *p = file->private_data;
654 path_put(&p->root);
655 put_mnt_ns(p->ns);
656 return seq_release(inode, file);
659 static unsigned mounts_poll(struct file *file, poll_table *wait)
661 struct proc_mounts *p = file->private_data;
662 unsigned res = POLLIN | POLLRDNORM;
664 poll_wait(file, &p->ns->poll, wait);
665 if (mnt_had_events(p))
666 res |= POLLERR | POLLPRI;
668 return res;
671 static int mounts_open(struct inode *inode, struct file *file)
673 return mounts_open_common(inode, file, &mounts_op);
676 static const struct file_operations proc_mounts_operations = {
677 .open = mounts_open,
678 .read = seq_read,
679 .llseek = seq_lseek,
680 .release = mounts_release,
681 .poll = mounts_poll,
684 static int mountinfo_open(struct inode *inode, struct file *file)
686 return mounts_open_common(inode, file, &mountinfo_op);
689 static const struct file_operations proc_mountinfo_operations = {
690 .open = mountinfo_open,
691 .read = seq_read,
692 .llseek = seq_lseek,
693 .release = mounts_release,
694 .poll = mounts_poll,
697 static int mountstats_open(struct inode *inode, struct file *file)
699 return mounts_open_common(inode, file, &mountstats_op);
702 static const struct file_operations proc_mountstats_operations = {
703 .open = mountstats_open,
704 .read = seq_read,
705 .llseek = seq_lseek,
706 .release = mounts_release,
709 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
711 static ssize_t proc_info_read(struct file * file, char __user * buf,
712 size_t count, loff_t *ppos)
714 struct inode * inode = file->f_path.dentry->d_inode;
715 unsigned long page;
716 ssize_t length;
717 struct task_struct *task = get_proc_task(inode);
719 length = -ESRCH;
720 if (!task)
721 goto out_no_task;
723 if (count > PROC_BLOCK_SIZE)
724 count = PROC_BLOCK_SIZE;
726 length = -ENOMEM;
727 if (!(page = __get_free_page(GFP_TEMPORARY)))
728 goto out;
730 length = PROC_I(inode)->op.proc_read(task, (char*)page);
732 if (length >= 0)
733 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
734 free_page(page);
735 out:
736 put_task_struct(task);
737 out_no_task:
738 return length;
741 static const struct file_operations proc_info_file_operations = {
742 .read = proc_info_read,
743 .llseek = generic_file_llseek,
746 static int proc_single_show(struct seq_file *m, void *v)
748 struct inode *inode = m->private;
749 struct pid_namespace *ns;
750 struct pid *pid;
751 struct task_struct *task;
752 int ret;
754 ns = inode->i_sb->s_fs_info;
755 pid = proc_pid(inode);
756 task = get_pid_task(pid, PIDTYPE_PID);
757 if (!task)
758 return -ESRCH;
760 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
762 put_task_struct(task);
763 return ret;
766 static int proc_single_open(struct inode *inode, struct file *filp)
768 return single_open(filp, proc_single_show, inode);
771 static const struct file_operations proc_single_file_operations = {
772 .open = proc_single_open,
773 .read = seq_read,
774 .llseek = seq_lseek,
775 .release = single_release,
778 static int mem_open(struct inode* inode, struct file* file)
780 file->private_data = (void*)((long)current->self_exec_id);
781 /* OK to pass negative loff_t, we can catch out-of-range */
782 file->f_mode |= FMODE_UNSIGNED_OFFSET;
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 = -EIO;
872 if (file->private_data != (void *)((long)current->self_exec_id))
873 goto out;
875 copied = -ENOMEM;
876 page = (char *)__get_free_page(GFP_TEMPORARY);
877 if (!page)
878 goto out;
880 copied = 0;
881 while (count > 0) {
882 int this_len, retval;
884 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
885 if (copy_from_user(page, buf, this_len)) {
886 copied = -EFAULT;
887 break;
889 retval = access_process_vm(task, dst, page, this_len, 1);
890 if (!retval) {
891 if (!copied)
892 copied = -EIO;
893 break;
895 copied += retval;
896 buf += retval;
897 dst += retval;
898 count -= retval;
900 *ppos = dst;
901 free_page((unsigned long) page);
902 out:
903 put_task_struct(task);
904 out_no_task:
905 return copied;
907 #endif
909 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
911 switch (orig) {
912 case 0:
913 file->f_pos = offset;
914 break;
915 case 1:
916 file->f_pos += offset;
917 break;
918 default:
919 return -EINVAL;
921 force_successful_syscall_return();
922 return file->f_pos;
925 static const struct file_operations proc_mem_operations = {
926 .llseek = mem_lseek,
927 .read = mem_read,
928 .write = mem_write,
929 .open = mem_open,
932 static ssize_t environ_read(struct file *file, char __user *buf,
933 size_t count, loff_t *ppos)
935 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
936 char *page;
937 unsigned long src = *ppos;
938 int ret = -ESRCH;
939 struct mm_struct *mm;
941 if (!task)
942 goto out_no_task;
944 ret = -ENOMEM;
945 page = (char *)__get_free_page(GFP_TEMPORARY);
946 if (!page)
947 goto out;
950 mm = mm_for_maps(task);
951 ret = PTR_ERR(mm);
952 if (!mm || IS_ERR(mm))
953 goto out_free;
955 ret = 0;
956 while (count > 0) {
957 int this_len, retval, max_len;
959 this_len = mm->env_end - (mm->env_start + src);
961 if (this_len <= 0)
962 break;
964 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
965 this_len = (this_len > max_len) ? max_len : this_len;
967 retval = access_process_vm(task, (mm->env_start + src),
968 page, this_len, 0);
970 if (retval <= 0) {
971 ret = retval;
972 break;
975 if (copy_to_user(buf, page, retval)) {
976 ret = -EFAULT;
977 break;
980 ret += retval;
981 src += retval;
982 buf += retval;
983 count -= retval;
985 *ppos = src;
987 mmput(mm);
988 out_free:
989 free_page((unsigned long) page);
990 out:
991 put_task_struct(task);
992 out_no_task:
993 return ret;
996 static const struct file_operations proc_environ_operations = {
997 .read = environ_read,
998 .llseek = generic_file_llseek,
1001 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1002 size_t count, loff_t *ppos)
1004 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1005 char buffer[PROC_NUMBUF];
1006 size_t len;
1007 int oom_adjust = OOM_DISABLE;
1008 unsigned long flags;
1010 if (!task)
1011 return -ESRCH;
1013 if (lock_task_sighand(task, &flags)) {
1014 oom_adjust = task->signal->oom_adj;
1015 unlock_task_sighand(task, &flags);
1018 put_task_struct(task);
1020 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
1022 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1025 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1026 size_t count, loff_t *ppos)
1028 struct task_struct *task;
1029 char buffer[PROC_NUMBUF];
1030 long oom_adjust;
1031 unsigned long flags;
1032 int err;
1034 memset(buffer, 0, sizeof(buffer));
1035 if (count > sizeof(buffer) - 1)
1036 count = sizeof(buffer) - 1;
1037 if (copy_from_user(buffer, buf, count)) {
1038 err = -EFAULT;
1039 goto out;
1042 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1043 if (err)
1044 goto out;
1045 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1046 oom_adjust != OOM_DISABLE) {
1047 err = -EINVAL;
1048 goto out;
1051 task = get_proc_task(file->f_path.dentry->d_inode);
1052 if (!task) {
1053 err = -ESRCH;
1054 goto out;
1057 task_lock(task);
1058 if (!task->mm) {
1059 err = -EINVAL;
1060 goto err_task_lock;
1063 if (!lock_task_sighand(task, &flags)) {
1064 err = -ESRCH;
1065 goto err_task_lock;
1068 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1069 err = -EACCES;
1070 goto err_sighand;
1073 if (oom_adjust != task->signal->oom_adj) {
1074 if (oom_adjust == OOM_DISABLE)
1075 atomic_inc(&task->mm->oom_disable_count);
1076 if (task->signal->oom_adj == OOM_DISABLE)
1077 atomic_dec(&task->mm->oom_disable_count);
1081 * Warn that /proc/pid/oom_adj is deprecated, see
1082 * Documentation/feature-removal-schedule.txt.
1084 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1085 "please use /proc/%d/oom_score_adj instead.\n",
1086 current->comm, task_pid_nr(current),
1087 task_pid_nr(task), task_pid_nr(task));
1088 task->signal->oom_adj = oom_adjust;
1090 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1091 * value is always attainable.
1093 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1094 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1095 else
1096 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1097 -OOM_DISABLE;
1098 err_sighand:
1099 unlock_task_sighand(task, &flags);
1100 err_task_lock:
1101 task_unlock(task);
1102 put_task_struct(task);
1103 out:
1104 return err < 0 ? err : count;
1107 static const struct file_operations proc_oom_adjust_operations = {
1108 .read = oom_adjust_read,
1109 .write = oom_adjust_write,
1110 .llseek = generic_file_llseek,
1113 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1114 size_t count, loff_t *ppos)
1116 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1117 char buffer[PROC_NUMBUF];
1118 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1119 unsigned long flags;
1120 size_t len;
1122 if (!task)
1123 return -ESRCH;
1124 if (lock_task_sighand(task, &flags)) {
1125 oom_score_adj = task->signal->oom_score_adj;
1126 unlock_task_sighand(task, &flags);
1128 put_task_struct(task);
1129 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1130 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1133 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1134 size_t count, loff_t *ppos)
1136 struct task_struct *task;
1137 char buffer[PROC_NUMBUF];
1138 unsigned long flags;
1139 long oom_score_adj;
1140 int err;
1142 memset(buffer, 0, sizeof(buffer));
1143 if (count > sizeof(buffer) - 1)
1144 count = sizeof(buffer) - 1;
1145 if (copy_from_user(buffer, buf, count)) {
1146 err = -EFAULT;
1147 goto out;
1150 err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1151 if (err)
1152 goto out;
1153 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1154 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1155 err = -EINVAL;
1156 goto out;
1159 task = get_proc_task(file->f_path.dentry->d_inode);
1160 if (!task) {
1161 err = -ESRCH;
1162 goto out;
1165 task_lock(task);
1166 if (!task->mm) {
1167 err = -EINVAL;
1168 goto err_task_lock;
1171 if (!lock_task_sighand(task, &flags)) {
1172 err = -ESRCH;
1173 goto err_task_lock;
1176 if (oom_score_adj < task->signal->oom_score_adj_min &&
1177 !capable(CAP_SYS_RESOURCE)) {
1178 err = -EACCES;
1179 goto err_sighand;
1182 if (oom_score_adj != task->signal->oom_score_adj) {
1183 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1184 atomic_inc(&task->mm->oom_disable_count);
1185 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1186 atomic_dec(&task->mm->oom_disable_count);
1188 task->signal->oom_score_adj = oom_score_adj;
1189 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1190 task->signal->oom_score_adj_min = oom_score_adj;
1192 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1193 * always attainable.
1195 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1196 task->signal->oom_adj = OOM_DISABLE;
1197 else
1198 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1199 OOM_SCORE_ADJ_MAX;
1200 err_sighand:
1201 unlock_task_sighand(task, &flags);
1202 err_task_lock:
1203 task_unlock(task);
1204 put_task_struct(task);
1205 out:
1206 return err < 0 ? err : count;
1209 static const struct file_operations proc_oom_score_adj_operations = {
1210 .read = oom_score_adj_read,
1211 .write = oom_score_adj_write,
1212 .llseek = default_llseek,
1215 #ifdef CONFIG_AUDITSYSCALL
1216 #define TMPBUFLEN 21
1217 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1218 size_t count, loff_t *ppos)
1220 struct inode * inode = file->f_path.dentry->d_inode;
1221 struct task_struct *task = get_proc_task(inode);
1222 ssize_t length;
1223 char tmpbuf[TMPBUFLEN];
1225 if (!task)
1226 return -ESRCH;
1227 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1228 audit_get_loginuid(task));
1229 put_task_struct(task);
1230 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1233 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1234 size_t count, loff_t *ppos)
1236 struct inode * inode = file->f_path.dentry->d_inode;
1237 char *page, *tmp;
1238 ssize_t length;
1239 uid_t loginuid;
1241 if (!capable(CAP_AUDIT_CONTROL))
1242 return -EPERM;
1244 rcu_read_lock();
1245 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1246 rcu_read_unlock();
1247 return -EPERM;
1249 rcu_read_unlock();
1251 if (count >= PAGE_SIZE)
1252 count = PAGE_SIZE - 1;
1254 if (*ppos != 0) {
1255 /* No partial writes. */
1256 return -EINVAL;
1258 page = (char*)__get_free_page(GFP_TEMPORARY);
1259 if (!page)
1260 return -ENOMEM;
1261 length = -EFAULT;
1262 if (copy_from_user(page, buf, count))
1263 goto out_free_page;
1265 page[count] = '\0';
1266 loginuid = simple_strtoul(page, &tmp, 10);
1267 if (tmp == page) {
1268 length = -EINVAL;
1269 goto out_free_page;
1272 length = audit_set_loginuid(current, loginuid);
1273 if (likely(length == 0))
1274 length = count;
1276 out_free_page:
1277 free_page((unsigned long) page);
1278 return length;
1281 static const struct file_operations proc_loginuid_operations = {
1282 .read = proc_loginuid_read,
1283 .write = proc_loginuid_write,
1284 .llseek = generic_file_llseek,
1287 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1288 size_t count, loff_t *ppos)
1290 struct inode * inode = file->f_path.dentry->d_inode;
1291 struct task_struct *task = get_proc_task(inode);
1292 ssize_t length;
1293 char tmpbuf[TMPBUFLEN];
1295 if (!task)
1296 return -ESRCH;
1297 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1298 audit_get_sessionid(task));
1299 put_task_struct(task);
1300 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1303 static const struct file_operations proc_sessionid_operations = {
1304 .read = proc_sessionid_read,
1305 .llseek = generic_file_llseek,
1307 #endif
1309 #ifdef CONFIG_FAULT_INJECTION
1310 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1311 size_t count, loff_t *ppos)
1313 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1314 char buffer[PROC_NUMBUF];
1315 size_t len;
1316 int make_it_fail;
1318 if (!task)
1319 return -ESRCH;
1320 make_it_fail = task->make_it_fail;
1321 put_task_struct(task);
1323 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1325 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1328 static ssize_t proc_fault_inject_write(struct file * file,
1329 const char __user * buf, size_t count, loff_t *ppos)
1331 struct task_struct *task;
1332 char buffer[PROC_NUMBUF], *end;
1333 int make_it_fail;
1335 if (!capable(CAP_SYS_RESOURCE))
1336 return -EPERM;
1337 memset(buffer, 0, sizeof(buffer));
1338 if (count > sizeof(buffer) - 1)
1339 count = sizeof(buffer) - 1;
1340 if (copy_from_user(buffer, buf, count))
1341 return -EFAULT;
1342 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1343 if (*end)
1344 return -EINVAL;
1345 task = get_proc_task(file->f_dentry->d_inode);
1346 if (!task)
1347 return -ESRCH;
1348 task->make_it_fail = make_it_fail;
1349 put_task_struct(task);
1351 return count;
1354 static const struct file_operations proc_fault_inject_operations = {
1355 .read = proc_fault_inject_read,
1356 .write = proc_fault_inject_write,
1357 .llseek = generic_file_llseek,
1359 #endif
1362 #ifdef CONFIG_SCHED_DEBUG
1364 * Print out various scheduling related per-task fields:
1366 static int sched_show(struct seq_file *m, void *v)
1368 struct inode *inode = m->private;
1369 struct task_struct *p;
1371 p = get_proc_task(inode);
1372 if (!p)
1373 return -ESRCH;
1374 proc_sched_show_task(p, m);
1376 put_task_struct(p);
1378 return 0;
1381 static ssize_t
1382 sched_write(struct file *file, const char __user *buf,
1383 size_t count, loff_t *offset)
1385 struct inode *inode = file->f_path.dentry->d_inode;
1386 struct task_struct *p;
1388 p = get_proc_task(inode);
1389 if (!p)
1390 return -ESRCH;
1391 proc_sched_set_task(p);
1393 put_task_struct(p);
1395 return count;
1398 static int sched_open(struct inode *inode, struct file *filp)
1400 return single_open(filp, sched_show, inode);
1403 static const struct file_operations proc_pid_sched_operations = {
1404 .open = sched_open,
1405 .read = seq_read,
1406 .write = sched_write,
1407 .llseek = seq_lseek,
1408 .release = single_release,
1411 #endif
1413 #ifdef CONFIG_SCHED_AUTOGROUP
1415 * Print out autogroup related information:
1417 static int sched_autogroup_show(struct seq_file *m, void *v)
1419 struct inode *inode = m->private;
1420 struct task_struct *p;
1422 p = get_proc_task(inode);
1423 if (!p)
1424 return -ESRCH;
1425 proc_sched_autogroup_show_task(p, m);
1427 put_task_struct(p);
1429 return 0;
1432 static ssize_t
1433 sched_autogroup_write(struct file *file, const char __user *buf,
1434 size_t count, loff_t *offset)
1436 struct inode *inode = file->f_path.dentry->d_inode;
1437 struct task_struct *p;
1438 char buffer[PROC_NUMBUF];
1439 long nice;
1440 int err;
1442 memset(buffer, 0, sizeof(buffer));
1443 if (count > sizeof(buffer) - 1)
1444 count = sizeof(buffer) - 1;
1445 if (copy_from_user(buffer, buf, count))
1446 return -EFAULT;
1448 err = strict_strtol(strstrip(buffer), 0, &nice);
1449 if (err)
1450 return -EINVAL;
1452 p = get_proc_task(inode);
1453 if (!p)
1454 return -ESRCH;
1456 err = nice;
1457 err = proc_sched_autogroup_set_nice(p, &err);
1458 if (err)
1459 count = err;
1461 put_task_struct(p);
1463 return count;
1466 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1468 int ret;
1470 ret = single_open(filp, sched_autogroup_show, NULL);
1471 if (!ret) {
1472 struct seq_file *m = filp->private_data;
1474 m->private = inode;
1476 return ret;
1479 static const struct file_operations proc_pid_sched_autogroup_operations = {
1480 .open = sched_autogroup_open,
1481 .read = seq_read,
1482 .write = sched_autogroup_write,
1483 .llseek = seq_lseek,
1484 .release = single_release,
1487 #endif /* CONFIG_SCHED_AUTOGROUP */
1489 static ssize_t comm_write(struct file *file, const char __user *buf,
1490 size_t count, loff_t *offset)
1492 struct inode *inode = file->f_path.dentry->d_inode;
1493 struct task_struct *p;
1494 char buffer[TASK_COMM_LEN];
1496 memset(buffer, 0, sizeof(buffer));
1497 if (count > sizeof(buffer) - 1)
1498 count = sizeof(buffer) - 1;
1499 if (copy_from_user(buffer, buf, count))
1500 return -EFAULT;
1502 p = get_proc_task(inode);
1503 if (!p)
1504 return -ESRCH;
1506 if (same_thread_group(current, p))
1507 set_task_comm(p, buffer);
1508 else
1509 count = -EINVAL;
1511 put_task_struct(p);
1513 return count;
1516 static int comm_show(struct seq_file *m, void *v)
1518 struct inode *inode = m->private;
1519 struct task_struct *p;
1521 p = get_proc_task(inode);
1522 if (!p)
1523 return -ESRCH;
1525 task_lock(p);
1526 seq_printf(m, "%s\n", p->comm);
1527 task_unlock(p);
1529 put_task_struct(p);
1531 return 0;
1534 static int comm_open(struct inode *inode, struct file *filp)
1536 return single_open(filp, comm_show, inode);
1539 static const struct file_operations proc_pid_set_comm_operations = {
1540 .open = comm_open,
1541 .read = seq_read,
1542 .write = comm_write,
1543 .llseek = seq_lseek,
1544 .release = single_release,
1548 * We added or removed a vma mapping the executable. The vmas are only mapped
1549 * during exec and are not mapped with the mmap system call.
1550 * Callers must hold down_write() on the mm's mmap_sem for these
1552 void added_exe_file_vma(struct mm_struct *mm)
1554 mm->num_exe_file_vmas++;
1557 void removed_exe_file_vma(struct mm_struct *mm)
1559 mm->num_exe_file_vmas--;
1560 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1561 fput(mm->exe_file);
1562 mm->exe_file = NULL;
1567 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1569 if (new_exe_file)
1570 get_file(new_exe_file);
1571 if (mm->exe_file)
1572 fput(mm->exe_file);
1573 mm->exe_file = new_exe_file;
1574 mm->num_exe_file_vmas = 0;
1577 struct file *get_mm_exe_file(struct mm_struct *mm)
1579 struct file *exe_file;
1581 /* We need mmap_sem to protect against races with removal of
1582 * VM_EXECUTABLE vmas */
1583 down_read(&mm->mmap_sem);
1584 exe_file = mm->exe_file;
1585 if (exe_file)
1586 get_file(exe_file);
1587 up_read(&mm->mmap_sem);
1588 return exe_file;
1591 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1593 /* It's safe to write the exe_file pointer without exe_file_lock because
1594 * this is called during fork when the task is not yet in /proc */
1595 newmm->exe_file = get_mm_exe_file(oldmm);
1598 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1600 struct task_struct *task;
1601 struct mm_struct *mm;
1602 struct file *exe_file;
1604 task = get_proc_task(inode);
1605 if (!task)
1606 return -ENOENT;
1607 mm = get_task_mm(task);
1608 put_task_struct(task);
1609 if (!mm)
1610 return -ENOENT;
1611 exe_file = get_mm_exe_file(mm);
1612 mmput(mm);
1613 if (exe_file) {
1614 *exe_path = exe_file->f_path;
1615 path_get(&exe_file->f_path);
1616 fput(exe_file);
1617 return 0;
1618 } else
1619 return -ENOENT;
1622 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1624 struct inode *inode = dentry->d_inode;
1625 int error = -EACCES;
1627 /* We don't need a base pointer in the /proc filesystem */
1628 path_put(&nd->path);
1630 /* Are we allowed to snoop on the tasks file descriptors? */
1631 if (!proc_fd_access_allowed(inode))
1632 goto out;
1634 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1635 out:
1636 return ERR_PTR(error);
1639 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1641 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1642 char *pathname;
1643 int len;
1645 if (!tmp)
1646 return -ENOMEM;
1648 pathname = d_path(path, tmp, PAGE_SIZE);
1649 len = PTR_ERR(pathname);
1650 if (IS_ERR(pathname))
1651 goto out;
1652 len = tmp + PAGE_SIZE - 1 - pathname;
1654 if (len > buflen)
1655 len = buflen;
1656 if (copy_to_user(buffer, pathname, len))
1657 len = -EFAULT;
1658 out:
1659 free_page((unsigned long)tmp);
1660 return len;
1663 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1665 int error = -EACCES;
1666 struct inode *inode = dentry->d_inode;
1667 struct path path;
1669 /* Are we allowed to snoop on the tasks file descriptors? */
1670 if (!proc_fd_access_allowed(inode))
1671 goto out;
1673 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1674 if (error)
1675 goto out;
1677 error = do_proc_readlink(&path, buffer, buflen);
1678 path_put(&path);
1679 out:
1680 return error;
1683 static const struct inode_operations proc_pid_link_inode_operations = {
1684 .readlink = proc_pid_readlink,
1685 .follow_link = proc_pid_follow_link,
1686 .setattr = proc_setattr,
1690 /* building an inode */
1692 static int task_dumpable(struct task_struct *task)
1694 int dumpable = 0;
1695 struct mm_struct *mm;
1697 task_lock(task);
1698 mm = task->mm;
1699 if (mm)
1700 dumpable = get_dumpable(mm);
1701 task_unlock(task);
1702 if(dumpable == 1)
1703 return 1;
1704 return 0;
1708 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1710 struct inode * inode;
1711 struct proc_inode *ei;
1712 const struct cred *cred;
1714 /* We need a new inode */
1716 inode = new_inode(sb);
1717 if (!inode)
1718 goto out;
1720 /* Common stuff */
1721 ei = PROC_I(inode);
1722 inode->i_ino = get_next_ino();
1723 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1724 inode->i_op = &proc_def_inode_operations;
1727 * grab the reference to task.
1729 ei->pid = get_task_pid(task, PIDTYPE_PID);
1730 if (!ei->pid)
1731 goto out_unlock;
1733 if (task_dumpable(task)) {
1734 rcu_read_lock();
1735 cred = __task_cred(task);
1736 inode->i_uid = cred->euid;
1737 inode->i_gid = cred->egid;
1738 rcu_read_unlock();
1740 security_task_to_inode(task, inode);
1742 out:
1743 return inode;
1745 out_unlock:
1746 iput(inode);
1747 return NULL;
1750 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1752 struct inode *inode = dentry->d_inode;
1753 struct task_struct *task;
1754 const struct cred *cred;
1756 generic_fillattr(inode, stat);
1758 rcu_read_lock();
1759 stat->uid = 0;
1760 stat->gid = 0;
1761 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1762 if (task) {
1763 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1764 task_dumpable(task)) {
1765 cred = __task_cred(task);
1766 stat->uid = cred->euid;
1767 stat->gid = cred->egid;
1770 rcu_read_unlock();
1771 return 0;
1774 /* dentry stuff */
1777 * Exceptional case: normally we are not allowed to unhash a busy
1778 * directory. In this case, however, we can do it - no aliasing problems
1779 * due to the way we treat inodes.
1781 * Rewrite the inode's ownerships here because the owning task may have
1782 * performed a setuid(), etc.
1784 * Before the /proc/pid/status file was created the only way to read
1785 * the effective uid of a /process was to stat /proc/pid. Reading
1786 * /proc/pid/status is slow enough that procps and other packages
1787 * kept stating /proc/pid. To keep the rules in /proc simple I have
1788 * made this apply to all per process world readable and executable
1789 * directories.
1791 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1793 struct inode *inode;
1794 struct task_struct *task;
1795 const struct cred *cred;
1797 if (nd && nd->flags & LOOKUP_RCU)
1798 return -ECHILD;
1800 inode = dentry->d_inode;
1801 task = get_proc_task(inode);
1803 if (task) {
1804 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1805 task_dumpable(task)) {
1806 rcu_read_lock();
1807 cred = __task_cred(task);
1808 inode->i_uid = cred->euid;
1809 inode->i_gid = cred->egid;
1810 rcu_read_unlock();
1811 } else {
1812 inode->i_uid = 0;
1813 inode->i_gid = 0;
1815 inode->i_mode &= ~(S_ISUID | S_ISGID);
1816 security_task_to_inode(task, inode);
1817 put_task_struct(task);
1818 return 1;
1820 d_drop(dentry);
1821 return 0;
1824 static int pid_delete_dentry(const struct dentry * dentry)
1826 /* Is the task we represent dead?
1827 * If so, then don't put the dentry on the lru list,
1828 * kill it immediately.
1830 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1833 static const struct dentry_operations pid_dentry_operations =
1835 .d_revalidate = pid_revalidate,
1836 .d_delete = pid_delete_dentry,
1839 /* Lookups */
1841 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1842 struct task_struct *, const void *);
1845 * Fill a directory entry.
1847 * If possible create the dcache entry and derive our inode number and
1848 * file type from dcache entry.
1850 * Since all of the proc inode numbers are dynamically generated, the inode
1851 * numbers do not exist until the inode is cache. This means creating the
1852 * the dcache entry in readdir is necessary to keep the inode numbers
1853 * reported by readdir in sync with the inode numbers reported
1854 * by stat.
1856 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1857 char *name, int len,
1858 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1860 struct dentry *child, *dir = filp->f_path.dentry;
1861 struct inode *inode;
1862 struct qstr qname;
1863 ino_t ino = 0;
1864 unsigned type = DT_UNKNOWN;
1866 qname.name = name;
1867 qname.len = len;
1868 qname.hash = full_name_hash(name, len);
1870 child = d_lookup(dir, &qname);
1871 if (!child) {
1872 struct dentry *new;
1873 new = d_alloc(dir, &qname);
1874 if (new) {
1875 child = instantiate(dir->d_inode, new, task, ptr);
1876 if (child)
1877 dput(new);
1878 else
1879 child = new;
1882 if (!child || IS_ERR(child) || !child->d_inode)
1883 goto end_instantiate;
1884 inode = child->d_inode;
1885 if (inode) {
1886 ino = inode->i_ino;
1887 type = inode->i_mode >> 12;
1889 dput(child);
1890 end_instantiate:
1891 if (!ino)
1892 ino = find_inode_number(dir, &qname);
1893 if (!ino)
1894 ino = 1;
1895 return filldir(dirent, name, len, filp->f_pos, ino, type);
1898 static unsigned name_to_int(struct dentry *dentry)
1900 const char *name = dentry->d_name.name;
1901 int len = dentry->d_name.len;
1902 unsigned n = 0;
1904 if (len > 1 && *name == '0')
1905 goto out;
1906 while (len-- > 0) {
1907 unsigned c = *name++ - '0';
1908 if (c > 9)
1909 goto out;
1910 if (n >= (~0U-9)/10)
1911 goto out;
1912 n *= 10;
1913 n += c;
1915 return n;
1916 out:
1917 return ~0U;
1920 #define PROC_FDINFO_MAX 64
1922 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1924 struct task_struct *task = get_proc_task(inode);
1925 struct files_struct *files = NULL;
1926 struct file *file;
1927 int fd = proc_fd(inode);
1929 if (task) {
1930 files = get_files_struct(task);
1931 put_task_struct(task);
1933 if (files) {
1935 * We are not taking a ref to the file structure, so we must
1936 * hold ->file_lock.
1938 spin_lock(&files->file_lock);
1939 file = fcheck_files(files, fd);
1940 if (file) {
1941 if (path) {
1942 *path = file->f_path;
1943 path_get(&file->f_path);
1945 if (info)
1946 snprintf(info, PROC_FDINFO_MAX,
1947 "pos:\t%lli\n"
1948 "flags:\t0%o\n",
1949 (long long) file->f_pos,
1950 file->f_flags);
1951 spin_unlock(&files->file_lock);
1952 put_files_struct(files);
1953 return 0;
1955 spin_unlock(&files->file_lock);
1956 put_files_struct(files);
1958 return -ENOENT;
1961 static int proc_fd_link(struct inode *inode, struct path *path)
1963 return proc_fd_info(inode, path, NULL);
1966 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1968 struct inode *inode;
1969 struct task_struct *task;
1970 int fd;
1971 struct files_struct *files;
1972 const struct cred *cred;
1974 if (nd && nd->flags & LOOKUP_RCU)
1975 return -ECHILD;
1977 inode = dentry->d_inode;
1978 task = get_proc_task(inode);
1979 fd = proc_fd(inode);
1981 if (task) {
1982 files = get_files_struct(task);
1983 if (files) {
1984 rcu_read_lock();
1985 if (fcheck_files(files, fd)) {
1986 rcu_read_unlock();
1987 put_files_struct(files);
1988 if (task_dumpable(task)) {
1989 rcu_read_lock();
1990 cred = __task_cred(task);
1991 inode->i_uid = cred->euid;
1992 inode->i_gid = cred->egid;
1993 rcu_read_unlock();
1994 } else {
1995 inode->i_uid = 0;
1996 inode->i_gid = 0;
1998 inode->i_mode &= ~(S_ISUID | S_ISGID);
1999 security_task_to_inode(task, inode);
2000 put_task_struct(task);
2001 return 1;
2003 rcu_read_unlock();
2004 put_files_struct(files);
2006 put_task_struct(task);
2008 d_drop(dentry);
2009 return 0;
2012 static const struct dentry_operations tid_fd_dentry_operations =
2014 .d_revalidate = tid_fd_revalidate,
2015 .d_delete = pid_delete_dentry,
2018 static struct dentry *proc_fd_instantiate(struct inode *dir,
2019 struct dentry *dentry, struct task_struct *task, const void *ptr)
2021 unsigned fd = *(const unsigned *)ptr;
2022 struct file *file;
2023 struct files_struct *files;
2024 struct inode *inode;
2025 struct proc_inode *ei;
2026 struct dentry *error = ERR_PTR(-ENOENT);
2028 inode = proc_pid_make_inode(dir->i_sb, task);
2029 if (!inode)
2030 goto out;
2031 ei = PROC_I(inode);
2032 ei->fd = fd;
2033 files = get_files_struct(task);
2034 if (!files)
2035 goto out_iput;
2036 inode->i_mode = S_IFLNK;
2039 * We are not taking a ref to the file structure, so we must
2040 * hold ->file_lock.
2042 spin_lock(&files->file_lock);
2043 file = fcheck_files(files, fd);
2044 if (!file)
2045 goto out_unlock;
2046 if (file->f_mode & FMODE_READ)
2047 inode->i_mode |= S_IRUSR | S_IXUSR;
2048 if (file->f_mode & FMODE_WRITE)
2049 inode->i_mode |= S_IWUSR | S_IXUSR;
2050 spin_unlock(&files->file_lock);
2051 put_files_struct(files);
2053 inode->i_op = &proc_pid_link_inode_operations;
2054 inode->i_size = 64;
2055 ei->op.proc_get_link = proc_fd_link;
2056 d_set_d_op(dentry, &tid_fd_dentry_operations);
2057 d_add(dentry, inode);
2058 /* Close the race of the process dying before we return the dentry */
2059 if (tid_fd_revalidate(dentry, NULL))
2060 error = NULL;
2062 out:
2063 return error;
2064 out_unlock:
2065 spin_unlock(&files->file_lock);
2066 put_files_struct(files);
2067 out_iput:
2068 iput(inode);
2069 goto out;
2072 static struct dentry *proc_lookupfd_common(struct inode *dir,
2073 struct dentry *dentry,
2074 instantiate_t instantiate)
2076 struct task_struct *task = get_proc_task(dir);
2077 unsigned fd = name_to_int(dentry);
2078 struct dentry *result = ERR_PTR(-ENOENT);
2080 if (!task)
2081 goto out_no_task;
2082 if (fd == ~0U)
2083 goto out;
2085 result = instantiate(dir, dentry, task, &fd);
2086 out:
2087 put_task_struct(task);
2088 out_no_task:
2089 return result;
2092 static int proc_readfd_common(struct file * filp, void * dirent,
2093 filldir_t filldir, instantiate_t instantiate)
2095 struct dentry *dentry = filp->f_path.dentry;
2096 struct inode *inode = dentry->d_inode;
2097 struct task_struct *p = get_proc_task(inode);
2098 unsigned int fd, ino;
2099 int retval;
2100 struct files_struct * files;
2102 retval = -ENOENT;
2103 if (!p)
2104 goto out_no_task;
2105 retval = 0;
2107 fd = filp->f_pos;
2108 switch (fd) {
2109 case 0:
2110 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2111 goto out;
2112 filp->f_pos++;
2113 case 1:
2114 ino = parent_ino(dentry);
2115 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2116 goto out;
2117 filp->f_pos++;
2118 default:
2119 files = get_files_struct(p);
2120 if (!files)
2121 goto out;
2122 rcu_read_lock();
2123 for (fd = filp->f_pos-2;
2124 fd < files_fdtable(files)->max_fds;
2125 fd++, filp->f_pos++) {
2126 char name[PROC_NUMBUF];
2127 int len;
2129 if (!fcheck_files(files, fd))
2130 continue;
2131 rcu_read_unlock();
2133 len = snprintf(name, sizeof(name), "%d", fd);
2134 if (proc_fill_cache(filp, dirent, filldir,
2135 name, len, instantiate,
2136 p, &fd) < 0) {
2137 rcu_read_lock();
2138 break;
2140 rcu_read_lock();
2142 rcu_read_unlock();
2143 put_files_struct(files);
2145 out:
2146 put_task_struct(p);
2147 out_no_task:
2148 return retval;
2151 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2152 struct nameidata *nd)
2154 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2157 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2159 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2162 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2163 size_t len, loff_t *ppos)
2165 char tmp[PROC_FDINFO_MAX];
2166 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2167 if (!err)
2168 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2169 return err;
2172 static const struct file_operations proc_fdinfo_file_operations = {
2173 .open = nonseekable_open,
2174 .read = proc_fdinfo_read,
2175 .llseek = no_llseek,
2178 static const struct file_operations proc_fd_operations = {
2179 .read = generic_read_dir,
2180 .readdir = proc_readfd,
2181 .llseek = default_llseek,
2185 * /proc/pid/fd needs a special permission handler so that a process can still
2186 * access /proc/self/fd after it has executed a setuid().
2188 static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
2190 int rv;
2192 if (flags & IPERM_FLAG_RCU)
2193 return -ECHILD;
2194 rv = generic_permission(inode, mask, flags, NULL);
2195 if (rv == 0)
2196 return 0;
2197 if (task_pid(current) == proc_pid(inode))
2198 rv = 0;
2199 return rv;
2203 * proc directories can do almost nothing..
2205 static const struct inode_operations proc_fd_inode_operations = {
2206 .lookup = proc_lookupfd,
2207 .permission = proc_fd_permission,
2208 .setattr = proc_setattr,
2211 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2212 struct dentry *dentry, struct task_struct *task, const void *ptr)
2214 unsigned fd = *(unsigned *)ptr;
2215 struct inode *inode;
2216 struct proc_inode *ei;
2217 struct dentry *error = ERR_PTR(-ENOENT);
2219 inode = proc_pid_make_inode(dir->i_sb, task);
2220 if (!inode)
2221 goto out;
2222 ei = PROC_I(inode);
2223 ei->fd = fd;
2224 inode->i_mode = S_IFREG | S_IRUSR;
2225 inode->i_fop = &proc_fdinfo_file_operations;
2226 d_set_d_op(dentry, &tid_fd_dentry_operations);
2227 d_add(dentry, inode);
2228 /* Close the race of the process dying before we return the dentry */
2229 if (tid_fd_revalidate(dentry, NULL))
2230 error = NULL;
2232 out:
2233 return error;
2236 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2237 struct dentry *dentry,
2238 struct nameidata *nd)
2240 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2243 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2245 return proc_readfd_common(filp, dirent, filldir,
2246 proc_fdinfo_instantiate);
2249 static const struct file_operations proc_fdinfo_operations = {
2250 .read = generic_read_dir,
2251 .readdir = proc_readfdinfo,
2252 .llseek = default_llseek,
2256 * proc directories can do almost nothing..
2258 static const struct inode_operations proc_fdinfo_inode_operations = {
2259 .lookup = proc_lookupfdinfo,
2260 .setattr = proc_setattr,
2264 static struct dentry *proc_pident_instantiate(struct inode *dir,
2265 struct dentry *dentry, struct task_struct *task, const void *ptr)
2267 const struct pid_entry *p = ptr;
2268 struct inode *inode;
2269 struct proc_inode *ei;
2270 struct dentry *error = ERR_PTR(-ENOENT);
2272 inode = proc_pid_make_inode(dir->i_sb, task);
2273 if (!inode)
2274 goto out;
2276 ei = PROC_I(inode);
2277 inode->i_mode = p->mode;
2278 if (S_ISDIR(inode->i_mode))
2279 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2280 if (p->iop)
2281 inode->i_op = p->iop;
2282 if (p->fop)
2283 inode->i_fop = p->fop;
2284 ei->op = p->op;
2285 d_set_d_op(dentry, &pid_dentry_operations);
2286 d_add(dentry, inode);
2287 /* Close the race of the process dying before we return the dentry */
2288 if (pid_revalidate(dentry, NULL))
2289 error = NULL;
2290 out:
2291 return error;
2294 static struct dentry *proc_pident_lookup(struct inode *dir,
2295 struct dentry *dentry,
2296 const struct pid_entry *ents,
2297 unsigned int nents)
2299 struct dentry *error;
2300 struct task_struct *task = get_proc_task(dir);
2301 const struct pid_entry *p, *last;
2303 error = ERR_PTR(-ENOENT);
2305 if (!task)
2306 goto out_no_task;
2309 * Yes, it does not scale. And it should not. Don't add
2310 * new entries into /proc/<tgid>/ without very good reasons.
2312 last = &ents[nents - 1];
2313 for (p = ents; p <= last; p++) {
2314 if (p->len != dentry->d_name.len)
2315 continue;
2316 if (!memcmp(dentry->d_name.name, p->name, p->len))
2317 break;
2319 if (p > last)
2320 goto out;
2322 error = proc_pident_instantiate(dir, dentry, task, p);
2323 out:
2324 put_task_struct(task);
2325 out_no_task:
2326 return error;
2329 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2330 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2332 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2333 proc_pident_instantiate, task, p);
2336 static int proc_pident_readdir(struct file *filp,
2337 void *dirent, filldir_t filldir,
2338 const struct pid_entry *ents, unsigned int nents)
2340 int i;
2341 struct dentry *dentry = filp->f_path.dentry;
2342 struct inode *inode = dentry->d_inode;
2343 struct task_struct *task = get_proc_task(inode);
2344 const struct pid_entry *p, *last;
2345 ino_t ino;
2346 int ret;
2348 ret = -ENOENT;
2349 if (!task)
2350 goto out_no_task;
2352 ret = 0;
2353 i = filp->f_pos;
2354 switch (i) {
2355 case 0:
2356 ino = inode->i_ino;
2357 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2358 goto out;
2359 i++;
2360 filp->f_pos++;
2361 /* fall through */
2362 case 1:
2363 ino = parent_ino(dentry);
2364 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2365 goto out;
2366 i++;
2367 filp->f_pos++;
2368 /* fall through */
2369 default:
2370 i -= 2;
2371 if (i >= nents) {
2372 ret = 1;
2373 goto out;
2375 p = ents + i;
2376 last = &ents[nents - 1];
2377 while (p <= last) {
2378 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2379 goto out;
2380 filp->f_pos++;
2381 p++;
2385 ret = 1;
2386 out:
2387 put_task_struct(task);
2388 out_no_task:
2389 return ret;
2392 #ifdef CONFIG_SECURITY
2393 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2394 size_t count, loff_t *ppos)
2396 struct inode * inode = file->f_path.dentry->d_inode;
2397 char *p = NULL;
2398 ssize_t length;
2399 struct task_struct *task = get_proc_task(inode);
2401 if (!task)
2402 return -ESRCH;
2404 length = security_getprocattr(task,
2405 (char*)file->f_path.dentry->d_name.name,
2406 &p);
2407 put_task_struct(task);
2408 if (length > 0)
2409 length = simple_read_from_buffer(buf, count, ppos, p, length);
2410 kfree(p);
2411 return length;
2414 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2415 size_t count, loff_t *ppos)
2417 struct inode * inode = file->f_path.dentry->d_inode;
2418 char *page;
2419 ssize_t length;
2420 struct task_struct *task = get_proc_task(inode);
2422 length = -ESRCH;
2423 if (!task)
2424 goto out_no_task;
2425 if (count > PAGE_SIZE)
2426 count = PAGE_SIZE;
2428 /* No partial writes. */
2429 length = -EINVAL;
2430 if (*ppos != 0)
2431 goto out;
2433 length = -ENOMEM;
2434 page = (char*)__get_free_page(GFP_TEMPORARY);
2435 if (!page)
2436 goto out;
2438 length = -EFAULT;
2439 if (copy_from_user(page, buf, count))
2440 goto out_free;
2442 /* Guard against adverse ptrace interaction */
2443 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2444 if (length < 0)
2445 goto out_free;
2447 length = security_setprocattr(task,
2448 (char*)file->f_path.dentry->d_name.name,
2449 (void*)page, count);
2450 mutex_unlock(&task->signal->cred_guard_mutex);
2451 out_free:
2452 free_page((unsigned long) page);
2453 out:
2454 put_task_struct(task);
2455 out_no_task:
2456 return length;
2459 static const struct file_operations proc_pid_attr_operations = {
2460 .read = proc_pid_attr_read,
2461 .write = proc_pid_attr_write,
2462 .llseek = generic_file_llseek,
2465 static const struct pid_entry attr_dir_stuff[] = {
2466 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2467 REG("prev", S_IRUGO, proc_pid_attr_operations),
2468 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2469 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2470 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2471 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2474 static int proc_attr_dir_readdir(struct file * filp,
2475 void * dirent, filldir_t filldir)
2477 return proc_pident_readdir(filp,dirent,filldir,
2478 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2481 static const struct file_operations proc_attr_dir_operations = {
2482 .read = generic_read_dir,
2483 .readdir = proc_attr_dir_readdir,
2484 .llseek = default_llseek,
2487 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2488 struct dentry *dentry, struct nameidata *nd)
2490 return proc_pident_lookup(dir, dentry,
2491 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2494 static const struct inode_operations proc_attr_dir_inode_operations = {
2495 .lookup = proc_attr_dir_lookup,
2496 .getattr = pid_getattr,
2497 .setattr = proc_setattr,
2500 #endif
2502 #ifdef CONFIG_ELF_CORE
2503 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2504 size_t count, loff_t *ppos)
2506 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2507 struct mm_struct *mm;
2508 char buffer[PROC_NUMBUF];
2509 size_t len;
2510 int ret;
2512 if (!task)
2513 return -ESRCH;
2515 ret = 0;
2516 mm = get_task_mm(task);
2517 if (mm) {
2518 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2519 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2520 MMF_DUMP_FILTER_SHIFT));
2521 mmput(mm);
2522 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2525 put_task_struct(task);
2527 return ret;
2530 static ssize_t proc_coredump_filter_write(struct file *file,
2531 const char __user *buf,
2532 size_t count,
2533 loff_t *ppos)
2535 struct task_struct *task;
2536 struct mm_struct *mm;
2537 char buffer[PROC_NUMBUF], *end;
2538 unsigned int val;
2539 int ret;
2540 int i;
2541 unsigned long mask;
2543 ret = -EFAULT;
2544 memset(buffer, 0, sizeof(buffer));
2545 if (count > sizeof(buffer) - 1)
2546 count = sizeof(buffer) - 1;
2547 if (copy_from_user(buffer, buf, count))
2548 goto out_no_task;
2550 ret = -EINVAL;
2551 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2552 if (*end == '\n')
2553 end++;
2554 if (end - buffer == 0)
2555 goto out_no_task;
2557 ret = -ESRCH;
2558 task = get_proc_task(file->f_dentry->d_inode);
2559 if (!task)
2560 goto out_no_task;
2562 ret = end - buffer;
2563 mm = get_task_mm(task);
2564 if (!mm)
2565 goto out_no_mm;
2567 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2568 if (val & mask)
2569 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2570 else
2571 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2574 mmput(mm);
2575 out_no_mm:
2576 put_task_struct(task);
2577 out_no_task:
2578 return ret;
2581 static const struct file_operations proc_coredump_filter_operations = {
2582 .read = proc_coredump_filter_read,
2583 .write = proc_coredump_filter_write,
2584 .llseek = generic_file_llseek,
2586 #endif
2589 * /proc/self:
2591 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2592 int buflen)
2594 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2595 pid_t tgid = task_tgid_nr_ns(current, ns);
2596 char tmp[PROC_NUMBUF];
2597 if (!tgid)
2598 return -ENOENT;
2599 sprintf(tmp, "%d", tgid);
2600 return vfs_readlink(dentry,buffer,buflen,tmp);
2603 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2605 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2606 pid_t tgid = task_tgid_nr_ns(current, ns);
2607 char *name = ERR_PTR(-ENOENT);
2608 if (tgid) {
2609 name = __getname();
2610 if (!name)
2611 name = ERR_PTR(-ENOMEM);
2612 else
2613 sprintf(name, "%d", tgid);
2615 nd_set_link(nd, name);
2616 return NULL;
2619 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2620 void *cookie)
2622 char *s = nd_get_link(nd);
2623 if (!IS_ERR(s))
2624 __putname(s);
2627 static const struct inode_operations proc_self_inode_operations = {
2628 .readlink = proc_self_readlink,
2629 .follow_link = proc_self_follow_link,
2630 .put_link = proc_self_put_link,
2634 * proc base
2636 * These are the directory entries in the root directory of /proc
2637 * that properly belong to the /proc filesystem, as they describe
2638 * describe something that is process related.
2640 static const struct pid_entry proc_base_stuff[] = {
2641 NOD("self", S_IFLNK|S_IRWXUGO,
2642 &proc_self_inode_operations, NULL, {}),
2645 static struct dentry *proc_base_instantiate(struct inode *dir,
2646 struct dentry *dentry, struct task_struct *task, const void *ptr)
2648 const struct pid_entry *p = ptr;
2649 struct inode *inode;
2650 struct proc_inode *ei;
2651 struct dentry *error;
2653 /* Allocate the inode */
2654 error = ERR_PTR(-ENOMEM);
2655 inode = new_inode(dir->i_sb);
2656 if (!inode)
2657 goto out;
2659 /* Initialize the inode */
2660 ei = PROC_I(inode);
2661 inode->i_ino = get_next_ino();
2662 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2665 * grab the reference to the task.
2667 ei->pid = get_task_pid(task, PIDTYPE_PID);
2668 if (!ei->pid)
2669 goto out_iput;
2671 inode->i_mode = p->mode;
2672 if (S_ISDIR(inode->i_mode))
2673 inode->i_nlink = 2;
2674 if (S_ISLNK(inode->i_mode))
2675 inode->i_size = 64;
2676 if (p->iop)
2677 inode->i_op = p->iop;
2678 if (p->fop)
2679 inode->i_fop = p->fop;
2680 ei->op = p->op;
2681 d_add(dentry, inode);
2682 error = NULL;
2683 out:
2684 return error;
2685 out_iput:
2686 iput(inode);
2687 goto out;
2690 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2692 struct dentry *error;
2693 struct task_struct *task = get_proc_task(dir);
2694 const struct pid_entry *p, *last;
2696 error = ERR_PTR(-ENOENT);
2698 if (!task)
2699 goto out_no_task;
2701 /* Lookup the directory entry */
2702 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2703 for (p = proc_base_stuff; p <= last; p++) {
2704 if (p->len != dentry->d_name.len)
2705 continue;
2706 if (!memcmp(dentry->d_name.name, p->name, p->len))
2707 break;
2709 if (p > last)
2710 goto out;
2712 error = proc_base_instantiate(dir, dentry, task, p);
2714 out:
2715 put_task_struct(task);
2716 out_no_task:
2717 return error;
2720 static int proc_base_fill_cache(struct file *filp, void *dirent,
2721 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2723 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2724 proc_base_instantiate, task, p);
2727 #ifdef CONFIG_TASK_IO_ACCOUNTING
2728 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2730 struct task_io_accounting acct = task->ioac;
2731 unsigned long flags;
2733 if (whole && lock_task_sighand(task, &flags)) {
2734 struct task_struct *t = task;
2736 task_io_accounting_add(&acct, &task->signal->ioac);
2737 while_each_thread(task, t)
2738 task_io_accounting_add(&acct, &t->ioac);
2740 unlock_task_sighand(task, &flags);
2742 return sprintf(buffer,
2743 "rchar: %llu\n"
2744 "wchar: %llu\n"
2745 "syscr: %llu\n"
2746 "syscw: %llu\n"
2747 "read_bytes: %llu\n"
2748 "write_bytes: %llu\n"
2749 "cancelled_write_bytes: %llu\n",
2750 (unsigned long long)acct.rchar,
2751 (unsigned long long)acct.wchar,
2752 (unsigned long long)acct.syscr,
2753 (unsigned long long)acct.syscw,
2754 (unsigned long long)acct.read_bytes,
2755 (unsigned long long)acct.write_bytes,
2756 (unsigned long long)acct.cancelled_write_bytes);
2759 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2761 return do_io_accounting(task, buffer, 0);
2764 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2766 return do_io_accounting(task, buffer, 1);
2768 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2770 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2771 struct pid *pid, struct task_struct *task)
2773 seq_printf(m, "%08x\n", task->personality);
2774 return 0;
2778 * Thread groups
2780 static const struct file_operations proc_task_operations;
2781 static const struct inode_operations proc_task_inode_operations;
2783 static const struct pid_entry tgid_base_stuff[] = {
2784 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2785 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2786 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2787 #ifdef CONFIG_NET
2788 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2789 #endif
2790 REG("environ", S_IRUSR, proc_environ_operations),
2791 INF("auxv", S_IRUSR, proc_pid_auxv),
2792 ONE("status", S_IRUGO, proc_pid_status),
2793 ONE("personality", S_IRUSR, proc_pid_personality),
2794 INF("limits", S_IRUGO, proc_pid_limits),
2795 #ifdef CONFIG_SCHED_DEBUG
2796 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2797 #endif
2798 #ifdef CONFIG_SCHED_AUTOGROUP
2799 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2800 #endif
2801 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2802 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2803 INF("syscall", S_IRUSR, proc_pid_syscall),
2804 #endif
2805 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2806 ONE("stat", S_IRUGO, proc_tgid_stat),
2807 ONE("statm", S_IRUGO, proc_pid_statm),
2808 REG("maps", S_IRUGO, proc_maps_operations),
2809 #ifdef CONFIG_NUMA
2810 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2811 #endif
2812 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2813 LNK("cwd", proc_cwd_link),
2814 LNK("root", proc_root_link),
2815 LNK("exe", proc_exe_link),
2816 REG("mounts", S_IRUGO, proc_mounts_operations),
2817 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2818 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2819 #ifdef CONFIG_PROC_PAGE_MONITOR
2820 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2821 REG("smaps", S_IRUGO, proc_smaps_operations),
2822 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2823 #endif
2824 #ifdef CONFIG_SECURITY
2825 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2826 #endif
2827 #ifdef CONFIG_KALLSYMS
2828 INF("wchan", S_IRUGO, proc_pid_wchan),
2829 #endif
2830 #ifdef CONFIG_STACKTRACE
2831 ONE("stack", S_IRUSR, proc_pid_stack),
2832 #endif
2833 #ifdef CONFIG_SCHEDSTATS
2834 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2835 #endif
2836 #ifdef CONFIG_LATENCYTOP
2837 REG("latency", S_IRUGO, proc_lstats_operations),
2838 #endif
2839 #ifdef CONFIG_PROC_PID_CPUSET
2840 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2841 #endif
2842 #ifdef CONFIG_CGROUPS
2843 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2844 #endif
2845 INF("oom_score", S_IRUGO, proc_oom_score),
2846 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
2847 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2848 #ifdef CONFIG_AUDITSYSCALL
2849 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2850 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2851 #endif
2852 #ifdef CONFIG_FAULT_INJECTION
2853 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2854 #endif
2855 #ifdef CONFIG_ELF_CORE
2856 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2857 #endif
2858 #ifdef CONFIG_TASK_IO_ACCOUNTING
2859 INF("io", S_IRUGO, proc_tgid_io_accounting),
2860 #endif
2863 static int proc_tgid_base_readdir(struct file * filp,
2864 void * dirent, filldir_t filldir)
2866 return proc_pident_readdir(filp,dirent,filldir,
2867 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2870 static const struct file_operations proc_tgid_base_operations = {
2871 .read = generic_read_dir,
2872 .readdir = proc_tgid_base_readdir,
2873 .llseek = default_llseek,
2876 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2877 return proc_pident_lookup(dir, dentry,
2878 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2881 static const struct inode_operations proc_tgid_base_inode_operations = {
2882 .lookup = proc_tgid_base_lookup,
2883 .getattr = pid_getattr,
2884 .setattr = proc_setattr,
2887 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2889 struct dentry *dentry, *leader, *dir;
2890 char buf[PROC_NUMBUF];
2891 struct qstr name;
2893 name.name = buf;
2894 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2895 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2896 if (dentry) {
2897 shrink_dcache_parent(dentry);
2898 d_drop(dentry);
2899 dput(dentry);
2902 name.name = buf;
2903 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2904 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2905 if (!leader)
2906 goto out;
2908 name.name = "task";
2909 name.len = strlen(name.name);
2910 dir = d_hash_and_lookup(leader, &name);
2911 if (!dir)
2912 goto out_put_leader;
2914 name.name = buf;
2915 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2916 dentry = d_hash_and_lookup(dir, &name);
2917 if (dentry) {
2918 shrink_dcache_parent(dentry);
2919 d_drop(dentry);
2920 dput(dentry);
2923 dput(dir);
2924 out_put_leader:
2925 dput(leader);
2926 out:
2927 return;
2931 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2932 * @task: task that should be flushed.
2934 * When flushing dentries from proc, one needs to flush them from global
2935 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2936 * in. This call is supposed to do all of this job.
2938 * Looks in the dcache for
2939 * /proc/@pid
2940 * /proc/@tgid/task/@pid
2941 * if either directory is present flushes it and all of it'ts children
2942 * from the dcache.
2944 * It is safe and reasonable to cache /proc entries for a task until
2945 * that task exits. After that they just clog up the dcache with
2946 * useless entries, possibly causing useful dcache entries to be
2947 * flushed instead. This routine is proved to flush those useless
2948 * dcache entries at process exit time.
2950 * NOTE: This routine is just an optimization so it does not guarantee
2951 * that no dcache entries will exist at process exit time it
2952 * just makes it very unlikely that any will persist.
2955 void proc_flush_task(struct task_struct *task)
2957 int i;
2958 struct pid *pid, *tgid;
2959 struct upid *upid;
2961 pid = task_pid(task);
2962 tgid = task_tgid(task);
2964 for (i = 0; i <= pid->level; i++) {
2965 upid = &pid->numbers[i];
2966 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2967 tgid->numbers[i].nr);
2970 upid = &pid->numbers[pid->level];
2971 if (upid->nr == 1)
2972 pid_ns_release_proc(upid->ns);
2975 static struct dentry *proc_pid_instantiate(struct inode *dir,
2976 struct dentry * dentry,
2977 struct task_struct *task, const void *ptr)
2979 struct dentry *error = ERR_PTR(-ENOENT);
2980 struct inode *inode;
2982 inode = proc_pid_make_inode(dir->i_sb, task);
2983 if (!inode)
2984 goto out;
2986 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2987 inode->i_op = &proc_tgid_base_inode_operations;
2988 inode->i_fop = &proc_tgid_base_operations;
2989 inode->i_flags|=S_IMMUTABLE;
2991 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2992 ARRAY_SIZE(tgid_base_stuff));
2994 d_set_d_op(dentry, &pid_dentry_operations);
2996 d_add(dentry, inode);
2997 /* Close the race of the process dying before we return the dentry */
2998 if (pid_revalidate(dentry, NULL))
2999 error = NULL;
3000 out:
3001 return error;
3004 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3006 struct dentry *result;
3007 struct task_struct *task;
3008 unsigned tgid;
3009 struct pid_namespace *ns;
3011 result = proc_base_lookup(dir, dentry);
3012 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3013 goto out;
3015 tgid = name_to_int(dentry);
3016 if (tgid == ~0U)
3017 goto out;
3019 ns = dentry->d_sb->s_fs_info;
3020 rcu_read_lock();
3021 task = find_task_by_pid_ns(tgid, ns);
3022 if (task)
3023 get_task_struct(task);
3024 rcu_read_unlock();
3025 if (!task)
3026 goto out;
3028 result = proc_pid_instantiate(dir, dentry, task, NULL);
3029 put_task_struct(task);
3030 out:
3031 return result;
3035 * Find the first task with tgid >= tgid
3038 struct tgid_iter {
3039 unsigned int tgid;
3040 struct task_struct *task;
3042 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3044 struct pid *pid;
3046 if (iter.task)
3047 put_task_struct(iter.task);
3048 rcu_read_lock();
3049 retry:
3050 iter.task = NULL;
3051 pid = find_ge_pid(iter.tgid, ns);
3052 if (pid) {
3053 iter.tgid = pid_nr_ns(pid, ns);
3054 iter.task = pid_task(pid, PIDTYPE_PID);
3055 /* What we to know is if the pid we have find is the
3056 * pid of a thread_group_leader. Testing for task
3057 * being a thread_group_leader is the obvious thing
3058 * todo but there is a window when it fails, due to
3059 * the pid transfer logic in de_thread.
3061 * So we perform the straight forward test of seeing
3062 * if the pid we have found is the pid of a thread
3063 * group leader, and don't worry if the task we have
3064 * found doesn't happen to be a thread group leader.
3065 * As we don't care in the case of readdir.
3067 if (!iter.task || !has_group_leader_pid(iter.task)) {
3068 iter.tgid += 1;
3069 goto retry;
3071 get_task_struct(iter.task);
3073 rcu_read_unlock();
3074 return iter;
3077 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3079 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3080 struct tgid_iter iter)
3082 char name[PROC_NUMBUF];
3083 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3084 return proc_fill_cache(filp, dirent, filldir, name, len,
3085 proc_pid_instantiate, iter.task, NULL);
3088 /* for the /proc/ directory itself, after non-process stuff has been done */
3089 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3091 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3092 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
3093 struct tgid_iter iter;
3094 struct pid_namespace *ns;
3096 if (!reaper)
3097 goto out_no_task;
3099 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3100 const struct pid_entry *p = &proc_base_stuff[nr];
3101 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3102 goto out;
3105 ns = filp->f_dentry->d_sb->s_fs_info;
3106 iter.task = NULL;
3107 iter.tgid = filp->f_pos - TGID_OFFSET;
3108 for (iter = next_tgid(ns, iter);
3109 iter.task;
3110 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3111 filp->f_pos = iter.tgid + TGID_OFFSET;
3112 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3113 put_task_struct(iter.task);
3114 goto out;
3117 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3118 out:
3119 put_task_struct(reaper);
3120 out_no_task:
3121 return 0;
3125 * Tasks
3127 static const struct pid_entry tid_base_stuff[] = {
3128 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3129 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3130 REG("environ", S_IRUSR, proc_environ_operations),
3131 INF("auxv", S_IRUSR, proc_pid_auxv),
3132 ONE("status", S_IRUGO, proc_pid_status),
3133 ONE("personality", S_IRUSR, proc_pid_personality),
3134 INF("limits", S_IRUGO, proc_pid_limits),
3135 #ifdef CONFIG_SCHED_DEBUG
3136 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3137 #endif
3138 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3139 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3140 INF("syscall", S_IRUSR, proc_pid_syscall),
3141 #endif
3142 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3143 ONE("stat", S_IRUGO, proc_tid_stat),
3144 ONE("statm", S_IRUGO, proc_pid_statm),
3145 REG("maps", S_IRUGO, proc_maps_operations),
3146 #ifdef CONFIG_NUMA
3147 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3148 #endif
3149 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3150 LNK("cwd", proc_cwd_link),
3151 LNK("root", proc_root_link),
3152 LNK("exe", proc_exe_link),
3153 REG("mounts", S_IRUGO, proc_mounts_operations),
3154 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3155 #ifdef CONFIG_PROC_PAGE_MONITOR
3156 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3157 REG("smaps", S_IRUGO, proc_smaps_operations),
3158 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3159 #endif
3160 #ifdef CONFIG_SECURITY
3161 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3162 #endif
3163 #ifdef CONFIG_KALLSYMS
3164 INF("wchan", S_IRUGO, proc_pid_wchan),
3165 #endif
3166 #ifdef CONFIG_STACKTRACE
3167 ONE("stack", S_IRUSR, proc_pid_stack),
3168 #endif
3169 #ifdef CONFIG_SCHEDSTATS
3170 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3171 #endif
3172 #ifdef CONFIG_LATENCYTOP
3173 REG("latency", S_IRUGO, proc_lstats_operations),
3174 #endif
3175 #ifdef CONFIG_PROC_PID_CPUSET
3176 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3177 #endif
3178 #ifdef CONFIG_CGROUPS
3179 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3180 #endif
3181 INF("oom_score", S_IRUGO, proc_oom_score),
3182 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3183 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3184 #ifdef CONFIG_AUDITSYSCALL
3185 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3186 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3187 #endif
3188 #ifdef CONFIG_FAULT_INJECTION
3189 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3190 #endif
3191 #ifdef CONFIG_TASK_IO_ACCOUNTING
3192 INF("io", S_IRUGO, proc_tid_io_accounting),
3193 #endif
3196 static int proc_tid_base_readdir(struct file * filp,
3197 void * dirent, filldir_t filldir)
3199 return proc_pident_readdir(filp,dirent,filldir,
3200 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3203 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3204 return proc_pident_lookup(dir, dentry,
3205 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3208 static const struct file_operations proc_tid_base_operations = {
3209 .read = generic_read_dir,
3210 .readdir = proc_tid_base_readdir,
3211 .llseek = default_llseek,
3214 static const struct inode_operations proc_tid_base_inode_operations = {
3215 .lookup = proc_tid_base_lookup,
3216 .getattr = pid_getattr,
3217 .setattr = proc_setattr,
3220 static struct dentry *proc_task_instantiate(struct inode *dir,
3221 struct dentry *dentry, struct task_struct *task, const void *ptr)
3223 struct dentry *error = ERR_PTR(-ENOENT);
3224 struct inode *inode;
3225 inode = proc_pid_make_inode(dir->i_sb, task);
3227 if (!inode)
3228 goto out;
3229 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3230 inode->i_op = &proc_tid_base_inode_operations;
3231 inode->i_fop = &proc_tid_base_operations;
3232 inode->i_flags|=S_IMMUTABLE;
3234 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3235 ARRAY_SIZE(tid_base_stuff));
3237 d_set_d_op(dentry, &pid_dentry_operations);
3239 d_add(dentry, inode);
3240 /* Close the race of the process dying before we return the dentry */
3241 if (pid_revalidate(dentry, NULL))
3242 error = NULL;
3243 out:
3244 return error;
3247 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3249 struct dentry *result = ERR_PTR(-ENOENT);
3250 struct task_struct *task;
3251 struct task_struct *leader = get_proc_task(dir);
3252 unsigned tid;
3253 struct pid_namespace *ns;
3255 if (!leader)
3256 goto out_no_task;
3258 tid = name_to_int(dentry);
3259 if (tid == ~0U)
3260 goto out;
3262 ns = dentry->d_sb->s_fs_info;
3263 rcu_read_lock();
3264 task = find_task_by_pid_ns(tid, ns);
3265 if (task)
3266 get_task_struct(task);
3267 rcu_read_unlock();
3268 if (!task)
3269 goto out;
3270 if (!same_thread_group(leader, task))
3271 goto out_drop_task;
3273 result = proc_task_instantiate(dir, dentry, task, NULL);
3274 out_drop_task:
3275 put_task_struct(task);
3276 out:
3277 put_task_struct(leader);
3278 out_no_task:
3279 return result;
3283 * Find the first tid of a thread group to return to user space.
3285 * Usually this is just the thread group leader, but if the users
3286 * buffer was too small or there was a seek into the middle of the
3287 * directory we have more work todo.
3289 * In the case of a short read we start with find_task_by_pid.
3291 * In the case of a seek we start with the leader and walk nr
3292 * threads past it.
3294 static struct task_struct *first_tid(struct task_struct *leader,
3295 int tid, int nr, struct pid_namespace *ns)
3297 struct task_struct *pos;
3299 rcu_read_lock();
3300 /* Attempt to start with the pid of a thread */
3301 if (tid && (nr > 0)) {
3302 pos = find_task_by_pid_ns(tid, ns);
3303 if (pos && (pos->group_leader == leader))
3304 goto found;
3307 /* If nr exceeds the number of threads there is nothing todo */
3308 pos = NULL;
3309 if (nr && nr >= get_nr_threads(leader))
3310 goto out;
3312 /* If we haven't found our starting place yet start
3313 * with the leader and walk nr threads forward.
3315 for (pos = leader; nr > 0; --nr) {
3316 pos = next_thread(pos);
3317 if (pos == leader) {
3318 pos = NULL;
3319 goto out;
3322 found:
3323 get_task_struct(pos);
3324 out:
3325 rcu_read_unlock();
3326 return pos;
3330 * Find the next thread in the thread list.
3331 * Return NULL if there is an error or no next thread.
3333 * The reference to the input task_struct is released.
3335 static struct task_struct *next_tid(struct task_struct *start)
3337 struct task_struct *pos = NULL;
3338 rcu_read_lock();
3339 if (pid_alive(start)) {
3340 pos = next_thread(start);
3341 if (thread_group_leader(pos))
3342 pos = NULL;
3343 else
3344 get_task_struct(pos);
3346 rcu_read_unlock();
3347 put_task_struct(start);
3348 return pos;
3351 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3352 struct task_struct *task, int tid)
3354 char name[PROC_NUMBUF];
3355 int len = snprintf(name, sizeof(name), "%d", tid);
3356 return proc_fill_cache(filp, dirent, filldir, name, len,
3357 proc_task_instantiate, task, NULL);
3360 /* for the /proc/TGID/task/ directories */
3361 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3363 struct dentry *dentry = filp->f_path.dentry;
3364 struct inode *inode = dentry->d_inode;
3365 struct task_struct *leader = NULL;
3366 struct task_struct *task;
3367 int retval = -ENOENT;
3368 ino_t ino;
3369 int tid;
3370 struct pid_namespace *ns;
3372 task = get_proc_task(inode);
3373 if (!task)
3374 goto out_no_task;
3375 rcu_read_lock();
3376 if (pid_alive(task)) {
3377 leader = task->group_leader;
3378 get_task_struct(leader);
3380 rcu_read_unlock();
3381 put_task_struct(task);
3382 if (!leader)
3383 goto out_no_task;
3384 retval = 0;
3386 switch ((unsigned long)filp->f_pos) {
3387 case 0:
3388 ino = inode->i_ino;
3389 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3390 goto out;
3391 filp->f_pos++;
3392 /* fall through */
3393 case 1:
3394 ino = parent_ino(dentry);
3395 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3396 goto out;
3397 filp->f_pos++;
3398 /* fall through */
3401 /* f_version caches the tgid value that the last readdir call couldn't
3402 * return. lseek aka telldir automagically resets f_version to 0.
3404 ns = filp->f_dentry->d_sb->s_fs_info;
3405 tid = (int)filp->f_version;
3406 filp->f_version = 0;
3407 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3408 task;
3409 task = next_tid(task), filp->f_pos++) {
3410 tid = task_pid_nr_ns(task, ns);
3411 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3412 /* returning this tgid failed, save it as the first
3413 * pid for the next readir call */
3414 filp->f_version = (u64)tid;
3415 put_task_struct(task);
3416 break;
3419 out:
3420 put_task_struct(leader);
3421 out_no_task:
3422 return retval;
3425 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3427 struct inode *inode = dentry->d_inode;
3428 struct task_struct *p = get_proc_task(inode);
3429 generic_fillattr(inode, stat);
3431 if (p) {
3432 stat->nlink += get_nr_threads(p);
3433 put_task_struct(p);
3436 return 0;
3439 static const struct inode_operations proc_task_inode_operations = {
3440 .lookup = proc_task_lookup,
3441 .getattr = proc_task_getattr,
3442 .setattr = proc_setattr,
3445 static const struct file_operations proc_task_operations = {
3446 .read = generic_read_dir,
3447 .readdir = proc_task_readdir,
3448 .llseek = default_llseek,