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