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