]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/proc/base.c
proc: convert /proc/$PID/schedstat to seq_file interface
[mirror_ubuntu-bionic-kernel.git] / fs / proc / base.c
1 /*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
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.
14 *
15 *
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>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
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.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
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.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
48 */
49
50 #include <asm/uaccess.h>
51
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"
96
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.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
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;
114 };
115
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, \
123 }
124
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 INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
141
142 /*
143 * Count the number of hardlinks for the pid_entry table, excluding the .
144 * and .. links.
145 */
146 static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 unsigned int n)
148 {
149 unsigned int i;
150 unsigned int count;
151
152 count = 0;
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
155 ++count;
156 }
157
158 return count;
159 }
160
161 static int get_task_root(struct task_struct *task, struct path *root)
162 {
163 int result = -ENOENT;
164
165 task_lock(task);
166 if (task->fs) {
167 get_fs_root(task->fs, root);
168 result = 0;
169 }
170 task_unlock(task);
171 return result;
172 }
173
174 static int proc_cwd_link(struct dentry *dentry, struct path *path)
175 {
176 struct task_struct *task = get_proc_task(dentry->d_inode);
177 int result = -ENOENT;
178
179 if (task) {
180 task_lock(task);
181 if (task->fs) {
182 get_fs_pwd(task->fs, path);
183 result = 0;
184 }
185 task_unlock(task);
186 put_task_struct(task);
187 }
188 return result;
189 }
190
191 static int proc_root_link(struct dentry *dentry, struct path *path)
192 {
193 struct task_struct *task = get_proc_task(dentry->d_inode);
194 int result = -ENOENT;
195
196 if (task) {
197 result = get_task_root(task, path);
198 put_task_struct(task);
199 }
200 return result;
201 }
202
203 static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
204 struct pid *pid, struct task_struct *task)
205 {
206 /*
207 * Rely on struct seq_operations::show() being called once
208 * per internal buffer allocation. See single_open(), traverse().
209 */
210 BUG_ON(m->size < PAGE_SIZE);
211 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
212 return 0;
213 }
214
215 static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
216 struct pid *pid, struct task_struct *task)
217 {
218 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
219 if (mm && !IS_ERR(mm)) {
220 unsigned int nwords = 0;
221 do {
222 nwords += 2;
223 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
224 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
225 mmput(mm);
226 return 0;
227 } else
228 return PTR_ERR(mm);
229 }
230
231
232 #ifdef CONFIG_KALLSYMS
233 /*
234 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
235 * Returns the resolved symbol. If that fails, simply return the address.
236 */
237 static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
238 struct pid *pid, struct task_struct *task)
239 {
240 unsigned long wchan;
241 char symname[KSYM_NAME_LEN];
242
243 wchan = get_wchan(task);
244
245 if (lookup_symbol_name(wchan, symname) < 0)
246 if (!ptrace_may_access(task, PTRACE_MODE_READ))
247 return 0;
248 else
249 return seq_printf(m, "%lu", wchan);
250 else
251 return seq_printf(m, "%s", symname);
252 }
253 #endif /* CONFIG_KALLSYMS */
254
255 static int lock_trace(struct task_struct *task)
256 {
257 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
258 if (err)
259 return err;
260 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
261 mutex_unlock(&task->signal->cred_guard_mutex);
262 return -EPERM;
263 }
264 return 0;
265 }
266
267 static void unlock_trace(struct task_struct *task)
268 {
269 mutex_unlock(&task->signal->cred_guard_mutex);
270 }
271
272 #ifdef CONFIG_STACKTRACE
273
274 #define MAX_STACK_TRACE_DEPTH 64
275
276 static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
277 struct pid *pid, struct task_struct *task)
278 {
279 struct stack_trace trace;
280 unsigned long *entries;
281 int err;
282 int i;
283
284 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
285 if (!entries)
286 return -ENOMEM;
287
288 trace.nr_entries = 0;
289 trace.max_entries = MAX_STACK_TRACE_DEPTH;
290 trace.entries = entries;
291 trace.skip = 0;
292
293 err = lock_trace(task);
294 if (!err) {
295 save_stack_trace_tsk(task, &trace);
296
297 for (i = 0; i < trace.nr_entries; i++) {
298 seq_printf(m, "[<%pK>] %pS\n",
299 (void *)entries[i], (void *)entries[i]);
300 }
301 unlock_trace(task);
302 }
303 kfree(entries);
304
305 return err;
306 }
307 #endif
308
309 #ifdef CONFIG_SCHEDSTATS
310 /*
311 * Provides /proc/PID/schedstat
312 */
313 static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
314 struct pid *pid, struct task_struct *task)
315 {
316 return seq_printf(m, "%llu %llu %lu\n",
317 (unsigned long long)task->se.sum_exec_runtime,
318 (unsigned long long)task->sched_info.run_delay,
319 task->sched_info.pcount);
320 }
321 #endif
322
323 #ifdef CONFIG_LATENCYTOP
324 static int lstats_show_proc(struct seq_file *m, void *v)
325 {
326 int i;
327 struct inode *inode = m->private;
328 struct task_struct *task = get_proc_task(inode);
329
330 if (!task)
331 return -ESRCH;
332 seq_puts(m, "Latency Top version : v0.1\n");
333 for (i = 0; i < 32; i++) {
334 struct latency_record *lr = &task->latency_record[i];
335 if (lr->backtrace[0]) {
336 int q;
337 seq_printf(m, "%i %li %li",
338 lr->count, lr->time, lr->max);
339 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
340 unsigned long bt = lr->backtrace[q];
341 if (!bt)
342 break;
343 if (bt == ULONG_MAX)
344 break;
345 seq_printf(m, " %ps", (void *)bt);
346 }
347 seq_putc(m, '\n');
348 }
349
350 }
351 put_task_struct(task);
352 return 0;
353 }
354
355 static int lstats_open(struct inode *inode, struct file *file)
356 {
357 return single_open(file, lstats_show_proc, inode);
358 }
359
360 static ssize_t lstats_write(struct file *file, const char __user *buf,
361 size_t count, loff_t *offs)
362 {
363 struct task_struct *task = get_proc_task(file_inode(file));
364
365 if (!task)
366 return -ESRCH;
367 clear_all_latency_tracing(task);
368 put_task_struct(task);
369
370 return count;
371 }
372
373 static const struct file_operations proc_lstats_operations = {
374 .open = lstats_open,
375 .read = seq_read,
376 .write = lstats_write,
377 .llseek = seq_lseek,
378 .release = single_release,
379 };
380
381 #endif
382
383 #ifdef CONFIG_CGROUPS
384 static int cgroup_open(struct inode *inode, struct file *file)
385 {
386 struct pid *pid = PROC_I(inode)->pid;
387 return single_open(file, proc_cgroup_show, pid);
388 }
389
390 static const struct file_operations proc_cgroup_operations = {
391 .open = cgroup_open,
392 .read = seq_read,
393 .llseek = seq_lseek,
394 .release = single_release,
395 };
396 #endif
397
398 #ifdef CONFIG_PROC_PID_CPUSET
399
400 static int cpuset_open(struct inode *inode, struct file *file)
401 {
402 struct pid *pid = PROC_I(inode)->pid;
403 return single_open(file, proc_cpuset_show, pid);
404 }
405
406 static const struct file_operations proc_cpuset_operations = {
407 .open = cpuset_open,
408 .read = seq_read,
409 .llseek = seq_lseek,
410 .release = single_release,
411 };
412 #endif
413
414 static int proc_oom_score(struct task_struct *task, char *buffer)
415 {
416 unsigned long totalpages = totalram_pages + total_swap_pages;
417 unsigned long points = 0;
418
419 read_lock(&tasklist_lock);
420 if (pid_alive(task))
421 points = oom_badness(task, NULL, NULL, totalpages) *
422 1000 / totalpages;
423 read_unlock(&tasklist_lock);
424 return sprintf(buffer, "%lu\n", points);
425 }
426
427 struct limit_names {
428 const char *name;
429 const char *unit;
430 };
431
432 static const struct limit_names lnames[RLIM_NLIMITS] = {
433 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
434 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
435 [RLIMIT_DATA] = {"Max data size", "bytes"},
436 [RLIMIT_STACK] = {"Max stack size", "bytes"},
437 [RLIMIT_CORE] = {"Max core file size", "bytes"},
438 [RLIMIT_RSS] = {"Max resident set", "bytes"},
439 [RLIMIT_NPROC] = {"Max processes", "processes"},
440 [RLIMIT_NOFILE] = {"Max open files", "files"},
441 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
442 [RLIMIT_AS] = {"Max address space", "bytes"},
443 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
444 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
445 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
446 [RLIMIT_NICE] = {"Max nice priority", NULL},
447 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
448 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
449 };
450
451 /* Display limits for a process */
452 static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
453 struct pid *pid, struct task_struct *task)
454 {
455 unsigned int i;
456 unsigned long flags;
457
458 struct rlimit rlim[RLIM_NLIMITS];
459
460 if (!lock_task_sighand(task, &flags))
461 return 0;
462 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
463 unlock_task_sighand(task, &flags);
464
465 /*
466 * print the file header
467 */
468 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
469 "Limit", "Soft Limit", "Hard Limit", "Units");
470
471 for (i = 0; i < RLIM_NLIMITS; i++) {
472 if (rlim[i].rlim_cur == RLIM_INFINITY)
473 seq_printf(m, "%-25s %-20s ",
474 lnames[i].name, "unlimited");
475 else
476 seq_printf(m, "%-25s %-20lu ",
477 lnames[i].name, rlim[i].rlim_cur);
478
479 if (rlim[i].rlim_max == RLIM_INFINITY)
480 seq_printf(m, "%-20s ", "unlimited");
481 else
482 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
483
484 if (lnames[i].unit)
485 seq_printf(m, "%-10s\n", lnames[i].unit);
486 else
487 seq_putc(m, '\n');
488 }
489
490 return 0;
491 }
492
493 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
494 static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
495 struct pid *pid, struct task_struct *task)
496 {
497 long nr;
498 unsigned long args[6], sp, pc;
499 int res = lock_trace(task);
500 if (res)
501 return res;
502
503 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
504 seq_puts(m, "running\n");
505 else if (nr < 0)
506 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
507 else
508 seq_printf(m,
509 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
510 nr,
511 args[0], args[1], args[2], args[3], args[4], args[5],
512 sp, pc);
513 unlock_trace(task);
514 return res;
515 }
516 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
517
518 /************************************************************************/
519 /* Here the fs part begins */
520 /************************************************************************/
521
522 /* permission checks */
523 static int proc_fd_access_allowed(struct inode *inode)
524 {
525 struct task_struct *task;
526 int allowed = 0;
527 /* Allow access to a task's file descriptors if it is us or we
528 * may use ptrace attach to the process and find out that
529 * information.
530 */
531 task = get_proc_task(inode);
532 if (task) {
533 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
534 put_task_struct(task);
535 }
536 return allowed;
537 }
538
539 int proc_setattr(struct dentry *dentry, struct iattr *attr)
540 {
541 int error;
542 struct inode *inode = dentry->d_inode;
543
544 if (attr->ia_valid & ATTR_MODE)
545 return -EPERM;
546
547 error = inode_change_ok(inode, attr);
548 if (error)
549 return error;
550
551 setattr_copy(inode, attr);
552 mark_inode_dirty(inode);
553 return 0;
554 }
555
556 /*
557 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
558 * or euid/egid (for hide_pid_min=2)?
559 */
560 static bool has_pid_permissions(struct pid_namespace *pid,
561 struct task_struct *task,
562 int hide_pid_min)
563 {
564 if (pid->hide_pid < hide_pid_min)
565 return true;
566 if (in_group_p(pid->pid_gid))
567 return true;
568 return ptrace_may_access(task, PTRACE_MODE_READ);
569 }
570
571
572 static int proc_pid_permission(struct inode *inode, int mask)
573 {
574 struct pid_namespace *pid = inode->i_sb->s_fs_info;
575 struct task_struct *task;
576 bool has_perms;
577
578 task = get_proc_task(inode);
579 if (!task)
580 return -ESRCH;
581 has_perms = has_pid_permissions(pid, task, 1);
582 put_task_struct(task);
583
584 if (!has_perms) {
585 if (pid->hide_pid == 2) {
586 /*
587 * Let's make getdents(), stat(), and open()
588 * consistent with each other. If a process
589 * may not stat() a file, it shouldn't be seen
590 * in procfs at all.
591 */
592 return -ENOENT;
593 }
594
595 return -EPERM;
596 }
597 return generic_permission(inode, mask);
598 }
599
600
601
602 static const struct inode_operations proc_def_inode_operations = {
603 .setattr = proc_setattr,
604 };
605
606 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
607
608 static ssize_t proc_info_read(struct file * file, char __user * buf,
609 size_t count, loff_t *ppos)
610 {
611 struct inode * inode = file_inode(file);
612 unsigned long page;
613 ssize_t length;
614 struct task_struct *task = get_proc_task(inode);
615
616 length = -ESRCH;
617 if (!task)
618 goto out_no_task;
619
620 if (count > PROC_BLOCK_SIZE)
621 count = PROC_BLOCK_SIZE;
622
623 length = -ENOMEM;
624 if (!(page = __get_free_page(GFP_TEMPORARY)))
625 goto out;
626
627 length = PROC_I(inode)->op.proc_read(task, (char*)page);
628
629 if (length >= 0)
630 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
631 free_page(page);
632 out:
633 put_task_struct(task);
634 out_no_task:
635 return length;
636 }
637
638 static const struct file_operations proc_info_file_operations = {
639 .read = proc_info_read,
640 .llseek = generic_file_llseek,
641 };
642
643 static int proc_single_show(struct seq_file *m, void *v)
644 {
645 struct inode *inode = m->private;
646 struct pid_namespace *ns;
647 struct pid *pid;
648 struct task_struct *task;
649 int ret;
650
651 ns = inode->i_sb->s_fs_info;
652 pid = proc_pid(inode);
653 task = get_pid_task(pid, PIDTYPE_PID);
654 if (!task)
655 return -ESRCH;
656
657 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
658
659 put_task_struct(task);
660 return ret;
661 }
662
663 static int proc_single_open(struct inode *inode, struct file *filp)
664 {
665 return single_open(filp, proc_single_show, inode);
666 }
667
668 static const struct file_operations proc_single_file_operations = {
669 .open = proc_single_open,
670 .read = seq_read,
671 .llseek = seq_lseek,
672 .release = single_release,
673 };
674
675 static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
676 {
677 struct task_struct *task = get_proc_task(file_inode(file));
678 struct mm_struct *mm;
679
680 if (!task)
681 return -ESRCH;
682
683 mm = mm_access(task, mode);
684 put_task_struct(task);
685
686 if (IS_ERR(mm))
687 return PTR_ERR(mm);
688
689 if (mm) {
690 /* ensure this mm_struct can't be freed */
691 atomic_inc(&mm->mm_count);
692 /* but do not pin its memory */
693 mmput(mm);
694 }
695
696 file->private_data = mm;
697
698 return 0;
699 }
700
701 static int mem_open(struct inode *inode, struct file *file)
702 {
703 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
704
705 /* OK to pass negative loff_t, we can catch out-of-range */
706 file->f_mode |= FMODE_UNSIGNED_OFFSET;
707
708 return ret;
709 }
710
711 static ssize_t mem_rw(struct file *file, char __user *buf,
712 size_t count, loff_t *ppos, int write)
713 {
714 struct mm_struct *mm = file->private_data;
715 unsigned long addr = *ppos;
716 ssize_t copied;
717 char *page;
718
719 if (!mm)
720 return 0;
721
722 page = (char *)__get_free_page(GFP_TEMPORARY);
723 if (!page)
724 return -ENOMEM;
725
726 copied = 0;
727 if (!atomic_inc_not_zero(&mm->mm_users))
728 goto free;
729
730 while (count > 0) {
731 int this_len = min_t(int, count, PAGE_SIZE);
732
733 if (write && copy_from_user(page, buf, this_len)) {
734 copied = -EFAULT;
735 break;
736 }
737
738 this_len = access_remote_vm(mm, addr, page, this_len, write);
739 if (!this_len) {
740 if (!copied)
741 copied = -EIO;
742 break;
743 }
744
745 if (!write && copy_to_user(buf, page, this_len)) {
746 copied = -EFAULT;
747 break;
748 }
749
750 buf += this_len;
751 addr += this_len;
752 copied += this_len;
753 count -= this_len;
754 }
755 *ppos = addr;
756
757 mmput(mm);
758 free:
759 free_page((unsigned long) page);
760 return copied;
761 }
762
763 static ssize_t mem_read(struct file *file, char __user *buf,
764 size_t count, loff_t *ppos)
765 {
766 return mem_rw(file, buf, count, ppos, 0);
767 }
768
769 static ssize_t mem_write(struct file *file, const char __user *buf,
770 size_t count, loff_t *ppos)
771 {
772 return mem_rw(file, (char __user*)buf, count, ppos, 1);
773 }
774
775 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
776 {
777 switch (orig) {
778 case 0:
779 file->f_pos = offset;
780 break;
781 case 1:
782 file->f_pos += offset;
783 break;
784 default:
785 return -EINVAL;
786 }
787 force_successful_syscall_return();
788 return file->f_pos;
789 }
790
791 static int mem_release(struct inode *inode, struct file *file)
792 {
793 struct mm_struct *mm = file->private_data;
794 if (mm)
795 mmdrop(mm);
796 return 0;
797 }
798
799 static const struct file_operations proc_mem_operations = {
800 .llseek = mem_lseek,
801 .read = mem_read,
802 .write = mem_write,
803 .open = mem_open,
804 .release = mem_release,
805 };
806
807 static int environ_open(struct inode *inode, struct file *file)
808 {
809 return __mem_open(inode, file, PTRACE_MODE_READ);
810 }
811
812 static ssize_t environ_read(struct file *file, char __user *buf,
813 size_t count, loff_t *ppos)
814 {
815 char *page;
816 unsigned long src = *ppos;
817 int ret = 0;
818 struct mm_struct *mm = file->private_data;
819
820 if (!mm)
821 return 0;
822
823 page = (char *)__get_free_page(GFP_TEMPORARY);
824 if (!page)
825 return -ENOMEM;
826
827 ret = 0;
828 if (!atomic_inc_not_zero(&mm->mm_users))
829 goto free;
830 while (count > 0) {
831 size_t this_len, max_len;
832 int retval;
833
834 if (src >= (mm->env_end - mm->env_start))
835 break;
836
837 this_len = mm->env_end - (mm->env_start + src);
838
839 max_len = min_t(size_t, PAGE_SIZE, count);
840 this_len = min(max_len, this_len);
841
842 retval = access_remote_vm(mm, (mm->env_start + src),
843 page, this_len, 0);
844
845 if (retval <= 0) {
846 ret = retval;
847 break;
848 }
849
850 if (copy_to_user(buf, page, retval)) {
851 ret = -EFAULT;
852 break;
853 }
854
855 ret += retval;
856 src += retval;
857 buf += retval;
858 count -= retval;
859 }
860 *ppos = src;
861 mmput(mm);
862
863 free:
864 free_page((unsigned long) page);
865 return ret;
866 }
867
868 static const struct file_operations proc_environ_operations = {
869 .open = environ_open,
870 .read = environ_read,
871 .llseek = generic_file_llseek,
872 .release = mem_release,
873 };
874
875 static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
876 loff_t *ppos)
877 {
878 struct task_struct *task = get_proc_task(file_inode(file));
879 char buffer[PROC_NUMBUF];
880 int oom_adj = OOM_ADJUST_MIN;
881 size_t len;
882 unsigned long flags;
883
884 if (!task)
885 return -ESRCH;
886 if (lock_task_sighand(task, &flags)) {
887 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
888 oom_adj = OOM_ADJUST_MAX;
889 else
890 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
891 OOM_SCORE_ADJ_MAX;
892 unlock_task_sighand(task, &flags);
893 }
894 put_task_struct(task);
895 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
896 return simple_read_from_buffer(buf, count, ppos, buffer, len);
897 }
898
899 static ssize_t oom_adj_write(struct file *file, const char __user *buf,
900 size_t count, loff_t *ppos)
901 {
902 struct task_struct *task;
903 char buffer[PROC_NUMBUF];
904 int oom_adj;
905 unsigned long flags;
906 int err;
907
908 memset(buffer, 0, sizeof(buffer));
909 if (count > sizeof(buffer) - 1)
910 count = sizeof(buffer) - 1;
911 if (copy_from_user(buffer, buf, count)) {
912 err = -EFAULT;
913 goto out;
914 }
915
916 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
917 if (err)
918 goto out;
919 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
920 oom_adj != OOM_DISABLE) {
921 err = -EINVAL;
922 goto out;
923 }
924
925 task = get_proc_task(file_inode(file));
926 if (!task) {
927 err = -ESRCH;
928 goto out;
929 }
930
931 task_lock(task);
932 if (!task->mm) {
933 err = -EINVAL;
934 goto err_task_lock;
935 }
936
937 if (!lock_task_sighand(task, &flags)) {
938 err = -ESRCH;
939 goto err_task_lock;
940 }
941
942 /*
943 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
944 * value is always attainable.
945 */
946 if (oom_adj == OOM_ADJUST_MAX)
947 oom_adj = OOM_SCORE_ADJ_MAX;
948 else
949 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
950
951 if (oom_adj < task->signal->oom_score_adj &&
952 !capable(CAP_SYS_RESOURCE)) {
953 err = -EACCES;
954 goto err_sighand;
955 }
956
957 /*
958 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
959 * /proc/pid/oom_score_adj instead.
960 */
961 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
962 current->comm, task_pid_nr(current), task_pid_nr(task),
963 task_pid_nr(task));
964
965 task->signal->oom_score_adj = oom_adj;
966 trace_oom_score_adj_update(task);
967 err_sighand:
968 unlock_task_sighand(task, &flags);
969 err_task_lock:
970 task_unlock(task);
971 put_task_struct(task);
972 out:
973 return err < 0 ? err : count;
974 }
975
976 static const struct file_operations proc_oom_adj_operations = {
977 .read = oom_adj_read,
978 .write = oom_adj_write,
979 .llseek = generic_file_llseek,
980 };
981
982 static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
983 size_t count, loff_t *ppos)
984 {
985 struct task_struct *task = get_proc_task(file_inode(file));
986 char buffer[PROC_NUMBUF];
987 short oom_score_adj = OOM_SCORE_ADJ_MIN;
988 unsigned long flags;
989 size_t len;
990
991 if (!task)
992 return -ESRCH;
993 if (lock_task_sighand(task, &flags)) {
994 oom_score_adj = task->signal->oom_score_adj;
995 unlock_task_sighand(task, &flags);
996 }
997 put_task_struct(task);
998 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
999 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1000 }
1001
1002 static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1003 size_t count, loff_t *ppos)
1004 {
1005 struct task_struct *task;
1006 char buffer[PROC_NUMBUF];
1007 unsigned long flags;
1008 int oom_score_adj;
1009 int err;
1010
1011 memset(buffer, 0, sizeof(buffer));
1012 if (count > sizeof(buffer) - 1)
1013 count = sizeof(buffer) - 1;
1014 if (copy_from_user(buffer, buf, count)) {
1015 err = -EFAULT;
1016 goto out;
1017 }
1018
1019 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
1020 if (err)
1021 goto out;
1022 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1023 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1024 err = -EINVAL;
1025 goto out;
1026 }
1027
1028 task = get_proc_task(file_inode(file));
1029 if (!task) {
1030 err = -ESRCH;
1031 goto out;
1032 }
1033
1034 task_lock(task);
1035 if (!task->mm) {
1036 err = -EINVAL;
1037 goto err_task_lock;
1038 }
1039
1040 if (!lock_task_sighand(task, &flags)) {
1041 err = -ESRCH;
1042 goto err_task_lock;
1043 }
1044
1045 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
1046 !capable(CAP_SYS_RESOURCE)) {
1047 err = -EACCES;
1048 goto err_sighand;
1049 }
1050
1051 task->signal->oom_score_adj = (short)oom_score_adj;
1052 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1053 task->signal->oom_score_adj_min = (short)oom_score_adj;
1054 trace_oom_score_adj_update(task);
1055
1056 err_sighand:
1057 unlock_task_sighand(task, &flags);
1058 err_task_lock:
1059 task_unlock(task);
1060 put_task_struct(task);
1061 out:
1062 return err < 0 ? err : count;
1063 }
1064
1065 static const struct file_operations proc_oom_score_adj_operations = {
1066 .read = oom_score_adj_read,
1067 .write = oom_score_adj_write,
1068 .llseek = default_llseek,
1069 };
1070
1071 #ifdef CONFIG_AUDITSYSCALL
1072 #define TMPBUFLEN 21
1073 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1074 size_t count, loff_t *ppos)
1075 {
1076 struct inode * inode = file_inode(file);
1077 struct task_struct *task = get_proc_task(inode);
1078 ssize_t length;
1079 char tmpbuf[TMPBUFLEN];
1080
1081 if (!task)
1082 return -ESRCH;
1083 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1084 from_kuid(file->f_cred->user_ns,
1085 audit_get_loginuid(task)));
1086 put_task_struct(task);
1087 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1088 }
1089
1090 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1091 size_t count, loff_t *ppos)
1092 {
1093 struct inode * inode = file_inode(file);
1094 char *page, *tmp;
1095 ssize_t length;
1096 uid_t loginuid;
1097 kuid_t kloginuid;
1098
1099 rcu_read_lock();
1100 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1101 rcu_read_unlock();
1102 return -EPERM;
1103 }
1104 rcu_read_unlock();
1105
1106 if (count >= PAGE_SIZE)
1107 count = PAGE_SIZE - 1;
1108
1109 if (*ppos != 0) {
1110 /* No partial writes. */
1111 return -EINVAL;
1112 }
1113 page = (char*)__get_free_page(GFP_TEMPORARY);
1114 if (!page)
1115 return -ENOMEM;
1116 length = -EFAULT;
1117 if (copy_from_user(page, buf, count))
1118 goto out_free_page;
1119
1120 page[count] = '\0';
1121 loginuid = simple_strtoul(page, &tmp, 10);
1122 if (tmp == page) {
1123 length = -EINVAL;
1124 goto out_free_page;
1125
1126 }
1127
1128 /* is userspace tring to explicitly UNSET the loginuid? */
1129 if (loginuid == AUDIT_UID_UNSET) {
1130 kloginuid = INVALID_UID;
1131 } else {
1132 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1133 if (!uid_valid(kloginuid)) {
1134 length = -EINVAL;
1135 goto out_free_page;
1136 }
1137 }
1138
1139 length = audit_set_loginuid(kloginuid);
1140 if (likely(length == 0))
1141 length = count;
1142
1143 out_free_page:
1144 free_page((unsigned long) page);
1145 return length;
1146 }
1147
1148 static const struct file_operations proc_loginuid_operations = {
1149 .read = proc_loginuid_read,
1150 .write = proc_loginuid_write,
1151 .llseek = generic_file_llseek,
1152 };
1153
1154 static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1155 size_t count, loff_t *ppos)
1156 {
1157 struct inode * inode = file_inode(file);
1158 struct task_struct *task = get_proc_task(inode);
1159 ssize_t length;
1160 char tmpbuf[TMPBUFLEN];
1161
1162 if (!task)
1163 return -ESRCH;
1164 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1165 audit_get_sessionid(task));
1166 put_task_struct(task);
1167 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1168 }
1169
1170 static const struct file_operations proc_sessionid_operations = {
1171 .read = proc_sessionid_read,
1172 .llseek = generic_file_llseek,
1173 };
1174 #endif
1175
1176 #ifdef CONFIG_FAULT_INJECTION
1177 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1178 size_t count, loff_t *ppos)
1179 {
1180 struct task_struct *task = get_proc_task(file_inode(file));
1181 char buffer[PROC_NUMBUF];
1182 size_t len;
1183 int make_it_fail;
1184
1185 if (!task)
1186 return -ESRCH;
1187 make_it_fail = task->make_it_fail;
1188 put_task_struct(task);
1189
1190 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1191
1192 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1193 }
1194
1195 static ssize_t proc_fault_inject_write(struct file * file,
1196 const char __user * buf, size_t count, loff_t *ppos)
1197 {
1198 struct task_struct *task;
1199 char buffer[PROC_NUMBUF], *end;
1200 int make_it_fail;
1201
1202 if (!capable(CAP_SYS_RESOURCE))
1203 return -EPERM;
1204 memset(buffer, 0, sizeof(buffer));
1205 if (count > sizeof(buffer) - 1)
1206 count = sizeof(buffer) - 1;
1207 if (copy_from_user(buffer, buf, count))
1208 return -EFAULT;
1209 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1210 if (*end)
1211 return -EINVAL;
1212 if (make_it_fail < 0 || make_it_fail > 1)
1213 return -EINVAL;
1214
1215 task = get_proc_task(file_inode(file));
1216 if (!task)
1217 return -ESRCH;
1218 task->make_it_fail = make_it_fail;
1219 put_task_struct(task);
1220
1221 return count;
1222 }
1223
1224 static const struct file_operations proc_fault_inject_operations = {
1225 .read = proc_fault_inject_read,
1226 .write = proc_fault_inject_write,
1227 .llseek = generic_file_llseek,
1228 };
1229 #endif
1230
1231
1232 #ifdef CONFIG_SCHED_DEBUG
1233 /*
1234 * Print out various scheduling related per-task fields:
1235 */
1236 static int sched_show(struct seq_file *m, void *v)
1237 {
1238 struct inode *inode = m->private;
1239 struct task_struct *p;
1240
1241 p = get_proc_task(inode);
1242 if (!p)
1243 return -ESRCH;
1244 proc_sched_show_task(p, m);
1245
1246 put_task_struct(p);
1247
1248 return 0;
1249 }
1250
1251 static ssize_t
1252 sched_write(struct file *file, const char __user *buf,
1253 size_t count, loff_t *offset)
1254 {
1255 struct inode *inode = file_inode(file);
1256 struct task_struct *p;
1257
1258 p = get_proc_task(inode);
1259 if (!p)
1260 return -ESRCH;
1261 proc_sched_set_task(p);
1262
1263 put_task_struct(p);
1264
1265 return count;
1266 }
1267
1268 static int sched_open(struct inode *inode, struct file *filp)
1269 {
1270 return single_open(filp, sched_show, inode);
1271 }
1272
1273 static const struct file_operations proc_pid_sched_operations = {
1274 .open = sched_open,
1275 .read = seq_read,
1276 .write = sched_write,
1277 .llseek = seq_lseek,
1278 .release = single_release,
1279 };
1280
1281 #endif
1282
1283 #ifdef CONFIG_SCHED_AUTOGROUP
1284 /*
1285 * Print out autogroup related information:
1286 */
1287 static int sched_autogroup_show(struct seq_file *m, void *v)
1288 {
1289 struct inode *inode = m->private;
1290 struct task_struct *p;
1291
1292 p = get_proc_task(inode);
1293 if (!p)
1294 return -ESRCH;
1295 proc_sched_autogroup_show_task(p, m);
1296
1297 put_task_struct(p);
1298
1299 return 0;
1300 }
1301
1302 static ssize_t
1303 sched_autogroup_write(struct file *file, const char __user *buf,
1304 size_t count, loff_t *offset)
1305 {
1306 struct inode *inode = file_inode(file);
1307 struct task_struct *p;
1308 char buffer[PROC_NUMBUF];
1309 int nice;
1310 int err;
1311
1312 memset(buffer, 0, sizeof(buffer));
1313 if (count > sizeof(buffer) - 1)
1314 count = sizeof(buffer) - 1;
1315 if (copy_from_user(buffer, buf, count))
1316 return -EFAULT;
1317
1318 err = kstrtoint(strstrip(buffer), 0, &nice);
1319 if (err < 0)
1320 return err;
1321
1322 p = get_proc_task(inode);
1323 if (!p)
1324 return -ESRCH;
1325
1326 err = proc_sched_autogroup_set_nice(p, nice);
1327 if (err)
1328 count = err;
1329
1330 put_task_struct(p);
1331
1332 return count;
1333 }
1334
1335 static int sched_autogroup_open(struct inode *inode, struct file *filp)
1336 {
1337 int ret;
1338
1339 ret = single_open(filp, sched_autogroup_show, NULL);
1340 if (!ret) {
1341 struct seq_file *m = filp->private_data;
1342
1343 m->private = inode;
1344 }
1345 return ret;
1346 }
1347
1348 static const struct file_operations proc_pid_sched_autogroup_operations = {
1349 .open = sched_autogroup_open,
1350 .read = seq_read,
1351 .write = sched_autogroup_write,
1352 .llseek = seq_lseek,
1353 .release = single_release,
1354 };
1355
1356 #endif /* CONFIG_SCHED_AUTOGROUP */
1357
1358 static ssize_t comm_write(struct file *file, const char __user *buf,
1359 size_t count, loff_t *offset)
1360 {
1361 struct inode *inode = file_inode(file);
1362 struct task_struct *p;
1363 char buffer[TASK_COMM_LEN];
1364 const size_t maxlen = sizeof(buffer) - 1;
1365
1366 memset(buffer, 0, sizeof(buffer));
1367 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
1368 return -EFAULT;
1369
1370 p = get_proc_task(inode);
1371 if (!p)
1372 return -ESRCH;
1373
1374 if (same_thread_group(current, p))
1375 set_task_comm(p, buffer);
1376 else
1377 count = -EINVAL;
1378
1379 put_task_struct(p);
1380
1381 return count;
1382 }
1383
1384 static int comm_show(struct seq_file *m, void *v)
1385 {
1386 struct inode *inode = m->private;
1387 struct task_struct *p;
1388
1389 p = get_proc_task(inode);
1390 if (!p)
1391 return -ESRCH;
1392
1393 task_lock(p);
1394 seq_printf(m, "%s\n", p->comm);
1395 task_unlock(p);
1396
1397 put_task_struct(p);
1398
1399 return 0;
1400 }
1401
1402 static int comm_open(struct inode *inode, struct file *filp)
1403 {
1404 return single_open(filp, comm_show, inode);
1405 }
1406
1407 static const struct file_operations proc_pid_set_comm_operations = {
1408 .open = comm_open,
1409 .read = seq_read,
1410 .write = comm_write,
1411 .llseek = seq_lseek,
1412 .release = single_release,
1413 };
1414
1415 static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
1416 {
1417 struct task_struct *task;
1418 struct mm_struct *mm;
1419 struct file *exe_file;
1420
1421 task = get_proc_task(dentry->d_inode);
1422 if (!task)
1423 return -ENOENT;
1424 mm = get_task_mm(task);
1425 put_task_struct(task);
1426 if (!mm)
1427 return -ENOENT;
1428 exe_file = get_mm_exe_file(mm);
1429 mmput(mm);
1430 if (exe_file) {
1431 *exe_path = exe_file->f_path;
1432 path_get(&exe_file->f_path);
1433 fput(exe_file);
1434 return 0;
1435 } else
1436 return -ENOENT;
1437 }
1438
1439 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1440 {
1441 struct inode *inode = dentry->d_inode;
1442 struct path path;
1443 int error = -EACCES;
1444
1445 /* Are we allowed to snoop on the tasks file descriptors? */
1446 if (!proc_fd_access_allowed(inode))
1447 goto out;
1448
1449 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1450 if (error)
1451 goto out;
1452
1453 nd_jump_link(nd, &path);
1454 return NULL;
1455 out:
1456 return ERR_PTR(error);
1457 }
1458
1459 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1460 {
1461 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1462 char *pathname;
1463 int len;
1464
1465 if (!tmp)
1466 return -ENOMEM;
1467
1468 pathname = d_path(path, tmp, PAGE_SIZE);
1469 len = PTR_ERR(pathname);
1470 if (IS_ERR(pathname))
1471 goto out;
1472 len = tmp + PAGE_SIZE - 1 - pathname;
1473
1474 if (len > buflen)
1475 len = buflen;
1476 if (copy_to_user(buffer, pathname, len))
1477 len = -EFAULT;
1478 out:
1479 free_page((unsigned long)tmp);
1480 return len;
1481 }
1482
1483 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1484 {
1485 int error = -EACCES;
1486 struct inode *inode = dentry->d_inode;
1487 struct path path;
1488
1489 /* Are we allowed to snoop on the tasks file descriptors? */
1490 if (!proc_fd_access_allowed(inode))
1491 goto out;
1492
1493 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1494 if (error)
1495 goto out;
1496
1497 error = do_proc_readlink(&path, buffer, buflen);
1498 path_put(&path);
1499 out:
1500 return error;
1501 }
1502
1503 const struct inode_operations proc_pid_link_inode_operations = {
1504 .readlink = proc_pid_readlink,
1505 .follow_link = proc_pid_follow_link,
1506 .setattr = proc_setattr,
1507 };
1508
1509
1510 /* building an inode */
1511
1512 struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1513 {
1514 struct inode * inode;
1515 struct proc_inode *ei;
1516 const struct cred *cred;
1517
1518 /* We need a new inode */
1519
1520 inode = new_inode(sb);
1521 if (!inode)
1522 goto out;
1523
1524 /* Common stuff */
1525 ei = PROC_I(inode);
1526 inode->i_ino = get_next_ino();
1527 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1528 inode->i_op = &proc_def_inode_operations;
1529
1530 /*
1531 * grab the reference to task.
1532 */
1533 ei->pid = get_task_pid(task, PIDTYPE_PID);
1534 if (!ei->pid)
1535 goto out_unlock;
1536
1537 if (task_dumpable(task)) {
1538 rcu_read_lock();
1539 cred = __task_cred(task);
1540 inode->i_uid = cred->euid;
1541 inode->i_gid = cred->egid;
1542 rcu_read_unlock();
1543 }
1544 security_task_to_inode(task, inode);
1545
1546 out:
1547 return inode;
1548
1549 out_unlock:
1550 iput(inode);
1551 return NULL;
1552 }
1553
1554 int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1555 {
1556 struct inode *inode = dentry->d_inode;
1557 struct task_struct *task;
1558 const struct cred *cred;
1559 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
1560
1561 generic_fillattr(inode, stat);
1562
1563 rcu_read_lock();
1564 stat->uid = GLOBAL_ROOT_UID;
1565 stat->gid = GLOBAL_ROOT_GID;
1566 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1567 if (task) {
1568 if (!has_pid_permissions(pid, task, 2)) {
1569 rcu_read_unlock();
1570 /*
1571 * This doesn't prevent learning whether PID exists,
1572 * it only makes getattr() consistent with readdir().
1573 */
1574 return -ENOENT;
1575 }
1576 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1577 task_dumpable(task)) {
1578 cred = __task_cred(task);
1579 stat->uid = cred->euid;
1580 stat->gid = cred->egid;
1581 }
1582 }
1583 rcu_read_unlock();
1584 return 0;
1585 }
1586
1587 /* dentry stuff */
1588
1589 /*
1590 * Exceptional case: normally we are not allowed to unhash a busy
1591 * directory. In this case, however, we can do it - no aliasing problems
1592 * due to the way we treat inodes.
1593 *
1594 * Rewrite the inode's ownerships here because the owning task may have
1595 * performed a setuid(), etc.
1596 *
1597 * Before the /proc/pid/status file was created the only way to read
1598 * the effective uid of a /process was to stat /proc/pid. Reading
1599 * /proc/pid/status is slow enough that procps and other packages
1600 * kept stating /proc/pid. To keep the rules in /proc simple I have
1601 * made this apply to all per process world readable and executable
1602 * directories.
1603 */
1604 int pid_revalidate(struct dentry *dentry, unsigned int flags)
1605 {
1606 struct inode *inode;
1607 struct task_struct *task;
1608 const struct cred *cred;
1609
1610 if (flags & LOOKUP_RCU)
1611 return -ECHILD;
1612
1613 inode = dentry->d_inode;
1614 task = get_proc_task(inode);
1615
1616 if (task) {
1617 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1618 task_dumpable(task)) {
1619 rcu_read_lock();
1620 cred = __task_cred(task);
1621 inode->i_uid = cred->euid;
1622 inode->i_gid = cred->egid;
1623 rcu_read_unlock();
1624 } else {
1625 inode->i_uid = GLOBAL_ROOT_UID;
1626 inode->i_gid = GLOBAL_ROOT_GID;
1627 }
1628 inode->i_mode &= ~(S_ISUID | S_ISGID);
1629 security_task_to_inode(task, inode);
1630 put_task_struct(task);
1631 return 1;
1632 }
1633 d_drop(dentry);
1634 return 0;
1635 }
1636
1637 static inline bool proc_inode_is_dead(struct inode *inode)
1638 {
1639 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1640 }
1641
1642 int pid_delete_dentry(const struct dentry *dentry)
1643 {
1644 /* Is the task we represent dead?
1645 * If so, then don't put the dentry on the lru list,
1646 * kill it immediately.
1647 */
1648 return proc_inode_is_dead(dentry->d_inode);
1649 }
1650
1651 const struct dentry_operations pid_dentry_operations =
1652 {
1653 .d_revalidate = pid_revalidate,
1654 .d_delete = pid_delete_dentry,
1655 };
1656
1657 /* Lookups */
1658
1659 /*
1660 * Fill a directory entry.
1661 *
1662 * If possible create the dcache entry and derive our inode number and
1663 * file type from dcache entry.
1664 *
1665 * Since all of the proc inode numbers are dynamically generated, the inode
1666 * numbers do not exist until the inode is cache. This means creating the
1667 * the dcache entry in readdir is necessary to keep the inode numbers
1668 * reported by readdir in sync with the inode numbers reported
1669 * by stat.
1670 */
1671 bool proc_fill_cache(struct file *file, struct dir_context *ctx,
1672 const char *name, int len,
1673 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1674 {
1675 struct dentry *child, *dir = file->f_path.dentry;
1676 struct qstr qname = QSTR_INIT(name, len);
1677 struct inode *inode;
1678 unsigned type;
1679 ino_t ino;
1680
1681 child = d_hash_and_lookup(dir, &qname);
1682 if (!child) {
1683 child = d_alloc(dir, &qname);
1684 if (!child)
1685 goto end_instantiate;
1686 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1687 dput(child);
1688 goto end_instantiate;
1689 }
1690 }
1691 inode = child->d_inode;
1692 ino = inode->i_ino;
1693 type = inode->i_mode >> 12;
1694 dput(child);
1695 return dir_emit(ctx, name, len, ino, type);
1696
1697 end_instantiate:
1698 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
1699 }
1700
1701 #ifdef CONFIG_CHECKPOINT_RESTORE
1702
1703 /*
1704 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1705 * which represent vma start and end addresses.
1706 */
1707 static int dname_to_vma_addr(struct dentry *dentry,
1708 unsigned long *start, unsigned long *end)
1709 {
1710 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1711 return -EINVAL;
1712
1713 return 0;
1714 }
1715
1716 static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
1717 {
1718 unsigned long vm_start, vm_end;
1719 bool exact_vma_exists = false;
1720 struct mm_struct *mm = NULL;
1721 struct task_struct *task;
1722 const struct cred *cred;
1723 struct inode *inode;
1724 int status = 0;
1725
1726 if (flags & LOOKUP_RCU)
1727 return -ECHILD;
1728
1729 if (!capable(CAP_SYS_ADMIN)) {
1730 status = -EPERM;
1731 goto out_notask;
1732 }
1733
1734 inode = dentry->d_inode;
1735 task = get_proc_task(inode);
1736 if (!task)
1737 goto out_notask;
1738
1739 mm = mm_access(task, PTRACE_MODE_READ);
1740 if (IS_ERR_OR_NULL(mm))
1741 goto out;
1742
1743 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1744 down_read(&mm->mmap_sem);
1745 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1746 up_read(&mm->mmap_sem);
1747 }
1748
1749 mmput(mm);
1750
1751 if (exact_vma_exists) {
1752 if (task_dumpable(task)) {
1753 rcu_read_lock();
1754 cred = __task_cred(task);
1755 inode->i_uid = cred->euid;
1756 inode->i_gid = cred->egid;
1757 rcu_read_unlock();
1758 } else {
1759 inode->i_uid = GLOBAL_ROOT_UID;
1760 inode->i_gid = GLOBAL_ROOT_GID;
1761 }
1762 security_task_to_inode(task, inode);
1763 status = 1;
1764 }
1765
1766 out:
1767 put_task_struct(task);
1768
1769 out_notask:
1770 if (status <= 0)
1771 d_drop(dentry);
1772
1773 return status;
1774 }
1775
1776 static const struct dentry_operations tid_map_files_dentry_operations = {
1777 .d_revalidate = map_files_d_revalidate,
1778 .d_delete = pid_delete_dentry,
1779 };
1780
1781 static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1782 {
1783 unsigned long vm_start, vm_end;
1784 struct vm_area_struct *vma;
1785 struct task_struct *task;
1786 struct mm_struct *mm;
1787 int rc;
1788
1789 rc = -ENOENT;
1790 task = get_proc_task(dentry->d_inode);
1791 if (!task)
1792 goto out;
1793
1794 mm = get_task_mm(task);
1795 put_task_struct(task);
1796 if (!mm)
1797 goto out;
1798
1799 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1800 if (rc)
1801 goto out_mmput;
1802
1803 rc = -ENOENT;
1804 down_read(&mm->mmap_sem);
1805 vma = find_exact_vma(mm, vm_start, vm_end);
1806 if (vma && vma->vm_file) {
1807 *path = vma->vm_file->f_path;
1808 path_get(path);
1809 rc = 0;
1810 }
1811 up_read(&mm->mmap_sem);
1812
1813 out_mmput:
1814 mmput(mm);
1815 out:
1816 return rc;
1817 }
1818
1819 struct map_files_info {
1820 fmode_t mode;
1821 unsigned long len;
1822 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1823 };
1824
1825 static int
1826 proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1827 struct task_struct *task, const void *ptr)
1828 {
1829 fmode_t mode = (fmode_t)(unsigned long)ptr;
1830 struct proc_inode *ei;
1831 struct inode *inode;
1832
1833 inode = proc_pid_make_inode(dir->i_sb, task);
1834 if (!inode)
1835 return -ENOENT;
1836
1837 ei = PROC_I(inode);
1838 ei->op.proc_get_link = proc_map_files_get_link;
1839
1840 inode->i_op = &proc_pid_link_inode_operations;
1841 inode->i_size = 64;
1842 inode->i_mode = S_IFLNK;
1843
1844 if (mode & FMODE_READ)
1845 inode->i_mode |= S_IRUSR;
1846 if (mode & FMODE_WRITE)
1847 inode->i_mode |= S_IWUSR;
1848
1849 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1850 d_add(dentry, inode);
1851
1852 return 0;
1853 }
1854
1855 static struct dentry *proc_map_files_lookup(struct inode *dir,
1856 struct dentry *dentry, unsigned int flags)
1857 {
1858 unsigned long vm_start, vm_end;
1859 struct vm_area_struct *vma;
1860 struct task_struct *task;
1861 int result;
1862 struct mm_struct *mm;
1863
1864 result = -EPERM;
1865 if (!capable(CAP_SYS_ADMIN))
1866 goto out;
1867
1868 result = -ENOENT;
1869 task = get_proc_task(dir);
1870 if (!task)
1871 goto out;
1872
1873 result = -EACCES;
1874 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1875 goto out_put_task;
1876
1877 result = -ENOENT;
1878 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
1879 goto out_put_task;
1880
1881 mm = get_task_mm(task);
1882 if (!mm)
1883 goto out_put_task;
1884
1885 down_read(&mm->mmap_sem);
1886 vma = find_exact_vma(mm, vm_start, vm_end);
1887 if (!vma)
1888 goto out_no_vma;
1889
1890 if (vma->vm_file)
1891 result = proc_map_files_instantiate(dir, dentry, task,
1892 (void *)(unsigned long)vma->vm_file->f_mode);
1893
1894 out_no_vma:
1895 up_read(&mm->mmap_sem);
1896 mmput(mm);
1897 out_put_task:
1898 put_task_struct(task);
1899 out:
1900 return ERR_PTR(result);
1901 }
1902
1903 static const struct inode_operations proc_map_files_inode_operations = {
1904 .lookup = proc_map_files_lookup,
1905 .permission = proc_fd_permission,
1906 .setattr = proc_setattr,
1907 };
1908
1909 static int
1910 proc_map_files_readdir(struct file *file, struct dir_context *ctx)
1911 {
1912 struct vm_area_struct *vma;
1913 struct task_struct *task;
1914 struct mm_struct *mm;
1915 unsigned long nr_files, pos, i;
1916 struct flex_array *fa = NULL;
1917 struct map_files_info info;
1918 struct map_files_info *p;
1919 int ret;
1920
1921 ret = -EPERM;
1922 if (!capable(CAP_SYS_ADMIN))
1923 goto out;
1924
1925 ret = -ENOENT;
1926 task = get_proc_task(file_inode(file));
1927 if (!task)
1928 goto out;
1929
1930 ret = -EACCES;
1931 if (!ptrace_may_access(task, PTRACE_MODE_READ))
1932 goto out_put_task;
1933
1934 ret = 0;
1935 if (!dir_emit_dots(file, ctx))
1936 goto out_put_task;
1937
1938 mm = get_task_mm(task);
1939 if (!mm)
1940 goto out_put_task;
1941 down_read(&mm->mmap_sem);
1942
1943 nr_files = 0;
1944
1945 /*
1946 * We need two passes here:
1947 *
1948 * 1) Collect vmas of mapped files with mmap_sem taken
1949 * 2) Release mmap_sem and instantiate entries
1950 *
1951 * otherwise we get lockdep complained, since filldir()
1952 * routine might require mmap_sem taken in might_fault().
1953 */
1954
1955 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1956 if (vma->vm_file && ++pos > ctx->pos)
1957 nr_files++;
1958 }
1959
1960 if (nr_files) {
1961 fa = flex_array_alloc(sizeof(info), nr_files,
1962 GFP_KERNEL);
1963 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1964 GFP_KERNEL)) {
1965 ret = -ENOMEM;
1966 if (fa)
1967 flex_array_free(fa);
1968 up_read(&mm->mmap_sem);
1969 mmput(mm);
1970 goto out_put_task;
1971 }
1972 for (i = 0, vma = mm->mmap, pos = 2; vma;
1973 vma = vma->vm_next) {
1974 if (!vma->vm_file)
1975 continue;
1976 if (++pos <= ctx->pos)
1977 continue;
1978
1979 info.mode = vma->vm_file->f_mode;
1980 info.len = snprintf(info.name,
1981 sizeof(info.name), "%lx-%lx",
1982 vma->vm_start, vma->vm_end);
1983 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1984 BUG();
1985 }
1986 }
1987 up_read(&mm->mmap_sem);
1988
1989 for (i = 0; i < nr_files; i++) {
1990 p = flex_array_get(fa, i);
1991 if (!proc_fill_cache(file, ctx,
1992 p->name, p->len,
1993 proc_map_files_instantiate,
1994 task,
1995 (void *)(unsigned long)p->mode))
1996 break;
1997 ctx->pos++;
1998 }
1999 if (fa)
2000 flex_array_free(fa);
2001 mmput(mm);
2002
2003 out_put_task:
2004 put_task_struct(task);
2005 out:
2006 return ret;
2007 }
2008
2009 static const struct file_operations proc_map_files_operations = {
2010 .read = generic_read_dir,
2011 .iterate = proc_map_files_readdir,
2012 .llseek = default_llseek,
2013 };
2014
2015 struct timers_private {
2016 struct pid *pid;
2017 struct task_struct *task;
2018 struct sighand_struct *sighand;
2019 struct pid_namespace *ns;
2020 unsigned long flags;
2021 };
2022
2023 static void *timers_start(struct seq_file *m, loff_t *pos)
2024 {
2025 struct timers_private *tp = m->private;
2026
2027 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2028 if (!tp->task)
2029 return ERR_PTR(-ESRCH);
2030
2031 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2032 if (!tp->sighand)
2033 return ERR_PTR(-ESRCH);
2034
2035 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2036 }
2037
2038 static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2039 {
2040 struct timers_private *tp = m->private;
2041 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2042 }
2043
2044 static void timers_stop(struct seq_file *m, void *v)
2045 {
2046 struct timers_private *tp = m->private;
2047
2048 if (tp->sighand) {
2049 unlock_task_sighand(tp->task, &tp->flags);
2050 tp->sighand = NULL;
2051 }
2052
2053 if (tp->task) {
2054 put_task_struct(tp->task);
2055 tp->task = NULL;
2056 }
2057 }
2058
2059 static int show_timer(struct seq_file *m, void *v)
2060 {
2061 struct k_itimer *timer;
2062 struct timers_private *tp = m->private;
2063 int notify;
2064 static const char * const nstr[] = {
2065 [SIGEV_SIGNAL] = "signal",
2066 [SIGEV_NONE] = "none",
2067 [SIGEV_THREAD] = "thread",
2068 };
2069
2070 timer = list_entry((struct list_head *)v, struct k_itimer, list);
2071 notify = timer->it_sigev_notify;
2072
2073 seq_printf(m, "ID: %d\n", timer->it_id);
2074 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2075 timer->sigq->info.si_value.sival_ptr);
2076 seq_printf(m, "notify: %s/%s.%d\n",
2077 nstr[notify & ~SIGEV_THREAD_ID],
2078 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2079 pid_nr_ns(timer->it_pid, tp->ns));
2080 seq_printf(m, "ClockID: %d\n", timer->it_clock);
2081
2082 return 0;
2083 }
2084
2085 static const struct seq_operations proc_timers_seq_ops = {
2086 .start = timers_start,
2087 .next = timers_next,
2088 .stop = timers_stop,
2089 .show = show_timer,
2090 };
2091
2092 static int proc_timers_open(struct inode *inode, struct file *file)
2093 {
2094 struct timers_private *tp;
2095
2096 tp = __seq_open_private(file, &proc_timers_seq_ops,
2097 sizeof(struct timers_private));
2098 if (!tp)
2099 return -ENOMEM;
2100
2101 tp->pid = proc_pid(inode);
2102 tp->ns = inode->i_sb->s_fs_info;
2103 return 0;
2104 }
2105
2106 static const struct file_operations proc_timers_operations = {
2107 .open = proc_timers_open,
2108 .read = seq_read,
2109 .llseek = seq_lseek,
2110 .release = seq_release_private,
2111 };
2112 #endif /* CONFIG_CHECKPOINT_RESTORE */
2113
2114 static int proc_pident_instantiate(struct inode *dir,
2115 struct dentry *dentry, struct task_struct *task, const void *ptr)
2116 {
2117 const struct pid_entry *p = ptr;
2118 struct inode *inode;
2119 struct proc_inode *ei;
2120
2121 inode = proc_pid_make_inode(dir->i_sb, task);
2122 if (!inode)
2123 goto out;
2124
2125 ei = PROC_I(inode);
2126 inode->i_mode = p->mode;
2127 if (S_ISDIR(inode->i_mode))
2128 set_nlink(inode, 2); /* Use getattr to fix if necessary */
2129 if (p->iop)
2130 inode->i_op = p->iop;
2131 if (p->fop)
2132 inode->i_fop = p->fop;
2133 ei->op = p->op;
2134 d_set_d_op(dentry, &pid_dentry_operations);
2135 d_add(dentry, inode);
2136 /* Close the race of the process dying before we return the dentry */
2137 if (pid_revalidate(dentry, 0))
2138 return 0;
2139 out:
2140 return -ENOENT;
2141 }
2142
2143 static struct dentry *proc_pident_lookup(struct inode *dir,
2144 struct dentry *dentry,
2145 const struct pid_entry *ents,
2146 unsigned int nents)
2147 {
2148 int error;
2149 struct task_struct *task = get_proc_task(dir);
2150 const struct pid_entry *p, *last;
2151
2152 error = -ENOENT;
2153
2154 if (!task)
2155 goto out_no_task;
2156
2157 /*
2158 * Yes, it does not scale. And it should not. Don't add
2159 * new entries into /proc/<tgid>/ without very good reasons.
2160 */
2161 last = &ents[nents - 1];
2162 for (p = ents; p <= last; p++) {
2163 if (p->len != dentry->d_name.len)
2164 continue;
2165 if (!memcmp(dentry->d_name.name, p->name, p->len))
2166 break;
2167 }
2168 if (p > last)
2169 goto out;
2170
2171 error = proc_pident_instantiate(dir, dentry, task, p);
2172 out:
2173 put_task_struct(task);
2174 out_no_task:
2175 return ERR_PTR(error);
2176 }
2177
2178 static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
2179 const struct pid_entry *ents, unsigned int nents)
2180 {
2181 struct task_struct *task = get_proc_task(file_inode(file));
2182 const struct pid_entry *p;
2183
2184 if (!task)
2185 return -ENOENT;
2186
2187 if (!dir_emit_dots(file, ctx))
2188 goto out;
2189
2190 if (ctx->pos >= nents + 2)
2191 goto out;
2192
2193 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2194 if (!proc_fill_cache(file, ctx, p->name, p->len,
2195 proc_pident_instantiate, task, p))
2196 break;
2197 ctx->pos++;
2198 }
2199 out:
2200 put_task_struct(task);
2201 return 0;
2202 }
2203
2204 #ifdef CONFIG_SECURITY
2205 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2206 size_t count, loff_t *ppos)
2207 {
2208 struct inode * inode = file_inode(file);
2209 char *p = NULL;
2210 ssize_t length;
2211 struct task_struct *task = get_proc_task(inode);
2212
2213 if (!task)
2214 return -ESRCH;
2215
2216 length = security_getprocattr(task,
2217 (char*)file->f_path.dentry->d_name.name,
2218 &p);
2219 put_task_struct(task);
2220 if (length > 0)
2221 length = simple_read_from_buffer(buf, count, ppos, p, length);
2222 kfree(p);
2223 return length;
2224 }
2225
2226 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2227 size_t count, loff_t *ppos)
2228 {
2229 struct inode * inode = file_inode(file);
2230 char *page;
2231 ssize_t length;
2232 struct task_struct *task = get_proc_task(inode);
2233
2234 length = -ESRCH;
2235 if (!task)
2236 goto out_no_task;
2237 if (count > PAGE_SIZE)
2238 count = PAGE_SIZE;
2239
2240 /* No partial writes. */
2241 length = -EINVAL;
2242 if (*ppos != 0)
2243 goto out;
2244
2245 length = -ENOMEM;
2246 page = (char*)__get_free_page(GFP_TEMPORARY);
2247 if (!page)
2248 goto out;
2249
2250 length = -EFAULT;
2251 if (copy_from_user(page, buf, count))
2252 goto out_free;
2253
2254 /* Guard against adverse ptrace interaction */
2255 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
2256 if (length < 0)
2257 goto out_free;
2258
2259 length = security_setprocattr(task,
2260 (char*)file->f_path.dentry->d_name.name,
2261 (void*)page, count);
2262 mutex_unlock(&task->signal->cred_guard_mutex);
2263 out_free:
2264 free_page((unsigned long) page);
2265 out:
2266 put_task_struct(task);
2267 out_no_task:
2268 return length;
2269 }
2270
2271 static const struct file_operations proc_pid_attr_operations = {
2272 .read = proc_pid_attr_read,
2273 .write = proc_pid_attr_write,
2274 .llseek = generic_file_llseek,
2275 };
2276
2277 static const struct pid_entry attr_dir_stuff[] = {
2278 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2279 REG("prev", S_IRUGO, proc_pid_attr_operations),
2280 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2281 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2282 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2283 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2284 };
2285
2286 static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
2287 {
2288 return proc_pident_readdir(file, ctx,
2289 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2290 }
2291
2292 static const struct file_operations proc_attr_dir_operations = {
2293 .read = generic_read_dir,
2294 .iterate = proc_attr_dir_readdir,
2295 .llseek = default_llseek,
2296 };
2297
2298 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2299 struct dentry *dentry, unsigned int flags)
2300 {
2301 return proc_pident_lookup(dir, dentry,
2302 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2303 }
2304
2305 static const struct inode_operations proc_attr_dir_inode_operations = {
2306 .lookup = proc_attr_dir_lookup,
2307 .getattr = pid_getattr,
2308 .setattr = proc_setattr,
2309 };
2310
2311 #endif
2312
2313 #ifdef CONFIG_ELF_CORE
2314 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2315 size_t count, loff_t *ppos)
2316 {
2317 struct task_struct *task = get_proc_task(file_inode(file));
2318 struct mm_struct *mm;
2319 char buffer[PROC_NUMBUF];
2320 size_t len;
2321 int ret;
2322
2323 if (!task)
2324 return -ESRCH;
2325
2326 ret = 0;
2327 mm = get_task_mm(task);
2328 if (mm) {
2329 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2330 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2331 MMF_DUMP_FILTER_SHIFT));
2332 mmput(mm);
2333 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2334 }
2335
2336 put_task_struct(task);
2337
2338 return ret;
2339 }
2340
2341 static ssize_t proc_coredump_filter_write(struct file *file,
2342 const char __user *buf,
2343 size_t count,
2344 loff_t *ppos)
2345 {
2346 struct task_struct *task;
2347 struct mm_struct *mm;
2348 char buffer[PROC_NUMBUF], *end;
2349 unsigned int val;
2350 int ret;
2351 int i;
2352 unsigned long mask;
2353
2354 ret = -EFAULT;
2355 memset(buffer, 0, sizeof(buffer));
2356 if (count > sizeof(buffer) - 1)
2357 count = sizeof(buffer) - 1;
2358 if (copy_from_user(buffer, buf, count))
2359 goto out_no_task;
2360
2361 ret = -EINVAL;
2362 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2363 if (*end == '\n')
2364 end++;
2365 if (end - buffer == 0)
2366 goto out_no_task;
2367
2368 ret = -ESRCH;
2369 task = get_proc_task(file_inode(file));
2370 if (!task)
2371 goto out_no_task;
2372
2373 ret = end - buffer;
2374 mm = get_task_mm(task);
2375 if (!mm)
2376 goto out_no_mm;
2377
2378 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2379 if (val & mask)
2380 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2381 else
2382 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2383 }
2384
2385 mmput(mm);
2386 out_no_mm:
2387 put_task_struct(task);
2388 out_no_task:
2389 return ret;
2390 }
2391
2392 static const struct file_operations proc_coredump_filter_operations = {
2393 .read = proc_coredump_filter_read,
2394 .write = proc_coredump_filter_write,
2395 .llseek = generic_file_llseek,
2396 };
2397 #endif
2398
2399 #ifdef CONFIG_TASK_IO_ACCOUNTING
2400 static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2401 {
2402 struct task_io_accounting acct = task->ioac;
2403 unsigned long flags;
2404 int result;
2405
2406 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2407 if (result)
2408 return result;
2409
2410 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2411 result = -EACCES;
2412 goto out_unlock;
2413 }
2414
2415 if (whole && lock_task_sighand(task, &flags)) {
2416 struct task_struct *t = task;
2417
2418 task_io_accounting_add(&acct, &task->signal->ioac);
2419 while_each_thread(task, t)
2420 task_io_accounting_add(&acct, &t->ioac);
2421
2422 unlock_task_sighand(task, &flags);
2423 }
2424 result = sprintf(buffer,
2425 "rchar: %llu\n"
2426 "wchar: %llu\n"
2427 "syscr: %llu\n"
2428 "syscw: %llu\n"
2429 "read_bytes: %llu\n"
2430 "write_bytes: %llu\n"
2431 "cancelled_write_bytes: %llu\n",
2432 (unsigned long long)acct.rchar,
2433 (unsigned long long)acct.wchar,
2434 (unsigned long long)acct.syscr,
2435 (unsigned long long)acct.syscw,
2436 (unsigned long long)acct.read_bytes,
2437 (unsigned long long)acct.write_bytes,
2438 (unsigned long long)acct.cancelled_write_bytes);
2439 out_unlock:
2440 mutex_unlock(&task->signal->cred_guard_mutex);
2441 return result;
2442 }
2443
2444 static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2445 {
2446 return do_io_accounting(task, buffer, 0);
2447 }
2448
2449 static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2450 {
2451 return do_io_accounting(task, buffer, 1);
2452 }
2453 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2454
2455 #ifdef CONFIG_USER_NS
2456 static int proc_id_map_open(struct inode *inode, struct file *file,
2457 const struct seq_operations *seq_ops)
2458 {
2459 struct user_namespace *ns = NULL;
2460 struct task_struct *task;
2461 struct seq_file *seq;
2462 int ret = -EINVAL;
2463
2464 task = get_proc_task(inode);
2465 if (task) {
2466 rcu_read_lock();
2467 ns = get_user_ns(task_cred_xxx(task, user_ns));
2468 rcu_read_unlock();
2469 put_task_struct(task);
2470 }
2471 if (!ns)
2472 goto err;
2473
2474 ret = seq_open(file, seq_ops);
2475 if (ret)
2476 goto err_put_ns;
2477
2478 seq = file->private_data;
2479 seq->private = ns;
2480
2481 return 0;
2482 err_put_ns:
2483 put_user_ns(ns);
2484 err:
2485 return ret;
2486 }
2487
2488 static int proc_id_map_release(struct inode *inode, struct file *file)
2489 {
2490 struct seq_file *seq = file->private_data;
2491 struct user_namespace *ns = seq->private;
2492 put_user_ns(ns);
2493 return seq_release(inode, file);
2494 }
2495
2496 static int proc_uid_map_open(struct inode *inode, struct file *file)
2497 {
2498 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2499 }
2500
2501 static int proc_gid_map_open(struct inode *inode, struct file *file)
2502 {
2503 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2504 }
2505
2506 static int proc_projid_map_open(struct inode *inode, struct file *file)
2507 {
2508 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2509 }
2510
2511 static const struct file_operations proc_uid_map_operations = {
2512 .open = proc_uid_map_open,
2513 .write = proc_uid_map_write,
2514 .read = seq_read,
2515 .llseek = seq_lseek,
2516 .release = proc_id_map_release,
2517 };
2518
2519 static const struct file_operations proc_gid_map_operations = {
2520 .open = proc_gid_map_open,
2521 .write = proc_gid_map_write,
2522 .read = seq_read,
2523 .llseek = seq_lseek,
2524 .release = proc_id_map_release,
2525 };
2526
2527 static const struct file_operations proc_projid_map_operations = {
2528 .open = proc_projid_map_open,
2529 .write = proc_projid_map_write,
2530 .read = seq_read,
2531 .llseek = seq_lseek,
2532 .release = proc_id_map_release,
2533 };
2534 #endif /* CONFIG_USER_NS */
2535
2536 static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2537 struct pid *pid, struct task_struct *task)
2538 {
2539 int err = lock_trace(task);
2540 if (!err) {
2541 seq_printf(m, "%08x\n", task->personality);
2542 unlock_trace(task);
2543 }
2544 return err;
2545 }
2546
2547 /*
2548 * Thread groups
2549 */
2550 static const struct file_operations proc_task_operations;
2551 static const struct inode_operations proc_task_inode_operations;
2552
2553 static const struct pid_entry tgid_base_stuff[] = {
2554 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2555 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2556 #ifdef CONFIG_CHECKPOINT_RESTORE
2557 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2558 #endif
2559 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
2560 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
2561 #ifdef CONFIG_NET
2562 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2563 #endif
2564 REG("environ", S_IRUSR, proc_environ_operations),
2565 ONE("auxv", S_IRUSR, proc_pid_auxv),
2566 ONE("status", S_IRUGO, proc_pid_status),
2567 ONE("personality", S_IRUSR, proc_pid_personality),
2568 ONE("limits", S_IRUGO, proc_pid_limits),
2569 #ifdef CONFIG_SCHED_DEBUG
2570 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2571 #endif
2572 #ifdef CONFIG_SCHED_AUTOGROUP
2573 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2574 #endif
2575 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2576 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2577 ONE("syscall", S_IRUSR, proc_pid_syscall),
2578 #endif
2579 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2580 ONE("stat", S_IRUGO, proc_tgid_stat),
2581 ONE("statm", S_IRUGO, proc_pid_statm),
2582 REG("maps", S_IRUGO, proc_pid_maps_operations),
2583 #ifdef CONFIG_NUMA
2584 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
2585 #endif
2586 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2587 LNK("cwd", proc_cwd_link),
2588 LNK("root", proc_root_link),
2589 LNK("exe", proc_exe_link),
2590 REG("mounts", S_IRUGO, proc_mounts_operations),
2591 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2592 REG("mountstats", S_IRUSR, proc_mountstats_operations),
2593 #ifdef CONFIG_PROC_PAGE_MONITOR
2594 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2595 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
2596 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2597 #endif
2598 #ifdef CONFIG_SECURITY
2599 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2600 #endif
2601 #ifdef CONFIG_KALLSYMS
2602 ONE("wchan", S_IRUGO, proc_pid_wchan),
2603 #endif
2604 #ifdef CONFIG_STACKTRACE
2605 ONE("stack", S_IRUSR, proc_pid_stack),
2606 #endif
2607 #ifdef CONFIG_SCHEDSTATS
2608 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2609 #endif
2610 #ifdef CONFIG_LATENCYTOP
2611 REG("latency", S_IRUGO, proc_lstats_operations),
2612 #endif
2613 #ifdef CONFIG_PROC_PID_CPUSET
2614 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2615 #endif
2616 #ifdef CONFIG_CGROUPS
2617 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2618 #endif
2619 INF("oom_score", S_IRUGO, proc_oom_score),
2620 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2621 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2622 #ifdef CONFIG_AUDITSYSCALL
2623 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2624 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2625 #endif
2626 #ifdef CONFIG_FAULT_INJECTION
2627 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2628 #endif
2629 #ifdef CONFIG_ELF_CORE
2630 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
2631 #endif
2632 #ifdef CONFIG_TASK_IO_ACCOUNTING
2633 INF("io", S_IRUSR, proc_tgid_io_accounting),
2634 #endif
2635 #ifdef CONFIG_HARDWALL
2636 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2637 #endif
2638 #ifdef CONFIG_USER_NS
2639 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2640 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2641 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2642 #endif
2643 #ifdef CONFIG_CHECKPOINT_RESTORE
2644 REG("timers", S_IRUGO, proc_timers_operations),
2645 #endif
2646 };
2647
2648 static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
2649 {
2650 return proc_pident_readdir(file, ctx,
2651 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2652 }
2653
2654 static const struct file_operations proc_tgid_base_operations = {
2655 .read = generic_read_dir,
2656 .iterate = proc_tgid_base_readdir,
2657 .llseek = default_llseek,
2658 };
2659
2660 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2661 {
2662 return proc_pident_lookup(dir, dentry,
2663 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2664 }
2665
2666 static const struct inode_operations proc_tgid_base_inode_operations = {
2667 .lookup = proc_tgid_base_lookup,
2668 .getattr = pid_getattr,
2669 .setattr = proc_setattr,
2670 .permission = proc_pid_permission,
2671 };
2672
2673 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2674 {
2675 struct dentry *dentry, *leader, *dir;
2676 char buf[PROC_NUMBUF];
2677 struct qstr name;
2678
2679 name.name = buf;
2680 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2681 /* no ->d_hash() rejects on procfs */
2682 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2683 if (dentry) {
2684 shrink_dcache_parent(dentry);
2685 d_drop(dentry);
2686 dput(dentry);
2687 }
2688
2689 name.name = buf;
2690 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2691 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2692 if (!leader)
2693 goto out;
2694
2695 name.name = "task";
2696 name.len = strlen(name.name);
2697 dir = d_hash_and_lookup(leader, &name);
2698 if (!dir)
2699 goto out_put_leader;
2700
2701 name.name = buf;
2702 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2703 dentry = d_hash_and_lookup(dir, &name);
2704 if (dentry) {
2705 shrink_dcache_parent(dentry);
2706 d_drop(dentry);
2707 dput(dentry);
2708 }
2709
2710 dput(dir);
2711 out_put_leader:
2712 dput(leader);
2713 out:
2714 return;
2715 }
2716
2717 /**
2718 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2719 * @task: task that should be flushed.
2720 *
2721 * When flushing dentries from proc, one needs to flush them from global
2722 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2723 * in. This call is supposed to do all of this job.
2724 *
2725 * Looks in the dcache for
2726 * /proc/@pid
2727 * /proc/@tgid/task/@pid
2728 * if either directory is present flushes it and all of it'ts children
2729 * from the dcache.
2730 *
2731 * It is safe and reasonable to cache /proc entries for a task until
2732 * that task exits. After that they just clog up the dcache with
2733 * useless entries, possibly causing useful dcache entries to be
2734 * flushed instead. This routine is proved to flush those useless
2735 * dcache entries at process exit time.
2736 *
2737 * NOTE: This routine is just an optimization so it does not guarantee
2738 * that no dcache entries will exist at process exit time it
2739 * just makes it very unlikely that any will persist.
2740 */
2741
2742 void proc_flush_task(struct task_struct *task)
2743 {
2744 int i;
2745 struct pid *pid, *tgid;
2746 struct upid *upid;
2747
2748 pid = task_pid(task);
2749 tgid = task_tgid(task);
2750
2751 for (i = 0; i <= pid->level; i++) {
2752 upid = &pid->numbers[i];
2753 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2754 tgid->numbers[i].nr);
2755 }
2756 }
2757
2758 static int proc_pid_instantiate(struct inode *dir,
2759 struct dentry * dentry,
2760 struct task_struct *task, const void *ptr)
2761 {
2762 struct inode *inode;
2763
2764 inode = proc_pid_make_inode(dir->i_sb, task);
2765 if (!inode)
2766 goto out;
2767
2768 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2769 inode->i_op = &proc_tgid_base_inode_operations;
2770 inode->i_fop = &proc_tgid_base_operations;
2771 inode->i_flags|=S_IMMUTABLE;
2772
2773 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2774 ARRAY_SIZE(tgid_base_stuff)));
2775
2776 d_set_d_op(dentry, &pid_dentry_operations);
2777
2778 d_add(dentry, inode);
2779 /* Close the race of the process dying before we return the dentry */
2780 if (pid_revalidate(dentry, 0))
2781 return 0;
2782 out:
2783 return -ENOENT;
2784 }
2785
2786 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
2787 {
2788 int result = -ENOENT;
2789 struct task_struct *task;
2790 unsigned tgid;
2791 struct pid_namespace *ns;
2792
2793 tgid = name_to_int(&dentry->d_name);
2794 if (tgid == ~0U)
2795 goto out;
2796
2797 ns = dentry->d_sb->s_fs_info;
2798 rcu_read_lock();
2799 task = find_task_by_pid_ns(tgid, ns);
2800 if (task)
2801 get_task_struct(task);
2802 rcu_read_unlock();
2803 if (!task)
2804 goto out;
2805
2806 result = proc_pid_instantiate(dir, dentry, task, NULL);
2807 put_task_struct(task);
2808 out:
2809 return ERR_PTR(result);
2810 }
2811
2812 /*
2813 * Find the first task with tgid >= tgid
2814 *
2815 */
2816 struct tgid_iter {
2817 unsigned int tgid;
2818 struct task_struct *task;
2819 };
2820 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2821 {
2822 struct pid *pid;
2823
2824 if (iter.task)
2825 put_task_struct(iter.task);
2826 rcu_read_lock();
2827 retry:
2828 iter.task = NULL;
2829 pid = find_ge_pid(iter.tgid, ns);
2830 if (pid) {
2831 iter.tgid = pid_nr_ns(pid, ns);
2832 iter.task = pid_task(pid, PIDTYPE_PID);
2833 /* What we to know is if the pid we have find is the
2834 * pid of a thread_group_leader. Testing for task
2835 * being a thread_group_leader is the obvious thing
2836 * todo but there is a window when it fails, due to
2837 * the pid transfer logic in de_thread.
2838 *
2839 * So we perform the straight forward test of seeing
2840 * if the pid we have found is the pid of a thread
2841 * group leader, and don't worry if the task we have
2842 * found doesn't happen to be a thread group leader.
2843 * As we don't care in the case of readdir.
2844 */
2845 if (!iter.task || !has_group_leader_pid(iter.task)) {
2846 iter.tgid += 1;
2847 goto retry;
2848 }
2849 get_task_struct(iter.task);
2850 }
2851 rcu_read_unlock();
2852 return iter;
2853 }
2854
2855 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2856
2857 /* for the /proc/ directory itself, after non-process stuff has been done */
2858 int proc_pid_readdir(struct file *file, struct dir_context *ctx)
2859 {
2860 struct tgid_iter iter;
2861 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
2862 loff_t pos = ctx->pos;
2863
2864 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
2865 return 0;
2866
2867 if (pos == TGID_OFFSET - 1) {
2868 struct inode *inode = ns->proc_self->d_inode;
2869 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
2870 return 0;
2871 iter.tgid = 0;
2872 } else {
2873 iter.tgid = pos - TGID_OFFSET;
2874 }
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;
2883
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;
2890 }
2891 }
2892 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
2893 return 0;
2894 }
2895
2896 /*
2897 * Tasks
2898 */
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 REG("environ", S_IRUSR, proc_environ_operations),
2904 ONE("auxv", S_IRUSR, proc_pid_auxv),
2905 ONE("status", S_IRUGO, proc_pid_status),
2906 ONE("personality", S_IRUSR, proc_pid_personality),
2907 ONE("limits", S_IRUGO, proc_pid_limits),
2908 #ifdef CONFIG_SCHED_DEBUG
2909 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
2910 #endif
2911 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
2912 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2913 ONE("syscall", S_IRUSR, proc_pid_syscall),
2914 #endif
2915 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
2916 ONE("stat", S_IRUGO, proc_tid_stat),
2917 ONE("statm", S_IRUGO, proc_pid_statm),
2918 REG("maps", S_IRUGO, proc_tid_maps_operations),
2919 #ifdef CONFIG_CHECKPOINT_RESTORE
2920 REG("children", S_IRUGO, proc_tid_children_operations),
2921 #endif
2922 #ifdef CONFIG_NUMA
2923 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
2924 #endif
2925 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2926 LNK("cwd", proc_cwd_link),
2927 LNK("root", proc_root_link),
2928 LNK("exe", proc_exe_link),
2929 REG("mounts", S_IRUGO, proc_mounts_operations),
2930 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2931 #ifdef CONFIG_PROC_PAGE_MONITOR
2932 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2933 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
2934 REG("pagemap", S_IRUSR, proc_pagemap_operations),
2935 #endif
2936 #ifdef CONFIG_SECURITY
2937 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
2938 #endif
2939 #ifdef CONFIG_KALLSYMS
2940 ONE("wchan", S_IRUGO, proc_pid_wchan),
2941 #endif
2942 #ifdef CONFIG_STACKTRACE
2943 ONE("stack", S_IRUSR, proc_pid_stack),
2944 #endif
2945 #ifdef CONFIG_SCHEDSTATS
2946 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
2947 #endif
2948 #ifdef CONFIG_LATENCYTOP
2949 REG("latency", S_IRUGO, proc_lstats_operations),
2950 #endif
2951 #ifdef CONFIG_PROC_PID_CPUSET
2952 REG("cpuset", S_IRUGO, proc_cpuset_operations),
2953 #endif
2954 #ifdef CONFIG_CGROUPS
2955 REG("cgroup", S_IRUGO, proc_cgroup_operations),
2956 #endif
2957 INF("oom_score", S_IRUGO, proc_oom_score),
2958 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
2959 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
2960 #ifdef CONFIG_AUDITSYSCALL
2961 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2962 REG("sessionid", S_IRUGO, proc_sessionid_operations),
2963 #endif
2964 #ifdef CONFIG_FAULT_INJECTION
2965 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
2966 #endif
2967 #ifdef CONFIG_TASK_IO_ACCOUNTING
2968 INF("io", S_IRUSR, proc_tid_io_accounting),
2969 #endif
2970 #ifdef CONFIG_HARDWALL
2971 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2972 #endif
2973 #ifdef CONFIG_USER_NS
2974 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2975 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
2976 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
2977 #endif
2978 };
2979
2980 static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
2981 {
2982 return proc_pident_readdir(file, ctx,
2983 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2984 }
2985
2986 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2987 {
2988 return proc_pident_lookup(dir, dentry,
2989 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2990 }
2991
2992 static const struct file_operations proc_tid_base_operations = {
2993 .read = generic_read_dir,
2994 .iterate = proc_tid_base_readdir,
2995 .llseek = default_llseek,
2996 };
2997
2998 static const struct inode_operations proc_tid_base_inode_operations = {
2999 .lookup = proc_tid_base_lookup,
3000 .getattr = pid_getattr,
3001 .setattr = proc_setattr,
3002 };
3003
3004 static int proc_task_instantiate(struct inode *dir,
3005 struct dentry *dentry, struct task_struct *task, const void *ptr)
3006 {
3007 struct inode *inode;
3008 inode = proc_pid_make_inode(dir->i_sb, task);
3009
3010 if (!inode)
3011 goto out;
3012 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3013 inode->i_op = &proc_tid_base_inode_operations;
3014 inode->i_fop = &proc_tid_base_operations;
3015 inode->i_flags|=S_IMMUTABLE;
3016
3017 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3018 ARRAY_SIZE(tid_base_stuff)));
3019
3020 d_set_d_op(dentry, &pid_dentry_operations);
3021
3022 d_add(dentry, inode);
3023 /* Close the race of the process dying before we return the dentry */
3024 if (pid_revalidate(dentry, 0))
3025 return 0;
3026 out:
3027 return -ENOENT;
3028 }
3029
3030 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
3031 {
3032 int result = -ENOENT;
3033 struct task_struct *task;
3034 struct task_struct *leader = get_proc_task(dir);
3035 unsigned tid;
3036 struct pid_namespace *ns;
3037
3038 if (!leader)
3039 goto out_no_task;
3040
3041 tid = name_to_int(&dentry->d_name);
3042 if (tid == ~0U)
3043 goto out;
3044
3045 ns = dentry->d_sb->s_fs_info;
3046 rcu_read_lock();
3047 task = find_task_by_pid_ns(tid, ns);
3048 if (task)
3049 get_task_struct(task);
3050 rcu_read_unlock();
3051 if (!task)
3052 goto out;
3053 if (!same_thread_group(leader, task))
3054 goto out_drop_task;
3055
3056 result = proc_task_instantiate(dir, dentry, task, NULL);
3057 out_drop_task:
3058 put_task_struct(task);
3059 out:
3060 put_task_struct(leader);
3061 out_no_task:
3062 return ERR_PTR(result);
3063 }
3064
3065 /*
3066 * Find the first tid of a thread group to return to user space.
3067 *
3068 * Usually this is just the thread group leader, but if the users
3069 * buffer was too small or there was a seek into the middle of the
3070 * directory we have more work todo.
3071 *
3072 * In the case of a short read we start with find_task_by_pid.
3073 *
3074 * In the case of a seek we start with the leader and walk nr
3075 * threads past it.
3076 */
3077 static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3078 struct pid_namespace *ns)
3079 {
3080 struct task_struct *pos, *task;
3081 unsigned long nr = f_pos;
3082
3083 if (nr != f_pos) /* 32bit overflow? */
3084 return NULL;
3085
3086 rcu_read_lock();
3087 task = pid_task(pid, PIDTYPE_PID);
3088 if (!task)
3089 goto fail;
3090
3091 /* Attempt to start with the tid of a thread */
3092 if (tid && nr) {
3093 pos = find_task_by_pid_ns(tid, ns);
3094 if (pos && same_thread_group(pos, task))
3095 goto found;
3096 }
3097
3098 /* If nr exceeds the number of threads there is nothing todo */
3099 if (nr >= get_nr_threads(task))
3100 goto fail;
3101
3102 /* If we haven't found our starting place yet start
3103 * with the leader and walk nr threads forward.
3104 */
3105 pos = task = task->group_leader;
3106 do {
3107 if (!nr--)
3108 goto found;
3109 } while_each_thread(task, pos);
3110 fail:
3111 pos = NULL;
3112 goto out;
3113 found:
3114 get_task_struct(pos);
3115 out:
3116 rcu_read_unlock();
3117 return pos;
3118 }
3119
3120 /*
3121 * Find the next thread in the thread list.
3122 * Return NULL if there is an error or no next thread.
3123 *
3124 * The reference to the input task_struct is released.
3125 */
3126 static struct task_struct *next_tid(struct task_struct *start)
3127 {
3128 struct task_struct *pos = NULL;
3129 rcu_read_lock();
3130 if (pid_alive(start)) {
3131 pos = next_thread(start);
3132 if (thread_group_leader(pos))
3133 pos = NULL;
3134 else
3135 get_task_struct(pos);
3136 }
3137 rcu_read_unlock();
3138 put_task_struct(start);
3139 return pos;
3140 }
3141
3142 /* for the /proc/TGID/task/ directories */
3143 static int proc_task_readdir(struct file *file, struct dir_context *ctx)
3144 {
3145 struct inode *inode = file_inode(file);
3146 struct task_struct *task;
3147 struct pid_namespace *ns;
3148 int tid;
3149
3150 if (proc_inode_is_dead(inode))
3151 return -ENOENT;
3152
3153 if (!dir_emit_dots(file, ctx))
3154 return 0;
3155
3156 /* f_version caches the tgid value that the last readdir call couldn't
3157 * return. lseek aka telldir automagically resets f_version to 0.
3158 */
3159 ns = file->f_dentry->d_sb->s_fs_info;
3160 tid = (int)file->f_version;
3161 file->f_version = 0;
3162 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
3163 task;
3164 task = next_tid(task), ctx->pos++) {
3165 char name[PROC_NUMBUF];
3166 int len;
3167 tid = task_pid_nr_ns(task, ns);
3168 len = snprintf(name, sizeof(name), "%d", tid);
3169 if (!proc_fill_cache(file, ctx, name, len,
3170 proc_task_instantiate, task, NULL)) {
3171 /* returning this tgid failed, save it as the first
3172 * pid for the next readir call */
3173 file->f_version = (u64)tid;
3174 put_task_struct(task);
3175 break;
3176 }
3177 }
3178
3179 return 0;
3180 }
3181
3182 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3183 {
3184 struct inode *inode = dentry->d_inode;
3185 struct task_struct *p = get_proc_task(inode);
3186 generic_fillattr(inode, stat);
3187
3188 if (p) {
3189 stat->nlink += get_nr_threads(p);
3190 put_task_struct(p);
3191 }
3192
3193 return 0;
3194 }
3195
3196 static const struct inode_operations proc_task_inode_operations = {
3197 .lookup = proc_task_lookup,
3198 .getattr = proc_task_getattr,
3199 .setattr = proc_setattr,
3200 .permission = proc_pid_permission,
3201 };
3202
3203 static const struct file_operations proc_task_operations = {
3204 .read = generic_read_dir,
3205 .iterate = proc_task_readdir,
3206 .llseek = default_llseek,
3207 };