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