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