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