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