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