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