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