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