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