Merge tag 'byteswap-for-linus-20121219' of git://git.infradead.org/users/dwmw2/byteswap
[linux/fpc-iii.git] / fs / proc / base.c
blob5a5a0be40e405f4693bad85fcc7d04703b362523
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/user_namespace.h>
85 #include <linux/fs_struct.h>
86 #include <linux/slab.h>
87 #include <linux/flex_array.h>
88 #ifdef CONFIG_HARDWALL
89 #include <asm/hardwall.h>
90 #endif
91 #include <trace/events/oom.h>
92 #include "internal.h"
93 #include "fd.h"
95 /* NOTE:
96 * Implementing inode permission operations in /proc is almost
97 * certainly an error. Permission checks need to happen during
98 * each system call not at open time. The reason is that most of
99 * what we wish to check for permissions in /proc varies at runtime.
101 * The classic example of a problem is opening file descriptors
102 * in /proc for a task before it execs a suid executable.
105 struct pid_entry {
106 char *name;
107 int len;
108 umode_t mode;
109 const struct inode_operations *iop;
110 const struct file_operations *fop;
111 union proc_op op;
114 #define NOD(NAME, MODE, IOP, FOP, OP) { \
115 .name = (NAME), \
116 .len = sizeof(NAME) - 1, \
117 .mode = MODE, \
118 .iop = IOP, \
119 .fop = FOP, \
120 .op = OP, \
123 #define DIR(NAME, MODE, iops, fops) \
124 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
125 #define LNK(NAME, get_link) \
126 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
127 &proc_pid_link_inode_operations, NULL, \
128 { .proc_get_link = get_link } )
129 #define REG(NAME, MODE, fops) \
130 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
131 #define INF(NAME, MODE, read) \
132 NOD(NAME, (S_IFREG|(MODE)), \
133 NULL, &proc_info_file_operations, \
134 { .proc_read = read } )
135 #define ONE(NAME, MODE, show) \
136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
138 { .proc_show = show } )
141 * Count the number of hardlinks for the pid_entry table, excluding the .
142 * and .. links.
144 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
145 unsigned int n)
147 unsigned int i;
148 unsigned int count;
150 count = 0;
151 for (i = 0; i < n; ++i) {
152 if (S_ISDIR(entries[i].mode))
153 ++count;
156 return count;
159 static int get_task_root(struct task_struct *task, struct path *root)
161 int result = -ENOENT;
163 task_lock(task);
164 if (task->fs) {
165 get_fs_root(task->fs, root);
166 result = 0;
168 task_unlock(task);
169 return result;
172 static int proc_cwd_link(struct dentry *dentry, struct path *path)
174 struct task_struct *task = get_proc_task(dentry->d_inode);
175 int result = -ENOENT;
177 if (task) {
178 task_lock(task);
179 if (task->fs) {
180 get_fs_pwd(task->fs, path);
181 result = 0;
183 task_unlock(task);
184 put_task_struct(task);
186 return result;
189 static int proc_root_link(struct dentry *dentry, struct path *path)
191 struct task_struct *task = get_proc_task(dentry->d_inode);
192 int result = -ENOENT;
194 if (task) {
195 result = get_task_root(task, path);
196 put_task_struct(task);
198 return result;
201 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
203 int res = 0;
204 unsigned int len;
205 struct mm_struct *mm = get_task_mm(task);
206 if (!mm)
207 goto out;
208 if (!mm->arg_end)
209 goto out_mm; /* Shh! No looking before we're done */
211 len = mm->arg_end - mm->arg_start;
213 if (len > PAGE_SIZE)
214 len = PAGE_SIZE;
216 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
218 // If the nul at the end of args has been overwritten, then
219 // assume application is using setproctitle(3).
220 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
221 len = strnlen(buffer, res);
222 if (len < res) {
223 res = len;
224 } else {
225 len = mm->env_end - mm->env_start;
226 if (len > PAGE_SIZE - res)
227 len = PAGE_SIZE - res;
228 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
229 res = strnlen(buffer, res);
232 out_mm:
233 mmput(mm);
234 out:
235 return res;
238 static int proc_pid_auxv(struct task_struct *task, char *buffer)
240 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
241 int res = PTR_ERR(mm);
242 if (mm && !IS_ERR(mm)) {
243 unsigned int nwords = 0;
244 do {
245 nwords += 2;
246 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
247 res = nwords * sizeof(mm->saved_auxv[0]);
248 if (res > PAGE_SIZE)
249 res = PAGE_SIZE;
250 memcpy(buffer, mm->saved_auxv, res);
251 mmput(mm);
253 return res;
257 #ifdef CONFIG_KALLSYMS
259 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
260 * Returns the resolved symbol. If that fails, simply return the address.
262 static int proc_pid_wchan(struct task_struct *task, char *buffer)
264 unsigned long wchan;
265 char symname[KSYM_NAME_LEN];
267 wchan = get_wchan(task);
269 if (lookup_symbol_name(wchan, symname) < 0)
270 if (!ptrace_may_access(task, PTRACE_MODE_READ))
271 return 0;
272 else
273 return sprintf(buffer, "%lu", wchan);
274 else
275 return sprintf(buffer, "%s", symname);
277 #endif /* CONFIG_KALLSYMS */
279 static int lock_trace(struct task_struct *task)
281 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
282 if (err)
283 return err;
284 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
285 mutex_unlock(&task->signal->cred_guard_mutex);
286 return -EPERM;
288 return 0;
291 static void unlock_trace(struct task_struct *task)
293 mutex_unlock(&task->signal->cred_guard_mutex);
296 #ifdef CONFIG_STACKTRACE
298 #define MAX_STACK_TRACE_DEPTH 64
300 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
301 struct pid *pid, struct task_struct *task)
303 struct stack_trace trace;
304 unsigned long *entries;
305 int err;
306 int i;
308 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
309 if (!entries)
310 return -ENOMEM;
312 trace.nr_entries = 0;
313 trace.max_entries = MAX_STACK_TRACE_DEPTH;
314 trace.entries = entries;
315 trace.skip = 0;
317 err = lock_trace(task);
318 if (!err) {
319 save_stack_trace_tsk(task, &trace);
321 for (i = 0; i < trace.nr_entries; i++) {
322 seq_printf(m, "[<%pK>] %pS\n",
323 (void *)entries[i], (void *)entries[i]);
325 unlock_trace(task);
327 kfree(entries);
329 return err;
331 #endif
333 #ifdef CONFIG_SCHEDSTATS
335 * Provides /proc/PID/schedstat
337 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
339 return sprintf(buffer, "%llu %llu %lu\n",
340 (unsigned long long)task->se.sum_exec_runtime,
341 (unsigned long long)task->sched_info.run_delay,
342 task->sched_info.pcount);
344 #endif
346 #ifdef CONFIG_LATENCYTOP
347 static int lstats_show_proc(struct seq_file *m, void *v)
349 int i;
350 struct inode *inode = m->private;
351 struct task_struct *task = get_proc_task(inode);
353 if (!task)
354 return -ESRCH;
355 seq_puts(m, "Latency Top version : v0.1\n");
356 for (i = 0; i < 32; i++) {
357 struct latency_record *lr = &task->latency_record[i];
358 if (lr->backtrace[0]) {
359 int q;
360 seq_printf(m, "%i %li %li",
361 lr->count, lr->time, lr->max);
362 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
363 unsigned long bt = lr->backtrace[q];
364 if (!bt)
365 break;
366 if (bt == ULONG_MAX)
367 break;
368 seq_printf(m, " %ps", (void *)bt);
370 seq_putc(m, '\n');
374 put_task_struct(task);
375 return 0;
378 static int lstats_open(struct inode *inode, struct file *file)
380 return single_open(file, lstats_show_proc, inode);
383 static ssize_t lstats_write(struct file *file, const char __user *buf,
384 size_t count, loff_t *offs)
386 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
388 if (!task)
389 return -ESRCH;
390 clear_all_latency_tracing(task);
391 put_task_struct(task);
393 return count;
396 static const struct file_operations proc_lstats_operations = {
397 .open = lstats_open,
398 .read = seq_read,
399 .write = lstats_write,
400 .llseek = seq_lseek,
401 .release = single_release,
404 #endif
406 static int proc_oom_score(struct task_struct *task, char *buffer)
408 unsigned long totalpages = totalram_pages + total_swap_pages;
409 unsigned long points = 0;
411 read_lock(&tasklist_lock);
412 if (pid_alive(task))
413 points = oom_badness(task, NULL, NULL, totalpages) *
414 1000 / totalpages;
415 read_unlock(&tasklist_lock);
416 return sprintf(buffer, "%lu\n", points);
419 struct limit_names {
420 char *name;
421 char *unit;
424 static const struct limit_names lnames[RLIM_NLIMITS] = {
425 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
426 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
427 [RLIMIT_DATA] = {"Max data size", "bytes"},
428 [RLIMIT_STACK] = {"Max stack size", "bytes"},
429 [RLIMIT_CORE] = {"Max core file size", "bytes"},
430 [RLIMIT_RSS] = {"Max resident set", "bytes"},
431 [RLIMIT_NPROC] = {"Max processes", "processes"},
432 [RLIMIT_NOFILE] = {"Max open files", "files"},
433 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
434 [RLIMIT_AS] = {"Max address space", "bytes"},
435 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
436 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
437 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
438 [RLIMIT_NICE] = {"Max nice priority", NULL},
439 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
440 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
443 /* Display limits for a process */
444 static int proc_pid_limits(struct task_struct *task, char *buffer)
446 unsigned int i;
447 int count = 0;
448 unsigned long flags;
449 char *bufptr = buffer;
451 struct rlimit rlim[RLIM_NLIMITS];
453 if (!lock_task_sighand(task, &flags))
454 return 0;
455 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
456 unlock_task_sighand(task, &flags);
459 * print the file header
461 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
462 "Limit", "Soft Limit", "Hard Limit", "Units");
464 for (i = 0; i < RLIM_NLIMITS; i++) {
465 if (rlim[i].rlim_cur == RLIM_INFINITY)
466 count += sprintf(&bufptr[count], "%-25s %-20s ",
467 lnames[i].name, "unlimited");
468 else
469 count += sprintf(&bufptr[count], "%-25s %-20lu ",
470 lnames[i].name, rlim[i].rlim_cur);
472 if (rlim[i].rlim_max == RLIM_INFINITY)
473 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
474 else
475 count += sprintf(&bufptr[count], "%-20lu ",
476 rlim[i].rlim_max);
478 if (lnames[i].unit)
479 count += sprintf(&bufptr[count], "%-10s\n",
480 lnames[i].unit);
481 else
482 count += sprintf(&bufptr[count], "\n");
485 return count;
488 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
489 static int proc_pid_syscall(struct task_struct *task, char *buffer)
491 long nr;
492 unsigned long args[6], sp, pc;
493 int res = lock_trace(task);
494 if (res)
495 return res;
497 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
498 res = sprintf(buffer, "running\n");
499 else if (nr < 0)
500 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
501 else
502 res = sprintf(buffer,
503 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
505 args[0], args[1], args[2], args[3], args[4], args[5],
506 sp, pc);
507 unlock_trace(task);
508 return res;
510 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
512 /************************************************************************/
513 /* Here the fs part begins */
514 /************************************************************************/
516 /* permission checks */
517 static int proc_fd_access_allowed(struct inode *inode)
519 struct task_struct *task;
520 int allowed = 0;
521 /* Allow access to a task's file descriptors if it is us or we
522 * may use ptrace attach to the process and find out that
523 * information.
525 task = get_proc_task(inode);
526 if (task) {
527 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
528 put_task_struct(task);
530 return allowed;
533 int proc_setattr(struct dentry *dentry, struct iattr *attr)
535 int error;
536 struct inode *inode = dentry->d_inode;
538 if (attr->ia_valid & ATTR_MODE)
539 return -EPERM;
541 error = inode_change_ok(inode, attr);
542 if (error)
543 return error;
545 if ((attr->ia_valid & ATTR_SIZE) &&
546 attr->ia_size != i_size_read(inode)) {
547 error = vmtruncate(inode, attr->ia_size);
548 if (error)
549 return error;
552 setattr_copy(inode, attr);
553 mark_inode_dirty(inode);
554 return 0;
558 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
559 * or euid/egid (for hide_pid_min=2)?
561 static bool has_pid_permissions(struct pid_namespace *pid,
562 struct task_struct *task,
563 int hide_pid_min)
565 if (pid->hide_pid < hide_pid_min)
566 return true;
567 if (in_group_p(pid->pid_gid))
568 return true;
569 return ptrace_may_access(task, PTRACE_MODE_READ);
573 static int proc_pid_permission(struct inode *inode, int mask)
575 struct pid_namespace *pid = inode->i_sb->s_fs_info;
576 struct task_struct *task;
577 bool has_perms;
579 task = get_proc_task(inode);
580 if (!task)
581 return -ESRCH;
582 has_perms = has_pid_permissions(pid, task, 1);
583 put_task_struct(task);
585 if (!has_perms) {
586 if (pid->hide_pid == 2) {
588 * Let's make getdents(), stat(), and open()
589 * consistent with each other. If a process
590 * may not stat() a file, it shouldn't be seen
591 * in procfs at all.
593 return -ENOENT;
596 return -EPERM;
598 return generic_permission(inode, mask);
603 static const struct inode_operations proc_def_inode_operations = {
604 .setattr = proc_setattr,
607 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
609 static ssize_t proc_info_read(struct file * file, char __user * buf,
610 size_t count, loff_t *ppos)
612 struct inode * inode = file->f_path.dentry->d_inode;
613 unsigned long page;
614 ssize_t length;
615 struct task_struct *task = get_proc_task(inode);
617 length = -ESRCH;
618 if (!task)
619 goto out_no_task;
621 if (count > PROC_BLOCK_SIZE)
622 count = PROC_BLOCK_SIZE;
624 length = -ENOMEM;
625 if (!(page = __get_free_page(GFP_TEMPORARY)))
626 goto out;
628 length = PROC_I(inode)->op.proc_read(task, (char*)page);
630 if (length >= 0)
631 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
632 free_page(page);
633 out:
634 put_task_struct(task);
635 out_no_task:
636 return length;
639 static const struct file_operations proc_info_file_operations = {
640 .read = proc_info_read,
641 .llseek = generic_file_llseek,
644 static int proc_single_show(struct seq_file *m, void *v)
646 struct inode *inode = m->private;
647 struct pid_namespace *ns;
648 struct pid *pid;
649 struct task_struct *task;
650 int ret;
652 ns = inode->i_sb->s_fs_info;
653 pid = proc_pid(inode);
654 task = get_pid_task(pid, PIDTYPE_PID);
655 if (!task)
656 return -ESRCH;
658 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
660 put_task_struct(task);
661 return ret;
664 static int proc_single_open(struct inode *inode, struct file *filp)
666 return single_open(filp, proc_single_show, inode);
669 static const struct file_operations proc_single_file_operations = {
670 .open = proc_single_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = single_release,
676 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
678 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
679 struct mm_struct *mm;
681 if (!task)
682 return -ESRCH;
684 mm = mm_access(task, mode);
685 put_task_struct(task);
687 if (IS_ERR(mm))
688 return PTR_ERR(mm);
690 if (mm) {
691 /* ensure this mm_struct can't be freed */
692 atomic_inc(&mm->mm_count);
693 /* but do not pin its memory */
694 mmput(mm);
697 file->private_data = mm;
699 return 0;
702 static int mem_open(struct inode *inode, struct file *file)
704 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
706 /* OK to pass negative loff_t, we can catch out-of-range */
707 file->f_mode |= FMODE_UNSIGNED_OFFSET;
709 return ret;
712 static ssize_t mem_rw(struct file *file, char __user *buf,
713 size_t count, loff_t *ppos, int write)
715 struct mm_struct *mm = file->private_data;
716 unsigned long addr = *ppos;
717 ssize_t copied;
718 char *page;
720 if (!mm)
721 return 0;
723 page = (char *)__get_free_page(GFP_TEMPORARY);
724 if (!page)
725 return -ENOMEM;
727 copied = 0;
728 if (!atomic_inc_not_zero(&mm->mm_users))
729 goto free;
731 while (count > 0) {
732 int this_len = min_t(int, count, PAGE_SIZE);
734 if (write && copy_from_user(page, buf, this_len)) {
735 copied = -EFAULT;
736 break;
739 this_len = access_remote_vm(mm, addr, page, this_len, write);
740 if (!this_len) {
741 if (!copied)
742 copied = -EIO;
743 break;
746 if (!write && copy_to_user(buf, page, this_len)) {
747 copied = -EFAULT;
748 break;
751 buf += this_len;
752 addr += this_len;
753 copied += this_len;
754 count -= this_len;
756 *ppos = addr;
758 mmput(mm);
759 free:
760 free_page((unsigned long) page);
761 return copied;
764 static ssize_t mem_read(struct file *file, char __user *buf,
765 size_t count, loff_t *ppos)
767 return mem_rw(file, buf, count, ppos, 0);
770 static ssize_t mem_write(struct file *file, const char __user *buf,
771 size_t count, loff_t *ppos)
773 return mem_rw(file, (char __user*)buf, count, ppos, 1);
776 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
778 switch (orig) {
779 case 0:
780 file->f_pos = offset;
781 break;
782 case 1:
783 file->f_pos += offset;
784 break;
785 default:
786 return -EINVAL;
788 force_successful_syscall_return();
789 return file->f_pos;
792 static int mem_release(struct inode *inode, struct file *file)
794 struct mm_struct *mm = file->private_data;
795 if (mm)
796 mmdrop(mm);
797 return 0;
800 static const struct file_operations proc_mem_operations = {
801 .llseek = mem_lseek,
802 .read = mem_read,
803 .write = mem_write,
804 .open = mem_open,
805 .release = mem_release,
808 static int environ_open(struct inode *inode, struct file *file)
810 return __mem_open(inode, file, PTRACE_MODE_READ);
813 static ssize_t environ_read(struct file *file, char __user *buf,
814 size_t count, loff_t *ppos)
816 char *page;
817 unsigned long src = *ppos;
818 int ret = 0;
819 struct mm_struct *mm = file->private_data;
821 if (!mm)
822 return 0;
824 page = (char *)__get_free_page(GFP_TEMPORARY);
825 if (!page)
826 return -ENOMEM;
828 ret = 0;
829 if (!atomic_inc_not_zero(&mm->mm_users))
830 goto free;
831 while (count > 0) {
832 size_t this_len, max_len;
833 int retval;
835 if (src >= (mm->env_end - mm->env_start))
836 break;
838 this_len = mm->env_end - (mm->env_start + src);
840 max_len = min_t(size_t, PAGE_SIZE, count);
841 this_len = min(max_len, this_len);
843 retval = access_remote_vm(mm, (mm->env_start + src),
844 page, this_len, 0);
846 if (retval <= 0) {
847 ret = retval;
848 break;
851 if (copy_to_user(buf, page, retval)) {
852 ret = -EFAULT;
853 break;
856 ret += retval;
857 src += retval;
858 buf += retval;
859 count -= retval;
861 *ppos = src;
862 mmput(mm);
864 free:
865 free_page((unsigned long) page);
866 return ret;
869 static const struct file_operations proc_environ_operations = {
870 .open = environ_open,
871 .read = environ_read,
872 .llseek = generic_file_llseek,
873 .release = mem_release,
876 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
877 loff_t *ppos)
879 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
880 char buffer[PROC_NUMBUF];
881 int oom_adj = OOM_ADJUST_MIN;
882 size_t len;
883 unsigned long flags;
885 if (!task)
886 return -ESRCH;
887 if (lock_task_sighand(task, &flags)) {
888 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
889 oom_adj = OOM_ADJUST_MAX;
890 else
891 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
892 OOM_SCORE_ADJ_MAX;
893 unlock_task_sighand(task, &flags);
895 put_task_struct(task);
896 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
897 return simple_read_from_buffer(buf, count, ppos, buffer, len);
900 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
901 size_t count, loff_t *ppos)
903 struct task_struct *task;
904 char buffer[PROC_NUMBUF];
905 int oom_adj;
906 unsigned long flags;
907 int err;
909 memset(buffer, 0, sizeof(buffer));
910 if (count > sizeof(buffer) - 1)
911 count = sizeof(buffer) - 1;
912 if (copy_from_user(buffer, buf, count)) {
913 err = -EFAULT;
914 goto out;
917 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
918 if (err)
919 goto out;
920 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
921 oom_adj != OOM_DISABLE) {
922 err = -EINVAL;
923 goto out;
926 task = get_proc_task(file->f_path.dentry->d_inode);
927 if (!task) {
928 err = -ESRCH;
929 goto out;
932 task_lock(task);
933 if (!task->mm) {
934 err = -EINVAL;
935 goto err_task_lock;
938 if (!lock_task_sighand(task, &flags)) {
939 err = -ESRCH;
940 goto err_task_lock;
944 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
945 * value is always attainable.
947 if (oom_adj == OOM_ADJUST_MAX)
948 oom_adj = OOM_SCORE_ADJ_MAX;
949 else
950 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
952 if (oom_adj < task->signal->oom_score_adj &&
953 !capable(CAP_SYS_RESOURCE)) {
954 err = -EACCES;
955 goto err_sighand;
959 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
960 * /proc/pid/oom_score_adj instead.
962 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
963 current->comm, task_pid_nr(current), task_pid_nr(task),
964 task_pid_nr(task));
966 task->signal->oom_score_adj = oom_adj;
967 trace_oom_score_adj_update(task);
968 err_sighand:
969 unlock_task_sighand(task, &flags);
970 err_task_lock:
971 task_unlock(task);
972 put_task_struct(task);
973 out:
974 return err < 0 ? err : count;
977 static const struct file_operations proc_oom_adj_operations = {
978 .read = oom_adj_read,
979 .write = oom_adj_write,
980 .llseek = generic_file_llseek,
983 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
984 size_t count, loff_t *ppos)
986 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
987 char buffer[PROC_NUMBUF];
988 short oom_score_adj = OOM_SCORE_ADJ_MIN;
989 unsigned long flags;
990 size_t len;
992 if (!task)
993 return -ESRCH;
994 if (lock_task_sighand(task, &flags)) {
995 oom_score_adj = task->signal->oom_score_adj;
996 unlock_task_sighand(task, &flags);
998 put_task_struct(task);
999 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
1000 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1003 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1004 size_t count, loff_t *ppos)
1006 struct task_struct *task;
1007 char buffer[PROC_NUMBUF];
1008 unsigned long flags;
1009 int oom_score_adj;
1010 int err;
1012 memset(buffer, 0, sizeof(buffer));
1013 if (count > sizeof(buffer) - 1)
1014 count = sizeof(buffer) - 1;
1015 if (copy_from_user(buffer, buf, count)) {
1016 err = -EFAULT;
1017 goto out;
1020 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1021 if (err)
1022 goto out;
1023 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1024 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1025 err = -EINVAL;
1026 goto out;
1029 task = get_proc_task(file->f_path.dentry->d_inode);
1030 if (!task) {
1031 err = -ESRCH;
1032 goto out;
1035 task_lock(task);
1036 if (!task->mm) {
1037 err = -EINVAL;
1038 goto err_task_lock;
1041 if (!lock_task_sighand(task, &flags)) {
1042 err = -ESRCH;
1043 goto err_task_lock;
1046 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1047 !capable(CAP_SYS_RESOURCE)) {
1048 err = -EACCES;
1049 goto err_sighand;
1052 task->signal->oom_score_adj = (short)oom_score_adj;
1053 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1054 task->signal->oom_score_adj_min = (short)oom_score_adj;
1055 trace_oom_score_adj_update(task);
1057 err_sighand:
1058 unlock_task_sighand(task, &flags);
1059 err_task_lock:
1060 task_unlock(task);
1061 put_task_struct(task);
1062 out:
1063 return err < 0 ? err : count;
1066 static const struct file_operations proc_oom_score_adj_operations = {
1067 .read = oom_score_adj_read,
1068 .write = oom_score_adj_write,
1069 .llseek = default_llseek,
1072 #ifdef CONFIG_AUDITSYSCALL
1073 #define TMPBUFLEN 21
1074 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1075 size_t count, loff_t *ppos)
1077 struct inode * inode = file->f_path.dentry->d_inode;
1078 struct task_struct *task = get_proc_task(inode);
1079 ssize_t length;
1080 char tmpbuf[TMPBUFLEN];
1082 if (!task)
1083 return -ESRCH;
1084 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1085 from_kuid(file->f_cred->user_ns,
1086 audit_get_loginuid(task)));
1087 put_task_struct(task);
1088 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1091 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1092 size_t count, loff_t *ppos)
1094 struct inode * inode = file->f_path.dentry->d_inode;
1095 char *page, *tmp;
1096 ssize_t length;
1097 uid_t loginuid;
1098 kuid_t kloginuid;
1100 rcu_read_lock();
1101 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1102 rcu_read_unlock();
1103 return -EPERM;
1105 rcu_read_unlock();
1107 if (count >= PAGE_SIZE)
1108 count = PAGE_SIZE - 1;
1110 if (*ppos != 0) {
1111 /* No partial writes. */
1112 return -EINVAL;
1114 page = (char*)__get_free_page(GFP_TEMPORARY);
1115 if (!page)
1116 return -ENOMEM;
1117 length = -EFAULT;
1118 if (copy_from_user(page, buf, count))
1119 goto out_free_page;
1121 page[count] = '\0';
1122 loginuid = simple_strtoul(page, &tmp, 10);
1123 if (tmp == page) {
1124 length = -EINVAL;
1125 goto out_free_page;
1128 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1129 if (!uid_valid(kloginuid)) {
1130 length = -EINVAL;
1131 goto out_free_page;
1134 length = audit_set_loginuid(kloginuid);
1135 if (likely(length == 0))
1136 length = count;
1138 out_free_page:
1139 free_page((unsigned long) page);
1140 return length;
1143 static const struct file_operations proc_loginuid_operations = {
1144 .read = proc_loginuid_read,
1145 .write = proc_loginuid_write,
1146 .llseek = generic_file_llseek,
1149 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1150 size_t count, loff_t *ppos)
1152 struct inode * inode = file->f_path.dentry->d_inode;
1153 struct task_struct *task = get_proc_task(inode);
1154 ssize_t length;
1155 char tmpbuf[TMPBUFLEN];
1157 if (!task)
1158 return -ESRCH;
1159 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1160 audit_get_sessionid(task));
1161 put_task_struct(task);
1162 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1165 static const struct file_operations proc_sessionid_operations = {
1166 .read = proc_sessionid_read,
1167 .llseek = generic_file_llseek,
1169 #endif
1171 #ifdef CONFIG_FAULT_INJECTION
1172 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1173 size_t count, loff_t *ppos)
1175 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1176 char buffer[PROC_NUMBUF];
1177 size_t len;
1178 int make_it_fail;
1180 if (!task)
1181 return -ESRCH;
1182 make_it_fail = task->make_it_fail;
1183 put_task_struct(task);
1185 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1187 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1190 static ssize_t proc_fault_inject_write(struct file * file,
1191 const char __user * buf, size_t count, loff_t *ppos)
1193 struct task_struct *task;
1194 char buffer[PROC_NUMBUF], *end;
1195 int make_it_fail;
1197 if (!capable(CAP_SYS_RESOURCE))
1198 return -EPERM;
1199 memset(buffer, 0, sizeof(buffer));
1200 if (count > sizeof(buffer) - 1)
1201 count = sizeof(buffer) - 1;
1202 if (copy_from_user(buffer, buf, count))
1203 return -EFAULT;
1204 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1205 if (*end)
1206 return -EINVAL;
1207 task = get_proc_task(file->f_dentry->d_inode);
1208 if (!task)
1209 return -ESRCH;
1210 task->make_it_fail = make_it_fail;
1211 put_task_struct(task);
1213 return count;
1216 static const struct file_operations proc_fault_inject_operations = {
1217 .read = proc_fault_inject_read,
1218 .write = proc_fault_inject_write,
1219 .llseek = generic_file_llseek,
1221 #endif
1224 #ifdef CONFIG_SCHED_DEBUG
1226 * Print out various scheduling related per-task fields:
1228 static int sched_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_show_task(p, m);
1238 put_task_struct(p);
1240 return 0;
1243 static ssize_t
1244 sched_write(struct file *file, const char __user *buf,
1245 size_t count, loff_t *offset)
1247 struct inode *inode = file->f_path.dentry->d_inode;
1248 struct task_struct *p;
1250 p = get_proc_task(inode);
1251 if (!p)
1252 return -ESRCH;
1253 proc_sched_set_task(p);
1255 put_task_struct(p);
1257 return count;
1260 static int sched_open(struct inode *inode, struct file *filp)
1262 return single_open(filp, sched_show, inode);
1265 static const struct file_operations proc_pid_sched_operations = {
1266 .open = sched_open,
1267 .read = seq_read,
1268 .write = sched_write,
1269 .llseek = seq_lseek,
1270 .release = single_release,
1273 #endif
1275 #ifdef CONFIG_SCHED_AUTOGROUP
1277 * Print out autogroup related information:
1279 static int sched_autogroup_show(struct seq_file *m, void *v)
1281 struct inode *inode = m->private;
1282 struct task_struct *p;
1284 p = get_proc_task(inode);
1285 if (!p)
1286 return -ESRCH;
1287 proc_sched_autogroup_show_task(p, m);
1289 put_task_struct(p);
1291 return 0;
1294 static ssize_t
1295 sched_autogroup_write(struct file *file, const char __user *buf,
1296 size_t count, loff_t *offset)
1298 struct inode *inode = file->f_path.dentry->d_inode;
1299 struct task_struct *p;
1300 char buffer[PROC_NUMBUF];
1301 int nice;
1302 int err;
1304 memset(buffer, 0, sizeof(buffer));
1305 if (count > sizeof(buffer) - 1)
1306 count = sizeof(buffer) - 1;
1307 if (copy_from_user(buffer, buf, count))
1308 return -EFAULT;
1310 err = kstrtoint(strstrip(buffer), 0, &nice);
1311 if (err < 0)
1312 return err;
1314 p = get_proc_task(inode);
1315 if (!p)
1316 return -ESRCH;
1318 err = proc_sched_autogroup_set_nice(p, nice);
1319 if (err)
1320 count = err;
1322 put_task_struct(p);
1324 return count;
1327 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1329 int ret;
1331 ret = single_open(filp, sched_autogroup_show, NULL);
1332 if (!ret) {
1333 struct seq_file *m = filp->private_data;
1335 m->private = inode;
1337 return ret;
1340 static const struct file_operations proc_pid_sched_autogroup_operations = {
1341 .open = sched_autogroup_open,
1342 .read = seq_read,
1343 .write = sched_autogroup_write,
1344 .llseek = seq_lseek,
1345 .release = single_release,
1348 #endif /* CONFIG_SCHED_AUTOGROUP */
1350 static ssize_t comm_write(struct file *file, const char __user *buf,
1351 size_t count, loff_t *offset)
1353 struct inode *inode = file->f_path.dentry->d_inode;
1354 struct task_struct *p;
1355 char buffer[TASK_COMM_LEN];
1357 memset(buffer, 0, sizeof(buffer));
1358 if (count > sizeof(buffer) - 1)
1359 count = sizeof(buffer) - 1;
1360 if (copy_from_user(buffer, buf, count))
1361 return -EFAULT;
1363 p = get_proc_task(inode);
1364 if (!p)
1365 return -ESRCH;
1367 if (same_thread_group(current, p))
1368 set_task_comm(p, buffer);
1369 else
1370 count = -EINVAL;
1372 put_task_struct(p);
1374 return count;
1377 static int comm_show(struct seq_file *m, void *v)
1379 struct inode *inode = m->private;
1380 struct task_struct *p;
1382 p = get_proc_task(inode);
1383 if (!p)
1384 return -ESRCH;
1386 task_lock(p);
1387 seq_printf(m, "%s\n", p->comm);
1388 task_unlock(p);
1390 put_task_struct(p);
1392 return 0;
1395 static int comm_open(struct inode *inode, struct file *filp)
1397 return single_open(filp, comm_show, inode);
1400 static const struct file_operations proc_pid_set_comm_operations = {
1401 .open = comm_open,
1402 .read = seq_read,
1403 .write = comm_write,
1404 .llseek = seq_lseek,
1405 .release = single_release,
1408 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1410 struct task_struct *task;
1411 struct mm_struct *mm;
1412 struct file *exe_file;
1414 task = get_proc_task(dentry->d_inode);
1415 if (!task)
1416 return -ENOENT;
1417 mm = get_task_mm(task);
1418 put_task_struct(task);
1419 if (!mm)
1420 return -ENOENT;
1421 exe_file = get_mm_exe_file(mm);
1422 mmput(mm);
1423 if (exe_file) {
1424 *exe_path = exe_file->f_path;
1425 path_get(&exe_file->f_path);
1426 fput(exe_file);
1427 return 0;
1428 } else
1429 return -ENOENT;
1432 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1434 struct inode *inode = dentry->d_inode;
1435 struct path path;
1436 int error = -EACCES;
1438 /* Are we allowed to snoop on the tasks file descriptors? */
1439 if (!proc_fd_access_allowed(inode))
1440 goto out;
1442 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1443 if (error)
1444 goto out;
1446 nd_jump_link(nd, &path);
1447 return NULL;
1448 out:
1449 return ERR_PTR(error);
1452 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1454 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1455 char *pathname;
1456 int len;
1458 if (!tmp)
1459 return -ENOMEM;
1461 pathname = d_path(path, tmp, PAGE_SIZE);
1462 len = PTR_ERR(pathname);
1463 if (IS_ERR(pathname))
1464 goto out;
1465 len = tmp + PAGE_SIZE - 1 - pathname;
1467 if (len > buflen)
1468 len = buflen;
1469 if (copy_to_user(buffer, pathname, len))
1470 len = -EFAULT;
1471 out:
1472 free_page((unsigned long)tmp);
1473 return len;
1476 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1478 int error = -EACCES;
1479 struct inode *inode = dentry->d_inode;
1480 struct path path;
1482 /* Are we allowed to snoop on the tasks file descriptors? */
1483 if (!proc_fd_access_allowed(inode))
1484 goto out;
1486 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1487 if (error)
1488 goto out;
1490 error = do_proc_readlink(&path, buffer, buflen);
1491 path_put(&path);
1492 out:
1493 return error;
1496 const struct inode_operations proc_pid_link_inode_operations = {
1497 .readlink = proc_pid_readlink,
1498 .follow_link = proc_pid_follow_link,
1499 .setattr = proc_setattr,
1503 /* building an inode */
1505 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1507 struct inode * inode;
1508 struct proc_inode *ei;
1509 const struct cred *cred;
1511 /* We need a new inode */
1513 inode = new_inode(sb);
1514 if (!inode)
1515 goto out;
1517 /* Common stuff */
1518 ei = PROC_I(inode);
1519 inode->i_ino = get_next_ino();
1520 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1521 inode->i_op = &proc_def_inode_operations;
1524 * grab the reference to task.
1526 ei->pid = get_task_pid(task, PIDTYPE_PID);
1527 if (!ei->pid)
1528 goto out_unlock;
1530 if (task_dumpable(task)) {
1531 rcu_read_lock();
1532 cred = __task_cred(task);
1533 inode->i_uid = cred->euid;
1534 inode->i_gid = cred->egid;
1535 rcu_read_unlock();
1537 security_task_to_inode(task, inode);
1539 out:
1540 return inode;
1542 out_unlock:
1543 iput(inode);
1544 return NULL;
1547 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1549 struct inode *inode = dentry->d_inode;
1550 struct task_struct *task;
1551 const struct cred *cred;
1552 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1554 generic_fillattr(inode, stat);
1556 rcu_read_lock();
1557 stat->uid = GLOBAL_ROOT_UID;
1558 stat->gid = GLOBAL_ROOT_GID;
1559 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1560 if (task) {
1561 if (!has_pid_permissions(pid, task, 2)) {
1562 rcu_read_unlock();
1564 * This doesn't prevent learning whether PID exists,
1565 * it only makes getattr() consistent with readdir().
1567 return -ENOENT;
1569 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1570 task_dumpable(task)) {
1571 cred = __task_cred(task);
1572 stat->uid = cred->euid;
1573 stat->gid = cred->egid;
1576 rcu_read_unlock();
1577 return 0;
1580 /* dentry stuff */
1583 * Exceptional case: normally we are not allowed to unhash a busy
1584 * directory. In this case, however, we can do it - no aliasing problems
1585 * due to the way we treat inodes.
1587 * Rewrite the inode's ownerships here because the owning task may have
1588 * performed a setuid(), etc.
1590 * Before the /proc/pid/status file was created the only way to read
1591 * the effective uid of a /process was to stat /proc/pid. Reading
1592 * /proc/pid/status is slow enough that procps and other packages
1593 * kept stating /proc/pid. To keep the rules in /proc simple I have
1594 * made this apply to all per process world readable and executable
1595 * directories.
1597 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1599 struct inode *inode;
1600 struct task_struct *task;
1601 const struct cred *cred;
1603 if (flags & LOOKUP_RCU)
1604 return -ECHILD;
1606 inode = dentry->d_inode;
1607 task = get_proc_task(inode);
1609 if (task) {
1610 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1611 task_dumpable(task)) {
1612 rcu_read_lock();
1613 cred = __task_cred(task);
1614 inode->i_uid = cred->euid;
1615 inode->i_gid = cred->egid;
1616 rcu_read_unlock();
1617 } else {
1618 inode->i_uid = GLOBAL_ROOT_UID;
1619 inode->i_gid = GLOBAL_ROOT_GID;
1621 inode->i_mode &= ~(S_ISUID | S_ISGID);
1622 security_task_to_inode(task, inode);
1623 put_task_struct(task);
1624 return 1;
1626 d_drop(dentry);
1627 return 0;
1630 const struct dentry_operations pid_dentry_operations =
1632 .d_revalidate = pid_revalidate,
1633 .d_delete = pid_delete_dentry,
1636 /* Lookups */
1639 * Fill a directory entry.
1641 * If possible create the dcache entry and derive our inode number and
1642 * file type from dcache entry.
1644 * Since all of the proc inode numbers are dynamically generated, the inode
1645 * numbers do not exist until the inode is cache. This means creating the
1646 * the dcache entry in readdir is necessary to keep the inode numbers
1647 * reported by readdir in sync with the inode numbers reported
1648 * by stat.
1650 int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1651 const char *name, int len,
1652 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1654 struct dentry *child, *dir = filp->f_path.dentry;
1655 struct inode *inode;
1656 struct qstr qname;
1657 ino_t ino = 0;
1658 unsigned type = DT_UNKNOWN;
1660 qname.name = name;
1661 qname.len = len;
1662 qname.hash = full_name_hash(name, len);
1664 child = d_lookup(dir, &qname);
1665 if (!child) {
1666 struct dentry *new;
1667 new = d_alloc(dir, &qname);
1668 if (new) {
1669 child = instantiate(dir->d_inode, new, task, ptr);
1670 if (child)
1671 dput(new);
1672 else
1673 child = new;
1676 if (!child || IS_ERR(child) || !child->d_inode)
1677 goto end_instantiate;
1678 inode = child->d_inode;
1679 if (inode) {
1680 ino = inode->i_ino;
1681 type = inode->i_mode >> 12;
1683 dput(child);
1684 end_instantiate:
1685 if (!ino)
1686 ino = find_inode_number(dir, &qname);
1687 if (!ino)
1688 ino = 1;
1689 return filldir(dirent, name, len, filp->f_pos, ino, type);
1692 #ifdef CONFIG_CHECKPOINT_RESTORE
1695 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1696 * which represent vma start and end addresses.
1698 static int dname_to_vma_addr(struct dentry *dentry,
1699 unsigned long *start, unsigned long *end)
1701 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1702 return -EINVAL;
1704 return 0;
1707 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1709 unsigned long vm_start, vm_end;
1710 bool exact_vma_exists = false;
1711 struct mm_struct *mm = NULL;
1712 struct task_struct *task;
1713 const struct cred *cred;
1714 struct inode *inode;
1715 int status = 0;
1717 if (flags & LOOKUP_RCU)
1718 return -ECHILD;
1720 if (!capable(CAP_SYS_ADMIN)) {
1721 status = -EACCES;
1722 goto out_notask;
1725 inode = dentry->d_inode;
1726 task = get_proc_task(inode);
1727 if (!task)
1728 goto out_notask;
1730 mm = mm_access(task, PTRACE_MODE_READ);
1731 if (IS_ERR_OR_NULL(mm))
1732 goto out;
1734 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1735 down_read(&mm->mmap_sem);
1736 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1737 up_read(&mm->mmap_sem);
1740 mmput(mm);
1742 if (exact_vma_exists) {
1743 if (task_dumpable(task)) {
1744 rcu_read_lock();
1745 cred = __task_cred(task);
1746 inode->i_uid = cred->euid;
1747 inode->i_gid = cred->egid;
1748 rcu_read_unlock();
1749 } else {
1750 inode->i_uid = GLOBAL_ROOT_UID;
1751 inode->i_gid = GLOBAL_ROOT_GID;
1753 security_task_to_inode(task, inode);
1754 status = 1;
1757 out:
1758 put_task_struct(task);
1760 out_notask:
1761 if (status <= 0)
1762 d_drop(dentry);
1764 return status;
1767 static const struct dentry_operations tid_map_files_dentry_operations = {
1768 .d_revalidate = map_files_d_revalidate,
1769 .d_delete = pid_delete_dentry,
1772 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1774 unsigned long vm_start, vm_end;
1775 struct vm_area_struct *vma;
1776 struct task_struct *task;
1777 struct mm_struct *mm;
1778 int rc;
1780 rc = -ENOENT;
1781 task = get_proc_task(dentry->d_inode);
1782 if (!task)
1783 goto out;
1785 mm = get_task_mm(task);
1786 put_task_struct(task);
1787 if (!mm)
1788 goto out;
1790 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1791 if (rc)
1792 goto out_mmput;
1794 down_read(&mm->mmap_sem);
1795 vma = find_exact_vma(mm, vm_start, vm_end);
1796 if (vma && vma->vm_file) {
1797 *path = vma->vm_file->f_path;
1798 path_get(path);
1799 rc = 0;
1801 up_read(&mm->mmap_sem);
1803 out_mmput:
1804 mmput(mm);
1805 out:
1806 return rc;
1809 struct map_files_info {
1810 fmode_t mode;
1811 unsigned long len;
1812 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1815 static struct dentry *
1816 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1817 struct task_struct *task, const void *ptr)
1819 fmode_t mode = (fmode_t)(unsigned long)ptr;
1820 struct proc_inode *ei;
1821 struct inode *inode;
1823 inode = proc_pid_make_inode(dir->i_sb, task);
1824 if (!inode)
1825 return ERR_PTR(-ENOENT);
1827 ei = PROC_I(inode);
1828 ei->op.proc_get_link = proc_map_files_get_link;
1830 inode->i_op = &proc_pid_link_inode_operations;
1831 inode->i_size = 64;
1832 inode->i_mode = S_IFLNK;
1834 if (mode & FMODE_READ)
1835 inode->i_mode |= S_IRUSR;
1836 if (mode & FMODE_WRITE)
1837 inode->i_mode |= S_IWUSR;
1839 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1840 d_add(dentry, inode);
1842 return NULL;
1845 static struct dentry *proc_map_files_lookup(struct inode *dir,
1846 struct dentry *dentry, unsigned int flags)
1848 unsigned long vm_start, vm_end;
1849 struct vm_area_struct *vma;
1850 struct task_struct *task;
1851 struct dentry *result;
1852 struct mm_struct *mm;
1854 result = ERR_PTR(-EACCES);
1855 if (!capable(CAP_SYS_ADMIN))
1856 goto out;
1858 result = ERR_PTR(-ENOENT);
1859 task = get_proc_task(dir);
1860 if (!task)
1861 goto out;
1863 result = ERR_PTR(-EACCES);
1864 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1865 goto out_put_task;
1867 result = ERR_PTR(-ENOENT);
1868 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1869 goto out_put_task;
1871 mm = get_task_mm(task);
1872 if (!mm)
1873 goto out_put_task;
1875 down_read(&mm->mmap_sem);
1876 vma = find_exact_vma(mm, vm_start, vm_end);
1877 if (!vma)
1878 goto out_no_vma;
1880 if (vma->vm_file)
1881 result = proc_map_files_instantiate(dir, dentry, task,
1882 (void *)(unsigned long)vma->vm_file->f_mode);
1884 out_no_vma:
1885 up_read(&mm->mmap_sem);
1886 mmput(mm);
1887 out_put_task:
1888 put_task_struct(task);
1889 out:
1890 return result;
1893 static const struct inode_operations proc_map_files_inode_operations = {
1894 .lookup = proc_map_files_lookup,
1895 .permission = proc_fd_permission,
1896 .setattr = proc_setattr,
1899 static int
1900 proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1902 struct dentry *dentry = filp->f_path.dentry;
1903 struct inode *inode = dentry->d_inode;
1904 struct vm_area_struct *vma;
1905 struct task_struct *task;
1906 struct mm_struct *mm;
1907 ino_t ino;
1908 int ret;
1910 ret = -EACCES;
1911 if (!capable(CAP_SYS_ADMIN))
1912 goto out;
1914 ret = -ENOENT;
1915 task = get_proc_task(inode);
1916 if (!task)
1917 goto out;
1919 ret = -EACCES;
1920 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1921 goto out_put_task;
1923 ret = 0;
1924 switch (filp->f_pos) {
1925 case 0:
1926 ino = inode->i_ino;
1927 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
1928 goto out_put_task;
1929 filp->f_pos++;
1930 case 1:
1931 ino = parent_ino(dentry);
1932 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1933 goto out_put_task;
1934 filp->f_pos++;
1935 default:
1937 unsigned long nr_files, pos, i;
1938 struct flex_array *fa = NULL;
1939 struct map_files_info info;
1940 struct map_files_info *p;
1942 mm = get_task_mm(task);
1943 if (!mm)
1944 goto out_put_task;
1945 down_read(&mm->mmap_sem);
1947 nr_files = 0;
1950 * We need two passes here:
1952 * 1) Collect vmas of mapped files with mmap_sem taken
1953 * 2) Release mmap_sem and instantiate entries
1955 * otherwise we get lockdep complained, since filldir()
1956 * routine might require mmap_sem taken in might_fault().
1959 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1960 if (vma->vm_file && ++pos > filp->f_pos)
1961 nr_files++;
1964 if (nr_files) {
1965 fa = flex_array_alloc(sizeof(info), nr_files,
1966 GFP_KERNEL);
1967 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1968 GFP_KERNEL)) {
1969 ret = -ENOMEM;
1970 if (fa)
1971 flex_array_free(fa);
1972 up_read(&mm->mmap_sem);
1973 mmput(mm);
1974 goto out_put_task;
1976 for (i = 0, vma = mm->mmap, pos = 2; vma;
1977 vma = vma->vm_next) {
1978 if (!vma->vm_file)
1979 continue;
1980 if (++pos <= filp->f_pos)
1981 continue;
1983 info.mode = vma->vm_file->f_mode;
1984 info.len = snprintf(info.name,
1985 sizeof(info.name), "%lx-%lx",
1986 vma->vm_start, vma->vm_end);
1987 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1988 BUG();
1991 up_read(&mm->mmap_sem);
1993 for (i = 0; i < nr_files; i++) {
1994 p = flex_array_get(fa, i);
1995 ret = proc_fill_cache(filp, dirent, filldir,
1996 p->name, p->len,
1997 proc_map_files_instantiate,
1998 task,
1999 (void *)(unsigned long)p->mode);
2000 if (ret)
2001 break;
2002 filp->f_pos++;
2004 if (fa)
2005 flex_array_free(fa);
2006 mmput(mm);
2010 out_put_task:
2011 put_task_struct(task);
2012 out:
2013 return ret;
2016 static const struct file_operations proc_map_files_operations = {
2017 .read = generic_read_dir,
2018 .readdir = proc_map_files_readdir,
2019 .llseek = default_llseek,
2022 #endif /* CONFIG_CHECKPOINT_RESTORE */
2024 static struct dentry *proc_pident_instantiate(struct inode *dir,
2025 struct dentry *dentry, struct task_struct *task, const void *ptr)
2027 const struct pid_entry *p = ptr;
2028 struct inode *inode;
2029 struct proc_inode *ei;
2030 struct dentry *error = ERR_PTR(-ENOENT);
2032 inode = proc_pid_make_inode(dir->i_sb, task);
2033 if (!inode)
2034 goto out;
2036 ei = PROC_I(inode);
2037 inode->i_mode = p->mode;
2038 if (S_ISDIR(inode->i_mode))
2039 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2040 if (p->iop)
2041 inode->i_op = p->iop;
2042 if (p->fop)
2043 inode->i_fop = p->fop;
2044 ei->op = p->op;
2045 d_set_d_op(dentry, &pid_dentry_operations);
2046 d_add(dentry, inode);
2047 /* Close the race of the process dying before we return the dentry */
2048 if (pid_revalidate(dentry, 0))
2049 error = NULL;
2050 out:
2051 return error;
2054 static struct dentry *proc_pident_lookup(struct inode *dir,
2055 struct dentry *dentry,
2056 const struct pid_entry *ents,
2057 unsigned int nents)
2059 struct dentry *error;
2060 struct task_struct *task = get_proc_task(dir);
2061 const struct pid_entry *p, *last;
2063 error = ERR_PTR(-ENOENT);
2065 if (!task)
2066 goto out_no_task;
2069 * Yes, it does not scale. And it should not. Don't add
2070 * new entries into /proc/<tgid>/ without very good reasons.
2072 last = &ents[nents - 1];
2073 for (p = ents; p <= last; p++) {
2074 if (p->len != dentry->d_name.len)
2075 continue;
2076 if (!memcmp(dentry->d_name.name, p->name, p->len))
2077 break;
2079 if (p > last)
2080 goto out;
2082 error = proc_pident_instantiate(dir, dentry, task, p);
2083 out:
2084 put_task_struct(task);
2085 out_no_task:
2086 return error;
2089 static int proc_pident_fill_cache(struct file *filp, void *dirent,
2090 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2092 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2093 proc_pident_instantiate, task, p);
2096 static int proc_pident_readdir(struct file *filp,
2097 void *dirent, filldir_t filldir,
2098 const struct pid_entry *ents, unsigned int nents)
2100 int i;
2101 struct dentry *dentry = filp->f_path.dentry;
2102 struct inode *inode = dentry->d_inode;
2103 struct task_struct *task = get_proc_task(inode);
2104 const struct pid_entry *p, *last;
2105 ino_t ino;
2106 int ret;
2108 ret = -ENOENT;
2109 if (!task)
2110 goto out_no_task;
2112 ret = 0;
2113 i = filp->f_pos;
2114 switch (i) {
2115 case 0:
2116 ino = inode->i_ino;
2117 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2118 goto out;
2119 i++;
2120 filp->f_pos++;
2121 /* fall through */
2122 case 1:
2123 ino = parent_ino(dentry);
2124 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2125 goto out;
2126 i++;
2127 filp->f_pos++;
2128 /* fall through */
2129 default:
2130 i -= 2;
2131 if (i >= nents) {
2132 ret = 1;
2133 goto out;
2135 p = ents + i;
2136 last = &ents[nents - 1];
2137 while (p <= last) {
2138 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
2139 goto out;
2140 filp->f_pos++;
2141 p++;
2145 ret = 1;
2146 out:
2147 put_task_struct(task);
2148 out_no_task:
2149 return ret;
2152 #ifdef CONFIG_SECURITY
2153 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2154 size_t count, loff_t *ppos)
2156 struct inode * inode = file->f_path.dentry->d_inode;
2157 char *p = NULL;
2158 ssize_t length;
2159 struct task_struct *task = get_proc_task(inode);
2161 if (!task)
2162 return -ESRCH;
2164 length = security_getprocattr(task,
2165 (char*)file->f_path.dentry->d_name.name,
2166 &p);
2167 put_task_struct(task);
2168 if (length > 0)
2169 length = simple_read_from_buffer(buf, count, ppos, p, length);
2170 kfree(p);
2171 return length;
2174 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2175 size_t count, loff_t *ppos)
2177 struct inode * inode = file->f_path.dentry->d_inode;
2178 char *page;
2179 ssize_t length;
2180 struct task_struct *task = get_proc_task(inode);
2182 length = -ESRCH;
2183 if (!task)
2184 goto out_no_task;
2185 if (count > PAGE_SIZE)
2186 count = PAGE_SIZE;
2188 /* No partial writes. */
2189 length = -EINVAL;
2190 if (*ppos != 0)
2191 goto out;
2193 length = -ENOMEM;
2194 page = (char*)__get_free_page(GFP_TEMPORARY);
2195 if (!page)
2196 goto out;
2198 length = -EFAULT;
2199 if (copy_from_user(page, buf, count))
2200 goto out_free;
2202 /* Guard against adverse ptrace interaction */
2203 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2204 if (length < 0)
2205 goto out_free;
2207 length = security_setprocattr(task,
2208 (char*)file->f_path.dentry->d_name.name,
2209 (void*)page, count);
2210 mutex_unlock(&task->signal->cred_guard_mutex);
2211 out_free:
2212 free_page((unsigned long) page);
2213 out:
2214 put_task_struct(task);
2215 out_no_task:
2216 return length;
2219 static const struct file_operations proc_pid_attr_operations = {
2220 .read = proc_pid_attr_read,
2221 .write = proc_pid_attr_write,
2222 .llseek = generic_file_llseek,
2225 static const struct pid_entry attr_dir_stuff[] = {
2226 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2227 REG("prev", S_IRUGO, proc_pid_attr_operations),
2228 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2229 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2230 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2231 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2234 static int proc_attr_dir_readdir(struct file * filp,
2235 void * dirent, filldir_t filldir)
2237 return proc_pident_readdir(filp,dirent,filldir,
2238 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
2241 static const struct file_operations proc_attr_dir_operations = {
2242 .read = generic_read_dir,
2243 .readdir = proc_attr_dir_readdir,
2244 .llseek = default_llseek,
2247 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2248 struct dentry *dentry, unsigned int flags)
2250 return proc_pident_lookup(dir, dentry,
2251 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2254 static const struct inode_operations proc_attr_dir_inode_operations = {
2255 .lookup = proc_attr_dir_lookup,
2256 .getattr = pid_getattr,
2257 .setattr = proc_setattr,
2260 #endif
2262 #ifdef CONFIG_ELF_CORE
2263 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2264 size_t count, loff_t *ppos)
2266 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2267 struct mm_struct *mm;
2268 char buffer[PROC_NUMBUF];
2269 size_t len;
2270 int ret;
2272 if (!task)
2273 return -ESRCH;
2275 ret = 0;
2276 mm = get_task_mm(task);
2277 if (mm) {
2278 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2279 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2280 MMF_DUMP_FILTER_SHIFT));
2281 mmput(mm);
2282 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2285 put_task_struct(task);
2287 return ret;
2290 static ssize_t proc_coredump_filter_write(struct file *file,
2291 const char __user *buf,
2292 size_t count,
2293 loff_t *ppos)
2295 struct task_struct *task;
2296 struct mm_struct *mm;
2297 char buffer[PROC_NUMBUF], *end;
2298 unsigned int val;
2299 int ret;
2300 int i;
2301 unsigned long mask;
2303 ret = -EFAULT;
2304 memset(buffer, 0, sizeof(buffer));
2305 if (count > sizeof(buffer) - 1)
2306 count = sizeof(buffer) - 1;
2307 if (copy_from_user(buffer, buf, count))
2308 goto out_no_task;
2310 ret = -EINVAL;
2311 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2312 if (*end == '\n')
2313 end++;
2314 if (end - buffer == 0)
2315 goto out_no_task;
2317 ret = -ESRCH;
2318 task = get_proc_task(file->f_dentry->d_inode);
2319 if (!task)
2320 goto out_no_task;
2322 ret = end - buffer;
2323 mm = get_task_mm(task);
2324 if (!mm)
2325 goto out_no_mm;
2327 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2328 if (val & mask)
2329 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2330 else
2331 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2334 mmput(mm);
2335 out_no_mm:
2336 put_task_struct(task);
2337 out_no_task:
2338 return ret;
2341 static const struct file_operations proc_coredump_filter_operations = {
2342 .read = proc_coredump_filter_read,
2343 .write = proc_coredump_filter_write,
2344 .llseek = generic_file_llseek,
2346 #endif
2348 #ifdef CONFIG_TASK_IO_ACCOUNTING
2349 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2351 struct task_io_accounting acct = task->ioac;
2352 unsigned long flags;
2353 int result;
2355 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2356 if (result)
2357 return result;
2359 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2360 result = -EACCES;
2361 goto out_unlock;
2364 if (whole && lock_task_sighand(task, &flags)) {
2365 struct task_struct *t = task;
2367 task_io_accounting_add(&acct, &task->signal->ioac);
2368 while_each_thread(task, t)
2369 task_io_accounting_add(&acct, &t->ioac);
2371 unlock_task_sighand(task, &flags);
2373 result = sprintf(buffer,
2374 "rchar: %llu\n"
2375 "wchar: %llu\n"
2376 "syscr: %llu\n"
2377 "syscw: %llu\n"
2378 "read_bytes: %llu\n"
2379 "write_bytes: %llu\n"
2380 "cancelled_write_bytes: %llu\n",
2381 (unsigned long long)acct.rchar,
2382 (unsigned long long)acct.wchar,
2383 (unsigned long long)acct.syscr,
2384 (unsigned long long)acct.syscw,
2385 (unsigned long long)acct.read_bytes,
2386 (unsigned long long)acct.write_bytes,
2387 (unsigned long long)acct.cancelled_write_bytes);
2388 out_unlock:
2389 mutex_unlock(&task->signal->cred_guard_mutex);
2390 return result;
2393 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2395 return do_io_accounting(task, buffer, 0);
2398 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2400 return do_io_accounting(task, buffer, 1);
2402 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2404 #ifdef CONFIG_USER_NS
2405 static int proc_id_map_open(struct inode *inode, struct file *file,
2406 struct seq_operations *seq_ops)
2408 struct user_namespace *ns = NULL;
2409 struct task_struct *task;
2410 struct seq_file *seq;
2411 int ret = -EINVAL;
2413 task = get_proc_task(inode);
2414 if (task) {
2415 rcu_read_lock();
2416 ns = get_user_ns(task_cred_xxx(task, user_ns));
2417 rcu_read_unlock();
2418 put_task_struct(task);
2420 if (!ns)
2421 goto err;
2423 ret = seq_open(file, seq_ops);
2424 if (ret)
2425 goto err_put_ns;
2427 seq = file->private_data;
2428 seq->private = ns;
2430 return 0;
2431 err_put_ns:
2432 put_user_ns(ns);
2433 err:
2434 return ret;
2437 static int proc_id_map_release(struct inode *inode, struct file *file)
2439 struct seq_file *seq = file->private_data;
2440 struct user_namespace *ns = seq->private;
2441 put_user_ns(ns);
2442 return seq_release(inode, file);
2445 static int proc_uid_map_open(struct inode *inode, struct file *file)
2447 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2450 static int proc_gid_map_open(struct inode *inode, struct file *file)
2452 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2455 static int proc_projid_map_open(struct inode *inode, struct file *file)
2457 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2460 static const struct file_operations proc_uid_map_operations = {
2461 .open = proc_uid_map_open,
2462 .write = proc_uid_map_write,
2463 .read = seq_read,
2464 .llseek = seq_lseek,
2465 .release = proc_id_map_release,
2468 static const struct file_operations proc_gid_map_operations = {
2469 .open = proc_gid_map_open,
2470 .write = proc_gid_map_write,
2471 .read = seq_read,
2472 .llseek = seq_lseek,
2473 .release = proc_id_map_release,
2476 static const struct file_operations proc_projid_map_operations = {
2477 .open = proc_projid_map_open,
2478 .write = proc_projid_map_write,
2479 .read = seq_read,
2480 .llseek = seq_lseek,
2481 .release = proc_id_map_release,
2483 #endif /* CONFIG_USER_NS */
2485 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2486 struct pid *pid, struct task_struct *task)
2488 int err = lock_trace(task);
2489 if (!err) {
2490 seq_printf(m, "%08x\n", task->personality);
2491 unlock_trace(task);
2493 return err;
2497 * Thread groups
2499 static const struct file_operations proc_task_operations;
2500 static const struct inode_operations proc_task_inode_operations;
2502 static const struct pid_entry tgid_base_stuff[] = {
2503 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2504 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2505 #ifdef CONFIG_CHECKPOINT_RESTORE
2506 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2507 #endif
2508 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2509 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2510 #ifdef CONFIG_NET
2511 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2512 #endif
2513 REG("environ", S_IRUSR, proc_environ_operations),
2514 INF("auxv", S_IRUSR, proc_pid_auxv),
2515 ONE("status", S_IRUGO, proc_pid_status),
2516 ONE("personality", S_IRUGO, proc_pid_personality),
2517 INF("limits", S_IRUGO, proc_pid_limits),
2518 #ifdef CONFIG_SCHED_DEBUG
2519 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2520 #endif
2521 #ifdef CONFIG_SCHED_AUTOGROUP
2522 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2523 #endif
2524 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2525 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2526 INF("syscall", S_IRUGO, proc_pid_syscall),
2527 #endif
2528 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2529 ONE("stat", S_IRUGO, proc_tgid_stat),
2530 ONE("statm", S_IRUGO, proc_pid_statm),
2531 REG("maps", S_IRUGO, proc_pid_maps_operations),
2532 #ifdef CONFIG_NUMA
2533 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2534 #endif
2535 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2536 LNK("cwd", proc_cwd_link),
2537 LNK("root", proc_root_link),
2538 LNK("exe", proc_exe_link),
2539 REG("mounts", S_IRUGO, proc_mounts_operations),
2540 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2541 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2542 #ifdef CONFIG_PROC_PAGE_MONITOR
2543 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2544 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2545 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2546 #endif
2547 #ifdef CONFIG_SECURITY
2548 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2549 #endif
2550 #ifdef CONFIG_KALLSYMS
2551 INF("wchan", S_IRUGO, proc_pid_wchan),
2552 #endif
2553 #ifdef CONFIG_STACKTRACE
2554 ONE("stack", S_IRUGO, proc_pid_stack),
2555 #endif
2556 #ifdef CONFIG_SCHEDSTATS
2557 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2558 #endif
2559 #ifdef CONFIG_LATENCYTOP
2560 REG("latency", S_IRUGO, proc_lstats_operations),
2561 #endif
2562 #ifdef CONFIG_PROC_PID_CPUSET
2563 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2564 #endif
2565 #ifdef CONFIG_CGROUPS
2566 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2567 #endif
2568 INF("oom_score", S_IRUGO, proc_oom_score),
2569 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2570 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2571 #ifdef CONFIG_AUDITSYSCALL
2572 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2573 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2574 #endif
2575 #ifdef CONFIG_FAULT_INJECTION
2576 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2577 #endif
2578 #ifdef CONFIG_ELF_CORE
2579 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2580 #endif
2581 #ifdef CONFIG_TASK_IO_ACCOUNTING
2582 INF("io", S_IRUSR, proc_tgid_io_accounting),
2583 #endif
2584 #ifdef CONFIG_HARDWALL
2585 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2586 #endif
2587 #ifdef CONFIG_USER_NS
2588 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2589 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2590 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2591 #endif
2594 static int proc_tgid_base_readdir(struct file * filp,
2595 void * dirent, filldir_t filldir)
2597 return proc_pident_readdir(filp,dirent,filldir,
2598 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2601 static const struct file_operations proc_tgid_base_operations = {
2602 .read = generic_read_dir,
2603 .readdir = proc_tgid_base_readdir,
2604 .llseek = default_llseek,
2607 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2609 return proc_pident_lookup(dir, dentry,
2610 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2613 static const struct inode_operations proc_tgid_base_inode_operations = {
2614 .lookup = proc_tgid_base_lookup,
2615 .getattr = pid_getattr,
2616 .setattr = proc_setattr,
2617 .permission = proc_pid_permission,
2620 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2622 struct dentry *dentry, *leader, *dir;
2623 char buf[PROC_NUMBUF];
2624 struct qstr name;
2626 name.name = buf;
2627 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2628 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2629 if (dentry) {
2630 shrink_dcache_parent(dentry);
2631 d_drop(dentry);
2632 dput(dentry);
2635 name.name = buf;
2636 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2637 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2638 if (!leader)
2639 goto out;
2641 name.name = "task";
2642 name.len = strlen(name.name);
2643 dir = d_hash_and_lookup(leader, &name);
2644 if (!dir)
2645 goto out_put_leader;
2647 name.name = buf;
2648 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2649 dentry = d_hash_and_lookup(dir, &name);
2650 if (dentry) {
2651 shrink_dcache_parent(dentry);
2652 d_drop(dentry);
2653 dput(dentry);
2656 dput(dir);
2657 out_put_leader:
2658 dput(leader);
2659 out:
2660 return;
2664 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2665 * @task: task that should be flushed.
2667 * When flushing dentries from proc, one needs to flush them from global
2668 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2669 * in. This call is supposed to do all of this job.
2671 * Looks in the dcache for
2672 * /proc/@pid
2673 * /proc/@tgid/task/@pid
2674 * if either directory is present flushes it and all of it'ts children
2675 * from the dcache.
2677 * It is safe and reasonable to cache /proc entries for a task until
2678 * that task exits. After that they just clog up the dcache with
2679 * useless entries, possibly causing useful dcache entries to be
2680 * flushed instead. This routine is proved to flush those useless
2681 * dcache entries at process exit time.
2683 * NOTE: This routine is just an optimization so it does not guarantee
2684 * that no dcache entries will exist at process exit time it
2685 * just makes it very unlikely that any will persist.
2688 void proc_flush_task(struct task_struct *task)
2690 int i;
2691 struct pid *pid, *tgid;
2692 struct upid *upid;
2694 pid = task_pid(task);
2695 tgid = task_tgid(task);
2697 for (i = 0; i <= pid->level; i++) {
2698 upid = &pid->numbers[i];
2699 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2700 tgid->numbers[i].nr);
2704 static struct dentry *proc_pid_instantiate(struct inode *dir,
2705 struct dentry * dentry,
2706 struct task_struct *task, const void *ptr)
2708 struct dentry *error = ERR_PTR(-ENOENT);
2709 struct inode *inode;
2711 inode = proc_pid_make_inode(dir->i_sb, task);
2712 if (!inode)
2713 goto out;
2715 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2716 inode->i_op = &proc_tgid_base_inode_operations;
2717 inode->i_fop = &proc_tgid_base_operations;
2718 inode->i_flags|=S_IMMUTABLE;
2720 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2721 ARRAY_SIZE(tgid_base_stuff)));
2723 d_set_d_op(dentry, &pid_dentry_operations);
2725 d_add(dentry, inode);
2726 /* Close the race of the process dying before we return the dentry */
2727 if (pid_revalidate(dentry, 0))
2728 error = NULL;
2729 out:
2730 return error;
2733 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2735 struct dentry *result = NULL;
2736 struct task_struct *task;
2737 unsigned tgid;
2738 struct pid_namespace *ns;
2740 tgid = name_to_int(dentry);
2741 if (tgid == ~0U)
2742 goto out;
2744 ns = dentry->d_sb->s_fs_info;
2745 rcu_read_lock();
2746 task = find_task_by_pid_ns(tgid, ns);
2747 if (task)
2748 get_task_struct(task);
2749 rcu_read_unlock();
2750 if (!task)
2751 goto out;
2753 result = proc_pid_instantiate(dir, dentry, task, NULL);
2754 put_task_struct(task);
2755 out:
2756 return result;
2760 * Find the first task with tgid >= tgid
2763 struct tgid_iter {
2764 unsigned int tgid;
2765 struct task_struct *task;
2767 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2769 struct pid *pid;
2771 if (iter.task)
2772 put_task_struct(iter.task);
2773 rcu_read_lock();
2774 retry:
2775 iter.task = NULL;
2776 pid = find_ge_pid(iter.tgid, ns);
2777 if (pid) {
2778 iter.tgid = pid_nr_ns(pid, ns);
2779 iter.task = pid_task(pid, PIDTYPE_PID);
2780 /* What we to know is if the pid we have find is the
2781 * pid of a thread_group_leader. Testing for task
2782 * being a thread_group_leader is the obvious thing
2783 * todo but there is a window when it fails, due to
2784 * the pid transfer logic in de_thread.
2786 * So we perform the straight forward test of seeing
2787 * if the pid we have found is the pid of a thread
2788 * group leader, and don't worry if the task we have
2789 * found doesn't happen to be a thread group leader.
2790 * As we don't care in the case of readdir.
2792 if (!iter.task || !has_group_leader_pid(iter.task)) {
2793 iter.tgid += 1;
2794 goto retry;
2796 get_task_struct(iter.task);
2798 rcu_read_unlock();
2799 return iter;
2802 #define TGID_OFFSET (FIRST_PROCESS_ENTRY)
2804 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2805 struct tgid_iter iter)
2807 char name[PROC_NUMBUF];
2808 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2809 return proc_fill_cache(filp, dirent, filldir, name, len,
2810 proc_pid_instantiate, iter.task, NULL);
2813 static int fake_filldir(void *buf, const char *name, int namelen,
2814 loff_t offset, u64 ino, unsigned d_type)
2816 return 0;
2819 /* for the /proc/ directory itself, after non-process stuff has been done */
2820 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2822 struct tgid_iter iter;
2823 struct pid_namespace *ns;
2824 filldir_t __filldir;
2826 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
2827 goto out;
2829 ns = filp->f_dentry->d_sb->s_fs_info;
2830 iter.task = NULL;
2831 iter.tgid = filp->f_pos - TGID_OFFSET;
2832 for (iter = next_tgid(ns, iter);
2833 iter.task;
2834 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2835 if (has_pid_permissions(ns, iter.task, 2))
2836 __filldir = filldir;
2837 else
2838 __filldir = fake_filldir;
2840 filp->f_pos = iter.tgid + TGID_OFFSET;
2841 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
2842 put_task_struct(iter.task);
2843 goto out;
2846 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2847 out:
2848 return 0;
2852 * Tasks
2854 static const struct pid_entry tid_base_stuff[] = {
2855 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2856 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2857 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2858 REG("environ", S_IRUSR, proc_environ_operations),
2859 INF("auxv", S_IRUSR, proc_pid_auxv),
2860 ONE("status", S_IRUGO, proc_pid_status),
2861 ONE("personality", S_IRUGO, proc_pid_personality),
2862 INF("limits", S_IRUGO, proc_pid_limits),
2863 #ifdef CONFIG_SCHED_DEBUG
2864 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2865 #endif
2866 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2867 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2868 INF("syscall", S_IRUGO, proc_pid_syscall),
2869 #endif
2870 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2871 ONE("stat", S_IRUGO, proc_tid_stat),
2872 ONE("statm", S_IRUGO, proc_pid_statm),
2873 REG("maps", S_IRUGO, proc_tid_maps_operations),
2874 #ifdef CONFIG_CHECKPOINT_RESTORE
2875 REG("children", S_IRUGO, proc_tid_children_operations),
2876 #endif
2877 #ifdef CONFIG_NUMA
2878 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2879 #endif
2880 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2881 LNK("cwd", proc_cwd_link),
2882 LNK("root", proc_root_link),
2883 LNK("exe", proc_exe_link),
2884 REG("mounts", S_IRUGO, proc_mounts_operations),
2885 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2886 #ifdef CONFIG_PROC_PAGE_MONITOR
2887 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2888 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2889 REG("pagemap", S_IRUGO, proc_pagemap_operations),
2890 #endif
2891 #ifdef CONFIG_SECURITY
2892 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2893 #endif
2894 #ifdef CONFIG_KALLSYMS
2895 INF("wchan", S_IRUGO, proc_pid_wchan),
2896 #endif
2897 #ifdef CONFIG_STACKTRACE
2898 ONE("stack", S_IRUGO, proc_pid_stack),
2899 #endif
2900 #ifdef CONFIG_SCHEDSTATS
2901 INF("schedstat", S_IRUGO, proc_pid_schedstat),
2902 #endif
2903 #ifdef CONFIG_LATENCYTOP
2904 REG("latency", S_IRUGO, proc_lstats_operations),
2905 #endif
2906 #ifdef CONFIG_PROC_PID_CPUSET
2907 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2908 #endif
2909 #ifdef CONFIG_CGROUPS
2910 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2911 #endif
2912 INF("oom_score", S_IRUGO, proc_oom_score),
2913 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2914 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2915 #ifdef CONFIG_AUDITSYSCALL
2916 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2917 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2918 #endif
2919 #ifdef CONFIG_FAULT_INJECTION
2920 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2921 #endif
2922 #ifdef CONFIG_TASK_IO_ACCOUNTING
2923 INF("io", S_IRUSR, proc_tid_io_accounting),
2924 #endif
2925 #ifdef CONFIG_HARDWALL
2926 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2927 #endif
2928 #ifdef CONFIG_USER_NS
2929 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2930 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2931 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2932 #endif
2935 static int proc_tid_base_readdir(struct file * filp,
2936 void * dirent, filldir_t filldir)
2938 return proc_pident_readdir(filp,dirent,filldir,
2939 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2942 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2944 return proc_pident_lookup(dir, dentry,
2945 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2948 static const struct file_operations proc_tid_base_operations = {
2949 .read = generic_read_dir,
2950 .readdir = proc_tid_base_readdir,
2951 .llseek = default_llseek,
2954 static const struct inode_operations proc_tid_base_inode_operations = {
2955 .lookup = proc_tid_base_lookup,
2956 .getattr = pid_getattr,
2957 .setattr = proc_setattr,
2960 static struct dentry *proc_task_instantiate(struct inode *dir,
2961 struct dentry *dentry, struct task_struct *task, const void *ptr)
2963 struct dentry *error = ERR_PTR(-ENOENT);
2964 struct inode *inode;
2965 inode = proc_pid_make_inode(dir->i_sb, task);
2967 if (!inode)
2968 goto out;
2969 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2970 inode->i_op = &proc_tid_base_inode_operations;
2971 inode->i_fop = &proc_tid_base_operations;
2972 inode->i_flags|=S_IMMUTABLE;
2974 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2975 ARRAY_SIZE(tid_base_stuff)));
2977 d_set_d_op(dentry, &pid_dentry_operations);
2979 d_add(dentry, inode);
2980 /* Close the race of the process dying before we return the dentry */
2981 if (pid_revalidate(dentry, 0))
2982 error = NULL;
2983 out:
2984 return error;
2987 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2989 struct dentry *result = ERR_PTR(-ENOENT);
2990 struct task_struct *task;
2991 struct task_struct *leader = get_proc_task(dir);
2992 unsigned tid;
2993 struct pid_namespace *ns;
2995 if (!leader)
2996 goto out_no_task;
2998 tid = name_to_int(dentry);
2999 if (tid == ~0U)
3000 goto out;
3002 ns = dentry->d_sb->s_fs_info;
3003 rcu_read_lock();
3004 task = find_task_by_pid_ns(tid, ns);
3005 if (task)
3006 get_task_struct(task);
3007 rcu_read_unlock();
3008 if (!task)
3009 goto out;
3010 if (!same_thread_group(leader, task))
3011 goto out_drop_task;
3013 result = proc_task_instantiate(dir, dentry, task, NULL);
3014 out_drop_task:
3015 put_task_struct(task);
3016 out:
3017 put_task_struct(leader);
3018 out_no_task:
3019 return result;
3023 * Find the first tid of a thread group to return to user space.
3025 * Usually this is just the thread group leader, but if the users
3026 * buffer was too small or there was a seek into the middle of the
3027 * directory we have more work todo.
3029 * In the case of a short read we start with find_task_by_pid.
3031 * In the case of a seek we start with the leader and walk nr
3032 * threads past it.
3034 static struct task_struct *first_tid(struct task_struct *leader,
3035 int tid, int nr, struct pid_namespace *ns)
3037 struct task_struct *pos;
3039 rcu_read_lock();
3040 /* Attempt to start with the pid of a thread */
3041 if (tid && (nr > 0)) {
3042 pos = find_task_by_pid_ns(tid, ns);
3043 if (pos && (pos->group_leader == leader))
3044 goto found;
3047 /* If nr exceeds the number of threads there is nothing todo */
3048 pos = NULL;
3049 if (nr && nr >= get_nr_threads(leader))
3050 goto out;
3052 /* If we haven't found our starting place yet start
3053 * with the leader and walk nr threads forward.
3055 for (pos = leader; nr > 0; --nr) {
3056 pos = next_thread(pos);
3057 if (pos == leader) {
3058 pos = NULL;
3059 goto out;
3062 found:
3063 get_task_struct(pos);
3064 out:
3065 rcu_read_unlock();
3066 return pos;
3070 * Find the next thread in the thread list.
3071 * Return NULL if there is an error or no next thread.
3073 * The reference to the input task_struct is released.
3075 static struct task_struct *next_tid(struct task_struct *start)
3077 struct task_struct *pos = NULL;
3078 rcu_read_lock();
3079 if (pid_alive(start)) {
3080 pos = next_thread(start);
3081 if (thread_group_leader(pos))
3082 pos = NULL;
3083 else
3084 get_task_struct(pos);
3086 rcu_read_unlock();
3087 put_task_struct(start);
3088 return pos;
3091 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3092 struct task_struct *task, int tid)
3094 char name[PROC_NUMBUF];
3095 int len = snprintf(name, sizeof(name), "%d", tid);
3096 return proc_fill_cache(filp, dirent, filldir, name, len,
3097 proc_task_instantiate, task, NULL);
3100 /* for the /proc/TGID/task/ directories */
3101 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3103 struct dentry *dentry = filp->f_path.dentry;
3104 struct inode *inode = dentry->d_inode;
3105 struct task_struct *leader = NULL;
3106 struct task_struct *task;
3107 int retval = -ENOENT;
3108 ino_t ino;
3109 int tid;
3110 struct pid_namespace *ns;
3112 task = get_proc_task(inode);
3113 if (!task)
3114 goto out_no_task;
3115 rcu_read_lock();
3116 if (pid_alive(task)) {
3117 leader = task->group_leader;
3118 get_task_struct(leader);
3120 rcu_read_unlock();
3121 put_task_struct(task);
3122 if (!leader)
3123 goto out_no_task;
3124 retval = 0;
3126 switch ((unsigned long)filp->f_pos) {
3127 case 0:
3128 ino = inode->i_ino;
3129 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
3130 goto out;
3131 filp->f_pos++;
3132 /* fall through */
3133 case 1:
3134 ino = parent_ino(dentry);
3135 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
3136 goto out;
3137 filp->f_pos++;
3138 /* fall through */
3141 /* f_version caches the tgid value that the last readdir call couldn't
3142 * return. lseek aka telldir automagically resets f_version to 0.
3144 ns = filp->f_dentry->d_sb->s_fs_info;
3145 tid = (int)filp->f_version;
3146 filp->f_version = 0;
3147 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
3148 task;
3149 task = next_tid(task), filp->f_pos++) {
3150 tid = task_pid_nr_ns(task, ns);
3151 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
3152 /* returning this tgid failed, save it as the first
3153 * pid for the next readir call */
3154 filp->f_version = (u64)tid;
3155 put_task_struct(task);
3156 break;
3159 out:
3160 put_task_struct(leader);
3161 out_no_task:
3162 return retval;
3165 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3167 struct inode *inode = dentry->d_inode;
3168 struct task_struct *p = get_proc_task(inode);
3169 generic_fillattr(inode, stat);
3171 if (p) {
3172 stat->nlink += get_nr_threads(p);
3173 put_task_struct(p);
3176 return 0;
3179 static const struct inode_operations proc_task_inode_operations = {
3180 .lookup = proc_task_lookup,
3181 .getattr = proc_task_getattr,
3182 .setattr = proc_setattr,
3183 .permission = proc_pid_permission,
3186 static const struct file_operations proc_task_operations = {
3187 .read = generic_read_dir,
3188 .readdir = proc_task_readdir,
3189 .llseek = default_llseek,