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