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