[PATCH] proc: Move proc_maps_operations into task_mmu.c
[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 mounts_op;
543 struct proc_mounts {
544         struct seq_file m;
545         int event;
546 };
547
548 static int mounts_open(struct inode *inode, struct file *file)
549 {
550         struct task_struct *task = proc_task(inode);
551         struct namespace *namespace;
552         struct proc_mounts *p;
553         int ret = -EINVAL;
554
555         task_lock(task);
556         namespace = task->namespace;
557         if (namespace)
558                 get_namespace(namespace);
559         task_unlock(task);
560
561         if (namespace) {
562                 ret = -ENOMEM;
563                 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
564                 if (p) {
565                         file->private_data = &p->m;
566                         ret = seq_open(file, &mounts_op);
567                         if (!ret) {
568                                 p->m.private = namespace;
569                                 p->event = namespace->event;
570                                 return 0;
571                         }
572                         kfree(p);
573                 }
574                 put_namespace(namespace);
575         }
576         return ret;
577 }
578
579 static int mounts_release(struct inode *inode, struct file *file)
580 {
581         struct seq_file *m = file->private_data;
582         struct namespace *namespace = m->private;
583         put_namespace(namespace);
584         return seq_release(inode, file);
585 }
586
587 static unsigned mounts_poll(struct file *file, poll_table *wait)
588 {
589         struct proc_mounts *p = file->private_data;
590         struct namespace *ns = p->m.private;
591         unsigned res = 0;
592
593         poll_wait(file, &ns->poll, wait);
594
595         spin_lock(&vfsmount_lock);
596         if (p->event != ns->event) {
597                 p->event = ns->event;
598                 res = POLLERR;
599         }
600         spin_unlock(&vfsmount_lock);
601
602         return res;
603 }
604
605 static struct file_operations proc_mounts_operations = {
606         .open           = mounts_open,
607         .read           = seq_read,
608         .llseek         = seq_lseek,
609         .release        = mounts_release,
610         .poll           = mounts_poll,
611 };
612
613 extern struct seq_operations mountstats_op;
614 static int mountstats_open(struct inode *inode, struct file *file)
615 {
616         struct task_struct *task = proc_task(inode);
617         int ret = seq_open(file, &mountstats_op);
618
619         if (!ret) {
620                 struct seq_file *m = file->private_data;
621                 struct namespace *namespace;
622                 task_lock(task);
623                 namespace = task->namespace;
624                 if (namespace)
625                         get_namespace(namespace);
626                 task_unlock(task);
627
628                 if (namespace)
629                         m->private = namespace;
630                 else {
631                         seq_release(inode, file);
632                         ret = -EINVAL;
633                 }
634         }
635         return ret;
636 }
637
638 static struct file_operations proc_mountstats_operations = {
639         .open           = mountstats_open,
640         .read           = seq_read,
641         .llseek         = seq_lseek,
642         .release        = mounts_release,
643 };
644
645 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
646
647 static ssize_t proc_info_read(struct file * file, char __user * buf,
648                           size_t count, loff_t *ppos)
649 {
650         struct inode * inode = file->f_dentry->d_inode;
651         unsigned long page;
652         ssize_t length;
653         struct task_struct *task = proc_task(inode);
654
655         if (count > PROC_BLOCK_SIZE)
656                 count = PROC_BLOCK_SIZE;
657         if (!(page = __get_free_page(GFP_KERNEL)))
658                 return -ENOMEM;
659
660         length = PROC_I(inode)->op.proc_read(task, (char*)page);
661
662         if (length >= 0)
663                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
664         free_page(page);
665         return length;
666 }
667
668 static struct file_operations proc_info_file_operations = {
669         .read           = proc_info_read,
670 };
671
672 static int mem_open(struct inode* inode, struct file* file)
673 {
674         file->private_data = (void*)((long)current->self_exec_id);
675         return 0;
676 }
677
678 static ssize_t mem_read(struct file * file, char __user * buf,
679                         size_t count, loff_t *ppos)
680 {
681         struct task_struct *task = proc_task(file->f_dentry->d_inode);
682         char *page;
683         unsigned long src = *ppos;
684         int ret = -ESRCH;
685         struct mm_struct *mm;
686
687         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
688                 goto out;
689
690         ret = -ENOMEM;
691         page = (char *)__get_free_page(GFP_USER);
692         if (!page)
693                 goto out;
694
695         ret = 0;
696  
697         mm = get_task_mm(task);
698         if (!mm)
699                 goto out_free;
700
701         ret = -EIO;
702  
703         if (file->private_data != (void*)((long)current->self_exec_id))
704                 goto out_put;
705
706         ret = 0;
707  
708         while (count > 0) {
709                 int this_len, retval;
710
711                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
712                 retval = access_process_vm(task, src, page, this_len, 0);
713                 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
714                         if (!ret)
715                                 ret = -EIO;
716                         break;
717                 }
718
719                 if (copy_to_user(buf, page, retval)) {
720                         ret = -EFAULT;
721                         break;
722                 }
723  
724                 ret += retval;
725                 src += retval;
726                 buf += retval;
727                 count -= retval;
728         }
729         *ppos = src;
730
731 out_put:
732         mmput(mm);
733 out_free:
734         free_page((unsigned long) page);
735 out:
736         return ret;
737 }
738
739 #define mem_write NULL
740
741 #ifndef mem_write
742 /* This is a security hazard */
743 static ssize_t mem_write(struct file * file, const char * buf,
744                          size_t count, loff_t *ppos)
745 {
746         int copied = 0;
747         char *page;
748         struct task_struct *task = proc_task(file->f_dentry->d_inode);
749         unsigned long dst = *ppos;
750
751         if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
752                 return -ESRCH;
753
754         page = (char *)__get_free_page(GFP_USER);
755         if (!page)
756                 return -ENOMEM;
757
758         while (count > 0) {
759                 int this_len, retval;
760
761                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
762                 if (copy_from_user(page, buf, this_len)) {
763                         copied = -EFAULT;
764                         break;
765                 }
766                 retval = access_process_vm(task, dst, page, this_len, 1);
767                 if (!retval) {
768                         if (!copied)
769                                 copied = -EIO;
770                         break;
771                 }
772                 copied += retval;
773                 buf += retval;
774                 dst += retval;
775                 count -= retval;                        
776         }
777         *ppos = dst;
778         free_page((unsigned long) page);
779         return copied;
780 }
781 #endif
782
783 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
784 {
785         switch (orig) {
786         case 0:
787                 file->f_pos = offset;
788                 break;
789         case 1:
790                 file->f_pos += offset;
791                 break;
792         default:
793                 return -EINVAL;
794         }
795         force_successful_syscall_return();
796         return file->f_pos;
797 }
798
799 static struct file_operations proc_mem_operations = {
800         .llseek         = mem_lseek,
801         .read           = mem_read,
802         .write          = mem_write,
803         .open           = mem_open,
804 };
805
806 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
807                                 size_t count, loff_t *ppos)
808 {
809         struct task_struct *task = proc_task(file->f_dentry->d_inode);
810         char buffer[8];
811         size_t len;
812         int oom_adjust = task->oomkilladj;
813         loff_t __ppos = *ppos;
814
815         len = sprintf(buffer, "%i\n", oom_adjust);
816         if (__ppos >= len)
817                 return 0;
818         if (count > len-__ppos)
819                 count = len-__ppos;
820         if (copy_to_user(buf, buffer + __ppos, count))
821                 return -EFAULT;
822         *ppos = __ppos + count;
823         return count;
824 }
825
826 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
827                                 size_t count, loff_t *ppos)
828 {
829         struct task_struct *task = proc_task(file->f_dentry->d_inode);
830         char buffer[8], *end;
831         int oom_adjust;
832
833         if (!capable(CAP_SYS_RESOURCE))
834                 return -EPERM;
835         memset(buffer, 0, 8);
836         if (count > 6)
837                 count = 6;
838         if (copy_from_user(buffer, buf, count))
839                 return -EFAULT;
840         oom_adjust = simple_strtol(buffer, &end, 0);
841         if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
842                 return -EINVAL;
843         if (*end == '\n')
844                 end++;
845         task->oomkilladj = oom_adjust;
846         if (end - buffer == 0)
847                 return -EIO;
848         return end - buffer;
849 }
850
851 static struct file_operations proc_oom_adjust_operations = {
852         .read           = oom_adjust_read,
853         .write          = oom_adjust_write,
854 };
855
856 #ifdef CONFIG_AUDITSYSCALL
857 #define TMPBUFLEN 21
858 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
859                                   size_t count, loff_t *ppos)
860 {
861         struct inode * inode = file->f_dentry->d_inode;
862         struct task_struct *task = proc_task(inode);
863         ssize_t length;
864         char tmpbuf[TMPBUFLEN];
865
866         length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
867                                 audit_get_loginuid(task->audit_context));
868         return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
869 }
870
871 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
872                                    size_t count, loff_t *ppos)
873 {
874         struct inode * inode = file->f_dentry->d_inode;
875         char *page, *tmp;
876         ssize_t length;
877         struct task_struct *task = proc_task(inode);
878         uid_t loginuid;
879
880         if (!capable(CAP_AUDIT_CONTROL))
881                 return -EPERM;
882
883         if (current != task)
884                 return -EPERM;
885
886         if (count >= PAGE_SIZE)
887                 count = PAGE_SIZE - 1;
888
889         if (*ppos != 0) {
890                 /* No partial writes. */
891                 return -EINVAL;
892         }
893         page = (char*)__get_free_page(GFP_USER);
894         if (!page)
895                 return -ENOMEM;
896         length = -EFAULT;
897         if (copy_from_user(page, buf, count))
898                 goto out_free_page;
899
900         page[count] = '\0';
901         loginuid = simple_strtoul(page, &tmp, 10);
902         if (tmp == page) {
903                 length = -EINVAL;
904                 goto out_free_page;
905
906         }
907         length = audit_set_loginuid(task, loginuid);
908         if (likely(length == 0))
909                 length = count;
910
911 out_free_page:
912         free_page((unsigned long) page);
913         return length;
914 }
915
916 static struct file_operations proc_loginuid_operations = {
917         .read           = proc_loginuid_read,
918         .write          = proc_loginuid_write,
919 };
920 #endif
921
922 #ifdef CONFIG_SECCOMP
923 static ssize_t seccomp_read(struct file *file, char __user *buf,
924                             size_t count, loff_t *ppos)
925 {
926         struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
927         char __buf[20];
928         loff_t __ppos = *ppos;
929         size_t len;
930
931         /* no need to print the trailing zero, so use only len */
932         len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
933         if (__ppos >= len)
934                 return 0;
935         if (count > len - __ppos)
936                 count = len - __ppos;
937         if (copy_to_user(buf, __buf + __ppos, count))
938                 return -EFAULT;
939         *ppos = __ppos + count;
940         return count;
941 }
942
943 static ssize_t seccomp_write(struct file *file, const char __user *buf,
944                              size_t count, loff_t *ppos)
945 {
946         struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
947         char __buf[20], *end;
948         unsigned int seccomp_mode;
949
950         /* can set it only once to be even more secure */
951         if (unlikely(tsk->seccomp.mode))
952                 return -EPERM;
953
954         memset(__buf, 0, sizeof(__buf));
955         count = min(count, sizeof(__buf) - 1);
956         if (copy_from_user(__buf, buf, count))
957                 return -EFAULT;
958         seccomp_mode = simple_strtoul(__buf, &end, 0);
959         if (*end == '\n')
960                 end++;
961         if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
962                 tsk->seccomp.mode = seccomp_mode;
963                 set_tsk_thread_flag(tsk, TIF_SECCOMP);
964         } else
965                 return -EINVAL;
966         if (unlikely(!(end - __buf)))
967                 return -EIO;
968         return end - __buf;
969 }
970
971 static struct file_operations proc_seccomp_operations = {
972         .read           = seccomp_read,
973         .write          = seccomp_write,
974 };
975 #endif /* CONFIG_SECCOMP */
976
977 static int proc_check_dentry_visible(struct inode *inode,
978         struct dentry *dentry, struct vfsmount *mnt)
979 {
980         /* Verify that the current process can already see the
981          * file pointed at by the file descriptor.
982          * This prevents /proc from being an accidental information leak.
983          *
984          * This prevents access to files that are not visible do to
985          * being on the otherside of a chroot, in a different
986          * namespace, or are simply process local (like pipes).
987          */
988         struct task_struct *task;
989         struct files_struct *task_files, *files;
990         int error = -EACCES;
991
992         /* See if the the two tasks share a commone set of
993          * file descriptors.  If so everything is visible.
994          */
995         task = proc_task(inode);
996         if (!task)
997                 goto out;
998         files = get_files_struct(current);
999         task_files = get_files_struct(task);
1000         if (files && task_files && (files == task_files))
1001                 error = 0;
1002         if (task_files)
1003                 put_files_struct(task_files);
1004         if (files)
1005                 put_files_struct(files);
1006         if (!error)
1007                 goto out;
1008
1009         /* If the two tasks don't share a common set of file
1010          * descriptors see if the destination dentry is already
1011          * visible in the current tasks filesystem namespace.
1012          */
1013         error = proc_check_chroot(dentry, mnt);
1014 out:
1015         return error;
1016
1017 }
1018
1019 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1020 {
1021         struct inode *inode = dentry->d_inode;
1022         int error = -EACCES;
1023
1024         /* We don't need a base pointer in the /proc filesystem */
1025         path_release(nd);
1026
1027         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1028                 goto out;
1029
1030         error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
1031         nd->last_type = LAST_BIND;
1032         if (error)
1033                 goto out;
1034
1035         /* Only return files this task can already see */
1036         error = proc_check_dentry_visible(inode, nd->dentry, nd->mnt);
1037         if (error)
1038                 path_release(nd);
1039 out:
1040         return ERR_PTR(error);
1041 }
1042
1043 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1044                             char __user *buffer, int buflen)
1045 {
1046         struct inode * inode;
1047         char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
1048         int len;
1049
1050         if (!tmp)
1051                 return -ENOMEM;
1052                 
1053         inode = dentry->d_inode;
1054         path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1055         len = PTR_ERR(path);
1056         if (IS_ERR(path))
1057                 goto out;
1058         len = tmp + PAGE_SIZE - 1 - path;
1059
1060         if (len > buflen)
1061                 len = buflen;
1062         if (copy_to_user(buffer, path, len))
1063                 len = -EFAULT;
1064  out:
1065         free_page((unsigned long)tmp);
1066         return len;
1067 }
1068
1069 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1070 {
1071         int error = -EACCES;
1072         struct inode *inode = dentry->d_inode;
1073         struct dentry *de;
1074         struct vfsmount *mnt = NULL;
1075
1076
1077         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
1078                 goto out;
1079
1080         error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1081         if (error)
1082                 goto out;
1083
1084         /* Only return files this task can already see */
1085         error = proc_check_dentry_visible(inode, de, mnt);
1086         if (error)
1087                 goto out_put;
1088
1089         error = do_proc_readlink(de, mnt, buffer, buflen);
1090 out_put:
1091         dput(de);
1092         mntput(mnt);
1093 out:
1094         return error;
1095 }
1096
1097 static struct inode_operations proc_pid_link_inode_operations = {
1098         .readlink       = proc_pid_readlink,
1099         .follow_link    = proc_pid_follow_link
1100 };
1101
1102 #define NUMBUF 10
1103
1104 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1105 {
1106         struct dentry *dentry = filp->f_dentry;
1107         struct inode *inode = dentry->d_inode;
1108         struct task_struct *p = proc_task(inode);
1109         unsigned int fd, tid, ino;
1110         int retval;
1111         char buf[NUMBUF];
1112         struct files_struct * files;
1113         struct fdtable *fdt;
1114
1115         retval = -ENOENT;
1116         if (!pid_alive(p))
1117                 goto out;
1118         retval = 0;
1119         tid = p->pid;
1120
1121         fd = filp->f_pos;
1122         switch (fd) {
1123                 case 0:
1124                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1125                                 goto out;
1126                         filp->f_pos++;
1127                 case 1:
1128                         ino = parent_ino(dentry);
1129                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1130                                 goto out;
1131                         filp->f_pos++;
1132                 default:
1133                         files = get_files_struct(p);
1134                         if (!files)
1135                                 goto out;
1136                         rcu_read_lock();
1137                         fdt = files_fdtable(files);
1138                         for (fd = filp->f_pos-2;
1139                              fd < fdt->max_fds;
1140                              fd++, filp->f_pos++) {
1141                                 unsigned int i,j;
1142
1143                                 if (!fcheck_files(files, fd))
1144                                         continue;
1145                                 rcu_read_unlock();
1146
1147                                 j = NUMBUF;
1148                                 i = fd;
1149                                 do {
1150                                         j--;
1151                                         buf[j] = '0' + (i % 10);
1152                                         i /= 10;
1153                                 } while (i);
1154
1155                                 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1156                                 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1157                                         rcu_read_lock();
1158                                         break;
1159                                 }
1160                                 rcu_read_lock();
1161                         }
1162                         rcu_read_unlock();
1163                         put_files_struct(files);
1164         }
1165 out:
1166         return retval;
1167 }
1168
1169 static int proc_pident_readdir(struct file *filp,
1170                 void *dirent, filldir_t filldir,
1171                 struct pid_entry *ents, unsigned int nents)
1172 {
1173         int i;
1174         int pid;
1175         struct dentry *dentry = filp->f_dentry;
1176         struct inode *inode = dentry->d_inode;
1177         struct pid_entry *p;
1178         ino_t ino;
1179         int ret;
1180
1181         ret = -ENOENT;
1182         if (!pid_alive(proc_task(inode)))
1183                 goto out;
1184
1185         ret = 0;
1186         pid = proc_task(inode)->pid;
1187         i = filp->f_pos;
1188         switch (i) {
1189         case 0:
1190                 ino = inode->i_ino;
1191                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1192                         goto out;
1193                 i++;
1194                 filp->f_pos++;
1195                 /* fall through */
1196         case 1:
1197                 ino = parent_ino(dentry);
1198                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1199                         goto out;
1200                 i++;
1201                 filp->f_pos++;
1202                 /* fall through */
1203         default:
1204                 i -= 2;
1205                 if (i >= nents) {
1206                         ret = 1;
1207                         goto out;
1208                 }
1209                 p = ents + i;
1210                 while (p->name) {
1211                         if (filldir(dirent, p->name, p->len, filp->f_pos,
1212                                     fake_ino(pid, p->type), p->mode >> 12) < 0)
1213                                 goto out;
1214                         filp->f_pos++;
1215                         p++;
1216                 }
1217         }
1218
1219         ret = 1;
1220 out:
1221         return ret;
1222 }
1223
1224 static int proc_tgid_base_readdir(struct file * filp,
1225                              void * dirent, filldir_t filldir)
1226 {
1227         return proc_pident_readdir(filp,dirent,filldir,
1228                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1229 }
1230
1231 static int proc_tid_base_readdir(struct file * filp,
1232                              void * dirent, filldir_t filldir)
1233 {
1234         return proc_pident_readdir(filp,dirent,filldir,
1235                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1236 }
1237
1238 /* building an inode */
1239
1240 static int task_dumpable(struct task_struct *task)
1241 {
1242         int dumpable = 0;
1243         struct mm_struct *mm;
1244
1245         task_lock(task);
1246         mm = task->mm;
1247         if (mm)
1248                 dumpable = mm->dumpable;
1249         task_unlock(task);
1250         if(dumpable == 1)
1251                 return 1;
1252         return 0;
1253 }
1254
1255
1256 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1257 {
1258         struct inode * inode;
1259         struct proc_inode *ei;
1260
1261         /* We need a new inode */
1262         
1263         inode = new_inode(sb);
1264         if (!inode)
1265                 goto out;
1266
1267         /* Common stuff */
1268         ei = PROC_I(inode);
1269         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1270         inode->i_ino = fake_ino(task->pid, ino);
1271
1272         if (!pid_alive(task))
1273                 goto out_unlock;
1274
1275         /*
1276          * grab the reference to task.
1277          */
1278         get_task_struct(task);
1279         ei->task = task;
1280         inode->i_uid = 0;
1281         inode->i_gid = 0;
1282         if (task_dumpable(task)) {
1283                 inode->i_uid = task->euid;
1284                 inode->i_gid = task->egid;
1285         }
1286         security_task_to_inode(task, inode);
1287
1288 out:
1289         return inode;
1290
1291 out_unlock:
1292         iput(inode);
1293         return NULL;
1294 }
1295
1296 /* dentry stuff */
1297
1298 /*
1299  *      Exceptional case: normally we are not allowed to unhash a busy
1300  * directory. In this case, however, we can do it - no aliasing problems
1301  * due to the way we treat inodes.
1302  *
1303  * Rewrite the inode's ownerships here because the owning task may have
1304  * performed a setuid(), etc.
1305  */
1306 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1307 {
1308         struct inode *inode = dentry->d_inode;
1309         struct task_struct *task = proc_task(inode);
1310         if (pid_alive(task)) {
1311                 if (task_dumpable(task)) {
1312                         inode->i_uid = task->euid;
1313                         inode->i_gid = task->egid;
1314                 } else {
1315                         inode->i_uid = 0;
1316                         inode->i_gid = 0;
1317                 }
1318                 security_task_to_inode(task, inode);
1319                 return 1;
1320         }
1321         d_drop(dentry);
1322         return 0;
1323 }
1324
1325 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1326 {
1327         struct inode *inode = dentry->d_inode;
1328         struct task_struct *task = proc_task(inode);
1329         int fd = proc_fd(inode);
1330         struct files_struct *files;
1331
1332         files = get_files_struct(task);
1333         if (files) {
1334                 rcu_read_lock();
1335                 if (fcheck_files(files, fd)) {
1336                         rcu_read_unlock();
1337                         put_files_struct(files);
1338                         if (task_dumpable(task)) {
1339                                 inode->i_uid = task->euid;
1340                                 inode->i_gid = task->egid;
1341                         } else {
1342                                 inode->i_uid = 0;
1343                                 inode->i_gid = 0;
1344                         }
1345                         security_task_to_inode(task, inode);
1346                         return 1;
1347                 }
1348                 rcu_read_unlock();
1349                 put_files_struct(files);
1350         }
1351         d_drop(dentry);
1352         return 0;
1353 }
1354
1355 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1356 {
1357         struct task_struct *task = proc_task(inode);
1358         spin_lock(&task->proc_lock);
1359         if (task->proc_dentry == dentry)
1360                 task->proc_dentry = NULL;
1361         spin_unlock(&task->proc_lock);
1362         iput(inode);
1363 }
1364
1365 static int pid_delete_dentry(struct dentry * dentry)
1366 {
1367         /* Is the task we represent dead?
1368          * If so, then don't put the dentry on the lru list,
1369          * kill it immediately.
1370          */
1371         return !pid_alive(proc_task(dentry->d_inode));
1372 }
1373
1374 static struct dentry_operations tid_fd_dentry_operations =
1375 {
1376         .d_revalidate   = tid_fd_revalidate,
1377         .d_delete       = pid_delete_dentry,
1378 };
1379
1380 static struct dentry_operations pid_dentry_operations =
1381 {
1382         .d_revalidate   = pid_revalidate,
1383         .d_delete       = pid_delete_dentry,
1384 };
1385
1386 static struct dentry_operations pid_base_dentry_operations =
1387 {
1388         .d_revalidate   = pid_revalidate,
1389         .d_iput         = pid_base_iput,
1390         .d_delete       = pid_delete_dentry,
1391 };
1392
1393 /* Lookups */
1394
1395 static unsigned name_to_int(struct dentry *dentry)
1396 {
1397         const char *name = dentry->d_name.name;
1398         int len = dentry->d_name.len;
1399         unsigned n = 0;
1400
1401         if (len > 1 && *name == '0')
1402                 goto out;
1403         while (len-- > 0) {
1404                 unsigned c = *name++ - '0';
1405                 if (c > 9)
1406                         goto out;
1407                 if (n >= (~0U-9)/10)
1408                         goto out;
1409                 n *= 10;
1410                 n += c;
1411         }
1412         return n;
1413 out:
1414         return ~0U;
1415 }
1416
1417 /* SMP-safe */
1418 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1419 {
1420         struct task_struct *task = proc_task(dir);
1421         unsigned fd = name_to_int(dentry);
1422         struct file * file;
1423         struct files_struct * files;
1424         struct inode *inode;
1425         struct proc_inode *ei;
1426
1427         if (fd == ~0U)
1428                 goto out;
1429         if (!pid_alive(task))
1430                 goto out;
1431
1432         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1433         if (!inode)
1434                 goto out;
1435         ei = PROC_I(inode);
1436         ei->fd = fd;
1437         files = get_files_struct(task);
1438         if (!files)
1439                 goto out_unlock;
1440         inode->i_mode = S_IFLNK;
1441
1442         /*
1443          * We are not taking a ref to the file structure, so we must
1444          * hold ->file_lock.
1445          */
1446         spin_lock(&files->file_lock);
1447         file = fcheck_files(files, fd);
1448         if (!file)
1449                 goto out_unlock2;
1450         if (file->f_mode & 1)
1451                 inode->i_mode |= S_IRUSR | S_IXUSR;
1452         if (file->f_mode & 2)
1453                 inode->i_mode |= S_IWUSR | S_IXUSR;
1454         spin_unlock(&files->file_lock);
1455         put_files_struct(files);
1456         inode->i_op = &proc_pid_link_inode_operations;
1457         inode->i_size = 64;
1458         ei->op.proc_get_link = proc_fd_link;
1459         dentry->d_op = &tid_fd_dentry_operations;
1460         d_add(dentry, inode);
1461         return NULL;
1462
1463 out_unlock2:
1464         spin_unlock(&files->file_lock);
1465         put_files_struct(files);
1466 out_unlock:
1467         iput(inode);
1468 out:
1469         return ERR_PTR(-ENOENT);
1470 }
1471
1472 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1473 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1474 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat);
1475
1476 static struct file_operations proc_fd_operations = {
1477         .read           = generic_read_dir,
1478         .readdir        = proc_readfd,
1479 };
1480
1481 static struct file_operations proc_task_operations = {
1482         .read           = generic_read_dir,
1483         .readdir        = proc_task_readdir,
1484 };
1485
1486 /*
1487  * proc directories can do almost nothing..
1488  */
1489 static struct inode_operations proc_fd_inode_operations = {
1490         .lookup         = proc_lookupfd,
1491 };
1492
1493 static struct inode_operations proc_task_inode_operations = {
1494         .lookup         = proc_task_lookup,
1495         .getattr        = proc_task_getattr,
1496 };
1497
1498 #ifdef CONFIG_SECURITY
1499 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1500                                   size_t count, loff_t *ppos)
1501 {
1502         struct inode * inode = file->f_dentry->d_inode;
1503         unsigned long page;
1504         ssize_t length;
1505         struct task_struct *task = proc_task(inode);
1506
1507         if (count > PAGE_SIZE)
1508                 count = PAGE_SIZE;
1509         if (!(page = __get_free_page(GFP_KERNEL)))
1510                 return -ENOMEM;
1511
1512         length = security_getprocattr(task, 
1513                                       (char*)file->f_dentry->d_name.name, 
1514                                       (void*)page, count);
1515         if (length >= 0)
1516                 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1517         free_page(page);
1518         return length;
1519 }
1520
1521 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1522                                    size_t count, loff_t *ppos)
1523
1524         struct inode * inode = file->f_dentry->d_inode;
1525         char *page; 
1526         ssize_t length; 
1527         struct task_struct *task = proc_task(inode); 
1528
1529         if (count > PAGE_SIZE) 
1530                 count = PAGE_SIZE; 
1531         if (*ppos != 0) {
1532                 /* No partial writes. */
1533                 return -EINVAL;
1534         }
1535         page = (char*)__get_free_page(GFP_USER); 
1536         if (!page) 
1537                 return -ENOMEM;
1538         length = -EFAULT; 
1539         if (copy_from_user(page, buf, count)) 
1540                 goto out;
1541
1542         length = security_setprocattr(task, 
1543                                       (char*)file->f_dentry->d_name.name, 
1544                                       (void*)page, count);
1545 out:
1546         free_page((unsigned long) page);
1547         return length;
1548
1549
1550 static struct file_operations proc_pid_attr_operations = {
1551         .read           = proc_pid_attr_read,
1552         .write          = proc_pid_attr_write,
1553 };
1554
1555 static struct file_operations proc_tid_attr_operations;
1556 static struct inode_operations proc_tid_attr_inode_operations;
1557 static struct file_operations proc_tgid_attr_operations;
1558 static struct inode_operations proc_tgid_attr_inode_operations;
1559 #endif
1560
1561 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1562
1563 /* SMP-safe */
1564 static struct dentry *proc_pident_lookup(struct inode *dir, 
1565                                          struct dentry *dentry,
1566                                          struct pid_entry *ents)
1567 {
1568         struct inode *inode;
1569         int error;
1570         struct task_struct *task = proc_task(dir);
1571         struct pid_entry *p;
1572         struct proc_inode *ei;
1573
1574         error = -ENOENT;
1575         inode = NULL;
1576
1577         if (!pid_alive(task))
1578                 goto out;
1579
1580         for (p = ents; p->name; p++) {
1581                 if (p->len != dentry->d_name.len)
1582                         continue;
1583                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1584                         break;
1585         }
1586         if (!p->name)
1587                 goto out;
1588
1589         error = -EINVAL;
1590         inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1591         if (!inode)
1592                 goto out;
1593
1594         ei = PROC_I(inode);
1595         inode->i_mode = p->mode;
1596         /*
1597          * Yes, it does not scale. And it should not. Don't add
1598          * new entries into /proc/<tgid>/ without very good reasons.
1599          */
1600         switch(p->type) {
1601                 case PROC_TGID_TASK:
1602                         inode->i_nlink = 2;
1603                         inode->i_op = &proc_task_inode_operations;
1604                         inode->i_fop = &proc_task_operations;
1605                         break;
1606                 case PROC_TID_FD:
1607                 case PROC_TGID_FD:
1608                         inode->i_nlink = 2;
1609                         inode->i_op = &proc_fd_inode_operations;
1610                         inode->i_fop = &proc_fd_operations;
1611                         break;
1612                 case PROC_TID_EXE:
1613                 case PROC_TGID_EXE:
1614                         inode->i_op = &proc_pid_link_inode_operations;
1615                         ei->op.proc_get_link = proc_exe_link;
1616                         break;
1617                 case PROC_TID_CWD:
1618                 case PROC_TGID_CWD:
1619                         inode->i_op = &proc_pid_link_inode_operations;
1620                         ei->op.proc_get_link = proc_cwd_link;
1621                         break;
1622                 case PROC_TID_ROOT:
1623                 case PROC_TGID_ROOT:
1624                         inode->i_op = &proc_pid_link_inode_operations;
1625                         ei->op.proc_get_link = proc_root_link;
1626                         break;
1627                 case PROC_TID_ENVIRON:
1628                 case PROC_TGID_ENVIRON:
1629                         inode->i_fop = &proc_info_file_operations;
1630                         ei->op.proc_read = proc_pid_environ;
1631                         break;
1632                 case PROC_TID_AUXV:
1633                 case PROC_TGID_AUXV:
1634                         inode->i_fop = &proc_info_file_operations;
1635                         ei->op.proc_read = proc_pid_auxv;
1636                         break;
1637                 case PROC_TID_STATUS:
1638                 case PROC_TGID_STATUS:
1639                         inode->i_fop = &proc_info_file_operations;
1640                         ei->op.proc_read = proc_pid_status;
1641                         break;
1642                 case PROC_TID_STAT:
1643                         inode->i_fop = &proc_info_file_operations;
1644                         ei->op.proc_read = proc_tid_stat;
1645                         break;
1646                 case PROC_TGID_STAT:
1647                         inode->i_fop = &proc_info_file_operations;
1648                         ei->op.proc_read = proc_tgid_stat;
1649                         break;
1650                 case PROC_TID_CMDLINE:
1651                 case PROC_TGID_CMDLINE:
1652                         inode->i_fop = &proc_info_file_operations;
1653                         ei->op.proc_read = proc_pid_cmdline;
1654                         break;
1655                 case PROC_TID_STATM:
1656                 case PROC_TGID_STATM:
1657                         inode->i_fop = &proc_info_file_operations;
1658                         ei->op.proc_read = proc_pid_statm;
1659                         break;
1660                 case PROC_TID_MAPS:
1661                 case PROC_TGID_MAPS:
1662                         inode->i_fop = &proc_maps_operations;
1663                         break;
1664 #ifdef CONFIG_NUMA
1665                 case PROC_TID_NUMA_MAPS:
1666                 case PROC_TGID_NUMA_MAPS:
1667                         inode->i_fop = &proc_numa_maps_operations;
1668                         break;
1669 #endif
1670                 case PROC_TID_MEM:
1671                 case PROC_TGID_MEM:
1672                         inode->i_fop = &proc_mem_operations;
1673                         break;
1674 #ifdef CONFIG_SECCOMP
1675                 case PROC_TID_SECCOMP:
1676                 case PROC_TGID_SECCOMP:
1677                         inode->i_fop = &proc_seccomp_operations;
1678                         break;
1679 #endif /* CONFIG_SECCOMP */
1680                 case PROC_TID_MOUNTS:
1681                 case PROC_TGID_MOUNTS:
1682                         inode->i_fop = &proc_mounts_operations;
1683                         break;
1684 #ifdef CONFIG_MMU
1685                 case PROC_TID_SMAPS:
1686                 case PROC_TGID_SMAPS:
1687                         inode->i_fop = &proc_smaps_operations;
1688                         break;
1689 #endif
1690                 case PROC_TID_MOUNTSTATS:
1691                 case PROC_TGID_MOUNTSTATS:
1692                         inode->i_fop = &proc_mountstats_operations;
1693                         break;
1694 #ifdef CONFIG_SECURITY
1695                 case PROC_TID_ATTR:
1696                         inode->i_nlink = 2;
1697                         inode->i_op = &proc_tid_attr_inode_operations;
1698                         inode->i_fop = &proc_tid_attr_operations;
1699                         break;
1700                 case PROC_TGID_ATTR:
1701                         inode->i_nlink = 2;
1702                         inode->i_op = &proc_tgid_attr_inode_operations;
1703                         inode->i_fop = &proc_tgid_attr_operations;
1704                         break;
1705                 case PROC_TID_ATTR_CURRENT:
1706                 case PROC_TGID_ATTR_CURRENT:
1707                 case PROC_TID_ATTR_PREV:
1708                 case PROC_TGID_ATTR_PREV:
1709                 case PROC_TID_ATTR_EXEC:
1710                 case PROC_TGID_ATTR_EXEC:
1711                 case PROC_TID_ATTR_FSCREATE:
1712                 case PROC_TGID_ATTR_FSCREATE:
1713                 case PROC_TID_ATTR_KEYCREATE:
1714                 case PROC_TGID_ATTR_KEYCREATE:
1715                         inode->i_fop = &proc_pid_attr_operations;
1716                         break;
1717 #endif
1718 #ifdef CONFIG_KALLSYMS
1719                 case PROC_TID_WCHAN:
1720                 case PROC_TGID_WCHAN:
1721                         inode->i_fop = &proc_info_file_operations;
1722                         ei->op.proc_read = proc_pid_wchan;
1723                         break;
1724 #endif
1725 #ifdef CONFIG_SCHEDSTATS
1726                 case PROC_TID_SCHEDSTAT:
1727                 case PROC_TGID_SCHEDSTAT:
1728                         inode->i_fop = &proc_info_file_operations;
1729                         ei->op.proc_read = proc_pid_schedstat;
1730                         break;
1731 #endif
1732 #ifdef CONFIG_CPUSETS
1733                 case PROC_TID_CPUSET:
1734                 case PROC_TGID_CPUSET:
1735                         inode->i_fop = &proc_cpuset_operations;
1736                         break;
1737 #endif
1738                 case PROC_TID_OOM_SCORE:
1739                 case PROC_TGID_OOM_SCORE:
1740                         inode->i_fop = &proc_info_file_operations;
1741                         ei->op.proc_read = proc_oom_score;
1742                         break;
1743                 case PROC_TID_OOM_ADJUST:
1744                 case PROC_TGID_OOM_ADJUST:
1745                         inode->i_fop = &proc_oom_adjust_operations;
1746                         break;
1747 #ifdef CONFIG_AUDITSYSCALL
1748                 case PROC_TID_LOGINUID:
1749                 case PROC_TGID_LOGINUID:
1750                         inode->i_fop = &proc_loginuid_operations;
1751                         break;
1752 #endif
1753                 default:
1754                         printk("procfs: impossible type (%d)",p->type);
1755                         iput(inode);
1756                         return ERR_PTR(-EINVAL);
1757         }
1758         dentry->d_op = &pid_dentry_operations;
1759         d_add(dentry, inode);
1760         return NULL;
1761
1762 out:
1763         return ERR_PTR(error);
1764 }
1765
1766 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1767         return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1768 }
1769
1770 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1771         return proc_pident_lookup(dir, dentry, tid_base_stuff);
1772 }
1773
1774 static struct file_operations proc_tgid_base_operations = {
1775         .read           = generic_read_dir,
1776         .readdir        = proc_tgid_base_readdir,
1777 };
1778
1779 static struct file_operations proc_tid_base_operations = {
1780         .read           = generic_read_dir,
1781         .readdir        = proc_tid_base_readdir,
1782 };
1783
1784 static struct inode_operations proc_tgid_base_inode_operations = {
1785         .lookup         = proc_tgid_base_lookup,
1786 };
1787
1788 static struct inode_operations proc_tid_base_inode_operations = {
1789         .lookup         = proc_tid_base_lookup,
1790 };
1791
1792 #ifdef CONFIG_SECURITY
1793 static int proc_tgid_attr_readdir(struct file * filp,
1794                              void * dirent, filldir_t filldir)
1795 {
1796         return proc_pident_readdir(filp,dirent,filldir,
1797                                    tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1798 }
1799
1800 static int proc_tid_attr_readdir(struct file * filp,
1801                              void * dirent, filldir_t filldir)
1802 {
1803         return proc_pident_readdir(filp,dirent,filldir,
1804                                    tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1805 }
1806
1807 static struct file_operations proc_tgid_attr_operations = {
1808         .read           = generic_read_dir,
1809         .readdir        = proc_tgid_attr_readdir,
1810 };
1811
1812 static struct file_operations proc_tid_attr_operations = {
1813         .read           = generic_read_dir,
1814         .readdir        = proc_tid_attr_readdir,
1815 };
1816
1817 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1818                                 struct dentry *dentry, struct nameidata *nd)
1819 {
1820         return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1821 }
1822
1823 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1824                                 struct dentry *dentry, struct nameidata *nd)
1825 {
1826         return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1827 }
1828
1829 static struct inode_operations proc_tgid_attr_inode_operations = {
1830         .lookup         = proc_tgid_attr_lookup,
1831 };
1832
1833 static struct inode_operations proc_tid_attr_inode_operations = {
1834         .lookup         = proc_tid_attr_lookup,
1835 };
1836 #endif
1837
1838 /*
1839  * /proc/self:
1840  */
1841 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1842                               int buflen)
1843 {
1844         char tmp[30];
1845         sprintf(tmp, "%d", current->tgid);
1846         return vfs_readlink(dentry,buffer,buflen,tmp);
1847 }
1848
1849 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1850 {
1851         char tmp[30];
1852         sprintf(tmp, "%d", current->tgid);
1853         return ERR_PTR(vfs_follow_link(nd,tmp));
1854 }       
1855
1856 static struct inode_operations proc_self_inode_operations = {
1857         .readlink       = proc_self_readlink,
1858         .follow_link    = proc_self_follow_link,
1859 };
1860
1861 /**
1862  * proc_pid_unhash -  Unhash /proc/@pid entry from the dcache.
1863  * @p: task that should be flushed.
1864  *
1865  * Drops the /proc/@pid dcache entry from the hash chains.
1866  *
1867  * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1868  * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1869  * if the pid value is immediately reused. This is enforced by
1870  * - caller must acquire spin_lock(p->proc_lock)
1871  * - must be called before detach_pid()
1872  * - proc_pid_lookup acquires proc_lock, and checks that
1873  *   the target is not dead by looking at the attach count
1874  *   of PIDTYPE_PID.
1875  */
1876
1877 struct dentry *proc_pid_unhash(struct task_struct *p)
1878 {
1879         struct dentry *proc_dentry;
1880
1881         proc_dentry = p->proc_dentry;
1882         if (proc_dentry != NULL) {
1883
1884                 spin_lock(&dcache_lock);
1885                 spin_lock(&proc_dentry->d_lock);
1886                 if (!d_unhashed(proc_dentry)) {
1887                         dget_locked(proc_dentry);
1888                         __d_drop(proc_dentry);
1889                         spin_unlock(&proc_dentry->d_lock);
1890                 } else {
1891                         spin_unlock(&proc_dentry->d_lock);
1892                         proc_dentry = NULL;
1893                 }
1894                 spin_unlock(&dcache_lock);
1895         }
1896         return proc_dentry;
1897 }
1898
1899 /**
1900  * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1901  * @proc_dentry: directoy to prune.
1902  *
1903  * Shrink the /proc directory that was used by the just killed thread.
1904  */
1905         
1906 void proc_pid_flush(struct dentry *proc_dentry)
1907 {
1908         might_sleep();
1909         if(proc_dentry != NULL) {
1910                 shrink_dcache_parent(proc_dentry);
1911                 dput(proc_dentry);
1912         }
1913 }
1914
1915 /* SMP-safe */
1916 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1917 {
1918         struct task_struct *task;
1919         struct inode *inode;
1920         struct proc_inode *ei;
1921         unsigned tgid;
1922         int died;
1923
1924         if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1925                 inode = new_inode(dir->i_sb);
1926                 if (!inode)
1927                         return ERR_PTR(-ENOMEM);
1928                 ei = PROC_I(inode);
1929                 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1930                 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1931                 ei->pde = NULL;
1932                 inode->i_mode = S_IFLNK|S_IRWXUGO;
1933                 inode->i_uid = inode->i_gid = 0;
1934                 inode->i_size = 64;
1935                 inode->i_op = &proc_self_inode_operations;
1936                 d_add(dentry, inode);
1937                 return NULL;
1938         }
1939         tgid = name_to_int(dentry);
1940         if (tgid == ~0U)
1941                 goto out;
1942
1943         read_lock(&tasklist_lock);
1944         task = find_task_by_pid(tgid);
1945         if (task)
1946                 get_task_struct(task);
1947         read_unlock(&tasklist_lock);
1948         if (!task)
1949                 goto out;
1950
1951         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1952
1953
1954         if (!inode) {
1955                 put_task_struct(task);
1956                 goto out;
1957         }
1958         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1959         inode->i_op = &proc_tgid_base_inode_operations;
1960         inode->i_fop = &proc_tgid_base_operations;
1961         inode->i_flags|=S_IMMUTABLE;
1962 #ifdef CONFIG_SECURITY
1963         inode->i_nlink = 5;
1964 #else
1965         inode->i_nlink = 4;
1966 #endif
1967
1968         dentry->d_op = &pid_base_dentry_operations;
1969
1970         died = 0;
1971         d_add(dentry, inode);
1972         spin_lock(&task->proc_lock);
1973         task->proc_dentry = dentry;
1974         if (!pid_alive(task)) {
1975                 dentry = proc_pid_unhash(task);
1976                 died = 1;
1977         }
1978         spin_unlock(&task->proc_lock);
1979
1980         put_task_struct(task);
1981         if (died) {
1982                 proc_pid_flush(dentry);
1983                 goto out;
1984         }
1985         return NULL;
1986 out:
1987         return ERR_PTR(-ENOENT);
1988 }
1989
1990 /* SMP-safe */
1991 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1992 {
1993         struct task_struct *task;
1994         struct task_struct *leader = proc_task(dir);
1995         struct inode *inode;
1996         unsigned tid;
1997
1998         tid = name_to_int(dentry);
1999         if (tid == ~0U)
2000                 goto out;
2001
2002         read_lock(&tasklist_lock);
2003         task = find_task_by_pid(tid);
2004         if (task)
2005                 get_task_struct(task);
2006         read_unlock(&tasklist_lock);
2007         if (!task)
2008                 goto out;
2009         if (leader->tgid != task->tgid)
2010                 goto out_drop_task;
2011
2012         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
2013
2014
2015         if (!inode)
2016                 goto out_drop_task;
2017         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2018         inode->i_op = &proc_tid_base_inode_operations;
2019         inode->i_fop = &proc_tid_base_operations;
2020         inode->i_flags|=S_IMMUTABLE;
2021 #ifdef CONFIG_SECURITY
2022         inode->i_nlink = 4;
2023 #else
2024         inode->i_nlink = 3;
2025 #endif
2026
2027         dentry->d_op = &pid_base_dentry_operations;
2028
2029         d_add(dentry, inode);
2030
2031         put_task_struct(task);
2032         return NULL;
2033 out_drop_task:
2034         put_task_struct(task);
2035 out:
2036         return ERR_PTR(-ENOENT);
2037 }
2038
2039 #define PROC_NUMBUF 10
2040 #define PROC_MAXPIDS 20
2041
2042 /*
2043  * Get a few tgid's to return for filldir - we need to hold the
2044  * tasklist lock while doing this, and we must release it before
2045  * we actually do the filldir itself, so we use a temp buffer..
2046  */
2047 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
2048 {
2049         struct task_struct *p;
2050         int nr_tgids = 0;
2051
2052         index--;
2053         read_lock(&tasklist_lock);
2054         p = NULL;
2055         if (version) {
2056                 p = find_task_by_pid(version);
2057                 if (p && !thread_group_leader(p))
2058                         p = NULL;
2059         }
2060
2061         if (p)
2062                 index = 0;
2063         else
2064                 p = next_task(&init_task);
2065
2066         for ( ; p != &init_task; p = next_task(p)) {
2067                 int tgid = p->pid;
2068                 if (!pid_alive(p))
2069                         continue;
2070                 if (--index >= 0)
2071                         continue;
2072                 tgids[nr_tgids] = tgid;
2073                 nr_tgids++;
2074                 if (nr_tgids >= PROC_MAXPIDS)
2075                         break;
2076         }
2077         read_unlock(&tasklist_lock);
2078         return nr_tgids;
2079 }
2080
2081 /*
2082  * Get a few tid's to return for filldir - we need to hold the
2083  * tasklist lock while doing this, and we must release it before
2084  * we actually do the filldir itself, so we use a temp buffer..
2085  */
2086 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2087 {
2088         struct task_struct *leader_task = proc_task(dir);
2089         struct task_struct *task = leader_task;
2090         int nr_tids = 0;
2091
2092         index -= 2;
2093         read_lock(&tasklist_lock);
2094         /*
2095          * The starting point task (leader_task) might be an already
2096          * unlinked task, which cannot be used to access the task-list
2097          * via next_thread().
2098          */
2099         if (pid_alive(task)) do {
2100                 int tid = task->pid;
2101
2102                 if (--index >= 0)
2103                         continue;
2104                 if (tids != NULL)
2105                         tids[nr_tids] = tid;
2106                 nr_tids++;
2107                 if (nr_tids >= PROC_MAXPIDS)
2108                         break;
2109         } while ((task = next_thread(task)) != leader_task);
2110         read_unlock(&tasklist_lock);
2111         return nr_tids;
2112 }
2113
2114 /* for the /proc/ directory itself, after non-process stuff has been done */
2115 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2116 {
2117         unsigned int tgid_array[PROC_MAXPIDS];
2118         char buf[PROC_NUMBUF];
2119         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2120         unsigned int nr_tgids, i;
2121         int next_tgid;
2122
2123         if (!nr) {
2124                 ino_t ino = fake_ino(0,PROC_TGID_INO);
2125                 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2126                         return 0;
2127                 filp->f_pos++;
2128                 nr++;
2129         }
2130
2131         /* f_version caches the tgid value that the last readdir call couldn't
2132          * return. lseek aka telldir automagically resets f_version to 0.
2133          */
2134         next_tgid = filp->f_version;
2135         filp->f_version = 0;
2136         for (;;) {
2137                 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2138                 if (!nr_tgids) {
2139                         /* no more entries ! */
2140                         break;
2141                 }
2142                 next_tgid = 0;
2143
2144                 /* do not use the last found pid, reserve it for next_tgid */
2145                 if (nr_tgids == PROC_MAXPIDS) {
2146                         nr_tgids--;
2147                         next_tgid = tgid_array[nr_tgids];
2148                 }
2149
2150                 for (i=0;i<nr_tgids;i++) {
2151                         int tgid = tgid_array[i];
2152                         ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2153                         unsigned long j = PROC_NUMBUF;
2154
2155                         do
2156                                 buf[--j] = '0' + (tgid % 10);
2157                         while ((tgid /= 10) != 0);
2158
2159                         if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2160                                 /* returning this tgid failed, save it as the first
2161                                  * pid for the next readir call */
2162                                 filp->f_version = tgid_array[i];
2163                                 goto out;
2164                         }
2165                         filp->f_pos++;
2166                         nr++;
2167                 }
2168         }
2169 out:
2170         return 0;
2171 }
2172
2173 /* for the /proc/TGID/task/ directories */
2174 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2175 {
2176         unsigned int tid_array[PROC_MAXPIDS];
2177         char buf[PROC_NUMBUF];
2178         unsigned int nr_tids, i;
2179         struct dentry *dentry = filp->f_dentry;
2180         struct inode *inode = dentry->d_inode;
2181         int retval = -ENOENT;
2182         ino_t ino;
2183         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
2184
2185         if (!pid_alive(proc_task(inode)))
2186                 goto out;
2187         retval = 0;
2188
2189         switch (pos) {
2190         case 0:
2191                 ino = inode->i_ino;
2192                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2193                         goto out;
2194                 pos++;
2195                 /* fall through */
2196         case 1:
2197                 ino = parent_ino(dentry);
2198                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2199                         goto out;
2200                 pos++;
2201                 /* fall through */
2202         }
2203
2204         nr_tids = get_tid_list(pos, tid_array, inode);
2205
2206         for (i = 0; i < nr_tids; i++) {
2207                 unsigned long j = PROC_NUMBUF;
2208                 int tid = tid_array[i];
2209
2210                 ino = fake_ino(tid,PROC_TID_INO);
2211
2212                 do
2213                         buf[--j] = '0' + (tid % 10);
2214                 while ((tid /= 10) != 0);
2215
2216                 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
2217                         break;
2218                 pos++;
2219         }
2220 out:
2221         filp->f_pos = pos;
2222         return retval;
2223 }
2224
2225 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2226 {
2227         struct inode *inode = dentry->d_inode;
2228         struct task_struct *p = proc_task(inode);
2229         generic_fillattr(inode, stat);
2230
2231         if (pid_alive(p)) {
2232                 task_lock(p);
2233                 if (p->signal)
2234                         stat->nlink += atomic_read(&p->signal->count);
2235                 task_unlock(p);
2236         }
2237
2238         return 0;
2239 }