thinkpad-acpi: Fix procfs hotkey reset command
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / proc / base.c
blob6505ff8f19d46f85b5c986ee6fe9a93f60977da8
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/resource.h>
69 #include <linux/module.h>
70 #include <linux/mount.h>
71 #include <linux/security.h>
72 #include <linux/ptrace.h>
73 #include <linux/tracehook.h>
74 #include <linux/cgroup.h>
75 #include <linux/cpuset.h>
76 #include <linux/audit.h>
77 #include <linux/poll.h>
78 #include <linux/nsproxy.h>
79 #include <linux/oom.h>
80 #include <linux/elf.h>
81 #include <linux/pid_namespace.h>
82 #include "internal.h"
84 /* NOTE:
85 * Implementing inode permission operations in /proc is almost
86 * certainly an error. Permission checks need to happen during
87 * each system call not at open time. The reason is that most of
88 * what we wish to check for permissions in /proc varies at runtime.
90 * The classic example of a problem is opening file descriptors
91 * in /proc for a task before it execs a suid executable.
94 struct pid_entry {
95 char *name;
96 int len;
97 mode_t mode;
98 const struct inode_operations *iop;
99 const struct file_operations *fop;
100 union proc_op op;
103 #define NOD(NAME, MODE, IOP, FOP, OP) { \
104 .name = (NAME), \
105 .len = sizeof(NAME) - 1, \
106 .mode = MODE, \
107 .iop = IOP, \
108 .fop = FOP, \
109 .op = OP, \
112 #define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 {} )
116 #define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
120 #define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
122 &proc_##OTYPE##_operations, {})
123 #define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
127 #define ONE(NAME, MODE, OTYPE) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
130 { .proc_show = &proc_##OTYPE } )
133 * Count the number of hardlinks for the pid_entry table, excluding the .
134 * and .. links.
136 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
137 unsigned int n)
139 unsigned int i;
140 unsigned int count;
142 count = 0;
143 for (i = 0; i < n; ++i) {
144 if (S_ISDIR(entries[i].mode))
145 ++count;
148 return count;
151 static int get_fs_path(struct task_struct *task, struct path *path, bool root)
153 struct fs_struct *fs;
154 int result = -ENOENT;
156 task_lock(task);
157 fs = task->fs;
158 if (fs) {
159 read_lock(&fs->lock);
160 *path = root ? fs->root : fs->pwd;
161 path_get(path);
162 read_unlock(&fs->lock);
163 result = 0;
165 task_unlock(task);
166 return result;
169 static int get_nr_threads(struct task_struct *tsk)
171 unsigned long flags;
172 int count = 0;
174 if (lock_task_sighand(tsk, &flags)) {
175 count = atomic_read(&tsk->signal->count);
176 unlock_task_sighand(tsk, &flags);
178 return count;
181 static int proc_cwd_link(struct inode *inode, struct path *path)
183 struct task_struct *task = get_proc_task(inode);
184 int result = -ENOENT;
186 if (task) {
187 result = get_fs_path(task, path, 0);
188 put_task_struct(task);
190 return result;
193 static int proc_root_link(struct inode *inode, struct path *path)
195 struct task_struct *task = get_proc_task(inode);
196 int result = -ENOENT;
198 if (task) {
199 result = get_fs_path(task, path, 1);
200 put_task_struct(task);
202 return result;
206 * Return zero if current may access user memory in @task, -error if not.
208 static int check_mem_permission(struct task_struct *task)
211 * A task can always look at itself, in case it chooses
212 * to use system calls instead of load instructions.
214 if (task == current)
215 return 0;
218 * If current is actively ptrace'ing, and would also be
219 * permitted to freshly attach with ptrace now, permit it.
221 if (task_is_stopped_or_traced(task)) {
222 int match;
223 rcu_read_lock();
224 match = (tracehook_tracer_task(task) == current);
225 rcu_read_unlock();
226 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
227 return 0;
231 * Noone else is allowed.
233 return -EPERM;
236 struct mm_struct *mm_for_maps(struct task_struct *task)
238 struct mm_struct *mm = get_task_mm(task);
239 if (!mm)
240 return NULL;
241 down_read(&mm->mmap_sem);
242 task_lock(task);
243 if (task->mm != mm)
244 goto out;
245 if (task->mm != current->mm &&
246 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
247 goto out;
248 task_unlock(task);
249 return mm;
250 out:
251 task_unlock(task);
252 up_read(&mm->mmap_sem);
253 mmput(mm);
254 return NULL;
257 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
259 int res = 0;
260 unsigned int len;
261 struct mm_struct *mm = get_task_mm(task);
262 if (!mm)
263 goto out;
264 if (!mm->arg_end)
265 goto out_mm; /* Shh! No looking before we're done */
267 len = mm->arg_end - mm->arg_start;
269 if (len > PAGE_SIZE)
270 len = PAGE_SIZE;
272 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
274 // If the nul at the end of args has been overwritten, then
275 // assume application is using setproctitle(3).
276 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
277 len = strnlen(buffer, res);
278 if (len < res) {
279 res = len;
280 } else {
281 len = mm->env_end - mm->env_start;
282 if (len > PAGE_SIZE - res)
283 len = PAGE_SIZE - res;
284 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
285 res = strnlen(buffer, res);
288 out_mm:
289 mmput(mm);
290 out:
291 return res;
294 static int proc_pid_auxv(struct task_struct *task, char *buffer)
296 int res = 0;
297 struct mm_struct *mm = get_task_mm(task);
298 if (mm) {
299 unsigned int nwords = 0;
301 nwords += 2;
302 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
303 res = nwords * sizeof(mm->saved_auxv[0]);
304 if (res > PAGE_SIZE)
305 res = PAGE_SIZE;
306 memcpy(buffer, mm->saved_auxv, res);
307 mmput(mm);
309 return res;
313 #ifdef CONFIG_KALLSYMS
315 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
316 * Returns the resolved symbol. If that fails, simply return the address.
318 static int proc_pid_wchan(struct task_struct *task, char *buffer)
320 unsigned long wchan;
321 char symname[KSYM_NAME_LEN];
323 wchan = get_wchan(task);
325 if (lookup_symbol_name(wchan, symname) < 0)
326 return sprintf(buffer, "%lu", wchan);
327 else
328 return sprintf(buffer, "%s", symname);
330 #endif /* CONFIG_KALLSYMS */
332 #ifdef CONFIG_SCHEDSTATS
334 * Provides /proc/PID/schedstat
336 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
338 return sprintf(buffer, "%llu %llu %lu\n",
339 task->sched_info.cpu_time,
340 task->sched_info.run_delay,
341 task->sched_info.pcount);
343 #endif
345 #ifdef CONFIG_LATENCYTOP
346 static int lstats_show_proc(struct seq_file *m, void *v)
348 int i;
349 struct inode *inode = m->private;
350 struct task_struct *task = get_proc_task(inode);
352 if (!task)
353 return -ESRCH;
354 seq_puts(m, "Latency Top version : v0.1\n");
355 for (i = 0; i < 32; i++) {
356 if (task->latency_record[i].backtrace[0]) {
357 int q;
358 seq_printf(m, "%i %li %li ",
359 task->latency_record[i].count,
360 task->latency_record[i].time,
361 task->latency_record[i].max);
362 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
363 char sym[KSYM_SYMBOL_LEN];
364 char *c;
365 if (!task->latency_record[i].backtrace[q])
366 break;
367 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
368 break;
369 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
370 c = strchr(sym, '+');
371 if (c)
372 *c = 0;
373 seq_printf(m, "%s ", sym);
375 seq_printf(m, "\n");
379 put_task_struct(task);
380 return 0;
383 static int lstats_open(struct inode *inode, struct file *file)
385 return single_open(file, lstats_show_proc, inode);
388 static ssize_t lstats_write(struct file *file, const char __user *buf,
389 size_t count, loff_t *offs)
391 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
393 if (!task)
394 return -ESRCH;
395 clear_all_latency_tracing(task);
396 put_task_struct(task);
398 return count;
401 static const struct file_operations proc_lstats_operations = {
402 .open = lstats_open,
403 .read = seq_read,
404 .write = lstats_write,
405 .llseek = seq_lseek,
406 .release = single_release,
409 #endif
411 /* The badness from the OOM killer */
412 unsigned long badness(struct task_struct *p, unsigned long uptime);
413 static int proc_oom_score(struct task_struct *task, char *buffer)
415 unsigned long points;
416 struct timespec uptime;
418 do_posix_clock_monotonic_gettime(&uptime);
419 read_lock(&tasklist_lock);
420 points = badness(task, uptime.tv_sec);
421 read_unlock(&tasklist_lock);
422 return sprintf(buffer, "%lu\n", points);
425 struct limit_names {
426 char *name;
427 char *unit;
430 static const struct limit_names lnames[RLIM_NLIMITS] = {
431 [RLIMIT_CPU] = {"Max cpu time", "ms"},
432 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
433 [RLIMIT_DATA] = {"Max data size", "bytes"},
434 [RLIMIT_STACK] = {"Max stack size", "bytes"},
435 [RLIMIT_CORE] = {"Max core file size", "bytes"},
436 [RLIMIT_RSS] = {"Max resident set", "bytes"},
437 [RLIMIT_NPROC] = {"Max processes", "processes"},
438 [RLIMIT_NOFILE] = {"Max open files", "files"},
439 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
440 [RLIMIT_AS] = {"Max address space", "bytes"},
441 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
442 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
443 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
444 [RLIMIT_NICE] = {"Max nice priority", NULL},
445 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
446 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
449 /* Display limits for a process */
450 static int proc_pid_limits(struct task_struct *task, char *buffer)
452 unsigned int i;
453 int count = 0;
454 unsigned long flags;
455 char *bufptr = buffer;
457 struct rlimit rlim[RLIM_NLIMITS];
459 if (!lock_task_sighand(task, &flags))
460 return 0;
461 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
462 unlock_task_sighand(task, &flags);
465 * print the file header
467 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
468 "Limit", "Soft Limit", "Hard Limit", "Units");
470 for (i = 0; i < RLIM_NLIMITS; i++) {
471 if (rlim[i].rlim_cur == RLIM_INFINITY)
472 count += sprintf(&bufptr[count], "%-25s %-20s ",
473 lnames[i].name, "unlimited");
474 else
475 count += sprintf(&bufptr[count], "%-25s %-20lu ",
476 lnames[i].name, rlim[i].rlim_cur);
478 if (rlim[i].rlim_max == RLIM_INFINITY)
479 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
480 else
481 count += sprintf(&bufptr[count], "%-20lu ",
482 rlim[i].rlim_max);
484 if (lnames[i].unit)
485 count += sprintf(&bufptr[count], "%-10s\n",
486 lnames[i].unit);
487 else
488 count += sprintf(&bufptr[count], "\n");
491 return count;
494 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
495 static int proc_pid_syscall(struct task_struct *task, char *buffer)
497 long nr;
498 unsigned long args[6], sp, pc;
500 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
501 return sprintf(buffer, "running\n");
503 if (nr < 0)
504 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
506 return sprintf(buffer,
507 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
509 args[0], args[1], args[2], args[3], args[4], args[5],
510 sp, pc);
512 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
514 /************************************************************************/
515 /* Here the fs part begins */
516 /************************************************************************/
518 /* permission checks */
519 static int proc_fd_access_allowed(struct inode *inode)
521 struct task_struct *task;
522 int allowed = 0;
523 /* Allow access to a task's file descriptors if it is us or we
524 * may use ptrace attach to the process and find out that
525 * information.
527 task = get_proc_task(inode);
528 if (task) {
529 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
530 put_task_struct(task);
532 return allowed;
535 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
537 int error;
538 struct inode *inode = dentry->d_inode;
540 if (attr->ia_valid & ATTR_MODE)
541 return -EPERM;
543 error = inode_change_ok(inode, attr);
544 if (!error)
545 error = inode_setattr(inode, attr);
546 return error;
549 static const struct inode_operations proc_def_inode_operations = {
550 .setattr = proc_setattr,
553 static int mounts_open_common(struct inode *inode, struct file *file,
554 const struct seq_operations *op)
556 struct task_struct *task = get_proc_task(inode);
557 struct nsproxy *nsp;
558 struct mnt_namespace *ns = NULL;
559 struct path root;
560 struct proc_mounts *p;
561 int ret = -EINVAL;
563 if (task) {
564 rcu_read_lock();
565 nsp = task_nsproxy(task);
566 if (nsp) {
567 ns = nsp->mnt_ns;
568 if (ns)
569 get_mnt_ns(ns);
571 rcu_read_unlock();
572 if (ns && get_fs_path(task, &root, 1) == 0)
573 ret = 0;
574 put_task_struct(task);
577 if (!ns)
578 goto err;
579 if (ret)
580 goto err_put_ns;
582 ret = -ENOMEM;
583 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
584 if (!p)
585 goto err_put_path;
587 file->private_data = &p->m;
588 ret = seq_open(file, op);
589 if (ret)
590 goto err_free;
592 p->m.private = p;
593 p->ns = ns;
594 p->root = root;
595 p->event = ns->event;
597 return 0;
599 err_free:
600 kfree(p);
601 err_put_path:
602 path_put(&root);
603 err_put_ns:
604 put_mnt_ns(ns);
605 err:
606 return ret;
609 static int mounts_release(struct inode *inode, struct file *file)
611 struct proc_mounts *p = file->private_data;
612 path_put(&p->root);
613 put_mnt_ns(p->ns);
614 return seq_release(inode, file);
617 static unsigned mounts_poll(struct file *file, poll_table *wait)
619 struct proc_mounts *p = file->private_data;
620 struct mnt_namespace *ns = p->ns;
621 unsigned res = 0;
623 poll_wait(file, &ns->poll, wait);
625 spin_lock(&vfsmount_lock);
626 if (p->event != ns->event) {
627 p->event = ns->event;
628 res = POLLERR;
630 spin_unlock(&vfsmount_lock);
632 return res;
635 static int mounts_open(struct inode *inode, struct file *file)
637 return mounts_open_common(inode, file, &mounts_op);
640 static const struct file_operations proc_mounts_operations = {
641 .open = mounts_open,
642 .read = seq_read,
643 .llseek = seq_lseek,
644 .release = mounts_release,
645 .poll = mounts_poll,
648 static int mountinfo_open(struct inode *inode, struct file *file)
650 return mounts_open_common(inode, file, &mountinfo_op);
653 static const struct file_operations proc_mountinfo_operations = {
654 .open = mountinfo_open,
655 .read = seq_read,
656 .llseek = seq_lseek,
657 .release = mounts_release,
658 .poll = mounts_poll,
661 static int mountstats_open(struct inode *inode, struct file *file)
663 return mounts_open_common(inode, file, &mountstats_op);
666 static const struct file_operations proc_mountstats_operations = {
667 .open = mountstats_open,
668 .read = seq_read,
669 .llseek = seq_lseek,
670 .release = mounts_release,
673 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
675 static ssize_t proc_info_read(struct file * file, char __user * buf,
676 size_t count, loff_t *ppos)
678 struct inode * inode = file->f_path.dentry->d_inode;
679 unsigned long page;
680 ssize_t length;
681 struct task_struct *task = get_proc_task(inode);
683 length = -ESRCH;
684 if (!task)
685 goto out_no_task;
687 if (count > PROC_BLOCK_SIZE)
688 count = PROC_BLOCK_SIZE;
690 length = -ENOMEM;
691 if (!(page = __get_free_page(GFP_TEMPORARY)))
692 goto out;
694 length = PROC_I(inode)->op.proc_read(task, (char*)page);
696 if (length >= 0)
697 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
698 free_page(page);
699 out:
700 put_task_struct(task);
701 out_no_task:
702 return length;
705 static const struct file_operations proc_info_file_operations = {
706 .read = proc_info_read,
709 static int proc_single_show(struct seq_file *m, void *v)
711 struct inode *inode = m->private;
712 struct pid_namespace *ns;
713 struct pid *pid;
714 struct task_struct *task;
715 int ret;
717 ns = inode->i_sb->s_fs_info;
718 pid = proc_pid(inode);
719 task = get_pid_task(pid, PIDTYPE_PID);
720 if (!task)
721 return -ESRCH;
723 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
725 put_task_struct(task);
726 return ret;
729 static int proc_single_open(struct inode *inode, struct file *filp)
731 int ret;
732 ret = single_open(filp, proc_single_show, NULL);
733 if (!ret) {
734 struct seq_file *m = filp->private_data;
736 m->private = inode;
738 return ret;
741 static const struct file_operations proc_single_file_operations = {
742 .open = proc_single_open,
743 .read = seq_read,
744 .llseek = seq_lseek,
745 .release = single_release,
748 static int mem_open(struct inode* inode, struct file* file)
750 file->private_data = (void*)((long)current->self_exec_id);
751 return 0;
754 static ssize_t mem_read(struct file * file, char __user * buf,
755 size_t count, loff_t *ppos)
757 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
758 char *page;
759 unsigned long src = *ppos;
760 int ret = -ESRCH;
761 struct mm_struct *mm;
763 if (!task)
764 goto out_no_task;
766 if (check_mem_permission(task))
767 goto out;
769 ret = -ENOMEM;
770 page = (char *)__get_free_page(GFP_TEMPORARY);
771 if (!page)
772 goto out;
774 ret = 0;
776 mm = get_task_mm(task);
777 if (!mm)
778 goto out_free;
780 ret = -EIO;
782 if (file->private_data != (void*)((long)current->self_exec_id))
783 goto out_put;
785 ret = 0;
787 while (count > 0) {
788 int this_len, retval;
790 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
791 retval = access_process_vm(task, src, page, this_len, 0);
792 if (!retval || check_mem_permission(task)) {
793 if (!ret)
794 ret = -EIO;
795 break;
798 if (copy_to_user(buf, page, retval)) {
799 ret = -EFAULT;
800 break;
803 ret += retval;
804 src += retval;
805 buf += retval;
806 count -= retval;
808 *ppos = src;
810 out_put:
811 mmput(mm);
812 out_free:
813 free_page((unsigned long) page);
814 out:
815 put_task_struct(task);
816 out_no_task:
817 return ret;
820 #define mem_write NULL
822 #ifndef mem_write
823 /* This is a security hazard */
824 static ssize_t mem_write(struct file * file, const char __user *buf,
825 size_t count, loff_t *ppos)
827 int copied;
828 char *page;
829 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
830 unsigned long dst = *ppos;
832 copied = -ESRCH;
833 if (!task)
834 goto out_no_task;
836 if (check_mem_permission(task))
837 goto out;
839 copied = -ENOMEM;
840 page = (char *)__get_free_page(GFP_TEMPORARY);
841 if (!page)
842 goto out;
844 copied = 0;
845 while (count > 0) {
846 int this_len, retval;
848 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
849 if (copy_from_user(page, buf, this_len)) {
850 copied = -EFAULT;
851 break;
853 retval = access_process_vm(task, dst, page, this_len, 1);
854 if (!retval) {
855 if (!copied)
856 copied = -EIO;
857 break;
859 copied += retval;
860 buf += retval;
861 dst += retval;
862 count -= retval;
864 *ppos = dst;
865 free_page((unsigned long) page);
866 out:
867 put_task_struct(task);
868 out_no_task:
869 return copied;
871 #endif
873 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
875 switch (orig) {
876 case 0:
877 file->f_pos = offset;
878 break;
879 case 1:
880 file->f_pos += offset;
881 break;
882 default:
883 return -EINVAL;
885 force_successful_syscall_return();
886 return file->f_pos;
889 static const struct file_operations proc_mem_operations = {
890 .llseek = mem_lseek,
891 .read = mem_read,
892 .write = mem_write,
893 .open = mem_open,
896 static ssize_t environ_read(struct file *file, char __user *buf,
897 size_t count, loff_t *ppos)
899 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
900 char *page;
901 unsigned long src = *ppos;
902 int ret = -ESRCH;
903 struct mm_struct *mm;
905 if (!task)
906 goto out_no_task;
908 if (!ptrace_may_access(task, PTRACE_MODE_READ))
909 goto out;
911 ret = -ENOMEM;
912 page = (char *)__get_free_page(GFP_TEMPORARY);
913 if (!page)
914 goto out;
916 ret = 0;
918 mm = get_task_mm(task);
919 if (!mm)
920 goto out_free;
922 while (count > 0) {
923 int this_len, retval, max_len;
925 this_len = mm->env_end - (mm->env_start + src);
927 if (this_len <= 0)
928 break;
930 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
931 this_len = (this_len > max_len) ? max_len : this_len;
933 retval = access_process_vm(task, (mm->env_start + src),
934 page, this_len, 0);
936 if (retval <= 0) {
937 ret = retval;
938 break;
941 if (copy_to_user(buf, page, retval)) {
942 ret = -EFAULT;
943 break;
946 ret += retval;
947 src += retval;
948 buf += retval;
949 count -= retval;
951 *ppos = src;
953 mmput(mm);
954 out_free:
955 free_page((unsigned long) page);
956 out:
957 put_task_struct(task);
958 out_no_task:
959 return ret;
962 static const struct file_operations proc_environ_operations = {
963 .read = environ_read,
966 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
967 size_t count, loff_t *ppos)
969 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
970 char buffer[PROC_NUMBUF];
971 size_t len;
972 int oom_adjust;
974 if (!task)
975 return -ESRCH;
976 oom_adjust = task->oomkilladj;
977 put_task_struct(task);
979 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
981 return simple_read_from_buffer(buf, count, ppos, buffer, len);
984 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
985 size_t count, loff_t *ppos)
987 struct task_struct *task;
988 char buffer[PROC_NUMBUF], *end;
989 int oom_adjust;
991 memset(buffer, 0, sizeof(buffer));
992 if (count > sizeof(buffer) - 1)
993 count = sizeof(buffer) - 1;
994 if (copy_from_user(buffer, buf, count))
995 return -EFAULT;
996 oom_adjust = simple_strtol(buffer, &end, 0);
997 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
998 oom_adjust != OOM_DISABLE)
999 return -EINVAL;
1000 if (*end == '\n')
1001 end++;
1002 task = get_proc_task(file->f_path.dentry->d_inode);
1003 if (!task)
1004 return -ESRCH;
1005 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1006 put_task_struct(task);
1007 return -EACCES;
1009 task->oomkilladj = oom_adjust;
1010 put_task_struct(task);
1011 if (end - buffer == 0)
1012 return -EIO;
1013 return end - buffer;
1016 static const struct file_operations proc_oom_adjust_operations = {
1017 .read = oom_adjust_read,
1018 .write = oom_adjust_write,
1021 #ifdef CONFIG_AUDITSYSCALL
1022 #define TMPBUFLEN 21
1023 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1024 size_t count, loff_t *ppos)
1026 struct inode * inode = file->f_path.dentry->d_inode;
1027 struct task_struct *task = get_proc_task(inode);
1028 ssize_t length;
1029 char tmpbuf[TMPBUFLEN];
1031 if (!task)
1032 return -ESRCH;
1033 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1034 audit_get_loginuid(task));
1035 put_task_struct(task);
1036 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1039 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1040 size_t count, loff_t *ppos)
1042 struct inode * inode = file->f_path.dentry->d_inode;
1043 char *page, *tmp;
1044 ssize_t length;
1045 uid_t loginuid;
1047 if (!capable(CAP_AUDIT_CONTROL))
1048 return -EPERM;
1050 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1051 return -EPERM;
1053 if (count >= PAGE_SIZE)
1054 count = PAGE_SIZE - 1;
1056 if (*ppos != 0) {
1057 /* No partial writes. */
1058 return -EINVAL;
1060 page = (char*)__get_free_page(GFP_TEMPORARY);
1061 if (!page)
1062 return -ENOMEM;
1063 length = -EFAULT;
1064 if (copy_from_user(page, buf, count))
1065 goto out_free_page;
1067 page[count] = '\0';
1068 loginuid = simple_strtoul(page, &tmp, 10);
1069 if (tmp == page) {
1070 length = -EINVAL;
1071 goto out_free_page;
1074 length = audit_set_loginuid(current, loginuid);
1075 if (likely(length == 0))
1076 length = count;
1078 out_free_page:
1079 free_page((unsigned long) page);
1080 return length;
1083 static const struct file_operations proc_loginuid_operations = {
1084 .read = proc_loginuid_read,
1085 .write = proc_loginuid_write,
1088 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1089 size_t count, loff_t *ppos)
1091 struct inode * inode = file->f_path.dentry->d_inode;
1092 struct task_struct *task = get_proc_task(inode);
1093 ssize_t length;
1094 char tmpbuf[TMPBUFLEN];
1096 if (!task)
1097 return -ESRCH;
1098 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1099 audit_get_sessionid(task));
1100 put_task_struct(task);
1101 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1104 static const struct file_operations proc_sessionid_operations = {
1105 .read = proc_sessionid_read,
1107 #endif
1109 #ifdef CONFIG_FAULT_INJECTION
1110 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1111 size_t count, loff_t *ppos)
1113 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1114 char buffer[PROC_NUMBUF];
1115 size_t len;
1116 int make_it_fail;
1118 if (!task)
1119 return -ESRCH;
1120 make_it_fail = task->make_it_fail;
1121 put_task_struct(task);
1123 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1125 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1128 static ssize_t proc_fault_inject_write(struct file * file,
1129 const char __user * buf, size_t count, loff_t *ppos)
1131 struct task_struct *task;
1132 char buffer[PROC_NUMBUF], *end;
1133 int make_it_fail;
1135 if (!capable(CAP_SYS_RESOURCE))
1136 return -EPERM;
1137 memset(buffer, 0, sizeof(buffer));
1138 if (count > sizeof(buffer) - 1)
1139 count = sizeof(buffer) - 1;
1140 if (copy_from_user(buffer, buf, count))
1141 return -EFAULT;
1142 make_it_fail = simple_strtol(buffer, &end, 0);
1143 if (*end == '\n')
1144 end++;
1145 task = get_proc_task(file->f_dentry->d_inode);
1146 if (!task)
1147 return -ESRCH;
1148 task->make_it_fail = make_it_fail;
1149 put_task_struct(task);
1150 if (end - buffer == 0)
1151 return -EIO;
1152 return end - buffer;
1155 static const struct file_operations proc_fault_inject_operations = {
1156 .read = proc_fault_inject_read,
1157 .write = proc_fault_inject_write,
1159 #endif
1162 #ifdef CONFIG_SCHED_DEBUG
1164 * Print out various scheduling related per-task fields:
1166 static int sched_show(struct seq_file *m, void *v)
1168 struct inode *inode = m->private;
1169 struct task_struct *p;
1171 WARN_ON(!inode);
1173 p = get_proc_task(inode);
1174 if (!p)
1175 return -ESRCH;
1176 proc_sched_show_task(p, m);
1178 put_task_struct(p);
1180 return 0;
1183 static ssize_t
1184 sched_write(struct file *file, const char __user *buf,
1185 size_t count, loff_t *offset)
1187 struct inode *inode = file->f_path.dentry->d_inode;
1188 struct task_struct *p;
1190 WARN_ON(!inode);
1192 p = get_proc_task(inode);
1193 if (!p)
1194 return -ESRCH;
1195 proc_sched_set_task(p);
1197 put_task_struct(p);
1199 return count;
1202 static int sched_open(struct inode *inode, struct file *filp)
1204 int ret;
1206 ret = single_open(filp, sched_show, NULL);
1207 if (!ret) {
1208 struct seq_file *m = filp->private_data;
1210 m->private = inode;
1212 return ret;
1215 static const struct file_operations proc_pid_sched_operations = {
1216 .open = sched_open,
1217 .read = seq_read,
1218 .write = sched_write,
1219 .llseek = seq_lseek,
1220 .release = single_release,
1223 #endif
1226 * We added or removed a vma mapping the executable. The vmas are only mapped
1227 * during exec and are not mapped with the mmap system call.
1228 * Callers must hold down_write() on the mm's mmap_sem for these
1230 void added_exe_file_vma(struct mm_struct *mm)
1232 mm->num_exe_file_vmas++;
1235 void removed_exe_file_vma(struct mm_struct *mm)
1237 mm->num_exe_file_vmas--;
1238 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1239 fput(mm->exe_file);
1240 mm->exe_file = NULL;
1245 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1247 if (new_exe_file)
1248 get_file(new_exe_file);
1249 if (mm->exe_file)
1250 fput(mm->exe_file);
1251 mm->exe_file = new_exe_file;
1252 mm->num_exe_file_vmas = 0;
1255 struct file *get_mm_exe_file(struct mm_struct *mm)
1257 struct file *exe_file;
1259 /* We need mmap_sem to protect against races with removal of
1260 * VM_EXECUTABLE vmas */
1261 down_read(&mm->mmap_sem);
1262 exe_file = mm->exe_file;
1263 if (exe_file)
1264 get_file(exe_file);
1265 up_read(&mm->mmap_sem);
1266 return exe_file;
1269 void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1271 /* It's safe to write the exe_file pointer without exe_file_lock because
1272 * this is called during fork when the task is not yet in /proc */
1273 newmm->exe_file = get_mm_exe_file(oldmm);
1276 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1278 struct task_struct *task;
1279 struct mm_struct *mm;
1280 struct file *exe_file;
1282 task = get_proc_task(inode);
1283 if (!task)
1284 return -ENOENT;
1285 mm = get_task_mm(task);
1286 put_task_struct(task);
1287 if (!mm)
1288 return -ENOENT;
1289 exe_file = get_mm_exe_file(mm);
1290 mmput(mm);
1291 if (exe_file) {
1292 *exe_path = exe_file->f_path;
1293 path_get(&exe_file->f_path);
1294 fput(exe_file);
1295 return 0;
1296 } else
1297 return -ENOENT;
1300 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1302 struct inode *inode = dentry->d_inode;
1303 int error = -EACCES;
1305 /* We don't need a base pointer in the /proc filesystem */
1306 path_put(&nd->path);
1308 /* Are we allowed to snoop on the tasks file descriptors? */
1309 if (!proc_fd_access_allowed(inode))
1310 goto out;
1312 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1313 nd->last_type = LAST_BIND;
1314 out:
1315 return ERR_PTR(error);
1318 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1320 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1321 char *pathname;
1322 int len;
1324 if (!tmp)
1325 return -ENOMEM;
1327 pathname = d_path(path, tmp, PAGE_SIZE);
1328 len = PTR_ERR(pathname);
1329 if (IS_ERR(pathname))
1330 goto out;
1331 len = tmp + PAGE_SIZE - 1 - pathname;
1333 if (len > buflen)
1334 len = buflen;
1335 if (copy_to_user(buffer, pathname, len))
1336 len = -EFAULT;
1337 out:
1338 free_page((unsigned long)tmp);
1339 return len;
1342 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1344 int error = -EACCES;
1345 struct inode *inode = dentry->d_inode;
1346 struct path path;
1348 /* Are we allowed to snoop on the tasks file descriptors? */
1349 if (!proc_fd_access_allowed(inode))
1350 goto out;
1352 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1353 if (error)
1354 goto out;
1356 error = do_proc_readlink(&path, buffer, buflen);
1357 path_put(&path);
1358 out:
1359 return error;
1362 static const struct inode_operations proc_pid_link_inode_operations = {
1363 .readlink = proc_pid_readlink,
1364 .follow_link = proc_pid_follow_link,
1365 .setattr = proc_setattr,
1369 /* building an inode */
1371 static int task_dumpable(struct task_struct *task)
1373 int dumpable = 0;
1374 struct mm_struct *mm;
1376 task_lock(task);
1377 mm = task->mm;
1378 if (mm)
1379 dumpable = get_dumpable(mm);
1380 task_unlock(task);
1381 if(dumpable == 1)
1382 return 1;
1383 return 0;
1387 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1389 struct inode * inode;
1390 struct proc_inode *ei;
1392 /* We need a new inode */
1394 inode = new_inode(sb);
1395 if (!inode)
1396 goto out;
1398 /* Common stuff */
1399 ei = PROC_I(inode);
1400 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1401 inode->i_op = &proc_def_inode_operations;
1404 * grab the reference to task.
1406 ei->pid = get_task_pid(task, PIDTYPE_PID);
1407 if (!ei->pid)
1408 goto out_unlock;
1410 inode->i_uid = 0;
1411 inode->i_gid = 0;
1412 if (task_dumpable(task)) {
1413 inode->i_uid = task->euid;
1414 inode->i_gid = task->egid;
1416 security_task_to_inode(task, inode);
1418 out:
1419 return inode;
1421 out_unlock:
1422 iput(inode);
1423 return NULL;
1426 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1428 struct inode *inode = dentry->d_inode;
1429 struct task_struct *task;
1430 generic_fillattr(inode, stat);
1432 rcu_read_lock();
1433 stat->uid = 0;
1434 stat->gid = 0;
1435 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1436 if (task) {
1437 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1438 task_dumpable(task)) {
1439 stat->uid = task->euid;
1440 stat->gid = task->egid;
1443 rcu_read_unlock();
1444 return 0;
1447 /* dentry stuff */
1450 * Exceptional case: normally we are not allowed to unhash a busy
1451 * directory. In this case, however, we can do it - no aliasing problems
1452 * due to the way we treat inodes.
1454 * Rewrite the inode's ownerships here because the owning task may have
1455 * performed a setuid(), etc.
1457 * Before the /proc/pid/status file was created the only way to read
1458 * the effective uid of a /process was to stat /proc/pid. Reading
1459 * /proc/pid/status is slow enough that procps and other packages
1460 * kept stating /proc/pid. To keep the rules in /proc simple I have
1461 * made this apply to all per process world readable and executable
1462 * directories.
1464 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1466 struct inode *inode = dentry->d_inode;
1467 struct task_struct *task = get_proc_task(inode);
1468 if (task) {
1469 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1470 task_dumpable(task)) {
1471 inode->i_uid = task->euid;
1472 inode->i_gid = task->egid;
1473 } else {
1474 inode->i_uid = 0;
1475 inode->i_gid = 0;
1477 inode->i_mode &= ~(S_ISUID | S_ISGID);
1478 security_task_to_inode(task, inode);
1479 put_task_struct(task);
1480 return 1;
1482 d_drop(dentry);
1483 return 0;
1486 static int pid_delete_dentry(struct dentry * dentry)
1488 /* Is the task we represent dead?
1489 * If so, then don't put the dentry on the lru list,
1490 * kill it immediately.
1492 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1495 static struct dentry_operations pid_dentry_operations =
1497 .d_revalidate = pid_revalidate,
1498 .d_delete = pid_delete_dentry,
1501 /* Lookups */
1503 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1504 struct task_struct *, const void *);
1507 * Fill a directory entry.
1509 * If possible create the dcache entry and derive our inode number and
1510 * file type from dcache entry.
1512 * Since all of the proc inode numbers are dynamically generated, the inode
1513 * numbers do not exist until the inode is cache. This means creating the
1514 * the dcache entry in readdir is necessary to keep the inode numbers
1515 * reported by readdir in sync with the inode numbers reported
1516 * by stat.
1518 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1519 char *name, int len,
1520 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1522 struct dentry *child, *dir = filp->f_path.dentry;
1523 struct inode *inode;
1524 struct qstr qname;
1525 ino_t ino = 0;
1526 unsigned type = DT_UNKNOWN;
1528 qname.name = name;
1529 qname.len = len;
1530 qname.hash = full_name_hash(name, len);
1532 child = d_lookup(dir, &qname);
1533 if (!child) {
1534 struct dentry *new;
1535 new = d_alloc(dir, &qname);
1536 if (new) {
1537 child = instantiate(dir->d_inode, new, task, ptr);
1538 if (child)
1539 dput(new);
1540 else
1541 child = new;
1544 if (!child || IS_ERR(child) || !child->d_inode)
1545 goto end_instantiate;
1546 inode = child->d_inode;
1547 if (inode) {
1548 ino = inode->i_ino;
1549 type = inode->i_mode >> 12;
1551 dput(child);
1552 end_instantiate:
1553 if (!ino)
1554 ino = find_inode_number(dir, &qname);
1555 if (!ino)
1556 ino = 1;
1557 return filldir(dirent, name, len, filp->f_pos, ino, type);
1560 static unsigned name_to_int(struct dentry *dentry)
1562 const char *name = dentry->d_name.name;
1563 int len = dentry->d_name.len;
1564 unsigned n = 0;
1566 if (len > 1 && *name == '0')
1567 goto out;
1568 while (len-- > 0) {
1569 unsigned c = *name++ - '0';
1570 if (c > 9)
1571 goto out;
1572 if (n >= (~0U-9)/10)
1573 goto out;
1574 n *= 10;
1575 n += c;
1577 return n;
1578 out:
1579 return ~0U;
1582 #define PROC_FDINFO_MAX 64
1584 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1586 struct task_struct *task = get_proc_task(inode);
1587 struct files_struct *files = NULL;
1588 struct file *file;
1589 int fd = proc_fd(inode);
1591 if (task) {
1592 files = get_files_struct(task);
1593 put_task_struct(task);
1595 if (files) {
1597 * We are not taking a ref to the file structure, so we must
1598 * hold ->file_lock.
1600 spin_lock(&files->file_lock);
1601 file = fcheck_files(files, fd);
1602 if (file) {
1603 if (path) {
1604 *path = file->f_path;
1605 path_get(&file->f_path);
1607 if (info)
1608 snprintf(info, PROC_FDINFO_MAX,
1609 "pos:\t%lli\n"
1610 "flags:\t0%o\n",
1611 (long long) file->f_pos,
1612 file->f_flags);
1613 spin_unlock(&files->file_lock);
1614 put_files_struct(files);
1615 return 0;
1617 spin_unlock(&files->file_lock);
1618 put_files_struct(files);
1620 return -ENOENT;
1623 static int proc_fd_link(struct inode *inode, struct path *path)
1625 return proc_fd_info(inode, path, NULL);
1628 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1630 struct inode *inode = dentry->d_inode;
1631 struct task_struct *task = get_proc_task(inode);
1632 int fd = proc_fd(inode);
1633 struct files_struct *files;
1635 if (task) {
1636 files = get_files_struct(task);
1637 if (files) {
1638 rcu_read_lock();
1639 if (fcheck_files(files, fd)) {
1640 rcu_read_unlock();
1641 put_files_struct(files);
1642 if (task_dumpable(task)) {
1643 inode->i_uid = task->euid;
1644 inode->i_gid = task->egid;
1645 } else {
1646 inode->i_uid = 0;
1647 inode->i_gid = 0;
1649 inode->i_mode &= ~(S_ISUID | S_ISGID);
1650 security_task_to_inode(task, inode);
1651 put_task_struct(task);
1652 return 1;
1654 rcu_read_unlock();
1655 put_files_struct(files);
1657 put_task_struct(task);
1659 d_drop(dentry);
1660 return 0;
1663 static struct dentry_operations tid_fd_dentry_operations =
1665 .d_revalidate = tid_fd_revalidate,
1666 .d_delete = pid_delete_dentry,
1669 static struct dentry *proc_fd_instantiate(struct inode *dir,
1670 struct dentry *dentry, struct task_struct *task, const void *ptr)
1672 unsigned fd = *(const unsigned *)ptr;
1673 struct file *file;
1674 struct files_struct *files;
1675 struct inode *inode;
1676 struct proc_inode *ei;
1677 struct dentry *error = ERR_PTR(-ENOENT);
1679 inode = proc_pid_make_inode(dir->i_sb, task);
1680 if (!inode)
1681 goto out;
1682 ei = PROC_I(inode);
1683 ei->fd = fd;
1684 files = get_files_struct(task);
1685 if (!files)
1686 goto out_iput;
1687 inode->i_mode = S_IFLNK;
1690 * We are not taking a ref to the file structure, so we must
1691 * hold ->file_lock.
1693 spin_lock(&files->file_lock);
1694 file = fcheck_files(files, fd);
1695 if (!file)
1696 goto out_unlock;
1697 if (file->f_mode & FMODE_READ)
1698 inode->i_mode |= S_IRUSR | S_IXUSR;
1699 if (file->f_mode & FMODE_WRITE)
1700 inode->i_mode |= S_IWUSR | S_IXUSR;
1701 spin_unlock(&files->file_lock);
1702 put_files_struct(files);
1704 inode->i_op = &proc_pid_link_inode_operations;
1705 inode->i_size = 64;
1706 ei->op.proc_get_link = proc_fd_link;
1707 dentry->d_op = &tid_fd_dentry_operations;
1708 d_add(dentry, inode);
1709 /* Close the race of the process dying before we return the dentry */
1710 if (tid_fd_revalidate(dentry, NULL))
1711 error = NULL;
1713 out:
1714 return error;
1715 out_unlock:
1716 spin_unlock(&files->file_lock);
1717 put_files_struct(files);
1718 out_iput:
1719 iput(inode);
1720 goto out;
1723 static struct dentry *proc_lookupfd_common(struct inode *dir,
1724 struct dentry *dentry,
1725 instantiate_t instantiate)
1727 struct task_struct *task = get_proc_task(dir);
1728 unsigned fd = name_to_int(dentry);
1729 struct dentry *result = ERR_PTR(-ENOENT);
1731 if (!task)
1732 goto out_no_task;
1733 if (fd == ~0U)
1734 goto out;
1736 result = instantiate(dir, dentry, task, &fd);
1737 out:
1738 put_task_struct(task);
1739 out_no_task:
1740 return result;
1743 static int proc_readfd_common(struct file * filp, void * dirent,
1744 filldir_t filldir, instantiate_t instantiate)
1746 struct dentry *dentry = filp->f_path.dentry;
1747 struct inode *inode = dentry->d_inode;
1748 struct task_struct *p = get_proc_task(inode);
1749 unsigned int fd, ino;
1750 int retval;
1751 struct files_struct * files;
1753 retval = -ENOENT;
1754 if (!p)
1755 goto out_no_task;
1756 retval = 0;
1758 fd = filp->f_pos;
1759 switch (fd) {
1760 case 0:
1761 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1762 goto out;
1763 filp->f_pos++;
1764 case 1:
1765 ino = parent_ino(dentry);
1766 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1767 goto out;
1768 filp->f_pos++;
1769 default:
1770 files = get_files_struct(p);
1771 if (!files)
1772 goto out;
1773 rcu_read_lock();
1774 for (fd = filp->f_pos-2;
1775 fd < files_fdtable(files)->max_fds;
1776 fd++, filp->f_pos++) {
1777 char name[PROC_NUMBUF];
1778 int len;
1780 if (!fcheck_files(files, fd))
1781 continue;
1782 rcu_read_unlock();
1784 len = snprintf(name, sizeof(name), "%d", fd);
1785 if (proc_fill_cache(filp, dirent, filldir,
1786 name, len, instantiate,
1787 p, &fd) < 0) {
1788 rcu_read_lock();
1789 break;
1791 rcu_read_lock();
1793 rcu_read_unlock();
1794 put_files_struct(files);
1796 out:
1797 put_task_struct(p);
1798 out_no_task:
1799 return retval;
1802 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1803 struct nameidata *nd)
1805 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1808 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1810 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1813 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1814 size_t len, loff_t *ppos)
1816 char tmp[PROC_FDINFO_MAX];
1817 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1818 if (!err)
1819 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1820 return err;
1823 static const struct file_operations proc_fdinfo_file_operations = {
1824 .open = nonseekable_open,
1825 .read = proc_fdinfo_read,
1828 static const struct file_operations proc_fd_operations = {
1829 .read = generic_read_dir,
1830 .readdir = proc_readfd,
1834 * /proc/pid/fd needs a special permission handler so that a process can still
1835 * access /proc/self/fd after it has executed a setuid().
1837 static int proc_fd_permission(struct inode *inode, int mask)
1839 int rv;
1841 rv = generic_permission(inode, mask, NULL);
1842 if (rv == 0)
1843 return 0;
1844 if (task_pid(current) == proc_pid(inode))
1845 rv = 0;
1846 return rv;
1850 * proc directories can do almost nothing..
1852 static const struct inode_operations proc_fd_inode_operations = {
1853 .lookup = proc_lookupfd,
1854 .permission = proc_fd_permission,
1855 .setattr = proc_setattr,
1858 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1859 struct dentry *dentry, struct task_struct *task, const void *ptr)
1861 unsigned fd = *(unsigned *)ptr;
1862 struct inode *inode;
1863 struct proc_inode *ei;
1864 struct dentry *error = ERR_PTR(-ENOENT);
1866 inode = proc_pid_make_inode(dir->i_sb, task);
1867 if (!inode)
1868 goto out;
1869 ei = PROC_I(inode);
1870 ei->fd = fd;
1871 inode->i_mode = S_IFREG | S_IRUSR;
1872 inode->i_fop = &proc_fdinfo_file_operations;
1873 dentry->d_op = &tid_fd_dentry_operations;
1874 d_add(dentry, inode);
1875 /* Close the race of the process dying before we return the dentry */
1876 if (tid_fd_revalidate(dentry, NULL))
1877 error = NULL;
1879 out:
1880 return error;
1883 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1884 struct dentry *dentry,
1885 struct nameidata *nd)
1887 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1890 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1892 return proc_readfd_common(filp, dirent, filldir,
1893 proc_fdinfo_instantiate);
1896 static const struct file_operations proc_fdinfo_operations = {
1897 .read = generic_read_dir,
1898 .readdir = proc_readfdinfo,
1902 * proc directories can do almost nothing..
1904 static const struct inode_operations proc_fdinfo_inode_operations = {
1905 .lookup = proc_lookupfdinfo,
1906 .setattr = proc_setattr,
1910 static struct dentry *proc_pident_instantiate(struct inode *dir,
1911 struct dentry *dentry, struct task_struct *task, const void *ptr)
1913 const struct pid_entry *p = ptr;
1914 struct inode *inode;
1915 struct proc_inode *ei;
1916 struct dentry *error = ERR_PTR(-EINVAL);
1918 inode = proc_pid_make_inode(dir->i_sb, task);
1919 if (!inode)
1920 goto out;
1922 ei = PROC_I(inode);
1923 inode->i_mode = p->mode;
1924 if (S_ISDIR(inode->i_mode))
1925 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1926 if (p->iop)
1927 inode->i_op = p->iop;
1928 if (p->fop)
1929 inode->i_fop = p->fop;
1930 ei->op = p->op;
1931 dentry->d_op = &pid_dentry_operations;
1932 d_add(dentry, inode);
1933 /* Close the race of the process dying before we return the dentry */
1934 if (pid_revalidate(dentry, NULL))
1935 error = NULL;
1936 out:
1937 return error;
1940 static struct dentry *proc_pident_lookup(struct inode *dir,
1941 struct dentry *dentry,
1942 const struct pid_entry *ents,
1943 unsigned int nents)
1945 struct inode *inode;
1946 struct dentry *error;
1947 struct task_struct *task = get_proc_task(dir);
1948 const struct pid_entry *p, *last;
1950 error = ERR_PTR(-ENOENT);
1951 inode = NULL;
1953 if (!task)
1954 goto out_no_task;
1957 * Yes, it does not scale. And it should not. Don't add
1958 * new entries into /proc/<tgid>/ without very good reasons.
1960 last = &ents[nents - 1];
1961 for (p = ents; p <= last; p++) {
1962 if (p->len != dentry->d_name.len)
1963 continue;
1964 if (!memcmp(dentry->d_name.name, p->name, p->len))
1965 break;
1967 if (p > last)
1968 goto out;
1970 error = proc_pident_instantiate(dir, dentry, task, p);
1971 out:
1972 put_task_struct(task);
1973 out_no_task:
1974 return error;
1977 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1978 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1980 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1981 proc_pident_instantiate, task, p);
1984 static int proc_pident_readdir(struct file *filp,
1985 void *dirent, filldir_t filldir,
1986 const struct pid_entry *ents, unsigned int nents)
1988 int i;
1989 struct dentry *dentry = filp->f_path.dentry;
1990 struct inode *inode = dentry->d_inode;
1991 struct task_struct *task = get_proc_task(inode);
1992 const struct pid_entry *p, *last;
1993 ino_t ino;
1994 int ret;
1996 ret = -ENOENT;
1997 if (!task)
1998 goto out_no_task;
2000 ret = 0;
2001 i = filp->f_pos;
2002 switch (i) {
2003 case 0:
2004 ino = inode->i_ino;
2005 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2006 goto out;
2007 i++;
2008 filp->f_pos++;
2009 /* fall through */
2010 case 1:
2011 ino = parent_ino(dentry);
2012 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2013 goto out;
2014 i++;
2015 filp->f_pos++;
2016 /* fall through */
2017 default:
2018 i -= 2;
2019 if (i >= nents) {
2020 ret = 1;
2021 goto out;
2023 p = ents + i;
2024 last = &ents[nents - 1];
2025 while (p <= last) {
2026 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2027 goto out;
2028 filp->f_pos++;
2029 p++;
2033 ret = 1;
2034 out:
2035 put_task_struct(task);
2036 out_no_task:
2037 return ret;
2040 #ifdef CONFIG_SECURITY
2041 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2042 size_t count, loff_t *ppos)
2044 struct inode * inode = file->f_path.dentry->d_inode;
2045 char *p = NULL;
2046 ssize_t length;
2047 struct task_struct *task = get_proc_task(inode);
2049 if (!task)
2050 return -ESRCH;
2052 length = security_getprocattr(task,
2053 (char*)file->f_path.dentry->d_name.name,
2054 &p);
2055 put_task_struct(task);
2056 if (length > 0)
2057 length = simple_read_from_buffer(buf, count, ppos, p, length);
2058 kfree(p);
2059 return length;
2062 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2063 size_t count, loff_t *ppos)
2065 struct inode * inode = file->f_path.dentry->d_inode;
2066 char *page;
2067 ssize_t length;
2068 struct task_struct *task = get_proc_task(inode);
2070 length = -ESRCH;
2071 if (!task)
2072 goto out_no_task;
2073 if (count > PAGE_SIZE)
2074 count = PAGE_SIZE;
2076 /* No partial writes. */
2077 length = -EINVAL;
2078 if (*ppos != 0)
2079 goto out;
2081 length = -ENOMEM;
2082 page = (char*)__get_free_page(GFP_TEMPORARY);
2083 if (!page)
2084 goto out;
2086 length = -EFAULT;
2087 if (copy_from_user(page, buf, count))
2088 goto out_free;
2090 length = security_setprocattr(task,
2091 (char*)file->f_path.dentry->d_name.name,
2092 (void*)page, count);
2093 out_free:
2094 free_page((unsigned long) page);
2095 out:
2096 put_task_struct(task);
2097 out_no_task:
2098 return length;
2101 static const struct file_operations proc_pid_attr_operations = {
2102 .read = proc_pid_attr_read,
2103 .write = proc_pid_attr_write,
2106 static const struct pid_entry attr_dir_stuff[] = {
2107 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2108 REG("prev", S_IRUGO, pid_attr),
2109 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2110 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2111 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2112 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
2115 static int proc_attr_dir_readdir(struct file * filp,
2116 void * dirent, filldir_t filldir)
2118 return proc_pident_readdir(filp,dirent,filldir,
2119 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2122 static const struct file_operations proc_attr_dir_operations = {
2123 .read = generic_read_dir,
2124 .readdir = proc_attr_dir_readdir,
2127 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2128 struct dentry *dentry, struct nameidata *nd)
2130 return proc_pident_lookup(dir, dentry,
2131 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2134 static const struct inode_operations proc_attr_dir_inode_operations = {
2135 .lookup = proc_attr_dir_lookup,
2136 .getattr = pid_getattr,
2137 .setattr = proc_setattr,
2140 #endif
2142 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2143 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2144 size_t count, loff_t *ppos)
2146 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2147 struct mm_struct *mm;
2148 char buffer[PROC_NUMBUF];
2149 size_t len;
2150 int ret;
2152 if (!task)
2153 return -ESRCH;
2155 ret = 0;
2156 mm = get_task_mm(task);
2157 if (mm) {
2158 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2159 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2160 MMF_DUMP_FILTER_SHIFT));
2161 mmput(mm);
2162 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2165 put_task_struct(task);
2167 return ret;
2170 static ssize_t proc_coredump_filter_write(struct file *file,
2171 const char __user *buf,
2172 size_t count,
2173 loff_t *ppos)
2175 struct task_struct *task;
2176 struct mm_struct *mm;
2177 char buffer[PROC_NUMBUF], *end;
2178 unsigned int val;
2179 int ret;
2180 int i;
2181 unsigned long mask;
2183 ret = -EFAULT;
2184 memset(buffer, 0, sizeof(buffer));
2185 if (count > sizeof(buffer) - 1)
2186 count = sizeof(buffer) - 1;
2187 if (copy_from_user(buffer, buf, count))
2188 goto out_no_task;
2190 ret = -EINVAL;
2191 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2192 if (*end == '\n')
2193 end++;
2194 if (end - buffer == 0)
2195 goto out_no_task;
2197 ret = -ESRCH;
2198 task = get_proc_task(file->f_dentry->d_inode);
2199 if (!task)
2200 goto out_no_task;
2202 ret = end - buffer;
2203 mm = get_task_mm(task);
2204 if (!mm)
2205 goto out_no_mm;
2207 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2208 if (val & mask)
2209 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2210 else
2211 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2214 mmput(mm);
2215 out_no_mm:
2216 put_task_struct(task);
2217 out_no_task:
2218 return ret;
2221 static const struct file_operations proc_coredump_filter_operations = {
2222 .read = proc_coredump_filter_read,
2223 .write = proc_coredump_filter_write,
2225 #endif
2228 * /proc/self:
2230 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2231 int buflen)
2233 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2234 pid_t tgid = task_tgid_nr_ns(current, ns);
2235 char tmp[PROC_NUMBUF];
2236 if (!tgid)
2237 return -ENOENT;
2238 sprintf(tmp, "%d", tgid);
2239 return vfs_readlink(dentry,buffer,buflen,tmp);
2242 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2244 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2245 pid_t tgid = task_tgid_nr_ns(current, ns);
2246 char tmp[PROC_NUMBUF];
2247 if (!tgid)
2248 return ERR_PTR(-ENOENT);
2249 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2250 return ERR_PTR(vfs_follow_link(nd,tmp));
2253 static const struct inode_operations proc_self_inode_operations = {
2254 .readlink = proc_self_readlink,
2255 .follow_link = proc_self_follow_link,
2259 * proc base
2261 * These are the directory entries in the root directory of /proc
2262 * that properly belong to the /proc filesystem, as they describe
2263 * describe something that is process related.
2265 static const struct pid_entry proc_base_stuff[] = {
2266 NOD("self", S_IFLNK|S_IRWXUGO,
2267 &proc_self_inode_operations, NULL, {}),
2271 * Exceptional case: normally we are not allowed to unhash a busy
2272 * directory. In this case, however, we can do it - no aliasing problems
2273 * due to the way we treat inodes.
2275 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2277 struct inode *inode = dentry->d_inode;
2278 struct task_struct *task = get_proc_task(inode);
2279 if (task) {
2280 put_task_struct(task);
2281 return 1;
2283 d_drop(dentry);
2284 return 0;
2287 static struct dentry_operations proc_base_dentry_operations =
2289 .d_revalidate = proc_base_revalidate,
2290 .d_delete = pid_delete_dentry,
2293 static struct dentry *proc_base_instantiate(struct inode *dir,
2294 struct dentry *dentry, struct task_struct *task, const void *ptr)
2296 const struct pid_entry *p = ptr;
2297 struct inode *inode;
2298 struct proc_inode *ei;
2299 struct dentry *error = ERR_PTR(-EINVAL);
2301 /* Allocate the inode */
2302 error = ERR_PTR(-ENOMEM);
2303 inode = new_inode(dir->i_sb);
2304 if (!inode)
2305 goto out;
2307 /* Initialize the inode */
2308 ei = PROC_I(inode);
2309 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2312 * grab the reference to the task.
2314 ei->pid = get_task_pid(task, PIDTYPE_PID);
2315 if (!ei->pid)
2316 goto out_iput;
2318 inode->i_uid = 0;
2319 inode->i_gid = 0;
2320 inode->i_mode = p->mode;
2321 if (S_ISDIR(inode->i_mode))
2322 inode->i_nlink = 2;
2323 if (S_ISLNK(inode->i_mode))
2324 inode->i_size = 64;
2325 if (p->iop)
2326 inode->i_op = p->iop;
2327 if (p->fop)
2328 inode->i_fop = p->fop;
2329 ei->op = p->op;
2330 dentry->d_op = &proc_base_dentry_operations;
2331 d_add(dentry, inode);
2332 error = NULL;
2333 out:
2334 return error;
2335 out_iput:
2336 iput(inode);
2337 goto out;
2340 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2342 struct dentry *error;
2343 struct task_struct *task = get_proc_task(dir);
2344 const struct pid_entry *p, *last;
2346 error = ERR_PTR(-ENOENT);
2348 if (!task)
2349 goto out_no_task;
2351 /* Lookup the directory entry */
2352 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2353 for (p = proc_base_stuff; p <= last; p++) {
2354 if (p->len != dentry->d_name.len)
2355 continue;
2356 if (!memcmp(dentry->d_name.name, p->name, p->len))
2357 break;
2359 if (p > last)
2360 goto out;
2362 error = proc_base_instantiate(dir, dentry, task, p);
2364 out:
2365 put_task_struct(task);
2366 out_no_task:
2367 return error;
2370 static int proc_base_fill_cache(struct file *filp, void *dirent,
2371 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2373 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2374 proc_base_instantiate, task, p);
2377 #ifdef CONFIG_TASK_IO_ACCOUNTING
2378 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2380 struct task_io_accounting acct = task->ioac;
2381 unsigned long flags;
2383 if (whole && lock_task_sighand(task, &flags)) {
2384 struct task_struct *t = task;
2386 task_io_accounting_add(&acct, &task->signal->ioac);
2387 while_each_thread(task, t)
2388 task_io_accounting_add(&acct, &t->ioac);
2390 unlock_task_sighand(task, &flags);
2392 return sprintf(buffer,
2393 "rchar: %llu\n"
2394 "wchar: %llu\n"
2395 "syscr: %llu\n"
2396 "syscw: %llu\n"
2397 "read_bytes: %llu\n"
2398 "write_bytes: %llu\n"
2399 "cancelled_write_bytes: %llu\n",
2400 (unsigned long long)acct.rchar,
2401 (unsigned long long)acct.wchar,
2402 (unsigned long long)acct.syscr,
2403 (unsigned long long)acct.syscw,
2404 (unsigned long long)acct.read_bytes,
2405 (unsigned long long)acct.write_bytes,
2406 (unsigned long long)acct.cancelled_write_bytes);
2409 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2411 return do_io_accounting(task, buffer, 0);
2414 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2416 return do_io_accounting(task, buffer, 1);
2418 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2420 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2421 struct pid *pid, struct task_struct *task)
2423 seq_printf(m, "%08x\n", task->personality);
2424 return 0;
2428 * Thread groups
2430 static const struct file_operations proc_task_operations;
2431 static const struct inode_operations proc_task_inode_operations;
2433 static const struct pid_entry tgid_base_stuff[] = {
2434 DIR("task", S_IRUGO|S_IXUGO, task),
2435 DIR("fd", S_IRUSR|S_IXUSR, fd),
2436 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2437 #ifdef CONFIG_NET
2438 DIR("net", S_IRUGO|S_IXUGO, net),
2439 #endif
2440 REG("environ", S_IRUSR, environ),
2441 INF("auxv", S_IRUSR, pid_auxv),
2442 ONE("status", S_IRUGO, pid_status),
2443 ONE("personality", S_IRUSR, pid_personality),
2444 INF("limits", S_IRUSR, pid_limits),
2445 #ifdef CONFIG_SCHED_DEBUG
2446 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2447 #endif
2448 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2449 INF("syscall", S_IRUSR, pid_syscall),
2450 #endif
2451 INF("cmdline", S_IRUGO, pid_cmdline),
2452 ONE("stat", S_IRUGO, tgid_stat),
2453 ONE("statm", S_IRUGO, pid_statm),
2454 REG("maps", S_IRUGO, maps),
2455 #ifdef CONFIG_NUMA
2456 REG("numa_maps", S_IRUGO, numa_maps),
2457 #endif
2458 REG("mem", S_IRUSR|S_IWUSR, mem),
2459 LNK("cwd", cwd),
2460 LNK("root", root),
2461 LNK("exe", exe),
2462 REG("mounts", S_IRUGO, mounts),
2463 REG("mountinfo", S_IRUGO, mountinfo),
2464 REG("mountstats", S_IRUSR, mountstats),
2465 #ifdef CONFIG_PROC_PAGE_MONITOR
2466 REG("clear_refs", S_IWUSR, clear_refs),
2467 REG("smaps", S_IRUGO, smaps),
2468 REG("pagemap", S_IRUSR, pagemap),
2469 #endif
2470 #ifdef CONFIG_SECURITY
2471 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2472 #endif
2473 #ifdef CONFIG_KALLSYMS
2474 INF("wchan", S_IRUGO, pid_wchan),
2475 #endif
2476 #ifdef CONFIG_SCHEDSTATS
2477 INF("schedstat", S_IRUGO, pid_schedstat),
2478 #endif
2479 #ifdef CONFIG_LATENCYTOP
2480 REG("latency", S_IRUGO, lstats),
2481 #endif
2482 #ifdef CONFIG_PROC_PID_CPUSET
2483 REG("cpuset", S_IRUGO, cpuset),
2484 #endif
2485 #ifdef CONFIG_CGROUPS
2486 REG("cgroup", S_IRUGO, cgroup),
2487 #endif
2488 INF("oom_score", S_IRUGO, oom_score),
2489 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2490 #ifdef CONFIG_AUDITSYSCALL
2491 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2492 REG("sessionid", S_IRUGO, sessionid),
2493 #endif
2494 #ifdef CONFIG_FAULT_INJECTION
2495 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2496 #endif
2497 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2498 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2499 #endif
2500 #ifdef CONFIG_TASK_IO_ACCOUNTING
2501 INF("io", S_IRUGO, tgid_io_accounting),
2502 #endif
2505 static int proc_tgid_base_readdir(struct file * filp,
2506 void * dirent, filldir_t filldir)
2508 return proc_pident_readdir(filp,dirent,filldir,
2509 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2512 static const struct file_operations proc_tgid_base_operations = {
2513 .read = generic_read_dir,
2514 .readdir = proc_tgid_base_readdir,
2517 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2518 return proc_pident_lookup(dir, dentry,
2519 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2522 static const struct inode_operations proc_tgid_base_inode_operations = {
2523 .lookup = proc_tgid_base_lookup,
2524 .getattr = pid_getattr,
2525 .setattr = proc_setattr,
2528 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2530 struct dentry *dentry, *leader, *dir;
2531 char buf[PROC_NUMBUF];
2532 struct qstr name;
2534 name.name = buf;
2535 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2536 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2537 if (dentry) {
2538 if (!(current->flags & PF_EXITING))
2539 shrink_dcache_parent(dentry);
2540 d_drop(dentry);
2541 dput(dentry);
2544 if (tgid == 0)
2545 goto out;
2547 name.name = buf;
2548 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2549 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2550 if (!leader)
2551 goto out;
2553 name.name = "task";
2554 name.len = strlen(name.name);
2555 dir = d_hash_and_lookup(leader, &name);
2556 if (!dir)
2557 goto out_put_leader;
2559 name.name = buf;
2560 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2561 dentry = d_hash_and_lookup(dir, &name);
2562 if (dentry) {
2563 shrink_dcache_parent(dentry);
2564 d_drop(dentry);
2565 dput(dentry);
2568 dput(dir);
2569 out_put_leader:
2570 dput(leader);
2571 out:
2572 return;
2576 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2577 * @task: task that should be flushed.
2579 * When flushing dentries from proc, one needs to flush them from global
2580 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2581 * in. This call is supposed to do all of this job.
2583 * Looks in the dcache for
2584 * /proc/@pid
2585 * /proc/@tgid/task/@pid
2586 * if either directory is present flushes it and all of it'ts children
2587 * from the dcache.
2589 * It is safe and reasonable to cache /proc entries for a task until
2590 * that task exits. After that they just clog up the dcache with
2591 * useless entries, possibly causing useful dcache entries to be
2592 * flushed instead. This routine is proved to flush those useless
2593 * dcache entries at process exit time.
2595 * NOTE: This routine is just an optimization so it does not guarantee
2596 * that no dcache entries will exist at process exit time it
2597 * just makes it very unlikely that any will persist.
2600 void proc_flush_task(struct task_struct *task)
2602 int i;
2603 struct pid *pid, *tgid = NULL;
2604 struct upid *upid;
2606 pid = task_pid(task);
2607 if (thread_group_leader(task))
2608 tgid = task_tgid(task);
2610 for (i = 0; i <= pid->level; i++) {
2611 upid = &pid->numbers[i];
2612 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2613 tgid ? tgid->numbers[i].nr : 0);
2616 upid = &pid->numbers[pid->level];
2617 if (upid->nr == 1)
2618 pid_ns_release_proc(upid->ns);
2621 static struct dentry *proc_pid_instantiate(struct inode *dir,
2622 struct dentry * dentry,
2623 struct task_struct *task, const void *ptr)
2625 struct dentry *error = ERR_PTR(-ENOENT);
2626 struct inode *inode;
2628 inode = proc_pid_make_inode(dir->i_sb, task);
2629 if (!inode)
2630 goto out;
2632 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2633 inode->i_op = &proc_tgid_base_inode_operations;
2634 inode->i_fop = &proc_tgid_base_operations;
2635 inode->i_flags|=S_IMMUTABLE;
2637 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2638 ARRAY_SIZE(tgid_base_stuff));
2640 dentry->d_op = &pid_dentry_operations;
2642 d_add(dentry, inode);
2643 /* Close the race of the process dying before we return the dentry */
2644 if (pid_revalidate(dentry, NULL))
2645 error = NULL;
2646 out:
2647 return error;
2650 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2652 struct dentry *result = ERR_PTR(-ENOENT);
2653 struct task_struct *task;
2654 unsigned tgid;
2655 struct pid_namespace *ns;
2657 result = proc_base_lookup(dir, dentry);
2658 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2659 goto out;
2661 tgid = name_to_int(dentry);
2662 if (tgid == ~0U)
2663 goto out;
2665 ns = dentry->d_sb->s_fs_info;
2666 rcu_read_lock();
2667 task = find_task_by_pid_ns(tgid, ns);
2668 if (task)
2669 get_task_struct(task);
2670 rcu_read_unlock();
2671 if (!task)
2672 goto out;
2674 result = proc_pid_instantiate(dir, dentry, task, NULL);
2675 put_task_struct(task);
2676 out:
2677 return result;
2681 * Find the first task with tgid >= tgid
2684 struct tgid_iter {
2685 unsigned int tgid;
2686 struct task_struct *task;
2688 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2690 struct pid *pid;
2692 if (iter.task)
2693 put_task_struct(iter.task);
2694 rcu_read_lock();
2695 retry:
2696 iter.task = NULL;
2697 pid = find_ge_pid(iter.tgid, ns);
2698 if (pid) {
2699 iter.tgid = pid_nr_ns(pid, ns);
2700 iter.task = pid_task(pid, PIDTYPE_PID);
2701 /* What we to know is if the pid we have find is the
2702 * pid of a thread_group_leader. Testing for task
2703 * being a thread_group_leader is the obvious thing
2704 * todo but there is a window when it fails, due to
2705 * the pid transfer logic in de_thread.
2707 * So we perform the straight forward test of seeing
2708 * if the pid we have found is the pid of a thread
2709 * group leader, and don't worry if the task we have
2710 * found doesn't happen to be a thread group leader.
2711 * As we don't care in the case of readdir.
2713 if (!iter.task || !has_group_leader_pid(iter.task)) {
2714 iter.tgid += 1;
2715 goto retry;
2717 get_task_struct(iter.task);
2719 rcu_read_unlock();
2720 return iter;
2723 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2725 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2726 struct tgid_iter iter)
2728 char name[PROC_NUMBUF];
2729 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2730 return proc_fill_cache(filp, dirent, filldir, name, len,
2731 proc_pid_instantiate, iter.task, NULL);
2734 /* for the /proc/ directory itself, after non-process stuff has been done */
2735 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2737 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2738 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2739 struct tgid_iter iter;
2740 struct pid_namespace *ns;
2742 if (!reaper)
2743 goto out_no_task;
2745 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2746 const struct pid_entry *p = &proc_base_stuff[nr];
2747 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2748 goto out;
2751 ns = filp->f_dentry->d_sb->s_fs_info;
2752 iter.task = NULL;
2753 iter.tgid = filp->f_pos - TGID_OFFSET;
2754 for (iter = next_tgid(ns, iter);
2755 iter.task;
2756 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2757 filp->f_pos = iter.tgid + TGID_OFFSET;
2758 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2759 put_task_struct(iter.task);
2760 goto out;
2763 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2764 out:
2765 put_task_struct(reaper);
2766 out_no_task:
2767 return 0;
2771 * Tasks
2773 static const struct pid_entry tid_base_stuff[] = {
2774 DIR("fd", S_IRUSR|S_IXUSR, fd),
2775 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2776 REG("environ", S_IRUSR, environ),
2777 INF("auxv", S_IRUSR, pid_auxv),
2778 ONE("status", S_IRUGO, pid_status),
2779 ONE("personality", S_IRUSR, pid_personality),
2780 INF("limits", S_IRUSR, pid_limits),
2781 #ifdef CONFIG_SCHED_DEBUG
2782 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2783 #endif
2784 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2785 INF("syscall", S_IRUSR, pid_syscall),
2786 #endif
2787 INF("cmdline", S_IRUGO, pid_cmdline),
2788 ONE("stat", S_IRUGO, tid_stat),
2789 ONE("statm", S_IRUGO, pid_statm),
2790 REG("maps", S_IRUGO, maps),
2791 #ifdef CONFIG_NUMA
2792 REG("numa_maps", S_IRUGO, numa_maps),
2793 #endif
2794 REG("mem", S_IRUSR|S_IWUSR, mem),
2795 LNK("cwd", cwd),
2796 LNK("root", root),
2797 LNK("exe", exe),
2798 REG("mounts", S_IRUGO, mounts),
2799 REG("mountinfo", S_IRUGO, mountinfo),
2800 #ifdef CONFIG_PROC_PAGE_MONITOR
2801 REG("clear_refs", S_IWUSR, clear_refs),
2802 REG("smaps", S_IRUGO, smaps),
2803 REG("pagemap", S_IRUSR, pagemap),
2804 #endif
2805 #ifdef CONFIG_SECURITY
2806 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2807 #endif
2808 #ifdef CONFIG_KALLSYMS
2809 INF("wchan", S_IRUGO, pid_wchan),
2810 #endif
2811 #ifdef CONFIG_SCHEDSTATS
2812 INF("schedstat", S_IRUGO, pid_schedstat),
2813 #endif
2814 #ifdef CONFIG_LATENCYTOP
2815 REG("latency", S_IRUGO, lstats),
2816 #endif
2817 #ifdef CONFIG_PROC_PID_CPUSET
2818 REG("cpuset", S_IRUGO, cpuset),
2819 #endif
2820 #ifdef CONFIG_CGROUPS
2821 REG("cgroup", S_IRUGO, cgroup),
2822 #endif
2823 INF("oom_score", S_IRUGO, oom_score),
2824 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2825 #ifdef CONFIG_AUDITSYSCALL
2826 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2827 REG("sessionid", S_IRUSR, sessionid),
2828 #endif
2829 #ifdef CONFIG_FAULT_INJECTION
2830 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2831 #endif
2832 #ifdef CONFIG_TASK_IO_ACCOUNTING
2833 INF("io", S_IRUGO, tid_io_accounting),
2834 #endif
2837 static int proc_tid_base_readdir(struct file * filp,
2838 void * dirent, filldir_t filldir)
2840 return proc_pident_readdir(filp,dirent,filldir,
2841 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2844 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2845 return proc_pident_lookup(dir, dentry,
2846 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2849 static const struct file_operations proc_tid_base_operations = {
2850 .read = generic_read_dir,
2851 .readdir = proc_tid_base_readdir,
2854 static const struct inode_operations proc_tid_base_inode_operations = {
2855 .lookup = proc_tid_base_lookup,
2856 .getattr = pid_getattr,
2857 .setattr = proc_setattr,
2860 static struct dentry *proc_task_instantiate(struct inode *dir,
2861 struct dentry *dentry, struct task_struct *task, const void *ptr)
2863 struct dentry *error = ERR_PTR(-ENOENT);
2864 struct inode *inode;
2865 inode = proc_pid_make_inode(dir->i_sb, task);
2867 if (!inode)
2868 goto out;
2869 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2870 inode->i_op = &proc_tid_base_inode_operations;
2871 inode->i_fop = &proc_tid_base_operations;
2872 inode->i_flags|=S_IMMUTABLE;
2874 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2875 ARRAY_SIZE(tid_base_stuff));
2877 dentry->d_op = &pid_dentry_operations;
2879 d_add(dentry, inode);
2880 /* Close the race of the process dying before we return the dentry */
2881 if (pid_revalidate(dentry, NULL))
2882 error = NULL;
2883 out:
2884 return error;
2887 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2889 struct dentry *result = ERR_PTR(-ENOENT);
2890 struct task_struct *task;
2891 struct task_struct *leader = get_proc_task(dir);
2892 unsigned tid;
2893 struct pid_namespace *ns;
2895 if (!leader)
2896 goto out_no_task;
2898 tid = name_to_int(dentry);
2899 if (tid == ~0U)
2900 goto out;
2902 ns = dentry->d_sb->s_fs_info;
2903 rcu_read_lock();
2904 task = find_task_by_pid_ns(tid, ns);
2905 if (task)
2906 get_task_struct(task);
2907 rcu_read_unlock();
2908 if (!task)
2909 goto out;
2910 if (!same_thread_group(leader, task))
2911 goto out_drop_task;
2913 result = proc_task_instantiate(dir, dentry, task, NULL);
2914 out_drop_task:
2915 put_task_struct(task);
2916 out:
2917 put_task_struct(leader);
2918 out_no_task:
2919 return result;
2923 * Find the first tid of a thread group to return to user space.
2925 * Usually this is just the thread group leader, but if the users
2926 * buffer was too small or there was a seek into the middle of the
2927 * directory we have more work todo.
2929 * In the case of a short read we start with find_task_by_pid.
2931 * In the case of a seek we start with the leader and walk nr
2932 * threads past it.
2934 static struct task_struct *first_tid(struct task_struct *leader,
2935 int tid, int nr, struct pid_namespace *ns)
2937 struct task_struct *pos;
2939 rcu_read_lock();
2940 /* Attempt to start with the pid of a thread */
2941 if (tid && (nr > 0)) {
2942 pos = find_task_by_pid_ns(tid, ns);
2943 if (pos && (pos->group_leader == leader))
2944 goto found;
2947 /* If nr exceeds the number of threads there is nothing todo */
2948 pos = NULL;
2949 if (nr && nr >= get_nr_threads(leader))
2950 goto out;
2952 /* If we haven't found our starting place yet start
2953 * with the leader and walk nr threads forward.
2955 for (pos = leader; nr > 0; --nr) {
2956 pos = next_thread(pos);
2957 if (pos == leader) {
2958 pos = NULL;
2959 goto out;
2962 found:
2963 get_task_struct(pos);
2964 out:
2965 rcu_read_unlock();
2966 return pos;
2970 * Find the next thread in the thread list.
2971 * Return NULL if there is an error or no next thread.
2973 * The reference to the input task_struct is released.
2975 static struct task_struct *next_tid(struct task_struct *start)
2977 struct task_struct *pos = NULL;
2978 rcu_read_lock();
2979 if (pid_alive(start)) {
2980 pos = next_thread(start);
2981 if (thread_group_leader(pos))
2982 pos = NULL;
2983 else
2984 get_task_struct(pos);
2986 rcu_read_unlock();
2987 put_task_struct(start);
2988 return pos;
2991 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2992 struct task_struct *task, int tid)
2994 char name[PROC_NUMBUF];
2995 int len = snprintf(name, sizeof(name), "%d", tid);
2996 return proc_fill_cache(filp, dirent, filldir, name, len,
2997 proc_task_instantiate, task, NULL);
3000 /* for the /proc/TGID/task/ directories */
3001 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3003 struct dentry *dentry = filp->f_path.dentry;
3004 struct inode *inode = dentry->d_inode;
3005 struct task_struct *leader = NULL;
3006 struct task_struct *task;
3007 int retval = -ENOENT;
3008 ino_t ino;
3009 int tid;
3010 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
3011 struct pid_namespace *ns;
3013 task = get_proc_task(inode);
3014 if (!task)
3015 goto out_no_task;
3016 rcu_read_lock();
3017 if (pid_alive(task)) {
3018 leader = task->group_leader;
3019 get_task_struct(leader);
3021 rcu_read_unlock();
3022 put_task_struct(task);
3023 if (!leader)
3024 goto out_no_task;
3025 retval = 0;
3027 switch (pos) {
3028 case 0:
3029 ino = inode->i_ino;
3030 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3031 goto out;
3032 pos++;
3033 /* fall through */
3034 case 1:
3035 ino = parent_ino(dentry);
3036 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3037 goto out;
3038 pos++;
3039 /* fall through */
3042 /* f_version caches the tgid value that the last readdir call couldn't
3043 * return. lseek aka telldir automagically resets f_version to 0.
3045 ns = filp->f_dentry->d_sb->s_fs_info;
3046 tid = (int)filp->f_version;
3047 filp->f_version = 0;
3048 for (task = first_tid(leader, tid, pos - 2, ns);
3049 task;
3050 task = next_tid(task), pos++) {
3051 tid = task_pid_nr_ns(task, ns);
3052 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3053 /* returning this tgid failed, save it as the first
3054 * pid for the next readir call */
3055 filp->f_version = (u64)tid;
3056 put_task_struct(task);
3057 break;
3060 out:
3061 filp->f_pos = pos;
3062 put_task_struct(leader);
3063 out_no_task:
3064 return retval;
3067 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3069 struct inode *inode = dentry->d_inode;
3070 struct task_struct *p = get_proc_task(inode);
3071 generic_fillattr(inode, stat);
3073 if (p) {
3074 stat->nlink += get_nr_threads(p);
3075 put_task_struct(p);
3078 return 0;
3081 static const struct inode_operations proc_task_inode_operations = {
3082 .lookup = proc_task_lookup,
3083 .getattr = proc_task_getattr,
3084 .setattr = proc_setattr,
3087 static const struct file_operations proc_task_operations = {
3088 .read = generic_read_dir,
3089 .readdir = proc_task_readdir,