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