Linux 4.1.16
[linux/fpc-iii.git] / fs / proc / base.c
blobfcdeb1eb39211974b51565202750643bc8f24bc4
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/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
92 #endif
93 #include <trace/events/oom.h>
94 #include "internal.h"
95 #include "fd.h"
97 /* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
107 struct pid_entry {
108 const char *name;
109 int len;
110 umode_t mode;
111 const struct inode_operations *iop;
112 const struct file_operations *fop;
113 union proc_op op;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
117 .name = (NAME), \
118 .len = sizeof(NAME) - 1, \
119 .mode = MODE, \
120 .iop = IOP, \
121 .fop = FOP, \
122 .op = OP, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define ONE(NAME, MODE, show) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
139 * Count the number of hardlinks for the pid_entry table, excluding the .
140 * and .. links.
142 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
143 unsigned int n)
145 unsigned int i;
146 unsigned int count;
148 count = 0;
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
151 ++count;
154 return count;
157 static int get_task_root(struct task_struct *task, struct path *root)
159 int result = -ENOENT;
161 task_lock(task);
162 if (task->fs) {
163 get_fs_root(task->fs, root);
164 result = 0;
166 task_unlock(task);
167 return result;
170 static int proc_cwd_link(struct dentry *dentry, struct path *path)
172 struct task_struct *task = get_proc_task(d_inode(dentry));
173 int result = -ENOENT;
175 if (task) {
176 task_lock(task);
177 if (task->fs) {
178 get_fs_pwd(task->fs, path);
179 result = 0;
181 task_unlock(task);
182 put_task_struct(task);
184 return result;
187 static int proc_root_link(struct dentry *dentry, struct path *path)
189 struct task_struct *task = get_proc_task(d_inode(dentry));
190 int result = -ENOENT;
192 if (task) {
193 result = get_task_root(task, path);
194 put_task_struct(task);
196 return result;
199 static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
200 struct pid *pid, struct task_struct *task)
203 * Rely on struct seq_operations::show() being called once
204 * per internal buffer allocation. See single_open(), traverse().
206 BUG_ON(m->size < PAGE_SIZE);
207 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
208 return 0;
211 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
212 struct pid *pid, struct task_struct *task)
214 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
215 if (mm && !IS_ERR(mm)) {
216 unsigned int nwords = 0;
217 do {
218 nwords += 2;
219 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
220 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
221 mmput(mm);
222 return 0;
223 } else
224 return PTR_ERR(mm);
228 #ifdef CONFIG_KALLSYMS
230 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
231 * Returns the resolved symbol. If that fails, simply return the address.
233 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
234 struct pid *pid, struct task_struct *task)
236 unsigned long wchan;
237 char symname[KSYM_NAME_LEN];
239 wchan = get_wchan(task);
241 if (wchan && ptrace_may_access(task, PTRACE_MODE_READ) && !lookup_symbol_name(wchan, symname))
242 seq_printf(m, "%s", symname);
243 else
244 seq_putc(m, '0');
246 return 0;
248 #endif /* CONFIG_KALLSYMS */
250 static int lock_trace(struct task_struct *task)
252 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
253 if (err)
254 return err;
255 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
256 mutex_unlock(&task->signal->cred_guard_mutex);
257 return -EPERM;
259 return 0;
262 static void unlock_trace(struct task_struct *task)
264 mutex_unlock(&task->signal->cred_guard_mutex);
267 #ifdef CONFIG_STACKTRACE
269 #define MAX_STACK_TRACE_DEPTH 64
271 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
272 struct pid *pid, struct task_struct *task)
274 struct stack_trace trace;
275 unsigned long *entries;
276 int err;
277 int i;
279 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
280 if (!entries)
281 return -ENOMEM;
283 trace.nr_entries = 0;
284 trace.max_entries = MAX_STACK_TRACE_DEPTH;
285 trace.entries = entries;
286 trace.skip = 0;
288 err = lock_trace(task);
289 if (!err) {
290 save_stack_trace_tsk(task, &trace);
292 for (i = 0; i < trace.nr_entries; i++) {
293 seq_printf(m, "[<%pK>] %pS\n",
294 (void *)entries[i], (void *)entries[i]);
296 unlock_trace(task);
298 kfree(entries);
300 return err;
302 #endif
304 #ifdef CONFIG_SCHEDSTATS
306 * Provides /proc/PID/schedstat
308 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
309 struct pid *pid, struct task_struct *task)
311 seq_printf(m, "%llu %llu %lu\n",
312 (unsigned long long)task->se.sum_exec_runtime,
313 (unsigned long long)task->sched_info.run_delay,
314 task->sched_info.pcount);
316 return 0;
318 #endif
320 #ifdef CONFIG_LATENCYTOP
321 static int lstats_show_proc(struct seq_file *m, void *v)
323 int i;
324 struct inode *inode = m->private;
325 struct task_struct *task = get_proc_task(inode);
327 if (!task)
328 return -ESRCH;
329 seq_puts(m, "Latency Top version : v0.1\n");
330 for (i = 0; i < 32; i++) {
331 struct latency_record *lr = &task->latency_record[i];
332 if (lr->backtrace[0]) {
333 int q;
334 seq_printf(m, "%i %li %li",
335 lr->count, lr->time, lr->max);
336 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
337 unsigned long bt = lr->backtrace[q];
338 if (!bt)
339 break;
340 if (bt == ULONG_MAX)
341 break;
342 seq_printf(m, " %ps", (void *)bt);
344 seq_putc(m, '\n');
348 put_task_struct(task);
349 return 0;
352 static int lstats_open(struct inode *inode, struct file *file)
354 return single_open(file, lstats_show_proc, inode);
357 static ssize_t lstats_write(struct file *file, const char __user *buf,
358 size_t count, loff_t *offs)
360 struct task_struct *task = get_proc_task(file_inode(file));
362 if (!task)
363 return -ESRCH;
364 clear_all_latency_tracing(task);
365 put_task_struct(task);
367 return count;
370 static const struct file_operations proc_lstats_operations = {
371 .open = lstats_open,
372 .read = seq_read,
373 .write = lstats_write,
374 .llseek = seq_lseek,
375 .release = single_release,
378 #endif
380 static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
381 struct pid *pid, struct task_struct *task)
383 unsigned long totalpages = totalram_pages + total_swap_pages;
384 unsigned long points = 0;
386 read_lock(&tasklist_lock);
387 if (pid_alive(task))
388 points = oom_badness(task, NULL, NULL, totalpages) *
389 1000 / totalpages;
390 read_unlock(&tasklist_lock);
391 seq_printf(m, "%lu\n", points);
393 return 0;
396 struct limit_names {
397 const char *name;
398 const char *unit;
401 static const struct limit_names lnames[RLIM_NLIMITS] = {
402 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
403 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
404 [RLIMIT_DATA] = {"Max data size", "bytes"},
405 [RLIMIT_STACK] = {"Max stack size", "bytes"},
406 [RLIMIT_CORE] = {"Max core file size", "bytes"},
407 [RLIMIT_RSS] = {"Max resident set", "bytes"},
408 [RLIMIT_NPROC] = {"Max processes", "processes"},
409 [RLIMIT_NOFILE] = {"Max open files", "files"},
410 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
411 [RLIMIT_AS] = {"Max address space", "bytes"},
412 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
413 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
414 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
415 [RLIMIT_NICE] = {"Max nice priority", NULL},
416 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
417 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
420 /* Display limits for a process */
421 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
422 struct pid *pid, struct task_struct *task)
424 unsigned int i;
425 unsigned long flags;
427 struct rlimit rlim[RLIM_NLIMITS];
429 if (!lock_task_sighand(task, &flags))
430 return 0;
431 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
432 unlock_task_sighand(task, &flags);
435 * print the file header
437 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
438 "Limit", "Soft Limit", "Hard Limit", "Units");
440 for (i = 0; i < RLIM_NLIMITS; i++) {
441 if (rlim[i].rlim_cur == RLIM_INFINITY)
442 seq_printf(m, "%-25s %-20s ",
443 lnames[i].name, "unlimited");
444 else
445 seq_printf(m, "%-25s %-20lu ",
446 lnames[i].name, rlim[i].rlim_cur);
448 if (rlim[i].rlim_max == RLIM_INFINITY)
449 seq_printf(m, "%-20s ", "unlimited");
450 else
451 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
453 if (lnames[i].unit)
454 seq_printf(m, "%-10s\n", lnames[i].unit);
455 else
456 seq_putc(m, '\n');
459 return 0;
462 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
463 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
464 struct pid *pid, struct task_struct *task)
466 long nr;
467 unsigned long args[6], sp, pc;
468 int res;
470 res = lock_trace(task);
471 if (res)
472 return res;
474 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
475 seq_puts(m, "running\n");
476 else if (nr < 0)
477 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
478 else
479 seq_printf(m,
480 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
482 args[0], args[1], args[2], args[3], args[4], args[5],
483 sp, pc);
484 unlock_trace(task);
486 return 0;
488 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
490 /************************************************************************/
491 /* Here the fs part begins */
492 /************************************************************************/
494 /* permission checks */
495 static int proc_fd_access_allowed(struct inode *inode)
497 struct task_struct *task;
498 int allowed = 0;
499 /* Allow access to a task's file descriptors if it is us or we
500 * may use ptrace attach to the process and find out that
501 * information.
503 task = get_proc_task(inode);
504 if (task) {
505 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
506 put_task_struct(task);
508 return allowed;
511 int proc_setattr(struct dentry *dentry, struct iattr *attr)
513 int error;
514 struct inode *inode = d_inode(dentry);
516 if (attr->ia_valid & ATTR_MODE)
517 return -EPERM;
519 error = inode_change_ok(inode, attr);
520 if (error)
521 return error;
523 setattr_copy(inode, attr);
524 mark_inode_dirty(inode);
525 return 0;
529 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
530 * or euid/egid (for hide_pid_min=2)?
532 static bool has_pid_permissions(struct pid_namespace *pid,
533 struct task_struct *task,
534 int hide_pid_min)
536 if (pid->hide_pid < hide_pid_min)
537 return true;
538 if (in_group_p(pid->pid_gid))
539 return true;
540 return ptrace_may_access(task, PTRACE_MODE_READ);
544 static int proc_pid_permission(struct inode *inode, int mask)
546 struct pid_namespace *pid = inode->i_sb->s_fs_info;
547 struct task_struct *task;
548 bool has_perms;
550 task = get_proc_task(inode);
551 if (!task)
552 return -ESRCH;
553 has_perms = has_pid_permissions(pid, task, 1);
554 put_task_struct(task);
556 if (!has_perms) {
557 if (pid->hide_pid == 2) {
559 * Let's make getdents(), stat(), and open()
560 * consistent with each other. If a process
561 * may not stat() a file, it shouldn't be seen
562 * in procfs at all.
564 return -ENOENT;
567 return -EPERM;
569 return generic_permission(inode, mask);
574 static const struct inode_operations proc_def_inode_operations = {
575 .setattr = proc_setattr,
578 static int proc_single_show(struct seq_file *m, void *v)
580 struct inode *inode = m->private;
581 struct pid_namespace *ns;
582 struct pid *pid;
583 struct task_struct *task;
584 int ret;
586 ns = inode->i_sb->s_fs_info;
587 pid = proc_pid(inode);
588 task = get_pid_task(pid, PIDTYPE_PID);
589 if (!task)
590 return -ESRCH;
592 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
594 put_task_struct(task);
595 return ret;
598 static int proc_single_open(struct inode *inode, struct file *filp)
600 return single_open(filp, proc_single_show, inode);
603 static const struct file_operations proc_single_file_operations = {
604 .open = proc_single_open,
605 .read = seq_read,
606 .llseek = seq_lseek,
607 .release = single_release,
611 struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
613 struct task_struct *task = get_proc_task(inode);
614 struct mm_struct *mm = ERR_PTR(-ESRCH);
616 if (task) {
617 mm = mm_access(task, mode);
618 put_task_struct(task);
620 if (!IS_ERR_OR_NULL(mm)) {
621 /* ensure this mm_struct can't be freed */
622 atomic_inc(&mm->mm_count);
623 /* but do not pin its memory */
624 mmput(mm);
628 return mm;
631 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
633 struct mm_struct *mm = proc_mem_open(inode, mode);
635 if (IS_ERR(mm))
636 return PTR_ERR(mm);
638 file->private_data = mm;
639 return 0;
642 static int mem_open(struct inode *inode, struct file *file)
644 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
646 /* OK to pass negative loff_t, we can catch out-of-range */
647 file->f_mode |= FMODE_UNSIGNED_OFFSET;
649 return ret;
652 static ssize_t mem_rw(struct file *file, char __user *buf,
653 size_t count, loff_t *ppos, int write)
655 struct mm_struct *mm = file->private_data;
656 unsigned long addr = *ppos;
657 ssize_t copied;
658 char *page;
660 if (!mm)
661 return 0;
663 page = (char *)__get_free_page(GFP_TEMPORARY);
664 if (!page)
665 return -ENOMEM;
667 copied = 0;
668 if (!atomic_inc_not_zero(&mm->mm_users))
669 goto free;
671 while (count > 0) {
672 int this_len = min_t(int, count, PAGE_SIZE);
674 if (write && copy_from_user(page, buf, this_len)) {
675 copied = -EFAULT;
676 break;
679 this_len = access_remote_vm(mm, addr, page, this_len, write);
680 if (!this_len) {
681 if (!copied)
682 copied = -EIO;
683 break;
686 if (!write && copy_to_user(buf, page, this_len)) {
687 copied = -EFAULT;
688 break;
691 buf += this_len;
692 addr += this_len;
693 copied += this_len;
694 count -= this_len;
696 *ppos = addr;
698 mmput(mm);
699 free:
700 free_page((unsigned long) page);
701 return copied;
704 static ssize_t mem_read(struct file *file, char __user *buf,
705 size_t count, loff_t *ppos)
707 return mem_rw(file, buf, count, ppos, 0);
710 static ssize_t mem_write(struct file *file, const char __user *buf,
711 size_t count, loff_t *ppos)
713 return mem_rw(file, (char __user*)buf, count, ppos, 1);
716 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
718 switch (orig) {
719 case 0:
720 file->f_pos = offset;
721 break;
722 case 1:
723 file->f_pos += offset;
724 break;
725 default:
726 return -EINVAL;
728 force_successful_syscall_return();
729 return file->f_pos;
732 static int mem_release(struct inode *inode, struct file *file)
734 struct mm_struct *mm = file->private_data;
735 if (mm)
736 mmdrop(mm);
737 return 0;
740 static const struct file_operations proc_mem_operations = {
741 .llseek = mem_lseek,
742 .read = mem_read,
743 .write = mem_write,
744 .open = mem_open,
745 .release = mem_release,
748 static int environ_open(struct inode *inode, struct file *file)
750 return __mem_open(inode, file, PTRACE_MODE_READ);
753 static ssize_t environ_read(struct file *file, char __user *buf,
754 size_t count, loff_t *ppos)
756 char *page;
757 unsigned long src = *ppos;
758 int ret = 0;
759 struct mm_struct *mm = file->private_data;
761 if (!mm)
762 return 0;
764 page = (char *)__get_free_page(GFP_TEMPORARY);
765 if (!page)
766 return -ENOMEM;
768 ret = 0;
769 if (!atomic_inc_not_zero(&mm->mm_users))
770 goto free;
771 while (count > 0) {
772 size_t this_len, max_len;
773 int retval;
775 if (src >= (mm->env_end - mm->env_start))
776 break;
778 this_len = mm->env_end - (mm->env_start + src);
780 max_len = min_t(size_t, PAGE_SIZE, count);
781 this_len = min(max_len, this_len);
783 retval = access_remote_vm(mm, (mm->env_start + src),
784 page, this_len, 0);
786 if (retval <= 0) {
787 ret = retval;
788 break;
791 if (copy_to_user(buf, page, retval)) {
792 ret = -EFAULT;
793 break;
796 ret += retval;
797 src += retval;
798 buf += retval;
799 count -= retval;
801 *ppos = src;
802 mmput(mm);
804 free:
805 free_page((unsigned long) page);
806 return ret;
809 static const struct file_operations proc_environ_operations = {
810 .open = environ_open,
811 .read = environ_read,
812 .llseek = generic_file_llseek,
813 .release = mem_release,
816 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
817 loff_t *ppos)
819 struct task_struct *task = get_proc_task(file_inode(file));
820 char buffer[PROC_NUMBUF];
821 int oom_adj = OOM_ADJUST_MIN;
822 size_t len;
823 unsigned long flags;
825 if (!task)
826 return -ESRCH;
827 if (lock_task_sighand(task, &flags)) {
828 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
829 oom_adj = OOM_ADJUST_MAX;
830 else
831 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
832 OOM_SCORE_ADJ_MAX;
833 unlock_task_sighand(task, &flags);
835 put_task_struct(task);
836 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
837 return simple_read_from_buffer(buf, count, ppos, buffer, len);
840 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
841 size_t count, loff_t *ppos)
843 struct task_struct *task;
844 char buffer[PROC_NUMBUF];
845 int oom_adj;
846 unsigned long flags;
847 int err;
849 memset(buffer, 0, sizeof(buffer));
850 if (count > sizeof(buffer) - 1)
851 count = sizeof(buffer) - 1;
852 if (copy_from_user(buffer, buf, count)) {
853 err = -EFAULT;
854 goto out;
857 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
858 if (err)
859 goto out;
860 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
861 oom_adj != OOM_DISABLE) {
862 err = -EINVAL;
863 goto out;
866 task = get_proc_task(file_inode(file));
867 if (!task) {
868 err = -ESRCH;
869 goto out;
872 task_lock(task);
873 if (!task->mm) {
874 err = -EINVAL;
875 goto err_task_lock;
878 if (!lock_task_sighand(task, &flags)) {
879 err = -ESRCH;
880 goto err_task_lock;
884 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
885 * value is always attainable.
887 if (oom_adj == OOM_ADJUST_MAX)
888 oom_adj = OOM_SCORE_ADJ_MAX;
889 else
890 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
892 if (oom_adj < task->signal->oom_score_adj &&
893 !capable(CAP_SYS_RESOURCE)) {
894 err = -EACCES;
895 goto err_sighand;
899 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
900 * /proc/pid/oom_score_adj instead.
902 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
903 current->comm, task_pid_nr(current), task_pid_nr(task),
904 task_pid_nr(task));
906 task->signal->oom_score_adj = oom_adj;
907 trace_oom_score_adj_update(task);
908 err_sighand:
909 unlock_task_sighand(task, &flags);
910 err_task_lock:
911 task_unlock(task);
912 put_task_struct(task);
913 out:
914 return err < 0 ? err : count;
917 static const struct file_operations proc_oom_adj_operations = {
918 .read = oom_adj_read,
919 .write = oom_adj_write,
920 .llseek = generic_file_llseek,
923 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
924 size_t count, loff_t *ppos)
926 struct task_struct *task = get_proc_task(file_inode(file));
927 char buffer[PROC_NUMBUF];
928 short oom_score_adj = OOM_SCORE_ADJ_MIN;
929 unsigned long flags;
930 size_t len;
932 if (!task)
933 return -ESRCH;
934 if (lock_task_sighand(task, &flags)) {
935 oom_score_adj = task->signal->oom_score_adj;
936 unlock_task_sighand(task, &flags);
938 put_task_struct(task);
939 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
940 return simple_read_from_buffer(buf, count, ppos, buffer, len);
943 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
944 size_t count, loff_t *ppos)
946 struct task_struct *task;
947 char buffer[PROC_NUMBUF];
948 unsigned long flags;
949 int oom_score_adj;
950 int err;
952 memset(buffer, 0, sizeof(buffer));
953 if (count > sizeof(buffer) - 1)
954 count = sizeof(buffer) - 1;
955 if (copy_from_user(buffer, buf, count)) {
956 err = -EFAULT;
957 goto out;
960 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
961 if (err)
962 goto out;
963 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
964 oom_score_adj > OOM_SCORE_ADJ_MAX) {
965 err = -EINVAL;
966 goto out;
969 task = get_proc_task(file_inode(file));
970 if (!task) {
971 err = -ESRCH;
972 goto out;
975 task_lock(task);
976 if (!task->mm) {
977 err = -EINVAL;
978 goto err_task_lock;
981 if (!lock_task_sighand(task, &flags)) {
982 err = -ESRCH;
983 goto err_task_lock;
986 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
987 !capable(CAP_SYS_RESOURCE)) {
988 err = -EACCES;
989 goto err_sighand;
992 task->signal->oom_score_adj = (short)oom_score_adj;
993 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
994 task->signal->oom_score_adj_min = (short)oom_score_adj;
995 trace_oom_score_adj_update(task);
997 err_sighand:
998 unlock_task_sighand(task, &flags);
999 err_task_lock:
1000 task_unlock(task);
1001 put_task_struct(task);
1002 out:
1003 return err < 0 ? err : count;
1006 static const struct file_operations proc_oom_score_adj_operations = {
1007 .read = oom_score_adj_read,
1008 .write = oom_score_adj_write,
1009 .llseek = default_llseek,
1012 #ifdef CONFIG_AUDITSYSCALL
1013 #define TMPBUFLEN 21
1014 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1015 size_t count, loff_t *ppos)
1017 struct inode * inode = file_inode(file);
1018 struct task_struct *task = get_proc_task(inode);
1019 ssize_t length;
1020 char tmpbuf[TMPBUFLEN];
1022 if (!task)
1023 return -ESRCH;
1024 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1025 from_kuid(file->f_cred->user_ns,
1026 audit_get_loginuid(task)));
1027 put_task_struct(task);
1028 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1031 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1032 size_t count, loff_t *ppos)
1034 struct inode * inode = file_inode(file);
1035 char *page, *tmp;
1036 ssize_t length;
1037 uid_t loginuid;
1038 kuid_t kloginuid;
1040 rcu_read_lock();
1041 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1042 rcu_read_unlock();
1043 return -EPERM;
1045 rcu_read_unlock();
1047 if (count >= PAGE_SIZE)
1048 count = PAGE_SIZE - 1;
1050 if (*ppos != 0) {
1051 /* No partial writes. */
1052 return -EINVAL;
1054 page = (char*)__get_free_page(GFP_TEMPORARY);
1055 if (!page)
1056 return -ENOMEM;
1057 length = -EFAULT;
1058 if (copy_from_user(page, buf, count))
1059 goto out_free_page;
1061 page[count] = '\0';
1062 loginuid = simple_strtoul(page, &tmp, 10);
1063 if (tmp == page) {
1064 length = -EINVAL;
1065 goto out_free_page;
1069 /* is userspace tring to explicitly UNSET the loginuid? */
1070 if (loginuid == AUDIT_UID_UNSET) {
1071 kloginuid = INVALID_UID;
1072 } else {
1073 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1074 if (!uid_valid(kloginuid)) {
1075 length = -EINVAL;
1076 goto out_free_page;
1080 length = audit_set_loginuid(kloginuid);
1081 if (likely(length == 0))
1082 length = count;
1084 out_free_page:
1085 free_page((unsigned long) page);
1086 return length;
1089 static const struct file_operations proc_loginuid_operations = {
1090 .read = proc_loginuid_read,
1091 .write = proc_loginuid_write,
1092 .llseek = generic_file_llseek,
1095 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1096 size_t count, loff_t *ppos)
1098 struct inode * inode = file_inode(file);
1099 struct task_struct *task = get_proc_task(inode);
1100 ssize_t length;
1101 char tmpbuf[TMPBUFLEN];
1103 if (!task)
1104 return -ESRCH;
1105 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1106 audit_get_sessionid(task));
1107 put_task_struct(task);
1108 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1111 static const struct file_operations proc_sessionid_operations = {
1112 .read = proc_sessionid_read,
1113 .llseek = generic_file_llseek,
1115 #endif
1117 #ifdef CONFIG_FAULT_INJECTION
1118 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1119 size_t count, loff_t *ppos)
1121 struct task_struct *task = get_proc_task(file_inode(file));
1122 char buffer[PROC_NUMBUF];
1123 size_t len;
1124 int make_it_fail;
1126 if (!task)
1127 return -ESRCH;
1128 make_it_fail = task->make_it_fail;
1129 put_task_struct(task);
1131 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1133 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1136 static ssize_t proc_fault_inject_write(struct file * file,
1137 const char __user * buf, size_t count, loff_t *ppos)
1139 struct task_struct *task;
1140 char buffer[PROC_NUMBUF], *end;
1141 int make_it_fail;
1143 if (!capable(CAP_SYS_RESOURCE))
1144 return -EPERM;
1145 memset(buffer, 0, sizeof(buffer));
1146 if (count > sizeof(buffer) - 1)
1147 count = sizeof(buffer) - 1;
1148 if (copy_from_user(buffer, buf, count))
1149 return -EFAULT;
1150 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1151 if (*end)
1152 return -EINVAL;
1153 if (make_it_fail < 0 || make_it_fail > 1)
1154 return -EINVAL;
1156 task = get_proc_task(file_inode(file));
1157 if (!task)
1158 return -ESRCH;
1159 task->make_it_fail = make_it_fail;
1160 put_task_struct(task);
1162 return count;
1165 static const struct file_operations proc_fault_inject_operations = {
1166 .read = proc_fault_inject_read,
1167 .write = proc_fault_inject_write,
1168 .llseek = generic_file_llseek,
1170 #endif
1173 #ifdef CONFIG_SCHED_DEBUG
1175 * Print out various scheduling related per-task fields:
1177 static int sched_show(struct seq_file *m, void *v)
1179 struct inode *inode = m->private;
1180 struct task_struct *p;
1182 p = get_proc_task(inode);
1183 if (!p)
1184 return -ESRCH;
1185 proc_sched_show_task(p, m);
1187 put_task_struct(p);
1189 return 0;
1192 static ssize_t
1193 sched_write(struct file *file, const char __user *buf,
1194 size_t count, loff_t *offset)
1196 struct inode *inode = file_inode(file);
1197 struct task_struct *p;
1199 p = get_proc_task(inode);
1200 if (!p)
1201 return -ESRCH;
1202 proc_sched_set_task(p);
1204 put_task_struct(p);
1206 return count;
1209 static int sched_open(struct inode *inode, struct file *filp)
1211 return single_open(filp, sched_show, inode);
1214 static const struct file_operations proc_pid_sched_operations = {
1215 .open = sched_open,
1216 .read = seq_read,
1217 .write = sched_write,
1218 .llseek = seq_lseek,
1219 .release = single_release,
1222 #endif
1224 #ifdef CONFIG_SCHED_AUTOGROUP
1226 * Print out autogroup related information:
1228 static int sched_autogroup_show(struct seq_file *m, void *v)
1230 struct inode *inode = m->private;
1231 struct task_struct *p;
1233 p = get_proc_task(inode);
1234 if (!p)
1235 return -ESRCH;
1236 proc_sched_autogroup_show_task(p, m);
1238 put_task_struct(p);
1240 return 0;
1243 static ssize_t
1244 sched_autogroup_write(struct file *file, const char __user *buf,
1245 size_t count, loff_t *offset)
1247 struct inode *inode = file_inode(file);
1248 struct task_struct *p;
1249 char buffer[PROC_NUMBUF];
1250 int nice;
1251 int err;
1253 memset(buffer, 0, sizeof(buffer));
1254 if (count > sizeof(buffer) - 1)
1255 count = sizeof(buffer) - 1;
1256 if (copy_from_user(buffer, buf, count))
1257 return -EFAULT;
1259 err = kstrtoint(strstrip(buffer), 0, &nice);
1260 if (err < 0)
1261 return err;
1263 p = get_proc_task(inode);
1264 if (!p)
1265 return -ESRCH;
1267 err = proc_sched_autogroup_set_nice(p, nice);
1268 if (err)
1269 count = err;
1271 put_task_struct(p);
1273 return count;
1276 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1278 int ret;
1280 ret = single_open(filp, sched_autogroup_show, NULL);
1281 if (!ret) {
1282 struct seq_file *m = filp->private_data;
1284 m->private = inode;
1286 return ret;
1289 static const struct file_operations proc_pid_sched_autogroup_operations = {
1290 .open = sched_autogroup_open,
1291 .read = seq_read,
1292 .write = sched_autogroup_write,
1293 .llseek = seq_lseek,
1294 .release = single_release,
1297 #endif /* CONFIG_SCHED_AUTOGROUP */
1299 static ssize_t comm_write(struct file *file, const char __user *buf,
1300 size_t count, loff_t *offset)
1302 struct inode *inode = file_inode(file);
1303 struct task_struct *p;
1304 char buffer[TASK_COMM_LEN];
1305 const size_t maxlen = sizeof(buffer) - 1;
1307 memset(buffer, 0, sizeof(buffer));
1308 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1309 return -EFAULT;
1311 p = get_proc_task(inode);
1312 if (!p)
1313 return -ESRCH;
1315 if (same_thread_group(current, p))
1316 set_task_comm(p, buffer);
1317 else
1318 count = -EINVAL;
1320 put_task_struct(p);
1322 return count;
1325 static int comm_show(struct seq_file *m, void *v)
1327 struct inode *inode = m->private;
1328 struct task_struct *p;
1330 p = get_proc_task(inode);
1331 if (!p)
1332 return -ESRCH;
1334 task_lock(p);
1335 seq_printf(m, "%s\n", p->comm);
1336 task_unlock(p);
1338 put_task_struct(p);
1340 return 0;
1343 static int comm_open(struct inode *inode, struct file *filp)
1345 return single_open(filp, comm_show, inode);
1348 static const struct file_operations proc_pid_set_comm_operations = {
1349 .open = comm_open,
1350 .read = seq_read,
1351 .write = comm_write,
1352 .llseek = seq_lseek,
1353 .release = single_release,
1356 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1358 struct task_struct *task;
1359 struct mm_struct *mm;
1360 struct file *exe_file;
1362 task = get_proc_task(d_inode(dentry));
1363 if (!task)
1364 return -ENOENT;
1365 mm = get_task_mm(task);
1366 put_task_struct(task);
1367 if (!mm)
1368 return -ENOENT;
1369 exe_file = get_mm_exe_file(mm);
1370 mmput(mm);
1371 if (exe_file) {
1372 *exe_path = exe_file->f_path;
1373 path_get(&exe_file->f_path);
1374 fput(exe_file);
1375 return 0;
1376 } else
1377 return -ENOENT;
1380 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1382 struct inode *inode = d_inode(dentry);
1383 struct path path;
1384 int error = -EACCES;
1386 /* Are we allowed to snoop on the tasks file descriptors? */
1387 if (!proc_fd_access_allowed(inode))
1388 goto out;
1390 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1391 if (error)
1392 goto out;
1394 nd_jump_link(nd, &path);
1395 return NULL;
1396 out:
1397 return ERR_PTR(error);
1400 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1402 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1403 char *pathname;
1404 int len;
1406 if (!tmp)
1407 return -ENOMEM;
1409 pathname = d_path(path, tmp, PAGE_SIZE);
1410 len = PTR_ERR(pathname);
1411 if (IS_ERR(pathname))
1412 goto out;
1413 len = tmp + PAGE_SIZE - 1 - pathname;
1415 if (len > buflen)
1416 len = buflen;
1417 if (copy_to_user(buffer, pathname, len))
1418 len = -EFAULT;
1419 out:
1420 free_page((unsigned long)tmp);
1421 return len;
1424 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1426 int error = -EACCES;
1427 struct inode *inode = d_inode(dentry);
1428 struct path path;
1430 /* Are we allowed to snoop on the tasks file descriptors? */
1431 if (!proc_fd_access_allowed(inode))
1432 goto out;
1434 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1435 if (error)
1436 goto out;
1438 error = do_proc_readlink(&path, buffer, buflen);
1439 path_put(&path);
1440 out:
1441 return error;
1444 const struct inode_operations proc_pid_link_inode_operations = {
1445 .readlink = proc_pid_readlink,
1446 .follow_link = proc_pid_follow_link,
1447 .setattr = proc_setattr,
1451 /* building an inode */
1453 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1455 struct inode * inode;
1456 struct proc_inode *ei;
1457 const struct cred *cred;
1459 /* We need a new inode */
1461 inode = new_inode(sb);
1462 if (!inode)
1463 goto out;
1465 /* Common stuff */
1466 ei = PROC_I(inode);
1467 inode->i_ino = get_next_ino();
1468 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1469 inode->i_op = &proc_def_inode_operations;
1472 * grab the reference to task.
1474 ei->pid = get_task_pid(task, PIDTYPE_PID);
1475 if (!ei->pid)
1476 goto out_unlock;
1478 if (task_dumpable(task)) {
1479 rcu_read_lock();
1480 cred = __task_cred(task);
1481 inode->i_uid = cred->euid;
1482 inode->i_gid = cred->egid;
1483 rcu_read_unlock();
1485 security_task_to_inode(task, inode);
1487 out:
1488 return inode;
1490 out_unlock:
1491 iput(inode);
1492 return NULL;
1495 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1497 struct inode *inode = d_inode(dentry);
1498 struct task_struct *task;
1499 const struct cred *cred;
1500 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1502 generic_fillattr(inode, stat);
1504 rcu_read_lock();
1505 stat->uid = GLOBAL_ROOT_UID;
1506 stat->gid = GLOBAL_ROOT_GID;
1507 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1508 if (task) {
1509 if (!has_pid_permissions(pid, task, 2)) {
1510 rcu_read_unlock();
1512 * This doesn't prevent learning whether PID exists,
1513 * it only makes getattr() consistent with readdir().
1515 return -ENOENT;
1517 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1518 task_dumpable(task)) {
1519 cred = __task_cred(task);
1520 stat->uid = cred->euid;
1521 stat->gid = cred->egid;
1524 rcu_read_unlock();
1525 return 0;
1528 /* dentry stuff */
1531 * Exceptional case: normally we are not allowed to unhash a busy
1532 * directory. In this case, however, we can do it - no aliasing problems
1533 * due to the way we treat inodes.
1535 * Rewrite the inode's ownerships here because the owning task may have
1536 * performed a setuid(), etc.
1538 * Before the /proc/pid/status file was created the only way to read
1539 * the effective uid of a /process was to stat /proc/pid. Reading
1540 * /proc/pid/status is slow enough that procps and other packages
1541 * kept stating /proc/pid. To keep the rules in /proc simple I have
1542 * made this apply to all per process world readable and executable
1543 * directories.
1545 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1547 struct inode *inode;
1548 struct task_struct *task;
1549 const struct cred *cred;
1551 if (flags & LOOKUP_RCU)
1552 return -ECHILD;
1554 inode = d_inode(dentry);
1555 task = get_proc_task(inode);
1557 if (task) {
1558 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1559 task_dumpable(task)) {
1560 rcu_read_lock();
1561 cred = __task_cred(task);
1562 inode->i_uid = cred->euid;
1563 inode->i_gid = cred->egid;
1564 rcu_read_unlock();
1565 } else {
1566 inode->i_uid = GLOBAL_ROOT_UID;
1567 inode->i_gid = GLOBAL_ROOT_GID;
1569 inode->i_mode &= ~(S_ISUID | S_ISGID);
1570 security_task_to_inode(task, inode);
1571 put_task_struct(task);
1572 return 1;
1574 return 0;
1577 static inline bool proc_inode_is_dead(struct inode *inode)
1579 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1582 int pid_delete_dentry(const struct dentry *dentry)
1584 /* Is the task we represent dead?
1585 * If so, then don't put the dentry on the lru list,
1586 * kill it immediately.
1588 return proc_inode_is_dead(d_inode(dentry));
1591 const struct dentry_operations pid_dentry_operations =
1593 .d_revalidate = pid_revalidate,
1594 .d_delete = pid_delete_dentry,
1597 /* Lookups */
1600 * Fill a directory entry.
1602 * If possible create the dcache entry and derive our inode number and
1603 * file type from dcache entry.
1605 * Since all of the proc inode numbers are dynamically generated, the inode
1606 * numbers do not exist until the inode is cache. This means creating the
1607 * the dcache entry in readdir is necessary to keep the inode numbers
1608 * reported by readdir in sync with the inode numbers reported
1609 * by stat.
1611 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1612 const char *name, int len,
1613 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1615 struct dentry *child, *dir = file->f_path.dentry;
1616 struct qstr qname = QSTR_INIT(name, len);
1617 struct inode *inode;
1618 unsigned type;
1619 ino_t ino;
1621 child = d_hash_and_lookup(dir, &qname);
1622 if (!child) {
1623 child = d_alloc(dir, &qname);
1624 if (!child)
1625 goto end_instantiate;
1626 if (instantiate(d_inode(dir), child, task, ptr) < 0) {
1627 dput(child);
1628 goto end_instantiate;
1631 inode = d_inode(child);
1632 ino = inode->i_ino;
1633 type = inode->i_mode >> 12;
1634 dput(child);
1635 return dir_emit(ctx, name, len, ino, type);
1637 end_instantiate:
1638 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1641 #ifdef CONFIG_CHECKPOINT_RESTORE
1644 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1645 * which represent vma start and end addresses.
1647 static int dname_to_vma_addr(struct dentry *dentry,
1648 unsigned long *start, unsigned long *end)
1650 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1651 return -EINVAL;
1653 return 0;
1656 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1658 unsigned long vm_start, vm_end;
1659 bool exact_vma_exists = false;
1660 struct mm_struct *mm = NULL;
1661 struct task_struct *task;
1662 const struct cred *cred;
1663 struct inode *inode;
1664 int status = 0;
1666 if (flags & LOOKUP_RCU)
1667 return -ECHILD;
1669 if (!capable(CAP_SYS_ADMIN)) {
1670 status = -EPERM;
1671 goto out_notask;
1674 inode = d_inode(dentry);
1675 task = get_proc_task(inode);
1676 if (!task)
1677 goto out_notask;
1679 mm = mm_access(task, PTRACE_MODE_READ);
1680 if (IS_ERR_OR_NULL(mm))
1681 goto out;
1683 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1684 down_read(&mm->mmap_sem);
1685 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1686 up_read(&mm->mmap_sem);
1689 mmput(mm);
1691 if (exact_vma_exists) {
1692 if (task_dumpable(task)) {
1693 rcu_read_lock();
1694 cred = __task_cred(task);
1695 inode->i_uid = cred->euid;
1696 inode->i_gid = cred->egid;
1697 rcu_read_unlock();
1698 } else {
1699 inode->i_uid = GLOBAL_ROOT_UID;
1700 inode->i_gid = GLOBAL_ROOT_GID;
1702 security_task_to_inode(task, inode);
1703 status = 1;
1706 out:
1707 put_task_struct(task);
1709 out_notask:
1710 return status;
1713 static const struct dentry_operations tid_map_files_dentry_operations = {
1714 .d_revalidate = map_files_d_revalidate,
1715 .d_delete = pid_delete_dentry,
1718 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1720 unsigned long vm_start, vm_end;
1721 struct vm_area_struct *vma;
1722 struct task_struct *task;
1723 struct mm_struct *mm;
1724 int rc;
1726 rc = -ENOENT;
1727 task = get_proc_task(d_inode(dentry));
1728 if (!task)
1729 goto out;
1731 mm = get_task_mm(task);
1732 put_task_struct(task);
1733 if (!mm)
1734 goto out;
1736 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1737 if (rc)
1738 goto out_mmput;
1740 rc = -ENOENT;
1741 down_read(&mm->mmap_sem);
1742 vma = find_exact_vma(mm, vm_start, vm_end);
1743 if (vma && vma->vm_file) {
1744 *path = vma->vm_file->f_path;
1745 path_get(path);
1746 rc = 0;
1748 up_read(&mm->mmap_sem);
1750 out_mmput:
1751 mmput(mm);
1752 out:
1753 return rc;
1756 struct map_files_info {
1757 fmode_t mode;
1758 unsigned long len;
1759 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1762 static int
1763 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1764 struct task_struct *task, const void *ptr)
1766 fmode_t mode = (fmode_t)(unsigned long)ptr;
1767 struct proc_inode *ei;
1768 struct inode *inode;
1770 inode = proc_pid_make_inode(dir->i_sb, task);
1771 if (!inode)
1772 return -ENOENT;
1774 ei = PROC_I(inode);
1775 ei->op.proc_get_link = proc_map_files_get_link;
1777 inode->i_op = &proc_pid_link_inode_operations;
1778 inode->i_size = 64;
1779 inode->i_mode = S_IFLNK;
1781 if (mode & FMODE_READ)
1782 inode->i_mode |= S_IRUSR;
1783 if (mode & FMODE_WRITE)
1784 inode->i_mode |= S_IWUSR;
1786 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1787 d_add(dentry, inode);
1789 return 0;
1792 static struct dentry *proc_map_files_lookup(struct inode *dir,
1793 struct dentry *dentry, unsigned int flags)
1795 unsigned long vm_start, vm_end;
1796 struct vm_area_struct *vma;
1797 struct task_struct *task;
1798 int result;
1799 struct mm_struct *mm;
1801 result = -EPERM;
1802 if (!capable(CAP_SYS_ADMIN))
1803 goto out;
1805 result = -ENOENT;
1806 task = get_proc_task(dir);
1807 if (!task)
1808 goto out;
1810 result = -EACCES;
1811 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1812 goto out_put_task;
1814 result = -ENOENT;
1815 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1816 goto out_put_task;
1818 mm = get_task_mm(task);
1819 if (!mm)
1820 goto out_put_task;
1822 down_read(&mm->mmap_sem);
1823 vma = find_exact_vma(mm, vm_start, vm_end);
1824 if (!vma)
1825 goto out_no_vma;
1827 if (vma->vm_file)
1828 result = proc_map_files_instantiate(dir, dentry, task,
1829 (void *)(unsigned long)vma->vm_file->f_mode);
1831 out_no_vma:
1832 up_read(&mm->mmap_sem);
1833 mmput(mm);
1834 out_put_task:
1835 put_task_struct(task);
1836 out:
1837 return ERR_PTR(result);
1840 static const struct inode_operations proc_map_files_inode_operations = {
1841 .lookup = proc_map_files_lookup,
1842 .permission = proc_fd_permission,
1843 .setattr = proc_setattr,
1846 static int
1847 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1849 struct vm_area_struct *vma;
1850 struct task_struct *task;
1851 struct mm_struct *mm;
1852 unsigned long nr_files, pos, i;
1853 struct flex_array *fa = NULL;
1854 struct map_files_info info;
1855 struct map_files_info *p;
1856 int ret;
1858 ret = -EPERM;
1859 if (!capable(CAP_SYS_ADMIN))
1860 goto out;
1862 ret = -ENOENT;
1863 task = get_proc_task(file_inode(file));
1864 if (!task)
1865 goto out;
1867 ret = -EACCES;
1868 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1869 goto out_put_task;
1871 ret = 0;
1872 if (!dir_emit_dots(file, ctx))
1873 goto out_put_task;
1875 mm = get_task_mm(task);
1876 if (!mm)
1877 goto out_put_task;
1878 down_read(&mm->mmap_sem);
1880 nr_files = 0;
1883 * We need two passes here:
1885 * 1) Collect vmas of mapped files with mmap_sem taken
1886 * 2) Release mmap_sem and instantiate entries
1888 * otherwise we get lockdep complained, since filldir()
1889 * routine might require mmap_sem taken in might_fault().
1892 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1893 if (vma->vm_file && ++pos > ctx->pos)
1894 nr_files++;
1897 if (nr_files) {
1898 fa = flex_array_alloc(sizeof(info), nr_files,
1899 GFP_KERNEL);
1900 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1901 GFP_KERNEL)) {
1902 ret = -ENOMEM;
1903 if (fa)
1904 flex_array_free(fa);
1905 up_read(&mm->mmap_sem);
1906 mmput(mm);
1907 goto out_put_task;
1909 for (i = 0, vma = mm->mmap, pos = 2; vma;
1910 vma = vma->vm_next) {
1911 if (!vma->vm_file)
1912 continue;
1913 if (++pos <= ctx->pos)
1914 continue;
1916 info.mode = vma->vm_file->f_mode;
1917 info.len = snprintf(info.name,
1918 sizeof(info.name), "%lx-%lx",
1919 vma->vm_start, vma->vm_end);
1920 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1921 BUG();
1924 up_read(&mm->mmap_sem);
1926 for (i = 0; i < nr_files; i++) {
1927 p = flex_array_get(fa, i);
1928 if (!proc_fill_cache(file, ctx,
1929 p->name, p->len,
1930 proc_map_files_instantiate,
1931 task,
1932 (void *)(unsigned long)p->mode))
1933 break;
1934 ctx->pos++;
1936 if (fa)
1937 flex_array_free(fa);
1938 mmput(mm);
1940 out_put_task:
1941 put_task_struct(task);
1942 out:
1943 return ret;
1946 static const struct file_operations proc_map_files_operations = {
1947 .read = generic_read_dir,
1948 .iterate = proc_map_files_readdir,
1949 .llseek = default_llseek,
1952 struct timers_private {
1953 struct pid *pid;
1954 struct task_struct *task;
1955 struct sighand_struct *sighand;
1956 struct pid_namespace *ns;
1957 unsigned long flags;
1960 static void *timers_start(struct seq_file *m, loff_t *pos)
1962 struct timers_private *tp = m->private;
1964 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
1965 if (!tp->task)
1966 return ERR_PTR(-ESRCH);
1968 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
1969 if (!tp->sighand)
1970 return ERR_PTR(-ESRCH);
1972 return seq_list_start(&tp->task->signal->posix_timers, *pos);
1975 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
1977 struct timers_private *tp = m->private;
1978 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
1981 static void timers_stop(struct seq_file *m, void *v)
1983 struct timers_private *tp = m->private;
1985 if (tp->sighand) {
1986 unlock_task_sighand(tp->task, &tp->flags);
1987 tp->sighand = NULL;
1990 if (tp->task) {
1991 put_task_struct(tp->task);
1992 tp->task = NULL;
1996 static int show_timer(struct seq_file *m, void *v)
1998 struct k_itimer *timer;
1999 struct timers_private *tp = m->private;
2000 int notify;
2001 static const char * const nstr[] = {
2002 [SIGEV_SIGNAL] = "signal",
2003 [SIGEV_NONE] = "none",
2004 [SIGEV_THREAD] = "thread",
2007 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2008 notify = timer->it_sigev_notify;
2010 seq_printf(m, "ID: %d\n", timer->it_id);
2011 seq_printf(m, "signal: %d/%p\n",
2012 timer->sigq->info.si_signo,
2013 timer->sigq->info.si_value.sival_ptr);
2014 seq_printf(m, "notify: %s/%s.%d\n",
2015 nstr[notify & ~SIGEV_THREAD_ID],
2016 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2017 pid_nr_ns(timer->it_pid, tp->ns));
2018 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2020 return 0;
2023 static const struct seq_operations proc_timers_seq_ops = {
2024 .start = timers_start,
2025 .next = timers_next,
2026 .stop = timers_stop,
2027 .show = show_timer,
2030 static int proc_timers_open(struct inode *inode, struct file *file)
2032 struct timers_private *tp;
2034 tp = __seq_open_private(file, &proc_timers_seq_ops,
2035 sizeof(struct timers_private));
2036 if (!tp)
2037 return -ENOMEM;
2039 tp->pid = proc_pid(inode);
2040 tp->ns = inode->i_sb->s_fs_info;
2041 return 0;
2044 static const struct file_operations proc_timers_operations = {
2045 .open = proc_timers_open,
2046 .read = seq_read,
2047 .llseek = seq_lseek,
2048 .release = seq_release_private,
2050 #endif /* CONFIG_CHECKPOINT_RESTORE */
2052 static int proc_pident_instantiate(struct inode *dir,
2053 struct dentry *dentry, struct task_struct *task, const void *ptr)
2055 const struct pid_entry *p = ptr;
2056 struct inode *inode;
2057 struct proc_inode *ei;
2059 inode = proc_pid_make_inode(dir->i_sb, task);
2060 if (!inode)
2061 goto out;
2063 ei = PROC_I(inode);
2064 inode->i_mode = p->mode;
2065 if (S_ISDIR(inode->i_mode))
2066 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2067 if (p->iop)
2068 inode->i_op = p->iop;
2069 if (p->fop)
2070 inode->i_fop = p->fop;
2071 ei->op = p->op;
2072 d_set_d_op(dentry, &pid_dentry_operations);
2073 d_add(dentry, inode);
2074 /* Close the race of the process dying before we return the dentry */
2075 if (pid_revalidate(dentry, 0))
2076 return 0;
2077 out:
2078 return -ENOENT;
2081 static struct dentry *proc_pident_lookup(struct inode *dir,
2082 struct dentry *dentry,
2083 const struct pid_entry *ents,
2084 unsigned int nents)
2086 int error;
2087 struct task_struct *task = get_proc_task(dir);
2088 const struct pid_entry *p, *last;
2090 error = -ENOENT;
2092 if (!task)
2093 goto out_no_task;
2096 * Yes, it does not scale. And it should not. Don't add
2097 * new entries into /proc/<tgid>/ without very good reasons.
2099 last = &ents[nents - 1];
2100 for (p = ents; p <= last; p++) {
2101 if (p->len != dentry->d_name.len)
2102 continue;
2103 if (!memcmp(dentry->d_name.name, p->name, p->len))
2104 break;
2106 if (p > last)
2107 goto out;
2109 error = proc_pident_instantiate(dir, dentry, task, p);
2110 out:
2111 put_task_struct(task);
2112 out_no_task:
2113 return ERR_PTR(error);
2116 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2117 const struct pid_entry *ents, unsigned int nents)
2119 struct task_struct *task = get_proc_task(file_inode(file));
2120 const struct pid_entry *p;
2122 if (!task)
2123 return -ENOENT;
2125 if (!dir_emit_dots(file, ctx))
2126 goto out;
2128 if (ctx->pos >= nents + 2)
2129 goto out;
2131 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2132 if (!proc_fill_cache(file, ctx, p->name, p->len,
2133 proc_pident_instantiate, task, p))
2134 break;
2135 ctx->pos++;
2137 out:
2138 put_task_struct(task);
2139 return 0;
2142 #ifdef CONFIG_SECURITY
2143 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2144 size_t count, loff_t *ppos)
2146 struct inode * inode = file_inode(file);
2147 char *p = NULL;
2148 ssize_t length;
2149 struct task_struct *task = get_proc_task(inode);
2151 if (!task)
2152 return -ESRCH;
2154 length = security_getprocattr(task,
2155 (char*)file->f_path.dentry->d_name.name,
2156 &p);
2157 put_task_struct(task);
2158 if (length > 0)
2159 length = simple_read_from_buffer(buf, count, ppos, p, length);
2160 kfree(p);
2161 return length;
2164 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2165 size_t count, loff_t *ppos)
2167 struct inode * inode = file_inode(file);
2168 char *page;
2169 ssize_t length;
2170 struct task_struct *task = get_proc_task(inode);
2172 length = -ESRCH;
2173 if (!task)
2174 goto out_no_task;
2175 if (count > PAGE_SIZE)
2176 count = PAGE_SIZE;
2178 /* No partial writes. */
2179 length = -EINVAL;
2180 if (*ppos != 0)
2181 goto out;
2183 length = -ENOMEM;
2184 page = (char*)__get_free_page(GFP_TEMPORARY);
2185 if (!page)
2186 goto out;
2188 length = -EFAULT;
2189 if (copy_from_user(page, buf, count))
2190 goto out_free;
2192 /* Guard against adverse ptrace interaction */
2193 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2194 if (length < 0)
2195 goto out_free;
2197 length = security_setprocattr(task,
2198 (char*)file->f_path.dentry->d_name.name,
2199 (void*)page, count);
2200 mutex_unlock(&task->signal->cred_guard_mutex);
2201 out_free:
2202 free_page((unsigned long) page);
2203 out:
2204 put_task_struct(task);
2205 out_no_task:
2206 return length;
2209 static const struct file_operations proc_pid_attr_operations = {
2210 .read = proc_pid_attr_read,
2211 .write = proc_pid_attr_write,
2212 .llseek = generic_file_llseek,
2215 static const struct pid_entry attr_dir_stuff[] = {
2216 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2217 REG("prev", S_IRUGO, proc_pid_attr_operations),
2218 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2219 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2220 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2221 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2224 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2226 return proc_pident_readdir(file, ctx,
2227 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2230 static const struct file_operations proc_attr_dir_operations = {
2231 .read = generic_read_dir,
2232 .iterate = proc_attr_dir_readdir,
2233 .llseek = default_llseek,
2236 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2237 struct dentry *dentry, unsigned int flags)
2239 return proc_pident_lookup(dir, dentry,
2240 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2243 static const struct inode_operations proc_attr_dir_inode_operations = {
2244 .lookup = proc_attr_dir_lookup,
2245 .getattr = pid_getattr,
2246 .setattr = proc_setattr,
2249 #endif
2251 #ifdef CONFIG_ELF_CORE
2252 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2253 size_t count, loff_t *ppos)
2255 struct task_struct *task = get_proc_task(file_inode(file));
2256 struct mm_struct *mm;
2257 char buffer[PROC_NUMBUF];
2258 size_t len;
2259 int ret;
2261 if (!task)
2262 return -ESRCH;
2264 ret = 0;
2265 mm = get_task_mm(task);
2266 if (mm) {
2267 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2268 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2269 MMF_DUMP_FILTER_SHIFT));
2270 mmput(mm);
2271 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2274 put_task_struct(task);
2276 return ret;
2279 static ssize_t proc_coredump_filter_write(struct file *file,
2280 const char __user *buf,
2281 size_t count,
2282 loff_t *ppos)
2284 struct task_struct *task;
2285 struct mm_struct *mm;
2286 char buffer[PROC_NUMBUF], *end;
2287 unsigned int val;
2288 int ret;
2289 int i;
2290 unsigned long mask;
2292 ret = -EFAULT;
2293 memset(buffer, 0, sizeof(buffer));
2294 if (count > sizeof(buffer) - 1)
2295 count = sizeof(buffer) - 1;
2296 if (copy_from_user(buffer, buf, count))
2297 goto out_no_task;
2299 ret = -EINVAL;
2300 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2301 if (*end == '\n')
2302 end++;
2303 if (end - buffer == 0)
2304 goto out_no_task;
2306 ret = -ESRCH;
2307 task = get_proc_task(file_inode(file));
2308 if (!task)
2309 goto out_no_task;
2311 ret = end - buffer;
2312 mm = get_task_mm(task);
2313 if (!mm)
2314 goto out_no_mm;
2316 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2317 if (val & mask)
2318 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2319 else
2320 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2323 mmput(mm);
2324 out_no_mm:
2325 put_task_struct(task);
2326 out_no_task:
2327 return ret;
2330 static const struct file_operations proc_coredump_filter_operations = {
2331 .read = proc_coredump_filter_read,
2332 .write = proc_coredump_filter_write,
2333 .llseek = generic_file_llseek,
2335 #endif
2337 #ifdef CONFIG_TASK_IO_ACCOUNTING
2338 static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
2340 struct task_io_accounting acct = task->ioac;
2341 unsigned long flags;
2342 int result;
2344 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2345 if (result)
2346 return result;
2348 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2349 result = -EACCES;
2350 goto out_unlock;
2353 if (whole && lock_task_sighand(task, &flags)) {
2354 struct task_struct *t = task;
2356 task_io_accounting_add(&acct, &task->signal->ioac);
2357 while_each_thread(task, t)
2358 task_io_accounting_add(&acct, &t->ioac);
2360 unlock_task_sighand(task, &flags);
2362 seq_printf(m,
2363 "rchar: %llu\n"
2364 "wchar: %llu\n"
2365 "syscr: %llu\n"
2366 "syscw: %llu\n"
2367 "read_bytes: %llu\n"
2368 "write_bytes: %llu\n"
2369 "cancelled_write_bytes: %llu\n",
2370 (unsigned long long)acct.rchar,
2371 (unsigned long long)acct.wchar,
2372 (unsigned long long)acct.syscr,
2373 (unsigned long long)acct.syscw,
2374 (unsigned long long)acct.read_bytes,
2375 (unsigned long long)acct.write_bytes,
2376 (unsigned long long)acct.cancelled_write_bytes);
2377 result = 0;
2379 out_unlock:
2380 mutex_unlock(&task->signal->cred_guard_mutex);
2381 return result;
2384 static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2385 struct pid *pid, struct task_struct *task)
2387 return do_io_accounting(task, m, 0);
2390 static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2391 struct pid *pid, struct task_struct *task)
2393 return do_io_accounting(task, m, 1);
2395 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2397 #ifdef CONFIG_USER_NS
2398 static int proc_id_map_open(struct inode *inode, struct file *file,
2399 const struct seq_operations *seq_ops)
2401 struct user_namespace *ns = NULL;
2402 struct task_struct *task;
2403 struct seq_file *seq;
2404 int ret = -EINVAL;
2406 task = get_proc_task(inode);
2407 if (task) {
2408 rcu_read_lock();
2409 ns = get_user_ns(task_cred_xxx(task, user_ns));
2410 rcu_read_unlock();
2411 put_task_struct(task);
2413 if (!ns)
2414 goto err;
2416 ret = seq_open(file, seq_ops);
2417 if (ret)
2418 goto err_put_ns;
2420 seq = file->private_data;
2421 seq->private = ns;
2423 return 0;
2424 err_put_ns:
2425 put_user_ns(ns);
2426 err:
2427 return ret;
2430 static int proc_id_map_release(struct inode *inode, struct file *file)
2432 struct seq_file *seq = file->private_data;
2433 struct user_namespace *ns = seq->private;
2434 put_user_ns(ns);
2435 return seq_release(inode, file);
2438 static int proc_uid_map_open(struct inode *inode, struct file *file)
2440 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2443 static int proc_gid_map_open(struct inode *inode, struct file *file)
2445 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2448 static int proc_projid_map_open(struct inode *inode, struct file *file)
2450 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2453 static const struct file_operations proc_uid_map_operations = {
2454 .open = proc_uid_map_open,
2455 .write = proc_uid_map_write,
2456 .read = seq_read,
2457 .llseek = seq_lseek,
2458 .release = proc_id_map_release,
2461 static const struct file_operations proc_gid_map_operations = {
2462 .open = proc_gid_map_open,
2463 .write = proc_gid_map_write,
2464 .read = seq_read,
2465 .llseek = seq_lseek,
2466 .release = proc_id_map_release,
2469 static const struct file_operations proc_projid_map_operations = {
2470 .open = proc_projid_map_open,
2471 .write = proc_projid_map_write,
2472 .read = seq_read,
2473 .llseek = seq_lseek,
2474 .release = proc_id_map_release,
2477 static int proc_setgroups_open(struct inode *inode, struct file *file)
2479 struct user_namespace *ns = NULL;
2480 struct task_struct *task;
2481 int ret;
2483 ret = -ESRCH;
2484 task = get_proc_task(inode);
2485 if (task) {
2486 rcu_read_lock();
2487 ns = get_user_ns(task_cred_xxx(task, user_ns));
2488 rcu_read_unlock();
2489 put_task_struct(task);
2491 if (!ns)
2492 goto err;
2494 if (file->f_mode & FMODE_WRITE) {
2495 ret = -EACCES;
2496 if (!ns_capable(ns, CAP_SYS_ADMIN))
2497 goto err_put_ns;
2500 ret = single_open(file, &proc_setgroups_show, ns);
2501 if (ret)
2502 goto err_put_ns;
2504 return 0;
2505 err_put_ns:
2506 put_user_ns(ns);
2507 err:
2508 return ret;
2511 static int proc_setgroups_release(struct inode *inode, struct file *file)
2513 struct seq_file *seq = file->private_data;
2514 struct user_namespace *ns = seq->private;
2515 int ret = single_release(inode, file);
2516 put_user_ns(ns);
2517 return ret;
2520 static const struct file_operations proc_setgroups_operations = {
2521 .open = proc_setgroups_open,
2522 .write = proc_setgroups_write,
2523 .read = seq_read,
2524 .llseek = seq_lseek,
2525 .release = proc_setgroups_release,
2527 #endif /* CONFIG_USER_NS */
2529 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2530 struct pid *pid, struct task_struct *task)
2532 int err = lock_trace(task);
2533 if (!err) {
2534 seq_printf(m, "%08x\n", task->personality);
2535 unlock_trace(task);
2537 return err;
2541 * Thread groups
2543 static const struct file_operations proc_task_operations;
2544 static const struct inode_operations proc_task_inode_operations;
2546 static const struct pid_entry tgid_base_stuff[] = {
2547 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2548 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2549 #ifdef CONFIG_CHECKPOINT_RESTORE
2550 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2551 #endif
2552 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2553 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2554 #ifdef CONFIG_NET
2555 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2556 #endif
2557 REG("environ", S_IRUSR, proc_environ_operations),
2558 ONE("auxv", S_IRUSR, proc_pid_auxv),
2559 ONE("status", S_IRUGO, proc_pid_status),
2560 ONE("personality", S_IRUSR, proc_pid_personality),
2561 ONE("limits", S_IRUGO, proc_pid_limits),
2562 #ifdef CONFIG_SCHED_DEBUG
2563 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2564 #endif
2565 #ifdef CONFIG_SCHED_AUTOGROUP
2566 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2567 #endif
2568 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2569 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2570 ONE("syscall", S_IRUSR, proc_pid_syscall),
2571 #endif
2572 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2573 ONE("stat", S_IRUGO, proc_tgid_stat),
2574 ONE("statm", S_IRUGO, proc_pid_statm),
2575 REG("maps", S_IRUGO, proc_pid_maps_operations),
2576 #ifdef CONFIG_NUMA
2577 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2578 #endif
2579 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2580 LNK("cwd", proc_cwd_link),
2581 LNK("root", proc_root_link),
2582 LNK("exe", proc_exe_link),
2583 REG("mounts", S_IRUGO, proc_mounts_operations),
2584 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2585 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2586 #ifdef CONFIG_PROC_PAGE_MONITOR
2587 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2588 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2589 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2590 #endif
2591 #ifdef CONFIG_SECURITY
2592 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2593 #endif
2594 #ifdef CONFIG_KALLSYMS
2595 ONE("wchan", S_IRUGO, proc_pid_wchan),
2596 #endif
2597 #ifdef CONFIG_STACKTRACE
2598 ONE("stack", S_IRUSR, proc_pid_stack),
2599 #endif
2600 #ifdef CONFIG_SCHEDSTATS
2601 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2602 #endif
2603 #ifdef CONFIG_LATENCYTOP
2604 REG("latency", S_IRUGO, proc_lstats_operations),
2605 #endif
2606 #ifdef CONFIG_PROC_PID_CPUSET
2607 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2608 #endif
2609 #ifdef CONFIG_CGROUPS
2610 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2611 #endif
2612 ONE("oom_score", S_IRUGO, proc_oom_score),
2613 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2614 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2615 #ifdef CONFIG_AUDITSYSCALL
2616 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2617 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2618 #endif
2619 #ifdef CONFIG_FAULT_INJECTION
2620 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2621 #endif
2622 #ifdef CONFIG_ELF_CORE
2623 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2624 #endif
2625 #ifdef CONFIG_TASK_IO_ACCOUNTING
2626 ONE("io", S_IRUSR, proc_tgid_io_accounting),
2627 #endif
2628 #ifdef CONFIG_HARDWALL
2629 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2630 #endif
2631 #ifdef CONFIG_USER_NS
2632 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2633 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2634 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2635 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2636 #endif
2637 #ifdef CONFIG_CHECKPOINT_RESTORE
2638 REG("timers", S_IRUGO, proc_timers_operations),
2639 #endif
2642 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2644 return proc_pident_readdir(file, ctx,
2645 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2648 static const struct file_operations proc_tgid_base_operations = {
2649 .read = generic_read_dir,
2650 .iterate = proc_tgid_base_readdir,
2651 .llseek = default_llseek,
2654 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2656 return proc_pident_lookup(dir, dentry,
2657 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2660 static const struct inode_operations proc_tgid_base_inode_operations = {
2661 .lookup = proc_tgid_base_lookup,
2662 .getattr = pid_getattr,
2663 .setattr = proc_setattr,
2664 .permission = proc_pid_permission,
2667 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2669 struct dentry *dentry, *leader, *dir;
2670 char buf[PROC_NUMBUF];
2671 struct qstr name;
2673 name.name = buf;
2674 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2675 /* no ->d_hash() rejects on procfs */
2676 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2677 if (dentry) {
2678 d_invalidate(dentry);
2679 dput(dentry);
2682 if (pid == tgid)
2683 return;
2685 name.name = buf;
2686 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2687 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2688 if (!leader)
2689 goto out;
2691 name.name = "task";
2692 name.len = strlen(name.name);
2693 dir = d_hash_and_lookup(leader, &name);
2694 if (!dir)
2695 goto out_put_leader;
2697 name.name = buf;
2698 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2699 dentry = d_hash_and_lookup(dir, &name);
2700 if (dentry) {
2701 d_invalidate(dentry);
2702 dput(dentry);
2705 dput(dir);
2706 out_put_leader:
2707 dput(leader);
2708 out:
2709 return;
2713 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2714 * @task: task that should be flushed.
2716 * When flushing dentries from proc, one needs to flush them from global
2717 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2718 * in. This call is supposed to do all of this job.
2720 * Looks in the dcache for
2721 * /proc/@pid
2722 * /proc/@tgid/task/@pid
2723 * if either directory is present flushes it and all of it'ts children
2724 * from the dcache.
2726 * It is safe and reasonable to cache /proc entries for a task until
2727 * that task exits. After that they just clog up the dcache with
2728 * useless entries, possibly causing useful dcache entries to be
2729 * flushed instead. This routine is proved to flush those useless
2730 * dcache entries at process exit time.
2732 * NOTE: This routine is just an optimization so it does not guarantee
2733 * that no dcache entries will exist at process exit time it
2734 * just makes it very unlikely that any will persist.
2737 void proc_flush_task(struct task_struct *task)
2739 int i;
2740 struct pid *pid, *tgid;
2741 struct upid *upid;
2743 pid = task_pid(task);
2744 tgid = task_tgid(task);
2746 for (i = 0; i <= pid->level; i++) {
2747 upid = &pid->numbers[i];
2748 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2749 tgid->numbers[i].nr);
2753 static int proc_pid_instantiate(struct inode *dir,
2754 struct dentry * dentry,
2755 struct task_struct *task, const void *ptr)
2757 struct inode *inode;
2759 inode = proc_pid_make_inode(dir->i_sb, task);
2760 if (!inode)
2761 goto out;
2763 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2764 inode->i_op = &proc_tgid_base_inode_operations;
2765 inode->i_fop = &proc_tgid_base_operations;
2766 inode->i_flags|=S_IMMUTABLE;
2768 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2769 ARRAY_SIZE(tgid_base_stuff)));
2771 d_set_d_op(dentry, &pid_dentry_operations);
2773 d_add(dentry, inode);
2774 /* Close the race of the process dying before we return the dentry */
2775 if (pid_revalidate(dentry, 0))
2776 return 0;
2777 out:
2778 return -ENOENT;
2781 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2783 int result = -ENOENT;
2784 struct task_struct *task;
2785 unsigned tgid;
2786 struct pid_namespace *ns;
2788 tgid = name_to_int(&dentry->d_name);
2789 if (tgid == ~0U)
2790 goto out;
2792 ns = dentry->d_sb->s_fs_info;
2793 rcu_read_lock();
2794 task = find_task_by_pid_ns(tgid, ns);
2795 if (task)
2796 get_task_struct(task);
2797 rcu_read_unlock();
2798 if (!task)
2799 goto out;
2801 result = proc_pid_instantiate(dir, dentry, task, NULL);
2802 put_task_struct(task);
2803 out:
2804 return ERR_PTR(result);
2808 * Find the first task with tgid >= tgid
2811 struct tgid_iter {
2812 unsigned int tgid;
2813 struct task_struct *task;
2815 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2817 struct pid *pid;
2819 if (iter.task)
2820 put_task_struct(iter.task);
2821 rcu_read_lock();
2822 retry:
2823 iter.task = NULL;
2824 pid = find_ge_pid(iter.tgid, ns);
2825 if (pid) {
2826 iter.tgid = pid_nr_ns(pid, ns);
2827 iter.task = pid_task(pid, PIDTYPE_PID);
2828 /* What we to know is if the pid we have find is the
2829 * pid of a thread_group_leader. Testing for task
2830 * being a thread_group_leader is the obvious thing
2831 * todo but there is a window when it fails, due to
2832 * the pid transfer logic in de_thread.
2834 * So we perform the straight forward test of seeing
2835 * if the pid we have found is the pid of a thread
2836 * group leader, and don't worry if the task we have
2837 * found doesn't happen to be a thread group leader.
2838 * As we don't care in the case of readdir.
2840 if (!iter.task || !has_group_leader_pid(iter.task)) {
2841 iter.tgid += 1;
2842 goto retry;
2844 get_task_struct(iter.task);
2846 rcu_read_unlock();
2847 return iter;
2850 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
2852 /* for the /proc/ directory itself, after non-process stuff has been done */
2853 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2855 struct tgid_iter iter;
2856 struct pid_namespace *ns = file_inode(file)->i_sb->s_fs_info;
2857 loff_t pos = ctx->pos;
2859 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2860 return 0;
2862 if (pos == TGID_OFFSET - 2) {
2863 struct inode *inode = d_inode(ns->proc_self);
2864 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2865 return 0;
2866 ctx->pos = pos = pos + 1;
2868 if (pos == TGID_OFFSET - 1) {
2869 struct inode *inode = d_inode(ns->proc_thread_self);
2870 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
2871 return 0;
2872 ctx->pos = pos = pos + 1;
2874 iter.tgid = pos - TGID_OFFSET;
2875 iter.task = NULL;
2876 for (iter = next_tgid(ns, iter);
2877 iter.task;
2878 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2879 char name[PROC_NUMBUF];
2880 int len;
2881 if (!has_pid_permissions(ns, iter.task, 2))
2882 continue;
2884 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2885 ctx->pos = iter.tgid + TGID_OFFSET;
2886 if (!proc_fill_cache(file, ctx, name, len,
2887 proc_pid_instantiate, iter.task, NULL)) {
2888 put_task_struct(iter.task);
2889 return 0;
2892 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2893 return 0;
2897 * Tasks
2899 static const struct pid_entry tid_base_stuff[] = {
2900 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2901 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2902 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2903 #ifdef CONFIG_NET
2904 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2905 #endif
2906 REG("environ", S_IRUSR, proc_environ_operations),
2907 ONE("auxv", S_IRUSR, proc_pid_auxv),
2908 ONE("status", S_IRUGO, proc_pid_status),
2909 ONE("personality", S_IRUSR, proc_pid_personality),
2910 ONE("limits", S_IRUGO, proc_pid_limits),
2911 #ifdef CONFIG_SCHED_DEBUG
2912 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2913 #endif
2914 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2915 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2916 ONE("syscall", S_IRUSR, proc_pid_syscall),
2917 #endif
2918 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2919 ONE("stat", S_IRUGO, proc_tid_stat),
2920 ONE("statm", S_IRUGO, proc_pid_statm),
2921 REG("maps", S_IRUGO, proc_tid_maps_operations),
2922 #ifdef CONFIG_CHECKPOINT_RESTORE
2923 REG("children", S_IRUGO, proc_tid_children_operations),
2924 #endif
2925 #ifdef CONFIG_NUMA
2926 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2927 #endif
2928 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2929 LNK("cwd", proc_cwd_link),
2930 LNK("root", proc_root_link),
2931 LNK("exe", proc_exe_link),
2932 REG("mounts", S_IRUGO, proc_mounts_operations),
2933 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2934 #ifdef CONFIG_PROC_PAGE_MONITOR
2935 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2936 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2937 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2938 #endif
2939 #ifdef CONFIG_SECURITY
2940 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2941 #endif
2942 #ifdef CONFIG_KALLSYMS
2943 ONE("wchan", S_IRUGO, proc_pid_wchan),
2944 #endif
2945 #ifdef CONFIG_STACKTRACE
2946 ONE("stack", S_IRUSR, proc_pid_stack),
2947 #endif
2948 #ifdef CONFIG_SCHEDSTATS
2949 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2950 #endif
2951 #ifdef CONFIG_LATENCYTOP
2952 REG("latency", S_IRUGO, proc_lstats_operations),
2953 #endif
2954 #ifdef CONFIG_PROC_PID_CPUSET
2955 ONE("cpuset", S_IRUGO, proc_cpuset_show),
2956 #endif
2957 #ifdef CONFIG_CGROUPS
2958 ONE("cgroup", S_IRUGO, proc_cgroup_show),
2959 #endif
2960 ONE("oom_score", S_IRUGO, proc_oom_score),
2961 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2962 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2963 #ifdef CONFIG_AUDITSYSCALL
2964 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2965 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2966 #endif
2967 #ifdef CONFIG_FAULT_INJECTION
2968 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2969 #endif
2970 #ifdef CONFIG_TASK_IO_ACCOUNTING
2971 ONE("io", S_IRUSR, proc_tid_io_accounting),
2972 #endif
2973 #ifdef CONFIG_HARDWALL
2974 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
2975 #endif
2976 #ifdef CONFIG_USER_NS
2977 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2978 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2979 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2980 REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
2981 #endif
2984 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2986 return proc_pident_readdir(file, ctx,
2987 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2990 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2992 return proc_pident_lookup(dir, dentry,
2993 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2996 static const struct file_operations proc_tid_base_operations = {
2997 .read = generic_read_dir,
2998 .iterate = proc_tid_base_readdir,
2999 .llseek = default_llseek,
3002 static const struct inode_operations proc_tid_base_inode_operations = {
3003 .lookup = proc_tid_base_lookup,
3004 .getattr = pid_getattr,
3005 .setattr = proc_setattr,
3008 static int proc_task_instantiate(struct inode *dir,
3009 struct dentry *dentry, struct task_struct *task, const void *ptr)
3011 struct inode *inode;
3012 inode = proc_pid_make_inode(dir->i_sb, task);
3014 if (!inode)
3015 goto out;
3016 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3017 inode->i_op = &proc_tid_base_inode_operations;
3018 inode->i_fop = &proc_tid_base_operations;
3019 inode->i_flags|=S_IMMUTABLE;
3021 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3022 ARRAY_SIZE(tid_base_stuff)));
3024 d_set_d_op(dentry, &pid_dentry_operations);
3026 d_add(dentry, inode);
3027 /* Close the race of the process dying before we return the dentry */
3028 if (pid_revalidate(dentry, 0))
3029 return 0;
3030 out:
3031 return -ENOENT;
3034 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3036 int result = -ENOENT;
3037 struct task_struct *task;
3038 struct task_struct *leader = get_proc_task(dir);
3039 unsigned tid;
3040 struct pid_namespace *ns;
3042 if (!leader)
3043 goto out_no_task;
3045 tid = name_to_int(&dentry->d_name);
3046 if (tid == ~0U)
3047 goto out;
3049 ns = dentry->d_sb->s_fs_info;
3050 rcu_read_lock();
3051 task = find_task_by_pid_ns(tid, ns);
3052 if (task)
3053 get_task_struct(task);
3054 rcu_read_unlock();
3055 if (!task)
3056 goto out;
3057 if (!same_thread_group(leader, task))
3058 goto out_drop_task;
3060 result = proc_task_instantiate(dir, dentry, task, NULL);
3061 out_drop_task:
3062 put_task_struct(task);
3063 out:
3064 put_task_struct(leader);
3065 out_no_task:
3066 return ERR_PTR(result);
3070 * Find the first tid of a thread group to return to user space.
3072 * Usually this is just the thread group leader, but if the users
3073 * buffer was too small or there was a seek into the middle of the
3074 * directory we have more work todo.
3076 * In the case of a short read we start with find_task_by_pid.
3078 * In the case of a seek we start with the leader and walk nr
3079 * threads past it.
3081 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3082 struct pid_namespace *ns)
3084 struct task_struct *pos, *task;
3085 unsigned long nr = f_pos;
3087 if (nr != f_pos) /* 32bit overflow? */
3088 return NULL;
3090 rcu_read_lock();
3091 task = pid_task(pid, PIDTYPE_PID);
3092 if (!task)
3093 goto fail;
3095 /* Attempt to start with the tid of a thread */
3096 if (tid && nr) {
3097 pos = find_task_by_pid_ns(tid, ns);
3098 if (pos && same_thread_group(pos, task))
3099 goto found;
3102 /* If nr exceeds the number of threads there is nothing todo */
3103 if (nr >= get_nr_threads(task))
3104 goto fail;
3106 /* If we haven't found our starting place yet start
3107 * with the leader and walk nr threads forward.
3109 pos = task = task->group_leader;
3110 do {
3111 if (!nr--)
3112 goto found;
3113 } while_each_thread(task, pos);
3114 fail:
3115 pos = NULL;
3116 goto out;
3117 found:
3118 get_task_struct(pos);
3119 out:
3120 rcu_read_unlock();
3121 return pos;
3125 * Find the next thread in the thread list.
3126 * Return NULL if there is an error or no next thread.
3128 * The reference to the input task_struct is released.
3130 static struct task_struct *next_tid(struct task_struct *start)
3132 struct task_struct *pos = NULL;
3133 rcu_read_lock();
3134 if (pid_alive(start)) {
3135 pos = next_thread(start);
3136 if (thread_group_leader(pos))
3137 pos = NULL;
3138 else
3139 get_task_struct(pos);
3141 rcu_read_unlock();
3142 put_task_struct(start);
3143 return pos;
3146 /* for the /proc/TGID/task/ directories */
3147 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3149 struct inode *inode = file_inode(file);
3150 struct task_struct *task;
3151 struct pid_namespace *ns;
3152 int tid;
3154 if (proc_inode_is_dead(inode))
3155 return -ENOENT;
3157 if (!dir_emit_dots(file, ctx))
3158 return 0;
3160 /* f_version caches the tgid value that the last readdir call couldn't
3161 * return. lseek aka telldir automagically resets f_version to 0.
3163 ns = inode->i_sb->s_fs_info;
3164 tid = (int)file->f_version;
3165 file->f_version = 0;
3166 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3167 task;
3168 task = next_tid(task), ctx->pos++) {
3169 char name[PROC_NUMBUF];
3170 int len;
3171 tid = task_pid_nr_ns(task, ns);
3172 len = snprintf(name, sizeof(name), "%d", tid);
3173 if (!proc_fill_cache(file, ctx, name, len,
3174 proc_task_instantiate, task, NULL)) {
3175 /* returning this tgid failed, save it as the first
3176 * pid for the next readir call */
3177 file->f_version = (u64)tid;
3178 put_task_struct(task);
3179 break;
3183 return 0;
3186 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3188 struct inode *inode = d_inode(dentry);
3189 struct task_struct *p = get_proc_task(inode);
3190 generic_fillattr(inode, stat);
3192 if (p) {
3193 stat->nlink += get_nr_threads(p);
3194 put_task_struct(p);
3197 return 0;
3200 static const struct inode_operations proc_task_inode_operations = {
3201 .lookup = proc_task_lookup,
3202 .getattr = proc_task_getattr,
3203 .setattr = proc_setattr,
3204 .permission = proc_pid_permission,
3207 static const struct file_operations proc_task_operations = {
3208 .read = generic_read_dir,
3209 .iterate = proc_task_readdir,
3210 .llseek = default_llseek,