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