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