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