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