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