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