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