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