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