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