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/ninline.h>
36
37 /*
38  * For hysterical raisins we keep the same inumbers as in the old procfs.
39  * Feel free to change the macro below - just keep the range distinct from
40  * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
41  * As soon as we'll get a separate superblock we will be able to forget
42  * about magical ranges too.
43  */
44
45 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
46
47 enum pid_directory_inos {
48         PROC_TGID_INO = 2,
49         PROC_TGID_TASK,
50         PROC_TGID_STATUS,
51         PROC_TGID_MEM,
52         PROC_TGID_CWD,
53         PROC_TGID_ROOT,
54         PROC_TGID_EXE,
55         PROC_TGID_FD,
56         PROC_TGID_ENVIRON,
57         PROC_TGID_AUXV,
58         PROC_TGID_CMDLINE,
59         PROC_TGID_STAT,
60         PROC_TGID_STATM,
61         PROC_TGID_MAPS,
62         PROC_TGID_MOUNTS,
63         PROC_TGID_WCHAN,
64 #ifdef CONFIG_SECURITY
65         PROC_TGID_ATTR,
66         PROC_TGID_ATTR_CURRENT,
67         PROC_TGID_ATTR_PREV,
68         PROC_TGID_ATTR_EXEC,
69         PROC_TGID_ATTR_FSCREATE,
70 #endif
71         PROC_TGID_VX_INFO,
72         PROC_TGID_IP_INFO,
73         PROC_TGID_FD_DIR,
74         PROC_TID_INO,
75         PROC_TID_STATUS,
76         PROC_TID_MEM,
77         PROC_TID_CWD,
78         PROC_TID_ROOT,
79         PROC_TID_EXE,
80         PROC_TID_FD,
81         PROC_TID_ENVIRON,
82         PROC_TID_AUXV,
83         PROC_TID_CMDLINE,
84         PROC_TID_STAT,
85         PROC_TID_STATM,
86         PROC_TID_MAPS,
87         PROC_TID_MOUNTS,
88         PROC_TID_WCHAN,
89 #ifdef CONFIG_SECURITY
90         PROC_TID_ATTR,
91         PROC_TID_ATTR_CURRENT,
92         PROC_TID_ATTR_PREV,
93         PROC_TID_ATTR_EXEC,
94         PROC_TID_ATTR_FSCREATE,
95 #endif
96         PROC_TID_VX_INFO,
97         PROC_TID_IP_INFO,
98 #ifdef CONFIG_DELAY_ACCT
99         PROC_TID_DELAY_ACCT,
100         PROC_TGID_DELAY_ACCT,
101 #endif
102         PROC_TID_FD_DIR = 0x8000,       /* 0x8000-0xffff */
103 };
104
105 struct pid_entry {
106         int type;
107         int len;
108         char *name;
109         mode_t mode;
110 };
111
112 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
113
114 static struct pid_entry tgid_base_stuff[] = {
115         E(PROC_TGID_TASK,      "task",    S_IFDIR|S_IRUGO|S_IXUGO),
116         E(PROC_TGID_FD,        "fd",      S_IFDIR|S_IRUSR|S_IXUSR),
117         E(PROC_TGID_ENVIRON,   "environ", S_IFREG|S_IRUSR),
118         E(PROC_TGID_AUXV,      "auxv",    S_IFREG|S_IRUSR),
119         E(PROC_TGID_STATUS,    "status",  S_IFREG|S_IRUGO),
120         E(PROC_TGID_CMDLINE,   "cmdline", S_IFREG|S_IRUGO),
121         E(PROC_TGID_STAT,      "stat",    S_IFREG|S_IRUGO),
122         E(PROC_TGID_STATM,     "statm",   S_IFREG|S_IRUGO),
123         E(PROC_TGID_MAPS,      "maps",    S_IFREG|S_IRUSR),
124         E(PROC_TGID_MEM,       "mem",     S_IFREG|S_IRUSR|S_IWUSR),
125         E(PROC_TGID_CWD,       "cwd",     S_IFLNK|S_IRWXUGO),
126         E(PROC_TGID_ROOT,      "root",    S_IFLNK|S_IRWXUGO),
127         E(PROC_TGID_EXE,       "exe",     S_IFLNK|S_IRWXUGO),
128         E(PROC_TGID_MOUNTS,    "mounts",  S_IFREG|S_IRUGO),
129 #ifdef CONFIG_SECURITY
130         E(PROC_TGID_ATTR,      "attr",    S_IFDIR|S_IRUGO|S_IXUGO),
131 #endif
132 #ifdef CONFIG_DELAY_ACCT
133         E(PROC_TGID_DELAY_ACCT,"delay",   S_IFREG|S_IRUGO),
134 #endif
135 #ifdef CONFIG_KALLSYMS
136         E(PROC_TGID_WCHAN,     "wchan",   S_IFREG|S_IRUGO),
137 #endif
138         E(PROC_TGID_VX_INFO,   "vinfo",   S_IFREG|S_IRUGO),
139         E(PROC_TGID_IP_INFO,   "ninfo",   S_IFREG|S_IRUGO),
140         {0,0,NULL,0}
141 };
142 static struct pid_entry tid_base_stuff[] = {
143         E(PROC_TID_FD,         "fd",      S_IFDIR|S_IRUSR|S_IXUSR),
144         E(PROC_TID_ENVIRON,    "environ", S_IFREG|S_IRUSR),
145         E(PROC_TID_AUXV,       "auxv",    S_IFREG|S_IRUSR),
146         E(PROC_TID_STATUS,     "status",  S_IFREG|S_IRUGO),
147         E(PROC_TID_CMDLINE,    "cmdline", S_IFREG|S_IRUGO),
148         E(PROC_TID_STAT,       "stat",    S_IFREG|S_IRUGO),
149         E(PROC_TID_STATM,      "statm",   S_IFREG|S_IRUGO),
150         E(PROC_TID_MAPS,       "maps",    S_IFREG|S_IRUSR),
151         E(PROC_TID_MEM,        "mem",     S_IFREG|S_IRUSR|S_IWUSR),
152         E(PROC_TID_CWD,        "cwd",     S_IFLNK|S_IRWXUGO),
153         E(PROC_TID_ROOT,       "root",    S_IFLNK|S_IRWXUGO),
154         E(PROC_TID_EXE,        "exe",     S_IFLNK|S_IRWXUGO),
155         E(PROC_TID_MOUNTS,     "mounts",  S_IFREG|S_IRUGO),
156 #ifdef CONFIG_SECURITY
157         E(PROC_TID_ATTR,       "attr",    S_IFDIR|S_IRUGO|S_IXUGO),
158 #endif
159 #ifdef CONFIG_DELAY_ACCT
160         E(PROC_TGID_DELAY_ACCT,"delay",   S_IFREG|S_IRUGO),
161 #endif
162 #ifdef CONFIG_KALLSYMS
163         E(PROC_TID_WCHAN,      "wchan",   S_IFREG|S_IRUGO),
164 #endif
165         E(PROC_TID_VX_INFO,    "vinfo",   S_IFREG|S_IRUGO),
166         E(PROC_TID_IP_INFO,    "ninfo",   S_IFREG|S_IRUGO),
167         {0,0,NULL,0}
168 };
169
170 #ifdef CONFIG_SECURITY
171 static struct pid_entry tgid_attr_stuff[] = {
172         E(PROC_TGID_ATTR_CURRENT,  "current",  S_IFREG|S_IRUGO|S_IWUGO),
173         E(PROC_TGID_ATTR_PREV,     "prev",     S_IFREG|S_IRUGO),
174         E(PROC_TGID_ATTR_EXEC,     "exec",     S_IFREG|S_IRUGO|S_IWUGO),
175         E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
176         {0,0,NULL,0}
177 };
178 static struct pid_entry tid_attr_stuff[] = {
179         E(PROC_TID_ATTR_CURRENT,   "current",  S_IFREG|S_IRUGO|S_IWUGO),
180         E(PROC_TID_ATTR_PREV,      "prev",     S_IFREG|S_IRUGO),
181         E(PROC_TID_ATTR_EXEC,      "exec",     S_IFREG|S_IRUGO|S_IWUGO),
182         E(PROC_TID_ATTR_FSCREATE,  "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
183         {0,0,NULL,0}
184 };
185 #endif
186
187 #undef E
188
189 static inline struct task_struct *proc_task(struct inode *inode)
190 {
191         return PROC_I(inode)->task;
192 }
193
194 static inline int proc_type(struct inode *inode)
195 {
196         return PROC_I(inode)->type;
197 }
198
199 int proc_pid_stat(struct task_struct*,char*);
200 int proc_pid_status(struct task_struct*,char*);
201 int proc_pid_statm(struct task_struct*,char*);
202 int proc_pid_cpu(struct task_struct*,char*);
203 int proc_pid_delay(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) &&  task->state == TASK_STOPPED && \
300          security_ptrace(current,task) == 0))
301
302 static int may_ptrace_attach(struct task_struct *task)
303 {
304         int retval = 0;
305
306         task_lock(task);
307
308         if (!task->mm)
309                 goto out;
310         if (((current->uid != task->euid) ||
311              (current->uid != task->suid) ||
312              (current->uid != task->uid) ||
313              (current->gid != task->egid) ||
314              (current->gid != task->sgid) ||
315              (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
316                 goto out;
317         rmb();
318         if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
319                 goto out;
320         if (security_ptrace(current, task))
321                 goto out;
322
323         retval = 1;
324 out:
325         task_unlock(task);
326         return retval;
327 }
328
329 static int proc_pid_environ(struct task_struct *task, char * buffer)
330 {
331         int res = 0;
332         struct mm_struct *mm = get_task_mm(task);
333         if (mm) {
334                 unsigned int len = mm->env_end - mm->env_start;
335                 if (len > PAGE_SIZE)
336                         len = PAGE_SIZE;
337                 res = access_process_vm(task, mm->env_start, buffer, len, 0);
338                 if (!may_ptrace_attach(task))
339                         res = -ESRCH;
340                 mmput(mm);
341         }
342         return res;
343 }
344
345 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
346 {
347         int res = 0;
348         unsigned int len;
349         struct mm_struct *mm = get_task_mm(task);
350         if (!mm)
351                 goto out;
352
353         len = mm->arg_end - mm->arg_start;
354  
355         if (len > PAGE_SIZE)
356                 len = PAGE_SIZE;
357  
358         res = access_process_vm(task, mm->arg_start, buffer, len, 0);
359
360         // If the nul at the end of args has been overwritten, then
361         // assume application is using setproctitle(3).
362         if (res > 0 && buffer[res-1] != '\0') {
363                 len = strnlen(buffer, res);
364                 if (len < res) {
365                     res = len;
366                 } else {
367                         len = mm->env_end - mm->env_start;
368                         if (len > PAGE_SIZE - res)
369                                 len = PAGE_SIZE - res;
370                         res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
371                         res = strnlen(buffer, res);
372                 }
373         }
374         mmput(mm);
375
376 out:
377         return res;
378 }
379
380 static int proc_pid_auxv(struct task_struct *task, char *buffer)
381 {
382         int res = 0;
383         struct mm_struct *mm = get_task_mm(task);
384         if (mm) {
385                 unsigned int nwords = 0;
386                 do
387                         nwords += 2;
388                 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
389                 res = nwords * sizeof(mm->saved_auxv[0]);
390                 if (res > PAGE_SIZE)
391                         res = PAGE_SIZE;
392                 memcpy(buffer, mm->saved_auxv, res);
393                 mmput(mm);
394         }
395         return res;
396 }
397
398
399 #ifdef CONFIG_KALLSYMS
400 /*
401  * Provides a wchan file via kallsyms in a proper one-value-per-file format.
402  * Returns the resolved symbol.  If that fails, simply return the address.
403  */
404 static int proc_pid_wchan(struct task_struct *task, char *buffer)
405 {
406         char *modname;
407         const char *sym_name;
408         unsigned long wchan, size, offset;
409         char namebuf[128];
410
411         wchan = get_wchan(task);
412
413         sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
414         if (sym_name)
415                 return sprintf(buffer, "%s", sym_name);
416         return sprintf(buffer, "%lu", wchan);
417 }
418 #endif /* CONFIG_KALLSYMS */
419
420 /************************************************************************/
421 /*                       Here the fs part begins                        */
422 /************************************************************************/
423
424 /* permission checks */
425
426 static int proc_check_root(struct inode *inode)
427 {
428         struct dentry *de, *base, *root;
429         struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
430         int res = 0;
431
432         if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
433                 return -ENOENT;
434         read_lock(&current->fs->lock);
435         our_vfsmnt = mntget(current->fs->rootmnt);
436         base = dget(current->fs->root);
437         read_unlock(&current->fs->lock);
438
439         spin_lock(&vfsmount_lock);
440         de = root;
441         mnt = vfsmnt;
442
443         while (vfsmnt != our_vfsmnt) {
444                 if (vfsmnt == vfsmnt->mnt_parent)
445                         goto out;
446                 de = vfsmnt->mnt_mountpoint;
447                 vfsmnt = vfsmnt->mnt_parent;
448         }
449
450         if (!is_subdir(de, base))
451                 goto out;
452         spin_unlock(&vfsmount_lock);
453
454 exit:
455         dput(base);
456         mntput(our_vfsmnt);
457         dput(root);
458         mntput(mnt);
459         return res;
460 out:
461         spin_unlock(&vfsmount_lock);
462         res = -EACCES;
463         goto exit;
464 }
465
466 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
467 {
468         if (vfs_permission(inode, mask) != 0)
469                 return -EACCES;
470         return proc_check_root(inode);
471 }
472
473 extern struct seq_operations proc_pid_maps_op;
474 static int maps_open(struct inode *inode, struct file *file)
475 {
476         struct task_struct *task = proc_task(inode);
477         int ret = seq_open(file, &proc_pid_maps_op);
478         if (!ret) {
479                 struct seq_file *m = file->private_data;
480                 m->private = task;
481         }
482         return ret;
483 }
484
485 static struct file_operations proc_maps_operations = {
486         .open           = maps_open,
487         .read           = seq_read,
488         .llseek         = seq_lseek,
489         .release        = seq_release,
490 };
491
492 extern struct seq_operations mounts_op;
493 static int mounts_open(struct inode *inode, struct file *file)
494 {
495         struct task_struct *task = proc_task(inode);
496         int ret = seq_open(file, &mounts_op);
497
498         if (!ret) {
499                 struct seq_file *m = file->private_data;
500                 struct namespace *namespace;
501                 task_lock(task);
502                 namespace = task->namespace;
503                 if (namespace)
504                         get_namespace(namespace);
505                 task_unlock(task);
506
507                 if (namespace)
508                         m->private = namespace;
509                 else {
510                         seq_release(inode, file);
511                         ret = -EINVAL;
512                 }
513         }
514         return ret;
515 }
516
517 static int mounts_release(struct inode *inode, struct file *file)
518 {
519         struct seq_file *m = file->private_data;
520         struct namespace *namespace = m->private;
521         put_namespace(namespace);
522         return seq_release(inode, file);
523 }
524
525 static struct file_operations proc_mounts_operations = {
526         .open           = mounts_open,
527         .read           = seq_read,
528         .llseek         = seq_lseek,
529         .release        = mounts_release,
530 };
531
532 #define PROC_BLOCK_SIZE (3*1024)                /* 4K page size but our output routines use some slack for overruns */
533
534 static ssize_t proc_info_read(struct file * file, char __user * buf,
535                           size_t count, loff_t *ppos)
536 {
537         struct inode * inode = file->f_dentry->d_inode;
538         unsigned long page;
539         ssize_t length;
540         ssize_t end;
541         struct task_struct *task = proc_task(inode);
542
543         if (count > PROC_BLOCK_SIZE)
544                 count = PROC_BLOCK_SIZE;
545         if (!(page = __get_free_page(GFP_KERNEL)))
546                 return -ENOMEM;
547
548         length = PROC_I(inode)->op.proc_read(task, (char*)page);
549
550         if (length < 0) {
551                 free_page(page);
552                 return length;
553         }
554         /* Static 4kB (or whatever) block capacity */
555         if (*ppos >= length) {
556                 free_page(page);
557                 return 0;
558         }
559         if (count + *ppos > length)
560                 count = length - *ppos;
561         end = count + *ppos;
562         if (copy_to_user(buf, (char *) page + *ppos, count))
563                 count = -EFAULT;
564         else
565                 *ppos = end;
566         free_page(page);
567         return count;
568 }
569
570 static struct file_operations proc_info_file_operations = {
571         .read           = proc_info_read,
572 };
573
574 static int mem_open(struct inode* inode, struct file* file)
575 {
576         file->private_data = (void*)((long)current->self_exec_id);
577         return 0;
578 }
579
580 static ssize_t mem_read(struct file * file, char __user * buf,
581                         size_t count, loff_t *ppos)
582 {
583         struct task_struct *task = proc_task(file->f_dentry->d_inode);
584         char *page;
585         unsigned long src = *ppos;
586         int ret = -ESRCH;
587         struct mm_struct *mm;
588
589         if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
590                 goto out;
591
592         ret = -ENOMEM;
593         page = (char *)__get_free_page(GFP_USER);
594         if (!page)
595                 goto out;
596
597         ret = 0;
598  
599         mm = get_task_mm(task);
600         if (!mm)
601                 goto out_free;
602
603         ret = -EIO;
604  
605         if (file->private_data != (void*)((long)current->self_exec_id))
606                 goto out_put;
607
608         ret = 0;
609  
610         while (count > 0) {
611                 int this_len, retval;
612
613                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
614                 retval = access_process_vm(task, src, page, this_len, 0);
615                 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
616                         if (!ret)
617                                 ret = -EIO;
618                         break;
619                 }
620
621                 if (copy_to_user(buf, page, retval)) {
622                         ret = -EFAULT;
623                         break;
624                 }
625  
626                 ret += retval;
627                 src += retval;
628                 buf += retval;
629                 count -= retval;
630         }
631         *ppos = src;
632
633 out_put:
634         mmput(mm);
635 out_free:
636         free_page((unsigned long) page);
637 out:
638         return ret;
639 }
640
641 #define mem_write NULL
642
643 #ifndef mem_write
644 /* This is a security hazard */
645 static ssize_t mem_write(struct file * file, const char * buf,
646                          size_t count, loff_t *ppos)
647 {
648         int copied = 0;
649         char *page;
650         struct task_struct *task = proc_task(file->f_dentry->d_inode);
651         unsigned long dst = *ppos;
652
653         if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
654                 return -ESRCH;
655
656         page = (char *)__get_free_page(GFP_USER);
657         if (!page)
658                 return -ENOMEM;
659
660         while (count > 0) {
661                 int this_len, retval;
662
663                 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
664                 if (copy_from_user(page, buf, this_len)) {
665                         copied = -EFAULT;
666                         break;
667                 }
668                 retval = access_process_vm(task, dst, page, this_len, 1);
669                 if (!retval) {
670                         if (!copied)
671                                 copied = -EIO;
672                         break;
673                 }
674                 copied += retval;
675                 buf += retval;
676                 dst += retval;
677                 count -= retval;                        
678         }
679         *ppos = dst;
680         free_page((unsigned long) page);
681         return copied;
682 }
683 #endif
684
685 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
686 {
687         switch (orig) {
688         case 0:
689                 file->f_pos = offset;
690                 break;
691         case 1:
692                 file->f_pos += offset;
693                 break;
694         default:
695                 return -EINVAL;
696         }
697         force_successful_syscall_return();
698         return file->f_pos;
699 }
700
701 static struct file_operations proc_mem_operations = {
702         .llseek         = mem_lseek,
703         .read           = mem_read,
704         .write          = mem_write,
705         .open           = mem_open,
706 };
707
708 static struct inode_operations proc_mem_inode_operations = {
709         .permission     = proc_permission,
710 };
711
712 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
713 {
714         struct inode *inode = dentry->d_inode;
715         int error = -EACCES;
716
717         /* We don't need a base pointer in the /proc filesystem */
718         path_release(nd);
719
720         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
721                 goto out;
722         error = proc_check_root(inode);
723         if (error)
724                 goto out;
725
726         error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
727         nd->last_type = LAST_BIND;
728 out:
729         return error;
730 }
731
732 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
733                             char __user *buffer, int buflen)
734 {
735         struct inode * inode;
736         char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
737         int len;
738
739         if (!tmp)
740                 return -ENOMEM;
741                 
742         inode = dentry->d_inode;
743         path = d_path(dentry, mnt, tmp, PAGE_SIZE);
744         len = PTR_ERR(path);
745         if (IS_ERR(path))
746                 goto out;
747         len = tmp + PAGE_SIZE - 1 - path;
748
749         if (len > buflen)
750                 len = buflen;
751         if (copy_to_user(buffer, path, len))
752                 len = -EFAULT;
753  out:
754         free_page((unsigned long)tmp);
755         return len;
756 }
757
758 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
759 {
760         int error = -EACCES;
761         struct inode *inode = dentry->d_inode;
762         struct dentry *de;
763         struct vfsmount *mnt = NULL;
764
765         lock_kernel();
766
767         if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
768                 goto out;
769         error = proc_check_root(inode);
770         if (error)
771                 goto out;
772
773         error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
774         if (error)
775                 goto out;
776
777         error = do_proc_readlink(de, mnt, buffer, buflen);
778         dput(de);
779         mntput(mnt);
780 out:
781         unlock_kernel();
782         return error;
783 }
784
785 static struct inode_operations proc_pid_link_inode_operations = {
786         .readlink       = proc_pid_readlink,
787         .follow_link    = proc_pid_follow_link
788 };
789
790 static int pid_alive(struct task_struct *p)
791 {
792         BUG_ON(p->pids[PIDTYPE_PID].pidptr != &p->pids[PIDTYPE_PID].pid);
793         return atomic_read(&p->pids[PIDTYPE_PID].pid.count);
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 * 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         ssize_t end;
1199         struct task_struct *task = proc_task(inode);
1200
1201         if (count > PAGE_SIZE)
1202                 count = PAGE_SIZE;
1203         if (!(page = __get_free_page(GFP_KERNEL)))
1204                 return -ENOMEM;
1205
1206         length = security_getprocattr(task, 
1207                                       (char*)file->f_dentry->d_name.name, 
1208                                       (void*)page, count);
1209         if (length < 0) {
1210                 free_page(page);
1211                 return length;
1212         }
1213         /* Static 4kB (or whatever) block capacity */
1214         if (*ppos >= length) {
1215                 free_page(page);
1216                 return 0;
1217         }
1218         if (count + *ppos > length)
1219                 count = length - *ppos;
1220         end = count + *ppos;
1221         if (copy_to_user(buf, (char *) page + *ppos, count))
1222                 count = -EFAULT;
1223         else
1224                 *ppos = end;
1225         free_page(page);
1226         return count;
1227 }
1228
1229 static ssize_t proc_pid_attr_write(struct file * file, const char * buf,
1230                                    size_t count, loff_t *ppos)
1231
1232         struct inode * inode = file->f_dentry->d_inode;
1233         char *page; 
1234         ssize_t length; 
1235         struct task_struct *task = proc_task(inode); 
1236
1237         if (count > PAGE_SIZE) 
1238                 count = PAGE_SIZE; 
1239         if (*ppos != 0) {
1240                 /* No partial writes. */
1241                 return -EINVAL;
1242         }
1243         page = (char*)__get_free_page(GFP_USER); 
1244         if (!page) 
1245                 return -ENOMEM;
1246         length = -EFAULT; 
1247         if (copy_from_user(page, buf, count)) 
1248                 goto out;
1249
1250         length = security_setprocattr(task, 
1251                                       (char*)file->f_dentry->d_name.name, 
1252                                       (void*)page, count);
1253 out:
1254         free_page((unsigned long) page);
1255         return length;
1256
1257
1258 static struct file_operations proc_pid_attr_operations = {
1259         .read           = proc_pid_attr_read,
1260         .write          = proc_pid_attr_write,
1261 };
1262
1263 static struct file_operations proc_tid_attr_operations;
1264 static struct inode_operations proc_tid_attr_inode_operations;
1265 static struct file_operations proc_tgid_attr_operations;
1266 static struct inode_operations proc_tgid_attr_inode_operations;
1267 #endif
1268
1269 /* SMP-safe */
1270 static struct dentry *proc_pident_lookup(struct inode *dir, 
1271                                          struct dentry *dentry,
1272                                          struct pid_entry *ents)
1273 {
1274         struct inode *inode;
1275         int error;
1276         struct task_struct *task = proc_task(dir);
1277         struct pid_entry *p;
1278         struct proc_inode *ei;
1279
1280         error = -ENOENT;
1281         inode = NULL;
1282
1283         if (!pid_alive(task))
1284                 goto out;
1285
1286         for (p = ents; p->name; p++) {
1287                 if (p->len != dentry->d_name.len)
1288                         continue;
1289                 if (!memcmp(dentry->d_name.name, p->name, p->len))
1290                         break;
1291         }
1292         if (!p->name)
1293                 goto out;
1294
1295         error = -EINVAL;
1296         inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1297         if (!inode)
1298                 goto out;
1299
1300         ei = PROC_I(inode);
1301         inode->i_mode = p->mode;
1302         /*
1303          * Yes, it does not scale. And it should not. Don't add
1304          * new entries into /proc/<tgid>/ without very good reasons.
1305          */
1306         switch(p->type) {
1307                 case PROC_TGID_TASK:
1308                         inode->i_nlink = 3;
1309                         inode->i_op = &proc_task_inode_operations;
1310                         inode->i_fop = &proc_task_operations;
1311                         break;
1312                 case PROC_TID_FD:
1313                 case PROC_TGID_FD:
1314                         inode->i_nlink = 2;
1315                         inode->i_op = &proc_fd_inode_operations;
1316                         inode->i_fop = &proc_fd_operations;
1317                         break;
1318                 case PROC_TID_EXE:
1319                 case PROC_TGID_EXE:
1320                         inode->i_op = &proc_pid_link_inode_operations;
1321                         ei->op.proc_get_link = proc_exe_link;
1322                         break;
1323                 case PROC_TID_CWD:
1324                 case PROC_TGID_CWD:
1325                         inode->i_op = &proc_pid_link_inode_operations;
1326                         ei->op.proc_get_link = proc_cwd_link;
1327                         break;
1328                 case PROC_TID_ROOT:
1329                 case PROC_TGID_ROOT:
1330                         inode->i_op = &proc_pid_link_inode_operations;
1331                         ei->op.proc_get_link = proc_root_link;
1332                         break;
1333                 case PROC_TID_ENVIRON:
1334                 case PROC_TGID_ENVIRON:
1335                         inode->i_fop = &proc_info_file_operations;
1336                         ei->op.proc_read = proc_pid_environ;
1337                         break;
1338                 case PROC_TID_AUXV:
1339                 case PROC_TGID_AUXV:
1340                         inode->i_fop = &proc_info_file_operations;
1341                         ei->op.proc_read = proc_pid_auxv;
1342                         break;
1343                 case PROC_TID_STATUS:
1344                 case PROC_TGID_STATUS:
1345                         inode->i_fop = &proc_info_file_operations;
1346                         ei->op.proc_read = proc_pid_status;
1347                         break;
1348                 case PROC_TID_STAT:
1349                 case PROC_TGID_STAT:
1350                         inode->i_fop = &proc_info_file_operations;
1351                         ei->op.proc_read = proc_pid_stat;
1352                         break;
1353                 case PROC_TID_CMDLINE:
1354                 case PROC_TGID_CMDLINE:
1355                         inode->i_fop = &proc_info_file_operations;
1356                         ei->op.proc_read = proc_pid_cmdline;
1357                         break;
1358                 case PROC_TID_STATM:
1359                 case PROC_TGID_STATM:
1360                         inode->i_fop = &proc_info_file_operations;
1361                         ei->op.proc_read = proc_pid_statm;
1362                         break;
1363                 case PROC_TID_MAPS:
1364                 case PROC_TGID_MAPS:
1365                         inode->i_fop = &proc_maps_operations;
1366                         break;
1367                 case PROC_TID_MEM:
1368                 case PROC_TGID_MEM:
1369                         inode->i_op = &proc_mem_inode_operations;
1370                         inode->i_fop = &proc_mem_operations;
1371                         break;
1372                 case PROC_TID_MOUNTS:
1373                 case PROC_TGID_MOUNTS:
1374                         inode->i_fop = &proc_mounts_operations;
1375                         break;
1376 #ifdef CONFIG_SECURITY
1377                 case PROC_TID_ATTR:
1378                         inode->i_nlink = 2;
1379                         inode->i_op = &proc_tid_attr_inode_operations;
1380                         inode->i_fop = &proc_tid_attr_operations;
1381                         break;
1382                 case PROC_TGID_ATTR:
1383                         inode->i_nlink = 2;
1384                         inode->i_op = &proc_tgid_attr_inode_operations;
1385                         inode->i_fop = &proc_tgid_attr_operations;
1386                         break;
1387                 case PROC_TID_ATTR_CURRENT:
1388                 case PROC_TGID_ATTR_CURRENT:
1389                 case PROC_TID_ATTR_PREV:
1390                 case PROC_TGID_ATTR_PREV:
1391                 case PROC_TID_ATTR_EXEC:
1392                 case PROC_TGID_ATTR_EXEC:
1393                 case PROC_TID_ATTR_FSCREATE:
1394                 case PROC_TGID_ATTR_FSCREATE:
1395                         inode->i_fop = &proc_pid_attr_operations;
1396                         break;
1397 #endif
1398 #ifdef CONFIG_KALLSYMS
1399                 case PROC_TID_WCHAN:
1400                 case PROC_TGID_WCHAN:
1401                         inode->i_fop = &proc_info_file_operations;
1402                         ei->op.proc_read = proc_pid_wchan;
1403                         break;
1404 #endif
1405                 case PROC_TID_VX_INFO:
1406                 case PROC_TGID_VX_INFO:
1407                         inode->i_fop = &proc_info_file_operations;
1408                         ei->op.proc_read = proc_pid_vx_info;
1409                         break;
1410                 case PROC_TID_IP_INFO:
1411                 case PROC_TGID_IP_INFO:
1412                         inode->i_fop = &proc_info_file_operations;
1413                         ei->op.proc_read = proc_pid_nx_info;
1414                         break;
1415 #ifdef CONFIG_DELAY_ACCT
1416                 case PROC_TID_DELAY_ACCT:
1417                 case PROC_TGID_DELAY_ACCT:
1418                         inode->i_fop = &proc_info_file_operations;
1419                         ei->op.proc_read = proc_pid_delay;
1420                         break;
1421 #endif
1422                 default:
1423                         printk("procfs: impossible type (%d)",p->type);
1424                         iput(inode);
1425                         return ERR_PTR(-EINVAL);
1426         }
1427         dentry->d_op = &pid_dentry_operations;
1428         d_add(dentry, inode);
1429         return NULL;
1430
1431 out:
1432         return ERR_PTR(error);
1433 }
1434
1435 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1436         return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1437 }
1438
1439 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1440         return proc_pident_lookup(dir, dentry, tid_base_stuff);
1441 }
1442
1443 static struct file_operations proc_tgid_base_operations = {
1444         .read           = generic_read_dir,
1445         .readdir        = proc_tgid_base_readdir,
1446 };
1447
1448 static struct file_operations proc_tid_base_operations = {
1449         .read           = generic_read_dir,
1450         .readdir        = proc_tid_base_readdir,
1451 };
1452
1453 static struct inode_operations proc_tgid_base_inode_operations = {
1454         .lookup         = proc_tgid_base_lookup,
1455 };
1456
1457 static struct inode_operations proc_tid_base_inode_operations = {
1458         .lookup         = proc_tid_base_lookup,
1459 };
1460
1461 #ifdef CONFIG_SECURITY
1462 static int proc_tgid_attr_readdir(struct file * filp,
1463                              void * dirent, filldir_t filldir)
1464 {
1465         return proc_pident_readdir(filp,dirent,filldir,
1466                                    tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1467 }
1468
1469 static int proc_tid_attr_readdir(struct file * filp,
1470                              void * dirent, filldir_t filldir)
1471 {
1472         return proc_pident_readdir(filp,dirent,filldir,
1473                                    tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1474 }
1475
1476 static struct file_operations proc_tgid_attr_operations = {
1477         .read           = generic_read_dir,
1478         .readdir        = proc_tgid_attr_readdir,
1479 };
1480
1481 static struct file_operations proc_tid_attr_operations = {
1482         .read           = generic_read_dir,
1483         .readdir        = proc_tid_attr_readdir,
1484 };
1485
1486 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1487                                 struct dentry *dentry, struct nameidata *nd)
1488 {
1489         return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1490 }
1491
1492 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1493                                 struct dentry *dentry, struct nameidata *nd)
1494 {
1495         return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1496 }
1497
1498 static struct inode_operations proc_tgid_attr_inode_operations = {
1499         .lookup         = proc_tgid_attr_lookup,
1500 };
1501
1502 static struct inode_operations proc_tid_attr_inode_operations = {
1503         .lookup         = proc_tid_attr_lookup,
1504 };
1505 #endif
1506
1507 /*
1508  * /proc/self:
1509  */
1510 static int proc_self_readlink(struct dentry *dentry, char *buffer, int buflen)
1511 {
1512         char tmp[30];
1513         sprintf(tmp, "%d", current->tgid);
1514         return vfs_readlink(dentry,buffer,buflen,tmp);
1515 }
1516
1517 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1518 {
1519         char tmp[30];
1520         sprintf(tmp, "%d", current->tgid);
1521         return vfs_follow_link(nd,tmp);
1522 }       
1523
1524 static struct inode_operations proc_self_inode_operations = {
1525         .readlink       = proc_self_readlink,
1526         .follow_link    = proc_self_follow_link,
1527 };
1528
1529 /**
1530  * proc_pid_unhash -  Unhash /proc/<pid> entry from the dcache.
1531  * @p: task that should be flushed.
1532  *
1533  * Drops the /proc/<pid> dcache entry from the hash chains.
1534  *
1535  * Dropping /proc/<pid> entries and detach_pid must be synchroneous,
1536  * otherwise e.g. /proc/<pid>/exe might point to the wrong executable,
1537  * if the pid value is immediately reused. This is enforced by
1538  * - caller must acquire spin_lock(p->proc_lock)
1539  * - must be called before detach_pid()
1540  * - proc_pid_lookup acquires proc_lock, and checks that
1541  *   the target is not dead by looking at the attach count
1542  *   of PIDTYPE_PID.
1543  */
1544
1545 struct dentry *proc_pid_unhash(struct task_struct *p)
1546 {
1547         struct dentry *proc_dentry;
1548
1549         proc_dentry = p->proc_dentry;
1550         if (proc_dentry != NULL) {
1551
1552                 spin_lock(&dcache_lock);
1553                 if (!d_unhashed(proc_dentry)) {
1554                         dget_locked(proc_dentry);
1555                         __d_drop(proc_dentry);
1556                 } else
1557                         proc_dentry = NULL;
1558                 spin_unlock(&dcache_lock);
1559         }
1560         return proc_dentry;
1561 }
1562
1563 /**
1564  * proc_pid_flush - recover memory used by stale /proc/<pid>/x entries
1565  * @proc_entry: directoy to prune.
1566  *
1567  * Shrink the /proc directory that was used by the just killed thread.
1568  */
1569         
1570 void proc_pid_flush(struct dentry *proc_dentry)
1571 {
1572         if(proc_dentry != NULL) {
1573                 shrink_dcache_parent(proc_dentry);
1574                 dput(proc_dentry);
1575         }
1576 }
1577
1578 /* SMP-safe */
1579 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1580 {
1581         struct task_struct *task;
1582         struct inode *inode;
1583         struct proc_inode *ei;
1584         unsigned tgid;
1585         int died;
1586
1587         if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1588                 inode = new_inode(dir->i_sb);
1589                 if (!inode)
1590                         return ERR_PTR(-ENOMEM);
1591                 ei = PROC_I(inode);
1592                 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1593                 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1594                 ei->pde = NULL;
1595                 inode->i_mode = S_IFLNK|S_IRWXUGO;
1596                 inode->i_uid = inode->i_gid = 0;
1597                 inode->i_size = 64;
1598                 inode->i_op = &proc_self_inode_operations;
1599                 d_add(dentry, inode);
1600                 return NULL;
1601         }
1602         tgid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1603         if (tgid == ~0U)
1604                 goto out;
1605
1606         read_lock(&tasklist_lock);
1607         task = find_task_by_pid(tgid);
1608         if (task)
1609                 get_task_struct(task);
1610         read_unlock(&tasklist_lock);
1611         if (!task)
1612                 goto out;
1613
1614         inode = NULL;
1615         if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1616                 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1617
1618         if (!inode) {
1619                 put_task_struct(task);
1620                 goto out;
1621         }
1622         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1623         inode->i_op = &proc_tgid_base_inode_operations;
1624         inode->i_fop = &proc_tgid_base_operations;
1625         inode->i_nlink = 3;
1626         inode->i_flags|=S_IMMUTABLE;
1627
1628         dentry->d_op = &pid_base_dentry_operations;
1629
1630         died = 0;
1631         d_add(dentry, inode);
1632         spin_lock(&task->proc_lock);
1633         task->proc_dentry = dentry;
1634         if (!pid_alive(task)) {
1635                 dentry = proc_pid_unhash(task);
1636                 died = 1;
1637         }
1638         spin_unlock(&task->proc_lock);
1639
1640         put_task_struct(task);
1641         if (died) {
1642                 proc_pid_flush(dentry);
1643                 goto out;
1644         }
1645         return NULL;
1646 out:
1647         return ERR_PTR(-ENOENT);
1648 }
1649
1650 /* SMP-safe */
1651 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1652 {
1653         struct task_struct *task;
1654         struct task_struct *leader = proc_task(dir);
1655         struct inode *inode;
1656         unsigned tid;
1657
1658         tid = vx_rmap_tgid(current->vx_info, name_to_int(dentry));
1659         if (tid == ~0U)
1660                 goto out;
1661
1662 /*      handle fakeinit */
1663
1664         read_lock(&tasklist_lock);
1665         task = find_task_by_pid(tid);
1666         if (task)
1667                 get_task_struct(task);
1668         read_unlock(&tasklist_lock);
1669         if (!task)
1670                 goto out;
1671         if (leader->tgid != task->tgid)
1672                 goto out_drop_task;
1673
1674         inode = NULL;
1675         if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1676                 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1677
1678         if (!inode)
1679                 goto out_drop_task;
1680         inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1681         inode->i_op = &proc_tid_base_inode_operations;
1682         inode->i_fop = &proc_tid_base_operations;
1683         inode->i_nlink = 3;
1684         inode->i_flags|=S_IMMUTABLE;
1685
1686         dentry->d_op = &pid_base_dentry_operations;
1687
1688         d_add(dentry, inode);
1689
1690         put_task_struct(task);
1691         return NULL;
1692 out_drop_task:
1693         put_task_struct(task);
1694 out:
1695         return ERR_PTR(-ENOENT);
1696 }
1697
1698 #define PROC_NUMBUF 10
1699 #define PROC_MAXPIDS 20
1700
1701 /*
1702  * Get a few tgid's to return for filldir - we need to hold the
1703  * tasklist lock while doing this, and we must release it before
1704  * we actually do the filldir itself, so we use a temp buffer..
1705  */
1706 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1707 {
1708         struct task_struct *p;
1709         int nr_tgids = 0;
1710
1711         index--;
1712         read_lock(&tasklist_lock);
1713         p = NULL;
1714         if (version) {
1715                 p = find_task_by_pid(version);
1716                 if (!thread_group_leader(p))
1717                         p = NULL;
1718         }
1719
1720         if (p)
1721                 index = 0;
1722         else
1723                 p = next_task(&init_task);
1724
1725         for ( ; p != &init_task; p = next_task(p)) {
1726                 int tgid = p->pid;
1727
1728                 if (!pid_alive(p))
1729                         continue;
1730                 if (!vx_check(vx_task_xid(p), VX_WATCH|VX_IDENT))
1731                         continue;
1732                 if (--index >= 0)
1733                         continue;
1734                 tgids[nr_tgids] = vx_map_tgid(current->vx_info, tgid);
1735                 nr_tgids++;
1736                 if (nr_tgids >= PROC_MAXPIDS)
1737                         break;
1738         }
1739         read_unlock(&tasklist_lock);
1740         return nr_tgids;
1741 }
1742
1743 /*
1744  * Get a few tid's to return for filldir - we need to hold the
1745  * tasklist lock while doing this, and we must release it before
1746  * we actually do the filldir itself, so we use a temp buffer..
1747  */
1748 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1749 {
1750         struct task_struct *leader_task = proc_task(dir);
1751         struct task_struct *task = leader_task;
1752         int nr_tids = 0;
1753
1754         index -= 2;
1755         read_lock(&tasklist_lock);
1756         /*
1757          * The starting point task (leader_task) might be an already
1758          * unlinked task, which cannot be used to access the task-list
1759          * via next_thread().
1760          */
1761         if (pid_alive(task)) do {
1762                 int tid = task->pid;
1763
1764                 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1765                         continue;
1766                 if (--index >= 0)
1767                         continue;
1768                 tids[nr_tids] = vx_map_tgid(current->vx_info, tid);
1769                 nr_tids++;
1770                 if (nr_tids >= PROC_MAXPIDS)
1771                         break;
1772         } while ((task = next_thread(task)) != leader_task);
1773         read_unlock(&tasklist_lock);
1774         return nr_tids;
1775 }
1776
1777 /* for the /proc/ directory itself, after non-process stuff has been done */
1778 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1779 {
1780         unsigned int tgid_array[PROC_MAXPIDS];
1781         char buf[PROC_NUMBUF];
1782         unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1783         unsigned int nr_tgids, i;
1784
1785         if (!nr) {
1786                 ino_t ino = fake_ino(0,PROC_TGID_INO);
1787                 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
1788                         return 0;
1789                 filp->f_pos++;
1790                 nr++;
1791         }
1792
1793         /*
1794          * f_version caches the last tgid which was returned from readdir
1795          */
1796         nr_tgids = get_tgid_list(nr, filp->f_version, tgid_array);
1797
1798         for (i = 0; i < nr_tgids; i++) {
1799                 int tgid = tgid_array[i];
1800                 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
1801                 unsigned long j = PROC_NUMBUF;
1802
1803                 do buf[--j] = '0' + (tgid % 10); while (tgid/=10);
1804
1805                 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
1806                         filp->f_version = tgid;
1807                         break;
1808                 }
1809                 filp->f_pos++;
1810         }
1811         return 0;
1812 }
1813
1814 /* for the /proc/TGID/task/ directories */
1815 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
1816 {
1817         unsigned int tid_array[PROC_MAXPIDS];
1818         char buf[PROC_NUMBUF];
1819         unsigned int nr_tids, i;
1820         struct dentry *dentry = filp->f_dentry;
1821         struct inode *inode = dentry->d_inode;
1822         struct task_struct *task = proc_task(inode);
1823         int retval = -ENOENT;
1824         ino_t ino;
1825         unsigned long pos = filp->f_pos;  /* avoiding "long long" filp->f_pos */
1826
1827         if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1828                 goto out;
1829         if (!pid_alive(task))
1830                 goto out;
1831         retval = 0;
1832
1833         switch (pos) {
1834         case 0:
1835                 ino = inode->i_ino;
1836                 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
1837                         goto out;
1838                 pos++;
1839                 /* fall through */
1840         case 1:
1841                 ino = parent_ino(dentry);
1842                 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
1843                         goto out;
1844                 pos++;
1845                 /* fall through */
1846         }
1847
1848         nr_tids = get_tid_list(pos, tid_array, inode);
1849
1850         for (i = 0; i < nr_tids; i++) {
1851                 unsigned long j = PROC_NUMBUF;
1852                 int tid = tid_array[i];
1853
1854                 ino = fake_ino(tid,PROC_TID_INO);
1855
1856                 do
1857                         buf[--j] = '0' + (tid % 10);
1858                 while (tid /= 10);
1859
1860                 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)
1861                         break;
1862                 pos++;
1863         }
1864 out:
1865         filp->f_pos = pos;
1866         return retval;
1867 }