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