[readdir] convert procfs
[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 }
e1760bd5
EB
1154 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1155 if (!uid_valid(kloginuid)) {
1156 length = -EINVAL;
1157 goto out_free_page;
1158 }
1159
1160 length = audit_set_loginuid(kloginuid);
1da177e4
LT
1161 if (likely(length == 0))
1162 length = count;
1163
1164out_free_page:
1165 free_page((unsigned long) page);
1166 return length;
1167}
1168
00977a59 1169static const struct file_operations proc_loginuid_operations = {
1da177e4
LT
1170 .read = proc_loginuid_read,
1171 .write = proc_loginuid_write,
87df8424 1172 .llseek = generic_file_llseek,
1da177e4 1173};
1e0bd755
EP
1174
1175static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1176 size_t count, loff_t *ppos)
1177{
496ad9aa 1178 struct inode * inode = file_inode(file);
1e0bd755
EP
1179 struct task_struct *task = get_proc_task(inode);
1180 ssize_t length;
1181 char tmpbuf[TMPBUFLEN];
1182
1183 if (!task)
1184 return -ESRCH;
1185 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1186 audit_get_sessionid(task));
1187 put_task_struct(task);
1188 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1189}
1190
1191static const struct file_operations proc_sessionid_operations = {
1192 .read = proc_sessionid_read,
87df8424 1193 .llseek = generic_file_llseek,
1e0bd755 1194};
1da177e4
LT
1195#endif
1196
f4f154fd
AM
1197#ifdef CONFIG_FAULT_INJECTION
1198static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1199 size_t count, loff_t *ppos)
1200{
496ad9aa 1201 struct task_struct *task = get_proc_task(file_inode(file));
f4f154fd
AM
1202 char buffer[PROC_NUMBUF];
1203 size_t len;
1204 int make_it_fail;
f4f154fd
AM
1205
1206 if (!task)
1207 return -ESRCH;
1208 make_it_fail = task->make_it_fail;
1209 put_task_struct(task);
1210
1211 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
0c28f287
AM
1212
1213 return simple_read_from_buffer(buf, count, ppos, buffer, len);
f4f154fd
AM
1214}
1215
1216static ssize_t proc_fault_inject_write(struct file * file,
1217 const char __user * buf, size_t count, loff_t *ppos)
1218{
1219 struct task_struct *task;
1220 char buffer[PROC_NUMBUF], *end;
1221 int make_it_fail;
1222
1223 if (!capable(CAP_SYS_RESOURCE))
1224 return -EPERM;
1225 memset(buffer, 0, sizeof(buffer));
1226 if (count > sizeof(buffer) - 1)
1227 count = sizeof(buffer) - 1;
1228 if (copy_from_user(buffer, buf, count))
1229 return -EFAULT;
cba8aafe
VL
1230 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1231 if (*end)
1232 return -EINVAL;
496ad9aa 1233 task = get_proc_task(file_inode(file));
f4f154fd
AM
1234 if (!task)
1235 return -ESRCH;
1236 task->make_it_fail = make_it_fail;
1237 put_task_struct(task);
cba8aafe
VL
1238
1239 return count;
f4f154fd
AM
1240}
1241
00977a59 1242static const struct file_operations proc_fault_inject_operations = {
f4f154fd
AM
1243 .read = proc_fault_inject_read,
1244 .write = proc_fault_inject_write,
87df8424 1245 .llseek = generic_file_llseek,
f4f154fd
AM
1246};
1247#endif
1248
9745512c 1249
43ae34cb
IM
1250#ifdef CONFIG_SCHED_DEBUG
1251/*
1252 * Print out various scheduling related per-task fields:
1253 */
1254static int sched_show(struct seq_file *m, void *v)
1255{
1256 struct inode *inode = m->private;
1257 struct task_struct *p;
1258
43ae34cb
IM
1259 p = get_proc_task(inode);
1260 if (!p)
1261 return -ESRCH;
1262 proc_sched_show_task(p, m);
1263
1264 put_task_struct(p);
1265
1266 return 0;
1267}
1268
1269static ssize_t
1270sched_write(struct file *file, const char __user *buf,
1271 size_t count, loff_t *offset)
1272{
496ad9aa 1273 struct inode *inode = file_inode(file);
43ae34cb
IM
1274 struct task_struct *p;
1275
43ae34cb
IM
1276 p = get_proc_task(inode);
1277 if (!p)
1278 return -ESRCH;
1279 proc_sched_set_task(p);
1280
1281 put_task_struct(p);
1282
1283 return count;
1284}
1285
1286static int sched_open(struct inode *inode, struct file *filp)
1287{
c6a34058 1288 return single_open(filp, sched_show, inode);
43ae34cb
IM
1289}
1290
1291static const struct file_operations proc_pid_sched_operations = {
1292 .open = sched_open,
1293 .read = seq_read,
1294 .write = sched_write,
1295 .llseek = seq_lseek,
5ea473a1 1296 .release = single_release,
43ae34cb
IM
1297};
1298
1299#endif
1300
5091faa4
MG
1301#ifdef CONFIG_SCHED_AUTOGROUP
1302/*
1303 * Print out autogroup related information:
1304 */
1305static int sched_autogroup_show(struct seq_file *m, void *v)
1306{
1307 struct inode *inode = m->private;
1308 struct task_struct *p;
1309
1310 p = get_proc_task(inode);
1311 if (!p)
1312 return -ESRCH;
1313 proc_sched_autogroup_show_task(p, m);
1314
1315 put_task_struct(p);
1316
1317 return 0;
1318}
1319
1320static ssize_t
1321sched_autogroup_write(struct file *file, const char __user *buf,
1322 size_t count, loff_t *offset)
1323{
496ad9aa 1324 struct inode *inode = file_inode(file);
5091faa4
MG
1325 struct task_struct *p;
1326 char buffer[PROC_NUMBUF];
0a8cb8e3 1327 int nice;
5091faa4
MG
1328 int err;
1329
1330 memset(buffer, 0, sizeof(buffer));
1331 if (count > sizeof(buffer) - 1)
1332 count = sizeof(buffer) - 1;
1333 if (copy_from_user(buffer, buf, count))
1334 return -EFAULT;
1335
0a8cb8e3
AD
1336 err = kstrtoint(strstrip(buffer), 0, &nice);
1337 if (err < 0)
1338 return err;
5091faa4
MG
1339
1340 p = get_proc_task(inode);
1341 if (!p)
1342 return -ESRCH;
1343
2e5b5b3a 1344 err = proc_sched_autogroup_set_nice(p, nice);
5091faa4
MG
1345 if (err)
1346 count = err;
1347
1348 put_task_struct(p);
1349
1350 return count;
1351}
1352
1353static int sched_autogroup_open(struct inode *inode, struct file *filp)
1354{
1355 int ret;
1356
1357 ret = single_open(filp, sched_autogroup_show, NULL);
1358 if (!ret) {
1359 struct seq_file *m = filp->private_data;
1360
1361 m->private = inode;
1362 }
1363 return ret;
1364}
1365
1366static const struct file_operations proc_pid_sched_autogroup_operations = {
1367 .open = sched_autogroup_open,
1368 .read = seq_read,
1369 .write = sched_autogroup_write,
1370 .llseek = seq_lseek,
1371 .release = single_release,
1372};
1373
1374#endif /* CONFIG_SCHED_AUTOGROUP */
1375
4614a696 1376static ssize_t comm_write(struct file *file, const char __user *buf,
1377 size_t count, loff_t *offset)
1378{
496ad9aa 1379 struct inode *inode = file_inode(file);
4614a696 1380 struct task_struct *p;
1381 char buffer[TASK_COMM_LEN];
830e0fc9 1382 const size_t maxlen = sizeof(buffer) - 1;
4614a696 1383
1384 memset(buffer, 0, sizeof(buffer));
830e0fc9 1385 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
4614a696 1386 return -EFAULT;
1387
1388 p = get_proc_task(inode);
1389 if (!p)
1390 return -ESRCH;
1391
1392 if (same_thread_group(current, p))
1393 set_task_comm(p, buffer);
1394 else
1395 count = -EINVAL;
1396
1397 put_task_struct(p);
1398
1399 return count;
1400}
1401
1402static int comm_show(struct seq_file *m, void *v)
1403{
1404 struct inode *inode = m->private;
1405 struct task_struct *p;
1406
1407 p = get_proc_task(inode);
1408 if (!p)
1409 return -ESRCH;
1410
1411 task_lock(p);
1412 seq_printf(m, "%s\n", p->comm);
1413 task_unlock(p);
1414
1415 put_task_struct(p);
1416
1417 return 0;
1418}
1419
1420static int comm_open(struct inode *inode, struct file *filp)
1421{
c6a34058 1422 return single_open(filp, comm_show, inode);
4614a696 1423}
1424
1425static const struct file_operations proc_pid_set_comm_operations = {
1426 .open = comm_open,
1427 .read = seq_read,
1428 .write = comm_write,
1429 .llseek = seq_lseek,
1430 .release = single_release,
1431};
1432
7773fbc5 1433static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
925d1c40
MH
1434{
1435 struct task_struct *task;
1436 struct mm_struct *mm;
1437 struct file *exe_file;
1438
7773fbc5 1439 task = get_proc_task(dentry->d_inode);
925d1c40
MH
1440 if (!task)
1441 return -ENOENT;
1442 mm = get_task_mm(task);
1443 put_task_struct(task);
1444 if (!mm)
1445 return -ENOENT;
1446 exe_file = get_mm_exe_file(mm);
1447 mmput(mm);
1448 if (exe_file) {
1449 *exe_path = exe_file->f_path;
1450 path_get(&exe_file->f_path);
1451 fput(exe_file);
1452 return 0;
1453 } else
1454 return -ENOENT;
1455}
1456
008b150a 1457static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
1458{
1459 struct inode *inode = dentry->d_inode;
408ef013 1460 struct path path;
1da177e4
LT
1461 int error = -EACCES;
1462
778c1144
EB
1463 /* Are we allowed to snoop on the tasks file descriptors? */
1464 if (!proc_fd_access_allowed(inode))
1da177e4 1465 goto out;
1da177e4 1466
408ef013
CH
1467 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1468 if (error)
1469 goto out;
1470
b5fb63c1 1471 nd_jump_link(nd, &path);
408ef013 1472 return NULL;
1da177e4 1473out:
008b150a 1474 return ERR_PTR(error);
1da177e4
LT
1475}
1476
3dcd25f3 1477static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1da177e4 1478{
e12ba74d 1479 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
3dcd25f3 1480 char *pathname;
1da177e4
LT
1481 int len;
1482
1483 if (!tmp)
1484 return -ENOMEM;
0c28f287 1485
7b2a69ba 1486 pathname = d_path(path, tmp, PAGE_SIZE);
3dcd25f3
JB
1487 len = PTR_ERR(pathname);
1488 if (IS_ERR(pathname))
1da177e4 1489 goto out;
3dcd25f3 1490 len = tmp + PAGE_SIZE - 1 - pathname;
1da177e4
LT
1491
1492 if (len > buflen)
1493 len = buflen;
3dcd25f3 1494 if (copy_to_user(buffer, pathname, len))
1da177e4
LT
1495 len = -EFAULT;
1496 out:
1497 free_page((unsigned long)tmp);
1498 return len;
1499}
1500
1501static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1502{
1503 int error = -EACCES;
1504 struct inode *inode = dentry->d_inode;
3dcd25f3 1505 struct path path;
1da177e4 1506
778c1144
EB
1507 /* Are we allowed to snoop on the tasks file descriptors? */
1508 if (!proc_fd_access_allowed(inode))
1da177e4 1509 goto out;
1da177e4 1510
7773fbc5 1511 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1da177e4
LT
1512 if (error)
1513 goto out;
1514
3dcd25f3
JB
1515 error = do_proc_readlink(&path, buffer, buflen);
1516 path_put(&path);
1da177e4 1517out:
1da177e4
LT
1518 return error;
1519}
1520
faf60af1 1521const struct inode_operations proc_pid_link_inode_operations = {
1da177e4 1522 .readlink = proc_pid_readlink,
6d76fa58
LT
1523 .follow_link = proc_pid_follow_link,
1524 .setattr = proc_setattr,
1da177e4
LT
1525};
1526
28a6d671
EB
1527
1528/* building an inode */
1529
6b4e306a 1530struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
28a6d671
EB
1531{
1532 struct inode * inode;
1533 struct proc_inode *ei;
c69e8d9c 1534 const struct cred *cred;
1da177e4 1535
28a6d671 1536 /* We need a new inode */
1da177e4 1537
28a6d671
EB
1538 inode = new_inode(sb);
1539 if (!inode)
1540 goto out;
1541
1542 /* Common stuff */
1543 ei = PROC_I(inode);
85fe4025 1544 inode->i_ino = get_next_ino();
28a6d671 1545 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
28a6d671
EB
1546 inode->i_op = &proc_def_inode_operations;
1547
1548 /*
1549 * grab the reference to task.
1550 */
1a657f78 1551 ei->pid = get_task_pid(task, PIDTYPE_PID);
28a6d671
EB
1552 if (!ei->pid)
1553 goto out_unlock;
1554
28a6d671 1555 if (task_dumpable(task)) {
c69e8d9c
DH
1556 rcu_read_lock();
1557 cred = __task_cred(task);
1558 inode->i_uid = cred->euid;
1559 inode->i_gid = cred->egid;
1560 rcu_read_unlock();
1da177e4 1561 }
28a6d671
EB
1562 security_task_to_inode(task, inode);
1563
1da177e4 1564out:
28a6d671
EB
1565 return inode;
1566
1567out_unlock:
1568 iput(inode);
1569 return NULL;
1da177e4
LT
1570}
1571
6b4e306a 1572int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1da177e4 1573{
1da177e4 1574 struct inode *inode = dentry->d_inode;
28a6d671 1575 struct task_struct *task;
c69e8d9c 1576 const struct cred *cred;
0499680a 1577 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
c69e8d9c 1578
28a6d671 1579 generic_fillattr(inode, stat);
1da177e4 1580
28a6d671 1581 rcu_read_lock();
dcb0f222
EB
1582 stat->uid = GLOBAL_ROOT_UID;
1583 stat->gid = GLOBAL_ROOT_GID;
28a6d671
EB
1584 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1585 if (task) {
0499680a
VK
1586 if (!has_pid_permissions(pid, task, 2)) {
1587 rcu_read_unlock();
1588 /*
1589 * This doesn't prevent learning whether PID exists,
1590 * it only makes getattr() consistent with readdir().
1591 */
1592 return -ENOENT;
1593 }
28a6d671
EB
1594 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1595 task_dumpable(task)) {
c69e8d9c
DH
1596 cred = __task_cred(task);
1597 stat->uid = cred->euid;
1598 stat->gid = cred->egid;
1da177e4
LT
1599 }
1600 }
28a6d671 1601 rcu_read_unlock();
d6e71144 1602 return 0;
1da177e4
LT
1603}
1604
1da177e4
LT
1605/* dentry stuff */
1606
1607/*
1608 * Exceptional case: normally we are not allowed to unhash a busy
1609 * directory. In this case, however, we can do it - no aliasing problems
1610 * due to the way we treat inodes.
1611 *
1612 * Rewrite the inode's ownerships here because the owning task may have
1613 * performed a setuid(), etc.
99f89551
EB
1614 *
1615 * Before the /proc/pid/status file was created the only way to read
1616 * the effective uid of a /process was to stat /proc/pid. Reading
1617 * /proc/pid/status is slow enough that procps and other packages
1618 * kept stating /proc/pid. To keep the rules in /proc simple I have
1619 * made this apply to all per process world readable and executable
1620 * directories.
1da177e4 1621 */
0b728e19 1622int pid_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4 1623{
34286d66
NP
1624 struct inode *inode;
1625 struct task_struct *task;
c69e8d9c
DH
1626 const struct cred *cred;
1627
0b728e19 1628 if (flags & LOOKUP_RCU)
34286d66
NP
1629 return -ECHILD;
1630
1631 inode = dentry->d_inode;
1632 task = get_proc_task(inode);
1633
99f89551
EB
1634 if (task) {
1635 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1636 task_dumpable(task)) {
c69e8d9c
DH
1637 rcu_read_lock();
1638 cred = __task_cred(task);
1639 inode->i_uid = cred->euid;
1640 inode->i_gid = cred->egid;
1641 rcu_read_unlock();
1da177e4 1642 } else {
dcb0f222
EB
1643 inode->i_uid = GLOBAL_ROOT_UID;
1644 inode->i_gid = GLOBAL_ROOT_GID;
1da177e4 1645 }
9ee8ab9f 1646 inode->i_mode &= ~(S_ISUID | S_ISGID);
1da177e4 1647 security_task_to_inode(task, inode);
99f89551 1648 put_task_struct(task);
1da177e4
LT
1649 return 1;
1650 }
1651 d_drop(dentry);
1652 return 0;
1653}
1654
1dd704b6
DH
1655int pid_delete_dentry(const struct dentry *dentry)
1656{
1657 /* Is the task we represent dead?
1658 * If so, then don't put the dentry on the lru list,
1659 * kill it immediately.
1660 */
1661 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1662}
1663
6b4e306a 1664const struct dentry_operations pid_dentry_operations =
28a6d671
EB
1665{
1666 .d_revalidate = pid_revalidate,
1667 .d_delete = pid_delete_dentry,
1668};
1669
1670/* Lookups */
1671
1c0d04c9
EB
1672/*
1673 * Fill a directory entry.
1674 *
1675 * If possible create the dcache entry and derive our inode number and
1676 * file type from dcache entry.
1677 *
1678 * Since all of the proc inode numbers are dynamically generated, the inode
1679 * numbers do not exist until the inode is cache. This means creating the
1680 * the dcache entry in readdir is necessary to keep the inode numbers
1681 * reported by readdir in sync with the inode numbers reported
1682 * by stat.
1683 */
f0c3b509 1684bool proc_fill_cache(struct file *file, struct dir_context *ctx,
6b4e306a 1685 const char *name, int len,
c5141e6d 1686 instantiate_t instantiate, struct task_struct *task, const void *ptr)
61a28784 1687{
f0c3b509 1688 struct dentry *child, *dir = file->f_path.dentry;
61a28784
EB
1689 struct inode *inode;
1690 struct qstr qname;
1691 ino_t ino = 0;
1692 unsigned type = DT_UNKNOWN;
1693
1694 qname.name = name;
1695 qname.len = len;
1696 qname.hash = full_name_hash(name, len);
1697
1698 child = d_lookup(dir, &qname);
1699 if (!child) {
1700 struct dentry *new;
1701 new = d_alloc(dir, &qname);
1702 if (new) {
1703 child = instantiate(dir->d_inode, new, task, ptr);
1704 if (child)
1705 dput(new);
1706 else
1707 child = new;
1708 }
1709 }
1710 if (!child || IS_ERR(child) || !child->d_inode)
1711 goto end_instantiate;
1712 inode = child->d_inode;
1713 if (inode) {
1714 ino = inode->i_ino;
1715 type = inode->i_mode >> 12;
1716 }
1717 dput(child);
1718end_instantiate:
1719 if (!ino)
1720 ino = find_inode_number(dir, &qname);
1721 if (!ino)
1722 ino = 1;
f0c3b509 1723 return dir_emit(ctx, name, len, ino, type);
61a28784
EB
1724}
1725
640708a2
PE
1726#ifdef CONFIG_CHECKPOINT_RESTORE
1727
1728/*
1729 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1730 * which represent vma start and end addresses.
1731 */
1732static int dname_to_vma_addr(struct dentry *dentry,
1733 unsigned long *start, unsigned long *end)
1734{
1735 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1736 return -EINVAL;
1737
1738 return 0;
1739}
1740
0b728e19 1741static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
640708a2
PE
1742{
1743 unsigned long vm_start, vm_end;
1744 bool exact_vma_exists = false;
1745 struct mm_struct *mm = NULL;
1746 struct task_struct *task;
1747 const struct cred *cred;
1748 struct inode *inode;
1749 int status = 0;
1750
0b728e19 1751 if (flags & LOOKUP_RCU)
640708a2
PE
1752 return -ECHILD;
1753
1754 if (!capable(CAP_SYS_ADMIN)) {
41735818 1755 status = -EPERM;
640708a2
PE
1756 goto out_notask;
1757 }
1758
1759 inode = dentry->d_inode;
1760 task = get_proc_task(inode);
1761 if (!task)
1762 goto out_notask;
1763
2344bec7
CW
1764 mm = mm_access(task, PTRACE_MODE_READ);
1765 if (IS_ERR_OR_NULL(mm))
640708a2
PE
1766 goto out;
1767
1768 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1769 down_read(&mm->mmap_sem);
1770 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1771 up_read(&mm->mmap_sem);
1772 }
1773
1774 mmput(mm);
1775
1776 if (exact_vma_exists) {
1777 if (task_dumpable(task)) {
1778 rcu_read_lock();
1779 cred = __task_cred(task);
1780 inode->i_uid = cred->euid;
1781 inode->i_gid = cred->egid;
1782 rcu_read_unlock();
1783 } else {
dcb0f222
EB
1784 inode->i_uid = GLOBAL_ROOT_UID;
1785 inode->i_gid = GLOBAL_ROOT_GID;
640708a2
PE
1786 }
1787 security_task_to_inode(task, inode);
1788 status = 1;
1789 }
1790
1791out:
1792 put_task_struct(task);
1793
1794out_notask:
1795 if (status <= 0)
1796 d_drop(dentry);
1797
1798 return status;
1799}
1800
1801static const struct dentry_operations tid_map_files_dentry_operations = {
1802 .d_revalidate = map_files_d_revalidate,
1803 .d_delete = pid_delete_dentry,
1804};
1805
1806static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1807{
1808 unsigned long vm_start, vm_end;
1809 struct vm_area_struct *vma;
1810 struct task_struct *task;
1811 struct mm_struct *mm;
1812 int rc;
1813
1814 rc = -ENOENT;
1815 task = get_proc_task(dentry->d_inode);
1816 if (!task)
1817 goto out;
1818
1819 mm = get_task_mm(task);
1820 put_task_struct(task);
1821 if (!mm)
1822 goto out;
1823
1824 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1825 if (rc)
1826 goto out_mmput;
1827
1828 down_read(&mm->mmap_sem);
1829 vma = find_exact_vma(mm, vm_start, vm_end);
1830 if (vma && vma->vm_file) {
1831 *path = vma->vm_file->f_path;
1832 path_get(path);
1833 rc = 0;
1834 }
1835 up_read(&mm->mmap_sem);
1836
1837out_mmput:
1838 mmput(mm);
1839out:
1840 return rc;
1841}
1842
1843struct map_files_info {
7b540d06 1844 fmode_t mode;
640708a2
PE
1845 unsigned long len;
1846 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1847};
1848
1849static struct dentry *
1850proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1851 struct task_struct *task, const void *ptr)
1852{
7b540d06 1853 fmode_t mode = (fmode_t)(unsigned long)ptr;
640708a2
PE
1854 struct proc_inode *ei;
1855 struct inode *inode;
1856
640708a2
PE
1857 inode = proc_pid_make_inode(dir->i_sb, task);
1858 if (!inode)
1859 return ERR_PTR(-ENOENT);
1860
1861 ei = PROC_I(inode);
1862 ei->op.proc_get_link = proc_map_files_get_link;
1863
1864 inode->i_op = &proc_pid_link_inode_operations;
1865 inode->i_size = 64;
1866 inode->i_mode = S_IFLNK;
1867
7b540d06 1868 if (mode & FMODE_READ)
640708a2 1869 inode->i_mode |= S_IRUSR;
7b540d06 1870 if (mode & FMODE_WRITE)
640708a2
PE
1871 inode->i_mode |= S_IWUSR;
1872
1873 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1874 d_add(dentry, inode);
1875
1876 return NULL;
1877}
1878
1879static struct dentry *proc_map_files_lookup(struct inode *dir,
00cd8dd3 1880 struct dentry *dentry, unsigned int flags)
640708a2
PE
1881{
1882 unsigned long vm_start, vm_end;
1883 struct vm_area_struct *vma;
1884 struct task_struct *task;
1885 struct dentry *result;
1886 struct mm_struct *mm;
1887
41735818 1888 result = ERR_PTR(-EPERM);
640708a2
PE
1889 if (!capable(CAP_SYS_ADMIN))
1890 goto out;
1891
1892 result = ERR_PTR(-ENOENT);
1893 task = get_proc_task(dir);
1894 if (!task)
1895 goto out;
1896
1897 result = ERR_PTR(-EACCES);
eb94cd96 1898 if (!ptrace_may_access(task, PTRACE_MODE_READ))
640708a2
PE
1899 goto out_put_task;
1900
1901 result = ERR_PTR(-ENOENT);
1902 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
eb94cd96 1903 goto out_put_task;
640708a2
PE
1904
1905 mm = get_task_mm(task);
1906 if (!mm)
eb94cd96 1907 goto out_put_task;
640708a2
PE
1908
1909 down_read(&mm->mmap_sem);
1910 vma = find_exact_vma(mm, vm_start, vm_end);
1911 if (!vma)
1912 goto out_no_vma;
1913
05f56484
SK
1914 if (vma->vm_file)
1915 result = proc_map_files_instantiate(dir, dentry, task,
1916 (void *)(unsigned long)vma->vm_file->f_mode);
640708a2
PE
1917
1918out_no_vma:
1919 up_read(&mm->mmap_sem);
1920 mmput(mm);
640708a2
PE
1921out_put_task:
1922 put_task_struct(task);
1923out:
1924 return result;
1925}
1926
1927static const struct inode_operations proc_map_files_inode_operations = {
1928 .lookup = proc_map_files_lookup,
1929 .permission = proc_fd_permission,
1930 .setattr = proc_setattr,
1931};
1932
1933static int
f0c3b509 1934proc_map_files_readdir(struct file *file, struct dir_context *ctx)
640708a2 1935{
640708a2
PE
1936 struct vm_area_struct *vma;
1937 struct task_struct *task;
1938 struct mm_struct *mm;
f0c3b509
AV
1939 unsigned long nr_files, pos, i;
1940 struct flex_array *fa = NULL;
1941 struct map_files_info info;
1942 struct map_files_info *p;
640708a2
PE
1943 int ret;
1944
41735818 1945 ret = -EPERM;
640708a2
PE
1946 if (!capable(CAP_SYS_ADMIN))
1947 goto out;
1948
1949 ret = -ENOENT;
f0c3b509 1950 task = get_proc_task(file_inode(file));
640708a2
PE
1951 if (!task)
1952 goto out;
1953
1954 ret = -EACCES;
eb94cd96 1955 if (!ptrace_may_access(task, PTRACE_MODE_READ))
640708a2
PE
1956 goto out_put_task;
1957
1958 ret = 0;
f0c3b509
AV
1959 if (!dir_emit_dots(file, ctx))
1960 goto out_put_task;
640708a2 1961
f0c3b509
AV
1962 mm = get_task_mm(task);
1963 if (!mm)
1964 goto out_put_task;
1965 down_read(&mm->mmap_sem);
640708a2 1966
f0c3b509 1967 nr_files = 0;
640708a2 1968
f0c3b509
AV
1969 /*
1970 * We need two passes here:
1971 *
1972 * 1) Collect vmas of mapped files with mmap_sem taken
1973 * 2) Release mmap_sem and instantiate entries
1974 *
1975 * otherwise we get lockdep complained, since filldir()
1976 * routine might require mmap_sem taken in might_fault().
1977 */
640708a2 1978
f0c3b509
AV
1979 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1980 if (vma->vm_file && ++pos > ctx->pos)
1981 nr_files++;
1982 }
1983
1984 if (nr_files) {
1985 fa = flex_array_alloc(sizeof(info), nr_files,
1986 GFP_KERNEL);
1987 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1988 GFP_KERNEL)) {
1989 ret = -ENOMEM;
1990 if (fa)
1991 flex_array_free(fa);
1992 up_read(&mm->mmap_sem);
1993 mmput(mm);
1994 goto out_put_task;
640708a2 1995 }
f0c3b509
AV
1996 for (i = 0, vma = mm->mmap, pos = 2; vma;
1997 vma = vma->vm_next) {
1998 if (!vma->vm_file)
1999 continue;
2000 if (++pos <= ctx->pos)
2001 continue;
2002
2003 info.mode = vma->vm_file->f_mode;
2004 info.len = snprintf(info.name,
2005 sizeof(info.name), "%lx-%lx",
2006 vma->vm_start, vma->vm_end);
2007 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2008 BUG();
640708a2 2009 }
640708a2 2010 }
f0c3b509
AV
2011 up_read(&mm->mmap_sem);
2012
2013 for (i = 0; i < nr_files; i++) {
2014 p = flex_array_get(fa, i);
2015 if (!proc_fill_cache(file, ctx,
2016 p->name, p->len,
2017 proc_map_files_instantiate,
2018 task,
2019 (void *)(unsigned long)p->mode))
2020 break;
2021 ctx->pos++;
640708a2 2022 }
f0c3b509
AV
2023 if (fa)
2024 flex_array_free(fa);
2025 mmput(mm);
640708a2 2026
640708a2
PE
2027out_put_task:
2028 put_task_struct(task);
2029out:
2030 return ret;
2031}
2032
2033static const struct file_operations proc_map_files_operations = {
2034 .read = generic_read_dir,
f0c3b509 2035 .iterate = proc_map_files_readdir,
640708a2
PE
2036 .llseek = default_llseek,
2037};
2038
48f6a7a5
PE
2039struct timers_private {
2040 struct pid *pid;
2041 struct task_struct *task;
2042 struct sighand_struct *sighand;
57b8015e 2043 struct pid_namespace *ns;
48f6a7a5
PE
2044 unsigned long flags;
2045};
2046
2047static void *timers_start(struct seq_file *m, loff_t *pos)
2048{
2049 struct timers_private *tp = m->private;
2050
2051 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2052 if (!tp->task)
2053 return ERR_PTR(-ESRCH);
2054
2055 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2056 if (!tp->sighand)
2057 return ERR_PTR(-ESRCH);
2058
2059 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2060}
2061
2062static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2063{
2064 struct timers_private *tp = m->private;
2065 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2066}
2067
2068static void timers_stop(struct seq_file *m, void *v)
2069{
2070 struct timers_private *tp = m->private;
2071
2072 if (tp->sighand) {
2073 unlock_task_sighand(tp->task, &tp->flags);
2074 tp->sighand = NULL;
2075 }
2076
2077 if (tp->task) {
2078 put_task_struct(tp->task);
2079 tp->task = NULL;
2080 }
2081}
2082
2083static int show_timer(struct seq_file *m, void *v)
2084{
2085 struct k_itimer *timer;
57b8015e
PE
2086 struct timers_private *tp = m->private;
2087 int notify;
2088 static char *nstr[] = {
2089 [SIGEV_SIGNAL] = "signal",
2090 [SIGEV_NONE] = "none",
2091 [SIGEV_THREAD] = "thread",
2092 };
48f6a7a5
PE
2093
2094 timer = list_entry((struct list_head *)v, struct k_itimer, list);
57b8015e
PE
2095 notify = timer->it_sigev_notify;
2096
48f6a7a5 2097 seq_printf(m, "ID: %d\n", timer->it_id);
57b8015e
PE
2098 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2099 timer->sigq->info.si_value.sival_ptr);
2100 seq_printf(m, "notify: %s/%s.%d\n",
2101 nstr[notify & ~SIGEV_THREAD_ID],
2102 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2103 pid_nr_ns(timer->it_pid, tp->ns));
15ef0298 2104 seq_printf(m, "ClockID: %d\n", timer->it_clock);
48f6a7a5
PE
2105
2106 return 0;
2107}
2108
2109static const struct seq_operations proc_timers_seq_ops = {
2110 .start = timers_start,
2111 .next = timers_next,
2112 .stop = timers_stop,
2113 .show = show_timer,
2114};
2115
2116static int proc_timers_open(struct inode *inode, struct file *file)
2117{
2118 struct timers_private *tp;
2119
2120 tp = __seq_open_private(file, &proc_timers_seq_ops,
2121 sizeof(struct timers_private));
2122 if (!tp)
2123 return -ENOMEM;
2124
2125 tp->pid = proc_pid(inode);
57b8015e 2126 tp->ns = inode->i_sb->s_fs_info;
48f6a7a5
PE
2127 return 0;
2128}
2129
2130static const struct file_operations proc_timers_operations = {
2131 .open = proc_timers_open,
2132 .read = seq_read,
2133 .llseek = seq_lseek,
2134 .release = seq_release_private,
2135};
640708a2
PE
2136#endif /* CONFIG_CHECKPOINT_RESTORE */
2137
444ceed8 2138static struct dentry *proc_pident_instantiate(struct inode *dir,
c5141e6d 2139 struct dentry *dentry, struct task_struct *task, const void *ptr)
444ceed8 2140{
c5141e6d 2141 const struct pid_entry *p = ptr;
444ceed8
EB
2142 struct inode *inode;
2143 struct proc_inode *ei;
bd6daba9 2144 struct dentry *error = ERR_PTR(-ENOENT);
444ceed8 2145
61a28784 2146 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
2147 if (!inode)
2148 goto out;
2149
2150 ei = PROC_I(inode);
2151 inode->i_mode = p->mode;
2152 if (S_ISDIR(inode->i_mode))
bfe86848 2153 set_nlink(inode, 2); /* Use getattr to fix if necessary */
444ceed8
EB
2154 if (p->iop)
2155 inode->i_op = p->iop;
2156 if (p->fop)
2157 inode->i_fop = p->fop;
2158 ei->op = p->op;
fb045adb 2159 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
2160 d_add(dentry, inode);
2161 /* Close the race of the process dying before we return the dentry */
0b728e19 2162 if (pid_revalidate(dentry, 0))
444ceed8
EB
2163 error = NULL;
2164out:
2165 return error;
2166}
2167
1da177e4
LT
2168static struct dentry *proc_pident_lookup(struct inode *dir,
2169 struct dentry *dentry,
c5141e6d 2170 const struct pid_entry *ents,
7bcd6b0e 2171 unsigned int nents)
1da177e4 2172{
cd6a3ce9 2173 struct dentry *error;
99f89551 2174 struct task_struct *task = get_proc_task(dir);
c5141e6d 2175 const struct pid_entry *p, *last;
1da177e4 2176
cd6a3ce9 2177 error = ERR_PTR(-ENOENT);
1da177e4 2178
99f89551
EB
2179 if (!task)
2180 goto out_no_task;
1da177e4 2181
20cdc894
EB
2182 /*
2183 * Yes, it does not scale. And it should not. Don't add
2184 * new entries into /proc/<tgid>/ without very good reasons.
2185 */
7bcd6b0e
EB
2186 last = &ents[nents - 1];
2187 for (p = ents; p <= last; p++) {
1da177e4
LT
2188 if (p->len != dentry->d_name.len)
2189 continue;
2190 if (!memcmp(dentry->d_name.name, p->name, p->len))
2191 break;
2192 }
7bcd6b0e 2193 if (p > last)
1da177e4
LT
2194 goto out;
2195
444ceed8 2196 error = proc_pident_instantiate(dir, dentry, task, p);
1da177e4 2197out:
99f89551
EB
2198 put_task_struct(task);
2199out_no_task:
cd6a3ce9 2200 return error;
1da177e4
LT
2201}
2202
f0c3b509 2203static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
c5141e6d 2204 const struct pid_entry *ents, unsigned int nents)
28a6d671 2205{
f0c3b509
AV
2206 struct task_struct *task = get_proc_task(file_inode(file));
2207 const struct pid_entry *p;
28a6d671 2208
28a6d671 2209 if (!task)
f0c3b509 2210 return -ENOENT;
28a6d671 2211
f0c3b509
AV
2212 if (!dir_emit_dots(file, ctx))
2213 goto out;
2214
2215 if (ctx->pos >= nents + 2)
2216 goto out;
28a6d671 2217
f0c3b509
AV
2218 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2219 if (!proc_fill_cache(file, ctx, p->name, p->len,
2220 proc_pident_instantiate, task, p))
2221 break;
2222 ctx->pos++;
2223 }
28a6d671 2224out:
61a28784 2225 put_task_struct(task);
f0c3b509 2226 return 0;
1da177e4
LT
2227}
2228
28a6d671
EB
2229#ifdef CONFIG_SECURITY
2230static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2231 size_t count, loff_t *ppos)
2232{
496ad9aa 2233 struct inode * inode = file_inode(file);
04ff9708 2234 char *p = NULL;
28a6d671
EB
2235 ssize_t length;
2236 struct task_struct *task = get_proc_task(inode);
2237
28a6d671 2238 if (!task)
04ff9708 2239 return -ESRCH;
28a6d671
EB
2240
2241 length = security_getprocattr(task,
2fddfeef 2242 (char*)file->f_path.dentry->d_name.name,
04ff9708 2243 &p);
28a6d671 2244 put_task_struct(task);
04ff9708
AV
2245 if (length > 0)
2246 length = simple_read_from_buffer(buf, count, ppos, p, length);
2247 kfree(p);
28a6d671 2248 return length;
1da177e4
LT
2249}
2250
28a6d671
EB
2251static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2252 size_t count, loff_t *ppos)
2253{
496ad9aa 2254 struct inode * inode = file_inode(file);
28a6d671
EB
2255 char *page;
2256 ssize_t length;
2257 struct task_struct *task = get_proc_task(inode);
2258
2259 length = -ESRCH;
2260 if (!task)
2261 goto out_no_task;
2262 if (count > PAGE_SIZE)
2263 count = PAGE_SIZE;
2264
2265 /* No partial writes. */
2266 length = -EINVAL;
2267 if (*ppos != 0)
2268 goto out;
2269
2270 length = -ENOMEM;
e12ba74d 2271 page = (char*)__get_free_page(GFP_TEMPORARY);
28a6d671
EB
2272 if (!page)
2273 goto out;
2274
2275 length = -EFAULT;
2276 if (copy_from_user(page, buf, count))
2277 goto out_free;
2278
107db7c7 2279 /* Guard against adverse ptrace interaction */
9b1bf12d 2280 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
107db7c7
DH
2281 if (length < 0)
2282 goto out_free;
2283
28a6d671 2284 length = security_setprocattr(task,
2fddfeef 2285 (char*)file->f_path.dentry->d_name.name,
28a6d671 2286 (void*)page, count);
9b1bf12d 2287 mutex_unlock(&task->signal->cred_guard_mutex);
28a6d671
EB
2288out_free:
2289 free_page((unsigned long) page);
2290out:
2291 put_task_struct(task);
2292out_no_task:
2293 return length;
2294}
2295
00977a59 2296static const struct file_operations proc_pid_attr_operations = {
28a6d671
EB
2297 .read = proc_pid_attr_read,
2298 .write = proc_pid_attr_write,
87df8424 2299 .llseek = generic_file_llseek,
28a6d671
EB
2300};
2301
c5141e6d 2302static const struct pid_entry attr_dir_stuff[] = {
631f9c18
AD
2303 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2304 REG("prev", S_IRUGO, proc_pid_attr_operations),
2305 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2306 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2307 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2308 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
28a6d671
EB
2309};
2310
f0c3b509 2311static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
28a6d671 2312{
f0c3b509
AV
2313 return proc_pident_readdir(file, ctx,
2314 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
2315}
2316
00977a59 2317static const struct file_operations proc_attr_dir_operations = {
1da177e4 2318 .read = generic_read_dir,
f0c3b509 2319 .iterate = proc_attr_dir_readdir,
6038f373 2320 .llseek = default_llseek,
1da177e4
LT
2321};
2322
72d9dcfc 2323static struct dentry *proc_attr_dir_lookup(struct inode *dir,
00cd8dd3 2324 struct dentry *dentry, unsigned int flags)
28a6d671 2325{
7bcd6b0e
EB
2326 return proc_pident_lookup(dir, dentry,
2327 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
2328}
2329
c5ef1c42 2330static const struct inode_operations proc_attr_dir_inode_operations = {
72d9dcfc 2331 .lookup = proc_attr_dir_lookup,
99f89551 2332 .getattr = pid_getattr,
6d76fa58 2333 .setattr = proc_setattr,
1da177e4
LT
2334};
2335
28a6d671
EB
2336#endif
2337
698ba7b5 2338#ifdef CONFIG_ELF_CORE
3cb4a0bb
KH
2339static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2340 size_t count, loff_t *ppos)
2341{
496ad9aa 2342 struct task_struct *task = get_proc_task(file_inode(file));
3cb4a0bb
KH
2343 struct mm_struct *mm;
2344 char buffer[PROC_NUMBUF];
2345 size_t len;
2346 int ret;
2347
2348 if (!task)
2349 return -ESRCH;
2350
2351 ret = 0;
2352 mm = get_task_mm(task);
2353 if (mm) {
2354 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2355 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2356 MMF_DUMP_FILTER_SHIFT));
2357 mmput(mm);
2358 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2359 }
2360
2361 put_task_struct(task);
2362
2363 return ret;
2364}
2365
2366static ssize_t proc_coredump_filter_write(struct file *file,
2367 const char __user *buf,
2368 size_t count,
2369 loff_t *ppos)
2370{
2371 struct task_struct *task;
2372 struct mm_struct *mm;
2373 char buffer[PROC_NUMBUF], *end;
2374 unsigned int val;
2375 int ret;
2376 int i;
2377 unsigned long mask;
2378
2379 ret = -EFAULT;
2380 memset(buffer, 0, sizeof(buffer));
2381 if (count > sizeof(buffer) - 1)
2382 count = sizeof(buffer) - 1;
2383 if (copy_from_user(buffer, buf, count))
2384 goto out_no_task;
2385
2386 ret = -EINVAL;
2387 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2388 if (*end == '\n')
2389 end++;
2390 if (end - buffer == 0)
2391 goto out_no_task;
2392
2393 ret = -ESRCH;
496ad9aa 2394 task = get_proc_task(file_inode(file));
3cb4a0bb
KH
2395 if (!task)
2396 goto out_no_task;
2397
2398 ret = end - buffer;
2399 mm = get_task_mm(task);
2400 if (!mm)
2401 goto out_no_mm;
2402
2403 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2404 if (val & mask)
2405 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2406 else
2407 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2408 }
2409
2410 mmput(mm);
2411 out_no_mm:
2412 put_task_struct(task);
2413 out_no_task:
2414 return ret;
2415}
2416
2417static const struct file_operations proc_coredump_filter_operations = {
2418 .read = proc_coredump_filter_read,
2419 .write = proc_coredump_filter_write,
87df8424 2420 .llseek = generic_file_llseek,
3cb4a0bb
KH
2421};
2422#endif
2423
aba76fdb 2424#ifdef CONFIG_TASK_IO_ACCOUNTING
297c5d92
AR
2425static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
2426{
940389b8 2427 struct task_io_accounting acct = task->ioac;
5995477a 2428 unsigned long flags;
293eb1e7 2429 int result;
5995477a 2430
293eb1e7
VK
2431 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2432 if (result)
2433 return result;
2434
2435 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2436 result = -EACCES;
2437 goto out_unlock;
2438 }
1d1221f3 2439
5995477a
AR
2440 if (whole && lock_task_sighand(task, &flags)) {
2441 struct task_struct *t = task;
2442
2443 task_io_accounting_add(&acct, &task->signal->ioac);
2444 while_each_thread(task, t)
2445 task_io_accounting_add(&acct, &t->ioac);
2446
2447 unlock_task_sighand(task, &flags);
297c5d92 2448 }
293eb1e7 2449 result = sprintf(buffer,
aba76fdb
AM
2450 "rchar: %llu\n"
2451 "wchar: %llu\n"
2452 "syscr: %llu\n"
2453 "syscw: %llu\n"
2454 "read_bytes: %llu\n"
2455 "write_bytes: %llu\n"
2456 "cancelled_write_bytes: %llu\n",
7c44319d
AB
2457 (unsigned long long)acct.rchar,
2458 (unsigned long long)acct.wchar,
2459 (unsigned long long)acct.syscr,
2460 (unsigned long long)acct.syscw,
2461 (unsigned long long)acct.read_bytes,
2462 (unsigned long long)acct.write_bytes,
2463 (unsigned long long)acct.cancelled_write_bytes);
293eb1e7
VK
2464out_unlock:
2465 mutex_unlock(&task->signal->cred_guard_mutex);
2466 return result;
297c5d92
AR
2467}
2468
2469static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2470{
2471 return do_io_accounting(task, buffer, 0);
aba76fdb 2472}
297c5d92
AR
2473
2474static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2475{
2476 return do_io_accounting(task, buffer, 1);
2477}
2478#endif /* CONFIG_TASK_IO_ACCOUNTING */
aba76fdb 2479
22d917d8
EB
2480#ifdef CONFIG_USER_NS
2481static int proc_id_map_open(struct inode *inode, struct file *file,
2482 struct seq_operations *seq_ops)
2483{
2484 struct user_namespace *ns = NULL;
2485 struct task_struct *task;
2486 struct seq_file *seq;
2487 int ret = -EINVAL;
2488
2489 task = get_proc_task(inode);
2490 if (task) {
2491 rcu_read_lock();
2492 ns = get_user_ns(task_cred_xxx(task, user_ns));
2493 rcu_read_unlock();
2494 put_task_struct(task);
2495 }
2496 if (!ns)
2497 goto err;
2498
2499 ret = seq_open(file, seq_ops);
2500 if (ret)
2501 goto err_put_ns;
2502
2503 seq = file->private_data;
2504 seq->private = ns;
2505
2506 return 0;
2507err_put_ns:
2508 put_user_ns(ns);
2509err:
2510 return ret;
2511}
2512
2513static int proc_id_map_release(struct inode *inode, struct file *file)
2514{
2515 struct seq_file *seq = file->private_data;
2516 struct user_namespace *ns = seq->private;
2517 put_user_ns(ns);
2518 return seq_release(inode, file);
2519}
2520
2521static int proc_uid_map_open(struct inode *inode, struct file *file)
2522{
2523 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2524}
2525
2526static int proc_gid_map_open(struct inode *inode, struct file *file)
2527{
2528 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2529}
2530
f76d207a
EB
2531static int proc_projid_map_open(struct inode *inode, struct file *file)
2532{
2533 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2534}
2535
22d917d8
EB
2536static const struct file_operations proc_uid_map_operations = {
2537 .open = proc_uid_map_open,
2538 .write = proc_uid_map_write,
2539 .read = seq_read,
2540 .llseek = seq_lseek,
2541 .release = proc_id_map_release,
2542};
2543
2544static const struct file_operations proc_gid_map_operations = {
2545 .open = proc_gid_map_open,
2546 .write = proc_gid_map_write,
2547 .read = seq_read,
2548 .llseek = seq_lseek,
2549 .release = proc_id_map_release,
2550};
f76d207a
EB
2551
2552static const struct file_operations proc_projid_map_operations = {
2553 .open = proc_projid_map_open,
2554 .write = proc_projid_map_write,
2555 .read = seq_read,
2556 .llseek = seq_lseek,
2557 .release = proc_id_map_release,
2558};
22d917d8
EB
2559#endif /* CONFIG_USER_NS */
2560
47830723
KC
2561static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2562 struct pid *pid, struct task_struct *task)
2563{
a9712bc1
AV
2564 int err = lock_trace(task);
2565 if (!err) {
2566 seq_printf(m, "%08x\n", task->personality);
2567 unlock_trace(task);
2568 }
2569 return err;
47830723
KC
2570}
2571
28a6d671
EB
2572/*
2573 * Thread groups
2574 */
00977a59 2575static const struct file_operations proc_task_operations;
c5ef1c42 2576static const struct inode_operations proc_task_inode_operations;
20cdc894 2577
c5141e6d 2578static const struct pid_entry tgid_base_stuff[] = {
631f9c18
AD
2579 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2580 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
640708a2
PE
2581#ifdef CONFIG_CHECKPOINT_RESTORE
2582 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2583#endif
631f9c18 2584 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
6b4e306a 2585 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
b2211a36 2586#ifdef CONFIG_NET
631f9c18 2587 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
b2211a36 2588#endif
631f9c18
AD
2589 REG("environ", S_IRUSR, proc_environ_operations),
2590 INF("auxv", S_IRUSR, proc_pid_auxv),
2591 ONE("status", S_IRUGO, proc_pid_status),
a9712bc1 2592 ONE("personality", S_IRUGO, proc_pid_personality),
3036e7b4 2593 INF("limits", S_IRUGO, proc_pid_limits),
43ae34cb 2594#ifdef CONFIG_SCHED_DEBUG
631f9c18 2595 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
5091faa4
MG
2596#endif
2597#ifdef CONFIG_SCHED_AUTOGROUP
2598 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
ebcb6734 2599#endif
4614a696 2600 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
ebcb6734 2601#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
a9712bc1 2602 INF("syscall", S_IRUGO, proc_pid_syscall),
43ae34cb 2603#endif
631f9c18
AD
2604 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2605 ONE("stat", S_IRUGO, proc_tgid_stat),
2606 ONE("statm", S_IRUGO, proc_pid_statm),
b7643757 2607 REG("maps", S_IRUGO, proc_pid_maps_operations),
28a6d671 2608#ifdef CONFIG_NUMA
b7643757 2609 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
28a6d671 2610#endif
631f9c18
AD
2611 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2612 LNK("cwd", proc_cwd_link),
2613 LNK("root", proc_root_link),
2614 LNK("exe", proc_exe_link),
2615 REG("mounts", S_IRUGO, proc_mounts_operations),
2616 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2617 REG("mountstats", S_IRUSR, proc_mountstats_operations),
1e883281 2618#ifdef CONFIG_PROC_PAGE_MONITOR
631f9c18 2619 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
b7643757 2620 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
ca6b0bf0 2621 REG("pagemap", S_IRUGO, proc_pagemap_operations),
28a6d671
EB
2622#endif
2623#ifdef CONFIG_SECURITY
631f9c18 2624 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
28a6d671
EB
2625#endif
2626#ifdef CONFIG_KALLSYMS
631f9c18 2627 INF("wchan", S_IRUGO, proc_pid_wchan),
28a6d671 2628#endif
2ec220e2 2629#ifdef CONFIG_STACKTRACE
a9712bc1 2630 ONE("stack", S_IRUGO, proc_pid_stack),
28a6d671
EB
2631#endif
2632#ifdef CONFIG_SCHEDSTATS
631f9c18 2633 INF("schedstat", S_IRUGO, proc_pid_schedstat),
28a6d671 2634#endif
9745512c 2635#ifdef CONFIG_LATENCYTOP
631f9c18 2636 REG("latency", S_IRUGO, proc_lstats_operations),
9745512c 2637#endif
8793d854 2638#ifdef CONFIG_PROC_PID_CPUSET
631f9c18 2639 REG("cpuset", S_IRUGO, proc_cpuset_operations),
a424316c
PM
2640#endif
2641#ifdef CONFIG_CGROUPS
631f9c18 2642 REG("cgroup", S_IRUGO, proc_cgroup_operations),
28a6d671 2643#endif
631f9c18 2644 INF("oom_score", S_IRUGO, proc_oom_score),
fa0cbbf1 2645 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
a63d83f4 2646 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
28a6d671 2647#ifdef CONFIG_AUDITSYSCALL
631f9c18
AD
2648 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2649 REG("sessionid", S_IRUGO, proc_sessionid_operations),
28a6d671 2650#endif
f4f154fd 2651#ifdef CONFIG_FAULT_INJECTION
631f9c18 2652 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
f4f154fd 2653#endif
698ba7b5 2654#ifdef CONFIG_ELF_CORE
631f9c18 2655 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
3cb4a0bb 2656#endif
aba76fdb 2657#ifdef CONFIG_TASK_IO_ACCOUNTING
1d1221f3 2658 INF("io", S_IRUSR, proc_tgid_io_accounting),
aba76fdb 2659#endif
f133ecca
CM
2660#ifdef CONFIG_HARDWALL
2661 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2662#endif
22d917d8
EB
2663#ifdef CONFIG_USER_NS
2664 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2665 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
f76d207a 2666 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
22d917d8 2667#endif
48f6a7a5
PE
2668#ifdef CONFIG_CHECKPOINT_RESTORE
2669 REG("timers", S_IRUGO, proc_timers_operations),
2670#endif
28a6d671 2671};
1da177e4 2672
f0c3b509 2673static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
1da177e4 2674{
f0c3b509
AV
2675 return proc_pident_readdir(file, ctx,
2676 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
2677}
2678
00977a59 2679static const struct file_operations proc_tgid_base_operations = {
1da177e4 2680 .read = generic_read_dir,
f0c3b509 2681 .iterate = proc_tgid_base_readdir,
6038f373 2682 .llseek = default_llseek,
1da177e4
LT
2683};
2684
00cd8dd3
AV
2685static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2686{
7bcd6b0e
EB
2687 return proc_pident_lookup(dir, dentry,
2688 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
2689}
2690
c5ef1c42 2691static const struct inode_operations proc_tgid_base_inode_operations = {
28a6d671 2692 .lookup = proc_tgid_base_lookup,
99f89551 2693 .getattr = pid_getattr,
6d76fa58 2694 .setattr = proc_setattr,
0499680a 2695 .permission = proc_pid_permission,
1da177e4 2696};
1da177e4 2697
60347f67 2698static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
1da177e4 2699{
48e6484d 2700 struct dentry *dentry, *leader, *dir;
8578cea7 2701 char buf[PROC_NUMBUF];
48e6484d
EB
2702 struct qstr name;
2703
2704 name.name = buf;
60347f67 2705 name.len = snprintf(buf, sizeof(buf), "%d", pid);
4f522a24 2706 /* no ->d_hash() rejects on procfs */
60347f67 2707 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
48e6484d 2708 if (dentry) {
29f12ca3 2709 shrink_dcache_parent(dentry);
48e6484d
EB
2710 d_drop(dentry);
2711 dput(dentry);
2712 }
1da177e4 2713
48e6484d 2714 name.name = buf;
60347f67
PE
2715 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2716 leader = d_hash_and_lookup(mnt->mnt_root, &name);
48e6484d
EB
2717 if (!leader)
2718 goto out;
1da177e4 2719
48e6484d
EB
2720 name.name = "task";
2721 name.len = strlen(name.name);
2722 dir = d_hash_and_lookup(leader, &name);
2723 if (!dir)
2724 goto out_put_leader;
2725
2726 name.name = buf;
60347f67 2727 name.len = snprintf(buf, sizeof(buf), "%d", pid);
48e6484d
EB
2728 dentry = d_hash_and_lookup(dir, &name);
2729 if (dentry) {
2730 shrink_dcache_parent(dentry);
2731 d_drop(dentry);
2732 dput(dentry);
1da177e4 2733 }
48e6484d
EB
2734
2735 dput(dir);
2736out_put_leader:
2737 dput(leader);
2738out:
2739 return;
1da177e4
LT
2740}
2741
0895e91d
RD
2742/**
2743 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2744 * @task: task that should be flushed.
2745 *
2746 * When flushing dentries from proc, one needs to flush them from global
60347f67 2747 * proc (proc_mnt) and from all the namespaces' procs this task was seen
0895e91d
RD
2748 * in. This call is supposed to do all of this job.
2749 *
2750 * Looks in the dcache for
2751 * /proc/@pid
2752 * /proc/@tgid/task/@pid
2753 * if either directory is present flushes it and all of it'ts children
2754 * from the dcache.
2755 *
2756 * It is safe and reasonable to cache /proc entries for a task until
2757 * that task exits. After that they just clog up the dcache with
2758 * useless entries, possibly causing useful dcache entries to be
2759 * flushed instead. This routine is proved to flush those useless
2760 * dcache entries at process exit time.
2761 *
2762 * NOTE: This routine is just an optimization so it does not guarantee
2763 * that no dcache entries will exist at process exit time it
2764 * just makes it very unlikely that any will persist.
60347f67
PE
2765 */
2766
2767void proc_flush_task(struct task_struct *task)
2768{
9fcc2d15 2769 int i;
9b4d1cbe 2770 struct pid *pid, *tgid;
130f77ec
PE
2771 struct upid *upid;
2772
130f77ec 2773 pid = task_pid(task);
9b4d1cbe 2774 tgid = task_tgid(task);
130f77ec 2775
9fcc2d15 2776 for (i = 0; i <= pid->level; i++) {
130f77ec
PE
2777 upid = &pid->numbers[i];
2778 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
9b4d1cbe 2779 tgid->numbers[i].nr);
130f77ec 2780 }
60347f67
PE
2781}
2782
9711ef99
AB
2783static struct dentry *proc_pid_instantiate(struct inode *dir,
2784 struct dentry * dentry,
c5141e6d 2785 struct task_struct *task, const void *ptr)
444ceed8
EB
2786{
2787 struct dentry *error = ERR_PTR(-ENOENT);
2788 struct inode *inode;
2789
61a28784 2790 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
2791 if (!inode)
2792 goto out;
2793
2794 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2795 inode->i_op = &proc_tgid_base_inode_operations;
2796 inode->i_fop = &proc_tgid_base_operations;
2797 inode->i_flags|=S_IMMUTABLE;
aed54175 2798
bfe86848
MS
2799 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2800 ARRAY_SIZE(tgid_base_stuff)));
444ceed8 2801
fb045adb 2802 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
2803
2804 d_add(dentry, inode);
2805 /* Close the race of the process dying before we return the dentry */
0b728e19 2806 if (pid_revalidate(dentry, 0))
444ceed8
EB
2807 error = NULL;
2808out:
2809 return error;
2810}
2811
00cd8dd3 2812struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
1da177e4 2813{
e656d8a6 2814 struct dentry *result = NULL;
1da177e4 2815 struct task_struct *task;
1da177e4 2816 unsigned tgid;
b488893a 2817 struct pid_namespace *ns;
1da177e4 2818
1da177e4
LT
2819 tgid = name_to_int(dentry);
2820 if (tgid == ~0U)
2821 goto out;
2822
b488893a 2823 ns = dentry->d_sb->s_fs_info;
de758734 2824 rcu_read_lock();
b488893a 2825 task = find_task_by_pid_ns(tgid, ns);
1da177e4
LT
2826 if (task)
2827 get_task_struct(task);
de758734 2828 rcu_read_unlock();
1da177e4
LT
2829 if (!task)
2830 goto out;
2831
444ceed8 2832 result = proc_pid_instantiate(dir, dentry, task, NULL);
1da177e4 2833 put_task_struct(task);
1da177e4 2834out:
cd6a3ce9 2835 return result;
1da177e4
LT
2836}
2837
1da177e4 2838/*
0804ef4b 2839 * Find the first task with tgid >= tgid
0bc58a91 2840 *
1da177e4 2841 */
19fd4bb2
EB
2842struct tgid_iter {
2843 unsigned int tgid;
0804ef4b 2844 struct task_struct *task;
19fd4bb2
EB
2845};
2846static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2847{
0804ef4b 2848 struct pid *pid;
1da177e4 2849
19fd4bb2
EB
2850 if (iter.task)
2851 put_task_struct(iter.task);
454cc105 2852 rcu_read_lock();
0804ef4b 2853retry:
19fd4bb2
EB
2854 iter.task = NULL;
2855 pid = find_ge_pid(iter.tgid, ns);
0804ef4b 2856 if (pid) {
19fd4bb2
EB
2857 iter.tgid = pid_nr_ns(pid, ns);
2858 iter.task = pid_task(pid, PIDTYPE_PID);
0804ef4b
EB
2859 /* What we to know is if the pid we have find is the
2860 * pid of a thread_group_leader. Testing for task
2861 * being a thread_group_leader is the obvious thing
2862 * todo but there is a window when it fails, due to
2863 * the pid transfer logic in de_thread.
2864 *
2865 * So we perform the straight forward test of seeing
2866 * if the pid we have found is the pid of a thread
2867 * group leader, and don't worry if the task we have
2868 * found doesn't happen to be a thread group leader.
2869 * As we don't care in the case of readdir.
2870 */
19fd4bb2
EB
2871 if (!iter.task || !has_group_leader_pid(iter.task)) {
2872 iter.tgid += 1;
0804ef4b 2873 goto retry;
19fd4bb2
EB
2874 }
2875 get_task_struct(iter.task);
0bc58a91 2876 }
454cc105 2877 rcu_read_unlock();
19fd4bb2 2878 return iter;
1da177e4
LT
2879}
2880
021ada7d 2881#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
0804ef4b 2882
1da177e4 2883/* for the /proc/ directory itself, after non-process stuff has been done */
f0c3b509 2884int proc_pid_readdir(struct file *file, struct dir_context *ctx)
1da177e4 2885{
19fd4bb2 2886 struct tgid_iter iter;
b488893a 2887 struct pid_namespace *ns;
f0c3b509 2888 loff_t pos = ctx->pos;
1da177e4 2889
021ada7d 2890 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
f0c3b509 2891 return 0;
1da177e4 2892
021ada7d 2893 if (pos == TGID_OFFSET - 1) {
f0c3b509
AV
2894 if (!proc_fill_cache(file, ctx, "self", 4, NULL, NULL, NULL))
2895 return 0;
021ada7d
AV
2896 iter.tgid = 0;
2897 } else {
2898 iter.tgid = pos - TGID_OFFSET;
2899 }
19fd4bb2 2900 iter.task = NULL;
f0c3b509 2901 ns = file->f_dentry->d_sb->s_fs_info;
19fd4bb2
EB
2902 for (iter = next_tgid(ns, iter);
2903 iter.task;
2904 iter.tgid += 1, iter = next_tgid(ns, iter)) {
f0c3b509
AV
2905 char name[PROC_NUMBUF];
2906 int len;
2907 if (!has_pid_permissions(ns, iter.task, 2))
2908 continue;
0499680a 2909
f0c3b509
AV
2910 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2911 ctx->pos = iter.tgid + TGID_OFFSET;
2912 if (!proc_fill_cache(file, ctx, name, len,
2913 proc_pid_instantiate, iter.task, NULL)) {
19fd4bb2 2914 put_task_struct(iter.task);
f0c3b509 2915 return 0;
1da177e4 2916 }
0bc58a91 2917 }
f0c3b509 2918 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
0bc58a91
EB
2919 return 0;
2920}
1da177e4 2921
28a6d671
EB
2922/*
2923 * Tasks
2924 */
c5141e6d 2925static const struct pid_entry tid_base_stuff[] = {
631f9c18 2926 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
3835541d 2927 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
6b4e306a 2928 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
631f9c18
AD
2929 REG("environ", S_IRUSR, proc_environ_operations),
2930 INF("auxv", S_IRUSR, proc_pid_auxv),
2931 ONE("status", S_IRUGO, proc_pid_status),
a9712bc1 2932 ONE("personality", S_IRUGO, proc_pid_personality),
3036e7b4 2933 INF("limits", S_IRUGO, proc_pid_limits),
43ae34cb 2934#ifdef CONFIG_SCHED_DEBUG
631f9c18 2935 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
ebcb6734 2936#endif
4614a696 2937 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
ebcb6734 2938#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
a9712bc1 2939 INF("syscall", S_IRUGO, proc_pid_syscall),
43ae34cb 2940#endif
631f9c18
AD
2941 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2942 ONE("stat", S_IRUGO, proc_tid_stat),
2943 ONE("statm", S_IRUGO, proc_pid_statm),
b7643757 2944 REG("maps", S_IRUGO, proc_tid_maps_operations),
81841161
CG
2945#ifdef CONFIG_CHECKPOINT_RESTORE
2946 REG("children", S_IRUGO, proc_tid_children_operations),
2947#endif
28a6d671 2948#ifdef CONFIG_NUMA
b7643757 2949 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
28a6d671 2950#endif
631f9c18
AD
2951 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2952 LNK("cwd", proc_cwd_link),
2953 LNK("root", proc_root_link),
2954 LNK("exe", proc_exe_link),
2955 REG("mounts", S_IRUGO, proc_mounts_operations),
2956 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
1e883281 2957#ifdef CONFIG_PROC_PAGE_MONITOR
631f9c18 2958 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
b7643757 2959 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
ca6b0bf0 2960 REG("pagemap", S_IRUGO, proc_pagemap_operations),
28a6d671
EB
2961#endif
2962#ifdef CONFIG_SECURITY
631f9c18 2963 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
28a6d671
EB
2964#endif
2965#ifdef CONFIG_KALLSYMS
631f9c18 2966 INF("wchan", S_IRUGO, proc_pid_wchan),
28a6d671 2967#endif
2ec220e2 2968#ifdef CONFIG_STACKTRACE
a9712bc1 2969 ONE("stack", S_IRUGO, proc_pid_stack),
28a6d671
EB
2970#endif
2971#ifdef CONFIG_SCHEDSTATS
631f9c18 2972 INF("schedstat", S_IRUGO, proc_pid_schedstat),
28a6d671 2973#endif
9745512c 2974#ifdef CONFIG_LATENCYTOP
631f9c18 2975 REG("latency", S_IRUGO, proc_lstats_operations),
9745512c 2976#endif
8793d854 2977#ifdef CONFIG_PROC_PID_CPUSET
631f9c18 2978 REG("cpuset", S_IRUGO, proc_cpuset_operations),
a424316c
PM
2979#endif
2980#ifdef CONFIG_CGROUPS
631f9c18 2981 REG("cgroup", S_IRUGO, proc_cgroup_operations),
28a6d671 2982#endif
631f9c18 2983 INF("oom_score", S_IRUGO, proc_oom_score),
fa0cbbf1 2984 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
a63d83f4 2985 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
28a6d671 2986#ifdef CONFIG_AUDITSYSCALL
631f9c18 2987 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
26ec3c64 2988 REG("sessionid", S_IRUGO, proc_sessionid_operations),
28a6d671 2989#endif
f4f154fd 2990#ifdef CONFIG_FAULT_INJECTION
631f9c18 2991 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
f4f154fd 2992#endif
297c5d92 2993#ifdef CONFIG_TASK_IO_ACCOUNTING
1d1221f3 2994 INF("io", S_IRUSR, proc_tid_io_accounting),
297c5d92 2995#endif
f133ecca
CM
2996#ifdef CONFIG_HARDWALL
2997 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2998#endif
22d917d8
EB
2999#ifdef CONFIG_USER_NS
3000 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3001 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
f76d207a 3002 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
22d917d8 3003#endif
28a6d671
EB
3004};
3005
f0c3b509 3006static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
28a6d671 3007{
f0c3b509
AV
3008 return proc_pident_readdir(file, ctx,
3009 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
28a6d671
EB
3010}
3011
00cd8dd3
AV
3012static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3013{
7bcd6b0e
EB
3014 return proc_pident_lookup(dir, dentry,
3015 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
28a6d671
EB
3016}
3017
00977a59 3018static const struct file_operations proc_tid_base_operations = {
28a6d671 3019 .read = generic_read_dir,
f0c3b509 3020 .iterate = proc_tid_base_readdir,
6038f373 3021 .llseek = default_llseek,
28a6d671
EB
3022};
3023
c5ef1c42 3024static const struct inode_operations proc_tid_base_inode_operations = {
28a6d671
EB
3025 .lookup = proc_tid_base_lookup,
3026 .getattr = pid_getattr,
3027 .setattr = proc_setattr,
3028};
3029
444ceed8 3030static struct dentry *proc_task_instantiate(struct inode *dir,
c5141e6d 3031 struct dentry *dentry, struct task_struct *task, const void *ptr)
444ceed8
EB
3032{
3033 struct dentry *error = ERR_PTR(-ENOENT);
3034 struct inode *inode;
61a28784 3035 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
3036
3037 if (!inode)
3038 goto out;
3039 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3040 inode->i_op = &proc_tid_base_inode_operations;
3041 inode->i_fop = &proc_tid_base_operations;
3042 inode->i_flags|=S_IMMUTABLE;
aed54175 3043
bfe86848
MS
3044 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3045 ARRAY_SIZE(tid_base_stuff)));
444ceed8 3046
fb045adb 3047 d_set_d_op(dentry, &pid_dentry_operations);
444ceed8
EB
3048
3049 d_add(dentry, inode);
3050 /* Close the race of the process dying before we return the dentry */
0b728e19 3051 if (pid_revalidate(dentry, 0))
444ceed8
EB
3052 error = NULL;
3053out:
3054 return error;
3055}
3056
00cd8dd3 3057static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
28a6d671
EB
3058{
3059 struct dentry *result = ERR_PTR(-ENOENT);
3060 struct task_struct *task;
3061 struct task_struct *leader = get_proc_task(dir);
28a6d671 3062 unsigned tid;
b488893a 3063 struct pid_namespace *ns;
28a6d671
EB
3064
3065 if (!leader)
3066 goto out_no_task;
3067
3068 tid = name_to_int(dentry);
3069 if (tid == ~0U)
3070 goto out;
3071
b488893a 3072 ns = dentry->d_sb->s_fs_info;
28a6d671 3073 rcu_read_lock();
b488893a 3074 task = find_task_by_pid_ns(tid, ns);
28a6d671
EB
3075 if (task)
3076 get_task_struct(task);
3077 rcu_read_unlock();
3078 if (!task)
3079 goto out;
bac0abd6 3080 if (!same_thread_group(leader, task))
28a6d671
EB
3081 goto out_drop_task;
3082
444ceed8 3083 result = proc_task_instantiate(dir, dentry, task, NULL);
28a6d671
EB
3084out_drop_task:
3085 put_task_struct(task);
3086out:
3087 put_task_struct(leader);
3088out_no_task:
3089 return result;
3090}
3091
0bc58a91
EB
3092/*
3093 * Find the first tid of a thread group to return to user space.
3094 *
3095 * Usually this is just the thread group leader, but if the users
3096 * buffer was too small or there was a seek into the middle of the
3097 * directory we have more work todo.
3098 *
3099 * In the case of a short read we start with find_task_by_pid.
3100 *
3101 * In the case of a seek we start with the leader and walk nr
3102 * threads past it.
3103 */
cc288738 3104static struct task_struct *first_tid(struct task_struct *leader,
b488893a 3105 int tid, int nr, struct pid_namespace *ns)
0bc58a91 3106{
a872ff0c 3107 struct task_struct *pos;
1da177e4 3108
cc288738 3109 rcu_read_lock();
0bc58a91
EB
3110 /* Attempt to start with the pid of a thread */
3111 if (tid && (nr > 0)) {
b488893a 3112 pos = find_task_by_pid_ns(tid, ns);
a872ff0c
ON
3113 if (pos && (pos->group_leader == leader))
3114 goto found;
0bc58a91 3115 }
1da177e4 3116
0bc58a91 3117 /* If nr exceeds the number of threads there is nothing todo */
a872ff0c
ON
3118 pos = NULL;
3119 if (nr && nr >= get_nr_threads(leader))
3120 goto out;
1da177e4 3121
a872ff0c
ON
3122 /* If we haven't found our starting place yet start
3123 * with the leader and walk nr threads forward.
0bc58a91 3124 */
a872ff0c
ON
3125 for (pos = leader; nr > 0; --nr) {
3126 pos = next_thread(pos);
3127 if (pos == leader) {
3128 pos = NULL;
3129 goto out;
3130 }
1da177e4 3131 }
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{
7d895244 3164 struct task_struct *leader = NULL;
f0c3b509 3165 struct task_struct *task = get_proc_task(file_inode(file));
b488893a 3166 struct pid_namespace *ns;
f0c3b509 3167 int tid;
1da177e4 3168
7d895244 3169 if (!task)
f0c3b509 3170 return -ENOENT;
7d895244
GC
3171 rcu_read_lock();
3172 if (pid_alive(task)) {
3173 leader = task->group_leader;
3174 get_task_struct(leader);
3175 }
3176 rcu_read_unlock();
3177 put_task_struct(task);
99f89551 3178 if (!leader)
f0c3b509 3179 return -ENOENT;
1da177e4 3180
f0c3b509
AV
3181 if (!dir_emit_dots(file, ctx))
3182 goto out;
1da177e4 3183
0bc58a91
EB
3184 /* f_version caches the tgid value that the last readdir call couldn't
3185 * return. lseek aka telldir automagically resets f_version to 0.
3186 */
f0c3b509
AV
3187 ns = file->f_dentry->d_sb->s_fs_info;
3188 tid = (int)file->f_version;
3189 file->f_version = 0;
3190 for (task = first_tid(leader, tid, ctx->pos - 2, ns);
0bc58a91 3191 task;
f0c3b509
AV
3192 task = next_tid(task), ctx->pos++) {
3193 char name[PROC_NUMBUF];
3194 int len;
b488893a 3195 tid = task_pid_nr_ns(task, ns);
f0c3b509
AV
3196 len = snprintf(name, sizeof(name), "%d", tid);
3197 if (!proc_fill_cache(file, ctx, name, len,
3198 proc_task_instantiate, task, NULL)) {
0bc58a91
EB
3199 /* returning this tgid failed, save it as the first
3200 * pid for the next readir call */
f0c3b509 3201 file->f_version = (u64)tid;
0bc58a91 3202 put_task_struct(task);
1da177e4 3203 break;
0bc58a91 3204 }
1da177e4
LT
3205 }
3206out:
99f89551 3207 put_task_struct(leader);
f0c3b509 3208 return 0;
1da177e4 3209}
6e66b52b
EB
3210
3211static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3212{
3213 struct inode *inode = dentry->d_inode;
99f89551 3214 struct task_struct *p = get_proc_task(inode);
6e66b52b
EB
3215 generic_fillattr(inode, stat);
3216
99f89551 3217 if (p) {
99f89551 3218 stat->nlink += get_nr_threads(p);
99f89551 3219 put_task_struct(p);
6e66b52b
EB
3220 }
3221
3222 return 0;
3223}
28a6d671 3224
c5ef1c42 3225static const struct inode_operations proc_task_inode_operations = {
28a6d671
EB
3226 .lookup = proc_task_lookup,
3227 .getattr = proc_task_getattr,
3228 .setattr = proc_setattr,
0499680a 3229 .permission = proc_pid_permission,
28a6d671
EB
3230};
3231
00977a59 3232static const struct file_operations proc_task_operations = {
28a6d671 3233 .read = generic_read_dir,
f0c3b509 3234 .iterate = proc_task_readdir,
6038f373 3235 .llseek = default_llseek,
28a6d671 3236};
This page took 1.043774 seconds and 5 git commands to generate.