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