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.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
74 #include <linux/nsproxy.h>
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
88 /* Worst case buffer size needed for holding an integer. */
89 #define PROC_NUMBUF 10
95 struct inode_operations *iop;
96 struct file_operations *fop;
100 #define NOD(NAME, MODE, IOP, FOP, OP) { \
101 .len = sizeof(NAME) - 1, \
109 #define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 #define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
117 #define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
119 &proc_##OTYPE##_operations, {})
120 #define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
125 static struct fs_struct *get_fs_struct(struct task_struct *task)
127 struct fs_struct *fs;
131 atomic_inc(&fs->count);
136 static int get_nr_threads(struct task_struct *tsk)
138 /* Must be called with the rcu_read_lock held */
142 if (lock_task_sighand(tsk, &flags)) {
143 count = atomic_read(&tsk->signal->count);
144 unlock_task_sighand(tsk, &flags);
149 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
151 struct task_struct *task = get_proc_task(inode);
152 struct fs_struct *fs = NULL;
153 int result = -ENOENT;
156 fs = get_fs_struct(task);
157 put_task_struct(task);
160 read_lock(&fs->lock);
161 *mnt = mntget(fs->pwdmnt);
162 *dentry = dget(fs->pwd);
163 read_unlock(&fs->lock);
170 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
172 struct task_struct *task = get_proc_task(inode);
173 struct fs_struct *fs = NULL;
174 int result = -ENOENT;
177 fs = get_fs_struct(task);
178 put_task_struct(task);
181 read_lock(&fs->lock);
182 *mnt = mntget(fs->rootmnt);
183 *dentry = dget(fs->root);
184 read_unlock(&fs->lock);
191 #define MAY_PTRACE(task) \
192 (task == current || \
193 (task->parent == current && \
194 (task->ptrace & PT_PTRACED) && \
195 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
196 security_ptrace(current,task) == 0))
198 static int proc_pid_environ(struct task_struct *task, char * buffer)
201 struct mm_struct *mm = get_task_mm(task);
203 unsigned int len = mm->env_end - mm->env_start;
206 res = access_process_vm(task, mm->env_start, buffer, len, 0);
207 if (!ptrace_may_attach(task))
214 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
218 struct mm_struct *mm = get_task_mm(task);
222 goto out_mm; /* Shh! No looking before we're done */
224 len = mm->arg_end - mm->arg_start;
229 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
231 // If the nul at the end of args has been overwritten, then
232 // assume application is using setproctitle(3).
233 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
234 len = strnlen(buffer, res);
238 len = mm->env_end - mm->env_start;
239 if (len > PAGE_SIZE - res)
240 len = PAGE_SIZE - res;
241 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
242 res = strnlen(buffer, res);
251 static int proc_pid_auxv(struct task_struct *task, char *buffer)
254 struct mm_struct *mm = get_task_mm(task);
256 unsigned int nwords = 0;
259 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
260 res = nwords * sizeof(mm->saved_auxv[0]);
263 memcpy(buffer, mm->saved_auxv, res);
270 #ifdef CONFIG_KALLSYMS
272 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
273 * Returns the resolved symbol. If that fails, simply return the address.
275 static int proc_pid_wchan(struct task_struct *task, char *buffer)
278 const char *sym_name;
279 unsigned long wchan, size, offset;
280 char namebuf[KSYM_NAME_LEN+1];
282 wchan = get_wchan(task);
284 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
286 return sprintf(buffer, "%s", sym_name);
287 return sprintf(buffer, "%lu", wchan);
289 #endif /* CONFIG_KALLSYMS */
291 #ifdef CONFIG_SCHEDSTATS
293 * Provides /proc/PID/schedstat
295 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
297 return sprintf(buffer, "%lu %lu %lu\n",
298 task->sched_info.cpu_time,
299 task->sched_info.run_delay,
300 task->sched_info.pcnt);
304 /* The badness from the OOM killer */
305 unsigned long badness(struct task_struct *p, unsigned long uptime);
306 static int proc_oom_score(struct task_struct *task, char *buffer)
308 unsigned long points;
309 struct timespec uptime;
311 do_posix_clock_monotonic_gettime(&uptime);
312 points = badness(task, uptime.tv_sec);
313 return sprintf(buffer, "%lu\n", points);
316 /************************************************************************/
317 /* Here the fs part begins */
318 /************************************************************************/
320 /* permission checks */
321 static int proc_fd_access_allowed(struct inode *inode)
323 struct task_struct *task;
325 /* Allow access to a task's file descriptors if it is us or we
326 * may use ptrace attach to the process and find out that
329 task = get_proc_task(inode);
331 allowed = ptrace_may_attach(task);
332 put_task_struct(task);
337 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
340 struct inode *inode = dentry->d_inode;
342 if (attr->ia_valid & ATTR_MODE)
345 error = inode_change_ok(inode, attr);
347 error = security_inode_setattr(dentry, attr);
349 error = inode_setattr(inode, attr);
354 static struct inode_operations proc_def_inode_operations = {
355 .setattr = proc_setattr,
358 extern struct seq_operations mounts_op;
364 static int mounts_open(struct inode *inode, struct file *file)
366 struct task_struct *task = get_proc_task(inode);
367 struct namespace *namespace = NULL;
368 struct proc_mounts *p;
373 namespace = task->nsproxy->namespace;
375 get_namespace(namespace);
377 put_task_struct(task);
382 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
384 file->private_data = &p->m;
385 ret = seq_open(file, &mounts_op);
387 p->m.private = namespace;
388 p->event = namespace->event;
393 put_namespace(namespace);
398 static int mounts_release(struct inode *inode, struct file *file)
400 struct seq_file *m = file->private_data;
401 struct namespace *namespace = m->private;
402 put_namespace(namespace);
403 return seq_release(inode, file);
406 static unsigned mounts_poll(struct file *file, poll_table *wait)
408 struct proc_mounts *p = file->private_data;
409 struct namespace *ns = p->m.private;
412 poll_wait(file, &ns->poll, wait);
414 spin_lock(&vfsmount_lock);
415 if (p->event != ns->event) {
416 p->event = ns->event;
419 spin_unlock(&vfsmount_lock);
424 static struct file_operations proc_mounts_operations = {
428 .release = mounts_release,
432 extern struct seq_operations mountstats_op;
433 static int mountstats_open(struct inode *inode, struct file *file)
435 int ret = seq_open(file, &mountstats_op);
438 struct seq_file *m = file->private_data;
439 struct namespace *namespace = NULL;
440 struct task_struct *task = get_proc_task(inode);
444 namespace = task->nsproxy->namespace;
446 get_namespace(namespace);
448 put_task_struct(task);
452 m->private = namespace;
454 seq_release(inode, file);
461 static struct file_operations proc_mountstats_operations = {
462 .open = mountstats_open,
465 .release = mounts_release,
468 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
470 static ssize_t proc_info_read(struct file * file, char __user * buf,
471 size_t count, loff_t *ppos)
473 struct inode * inode = file->f_dentry->d_inode;
476 struct task_struct *task = get_proc_task(inode);
482 if (count > PROC_BLOCK_SIZE)
483 count = PROC_BLOCK_SIZE;
486 if (!(page = __get_free_page(GFP_KERNEL)))
489 length = PROC_I(inode)->op.proc_read(task, (char*)page);
492 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
495 put_task_struct(task);
500 static struct file_operations proc_info_file_operations = {
501 .read = proc_info_read,
504 static int mem_open(struct inode* inode, struct file* file)
506 file->private_data = (void*)((long)current->self_exec_id);
510 static ssize_t mem_read(struct file * file, char __user * buf,
511 size_t count, loff_t *ppos)
513 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
515 unsigned long src = *ppos;
517 struct mm_struct *mm;
522 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
526 page = (char *)__get_free_page(GFP_USER);
532 mm = get_task_mm(task);
538 if (file->private_data != (void*)((long)current->self_exec_id))
544 int this_len, retval;
546 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
547 retval = access_process_vm(task, src, page, this_len, 0);
548 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
554 if (copy_to_user(buf, page, retval)) {
569 free_page((unsigned long) page);
571 put_task_struct(task);
576 #define mem_write NULL
579 /* This is a security hazard */
580 static ssize_t mem_write(struct file * file, const char * buf,
581 size_t count, loff_t *ppos)
585 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
586 unsigned long dst = *ppos;
592 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
596 page = (char *)__get_free_page(GFP_USER);
602 int this_len, retval;
604 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
605 if (copy_from_user(page, buf, this_len)) {
609 retval = access_process_vm(task, dst, page, this_len, 1);
621 free_page((unsigned long) page);
623 put_task_struct(task);
629 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
633 file->f_pos = offset;
636 file->f_pos += offset;
641 force_successful_syscall_return();
645 static struct file_operations proc_mem_operations = {
652 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
653 size_t count, loff_t *ppos)
655 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
656 char buffer[PROC_NUMBUF];
659 loff_t __ppos = *ppos;
663 oom_adjust = task->oomkilladj;
664 put_task_struct(task);
666 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
669 if (count > len-__ppos)
671 if (copy_to_user(buf, buffer + __ppos, count))
673 *ppos = __ppos + count;
677 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
678 size_t count, loff_t *ppos)
680 struct task_struct *task;
681 char buffer[PROC_NUMBUF], *end;
684 if (!capable(CAP_SYS_RESOURCE))
686 memset(buffer, 0, sizeof(buffer));
687 if (count > sizeof(buffer) - 1)
688 count = sizeof(buffer) - 1;
689 if (copy_from_user(buffer, buf, count))
691 oom_adjust = simple_strtol(buffer, &end, 0);
692 if ((oom_adjust < -16 || oom_adjust > 15) && oom_adjust != OOM_DISABLE)
696 task = get_proc_task(file->f_dentry->d_inode);
699 task->oomkilladj = oom_adjust;
700 put_task_struct(task);
701 if (end - buffer == 0)
706 static struct file_operations proc_oom_adjust_operations = {
707 .read = oom_adjust_read,
708 .write = oom_adjust_write,
711 #ifdef CONFIG_AUDITSYSCALL
713 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
714 size_t count, loff_t *ppos)
716 struct inode * inode = file->f_dentry->d_inode;
717 struct task_struct *task = get_proc_task(inode);
719 char tmpbuf[TMPBUFLEN];
723 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
724 audit_get_loginuid(task->audit_context));
725 put_task_struct(task);
726 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
729 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
730 size_t count, loff_t *ppos)
732 struct inode * inode = file->f_dentry->d_inode;
737 if (!capable(CAP_AUDIT_CONTROL))
740 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
743 if (count >= PAGE_SIZE)
744 count = PAGE_SIZE - 1;
747 /* No partial writes. */
750 page = (char*)__get_free_page(GFP_USER);
754 if (copy_from_user(page, buf, count))
758 loginuid = simple_strtoul(page, &tmp, 10);
764 length = audit_set_loginuid(current, loginuid);
765 if (likely(length == 0))
769 free_page((unsigned long) page);
773 static struct file_operations proc_loginuid_operations = {
774 .read = proc_loginuid_read,
775 .write = proc_loginuid_write,
779 #ifdef CONFIG_SECCOMP
780 static ssize_t seccomp_read(struct file *file, char __user *buf,
781 size_t count, loff_t *ppos)
783 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
785 loff_t __ppos = *ppos;
790 /* no need to print the trailing zero, so use only len */
791 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
792 put_task_struct(tsk);
795 if (count > len - __ppos)
796 count = len - __ppos;
797 if (copy_to_user(buf, __buf + __ppos, count))
799 *ppos = __ppos + count;
803 static ssize_t seccomp_write(struct file *file, const char __user *buf,
804 size_t count, loff_t *ppos)
806 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
807 char __buf[20], *end;
808 unsigned int seccomp_mode;
815 /* can set it only once to be even more secure */
817 if (unlikely(tsk->seccomp.mode))
821 memset(__buf, 0, sizeof(__buf));
822 count = min(count, sizeof(__buf) - 1);
823 if (copy_from_user(__buf, buf, count))
826 seccomp_mode = simple_strtoul(__buf, &end, 0);
830 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
831 tsk->seccomp.mode = seccomp_mode;
832 set_tsk_thread_flag(tsk, TIF_SECCOMP);
836 if (unlikely(!(end - __buf)))
838 result = end - __buf;
840 put_task_struct(tsk);
845 static struct file_operations proc_seccomp_operations = {
846 .read = seccomp_read,
847 .write = seccomp_write,
849 #endif /* CONFIG_SECCOMP */
851 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
853 struct inode *inode = dentry->d_inode;
856 /* We don't need a base pointer in the /proc filesystem */
859 /* Are we allowed to snoop on the tasks file descriptors? */
860 if (!proc_fd_access_allowed(inode))
863 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
864 nd->last_type = LAST_BIND;
866 return ERR_PTR(error);
869 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
870 char __user *buffer, int buflen)
872 struct inode * inode;
873 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
879 inode = dentry->d_inode;
880 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
884 len = tmp + PAGE_SIZE - 1 - path;
888 if (copy_to_user(buffer, path, len))
891 free_page((unsigned long)tmp);
895 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
898 struct inode *inode = dentry->d_inode;
900 struct vfsmount *mnt = NULL;
902 /* Are we allowed to snoop on the tasks file descriptors? */
903 if (!proc_fd_access_allowed(inode))
906 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
910 error = do_proc_readlink(de, mnt, buffer, buflen);
917 static struct inode_operations proc_pid_link_inode_operations = {
918 .readlink = proc_pid_readlink,
919 .follow_link = proc_pid_follow_link,
920 .setattr = proc_setattr,
924 /* building an inode */
926 static int task_dumpable(struct task_struct *task)
929 struct mm_struct *mm;
934 dumpable = mm->dumpable;
942 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
944 struct inode * inode;
945 struct proc_inode *ei;
947 /* We need a new inode */
949 inode = new_inode(sb);
955 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
956 inode->i_op = &proc_def_inode_operations;
959 * grab the reference to task.
961 ei->pid = get_pid(task->pids[PIDTYPE_PID].pid);
967 if (task_dumpable(task)) {
968 inode->i_uid = task->euid;
969 inode->i_gid = task->egid;
971 security_task_to_inode(task, inode);
981 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
983 struct inode *inode = dentry->d_inode;
984 struct task_struct *task;
985 generic_fillattr(inode, stat);
990 task = pid_task(proc_pid(inode), PIDTYPE_PID);
992 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
993 task_dumpable(task)) {
994 stat->uid = task->euid;
995 stat->gid = task->egid;
1005 * Exceptional case: normally we are not allowed to unhash a busy
1006 * directory. In this case, however, we can do it - no aliasing problems
1007 * due to the way we treat inodes.
1009 * Rewrite the inode's ownerships here because the owning task may have
1010 * performed a setuid(), etc.
1012 * Before the /proc/pid/status file was created the only way to read
1013 * the effective uid of a /process was to stat /proc/pid. Reading
1014 * /proc/pid/status is slow enough that procps and other packages
1015 * kept stating /proc/pid. To keep the rules in /proc simple I have
1016 * made this apply to all per process world readable and executable
1019 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1021 struct inode *inode = dentry->d_inode;
1022 struct task_struct *task = get_proc_task(inode);
1024 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1025 task_dumpable(task)) {
1026 inode->i_uid = task->euid;
1027 inode->i_gid = task->egid;
1032 inode->i_mode &= ~(S_ISUID | S_ISGID);
1033 security_task_to_inode(task, inode);
1034 put_task_struct(task);
1041 static int pid_delete_dentry(struct dentry * dentry)
1043 /* Is the task we represent dead?
1044 * If so, then don't put the dentry on the lru list,
1045 * kill it immediately.
1047 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1050 static struct dentry_operations pid_dentry_operations =
1052 .d_revalidate = pid_revalidate,
1053 .d_delete = pid_delete_dentry,
1058 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1060 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1061 char *name, int len,
1062 instantiate_t instantiate, struct task_struct *task, void *ptr)
1064 struct dentry *child, *dir = filp->f_dentry;
1065 struct inode *inode;
1068 unsigned type = DT_UNKNOWN;
1072 qname.hash = full_name_hash(name, len);
1074 child = d_lookup(dir, &qname);
1077 new = d_alloc(dir, &qname);
1079 child = instantiate(dir->d_inode, new, task, ptr);
1086 if (!child || IS_ERR(child) || !child->d_inode)
1087 goto end_instantiate;
1088 inode = child->d_inode;
1091 type = inode->i_mode >> 12;
1096 ino = find_inode_number(dir, &qname);
1099 return filldir(dirent, name, len, filp->f_pos, ino, type);
1102 static unsigned name_to_int(struct dentry *dentry)
1104 const char *name = dentry->d_name.name;
1105 int len = dentry->d_name.len;
1108 if (len > 1 && *name == '0')
1111 unsigned c = *name++ - '0';
1114 if (n >= (~0U-9)/10)
1124 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1126 struct task_struct *task = get_proc_task(inode);
1127 struct files_struct *files = NULL;
1129 int fd = proc_fd(inode);
1132 files = get_files_struct(task);
1133 put_task_struct(task);
1137 * We are not taking a ref to the file structure, so we must
1140 spin_lock(&files->file_lock);
1141 file = fcheck_files(files, fd);
1143 *mnt = mntget(file->f_vfsmnt);
1144 *dentry = dget(file->f_dentry);
1145 spin_unlock(&files->file_lock);
1146 put_files_struct(files);
1149 spin_unlock(&files->file_lock);
1150 put_files_struct(files);
1155 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1157 struct inode *inode = dentry->d_inode;
1158 struct task_struct *task = get_proc_task(inode);
1159 int fd = proc_fd(inode);
1160 struct files_struct *files;
1163 files = get_files_struct(task);
1166 if (fcheck_files(files, fd)) {
1168 put_files_struct(files);
1169 if (task_dumpable(task)) {
1170 inode->i_uid = task->euid;
1171 inode->i_gid = task->egid;
1176 inode->i_mode &= ~(S_ISUID | S_ISGID);
1177 security_task_to_inode(task, inode);
1178 put_task_struct(task);
1182 put_files_struct(files);
1184 put_task_struct(task);
1190 static struct dentry_operations tid_fd_dentry_operations =
1192 .d_revalidate = tid_fd_revalidate,
1193 .d_delete = pid_delete_dentry,
1196 static struct dentry *proc_fd_instantiate(struct inode *dir,
1197 struct dentry *dentry, struct task_struct *task, void *ptr)
1199 unsigned fd = *(unsigned *)ptr;
1201 struct files_struct *files;
1202 struct inode *inode;
1203 struct proc_inode *ei;
1204 struct dentry *error = ERR_PTR(-ENOENT);
1206 inode = proc_pid_make_inode(dir->i_sb, task);
1211 files = get_files_struct(task);
1214 inode->i_mode = S_IFLNK;
1217 * We are not taking a ref to the file structure, so we must
1220 spin_lock(&files->file_lock);
1221 file = fcheck_files(files, fd);
1224 if (file->f_mode & 1)
1225 inode->i_mode |= S_IRUSR | S_IXUSR;
1226 if (file->f_mode & 2)
1227 inode->i_mode |= S_IWUSR | S_IXUSR;
1228 spin_unlock(&files->file_lock);
1229 put_files_struct(files);
1231 inode->i_op = &proc_pid_link_inode_operations;
1233 ei->op.proc_get_link = proc_fd_link;
1234 dentry->d_op = &tid_fd_dentry_operations;
1235 d_add(dentry, inode);
1236 /* Close the race of the process dying before we return the dentry */
1237 if (tid_fd_revalidate(dentry, NULL))
1243 spin_unlock(&files->file_lock);
1244 put_files_struct(files);
1251 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1253 struct task_struct *task = get_proc_task(dir);
1254 unsigned fd = name_to_int(dentry);
1255 struct dentry *result = ERR_PTR(-ENOENT);
1262 result = proc_fd_instantiate(dir, dentry, task, &fd);
1264 put_task_struct(task);
1269 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1270 struct task_struct *task, int fd)
1272 char name[PROC_NUMBUF];
1273 int len = snprintf(name, sizeof(name), "%d", fd);
1274 return proc_fill_cache(filp, dirent, filldir, name, len,
1275 proc_fd_instantiate, task, &fd);
1278 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1280 struct dentry *dentry = filp->f_dentry;
1281 struct inode *inode = dentry->d_inode;
1282 struct task_struct *p = get_proc_task(inode);
1283 unsigned int fd, tid, ino;
1285 struct files_struct * files;
1286 struct fdtable *fdt;
1297 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1301 ino = parent_ino(dentry);
1302 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1306 files = get_files_struct(p);
1310 fdt = files_fdtable(files);
1311 for (fd = filp->f_pos-2;
1313 fd++, filp->f_pos++) {
1315 if (!fcheck_files(files, fd))
1319 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1326 put_files_struct(files);
1334 static struct file_operations proc_fd_operations = {
1335 .read = generic_read_dir,
1336 .readdir = proc_readfd,
1340 * proc directories can do almost nothing..
1342 static struct inode_operations proc_fd_inode_operations = {
1343 .lookup = proc_lookupfd,
1344 .setattr = proc_setattr,
1347 static struct dentry *proc_pident_instantiate(struct inode *dir,
1348 struct dentry *dentry, struct task_struct *task, void *ptr)
1350 struct pid_entry *p = ptr;
1351 struct inode *inode;
1352 struct proc_inode *ei;
1353 struct dentry *error = ERR_PTR(-EINVAL);
1355 inode = proc_pid_make_inode(dir->i_sb, task);
1360 inode->i_mode = p->mode;
1361 if (S_ISDIR(inode->i_mode))
1362 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1364 inode->i_op = p->iop;
1366 inode->i_fop = p->fop;
1368 dentry->d_op = &pid_dentry_operations;
1369 d_add(dentry, inode);
1370 /* Close the race of the process dying before we return the dentry */
1371 if (pid_revalidate(dentry, NULL))
1378 static struct dentry *proc_pident_lookup(struct inode *dir,
1379 struct dentry *dentry,
1380 struct pid_entry *ents)
1382 struct inode *inode;
1383 struct dentry *error;
1384 struct task_struct *task = get_proc_task(dir);
1385 struct pid_entry *p;
1387 error = ERR_PTR(-ENOENT);
1394 * Yes, it does not scale. And it should not. Don't add
1395 * new entries into /proc/<tgid>/ without very good reasons.
1397 for (p = ents; p->name; p++) {
1398 if (p->len != dentry->d_name.len)
1400 if (!memcmp(dentry->d_name.name, p->name, p->len))
1406 error = proc_pident_instantiate(dir, dentry, task, p);
1408 put_task_struct(task);
1413 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1414 struct task_struct *task, struct pid_entry *p)
1416 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1417 proc_pident_instantiate, task, p);
1420 static int proc_pident_readdir(struct file *filp,
1421 void *dirent, filldir_t filldir,
1422 struct pid_entry *ents, unsigned int nents)
1426 struct dentry *dentry = filp->f_dentry;
1427 struct inode *inode = dentry->d_inode;
1428 struct task_struct *task = get_proc_task(inode);
1429 struct pid_entry *p;
1443 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1449 ino = parent_ino(dentry);
1450 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1463 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1472 put_task_struct(task);
1477 #ifdef CONFIG_SECURITY
1478 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1479 size_t count, loff_t *ppos)
1481 struct inode * inode = file->f_dentry->d_inode;
1484 struct task_struct *task = get_proc_task(inode);
1490 if (count > PAGE_SIZE)
1493 if (!(page = __get_free_page(GFP_KERNEL)))
1496 length = security_getprocattr(task,
1497 (char*)file->f_dentry->d_name.name,
1498 (void*)page, count);
1500 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1503 put_task_struct(task);
1508 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1509 size_t count, loff_t *ppos)
1511 struct inode * inode = file->f_dentry->d_inode;
1514 struct task_struct *task = get_proc_task(inode);
1519 if (count > PAGE_SIZE)
1522 /* No partial writes. */
1528 page = (char*)__get_free_page(GFP_USER);
1533 if (copy_from_user(page, buf, count))
1536 length = security_setprocattr(task,
1537 (char*)file->f_dentry->d_name.name,
1538 (void*)page, count);
1540 free_page((unsigned long) page);
1542 put_task_struct(task);
1547 static struct file_operations proc_pid_attr_operations = {
1548 .read = proc_pid_attr_read,
1549 .write = proc_pid_attr_write,
1552 static struct pid_entry attr_dir_stuff[] = {
1553 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1554 REG("prev", S_IRUGO, pid_attr),
1555 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1556 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1557 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1558 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1562 static int proc_attr_dir_readdir(struct file * filp,
1563 void * dirent, filldir_t filldir)
1565 return proc_pident_readdir(filp,dirent,filldir,
1566 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1569 static struct file_operations proc_attr_dir_operations = {
1570 .read = generic_read_dir,
1571 .readdir = proc_attr_dir_readdir,
1574 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1575 struct dentry *dentry, struct nameidata *nd)
1577 return proc_pident_lookup(dir, dentry, attr_dir_stuff);
1580 static struct inode_operations proc_attr_dir_inode_operations = {
1581 .lookup = proc_attr_dir_lookup,
1582 .getattr = pid_getattr,
1583 .setattr = proc_setattr,
1591 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1594 char tmp[PROC_NUMBUF];
1595 sprintf(tmp, "%d", current->tgid);
1596 return vfs_readlink(dentry,buffer,buflen,tmp);
1599 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1601 char tmp[PROC_NUMBUF];
1602 sprintf(tmp, "%d", current->tgid);
1603 return ERR_PTR(vfs_follow_link(nd,tmp));
1606 static struct inode_operations proc_self_inode_operations = {
1607 .readlink = proc_self_readlink,
1608 .follow_link = proc_self_follow_link,
1614 * These are the directory entries in the root directory of /proc
1615 * that properly belong to the /proc filesystem, as they describe
1616 * describe something that is process related.
1618 static struct pid_entry proc_base_stuff[] = {
1619 NOD("self", S_IFLNK|S_IRWXUGO,
1620 &proc_self_inode_operations, NULL, {}),
1625 * Exceptional case: normally we are not allowed to unhash a busy
1626 * directory. In this case, however, we can do it - no aliasing problems
1627 * due to the way we treat inodes.
1629 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1631 struct inode *inode = dentry->d_inode;
1632 struct task_struct *task = get_proc_task(inode);
1634 put_task_struct(task);
1641 static struct dentry_operations proc_base_dentry_operations =
1643 .d_revalidate = proc_base_revalidate,
1644 .d_delete = pid_delete_dentry,
1647 static struct dentry *proc_base_instantiate(struct inode *dir,
1648 struct dentry *dentry, struct task_struct *task, void *ptr)
1650 struct pid_entry *p = ptr;
1651 struct inode *inode;
1652 struct proc_inode *ei;
1653 struct dentry *error = ERR_PTR(-EINVAL);
1655 /* Allocate the inode */
1656 error = ERR_PTR(-ENOMEM);
1657 inode = new_inode(dir->i_sb);
1661 /* Initialize the inode */
1663 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1666 * grab the reference to the task.
1668 ei->pid = get_pid(task_pid(task));
1674 inode->i_mode = p->mode;
1675 if (S_ISDIR(inode->i_mode))
1677 if (S_ISLNK(inode->i_mode))
1680 inode->i_op = p->iop;
1682 inode->i_fop = p->fop;
1684 dentry->d_op = &proc_base_dentry_operations;
1685 d_add(dentry, inode);
1694 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1696 struct dentry *error;
1697 struct task_struct *task = get_proc_task(dir);
1698 struct pid_entry *p;
1700 error = ERR_PTR(-ENOENT);
1705 /* Lookup the directory entry */
1706 for (p = proc_base_stuff; p->name; p++) {
1707 if (p->len != dentry->d_name.len)
1709 if (!memcmp(dentry->d_name.name, p->name, p->len))
1715 error = proc_base_instantiate(dir, dentry, task, p);
1718 put_task_struct(task);
1723 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1724 struct task_struct *task, struct pid_entry *p)
1726 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1727 proc_base_instantiate, task, p);
1733 static struct file_operations proc_task_operations;
1734 static struct inode_operations proc_task_inode_operations;
1736 static struct pid_entry tgid_base_stuff[] = {
1737 DIR("task", S_IRUGO|S_IXUGO, task),
1738 DIR("fd", S_IRUSR|S_IXUSR, fd),
1739 INF("environ", S_IRUSR, pid_environ),
1740 INF("auxv", S_IRUSR, pid_auxv),
1741 INF("status", S_IRUGO, pid_status),
1742 INF("cmdline", S_IRUGO, pid_cmdline),
1743 INF("stat", S_IRUGO, tgid_stat),
1744 INF("statm", S_IRUGO, pid_statm),
1745 REG("maps", S_IRUGO, maps),
1747 REG("numa_maps", S_IRUGO, numa_maps),
1749 REG("mem", S_IRUSR|S_IWUSR, mem),
1750 #ifdef CONFIG_SECCOMP
1751 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1756 REG("mounts", S_IRUGO, mounts),
1757 REG("mountstats", S_IRUSR, mountstats),
1759 REG("smaps", S_IRUGO, smaps),
1761 #ifdef CONFIG_SECURITY
1762 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1764 #ifdef CONFIG_KALLSYMS
1765 INF("wchan", S_IRUGO, pid_wchan),
1767 #ifdef CONFIG_SCHEDSTATS
1768 INF("schedstat", S_IRUGO, pid_schedstat),
1770 #ifdef CONFIG_CPUSETS
1771 REG("cpuset", S_IRUGO, cpuset),
1773 INF("oom_score", S_IRUGO, oom_score),
1774 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1775 #ifdef CONFIG_AUDITSYSCALL
1776 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
1781 static int proc_tgid_base_readdir(struct file * filp,
1782 void * dirent, filldir_t filldir)
1784 return proc_pident_readdir(filp,dirent,filldir,
1785 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1788 static struct file_operations proc_tgid_base_operations = {
1789 .read = generic_read_dir,
1790 .readdir = proc_tgid_base_readdir,
1793 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1794 return proc_pident_lookup(dir, dentry, tgid_base_stuff);
1797 static struct inode_operations proc_tgid_base_inode_operations = {
1798 .lookup = proc_tgid_base_lookup,
1799 .getattr = pid_getattr,
1800 .setattr = proc_setattr,
1804 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1806 * @task: task that should be flushed.
1808 * Looks in the dcache for
1810 * /proc/@tgid/task/@pid
1811 * if either directory is present flushes it and all of it'ts children
1814 * It is safe and reasonable to cache /proc entries for a task until
1815 * that task exits. After that they just clog up the dcache with
1816 * useless entries, possibly causing useful dcache entries to be
1817 * flushed instead. This routine is proved to flush those useless
1818 * dcache entries at process exit time.
1820 * NOTE: This routine is just an optimization so it does not guarantee
1821 * that no dcache entries will exist at process exit time it
1822 * just makes it very unlikely that any will persist.
1824 void proc_flush_task(struct task_struct *task)
1826 struct dentry *dentry, *leader, *dir;
1827 char buf[PROC_NUMBUF];
1831 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1832 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1834 shrink_dcache_parent(dentry);
1839 if (thread_group_leader(task))
1843 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1844 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1849 name.len = strlen(name.name);
1850 dir = d_hash_and_lookup(leader, &name);
1852 goto out_put_leader;
1855 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1856 dentry = d_hash_and_lookup(dir, &name);
1858 shrink_dcache_parent(dentry);
1870 struct dentry *proc_pid_instantiate(struct inode *dir,
1871 struct dentry * dentry, struct task_struct *task, void *ptr)
1873 struct dentry *error = ERR_PTR(-ENOENT);
1874 struct inode *inode;
1876 inode = proc_pid_make_inode(dir->i_sb, task);
1880 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1881 inode->i_op = &proc_tgid_base_inode_operations;
1882 inode->i_fop = &proc_tgid_base_operations;
1883 inode->i_flags|=S_IMMUTABLE;
1885 #ifdef CONFIG_SECURITY
1886 inode->i_nlink += 1;
1889 dentry->d_op = &pid_dentry_operations;
1891 d_add(dentry, inode);
1892 /* Close the race of the process dying before we return the dentry */
1893 if (pid_revalidate(dentry, NULL))
1900 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1902 struct dentry *result = ERR_PTR(-ENOENT);
1903 struct task_struct *task;
1906 result = proc_base_lookup(dir, dentry);
1907 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1910 tgid = name_to_int(dentry);
1915 task = find_task_by_pid(tgid);
1917 get_task_struct(task);
1922 result = proc_pid_instantiate(dir, dentry, task, NULL);
1923 put_task_struct(task);
1929 * Find the first task with tgid >= tgid
1932 static struct task_struct *next_tgid(unsigned int tgid)
1934 struct task_struct *task;
1940 pid = find_ge_pid(tgid);
1943 task = pid_task(pid, PIDTYPE_PID);
1944 /* What we to know is if the pid we have find is the
1945 * pid of a thread_group_leader. Testing for task
1946 * being a thread_group_leader is the obvious thing
1947 * todo but there is a window when it fails, due to
1948 * the pid transfer logic in de_thread.
1950 * So we perform the straight forward test of seeing
1951 * if the pid we have found is the pid of a thread
1952 * group leader, and don't worry if the task we have
1953 * found doesn't happen to be a thread group leader.
1954 * As we don't care in the case of readdir.
1956 if (!task || !has_group_leader_pid(task))
1958 get_task_struct(task);
1964 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + (1 /* /proc/self */))
1966 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1967 struct task_struct *task, int tgid)
1969 char name[PROC_NUMBUF];
1970 int len = snprintf(name, sizeof(name), "%d", tgid);
1971 return proc_fill_cache(filp, dirent, filldir, name, len,
1972 proc_pid_instantiate, task, NULL);
1975 /* for the /proc/ directory itself, after non-process stuff has been done */
1976 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1978 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1979 struct task_struct *reaper = get_proc_task(filp->f_dentry->d_inode);
1980 struct task_struct *task;
1986 for (; nr < (ARRAY_SIZE(proc_base_stuff) - 1); filp->f_pos++, nr++) {
1987 struct pid_entry *p = &proc_base_stuff[nr];
1988 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
1992 tgid = filp->f_pos - TGID_OFFSET;
1993 for (task = next_tgid(tgid);
1995 put_task_struct(task), task = next_tgid(tgid + 1)) {
1997 filp->f_pos = tgid + TGID_OFFSET;
1998 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
1999 put_task_struct(task);
2003 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2005 put_task_struct(reaper);
2013 static struct pid_entry tid_base_stuff[] = {
2014 DIR("fd", S_IRUSR|S_IXUSR, fd),
2015 INF("environ", S_IRUSR, pid_environ),
2016 INF("auxv", S_IRUSR, pid_auxv),
2017 INF("status", S_IRUGO, pid_status),
2018 INF("cmdline", S_IRUGO, pid_cmdline),
2019 INF("stat", S_IRUGO, tid_stat),
2020 INF("statm", S_IRUGO, pid_statm),
2021 REG("maps", S_IRUGO, maps),
2023 REG("numa_maps", S_IRUGO, numa_maps),
2025 REG("mem", S_IRUSR|S_IWUSR, mem),
2026 #ifdef CONFIG_SECCOMP
2027 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2032 REG("mounts", S_IRUGO, mounts),
2034 REG("smaps", S_IRUGO, smaps),
2036 #ifdef CONFIG_SECURITY
2037 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2039 #ifdef CONFIG_KALLSYMS
2040 INF("wchan", S_IRUGO, pid_wchan),
2042 #ifdef CONFIG_SCHEDSTATS
2043 INF("schedstat", S_IRUGO, pid_schedstat),
2045 #ifdef CONFIG_CPUSETS
2046 REG("cpuset", S_IRUGO, cpuset),
2048 INF("oom_score", S_IRUGO, oom_score),
2049 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2050 #ifdef CONFIG_AUDITSYSCALL
2051 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2056 static int proc_tid_base_readdir(struct file * filp,
2057 void * dirent, filldir_t filldir)
2059 return proc_pident_readdir(filp,dirent,filldir,
2060 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2063 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2064 return proc_pident_lookup(dir, dentry, tid_base_stuff);
2067 static struct file_operations proc_tid_base_operations = {
2068 .read = generic_read_dir,
2069 .readdir = proc_tid_base_readdir,
2072 static struct inode_operations proc_tid_base_inode_operations = {
2073 .lookup = proc_tid_base_lookup,
2074 .getattr = pid_getattr,
2075 .setattr = proc_setattr,
2078 static struct dentry *proc_task_instantiate(struct inode *dir,
2079 struct dentry *dentry, struct task_struct *task, void *ptr)
2081 struct dentry *error = ERR_PTR(-ENOENT);
2082 struct inode *inode;
2083 inode = proc_pid_make_inode(dir->i_sb, task);
2087 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2088 inode->i_op = &proc_tid_base_inode_operations;
2089 inode->i_fop = &proc_tid_base_operations;
2090 inode->i_flags|=S_IMMUTABLE;
2092 #ifdef CONFIG_SECURITY
2093 inode->i_nlink += 1;
2096 dentry->d_op = &pid_dentry_operations;
2098 d_add(dentry, inode);
2099 /* Close the race of the process dying before we return the dentry */
2100 if (pid_revalidate(dentry, NULL))
2107 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2109 struct dentry *result = ERR_PTR(-ENOENT);
2110 struct task_struct *task;
2111 struct task_struct *leader = get_proc_task(dir);
2117 tid = name_to_int(dentry);
2122 task = find_task_by_pid(tid);
2124 get_task_struct(task);
2128 if (leader->tgid != task->tgid)
2131 result = proc_task_instantiate(dir, dentry, task, NULL);
2133 put_task_struct(task);
2135 put_task_struct(leader);
2141 * Find the first tid of a thread group to return to user space.
2143 * Usually this is just the thread group leader, but if the users
2144 * buffer was too small or there was a seek into the middle of the
2145 * directory we have more work todo.
2147 * In the case of a short read we start with find_task_by_pid.
2149 * In the case of a seek we start with the leader and walk nr
2152 static struct task_struct *first_tid(struct task_struct *leader,
2155 struct task_struct *pos;
2158 /* Attempt to start with the pid of a thread */
2159 if (tid && (nr > 0)) {
2160 pos = find_task_by_pid(tid);
2161 if (pos && (pos->group_leader == leader))
2165 /* If nr exceeds the number of threads there is nothing todo */
2167 if (nr && nr >= get_nr_threads(leader))
2170 /* If we haven't found our starting place yet start
2171 * with the leader and walk nr threads forward.
2173 for (pos = leader; nr > 0; --nr) {
2174 pos = next_thread(pos);
2175 if (pos == leader) {
2181 get_task_struct(pos);
2188 * Find the next thread in the thread list.
2189 * Return NULL if there is an error or no next thread.
2191 * The reference to the input task_struct is released.
2193 static struct task_struct *next_tid(struct task_struct *start)
2195 struct task_struct *pos = NULL;
2197 if (pid_alive(start)) {
2198 pos = next_thread(start);
2199 if (thread_group_leader(pos))
2202 get_task_struct(pos);
2205 put_task_struct(start);
2209 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2210 struct task_struct *task, int tid)
2212 char name[PROC_NUMBUF];
2213 int len = snprintf(name, sizeof(name), "%d", tid);
2214 return proc_fill_cache(filp, dirent, filldir, name, len,
2215 proc_task_instantiate, task, NULL);
2218 /* for the /proc/TGID/task/ directories */
2219 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2221 struct dentry *dentry = filp->f_dentry;
2222 struct inode *inode = dentry->d_inode;
2223 struct task_struct *leader = get_proc_task(inode);
2224 struct task_struct *task;
2225 int retval = -ENOENT;
2228 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2237 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2242 ino = parent_ino(dentry);
2243 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2249 /* f_version caches the tgid value that the last readdir call couldn't
2250 * return. lseek aka telldir automagically resets f_version to 0.
2252 tid = filp->f_version;
2253 filp->f_version = 0;
2254 for (task = first_tid(leader, tid, pos - 2);
2256 task = next_tid(task), pos++) {
2258 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2259 /* returning this tgid failed, save it as the first
2260 * pid for the next readir call */
2261 filp->f_version = tid;
2262 put_task_struct(task);
2268 put_task_struct(leader);
2273 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2275 struct inode *inode = dentry->d_inode;
2276 struct task_struct *p = get_proc_task(inode);
2277 generic_fillattr(inode, stat);
2281 stat->nlink += get_nr_threads(p);
2289 static struct inode_operations proc_task_inode_operations = {
2290 .lookup = proc_task_lookup,
2291 .getattr = proc_task_getattr,
2292 .setattr = proc_setattr,
2295 static struct file_operations proc_task_operations = {
2296 .read = generic_read_dir,
2297 .readdir = proc_task_readdir,