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