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