fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / proc / base.c
1 /*
2  *  linux/fs/proc/base.c
3  *
4  *  Copyright (C) 1991, 1992 Linus Torvalds
5  *
6  *  proc base directory handling functions
7  *
8  *  1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9  *  Instead of using magical inumbers to determine the kind of object
10  *  we allocate and fill in-core inodes upon lookup. They don't even
11  *  go into icache. We cache the reference to task_struct upon lookup too.
12  *  Eventually it should become a filesystem in its own. We don't use the
13  *  rest of procfs anymore.
14  *
15  *
16  *  Changelog:
17  *  17-Jan-2005
18  *  Allan Bezerra
19  *  Bruna Moreira <bruna.moreira@indt.org.br>
20  *  Edjard Mota <edjard.mota@indt.org.br>
21  *  Ilias Biris <ilias.biris@indt.org.br>
22  *  Mauricio Lin <mauricio.lin@indt.org.br>
23  *
24  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25  *
26  *  A new process specific entry (smaps) included in /proc. It shows the
27  *  size of rss for each memory area. The maps entry lacks information
28  *  about physical memory size (rss) for each mapped file, i.e.,
29  *  rss information for executables and library files.
30  *  This additional information is useful for any tools that need to know
31  *  about physical memory consumption for a process specific library.
32  *
33  *  Changelog:
34  *  21-Feb-2005
35  *  Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36  *  Pud inclusion in the page table walking.
37  *
38  *  ChangeLog:
39  *  10-Mar-2005
40  *  10LE Instituto Nokia de Tecnologia - INdT:
41  *  A better way to walks through the page table as suggested by Hugh Dickins.
42  *
43  *  Simo Piiroinen <simo.piiroinen@nokia.com>:
44  *  Smaps information related to shared, private, clean and dirty pages.
45  *
46  *  Paul Mundt <paul.mundt@nokia.com>:
47  *  Overall revision about smaps.
48  */
49
50 #include <asm/uaccess.h>
51
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
63 #include <linux/mm.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/tracehook.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include <linux/vs_context.h>
78 #include <linux/vs_network.h>
79
80 #include "internal.h"
81
82 /* NOTE:
83  *      Implementing inode permission operations in /proc is almost
84  *      certainly an error.  Permission checks need to happen during
85  *      each system call not at open time.  The reason is that most of
86  *      what we wish to check for permissions in /proc varies at runtime.
87  *
88  *      The classic example of a problem is opening file descriptors
89  *      in /proc for a task before it execs a suid executable.
90  */
91
92
93 /* Worst case buffer size needed for holding an integer. */
94 #define PROC_NUMBUF 13
95
96 struct pid_entry {
97         int len;
98         char *name;
99         mode_t mode;
100         struct inode_operations *iop;
101         struct file_operations *fop;
102         union proc_op op;
103 };
104
105 #define NOD(NAME, MODE, IOP, FOP, OP) {                 \
106         .len  = sizeof(NAME) - 1,                       \
107         .name = (NAME),                                 \
108         .mode = MODE,                                   \
109         .iop  = IOP,                                    \
110         .fop  = FOP,                                    \
111         .op   = OP,                                     \
112 }
113
114 #define DIR(NAME, MODE, OTYPE)                                                  \
115         NOD(NAME, (S_IFDIR|(MODE)),                                             \
116                 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations,   \
117                 {} )
118 #define LNK(NAME, OTYPE)                                        \
119         NOD(NAME, (S_IFLNK|S_IRWXUGO),                          \
120                 &proc_pid_link_inode_operations, NULL,          \
121                 { .proc_get_link = &proc_##OTYPE##_link } )
122 #define REG(NAME, MODE, OTYPE)                          \
123         NOD(NAME, (S_IFREG|(MODE)), NULL,               \
124                 &proc_##OTYPE##_operations, {})
125 #define INF(NAME, MODE, OTYPE)                          \
126         NOD(NAME, (S_IFREG|(MODE)),                     \
127                 NULL, &proc_info_file_operations,       \
128                 { .proc_read = &proc_##OTYPE } )
129
130 static struct fs_struct *get_fs_struct(struct task_struct *task)
131 {
132         struct fs_struct *fs;
133         task_lock(task);
134         fs = task->fs;
135         if(fs)
136                 atomic_inc(&fs->count);
137         task_unlock(task);
138         return fs;
139 }
140
141 static int get_nr_threads(struct task_struct *tsk)
142 {
143         /* Must be called with the rcu_read_lock held */
144         unsigned long flags;
145         int count = 0;
146
147         if (lock_task_sighand(tsk, &flags)) {
148                 count = atomic_read(&tsk->signal->count);
149                 unlock_task_sighand(tsk, &flags);
150         }
151         return count;
152 }
153
154 static int __ptrace_may_attach(struct task_struct *task)
155 {
156         /* May we inspect the given task?
157          * This check is used both for attaching with ptrace
158          * and for allowing access to sensitive information in /proc.
159          *
160          * ptrace_attach denies several cases that /proc allows
161          * because setting up the necessary parent/child relationship
162          * or halting the specified task is impossible.
163          */
164         int dumpable = 0;
165         /* Don't let security modules deny introspection */
166         if (task == current)
167                 return 0;
168         if (((current->uid != task->euid) ||
169              (current->uid != task->suid) ||
170              (current->uid != task->uid) ||
171              (current->gid != task->egid) ||
172              (current->gid != task->sgid) ||
173              (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
174                 return -EPERM;
175         smp_rmb();
176         if (task->mm)
177                 dumpable = task->mm->dumpable;
178         if (!dumpable && !capable(CAP_SYS_PTRACE))
179                 return -EPERM;
180         if (!vx_check(task->xid, VS_ADMIN_P|VS_IDENT))
181                 return -EPERM;
182         if (!vx_check(task->xid, VS_IDENT) &&
183                 !task_vx_flags(task, VXF_STATE_ADMIN, 0))
184                 return -EACCES;
185
186         return security_ptrace(current, task);
187 }
188
189 int ptrace_may_attach(struct task_struct *task)
190 {
191         int err;
192         task_lock(task);
193         err = __ptrace_may_attach(task);
194         task_unlock(task);
195         return !err;
196 }
197
198
199 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
200 {
201         struct task_struct *task = get_proc_task(inode);
202         struct fs_struct *fs = NULL;
203         int result = -ENOENT;
204
205         if (task) {
206                 fs = get_fs_struct(task);
207                 put_task_struct(task);
208         }
209         if (fs) {
210                 read_lock(&fs->lock);
211                 *mnt = mntget(fs->pwdmnt);
212                 *dentry = dget(fs->pwd);
213                 read_unlock(&fs->lock);
214                 result = 0;
215                 put_fs_struct(fs);
216         }
217         return result;
218 }
219
220 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
221 {
222         struct task_struct *task = get_proc_task(inode);
223         struct fs_struct *fs = NULL;
224         int result = -ENOENT;
225
226         if (task) {
227                 fs = get_fs_struct(task);
228                 put_task_struct(task);
229         }
230         if (fs) {
231                 read_lock(&fs->lock);
232                 *mnt = mntget(fs->rootmnt);
233                 *dentry = dget(fs->root);
234                 read_unlock(&fs->lock);
235                 result = 0;
236                 put_fs_struct(fs);
237         }
238         return result;
239 }
240
241 struct mm_struct *mm_for_maps(struct task_struct *task)
242 {
243         struct mm_struct *mm = get_task_mm(task);
244         if (!mm)
245                 return NULL;
246         down_read(&mm->mmap_sem);
247         task_lock(task);
248         if (task->mm != mm)
249                 goto out;
250         if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
251                 goto out;
252         task_unlock(task);
253         return mm;
254 out:
255         task_unlock(task);
256         up_read(&mm->mmap_sem);
257         mmput(mm);
258         return NULL;
259 }
260
261 static int proc_pid_environ(struct task_struct *task, char * buffer)
262 {
263         int res = 0;
264         struct mm_struct *mm = get_task_mm(task);
265         if (mm) {
266                 unsigned int len = mm->env_end - mm->env_start;
267                 if (len > PAGE_SIZE)
268                         len = PAGE_SIZE;
269                 res = access_process_vm(task, mm->env_start, buffer, len, 0);
270                 if (!ptrace_may_attach(task))
271                         res = -ESRCH;
272                 mmput(mm);
273         }
274         return res;
275 }
276
277 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
278 {
279         int res = 0;
280         unsigned int len;
281         struct mm_struct *mm = get_task_mm(task);
282         if (!mm)
283                 goto out;
284         if (!mm->arg_end)
285                 goto out_mm;    /* Shh! No looking before we're done */
286
287         len = mm->arg_end - mm->arg_start;
288  
289         if (len > PAGE_SIZE)
290                 len = PAGE_SIZE;
291  
292         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
293
294         // If the nul at the end of args has been overwritten, then
295         // assume application is using setproctitle(3).
296         if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
297                 len = strnlen(buffer, res);
298                 if (len < res) {
299                     res = len;
300                 } else {
301                         len = mm->env_end - mm->env_start;
302                         if (len > PAGE_SIZE - res)
303                                 len = PAGE_SIZE - res;
304                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
305                         res = strnlen(buffer, res);
306                 }
307         }
308 out_mm:
309         mmput(mm);
310 out:
311         return res;
312 }
313
314 static int proc_pid_auxv(struct task_struct *task, char *buffer)
315 {
316         int res = 0;
317         struct mm_struct *mm = get_task_mm(task);
318         if (mm) {
319                 unsigned int nwords = 0;
320                 do
321                         nwords += 2;
322                 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
323                 res = nwords * sizeof(mm->saved_auxv[0]);
324                 if (res > PAGE_SIZE)
325                         res = PAGE_SIZE;
326                 memcpy(buffer, mm->saved_auxv, res);
327                 mmput(mm);
328         }
329         return res;
330 }
331
332
333 #ifdef CONFIG_KALLSYMS
334 /*
335  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
336  * Returns the resolved symbol.  If that fails, simply return the address.
337  */
338 static int proc_pid_wchan(struct task_struct *task, char *buffer)
339 {
340         char *modname;
341         const char *sym_name;
342         unsigned long wchan, size, offset;
343         char namebuf[KSYM_NAME_LEN+1];
344
345         wchan = get_wchan(task);
346
347         sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
348         if (sym_name)
349                 return sprintf(buffer, "%s", sym_name);
350         return sprintf(buffer, "%lu", wchan);
351 }
352 #endif /* CONFIG_KALLSYMS */
353
354 #ifdef CONFIG_SCHEDSTATS
355 /*
356  * Provides /proc/PID/schedstat
357  */
358 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
359 {
360         return sprintf(buffer, "%lu %lu %lu\n",
361                         task->sched_info.cpu_time,
362                         task->sched_info.run_delay,
363                         task->sched_info.pcnt);
364 }
365 #endif
366
367 /* The badness from the OOM killer */
368 unsigned long badness(struct task_struct *p, unsigned long uptime);
369 static int proc_oom_score(struct task_struct *task, char *buffer)
370 {
371         unsigned long points;
372         struct timespec uptime;
373
374         do_posix_clock_monotonic_gettime(&uptime);
375         points = badness(task, uptime.tv_sec);
376         return sprintf(buffer, "%lu\n", points);
377 }
378
379 /************************************************************************/
380 /*                       Here the fs part begins                        */
381 /************************************************************************/
382
383 /* permission checks */
384 static int proc_fd_access_allowed(struct inode *inode)
385 {
386         struct task_struct *task;
387         int allowed = 0;
388         /* Allow access to a task's file descriptors if it is us or we
389          * may use ptrace attach to the process and find out that
390          * information.
391          */
392         task = get_proc_task(inode);
393         if (task) {
394                 allowed = ptrace_may_attach(task);
395                 put_task_struct(task);
396         }
397         return allowed;
398 }
399
400 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
401 {
402         int error;
403         struct inode *inode = dentry->d_inode;
404
405         if (attr->ia_valid & ATTR_MODE)
406                 return -EPERM;
407
408         error = inode_change_ok(inode, attr);
409         if (!error) {
410                 error = security_inode_setattr(dentry, attr);
411                 if (!error)
412                         error = inode_setattr(inode, attr);
413         }
414         return error;
415 }
416
417 static struct inode_operations proc_def_inode_operations = {
418         .setattr        = proc_setattr,
419 };
420
421 extern struct seq_operations mounts_op;
422 struct proc_mounts {
423         struct seq_file m;
424         int event;
425 };
426
427 static int mounts_open(struct inode *inode, struct file *file)
428 {
429         struct task_struct *task = get_proc_task(inode);
430         struct mnt_namespace *ns = NULL;
431         struct proc_mounts *p;
432         int ret = -EINVAL;
433
434         if (task) {
435                 task_lock(task);
436                 if (task->nsproxy) {
437                         ns = task->nsproxy->mnt_ns;
438                         if (ns)
439                                 get_mnt_ns(ns);
440                 }
441                 task_unlock(task);
442                 put_task_struct(task);
443         }
444
445         if (ns) {
446                 ret = -ENOMEM;
447                 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
448                 if (p) {
449                         file->private_data = &p->m;
450                         ret = seq_open(file, &mounts_op);
451                         if (!ret) {
452                                 p->m.private = ns;
453                                 p->event = ns->event;
454                                 return 0;
455                         }
456                         kfree(p);
457                 }
458                 put_mnt_ns(ns);
459         }
460         return ret;
461 }
462
463 static int mounts_release(struct inode *inode, struct file *file)
464 {
465         struct seq_file *m = file->private_data;
466         struct mnt_namespace *ns = m->private;
467         put_mnt_ns(ns);
468         return seq_release(inode, file);
469 }
470
471 static unsigned mounts_poll(struct file *file, poll_table *wait)
472 {
473         struct proc_mounts *p = file->private_data;
474         struct mnt_namespace *ns = p->m.private;
475         unsigned res = 0;
476
477         poll_wait(file, &ns->poll, wait);
478
479         spin_lock(&vfsmount_lock);
480         if (p->event != ns->event) {
481                 p->event = ns->event;
482                 res = POLLERR;
483         }
484         spin_unlock(&vfsmount_lock);
485
486         return res;
487 }
488
489 static struct file_operations proc_mounts_operations = {
490         .open           = mounts_open,
491         .read           = seq_read,
492         .llseek         = seq_lseek,
493         .release        = mounts_release,
494         .poll           = mounts_poll,
495 };
496
497 extern struct seq_operations mountstats_op;
498 static int mountstats_open(struct inode *inode, struct file *file)
499 {
500         int ret = seq_open(file, &mountstats_op);
501
502         if (!ret) {
503                 struct seq_file *m = file->private_data;
504                 struct mnt_namespace *mnt_ns = NULL;
505                 struct task_struct *task = get_proc_task(inode);
506
507                 if (task) {
508                         task_lock(task);
509                         if (task->nsproxy)
510                                 mnt_ns = task->nsproxy->mnt_ns;
511                         if (mnt_ns)
512                                 get_mnt_ns(mnt_ns);
513                         task_unlock(task);
514                         put_task_struct(task);
515                 }
516
517                 if (mnt_ns)
518                         m->private = mnt_ns;
519                 else {
520                         seq_release(inode, file);
521                         ret = -EINVAL;
522                 }
523         }
524         return ret;
525 }
526
527 static struct file_operations proc_mountstats_operations = {
528         .open           = mountstats_open,
529         .read           = seq_read,
530         .llseek         = seq_lseek,
531         .release        = mounts_release,
532 };
533
534 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
535
536 static ssize_t proc_info_read(struct file * file, char __user * buf,
537                           size_t count, loff_t *ppos)
538 {
539         struct inode * inode = file->f_path.dentry->d_inode;
540         unsigned long page;
541         ssize_t length;
542         struct task_struct *task = get_proc_task(inode);
543
544         length = -ESRCH;
545         if (!task)
546                 goto out_no_task;
547
548         if (count > PROC_BLOCK_SIZE)
549                 count = PROC_BLOCK_SIZE;
550
551         length = -ENOMEM;
552         if (!(page = __get_free_page(GFP_KERNEL)))
553                 goto out;
554
555         length = PROC_I(inode)->op.proc_read(task, (char*)page);
556
557         if (length >= 0)
558                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
559         free_page(page);
560 out:
561         put_task_struct(task);
562 out_no_task:
563         return length;
564 }
565
566 static struct file_operations proc_info_file_operations = {
567         .read           = proc_info_read,
568 };
569
570 static int mem_open(struct inode* inode, struct file* file)
571 {
572         file->private_data = (void*)((long)current->self_exec_id);
573         return 0;
574 }
575
576 static ssize_t mem_read(struct file * file, char __user * buf,
577                         size_t count, loff_t *ppos)
578 {
579         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
580         char *page;
581         unsigned long src = *ppos;
582         int ret = -ESRCH;
583         struct mm_struct *mm;
584
585         if (!task)
586                 goto out_no_task;
587
588         if (!tracehook_allow_access_process_vm(task)
589             || !ptrace_may_attach(task))
590                 goto out;
591
592         ret = -ENOMEM;
593         page = (char *)__get_free_page(GFP_USER);
594         if (!page)
595                 goto out;
596
597         ret = 0;
598  
599         mm = get_task_mm(task);
600         if (!mm)
601                 goto out_free;
602
603         ret = -EIO;
604  
605         if (file->private_data != (void*)((long)current->self_exec_id))
606                 goto out_put;
607
608         ret = 0;
609  
610         while (count > 0) {
611                 int this_len, retval;
612
613                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
614                 retval = access_process_vm(task, src, page, this_len, 0);
615                 if (!retval || !tracehook_allow_access_process_vm(task)
616                     || !ptrace_may_attach(task)) {
617                         if (!ret)
618                                 ret = -EIO;
619                         break;
620                 }
621
622                 if (copy_to_user(buf, page, retval)) {
623                         ret = -EFAULT;
624                         break;
625                 }
626  
627                 ret += retval;
628                 src += retval;
629                 buf += retval;
630                 count -= retval;
631         }
632         *ppos = src;
633
634 out_put:
635         mmput(mm);
636 out_free:
637         free_page((unsigned long) page);
638 out:
639         put_task_struct(task);
640 out_no_task:
641         return ret;
642 }
643
644 #define mem_write NULL
645
646 #ifndef mem_write
647 /* This is a security hazard */
648 static ssize_t mem_write(struct file * file, const char * buf,
649                          size_t count, loff_t *ppos)
650 {
651         int copied;
652         char *page;
653         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
654         unsigned long dst = *ppos;
655
656         copied = -ESRCH;
657         if (!task)
658                 goto out_no_task;
659
660         if (!tracehook_allow_access_process_vm(task)
661             || !ptrace_may_attach(task))
662                 goto out;
663
664         copied = -ENOMEM;
665         page = (char *)__get_free_page(GFP_USER);
666         if (!page)
667                 goto out;
668
669         copied = 0;
670         while (count > 0) {
671                 int this_len, retval;
672
673                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
674                 if (copy_from_user(page, buf, this_len)) {
675                         copied = -EFAULT;
676                         break;
677                 }
678                 retval = access_process_vm(task, dst, page, this_len, 1);
679                 if (!retval) {
680                         if (!copied)
681                                 copied = -EIO;
682                         break;
683                 }
684                 copied += retval;
685                 buf += retval;
686                 dst += retval;
687                 count -= retval;                        
688         }
689         *ppos = dst;
690         free_page((unsigned long) page);
691 out:
692         put_task_struct(task);
693 out_no_task:
694         return copied;
695 }
696 #endif
697
698 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
699 {
700         switch (orig) {
701         case 0:
702                 file->f_pos = offset;
703                 break;
704         case 1:
705                 file->f_pos += offset;
706                 break;
707         default:
708                 return -EINVAL;
709         }
710         force_successful_syscall_return();
711         return file->f_pos;
712 }
713
714 static struct file_operations proc_mem_operations = {
715         .llseek         = mem_lseek,
716         .read           = mem_read,
717         .write          = mem_write,
718         .open           = mem_open,
719 };
720
721 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
722                                 size_t count, loff_t *ppos)
723 {
724         struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
725         char buffer[PROC_NUMBUF];
726         size_t len;
727         int oom_adjust;
728         loff_t __ppos = *ppos;
729
730         if (!task)
731                 return -ESRCH;
732         oom_adjust = task->oomkilladj;
733         put_task_struct(task);
734
735         len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
736         if (__ppos >= len)
737                 return 0;
738         if (count > len-__ppos)
739                 count = len-__ppos;
740         if (copy_to_user(buf, buffer + __ppos, count))
741                 return -EFAULT;
742         *ppos = __ppos + count;
743         return count;
744 }
745
746 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
747                                 size_t count, loff_t *ppos)
748 {
749         struct task_struct *task;
750         char buffer[PROC_NUMBUF], *end;
751         int oom_adjust;
752
753         memset(buffer, 0, sizeof(buffer));
754         if (count > sizeof(buffer) - 1)
755                 count = sizeof(buffer) - 1;
756         if (copy_from_user(buffer, buf, count))
757                 return -EFAULT;
758         oom_adjust = simple_strtol(buffer, &end, 0);
759         if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
760              oom_adjust != OOM_DISABLE)
761                 return -EINVAL;
762         if (*end == '\n')
763                 end++;
764         task = get_proc_task(file->f_path.dentry->d_inode);
765         if (!task)
766                 return -ESRCH;
767         if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
768                 put_task_struct(task);
769                 return -EACCES;
770         }
771         task->oomkilladj = oom_adjust;
772         put_task_struct(task);
773         if (end - buffer == 0)
774                 return -EIO;
775         return end - buffer;
776 }
777
778 static struct file_operations proc_oom_adjust_operations = {
779         .read           = oom_adjust_read,
780         .write          = oom_adjust_write,
781 };
782
783 #ifdef CONFIG_AUDITSYSCALL
784 #define TMPBUFLEN 21
785 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
786                                   size_t count, loff_t *ppos)
787 {
788         struct inode * inode = file->f_path.dentry->d_inode;
789         struct task_struct *task = get_proc_task(inode);
790         ssize_t length;
791         char tmpbuf[TMPBUFLEN];
792
793         if (!task)
794                 return -ESRCH;
795         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
796                                 audit_get_loginuid(task->audit_context));
797         put_task_struct(task);
798         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
799 }
800
801 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
802                                    size_t count, loff_t *ppos)
803 {
804         struct inode * inode = file->f_path.dentry->d_inode;
805         char *page, *tmp;
806         ssize_t length;
807         uid_t loginuid;
808
809         if (!capable(CAP_AUDIT_CONTROL))
810                 return -EPERM;
811
812         if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
813                 return -EPERM;
814
815         if (count >= PAGE_SIZE)
816                 count = PAGE_SIZE - 1;
817
818         if (*ppos != 0) {
819                 /* No partial writes. */
820                 return -EINVAL;
821         }
822         page = (char*)__get_free_page(GFP_USER);
823         if (!page)
824                 return -ENOMEM;
825         length = -EFAULT;
826         if (copy_from_user(page, buf, count))
827                 goto out_free_page;
828
829         page[count] = '\0';
830         loginuid = simple_strtoul(page, &tmp, 10);
831         if (tmp == page) {
832                 length = -EINVAL;
833                 goto out_free_page;
834
835         }
836         length = audit_set_loginuid(current, loginuid);
837         if (likely(length == 0))
838                 length = count;
839
840 out_free_page:
841         free_page((unsigned long) page);
842         return length;
843 }
844
845 static struct file_operations proc_loginuid_operations = {
846         .read           = proc_loginuid_read,
847         .write          = proc_loginuid_write,
848 };
849 #endif
850
851 #ifdef CONFIG_SECCOMP
852 static ssize_t seccomp_read(struct file *file, char __user *buf,
853                             size_t count, loff_t *ppos)
854 {
855         struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
856         char __buf[20];
857         loff_t __ppos = *ppos;
858         size_t len;
859
860         if (!tsk)
861                 return -ESRCH;
862         /* no need to print the trailing zero, so use only len */
863         len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
864         put_task_struct(tsk);
865         if (__ppos >= len)
866                 return 0;
867         if (count > len - __ppos)
868                 count = len - __ppos;
869         if (copy_to_user(buf, __buf + __ppos, count))
870                 return -EFAULT;
871         *ppos = __ppos + count;
872         return count;
873 }
874
875 static ssize_t seccomp_write(struct file *file, const char __user *buf,
876                              size_t count, loff_t *ppos)
877 {
878         struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
879         char __buf[20], *end;
880         unsigned int seccomp_mode;
881         ssize_t result;
882
883         result = -ESRCH;
884         if (!tsk)
885                 goto out_no_task;
886
887         /* can set it only once to be even more secure */
888         result = -EPERM;
889         if (unlikely(tsk->seccomp.mode))
890                 goto out;
891
892         result = -EFAULT;
893         memset(__buf, 0, sizeof(__buf));
894         count = min(count, sizeof(__buf) - 1);
895         if (copy_from_user(__buf, buf, count))
896                 goto out;
897
898         seccomp_mode = simple_strtoul(__buf, &end, 0);
899         if (*end == '\n')
900                 end++;
901         result = -EINVAL;
902         if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
903                 tsk->seccomp.mode = seccomp_mode;
904                 set_tsk_thread_flag(tsk, TIF_SECCOMP);
905         } else
906                 goto out;
907         result = -EIO;
908         if (unlikely(!(end - __buf)))
909                 goto out;
910         result = end - __buf;
911 out:
912         put_task_struct(tsk);
913 out_no_task:
914         return result;
915 }
916
917 static struct file_operations proc_seccomp_operations = {
918         .read           = seccomp_read,
919         .write          = seccomp_write,
920 };
921 #endif /* CONFIG_SECCOMP */
922
923 #ifdef CONFIG_FAULT_INJECTION
924 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
925                                       size_t count, loff_t *ppos)
926 {
927         struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
928         char buffer[PROC_NUMBUF];
929         size_t len;
930         int make_it_fail;
931         loff_t __ppos = *ppos;
932
933         if (!task)
934                 return -ESRCH;
935         make_it_fail = task->make_it_fail;
936         put_task_struct(task);
937
938         len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
939         if (__ppos >= len)
940                 return 0;
941         if (count > len-__ppos)
942                 count = len-__ppos;
943         if (copy_to_user(buf, buffer + __ppos, count))
944                 return -EFAULT;
945         *ppos = __ppos + count;
946         return count;
947 }
948
949 static ssize_t proc_fault_inject_write(struct file * file,
950                         const char __user * buf, size_t count, loff_t *ppos)
951 {
952         struct task_struct *task;
953         char buffer[PROC_NUMBUF], *end;
954         int make_it_fail;
955
956         if (!capable(CAP_SYS_RESOURCE))
957                 return -EPERM;
958         memset(buffer, 0, sizeof(buffer));
959         if (count > sizeof(buffer) - 1)
960                 count = sizeof(buffer) - 1;
961         if (copy_from_user(buffer, buf, count))
962                 return -EFAULT;
963         make_it_fail = simple_strtol(buffer, &end, 0);
964         if (*end == '\n')
965                 end++;
966         task = get_proc_task(file->f_dentry->d_inode);
967         if (!task)
968                 return -ESRCH;
969         task->make_it_fail = make_it_fail;
970         put_task_struct(task);
971         if (end - buffer == 0)
972                 return -EIO;
973         return end - buffer;
974 }
975
976 static struct file_operations proc_fault_inject_operations = {
977         .read           = proc_fault_inject_read,
978         .write          = proc_fault_inject_write,
979 };
980 #endif
981
982 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
983 {
984         struct inode *inode = dentry->d_inode;
985         int error = -EACCES;
986
987         /* We don't need a base pointer in the /proc filesystem */
988         path_release(nd);
989
990         /* Are we allowed to snoop on the tasks file descriptors? */
991         if (!proc_fd_access_allowed(inode))
992                 goto out;
993
994         error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
995         nd->last_type = LAST_BIND;
996 out:
997         return ERR_PTR(error);
998 }
999
1000 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1001                             char __user *buffer, int buflen)
1002 {
1003         struct inode * inode;
1004         char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1005         int len;
1006
1007         if (!tmp)
1008                 return -ENOMEM;
1009                 
1010         inode = dentry->d_inode;
1011         path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1012         len = PTR_ERR(path);
1013         if (IS_ERR(path))
1014                 goto out;
1015         len = tmp + PAGE_SIZE - 1 - path;
1016
1017         if (len > buflen)
1018                 len = buflen;
1019         if (copy_to_user(buffer, path, len))
1020                 len = -EFAULT;
1021  out:
1022         free_page((unsigned long)tmp);
1023         return len;
1024 }
1025
1026 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1027 {
1028         int error = -EACCES;
1029         struct inode *inode = dentry->d_inode;
1030         struct dentry *de;
1031         struct vfsmount *mnt = NULL;
1032
1033         /* Are we allowed to snoop on the tasks file descriptors? */
1034         if (!proc_fd_access_allowed(inode))
1035                 goto out;
1036
1037         error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1038         if (error)
1039                 goto out;
1040
1041         error = do_proc_readlink(de, mnt, buffer, buflen);
1042         dput(de);
1043         mntput(mnt);
1044 out:
1045         return error;
1046 }
1047
1048 static struct inode_operations proc_pid_link_inode_operations = {
1049         .readlink       = proc_pid_readlink,
1050         .follow_link    = proc_pid_follow_link,
1051         .setattr        = proc_setattr,
1052 };
1053
1054
1055 /* building an inode */
1056
1057 static int task_dumpable(struct task_struct *task)
1058 {
1059         int dumpable = 0;
1060         struct mm_struct *mm;
1061
1062         task_lock(task);
1063         mm = task->mm;
1064         if (mm)
1065                 dumpable = mm->dumpable;
1066         task_unlock(task);
1067         if(dumpable == 1)
1068                 return 1;
1069         return 0;
1070 }
1071
1072
1073 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1074 {
1075         struct inode * inode;
1076         struct proc_inode *ei;
1077
1078         /* We need a new inode */
1079
1080         inode = new_inode(sb);
1081         if (!inode)
1082                 goto out;
1083
1084         /* Common stuff */
1085         ei = PROC_I(inode);
1086         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1087         inode->i_op = &proc_def_inode_operations;
1088
1089         /*
1090          * grab the reference to task.
1091          */
1092         ei->pid = get_task_pid(task, PIDTYPE_PID);
1093         if (!ei->pid)
1094                 goto out_unlock;
1095
1096         inode->i_uid = 0;
1097         inode->i_gid = 0;
1098         if (task_dumpable(task)) {
1099                 inode->i_uid = task->euid;
1100                 inode->i_gid = task->egid;
1101         }
1102         /* procfs is xid tagged */
1103         inode->i_tag = (tag_t)vx_task_xid(task);
1104         security_task_to_inode(task, inode);
1105
1106 out:
1107         return inode;
1108
1109 out_unlock:
1110         iput(inode);
1111         return NULL;
1112 }
1113
1114 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1115 {
1116         struct inode *inode = dentry->d_inode;
1117         struct task_struct *task;
1118         generic_fillattr(inode, stat);
1119
1120         rcu_read_lock();
1121         stat->uid = 0;
1122         stat->gid = 0;
1123         task = pid_task(proc_pid(inode), PIDTYPE_PID);
1124         if (task) {
1125                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1126                     task_dumpable(task)) {
1127                         stat->uid = task->euid;
1128                         stat->gid = task->egid;
1129                 }
1130         }
1131         rcu_read_unlock();
1132         return 0;
1133 }
1134
1135 /* dentry stuff */
1136
1137 /*
1138  *      Exceptional case: normally we are not allowed to unhash a busy
1139  * directory. In this case, however, we can do it - no aliasing problems
1140  * due to the way we treat inodes.
1141  *
1142  * Rewrite the inode's ownerships here because the owning task may have
1143  * performed a setuid(), etc.
1144  *
1145  * Before the /proc/pid/status file was created the only way to read
1146  * the effective uid of a /process was to stat /proc/pid.  Reading
1147  * /proc/pid/status is slow enough that procps and other packages
1148  * kept stating /proc/pid.  To keep the rules in /proc simple I have
1149  * made this apply to all per process world readable and executable
1150  * directories.
1151  */
1152 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1153 {
1154         struct inode *inode = dentry->d_inode;
1155         struct task_struct *task = get_proc_task(inode);
1156         if (task) {
1157                 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1158                     task_dumpable(task)) {
1159                         inode->i_uid = task->euid;
1160                         inode->i_gid = task->egid;
1161                 } else {
1162                         inode->i_uid = 0;
1163                         inode->i_gid = 0;
1164                 }
1165                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1166                 security_task_to_inode(task, inode);
1167                 put_task_struct(task);
1168                 return 1;
1169         }
1170         d_drop(dentry);
1171         return 0;
1172 }
1173
1174 static int pid_delete_dentry(struct dentry * dentry)
1175 {
1176         /* Is the task we represent dead?
1177          * If so, then don't put the dentry on the lru list,
1178          * kill it immediately.
1179          */
1180         return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1181 }
1182
1183 static struct dentry_operations pid_dentry_operations =
1184 {
1185         .d_revalidate   = pid_revalidate,
1186         .d_delete       = pid_delete_dentry,
1187 };
1188
1189 /* Lookups */
1190
1191 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1192
1193 /*
1194  * Fill a directory entry.
1195  *
1196  * If possible create the dcache entry and derive our inode number and
1197  * file type from dcache entry.
1198  *
1199  * Since all of the proc inode numbers are dynamically generated, the inode
1200  * numbers do not exist until the inode is cache.  This means creating the
1201  * the dcache entry in readdir is necessary to keep the inode numbers
1202  * reported by readdir in sync with the inode numbers reported
1203  * by stat.
1204  */
1205 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1206         char *name, int len,
1207         instantiate_t instantiate, struct task_struct *task, void *ptr)
1208 {
1209         struct dentry *child, *dir = filp->f_path.dentry;
1210         struct inode *inode;
1211         struct qstr qname;
1212         ino_t ino = 0;
1213         unsigned type = DT_UNKNOWN;
1214
1215         qname.name = name;
1216         qname.len  = len;
1217         qname.hash = full_name_hash(name, len);
1218
1219         child = d_lookup(dir, &qname);
1220         if (!child) {
1221                 struct dentry *new;
1222                 new = d_alloc(dir, &qname);
1223                 if (new) {
1224                         child = instantiate(dir->d_inode, new, task, ptr);
1225                         if (child)
1226                                 dput(new);
1227                         else
1228                                 child = new;
1229                 }
1230         }
1231         if (!child || IS_ERR(child) || !child->d_inode)
1232                 goto end_instantiate;
1233         inode = child->d_inode;
1234         if (inode) {
1235                 ino = inode->i_ino;
1236                 type = inode->i_mode >> 12;
1237         }
1238         dput(child);
1239 end_instantiate:
1240         if (!ino)
1241                 ino = find_inode_number(dir, &qname);
1242         if (!ino)
1243                 ino = 1;
1244         return filldir(dirent, name, len, filp->f_pos, ino, type);
1245 }
1246
1247 static unsigned name_to_int(struct dentry *dentry)
1248 {
1249         const char *name = dentry->d_name.name;
1250         int len = dentry->d_name.len;
1251         unsigned n = 0;
1252
1253         if (len > 1 && *name == '0')
1254                 goto out;
1255         while (len-- > 0) {
1256                 unsigned c = *name++ - '0';
1257                 if (c > 9)
1258                         goto out;
1259                 if (n >= (~0U-9)/10)
1260                         goto out;
1261                 n *= 10;
1262                 n += c;
1263         }
1264         return n;
1265 out:
1266         return ~0U;
1267 }
1268
1269 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1270 {
1271         struct task_struct *task = get_proc_task(inode);
1272         struct files_struct *files = NULL;
1273         struct file *file;
1274         int fd = proc_fd(inode);
1275
1276         if (task) {
1277                 files = get_files_struct(task);
1278                 put_task_struct(task);
1279         }
1280         if (files) {
1281                 /*
1282                  * We are not taking a ref to the file structure, so we must
1283                  * hold ->file_lock.
1284                  */
1285                 spin_lock(&files->file_lock);
1286                 file = fcheck_files(files, fd);
1287                 if (file) {
1288                         *mnt = mntget(file->f_path.mnt);
1289                         *dentry = dget(file->f_path.dentry);
1290                         spin_unlock(&files->file_lock);
1291                         put_files_struct(files);
1292                         return 0;
1293                 }
1294                 spin_unlock(&files->file_lock);
1295                 put_files_struct(files);
1296         }
1297         return -ENOENT;
1298 }
1299
1300 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1301 {
1302         struct inode *inode = dentry->d_inode;
1303         struct task_struct *task = get_proc_task(inode);
1304         int fd = proc_fd(inode);
1305         struct files_struct *files;
1306
1307         if (task) {
1308                 files = get_files_struct(task);
1309                 if (files) {
1310                         rcu_read_lock();
1311                         if (fcheck_files(files, fd)) {
1312                                 rcu_read_unlock();
1313                                 put_files_struct(files);
1314                                 if (task_dumpable(task)) {
1315                                         inode->i_uid = task->euid;
1316                                         inode->i_gid = task->egid;
1317                                 } else {
1318                                         inode->i_uid = 0;
1319                                         inode->i_gid = 0;
1320                                 }
1321                                 inode->i_mode &= ~(S_ISUID | S_ISGID);
1322                                 security_task_to_inode(task, inode);
1323                                 put_task_struct(task);
1324                                 return 1;
1325                         }
1326                         rcu_read_unlock();
1327                         put_files_struct(files);
1328                 }
1329                 put_task_struct(task);
1330         }
1331         d_drop(dentry);
1332         return 0;
1333 }
1334
1335 static struct dentry_operations tid_fd_dentry_operations =
1336 {
1337         .d_revalidate   = tid_fd_revalidate,
1338         .d_delete       = pid_delete_dentry,
1339 };
1340
1341 static struct dentry *proc_fd_instantiate(struct inode *dir,
1342         struct dentry *dentry, struct task_struct *task, void *ptr)
1343 {
1344         unsigned fd = *(unsigned *)ptr;
1345         struct file *file;
1346         struct files_struct *files;
1347         struct inode *inode;
1348         struct proc_inode *ei;
1349         struct dentry *error = ERR_PTR(-ENOENT);
1350
1351         inode = proc_pid_make_inode(dir->i_sb, task);
1352         if (!inode)
1353                 goto out;
1354         ei = PROC_I(inode);
1355         ei->fd = fd;
1356         files = get_files_struct(task);
1357         if (!files)
1358                 goto out_iput;
1359         inode->i_mode = S_IFLNK;
1360
1361         /*
1362          * We are not taking a ref to the file structure, so we must
1363          * hold ->file_lock.
1364          */
1365         spin_lock(&files->file_lock);
1366         file = fcheck_files(files, fd);
1367         if (!file)
1368                 goto out_unlock;
1369         if (file->f_mode & 1)
1370                 inode->i_mode |= S_IRUSR | S_IXUSR;
1371         if (file->f_mode & 2)
1372                 inode->i_mode |= S_IWUSR | S_IXUSR;
1373         spin_unlock(&files->file_lock);
1374         put_files_struct(files);
1375
1376         inode->i_op = &proc_pid_link_inode_operations;
1377         inode->i_size = 64;
1378         ei->op.proc_get_link = proc_fd_link;
1379         dentry->d_op = &tid_fd_dentry_operations;
1380         d_add(dentry, inode);
1381         /* Close the race of the process dying before we return the dentry */
1382         if (tid_fd_revalidate(dentry, NULL))
1383                 error = NULL;
1384
1385  out:
1386         return error;
1387 out_unlock:
1388         spin_unlock(&files->file_lock);
1389         put_files_struct(files);
1390 out_iput:
1391         iput(inode);
1392         goto out;
1393 }
1394
1395 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1396 {
1397         struct task_struct *task = get_proc_task(dir);
1398         unsigned fd = name_to_int(dentry);
1399         struct dentry *result = ERR_PTR(-ENOENT);
1400
1401         if (!task)
1402                 goto out_no_task;
1403         if (fd == ~0U)
1404                 goto out;
1405
1406         result = proc_fd_instantiate(dir, dentry, task, &fd);
1407 out:
1408         put_task_struct(task);
1409 out_no_task:
1410         return result;
1411 }
1412
1413 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1414         struct task_struct *task, int fd)
1415 {
1416         char name[PROC_NUMBUF];
1417         int len = snprintf(name, sizeof(name), "%d", fd);
1418         return proc_fill_cache(filp, dirent, filldir, name, len,
1419                                 proc_fd_instantiate, task, &fd);
1420 }
1421
1422 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1423 {
1424         struct dentry *dentry = filp->f_path.dentry;
1425         struct inode *inode = dentry->d_inode;
1426         struct task_struct *p = get_proc_task(inode);
1427         unsigned int fd, tid, ino;
1428         int retval;
1429         struct files_struct * files;
1430         struct fdtable *fdt;
1431
1432         retval = -ENOENT;
1433         if (!p)
1434                 goto out_no_task;
1435         retval = 0;
1436         tid = p->pid;
1437
1438         fd = filp->f_pos;
1439         switch (fd) {
1440                 case 0:
1441                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1442                                 goto out;
1443                         filp->f_pos++;
1444                 case 1:
1445                         ino = parent_ino(dentry);
1446                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1447                                 goto out;
1448                         filp->f_pos++;
1449                 default:
1450                         files = get_files_struct(p);
1451                         if (!files)
1452                                 goto out;
1453                         rcu_read_lock();
1454                         fdt = files_fdtable(files);
1455                         for (fd = filp->f_pos-2;
1456                              fd < fdt->max_fds;
1457                              fd++, filp->f_pos++) {
1458
1459                                 if (!fcheck_files(files, fd))
1460                                         continue;
1461                                 rcu_read_unlock();
1462
1463                                 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1464                                         rcu_read_lock();
1465                                         break;
1466                                 }
1467                                 rcu_read_lock();
1468                         }
1469                         rcu_read_unlock();
1470                         put_files_struct(files);
1471         }
1472 out:
1473         put_task_struct(p);
1474 out_no_task:
1475         return retval;
1476 }
1477
1478 static struct file_operations proc_fd_operations = {
1479         .read           = generic_read_dir,
1480         .readdir        = proc_readfd,
1481 };
1482
1483 /*
1484  * proc directories can do almost nothing..
1485  */
1486 static struct inode_operations proc_fd_inode_operations = {
1487         .lookup         = proc_lookupfd,
1488         .setattr        = proc_setattr,
1489 };
1490
1491 static struct dentry *proc_pident_instantiate(struct inode *dir,
1492         struct dentry *dentry, struct task_struct *task, void *ptr)
1493 {
1494         struct pid_entry *p = ptr;
1495         struct inode *inode;
1496         struct proc_inode *ei;
1497         struct dentry *error = ERR_PTR(-EINVAL);
1498
1499         inode = proc_pid_make_inode(dir->i_sb, task);
1500         if (!inode)
1501                 goto out;
1502
1503         ei = PROC_I(inode);
1504         inode->i_mode = p->mode;
1505         if (S_ISDIR(inode->i_mode))
1506                 inode->i_nlink = 2;     /* Use getattr to fix if necessary */
1507         if (p->iop)
1508                 inode->i_op = p->iop;
1509         if (p->fop)
1510                 inode->i_fop = p->fop;
1511         ei->op = p->op;
1512         dentry->d_op = &pid_dentry_operations;
1513         d_add(dentry, inode);
1514         /* Close the race of the process dying before we return the dentry */
1515         if (pid_revalidate(dentry, NULL))
1516                 error = NULL;
1517 out:
1518         return error;
1519 }
1520
1521 static struct dentry *proc_pident_lookup(struct inode *dir, 
1522                                          struct dentry *dentry,
1523                                          struct pid_entry *ents,
1524                                          unsigned int nents)
1525 {
1526         struct inode *inode;
1527         struct dentry *error;
1528         struct task_struct *task = get_proc_task(dir);
1529         struct pid_entry *p, *last;
1530
1531         error = ERR_PTR(-ENOENT);
1532         inode = NULL;
1533
1534         if (!task)
1535                 goto out_no_task;
1536
1537         /* TODO: maybe we can come up with a generic approach? */
1538         if (task_vx_flags(task, VXF_HIDE_VINFO, 0) &&
1539                 (dentry->d_name.len == 5) &&
1540                 (!memcmp(dentry->d_name.name, "vinfo", 5) ||
1541                 !memcmp(dentry->d_name.name, "ninfo", 5)))
1542                 goto out;
1543
1544         /*
1545          * Yes, it does not scale. And it should not. Don't add
1546          * new entries into /proc/<tgid>/ without very good reasons.
1547          */
1548         last = &ents[nents - 1];
1549         for (p = ents; p <= last; p++) {
1550                 if (p->len != dentry->d_name.len)
1551                         continue;
1552                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1553                         break;
1554         }
1555         if (p > last)
1556                 goto out;
1557
1558         error = proc_pident_instantiate(dir, dentry, task, p);
1559 out:
1560         put_task_struct(task);
1561 out_no_task:
1562         return error;
1563 }
1564
1565 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1566         struct task_struct *task, struct pid_entry *p)
1567 {
1568         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1569                                 proc_pident_instantiate, task, p);
1570 }
1571
1572 static int proc_pident_readdir(struct file *filp,
1573                 void *dirent, filldir_t filldir,
1574                 struct pid_entry *ents, unsigned int nents)
1575 {
1576         int i;
1577         int pid;
1578         struct dentry *dentry = filp->f_path.dentry;
1579         struct inode *inode = dentry->d_inode;
1580         struct task_struct *task = get_proc_task(inode);
1581         struct pid_entry *p, *last;
1582         ino_t ino;
1583         int ret;
1584
1585         ret = -ENOENT;
1586         if (!task)
1587                 goto out_no_task;
1588
1589         ret = 0;
1590         pid = task->pid;
1591         i = filp->f_pos;
1592         switch (i) {
1593         case 0:
1594                 ino = inode->i_ino;
1595                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1596                         goto out;
1597                 i++;
1598                 filp->f_pos++;
1599                 /* fall through */
1600         case 1:
1601                 ino = parent_ino(dentry);
1602                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1603                         goto out;
1604                 i++;
1605                 filp->f_pos++;
1606                 /* fall through */
1607         default:
1608                 i -= 2;
1609                 if (i >= nents) {
1610                         ret = 1;
1611                         goto out;
1612                 }
1613                 p = ents + i;
1614                 last = &ents[nents - 1];
1615                 while (p <= last) {
1616                         if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1617                                 goto out;
1618                         filp->f_pos++;
1619                         p++;
1620                 }
1621         }
1622
1623         ret = 1;
1624 out:
1625         put_task_struct(task);
1626 out_no_task:
1627         return ret;
1628 }
1629
1630 #ifdef CONFIG_SECURITY
1631 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1632                                   size_t count, loff_t *ppos)
1633 {
1634         struct inode * inode = file->f_path.dentry->d_inode;
1635         unsigned long page;
1636         ssize_t length;
1637         struct task_struct *task = get_proc_task(inode);
1638
1639         length = -ESRCH;
1640         if (!task)
1641                 goto out_no_task;
1642
1643         if (count > PAGE_SIZE)
1644                 count = PAGE_SIZE;
1645         length = -ENOMEM;
1646         if (!(page = __get_free_page(GFP_KERNEL)))
1647                 goto out;
1648
1649         length = security_getprocattr(task,
1650                                       (char*)file->f_path.dentry->d_name.name,
1651                                       (void*)page, count);
1652         if (length >= 0)
1653                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1654         free_page(page);
1655 out:
1656         put_task_struct(task);
1657 out_no_task:
1658         return length;
1659 }
1660
1661 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1662                                    size_t count, loff_t *ppos)
1663 {
1664         struct inode * inode = file->f_path.dentry->d_inode;
1665         char *page;
1666         ssize_t length;
1667         struct task_struct *task = get_proc_task(inode);
1668
1669         length = -ESRCH;
1670         if (!task)
1671                 goto out_no_task;
1672         if (count > PAGE_SIZE)
1673                 count = PAGE_SIZE;
1674
1675         /* No partial writes. */
1676         length = -EINVAL;
1677         if (*ppos != 0)
1678                 goto out;
1679
1680         length = -ENOMEM;
1681         page = (char*)__get_free_page(GFP_USER);
1682         if (!page)
1683                 goto out;
1684
1685         length = -EFAULT;
1686         if (copy_from_user(page, buf, count))
1687                 goto out_free;
1688
1689         length = security_setprocattr(task,
1690                                       (char*)file->f_path.dentry->d_name.name,
1691                                       (void*)page, count);
1692 out_free:
1693         free_page((unsigned long) page);
1694 out:
1695         put_task_struct(task);
1696 out_no_task:
1697         return length;
1698 }
1699
1700 static struct file_operations proc_pid_attr_operations = {
1701         .read           = proc_pid_attr_read,
1702         .write          = proc_pid_attr_write,
1703 };
1704
1705 static struct pid_entry attr_dir_stuff[] = {
1706         REG("current",    S_IRUGO|S_IWUGO, pid_attr),
1707         REG("prev",       S_IRUGO,         pid_attr),
1708         REG("exec",       S_IRUGO|S_IWUGO, pid_attr),
1709         REG("fscreate",   S_IRUGO|S_IWUGO, pid_attr),
1710         REG("keycreate",  S_IRUGO|S_IWUGO, pid_attr),
1711         REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1712 };
1713
1714 static int proc_attr_dir_readdir(struct file * filp,
1715                              void * dirent, filldir_t filldir)
1716 {
1717         return proc_pident_readdir(filp,dirent,filldir,
1718                                    attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1719 }
1720
1721 static struct file_operations proc_attr_dir_operations = {
1722         .read           = generic_read_dir,
1723         .readdir        = proc_attr_dir_readdir,
1724 };
1725
1726 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1727                                 struct dentry *dentry, struct nameidata *nd)
1728 {
1729         return proc_pident_lookup(dir, dentry,
1730                                   attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1731 }
1732
1733 static struct inode_operations proc_attr_dir_inode_operations = {
1734         .lookup         = proc_attr_dir_lookup,
1735         .getattr        = pid_getattr,
1736         .setattr        = proc_setattr,
1737 };
1738
1739 #endif
1740
1741 /*
1742  * /proc/self:
1743  */
1744 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1745                               int buflen)
1746 {
1747         char tmp[PROC_NUMBUF];
1748         sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1749         return vfs_readlink(dentry,buffer,buflen,tmp);
1750 }
1751
1752 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1753 {
1754         char tmp[PROC_NUMBUF];
1755         sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1756         return ERR_PTR(vfs_follow_link(nd,tmp));
1757 }
1758
1759 static struct inode_operations proc_self_inode_operations = {
1760         .readlink       = proc_self_readlink,
1761         .follow_link    = proc_self_follow_link,
1762 };
1763
1764 /*
1765  * proc base
1766  *
1767  * These are the directory entries in the root directory of /proc
1768  * that properly belong to the /proc filesystem, as they describe
1769  * describe something that is process related.
1770  */
1771 static struct pid_entry proc_base_stuff[] = {
1772         NOD("self", S_IFLNK|S_IRWXUGO,
1773                 &proc_self_inode_operations, NULL, {}),
1774 };
1775
1776 /*
1777  *      Exceptional case: normally we are not allowed to unhash a busy
1778  * directory. In this case, however, we can do it - no aliasing problems
1779  * due to the way we treat inodes.
1780  */
1781 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1782 {
1783         struct inode *inode = dentry->d_inode;
1784         struct task_struct *task = get_proc_task(inode);
1785         if (task) {
1786                 put_task_struct(task);
1787                 return 1;
1788         }
1789         d_drop(dentry);
1790         return 0;
1791 }
1792
1793 static struct dentry_operations proc_base_dentry_operations =
1794 {
1795         .d_revalidate   = proc_base_revalidate,
1796         .d_delete       = pid_delete_dentry,
1797 };
1798
1799 static struct dentry *proc_base_instantiate(struct inode *dir,
1800         struct dentry *dentry, struct task_struct *task, void *ptr)
1801 {
1802         struct pid_entry *p = ptr;
1803         struct inode *inode;
1804         struct proc_inode *ei;
1805         struct dentry *error = ERR_PTR(-EINVAL);
1806
1807         /* Allocate the inode */
1808         error = ERR_PTR(-ENOMEM);
1809         inode = new_inode(dir->i_sb);
1810         if (!inode)
1811                 goto out;
1812
1813         /* Initialize the inode */
1814         ei = PROC_I(inode);
1815         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1816
1817         /*
1818          * grab the reference to the task.
1819          */
1820         ei->pid = get_task_pid(task, PIDTYPE_PID);
1821         if (!ei->pid)
1822                 goto out_iput;
1823
1824         inode->i_uid = 0;
1825         inode->i_gid = 0;
1826         inode->i_mode = p->mode;
1827         if (S_ISDIR(inode->i_mode))
1828                 inode->i_nlink = 2;
1829         if (S_ISLNK(inode->i_mode))
1830                 inode->i_size = 64;
1831         if (p->iop)
1832                 inode->i_op = p->iop;
1833         if (p->fop)
1834                 inode->i_fop = p->fop;
1835         ei->op = p->op;
1836         dentry->d_op = &proc_base_dentry_operations;
1837         d_add(dentry, inode);
1838         error = NULL;
1839 out:
1840         return error;
1841 out_iput:
1842         iput(inode);
1843         goto out;
1844 }
1845
1846 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1847 {
1848         struct dentry *error;
1849         struct task_struct *task = get_proc_task_real(dir);
1850         struct pid_entry *p, *last;
1851
1852         error = ERR_PTR(-ENOENT);
1853
1854         if (!task)
1855                 goto out_no_task;
1856
1857         /* Lookup the directory entry */
1858         last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1859         for (p = proc_base_stuff; p <= last; p++) {
1860                 if (p->len != dentry->d_name.len)
1861                         continue;
1862                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1863                         break;
1864         }
1865         if (p > last)
1866                 goto out;
1867
1868         error = proc_base_instantiate(dir, dentry, task, p);
1869
1870 out:
1871         put_task_struct(task);
1872 out_no_task:
1873         return error;
1874 }
1875
1876 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1877         struct task_struct *task, struct pid_entry *p)
1878 {
1879         return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1880                                 proc_base_instantiate, task, p);
1881 }
1882
1883 #ifdef CONFIG_TASK_IO_ACCOUNTING
1884 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1885 {
1886         return sprintf(buffer,
1887                         "rchar: %llu\n"
1888                         "wchar: %llu\n"
1889                         "syscr: %llu\n"
1890                         "syscw: %llu\n"
1891                         "read_bytes: %llu\n"
1892                         "write_bytes: %llu\n"
1893                         "cancelled_write_bytes: %llu\n",
1894                         (unsigned long long)task->rchar,
1895                         (unsigned long long)task->wchar,
1896                         (unsigned long long)task->syscr,
1897                         (unsigned long long)task->syscw,
1898                         (unsigned long long)task->ioac.read_bytes,
1899                         (unsigned long long)task->ioac.write_bytes,
1900                         (unsigned long long)task->ioac.cancelled_write_bytes);
1901 }
1902 #endif
1903
1904 /*
1905  * Thread groups
1906  */
1907 static struct file_operations proc_task_operations;
1908 static struct inode_operations proc_task_inode_operations;
1909
1910 extern int proc_pid_vx_info(struct task_struct *, char *);
1911 extern int proc_pid_nx_info(struct task_struct *, char *);
1912
1913 static struct pid_entry tgid_base_stuff[] = {
1914         DIR("task",       S_IRUGO|S_IXUGO, task),
1915         DIR("fd",         S_IRUSR|S_IXUSR, fd),
1916         INF("environ",    S_IRUSR, pid_environ),
1917         INF("auxv",       S_IRUSR, pid_auxv),
1918         INF("status",     S_IRUGO, pid_status),
1919         INF("cmdline",    S_IRUGO, pid_cmdline),
1920         INF("stat",       S_IRUGO, tgid_stat),
1921         INF("statm",      S_IRUGO, pid_statm),
1922         REG("maps",       S_IRUSR, maps),
1923 #ifdef CONFIG_NUMA
1924         REG("numa_maps",  S_IRUGO, numa_maps),
1925 #endif
1926         REG("mem",        S_IRUSR|S_IWUSR, mem),
1927 #ifdef CONFIG_SECCOMP
1928         REG("seccomp",    S_IRUSR|S_IWUSR, seccomp),
1929 #endif
1930         LNK("cwd",        cwd),
1931         LNK("root",       root),
1932         LNK("exe",        exe),
1933         REG("mounts",     S_IRUGO, mounts),
1934         REG("mountstats", S_IRUSR, mountstats),
1935 #ifdef CONFIG_MMU
1936         REG("smaps",      S_IRUSR, smaps),
1937 #endif
1938 #ifdef CONFIG_SECURITY
1939         DIR("attr",       S_IRUGO|S_IXUGO, attr_dir),
1940 #endif
1941 #ifdef CONFIG_KALLSYMS
1942         INF("wchan",      S_IRUGO, pid_wchan),
1943 #endif
1944 #ifdef CONFIG_SCHEDSTATS
1945         INF("schedstat",  S_IRUGO, pid_schedstat),
1946 #endif
1947 #ifdef CONFIG_CPUSETS
1948         REG("cpuset",     S_IRUGO, cpuset),
1949 #endif
1950         INF("vinfo",      S_IRUGO, pid_vx_info),
1951         INF("ninfo",      S_IRUGO, pid_nx_info),
1952         INF("oom_score",  S_IRUGO, oom_score),
1953         REG("oom_adj",    S_IRUGO|S_IWUSR, oom_adjust),
1954 #ifdef CONFIG_AUDITSYSCALL
1955         REG("loginuid",   S_IWUSR|S_IRUGO, loginuid),
1956 #endif
1957 #ifdef CONFIG_FAULT_INJECTION
1958         REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
1959 #endif
1960 #ifdef CONFIG_TASK_IO_ACCOUNTING
1961         INF("io",       S_IRUGO, pid_io_accounting),
1962 #endif
1963 };
1964
1965 static int proc_tgid_base_readdir(struct file * filp,
1966                              void * dirent, filldir_t filldir)
1967 {
1968         return proc_pident_readdir(filp,dirent,filldir,
1969                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1970 }
1971
1972 static struct file_operations proc_tgid_base_operations = {
1973         .read           = generic_read_dir,
1974         .readdir        = proc_tgid_base_readdir,
1975 };
1976
1977 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1978         return proc_pident_lookup(dir, dentry,
1979                                   tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1980 }
1981
1982 static struct inode_operations proc_tgid_base_inode_operations = {
1983         .lookup         = proc_tgid_base_lookup,
1984         .getattr        = pid_getattr,
1985         .setattr        = proc_setattr,
1986 };
1987
1988 /**
1989  * proc_flush_task -  Remove dcache entries for @task from the /proc dcache.
1990  *
1991  * @task: task that should be flushed.
1992  *
1993  * Looks in the dcache for
1994  * /proc/@pid
1995  * /proc/@tgid/task/@pid
1996  * if either directory is present flushes it and all of it'ts children
1997  * from the dcache.
1998  *
1999  * It is safe and reasonable to cache /proc entries for a task until
2000  * that task exits.  After that they just clog up the dcache with
2001  * useless entries, possibly causing useful dcache entries to be
2002  * flushed instead.  This routine is proved to flush those useless
2003  * dcache entries at process exit time.
2004  *
2005  * NOTE: This routine is just an optimization so it does not guarantee
2006  *       that no dcache entries will exist at process exit time it
2007  *       just makes it very unlikely that any will persist.
2008  */
2009 void proc_flush_task(struct task_struct *task)
2010 {
2011         struct dentry *dentry, *leader, *dir;
2012         char buf[PROC_NUMBUF];
2013         struct qstr name;
2014
2015         name.name = buf;
2016         name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2017         dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2018         if (dentry) {
2019                 shrink_dcache_parent(dentry);
2020                 d_drop(dentry);
2021                 dput(dentry);
2022         }
2023
2024         if (thread_group_leader(task))
2025                 goto out;
2026
2027         name.name = buf;
2028         name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2029         leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2030         if (!leader)
2031                 goto out;
2032
2033         name.name = "task";
2034         name.len = strlen(name.name);
2035         dir = d_hash_and_lookup(leader, &name);
2036         if (!dir)
2037                 goto out_put_leader;
2038
2039         name.name = buf;
2040         name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2041         dentry = d_hash_and_lookup(dir, &name);
2042         if (dentry) {
2043                 shrink_dcache_parent(dentry);
2044                 d_drop(dentry);
2045                 dput(dentry);
2046         }
2047
2048         dput(dir);
2049 out_put_leader:
2050         dput(leader);
2051 out:
2052         return;
2053 }
2054
2055 static struct dentry *proc_pid_instantiate(struct inode *dir,
2056                                            struct dentry * dentry,
2057                                            struct task_struct *task, void *ptr)
2058 {
2059         struct dentry *error = ERR_PTR(-ENOENT);
2060         struct inode *inode;
2061
2062         inode = proc_pid_make_inode(dir->i_sb, task);
2063         if (!inode)
2064                 goto out;
2065
2066         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2067         inode->i_op = &proc_tgid_base_inode_operations;
2068         inode->i_fop = &proc_tgid_base_operations;
2069         inode->i_flags|=S_IMMUTABLE;
2070         inode->i_nlink = 4;
2071 #ifdef CONFIG_SECURITY
2072         inode->i_nlink += 1;
2073 #endif
2074
2075         dentry->d_op = &pid_dentry_operations;
2076
2077         d_add(dentry, inode);
2078         /* Close the race of the process dying before we return the dentry */
2079         if (pid_revalidate(dentry, NULL))
2080                 error = NULL;
2081 out:
2082         return error;
2083 }
2084
2085 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2086 {
2087         struct dentry *result = ERR_PTR(-ENOENT);
2088         struct task_struct *task;
2089         unsigned tgid;
2090
2091         result = proc_base_lookup(dir, dentry);
2092         if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2093                 goto out;
2094
2095         tgid = name_to_int(dentry);
2096         if (tgid == ~0U)
2097                 goto out;
2098
2099         rcu_read_lock();
2100         task = vx_find_proc_task_by_pid(tgid);
2101         if (task)
2102                 get_task_struct(task);
2103         rcu_read_unlock();
2104         if (!task)
2105                 goto out;
2106
2107         result = proc_pid_instantiate(dir, dentry, task, NULL);
2108         put_task_struct(task);
2109 out:
2110         return result;
2111 }
2112
2113 /*
2114  * Find the first task with tgid >= tgid
2115  *
2116  */
2117 static struct task_struct *next_tgid(unsigned int tgid)
2118 {
2119         struct task_struct *task;
2120         struct pid *pid;
2121
2122         rcu_read_lock();
2123 retry:
2124         task = NULL;
2125         pid = find_ge_pid(tgid);
2126         if (pid) {
2127                 tgid = pid->nr + 1;
2128                 task = pid_task(pid, PIDTYPE_PID);
2129                 /* What we to know is if the pid we have find is the
2130                  * pid of a thread_group_leader.  Testing for task
2131                  * being a thread_group_leader is the obvious thing
2132                  * todo but there is a window when it fails, due to
2133                  * the pid transfer logic in de_thread.
2134                  *
2135                  * So we perform the straight forward test of seeing
2136                  * if the pid we have found is the pid of a thread
2137                  * group leader, and don't worry if the task we have
2138                  * found doesn't happen to be a thread group leader.
2139                  * As we don't care in the case of readdir.
2140                  */
2141                 if (!task || !has_group_leader_pid(task))
2142                         goto retry;
2143                 get_task_struct(task);
2144         }
2145         rcu_read_unlock();
2146         return task;
2147 }
2148
2149 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2150
2151 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2152         struct task_struct *task, int tgid)
2153 {
2154         char name[PROC_NUMBUF];
2155         int len = snprintf(name, sizeof(name), "%d", tgid);
2156         return proc_fill_cache(filp, dirent, filldir, name, len,
2157                                 proc_pid_instantiate, task, NULL);
2158 }
2159
2160 /* for the /proc/ directory itself, after non-process stuff has been done */
2161 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2162 {
2163         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2164         struct task_struct *reaper = get_proc_task_real(filp->f_path.dentry->d_inode);
2165         struct task_struct *task;
2166         int tgid;
2167
2168         if (!reaper)
2169                 goto out_no_task;
2170
2171         for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2172                 struct pid_entry *p = &proc_base_stuff[nr];
2173                 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2174                         goto out;
2175         }
2176
2177         tgid = filp->f_pos - TGID_OFFSET;
2178         for (task = next_tgid(tgid);
2179              task;
2180              put_task_struct(task), task = next_tgid(tgid + 1)) {
2181                 tgid = task->pid;
2182                 filp->f_pos = tgid + TGID_OFFSET;
2183                 if (!vx_proc_task_visible(task))
2184                         continue;
2185                 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2186                         put_task_struct(task);
2187                         goto out;
2188                 }
2189         }
2190         filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2191 out:
2192         put_task_struct(reaper);
2193 out_no_task:
2194         return 0;
2195 }
2196
2197 /*
2198  * Tasks
2199  */
2200 static struct pid_entry tid_base_stuff[] = {
2201         DIR("fd",        S_IRUSR|S_IXUSR, fd),
2202         INF("environ",   S_IRUSR, pid_environ),
2203         INF("auxv",      S_IRUSR, pid_auxv),
2204         INF("status",    S_IRUGO, pid_status),
2205         INF("cmdline",   S_IRUGO, pid_cmdline),
2206         INF("stat",      S_IRUGO, tid_stat),
2207         INF("statm",     S_IRUGO, pid_statm),
2208         REG("maps",      S_IRUSR, maps),
2209 #ifdef CONFIG_NUMA
2210         REG("numa_maps", S_IRUGO, numa_maps),
2211 #endif
2212         REG("mem",       S_IRUSR|S_IWUSR, mem),
2213 #ifdef CONFIG_SECCOMP
2214         REG("seccomp",   S_IRUSR|S_IWUSR, seccomp),
2215 #endif
2216         LNK("cwd",       cwd),
2217         LNK("root",      root),
2218         LNK("exe",       exe),
2219         REG("mounts",    S_IRUGO, mounts),
2220 #ifdef CONFIG_MMU
2221         REG("smaps",     S_IRUSR, smaps),
2222 #endif
2223 #ifdef CONFIG_SECURITY
2224         DIR("attr",      S_IRUGO|S_IXUGO, attr_dir),
2225 #endif
2226 #ifdef CONFIG_KALLSYMS
2227         INF("wchan",     S_IRUGO, pid_wchan),
2228 #endif
2229 #ifdef CONFIG_SCHEDSTATS
2230         INF("schedstat", S_IRUGO, pid_schedstat),
2231 #endif
2232 #ifdef CONFIG_CPUSETS
2233         REG("cpuset",    S_IRUGO, cpuset),
2234 #endif
2235         INF("oom_score", S_IRUGO, oom_score),
2236         REG("oom_adj",   S_IRUGO|S_IWUSR, oom_adjust),
2237 #ifdef CONFIG_AUDITSYSCALL
2238         REG("loginuid",  S_IWUSR|S_IRUGO, loginuid),
2239 #endif
2240 #ifdef CONFIG_FAULT_INJECTION
2241         REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2242 #endif
2243 };
2244
2245 static int proc_tid_base_readdir(struct file * filp,
2246                              void * dirent, filldir_t filldir)
2247 {
2248         return proc_pident_readdir(filp,dirent,filldir,
2249                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2250 }
2251
2252 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2253         return proc_pident_lookup(dir, dentry,
2254                                   tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2255 }
2256
2257 static struct file_operations proc_tid_base_operations = {
2258         .read           = generic_read_dir,
2259         .readdir        = proc_tid_base_readdir,
2260 };
2261
2262 static struct inode_operations proc_tid_base_inode_operations = {
2263         .lookup         = proc_tid_base_lookup,
2264         .getattr        = pid_getattr,
2265         .setattr        = proc_setattr,
2266 };
2267
2268 static struct dentry *proc_task_instantiate(struct inode *dir,
2269         struct dentry *dentry, struct task_struct *task, void *ptr)
2270 {
2271         struct dentry *error = ERR_PTR(-ENOENT);
2272         struct inode *inode;
2273         inode = proc_pid_make_inode(dir->i_sb, task);
2274
2275         if (!inode)
2276                 goto out;
2277         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2278         inode->i_op = &proc_tid_base_inode_operations;
2279         inode->i_fop = &proc_tid_base_operations;
2280         inode->i_flags|=S_IMMUTABLE;
2281         inode->i_nlink = 3;
2282 #ifdef CONFIG_SECURITY
2283         inode->i_nlink += 1;
2284 #endif
2285
2286         dentry->d_op = &pid_dentry_operations;
2287
2288         d_add(dentry, inode);
2289         /* Close the race of the process dying before we return the dentry */
2290         if (pid_revalidate(dentry, NULL))
2291                 error = NULL;
2292 out:
2293         return error;
2294 }
2295
2296 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2297 {
2298         struct dentry *result = ERR_PTR(-ENOENT);
2299         struct task_struct *task;
2300         struct task_struct *leader = get_proc_task(dir);
2301         unsigned tid;
2302
2303         if (!leader)
2304                 goto out_no_task;
2305
2306         tid = name_to_int(dentry);
2307         if (tid == ~0U)
2308                 goto out;
2309         if (vx_current_initpid(tid))
2310                 goto out;
2311
2312         rcu_read_lock();
2313         task = vx_find_proc_task_by_pid(tid);
2314         if (task)
2315                 get_task_struct(task);
2316         rcu_read_unlock();
2317         if (!task)
2318                 goto out;
2319         if (leader->tgid != task->tgid)
2320                 goto out_drop_task;
2321
2322         result = proc_task_instantiate(dir, dentry, task, NULL);
2323 out_drop_task:
2324         put_task_struct(task);
2325 out:
2326         put_task_struct(leader);
2327 out_no_task:
2328         return result;
2329 }
2330
2331 /*
2332  * Find the first tid of a thread group to return to user space.
2333  *
2334  * Usually this is just the thread group leader, but if the users
2335  * buffer was too small or there was a seek into the middle of the
2336  * directory we have more work todo.
2337  *
2338  * In the case of a short read we start with find_task_by_pid.
2339  *
2340  * In the case of a seek we start with the leader and walk nr
2341  * threads past it.
2342  */
2343 static struct task_struct *first_tid(struct task_struct *leader,
2344                                         int tid, int nr)
2345 {
2346         struct task_struct *pos;
2347
2348         rcu_read_lock();
2349         /* Attempt to start with the pid of a thread */
2350         if (tid && (nr > 0)) {
2351                 pos = find_task_by_pid(tid);
2352                 if (pos && (pos->group_leader == leader))
2353                         goto found;
2354         }
2355
2356         /* If nr exceeds the number of threads there is nothing todo */
2357         pos = NULL;
2358         if (nr && nr >= get_nr_threads(leader))
2359                 goto out;
2360
2361         /* If we haven't found our starting place yet start
2362          * with the leader and walk nr threads forward.
2363          */
2364         for (pos = leader; nr > 0; --nr) {
2365                 pos = next_thread(pos);
2366                 if (pos == leader) {
2367                         pos = NULL;
2368                         goto out;
2369                 }
2370         }
2371 found:
2372         get_task_struct(pos);
2373 out:
2374         rcu_read_unlock();
2375         return pos;
2376 }
2377
2378 /*
2379  * Find the next thread in the thread list.
2380  * Return NULL if there is an error or no next thread.
2381  *
2382  * The reference to the input task_struct is released.
2383  */
2384 static struct task_struct *next_tid(struct task_struct *start)
2385 {
2386         struct task_struct *pos = NULL;
2387         rcu_read_lock();
2388         if (pid_alive(start)) {
2389                 pos = next_thread(start);
2390                 if (thread_group_leader(pos))
2391                         pos = NULL;
2392                 else
2393                         get_task_struct(pos);
2394         }
2395         rcu_read_unlock();
2396         put_task_struct(start);
2397         return pos;
2398 }
2399
2400 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2401         struct task_struct *task, int tid)
2402 {
2403         char name[PROC_NUMBUF];
2404         int len = snprintf(name, sizeof(name), "%d", tid);
2405         return proc_fill_cache(filp, dirent, filldir, name, len,
2406                                 proc_task_instantiate, task, NULL);
2407 }
2408
2409 /* for the /proc/TGID/task/ directories */
2410 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2411 {
2412         struct dentry *dentry = filp->f_path.dentry;
2413         struct inode *inode = dentry->d_inode;
2414         struct task_struct *leader = NULL;
2415         struct task_struct *task;
2416         int retval = -ENOENT;
2417         ino_t ino;
2418         int tid;
2419         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2420
2421         task = get_proc_task(inode);
2422         if (!task)
2423                 goto out_no_task;
2424         rcu_read_lock();
2425         if (pid_alive(task)) {
2426                 leader = task->group_leader;
2427                 get_task_struct(leader);
2428         }
2429         rcu_read_unlock();
2430         put_task_struct(task);
2431         if (!leader)
2432                 goto out_no_task;
2433         retval = 0;
2434
2435         switch (pos) {
2436         case 0:
2437                 ino = inode->i_ino;
2438                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2439                         goto out;
2440                 pos++;
2441                 /* fall through */
2442         case 1:
2443                 ino = parent_ino(dentry);
2444                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2445                         goto out;
2446                 pos++;
2447                 /* fall through */
2448         }
2449
2450         /* f_version caches the tgid value that the last readdir call couldn't
2451          * return. lseek aka telldir automagically resets f_version to 0.
2452          */
2453         tid = filp->f_version;
2454         filp->f_version = 0;
2455         for (task = first_tid(leader, tid, pos - 2);
2456              task;
2457              task = next_tid(task), pos++) {
2458                 tid = vx_map_pid(task->pid);
2459                 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2460                         /* returning this tgid failed, save it as the first
2461                          * pid for the next readir call */
2462                         filp->f_version = tid;
2463                         put_task_struct(task);
2464                         break;
2465                 }
2466         }
2467 out:
2468         filp->f_pos = pos;
2469         put_task_struct(leader);
2470 out_no_task:
2471         return retval;
2472 }
2473
2474 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2475 {
2476         struct inode *inode = dentry->d_inode;
2477         struct task_struct *p = get_proc_task(inode);
2478         generic_fillattr(inode, stat);
2479
2480         if (p) {
2481                 rcu_read_lock();
2482                 stat->nlink += get_nr_threads(p);
2483                 rcu_read_unlock();
2484                 put_task_struct(p);
2485         }
2486
2487         return 0;
2488 }
2489
2490 static struct inode_operations proc_task_inode_operations = {
2491         .lookup         = proc_task_lookup,
2492         .getattr        = proc_task_getattr,
2493         .setattr        = proc_setattr,
2494 };
2495
2496 static struct file_operations proc_task_operations = {
2497         .read           = generic_read_dir,
2498         .readdir        = proc_task_readdir,
2499 };