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