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