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