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