[PATCH] fix deadlock in audit_log_task_context()
[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
1da177e4
LT
718#ifdef CONFIG_AUDITSYSCALL
719#define TMPBUFLEN 21
720static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
721 size_t count, loff_t *ppos)
722{
2fddfeef 723 struct inode * inode = file->f_path.dentry->d_inode;
99f89551 724 struct task_struct *task = get_proc_task(inode);
1da177e4
LT
725 ssize_t length;
726 char tmpbuf[TMPBUFLEN];
727
99f89551
EB
728 if (!task)
729 return -ESRCH;
1da177e4
LT
730 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
731 audit_get_loginuid(task->audit_context));
99f89551 732 put_task_struct(task);
1da177e4
LT
733 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
734}
735
736static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
737 size_t count, loff_t *ppos)
738{
2fddfeef 739 struct inode * inode = file->f_path.dentry->d_inode;
1da177e4
LT
740 char *page, *tmp;
741 ssize_t length;
1da177e4
LT
742 uid_t loginuid;
743
744 if (!capable(CAP_AUDIT_CONTROL))
745 return -EPERM;
746
13b41b09 747 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1da177e4
LT
748 return -EPERM;
749
e0182909
AV
750 if (count >= PAGE_SIZE)
751 count = PAGE_SIZE - 1;
1da177e4
LT
752
753 if (*ppos != 0) {
754 /* No partial writes. */
755 return -EINVAL;
756 }
757 page = (char*)__get_free_page(GFP_USER);
758 if (!page)
759 return -ENOMEM;
760 length = -EFAULT;
761 if (copy_from_user(page, buf, count))
762 goto out_free_page;
763
e0182909 764 page[count] = '\0';
1da177e4
LT
765 loginuid = simple_strtoul(page, &tmp, 10);
766 if (tmp == page) {
767 length = -EINVAL;
768 goto out_free_page;
769
770 }
99f89551 771 length = audit_set_loginuid(current, loginuid);
1da177e4
LT
772 if (likely(length == 0))
773 length = count;
774
775out_free_page:
776 free_page((unsigned long) page);
777 return length;
778}
779
00977a59 780static const struct file_operations proc_loginuid_operations = {
1da177e4
LT
781 .read = proc_loginuid_read,
782 .write = proc_loginuid_write,
783};
784#endif
785
786#ifdef CONFIG_SECCOMP
787static ssize_t seccomp_read(struct file *file, char __user *buf,
788 size_t count, loff_t *ppos)
789{
99f89551 790 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1da177e4
LT
791 char __buf[20];
792 loff_t __ppos = *ppos;
793 size_t len;
794
99f89551
EB
795 if (!tsk)
796 return -ESRCH;
1da177e4
LT
797 /* no need to print the trailing zero, so use only len */
798 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
99f89551 799 put_task_struct(tsk);
1da177e4
LT
800 if (__ppos >= len)
801 return 0;
802 if (count > len - __ppos)
803 count = len - __ppos;
804 if (copy_to_user(buf, __buf + __ppos, count))
805 return -EFAULT;
806 *ppos = __ppos + count;
807 return count;
808}
809
810static ssize_t seccomp_write(struct file *file, const char __user *buf,
811 size_t count, loff_t *ppos)
812{
99f89551 813 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
1da177e4
LT
814 char __buf[20], *end;
815 unsigned int seccomp_mode;
99f89551
EB
816 ssize_t result;
817
818 result = -ESRCH;
819 if (!tsk)
820 goto out_no_task;
1da177e4
LT
821
822 /* can set it only once to be even more secure */
99f89551 823 result = -EPERM;
1da177e4 824 if (unlikely(tsk->seccomp.mode))
99f89551 825 goto out;
1da177e4 826
99f89551 827 result = -EFAULT;
1da177e4
LT
828 memset(__buf, 0, sizeof(__buf));
829 count = min(count, sizeof(__buf) - 1);
830 if (copy_from_user(__buf, buf, count))
99f89551
EB
831 goto out;
832
1da177e4
LT
833 seccomp_mode = simple_strtoul(__buf, &end, 0);
834 if (*end == '\n')
835 end++;
99f89551 836 result = -EINVAL;
1da177e4
LT
837 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
838 tsk->seccomp.mode = seccomp_mode;
839 set_tsk_thread_flag(tsk, TIF_SECCOMP);
840 } else
99f89551
EB
841 goto out;
842 result = -EIO;
1da177e4 843 if (unlikely(!(end - __buf)))
99f89551
EB
844 goto out;
845 result = end - __buf;
846out:
847 put_task_struct(tsk);
848out_no_task:
849 return result;
1da177e4
LT
850}
851
00977a59 852static const struct file_operations proc_seccomp_operations = {
1da177e4
LT
853 .read = seccomp_read,
854 .write = seccomp_write,
855};
856#endif /* CONFIG_SECCOMP */
857
f4f154fd
AM
858#ifdef CONFIG_FAULT_INJECTION
859static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
860 size_t count, loff_t *ppos)
861{
862 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
863 char buffer[PROC_NUMBUF];
864 size_t len;
865 int make_it_fail;
866 loff_t __ppos = *ppos;
867
868 if (!task)
869 return -ESRCH;
870 make_it_fail = task->make_it_fail;
871 put_task_struct(task);
872
873 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
874 if (__ppos >= len)
875 return 0;
876 if (count > len-__ppos)
877 count = len-__ppos;
878 if (copy_to_user(buf, buffer + __ppos, count))
879 return -EFAULT;
880 *ppos = __ppos + count;
881 return count;
882}
883
884static ssize_t proc_fault_inject_write(struct file * file,
885 const char __user * buf, size_t count, loff_t *ppos)
886{
887 struct task_struct *task;
888 char buffer[PROC_NUMBUF], *end;
889 int make_it_fail;
890
891 if (!capable(CAP_SYS_RESOURCE))
892 return -EPERM;
893 memset(buffer, 0, sizeof(buffer));
894 if (count > sizeof(buffer) - 1)
895 count = sizeof(buffer) - 1;
896 if (copy_from_user(buffer, buf, count))
897 return -EFAULT;
898 make_it_fail = simple_strtol(buffer, &end, 0);
899 if (*end == '\n')
900 end++;
901 task = get_proc_task(file->f_dentry->d_inode);
902 if (!task)
903 return -ESRCH;
904 task->make_it_fail = make_it_fail;
905 put_task_struct(task);
906 if (end - buffer == 0)
907 return -EIO;
908 return end - buffer;
909}
910
00977a59 911static const struct file_operations proc_fault_inject_operations = {
f4f154fd
AM
912 .read = proc_fault_inject_read,
913 .write = proc_fault_inject_write,
914};
915#endif
916
008b150a 917static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
918{
919 struct inode *inode = dentry->d_inode;
920 int error = -EACCES;
921
922 /* We don't need a base pointer in the /proc filesystem */
923 path_release(nd);
924
778c1144
EB
925 /* Are we allowed to snoop on the tasks file descriptors? */
926 if (!proc_fd_access_allowed(inode))
1da177e4 927 goto out;
1da177e4
LT
928
929 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
930 nd->last_type = LAST_BIND;
931out:
008b150a 932 return ERR_PTR(error);
1da177e4
LT
933}
934
935static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
936 char __user *buffer, int buflen)
937{
938 struct inode * inode;
939 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
940 int len;
941
942 if (!tmp)
943 return -ENOMEM;
944
945 inode = dentry->d_inode;
946 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
947 len = PTR_ERR(path);
948 if (IS_ERR(path))
949 goto out;
950 len = tmp + PAGE_SIZE - 1 - path;
951
952 if (len > buflen)
953 len = buflen;
954 if (copy_to_user(buffer, path, len))
955 len = -EFAULT;
956 out:
957 free_page((unsigned long)tmp);
958 return len;
959}
960
961static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
962{
963 int error = -EACCES;
964 struct inode *inode = dentry->d_inode;
965 struct dentry *de;
966 struct vfsmount *mnt = NULL;
967
778c1144
EB
968 /* Are we allowed to snoop on the tasks file descriptors? */
969 if (!proc_fd_access_allowed(inode))
1da177e4 970 goto out;
1da177e4
LT
971
972 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
973 if (error)
974 goto out;
975
976 error = do_proc_readlink(de, mnt, buffer, buflen);
977 dput(de);
978 mntput(mnt);
979out:
1da177e4
LT
980 return error;
981}
982
c5ef1c42 983static const struct inode_operations proc_pid_link_inode_operations = {
1da177e4 984 .readlink = proc_pid_readlink,
6d76fa58
LT
985 .follow_link = proc_pid_follow_link,
986 .setattr = proc_setattr,
1da177e4
LT
987};
988
28a6d671
EB
989
990/* building an inode */
991
992static int task_dumpable(struct task_struct *task)
1da177e4 993{
28a6d671
EB
994 int dumpable = 0;
995 struct mm_struct *mm;
1da177e4 996
28a6d671
EB
997 task_lock(task);
998 mm = task->mm;
999 if (mm)
1000 dumpable = mm->dumpable;
1001 task_unlock(task);
1002 if(dumpable == 1)
1003 return 1;
1004 return 0;
1005}
1da177e4 1006
1da177e4 1007
61a28784 1008static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
28a6d671
EB
1009{
1010 struct inode * inode;
1011 struct proc_inode *ei;
1da177e4 1012
28a6d671 1013 /* We need a new inode */
1da177e4 1014
28a6d671
EB
1015 inode = new_inode(sb);
1016 if (!inode)
1017 goto out;
1018
1019 /* Common stuff */
1020 ei = PROC_I(inode);
1021 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
28a6d671
EB
1022 inode->i_op = &proc_def_inode_operations;
1023
1024 /*
1025 * grab the reference to task.
1026 */
1a657f78 1027 ei->pid = get_task_pid(task, PIDTYPE_PID);
28a6d671
EB
1028 if (!ei->pid)
1029 goto out_unlock;
1030
1031 inode->i_uid = 0;
1032 inode->i_gid = 0;
1033 if (task_dumpable(task)) {
1034 inode->i_uid = task->euid;
1035 inode->i_gid = task->egid;
1da177e4 1036 }
28a6d671
EB
1037 security_task_to_inode(task, inode);
1038
1da177e4 1039out:
28a6d671
EB
1040 return inode;
1041
1042out_unlock:
1043 iput(inode);
1044 return NULL;
1da177e4
LT
1045}
1046
28a6d671 1047static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1da177e4 1048{
1da177e4 1049 struct inode *inode = dentry->d_inode;
28a6d671
EB
1050 struct task_struct *task;
1051 generic_fillattr(inode, stat);
1da177e4 1052
28a6d671
EB
1053 rcu_read_lock();
1054 stat->uid = 0;
1055 stat->gid = 0;
1056 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1057 if (task) {
1058 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1059 task_dumpable(task)) {
1060 stat->uid = task->euid;
1061 stat->gid = task->egid;
1da177e4
LT
1062 }
1063 }
28a6d671 1064 rcu_read_unlock();
d6e71144 1065 return 0;
1da177e4
LT
1066}
1067
1da177e4
LT
1068/* dentry stuff */
1069
1070/*
1071 * Exceptional case: normally we are not allowed to unhash a busy
1072 * directory. In this case, however, we can do it - no aliasing problems
1073 * due to the way we treat inodes.
1074 *
1075 * Rewrite the inode's ownerships here because the owning task may have
1076 * performed a setuid(), etc.
99f89551
EB
1077 *
1078 * Before the /proc/pid/status file was created the only way to read
1079 * the effective uid of a /process was to stat /proc/pid. Reading
1080 * /proc/pid/status is slow enough that procps and other packages
1081 * kept stating /proc/pid. To keep the rules in /proc simple I have
1082 * made this apply to all per process world readable and executable
1083 * directories.
1da177e4
LT
1084 */
1085static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1086{
1087 struct inode *inode = dentry->d_inode;
99f89551
EB
1088 struct task_struct *task = get_proc_task(inode);
1089 if (task) {
1090 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1091 task_dumpable(task)) {
1da177e4
LT
1092 inode->i_uid = task->euid;
1093 inode->i_gid = task->egid;
1094 } else {
1095 inode->i_uid = 0;
1096 inode->i_gid = 0;
1097 }
9ee8ab9f 1098 inode->i_mode &= ~(S_ISUID | S_ISGID);
1da177e4 1099 security_task_to_inode(task, inode);
99f89551 1100 put_task_struct(task);
1da177e4
LT
1101 return 1;
1102 }
1103 d_drop(dentry);
1104 return 0;
1105}
1106
28a6d671 1107static int pid_delete_dentry(struct dentry * dentry)
99f89551 1108{
28a6d671
EB
1109 /* Is the task we represent dead?
1110 * If so, then don't put the dentry on the lru list,
1111 * kill it immediately.
1112 */
1113 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1114}
1115
1116static struct dentry_operations pid_dentry_operations =
1117{
1118 .d_revalidate = pid_revalidate,
1119 .d_delete = pid_delete_dentry,
1120};
1121
1122/* Lookups */
1123
61a28784
EB
1124typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1125
1c0d04c9
EB
1126/*
1127 * Fill a directory entry.
1128 *
1129 * If possible create the dcache entry and derive our inode number and
1130 * file type from dcache entry.
1131 *
1132 * Since all of the proc inode numbers are dynamically generated, the inode
1133 * numbers do not exist until the inode is cache. This means creating the
1134 * the dcache entry in readdir is necessary to keep the inode numbers
1135 * reported by readdir in sync with the inode numbers reported
1136 * by stat.
1137 */
61a28784
EB
1138static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1139 char *name, int len,
1140 instantiate_t instantiate, struct task_struct *task, void *ptr)
1141{
2fddfeef 1142 struct dentry *child, *dir = filp->f_path.dentry;
61a28784
EB
1143 struct inode *inode;
1144 struct qstr qname;
1145 ino_t ino = 0;
1146 unsigned type = DT_UNKNOWN;
1147
1148 qname.name = name;
1149 qname.len = len;
1150 qname.hash = full_name_hash(name, len);
1151
1152 child = d_lookup(dir, &qname);
1153 if (!child) {
1154 struct dentry *new;
1155 new = d_alloc(dir, &qname);
1156 if (new) {
1157 child = instantiate(dir->d_inode, new, task, ptr);
1158 if (child)
1159 dput(new);
1160 else
1161 child = new;
1162 }
1163 }
1164 if (!child || IS_ERR(child) || !child->d_inode)
1165 goto end_instantiate;
1166 inode = child->d_inode;
1167 if (inode) {
1168 ino = inode->i_ino;
1169 type = inode->i_mode >> 12;
1170 }
1171 dput(child);
1172end_instantiate:
1173 if (!ino)
1174 ino = find_inode_number(dir, &qname);
1175 if (!ino)
1176 ino = 1;
1177 return filldir(dirent, name, len, filp->f_pos, ino, type);
1178}
1179
28a6d671
EB
1180static unsigned name_to_int(struct dentry *dentry)
1181{
1182 const char *name = dentry->d_name.name;
1183 int len = dentry->d_name.len;
1184 unsigned n = 0;
1185
1186 if (len > 1 && *name == '0')
1187 goto out;
1188 while (len-- > 0) {
1189 unsigned c = *name++ - '0';
1190 if (c > 9)
1191 goto out;
1192 if (n >= (~0U-9)/10)
1193 goto out;
1194 n *= 10;
1195 n += c;
1196 }
1197 return n;
1198out:
1199 return ~0U;
1200}
1201
1202static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1203{
1204 struct task_struct *task = get_proc_task(inode);
1205 struct files_struct *files = NULL;
1206 struct file *file;
1207 int fd = proc_fd(inode);
99f89551 1208
99f89551 1209 if (task) {
28a6d671
EB
1210 files = get_files_struct(task);
1211 put_task_struct(task);
1212 }
1213 if (files) {
1214 /*
1215 * We are not taking a ref to the file structure, so we must
1216 * hold ->file_lock.
1217 */
1218 spin_lock(&files->file_lock);
1219 file = fcheck_files(files, fd);
1220 if (file) {
2fddfeef
JJS
1221 *mnt = mntget(file->f_path.mnt);
1222 *dentry = dget(file->f_path.dentry);
28a6d671
EB
1223 spin_unlock(&files->file_lock);
1224 put_files_struct(files);
1225 return 0;
99f89551 1226 }
28a6d671
EB
1227 spin_unlock(&files->file_lock);
1228 put_files_struct(files);
99f89551 1229 }
28a6d671 1230 return -ENOENT;
99f89551
EB
1231}
1232
1da177e4
LT
1233static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1234{
1235 struct inode *inode = dentry->d_inode;
99f89551 1236 struct task_struct *task = get_proc_task(inode);
aed7a6c4 1237 int fd = proc_fd(inode);
1da177e4
LT
1238 struct files_struct *files;
1239
99f89551
EB
1240 if (task) {
1241 files = get_files_struct(task);
1242 if (files) {
1243 rcu_read_lock();
1244 if (fcheck_files(files, fd)) {
1245 rcu_read_unlock();
1246 put_files_struct(files);
1247 if (task_dumpable(task)) {
1248 inode->i_uid = task->euid;
1249 inode->i_gid = task->egid;
1250 } else {
1251 inode->i_uid = 0;
1252 inode->i_gid = 0;
1253 }
9ee8ab9f 1254 inode->i_mode &= ~(S_ISUID | S_ISGID);
99f89551
EB
1255 security_task_to_inode(task, inode);
1256 put_task_struct(task);
1257 return 1;
1258 }
b835996f 1259 rcu_read_unlock();
1da177e4 1260 put_files_struct(files);
1da177e4 1261 }
99f89551 1262 put_task_struct(task);
1da177e4
LT
1263 }
1264 d_drop(dentry);
1265 return 0;
1266}
1267
1da177e4
LT
1268static struct dentry_operations tid_fd_dentry_operations =
1269{
1270 .d_revalidate = tid_fd_revalidate,
1271 .d_delete = pid_delete_dentry,
1272};
1273
444ceed8
EB
1274static struct dentry *proc_fd_instantiate(struct inode *dir,
1275 struct dentry *dentry, struct task_struct *task, void *ptr)
1da177e4 1276{
444ceed8
EB
1277 unsigned fd = *(unsigned *)ptr;
1278 struct file *file;
1279 struct files_struct *files;
1280 struct inode *inode;
1281 struct proc_inode *ei;
1282 struct dentry *error = ERR_PTR(-ENOENT);
1da177e4 1283
61a28784 1284 inode = proc_pid_make_inode(dir->i_sb, task);
1da177e4
LT
1285 if (!inode)
1286 goto out;
1287 ei = PROC_I(inode);
aed7a6c4 1288 ei->fd = fd;
1da177e4
LT
1289 files = get_files_struct(task);
1290 if (!files)
444ceed8 1291 goto out_iput;
1da177e4 1292 inode->i_mode = S_IFLNK;
ca99c1da
DS
1293
1294 /*
1295 * We are not taking a ref to the file structure, so we must
1296 * hold ->file_lock.
1297 */
1298 spin_lock(&files->file_lock);
1da177e4
LT
1299 file = fcheck_files(files, fd);
1300 if (!file)
444ceed8 1301 goto out_unlock;
1da177e4
LT
1302 if (file->f_mode & 1)
1303 inode->i_mode |= S_IRUSR | S_IXUSR;
1304 if (file->f_mode & 2)
1305 inode->i_mode |= S_IWUSR | S_IXUSR;
ca99c1da 1306 spin_unlock(&files->file_lock);
1da177e4 1307 put_files_struct(files);
444ceed8 1308
1da177e4
LT
1309 inode->i_op = &proc_pid_link_inode_operations;
1310 inode->i_size = 64;
1311 ei->op.proc_get_link = proc_fd_link;
1312 dentry->d_op = &tid_fd_dentry_operations;
1313 d_add(dentry, inode);
cd6a3ce9
EB
1314 /* Close the race of the process dying before we return the dentry */
1315 if (tid_fd_revalidate(dentry, NULL))
444ceed8 1316 error = NULL;
1da177e4 1317
444ceed8
EB
1318 out:
1319 return error;
1320out_unlock:
ca99c1da 1321 spin_unlock(&files->file_lock);
1da177e4 1322 put_files_struct(files);
444ceed8 1323out_iput:
1da177e4 1324 iput(inode);
cd6a3ce9 1325 goto out;
1da177e4
LT
1326}
1327
444ceed8
EB
1328static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1329{
1330 struct task_struct *task = get_proc_task(dir);
1331 unsigned fd = name_to_int(dentry);
1332 struct dentry *result = ERR_PTR(-ENOENT);
1333
1334 if (!task)
1335 goto out_no_task;
1336 if (fd == ~0U)
1337 goto out;
1338
1339 result = proc_fd_instantiate(dir, dentry, task, &fd);
1340out:
1341 put_task_struct(task);
1342out_no_task:
1343 return result;
1344}
1345
61a28784
EB
1346static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1347 struct task_struct *task, int fd)
1348{
1349 char name[PROC_NUMBUF];
1350 int len = snprintf(name, sizeof(name), "%d", fd);
1351 return proc_fill_cache(filp, dirent, filldir, name, len,
1352 proc_fd_instantiate, task, &fd);
1353}
1354
28a6d671
EB
1355static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1356{
2fddfeef 1357 struct dentry *dentry = filp->f_path.dentry;
28a6d671
EB
1358 struct inode *inode = dentry->d_inode;
1359 struct task_struct *p = get_proc_task(inode);
1360 unsigned int fd, tid, ino;
1361 int retval;
28a6d671
EB
1362 struct files_struct * files;
1363 struct fdtable *fdt;
1da177e4 1364
28a6d671
EB
1365 retval = -ENOENT;
1366 if (!p)
1367 goto out_no_task;
1368 retval = 0;
1369 tid = p->pid;
1370
1371 fd = filp->f_pos;
1372 switch (fd) {
1373 case 0:
1374 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1375 goto out;
1376 filp->f_pos++;
1377 case 1:
1378 ino = parent_ino(dentry);
1379 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1380 goto out;
1381 filp->f_pos++;
1382 default:
1383 files = get_files_struct(p);
1384 if (!files)
1385 goto out;
1386 rcu_read_lock();
1387 fdt = files_fdtable(files);
1388 for (fd = filp->f_pos-2;
1389 fd < fdt->max_fds;
1390 fd++, filp->f_pos++) {
28a6d671
EB
1391
1392 if (!fcheck_files(files, fd))
1393 continue;
1394 rcu_read_unlock();
1395
61a28784 1396 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
28a6d671
EB
1397 rcu_read_lock();
1398 break;
1399 }
1400 rcu_read_lock();
1401 }
1402 rcu_read_unlock();
1403 put_files_struct(files);
1404 }
1405out:
1406 put_task_struct(p);
1407out_no_task:
1408 return retval;
1409}
1410
00977a59 1411static const struct file_operations proc_fd_operations = {
28a6d671
EB
1412 .read = generic_read_dir,
1413 .readdir = proc_readfd,
1da177e4
LT
1414};
1415
1416/*
1417 * proc directories can do almost nothing..
1418 */
c5ef1c42 1419static const struct inode_operations proc_fd_inode_operations = {
1da177e4 1420 .lookup = proc_lookupfd,
6d76fa58 1421 .setattr = proc_setattr,
1da177e4
LT
1422};
1423
444ceed8
EB
1424static struct dentry *proc_pident_instantiate(struct inode *dir,
1425 struct dentry *dentry, struct task_struct *task, void *ptr)
1426{
1427 struct pid_entry *p = ptr;
1428 struct inode *inode;
1429 struct proc_inode *ei;
1430 struct dentry *error = ERR_PTR(-EINVAL);
1431
61a28784 1432 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
1433 if (!inode)
1434 goto out;
1435
1436 ei = PROC_I(inode);
1437 inode->i_mode = p->mode;
1438 if (S_ISDIR(inode->i_mode))
1439 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1440 if (p->iop)
1441 inode->i_op = p->iop;
1442 if (p->fop)
1443 inode->i_fop = p->fop;
1444 ei->op = p->op;
1445 dentry->d_op = &pid_dentry_operations;
1446 d_add(dentry, inode);
1447 /* Close the race of the process dying before we return the dentry */
1448 if (pid_revalidate(dentry, NULL))
1449 error = NULL;
1450out:
1451 return error;
1452}
1453
1da177e4
LT
1454static struct dentry *proc_pident_lookup(struct inode *dir,
1455 struct dentry *dentry,
7bcd6b0e
EB
1456 struct pid_entry *ents,
1457 unsigned int nents)
1da177e4
LT
1458{
1459 struct inode *inode;
cd6a3ce9 1460 struct dentry *error;
99f89551 1461 struct task_struct *task = get_proc_task(dir);
7bcd6b0e 1462 struct pid_entry *p, *last;
1da177e4 1463
cd6a3ce9 1464 error = ERR_PTR(-ENOENT);
1da177e4
LT
1465 inode = NULL;
1466
99f89551
EB
1467 if (!task)
1468 goto out_no_task;
1da177e4 1469
20cdc894
EB
1470 /*
1471 * Yes, it does not scale. And it should not. Don't add
1472 * new entries into /proc/<tgid>/ without very good reasons.
1473 */
7bcd6b0e
EB
1474 last = &ents[nents - 1];
1475 for (p = ents; p <= last; p++) {
1da177e4
LT
1476 if (p->len != dentry->d_name.len)
1477 continue;
1478 if (!memcmp(dentry->d_name.name, p->name, p->len))
1479 break;
1480 }
7bcd6b0e 1481 if (p > last)
1da177e4
LT
1482 goto out;
1483
444ceed8 1484 error = proc_pident_instantiate(dir, dentry, task, p);
1da177e4 1485out:
99f89551
EB
1486 put_task_struct(task);
1487out_no_task:
cd6a3ce9 1488 return error;
1da177e4
LT
1489}
1490
61a28784
EB
1491static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1492 struct task_struct *task, struct pid_entry *p)
1493{
1494 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1495 proc_pident_instantiate, task, p);
1496}
1497
28a6d671
EB
1498static int proc_pident_readdir(struct file *filp,
1499 void *dirent, filldir_t filldir,
1500 struct pid_entry *ents, unsigned int nents)
1501{
1502 int i;
1503 int pid;
2fddfeef 1504 struct dentry *dentry = filp->f_path.dentry;
28a6d671
EB
1505 struct inode *inode = dentry->d_inode;
1506 struct task_struct *task = get_proc_task(inode);
7bcd6b0e 1507 struct pid_entry *p, *last;
28a6d671
EB
1508 ino_t ino;
1509 int ret;
1510
1511 ret = -ENOENT;
1512 if (!task)
61a28784 1513 goto out_no_task;
28a6d671
EB
1514
1515 ret = 0;
1516 pid = task->pid;
28a6d671
EB
1517 i = filp->f_pos;
1518 switch (i) {
1519 case 0:
1520 ino = inode->i_ino;
1521 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1522 goto out;
1523 i++;
1524 filp->f_pos++;
1525 /* fall through */
1526 case 1:
1527 ino = parent_ino(dentry);
1528 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1529 goto out;
1530 i++;
1531 filp->f_pos++;
1532 /* fall through */
1533 default:
1534 i -= 2;
1535 if (i >= nents) {
1536 ret = 1;
1537 goto out;
1538 }
1539 p = ents + i;
7bcd6b0e
EB
1540 last = &ents[nents - 1];
1541 while (p <= last) {
61a28784 1542 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
28a6d671
EB
1543 goto out;
1544 filp->f_pos++;
1545 p++;
1546 }
1547 }
1548
1549 ret = 1;
1550out:
61a28784
EB
1551 put_task_struct(task);
1552out_no_task:
28a6d671 1553 return ret;
1da177e4
LT
1554}
1555
28a6d671
EB
1556#ifdef CONFIG_SECURITY
1557static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1558 size_t count, loff_t *ppos)
1559{
2fddfeef 1560 struct inode * inode = file->f_path.dentry->d_inode;
28a6d671
EB
1561 unsigned long page;
1562 ssize_t length;
1563 struct task_struct *task = get_proc_task(inode);
1564
1565 length = -ESRCH;
1566 if (!task)
1567 goto out_no_task;
1568
1569 if (count > PAGE_SIZE)
1570 count = PAGE_SIZE;
1571 length = -ENOMEM;
1572 if (!(page = __get_free_page(GFP_KERNEL)))
1573 goto out;
1574
1575 length = security_getprocattr(task,
2fddfeef 1576 (char*)file->f_path.dentry->d_name.name,
28a6d671
EB
1577 (void*)page, count);
1578 if (length >= 0)
1579 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1580 free_page(page);
1581out:
1582 put_task_struct(task);
1583out_no_task:
1584 return length;
1da177e4
LT
1585}
1586
28a6d671
EB
1587static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1588 size_t count, loff_t *ppos)
1589{
2fddfeef 1590 struct inode * inode = file->f_path.dentry->d_inode;
28a6d671
EB
1591 char *page;
1592 ssize_t length;
1593 struct task_struct *task = get_proc_task(inode);
1594
1595 length = -ESRCH;
1596 if (!task)
1597 goto out_no_task;
1598 if (count > PAGE_SIZE)
1599 count = PAGE_SIZE;
1600
1601 /* No partial writes. */
1602 length = -EINVAL;
1603 if (*ppos != 0)
1604 goto out;
1605
1606 length = -ENOMEM;
1607 page = (char*)__get_free_page(GFP_USER);
1608 if (!page)
1609 goto out;
1610
1611 length = -EFAULT;
1612 if (copy_from_user(page, buf, count))
1613 goto out_free;
1614
1615 length = security_setprocattr(task,
2fddfeef 1616 (char*)file->f_path.dentry->d_name.name,
28a6d671
EB
1617 (void*)page, count);
1618out_free:
1619 free_page((unsigned long) page);
1620out:
1621 put_task_struct(task);
1622out_no_task:
1623 return length;
1624}
1625
00977a59 1626static const struct file_operations proc_pid_attr_operations = {
28a6d671
EB
1627 .read = proc_pid_attr_read,
1628 .write = proc_pid_attr_write,
1629};
1630
72d9dcfc 1631static struct pid_entry attr_dir_stuff[] = {
61a28784
EB
1632 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1633 REG("prev", S_IRUGO, pid_attr),
1634 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1635 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1636 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1637 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
28a6d671
EB
1638};
1639
72d9dcfc 1640static int proc_attr_dir_readdir(struct file * filp,
28a6d671
EB
1641 void * dirent, filldir_t filldir)
1642{
1643 return proc_pident_readdir(filp,dirent,filldir,
72d9dcfc 1644 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
1645}
1646
00977a59 1647static const struct file_operations proc_attr_dir_operations = {
1da177e4 1648 .read = generic_read_dir,
72d9dcfc 1649 .readdir = proc_attr_dir_readdir,
1da177e4
LT
1650};
1651
72d9dcfc 1652static struct dentry *proc_attr_dir_lookup(struct inode *dir,
28a6d671
EB
1653 struct dentry *dentry, struct nameidata *nd)
1654{
7bcd6b0e
EB
1655 return proc_pident_lookup(dir, dentry,
1656 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
28a6d671
EB
1657}
1658
c5ef1c42 1659static const struct inode_operations proc_attr_dir_inode_operations = {
72d9dcfc 1660 .lookup = proc_attr_dir_lookup,
99f89551 1661 .getattr = pid_getattr,
6d76fa58 1662 .setattr = proc_setattr,
1da177e4
LT
1663};
1664
28a6d671
EB
1665#endif
1666
1667/*
1668 * /proc/self:
1669 */
1670static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1671 int buflen)
1672{
1673 char tmp[PROC_NUMBUF];
1674 sprintf(tmp, "%d", current->tgid);
1675 return vfs_readlink(dentry,buffer,buflen,tmp);
1676}
1677
1678static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1679{
1680 char tmp[PROC_NUMBUF];
1681 sprintf(tmp, "%d", current->tgid);
1682 return ERR_PTR(vfs_follow_link(nd,tmp));
1683}
1684
c5ef1c42 1685static const struct inode_operations proc_self_inode_operations = {
28a6d671
EB
1686 .readlink = proc_self_readlink,
1687 .follow_link = proc_self_follow_link,
1688};
1689
801199ce
EB
1690/*
1691 * proc base
1692 *
1693 * These are the directory entries in the root directory of /proc
1694 * that properly belong to the /proc filesystem, as they describe
1695 * describe something that is process related.
1696 */
1697static struct pid_entry proc_base_stuff[] = {
61a28784 1698 NOD("self", S_IFLNK|S_IRWXUGO,
801199ce 1699 &proc_self_inode_operations, NULL, {}),
801199ce
EB
1700};
1701
1702/*
1703 * Exceptional case: normally we are not allowed to unhash a busy
1704 * directory. In this case, however, we can do it - no aliasing problems
1705 * due to the way we treat inodes.
1706 */
1707static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1708{
1709 struct inode *inode = dentry->d_inode;
1710 struct task_struct *task = get_proc_task(inode);
1711 if (task) {
1712 put_task_struct(task);
1713 return 1;
1714 }
1715 d_drop(dentry);
1716 return 0;
1717}
1718
1719static struct dentry_operations proc_base_dentry_operations =
1720{
1721 .d_revalidate = proc_base_revalidate,
1722 .d_delete = pid_delete_dentry,
1723};
1724
444ceed8
EB
1725static struct dentry *proc_base_instantiate(struct inode *dir,
1726 struct dentry *dentry, struct task_struct *task, void *ptr)
801199ce 1727{
444ceed8 1728 struct pid_entry *p = ptr;
801199ce 1729 struct inode *inode;
801199ce 1730 struct proc_inode *ei;
444ceed8 1731 struct dentry *error = ERR_PTR(-EINVAL);
801199ce
EB
1732
1733 /* Allocate the inode */
1734 error = ERR_PTR(-ENOMEM);
1735 inode = new_inode(dir->i_sb);
1736 if (!inode)
1737 goto out;
1738
1739 /* Initialize the inode */
1740 ei = PROC_I(inode);
1741 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
801199ce
EB
1742
1743 /*
1744 * grab the reference to the task.
1745 */
1a657f78 1746 ei->pid = get_task_pid(task, PIDTYPE_PID);
801199ce
EB
1747 if (!ei->pid)
1748 goto out_iput;
1749
1750 inode->i_uid = 0;
1751 inode->i_gid = 0;
1752 inode->i_mode = p->mode;
1753 if (S_ISDIR(inode->i_mode))
1754 inode->i_nlink = 2;
1755 if (S_ISLNK(inode->i_mode))
1756 inode->i_size = 64;
1757 if (p->iop)
1758 inode->i_op = p->iop;
1759 if (p->fop)
1760 inode->i_fop = p->fop;
1761 ei->op = p->op;
1762 dentry->d_op = &proc_base_dentry_operations;
1763 d_add(dentry, inode);
1764 error = NULL;
1765out:
801199ce
EB
1766 return error;
1767out_iput:
1768 iput(inode);
1769 goto out;
1770}
1771
444ceed8
EB
1772static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1773{
1774 struct dentry *error;
1775 struct task_struct *task = get_proc_task(dir);
7bcd6b0e 1776 struct pid_entry *p, *last;
444ceed8
EB
1777
1778 error = ERR_PTR(-ENOENT);
1779
1780 if (!task)
1781 goto out_no_task;
1782
1783 /* Lookup the directory entry */
7bcd6b0e
EB
1784 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1785 for (p = proc_base_stuff; p <= last; p++) {
444ceed8
EB
1786 if (p->len != dentry->d_name.len)
1787 continue;
1788 if (!memcmp(dentry->d_name.name, p->name, p->len))
1789 break;
1790 }
7bcd6b0e 1791 if (p > last)
444ceed8
EB
1792 goto out;
1793
1794 error = proc_base_instantiate(dir, dentry, task, p);
1795
1796out:
1797 put_task_struct(task);
1798out_no_task:
1799 return error;
1800}
1801
61a28784
EB
1802static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1803 struct task_struct *task, struct pid_entry *p)
1804{
1805 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1806 proc_base_instantiate, task, p);
1807}
1808
aba76fdb
AM
1809#ifdef CONFIG_TASK_IO_ACCOUNTING
1810static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1811{
1812 return sprintf(buffer,
4b98d11b 1813#ifdef CONFIG_TASK_XACCT
aba76fdb
AM
1814 "rchar: %llu\n"
1815 "wchar: %llu\n"
1816 "syscr: %llu\n"
1817 "syscw: %llu\n"
4b98d11b 1818#endif
aba76fdb
AM
1819 "read_bytes: %llu\n"
1820 "write_bytes: %llu\n"
1821 "cancelled_write_bytes: %llu\n",
4b98d11b 1822#ifdef CONFIG_TASK_XACCT
aba76fdb
AM
1823 (unsigned long long)task->rchar,
1824 (unsigned long long)task->wchar,
1825 (unsigned long long)task->syscr,
1826 (unsigned long long)task->syscw,
4b98d11b 1827#endif
aba76fdb
AM
1828 (unsigned long long)task->ioac.read_bytes,
1829 (unsigned long long)task->ioac.write_bytes,
1830 (unsigned long long)task->ioac.cancelled_write_bytes);
1831}
1832#endif
1833
28a6d671
EB
1834/*
1835 * Thread groups
1836 */
00977a59 1837static const struct file_operations proc_task_operations;
c5ef1c42 1838static const struct inode_operations proc_task_inode_operations;
20cdc894 1839
28a6d671 1840static struct pid_entry tgid_base_stuff[] = {
61a28784
EB
1841 DIR("task", S_IRUGO|S_IXUGO, task),
1842 DIR("fd", S_IRUSR|S_IXUSR, fd),
1843 INF("environ", S_IRUSR, pid_environ),
1844 INF("auxv", S_IRUSR, pid_auxv),
1845 INF("status", S_IRUGO, pid_status),
1846 INF("cmdline", S_IRUGO, pid_cmdline),
1847 INF("stat", S_IRUGO, tgid_stat),
1848 INF("statm", S_IRUGO, pid_statm),
1849 REG("maps", S_IRUGO, maps),
28a6d671 1850#ifdef CONFIG_NUMA
61a28784 1851 REG("numa_maps", S_IRUGO, numa_maps),
28a6d671 1852#endif
61a28784 1853 REG("mem", S_IRUSR|S_IWUSR, mem),
28a6d671 1854#ifdef CONFIG_SECCOMP
61a28784 1855 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
28a6d671 1856#endif
61a28784
EB
1857 LNK("cwd", cwd),
1858 LNK("root", root),
1859 LNK("exe", exe),
1860 REG("mounts", S_IRUGO, mounts),
1861 REG("mountstats", S_IRUSR, mountstats),
28a6d671 1862#ifdef CONFIG_MMU
61a28784 1863 REG("smaps", S_IRUGO, smaps),
28a6d671
EB
1864#endif
1865#ifdef CONFIG_SECURITY
72d9dcfc 1866 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
28a6d671
EB
1867#endif
1868#ifdef CONFIG_KALLSYMS
61a28784 1869 INF("wchan", S_IRUGO, pid_wchan),
28a6d671
EB
1870#endif
1871#ifdef CONFIG_SCHEDSTATS
61a28784 1872 INF("schedstat", S_IRUGO, pid_schedstat),
28a6d671
EB
1873#endif
1874#ifdef CONFIG_CPUSETS
61a28784 1875 REG("cpuset", S_IRUGO, cpuset),
28a6d671 1876#endif
61a28784
EB
1877 INF("oom_score", S_IRUGO, oom_score),
1878 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
28a6d671 1879#ifdef CONFIG_AUDITSYSCALL
61a28784 1880 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
28a6d671 1881#endif
f4f154fd
AM
1882#ifdef CONFIG_FAULT_INJECTION
1883 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1884#endif
aba76fdb
AM
1885#ifdef CONFIG_TASK_IO_ACCOUNTING
1886 INF("io", S_IRUGO, pid_io_accounting),
1887#endif
28a6d671 1888};
1da177e4 1889
28a6d671 1890static int proc_tgid_base_readdir(struct file * filp,
1da177e4
LT
1891 void * dirent, filldir_t filldir)
1892{
1893 return proc_pident_readdir(filp,dirent,filldir,
28a6d671 1894 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
1895}
1896
00977a59 1897static const struct file_operations proc_tgid_base_operations = {
1da177e4 1898 .read = generic_read_dir,
28a6d671 1899 .readdir = proc_tgid_base_readdir,
1da177e4
LT
1900};
1901
28a6d671 1902static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
7bcd6b0e
EB
1903 return proc_pident_lookup(dir, dentry,
1904 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1da177e4
LT
1905}
1906
c5ef1c42 1907static const struct inode_operations proc_tgid_base_inode_operations = {
28a6d671 1908 .lookup = proc_tgid_base_lookup,
99f89551 1909 .getattr = pid_getattr,
6d76fa58 1910 .setattr = proc_setattr,
1da177e4 1911};
1da177e4
LT
1912
1913/**
48e6484d
EB
1914 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1915 *
1916 * @task: task that should be flushed.
1917 *
1918 * Looks in the dcache for
1919 * /proc/@pid
1920 * /proc/@tgid/task/@pid
1921 * if either directory is present flushes it and all of it'ts children
1922 * from the dcache.
1da177e4 1923 *
48e6484d
EB
1924 * It is safe and reasonable to cache /proc entries for a task until
1925 * that task exits. After that they just clog up the dcache with
1926 * useless entries, possibly causing useful dcache entries to be
1927 * flushed instead. This routine is proved to flush those useless
1928 * dcache entries at process exit time.
1da177e4 1929 *
48e6484d
EB
1930 * NOTE: This routine is just an optimization so it does not guarantee
1931 * that no dcache entries will exist at process exit time it
1932 * just makes it very unlikely that any will persist.
1da177e4 1933 */
48e6484d 1934void proc_flush_task(struct task_struct *task)
1da177e4 1935{
48e6484d 1936 struct dentry *dentry, *leader, *dir;
8578cea7 1937 char buf[PROC_NUMBUF];
48e6484d
EB
1938 struct qstr name;
1939
1940 name.name = buf;
1941 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1942 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1943 if (dentry) {
1944 shrink_dcache_parent(dentry);
1945 d_drop(dentry);
1946 dput(dentry);
1947 }
1da177e4 1948
48e6484d
EB
1949 if (thread_group_leader(task))
1950 goto out;
1da177e4 1951
48e6484d
EB
1952 name.name = buf;
1953 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1954 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1955 if (!leader)
1956 goto out;
1da177e4 1957
48e6484d
EB
1958 name.name = "task";
1959 name.len = strlen(name.name);
1960 dir = d_hash_and_lookup(leader, &name);
1961 if (!dir)
1962 goto out_put_leader;
1963
1964 name.name = buf;
1965 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1966 dentry = d_hash_and_lookup(dir, &name);
1967 if (dentry) {
1968 shrink_dcache_parent(dentry);
1969 d_drop(dentry);
1970 dput(dentry);
1da177e4 1971 }
48e6484d
EB
1972
1973 dput(dir);
1974out_put_leader:
1975 dput(leader);
1976out:
1977 return;
1da177e4
LT
1978}
1979
9711ef99
AB
1980static struct dentry *proc_pid_instantiate(struct inode *dir,
1981 struct dentry * dentry,
1982 struct task_struct *task, void *ptr)
444ceed8
EB
1983{
1984 struct dentry *error = ERR_PTR(-ENOENT);
1985 struct inode *inode;
1986
61a28784 1987 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
1988 if (!inode)
1989 goto out;
1990
1991 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1992 inode->i_op = &proc_tgid_base_inode_operations;
1993 inode->i_fop = &proc_tgid_base_operations;
1994 inode->i_flags|=S_IMMUTABLE;
1995 inode->i_nlink = 4;
1996#ifdef CONFIG_SECURITY
1997 inode->i_nlink += 1;
1998#endif
1999
2000 dentry->d_op = &pid_dentry_operations;
2001
2002 d_add(dentry, inode);
2003 /* Close the race of the process dying before we return the dentry */
2004 if (pid_revalidate(dentry, NULL))
2005 error = NULL;
2006out:
2007 return error;
2008}
2009
1da177e4
LT
2010struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2011{
cd6a3ce9 2012 struct dentry *result = ERR_PTR(-ENOENT);
1da177e4 2013 struct task_struct *task;
1da177e4 2014 unsigned tgid;
1da177e4 2015
801199ce
EB
2016 result = proc_base_lookup(dir, dentry);
2017 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2018 goto out;
2019
1da177e4
LT
2020 tgid = name_to_int(dentry);
2021 if (tgid == ~0U)
2022 goto out;
2023
de758734 2024 rcu_read_lock();
1da177e4
LT
2025 task = find_task_by_pid(tgid);
2026 if (task)
2027 get_task_struct(task);
de758734 2028 rcu_read_unlock();
1da177e4
LT
2029 if (!task)
2030 goto out;
2031
444ceed8 2032 result = proc_pid_instantiate(dir, dentry, task, NULL);
1da177e4 2033 put_task_struct(task);
1da177e4 2034out:
cd6a3ce9 2035 return result;
1da177e4
LT
2036}
2037
1da177e4 2038/*
0804ef4b 2039 * Find the first task with tgid >= tgid
0bc58a91 2040 *
1da177e4 2041 */
0804ef4b 2042static struct task_struct *next_tgid(unsigned int tgid)
1da177e4 2043{
0804ef4b
EB
2044 struct task_struct *task;
2045 struct pid *pid;
1da177e4 2046
454cc105 2047 rcu_read_lock();
0804ef4b
EB
2048retry:
2049 task = NULL;
2050 pid = find_ge_pid(tgid);
2051 if (pid) {
2052 tgid = pid->nr + 1;
2053 task = pid_task(pid, PIDTYPE_PID);
2054 /* What we to know is if the pid we have find is the
2055 * pid of a thread_group_leader. Testing for task
2056 * being a thread_group_leader is the obvious thing
2057 * todo but there is a window when it fails, due to
2058 * the pid transfer logic in de_thread.
2059 *
2060 * So we perform the straight forward test of seeing
2061 * if the pid we have found is the pid of a thread
2062 * group leader, and don't worry if the task we have
2063 * found doesn't happen to be a thread group leader.
2064 * As we don't care in the case of readdir.
2065 */
2066 if (!task || !has_group_leader_pid(task))
2067 goto retry;
2068 get_task_struct(task);
0bc58a91 2069 }
454cc105 2070 rcu_read_unlock();
0804ef4b 2071 return task;
1da177e4
LT
2072}
2073
7bcd6b0e 2074#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
0804ef4b 2075
61a28784
EB
2076static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2077 struct task_struct *task, int tgid)
2078{
2079 char name[PROC_NUMBUF];
2080 int len = snprintf(name, sizeof(name), "%d", tgid);
2081 return proc_fill_cache(filp, dirent, filldir, name, len,
2082 proc_pid_instantiate, task, NULL);
2083}
2084
1da177e4
LT
2085/* for the /proc/ directory itself, after non-process stuff has been done */
2086int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2087{
1da177e4 2088 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2fddfeef 2089 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
0bc58a91
EB
2090 struct task_struct *task;
2091 int tgid;
1da177e4 2092
61a28784
EB
2093 if (!reaper)
2094 goto out_no_task;
2095
7bcd6b0e 2096 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
801199ce 2097 struct pid_entry *p = &proc_base_stuff[nr];
61a28784 2098 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
801199ce 2099 goto out;
1da177e4
LT
2100 }
2101
0804ef4b
EB
2102 tgid = filp->f_pos - TGID_OFFSET;
2103 for (task = next_tgid(tgid);
0bc58a91 2104 task;
0804ef4b 2105 put_task_struct(task), task = next_tgid(tgid + 1)) {
0bc58a91 2106 tgid = task->pid;
0804ef4b 2107 filp->f_pos = tgid + TGID_OFFSET;
61a28784 2108 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
0bc58a91 2109 put_task_struct(task);
0804ef4b 2110 goto out;
1da177e4 2111 }
0bc58a91 2112 }
0804ef4b
EB
2113 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2114out:
61a28784
EB
2115 put_task_struct(reaper);
2116out_no_task:
0bc58a91
EB
2117 return 0;
2118}
1da177e4 2119
28a6d671
EB
2120/*
2121 * Tasks
2122 */
2123static struct pid_entry tid_base_stuff[] = {
61a28784
EB
2124 DIR("fd", S_IRUSR|S_IXUSR, fd),
2125 INF("environ", S_IRUSR, pid_environ),
2126 INF("auxv", S_IRUSR, pid_auxv),
2127 INF("status", S_IRUGO, pid_status),
2128 INF("cmdline", S_IRUGO, pid_cmdline),
2129 INF("stat", S_IRUGO, tid_stat),
2130 INF("statm", S_IRUGO, pid_statm),
2131 REG("maps", S_IRUGO, maps),
28a6d671 2132#ifdef CONFIG_NUMA
61a28784 2133 REG("numa_maps", S_IRUGO, numa_maps),
28a6d671 2134#endif
61a28784 2135 REG("mem", S_IRUSR|S_IWUSR, mem),
28a6d671 2136#ifdef CONFIG_SECCOMP
61a28784 2137 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
28a6d671 2138#endif
61a28784
EB
2139 LNK("cwd", cwd),
2140 LNK("root", root),
2141 LNK("exe", exe),
2142 REG("mounts", S_IRUGO, mounts),
28a6d671 2143#ifdef CONFIG_MMU
61a28784 2144 REG("smaps", S_IRUGO, smaps),
28a6d671
EB
2145#endif
2146#ifdef CONFIG_SECURITY
72d9dcfc 2147 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
28a6d671
EB
2148#endif
2149#ifdef CONFIG_KALLSYMS
61a28784 2150 INF("wchan", S_IRUGO, pid_wchan),
28a6d671
EB
2151#endif
2152#ifdef CONFIG_SCHEDSTATS
61a28784 2153 INF("schedstat", S_IRUGO, pid_schedstat),
28a6d671
EB
2154#endif
2155#ifdef CONFIG_CPUSETS
61a28784 2156 REG("cpuset", S_IRUGO, cpuset),
28a6d671 2157#endif
61a28784
EB
2158 INF("oom_score", S_IRUGO, oom_score),
2159 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
28a6d671 2160#ifdef CONFIG_AUDITSYSCALL
61a28784 2161 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
28a6d671 2162#endif
f4f154fd
AM
2163#ifdef CONFIG_FAULT_INJECTION
2164 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2165#endif
28a6d671
EB
2166};
2167
2168static int proc_tid_base_readdir(struct file * filp,
2169 void * dirent, filldir_t filldir)
2170{
2171 return proc_pident_readdir(filp,dirent,filldir,
2172 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2173}
2174
2175static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
7bcd6b0e
EB
2176 return proc_pident_lookup(dir, dentry,
2177 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
28a6d671
EB
2178}
2179
00977a59 2180static const struct file_operations proc_tid_base_operations = {
28a6d671
EB
2181 .read = generic_read_dir,
2182 .readdir = proc_tid_base_readdir,
2183};
2184
c5ef1c42 2185static const struct inode_operations proc_tid_base_inode_operations = {
28a6d671
EB
2186 .lookup = proc_tid_base_lookup,
2187 .getattr = pid_getattr,
2188 .setattr = proc_setattr,
2189};
2190
444ceed8
EB
2191static struct dentry *proc_task_instantiate(struct inode *dir,
2192 struct dentry *dentry, struct task_struct *task, void *ptr)
2193{
2194 struct dentry *error = ERR_PTR(-ENOENT);
2195 struct inode *inode;
61a28784 2196 inode = proc_pid_make_inode(dir->i_sb, task);
444ceed8
EB
2197
2198 if (!inode)
2199 goto out;
2200 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2201 inode->i_op = &proc_tid_base_inode_operations;
2202 inode->i_fop = &proc_tid_base_operations;
2203 inode->i_flags|=S_IMMUTABLE;
2204 inode->i_nlink = 3;
2205#ifdef CONFIG_SECURITY
2206 inode->i_nlink += 1;
2207#endif
2208
2209 dentry->d_op = &pid_dentry_operations;
2210
2211 d_add(dentry, inode);
2212 /* Close the race of the process dying before we return the dentry */
2213 if (pid_revalidate(dentry, NULL))
2214 error = NULL;
2215out:
2216 return error;
2217}
2218
28a6d671
EB
2219static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2220{
2221 struct dentry *result = ERR_PTR(-ENOENT);
2222 struct task_struct *task;
2223 struct task_struct *leader = get_proc_task(dir);
28a6d671
EB
2224 unsigned tid;
2225
2226 if (!leader)
2227 goto out_no_task;
2228
2229 tid = name_to_int(dentry);
2230 if (tid == ~0U)
2231 goto out;
2232
2233 rcu_read_lock();
2234 task = find_task_by_pid(tid);
2235 if (task)
2236 get_task_struct(task);
2237 rcu_read_unlock();
2238 if (!task)
2239 goto out;
2240 if (leader->tgid != task->tgid)
2241 goto out_drop_task;
2242
444ceed8 2243 result = proc_task_instantiate(dir, dentry, task, NULL);
28a6d671
EB
2244out_drop_task:
2245 put_task_struct(task);
2246out:
2247 put_task_struct(leader);
2248out_no_task:
2249 return result;
2250}
2251
0bc58a91
EB
2252/*
2253 * Find the first tid of a thread group to return to user space.
2254 *
2255 * Usually this is just the thread group leader, but if the users
2256 * buffer was too small or there was a seek into the middle of the
2257 * directory we have more work todo.
2258 *
2259 * In the case of a short read we start with find_task_by_pid.
2260 *
2261 * In the case of a seek we start with the leader and walk nr
2262 * threads past it.
2263 */
cc288738
EB
2264static struct task_struct *first_tid(struct task_struct *leader,
2265 int tid, int nr)
0bc58a91 2266{
a872ff0c 2267 struct task_struct *pos;
1da177e4 2268
cc288738 2269 rcu_read_lock();
0bc58a91
EB
2270 /* Attempt to start with the pid of a thread */
2271 if (tid && (nr > 0)) {
2272 pos = find_task_by_pid(tid);
a872ff0c
ON
2273 if (pos && (pos->group_leader == leader))
2274 goto found;
0bc58a91 2275 }
1da177e4 2276
0bc58a91 2277 /* If nr exceeds the number of threads there is nothing todo */
a872ff0c
ON
2278 pos = NULL;
2279 if (nr && nr >= get_nr_threads(leader))
2280 goto out;
1da177e4 2281
a872ff0c
ON
2282 /* If we haven't found our starting place yet start
2283 * with the leader and walk nr threads forward.
0bc58a91 2284 */
a872ff0c
ON
2285 for (pos = leader; nr > 0; --nr) {
2286 pos = next_thread(pos);
2287 if (pos == leader) {
2288 pos = NULL;
2289 goto out;
2290 }
1da177e4 2291 }
a872ff0c
ON
2292found:
2293 get_task_struct(pos);
2294out:
cc288738 2295 rcu_read_unlock();
0bc58a91
EB
2296 return pos;
2297}
2298
2299/*
2300 * Find the next thread in the thread list.
2301 * Return NULL if there is an error or no next thread.
2302 *
2303 * The reference to the input task_struct is released.
2304 */
2305static struct task_struct *next_tid(struct task_struct *start)
2306{
c1df7fb8 2307 struct task_struct *pos = NULL;
cc288738 2308 rcu_read_lock();
c1df7fb8 2309 if (pid_alive(start)) {
0bc58a91 2310 pos = next_thread(start);
c1df7fb8
ON
2311 if (thread_group_leader(pos))
2312 pos = NULL;
2313 else
2314 get_task_struct(pos);
2315 }
cc288738 2316 rcu_read_unlock();
0bc58a91
EB
2317 put_task_struct(start);
2318 return pos;
1da177e4
LT
2319}
2320
61a28784
EB
2321static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2322 struct task_struct *task, int tid)
2323{
2324 char name[PROC_NUMBUF];
2325 int len = snprintf(name, sizeof(name), "%d", tid);
2326 return proc_fill_cache(filp, dirent, filldir, name, len,
2327 proc_task_instantiate, task, NULL);
2328}
2329
1da177e4
LT
2330/* for the /proc/TGID/task/ directories */
2331static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2332{
2fddfeef 2333 struct dentry *dentry = filp->f_path.dentry;
1da177e4 2334 struct inode *inode = dentry->d_inode;
7d895244 2335 struct task_struct *leader = NULL;
0bc58a91 2336 struct task_struct *task;
1da177e4
LT
2337 int retval = -ENOENT;
2338 ino_t ino;
0bc58a91 2339 int tid;
1da177e4
LT
2340 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2341
7d895244
GC
2342 task = get_proc_task(inode);
2343 if (!task)
2344 goto out_no_task;
2345 rcu_read_lock();
2346 if (pid_alive(task)) {
2347 leader = task->group_leader;
2348 get_task_struct(leader);
2349 }
2350 rcu_read_unlock();
2351 put_task_struct(task);
99f89551
EB
2352 if (!leader)
2353 goto out_no_task;
1da177e4
LT
2354 retval = 0;
2355
2356 switch (pos) {
2357 case 0:
2358 ino = inode->i_ino;
2359 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2360 goto out;
2361 pos++;
2362 /* fall through */
2363 case 1:
2364 ino = parent_ino(dentry);
2365 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2366 goto out;
2367 pos++;
2368 /* fall through */
2369 }
2370
0bc58a91
EB
2371 /* f_version caches the tgid value that the last readdir call couldn't
2372 * return. lseek aka telldir automagically resets f_version to 0.
2373 */
2374 tid = filp->f_version;
2375 filp->f_version = 0;
2376 for (task = first_tid(leader, tid, pos - 2);
2377 task;
2378 task = next_tid(task), pos++) {
0bc58a91 2379 tid = task->pid;
61a28784 2380 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
0bc58a91
EB
2381 /* returning this tgid failed, save it as the first
2382 * pid for the next readir call */
2383 filp->f_version = tid;
2384 put_task_struct(task);
1da177e4 2385 break;
0bc58a91 2386 }
1da177e4
LT
2387 }
2388out:
2389 filp->f_pos = pos;
99f89551
EB
2390 put_task_struct(leader);
2391out_no_task:
1da177e4
LT
2392 return retval;
2393}
6e66b52b
EB
2394
2395static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2396{
2397 struct inode *inode = dentry->d_inode;
99f89551 2398 struct task_struct *p = get_proc_task(inode);
6e66b52b
EB
2399 generic_fillattr(inode, stat);
2400
99f89551
EB
2401 if (p) {
2402 rcu_read_lock();
2403 stat->nlink += get_nr_threads(p);
2404 rcu_read_unlock();
2405 put_task_struct(p);
6e66b52b
EB
2406 }
2407
2408 return 0;
2409}
28a6d671 2410
c5ef1c42 2411static const struct inode_operations proc_task_inode_operations = {
28a6d671
EB
2412 .lookup = proc_task_lookup,
2413 .getattr = proc_task_getattr,
2414 .setattr = proc_setattr,
2415};
2416
00977a59 2417static const struct file_operations proc_task_operations = {
28a6d671
EB
2418 .read = generic_read_dir,
2419 .readdir = proc_task_readdir,
2420};
This page took 0.343024 seconds and 5 git commands to generate.