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