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