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