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