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