Add support for Future Electronics FWBADAPT-7WVGA expansion board
[wandboard.git] / fs / proc / base.c
blob6a938aa9e29429179ccfef84f76ff881a2a80314
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 #ifdef CONFIG_HARDWALL
87 #include <asm/hardwall.h>
88 #endif
89 #include "internal.h"
91 /* NOTE:
92 * Implementing inode permission operations in /proc is almost
93 * certainly an error. Permission checks need to happen during
94 * each system call not at open time. The reason is that most of
95 * what we wish to check for permissions in /proc varies at runtime.
97 * The classic example of a problem is opening file descriptors
98 * in /proc for a task before it execs a suid executable.
101 struct pid_entry {
102 char *name;
103 int len;
104 mode_t mode;
105 const struct inode_operations *iop;
106 const struct file_operations *fop;
107 union proc_op op;
110 #define NOD(NAME, MODE, IOP, FOP, OP) { \
111 .name = (NAME), \
112 .len = sizeof(NAME) - 1, \
113 .mode = MODE, \
114 .iop = IOP, \
115 .fop = FOP, \
116 .op = OP, \
119 #define DIR(NAME, MODE, iops, fops) \
120 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
121 #define LNK(NAME, get_link) \
122 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
123 &proc_pid_link_inode_operations, NULL, \
124 { .proc_get_link = get_link } )
125 #define REG(NAME, MODE, fops) \
126 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
127 #define INF(NAME, MODE, read) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_info_file_operations, \
130 { .proc_read = read } )
131 #define ONE(NAME, MODE, show) \
132 NOD(NAME, (S_IFREG|(MODE)), \
133 NULL, &proc_single_file_operations, \
134 { .proc_show = show } )
136 /* ANDROID is for special files in /proc. */
137 #define ANDROID(NAME, MODE, OTYPE) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 &proc_##OTYPE##_inode_operations, \
140 &proc_##OTYPE##_operations, {})
143 * Count the number of hardlinks for the pid_entry table, excluding the .
144 * and .. links.
146 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 unsigned int n)
149 unsigned int i;
150 unsigned int count;
152 count = 0;
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
155 ++count;
158 return count;
161 static int get_task_root(struct task_struct *task, struct path *root)
163 int result = -ENOENT;
165 task_lock(task);
166 if (task->fs) {
167 get_fs_root(task->fs, root);
168 result = 0;
170 task_unlock(task);
171 return result;
174 static int proc_cwd_link(struct inode *inode, struct path *path)
176 struct task_struct *task = get_proc_task(inode);
177 int result = -ENOENT;
179 if (task) {
180 task_lock(task);
181 if (task->fs) {
182 get_fs_pwd(task->fs, path);
183 result = 0;
185 task_unlock(task);
186 put_task_struct(task);
188 return result;
191 static int proc_root_link(struct inode *inode, struct path *path)
193 struct task_struct *task = get_proc_task(inode);
194 int result = -ENOENT;
196 if (task) {
197 result = get_task_root(task, path);
198 put_task_struct(task);
200 return result;
203 static struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
205 struct mm_struct *mm;
206 int err;
208 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
209 if (err)
210 return ERR_PTR(err);
212 mm = get_task_mm(task);
213 if (mm && mm != current->mm &&
214 !ptrace_may_access(task, mode) &&
215 !capable(CAP_SYS_RESOURCE)) {
216 mmput(mm);
217 mm = ERR_PTR(-EACCES);
219 mutex_unlock(&task->signal->cred_guard_mutex);
221 return mm;
224 struct mm_struct *mm_for_maps(struct task_struct *task)
226 return mm_access(task, PTRACE_MODE_READ);
229 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
231 int res = 0;
232 unsigned int len;
233 struct mm_struct *mm = get_task_mm(task);
234 if (!mm)
235 goto out;
236 if (!mm->arg_end)
237 goto out_mm; /* Shh! No looking before we're done */
239 len = mm->arg_end - mm->arg_start;
241 if (len > PAGE_SIZE)
242 len = PAGE_SIZE;
244 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
246 // If the nul at the end of args has been overwritten, then
247 // assume application is using setproctitle(3).
248 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
249 len = strnlen(buffer, res);
250 if (len < res) {
251 res = len;
252 } else {
253 len = mm->env_end - mm->env_start;
254 if (len > PAGE_SIZE - res)
255 len = PAGE_SIZE - res;
256 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
257 res = strnlen(buffer, res);
260 out_mm:
261 mmput(mm);
262 out:
263 return res;
266 static int proc_pid_auxv(struct task_struct *task, char *buffer)
268 struct mm_struct *mm = mm_for_maps(task);
269 int res = PTR_ERR(mm);
270 if (mm && !IS_ERR(mm)) {
271 unsigned int nwords = 0;
272 do {
273 nwords += 2;
274 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
275 res = nwords * sizeof(mm->saved_auxv[0]);
276 if (res > PAGE_SIZE)
277 res = PAGE_SIZE;
278 memcpy(buffer, mm->saved_auxv, res);
279 mmput(mm);
281 return res;
285 #ifdef CONFIG_KALLSYMS
287 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
288 * Returns the resolved symbol. If that fails, simply return the address.
290 static int proc_pid_wchan(struct task_struct *task, char *buffer)
292 unsigned long wchan;
293 char symname[KSYM_NAME_LEN];
295 wchan = get_wchan(task);
297 if (lookup_symbol_name(wchan, symname) < 0)
298 if (!ptrace_may_access(task, PTRACE_MODE_READ))
299 return 0;
300 else
301 return sprintf(buffer, "%lu", wchan);
302 else
303 return sprintf(buffer, "%s", symname);
305 #endif /* CONFIG_KALLSYMS */
307 static int lock_trace(struct task_struct *task)
309 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
310 if (err)
311 return err;
312 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
313 mutex_unlock(&task->signal->cred_guard_mutex);
314 return -EPERM;
316 return 0;
319 static void unlock_trace(struct task_struct *task)
321 mutex_unlock(&task->signal->cred_guard_mutex);
324 #ifdef CONFIG_STACKTRACE
326 #define MAX_STACK_TRACE_DEPTH 64
328 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
329 struct pid *pid, struct task_struct *task)
331 struct stack_trace trace;
332 unsigned long *entries;
333 int err;
334 int i;
336 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
337 if (!entries)
338 return -ENOMEM;
340 trace.nr_entries = 0;
341 trace.max_entries = MAX_STACK_TRACE_DEPTH;
342 trace.entries = entries;
343 trace.skip = 0;
345 err = lock_trace(task);
346 if (!err) {
347 save_stack_trace_tsk(task, &trace);
349 for (i = 0; i < trace.nr_entries; i++) {
350 seq_printf(m, "[<%pK>] %pS\n",
351 (void *)entries[i], (void *)entries[i]);
353 unlock_trace(task);
355 kfree(entries);
357 return err;
359 #endif
361 #ifdef CONFIG_SCHEDSTATS
363 * Provides /proc/PID/schedstat
365 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
367 return sprintf(buffer, "%llu %llu %lu\n",
368 (unsigned long long)task->se.sum_exec_runtime,
369 (unsigned long long)task->sched_info.run_delay,
370 task->sched_info.pcount);
372 #endif
374 #ifdef CONFIG_LATENCYTOP
375 static int lstats_show_proc(struct seq_file *m, void *v)
377 int i;
378 struct inode *inode = m->private;
379 struct task_struct *task = get_proc_task(inode);
381 if (!task)
382 return -ESRCH;
383 seq_puts(m, "Latency Top version : v0.1\n");
384 for (i = 0; i < 32; i++) {
385 struct latency_record *lr = &task->latency_record[i];
386 if (lr->backtrace[0]) {
387 int q;
388 seq_printf(m, "%i %li %li",
389 lr->count, lr->time, lr->max);
390 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
391 unsigned long bt = lr->backtrace[q];
392 if (!bt)
393 break;
394 if (bt == ULONG_MAX)
395 break;
396 seq_printf(m, " %ps", (void *)bt);
398 seq_putc(m, '\n');
402 put_task_struct(task);
403 return 0;
406 static int lstats_open(struct inode *inode, struct file *file)
408 return single_open(file, lstats_show_proc, inode);
411 static ssize_t lstats_write(struct file *file, const char __user *buf,
412 size_t count, loff_t *offs)
414 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
416 if (!task)
417 return -ESRCH;
418 clear_all_latency_tracing(task);
419 put_task_struct(task);
421 return count;
424 static const struct file_operations proc_lstats_operations = {
425 .open = lstats_open,
426 .read = seq_read,
427 .write = lstats_write,
428 .llseek = seq_lseek,
429 .release = single_release,
432 #endif
434 static int proc_oom_score(struct task_struct *task, char *buffer)
436 unsigned long points = 0;
438 read_lock(&tasklist_lock);
439 if (pid_alive(task))
440 points = oom_badness(task, NULL, NULL,
441 totalram_pages + total_swap_pages);
442 read_unlock(&tasklist_lock);
443 return sprintf(buffer, "%lu\n", points);
446 struct limit_names {
447 char *name;
448 char *unit;
451 static const struct limit_names lnames[RLIM_NLIMITS] = {
452 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
453 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
454 [RLIMIT_DATA] = {"Max data size", "bytes"},
455 [RLIMIT_STACK] = {"Max stack size", "bytes"},
456 [RLIMIT_CORE] = {"Max core file size", "bytes"},
457 [RLIMIT_RSS] = {"Max resident set", "bytes"},
458 [RLIMIT_NPROC] = {"Max processes", "processes"},
459 [RLIMIT_NOFILE] = {"Max open files", "files"},
460 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
461 [RLIMIT_AS] = {"Max address space", "bytes"},
462 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
463 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
464 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
465 [RLIMIT_NICE] = {"Max nice priority", NULL},
466 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
467 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
470 /* Display limits for a process */
471 static int proc_pid_limits(struct task_struct *task, char *buffer)
473 unsigned int i;
474 int count = 0;
475 unsigned long flags;
476 char *bufptr = buffer;
478 struct rlimit rlim[RLIM_NLIMITS];
480 if (!lock_task_sighand(task, &flags))
481 return 0;
482 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
483 unlock_task_sighand(task, &flags);
486 * print the file header
488 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
489 "Limit", "Soft Limit", "Hard Limit", "Units");
491 for (i = 0; i < RLIM_NLIMITS; i++) {
492 if (rlim[i].rlim_cur == RLIM_INFINITY)
493 count += sprintf(&bufptr[count], "%-25s %-20s ",
494 lnames[i].name, "unlimited");
495 else
496 count += sprintf(&bufptr[count], "%-25s %-20lu ",
497 lnames[i].name, rlim[i].rlim_cur);
499 if (rlim[i].rlim_max == RLIM_INFINITY)
500 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
501 else
502 count += sprintf(&bufptr[count], "%-20lu ",
503 rlim[i].rlim_max);
505 if (lnames[i].unit)
506 count += sprintf(&bufptr[count], "%-10s\n",
507 lnames[i].unit);
508 else
509 count += sprintf(&bufptr[count], "\n");
512 return count;
515 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
516 static int proc_pid_syscall(struct task_struct *task, char *buffer)
518 long nr;
519 unsigned long args[6], sp, pc;
520 int res = lock_trace(task);
521 if (res)
522 return res;
524 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
525 res = sprintf(buffer, "running\n");
526 else if (nr < 0)
527 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
528 else
529 res = sprintf(buffer,
530 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
532 args[0], args[1], args[2], args[3], args[4], args[5],
533 sp, pc);
534 unlock_trace(task);
535 return res;
537 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
539 /************************************************************************/
540 /* Here the fs part begins */
541 /************************************************************************/
543 /* permission checks */
544 static int proc_fd_access_allowed(struct inode *inode)
546 struct task_struct *task;
547 int allowed = 0;
548 /* Allow access to a task's file descriptors if it is us or we
549 * may use ptrace attach to the process and find out that
550 * information.
552 task = get_proc_task(inode);
553 if (task) {
554 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
555 put_task_struct(task);
557 return allowed;
560 int proc_setattr(struct dentry *dentry, struct iattr *attr)
562 int error;
563 struct inode *inode = dentry->d_inode;
565 if (attr->ia_valid & ATTR_MODE)
566 return -EPERM;
568 error = inode_change_ok(inode, attr);
569 if (error)
570 return error;
572 if ((attr->ia_valid & ATTR_SIZE) &&
573 attr->ia_size != i_size_read(inode)) {
574 error = vmtruncate(inode, attr->ia_size);
575 if (error)
576 return error;
579 setattr_copy(inode, attr);
580 mark_inode_dirty(inode);
581 return 0;
584 static const struct inode_operations proc_def_inode_operations = {
585 .setattr = proc_setattr,
588 static int mounts_open_common(struct inode *inode, struct file *file,
589 const struct seq_operations *op)
591 struct task_struct *task = get_proc_task(inode);
592 struct nsproxy *nsp;
593 struct mnt_namespace *ns = NULL;
594 struct path root;
595 struct proc_mounts *p;
596 int ret = -EINVAL;
598 if (task) {
599 rcu_read_lock();
600 nsp = task_nsproxy(task);
601 if (nsp) {
602 ns = nsp->mnt_ns;
603 if (ns)
604 get_mnt_ns(ns);
606 rcu_read_unlock();
607 if (ns && get_task_root(task, &root) == 0)
608 ret = 0;
609 put_task_struct(task);
612 if (!ns)
613 goto err;
614 if (ret)
615 goto err_put_ns;
617 ret = -ENOMEM;
618 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
619 if (!p)
620 goto err_put_path;
622 file->private_data = &p->m;
623 ret = seq_open(file, op);
624 if (ret)
625 goto err_free;
627 p->m.private = p;
628 p->ns = ns;
629 p->root = root;
630 p->event = ns->event;
632 return 0;
634 err_free:
635 kfree(p);
636 err_put_path:
637 path_put(&root);
638 err_put_ns:
639 put_mnt_ns(ns);
640 err:
641 return ret;
644 static int mounts_release(struct inode *inode, struct file *file)
646 struct proc_mounts *p = file->private_data;
647 path_put(&p->root);
648 put_mnt_ns(p->ns);
649 return seq_release(inode, file);
652 static unsigned mounts_poll(struct file *file, poll_table *wait)
654 struct proc_mounts *p = file->private_data;
655 unsigned res = POLLIN | POLLRDNORM;
657 poll_wait(file, &p->ns->poll, wait);
658 if (mnt_had_events(p))
659 res |= POLLERR | POLLPRI;
661 return res;
664 static int mounts_open(struct inode *inode, struct file *file)
666 return mounts_open_common(inode, file, &mounts_op);
669 static const struct file_operations proc_mounts_operations = {
670 .open = mounts_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = mounts_release,
674 .poll = mounts_poll,
677 static int mountinfo_open(struct inode *inode, struct file *file)
679 return mounts_open_common(inode, file, &mountinfo_op);
682 static const struct file_operations proc_mountinfo_operations = {
683 .open = mountinfo_open,
684 .read = seq_read,
685 .llseek = seq_lseek,
686 .release = mounts_release,
687 .poll = mounts_poll,
690 static int mountstats_open(struct inode *inode, struct file *file)
692 return mounts_open_common(inode, file, &mountstats_op);
695 static const struct file_operations proc_mountstats_operations = {
696 .open = mountstats_open,
697 .read = seq_read,
698 .llseek = seq_lseek,
699 .release = mounts_release,
702 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
704 static ssize_t proc_info_read(struct file * file, char __user * buf,
705 size_t count, loff_t *ppos)
707 struct inode * inode = file->f_path.dentry->d_inode;
708 unsigned long page;
709 ssize_t length;
710 struct task_struct *task = get_proc_task(inode);
712 length = -ESRCH;
713 if (!task)
714 goto out_no_task;
716 if (count > PROC_BLOCK_SIZE)
717 count = PROC_BLOCK_SIZE;
719 length = -ENOMEM;
720 if (!(page = __get_free_page(GFP_TEMPORARY)))
721 goto out;
723 length = PROC_I(inode)->op.proc_read(task, (char*)page);
725 if (length >= 0)
726 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
727 free_page(page);
728 out:
729 put_task_struct(task);
730 out_no_task:
731 return length;
734 static const struct file_operations proc_info_file_operations = {
735 .read = proc_info_read,
736 .llseek = generic_file_llseek,
739 static int proc_single_show(struct seq_file *m, void *v)
741 struct inode *inode = m->private;
742 struct pid_namespace *ns;
743 struct pid *pid;
744 struct task_struct *task;
745 int ret;
747 ns = inode->i_sb->s_fs_info;
748 pid = proc_pid(inode);
749 task = get_pid_task(pid, PIDTYPE_PID);
750 if (!task)
751 return -ESRCH;
753 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
755 put_task_struct(task);
756 return ret;
759 static int proc_single_open(struct inode *inode, struct file *filp)
761 return single_open(filp, proc_single_show, inode);
764 static const struct file_operations proc_single_file_operations = {
765 .open = proc_single_open,
766 .read = seq_read,
767 .llseek = seq_lseek,
768 .release = single_release,
771 static int mem_open(struct inode* inode, struct file* file)
773 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
774 struct mm_struct *mm;
776 if (!task)
777 return -ESRCH;
779 mm = mm_access(task, PTRACE_MODE_ATTACH);
780 put_task_struct(task);
782 if (IS_ERR(mm))
783 return PTR_ERR(mm);
785 if (mm) {
786 /* ensure this mm_struct can't be freed */
787 atomic_inc(&mm->mm_count);
788 /* but do not pin its memory */
789 mmput(mm);
792 /* OK to pass negative loff_t, we can catch out-of-range */
793 file->f_mode |= FMODE_UNSIGNED_OFFSET;
794 file->private_data = mm;
796 return 0;
799 static ssize_t mem_rw(struct file *file, char __user *buf,
800 size_t count, loff_t *ppos, int write)
802 struct mm_struct *mm = file->private_data;
803 unsigned long addr = *ppos;
804 ssize_t copied;
805 char *page;
807 if (!mm)
808 return 0;
810 page = (char *)__get_free_page(GFP_TEMPORARY);
811 if (!page)
812 return -ENOMEM;
814 copied = 0;
815 if (!atomic_inc_not_zero(&mm->mm_users))
816 goto free;
818 while (count > 0) {
819 int this_len = min_t(int, count, PAGE_SIZE);
821 if (write && copy_from_user(page, buf, this_len)) {
822 copied = -EFAULT;
823 break;
826 this_len = access_remote_vm(mm, addr, page, this_len, write);
827 if (!this_len) {
828 if (!copied)
829 copied = -EIO;
830 break;
833 if (!write && copy_to_user(buf, page, this_len)) {
834 copied = -EFAULT;
835 break;
838 buf += this_len;
839 addr += this_len;
840 copied += this_len;
841 count -= this_len;
843 *ppos = addr;
845 mmput(mm);
846 free:
847 free_page((unsigned long) page);
848 return copied;
851 static ssize_t mem_read(struct file *file, char __user *buf,
852 size_t count, loff_t *ppos)
854 return mem_rw(file, buf, count, ppos, 0);
857 #define mem_write NULL
859 #ifndef mem_write
860 /* This is a security hazard */
861 static ssize_t mem_write(struct file *file, const char __user *buf,
862 size_t count, loff_t *ppos)
864 return mem_rw(file, (char __user*)buf, count, ppos, 1);
866 #endif
868 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
870 switch (orig) {
871 case 0:
872 file->f_pos = offset;
873 break;
874 case 1:
875 file->f_pos += offset;
876 break;
877 default:
878 return -EINVAL;
880 force_successful_syscall_return();
881 return file->f_pos;
884 static int mem_release(struct inode *inode, struct file *file)
886 struct mm_struct *mm = file->private_data;
887 if (mm)
888 mmdrop(mm);
889 return 0;
892 static const struct file_operations proc_mem_operations = {
893 .llseek = mem_lseek,
894 .read = mem_read,
895 .write = mem_write,
896 .open = mem_open,
897 .release = mem_release,
900 static ssize_t environ_read(struct file *file, char __user *buf,
901 size_t count, loff_t *ppos)
903 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
904 char *page;
905 unsigned long src = *ppos;
906 int ret = -ESRCH;
907 struct mm_struct *mm;
909 if (!task)
910 goto out_no_task;
912 ret = -ENOMEM;
913 page = (char *)__get_free_page(GFP_TEMPORARY);
914 if (!page)
915 goto out;
918 mm = mm_for_maps(task);
919 ret = PTR_ERR(mm);
920 if (!mm || IS_ERR(mm))
921 goto out_free;
923 ret = 0;
924 while (count > 0) {
925 int this_len, retval, max_len;
927 this_len = mm->env_end - (mm->env_start + src);
929 if (this_len <= 0)
930 break;
932 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
933 this_len = (this_len > max_len) ? max_len : this_len;
935 retval = access_process_vm(task, (mm->env_start + src),
936 page, this_len, 0);
938 if (retval <= 0) {
939 ret = retval;
940 break;
943 if (copy_to_user(buf, page, retval)) {
944 ret = -EFAULT;
945 break;
948 ret += retval;
949 src += retval;
950 buf += retval;
951 count -= retval;
953 *ppos = src;
955 mmput(mm);
956 out_free:
957 free_page((unsigned long) page);
958 out:
959 put_task_struct(task);
960 out_no_task:
961 return ret;
964 static const struct file_operations proc_environ_operations = {
965 .read = environ_read,
966 .llseek = generic_file_llseek,
969 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
970 size_t count, loff_t *ppos)
972 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
973 char buffer[PROC_NUMBUF];
974 size_t len;
975 int oom_adjust = OOM_DISABLE;
976 unsigned long flags;
978 if (!task)
979 return -ESRCH;
981 if (lock_task_sighand(task, &flags)) {
982 oom_adjust = task->signal->oom_adj;
983 unlock_task_sighand(task, &flags);
986 put_task_struct(task);
988 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
990 return simple_read_from_buffer(buf, count, ppos, buffer, len);
993 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
994 size_t count, loff_t *ppos)
996 struct task_struct *task;
997 char buffer[PROC_NUMBUF];
998 int oom_adjust;
999 unsigned long flags;
1000 int err;
1002 memset(buffer, 0, sizeof(buffer));
1003 if (count > sizeof(buffer) - 1)
1004 count = sizeof(buffer) - 1;
1005 if (copy_from_user(buffer, buf, count)) {
1006 err = -EFAULT;
1007 goto out;
1010 err = kstrtoint(strstrip(buffer), 0, &oom_adjust);
1011 if (err)
1012 goto out;
1013 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1014 oom_adjust != OOM_DISABLE) {
1015 err = -EINVAL;
1016 goto out;
1019 task = get_proc_task(file->f_path.dentry->d_inode);
1020 if (!task) {
1021 err = -ESRCH;
1022 goto out;
1025 task_lock(task);
1026 if (!task->mm) {
1027 err = -EINVAL;
1028 goto err_task_lock;
1031 if (!lock_task_sighand(task, &flags)) {
1032 err = -ESRCH;
1033 goto err_task_lock;
1036 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1037 err = -EACCES;
1038 goto err_sighand;
1041 if (oom_adjust != task->signal->oom_adj) {
1042 if (oom_adjust == OOM_DISABLE)
1043 atomic_inc(&task->mm->oom_disable_count);
1044 if (task->signal->oom_adj == OOM_DISABLE)
1045 atomic_dec(&task->mm->oom_disable_count);
1049 * Warn that /proc/pid/oom_adj is deprecated, see
1050 * Documentation/feature-removal-schedule.txt.
1052 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1053 "please use /proc/%d/oom_score_adj instead.\n",
1054 current->comm, task_pid_nr(current),
1055 task_pid_nr(task), task_pid_nr(task));
1056 task->signal->oom_adj = oom_adjust;
1058 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1059 * value is always attainable.
1061 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1062 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1063 else
1064 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1065 -OOM_DISABLE;
1066 err_sighand:
1067 unlock_task_sighand(task, &flags);
1068 err_task_lock:
1069 task_unlock(task);
1070 put_task_struct(task);
1071 out:
1072 return err < 0 ? err : count;
1075 static int oom_adjust_permission(struct inode *inode, int mask,
1076 unsigned int flags)
1078 uid_t uid;
1079 struct task_struct *p;
1081 if (flags & IPERM_FLAG_RCU)
1082 return -ECHILD;
1084 p = get_proc_task(inode);
1085 if(p) {
1086 uid = task_uid(p);
1087 put_task_struct(p);
1091 * System Server (uid == 1000) is granted access to oom_adj of all
1092 * android applications (uid > 10000) as and services (uid >= 1000)
1094 if (p && (current_fsuid() == 1000) && (uid >= 1000)) {
1095 if (inode->i_mode >> 6 & mask) {
1096 return 0;
1100 /* Fall back to default. */
1101 return generic_permission(inode, mask, flags, NULL);
1104 static const struct inode_operations proc_oom_adjust_inode_operations = {
1105 .permission = oom_adjust_permission,
1108 static const struct file_operations proc_oom_adjust_operations = {
1109 .read = oom_adjust_read,
1110 .write = oom_adjust_write,
1111 .llseek = generic_file_llseek,
1114 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1115 size_t count, loff_t *ppos)
1117 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1118 char buffer[PROC_NUMBUF];
1119 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1120 unsigned long flags;
1121 size_t len;
1123 if (!task)
1124 return -ESRCH;
1125 if (lock_task_sighand(task, &flags)) {
1126 oom_score_adj = task->signal->oom_score_adj;
1127 unlock_task_sighand(task, &flags);
1129 put_task_struct(task);
1130 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1131 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1134 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1135 size_t count, loff_t *ppos)
1137 struct task_struct *task;
1138 char buffer[PROC_NUMBUF];
1139 unsigned long flags;
1140 int oom_score_adj;
1141 int err;
1143 memset(buffer, 0, sizeof(buffer));
1144 if (count > sizeof(buffer) - 1)
1145 count = sizeof(buffer) - 1;
1146 if (copy_from_user(buffer, buf, count)) {
1147 err = -EFAULT;
1148 goto out;
1151 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1152 if (err)
1153 goto out;
1154 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1155 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1156 err = -EINVAL;
1157 goto out;
1160 task = get_proc_task(file->f_path.dentry->d_inode);
1161 if (!task) {
1162 err = -ESRCH;
1163 goto out;
1166 task_lock(task);
1167 if (!task->mm) {
1168 err = -EINVAL;
1169 goto err_task_lock;
1172 if (!lock_task_sighand(task, &flags)) {
1173 err = -ESRCH;
1174 goto err_task_lock;
1177 if (oom_score_adj < task->signal->oom_score_adj_min &&
1178 !capable(CAP_SYS_RESOURCE)) {
1179 err = -EACCES;
1180 goto err_sighand;
1183 if (oom_score_adj != task->signal->oom_score_adj) {
1184 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1185 atomic_inc(&task->mm->oom_disable_count);
1186 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1187 atomic_dec(&task->mm->oom_disable_count);
1189 task->signal->oom_score_adj = oom_score_adj;
1190 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1191 task->signal->oom_score_adj_min = oom_score_adj;
1193 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1194 * always attainable.
1196 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1197 task->signal->oom_adj = OOM_DISABLE;
1198 else
1199 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1200 OOM_SCORE_ADJ_MAX;
1201 err_sighand:
1202 unlock_task_sighand(task, &flags);
1203 err_task_lock:
1204 task_unlock(task);
1205 put_task_struct(task);
1206 out:
1207 return err < 0 ? err : count;
1210 static const struct file_operations proc_oom_score_adj_operations = {
1211 .read = oom_score_adj_read,
1212 .write = oom_score_adj_write,
1213 .llseek = default_llseek,
1216 #ifdef CONFIG_AUDITSYSCALL
1217 #define TMPBUFLEN 21
1218 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1219 size_t count, loff_t *ppos)
1221 struct inode * inode = file->f_path.dentry->d_inode;
1222 struct task_struct *task = get_proc_task(inode);
1223 ssize_t length;
1224 char tmpbuf[TMPBUFLEN];
1226 if (!task)
1227 return -ESRCH;
1228 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1229 audit_get_loginuid(task));
1230 put_task_struct(task);
1231 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1234 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1235 size_t count, loff_t *ppos)
1237 struct inode * inode = file->f_path.dentry->d_inode;
1238 char *page, *tmp;
1239 ssize_t length;
1240 uid_t loginuid;
1242 if (!capable(CAP_AUDIT_CONTROL))
1243 return -EPERM;
1245 rcu_read_lock();
1246 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1247 rcu_read_unlock();
1248 return -EPERM;
1250 rcu_read_unlock();
1252 if (count >= PAGE_SIZE)
1253 count = PAGE_SIZE - 1;
1255 if (*ppos != 0) {
1256 /* No partial writes. */
1257 return -EINVAL;
1259 page = (char*)__get_free_page(GFP_TEMPORARY);
1260 if (!page)
1261 return -ENOMEM;
1262 length = -EFAULT;
1263 if (copy_from_user(page, buf, count))
1264 goto out_free_page;
1266 page[count] = '\0';
1267 loginuid = simple_strtoul(page, &tmp, 10);
1268 if (tmp == page) {
1269 length = -EINVAL;
1270 goto out_free_page;
1273 length = audit_set_loginuid(current, loginuid);
1274 if (likely(length == 0))
1275 length = count;
1277 out_free_page:
1278 free_page((unsigned long) page);
1279 return length;
1282 static const struct file_operations proc_loginuid_operations = {
1283 .read = proc_loginuid_read,
1284 .write = proc_loginuid_write,
1285 .llseek = generic_file_llseek,
1288 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1289 size_t count, loff_t *ppos)
1291 struct inode * inode = file->f_path.dentry->d_inode;
1292 struct task_struct *task = get_proc_task(inode);
1293 ssize_t length;
1294 char tmpbuf[TMPBUFLEN];
1296 if (!task)
1297 return -ESRCH;
1298 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1299 audit_get_sessionid(task));
1300 put_task_struct(task);
1301 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1304 static const struct file_operations proc_sessionid_operations = {
1305 .read = proc_sessionid_read,
1306 .llseek = generic_file_llseek,
1308 #endif
1310 #ifdef CONFIG_FAULT_INJECTION
1311 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1312 size_t count, loff_t *ppos)
1314 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1315 char buffer[PROC_NUMBUF];
1316 size_t len;
1317 int make_it_fail;
1319 if (!task)
1320 return -ESRCH;
1321 make_it_fail = task->make_it_fail;
1322 put_task_struct(task);
1324 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1326 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1329 static ssize_t proc_fault_inject_write(struct file * file,
1330 const char __user * buf, size_t count, loff_t *ppos)
1332 struct task_struct *task;
1333 char buffer[PROC_NUMBUF], *end;
1334 int make_it_fail;
1336 if (!capable(CAP_SYS_RESOURCE))
1337 return -EPERM;
1338 memset(buffer, 0, sizeof(buffer));
1339 if (count > sizeof(buffer) - 1)
1340 count = sizeof(buffer) - 1;
1341 if (copy_from_user(buffer, buf, count))
1342 return -EFAULT;
1343 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1344 if (*end)
1345 return -EINVAL;
1346 task = get_proc_task(file->f_dentry->d_inode);
1347 if (!task)
1348 return -ESRCH;
1349 task->make_it_fail = make_it_fail;
1350 put_task_struct(task);
1352 return count;
1355 static const struct file_operations proc_fault_inject_operations = {
1356 .read = proc_fault_inject_read,
1357 .write = proc_fault_inject_write,
1358 .llseek = generic_file_llseek,
1360 #endif
1363 #ifdef CONFIG_SCHED_DEBUG
1365 * Print out various scheduling related per-task fields:
1367 static int sched_show(struct seq_file *m, void *v)
1369 struct inode *inode = m->private;
1370 struct task_struct *p;
1372 p = get_proc_task(inode);
1373 if (!p)
1374 return -ESRCH;
1375 proc_sched_show_task(p, m);
1377 put_task_struct(p);
1379 return 0;
1382 static ssize_t
1383 sched_write(struct file *file, const char __user *buf,
1384 size_t count, loff_t *offset)
1386 struct inode *inode = file->f_path.dentry->d_inode;
1387 struct task_struct *p;
1389 p = get_proc_task(inode);
1390 if (!p)
1391 return -ESRCH;
1392 proc_sched_set_task(p);
1394 put_task_struct(p);
1396 return count;
1399 static int sched_open(struct inode *inode, struct file *filp)
1401 return single_open(filp, sched_show, inode);
1404 static const struct file_operations proc_pid_sched_operations = {
1405 .open = sched_open,
1406 .read = seq_read,
1407 .write = sched_write,
1408 .llseek = seq_lseek,
1409 .release = single_release,
1412 #endif
1414 #ifdef CONFIG_SCHED_AUTOGROUP
1416 * Print out autogroup related information:
1418 static int sched_autogroup_show(struct seq_file *m, void *v)
1420 struct inode *inode = m->private;
1421 struct task_struct *p;
1423 p = get_proc_task(inode);
1424 if (!p)
1425 return -ESRCH;
1426 proc_sched_autogroup_show_task(p, m);
1428 put_task_struct(p);
1430 return 0;
1433 static ssize_t
1434 sched_autogroup_write(struct file *file, const char __user *buf,
1435 size_t count, loff_t *offset)
1437 struct inode *inode = file->f_path.dentry->d_inode;
1438 struct task_struct *p;
1439 char buffer[PROC_NUMBUF];
1440 int nice;
1441 int err;
1443 memset(buffer, 0, sizeof(buffer));
1444 if (count > sizeof(buffer) - 1)
1445 count = sizeof(buffer) - 1;
1446 if (copy_from_user(buffer, buf, count))
1447 return -EFAULT;
1449 err = kstrtoint(strstrip(buffer), 0, &nice);
1450 if (err < 0)
1451 return err;
1453 p = get_proc_task(inode);
1454 if (!p)
1455 return -ESRCH;
1457 err = nice;
1458 err = proc_sched_autogroup_set_nice(p, &err);
1459 if (err)
1460 count = err;
1462 put_task_struct(p);
1464 return count;
1467 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1469 int ret;
1471 ret = single_open(filp, sched_autogroup_show, NULL);
1472 if (!ret) {
1473 struct seq_file *m = filp->private_data;
1475 m->private = inode;
1477 return ret;
1480 static const struct file_operations proc_pid_sched_autogroup_operations = {
1481 .open = sched_autogroup_open,
1482 .read = seq_read,
1483 .write = sched_autogroup_write,
1484 .llseek = seq_lseek,
1485 .release = single_release,
1488 #endif /* CONFIG_SCHED_AUTOGROUP */
1490 static ssize_t comm_write(struct file *file, const char __user *buf,
1491 size_t count, loff_t *offset)
1493 struct inode *inode = file->f_path.dentry->d_inode;
1494 struct task_struct *p;
1495 char buffer[TASK_COMM_LEN];
1497 memset(buffer, 0, sizeof(buffer));
1498 if (count > sizeof(buffer) - 1)
1499 count = sizeof(buffer) - 1;
1500 if (copy_from_user(buffer, buf, count))
1501 return -EFAULT;
1503 p = get_proc_task(inode);
1504 if (!p)
1505 return -ESRCH;
1507 if (same_thread_group(current, p))
1508 set_task_comm(p, buffer);
1509 else
1510 count = -EINVAL;
1512 put_task_struct(p);
1514 return count;
1517 static int comm_show(struct seq_file *m, void *v)
1519 struct inode *inode = m->private;
1520 struct task_struct *p;
1522 p = get_proc_task(inode);
1523 if (!p)
1524 return -ESRCH;
1526 task_lock(p);
1527 seq_printf(m, "%s\n", p->comm);
1528 task_unlock(p);
1530 put_task_struct(p);
1532 return 0;
1535 static int comm_open(struct inode *inode, struct file *filp)
1537 return single_open(filp, comm_show, inode);
1540 static const struct file_operations proc_pid_set_comm_operations = {
1541 .open = comm_open,
1542 .read = seq_read,
1543 .write = comm_write,
1544 .llseek = seq_lseek,
1545 .release = single_release,
1548 static int proc_exe_link(struct inode *inode, struct path *exe_path)
1550 struct task_struct *task;
1551 struct mm_struct *mm;
1552 struct file *exe_file;
1554 task = get_proc_task(inode);
1555 if (!task)
1556 return -ENOENT;
1557 mm = get_task_mm(task);
1558 put_task_struct(task);
1559 if (!mm)
1560 return -ENOENT;
1561 exe_file = get_mm_exe_file(mm);
1562 mmput(mm);
1563 if (exe_file) {
1564 *exe_path = exe_file->f_path;
1565 path_get(&exe_file->f_path);
1566 fput(exe_file);
1567 return 0;
1568 } else
1569 return -ENOENT;
1572 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1574 struct inode *inode = dentry->d_inode;
1575 int error = -EACCES;
1577 /* We don't need a base pointer in the /proc filesystem */
1578 path_put(&nd->path);
1580 /* Are we allowed to snoop on the tasks file descriptors? */
1581 if (!proc_fd_access_allowed(inode))
1582 goto out;
1584 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1585 out:
1586 return ERR_PTR(error);
1589 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1591 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1592 char *pathname;
1593 int len;
1595 if (!tmp)
1596 return -ENOMEM;
1598 pathname = d_path(path, tmp, PAGE_SIZE);
1599 len = PTR_ERR(pathname);
1600 if (IS_ERR(pathname))
1601 goto out;
1602 len = tmp + PAGE_SIZE - 1 - pathname;
1604 if (len > buflen)
1605 len = buflen;
1606 if (copy_to_user(buffer, pathname, len))
1607 len = -EFAULT;
1608 out:
1609 free_page((unsigned long)tmp);
1610 return len;
1613 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1615 int error = -EACCES;
1616 struct inode *inode = dentry->d_inode;
1617 struct path path;
1619 /* Are we allowed to snoop on the tasks file descriptors? */
1620 if (!proc_fd_access_allowed(inode))
1621 goto out;
1623 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1624 if (error)
1625 goto out;
1627 error = do_proc_readlink(&path, buffer, buflen);
1628 path_put(&path);
1629 out:
1630 return error;
1633 static const struct inode_operations proc_pid_link_inode_operations = {
1634 .readlink = proc_pid_readlink,
1635 .follow_link = proc_pid_follow_link,
1636 .setattr = proc_setattr,
1640 /* building an inode */
1642 static int task_dumpable(struct task_struct *task)
1644 int dumpable = 0;
1645 struct mm_struct *mm;
1647 task_lock(task);
1648 mm = task->mm;
1649 if (mm)
1650 dumpable = get_dumpable(mm);
1651 task_unlock(task);
1652 if(dumpable == 1)
1653 return 1;
1654 return 0;
1657 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1659 struct inode * inode;
1660 struct proc_inode *ei;
1661 const struct cred *cred;
1663 /* We need a new inode */
1665 inode = new_inode(sb);
1666 if (!inode)
1667 goto out;
1669 /* Common stuff */
1670 ei = PROC_I(inode);
1671 inode->i_ino = get_next_ino();
1672 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1673 inode->i_op = &proc_def_inode_operations;
1676 * grab the reference to task.
1678 ei->pid = get_task_pid(task, PIDTYPE_PID);
1679 if (!ei->pid)
1680 goto out_unlock;
1682 if (task_dumpable(task)) {
1683 rcu_read_lock();
1684 cred = __task_cred(task);
1685 inode->i_uid = cred->euid;
1686 inode->i_gid = cred->egid;
1687 rcu_read_unlock();
1689 security_task_to_inode(task, inode);
1691 out:
1692 return inode;
1694 out_unlock:
1695 iput(inode);
1696 return NULL;
1699 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1701 struct inode *inode = dentry->d_inode;
1702 struct task_struct *task;
1703 const struct cred *cred;
1705 generic_fillattr(inode, stat);
1707 rcu_read_lock();
1708 stat->uid = 0;
1709 stat->gid = 0;
1710 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1711 if (task) {
1712 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1713 task_dumpable(task)) {
1714 cred = __task_cred(task);
1715 stat->uid = cred->euid;
1716 stat->gid = cred->egid;
1719 rcu_read_unlock();
1720 return 0;
1723 /* dentry stuff */
1726 * Exceptional case: normally we are not allowed to unhash a busy
1727 * directory. In this case, however, we can do it - no aliasing problems
1728 * due to the way we treat inodes.
1730 * Rewrite the inode's ownerships here because the owning task may have
1731 * performed a setuid(), etc.
1733 * Before the /proc/pid/status file was created the only way to read
1734 * the effective uid of a /process was to stat /proc/pid. Reading
1735 * /proc/pid/status is slow enough that procps and other packages
1736 * kept stating /proc/pid. To keep the rules in /proc simple I have
1737 * made this apply to all per process world readable and executable
1738 * directories.
1740 int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1742 struct inode *inode;
1743 struct task_struct *task;
1744 const struct cred *cred;
1746 if (nd && nd->flags & LOOKUP_RCU)
1747 return -ECHILD;
1749 inode = dentry->d_inode;
1750 task = get_proc_task(inode);
1752 if (task) {
1753 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1754 task_dumpable(task)) {
1755 rcu_read_lock();
1756 cred = __task_cred(task);
1757 inode->i_uid = cred->euid;
1758 inode->i_gid = cred->egid;
1759 rcu_read_unlock();
1760 } else {
1761 inode->i_uid = 0;
1762 inode->i_gid = 0;
1764 inode->i_mode &= ~(S_ISUID | S_ISGID);
1765 security_task_to_inode(task, inode);
1766 put_task_struct(task);
1767 return 1;
1769 d_drop(dentry);
1770 return 0;
1773 static int pid_delete_dentry(const struct dentry * dentry)
1775 /* Is the task we represent dead?
1776 * If so, then don't put the dentry on the lru list,
1777 * kill it immediately.
1779 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1782 const struct dentry_operations pid_dentry_operations =
1784 .d_revalidate = pid_revalidate,
1785 .d_delete = pid_delete_dentry,
1788 /* Lookups */
1791 * Fill a directory entry.
1793 * If possible create the dcache entry and derive our inode number and
1794 * file type from dcache entry.
1796 * Since all of the proc inode numbers are dynamically generated, the inode
1797 * numbers do not exist until the inode is cache. This means creating the
1798 * the dcache entry in readdir is necessary to keep the inode numbers
1799 * reported by readdir in sync with the inode numbers reported
1800 * by stat.
1802 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1803 const char *name, int len,
1804 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1806 struct dentry *child, *dir = filp->f_path.dentry;
1807 struct inode *inode;
1808 struct qstr qname;
1809 ino_t ino = 0;
1810 unsigned type = DT_UNKNOWN;
1812 qname.name = name;
1813 qname.len = len;
1814 qname.hash = full_name_hash(name, len);
1816 child = d_lookup(dir, &qname);
1817 if (!child) {
1818 struct dentry *new;
1819 new = d_alloc(dir, &qname);
1820 if (new) {
1821 child = instantiate(dir->d_inode, new, task, ptr);
1822 if (child)
1823 dput(new);
1824 else
1825 child = new;
1828 if (!child || IS_ERR(child) || !child->d_inode)
1829 goto end_instantiate;
1830 inode = child->d_inode;
1831 if (inode) {
1832 ino = inode->i_ino;
1833 type = inode->i_mode >> 12;
1835 dput(child);
1836 end_instantiate:
1837 if (!ino)
1838 ino = find_inode_number(dir, &qname);
1839 if (!ino)
1840 ino = 1;
1841 return filldir(dirent, name, len, filp->f_pos, ino, type);
1844 static unsigned name_to_int(struct dentry *dentry)
1846 const char *name = dentry->d_name.name;
1847 int len = dentry->d_name.len;
1848 unsigned n = 0;
1850 if (len > 1 && *name == '0')
1851 goto out;
1852 while (len-- > 0) {
1853 unsigned c = *name++ - '0';
1854 if (c > 9)
1855 goto out;
1856 if (n >= (~0U-9)/10)
1857 goto out;
1858 n *= 10;
1859 n += c;
1861 return n;
1862 out:
1863 return ~0U;
1866 #define PROC_FDINFO_MAX 64
1868 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1870 struct task_struct *task = get_proc_task(inode);
1871 struct files_struct *files = NULL;
1872 struct file *file;
1873 int fd = proc_fd(inode);
1875 if (task) {
1876 files = get_files_struct(task);
1877 put_task_struct(task);
1879 if (files) {
1881 * We are not taking a ref to the file structure, so we must
1882 * hold ->file_lock.
1884 spin_lock(&files->file_lock);
1885 file = fcheck_files(files, fd);
1886 if (file) {
1887 unsigned int f_flags;
1888 struct fdtable *fdt;
1890 fdt = files_fdtable(files);
1891 f_flags = file->f_flags & ~O_CLOEXEC;
1892 if (FD_ISSET(fd, fdt->close_on_exec))
1893 f_flags |= O_CLOEXEC;
1895 if (path) {
1896 *path = file->f_path;
1897 path_get(&file->f_path);
1899 if (info)
1900 snprintf(info, PROC_FDINFO_MAX,
1901 "pos:\t%lli\n"
1902 "flags:\t0%o\n",
1903 (long long) file->f_pos,
1904 f_flags);
1905 spin_unlock(&files->file_lock);
1906 put_files_struct(files);
1907 return 0;
1909 spin_unlock(&files->file_lock);
1910 put_files_struct(files);
1912 return -ENOENT;
1915 static int proc_fd_link(struct inode *inode, struct path *path)
1917 return proc_fd_info(inode, path, NULL);
1920 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1922 struct inode *inode;
1923 struct task_struct *task;
1924 int fd;
1925 struct files_struct *files;
1926 const struct cred *cred;
1928 if (nd && nd->flags & LOOKUP_RCU)
1929 return -ECHILD;
1931 inode = dentry->d_inode;
1932 task = get_proc_task(inode);
1933 fd = proc_fd(inode);
1935 if (task) {
1936 files = get_files_struct(task);
1937 if (files) {
1938 rcu_read_lock();
1939 if (fcheck_files(files, fd)) {
1940 rcu_read_unlock();
1941 put_files_struct(files);
1942 if (task_dumpable(task)) {
1943 rcu_read_lock();
1944 cred = __task_cred(task);
1945 inode->i_uid = cred->euid;
1946 inode->i_gid = cred->egid;
1947 rcu_read_unlock();
1948 } else {
1949 inode->i_uid = 0;
1950 inode->i_gid = 0;
1952 inode->i_mode &= ~(S_ISUID | S_ISGID);
1953 security_task_to_inode(task, inode);
1954 put_task_struct(task);
1955 return 1;
1957 rcu_read_unlock();
1958 put_files_struct(files);
1960 put_task_struct(task);
1962 d_drop(dentry);
1963 return 0;
1966 static const struct dentry_operations tid_fd_dentry_operations =
1968 .d_revalidate = tid_fd_revalidate,
1969 .d_delete = pid_delete_dentry,
1972 static struct dentry *proc_fd_instantiate(struct inode *dir,
1973 struct dentry *dentry, struct task_struct *task, const void *ptr)
1975 unsigned fd = *(const unsigned *)ptr;
1976 struct file *file;
1977 struct files_struct *files;
1978 struct inode *inode;
1979 struct proc_inode *ei;
1980 struct dentry *error = ERR_PTR(-ENOENT);
1982 inode = proc_pid_make_inode(dir->i_sb, task);
1983 if (!inode)
1984 goto out;
1985 ei = PROC_I(inode);
1986 ei->fd = fd;
1987 files = get_files_struct(task);
1988 if (!files)
1989 goto out_iput;
1990 inode->i_mode = S_IFLNK;
1993 * We are not taking a ref to the file structure, so we must
1994 * hold ->file_lock.
1996 spin_lock(&files->file_lock);
1997 file = fcheck_files(files, fd);
1998 if (!file)
1999 goto out_unlock;
2000 if (file->f_mode & FMODE_READ)
2001 inode->i_mode |= S_IRUSR | S_IXUSR;
2002 if (file->f_mode & FMODE_WRITE)
2003 inode->i_mode |= S_IWUSR | S_IXUSR;
2004 spin_unlock(&files->file_lock);
2005 put_files_struct(files);
2007 inode->i_op = &proc_pid_link_inode_operations;
2008 inode->i_size = 64;
2009 ei->op.proc_get_link = proc_fd_link;
2010 d_set_d_op(dentry, &tid_fd_dentry_operations);
2011 d_add(dentry, inode);
2012 /* Close the race of the process dying before we return the dentry */
2013 if (tid_fd_revalidate(dentry, NULL))
2014 error = NULL;
2016 out:
2017 return error;
2018 out_unlock:
2019 spin_unlock(&files->file_lock);
2020 put_files_struct(files);
2021 out_iput:
2022 iput(inode);
2023 goto out;
2026 static struct dentry *proc_lookupfd_common(struct inode *dir,
2027 struct dentry *dentry,
2028 instantiate_t instantiate)
2030 struct task_struct *task = get_proc_task(dir);
2031 unsigned fd = name_to_int(dentry);
2032 struct dentry *result = ERR_PTR(-ENOENT);
2034 if (!task)
2035 goto out_no_task;
2036 if (fd == ~0U)
2037 goto out;
2039 result = instantiate(dir, dentry, task, &fd);
2040 out:
2041 put_task_struct(task);
2042 out_no_task:
2043 return result;
2046 static int proc_readfd_common(struct file * filp, void * dirent,
2047 filldir_t filldir, instantiate_t instantiate)
2049 struct dentry *dentry = filp->f_path.dentry;
2050 struct inode *inode = dentry->d_inode;
2051 struct task_struct *p = get_proc_task(inode);
2052 unsigned int fd, ino;
2053 int retval;
2054 struct files_struct * files;
2056 retval = -ENOENT;
2057 if (!p)
2058 goto out_no_task;
2059 retval = 0;
2061 fd = filp->f_pos;
2062 switch (fd) {
2063 case 0:
2064 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2065 goto out;
2066 filp->f_pos++;
2067 case 1:
2068 ino = parent_ino(dentry);
2069 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2070 goto out;
2071 filp->f_pos++;
2072 default:
2073 files = get_files_struct(p);
2074 if (!files)
2075 goto out;
2076 rcu_read_lock();
2077 for (fd = filp->f_pos-2;
2078 fd < files_fdtable(files)->max_fds;
2079 fd++, filp->f_pos++) {
2080 char name[PROC_NUMBUF];
2081 int len;
2083 if (!fcheck_files(files, fd))
2084 continue;
2085 rcu_read_unlock();
2087 len = snprintf(name, sizeof(name), "%d", fd);
2088 if (proc_fill_cache(filp, dirent, filldir,
2089 name, len, instantiate,
2090 p, &fd) < 0) {
2091 rcu_read_lock();
2092 break;
2094 rcu_read_lock();
2096 rcu_read_unlock();
2097 put_files_struct(files);
2099 out:
2100 put_task_struct(p);
2101 out_no_task:
2102 return retval;
2105 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2106 struct nameidata *nd)
2108 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2111 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2113 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2116 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2117 size_t len, loff_t *ppos)
2119 char tmp[PROC_FDINFO_MAX];
2120 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
2121 if (!err)
2122 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2123 return err;
2126 static const struct file_operations proc_fdinfo_file_operations = {
2127 .open = nonseekable_open,
2128 .read = proc_fdinfo_read,
2129 .llseek = no_llseek,
2132 static const struct file_operations proc_fd_operations = {
2133 .read = generic_read_dir,
2134 .readdir = proc_readfd,
2135 .llseek = default_llseek,
2139 * /proc/pid/fd needs a special permission handler so that a process can still
2140 * access /proc/self/fd after it has executed a setuid().
2142 static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
2144 int rv = generic_permission(inode, mask, flags, NULL);
2145 if (rv == 0)
2146 return 0;
2147 if (task_pid(current) == proc_pid(inode))
2148 rv = 0;
2149 return rv;
2153 * proc directories can do almost nothing..
2155 static const struct inode_operations proc_fd_inode_operations = {
2156 .lookup = proc_lookupfd,
2157 .permission = proc_fd_permission,
2158 .setattr = proc_setattr,
2161 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2162 struct dentry *dentry, struct task_struct *task, const void *ptr)
2164 unsigned fd = *(unsigned *)ptr;
2165 struct inode *inode;
2166 struct proc_inode *ei;
2167 struct dentry *error = ERR_PTR(-ENOENT);
2169 inode = proc_pid_make_inode(dir->i_sb, task);
2170 if (!inode)
2171 goto out;
2172 ei = PROC_I(inode);
2173 ei->fd = fd;
2174 inode->i_mode = S_IFREG | S_IRUSR;
2175 inode->i_fop = &proc_fdinfo_file_operations;
2176 d_set_d_op(dentry, &tid_fd_dentry_operations);
2177 d_add(dentry, inode);
2178 /* Close the race of the process dying before we return the dentry */
2179 if (tid_fd_revalidate(dentry, NULL))
2180 error = NULL;
2182 out:
2183 return error;
2186 static struct dentry *proc_lookupfdinfo(struct inode *dir,
2187 struct dentry *dentry,
2188 struct nameidata *nd)
2190 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2193 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2195 return proc_readfd_common(filp, dirent, filldir,
2196 proc_fdinfo_instantiate);
2199 static const struct file_operations proc_fdinfo_operations = {
2200 .read = generic_read_dir,
2201 .readdir = proc_readfdinfo,
2202 .llseek = default_llseek,
2206 * proc directories can do almost nothing..
2208 static const struct inode_operations proc_fdinfo_inode_operations = {
2209 .lookup = proc_lookupfdinfo,
2210 .setattr = proc_setattr,
2214 static struct dentry *proc_pident_instantiate(struct inode *dir,
2215 struct dentry *dentry, struct task_struct *task, const void *ptr)
2217 const struct pid_entry *p = ptr;
2218 struct inode *inode;
2219 struct proc_inode *ei;
2220 struct dentry *error = ERR_PTR(-ENOENT);
2222 inode = proc_pid_make_inode(dir->i_sb, task);
2223 if (!inode)
2224 goto out;
2226 ei = PROC_I(inode);
2227 inode->i_mode = p->mode;
2228 if (S_ISDIR(inode->i_mode))
2229 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2230 if (p->iop)
2231 inode->i_op = p->iop;
2232 if (p->fop)
2233 inode->i_fop = p->fop;
2234 ei->op = p->op;
2235 d_set_d_op(dentry, &pid_dentry_operations);
2236 d_add(dentry, inode);
2237 /* Close the race of the process dying before we return the dentry */
2238 if (pid_revalidate(dentry, NULL))
2239 error = NULL;
2240 out:
2241 return error;
2244 static struct dentry *proc_pident_lookup(struct inode *dir,
2245 struct dentry *dentry,
2246 const struct pid_entry *ents,
2247 unsigned int nents)
2249 struct dentry *error;
2250 struct task_struct *task = get_proc_task(dir);
2251 const struct pid_entry *p, *last;
2253 error = ERR_PTR(-ENOENT);
2255 if (!task)
2256 goto out_no_task;
2259 * Yes, it does not scale. And it should not. Don't add
2260 * new entries into /proc/<tgid>/ without very good reasons.
2262 last = &ents[nents - 1];
2263 for (p = ents; p <= last; p++) {
2264 if (p->len != dentry->d_name.len)
2265 continue;
2266 if (!memcmp(dentry->d_name.name, p->name, p->len))
2267 break;
2269 if (p > last)
2270 goto out;
2272 error = proc_pident_instantiate(dir, dentry, task, p);
2273 out:
2274 put_task_struct(task);
2275 out_no_task:
2276 return error;
2279 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2280 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2282 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2283 proc_pident_instantiate, task, p);
2286 static int proc_pident_readdir(struct file *filp,
2287 void *dirent, filldir_t filldir,
2288 const struct pid_entry *ents, unsigned int nents)
2290 int i;
2291 struct dentry *dentry = filp->f_path.dentry;
2292 struct inode *inode = dentry->d_inode;
2293 struct task_struct *task = get_proc_task(inode);
2294 const struct pid_entry *p, *last;
2295 ino_t ino;
2296 int ret;
2298 ret = -ENOENT;
2299 if (!task)
2300 goto out_no_task;
2302 ret = 0;
2303 i = filp->f_pos;
2304 switch (i) {
2305 case 0:
2306 ino = inode->i_ino;
2307 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2308 goto out;
2309 i++;
2310 filp->f_pos++;
2311 /* fall through */
2312 case 1:
2313 ino = parent_ino(dentry);
2314 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2315 goto out;
2316 i++;
2317 filp->f_pos++;
2318 /* fall through */
2319 default:
2320 i -= 2;
2321 if (i >= nents) {
2322 ret = 1;
2323 goto out;
2325 p = ents + i;
2326 last = &ents[nents - 1];
2327 while (p <= last) {
2328 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2329 goto out;
2330 filp->f_pos++;
2331 p++;
2335 ret = 1;
2336 out:
2337 put_task_struct(task);
2338 out_no_task:
2339 return ret;
2342 #ifdef CONFIG_SECURITY
2343 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2344 size_t count, loff_t *ppos)
2346 struct inode * inode = file->f_path.dentry->d_inode;
2347 char *p = NULL;
2348 ssize_t length;
2349 struct task_struct *task = get_proc_task(inode);
2351 if (!task)
2352 return -ESRCH;
2354 length = security_getprocattr(task,
2355 (char*)file->f_path.dentry->d_name.name,
2356 &p);
2357 put_task_struct(task);
2358 if (length > 0)
2359 length = simple_read_from_buffer(buf, count, ppos, p, length);
2360 kfree(p);
2361 return length;
2364 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2365 size_t count, loff_t *ppos)
2367 struct inode * inode = file->f_path.dentry->d_inode;
2368 char *page;
2369 ssize_t length;
2370 struct task_struct *task = get_proc_task(inode);
2372 length = -ESRCH;
2373 if (!task)
2374 goto out_no_task;
2375 if (count > PAGE_SIZE)
2376 count = PAGE_SIZE;
2378 /* No partial writes. */
2379 length = -EINVAL;
2380 if (*ppos != 0)
2381 goto out;
2383 length = -ENOMEM;
2384 page = (char*)__get_free_page(GFP_TEMPORARY);
2385 if (!page)
2386 goto out;
2388 length = -EFAULT;
2389 if (copy_from_user(page, buf, count))
2390 goto out_free;
2392 /* Guard against adverse ptrace interaction */
2393 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2394 if (length < 0)
2395 goto out_free;
2397 length = security_setprocattr(task,
2398 (char*)file->f_path.dentry->d_name.name,
2399 (void*)page, count);
2400 mutex_unlock(&task->signal->cred_guard_mutex);
2401 out_free:
2402 free_page((unsigned long) page);
2403 out:
2404 put_task_struct(task);
2405 out_no_task:
2406 return length;
2409 static const struct file_operations proc_pid_attr_operations = {
2410 .read = proc_pid_attr_read,
2411 .write = proc_pid_attr_write,
2412 .llseek = generic_file_llseek,
2415 static const struct pid_entry attr_dir_stuff[] = {
2416 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2417 REG("prev", S_IRUGO, proc_pid_attr_operations),
2418 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2419 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2420 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2421 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2424 static int proc_attr_dir_readdir(struct file * filp,
2425 void * dirent, filldir_t filldir)
2427 return proc_pident_readdir(filp,dirent,filldir,
2428 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2431 static const struct file_operations proc_attr_dir_operations = {
2432 .read = generic_read_dir,
2433 .readdir = proc_attr_dir_readdir,
2434 .llseek = default_llseek,
2437 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2438 struct dentry *dentry, struct nameidata *nd)
2440 return proc_pident_lookup(dir, dentry,
2441 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2444 static const struct inode_operations proc_attr_dir_inode_operations = {
2445 .lookup = proc_attr_dir_lookup,
2446 .getattr = pid_getattr,
2447 .setattr = proc_setattr,
2450 #endif
2452 #ifdef CONFIG_ELF_CORE
2453 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2454 size_t count, loff_t *ppos)
2456 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2457 struct mm_struct *mm;
2458 char buffer[PROC_NUMBUF];
2459 size_t len;
2460 int ret;
2462 if (!task)
2463 return -ESRCH;
2465 ret = 0;
2466 mm = get_task_mm(task);
2467 if (mm) {
2468 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2469 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2470 MMF_DUMP_FILTER_SHIFT));
2471 mmput(mm);
2472 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2475 put_task_struct(task);
2477 return ret;
2480 static ssize_t proc_coredump_filter_write(struct file *file,
2481 const char __user *buf,
2482 size_t count,
2483 loff_t *ppos)
2485 struct task_struct *task;
2486 struct mm_struct *mm;
2487 char buffer[PROC_NUMBUF], *end;
2488 unsigned int val;
2489 int ret;
2490 int i;
2491 unsigned long mask;
2493 ret = -EFAULT;
2494 memset(buffer, 0, sizeof(buffer));
2495 if (count > sizeof(buffer) - 1)
2496 count = sizeof(buffer) - 1;
2497 if (copy_from_user(buffer, buf, count))
2498 goto out_no_task;
2500 ret = -EINVAL;
2501 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2502 if (*end == '\n')
2503 end++;
2504 if (end - buffer == 0)
2505 goto out_no_task;
2507 ret = -ESRCH;
2508 task = get_proc_task(file->f_dentry->d_inode);
2509 if (!task)
2510 goto out_no_task;
2512 ret = end - buffer;
2513 mm = get_task_mm(task);
2514 if (!mm)
2515 goto out_no_mm;
2517 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2518 if (val & mask)
2519 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2520 else
2521 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2524 mmput(mm);
2525 out_no_mm:
2526 put_task_struct(task);
2527 out_no_task:
2528 return ret;
2531 static const struct file_operations proc_coredump_filter_operations = {
2532 .read = proc_coredump_filter_read,
2533 .write = proc_coredump_filter_write,
2534 .llseek = generic_file_llseek,
2536 #endif
2539 * /proc/self:
2541 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2542 int buflen)
2544 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2545 pid_t tgid = task_tgid_nr_ns(current, ns);
2546 char tmp[PROC_NUMBUF];
2547 if (!tgid)
2548 return -ENOENT;
2549 sprintf(tmp, "%d", tgid);
2550 return vfs_readlink(dentry,buffer,buflen,tmp);
2553 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2555 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2556 pid_t tgid = task_tgid_nr_ns(current, ns);
2557 char *name = ERR_PTR(-ENOENT);
2558 if (tgid) {
2559 name = __getname();
2560 if (!name)
2561 name = ERR_PTR(-ENOMEM);
2562 else
2563 sprintf(name, "%d", tgid);
2565 nd_set_link(nd, name);
2566 return NULL;
2569 static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2570 void *cookie)
2572 char *s = nd_get_link(nd);
2573 if (!IS_ERR(s))
2574 __putname(s);
2577 static const struct inode_operations proc_self_inode_operations = {
2578 .readlink = proc_self_readlink,
2579 .follow_link = proc_self_follow_link,
2580 .put_link = proc_self_put_link,
2584 * proc base
2586 * These are the directory entries in the root directory of /proc
2587 * that properly belong to the /proc filesystem, as they describe
2588 * describe something that is process related.
2590 static const struct pid_entry proc_base_stuff[] = {
2591 NOD("self", S_IFLNK|S_IRWXUGO,
2592 &proc_self_inode_operations, NULL, {}),
2595 static struct dentry *proc_base_instantiate(struct inode *dir,
2596 struct dentry *dentry, struct task_struct *task, const void *ptr)
2598 const struct pid_entry *p = ptr;
2599 struct inode *inode;
2600 struct proc_inode *ei;
2601 struct dentry *error;
2603 /* Allocate the inode */
2604 error = ERR_PTR(-ENOMEM);
2605 inode = new_inode(dir->i_sb);
2606 if (!inode)
2607 goto out;
2609 /* Initialize the inode */
2610 ei = PROC_I(inode);
2611 inode->i_ino = get_next_ino();
2612 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2615 * grab the reference to the task.
2617 ei->pid = get_task_pid(task, PIDTYPE_PID);
2618 if (!ei->pid)
2619 goto out_iput;
2621 inode->i_mode = p->mode;
2622 if (S_ISDIR(inode->i_mode))
2623 inode->i_nlink = 2;
2624 if (S_ISLNK(inode->i_mode))
2625 inode->i_size = 64;
2626 if (p->iop)
2627 inode->i_op = p->iop;
2628 if (p->fop)
2629 inode->i_fop = p->fop;
2630 ei->op = p->op;
2631 d_add(dentry, inode);
2632 error = NULL;
2633 out:
2634 return error;
2635 out_iput:
2636 iput(inode);
2637 goto out;
2640 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2642 struct dentry *error;
2643 struct task_struct *task = get_proc_task(dir);
2644 const struct pid_entry *p, *last;
2646 error = ERR_PTR(-ENOENT);
2648 if (!task)
2649 goto out_no_task;
2651 /* Lookup the directory entry */
2652 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2653 for (p = proc_base_stuff; p <= last; p++) {
2654 if (p->len != dentry->d_name.len)
2655 continue;
2656 if (!memcmp(dentry->d_name.name, p->name, p->len))
2657 break;
2659 if (p > last)
2660 goto out;
2662 error = proc_base_instantiate(dir, dentry, task, p);
2664 out:
2665 put_task_struct(task);
2666 out_no_task:
2667 return error;
2670 static int proc_base_fill_cache(struct file *filp, void *dirent,
2671 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2673 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2674 proc_base_instantiate, task, p);
2677 #ifdef CONFIG_TASK_IO_ACCOUNTING
2678 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2680 struct task_io_accounting acct = task->ioac;
2681 unsigned long flags;
2682 int result;
2684 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2685 if (result)
2686 return result;
2688 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2689 result = -EACCES;
2690 goto out_unlock;
2693 if (whole && lock_task_sighand(task, &flags)) {
2694 struct task_struct *t = task;
2696 task_io_accounting_add(&acct, &task->signal->ioac);
2697 while_each_thread(task, t)
2698 task_io_accounting_add(&acct, &t->ioac);
2700 unlock_task_sighand(task, &flags);
2702 result = sprintf(buffer,
2703 "rchar: %llu\n"
2704 "wchar: %llu\n"
2705 "syscr: %llu\n"
2706 "syscw: %llu\n"
2707 "read_bytes: %llu\n"
2708 "write_bytes: %llu\n"
2709 "cancelled_write_bytes: %llu\n",
2710 (unsigned long long)acct.rchar,
2711 (unsigned long long)acct.wchar,
2712 (unsigned long long)acct.syscr,
2713 (unsigned long long)acct.syscw,
2714 (unsigned long long)acct.read_bytes,
2715 (unsigned long long)acct.write_bytes,
2716 (unsigned long long)acct.cancelled_write_bytes);
2717 out_unlock:
2718 mutex_unlock(&task->signal->cred_guard_mutex);
2719 return result;
2722 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2724 return do_io_accounting(task, buffer, 0);
2727 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2729 return do_io_accounting(task, buffer, 1);
2731 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2733 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2734 struct pid *pid, struct task_struct *task)
2736 int err = lock_trace(task);
2737 if (!err) {
2738 seq_printf(m, "%08x\n", task->personality);
2739 unlock_trace(task);
2741 return err;
2745 * Thread groups
2747 static const struct file_operations proc_task_operations;
2748 static const struct inode_operations proc_task_inode_operations;
2750 static const struct pid_entry tgid_base_stuff[] = {
2751 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2752 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2753 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2754 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2755 #ifdef CONFIG_NET
2756 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2757 #endif
2758 REG("environ", S_IRUSR, proc_environ_operations),
2759 INF("auxv", S_IRUSR, proc_pid_auxv),
2760 ONE("status", S_IRUGO, proc_pid_status),
2761 ONE("personality", S_IRUGO, proc_pid_personality),
2762 INF("limits", S_IRUGO, proc_pid_limits),
2763 #ifdef CONFIG_SCHED_DEBUG
2764 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2765 #endif
2766 #ifdef CONFIG_SCHED_AUTOGROUP
2767 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2768 #endif
2769 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2770 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2771 INF("syscall", S_IRUGO, proc_pid_syscall),
2772 #endif
2773 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2774 ONE("stat", S_IRUGO, proc_tgid_stat),
2775 ONE("statm", S_IRUGO, proc_pid_statm),
2776 REG("maps", S_IRUGO, proc_maps_operations),
2777 #ifdef CONFIG_NUMA
2778 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
2779 #endif
2780 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2781 LNK("cwd", proc_cwd_link),
2782 LNK("root", proc_root_link),
2783 LNK("exe", proc_exe_link),
2784 REG("mounts", S_IRUGO, proc_mounts_operations),
2785 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2786 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2787 #ifdef CONFIG_PROC_PAGE_MONITOR
2788 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2789 REG("smaps", S_IRUGO, proc_smaps_operations),
2790 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2791 #endif
2792 #ifdef CONFIG_SECURITY
2793 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2794 #endif
2795 #ifdef CONFIG_KALLSYMS
2796 INF("wchan", S_IRUGO, proc_pid_wchan),
2797 #endif
2798 #ifdef CONFIG_STACKTRACE
2799 ONE("stack", S_IRUGO, proc_pid_stack),
2800 #endif
2801 #ifdef CONFIG_SCHEDSTATS
2802 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2803 #endif
2804 #ifdef CONFIG_LATENCYTOP
2805 REG("latency", S_IRUGO, proc_lstats_operations),
2806 #endif
2807 #ifdef CONFIG_PROC_PID_CPUSET
2808 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2809 #endif
2810 #ifdef CONFIG_CGROUPS
2811 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2812 #endif
2813 INF("oom_score", S_IRUGO, proc_oom_score),
2814 ANDROID("oom_adj",S_IRUGO|S_IWUSR, oom_adjust),
2815 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2816 #ifdef CONFIG_AUDITSYSCALL
2817 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2818 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2819 #endif
2820 #ifdef CONFIG_FAULT_INJECTION
2821 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2822 #endif
2823 #ifdef CONFIG_ELF_CORE
2824 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2825 #endif
2826 #ifdef CONFIG_TASK_IO_ACCOUNTING
2827 INF("io", S_IRUSR, proc_tgid_io_accounting),
2828 #endif
2829 #ifdef CONFIG_HARDWALL
2830 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2831 #endif
2834 static int proc_tgid_base_readdir(struct file * filp,
2835 void * dirent, filldir_t filldir)
2837 return proc_pident_readdir(filp,dirent,filldir,
2838 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2841 static const struct file_operations proc_tgid_base_operations = {
2842 .read = generic_read_dir,
2843 .readdir = proc_tgid_base_readdir,
2844 .llseek = default_llseek,
2847 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2848 return proc_pident_lookup(dir, dentry,
2849 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2852 static const struct inode_operations proc_tgid_base_inode_operations = {
2853 .lookup = proc_tgid_base_lookup,
2854 .getattr = pid_getattr,
2855 .setattr = proc_setattr,
2858 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2860 struct dentry *dentry, *leader, *dir;
2861 char buf[PROC_NUMBUF];
2862 struct qstr name;
2864 name.name = buf;
2865 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2866 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2867 if (dentry) {
2868 shrink_dcache_parent(dentry);
2869 d_drop(dentry);
2870 dput(dentry);
2873 name.name = buf;
2874 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2875 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2876 if (!leader)
2877 goto out;
2879 name.name = "task";
2880 name.len = strlen(name.name);
2881 dir = d_hash_and_lookup(leader, &name);
2882 if (!dir)
2883 goto out_put_leader;
2885 name.name = buf;
2886 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2887 dentry = d_hash_and_lookup(dir, &name);
2888 if (dentry) {
2889 shrink_dcache_parent(dentry);
2890 d_drop(dentry);
2891 dput(dentry);
2894 dput(dir);
2895 out_put_leader:
2896 dput(leader);
2897 out:
2898 return;
2902 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2903 * @task: task that should be flushed.
2905 * When flushing dentries from proc, one needs to flush them from global
2906 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2907 * in. This call is supposed to do all of this job.
2909 * Looks in the dcache for
2910 * /proc/@pid
2911 * /proc/@tgid/task/@pid
2912 * if either directory is present flushes it and all of it'ts children
2913 * from the dcache.
2915 * It is safe and reasonable to cache /proc entries for a task until
2916 * that task exits. After that they just clog up the dcache with
2917 * useless entries, possibly causing useful dcache entries to be
2918 * flushed instead. This routine is proved to flush those useless
2919 * dcache entries at process exit time.
2921 * NOTE: This routine is just an optimization so it does not guarantee
2922 * that no dcache entries will exist at process exit time it
2923 * just makes it very unlikely that any will persist.
2926 void proc_flush_task(struct task_struct *task)
2928 int i;
2929 struct pid *pid, *tgid;
2930 struct upid *upid;
2932 pid = task_pid(task);
2933 tgid = task_tgid(task);
2935 for (i = 0; i <= pid->level; i++) {
2936 upid = &pid->numbers[i];
2937 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2938 tgid->numbers[i].nr);
2941 upid = &pid->numbers[pid->level];
2942 if (upid->nr == 1)
2943 pid_ns_release_proc(upid->ns);
2946 static struct dentry *proc_pid_instantiate(struct inode *dir,
2947 struct dentry * dentry,
2948 struct task_struct *task, const void *ptr)
2950 struct dentry *error = ERR_PTR(-ENOENT);
2951 struct inode *inode;
2953 inode = proc_pid_make_inode(dir->i_sb, task);
2954 if (!inode)
2955 goto out;
2957 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2958 inode->i_op = &proc_tgid_base_inode_operations;
2959 inode->i_fop = &proc_tgid_base_operations;
2960 inode->i_flags|=S_IMMUTABLE;
2962 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2963 ARRAY_SIZE(tgid_base_stuff));
2965 d_set_d_op(dentry, &pid_dentry_operations);
2967 d_add(dentry, inode);
2968 /* Close the race of the process dying before we return the dentry */
2969 if (pid_revalidate(dentry, NULL))
2970 error = NULL;
2971 out:
2972 return error;
2975 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2977 struct dentry *result;
2978 struct task_struct *task;
2979 unsigned tgid;
2980 struct pid_namespace *ns;
2982 result = proc_base_lookup(dir, dentry);
2983 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2984 goto out;
2986 tgid = name_to_int(dentry);
2987 if (tgid == ~0U)
2988 goto out;
2990 ns = dentry->d_sb->s_fs_info;
2991 rcu_read_lock();
2992 task = find_task_by_pid_ns(tgid, ns);
2993 if (task)
2994 get_task_struct(task);
2995 rcu_read_unlock();
2996 if (!task)
2997 goto out;
2999 result = proc_pid_instantiate(dir, dentry, task, NULL);
3000 put_task_struct(task);
3001 out:
3002 return result;
3006 * Find the first task with tgid >= tgid
3009 struct tgid_iter {
3010 unsigned int tgid;
3011 struct task_struct *task;
3013 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3015 struct pid *pid;
3017 if (iter.task)
3018 put_task_struct(iter.task);
3019 rcu_read_lock();
3020 retry:
3021 iter.task = NULL;
3022 pid = find_ge_pid(iter.tgid, ns);
3023 if (pid) {
3024 iter.tgid = pid_nr_ns(pid, ns);
3025 iter.task = pid_task(pid, PIDTYPE_PID);
3026 /* What we to know is if the pid we have find is the
3027 * pid of a thread_group_leader. Testing for task
3028 * being a thread_group_leader is the obvious thing
3029 * todo but there is a window when it fails, due to
3030 * the pid transfer logic in de_thread.
3032 * So we perform the straight forward test of seeing
3033 * if the pid we have found is the pid of a thread
3034 * group leader, and don't worry if the task we have
3035 * found doesn't happen to be a thread group leader.
3036 * As we don't care in the case of readdir.
3038 if (!iter.task || !has_group_leader_pid(iter.task)) {
3039 iter.tgid += 1;
3040 goto retry;
3042 get_task_struct(iter.task);
3044 rcu_read_unlock();
3045 return iter;
3048 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
3050 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3051 struct tgid_iter iter)
3053 char name[PROC_NUMBUF];
3054 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
3055 return proc_fill_cache(filp, dirent, filldir, name, len,
3056 proc_pid_instantiate, iter.task, NULL);
3059 /* for the /proc/ directory itself, after non-process stuff has been done */
3060 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3062 unsigned int nr;
3063 struct task_struct *reaper;
3064 struct tgid_iter iter;
3065 struct pid_namespace *ns;
3067 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
3068 goto out_no_task;
3069 nr = filp->f_pos - FIRST_PROCESS_ENTRY;
3071 reaper = get_proc_task(filp->f_path.dentry->d_inode);
3072 if (!reaper)
3073 goto out_no_task;
3075 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
3076 const struct pid_entry *p = &proc_base_stuff[nr];
3077 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
3078 goto out;
3081 ns = filp->f_dentry->d_sb->s_fs_info;
3082 iter.task = NULL;
3083 iter.tgid = filp->f_pos - TGID_OFFSET;
3084 for (iter = next_tgid(ns, iter);
3085 iter.task;
3086 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3087 filp->f_pos = iter.tgid + TGID_OFFSET;
3088 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3089 put_task_struct(iter.task);
3090 goto out;
3093 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3094 out:
3095 put_task_struct(reaper);
3096 out_no_task:
3097 return 0;
3101 * Tasks
3103 static const struct pid_entry tid_base_stuff[] = {
3104 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3105 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
3106 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
3107 REG("environ", S_IRUSR, proc_environ_operations),
3108 INF("auxv", S_IRUSR, proc_pid_auxv),
3109 ONE("status", S_IRUGO, proc_pid_status),
3110 ONE("personality", S_IRUGO, proc_pid_personality),
3111 INF("limits", S_IRUGO, proc_pid_limits),
3112 #ifdef CONFIG_SCHED_DEBUG
3113 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
3114 #endif
3115 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
3116 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3117 INF("syscall", S_IRUGO, proc_pid_syscall),
3118 #endif
3119 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3120 ONE("stat", S_IRUGO, proc_tid_stat),
3121 ONE("statm", S_IRUGO, proc_pid_statm),
3122 REG("maps", S_IRUGO, proc_maps_operations),
3123 #ifdef CONFIG_NUMA
3124 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
3125 #endif
3126 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3127 LNK("cwd", proc_cwd_link),
3128 LNK("root", proc_root_link),
3129 LNK("exe", proc_exe_link),
3130 REG("mounts", S_IRUGO, proc_mounts_operations),
3131 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
3132 #ifdef CONFIG_PROC_PAGE_MONITOR
3133 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3134 REG("smaps", S_IRUGO, proc_smaps_operations),
3135 REG("pagemap", S_IRUGO, proc_pagemap_operations),
3136 #endif
3137 #ifdef CONFIG_SECURITY
3138 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
3139 #endif
3140 #ifdef CONFIG_KALLSYMS
3141 INF("wchan", S_IRUGO, proc_pid_wchan),
3142 #endif
3143 #ifdef CONFIG_STACKTRACE
3144 ONE("stack", S_IRUGO, proc_pid_stack),
3145 #endif
3146 #ifdef CONFIG_SCHEDSTATS
3147 INF("schedstat", S_IRUGO, proc_pid_schedstat),
3148 #endif
3149 #ifdef CONFIG_LATENCYTOP
3150 REG("latency", S_IRUGO, proc_lstats_operations),
3151 #endif
3152 #ifdef CONFIG_PROC_PID_CPUSET
3153 REG("cpuset", S_IRUGO, proc_cpuset_operations),
3154 #endif
3155 #ifdef CONFIG_CGROUPS
3156 REG("cgroup", S_IRUGO, proc_cgroup_operations),
3157 #endif
3158 INF("oom_score", S_IRUGO, proc_oom_score),
3159 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
3160 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
3161 #ifdef CONFIG_AUDITSYSCALL
3162 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3163 REG("sessionid", S_IRUGO, proc_sessionid_operations),
3164 #endif
3165 #ifdef CONFIG_FAULT_INJECTION
3166 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
3167 #endif
3168 #ifdef CONFIG_TASK_IO_ACCOUNTING
3169 INF("io", S_IRUSR, proc_tid_io_accounting),
3170 #endif
3171 #ifdef CONFIG_HARDWALL
3172 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3173 #endif
3176 static int proc_tid_base_readdir(struct file * filp,
3177 void * dirent, filldir_t filldir)
3179 return proc_pident_readdir(filp,dirent,filldir,
3180 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3183 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
3184 return proc_pident_lookup(dir, dentry,
3185 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
3188 static const struct file_operations proc_tid_base_operations = {
3189 .read = generic_read_dir,
3190 .readdir = proc_tid_base_readdir,
3191 .llseek = default_llseek,
3194 static const struct inode_operations proc_tid_base_inode_operations = {
3195 .lookup = proc_tid_base_lookup,
3196 .getattr = pid_getattr,
3197 .setattr = proc_setattr,
3200 static struct dentry *proc_task_instantiate(struct inode *dir,
3201 struct dentry *dentry, struct task_struct *task, const void *ptr)
3203 struct dentry *error = ERR_PTR(-ENOENT);
3204 struct inode *inode;
3205 inode = proc_pid_make_inode(dir->i_sb, task);
3207 if (!inode)
3208 goto out;
3209 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3210 inode->i_op = &proc_tid_base_inode_operations;
3211 inode->i_fop = &proc_tid_base_operations;
3212 inode->i_flags|=S_IMMUTABLE;
3214 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3215 ARRAY_SIZE(tid_base_stuff));
3217 d_set_d_op(dentry, &pid_dentry_operations);
3219 d_add(dentry, inode);
3220 /* Close the race of the process dying before we return the dentry */
3221 if (pid_revalidate(dentry, NULL))
3222 error = NULL;
3223 out:
3224 return error;
3227 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3229 struct dentry *result = ERR_PTR(-ENOENT);
3230 struct task_struct *task;
3231 struct task_struct *leader = get_proc_task(dir);
3232 unsigned tid;
3233 struct pid_namespace *ns;
3235 if (!leader)
3236 goto out_no_task;
3238 tid = name_to_int(dentry);
3239 if (tid == ~0U)
3240 goto out;
3242 ns = dentry->d_sb->s_fs_info;
3243 rcu_read_lock();
3244 task = find_task_by_pid_ns(tid, ns);
3245 if (task)
3246 get_task_struct(task);
3247 rcu_read_unlock();
3248 if (!task)
3249 goto out;
3250 if (!same_thread_group(leader, task))
3251 goto out_drop_task;
3253 result = proc_task_instantiate(dir, dentry, task, NULL);
3254 out_drop_task:
3255 put_task_struct(task);
3256 out:
3257 put_task_struct(leader);
3258 out_no_task:
3259 return result;
3263 * Find the first tid of a thread group to return to user space.
3265 * Usually this is just the thread group leader, but if the users
3266 * buffer was too small or there was a seek into the middle of the
3267 * directory we have more work todo.
3269 * In the case of a short read we start with find_task_by_pid.
3271 * In the case of a seek we start with the leader and walk nr
3272 * threads past it.
3274 static struct task_struct *first_tid(struct task_struct *leader,
3275 int tid, int nr, struct pid_namespace *ns)
3277 struct task_struct *pos;
3279 rcu_read_lock();
3280 /* Attempt to start with the pid of a thread */
3281 if (tid && (nr > 0)) {
3282 pos = find_task_by_pid_ns(tid, ns);
3283 if (pos && (pos->group_leader == leader))
3284 goto found;
3287 /* If nr exceeds the number of threads there is nothing todo */
3288 pos = NULL;
3289 if (nr && nr >= get_nr_threads(leader))
3290 goto out;
3292 /* If we haven't found our starting place yet start
3293 * with the leader and walk nr threads forward.
3295 for (pos = leader; nr > 0; --nr) {
3296 pos = next_thread(pos);
3297 if (pos == leader) {
3298 pos = NULL;
3299 goto out;
3302 found:
3303 get_task_struct(pos);
3304 out:
3305 rcu_read_unlock();
3306 return pos;
3310 * Find the next thread in the thread list.
3311 * Return NULL if there is an error or no next thread.
3313 * The reference to the input task_struct is released.
3315 static struct task_struct *next_tid(struct task_struct *start)
3317 struct task_struct *pos = NULL;
3318 rcu_read_lock();
3319 if (pid_alive(start)) {
3320 pos = next_thread(start);
3321 if (thread_group_leader(pos))
3322 pos = NULL;
3323 else
3324 get_task_struct(pos);
3326 rcu_read_unlock();
3327 put_task_struct(start);
3328 return pos;
3331 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3332 struct task_struct *task, int tid)
3334 char name[PROC_NUMBUF];
3335 int len = snprintf(name, sizeof(name), "%d", tid);
3336 return proc_fill_cache(filp, dirent, filldir, name, len,
3337 proc_task_instantiate, task, NULL);
3340 /* for the /proc/TGID/task/ directories */
3341 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3343 struct dentry *dentry = filp->f_path.dentry;
3344 struct inode *inode = dentry->d_inode;
3345 struct task_struct *leader = NULL;
3346 struct task_struct *task;
3347 int retval = -ENOENT;
3348 ino_t ino;
3349 int tid;
3350 struct pid_namespace *ns;
3352 task = get_proc_task(inode);
3353 if (!task)
3354 goto out_no_task;
3355 rcu_read_lock();
3356 if (pid_alive(task)) {
3357 leader = task->group_leader;
3358 get_task_struct(leader);
3360 rcu_read_unlock();
3361 put_task_struct(task);
3362 if (!leader)
3363 goto out_no_task;
3364 retval = 0;
3366 switch ((unsigned long)filp->f_pos) {
3367 case 0:
3368 ino = inode->i_ino;
3369 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3370 goto out;
3371 filp->f_pos++;
3372 /* fall through */
3373 case 1:
3374 ino = parent_ino(dentry);
3375 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3376 goto out;
3377 filp->f_pos++;
3378 /* fall through */
3381 /* f_version caches the tgid value that the last readdir call couldn't
3382 * return. lseek aka telldir automagically resets f_version to 0.
3384 ns = filp->f_dentry->d_sb->s_fs_info;
3385 tid = (int)filp->f_version;
3386 filp->f_version = 0;
3387 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3388 task;
3389 task = next_tid(task), filp->f_pos++) {
3390 tid = task_pid_nr_ns(task, ns);
3391 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3392 /* returning this tgid failed, save it as the first
3393 * pid for the next readir call */
3394 filp->f_version = (u64)tid;
3395 put_task_struct(task);
3396 break;
3399 out:
3400 put_task_struct(leader);
3401 out_no_task:
3402 return retval;
3405 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3407 struct inode *inode = dentry->d_inode;
3408 struct task_struct *p = get_proc_task(inode);
3409 generic_fillattr(inode, stat);
3411 if (p) {
3412 stat->nlink += get_nr_threads(p);
3413 put_task_struct(p);
3416 return 0;
3419 static const struct inode_operations proc_task_inode_operations = {
3420 .lookup = proc_task_lookup,
3421 .getattr = proc_task_getattr,
3422 .setattr = proc_setattr,
3425 static const struct file_operations proc_task_operations = {
3426 .read = generic_read_dir,
3427 .readdir = proc_task_readdir,
3428 .llseek = default_llseek,