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>
75 #include <linux/oom.h>
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
89 /* Worst case buffer size needed for holding an integer. */
90 #define PROC_NUMBUF 13
96 struct inode_operations *iop;
97 struct file_operations *fop;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
102 .len = sizeof(NAME) - 1, \
110 #define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 #define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
118 #define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
120 &proc_##OTYPE##_operations, {})
121 #define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
126 static struct fs_struct *get_fs_struct(struct task_struct *task)
128 struct fs_struct *fs;
132 atomic_inc(&fs->count);
137 static int get_nr_threads(struct task_struct *tsk)
139 /* Must be called with the rcu_read_lock held */
143 if (lock_task_sighand(tsk, &flags)) {
144 count = atomic_read(&tsk->signal->count);
145 unlock_task_sighand(tsk, &flags);
150 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
152 struct task_struct *task = get_proc_task(inode);
153 struct fs_struct *fs = NULL;
154 int result = -ENOENT;
157 fs = get_fs_struct(task);
158 put_task_struct(task);
161 read_lock(&fs->lock);
162 *mnt = mntget(fs->pwdmnt);
163 *dentry = dget(fs->pwd);
164 read_unlock(&fs->lock);
171 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
173 struct task_struct *task = get_proc_task(inode);
174 struct fs_struct *fs = NULL;
175 int result = -ENOENT;
178 fs = get_fs_struct(task);
179 put_task_struct(task);
182 read_lock(&fs->lock);
183 *mnt = mntget(fs->rootmnt);
184 *dentry = dget(fs->root);
185 read_unlock(&fs->lock);
192 #define MAY_PTRACE(task) \
193 (task == current || \
194 (task->parent == current && \
195 (task->ptrace & PT_PTRACED) && \
196 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
197 security_ptrace(current,task) == 0))
199 static int proc_pid_environ(struct task_struct *task, char * buffer)
202 struct mm_struct *mm = get_task_mm(task);
204 unsigned int len = mm->env_end - mm->env_start;
207 res = access_process_vm(task, mm->env_start, buffer, len, 0);
208 if (!ptrace_may_attach(task))
215 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
219 struct mm_struct *mm = get_task_mm(task);
223 goto out_mm; /* Shh! No looking before we're done */
225 len = mm->arg_end - mm->arg_start;
230 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
232 // If the nul at the end of args has been overwritten, then
233 // assume application is using setproctitle(3).
234 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
235 len = strnlen(buffer, res);
239 len = mm->env_end - mm->env_start;
240 if (len > PAGE_SIZE - res)
241 len = PAGE_SIZE - res;
242 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
243 res = strnlen(buffer, res);
252 static int proc_pid_auxv(struct task_struct *task, char *buffer)
255 struct mm_struct *mm = get_task_mm(task);
257 unsigned int nwords = 0;
260 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
261 res = nwords * sizeof(mm->saved_auxv[0]);
264 memcpy(buffer, mm->saved_auxv, res);
271 #ifdef CONFIG_KALLSYMS
273 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
274 * Returns the resolved symbol. If that fails, simply return the address.
276 static int proc_pid_wchan(struct task_struct *task, char *buffer)
279 const char *sym_name;
280 unsigned long wchan, size, offset;
281 char namebuf[KSYM_NAME_LEN+1];
283 wchan = get_wchan(task);
285 sym_name = kallsyms_lookup(wchan, &size, &offset, &modname, namebuf);
287 return sprintf(buffer, "%s", sym_name);
288 return sprintf(buffer, "%lu", wchan);
290 #endif /* CONFIG_KALLSYMS */
292 #ifdef CONFIG_SCHEDSTATS
294 * Provides /proc/PID/schedstat
296 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
298 return sprintf(buffer, "%lu %lu %lu\n",
299 task->sched_info.cpu_time,
300 task->sched_info.run_delay,
301 task->sched_info.pcnt);
305 /* The badness from the OOM killer */
306 unsigned long badness(struct task_struct *p, unsigned long uptime);
307 static int proc_oom_score(struct task_struct *task, char *buffer)
309 unsigned long points;
310 struct timespec uptime;
312 do_posix_clock_monotonic_gettime(&uptime);
313 points = badness(task, uptime.tv_sec);
314 return sprintf(buffer, "%lu\n", points);
317 /************************************************************************/
318 /* Here the fs part begins */
319 /************************************************************************/
321 /* permission checks */
322 static int proc_fd_access_allowed(struct inode *inode)
324 struct task_struct *task;
326 /* Allow access to a task's file descriptors if it is us or we
327 * may use ptrace attach to the process and find out that
330 task = get_proc_task(inode);
332 allowed = ptrace_may_attach(task);
333 put_task_struct(task);
338 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
341 struct inode *inode = dentry->d_inode;
343 if (attr->ia_valid & ATTR_MODE)
346 error = inode_change_ok(inode, attr);
348 error = security_inode_setattr(dentry, attr);
350 error = inode_setattr(inode, attr);
355 static struct inode_operations proc_def_inode_operations = {
356 .setattr = proc_setattr,
359 extern struct seq_operations mounts_op;
365 static int mounts_open(struct inode *inode, struct file *file)
367 struct task_struct *task = get_proc_task(inode);
368 struct namespace *namespace = NULL;
369 struct proc_mounts *p;
374 namespace = task->nsproxy->namespace;
376 get_namespace(namespace);
378 put_task_struct(task);
383 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
385 file->private_data = &p->m;
386 ret = seq_open(file, &mounts_op);
388 p->m.private = namespace;
389 p->event = namespace->event;
394 put_namespace(namespace);
399 static int mounts_release(struct inode *inode, struct file *file)
401 struct seq_file *m = file->private_data;
402 struct namespace *namespace = m->private;
403 put_namespace(namespace);
404 return seq_release(inode, file);
407 static unsigned mounts_poll(struct file *file, poll_table *wait)
409 struct proc_mounts *p = file->private_data;
410 struct namespace *ns = p->m.private;
413 poll_wait(file, &ns->poll, wait);
415 spin_lock(&vfsmount_lock);
416 if (p->event != ns->event) {
417 p->event = ns->event;
420 spin_unlock(&vfsmount_lock);
425 static struct file_operations proc_mounts_operations = {
429 .release = mounts_release,
433 extern struct seq_operations mountstats_op;
434 static int mountstats_open(struct inode *inode, struct file *file)
436 int ret = seq_open(file, &mountstats_op);
439 struct seq_file *m = file->private_data;
440 struct namespace *namespace = NULL;
441 struct task_struct *task = get_proc_task(inode);
445 namespace = task->nsproxy->namespace;
447 get_namespace(namespace);
449 put_task_struct(task);
453 m->private = namespace;
455 seq_release(inode, file);
462 static struct file_operations proc_mountstats_operations = {
463 .open = mountstats_open,
466 .release = mounts_release,
469 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
471 static ssize_t proc_info_read(struct file * file, char __user * buf,
472 size_t count, loff_t *ppos)
474 struct inode * inode = file->f_dentry->d_inode;
477 struct task_struct *task = get_proc_task(inode);
483 if (count > PROC_BLOCK_SIZE)
484 count = PROC_BLOCK_SIZE;
487 if (!(page = __get_free_page(GFP_KERNEL)))
490 length = PROC_I(inode)->op.proc_read(task, (char*)page);
493 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
496 put_task_struct(task);
501 static struct file_operations proc_info_file_operations = {
502 .read = proc_info_read,
505 static int mem_open(struct inode* inode, struct file* file)
507 file->private_data = (void*)((long)current->self_exec_id);
511 static ssize_t mem_read(struct file * file, char __user * buf,
512 size_t count, loff_t *ppos)
514 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
516 unsigned long src = *ppos;
518 struct mm_struct *mm;
523 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
527 page = (char *)__get_free_page(GFP_USER);
533 mm = get_task_mm(task);
539 if (file->private_data != (void*)((long)current->self_exec_id))
545 int this_len, retval;
547 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
548 retval = access_process_vm(task, src, page, this_len, 0);
549 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
555 if (copy_to_user(buf, page, retval)) {
570 free_page((unsigned long) page);
572 put_task_struct(task);
577 #define mem_write NULL
580 /* This is a security hazard */
581 static ssize_t mem_write(struct file * file, const char * buf,
582 size_t count, loff_t *ppos)
586 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
587 unsigned long dst = *ppos;
593 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
597 page = (char *)__get_free_page(GFP_USER);
603 int this_len, retval;
605 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
606 if (copy_from_user(page, buf, this_len)) {
610 retval = access_process_vm(task, dst, page, this_len, 1);
622 free_page((unsigned long) page);
624 put_task_struct(task);
630 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
634 file->f_pos = offset;
637 file->f_pos += offset;
642 force_successful_syscall_return();
646 static struct file_operations proc_mem_operations = {
653 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
654 size_t count, loff_t *ppos)
656 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
657 char buffer[PROC_NUMBUF];
660 loff_t __ppos = *ppos;
664 oom_adjust = task->oomkilladj;
665 put_task_struct(task);
667 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
670 if (count > len-__ppos)
672 if (copy_to_user(buf, buffer + __ppos, count))
674 *ppos = __ppos + count;
678 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
679 size_t count, loff_t *ppos)
681 struct task_struct *task;
682 char buffer[PROC_NUMBUF], *end;
685 if (!capable(CAP_SYS_RESOURCE))
687 memset(buffer, 0, sizeof(buffer));
688 if (count > sizeof(buffer) - 1)
689 count = sizeof(buffer) - 1;
690 if (copy_from_user(buffer, buf, count))
692 oom_adjust = simple_strtol(buffer, &end, 0);
693 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
694 oom_adjust != OOM_DISABLE)
698 task = get_proc_task(file->f_dentry->d_inode);
701 task->oomkilladj = oom_adjust;
702 put_task_struct(task);
703 if (end - buffer == 0)
708 static struct file_operations proc_oom_adjust_operations = {
709 .read = oom_adjust_read,
710 .write = oom_adjust_write,
713 #ifdef CONFIG_AUDITSYSCALL
715 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
716 size_t count, loff_t *ppos)
718 struct inode * inode = file->f_dentry->d_inode;
719 struct task_struct *task = get_proc_task(inode);
721 char tmpbuf[TMPBUFLEN];
725 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
726 audit_get_loginuid(task->audit_context));
727 put_task_struct(task);
728 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
731 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
732 size_t count, loff_t *ppos)
734 struct inode * inode = file->f_dentry->d_inode;
739 if (!capable(CAP_AUDIT_CONTROL))
742 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
745 if (count >= PAGE_SIZE)
746 count = PAGE_SIZE - 1;
749 /* No partial writes. */
752 page = (char*)__get_free_page(GFP_USER);
756 if (copy_from_user(page, buf, count))
760 loginuid = simple_strtoul(page, &tmp, 10);
766 length = audit_set_loginuid(current, loginuid);
767 if (likely(length == 0))
771 free_page((unsigned long) page);
775 static struct file_operations proc_loginuid_operations = {
776 .read = proc_loginuid_read,
777 .write = proc_loginuid_write,
781 #ifdef CONFIG_SECCOMP
782 static ssize_t seccomp_read(struct file *file, char __user *buf,
783 size_t count, loff_t *ppos)
785 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
787 loff_t __ppos = *ppos;
792 /* no need to print the trailing zero, so use only len */
793 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
794 put_task_struct(tsk);
797 if (count > len - __ppos)
798 count = len - __ppos;
799 if (copy_to_user(buf, __buf + __ppos, count))
801 *ppos = __ppos + count;
805 static ssize_t seccomp_write(struct file *file, const char __user *buf,
806 size_t count, loff_t *ppos)
808 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
809 char __buf[20], *end;
810 unsigned int seccomp_mode;
817 /* can set it only once to be even more secure */
819 if (unlikely(tsk->seccomp.mode))
823 memset(__buf, 0, sizeof(__buf));
824 count = min(count, sizeof(__buf) - 1);
825 if (copy_from_user(__buf, buf, count))
828 seccomp_mode = simple_strtoul(__buf, &end, 0);
832 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
833 tsk->seccomp.mode = seccomp_mode;
834 set_tsk_thread_flag(tsk, TIF_SECCOMP);
838 if (unlikely(!(end - __buf)))
840 result = end - __buf;
842 put_task_struct(tsk);
847 static struct file_operations proc_seccomp_operations = {
848 .read = seccomp_read,
849 .write = seccomp_write,
851 #endif /* CONFIG_SECCOMP */
853 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
855 struct inode *inode = dentry->d_inode;
858 /* We don't need a base pointer in the /proc filesystem */
861 /* Are we allowed to snoop on the tasks file descriptors? */
862 if (!proc_fd_access_allowed(inode))
865 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
866 nd->last_type = LAST_BIND;
868 return ERR_PTR(error);
871 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
872 char __user *buffer, int buflen)
874 struct inode * inode;
875 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
881 inode = dentry->d_inode;
882 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
886 len = tmp + PAGE_SIZE - 1 - path;
890 if (copy_to_user(buffer, path, len))
893 free_page((unsigned long)tmp);
897 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
900 struct inode *inode = dentry->d_inode;
902 struct vfsmount *mnt = NULL;
904 /* Are we allowed to snoop on the tasks file descriptors? */
905 if (!proc_fd_access_allowed(inode))
908 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
912 error = do_proc_readlink(de, mnt, buffer, buflen);
919 static struct inode_operations proc_pid_link_inode_operations = {
920 .readlink = proc_pid_readlink,
921 .follow_link = proc_pid_follow_link,
922 .setattr = proc_setattr,
926 /* building an inode */
928 static int task_dumpable(struct task_struct *task)
931 struct mm_struct *mm;
936 dumpable = mm->dumpable;
944 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
946 struct inode * inode;
947 struct proc_inode *ei;
949 /* We need a new inode */
951 inode = new_inode(sb);
957 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
958 inode->i_op = &proc_def_inode_operations;
961 * grab the reference to task.
963 ei->pid = get_task_pid(task, PIDTYPE_PID);
969 if (task_dumpable(task)) {
970 inode->i_uid = task->euid;
971 inode->i_gid = task->egid;
973 security_task_to_inode(task, inode);
983 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
985 struct inode *inode = dentry->d_inode;
986 struct task_struct *task;
987 generic_fillattr(inode, stat);
992 task = pid_task(proc_pid(inode), PIDTYPE_PID);
994 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
995 task_dumpable(task)) {
996 stat->uid = task->euid;
997 stat->gid = task->egid;
1007 * Exceptional case: normally we are not allowed to unhash a busy
1008 * directory. In this case, however, we can do it - no aliasing problems
1009 * due to the way we treat inodes.
1011 * Rewrite the inode's ownerships here because the owning task may have
1012 * performed a setuid(), etc.
1014 * Before the /proc/pid/status file was created the only way to read
1015 * the effective uid of a /process was to stat /proc/pid. Reading
1016 * /proc/pid/status is slow enough that procps and other packages
1017 * kept stating /proc/pid. To keep the rules in /proc simple I have
1018 * made this apply to all per process world readable and executable
1021 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1023 struct inode *inode = dentry->d_inode;
1024 struct task_struct *task = get_proc_task(inode);
1026 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1027 task_dumpable(task)) {
1028 inode->i_uid = task->euid;
1029 inode->i_gid = task->egid;
1034 inode->i_mode &= ~(S_ISUID | S_ISGID);
1035 security_task_to_inode(task, inode);
1036 put_task_struct(task);
1043 static int pid_delete_dentry(struct dentry * dentry)
1045 /* Is the task we represent dead?
1046 * If so, then don't put the dentry on the lru list,
1047 * kill it immediately.
1049 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1052 static struct dentry_operations pid_dentry_operations =
1054 .d_revalidate = pid_revalidate,
1055 .d_delete = pid_delete_dentry,
1060 typedef struct dentry *instantiate_t(struct inode *, struct dentry *, struct task_struct *, void *);
1063 * Fill a directory entry.
1065 * If possible create the dcache entry and derive our inode number and
1066 * file type from dcache entry.
1068 * Since all of the proc inode numbers are dynamically generated, the inode
1069 * numbers do not exist until the inode is cache. This means creating the
1070 * the dcache entry in readdir is necessary to keep the inode numbers
1071 * reported by readdir in sync with the inode numbers reported
1074 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1075 char *name, int len,
1076 instantiate_t instantiate, struct task_struct *task, void *ptr)
1078 struct dentry *child, *dir = filp->f_dentry;
1079 struct inode *inode;
1082 unsigned type = DT_UNKNOWN;
1086 qname.hash = full_name_hash(name, len);
1088 child = d_lookup(dir, &qname);
1091 new = d_alloc(dir, &qname);
1093 child = instantiate(dir->d_inode, new, task, ptr);
1100 if (!child || IS_ERR(child) || !child->d_inode)
1101 goto end_instantiate;
1102 inode = child->d_inode;
1105 type = inode->i_mode >> 12;
1110 ino = find_inode_number(dir, &qname);
1113 return filldir(dirent, name, len, filp->f_pos, ino, type);
1116 static unsigned name_to_int(struct dentry *dentry)
1118 const char *name = dentry->d_name.name;
1119 int len = dentry->d_name.len;
1122 if (len > 1 && *name == '0')
1125 unsigned c = *name++ - '0';
1128 if (n >= (~0U-9)/10)
1138 static int proc_fd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
1140 struct task_struct *task = get_proc_task(inode);
1141 struct files_struct *files = NULL;
1143 int fd = proc_fd(inode);
1146 files = get_files_struct(task);
1147 put_task_struct(task);
1151 * We are not taking a ref to the file structure, so we must
1154 spin_lock(&files->file_lock);
1155 file = fcheck_files(files, fd);
1157 *mnt = mntget(file->f_vfsmnt);
1158 *dentry = dget(file->f_dentry);
1159 spin_unlock(&files->file_lock);
1160 put_files_struct(files);
1163 spin_unlock(&files->file_lock);
1164 put_files_struct(files);
1169 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1171 struct inode *inode = dentry->d_inode;
1172 struct task_struct *task = get_proc_task(inode);
1173 int fd = proc_fd(inode);
1174 struct files_struct *files;
1177 files = get_files_struct(task);
1180 if (fcheck_files(files, fd)) {
1182 put_files_struct(files);
1183 if (task_dumpable(task)) {
1184 inode->i_uid = task->euid;
1185 inode->i_gid = task->egid;
1190 inode->i_mode &= ~(S_ISUID | S_ISGID);
1191 security_task_to_inode(task, inode);
1192 put_task_struct(task);
1196 put_files_struct(files);
1198 put_task_struct(task);
1204 static struct dentry_operations tid_fd_dentry_operations =
1206 .d_revalidate = tid_fd_revalidate,
1207 .d_delete = pid_delete_dentry,
1210 static struct dentry *proc_fd_instantiate(struct inode *dir,
1211 struct dentry *dentry, struct task_struct *task, void *ptr)
1213 unsigned fd = *(unsigned *)ptr;
1215 struct files_struct *files;
1216 struct inode *inode;
1217 struct proc_inode *ei;
1218 struct dentry *error = ERR_PTR(-ENOENT);
1220 inode = proc_pid_make_inode(dir->i_sb, task);
1225 files = get_files_struct(task);
1228 inode->i_mode = S_IFLNK;
1231 * We are not taking a ref to the file structure, so we must
1234 spin_lock(&files->file_lock);
1235 file = fcheck_files(files, fd);
1238 if (file->f_mode & 1)
1239 inode->i_mode |= S_IRUSR | S_IXUSR;
1240 if (file->f_mode & 2)
1241 inode->i_mode |= S_IWUSR | S_IXUSR;
1242 spin_unlock(&files->file_lock);
1243 put_files_struct(files);
1245 inode->i_op = &proc_pid_link_inode_operations;
1247 ei->op.proc_get_link = proc_fd_link;
1248 dentry->d_op = &tid_fd_dentry_operations;
1249 d_add(dentry, inode);
1250 /* Close the race of the process dying before we return the dentry */
1251 if (tid_fd_revalidate(dentry, NULL))
1257 spin_unlock(&files->file_lock);
1258 put_files_struct(files);
1264 static struct dentry *proc_lookupfd(struct inode * dir, struct dentry * dentry, struct nameidata *nd)
1266 struct task_struct *task = get_proc_task(dir);
1267 unsigned fd = name_to_int(dentry);
1268 struct dentry *result = ERR_PTR(-ENOENT);
1275 result = proc_fd_instantiate(dir, dentry, task, &fd);
1277 put_task_struct(task);
1282 static int proc_fd_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1283 struct task_struct *task, int fd)
1285 char name[PROC_NUMBUF];
1286 int len = snprintf(name, sizeof(name), "%d", fd);
1287 return proc_fill_cache(filp, dirent, filldir, name, len,
1288 proc_fd_instantiate, task, &fd);
1291 static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
1293 struct dentry *dentry = filp->f_dentry;
1294 struct inode *inode = dentry->d_inode;
1295 struct task_struct *p = get_proc_task(inode);
1296 unsigned int fd, tid, ino;
1298 struct files_struct * files;
1299 struct fdtable *fdt;
1310 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1314 ino = parent_ino(dentry);
1315 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1319 files = get_files_struct(p);
1323 fdt = files_fdtable(files);
1324 for (fd = filp->f_pos-2;
1326 fd++, filp->f_pos++) {
1328 if (!fcheck_files(files, fd))
1332 if (proc_fd_fill_cache(filp, dirent, filldir, p, fd) < 0) {
1339 put_files_struct(files);
1347 static struct file_operations proc_fd_operations = {
1348 .read = generic_read_dir,
1349 .readdir = proc_readfd,
1353 * proc directories can do almost nothing..
1355 static struct inode_operations proc_fd_inode_operations = {
1356 .lookup = proc_lookupfd,
1357 .setattr = proc_setattr,
1360 static struct dentry *proc_pident_instantiate(struct inode *dir,
1361 struct dentry *dentry, struct task_struct *task, void *ptr)
1363 struct pid_entry *p = ptr;
1364 struct inode *inode;
1365 struct proc_inode *ei;
1366 struct dentry *error = ERR_PTR(-EINVAL);
1368 inode = proc_pid_make_inode(dir->i_sb, task);
1373 inode->i_mode = p->mode;
1374 if (S_ISDIR(inode->i_mode))
1375 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1377 inode->i_op = p->iop;
1379 inode->i_fop = p->fop;
1381 dentry->d_op = &pid_dentry_operations;
1382 d_add(dentry, inode);
1383 /* Close the race of the process dying before we return the dentry */
1384 if (pid_revalidate(dentry, NULL))
1390 static struct dentry *proc_pident_lookup(struct inode *dir,
1391 struct dentry *dentry,
1392 struct pid_entry *ents,
1395 struct inode *inode;
1396 struct dentry *error;
1397 struct task_struct *task = get_proc_task(dir);
1398 struct pid_entry *p, *last;
1400 error = ERR_PTR(-ENOENT);
1407 * Yes, it does not scale. And it should not. Don't add
1408 * new entries into /proc/<tgid>/ without very good reasons.
1410 last = &ents[nents - 1];
1411 for (p = ents; p <= last; p++) {
1412 if (p->len != dentry->d_name.len)
1414 if (!memcmp(dentry->d_name.name, p->name, p->len))
1420 error = proc_pident_instantiate(dir, dentry, task, p);
1422 put_task_struct(task);
1427 static int proc_pident_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1428 struct task_struct *task, struct pid_entry *p)
1430 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1431 proc_pident_instantiate, task, p);
1434 static int proc_pident_readdir(struct file *filp,
1435 void *dirent, filldir_t filldir,
1436 struct pid_entry *ents, unsigned int nents)
1440 struct dentry *dentry = filp->f_dentry;
1441 struct inode *inode = dentry->d_inode;
1442 struct task_struct *task = get_proc_task(inode);
1443 struct pid_entry *p, *last;
1457 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1463 ino = parent_ino(dentry);
1464 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1476 last = &ents[nents - 1];
1478 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1487 put_task_struct(task);
1492 #ifdef CONFIG_SECURITY
1493 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1494 size_t count, loff_t *ppos)
1496 struct inode * inode = file->f_dentry->d_inode;
1499 struct task_struct *task = get_proc_task(inode);
1505 if (count > PAGE_SIZE)
1508 if (!(page = __get_free_page(GFP_KERNEL)))
1511 length = security_getprocattr(task,
1512 (char*)file->f_dentry->d_name.name,
1513 (void*)page, count);
1515 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
1518 put_task_struct(task);
1523 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1524 size_t count, loff_t *ppos)
1526 struct inode * inode = file->f_dentry->d_inode;
1529 struct task_struct *task = get_proc_task(inode);
1534 if (count > PAGE_SIZE)
1537 /* No partial writes. */
1543 page = (char*)__get_free_page(GFP_USER);
1548 if (copy_from_user(page, buf, count))
1551 length = security_setprocattr(task,
1552 (char*)file->f_dentry->d_name.name,
1553 (void*)page, count);
1555 free_page((unsigned long) page);
1557 put_task_struct(task);
1562 static struct file_operations proc_pid_attr_operations = {
1563 .read = proc_pid_attr_read,
1564 .write = proc_pid_attr_write,
1567 static struct pid_entry attr_dir_stuff[] = {
1568 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1569 REG("prev", S_IRUGO, pid_attr),
1570 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1571 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1572 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1573 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1576 static int proc_attr_dir_readdir(struct file * filp,
1577 void * dirent, filldir_t filldir)
1579 return proc_pident_readdir(filp,dirent,filldir,
1580 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1583 static struct file_operations proc_attr_dir_operations = {
1584 .read = generic_read_dir,
1585 .readdir = proc_attr_dir_readdir,
1588 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1589 struct dentry *dentry, struct nameidata *nd)
1591 return proc_pident_lookup(dir, dentry,
1592 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1595 static struct inode_operations proc_attr_dir_inode_operations = {
1596 .lookup = proc_attr_dir_lookup,
1597 .getattr = pid_getattr,
1598 .setattr = proc_setattr,
1606 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1609 char tmp[PROC_NUMBUF];
1610 sprintf(tmp, "%d", current->tgid);
1611 return vfs_readlink(dentry,buffer,buflen,tmp);
1614 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1616 char tmp[PROC_NUMBUF];
1617 sprintf(tmp, "%d", current->tgid);
1618 return ERR_PTR(vfs_follow_link(nd,tmp));
1621 static struct inode_operations proc_self_inode_operations = {
1622 .readlink = proc_self_readlink,
1623 .follow_link = proc_self_follow_link,
1629 * These are the directory entries in the root directory of /proc
1630 * that properly belong to the /proc filesystem, as they describe
1631 * describe something that is process related.
1633 static struct pid_entry proc_base_stuff[] = {
1634 NOD("self", S_IFLNK|S_IRWXUGO,
1635 &proc_self_inode_operations, NULL, {}),
1639 * Exceptional case: normally we are not allowed to unhash a busy
1640 * directory. In this case, however, we can do it - no aliasing problems
1641 * due to the way we treat inodes.
1643 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1645 struct inode *inode = dentry->d_inode;
1646 struct task_struct *task = get_proc_task(inode);
1648 put_task_struct(task);
1655 static struct dentry_operations proc_base_dentry_operations =
1657 .d_revalidate = proc_base_revalidate,
1658 .d_delete = pid_delete_dentry,
1661 static struct dentry *proc_base_instantiate(struct inode *dir,
1662 struct dentry *dentry, struct task_struct *task, void *ptr)
1664 struct pid_entry *p = ptr;
1665 struct inode *inode;
1666 struct proc_inode *ei;
1667 struct dentry *error = ERR_PTR(-EINVAL);
1669 /* Allocate the inode */
1670 error = ERR_PTR(-ENOMEM);
1671 inode = new_inode(dir->i_sb);
1675 /* Initialize the inode */
1677 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1680 * grab the reference to the task.
1682 ei->pid = get_task_pid(task, PIDTYPE_PID);
1688 inode->i_mode = p->mode;
1689 if (S_ISDIR(inode->i_mode))
1691 if (S_ISLNK(inode->i_mode))
1694 inode->i_op = p->iop;
1696 inode->i_fop = p->fop;
1698 dentry->d_op = &proc_base_dentry_operations;
1699 d_add(dentry, inode);
1708 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1710 struct dentry *error;
1711 struct task_struct *task = get_proc_task(dir);
1712 struct pid_entry *p, *last;
1714 error = ERR_PTR(-ENOENT);
1719 /* Lookup the directory entry */
1720 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1721 for (p = proc_base_stuff; p <= last; p++) {
1722 if (p->len != dentry->d_name.len)
1724 if (!memcmp(dentry->d_name.name, p->name, p->len))
1730 error = proc_base_instantiate(dir, dentry, task, p);
1733 put_task_struct(task);
1738 static int proc_base_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1739 struct task_struct *task, struct pid_entry *p)
1741 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1742 proc_base_instantiate, task, p);
1748 static struct file_operations proc_task_operations;
1749 static struct inode_operations proc_task_inode_operations;
1751 static struct pid_entry tgid_base_stuff[] = {
1752 DIR("task", S_IRUGO|S_IXUGO, task),
1753 DIR("fd", S_IRUSR|S_IXUSR, fd),
1754 INF("environ", S_IRUSR, pid_environ),
1755 INF("auxv", S_IRUSR, pid_auxv),
1756 INF("status", S_IRUGO, pid_status),
1757 INF("cmdline", S_IRUGO, pid_cmdline),
1758 INF("stat", S_IRUGO, tgid_stat),
1759 INF("statm", S_IRUGO, pid_statm),
1760 REG("maps", S_IRUGO, maps),
1762 REG("numa_maps", S_IRUGO, numa_maps),
1764 REG("mem", S_IRUSR|S_IWUSR, mem),
1765 #ifdef CONFIG_SECCOMP
1766 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
1771 REG("mounts", S_IRUGO, mounts),
1772 REG("mountstats", S_IRUSR, mountstats),
1774 REG("smaps", S_IRUGO, smaps),
1776 #ifdef CONFIG_SECURITY
1777 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
1779 #ifdef CONFIG_KALLSYMS
1780 INF("wchan", S_IRUGO, pid_wchan),
1782 #ifdef CONFIG_SCHEDSTATS
1783 INF("schedstat", S_IRUGO, pid_schedstat),
1785 #ifdef CONFIG_CPUSETS
1786 REG("cpuset", S_IRUGO, cpuset),
1788 INF("oom_score", S_IRUGO, oom_score),
1789 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
1790 #ifdef CONFIG_AUDITSYSCALL
1791 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
1795 static int proc_tgid_base_readdir(struct file * filp,
1796 void * dirent, filldir_t filldir)
1798 return proc_pident_readdir(filp,dirent,filldir,
1799 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
1802 static struct file_operations proc_tgid_base_operations = {
1803 .read = generic_read_dir,
1804 .readdir = proc_tgid_base_readdir,
1807 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
1808 return proc_pident_lookup(dir, dentry,
1809 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
1812 static struct inode_operations proc_tgid_base_inode_operations = {
1813 .lookup = proc_tgid_base_lookup,
1814 .getattr = pid_getattr,
1815 .setattr = proc_setattr,
1819 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1821 * @task: task that should be flushed.
1823 * Looks in the dcache for
1825 * /proc/@tgid/task/@pid
1826 * if either directory is present flushes it and all of it'ts children
1829 * It is safe and reasonable to cache /proc entries for a task until
1830 * that task exits. After that they just clog up the dcache with
1831 * useless entries, possibly causing useful dcache entries to be
1832 * flushed instead. This routine is proved to flush those useless
1833 * dcache entries at process exit time.
1835 * NOTE: This routine is just an optimization so it does not guarantee
1836 * that no dcache entries will exist at process exit time it
1837 * just makes it very unlikely that any will persist.
1839 void proc_flush_task(struct task_struct *task)
1841 struct dentry *dentry, *leader, *dir;
1842 char buf[PROC_NUMBUF];
1846 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1847 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1849 shrink_dcache_parent(dentry);
1854 if (thread_group_leader(task))
1858 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
1859 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
1864 name.len = strlen(name.name);
1865 dir = d_hash_and_lookup(leader, &name);
1867 goto out_put_leader;
1870 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
1871 dentry = d_hash_and_lookup(dir, &name);
1873 shrink_dcache_parent(dentry);
1885 struct dentry *proc_pid_instantiate(struct inode *dir,
1886 struct dentry * dentry, struct task_struct *task, void *ptr)
1888 struct dentry *error = ERR_PTR(-ENOENT);
1889 struct inode *inode;
1891 inode = proc_pid_make_inode(dir->i_sb, task);
1895 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
1896 inode->i_op = &proc_tgid_base_inode_operations;
1897 inode->i_fop = &proc_tgid_base_operations;
1898 inode->i_flags|=S_IMMUTABLE;
1900 #ifdef CONFIG_SECURITY
1901 inode->i_nlink += 1;
1904 dentry->d_op = &pid_dentry_operations;
1906 d_add(dentry, inode);
1907 /* Close the race of the process dying before we return the dentry */
1908 if (pid_revalidate(dentry, NULL))
1914 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
1916 struct dentry *result = ERR_PTR(-ENOENT);
1917 struct task_struct *task;
1920 result = proc_base_lookup(dir, dentry);
1921 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
1924 tgid = name_to_int(dentry);
1929 task = find_task_by_pid(tgid);
1931 get_task_struct(task);
1936 result = proc_pid_instantiate(dir, dentry, task, NULL);
1937 put_task_struct(task);
1943 * Find the first task with tgid >= tgid
1946 static struct task_struct *next_tgid(unsigned int tgid)
1948 struct task_struct *task;
1954 pid = find_ge_pid(tgid);
1957 task = pid_task(pid, PIDTYPE_PID);
1958 /* What we to know is if the pid we have find is the
1959 * pid of a thread_group_leader. Testing for task
1960 * being a thread_group_leader is the obvious thing
1961 * todo but there is a window when it fails, due to
1962 * the pid transfer logic in de_thread.
1964 * So we perform the straight forward test of seeing
1965 * if the pid we have found is the pid of a thread
1966 * group leader, and don't worry if the task we have
1967 * found doesn't happen to be a thread group leader.
1968 * As we don't care in the case of readdir.
1970 if (!task || !has_group_leader_pid(task))
1972 get_task_struct(task);
1978 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
1980 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1981 struct task_struct *task, int tgid)
1983 char name[PROC_NUMBUF];
1984 int len = snprintf(name, sizeof(name), "%d", tgid);
1985 return proc_fill_cache(filp, dirent, filldir, name, len,
1986 proc_pid_instantiate, task, NULL);
1989 /* for the /proc/ directory itself, after non-process stuff has been done */
1990 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
1992 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
1993 struct task_struct *reaper = get_proc_task(filp->f_dentry->d_inode);
1994 struct task_struct *task;
2000 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2001 struct pid_entry *p = &proc_base_stuff[nr];
2002 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2006 tgid = filp->f_pos - TGID_OFFSET;
2007 for (task = next_tgid(tgid);
2009 put_task_struct(task), task = next_tgid(tgid + 1)) {
2011 filp->f_pos = tgid + TGID_OFFSET;
2012 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2013 put_task_struct(task);
2017 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2019 put_task_struct(reaper);
2027 static struct pid_entry tid_base_stuff[] = {
2028 DIR("fd", S_IRUSR|S_IXUSR, fd),
2029 INF("environ", S_IRUSR, pid_environ),
2030 INF("auxv", S_IRUSR, pid_auxv),
2031 INF("status", S_IRUGO, pid_status),
2032 INF("cmdline", S_IRUGO, pid_cmdline),
2033 INF("stat", S_IRUGO, tid_stat),
2034 INF("statm", S_IRUGO, pid_statm),
2035 REG("maps", S_IRUGO, maps),
2037 REG("numa_maps", S_IRUGO, numa_maps),
2039 REG("mem", S_IRUSR|S_IWUSR, mem),
2040 #ifdef CONFIG_SECCOMP
2041 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2046 REG("mounts", S_IRUGO, mounts),
2048 REG("smaps", S_IRUGO, smaps),
2050 #ifdef CONFIG_SECURITY
2051 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2053 #ifdef CONFIG_KALLSYMS
2054 INF("wchan", S_IRUGO, pid_wchan),
2056 #ifdef CONFIG_SCHEDSTATS
2057 INF("schedstat", S_IRUGO, pid_schedstat),
2059 #ifdef CONFIG_CPUSETS
2060 REG("cpuset", S_IRUGO, cpuset),
2062 INF("oom_score", S_IRUGO, oom_score),
2063 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2064 #ifdef CONFIG_AUDITSYSCALL
2065 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2069 static int proc_tid_base_readdir(struct file * filp,
2070 void * dirent, filldir_t filldir)
2072 return proc_pident_readdir(filp,dirent,filldir,
2073 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2076 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2077 return proc_pident_lookup(dir, dentry,
2078 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2081 static struct file_operations proc_tid_base_operations = {
2082 .read = generic_read_dir,
2083 .readdir = proc_tid_base_readdir,
2086 static struct inode_operations proc_tid_base_inode_operations = {
2087 .lookup = proc_tid_base_lookup,
2088 .getattr = pid_getattr,
2089 .setattr = proc_setattr,
2092 static struct dentry *proc_task_instantiate(struct inode *dir,
2093 struct dentry *dentry, struct task_struct *task, void *ptr)
2095 struct dentry *error = ERR_PTR(-ENOENT);
2096 struct inode *inode;
2097 inode = proc_pid_make_inode(dir->i_sb, task);
2101 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2102 inode->i_op = &proc_tid_base_inode_operations;
2103 inode->i_fop = &proc_tid_base_operations;
2104 inode->i_flags|=S_IMMUTABLE;
2106 #ifdef CONFIG_SECURITY
2107 inode->i_nlink += 1;
2110 dentry->d_op = &pid_dentry_operations;
2112 d_add(dentry, inode);
2113 /* Close the race of the process dying before we return the dentry */
2114 if (pid_revalidate(dentry, NULL))
2120 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2122 struct dentry *result = ERR_PTR(-ENOENT);
2123 struct task_struct *task;
2124 struct task_struct *leader = get_proc_task(dir);
2130 tid = name_to_int(dentry);
2135 task = find_task_by_pid(tid);
2137 get_task_struct(task);
2141 if (leader->tgid != task->tgid)
2144 result = proc_task_instantiate(dir, dentry, task, NULL);
2146 put_task_struct(task);
2148 put_task_struct(leader);
2154 * Find the first tid of a thread group to return to user space.
2156 * Usually this is just the thread group leader, but if the users
2157 * buffer was too small or there was a seek into the middle of the
2158 * directory we have more work todo.
2160 * In the case of a short read we start with find_task_by_pid.
2162 * In the case of a seek we start with the leader and walk nr
2165 static struct task_struct *first_tid(struct task_struct *leader,
2168 struct task_struct *pos;
2171 /* Attempt to start with the pid of a thread */
2172 if (tid && (nr > 0)) {
2173 pos = find_task_by_pid(tid);
2174 if (pos && (pos->group_leader == leader))
2178 /* If nr exceeds the number of threads there is nothing todo */
2180 if (nr && nr >= get_nr_threads(leader))
2183 /* If we haven't found our starting place yet start
2184 * with the leader and walk nr threads forward.
2186 for (pos = leader; nr > 0; --nr) {
2187 pos = next_thread(pos);
2188 if (pos == leader) {
2194 get_task_struct(pos);
2201 * Find the next thread in the thread list.
2202 * Return NULL if there is an error or no next thread.
2204 * The reference to the input task_struct is released.
2206 static struct task_struct *next_tid(struct task_struct *start)
2208 struct task_struct *pos = NULL;
2210 if (pid_alive(start)) {
2211 pos = next_thread(start);
2212 if (thread_group_leader(pos))
2215 get_task_struct(pos);
2218 put_task_struct(start);
2222 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2223 struct task_struct *task, int tid)
2225 char name[PROC_NUMBUF];
2226 int len = snprintf(name, sizeof(name), "%d", tid);
2227 return proc_fill_cache(filp, dirent, filldir, name, len,
2228 proc_task_instantiate, task, NULL);
2231 /* for the /proc/TGID/task/ directories */
2232 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2234 struct dentry *dentry = filp->f_dentry;
2235 struct inode *inode = dentry->d_inode;
2236 struct task_struct *leader = get_proc_task(inode);
2237 struct task_struct *task;
2238 int retval = -ENOENT;
2241 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2250 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2255 ino = parent_ino(dentry);
2256 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2262 /* f_version caches the tgid value that the last readdir call couldn't
2263 * return. lseek aka telldir automagically resets f_version to 0.
2265 tid = filp->f_version;
2266 filp->f_version = 0;
2267 for (task = first_tid(leader, tid, pos - 2);
2269 task = next_tid(task), pos++) {
2271 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2272 /* returning this tgid failed, save it as the first
2273 * pid for the next readir call */
2274 filp->f_version = tid;
2275 put_task_struct(task);
2281 put_task_struct(leader);
2286 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2288 struct inode *inode = dentry->d_inode;
2289 struct task_struct *p = get_proc_task(inode);
2290 generic_fillattr(inode, stat);
2294 stat->nlink += get_nr_threads(p);
2302 static struct inode_operations proc_task_inode_operations = {
2303 .lookup = proc_task_lookup,
2304 .getattr = proc_task_getattr,
2305 .setattr = proc_setattr,
2308 static struct file_operations proc_task_operations = {
2309 .read = generic_read_dir,
2310 .readdir = proc_task_readdir,