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