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