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