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