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