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