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