4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
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.
16 #include <asm/uaccess.h>
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>
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>
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.
49 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
51 enum pid_directory_inos {
71 #ifdef CONFIG_SCHEDSTATS
77 #ifdef CONFIG_SECURITY
79 PROC_TGID_ATTR_CURRENT,
82 PROC_TGID_ATTR_FSCREATE,
86 #ifdef CONFIG_AUDITSYSCALL
110 #ifdef CONFIG_SCHEDSTATS
113 #ifdef CONFIG_CPUSETS
116 #ifdef CONFIG_SECURITY
118 PROC_TID_ATTR_CURRENT,
121 PROC_TID_ATTR_FSCREATE,
125 #ifdef CONFIG_AUDITSYSCALL
128 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
140 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
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),
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),
163 #ifdef CONFIG_KALLSYMS
164 E(PROC_TGID_WCHAN, "wchan", S_IFREG|S_IRUGO),
166 #ifdef CONFIG_SCHEDSTATS
167 E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),
169 #ifdef CONFIG_CPUSETS
170 E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
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),
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),
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),
201 #ifdef CONFIG_KALLSYMS
202 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
204 #ifdef CONFIG_SCHEDSTATS
205 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
207 #ifdef CONFIG_CPUSETS
208 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
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),
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),
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),
239 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
241 struct task_struct *task = proc_task(inode);
242 struct files_struct *files;
244 int fd = proc_type(inode) - PROC_TID_FD_DIR;
246 files = get_files_struct(task);
248 spin_lock(&files->file_lock);
249 file = fcheck_files(files, fd);
251 *mnt = mntget(file->f_vfsmnt);
252 *dentry = dget(file->f_dentry);
253 spin_unlock(&files->file_lock);
254 put_files_struct(files);
257 spin_unlock(&files->file_lock);
258 put_files_struct(files);
263 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
265 struct fs_struct *fs;
266 int result = -ENOENT;
267 task_lock(proc_task(inode));
268 fs = proc_task(inode)->fs;
270 atomic_inc(&fs->count);
271 task_unlock(proc_task(inode));
273 read_lock(&fs->lock);
274 *mnt = mntget(fs->pwdmnt);
275 *dentry = dget(fs->pwd);
276 read_unlock(&fs->lock);
283 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
285 struct fs_struct *fs;
286 int result = -ENOENT;
287 task_lock(proc_task(inode));
288 fs = proc_task(inode)->fs;
290 atomic_inc(&fs->count);
291 task_unlock(proc_task(inode));
293 read_lock(&fs->lock);
294 *mnt = mntget(fs->rootmnt);
295 *dentry = dget(fs->root);
296 read_unlock(&fs->lock);
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))
310 static int may_ptrace_attach(struct task_struct *task)
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))
326 if (!task->mm->dumpable && !capable(CAP_SYS_PTRACE))
328 if (security_ptrace(current, task))
337 static int proc_pid_environ(struct task_struct *task, char * buffer)
340 struct mm_struct *mm = get_task_mm(task);
342 unsigned int len = mm->env_end - mm->env_start;
345 res = access_process_vm(task, mm->env_start, buffer, len, 0);
346 if (!may_ptrace_attach(task))
353 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
357 struct mm_struct *mm = get_task_mm(task);
361 goto out_mm; /* Shh! No looking before we're done */
363 len = mm->arg_end - mm->arg_start;
368 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
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);
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);
390 static int proc_pid_auxv(struct task_struct *task, char *buffer)
393 struct mm_struct *mm = get_task_mm(task);
395 unsigned int nwords = 0;
398 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
399 res = nwords * sizeof(mm->saved_auxv[0]);
402 memcpy(buffer, mm->saved_auxv, res);
409 #ifdef CONFIG_KALLSYMS
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.
414 static int proc_pid_wchan(struct task_struct *task, char *buffer)
417 const char *sym_name;
418 unsigned long wchan, size, offset;
419 char namebuf[KSYM_NAME_LEN+1];
421 wchan = get_wchan(task);
423 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
425 return sprintf(buffer, "%s", sym_name);
426 return sprintf(buffer, "%lu", wchan);
428 #endif /* CONFIG_KALLSYMS */
430 #ifdef CONFIG_SCHEDSTATS
432 * Provides /proc/PID/schedstat
434 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
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);
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)
447 unsigned long points;
448 struct timespec uptime;
450 do_posix_clock_monotonic_gettime(&uptime);
451 points = badness(task, uptime.tv_sec);
452 return sprintf(buffer, "%lu\n", points);
455 /************************************************************************/
456 /* Here the fs part begins */
457 /************************************************************************/
459 /* permission checks */
461 static int proc_check_root(struct inode *inode)
463 struct dentry *de, *base, *root;
464 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
467 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
469 read_lock(¤t->fs->lock);
470 our_vfsmnt = mntget(current->fs->rootmnt);
471 base = dget(current->fs->root);
472 read_unlock(¤t->fs->lock);
474 spin_lock(&vfsmount_lock);
478 while (vfsmnt != our_vfsmnt) {
479 if (vfsmnt == vfsmnt->mnt_parent)
481 de = vfsmnt->mnt_mountpoint;
482 vfsmnt = vfsmnt->mnt_parent;
485 if (!is_subdir(de, base))
487 spin_unlock(&vfsmount_lock);
496 spin_unlock(&vfsmount_lock);
501 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
503 if (generic_permission(inode, mask, NULL) != 0)
505 return proc_check_root(inode);
508 extern struct seq_operations proc_pid_maps_op;
509 static int maps_open(struct inode *inode, struct file *file)
511 struct task_struct *task = proc_task(inode);
512 int ret = seq_open(file, &proc_pid_maps_op);
514 struct seq_file *m = file->private_data;
520 static struct file_operations proc_maps_operations = {
524 .release = seq_release,
527 extern struct seq_operations mounts_op;
528 static int mounts_open(struct inode *inode, struct file *file)
530 struct task_struct *task = proc_task(inode);
531 int ret = seq_open(file, &mounts_op);
534 struct seq_file *m = file->private_data;
535 struct namespace *namespace;
537 namespace = task->namespace;
539 get_namespace(namespace);
543 m->private = namespace;
545 seq_release(inode, file);
552 static int mounts_release(struct inode *inode, struct file *file)
554 struct seq_file *m = file->private_data;
555 struct namespace *namespace = m->private;
556 put_namespace(namespace);
557 return seq_release(inode, file);
560 static struct file_operations proc_mounts_operations = {
564 .release = mounts_release,
567 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
569 static ssize_t proc_info_read(struct file * file, char __user * buf,
570 size_t count, loff_t *ppos)
572 struct inode * inode = file->f_dentry->d_inode;
575 struct task_struct *task = proc_task(inode);
577 if (count > PROC_BLOCK_SIZE)
578 count = PROC_BLOCK_SIZE;
579 if (!(page = __get_free_page(GFP_KERNEL)))
582 length = PROC_I(inode)->op.proc_read(task, (char*)page);
585 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
590 static struct file_operations proc_info_file_operations = {
591 .read = proc_info_read,
594 static int mem_open(struct inode* inode, struct file* file)
596 file->private_data = (void*)((long)current->self_exec_id);
600 static ssize_t mem_read(struct file * file, char __user * buf,
601 size_t count, loff_t *ppos)
603 struct task_struct *task = proc_task(file->f_dentry->d_inode);
605 unsigned long src = *ppos;
607 struct mm_struct *mm;
609 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
613 page = (char *)__get_free_page(GFP_USER);
619 mm = get_task_mm(task);
625 if (file->private_data != (void*)((long)current->self_exec_id))
631 int this_len, retval;
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)) {
641 if (copy_to_user(buf, page, retval)) {
656 free_page((unsigned long) page);
661 #define mem_write NULL
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)
670 struct task_struct *task = proc_task(file->f_dentry->d_inode);
671 unsigned long dst = *ppos;
673 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
676 page = (char *)__get_free_page(GFP_USER);
681 int this_len, retval;
683 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
684 if (copy_from_user(page, buf, this_len)) {
688 retval = access_process_vm(task, dst, page, this_len, 1);
700 free_page((unsigned long) page);
705 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
709 file->f_pos = offset;
712 file->f_pos += offset;
717 force_successful_syscall_return();
721 static struct file_operations proc_mem_operations = {
728 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
729 size_t count, loff_t *ppos)
731 struct task_struct *task = proc_task(file->f_dentry->d_inode);
734 int oom_adjust = task->oomkilladj;
735 loff_t __ppos = *ppos;
737 len = sprintf(buffer, "%i\n", oom_adjust);
740 if (count > len-__ppos)
742 if (copy_to_user(buf, buffer + __ppos, count))
744 *ppos = __ppos + count;
748 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
749 size_t count, loff_t *ppos)
751 struct task_struct *task = proc_task(file->f_dentry->d_inode);
752 char buffer[8], *end;
755 if (!capable(CAP_SYS_RESOURCE))
757 memset(buffer, 0, 8);
760 if (copy_from_user(buffer, buf, count))
762 oom_adjust = simple_strtol(buffer, &end, 0);
763 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
767 task->oomkilladj = oom_adjust;
768 if (end - buffer == 0)
773 static struct file_operations proc_oom_adjust_operations = {
774 .read = oom_adjust_read,
775 .write = oom_adjust_write,
778 static struct inode_operations proc_mem_inode_operations = {
779 .permission = proc_permission,
782 #ifdef CONFIG_AUDITSYSCALL
784 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
785 size_t count, loff_t *ppos)
787 struct inode * inode = file->f_dentry->d_inode;
788 struct task_struct *task = proc_task(inode);
790 char tmpbuf[TMPBUFLEN];
792 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
793 audit_get_loginuid(task->audit_context));
794 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
797 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
798 size_t count, loff_t *ppos)
800 struct inode * inode = file->f_dentry->d_inode;
803 struct task_struct *task = proc_task(inode);
806 if (!capable(CAP_AUDIT_CONTROL))
812 if (count > PAGE_SIZE)
816 /* No partial writes. */
819 page = (char*)__get_free_page(GFP_USER);
823 if (copy_from_user(page, buf, count))
826 loginuid = simple_strtoul(page, &tmp, 10);
832 length = audit_set_loginuid(task, loginuid);
833 if (likely(length == 0))
837 free_page((unsigned long) page);
841 static struct file_operations proc_loginuid_operations = {
842 .read = proc_loginuid_read,
843 .write = proc_loginuid_write,
847 #ifdef CONFIG_SECCOMP
848 static ssize_t seccomp_read(struct file *file, char __user *buf,
849 size_t count, loff_t *ppos)
851 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
853 loff_t __ppos = *ppos;
856 /* no need to print the trailing zero, so use only len */
857 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
860 if (count > len - __ppos)
861 count = len - __ppos;
862 if (copy_to_user(buf, __buf + __ppos, count))
864 *ppos = __ppos + count;
868 static ssize_t seccomp_write(struct file *file, const char __user *buf,
869 size_t count, loff_t *ppos)
871 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
872 char __buf[20], *end;
873 unsigned int seccomp_mode;
875 /* can set it only once to be even more secure */
876 if (unlikely(tsk->seccomp.mode))
879 memset(__buf, 0, sizeof(__buf));
880 count = min(count, sizeof(__buf) - 1);
881 if (copy_from_user(__buf, buf, count))
883 seccomp_mode = simple_strtoul(__buf, &end, 0);
886 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
887 tsk->seccomp.mode = seccomp_mode;
888 set_tsk_thread_flag(tsk, TIF_SECCOMP);
891 if (unlikely(!(end - __buf)))
896 static struct file_operations proc_seccomp_operations = {
897 .read = seccomp_read,
898 .write = seccomp_write,
900 #endif /* CONFIG_SECCOMP */
902 static int proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
904 struct inode *inode = dentry->d_inode;
907 /* We don't need a base pointer in the /proc filesystem */
910 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
912 error = proc_check_root(inode);
916 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
917 nd->last_type = LAST_BIND;
922 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
923 char __user *buffer, int buflen)
925 struct inode * inode;
926 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
932 inode = dentry->d_inode;
933 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
937 len = tmp + PAGE_SIZE - 1 - path;
941 if (copy_to_user(buffer, path, len))
944 free_page((unsigned long)tmp);
948 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
951 struct inode *inode = dentry->d_inode;
953 struct vfsmount *mnt = NULL;
957 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
959 error = proc_check_root(inode);
963 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
967 error = do_proc_readlink(de, mnt, buffer, buflen);
975 static struct inode_operations proc_pid_link_inode_operations = {
976 .readlink = proc_pid_readlink,
977 .follow_link = proc_pid_follow_link
982 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
984 struct inode *inode = filp->f_dentry->d_inode;
985 struct task_struct *p = proc_task(inode);
986 unsigned int fd, tid, ino;
989 struct files_struct * files;
1000 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1004 ino = fake_ino(tid, PROC_TID_INO);
1005 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1009 files = get_files_struct(p);
1012 spin_lock(&files->file_lock);
1013 for (fd = filp->f_pos-2;
1014 fd < files->max_fds;
1015 fd++, filp->f_pos++) {
1018 if (!fcheck_files(files, fd))
1020 spin_unlock(&files->file_lock);
1026 buf[j] = '0' + (i % 10);
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);
1035 spin_lock(&files->file_lock);
1037 spin_unlock(&files->file_lock);
1038 put_files_struct(files);
1044 static int proc_pident_readdir(struct file *filp,
1045 void *dirent, filldir_t filldir,
1046 struct pid_entry *ents, unsigned int nents)
1050 struct dentry *dentry = filp->f_dentry;
1051 struct inode *inode = dentry->d_inode;
1052 struct pid_entry *p;
1057 if (!pid_alive(proc_task(inode)))
1061 pid = proc_task(inode)->pid;
1066 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1072 ino = parent_ino(dentry);
1073 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1085 hide = vx_flags(VXF_INFO_HIDE, 0);
1089 case PROC_TGID_VX_INFO:
1090 case PROC_TGID_IP_INFO:
1094 if (filldir(dirent, p->name, p->len, filp->f_pos,
1095 fake_ino(pid, p->type), p->mode >> 12) < 0)
1108 static int proc_tgid_base_readdir(struct file * filp,
1109 void * dirent, filldir_t filldir)
1111 return proc_pident_readdir(filp,dirent,filldir,
1112 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1115 static int proc_tid_base_readdir(struct file * filp,
1116 void * dirent, filldir_t filldir)
1118 return proc_pident_readdir(filp,dirent,filldir,
1119 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1122 /* building an inode */
1124 static int task_dumpable(struct task_struct *task)
1127 struct mm_struct *mm;
1132 dumpable = mm->dumpable;
1138 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1140 struct inode * inode;
1141 struct proc_inode *ei;
1143 /* We need a new inode */
1145 inode = new_inode(sb);
1152 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1153 inode->i_ino = fake_ino(task->pid, ino);
1155 if (!pid_alive(task))
1159 * grab the reference to task.
1161 get_task_struct(task);
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;
1170 inode->i_xid = vx_task_xid(task);
1171 security_task_to_inode(task, inode);
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.
1189 * Rewrite the inode's ownerships here because the owning task may have
1190 * performed a setuid(), etc.
1192 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1194 struct inode *inode = dentry->d_inode;
1195 struct task_struct *task = proc_task(inode);
1197 if (!vx_check(vx_task_xid(task), VX_IDENT))
1199 /* discard wrong fakeinit */
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;
1209 security_task_to_inode(task, inode);
1217 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
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;
1224 files = get_files_struct(task);
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;
1237 security_task_to_inode(task, inode);
1240 spin_unlock(&files->file_lock);
1241 put_files_struct(files);
1247 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
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);
1257 static int pid_delete_dentry(struct dentry * dentry)
1259 /* Is the task we represent dead?
1260 * If so, then don't put the dentry on the lru list,
1261 * kill it immediately.
1263 return !pid_alive(proc_task(dentry->d_inode));
1266 static struct dentry_operations tid_fd_dentry_operations =
1268 .d_revalidate = tid_fd_revalidate,
1269 .d_delete = pid_delete_dentry,
1272 static struct dentry_operations pid_dentry_operations =
1274 .d_revalidate = pid_revalidate,
1275 .d_delete = pid_delete_dentry,
1278 static struct dentry_operations pid_base_dentry_operations =
1280 .d_revalidate = pid_revalidate,
1281 .d_iput = pid_base_iput,
1282 .d_delete = pid_delete_dentry,
1287 static unsigned name_to_int(struct dentry *dentry)
1289 const char *name = dentry->d_name.name;
1290 int len = dentry->d_name.len;
1293 if (len > 1 && *name == '0')
1296 unsigned c = *name++ - '0';
1299 if (n >= (~0U-9)/10)
1310 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1312 struct task_struct *task = proc_task(dir);
1313 unsigned fd = name_to_int(dentry);
1315 struct files_struct * files;
1316 struct inode *inode;
1317 struct proc_inode *ei;
1321 if (!pid_alive(task))
1324 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1328 files = get_files_struct(task);
1331 inode->i_mode = S_IFLNK;
1332 spin_lock(&files->file_lock);
1333 file = fcheck_files(files, fd);
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;
1344 ei->op.proc_get_link = proc_fd_link;
1345 dentry->d_op = &tid_fd_dentry_operations;
1346 d_add(dentry, inode);
1350 spin_unlock(&files->file_lock);
1351 put_files_struct(files);
1355 return ERR_PTR(-ENOENT);
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);
1361 static struct file_operations proc_fd_operations = {
1362 .read = generic_read_dir,
1363 .readdir = proc_readfd,
1366 static struct file_operations proc_task_operations = {
1367 .read = generic_read_dir,
1368 .readdir = proc_task_readdir,
1372 * proc directories can do almost nothing..
1374 static struct inode_operations proc_fd_inode_operations = {
1375 .lookup = proc_lookupfd,
1376 .permission = proc_permission,
1379 static struct inode_operations proc_task_inode_operations = {
1380 .lookup = proc_task_lookup,
1381 .permission = proc_permission,
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)
1388 struct inode * inode = file->f_dentry->d_inode;
1391 struct task_struct *task = proc_task(inode);
1393 if (count > PAGE_SIZE)
1395 if (!(page = __get_free_page(GFP_KERNEL)))
1398 length = security_getprocattr(task,
1399 (char*)file->f_dentry->d_name.name,
1400 (void*)page, count);
1402 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1407 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1408 size_t count, loff_t *ppos)
1410 struct inode * inode = file->f_dentry->d_inode;
1413 struct task_struct *task = proc_task(inode);
1415 if (count > PAGE_SIZE)
1418 /* No partial writes. */
1421 page = (char*)__get_free_page(GFP_USER);
1425 if (copy_from_user(page, buf, count))
1428 length = security_setprocattr(task,
1429 (char*)file->f_dentry->d_name.name,
1430 (void*)page, count);
1432 free_page((unsigned long) page);
1436 static struct file_operations proc_pid_attr_operations = {
1437 .read = proc_pid_attr_read,
1438 .write = proc_pid_attr_write,
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;
1447 extern int proc_pid_vx_info(struct task_struct *, char *);
1448 extern int proc_pid_nx_info(struct task_struct *, char *);
1450 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1453 static struct dentry *proc_pident_lookup(struct inode *dir,
1454 struct dentry *dentry,
1455 struct pid_entry *ents)
1457 struct inode *inode;
1459 struct task_struct *task = proc_task(dir);
1460 struct pid_entry *p;
1461 struct proc_inode *ei;
1466 if (!pid_alive(task))
1469 for (p = ents; p->name; p++) {
1470 if (p->len != dentry->d_name.len)
1472 if (!memcmp(dentry->d_name.name, p->name, p->len))
1479 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1484 inode->i_mode = p->mode;
1486 * Yes, it does not scale. And it should not. Don't add
1487 * new entries into /proc/<tgid>/ without very good reasons.
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;
1498 inode->i_op = &proc_fd_inode_operations;
1499 inode->i_fop = &proc_fd_operations;
1503 inode->i_op = &proc_pid_link_inode_operations;
1504 ei->op.proc_get_link = proc_exe_link;
1508 inode->i_op = &proc_pid_link_inode_operations;
1509 ei->op.proc_get_link = proc_cwd_link;
1512 case PROC_TGID_ROOT:
1513 inode->i_op = &proc_pid_link_inode_operations;
1514 ei->op.proc_get_link = proc_root_link;
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;
1522 case PROC_TGID_AUXV:
1523 inode->i_fop = &proc_info_file_operations;
1524 ei->op.proc_read = proc_pid_auxv;
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;
1532 inode->i_fop = &proc_info_file_operations;
1533 ei->op.proc_read = proc_tid_stat;
1535 case PROC_TGID_STAT:
1536 inode->i_fop = &proc_info_file_operations;
1537 ei->op.proc_read = proc_tgid_stat;
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;
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;
1550 case PROC_TGID_MAPS:
1551 inode->i_fop = &proc_maps_operations;
1555 inode->i_op = &proc_mem_inode_operations;
1556 inode->i_fop = &proc_mem_operations;
1558 #ifdef CONFIG_SECCOMP
1559 case PROC_TID_SECCOMP:
1560 case PROC_TGID_SECCOMP:
1561 inode->i_fop = &proc_seccomp_operations;
1563 #endif /* CONFIG_SECCOMP */
1564 case PROC_TID_MOUNTS:
1565 case PROC_TGID_MOUNTS:
1566 inode->i_fop = &proc_mounts_operations;
1568 #ifdef CONFIG_SECURITY
1571 inode->i_op = &proc_tid_attr_inode_operations;
1572 inode->i_fop = &proc_tid_attr_operations;
1574 case PROC_TGID_ATTR:
1576 inode->i_op = &proc_tgid_attr_inode_operations;
1577 inode->i_fop = &proc_tgid_attr_operations;
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;
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;
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;
1604 #ifdef CONFIG_CPUSETS
1605 case PROC_TID_CPUSET:
1606 case PROC_TGID_CPUSET:
1607 inode->i_fop = &proc_cpuset_operations;
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;
1615 case PROC_TID_OOM_ADJUST:
1616 case PROC_TGID_OOM_ADJUST:
1617 inode->i_fop = &proc_oom_adjust_operations;
1619 #ifdef CONFIG_AUDITSYSCALL
1620 case PROC_TID_LOGINUID:
1621 case PROC_TGID_LOGINUID:
1622 inode->i_fop = &proc_loginuid_operations;
1625 case PROC_TID_VX_INFO:
1626 case PROC_TGID_VX_INFO:
1627 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1629 inode->i_fop = &proc_info_file_operations;
1630 ei->op.proc_read = proc_pid_vx_info;
1632 case PROC_TID_IP_INFO:
1633 case PROC_TGID_IP_INFO:
1634 if (task_vx_flags(task, VXF_INFO_HIDE, 0))
1636 inode->i_fop = &proc_info_file_operations;
1637 ei->op.proc_read = proc_pid_nx_info;
1640 printk("procfs: impossible type (%d)",p->type);
1644 dentry->d_op = &pid_dentry_operations;
1645 d_add(dentry, inode);
1653 return ERR_PTR(error);
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);
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);
1664 static struct file_operations proc_tgid_base_operations = {
1665 .read = generic_read_dir,
1666 .readdir = proc_tgid_base_readdir,
1669 static struct file_operations proc_tid_base_operations = {
1670 .read = generic_read_dir,
1671 .readdir = proc_tid_base_readdir,
1674 static struct inode_operations proc_tgid_base_inode_operations = {
1675 .lookup = proc_tgid_base_lookup,
1678 static struct inode_operations proc_tid_base_inode_operations = {
1679 .lookup = proc_tid_base_lookup,
1682 #ifdef CONFIG_SECURITY
1683 static int proc_tgid_attr_readdir(struct file * filp,
1684 void * dirent, filldir_t filldir)
1686 return proc_pident_readdir(filp,dirent,filldir,
1687 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1690 static int proc_tid_attr_readdir(struct file * filp,
1691 void * dirent, filldir_t filldir)
1693 return proc_pident_readdir(filp,dirent,filldir,
1694 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1697 static struct file_operations proc_tgid_attr_operations = {
1698 .read = generic_read_dir,
1699 .readdir = proc_tgid_attr_readdir,
1702 static struct file_operations proc_tid_attr_operations = {
1703 .read = generic_read_dir,
1704 .readdir = proc_tid_attr_readdir,
1707 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1708 struct dentry *dentry, struct nameidata *nd)
1710 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1713 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1714 struct dentry *dentry, struct nameidata *nd)
1716 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1719 static struct inode_operations proc_tgid_attr_inode_operations = {
1720 .lookup = proc_tgid_attr_lookup,
1723 static struct inode_operations proc_tid_attr_inode_operations = {
1724 .lookup = proc_tid_attr_lookup,
1731 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1735 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1736 return vfs_readlink(dentry,buffer,buflen,tmp);
1739 static int proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1742 sprintf(tmp, "%d", vx_map_tgid(current->tgid));
1743 return vfs_follow_link(nd,tmp);
1746 static struct inode_operations proc_self_inode_operations = {
1747 .readlink = proc_self_readlink,
1748 .follow_link = proc_self_follow_link,
1752 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1753 * @p: task that should be flushed.
1755 * Drops the /proc/@pid dcache entry from the hash chains.
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
1767 struct dentry *proc_pid_unhash(struct task_struct *p)
1769 struct dentry *proc_dentry;
1771 proc_dentry = p->proc_dentry;
1772 if (proc_dentry != NULL) {
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);
1781 spin_unlock(&proc_dentry->d_lock);
1784 spin_unlock(&dcache_lock);
1790 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1791 * @proc_dentry: directoy to prune.
1793 * Shrink the /proc directory that was used by the just killed thread.
1796 void proc_pid_flush(struct dentry *proc_dentry)
1799 if(proc_dentry != NULL) {
1800 shrink_dcache_parent(proc_dentry);
1805 #define VXF_FAKE_INIT (VXF_INFO_INIT|VXF_STATE_INIT)
1807 static inline int proc_pid_visible(struct task_struct *task, int pid)
1810 !vx_flags(VXF_FAKE_INIT, VXF_FAKE_INIT))
1812 if (vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
1820 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1822 struct task_struct *task;
1823 struct inode *inode;
1824 struct proc_inode *ei;
1828 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1829 inode = new_inode(dir->i_sb);
1831 return ERR_PTR(-ENOMEM);
1833 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1834 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1836 inode->i_mode = S_IFLNK|S_IRWXUGO;
1837 inode->i_uid = inode->i_gid = 0;
1839 inode->i_op = &proc_self_inode_operations;
1840 d_add(dentry, inode);
1843 tgid = name_to_int(dentry);
1847 read_lock(&tasklist_lock);
1848 task = find_task_by_pid(tgid);
1850 get_task_struct(task);
1851 read_unlock(&tasklist_lock);
1855 /* check for context visibility */
1856 if (!proc_pid_visible(task, tgid))
1859 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
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
1873 dentry->d_op = &pid_base_dentry_operations;
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);
1883 spin_unlock(&task->proc_lock);
1885 put_task_struct(task);
1887 proc_pid_flush(dentry);
1892 put_task_struct(task);
1894 return ERR_PTR(-ENOENT);
1898 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1900 struct task_struct *task;
1901 struct task_struct *leader = proc_task(dir);
1902 struct inode *inode;
1905 tid = name_to_int(dentry);
1908 if (vx_current_initpid(tid))
1911 read_lock(&tasklist_lock);
1912 task = find_task_by_pid(tid);
1914 get_task_struct(task);
1915 read_unlock(&tasklist_lock);
1918 if (leader->tgid != task->tgid)
1921 /* check for context visibility */
1922 if (!proc_pid_visible(task, tid))
1925 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
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
1939 dentry->d_op = &pid_base_dentry_operations;
1941 d_add(dentry, inode);
1943 put_task_struct(task);
1946 put_task_struct(task);
1948 return ERR_PTR(-ENOENT);
1951 #define PROC_NUMBUF 10
1952 #define PROC_MAXPIDS 20
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..
1959 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1961 struct task_struct *p;
1965 read_lock(&tasklist_lock);
1968 p = find_task_by_real_pid(version);
1969 if (p && !thread_group_leader(p))
1976 p = next_task(&init_task);
1978 for ( ; p != &init_task; p = next_task(p)) {
1983 /* check for context visibility */
1984 if (!proc_pid_visible(p, tgid))
1988 tgids[nr_tgids] = vx_map_tgid(tgid);
1990 if (nr_tgids >= PROC_MAXPIDS)
1993 read_unlock(&tasklist_lock);
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..
2002 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
2004 struct task_struct *leader_task = proc_task(dir);
2005 struct task_struct *task = leader_task;
2009 read_lock(&tasklist_lock);
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().
2015 if (pid_alive(task)) do {
2016 int tid = task->pid;
2018 /* check for context visibility */
2019 if (!proc_pid_visible(task, tid))
2024 tids[nr_tids] = vx_map_pid(tid);
2026 if (nr_tids >= PROC_MAXPIDS)
2028 } while ((task = next_thread(task)) != leader_task);
2029 read_unlock(&tasklist_lock);
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)
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;
2043 ino_t ino = fake_ino(0,PROC_TGID_INO);
2044 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
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.
2053 next_tgid = filp->f_version;
2054 filp->f_version = 0;
2056 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2058 /* no more entries ! */
2063 /* do not use the last found pid, reserve it for next_tgid */
2064 if (nr_tgids == PROC_MAXPIDS) {
2066 next_tgid = tgid_array[nr_tgids];
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;
2075 buf[--j] = '0' + (tgid % 10);
2076 while ((tgid /= 10) != 0);
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];
2092 /* for the /proc/TGID/task/ directories */
2093 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
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;
2103 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2105 if (!vx_check(vx_task_xid(task), VX_WATCH|VX_IDENT))
2107 if (!pid_alive(task))
2114 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2119 ino = parent_ino(dentry);
2120 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2126 nr_tids = get_tid_list(pos, tid_array, inode);
2127 inode->i_nlink = pos + nr_tids;
2129 for (i = 0; i < nr_tids; i++) {
2130 unsigned long j = PROC_NUMBUF;
2131 int tid = tid_array[i];
2133 ino = fake_ino(tid,PROC_TID_INO);
2136 buf[--j] = '0' + (tid % 10);
2137 while ((tid /= 10) != 0);
2139 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)