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