update to 2.6.9-rc1
[linux-flexiantxendom0-3.2.10.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 #include <asm/uaccess.h>
17
18 #include <linux/config.h>
19 #include <linux/errno.h>
20 #include <linux/time.h>
21 #include <linux/proc_fs.h>
22 #include <linux/stat.h>
23 #include <linux/init.h>
24 #include <linux/file.h>
25 #include <linux/string.h>
26 #include <linux/seq_file.h>
27 #include <linux/namei.h>
28 #include <linux/namespace.h>
29 #include <linux/mm.h>
30 #include <linux/smp_lock.h>
31 #include <linux/kallsyms.h>
32 #include <linux/mount.h>
33 #include <linux/security.h>
34 #include <linux/ptrace.h>
35
36 /*
37  * For hysterical raisins we keep the same inumbers as in the old procfs.
38  * Feel free to change the macro below - just keep the range distinct from
39  * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
40  * As soon as we'll get a separate superblock we will be able to forget
41  * about magical ranges too.
42  */
43
44 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
45
46 enum pid_directory_inos {
47         PROC_TGID_INO = 2,
48         PROC_TGID_TASK,
49         PROC_TGID_STATUS,
50         PROC_TGID_MEM,
51         PROC_TGID_CWD,
52         PROC_TGID_ROOT,
53         PROC_TGID_EXE,
54         PROC_TGID_FD,
55         PROC_TGID_ENVIRON,
56         PROC_TGID_AUXV,
57         PROC_TGID_CMDLINE,
58         PROC_TGID_STAT,
59         PROC_TGID_STATM,
60         PROC_TGID_MAPS,
61         PROC_TGID_MOUNTS,
62         PROC_TGID_WCHAN,
63         PROC_TGID_MAPBASE,
64 #ifdef CONFIG_SECURITY
65         PROC_TGID_ATTR,
66         PROC_TGID_ATTR_CURRENT,
67         PROC_TGID_ATTR_PREV,
68         PROC_TGID_ATTR_EXEC,
69         PROC_TGID_ATTR_FSCREATE,
70 #endif
71         PROC_TGID_FD_DIR,
72         PROC_TID_INO,
73         PROC_TID_STATUS,
74         PROC_TID_MEM,
75         PROC_TID_CWD,
76         PROC_TID_ROOT,
77         PROC_TID_EXE,
78         PROC_TID_FD,
79         PROC_TID_ENVIRON,
80         PROC_TID_AUXV,
81         PROC_TID_CMDLINE,
82         PROC_TID_STAT,
83         PROC_TID_STATM,
84         PROC_TID_MAPS,
85         PROC_TID_MOUNTS,
86         PROC_TID_WCHAN,
87 #ifdef CONFIG_SECURITY
88         PROC_TID_ATTR,
89         PROC_TID_ATTR_CURRENT,
90         PROC_TID_ATTR_PREV,
91         PROC_TID_ATTR_EXEC,
92         PROC_TID_ATTR_FSCREATE,
93 #endif
94         PROC_TID_FD_DIR = 0x8000,       /* 0x8000-0xffff */
95 };
96
97 struct pid_entry {
98         int type;
99         int len;
100         char *name;
101         mode_t mode;
102 };
103
104 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
105
106 static struct pid_entry tgid_base_stuff[] = {
107         E(PROC_TGID_TASK,      "task",    S_IFDIR|S_IRUGO|S_IXUGO),
108         E(PROC_TGID_FD,        "fd",      S_IFDIR|S_IRUSR|S_IXUSR),
109         E(PROC_TGID_ENVIRON,   "environ", S_IFREG|S_IRUSR),
110         E(PROC_TGID_AUXV,      "auxv",    S_IFREG|S_IRUSR),
111         E(PROC_TGID_STATUS,    "status",  S_IFREG|S_IRUGO),
112         E(PROC_TGID_CMDLINE,   "cmdline", S_IFREG|S_IRUGO),
113         E(PROC_TGID_STAT,      "stat",    S_IFREG|S_IRUGO),
114         E(PROC_TGID_STATM,     "statm",   S_IFREG|S_IRUGO),
115         E(PROC_TGID_MAPS,      "maps",    S_IFREG|S_IRUGO),
116         E(PROC_TGID_MEM,       "mem",     S_IFREG|S_IRUSR|S_IWUSR),
117         E(PROC_TGID_CWD,       "cwd",     S_IFLNK|S_IRWXUGO),
118         E(PROC_TGID_ROOT,      "root",    S_IFLNK|S_IRWXUGO),
119         E(PROC_TGID_EXE,       "exe",     S_IFLNK|S_IRWXUGO),
120         E(PROC_TGID_MOUNTS,    "mounts",  S_IFREG|S_IRUGO),
121 #ifdef __HAS_ARCH_PROC_MAPPED_BASE
122         E(PROC_TGID_MAPBASE,    "mapped_base",  S_IFREG|S_IRUSR|S_IWUSR),
123 #endif
124 #ifdef CONFIG_SECURITY
125         E(PROC_TGID_ATTR,      "attr",    S_IFDIR|S_IRUGO|S_IXUGO),
126 #endif
127 #ifdef CONFIG_KALLSYMS
128         E(PROC_TGID_WCHAN,     "wchan",   S_IFREG|S_IRUGO),
129 #endif
130         {0,0,NULL,0}
131 };
132 static struct pid_entry tid_base_stuff[] = {
133         E(PROC_TID_FD,         "fd",      S_IFDIR|S_IRUSR|S_IXUSR),
134         E(PROC_TID_ENVIRON,    "environ", S_IFREG|S_IRUSR),
135         E(PROC_TID_AUXV,       "auxv",    S_IFREG|S_IRUSR),
136         E(PROC_TID_STATUS,     "status",  S_IFREG|S_IRUGO),
137         E(PROC_TID_CMDLINE,    "cmdline", S_IFREG|S_IRUGO),
138         E(PROC_TID_STAT,       "stat",    S_IFREG|S_IRUGO),
139         E(PROC_TID_STATM,      "statm",   S_IFREG|S_IRUGO),
140         E(PROC_TID_MAPS,       "maps",    S_IFREG|S_IRUGO),
141         E(PROC_TID_MEM,        "mem",     S_IFREG|S_IRUSR|S_IWUSR),
142         E(PROC_TID_CWD,        "cwd",     S_IFLNK|S_IRWXUGO),
143         E(PROC_TID_ROOT,       "root",    S_IFLNK|S_IRWXUGO),
144         E(PROC_TID_EXE,        "exe",     S_IFLNK|S_IRWXUGO),
145         E(PROC_TID_MOUNTS,     "mounts",  S_IFREG|S_IRUGO),
146 #ifdef CONFIG_SECURITY
147         E(PROC_TID_ATTR,       "attr",    S_IFDIR|S_IRUGO|S_IXUGO),
148 #endif
149 #ifdef CONFIG_KALLSYMS
150         E(PROC_TID_WCHAN,      "wchan",   S_IFREG|S_IRUGO),
151 #endif
152         {0,0,NULL,0}
153 };
154
155 #ifdef CONFIG_SECURITY
156 static struct pid_entry tgid_attr_stuff[] = {
157         E(PROC_TGID_ATTR_CURRENT,  "current",  S_IFREG|S_IRUGO|S_IWUGO),
158         E(PROC_TGID_ATTR_PREV,     "prev",     S_IFREG|S_IRUGO),
159         E(PROC_TGID_ATTR_EXEC,     "exec",     S_IFREG|S_IRUGO|S_IWUGO),
160         E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
161         {0,0,NULL,0}
162 };
163 static struct pid_entry tid_attr_stuff[] = {
164         E(PROC_TID_ATTR_CURRENT,   "current",  S_IFREG|S_IRUGO|S_IWUGO),
165         E(PROC_TID_ATTR_PREV,      "prev",     S_IFREG|S_IRUGO),
166         E(PROC_TID_ATTR_EXEC,      "exec",     S_IFREG|S_IRUGO|S_IWUGO),
167         E(PROC_TID_ATTR_FSCREATE,  "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
168         {0,0,NULL,0}
169 };
170 #endif
171
172 #undef E
173
174 static inline struct task_struct *proc_task(struct inode *inode)
175 {
176         return PROC_I(inode)->task;
177 }
178
179 static inline int proc_type(struct inode *inode)
180 {
181         return PROC_I(inode)->type;
182 }
183
184 int proc_pid_stat(struct task_struct*,char*);
185 int proc_pid_status(struct task_struct*,char*);
186 int proc_pid_statm(struct task_struct*,char*);
187
188 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
189 {
190         struct task_struct *task = proc_task(inode);
191         struct files_struct *files;
192         struct file *file;
193         int fd = proc_type(inode) - PROC_TID_FD_DIR;
194
195         files = get_files_struct(task);
196         if (files) {
197                 spin_lock(&files->file_lock);
198                 file = fcheck_files(files, fd);
199                 if (file) {
200                         *mnt = mntget(file->f_vfsmnt);
201                         *dentry = dget(file->f_dentry);
202                         spin_unlock(&files->file_lock);
203                         put_files_struct(files);
204                         return 0;
205                 }
206                 spin_unlock(&files->file_lock);
207                 put_files_struct(files);
208         }
209         return -ENOENT;
210 }
211
212 static int proc_exe_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
213 {
214         struct vm_area_struct * vma;
215         int result = -ENOENT;
216         struct task_struct *task = proc_task(inode);
217         struct mm_struct * mm = get_task_mm(task);
218
219         if (!mm)
220                 goto out;
221         down_read(&mm->mmap_sem);
222         vma = mm->mmap;
223         while (vma) {
224                 if ((vma->vm_flags & VM_EXECUTABLE) && 
225                     vma->vm_file) {
226                         *mnt = mntget(vma->vm_file->f_vfsmnt);
227                         *dentry = dget(vma->vm_file->f_dentry);
228                         result = 0;
229                         break;
230                 }
231                 vma = vma->vm_next;
232         }
233         up_read(&mm->mmap_sem);
234         mmput(mm);
235 out:
236         return result;
237 }
238
239 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
240 {
241         struct fs_struct *fs;
242         int result = -ENOENT;
243         task_lock(proc_task(inode));
244         fs = proc_task(inode)->fs;
245         if(fs)
246                 atomic_inc(&fs->count);
247         task_unlock(proc_task(inode));
248         if (fs) {
249                 read_lock(&fs->lock);
250                 *mnt = mntget(fs->pwdmnt);
251                 *dentry = dget(fs->pwd);
252                 read_unlock(&fs->lock);
253                 result = 0;
254                 put_fs_struct(fs);
255         }
256         return result;
257 }
258
259 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
260 {
261         struct fs_struct *fs;
262         int result = -ENOENT;
263         task_lock(proc_task(inode));
264         fs = proc_task(inode)->fs;
265         if(fs)
266                 atomic_inc(&fs->count);
267         task_unlock(proc_task(inode));
268         if (fs) {
269                 read_lock(&fs->lock);
270                 *mnt = mntget(fs->rootmnt);
271                 *dentry = dget(fs->root);
272                 read_unlock(&fs->lock);
273                 result = 0;
274                 put_fs_struct(fs);
275         }
276         return result;
277 }
278
279 #define MAY_PTRACE(task) \
280         (task == current || \
281         (task->parent == current && \
282         (task->ptrace & PT_PTRACED) &&  task->state == TASK_STOPPED && \
283          security_ptrace(current,task) == 0))
284
285 static int may_ptrace_attach(struct task_struct *task)
286 {
287         int retval = 0;
288
289         task_lock(task);
290
291         if (!task->mm)
292                 goto out;
293         if (((current->uid != task->euid) ||
294              (current->uid != task->suid) ||
295              (current->uid != task->uid) ||
296              (current->gid != task->egid) ||
297              (current->gid != task->sgid) ||
298              (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
299                 goto out;
300         rmb();
301         if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
302                 goto out;
303         if (security_ptrace(current, task))
304                 goto out;
305
306         retval = 1;
307 out:
308         task_unlock(task);
309         return retval;
310 }
311
312 static int proc_pid_environ(struct task_struct *task, char * buffer)
313 {
314         int res = 0;
315         struct mm_struct *mm = get_task_mm(task);
316         if (mm) {
317                 unsigned int len = mm->env_end - mm->env_start;
318                 if (len > PAGE_SIZE)
319                         len = PAGE_SIZE;
320                 res = access_process_vm(task, mm->env_start, buffer, len, 0);
321                 if (!may_ptrace_attach(task))
322                         res = -ESRCH;
323                 mmput(mm);
324         }
325         return res;
326 }
327
328 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
329 {
330         int res = 0;
331         unsigned int len;
332         struct mm_struct *mm = get_task_mm(task);
333         if (!mm)
334                 goto out;
335         if (!mm->arg_end)
336                 goto out;       /* Shh! No looking before we're done */
337
338         len = mm->arg_end - mm->arg_start;
339  
340         if (len > PAGE_SIZE)
341                 len = PAGE_SIZE;
342  
343         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
344
345         // If the nul at the end of args has been overwritten, then
346         // assume application is using setproctitle(3).
347         if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
348                 len = strnlen(buffer, res);
349                 if (len < res) {
350                     res = len;
351                 } else {
352                         len = mm->env_end - mm->env_start;
353                         if (len > PAGE_SIZE - res)
354                                 len = PAGE_SIZE - res;
355                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
356                         res = strnlen(buffer, res);
357                 }
358         }
359         mmput(mm);
360
361 out:
362         return res;
363 }
364
365 static int proc_pid_auxv(struct task_struct *task, char *buffer)
366 {
367         int res = 0;
368         struct mm_struct *mm = get_task_mm(task);
369         if (mm) {
370                 unsigned int nwords = 0;
371                 do
372                         nwords += 2;
373                 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
374                 res = nwords * sizeof(mm->saved_auxv[0]);
375                 if (res > PAGE_SIZE)
376                         res = PAGE_SIZE;
377                 memcpy(buffer, mm->saved_auxv, res);
378                 mmput(mm);
379         }
380         return res;
381 }
382
383
384 #ifdef CONFIG_KALLSYMS
385 /*
386  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
387  * Returns the resolved symbol.  If that fails, simply return the address.
388  */
389 static int proc_pid_wchan(struct task_struct *task, char *buffer)
390 {
391         char *modname;
392         const char *sym_name;
393         unsigned long wchan, size, offset;
394         char namebuf[128];
395
396         wchan = get_wchan(task);
397
398         sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
399         if (sym_name)
400                 return sprintf(buffer, "%s", sym_name);
401         return sprintf(buffer, "%lu", wchan);
402 }
403 #endif /* CONFIG_KALLSYMS */
404
405 /************************************************************************/
406 /*                       Here the fs part begins                        */
407 /************************************************************************/
408
409 /* permission checks */
410
411 static int proc_check_root(struct inode *inode)
412 {
413         struct dentry *de, *base, *root;
414         struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
415         int res = 0;
416
417         if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
418                 return -ENOENT;
419         read_lock(&current->fs->lock);
420         our_vfsmnt = mntget(current->fs->rootmnt);
421         base = dget(current->fs->root);
422         read_unlock(&current->fs->lock);
423
424         spin_lock(&vfsmount_lock);
425         de = root;
426         mnt = vfsmnt;
427
428         while (vfsmnt != our_vfsmnt) {
429                 if (vfsmnt == vfsmnt->mnt_parent)
430                         goto out;
431                 de = vfsmnt->mnt_mountpoint;
432                 vfsmnt = vfsmnt->mnt_parent;
433         }
434
435         if (!is_subdir(de, base))
436                 goto out;
437         spin_unlock(&vfsmount_lock);
438
439 exit:
440         dput(base);
441         mntput(our_vfsmnt);
442         dput(root);
443         mntput(mnt);
444         return res;
445 out:
446         spin_unlock(&vfsmount_lock);
447         res = -EACCES;
448         goto exit;
449 }
450
451 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
452 {
453         if (vfs_permission(inode, mask) != 0)
454                 return -EACCES;
455         return proc_check_root(inode);
456 }
457
458 extern struct seq_operations proc_pid_maps_op;
459 static int maps_open(struct inode *inode, struct file *file)
460 {
461         struct task_struct *task = proc_task(inode);
462         int ret = seq_open(file, &proc_pid_maps_op);
463         if (!ret) {
464                 struct seq_file *m = file->private_data;
465                 m->private = task;
466         }
467         return ret;
468 }
469
470 static struct file_operations proc_maps_operations = {
471         .open           = maps_open,
472         .read           = seq_read,
473         .llseek         = seq_lseek,
474         .release        = seq_release,
475 };
476
477 extern struct seq_operations mounts_op;
478 static int mounts_open(struct inode *inode, struct file *file)
479 {
480         struct task_struct *task = proc_task(inode);
481         int ret = seq_open(file, &mounts_op);
482
483         if (!ret) {
484                 struct seq_file *m = file->private_data;
485                 struct namespace *namespace;
486                 task_lock(task);
487                 namespace = task->namespace;
488                 if (namespace)
489                         get_namespace(namespace);
490                 task_unlock(task);
491
492                 if (namespace)
493                         m->private = namespace;
494                 else {
495                         seq_release(inode, file);
496                         ret = -EINVAL;
497                 }
498         }
499         return ret;
500 }
501
502 static int mounts_release(struct inode *inode, struct file *file)
503 {
504         struct seq_file *m = file->private_data;
505         struct namespace *namespace = m->private;
506         put_namespace(namespace);
507         return seq_release(inode, file);
508 }
509
510 static struct file_operations proc_mounts_operations = {
511         .open           = mounts_open,
512         .read           = seq_read,
513         .llseek         = seq_lseek,
514         .release        = mounts_release,
515 };
516
517 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
518
519 static ssize_t proc_info_read(struct file * file, char __user * buf,
520                           size_t count, loff_t *ppos)
521 {
522         struct inode * inode = file->f_dentry->d_inode;
523         unsigned long page;
524         ssize_t length;
525         ssize_t end;
526         struct task_struct *task = proc_task(inode);
527
528         if (count > PROC_BLOCK_SIZE)
529                 count = PROC_BLOCK_SIZE;
530         if (!(page = __get_free_page(GFP_KERNEL)))
531                 return -ENOMEM;
532
533         length = PROC_I(inode)->op.proc_read(task, (char*)page);
534
535         if (length < 0) {
536                 free_page(page);
537                 return length;
538         }
539         /* Static 4kB (or whatever) block capacity */
540         if (*ppos >= length) {
541                 free_page(page);
542                 return 0;
543         }
544         if (count + *ppos > length)
545                 count = length - *ppos;
546         end = count + *ppos;
547         if (copy_to_user(buf, (char *) page + *ppos, count))
548                 count = -EFAULT;
549         else
550                 *ppos = end;
551         free_page(page);
552         return count;
553 }
554
555 static struct file_operations proc_info_file_operations = {
556         .read           = proc_info_read,
557 };
558
559 static int mem_open(struct inode* inode, struct file* file)
560 {
561         file->private_data = (void*)((long)current->self_exec_id);
562         return 0;
563 }
564
565 static ssize_t mem_read(struct file * file, char __user * buf,
566                         size_t count, loff_t *ppos)
567 {
568         struct task_struct *task = proc_task(file->f_dentry->d_inode);
569         char *page;
570         unsigned long src = *ppos;
571         int ret = -ESRCH;
572         struct mm_struct *mm;
573
574         if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
575                 goto out;
576
577         ret = -ENOMEM;
578         page = (char *)__get_free_page(GFP_USER);
579         if (!page)
580                 goto out;
581
582         ret = 0;
583  
584         mm = get_task_mm(task);
585         if (!mm)
586                 goto out_free;
587
588         ret = -EIO;
589  
590         if (file->private_data != (void*)((long)current->self_exec_id))
591                 goto out_put;
592
593         ret = 0;
594  
595         while (count > 0) {
596                 int this_len, retval;
597
598                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
599                 retval = access_process_vm(task, src, page, this_len, 0);
600                 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
601                         if (!ret)
602                                 ret = -EIO;
603                         break;
604                 }
605
606                 if (copy_to_user(buf, page, retval)) {
607                         ret = -EFAULT;
608                         break;
609                 }
610  
611                 ret += retval;
612                 src += retval;
613                 buf += retval;
614                 count -= retval;
615         }
616         *ppos = src;
617
618 out_put:
619         mmput(mm);
620 out_free:
621         free_page((unsigned long) page);
622 out:
623         return ret;
624 }
625
626 #define mem_write NULL
627
628 #ifndef mem_write
629 /* This is a security hazard */
630 static ssize_t mem_write(struct file * file, const char * buf,
631                          size_t count, loff_t *ppos)
632 {
633         int copied = 0;
634         char *page;
635         struct task_struct *task = proc_task(file->f_dentry->d_inode);
636         unsigned long dst = *ppos;
637
638         if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
639                 return -ESRCH;
640
641         page = (char *)__get_free_page(GFP_USER);
642         if (!page)
643                 return -ENOMEM;
644
645         while (count > 0) {
646                 int this_len, retval;
647
648                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
649                 if (copy_from_user(page, buf, this_len)) {
650                         copied = -EFAULT;
651                         break;
652                 }
653                 retval = access_process_vm(task, dst, page, this_len, 1);
654                 if (!retval) {
655                         if (!copied)
656                                 copied = -EIO;
657                         break;
658                 }
659                 copied += retval;
660                 buf += retval;
661                 dst += retval;
662                 count -= retval;                        
663         }
664         *ppos = dst;
665         free_page((unsigned long) page);
666         return copied;
667 }
668 #endif
669
670 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
671 {
672         switch (orig) {
673         case 0:
674                 file->f_pos = offset;
675                 break;
676         case 1:
677                 file->f_pos += offset;
678                 break;
679         default:
680                 return -EINVAL;
681         }
682         force_successful_syscall_return();
683         return file->f_pos;
684 }
685
686 static struct file_operations proc_mem_operations = {
687         .llseek         = mem_lseek,
688         .read           = mem_read,
689         .write          = mem_write,
690         .open           = mem_open,
691 };
692
693 #ifdef __HAS_ARCH_PROC_MAPPED_BASE
694 static ssize_t mapbase_read(struct file * file, char * buf,
695                         size_t count, loff_t *ppos)
696 {
697         struct task_struct *task = proc_task(file->f_dentry->d_inode);
698         char buffer[64];
699         size_t len;
700
701         len = sprintf(buffer, "%li\n", task->map_base) + 1;
702         if (*ppos >= len)
703                 return 0;
704         if (count > len-*ppos)
705                 count = len-*ppos;
706         if (copy_to_user(buf, buffer + *ppos, count)) 
707                 return -EFAULT;
708         *ppos += count;
709         return count;
710 }
711
712 static ssize_t mapbase_write(struct file * file, const char * buf,
713                          size_t count, loff_t *ppos)
714 {
715         struct task_struct *task = proc_task(file->f_dentry->d_inode);
716         char buffer[64], *end;
717         unsigned long newbase;
718
719         if (!capable(CAP_SYS_ADMIN))
720                 return -EPERM;
721         memset(buffer, 0, 64);  
722         if (count > 62)
723                 count = 62;
724         if (copy_from_user(buffer, buf, count)) 
725                 return -EFAULT;
726         newbase = simple_strtoul(buffer, &end, 0);
727         if (*end == '\n')
728                 end++;
729         if (newbase > 0)
730                 task->map_base = newbase;
731         if (end - buffer == 0) 
732                 return -EIO;
733         return end - buffer;
734 }
735
736 static struct file_operations proc_mapbase_operations = {
737         read:           mapbase_read,
738         write:          mapbase_write,
739 };
740 #endif /* __HAS_ARCH_PROC_MAPPED_BASE */
741
742 static struct inode_operations proc_mem_inode_operations = {
743         .permission     = proc_permission,
744 };
745
746 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
747 {
748         struct inode *inode = dentry->d_inode;
749         int error = -EACCES;
750
751         /* We don't need a base pointer in the /proc filesystem */
752         path_release(nd);
753
754         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
755                 goto out;
756         error = proc_check_root(inode);
757         if (error)
758                 goto out;
759
760         error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
761         nd->last_type = LAST_BIND;
762 out:
763         return error;
764 }
765
766 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
767                             char __user *buffer, int buflen)
768 {
769         struct inode * inode;
770         char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
771         int len;
772
773         if (!tmp)
774                 return -ENOMEM;
775                 
776         inode = dentry->d_inode;
777         path = d_path(dentry, mnt, tmp, PAGE_SIZE);
778         len = PTR_ERR(path);
779         if (IS_ERR(path))
780                 goto out;
781         len = tmp + PAGE_SIZE - 1 - path;
782
783         if (len > buflen)
784                 len = buflen;
785         if (copy_to_user(buffer, path, len))
786                 len = -EFAULT;
787  out:
788         free_page((unsigned long)tmp);
789         return len;
790 }
791
792 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
793 {
794         int error = -EACCES;
795         struct inode *inode = dentry->d_inode;
796         struct dentry *de;
797         struct vfsmount *mnt = NULL;
798
799         lock_kernel();
800
801         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
802                 goto out;
803         error = proc_check_root(inode);
804         if (error)
805                 goto out;
806
807         error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
808         if (error)
809                 goto out;
810
811         error = do_proc_readlink(de, mnt, buffer, buflen);
812         dput(de);
813         mntput(mnt);
814 out:
815         unlock_kernel();
816         return error;
817 }
818
819 static struct inode_operations proc_pid_link_inode_operations = {
820         .readlink       = proc_pid_readlink,
821         .follow_link    = proc_pid_follow_link
822 };
823
824 static int pid_alive(struct task_struct *p)
825 {
826         BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
827         return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
828 }
829
830 #define NUMBUF 10
831
832 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
833 {
834         struct inode *inode = filp->f_dentry->d_inode;
835         struct task_struct *p = proc_task(inode);
836         unsigned int fd, tid, ino;
837         int retval;
838         char buf[NUMBUF];
839         struct files_struct * files;
840
841         retval = -ENOENT;
842         if (!pid_alive(p))
843                 goto out;
844         retval = 0;
845         tid = p->pid;
846
847         fd = filp->f_pos;
848         switch (fd) {
849                 case 0:
850                         if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
851                                 goto out;
852                         filp->f_pos++;
853                 case 1:
854                         ino = fake_ino(tid, PROC_TID_INO);
855                         if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
856                                 goto out;
857                         filp->f_pos++;
858                 default:
859                         files = get_files_struct(p);
860                         if (!files)
861                                 goto out;
862                         spin_lock(&files->file_lock);
863                         for (fd = filp->f_pos-2;
864                              fd < files->max_fds;
865                              fd++, filp->f_pos++) {
866                                 unsigned int i,j;
867
868                                 if (!fcheck_files(files, fd))
869                                         continue;
870                                 spin_unlock(&files->file_lock);
871
872                                 j = NUMBUF;
873                                 i = fd;
874                                 do {
875                                         j--;
876                                         buf[j] = '0' + (i % 10);
877                                         i /= 10;
878                                 } while (i);
879
880                                 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
881                                 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
882                                         spin_lock(&files->file_lock);
883                                         break;
884                                 }
885                                 spin_lock(&files->file_lock);
886                         }
887                         spin_unlock(&files->file_lock);
888                         put_files_struct(files);
889         }
890 out:
891         return retval;
892 }
893
894 static int proc_pident_readdir(struct file *filp,
895                 void *dirent, filldir_t filldir,
896                 struct pid_entry *ents, unsigned int nents)
897 {
898         int i;
899         int pid;
900         struct dentry *dentry = filp->f_dentry;
901         struct inode *inode = dentry->d_inode;
902         struct pid_entry *p;
903         ino_t ino;
904         int ret;
905
906         ret = -ENOENT;
907         if (!pid_alive(proc_task(inode)))
908                 goto out;
909
910         ret = 0;
911         pid = proc_task(inode)->pid;
912         i = filp->f_pos;
913         switch (i) {
914         case 0:
915                 ino = inode->i_ino;
916                 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
917                         goto out;
918                 i++;
919                 filp->f_pos++;
920                 /* fall through */
921         case 1:
922                 ino = parent_ino(dentry);
923                 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
924                         goto out;
925                 i++;
926                 filp->f_pos++;
927                 /* fall through */
928         default:
929                 i -= 2;
930                 if (i >= nents) {
931                         ret = 1;
932                         goto out;
933                 }
934                 p = ents + i;
935                 while (p->name) {
936                         if (filldir(dirent, p->name, p->len, filp->f_pos,
937                                     fake_ino(pid, p->type), p->mode >> 12) < 0)
938                                 goto out;
939                         filp->f_pos++;
940                         p++;
941                 }
942         }
943
944         ret = 1;
945 out:
946         return ret;
947 }
948
949 static int proc_tgid_base_readdir(struct file * filp,
950                              void * dirent, filldir_t filldir)
951 {
952         return proc_pident_readdir(filp,dirent,filldir,
953                                    tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
954 }
955
956 static int proc_tid_base_readdir(struct file * filp,
957                              void * dirent, filldir_t filldir)
958 {
959         return proc_pident_readdir(filp,dirent,filldir,
960                                    tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
961 }
962
963 /* building an inode */
964
965 static int task_dumpable(struct task_struct *task)
966 {
967         int dumpable = 0;
968         struct mm_struct *mm;
969
970         task_lock(task);
971         mm = task->mm;
972         if (mm)
973                 dumpable = mm->dumpable;
974         task_unlock(task);
975         return dumpable;
976 }
977
978
979 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
980 {
981         struct inode * inode;
982         struct proc_inode *ei;
983
984         /* We need a new inode */
985         
986         inode = new_inode(sb);
987         if (!inode)
988                 goto out;
989
990         /* Common stuff */
991         ei = PROC_I(inode);
992         ei->task = NULL;
993         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
994         inode->i_ino = fake_ino(task->pid, ino);
995
996         if (!pid_alive(task))
997                 goto out_unlock;
998
999         /*
1000          * grab the reference to task.
1001          */
1002         get_task_struct(task);
1003         ei->task = task;
1004         ei->type = ino;
1005         inode->i_uid = 0;
1006         inode->i_gid = 0;
1007         if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1008                 inode->i_uid = task->euid;
1009                 inode->i_gid = task->egid;
1010         }
1011         security_task_to_inode(task, inode);
1012
1013 out:
1014         return inode;
1015
1016 out_unlock:
1017         ei->pde = NULL;
1018         iput(inode);
1019         return NULL;
1020 }
1021
1022 /* dentry stuff */
1023
1024 /*
1025  *      Exceptional case: normally we are not allowed to unhash a busy
1026  * directory. In this case, however, we can do it - no aliasing problems
1027  * due to the way we treat inodes.
1028  *
1029  * Rewrite the inode's ownerships here because the owning task may have
1030  * performed a setuid(), etc.
1031  */
1032 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1033 {
1034         struct inode *inode = dentry->d_inode;
1035         struct task_struct *task = proc_task(inode);
1036         if (pid_alive(task)) {
1037                 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1038                         inode->i_uid = task->euid;
1039                         inode->i_gid = task->egid;
1040                 } else {
1041                         inode->i_uid = 0;
1042                         inode->i_gid = 0;
1043                 }
1044                 security_task_to_inode(task, inode);
1045                 return 1;
1046         }
1047         d_drop(dentry);
1048         return 0;
1049 }
1050
1051 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1052 {
1053         struct inode *inode = dentry->d_inode;
1054         struct task_struct *task = proc_task(inode);
1055         int fd = proc_type(inode) - PROC_TID_FD_DIR;
1056         struct files_struct *files;
1057
1058         files = get_files_struct(task);
1059         if (files) {
1060                 spin_lock(&files->file_lock);
1061                 if (fcheck_files(files, fd)) {
1062                         spin_unlock(&files->file_lock);
1063                         put_files_struct(files);
1064                         if (task_dumpable(task)) {
1065                                 inode->i_uid = task->euid;
1066                                 inode->i_gid = task->egid;
1067                         } else {
1068                                 inode->i_uid = 0;
1069                                 inode->i_gid = 0;
1070                         }
1071                         security_task_to_inode(task, inode);
1072                         return 1;
1073                 }
1074                 spin_unlock(&files->file_lock);
1075                 put_files_struct(files);
1076         }
1077         d_drop(dentry);
1078         return 0;
1079 }
1080
1081 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1082 {
1083         struct task_struct *task = proc_task(inode);
1084         spin_lock(&task->proc_lock);
1085         if (task->proc_dentry == dentry)
1086                 task->proc_dentry = NULL;
1087         spin_unlock(&task->proc_lock);
1088         iput(inode);
1089 }
1090
1091 static int pid_delete_dentry(struct dentry * dentry)
1092 {
1093         /* Is the task we represent dead?
1094          * If so, then don't put the dentry on the lru list,
1095          * kill it immediately.
1096          */
1097         return !pid_alive(proc_task(dentry->d_inode));
1098 }
1099
1100 static struct dentry_operations tid_fd_dentry_operations =
1101 {
1102         .d_revalidate   = tid_fd_revalidate,
1103         .d_delete       = pid_delete_dentry,
1104 };
1105
1106 static struct dentry_operations pid_dentry_operations =
1107 {
1108         .d_revalidate   = pid_revalidate,
1109         .d_delete       = pid_delete_dentry,
1110 };
1111
1112 static struct dentry_operations pid_base_dentry_operations =
1113 {
1114         .d_revalidate   = pid_revalidate,
1115         .d_iput         = pid_base_iput,
1116         .d_delete       = pid_delete_dentry,
1117 };
1118
1119 /* Lookups */
1120
1121 static unsigned name_to_int(struct dentry *dentry)
1122 {
1123         const char *name = dentry->d_name.name;
1124         int len = dentry->d_name.len;
1125         unsigned n = 0;
1126
1127         if (len > 1 && *name == '0')
1128                 goto out;
1129         while (len-- > 0) {
1130                 unsigned c = *name++ - '0';
1131                 if (c > 9)
1132                         goto out;
1133                 if (n >= (~0U-9)/10)
1134                         goto out;
1135                 n *= 10;
1136                 n += c;
1137         }
1138         return n;
1139 out:
1140         return ~0U;
1141 }
1142
1143 /* SMP-safe */
1144 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1145 {
1146         struct task_struct *task = proc_task(dir);
1147         unsigned fd = name_to_int(dentry);
1148         struct file * file;
1149         struct files_struct * files;
1150         struct inode *inode;
1151         struct proc_inode *ei;
1152
1153         if (fd == ~0U)
1154                 goto out;
1155         if (!pid_alive(task))
1156                 goto out;
1157
1158         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1159         if (!inode)
1160                 goto out;
1161         ei = PROC_I(inode);
1162         files = get_files_struct(task);
1163         if (!files)
1164                 goto out_unlock;
1165         inode->i_mode = S_IFLNK;
1166         spin_lock(&files->file_lock);
1167         file = fcheck_files(files, fd);
1168         if (!file)
1169                 goto out_unlock2;
1170         if (file->f_mode & 1)
1171                 inode->i_mode |= S_IRUSR | S_IXUSR;
1172         if (file->f_mode & 2)
1173                 inode->i_mode |= S_IWUSR | S_IXUSR;
1174         spin_unlock(&files->file_lock);
1175         put_files_struct(files);
1176         inode->i_op = &proc_pid_link_inode_operations;
1177         inode->i_size = 64;
1178         ei->op.proc_get_link = proc_fd_link;
1179         dentry->d_op = &tid_fd_dentry_operations;
1180         d_add(dentry, inode);
1181         return NULL;
1182
1183 out_unlock2:
1184         spin_unlock(&files->file_lock);
1185         put_files_struct(files);
1186 out_unlock:
1187         iput(inode);
1188 out:
1189         return ERR_PTR(-ENOENT);
1190 }
1191
1192 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1193 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1194
1195 static struct file_operations proc_fd_operations = {
1196         .read           = generic_read_dir,
1197         .readdir        = proc_readfd,
1198 };
1199
1200 static struct file_operations proc_task_operations = {
1201         .read           = generic_read_dir,
1202         .readdir        = proc_task_readdir,
1203 };
1204
1205 /*
1206  * proc directories can do almost nothing..
1207  */
1208 static struct inode_operations proc_fd_inode_operations = {
1209         .lookup         = proc_lookupfd,
1210         .permission     = proc_permission,
1211 };
1212
1213 static struct inode_operations proc_task_inode_operations = {
1214         .lookup         = proc_task_lookup,
1215         .permission     = proc_permission,
1216 };
1217
1218 #ifdef CONFIG_SECURITY
1219 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1220                                   size_t count, loff_t *ppos)
1221 {
1222         struct inode * inode = file->f_dentry->d_inode;
1223         unsigned long page;
1224         ssize_t length;
1225         ssize_t end;
1226         struct task_struct *task = proc_task(inode);
1227
1228         if (count > PAGE_SIZE)
1229                 count = PAGE_SIZE;
1230         if (!(page = __get_free_page(GFP_KERNEL)))
1231                 return -ENOMEM;
1232
1233         length = security_getprocattr(task, 
1234                                       (char*)file->f_dentry->d_name.name, 
1235                                       (void*)page, count);
1236         if (length < 0) {
1237                 free_page(page);
1238                 return length;
1239         }
1240         /* Static 4kB (or whatever) block capacity */
1241         if (*ppos >= length) {
1242                 free_page(page);
1243                 return 0;
1244         }
1245         if (count + *ppos > length)
1246                 count = length - *ppos;
1247         end = count + *ppos;
1248         if (copy_to_user(buf, (char *) page + *ppos, count))
1249                 count = -EFAULT;
1250         else
1251                 *ppos = end;
1252         free_page(page);
1253         return count;
1254 }
1255
1256 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1257                                    size_t count, loff_t *ppos)
1258
1259         struct inode * inode = file->f_dentry->d_inode;
1260         char *page; 
1261         ssize_t length; 
1262         struct task_struct *task = proc_task(inode); 
1263
1264         if (count > PAGE_SIZE) 
1265                 count = PAGE_SIZE; 
1266         if (*ppos != 0) {
1267                 /* No partial writes. */
1268                 return -EINVAL;
1269         }
1270         page = (char*)__get_free_page(GFP_USER); 
1271         if (!page) 
1272                 return -ENOMEM;
1273         length = -EFAULT; 
1274         if (copy_from_user(page, buf, count)) 
1275                 goto out;
1276
1277         length = security_setprocattr(task, 
1278                                       (char*)file->f_dentry->d_name.name, 
1279                                       (void*)page, count);
1280 out:
1281         free_page((unsigned long) page);
1282         return length;
1283
1284
1285 static struct file_operations proc_pid_attr_operations = {
1286         .read           = proc_pid_attr_read,
1287         .write          = proc_pid_attr_write,
1288 };
1289
1290 static struct file_operations proc_tid_attr_operations;
1291 static struct inode_operations proc_tid_attr_inode_operations;
1292 static struct file_operations proc_tgid_attr_operations;
1293 static struct inode_operations proc_tgid_attr_inode_operations;
1294 #endif
1295
1296 /* SMP-safe */
1297 static struct dentry *proc_pident_lookup(struct inode *dir, 
1298                                          struct dentry *dentry,
1299                                          struct pid_entry *ents)
1300 {
1301         struct inode *inode;
1302         int error;
1303         struct task_struct *task = proc_task(dir);
1304         struct pid_entry *p;
1305         struct proc_inode *ei;
1306
1307         error = -ENOENT;
1308         inode = NULL;
1309
1310         if (!pid_alive(task))
1311                 goto out;
1312
1313         for (p = ents; p->name; p++) {
1314                 if (p->len != dentry->d_name.len)
1315                         continue;
1316                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1317                         break;
1318         }
1319         if (!p->name)
1320                 goto out;
1321
1322         error = -EINVAL;
1323         inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1324         if (!inode)
1325                 goto out;
1326
1327         ei = PROC_I(inode);
1328         inode->i_mode = p->mode;
1329         /*
1330          * Yes, it does not scale. And it should not. Don't add
1331          * new entries into /proc/<tgid>/ without very good reasons.
1332          */
1333         switch(p->type) {
1334                 case PROC_TGID_TASK:
1335                         inode->i_nlink = 3;
1336                         inode->i_op = &proc_task_inode_operations;
1337                         inode->i_fop = &proc_task_operations;
1338                         break;
1339                 case PROC_TID_FD:
1340                 case PROC_TGID_FD:
1341                         inode->i_nlink = 2;
1342                         inode->i_op = &proc_fd_inode_operations;
1343                         inode->i_fop = &proc_fd_operations;
1344                         break;
1345                 case PROC_TID_EXE:
1346                 case PROC_TGID_EXE:
1347                         inode->i_op = &proc_pid_link_inode_operations;
1348                         ei->op.proc_get_link = proc_exe_link;
1349                         break;
1350                 case PROC_TID_CWD:
1351                 case PROC_TGID_CWD:
1352                         inode->i_op = &proc_pid_link_inode_operations;
1353                         ei->op.proc_get_link = proc_cwd_link;
1354                         break;
1355                 case PROC_TID_ROOT:
1356                 case PROC_TGID_ROOT:
1357                         inode->i_op = &proc_pid_link_inode_operations;
1358                         ei->op.proc_get_link = proc_root_link;
1359                         break;
1360                 case PROC_TID_ENVIRON:
1361                 case PROC_TGID_ENVIRON:
1362                         inode->i_fop = &proc_info_file_operations;
1363                         ei->op.proc_read = proc_pid_environ;
1364                         break;
1365                 case PROC_TID_AUXV:
1366                 case PROC_TGID_AUXV:
1367                         inode->i_fop = &proc_info_file_operations;
1368                         ei->op.proc_read = proc_pid_auxv;
1369                         break;
1370                 case PROC_TID_STATUS:
1371                 case PROC_TGID_STATUS:
1372                         inode->i_fop = &proc_info_file_operations;
1373                         ei->op.proc_read = proc_pid_status;
1374                         break;
1375                 case PROC_TID_STAT:
1376                 case PROC_TGID_STAT:
1377                         inode->i_fop = &proc_info_file_operations;
1378                         ei->op.proc_read = proc_pid_stat;
1379                         break;
1380                 case PROC_TID_CMDLINE:
1381                 case PROC_TGID_CMDLINE:
1382                         inode->i_fop = &proc_info_file_operations;
1383                         ei->op.proc_read = proc_pid_cmdline;
1384                         break;
1385                 case PROC_TID_STATM:
1386                 case PROC_TGID_STATM:
1387                         inode->i_fop = &proc_info_file_operations;
1388                         ei->op.proc_read = proc_pid_statm;
1389                         break;
1390                 case PROC_TID_MAPS:
1391                 case PROC_TGID_MAPS:
1392                         inode->i_fop = &proc_maps_operations;
1393                         break;
1394 #ifdef __HAS_ARCH_PROC_MAPPED_BASE
1395                 case PROC_TGID_MAPBASE:
1396                         inode->i_fop = &proc_mapbase_operations;
1397                         break;
1398 #endif
1399                 case PROC_TID_MEM:
1400                 case PROC_TGID_MEM:
1401                         inode->i_op = &proc_mem_inode_operations;
1402                         inode->i_fop = &proc_mem_operations;
1403                         break;
1404                 case PROC_TID_MOUNTS:
1405                 case PROC_TGID_MOUNTS:
1406                         inode->i_fop = &proc_mounts_operations;
1407                         break;
1408 #ifdef CONFIG_SECURITY
1409                 case PROC_TID_ATTR:
1410                         inode->i_nlink = 2;
1411                         inode->i_op = &proc_tid_attr_inode_operations;
1412                         inode->i_fop = &proc_tid_attr_operations;
1413                         break;
1414                 case PROC_TGID_ATTR:
1415                         inode->i_nlink = 2;
1416                         inode->i_op = &proc_tgid_attr_inode_operations;
1417                         inode->i_fop = &proc_tgid_attr_operations;
1418                         break;
1419                 case PROC_TID_ATTR_CURRENT:
1420                 case PROC_TGID_ATTR_CURRENT:
1421                 case PROC_TID_ATTR_PREV:
1422                 case PROC_TGID_ATTR_PREV:
1423                 case PROC_TID_ATTR_EXEC:
1424                 case PROC_TGID_ATTR_EXEC:
1425                 case PROC_TID_ATTR_FSCREATE:
1426                 case PROC_TGID_ATTR_FSCREATE:
1427                         inode->i_fop = &proc_pid_attr_operations;
1428                         break;
1429 #endif
1430 #ifdef CONFIG_KALLSYMS
1431                 case PROC_TID_WCHAN:
1432                 case PROC_TGID_WCHAN:
1433                         inode->i_fop = &proc_info_file_operations;
1434                         ei->op.proc_read = proc_pid_wchan;
1435                         break;
1436 #endif
1437                 default:
1438                         printk("procfs: impossible type (%d)",p->type);
1439                         iput(inode);
1440                         return ERR_PTR(-EINVAL);
1441         }
1442         dentry->d_op = &pid_dentry_operations;
1443         d_add(dentry, inode);
1444         return NULL;
1445
1446 out:
1447         return ERR_PTR(error);
1448 }
1449
1450 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1451         return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1452 }
1453
1454 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1455         return proc_pident_lookup(dir, dentry, tid_base_stuff);
1456 }
1457
1458 static struct file_operations proc_tgid_base_operations = {
1459         .read           = generic_read_dir,
1460         .readdir        = proc_tgid_base_readdir,
1461 };
1462
1463 static struct file_operations proc_tid_base_operations = {
1464         .read           = generic_read_dir,
1465         .readdir        = proc_tid_base_readdir,
1466 };
1467
1468 static struct inode_operations proc_tgid_base_inode_operations = {
1469         .lookup         = proc_tgid_base_lookup,
1470 };
1471
1472 static struct inode_operations proc_tid_base_inode_operations = {
1473         .lookup         = proc_tid_base_lookup,
1474 };
1475
1476 #ifdef CONFIG_SECURITY
1477 static int proc_tgid_attr_readdir(struct file * filp,
1478                              void * dirent, filldir_t filldir)
1479 {
1480         return proc_pident_readdir(filp,dirent,filldir,
1481                                    tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1482 }
1483
1484 static int proc_tid_attr_readdir(struct file * filp,
1485                              void * dirent, filldir_t filldir)
1486 {
1487         return proc_pident_readdir(filp,dirent,filldir,
1488                                    tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1489 }
1490
1491 static struct file_operations proc_tgid_attr_operations = {
1492         .read           = generic_read_dir,
1493         .readdir        = proc_tgid_attr_readdir,
1494 };
1495
1496 static struct file_operations proc_tid_attr_operations = {
1497         .read           = generic_read_dir,
1498         .readdir        = proc_tid_attr_readdir,
1499 };
1500
1501 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1502                                 struct dentry *dentry, struct nameidata *nd)
1503 {
1504         return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1505 }
1506
1507 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1508                                 struct dentry *dentry, struct nameidata *nd)
1509 {
1510         return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1511 }
1512
1513 static struct inode_operations proc_tgid_attr_inode_operations = {
1514         .lookup         = proc_tgid_attr_lookup,
1515 };
1516
1517 static struct inode_operations proc_tid_attr_inode_operations = {
1518         .lookup         = proc_tid_attr_lookup,
1519 };
1520 #endif
1521
1522 /*
1523  * /proc/self:
1524  */
1525 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1526                               int buflen)
1527 {
1528         char tmp[30];
1529         sprintf(tmp, "%d", current->tgid);
1530         return vfs_readlink(dentry,buffer,buflen,tmp);
1531 }
1532
1533 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1534 {
1535         char tmp[30];
1536         sprintf(tmp, "%d", current->tgid);
1537         return vfs_follow_link(nd,tmp);
1538 }       
1539
1540 static struct inode_operations proc_self_inode_operations = {
1541         .readlink       = proc_self_readlink,
1542         .follow_link    = proc_self_follow_link,
1543 };
1544
1545 /**
1546  * proc_pid_unhash -  Unhash /proc/<pid> entry from the dcache.
1547  * @p: task that should be flushed.
1548  *
1549  * Drops the /proc/<pid> dcache entry from the hash chains.
1550  *
1551  * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1552  * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1553  * if the pid value is immediately reused. This is enforced by
1554  * - caller must acquire spin_lock(p->proc_lock)
1555  * - must be called before detach_pid()
1556  * - proc_pid_lookup acquires proc_lock, and checks that
1557  *   the target is not dead by looking at the attach count
1558  *   of PIDTYPE_PID.
1559  */
1560
1561 struct dentry *proc_pid_unhash(struct task_struct *p)
1562 {
1563         struct dentry *proc_dentry;
1564
1565         proc_dentry = p->proc_dentry;
1566         if (proc_dentry != NULL) {
1567
1568                 spin_lock(&dcache_lock);
1569                 if (!d_unhashed(proc_dentry)) {
1570                         dget_locked(proc_dentry);
1571                         __d_drop(proc_dentry);
1572                 } else
1573                         proc_dentry = NULL;
1574                 spin_unlock(&dcache_lock);
1575         }
1576         return proc_dentry;
1577 }
1578
1579 /**
1580  * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1581  * @proc_entry: directoy to prune.
1582  *
1583  * Shrink the /proc directory that was used by the just killed thread.
1584  */
1585         
1586 void proc_pid_flush(struct dentry *proc_dentry)
1587 {
1588         if(proc_dentry != NULL) {
1589                 shrink_dcache_parent(proc_dentry);
1590                 dput(proc_dentry);
1591         }
1592 }
1593
1594 /* SMP-safe */
1595 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1596 {
1597         struct task_struct *task;
1598         struct inode *inode;
1599         struct proc_inode *ei;
1600         unsigned tgid;
1601         int died;
1602
1603         if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1604                 inode = new_inode(dir->i_sb);
1605                 if (!inode)
1606                         return ERR_PTR(-ENOMEM);
1607                 ei = PROC_I(inode);
1608                 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1609                 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1610                 ei->pde = NULL;
1611                 inode->i_mode = S_IFLNK|S_IRWXUGO;
1612                 inode->i_uid = inode->i_gid = 0;
1613                 inode->i_size = 64;
1614                 inode->i_op = &proc_self_inode_operations;
1615                 d_add(dentry, inode);
1616                 return NULL;
1617         }
1618         tgid = name_to_int(dentry);
1619         if (tgid == ~0U)
1620                 goto out;
1621
1622         read_lock(&tasklist_lock);
1623         task = find_task_by_pid(tgid);
1624         if (task)
1625                 get_task_struct(task);
1626         read_unlock(&tasklist_lock);
1627         if (!task)
1628                 goto out;
1629
1630         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1631
1632
1633         if (!inode) {
1634                 put_task_struct(task);
1635                 goto out;
1636         }
1637         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1638         inode->i_op = &proc_tgid_base_inode_operations;
1639         inode->i_fop = &proc_tgid_base_operations;
1640         inode->i_nlink = 3;
1641         inode->i_flags|=S_IMMUTABLE;
1642
1643         dentry->d_op = &pid_base_dentry_operations;
1644
1645         died = 0;
1646         d_add(dentry, inode);
1647         spin_lock(&task->proc_lock);
1648         task->proc_dentry = dentry;
1649         if (!pid_alive(task)) {
1650                 dentry = proc_pid_unhash(task);
1651                 died = 1;
1652         }
1653         spin_unlock(&task->proc_lock);
1654
1655         put_task_struct(task);
1656         if (died) {
1657                 proc_pid_flush(dentry);
1658                 goto out;
1659         }
1660         return NULL;
1661 out:
1662         return ERR_PTR(-ENOENT);
1663 }
1664
1665 /* SMP-safe */
1666 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1667 {
1668         struct task_struct *task;
1669         struct task_struct *leader = proc_task(dir);
1670         struct inode *inode;
1671         unsigned tid;
1672
1673         tid = name_to_int(dentry);
1674         if (tid == ~0U)
1675                 goto out;
1676
1677         read_lock(&tasklist_lock);
1678         task = find_task_by_pid(tid);
1679         if (task)
1680                 get_task_struct(task);
1681         read_unlock(&tasklist_lock);
1682         if (!task)
1683                 goto out;
1684         if (leader->tgid != task->tgid)
1685                 goto out_drop_task;
1686
1687         inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1688
1689
1690         if (!inode)
1691                 goto out_drop_task;
1692         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1693         inode->i_op = &proc_tid_base_inode_operations;
1694         inode->i_fop = &proc_tid_base_operations;
1695         inode->i_nlink = 3;
1696         inode->i_flags|=S_IMMUTABLE;
1697
1698         dentry->d_op = &pid_base_dentry_operations;
1699
1700         d_add(dentry, inode);
1701
1702         put_task_struct(task);
1703         return NULL;
1704 out_drop_task:
1705         put_task_struct(task);
1706 out:
1707         return ERR_PTR(-ENOENT);
1708 }
1709
1710 #define PROC_NUMBUF 10
1711 #define PROC_MAXPIDS 20
1712
1713 /*
1714  * Get a few tgid's to return for filldir - we need to hold the
1715  * tasklist lock while doing this, and we must release it before
1716  * we actually do the filldir itself, so we use a temp buffer..
1717  */
1718 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1719 {
1720         struct task_struct *p;
1721         int nr_tgids = 0;
1722
1723         index--;
1724         read_lock(&tasklist_lock);
1725         p = NULL;
1726         if (version) {
1727                 p = find_task_by_pid(version);
1728                 if (!thread_group_leader(p))
1729                         p = NULL;
1730         }
1731
1732         if (p)
1733                 index = 0;
1734         else
1735                 p = next_task(&init_task);
1736
1737         for ( ; p != &init_task; p = next_task(p)) {
1738                 int tgid = p->pid;
1739                 if (!pid_alive(p))
1740                         continue;
1741                 if (--index >= 0)
1742                         continue;
1743                 tgids[nr_tgids] = tgid;
1744                 nr_tgids++;
1745                 if (nr_tgids >= PROC_MAXPIDS)
1746                         break;
1747         }
1748         read_unlock(&tasklist_lock);
1749         return nr_tgids;
1750 }
1751
1752 /*
1753  * Get a few tid's to return for filldir - we need to hold the
1754  * tasklist lock while doing this, and we must release it before
1755  * we actually do the filldir itself, so we use a temp buffer..
1756  */
1757 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1758 {
1759         struct task_struct *leader_task = proc_task(dir);
1760         struct task_struct *task = leader_task;
1761         int nr_tids = 0;
1762
1763         index -= 2;
1764         read_lock(&tasklist_lock);
1765         /*
1766          * The starting point task (leader_task) might be an already
1767          * unlinked task, which cannot be used to access the task-list
1768          * via next_thread().
1769          */
1770         if (pid_alive(task)) do {
1771                 int tid = task->pid;
1772
1773                 if (--index >= 0)
1774                         continue;
1775                 tids[nr_tids] = tid;
1776                 nr_tids++;
1777                 if (nr_tids >= PROC_MAXPIDS)
1778                         break;
1779         } while ((task = next_thread(task)) != leader_task);
1780         read_unlock(&tasklist_lock);
1781         return nr_tids;
1782 }
1783
1784 /* for the /proc/ directory itself, after non-process stuff has been done */
1785 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1786 {
1787         unsigned int tgid_array[PROC_MAXPIDS];
1788         char buf[PROC_NUMBUF];
1789         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1790         unsigned int nr_tgids, i;
1791
1792         if (!nr) {
1793                 ino_t ino = fake_ino(0,PROC_TGID_INO);
1794                 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1795                         return 0;
1796                 filp->f_pos++;
1797                 nr++;
1798         }
1799
1800         /*
1801          * f_version caches the last tgid which was returned from readdir
1802          */
1803         nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1804
1805         for (i = 0; i < nr_tgids; i++) {
1806                 int tgid = tgid_array[i];
1807                 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1808                 unsigned long j = PROC_NUMBUF;
1809
1810                 do
1811                         buf[--j] = '0' + (tgid % 10);
1812                 while ((tgid /= 10) != 0);
1813
1814                 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1815                         filp->f_version = tgid;
1816                         break;
1817                 }
1818                 filp->f_pos++;
1819         }
1820         return 0;
1821 }
1822
1823 /* for the /proc/TGID/task/ directories */
1824 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1825 {
1826         unsigned int tid_array[PROC_MAXPIDS];
1827         char buf[PROC_NUMBUF];
1828         unsigned int nr_tids, i;
1829         struct dentry *dentry = filp->f_dentry;
1830         struct inode *inode = dentry->d_inode;
1831         int retval = -ENOENT;
1832         ino_t ino;
1833         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
1834
1835         if (!pid_alive(proc_task(inode)))
1836                 goto out;
1837         retval = 0;
1838
1839         switch (pos) {
1840         case 0:
1841                 ino = inode->i_ino;
1842                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1843                         goto out;
1844                 pos++;
1845                 /* fall through */
1846         case 1:
1847                 ino = parent_ino(dentry);
1848                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1849                         goto out;
1850                 pos++;
1851                 /* fall through */
1852         }
1853
1854         nr_tids = get_tid_list(pos, tid_array, inode);
1855
1856         for (i = 0; i < nr_tids; i++) {
1857                 unsigned long j = PROC_NUMBUF;
1858                 int tid = tid_array[i];
1859
1860                 ino = fake_ino(tid,PROC_TID_INO);
1861
1862                 do
1863                         buf[--j] = '0' + (tid % 10);
1864                 while ((tid /= 10) != 0);
1865
1866                 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
1867                         break;
1868                 pos++;
1869         }
1870 out:
1871         filp->f_pos = pos;
1872         return retval;
1873 }