]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/proc/base.c
[PATCH] autofs4: expire mounts that hold no (extra) references only
[linux-2.6-omap-h63xx.git] / 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 static int proc_check_chroot(struct dentry *root, struct vfsmount *vfsmnt)
539 {
540         struct dentry *de, *base;
541         struct vfsmount *our_vfsmnt, *mnt;
542         int res = 0;
543         read_lock(&current->fs->lock);
544         our_vfsmnt = mntget(current->fs->rootmnt);
545         base = dget(current->fs->root);
546         read_unlock(&current->fs->lock);
547
548         spin_lock(&vfsmount_lock);
549         de = root;
550         mnt = vfsmnt;
551
552         while (vfsmnt != our_vfsmnt) {
553                 if (vfsmnt == vfsmnt->mnt_parent)
554                         goto out;
555                 de = vfsmnt->mnt_mountpoint;
556                 vfsmnt = vfsmnt->mnt_parent;
557         }
558
559         if (!is_subdir(de, base))
560                 goto out;
561         spin_unlock(&vfsmount_lock);
562
563 exit:
564         dput(base);
565         mntput(our_vfsmnt);
566         dput(root);
567         mntput(mnt);
568         return res;
569 out:
570         spin_unlock(&vfsmount_lock);
571         res = -EACCES;
572         goto exit;
573 }
574
575 static int proc_check_root(struct inode *inode)
576 {
577         struct dentry *root;
578         struct vfsmount *vfsmnt;
579
580         if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
581                 return -ENOENT;
582         return proc_check_chroot(root, vfsmnt);
583 }
584
585 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
586 {
587         if (generic_permission(inode, mask, NULL) != 0)
588                 return -EACCES;
589         return proc_check_root(inode);
590 }
591
592 static int proc_task_permission(struct inode *inode, int mask, struct nameidata *nd)
593 {
594         struct dentry *root;
595         struct vfsmount *vfsmnt;
596
597         if (generic_permission(inode, mask, NULL) != 0)
598                 return -EACCES;
599
600         if (proc_task_root_link(inode, &root, &vfsmnt))
601                 return -ENOENT;
602
603         return proc_check_chroot(root, vfsmnt);
604 }
605
606 extern struct seq_operations proc_pid_maps_op;
607 static int maps_open(struct inode *inode, struct file *file)
608 {
609         struct task_struct *task = proc_task(inode);
610         int ret = seq_open(file, &proc_pid_maps_op);
611         if (!ret) {
612                 struct seq_file *m = file->private_data;
613                 m->private = task;
614         }
615         return ret;
616 }
617
618 static struct file_operations proc_maps_operations = {
619         .open           = maps_open,
620         .read           = seq_read,
621         .llseek         = seq_lseek,
622         .release        = seq_release,
623 };
624
625 #ifdef CONFIG_NUMA
626 extern struct seq_operations proc_pid_numa_maps_op;
627 static int numa_maps_open(struct inode *inode, struct file *file)
628 {
629         struct task_struct *task = proc_task(inode);
630         int ret = seq_open(file, &proc_pid_numa_maps_op);
631         if (!ret) {
632                 struct seq_file *m = file->private_data;
633                 m->private = task;
634         }
635         return ret;
636 }
637
638 static struct file_operations proc_numa_maps_operations = {
639         .open           = numa_maps_open,
640         .read           = seq_read,
641         .llseek         = seq_lseek,
642         .release        = seq_release,
643 };
644 #endif
645
646 #ifdef CONFIG_MMU
647 extern struct seq_operations proc_pid_smaps_op;
648 static int smaps_open(struct inode *inode, struct file *file)
649 {
650         struct task_struct *task = proc_task(inode);
651         int ret = seq_open(file, &proc_pid_smaps_op);
652         if (!ret) {
653                 struct seq_file *m = file->private_data;
654                 m->private = task;
655         }
656         return ret;
657 }
658
659 static struct file_operations proc_smaps_operations = {
660         .open           = smaps_open,
661         .read           = seq_read,
662         .llseek         = seq_lseek,
663         .release        = seq_release,
664 };
665 #endif
666
667 extern struct seq_operations mounts_op;
668 struct proc_mounts {
669         struct seq_file m;
670         int event;
671 };
672
673 static int mounts_open(struct inode *inode, struct file *file)
674 {
675         struct task_struct *task = proc_task(inode);
676         struct namespace *namespace;
677         struct proc_mounts *p;
678         int ret = -EINVAL;
679
680         task_lock(task);
681         namespace = task->namespace;
682         if (namespace)
683                 get_namespace(namespace);
684         task_unlock(task);
685
686         if (namespace) {
687                 ret = -ENOMEM;
688                 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
689                 if (p) {
690                         file->private_data = &p->m;
691                         ret = seq_open(file, &mounts_op);
692                         if (!ret) {
693                                 p->m.private = namespace;
694                                 p->event = namespace->event;
695                                 return 0;
696                         }
697                         kfree(p);
698                 }
699                 put_namespace(namespace);
700         }
701         return ret;
702 }
703
704 static int mounts_release(struct inode *inode, struct file *file)
705 {
706         struct seq_file *m = file->private_data;
707         struct namespace *namespace = m->private;
708         put_namespace(namespace);
709         return seq_release(inode, file);
710 }
711
712 static unsigned mounts_poll(struct file *file, poll_table *wait)
713 {
714         struct proc_mounts *p = file->private_data;
715         struct namespace *ns = p->m.private;
716         unsigned res = 0;
717
718         poll_wait(file, &ns->poll, wait);
719
720         spin_lock(&vfsmount_lock);
721         if (p->event != ns->event) {
722                 p->event = ns->event;
723                 res = POLLERR;
724         }
725         spin_unlock(&vfsmount_lock);
726
727         return res;
728 }
729
730 static struct file_operations proc_mounts_operations = {
731         .open           = mounts_open,
732         .read           = seq_read,
733         .llseek         = seq_lseek,
734         .release        = mounts_release,
735         .poll           = mounts_poll,
736 };
737
738 extern struct seq_operations mountstats_op;
739 static int mountstats_open(struct inode *inode, struct file *file)
740 {
741         struct task_struct *task = proc_task(inode);
742         int ret = seq_open(file, &mountstats_op);
743
744         if (!ret) {
745                 struct seq_file *m = file->private_data;
746                 struct namespace *namespace;
747                 task_lock(task);
748                 namespace = task->namespace;
749                 if (namespace)
750                         get_namespace(namespace);
751                 task_unlock(task);
752
753                 if (namespace)
754                         m->private = namespace;
755                 else {
756                         seq_release(inode, file);
757                         ret = -EINVAL;
758                 }
759         }
760         return ret;
761 }
762
763 static struct file_operations proc_mountstats_operations = {
764         .open           = mountstats_open,
765         .read           = seq_read,
766         .llseek         = seq_lseek,
767         .release        = mounts_release,
768 };
769
770 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
771
772 static ssize_t proc_info_read(struct file * file, char __user * buf,
773                           size_t count, loff_t *ppos)
774 {
775         struct inode * inode = file->f_dentry->d_inode;
776         unsigned long page;
777         ssize_t length;
778         struct task_struct *task = proc_task(inode);
779
780         if (count > PROC_BLOCK_SIZE)
781                 count = PROC_BLOCK_SIZE;
782         if (!(page = __get_free_page(GFP_KERNEL)))
783                 return -ENOMEM;
784
785         length = PROC_I(inode)->op.proc_read(task, (char*)page);
786
787         if (length >= 0)
788                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
789         free_page(page);
790         return length;
791 }
792
793 static struct file_operations proc_info_file_operations = {
794         .read           = proc_info_read,
795 };
796
797 static int mem_open(struct inode* inode, struct file* file)
798 {
799         file->private_data = (void*)((long)current->self_exec_id);
800         return 0;
801 }
802
803 static ssize_t mem_read(struct file * file, char __user * buf,
804                         size_t count, loff_t *ppos)
805 {
806         struct task_struct *task = proc_task(file->f_dentry->d_inode);
807         char *page;
808         unsigned long src = *ppos;
809         int ret = -ESRCH;
810         struct mm_struct *mm;
811
812         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
813                 goto out;
814
815         ret = -ENOMEM;
816         page = (char *)__get_free_page(GFP_USER);
817         if (!page)
818                 goto out;
819
820         ret = 0;
821  
822         mm = get_task_mm(task);
823         if (!mm)
824                 goto out_free;
825
826         ret = -EIO;
827  
828         if (file->private_data != (void*)((long)current->self_exec_id))
829                 goto out_put;
830
831         ret = 0;
832  
833         while (count > 0) {
834                 int this_len, retval;
835
836                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
837                 retval = access_process_vm(task, src, page, this_len, 0);
838                 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
839                         if (!ret)
840                                 ret = -EIO;
841                         break;
842                 }
843
844                 if (copy_to_user(buf, page, retval)) {
845                         ret = -EFAULT;
846                         break;
847                 }
848  
849                 ret += retval;
850                 src += retval;
851                 buf += retval;
852                 count -= retval;
853         }
854         *ppos = src;
855
856 out_put:
857         mmput(mm);
858 out_free:
859         free_page((unsigned long) page);
860 out:
861         return ret;
862 }
863
864 #define mem_write NULL
865
866 #ifndef mem_write
867 /* This is a security hazard */
868 static ssize_t mem_write(struct file * file, const char * buf,
869                          size_t count, loff_t *ppos)
870 {
871         int copied = 0;
872         char *page;
873         struct task_struct *task = proc_task(file->f_dentry->d_inode);
874         unsigned long dst = *ppos;
875
876         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
877                 return -ESRCH;
878
879         page = (char *)__get_free_page(GFP_USER);
880         if (!page)
881                 return -ENOMEM;
882
883         while (count > 0) {
884                 int this_len, retval;
885
886                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
887                 if (copy_from_user(page, buf, this_len)) {
888                         copied = -EFAULT;
889                         break;
890                 }
891                 retval = access_process_vm(task, dst, page, this_len, 1);
892                 if (!retval) {
893                         if (!copied)
894                                 copied = -EIO;
895                         break;
896                 }
897                 copied += retval;
898                 buf += retval;
899                 dst += retval;
900                 count -= retval;                        
901         }
902         *ppos = dst;
903         free_page((unsigned long) page);
904         return copied;
905 }
906 #endif
907
908 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
909 {
910         switch (orig) {
911         case 0:
912                 file->f_pos = offset;
913                 break;
914         case 1:
915                 file->f_pos += offset;
916                 break;
917         default:
918                 return -EINVAL;
919         }
920         force_successful_syscall_return();
921         return file->f_pos;
922 }
923
924 static struct file_operations proc_mem_operations = {
925         .llseek         = mem_lseek,
926         .read           = mem_read,
927         .write          = mem_write,
928         .open           = mem_open,
929 };
930
931 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
932                                 size_t count, loff_t *ppos)
933 {
934         struct task_struct *task = proc_task(file->f_dentry->d_inode);
935         char buffer[8];
936         size_t len;
937         int oom_adjust = task->oomkilladj;
938         loff_t __ppos = *ppos;
939
940         len = sprintf(buffer, "%i\n", oom_adjust);
941         if (__ppos >= len)
942                 return 0;
943         if (count > len-__ppos)
944                 count = len-__ppos;
945         if (copy_to_user(buf, buffer + __ppos, count))
946                 return -EFAULT;
947         *ppos = __ppos + count;
948         return count;
949 }
950
951 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
952                                 size_t count, loff_t *ppos)
953 {
954         struct task_struct *task = proc_task(file->f_dentry->d_inode);
955         char buffer[8], *end;
956         int oom_adjust;
957
958         if (!capable(CAP_SYS_RESOURCE))
959                 return -EPERM;
960         memset(buffer, 0, 8);
961         if (count > 6)
962                 count = 6;
963         if (copy_from_user(buffer, buf, count))
964                 return -EFAULT;
965         oom_adjust = simple_strtol(buffer, &end, 0);
966         if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
967                 return -EINVAL;
968         if (*end == '\n')
969                 end++;
970         task->oomkilladj = oom_adjust;
971         if (end - buffer == 0)
972                 return -EIO;
973         return end - buffer;
974 }
975
976 static struct file_operations proc_oom_adjust_operations = {
977         .read           = oom_adjust_read,
978         .write          = oom_adjust_write,
979 };
980
981 static struct inode_operations proc_mem_inode_operations = {
982         .permission     = proc_permission,
983 };
984
985 #ifdef CONFIG_AUDITSYSCALL
986 #define TMPBUFLEN 21
987 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
988                                   size_t count, loff_t *ppos)
989 {
990         struct inode * inode = file->f_dentry->d_inode;
991         struct task_struct *task = proc_task(inode);
992         ssize_t length;
993         char tmpbuf[TMPBUFLEN];
994
995         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
996                                 audit_get_loginuid(task->audit_context));
997         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
998 }
999
1000 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1001                                    size_t count, loff_t *ppos)
1002 {
1003         struct inode * inode = file->f_dentry->d_inode;
1004         char *page, *tmp;
1005         ssize_t length;
1006         struct task_struct *task = proc_task(inode);
1007         uid_t loginuid;
1008
1009         if (!capable(CAP_AUDIT_CONTROL))
1010                 return -EPERM;
1011
1012         if (current != task)
1013                 return -EPERM;
1014
1015         if (count > PAGE_SIZE)
1016                 count = PAGE_SIZE;
1017
1018         if (*ppos != 0) {
1019                 /* No partial writes. */
1020                 return -EINVAL;
1021         }
1022         page = (char*)__get_free_page(GFP_USER);
1023         if (!page)
1024                 return -ENOMEM;
1025         length = -EFAULT;
1026         if (copy_from_user(page, buf, count))
1027                 goto out_free_page;
1028
1029         loginuid = simple_strtoul(page, &tmp, 10);
1030         if (tmp == page) {
1031                 length = -EINVAL;
1032                 goto out_free_page;
1033
1034         }
1035         length = audit_set_loginuid(task, loginuid);
1036         if (likely(length == 0))
1037                 length = count;
1038
1039 out_free_page:
1040         free_page((unsigned long) page);
1041         return length;
1042 }
1043
1044 static struct file_operations proc_loginuid_operations = {
1045         .read           = proc_loginuid_read,
1046         .write          = proc_loginuid_write,
1047 };
1048 #endif
1049
1050 #ifdef CONFIG_SECCOMP
1051 static ssize_t seccomp_read(struct file *file, char __user *buf,
1052                             size_t count, loff_t *ppos)
1053 {
1054         struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1055         char __buf[20];
1056         loff_t __ppos = *ppos;
1057         size_t len;
1058
1059         /* no need to print the trailing zero, so use only len */
1060         len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
1061         if (__ppos >= len)
1062                 return 0;
1063         if (count > len - __ppos)
1064                 count = len - __ppos;
1065         if (copy_to_user(buf, __buf + __ppos, count))
1066                 return -EFAULT;
1067         *ppos = __ppos + count;
1068         return count;
1069 }
1070
1071 static ssize_t seccomp_write(struct file *file, const char __user *buf,
1072                              size_t count, loff_t *ppos)
1073 {
1074         struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
1075         char __buf[20], *end;
1076         unsigned int seccomp_mode;
1077
1078         /* can set it only once to be even more secure */
1079         if (unlikely(tsk->seccomp.mode))
1080                 return -EPERM;
1081
1082         memset(__buf, 0, sizeof(__buf));
1083         count = min(count, sizeof(__buf) - 1);
1084         if (copy_from_user(__buf, buf, count))
1085                 return -EFAULT;
1086         seccomp_mode = simple_strtoul(__buf, &end, 0);
1087         if (*end == '\n')
1088                 end++;
1089         if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
1090                 tsk->seccomp.mode = seccomp_mode;
1091                 set_tsk_thread_flag(tsk, TIF_SECCOMP);
1092         } else
1093                 return -EINVAL;
1094         if (unlikely(!(end - __buf)))
1095                 return -EIO;
1096         return end - __buf;
1097 }
1098
1099 static struct file_operations proc_seccomp_operations = {
1100         .read           = seccomp_read,
1101         .write          = seccomp_write,
1102 };
1103 #endif /* CONFIG_SECCOMP */
1104
1105 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1106 {
1107         struct inode *inode = dentry->d_inode;
1108         int error = -EACCES;
1109
1110         /* We don't need a base pointer in the /proc filesystem */
1111         path_release(nd);
1112
1113         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1114                 goto out;
1115         error = proc_check_root(inode);
1116         if (error)
1117                 goto out;
1118
1119         error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1120         nd->last_type = LAST_BIND;
1121 out:
1122         return ERR_PTR(error);
1123 }
1124
1125 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1126                             char __user *buffer, int buflen)
1127 {
1128         struct inode * inode;
1129         char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1130         int len;
1131
1132         if (!tmp)
1133                 return -ENOMEM;
1134                 
1135         inode = dentry->d_inode;
1136         path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1137         len = PTR_ERR(path);
1138         if (IS_ERR(path))
1139                 goto out;
1140         len = tmp + PAGE_SIZE - 1 - path;
1141
1142         if (len > buflen)
1143                 len = buflen;
1144         if (copy_to_user(buffer, path, len))
1145                 len = -EFAULT;
1146  out:
1147         free_page((unsigned long)tmp);
1148         return len;
1149 }
1150
1151 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1152 {
1153         int error = -EACCES;
1154         struct inode *inode = dentry->d_inode;
1155         struct dentry *de;
1156         struct vfsmount *mnt = NULL;
1157
1158         lock_kernel();
1159
1160         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1161                 goto out;
1162         error = proc_check_root(inode);
1163         if (error)
1164                 goto out;
1165
1166         error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1167         if (error)
1168                 goto out;
1169
1170         error = do_proc_readlink(de, mnt, buffer, buflen);
1171         dput(de);
1172         mntput(mnt);
1173 out:
1174         unlock_kernel();
1175         return error;
1176 }
1177
1178 static struct inode_operations proc_pid_link_inode_operations = {
1179         .readlink       = proc_pid_readlink,
1180         .follow_link    = proc_pid_follow_link
1181 };
1182
1183 #define NUMBUF 10
1184
1185 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1186 {
1187         struct inode *inode = filp->f_dentry->d_inode;
1188         struct task_struct *p = proc_task(inode);
1189         unsigned int fd, tid, ino;
1190         int retval;
1191         char buf[NUMBUF];
1192         struct files_struct * files;
1193         struct fdtable *fdt;
1194
1195         retval = -ENOENT;
1196         if (!pid_alive(p))
1197                 goto out;
1198         retval = 0;
1199         tid = p->pid;
1200
1201         fd = filp->f_pos;
1202         switch (fd) {
1203                 case 0:
1204                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1205                                 goto out;
1206                         filp->f_pos++;
1207                 case 1:
1208                         ino = fake_ino(tid, PROC_TID_INO);
1209                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1210                                 goto out;
1211                         filp->f_pos++;
1212                 default:
1213                         files = get_files_struct(p);
1214                         if (!files)
1215                                 goto out;
1216                         rcu_read_lock();
1217                         fdt = files_fdtable(files);
1218                         for (fd = filp->f_pos-2;
1219                              fd < fdt->max_fds;
1220                              fd++, filp->f_pos++) {
1221                                 unsigned int i,j;
1222
1223                                 if (!fcheck_files(files, fd))
1224                                         continue;
1225                                 rcu_read_unlock();
1226
1227                                 j = NUMBUF;
1228                                 i = fd;
1229                                 do {
1230                                         j--;
1231                                         buf[j] = '0' + (i % 10);
1232                                         i /= 10;
1233                                 } while (i);
1234
1235                                 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1236                                 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1237                                         rcu_read_lock();
1238                                         break;
1239                                 }
1240                                 rcu_read_lock();
1241                         }
1242                         rcu_read_unlock();
1243                         put_files_struct(files);
1244         }
1245 out:
1246         return retval;
1247 }
1248
1249 static int proc_pident_readdir(struct file *filp,
1250                 void *dirent, filldir_t filldir,
1251                 struct pid_entry *ents, unsigned int nents)
1252 {
1253         int i;
1254         int pid;
1255         struct dentry *dentry = filp->f_dentry;
1256         struct inode *inode = dentry->d_inode;
1257         struct pid_entry *p;
1258         ino_t ino;
1259         int ret;
1260
1261         ret = -ENOENT;
1262         if (!pid_alive(proc_task(inode)))
1263                 goto out;
1264
1265         ret = 0;
1266         pid = proc_task(inode)->pid;
1267         i = filp->f_pos;
1268         switch (i) {
1269         case 0:
1270                 ino = inode->i_ino;
1271                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1272                         goto out;
1273                 i++;
1274                 filp->f_pos++;
1275                 /* fall through */
1276         case 1:
1277                 ino = parent_ino(dentry);
1278                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1279                         goto out;
1280                 i++;
1281                 filp->f_pos++;
1282                 /* fall through */
1283         default:
1284                 i -= 2;
1285                 if (i >= nents) {
1286                         ret = 1;
1287                         goto out;
1288                 }
1289                 p = ents + i;
1290                 while (p->name) {
1291                         if (filldir(dirent, p->name, p->len, filp->f_pos,
1292                                     fake_ino(pid, p->type), p->mode >> 12) < 0)
1293                                 goto out;
1294                         filp->f_pos++;
1295                         p++;
1296                 }
1297         }
1298
1299         ret = 1;
1300 out:
1301         return ret;
1302 }
1303
1304 static int proc_tgid_base_readdir(struct file * filp,
1305                              void * dirent, filldir_t filldir)
1306 {
1307         return proc_pident_readdir(filp,dirent,filldir,
1308                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1309 }
1310
1311 static int proc_tid_base_readdir(struct file * filp,
1312                              void * dirent, filldir_t filldir)
1313 {
1314         return proc_pident_readdir(filp,dirent,filldir,
1315                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1316 }
1317
1318 /* building an inode */
1319
1320 static int task_dumpable(struct task_struct *task)
1321 {
1322         int dumpable = 0;
1323         struct mm_struct *mm;
1324
1325         task_lock(task);
1326         mm = task->mm;
1327         if (mm)
1328                 dumpable = mm->dumpable;
1329         task_unlock(task);
1330         if(dumpable == 1)
1331                 return 1;
1332         return 0;
1333 }
1334
1335
1336 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1337 {
1338         struct inode * inode;
1339         struct proc_inode *ei;
1340
1341         /* We need a new inode */
1342         
1343         inode = new_inode(sb);
1344         if (!inode)
1345                 goto out;
1346
1347         /* Common stuff */
1348         ei = PROC_I(inode);
1349         ei->task = NULL;
1350         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1351         inode->i_ino = fake_ino(task->pid, ino);
1352
1353         if (!pid_alive(task))
1354                 goto out_unlock;
1355
1356         /*
1357          * grab the reference to task.
1358          */
1359         get_task_struct(task);
1360         ei->task = task;
1361         ei->type = ino;
1362         inode->i_uid = 0;
1363         inode->i_gid = 0;
1364         if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1365                 inode->i_uid = task->euid;
1366                 inode->i_gid = task->egid;
1367         }
1368         security_task_to_inode(task, inode);
1369
1370 out:
1371         return inode;
1372
1373 out_unlock:
1374         ei->pde = NULL;
1375         iput(inode);
1376         return NULL;
1377 }
1378
1379 /* dentry stuff */
1380
1381 /*
1382  *      Exceptional case: normally we are not allowed to unhash a busy
1383  * directory. In this case, however, we can do it - no aliasing problems
1384  * due to the way we treat inodes.
1385  *
1386  * Rewrite the inode's ownerships here because the owning task may have
1387  * performed a setuid(), etc.
1388  */
1389 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1390 {
1391         struct inode *inode = dentry->d_inode;
1392         struct task_struct *task = proc_task(inode);
1393         if (pid_alive(task)) {
1394                 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1395                         inode->i_uid = task->euid;
1396                         inode->i_gid = task->egid;
1397                 } else {
1398                         inode->i_uid = 0;
1399                         inode->i_gid = 0;
1400                 }
1401                 security_task_to_inode(task, inode);
1402                 return 1;
1403         }
1404         d_drop(dentry);
1405         return 0;
1406 }
1407
1408 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1409 {
1410         struct inode *inode = dentry->d_inode;
1411         struct task_struct *task = proc_task(inode);
1412         int fd = proc_type(inode) - PROC_TID_FD_DIR;
1413         struct files_struct *files;
1414
1415         files = get_files_struct(task);
1416         if (files) {
1417                 rcu_read_lock();
1418                 if (fcheck_files(files, fd)) {
1419                         rcu_read_unlock();
1420                         put_files_struct(files);
1421                         if (task_dumpable(task)) {
1422                                 inode->i_uid = task->euid;
1423                                 inode->i_gid = task->egid;
1424                         } else {
1425                                 inode->i_uid = 0;
1426                                 inode->i_gid = 0;
1427                         }
1428                         security_task_to_inode(task, inode);
1429                         return 1;
1430                 }
1431                 rcu_read_unlock();
1432                 put_files_struct(files);
1433         }
1434         d_drop(dentry);
1435         return 0;
1436 }
1437
1438 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1439 {
1440         struct task_struct *task = proc_task(inode);
1441         spin_lock(&task->proc_lock);
1442         if (task->proc_dentry == dentry)
1443                 task->proc_dentry = NULL;
1444         spin_unlock(&task->proc_lock);
1445         iput(inode);
1446 }
1447
1448 static int pid_delete_dentry(struct dentry * dentry)
1449 {
1450         /* Is the task we represent dead?
1451          * If so, then don't put the dentry on the lru list,
1452          * kill it immediately.
1453          */
1454         return !pid_alive(proc_task(dentry->d_inode));
1455 }
1456
1457 static struct dentry_operations tid_fd_dentry_operations =
1458 {
1459         .d_revalidate   = tid_fd_revalidate,
1460         .d_delete       = pid_delete_dentry,
1461 };
1462
1463 static struct dentry_operations pid_dentry_operations =
1464 {
1465         .d_revalidate   = pid_revalidate,
1466         .d_delete       = pid_delete_dentry,
1467 };
1468
1469 static struct dentry_operations pid_base_dentry_operations =
1470 {
1471         .d_revalidate   = pid_revalidate,
1472         .d_iput         = pid_base_iput,
1473         .d_delete       = pid_delete_dentry,
1474 };
1475
1476 /* Lookups */
1477
1478 static unsigned name_to_int(struct dentry *dentry)
1479 {
1480         const char *name = dentry->d_name.name;
1481         int len = dentry->d_name.len;
1482         unsigned n = 0;
1483
1484         if (len > 1 && *name == '0')
1485                 goto out;
1486         while (len-- > 0) {
1487                 unsigned c = *name++ - '0';
1488                 if (c > 9)
1489                         goto out;
1490                 if (n >= (~0U-9)/10)
1491                         goto out;
1492                 n *= 10;
1493                 n += c;
1494         }
1495         return n;
1496 out:
1497         return ~0U;
1498 }
1499
1500 /* SMP-safe */
1501 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1502 {
1503         struct task_struct *task = proc_task(dir);
1504         unsigned fd = name_to_int(dentry);
1505         struct file * file;
1506         struct files_struct * files;
1507         struct inode *inode;
1508         struct proc_inode *ei;
1509
1510         if (fd == ~0U)
1511                 goto out;
1512         if (!pid_alive(task))
1513                 goto out;
1514
1515         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1516         if (!inode)
1517                 goto out;
1518         ei = PROC_I(inode);
1519         files = get_files_struct(task);
1520         if (!files)
1521                 goto out_unlock;
1522         inode->i_mode = S_IFLNK;
1523         rcu_read_lock();
1524         file = fcheck_files(files, fd);
1525         if (!file)
1526                 goto out_unlock2;
1527         if (file->f_mode & 1)
1528                 inode->i_mode |= S_IRUSR | S_IXUSR;
1529         if (file->f_mode & 2)
1530                 inode->i_mode |= S_IWUSR | S_IXUSR;
1531         rcu_read_unlock();
1532         put_files_struct(files);
1533         inode->i_op = &proc_pid_link_inode_operations;
1534         inode->i_size = 64;
1535         ei->op.proc_get_link = proc_fd_link;
1536         dentry->d_op = &tid_fd_dentry_operations;
1537         d_add(dentry, inode);
1538         return NULL;
1539
1540 out_unlock2:
1541         rcu_read_unlock();
1542         put_files_struct(files);
1543 out_unlock:
1544         iput(inode);
1545 out:
1546         return ERR_PTR(-ENOENT);
1547 }
1548
1549 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1550 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1551
1552 static struct file_operations proc_fd_operations = {
1553         .read           = generic_read_dir,
1554         .readdir        = proc_readfd,
1555 };
1556
1557 static struct file_operations proc_task_operations = {
1558         .read           = generic_read_dir,
1559         .readdir        = proc_task_readdir,
1560 };
1561
1562 /*
1563  * proc directories can do almost nothing..
1564  */
1565 static struct inode_operations proc_fd_inode_operations = {
1566         .lookup         = proc_lookupfd,
1567         .permission     = proc_permission,
1568 };
1569
1570 static struct inode_operations proc_task_inode_operations = {
1571         .lookup         = proc_task_lookup,
1572         .permission     = proc_task_permission,
1573 };
1574
1575 #ifdef CONFIG_SECURITY
1576 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1577                                   size_t count, loff_t *ppos)
1578 {
1579         struct inode * inode = file->f_dentry->d_inode;
1580         unsigned long page;
1581         ssize_t length;
1582         struct task_struct *task = proc_task(inode);
1583
1584         if (count > PAGE_SIZE)
1585                 count = PAGE_SIZE;
1586         if (!(page = __get_free_page(GFP_KERNEL)))
1587                 return -ENOMEM;
1588
1589         length = security_getprocattr(task, 
1590                                       (char*)file->f_dentry->d_name.name, 
1591                                       (void*)page, count);
1592         if (length >= 0)
1593                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1594         free_page(page);
1595         return length;
1596 }
1597
1598 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1599                                    size_t count, loff_t *ppos)
1600
1601         struct inode * inode = file->f_dentry->d_inode;
1602         char *page; 
1603         ssize_t length; 
1604         struct task_struct *task = proc_task(inode); 
1605
1606         if (count > PAGE_SIZE) 
1607                 count = PAGE_SIZE; 
1608         if (*ppos != 0) {
1609                 /* No partial writes. */
1610                 return -EINVAL;
1611         }
1612         page = (char*)__get_free_page(GFP_USER); 
1613         if (!page) 
1614                 return -ENOMEM;
1615         length = -EFAULT; 
1616         if (copy_from_user(page, buf, count)) 
1617                 goto out;
1618
1619         length = security_setprocattr(task, 
1620                                       (char*)file->f_dentry->d_name.name, 
1621                                       (void*)page, count);
1622 out:
1623         free_page((unsigned long) page);
1624         return length;
1625
1626
1627 static struct file_operations proc_pid_attr_operations = {
1628         .read           = proc_pid_attr_read,
1629         .write          = proc_pid_attr_write,
1630 };
1631
1632 static struct file_operations proc_tid_attr_operations;
1633 static struct inode_operations proc_tid_attr_inode_operations;
1634 static struct file_operations proc_tgid_attr_operations;
1635 static struct inode_operations proc_tgid_attr_inode_operations;
1636 #endif
1637
1638 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1639
1640 /* SMP-safe */
1641 static struct dentry *proc_pident_lookup(struct inode *dir, 
1642                                          struct dentry *dentry,
1643                                          struct pid_entry *ents)
1644 {
1645         struct inode *inode;
1646         int error;
1647         struct task_struct *task = proc_task(dir);
1648         struct pid_entry *p;
1649         struct proc_inode *ei;
1650
1651         error = -ENOENT;
1652         inode = NULL;
1653
1654         if (!pid_alive(task))
1655                 goto out;
1656
1657         for (p = ents; p->name; p++) {
1658                 if (p->len != dentry->d_name.len)
1659                         continue;
1660                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1661                         break;
1662         }
1663         if (!p->name)
1664                 goto out;
1665
1666         error = -EINVAL;
1667         inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1668         if (!inode)
1669                 goto out;
1670
1671         ei = PROC_I(inode);
1672         inode->i_mode = p->mode;
1673         /*
1674          * Yes, it does not scale. And it should not. Don't add
1675          * new entries into /proc/<tgid>/ without very good reasons.
1676          */
1677         switch(p->type) {
1678                 case PROC_TGID_TASK:
1679                         inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1680                         inode->i_op = &proc_task_inode_operations;
1681                         inode->i_fop = &proc_task_operations;
1682                         break;
1683                 case PROC_TID_FD:
1684                 case PROC_TGID_FD:
1685                         inode->i_nlink = 2;
1686                         inode->i_op = &proc_fd_inode_operations;
1687                         inode->i_fop = &proc_fd_operations;
1688                         break;
1689                 case PROC_TID_EXE:
1690                 case PROC_TGID_EXE:
1691                         inode->i_op = &proc_pid_link_inode_operations;
1692                         ei->op.proc_get_link = proc_exe_link;
1693                         break;
1694                 case PROC_TID_CWD:
1695                 case PROC_TGID_CWD:
1696                         inode->i_op = &proc_pid_link_inode_operations;
1697                         ei->op.proc_get_link = proc_cwd_link;
1698                         break;
1699                 case PROC_TID_ROOT:
1700                 case PROC_TGID_ROOT:
1701                         inode->i_op = &proc_pid_link_inode_operations;
1702                         ei->op.proc_get_link = proc_root_link;
1703                         break;
1704                 case PROC_TID_ENVIRON:
1705                 case PROC_TGID_ENVIRON:
1706                         inode->i_fop = &proc_info_file_operations;
1707                         ei->op.proc_read = proc_pid_environ;
1708                         break;
1709                 case PROC_TID_AUXV:
1710                 case PROC_TGID_AUXV:
1711                         inode->i_fop = &proc_info_file_operations;
1712                         ei->op.proc_read = proc_pid_auxv;
1713                         break;
1714                 case PROC_TID_STATUS:
1715                 case PROC_TGID_STATUS:
1716                         inode->i_fop = &proc_info_file_operations;
1717                         ei->op.proc_read = proc_pid_status;
1718                         break;
1719                 case PROC_TID_STAT:
1720                         inode->i_fop = &proc_info_file_operations;
1721                         ei->op.proc_read = proc_tid_stat;
1722                         break;
1723                 case PROC_TGID_STAT:
1724                         inode->i_fop = &proc_info_file_operations;
1725                         ei->op.proc_read = proc_tgid_stat;
1726                         break;
1727                 case PROC_TID_CMDLINE:
1728                 case PROC_TGID_CMDLINE:
1729                         inode->i_fop = &proc_info_file_operations;
1730                         ei->op.proc_read = proc_pid_cmdline;
1731                         break;
1732                 case PROC_TID_STATM:
1733                 case PROC_TGID_STATM:
1734                         inode->i_fop = &proc_info_file_operations;
1735                         ei->op.proc_read = proc_pid_statm;
1736                         break;
1737                 case PROC_TID_MAPS:
1738                 case PROC_TGID_MAPS:
1739                         inode->i_fop = &proc_maps_operations;
1740                         break;
1741 #ifdef CONFIG_NUMA
1742                 case PROC_TID_NUMA_MAPS:
1743                 case PROC_TGID_NUMA_MAPS:
1744                         inode->i_fop = &proc_numa_maps_operations;
1745                         break;
1746 #endif
1747                 case PROC_TID_MEM:
1748                 case PROC_TGID_MEM:
1749                         inode->i_op = &proc_mem_inode_operations;
1750                         inode->i_fop = &proc_mem_operations;
1751                         break;
1752 #ifdef CONFIG_SECCOMP
1753                 case PROC_TID_SECCOMP:
1754                 case PROC_TGID_SECCOMP:
1755                         inode->i_fop = &proc_seccomp_operations;
1756                         break;
1757 #endif /* CONFIG_SECCOMP */
1758                 case PROC_TID_MOUNTS:
1759                 case PROC_TGID_MOUNTS:
1760                         inode->i_fop = &proc_mounts_operations;
1761                         break;
1762 #ifdef CONFIG_MMU
1763                 case PROC_TID_SMAPS:
1764                 case PROC_TGID_SMAPS:
1765                         inode->i_fop = &proc_smaps_operations;
1766                         break;
1767 #endif
1768                 case PROC_TID_MOUNTSTATS:
1769                 case PROC_TGID_MOUNTSTATS:
1770                         inode->i_fop = &proc_mountstats_operations;
1771                         break;
1772 #ifdef CONFIG_SECURITY
1773                 case PROC_TID_ATTR:
1774                         inode->i_nlink = 2;
1775                         inode->i_op = &proc_tid_attr_inode_operations;
1776                         inode->i_fop = &proc_tid_attr_operations;
1777                         break;
1778                 case PROC_TGID_ATTR:
1779                         inode->i_nlink = 2;
1780                         inode->i_op = &proc_tgid_attr_inode_operations;
1781                         inode->i_fop = &proc_tgid_attr_operations;
1782                         break;
1783                 case PROC_TID_ATTR_CURRENT:
1784                 case PROC_TGID_ATTR_CURRENT:
1785                 case PROC_TID_ATTR_PREV:
1786                 case PROC_TGID_ATTR_PREV:
1787                 case PROC_TID_ATTR_EXEC:
1788                 case PROC_TGID_ATTR_EXEC:
1789                 case PROC_TID_ATTR_FSCREATE:
1790                 case PROC_TGID_ATTR_FSCREATE:
1791                         inode->i_fop = &proc_pid_attr_operations;
1792                         break;
1793 #endif
1794 #ifdef CONFIG_KALLSYMS
1795                 case PROC_TID_WCHAN:
1796                 case PROC_TGID_WCHAN:
1797                         inode->i_fop = &proc_info_file_operations;
1798                         ei->op.proc_read = proc_pid_wchan;
1799                         break;
1800 #endif
1801 #ifdef CONFIG_SCHEDSTATS
1802                 case PROC_TID_SCHEDSTAT:
1803                 case PROC_TGID_SCHEDSTAT:
1804                         inode->i_fop = &proc_info_file_operations;
1805                         ei->op.proc_read = proc_pid_schedstat;
1806                         break;
1807 #endif
1808 #ifdef CONFIG_CPUSETS
1809                 case PROC_TID_CPUSET:
1810                 case PROC_TGID_CPUSET:
1811                         inode->i_fop = &proc_cpuset_operations;
1812                         break;
1813 #endif
1814                 case PROC_TID_OOM_SCORE:
1815                 case PROC_TGID_OOM_SCORE:
1816                         inode->i_fop = &proc_info_file_operations;
1817                         ei->op.proc_read = proc_oom_score;
1818                         break;
1819                 case PROC_TID_OOM_ADJUST:
1820                 case PROC_TGID_OOM_ADJUST:
1821                         inode->i_fop = &proc_oom_adjust_operations;
1822                         break;
1823 #ifdef CONFIG_AUDITSYSCALL
1824                 case PROC_TID_LOGINUID:
1825                 case PROC_TGID_LOGINUID:
1826                         inode->i_fop = &proc_loginuid_operations;
1827                         break;
1828 #endif
1829                 default:
1830                         printk("procfs: impossible type (%d)",p->type);
1831                         iput(inode);
1832                         return ERR_PTR(-EINVAL);
1833         }
1834         dentry->d_op = &pid_dentry_operations;
1835         d_add(dentry, inode);
1836         return NULL;
1837
1838 out:
1839         return ERR_PTR(error);
1840 }
1841
1842 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1843         return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1844 }
1845
1846 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1847         return proc_pident_lookup(dir, dentry, tid_base_stuff);
1848 }
1849
1850 static struct file_operations proc_tgid_base_operations = {
1851         .read           = generic_read_dir,
1852         .readdir        = proc_tgid_base_readdir,
1853 };
1854
1855 static struct file_operations proc_tid_base_operations = {
1856         .read           = generic_read_dir,
1857         .readdir        = proc_tid_base_readdir,
1858 };
1859
1860 static struct inode_operations proc_tgid_base_inode_operations = {
1861         .lookup         = proc_tgid_base_lookup,
1862 };
1863
1864 static struct inode_operations proc_tid_base_inode_operations = {
1865         .lookup         = proc_tid_base_lookup,
1866 };
1867
1868 #ifdef CONFIG_SECURITY
1869 static int proc_tgid_attr_readdir(struct file * filp,
1870                              void * dirent, filldir_t filldir)
1871 {
1872         return proc_pident_readdir(filp,dirent,filldir,
1873                                    tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1874 }
1875
1876 static int proc_tid_attr_readdir(struct file * filp,
1877                              void * dirent, filldir_t filldir)
1878 {
1879         return proc_pident_readdir(filp,dirent,filldir,
1880                                    tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1881 }
1882
1883 static struct file_operations proc_tgid_attr_operations = {
1884         .read           = generic_read_dir,
1885         .readdir        = proc_tgid_attr_readdir,
1886 };
1887
1888 static struct file_operations proc_tid_attr_operations = {
1889         .read           = generic_read_dir,
1890         .readdir        = proc_tid_attr_readdir,
1891 };
1892
1893 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1894                                 struct dentry *dentry, struct nameidata *nd)
1895 {
1896         return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1897 }
1898
1899 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1900                                 struct dentry *dentry, struct nameidata *nd)
1901 {
1902         return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1903 }
1904
1905 static struct inode_operations proc_tgid_attr_inode_operations = {
1906         .lookup         = proc_tgid_attr_lookup,
1907 };
1908
1909 static struct inode_operations proc_tid_attr_inode_operations = {
1910         .lookup         = proc_tid_attr_lookup,
1911 };
1912 #endif
1913
1914 /*
1915  * /proc/self:
1916  */
1917 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1918                               int buflen)
1919 {
1920         char tmp[30];
1921         sprintf(tmp, "%d", current->tgid);
1922         return vfs_readlink(dentry,buffer,buflen,tmp);
1923 }
1924
1925 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1926 {
1927         char tmp[30];
1928         sprintf(tmp, "%d", current->tgid);
1929         return ERR_PTR(vfs_follow_link(nd,tmp));
1930 }       
1931
1932 static struct inode_operations proc_self_inode_operations = {
1933         .readlink       = proc_self_readlink,
1934         .follow_link    = proc_self_follow_link,
1935 };
1936
1937 /**
1938  * proc_pid_unhash -  Unhash /proc/@pid entry from the dcache.
1939  * @p: task that should be flushed.
1940  *
1941  * Drops the /proc/@pid dcache entry from the hash chains.
1942  *
1943  * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1944  * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1945  * if the pid value is immediately reused. This is enforced by
1946  * - caller must acquire spin_lock(p->proc_lock)
1947  * - must be called before detach_pid()
1948  * - proc_pid_lookup acquires proc_lock, and checks that
1949  *   the target is not dead by looking at the attach count
1950  *   of PIDTYPE_PID.
1951  */
1952
1953 struct dentry *proc_pid_unhash(struct task_struct *p)
1954 {
1955         struct dentry *proc_dentry;
1956
1957         proc_dentry = p->proc_dentry;
1958         if (proc_dentry != NULL) {
1959
1960                 spin_lock(&dcache_lock);
1961                 spin_lock(&proc_dentry->d_lock);
1962                 if (!d_unhashed(proc_dentry)) {
1963                         dget_locked(proc_dentry);
1964                         __d_drop(proc_dentry);
1965                         spin_unlock(&proc_dentry->d_lock);
1966                 } else {
1967                         spin_unlock(&proc_dentry->d_lock);
1968                         proc_dentry = NULL;
1969                 }
1970                 spin_unlock(&dcache_lock);
1971         }
1972         return proc_dentry;
1973 }
1974
1975 /**
1976  * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1977  * @proc_dentry: directoy to prune.
1978  *
1979  * Shrink the /proc directory that was used by the just killed thread.
1980  */
1981         
1982 void proc_pid_flush(struct dentry *proc_dentry)
1983 {
1984         might_sleep();
1985         if(proc_dentry != NULL) {
1986                 shrink_dcache_parent(proc_dentry);
1987                 dput(proc_dentry);
1988         }
1989 }
1990
1991 /* SMP-safe */
1992 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1993 {
1994         struct task_struct *task;
1995         struct inode *inode;
1996         struct proc_inode *ei;
1997         unsigned tgid;
1998         int died;
1999
2000         if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
2001                 inode = new_inode(dir->i_sb);
2002                 if (!inode)
2003                         return ERR_PTR(-ENOMEM);
2004                 ei = PROC_I(inode);
2005                 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2006                 inode->i_ino = fake_ino(0, PROC_TGID_INO);
2007                 ei->pde = NULL;
2008                 inode->i_mode = S_IFLNK|S_IRWXUGO;
2009                 inode->i_uid = inode->i_gid = 0;
2010                 inode->i_size = 64;
2011                 inode->i_op = &proc_self_inode_operations;
2012                 d_add(dentry, inode);
2013                 return NULL;
2014         }
2015         tgid = name_to_int(dentry);
2016         if (tgid == ~0U)
2017                 goto out;
2018
2019         read_lock(&tasklist_lock);
2020         task = find_task_by_pid(tgid);
2021         if (task)
2022                 get_task_struct(task);
2023         read_unlock(&tasklist_lock);
2024         if (!task)
2025                 goto out;
2026
2027         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
2028
2029
2030         if (!inode) {
2031                 put_task_struct(task);
2032                 goto out;
2033         }
2034         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2035         inode->i_op = &proc_tgid_base_inode_operations;
2036         inode->i_fop = &proc_tgid_base_operations;
2037         inode->i_flags|=S_IMMUTABLE;
2038 #ifdef CONFIG_SECURITY
2039         inode->i_nlink = 5;
2040 #else
2041         inode->i_nlink = 4;
2042 #endif
2043
2044         dentry->d_op = &pid_base_dentry_operations;
2045
2046         died = 0;
2047         d_add(dentry, inode);
2048         spin_lock(&task->proc_lock);
2049         task->proc_dentry = dentry;
2050         if (!pid_alive(task)) {
2051                 dentry = proc_pid_unhash(task);
2052                 died = 1;
2053         }
2054         spin_unlock(&task->proc_lock);
2055
2056         put_task_struct(task);
2057         if (died) {
2058                 proc_pid_flush(dentry);
2059                 goto out;
2060         }
2061         return NULL;
2062 out:
2063         return ERR_PTR(-ENOENT);
2064 }
2065
2066 /* SMP-safe */
2067 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2068 {
2069         struct task_struct *task;
2070         struct task_struct *leader = proc_task(dir);
2071         struct inode *inode;
2072         unsigned tid;
2073
2074         tid = name_to_int(dentry);
2075         if (tid == ~0U)
2076                 goto out;
2077
2078         read_lock(&tasklist_lock);
2079         task = find_task_by_pid(tid);
2080         if (task)
2081                 get_task_struct(task);
2082         read_unlock(&tasklist_lock);
2083         if (!task)
2084                 goto out;
2085         if (leader->tgid != task->tgid)
2086                 goto out_drop_task;
2087
2088         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2089
2090
2091         if (!inode)
2092                 goto out_drop_task;
2093         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2094         inode->i_op = &proc_tid_base_inode_operations;
2095         inode->i_fop = &proc_tid_base_operations;
2096         inode->i_flags|=S_IMMUTABLE;
2097 #ifdef CONFIG_SECURITY
2098         inode->i_nlink = 4;
2099 #else
2100         inode->i_nlink = 3;
2101 #endif
2102
2103         dentry->d_op = &pid_base_dentry_operations;
2104
2105         d_add(dentry, inode);
2106
2107         put_task_struct(task);
2108         return NULL;
2109 out_drop_task:
2110         put_task_struct(task);
2111 out:
2112         return ERR_PTR(-ENOENT);
2113 }
2114
2115 #define PROC_NUMBUF 10
2116 #define PROC_MAXPIDS 20
2117
2118 /*
2119  * Get a few tgid's to return for filldir - we need to hold the
2120  * tasklist lock while doing this, and we must release it before
2121  * we actually do the filldir itself, so we use a temp buffer..
2122  */
2123 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2124 {
2125         struct task_struct *p;
2126         int nr_tgids = 0;
2127
2128         index--;
2129         read_lock(&tasklist_lock);
2130         p = NULL;
2131         if (version) {
2132                 p = find_task_by_pid(version);
2133                 if (p && !thread_group_leader(p))
2134                         p = NULL;
2135         }
2136
2137         if (p)
2138                 index = 0;
2139         else
2140                 p = next_task(&init_task);
2141
2142         for ( ; p != &init_task; p = next_task(p)) {
2143                 int tgid = p->pid;
2144                 if (!pid_alive(p))
2145                         continue;
2146                 if (--index >= 0)
2147                         continue;
2148                 tgids[nr_tgids] = tgid;
2149                 nr_tgids++;
2150                 if (nr_tgids >= PROC_MAXPIDS)
2151                         break;
2152         }
2153         read_unlock(&tasklist_lock);
2154         return nr_tgids;
2155 }
2156
2157 /*
2158  * Get a few tid's to return for filldir - we need to hold the
2159  * tasklist lock while doing this, and we must release it before
2160  * we actually do the filldir itself, so we use a temp buffer..
2161  */
2162 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2163 {
2164         struct task_struct *leader_task = proc_task(dir);
2165         struct task_struct *task = leader_task;
2166         int nr_tids = 0;
2167
2168         index -= 2;
2169         read_lock(&tasklist_lock);
2170         /*
2171          * The starting point task (leader_task) might be an already
2172          * unlinked task, which cannot be used to access the task-list
2173          * via next_thread().
2174          */
2175         if (pid_alive(task)) do {
2176                 int tid = task->pid;
2177
2178                 if (--index >= 0)
2179                         continue;
2180                 if (tids != NULL)
2181                         tids[nr_tids] = tid;
2182                 nr_tids++;
2183                 if (nr_tids >= PROC_MAXPIDS)
2184                         break;
2185         } while ((task = next_thread(task)) != leader_task);
2186         read_unlock(&tasklist_lock);
2187         return nr_tids;
2188 }
2189
2190 /* for the /proc/ directory itself, after non-process stuff has been done */
2191 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2192 {
2193         unsigned int tgid_array[PROC_MAXPIDS];
2194         char buf[PROC_NUMBUF];
2195         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2196         unsigned int nr_tgids, i;
2197         int next_tgid;
2198
2199         if (!nr) {
2200                 ino_t ino = fake_ino(0,PROC_TGID_INO);
2201                 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2202                         return 0;
2203                 filp->f_pos++;
2204                 nr++;
2205         }
2206
2207         /* f_version caches the tgid value that the last readdir call couldn't
2208          * return. lseek aka telldir automagically resets f_version to 0.
2209          */
2210         next_tgid = filp->f_version;
2211         filp->f_version = 0;
2212         for (;;) {
2213                 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2214                 if (!nr_tgids) {
2215                         /* no more entries ! */
2216                         break;
2217                 }
2218                 next_tgid = 0;
2219
2220                 /* do not use the last found pid, reserve it for next_tgid */
2221                 if (nr_tgids == PROC_MAXPIDS) {
2222                         nr_tgids--;
2223                         next_tgid = tgid_array[nr_tgids];
2224                 }
2225
2226                 for (i=0;i<nr_tgids;i++) {
2227                         int tgid = tgid_array[i];
2228                         ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2229                         unsigned long j = PROC_NUMBUF;
2230
2231                         do
2232                                 buf[--j] = '0' + (tgid % 10);
2233                         while ((tgid /= 10) != 0);
2234
2235                         if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2236                                 /* returning this tgid failed, save it as the first
2237                                  * pid for the next readir call */
2238                                 filp->f_version = tgid_array[i];
2239                                 goto out;
2240                         }
2241                         filp->f_pos++;
2242                         nr++;
2243                 }
2244         }
2245 out:
2246         return 0;
2247 }
2248
2249 /* for the /proc/TGID/task/ directories */
2250 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2251 {
2252         unsigned int tid_array[PROC_MAXPIDS];
2253         char buf[PROC_NUMBUF];
2254         unsigned int nr_tids, i;
2255         struct dentry *dentry = filp->f_dentry;
2256         struct inode *inode = dentry->d_inode;
2257         int retval = -ENOENT;
2258         ino_t ino;
2259         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2260
2261         if (!pid_alive(proc_task(inode)))
2262                 goto out;
2263         retval = 0;
2264
2265         switch (pos) {
2266         case 0:
2267                 ino = inode->i_ino;
2268                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2269                         goto out;
2270                 pos++;
2271                 /* fall through */
2272         case 1:
2273                 ino = parent_ino(dentry);
2274                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2275                         goto out;
2276                 pos++;
2277                 /* fall through */
2278         }
2279
2280         nr_tids = get_tid_list(pos, tid_array, inode);
2281         inode->i_nlink = pos + nr_tids;
2282
2283         for (i = 0; i < nr_tids; i++) {
2284                 unsigned long j = PROC_NUMBUF;
2285                 int tid = tid_array[i];
2286
2287                 ino = fake_ino(tid,PROC_TID_INO);
2288
2289                 do
2290                         buf[--j] = '0' + (tid % 10);
2291                 while ((tid /= 10) != 0);
2292
2293                 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2294                         break;
2295                 pos++;
2296         }
2297 out:
2298         filp->f_pos = pos;
2299         return retval;
2300 }