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>
41 * For hysterical raisins we keep the same inumbers as in the old procfs.
42 * Feel free to change the macro below - just keep the range distinct from
43 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
44 * As soon as we'll get a separate superblock we will be able to forget
45 * about magical ranges too.
48 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
50 enum pid_directory_inos {
71 #ifdef CONFIG_SCHEDSTATS
77 #ifdef CONFIG_SECURITY
79 PROC_TGID_ATTR_CURRENT,
82 PROC_TGID_ATTR_FSCREATE,
84 #ifdef CONFIG_AUDITSYSCALL
109 #ifdef CONFIG_SCHEDSTATS
112 #ifdef CONFIG_CPUSETS
115 #ifdef CONFIG_SECURITY
117 PROC_TID_ATTR_CURRENT,
120 PROC_TID_ATTR_FSCREATE,
122 #ifdef CONFIG_AUDITSYSCALL
125 PROC_TID_FD_DIR = 0x8000, /* 0x8000-0xffff */
137 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
139 static struct pid_entry tgid_base_stuff[] = {
140 E(PROC_TGID_TASK, "task", S_IFDIR|S_IRUGO|S_IXUGO),
141 E(PROC_TGID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
142 E(PROC_TGID_ENVIRON, "environ", S_IFREG|S_IRUSR),
143 E(PROC_TGID_AUXV, "auxv", S_IFREG|S_IRUSR),
144 E(PROC_TGID_STATUS, "status", S_IFREG|S_IRUGO),
145 E(PROC_TGID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
146 E(PROC_TGID_STAT, "stat", S_IFREG|S_IRUGO),
147 E(PROC_TGID_STATM, "statm", S_IFREG|S_IRUGO),
148 E(PROC_TGID_MAPS, "maps", S_IFREG|S_IRUGO),
150 E(PROC_TGID_NUMA_MAPS, "numa_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_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
173 E(PROC_TGID_OOM_ADJUST,"oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
174 #ifdef CONFIG_AUDITSYSCALL
175 E(PROC_TGID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
179 static struct pid_entry tid_base_stuff[] = {
180 E(PROC_TID_FD, "fd", S_IFDIR|S_IRUSR|S_IXUSR),
181 E(PROC_TID_ENVIRON, "environ", S_IFREG|S_IRUSR),
182 E(PROC_TID_AUXV, "auxv", S_IFREG|S_IRUSR),
183 E(PROC_TID_STATUS, "status", S_IFREG|S_IRUGO),
184 E(PROC_TID_CMDLINE, "cmdline", S_IFREG|S_IRUGO),
185 E(PROC_TID_STAT, "stat", S_IFREG|S_IRUGO),
186 E(PROC_TID_STATM, "statm", S_IFREG|S_IRUGO),
187 E(PROC_TID_MAPS, "maps", S_IFREG|S_IRUGO),
189 E(PROC_TID_NUMA_MAPS, "numa_maps", S_IFREG|S_IRUGO),
191 E(PROC_TID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR),
192 #ifdef CONFIG_SECCOMP
193 E(PROC_TID_SECCOMP, "seccomp", S_IFREG|S_IRUSR|S_IWUSR),
195 E(PROC_TID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
196 E(PROC_TID_ROOT, "root", S_IFLNK|S_IRWXUGO),
197 E(PROC_TID_EXE, "exe", S_IFLNK|S_IRWXUGO),
198 E(PROC_TID_MOUNTS, "mounts", S_IFREG|S_IRUGO),
199 #ifdef CONFIG_SECURITY
200 E(PROC_TID_ATTR, "attr", S_IFDIR|S_IRUGO|S_IXUGO),
202 #ifdef CONFIG_KALLSYMS
203 E(PROC_TID_WCHAN, "wchan", S_IFREG|S_IRUGO),
205 #ifdef CONFIG_SCHEDSTATS
206 E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),
208 #ifdef CONFIG_CPUSETS
209 E(PROC_TID_CPUSET, "cpuset", S_IFREG|S_IRUGO),
211 E(PROC_TID_OOM_SCORE, "oom_score",S_IFREG|S_IRUGO),
212 E(PROC_TID_OOM_ADJUST, "oom_adj", S_IFREG|S_IRUGO|S_IWUSR),
213 #ifdef CONFIG_AUDITSYSCALL
214 E(PROC_TID_LOGINUID, "loginuid", S_IFREG|S_IWUSR|S_IRUGO),
219 #ifdef CONFIG_SECURITY
220 static struct pid_entry tgid_attr_stuff[] = {
221 E(PROC_TGID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
222 E(PROC_TGID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
223 E(PROC_TGID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
224 E(PROC_TGID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
227 static struct pid_entry tid_attr_stuff[] = {
228 E(PROC_TID_ATTR_CURRENT, "current", S_IFREG|S_IRUGO|S_IWUGO),
229 E(PROC_TID_ATTR_PREV, "prev", S_IFREG|S_IRUGO),
230 E(PROC_TID_ATTR_EXEC, "exec", S_IFREG|S_IRUGO|S_IWUGO),
231 E(PROC_TID_ATTR_FSCREATE, "fscreate", S_IFREG|S_IRUGO|S_IWUGO),
238 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
240 struct task_struct *task = proc_task(inode);
241 struct files_struct *files;
243 int fd = proc_type(inode) - PROC_TID_FD_DIR;
245 files = get_files_struct(task);
247 spin_lock(&files->file_lock);
248 file = fcheck_files(files, fd);
250 *mnt = mntget(file->f_vfsmnt);
251 *dentry = dget(file->f_dentry);
252 spin_unlock(&files->file_lock);
253 put_files_struct(files);
256 spin_unlock(&files->file_lock);
257 put_files_struct(files);
262 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
264 struct fs_struct *fs;
265 int result = -ENOENT;
266 task_lock(proc_task(inode));
267 fs = proc_task(inode)->fs;
269 atomic_inc(&fs->count);
270 task_unlock(proc_task(inode));
272 read_lock(&fs->lock);
273 *mnt = mntget(fs->pwdmnt);
274 *dentry = dget(fs->pwd);
275 read_unlock(&fs->lock);
282 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
284 struct fs_struct *fs;
285 int result = -ENOENT;
286 task_lock(proc_task(inode));
287 fs = proc_task(inode)->fs;
289 atomic_inc(&fs->count);
290 task_unlock(proc_task(inode));
292 read_lock(&fs->lock);
293 *mnt = mntget(fs->rootmnt);
294 *dentry = dget(fs->root);
295 read_unlock(&fs->lock);
302 #define MAY_PTRACE(task) \
303 (task == current || \
304 (task->parent == current && \
305 (task->ptrace & PT_PTRACED) && \
306 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
307 security_ptrace(current,task) == 0))
309 static int may_ptrace_attach(struct task_struct *task)
317 if (((current->uid != task->euid) ||
318 (current->uid != task->suid) ||
319 (current->uid != task->uid) ||
320 (current->gid != task->egid) ||
321 (current->gid != task->sgid) ||
322 (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE))
325 if (task->mm->dumpable != 1 && !capable(CAP_SYS_PTRACE))
327 if (security_ptrace(current, task))
336 static int proc_pid_environ(struct task_struct *task, char * buffer)
339 struct mm_struct *mm = get_task_mm(task);
341 unsigned int len = mm->env_end - mm->env_start;
344 res = access_process_vm(task, mm->env_start, buffer, len, 0);
345 if (!may_ptrace_attach(task))
352 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
356 struct mm_struct *mm = get_task_mm(task);
360 goto out_mm; /* Shh! No looking before we're done */
362 len = mm->arg_end - mm->arg_start;
367 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
369 // If the nul at the end of args has been overwritten, then
370 // assume application is using setproctitle(3).
371 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
372 len = strnlen(buffer, res);
376 len = mm->env_end - mm->env_start;
377 if (len > PAGE_SIZE - res)
378 len = PAGE_SIZE - res;
379 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
380 res = strnlen(buffer, res);
389 static int proc_pid_auxv(struct task_struct *task, char *buffer)
392 struct mm_struct *mm = get_task_mm(task);
394 unsigned int nwords = 0;
397 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
398 res = nwords * sizeof(mm->saved_auxv[0]);
401 memcpy(buffer, mm->saved_auxv, res);
408 #ifdef CONFIG_KALLSYMS
410 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
411 * Returns the resolved symbol. If that fails, simply return the address.
413 static int proc_pid_wchan(struct task_struct *task, char *buffer)
416 const char *sym_name;
417 unsigned long wchan, size, offset;
418 char namebuf[KSYM_NAME_LEN+1];
420 wchan = get_wchan(task);
422 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
424 return sprintf(buffer, "%s", sym_name);
425 return sprintf(buffer, "%lu", wchan);
427 #endif /* CONFIG_KALLSYMS */
429 #ifdef CONFIG_SCHEDSTATS
431 * Provides /proc/PID/schedstat
433 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
435 return sprintf(buffer, "%lu %lu %lu\n",
436 task->sched_info.cpu_time,
437 task->sched_info.run_delay,
438 task->sched_info.pcnt);
442 /* The badness from the OOM killer */
443 unsigned long badness(struct task_struct *p, unsigned long uptime);
444 static int proc_oom_score(struct task_struct *task, char *buffer)
446 unsigned long points;
447 struct timespec uptime;
449 do_posix_clock_monotonic_gettime(&uptime);
450 points = badness(task, uptime.tv_sec);
451 return sprintf(buffer, "%lu\n", points);
454 /************************************************************************/
455 /* Here the fs part begins */
456 /************************************************************************/
458 /* permission checks */
460 static int proc_check_root(struct inode *inode)
462 struct dentry *de, *base, *root;
463 struct vfsmount *our_vfsmnt, *vfsmnt, *mnt;
466 if (proc_root_link(inode, &root, &vfsmnt)) /* Ewww... */
468 read_lock(¤t->fs->lock);
469 our_vfsmnt = mntget(current->fs->rootmnt);
470 base = dget(current->fs->root);
471 read_unlock(¤t->fs->lock);
473 spin_lock(&vfsmount_lock);
477 while (vfsmnt != our_vfsmnt) {
478 if (vfsmnt == vfsmnt->mnt_parent)
480 de = vfsmnt->mnt_mountpoint;
481 vfsmnt = vfsmnt->mnt_parent;
484 if (!is_subdir(de, base))
486 spin_unlock(&vfsmount_lock);
495 spin_unlock(&vfsmount_lock);
500 static int proc_permission(struct inode *inode, int mask, struct nameidata *nd)
502 if (generic_permission(inode, mask, NULL) != 0)
504 return proc_check_root(inode);
507 extern struct seq_operations proc_pid_maps_op;
508 static int maps_open(struct inode *inode, struct file *file)
510 struct task_struct *task = proc_task(inode);
511 int ret = seq_open(file, &proc_pid_maps_op);
513 struct seq_file *m = file->private_data;
519 static struct file_operations proc_maps_operations = {
523 .release = seq_release,
527 extern struct seq_operations proc_pid_numa_maps_op;
528 static int numa_maps_open(struct inode *inode, struct file *file)
530 struct task_struct *task = proc_task(inode);
531 int ret = seq_open(file, &proc_pid_numa_maps_op);
533 struct seq_file *m = file->private_data;
539 static struct file_operations proc_numa_maps_operations = {
540 .open = numa_maps_open,
543 .release = seq_release,
547 extern struct seq_operations mounts_op;
548 static int mounts_open(struct inode *inode, struct file *file)
550 struct task_struct *task = proc_task(inode);
551 int ret = seq_open(file, &mounts_op);
554 struct seq_file *m = file->private_data;
555 struct namespace *namespace;
557 namespace = task->namespace;
559 get_namespace(namespace);
563 m->private = namespace;
565 seq_release(inode, file);
572 static int mounts_release(struct inode *inode, struct file *file)
574 struct seq_file *m = file->private_data;
575 struct namespace *namespace = m->private;
576 put_namespace(namespace);
577 return seq_release(inode, file);
580 static struct file_operations proc_mounts_operations = {
584 .release = mounts_release,
587 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
589 static ssize_t proc_info_read(struct file * file, char __user * buf,
590 size_t count, loff_t *ppos)
592 struct inode * inode = file->f_dentry->d_inode;
595 struct task_struct *task = proc_task(inode);
597 if (count > PROC_BLOCK_SIZE)
598 count = PROC_BLOCK_SIZE;
599 if (!(page = __get_free_page(GFP_KERNEL)))
602 length = PROC_I(inode)->op.proc_read(task, (char*)page);
605 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
610 static struct file_operations proc_info_file_operations = {
611 .read = proc_info_read,
614 static int mem_open(struct inode* inode, struct file* file)
616 file->private_data = (void*)((long)current->self_exec_id);
620 static ssize_t mem_read(struct file * file, char __user * buf,
621 size_t count, loff_t *ppos)
623 struct task_struct *task = proc_task(file->f_dentry->d_inode);
625 unsigned long src = *ppos;
627 struct mm_struct *mm;
629 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
633 page = (char *)__get_free_page(GFP_USER);
639 mm = get_task_mm(task);
645 if (file->private_data != (void*)((long)current->self_exec_id))
651 int this_len, retval;
653 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
654 retval = access_process_vm(task, src, page, this_len, 0);
655 if (!retval || !MAY_PTRACE(task) || !may_ptrace_attach(task)) {
661 if (copy_to_user(buf, page, retval)) {
676 free_page((unsigned long) page);
681 #define mem_write NULL
684 /* This is a security hazard */
685 static ssize_t mem_write(struct file * file, const char * buf,
686 size_t count, loff_t *ppos)
690 struct task_struct *task = proc_task(file->f_dentry->d_inode);
691 unsigned long dst = *ppos;
693 if (!MAY_PTRACE(task) || !may_ptrace_attach(task))
696 page = (char *)__get_free_page(GFP_USER);
701 int this_len, retval;
703 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
704 if (copy_from_user(page, buf, this_len)) {
708 retval = access_process_vm(task, dst, page, this_len, 1);
720 free_page((unsigned long) page);
725 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
729 file->f_pos = offset;
732 file->f_pos += offset;
737 force_successful_syscall_return();
741 static struct file_operations proc_mem_operations = {
748 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
749 size_t count, loff_t *ppos)
751 struct task_struct *task = proc_task(file->f_dentry->d_inode);
754 int oom_adjust = task->oomkilladj;
755 loff_t __ppos = *ppos;
757 len = sprintf(buffer, "%i\n", oom_adjust);
760 if (count > len-__ppos)
762 if (copy_to_user(buf, buffer + __ppos, count))
764 *ppos = __ppos + count;
768 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
769 size_t count, loff_t *ppos)
771 struct task_struct *task = proc_task(file->f_dentry->d_inode);
772 char buffer[8], *end;
775 if (!capable(CAP_SYS_RESOURCE))
777 memset(buffer, 0, 8);
780 if (copy_from_user(buffer, buf, count))
782 oom_adjust = simple_strtol(buffer, &end, 0);
783 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
787 task->oomkilladj = oom_adjust;
788 if (end - buffer == 0)
793 static struct file_operations proc_oom_adjust_operations = {
794 .read = oom_adjust_read,
795 .write = oom_adjust_write,
798 static struct inode_operations proc_mem_inode_operations = {
799 .permission = proc_permission,
802 #ifdef CONFIG_AUDITSYSCALL
804 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
805 size_t count, loff_t *ppos)
807 struct inode * inode = file->f_dentry->d_inode;
808 struct task_struct *task = proc_task(inode);
810 char tmpbuf[TMPBUFLEN];
812 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
813 audit_get_loginuid(task->audit_context));
814 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
817 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
818 size_t count, loff_t *ppos)
820 struct inode * inode = file->f_dentry->d_inode;
823 struct task_struct *task = proc_task(inode);
826 if (!capable(CAP_AUDIT_CONTROL))
832 if (count > PAGE_SIZE)
836 /* No partial writes. */
839 page = (char*)__get_free_page(GFP_USER);
843 if (copy_from_user(page, buf, count))
846 loginuid = simple_strtoul(page, &tmp, 10);
852 length = audit_set_loginuid(task, loginuid);
853 if (likely(length == 0))
857 free_page((unsigned long) page);
861 static struct file_operations proc_loginuid_operations = {
862 .read = proc_loginuid_read,
863 .write = proc_loginuid_write,
867 #ifdef CONFIG_SECCOMP
868 static ssize_t seccomp_read(struct file *file, char __user *buf,
869 size_t count, loff_t *ppos)
871 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
873 loff_t __ppos = *ppos;
876 /* no need to print the trailing zero, so use only len */
877 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
880 if (count > len - __ppos)
881 count = len - __ppos;
882 if (copy_to_user(buf, __buf + __ppos, count))
884 *ppos = __ppos + count;
888 static ssize_t seccomp_write(struct file *file, const char __user *buf,
889 size_t count, loff_t *ppos)
891 struct task_struct *tsk = proc_task(file->f_dentry->d_inode);
892 char __buf[20], *end;
893 unsigned int seccomp_mode;
895 /* can set it only once to be even more secure */
896 if (unlikely(tsk->seccomp.mode))
899 memset(__buf, 0, sizeof(__buf));
900 count = min(count, sizeof(__buf) - 1);
901 if (copy_from_user(__buf, buf, count))
903 seccomp_mode = simple_strtoul(__buf, &end, 0);
906 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
907 tsk->seccomp.mode = seccomp_mode;
908 set_tsk_thread_flag(tsk, TIF_SECCOMP);
911 if (unlikely(!(end - __buf)))
916 static struct file_operations proc_seccomp_operations = {
917 .read = seccomp_read,
918 .write = seccomp_write,
920 #endif /* CONFIG_SECCOMP */
922 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
924 struct inode *inode = dentry->d_inode;
927 /* We don't need a base pointer in the /proc filesystem */
930 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
932 error = proc_check_root(inode);
936 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
937 nd->last_type = LAST_BIND;
939 return ERR_PTR(error);
942 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
943 char __user *buffer, int buflen)
945 struct inode * inode;
946 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
952 inode = dentry->d_inode;
953 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
957 len = tmp + PAGE_SIZE - 1 - path;
961 if (copy_to_user(buffer, path, len))
964 free_page((unsigned long)tmp);
968 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
971 struct inode *inode = dentry->d_inode;
973 struct vfsmount *mnt = NULL;
977 if (current->fsuid != inode->i_uid && !capable(CAP_DAC_OVERRIDE))
979 error = proc_check_root(inode);
983 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
987 error = do_proc_readlink(de, mnt, buffer, buflen);
995 static struct inode_operations proc_pid_link_inode_operations = {
996 .readlink = proc_pid_readlink,
997 .follow_link = proc_pid_follow_link
1002 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1004 struct inode *inode = filp->f_dentry->d_inode;
1005 struct task_struct *p = proc_task(inode);
1006 unsigned int fd, tid, ino;
1009 struct files_struct * files;
1020 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1024 ino = fake_ino(tid, PROC_TID_INO);
1025 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1029 files = get_files_struct(p);
1032 spin_lock(&files->file_lock);
1033 for (fd = filp->f_pos-2;
1034 fd < files->max_fds;
1035 fd++, filp->f_pos++) {
1038 if (!fcheck_files(files, fd))
1040 spin_unlock(&files->file_lock);
1046 buf[j] = '0' + (i % 10);
1050 ino = fake_ino(tid, PROC_TID_FD_DIR + fd);
1051 if (filldir(dirent, buf+j, NUMBUF-j, fd+2, ino, DT_LNK) < 0) {
1052 spin_lock(&files->file_lock);
1055 spin_lock(&files->file_lock);
1057 spin_unlock(&files->file_lock);
1058 put_files_struct(files);
1064 static int proc_pident_readdir(struct file *filp,
1065 void *dirent, filldir_t filldir,
1066 struct pid_entry *ents, unsigned int nents)
1070 struct dentry *dentry = filp->f_dentry;
1071 struct inode *inode = dentry->d_inode;
1072 struct pid_entry *p;
1077 if (!pid_alive(proc_task(inode)))
1081 pid = proc_task(inode)->pid;
1086 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1092 ino = parent_ino(dentry);
1093 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1106 if (filldir(dirent, p->name, p->len, filp->f_pos,
1107 fake_ino(pid, p->type), p->mode >> 12) < 0)
1119 static int proc_tgid_base_readdir(struct file * filp,
1120 void * dirent, filldir_t filldir)
1122 return proc_pident_readdir(filp,dirent,filldir,
1123 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1126 static int proc_tid_base_readdir(struct file * filp,
1127 void * dirent, filldir_t filldir)
1129 return proc_pident_readdir(filp,dirent,filldir,
1130 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
1133 /* building an inode */
1135 static int task_dumpable(struct task_struct *task)
1138 struct mm_struct *mm;
1143 dumpable = mm->dumpable;
1151 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task, int ino)
1153 struct inode * inode;
1154 struct proc_inode *ei;
1156 /* We need a new inode */
1158 inode = new_inode(sb);
1165 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1166 inode->i_ino = fake_ino(task->pid, ino);
1168 if (!pid_alive(task))
1172 * grab the reference to task.
1174 get_task_struct(task);
1179 if (ino == PROC_TGID_INO || ino == PROC_TID_INO || task_dumpable(task)) {
1180 inode->i_uid = task->euid;
1181 inode->i_gid = task->egid;
1183 security_task_to_inode(task, inode);
1197 * Exceptional case: normally we are not allowed to unhash a busy
1198 * directory. In this case, however, we can do it - no aliasing problems
1199 * due to the way we treat inodes.
1201 * Rewrite the inode's ownerships here because the owning task may have
1202 * performed a setuid(), etc.
1204 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1206 struct inode *inode = dentry->d_inode;
1207 struct task_struct *task = proc_task(inode);
1208 if (pid_alive(task)) {
1209 if (proc_type(inode) == PROC_TGID_INO || proc_type(inode) == PROC_TID_INO || task_dumpable(task)) {
1210 inode->i_uid = task->euid;
1211 inode->i_gid = task->egid;
1216 security_task_to_inode(task, inode);
1223 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1225 struct inode *inode = dentry->d_inode;
1226 struct task_struct *task = proc_task(inode);
1227 int fd = proc_type(inode) - PROC_TID_FD_DIR;
1228 struct files_struct *files;
1230 files = get_files_struct(task);
1232 spin_lock(&files->file_lock);
1233 if (fcheck_files(files, fd)) {
1234 spin_unlock(&files->file_lock);
1235 put_files_struct(files);
1236 if (task_dumpable(task)) {
1237 inode->i_uid = task->euid;
1238 inode->i_gid = task->egid;
1243 security_task_to_inode(task, inode);
1246 spin_unlock(&files->file_lock);
1247 put_files_struct(files);
1253 static void pid_base_iput(struct dentry *dentry, struct inode *inode)
1255 struct task_struct *task = proc_task(inode);
1256 spin_lock(&task->proc_lock);
1257 if (task->proc_dentry == dentry)
1258 task->proc_dentry = NULL;
1259 spin_unlock(&task->proc_lock);
1263 static int pid_delete_dentry(struct dentry * dentry)
1265 /* Is the task we represent dead?
1266 * If so, then don't put the dentry on the lru list,
1267 * kill it immediately.
1269 return !pid_alive(proc_task(dentry->d_inode));
1272 static struct dentry_operations tid_fd_dentry_operations =
1274 .d_revalidate = tid_fd_revalidate,
1275 .d_delete = pid_delete_dentry,
1278 static struct dentry_operations pid_dentry_operations =
1280 .d_revalidate = pid_revalidate,
1281 .d_delete = pid_delete_dentry,
1284 static struct dentry_operations pid_base_dentry_operations =
1286 .d_revalidate = pid_revalidate,
1287 .d_iput = pid_base_iput,
1288 .d_delete = pid_delete_dentry,
1293 static unsigned name_to_int(struct dentry *dentry)
1295 const char *name = dentry->d_name.name;
1296 int len = dentry->d_name.len;
1299 if (len > 1 && *name == '0')
1302 unsigned c = *name++ - '0';
1305 if (n >= (~0U-9)/10)
1316 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1318 struct task_struct *task = proc_task(dir);
1319 unsigned fd = name_to_int(dentry);
1321 struct files_struct * files;
1322 struct inode *inode;
1323 struct proc_inode *ei;
1327 if (!pid_alive(task))
1330 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_FD_DIR+fd);
1334 files = get_files_struct(task);
1337 inode->i_mode = S_IFLNK;
1338 spin_lock(&files->file_lock);
1339 file = fcheck_files(files, fd);
1342 if (file->f_mode & 1)
1343 inode->i_mode |= S_IRUSR | S_IXUSR;
1344 if (file->f_mode & 2)
1345 inode->i_mode |= S_IWUSR | S_IXUSR;
1346 spin_unlock(&files->file_lock);
1347 put_files_struct(files);
1348 inode->i_op = &proc_pid_link_inode_operations;
1350 ei->op.proc_get_link = proc_fd_link;
1351 dentry->d_op = &tid_fd_dentry_operations;
1352 d_add(dentry, inode);
1356 spin_unlock(&files->file_lock);
1357 put_files_struct(files);
1361 return ERR_PTR(-ENOENT);
1364 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir);
1365 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd);
1367 static struct file_operations proc_fd_operations = {
1368 .read = generic_read_dir,
1369 .readdir = proc_readfd,
1372 static struct file_operations proc_task_operations = {
1373 .read = generic_read_dir,
1374 .readdir = proc_task_readdir,
1378 * proc directories can do almost nothing..
1380 static struct inode_operations proc_fd_inode_operations = {
1381 .lookup = proc_lookupfd,
1382 .permission = proc_permission,
1385 static struct inode_operations proc_task_inode_operations = {
1386 .lookup = proc_task_lookup,
1387 .permission = proc_permission,
1390 #ifdef CONFIG_SECURITY
1391 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1392 size_t count, loff_t *ppos)
1394 struct inode * inode = file->f_dentry->d_inode;
1397 struct task_struct *task = proc_task(inode);
1399 if (count > PAGE_SIZE)
1401 if (!(page = __get_free_page(GFP_KERNEL)))
1404 length = security_getprocattr(task,
1405 (char*)file->f_dentry->d_name.name,
1406 (void*)page, count);
1408 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1413 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1414 size_t count, loff_t *ppos)
1416 struct inode * inode = file->f_dentry->d_inode;
1419 struct task_struct *task = proc_task(inode);
1421 if (count > PAGE_SIZE)
1424 /* No partial writes. */
1427 page = (char*)__get_free_page(GFP_USER);
1431 if (copy_from_user(page, buf, count))
1434 length = security_setprocattr(task,
1435 (char*)file->f_dentry->d_name.name,
1436 (void*)page, count);
1438 free_page((unsigned long) page);
1442 static struct file_operations proc_pid_attr_operations = {
1443 .read = proc_pid_attr_read,
1444 .write = proc_pid_attr_write,
1447 static struct file_operations proc_tid_attr_operations;
1448 static struct inode_operations proc_tid_attr_inode_operations;
1449 static struct file_operations proc_tgid_attr_operations;
1450 static struct inode_operations proc_tgid_attr_inode_operations;
1453 static int get_tid_list(int index, unsigned int *tids, struct inode *dir);
1456 static struct dentry *proc_pident_lookup(struct inode *dir,
1457 struct dentry *dentry,
1458 struct pid_entry *ents)
1460 struct inode *inode;
1462 struct task_struct *task = proc_task(dir);
1463 struct pid_entry *p;
1464 struct proc_inode *ei;
1469 if (!pid_alive(task))
1472 for (p = ents; p->name; p++) {
1473 if (p->len != dentry->d_name.len)
1475 if (!memcmp(dentry->d_name.name, p->name, p->len))
1482 inode = proc_pid_make_inode(dir->i_sb, task, p->type);
1487 inode->i_mode = p->mode;
1489 * Yes, it does not scale. And it should not. Don't add
1490 * new entries into /proc/<tgid>/ without very good reasons.
1493 case PROC_TGID_TASK:
1494 inode->i_nlink = 2 + get_tid_list(2, NULL, dir);
1495 inode->i_op = &proc_task_inode_operations;
1496 inode->i_fop = &proc_task_operations;
1501 inode->i_op = &proc_fd_inode_operations;
1502 inode->i_fop = &proc_fd_operations;
1506 inode->i_op = &proc_pid_link_inode_operations;
1507 ei->op.proc_get_link = proc_exe_link;
1511 inode->i_op = &proc_pid_link_inode_operations;
1512 ei->op.proc_get_link = proc_cwd_link;
1515 case PROC_TGID_ROOT:
1516 inode->i_op = &proc_pid_link_inode_operations;
1517 ei->op.proc_get_link = proc_root_link;
1519 case PROC_TID_ENVIRON:
1520 case PROC_TGID_ENVIRON:
1521 inode->i_fop = &proc_info_file_operations;
1522 ei->op.proc_read = proc_pid_environ;
1525 case PROC_TGID_AUXV:
1526 inode->i_fop = &proc_info_file_operations;
1527 ei->op.proc_read = proc_pid_auxv;
1529 case PROC_TID_STATUS:
1530 case PROC_TGID_STATUS:
1531 inode->i_fop = &proc_info_file_operations;
1532 ei->op.proc_read = proc_pid_status;
1535 inode->i_fop = &proc_info_file_operations;
1536 ei->op.proc_read = proc_tid_stat;
1538 case PROC_TGID_STAT:
1539 inode->i_fop = &proc_info_file_operations;
1540 ei->op.proc_read = proc_tgid_stat;
1542 case PROC_TID_CMDLINE:
1543 case PROC_TGID_CMDLINE:
1544 inode->i_fop = &proc_info_file_operations;
1545 ei->op.proc_read = proc_pid_cmdline;
1547 case PROC_TID_STATM:
1548 case PROC_TGID_STATM:
1549 inode->i_fop = &proc_info_file_operations;
1550 ei->op.proc_read = proc_pid_statm;
1553 case PROC_TGID_MAPS:
1554 inode->i_fop = &proc_maps_operations;
1557 case PROC_TID_NUMA_MAPS:
1558 case PROC_TGID_NUMA_MAPS:
1559 inode->i_fop = &proc_numa_maps_operations;
1564 inode->i_op = &proc_mem_inode_operations;
1565 inode->i_fop = &proc_mem_operations;
1567 #ifdef CONFIG_SECCOMP
1568 case PROC_TID_SECCOMP:
1569 case PROC_TGID_SECCOMP:
1570 inode->i_fop = &proc_seccomp_operations;
1572 #endif /* CONFIG_SECCOMP */
1573 case PROC_TID_MOUNTS:
1574 case PROC_TGID_MOUNTS:
1575 inode->i_fop = &proc_mounts_operations;
1577 #ifdef CONFIG_SECURITY
1580 inode->i_op = &proc_tid_attr_inode_operations;
1581 inode->i_fop = &proc_tid_attr_operations;
1583 case PROC_TGID_ATTR:
1585 inode->i_op = &proc_tgid_attr_inode_operations;
1586 inode->i_fop = &proc_tgid_attr_operations;
1588 case PROC_TID_ATTR_CURRENT:
1589 case PROC_TGID_ATTR_CURRENT:
1590 case PROC_TID_ATTR_PREV:
1591 case PROC_TGID_ATTR_PREV:
1592 case PROC_TID_ATTR_EXEC:
1593 case PROC_TGID_ATTR_EXEC:
1594 case PROC_TID_ATTR_FSCREATE:
1595 case PROC_TGID_ATTR_FSCREATE:
1596 inode->i_fop = &proc_pid_attr_operations;
1599 #ifdef CONFIG_KALLSYMS
1600 case PROC_TID_WCHAN:
1601 case PROC_TGID_WCHAN:
1602 inode->i_fop = &proc_info_file_operations;
1603 ei->op.proc_read = proc_pid_wchan;
1606 #ifdef CONFIG_SCHEDSTATS
1607 case PROC_TID_SCHEDSTAT:
1608 case PROC_TGID_SCHEDSTAT:
1609 inode->i_fop = &proc_info_file_operations;
1610 ei->op.proc_read = proc_pid_schedstat;
1613 #ifdef CONFIG_CPUSETS
1614 case PROC_TID_CPUSET:
1615 case PROC_TGID_CPUSET:
1616 inode->i_fop = &proc_cpuset_operations;
1619 case PROC_TID_OOM_SCORE:
1620 case PROC_TGID_OOM_SCORE:
1621 inode->i_fop = &proc_info_file_operations;
1622 ei->op.proc_read = proc_oom_score;
1624 case PROC_TID_OOM_ADJUST:
1625 case PROC_TGID_OOM_ADJUST:
1626 inode->i_fop = &proc_oom_adjust_operations;
1628 #ifdef CONFIG_AUDITSYSCALL
1629 case PROC_TID_LOGINUID:
1630 case PROC_TGID_LOGINUID:
1631 inode->i_fop = &proc_loginuid_operations;
1635 printk("procfs: impossible type (%d)",p->type);
1637 return ERR_PTR(-EINVAL);
1639 dentry->d_op = &pid_dentry_operations;
1640 d_add(dentry, inode);
1644 return ERR_PTR(error);
1647 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1648 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1651 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1652 return proc_pident_lookup(dir, dentry, tid_base_stuff);
1655 static struct file_operations proc_tgid_base_operations = {
1656 .read = generic_read_dir,
1657 .readdir = proc_tgid_base_readdir,
1660 static struct file_operations proc_tid_base_operations = {
1661 .read = generic_read_dir,
1662 .readdir = proc_tid_base_readdir,
1665 static struct inode_operations proc_tgid_base_inode_operations = {
1666 .lookup = proc_tgid_base_lookup,
1669 static struct inode_operations proc_tid_base_inode_operations = {
1670 .lookup = proc_tid_base_lookup,
1673 #ifdef CONFIG_SECURITY
1674 static int proc_tgid_attr_readdir(struct file * filp,
1675 void * dirent, filldir_t filldir)
1677 return proc_pident_readdir(filp,dirent,filldir,
1678 tgid_attr_stuff,ARRAY_SIZE(tgid_attr_stuff));
1681 static int proc_tid_attr_readdir(struct file * filp,
1682 void * dirent, filldir_t filldir)
1684 return proc_pident_readdir(filp,dirent,filldir,
1685 tid_attr_stuff,ARRAY_SIZE(tid_attr_stuff));
1688 static struct file_operations proc_tgid_attr_operations = {
1689 .read = generic_read_dir,
1690 .readdir = proc_tgid_attr_readdir,
1693 static struct file_operations proc_tid_attr_operations = {
1694 .read = generic_read_dir,
1695 .readdir = proc_tid_attr_readdir,
1698 static struct dentry *proc_tgid_attr_lookup(struct inode *dir,
1699 struct dentry *dentry, struct nameidata *nd)
1701 return proc_pident_lookup(dir, dentry, tgid_attr_stuff);
1704 static struct dentry *proc_tid_attr_lookup(struct inode *dir,
1705 struct dentry *dentry, struct nameidata *nd)
1707 return proc_pident_lookup(dir, dentry, tid_attr_stuff);
1710 static struct inode_operations proc_tgid_attr_inode_operations = {
1711 .lookup = proc_tgid_attr_lookup,
1714 static struct inode_operations proc_tid_attr_inode_operations = {
1715 .lookup = proc_tid_attr_lookup,
1722 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1726 sprintf(tmp, "%d", current->tgid);
1727 return vfs_readlink(dentry,buffer,buflen,tmp);
1730 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1733 sprintf(tmp, "%d", current->tgid);
1734 return ERR_PTR(vfs_follow_link(nd,tmp));
1737 static struct inode_operations proc_self_inode_operations = {
1738 .readlink = proc_self_readlink,
1739 .follow_link = proc_self_follow_link,
1743 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1744 * @p: task that should be flushed.
1746 * Drops the /proc/@pid dcache entry from the hash chains.
1748 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1749 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1750 * if the pid value is immediately reused. This is enforced by
1751 * - caller must acquire spin_lock(p->proc_lock)
1752 * - must be called before detach_pid()
1753 * - proc_pid_lookup acquires proc_lock, and checks that
1754 * the target is not dead by looking at the attach count
1758 struct dentry *proc_pid_unhash(struct task_struct *p)
1760 struct dentry *proc_dentry;
1762 proc_dentry = p->proc_dentry;
1763 if (proc_dentry != NULL) {
1765 spin_lock(&dcache_lock);
1766 spin_lock(&proc_dentry->d_lock);
1767 if (!d_unhashed(proc_dentry)) {
1768 dget_locked(proc_dentry);
1769 __d_drop(proc_dentry);
1770 spin_unlock(&proc_dentry->d_lock);
1772 spin_unlock(&proc_dentry->d_lock);
1775 spin_unlock(&dcache_lock);
1781 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1782 * @proc_dentry: directoy to prune.
1784 * Shrink the /proc directory that was used by the just killed thread.
1787 void proc_pid_flush(struct dentry *proc_dentry)
1790 if(proc_dentry != NULL) {
1791 shrink_dcache_parent(proc_dentry);
1797 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1799 struct task_struct *task;
1800 struct inode *inode;
1801 struct proc_inode *ei;
1805 if (dentry->d_name.len == 4 && !memcmp(dentry->d_name.name,"self",4)) {
1806 inode = new_inode(dir->i_sb);
1808 return ERR_PTR(-ENOMEM);
1810 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1811 inode->i_ino = fake_ino(0, PROC_TGID_INO);
1813 inode->i_mode = S_IFLNK|S_IRWXUGO;
1814 inode->i_uid = inode->i_gid = 0;
1816 inode->i_op = &proc_self_inode_operations;
1817 d_add(dentry, inode);
1820 tgid = name_to_int(dentry);
1824 read_lock(&tasklist_lock);
1825 task = find_task_by_pid(tgid);
1827 get_task_struct(task);
1828 read_unlock(&tasklist_lock);
1832 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TGID_INO);
1836 put_task_struct(task);
1839 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1840 inode->i_op = &proc_tgid_base_inode_operations;
1841 inode->i_fop = &proc_tgid_base_operations;
1842 inode->i_flags|=S_IMMUTABLE;
1843 #ifdef CONFIG_SECURITY
1849 dentry->d_op = &pid_base_dentry_operations;
1852 d_add(dentry, inode);
1853 spin_lock(&task->proc_lock);
1854 task->proc_dentry = dentry;
1855 if (!pid_alive(task)) {
1856 dentry = proc_pid_unhash(task);
1859 spin_unlock(&task->proc_lock);
1861 put_task_struct(task);
1863 proc_pid_flush(dentry);
1868 return ERR_PTR(-ENOENT);
1872 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1874 struct task_struct *task;
1875 struct task_struct *leader = proc_task(dir);
1876 struct inode *inode;
1879 tid = name_to_int(dentry);
1883 read_lock(&tasklist_lock);
1884 task = find_task_by_pid(tid);
1886 get_task_struct(task);
1887 read_unlock(&tasklist_lock);
1890 if (leader->tgid != task->tgid)
1893 inode = proc_pid_make_inode(dir->i_sb, task, PROC_TID_INO);
1898 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1899 inode->i_op = &proc_tid_base_inode_operations;
1900 inode->i_fop = &proc_tid_base_operations;
1901 inode->i_flags|=S_IMMUTABLE;
1902 #ifdef CONFIG_SECURITY
1908 dentry->d_op = &pid_base_dentry_operations;
1910 d_add(dentry, inode);
1912 put_task_struct(task);
1915 put_task_struct(task);
1917 return ERR_PTR(-ENOENT);
1920 #define PROC_NUMBUF 10
1921 #define PROC_MAXPIDS 20
1924 * Get a few tgid's to return for filldir - we need to hold the
1925 * tasklist lock while doing this, and we must release it before
1926 * we actually do the filldir itself, so we use a temp buffer..
1928 static int get_tgid_list(int index, unsigned long version, unsigned int *tgids)
1930 struct task_struct *p;
1934 read_lock(&tasklist_lock);
1937 p = find_task_by_pid(version);
1938 if (p && !thread_group_leader(p))
1945 p = next_task(&init_task);
1947 for ( ; p != &init_task; p = next_task(p)) {
1953 tgids[nr_tgids] = tgid;
1955 if (nr_tgids >= PROC_MAXPIDS)
1958 read_unlock(&tasklist_lock);
1963 * Get a few tid's to return for filldir - we need to hold the
1964 * tasklist lock while doing this, and we must release it before
1965 * we actually do the filldir itself, so we use a temp buffer..
1967 static int get_tid_list(int index, unsigned int *tids, struct inode *dir)
1969 struct task_struct *leader_task = proc_task(dir);
1970 struct task_struct *task = leader_task;
1974 read_lock(&tasklist_lock);
1976 * The starting point task (leader_task) might be an already
1977 * unlinked task, which cannot be used to access the task-list
1978 * via next_thread().
1980 if (pid_alive(task)) do {
1981 int tid = task->pid;
1986 tids[nr_tids] = tid;
1988 if (nr_tids >= PROC_MAXPIDS)
1990 } while ((task = next_thread(task)) != leader_task);
1991 read_unlock(&tasklist_lock);
1995 /* for the /proc/ directory itself, after non-process stuff has been done */
1996 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1998 unsigned int tgid_array[PROC_MAXPIDS];
1999 char buf[PROC_NUMBUF];
2000 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2001 unsigned int nr_tgids, i;
2005 ino_t ino = fake_ino(0,PROC_TGID_INO);
2006 if (filldir(dirent, "self", 4, filp->f_pos, ino, DT_LNK) < 0)
2012 /* f_version caches the tgid value that the last readdir call couldn't
2013 * return. lseek aka telldir automagically resets f_version to 0.
2015 next_tgid = filp->f_version;
2016 filp->f_version = 0;
2018 nr_tgids = get_tgid_list(nr, next_tgid, tgid_array);
2020 /* no more entries ! */
2025 /* do not use the last found pid, reserve it for next_tgid */
2026 if (nr_tgids == PROC_MAXPIDS) {
2028 next_tgid = tgid_array[nr_tgids];
2031 for (i=0;i<nr_tgids;i++) {
2032 int tgid = tgid_array[i];
2033 ino_t ino = fake_ino(tgid,PROC_TGID_INO);
2034 unsigned long j = PROC_NUMBUF;
2037 buf[--j] = '0' + (tgid % 10);
2038 while ((tgid /= 10) != 0);
2040 if (filldir(dirent, buf+j, PROC_NUMBUF-j, filp->f_pos, ino, DT_DIR) < 0) {
2041 /* returning this tgid failed, save it as the first
2042 * pid for the next readir call */
2043 filp->f_version = tgid_array[i];
2054 /* for the /proc/TGID/task/ directories */
2055 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2057 unsigned int tid_array[PROC_MAXPIDS];
2058 char buf[PROC_NUMBUF];
2059 unsigned int nr_tids, i;
2060 struct dentry *dentry = filp->f_dentry;
2061 struct inode *inode = dentry->d_inode;
2062 int retval = -ENOENT;
2064 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2066 if (!pid_alive(proc_task(inode)))
2073 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2078 ino = parent_ino(dentry);
2079 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2085 nr_tids = get_tid_list(pos, tid_array, inode);
2086 inode->i_nlink = pos + nr_tids;
2088 for (i = 0; i < nr_tids; i++) {
2089 unsigned long j = PROC_NUMBUF;
2090 int tid = tid_array[i];
2092 ino = fake_ino(tid,PROC_TID_INO);
2095 buf[--j] = '0' + (tid % 10);
2096 while ((tid /= 10) != 0);
2098 if (filldir(dirent, buf+j, PROC_NUMBUF-j, pos, ino, DT_DIR) < 0)