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