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