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