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