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