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