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/mnt_namespace.h>
64 #include <linux/rcupdate.h>
65 #include <linux/kallsyms.h>
66 #include <linux/module.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 const struct inode_operations *iop;
97 const struct file_operations *fop;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
103 .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 } )
127 EXPORT_SYMBOL(maps_protect);
129 static struct fs_struct *get_fs_struct(struct task_struct *task)
131 struct fs_struct *fs;
135 atomic_inc(&fs->count);
140 static int get_nr_threads(struct task_struct *tsk)
142 /* Must be called with the rcu_read_lock held */
146 if (lock_task_sighand(tsk, &flags)) {
147 count = atomic_read(&tsk->signal->count);
148 unlock_task_sighand(tsk, &flags);
153 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
155 struct task_struct *task = get_proc_task(inode);
156 struct fs_struct *fs = NULL;
157 int result = -ENOENT;
160 fs = get_fs_struct(task);
161 put_task_struct(task);
164 read_lock(&fs->lock);
165 *mnt = mntget(fs->pwdmnt);
166 *dentry = dget(fs->pwd);
167 read_unlock(&fs->lock);
174 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
176 struct task_struct *task = get_proc_task(inode);
177 struct fs_struct *fs = NULL;
178 int result = -ENOENT;
181 fs = get_fs_struct(task);
182 put_task_struct(task);
185 read_lock(&fs->lock);
186 *mnt = mntget(fs->rootmnt);
187 *dentry = dget(fs->root);
188 read_unlock(&fs->lock);
195 #define MAY_PTRACE(task) \
196 (task == current || \
197 (task->parent == current && \
198 (task->ptrace & PT_PTRACED) && \
199 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
200 security_ptrace(current,task) == 0))
202 static int proc_pid_environ(struct task_struct *task, char * buffer)
205 struct mm_struct *mm = get_task_mm(task);
207 unsigned int len = mm->env_end - mm->env_start;
210 res = access_process_vm(task, mm->env_start, buffer, len, 0);
211 if (!ptrace_may_attach(task))
218 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
222 struct mm_struct *mm = get_task_mm(task);
226 goto out_mm; /* Shh! No looking before we're done */
228 len = mm->arg_end - mm->arg_start;
233 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
235 // If the nul at the end of args has been overwritten, then
236 // assume application is using setproctitle(3).
237 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
238 len = strnlen(buffer, res);
242 len = mm->env_end - mm->env_start;
243 if (len > PAGE_SIZE - res)
244 len = PAGE_SIZE - res;
245 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
246 res = strnlen(buffer, res);
255 static int proc_pid_auxv(struct task_struct *task, char *buffer)
258 struct mm_struct *mm = get_task_mm(task);
260 unsigned int nwords = 0;
263 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
264 res = nwords * sizeof(mm->saved_auxv[0]);
267 memcpy(buffer, mm->saved_auxv, res);
274 #ifdef CONFIG_KALLSYMS
276 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
277 * Returns the resolved symbol. If that fails, simply return the address.
279 static int proc_pid_wchan(struct task_struct *task, char *buffer)
282 char symname[KSYM_NAME_LEN+1];
284 wchan = get_wchan(task);
286 if (lookup_symbol_name(wchan, symname) < 0)
287 return sprintf(buffer, "%lu", wchan);
289 return sprintf(buffer, "%s", symname);
291 #endif /* CONFIG_KALLSYMS */
293 #ifdef CONFIG_SCHEDSTATS
295 * Provides /proc/PID/schedstat
297 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
299 return sprintf(buffer, "%lu %lu %lu\n",
300 task->sched_info.cpu_time,
301 task->sched_info.run_delay,
302 task->sched_info.pcnt);
306 /* The badness from the OOM killer */
307 unsigned long badness(struct task_struct *p, unsigned long uptime);
308 static int proc_oom_score(struct task_struct *task, char *buffer)
310 unsigned long points;
311 struct timespec uptime;
313 do_posix_clock_monotonic_gettime(&uptime);
314 read_lock(&tasklist_lock);
315 points = badness(task, uptime.tv_sec);
316 read_unlock(&tasklist_lock);
317 return sprintf(buffer, "%lu\n", points);
320 /************************************************************************/
321 /* Here the fs part begins */
322 /************************************************************************/
324 /* permission checks */
325 static int proc_fd_access_allowed(struct inode *inode)
327 struct task_struct *task;
329 /* Allow access to a task's file descriptors if it is us or we
330 * may use ptrace attach to the process and find out that
333 task = get_proc_task(inode);
335 allowed = ptrace_may_attach(task);
336 put_task_struct(task);
341 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
344 struct inode *inode = dentry->d_inode;
346 if (attr->ia_valid & ATTR_MODE)
349 error = inode_change_ok(inode, attr);
351 error = security_inode_setattr(dentry, attr);
353 error = inode_setattr(inode, attr);
358 static const struct inode_operations proc_def_inode_operations = {
359 .setattr = proc_setattr,
362 extern struct seq_operations mounts_op;
368 static int mounts_open(struct inode *inode, struct file *file)
370 struct task_struct *task = get_proc_task(inode);
371 struct mnt_namespace *ns = NULL;
372 struct proc_mounts *p;
378 ns = task->nsproxy->mnt_ns;
383 put_task_struct(task);
388 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
390 file->private_data = &p->m;
391 ret = seq_open(file, &mounts_op);
394 p->event = ns->event;
404 static int mounts_release(struct inode *inode, struct file *file)
406 struct seq_file *m = file->private_data;
407 struct mnt_namespace *ns = m->private;
409 return seq_release(inode, file);
412 static unsigned mounts_poll(struct file *file, poll_table *wait)
414 struct proc_mounts *p = file->private_data;
415 struct mnt_namespace *ns = p->m.private;
418 poll_wait(file, &ns->poll, wait);
420 spin_lock(&vfsmount_lock);
421 if (p->event != ns->event) {
422 p->event = ns->event;
425 spin_unlock(&vfsmount_lock);
430 static const struct file_operations proc_mounts_operations = {
434 .release = mounts_release,
438 extern struct seq_operations mountstats_op;
439 static int mountstats_open(struct inode *inode, struct file *file)
441 int ret = seq_open(file, &mountstats_op);
444 struct seq_file *m = file->private_data;
445 struct mnt_namespace *mnt_ns = NULL;
446 struct task_struct *task = get_proc_task(inode);
451 mnt_ns = task->nsproxy->mnt_ns;
455 put_task_struct(task);
461 seq_release(inode, file);
468 static const struct file_operations proc_mountstats_operations = {
469 .open = mountstats_open,
472 .release = mounts_release,
475 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
477 static ssize_t proc_info_read(struct file * file, char __user * buf,
478 size_t count, loff_t *ppos)
480 struct inode * inode = file->f_path.dentry->d_inode;
483 struct task_struct *task = get_proc_task(inode);
489 if (count > PROC_BLOCK_SIZE)
490 count = PROC_BLOCK_SIZE;
493 if (!(page = __get_free_page(GFP_KERNEL)))
496 length = PROC_I(inode)->op.proc_read(task, (char*)page);
499 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
502 put_task_struct(task);
507 static const struct file_operations proc_info_file_operations = {
508 .read = proc_info_read,
511 static int mem_open(struct inode* inode, struct file* file)
513 file->private_data = (void*)((long)current->self_exec_id);
517 static ssize_t mem_read(struct file * file, char __user * buf,
518 size_t count, loff_t *ppos)
520 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
522 unsigned long src = *ppos;
524 struct mm_struct *mm;
529 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
533 page = (char *)__get_free_page(GFP_USER);
539 mm = get_task_mm(task);
545 if (file->private_data != (void*)((long)current->self_exec_id))
551 int this_len, retval;
553 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
554 retval = access_process_vm(task, src, page, this_len, 0);
555 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
561 if (copy_to_user(buf, page, retval)) {
576 free_page((unsigned long) page);
578 put_task_struct(task);
583 #define mem_write NULL
586 /* This is a security hazard */
587 static ssize_t mem_write(struct file * file, const char __user *buf,
588 size_t count, loff_t *ppos)
592 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
593 unsigned long dst = *ppos;
599 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
603 page = (char *)__get_free_page(GFP_USER);
609 int this_len, retval;
611 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
612 if (copy_from_user(page, buf, this_len)) {
616 retval = access_process_vm(task, dst, page, this_len, 1);
628 free_page((unsigned long) page);
630 put_task_struct(task);
636 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
640 file->f_pos = offset;
643 file->f_pos += offset;
648 force_successful_syscall_return();
652 static const struct file_operations proc_mem_operations = {
659 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
660 size_t count, loff_t *ppos)
662 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
663 char buffer[PROC_NUMBUF];
666 loff_t __ppos = *ppos;
670 oom_adjust = task->oomkilladj;
671 put_task_struct(task);
673 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
676 if (count > len-__ppos)
678 if (copy_to_user(buf, buffer + __ppos, count))
680 *ppos = __ppos + count;
684 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
685 size_t count, loff_t *ppos)
687 struct task_struct *task;
688 char buffer[PROC_NUMBUF], *end;
691 memset(buffer, 0, sizeof(buffer));
692 if (count > sizeof(buffer) - 1)
693 count = sizeof(buffer) - 1;
694 if (copy_from_user(buffer, buf, count))
696 oom_adjust = simple_strtol(buffer, &end, 0);
697 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
698 oom_adjust != OOM_DISABLE)
702 task = get_proc_task(file->f_path.dentry->d_inode);
705 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
706 put_task_struct(task);
709 task->oomkilladj = oom_adjust;
710 put_task_struct(task);
711 if (end - buffer == 0)
716 static const struct file_operations proc_oom_adjust_operations = {
717 .read = oom_adjust_read,
718 .write = oom_adjust_write,
721 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
722 size_t count, loff_t *ppos)
724 struct task_struct *task;
725 char buffer[PROC_NUMBUF], *end;
726 struct mm_struct *mm;
728 memset(buffer, 0, sizeof(buffer));
729 if (count > sizeof(buffer) - 1)
730 count = sizeof(buffer) - 1;
731 if (copy_from_user(buffer, buf, count))
733 if (!simple_strtol(buffer, &end, 0))
737 task = get_proc_task(file->f_path.dentry->d_inode);
740 mm = get_task_mm(task);
745 put_task_struct(task);
746 if (end - buffer == 0)
751 static struct file_operations proc_clear_refs_operations = {
752 .write = clear_refs_write,
755 #ifdef CONFIG_AUDITSYSCALL
757 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
758 size_t count, loff_t *ppos)
760 struct inode * inode = file->f_path.dentry->d_inode;
761 struct task_struct *task = get_proc_task(inode);
763 char tmpbuf[TMPBUFLEN];
767 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
768 audit_get_loginuid(task->audit_context));
769 put_task_struct(task);
770 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
773 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
774 size_t count, loff_t *ppos)
776 struct inode * inode = file->f_path.dentry->d_inode;
781 if (!capable(CAP_AUDIT_CONTROL))
784 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
787 if (count >= PAGE_SIZE)
788 count = PAGE_SIZE - 1;
791 /* No partial writes. */
794 page = (char*)__get_free_page(GFP_USER);
798 if (copy_from_user(page, buf, count))
802 loginuid = simple_strtoul(page, &tmp, 10);
808 length = audit_set_loginuid(current, loginuid);
809 if (likely(length == 0))
813 free_page((unsigned long) page);
817 static const struct file_operations proc_loginuid_operations = {
818 .read = proc_loginuid_read,
819 .write = proc_loginuid_write,
823 #ifdef CONFIG_SECCOMP
824 static ssize_t seccomp_read(struct file *file, char __user *buf,
825 size_t count, loff_t *ppos)
827 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
829 loff_t __ppos = *ppos;
834 /* no need to print the trailing zero, so use only len */
835 len = sprintf(__buf, "%u\n", tsk->seccomp.mode);
836 put_task_struct(tsk);
839 if (count > len - __ppos)
840 count = len - __ppos;
841 if (copy_to_user(buf, __buf + __ppos, count))
843 *ppos = __ppos + count;
847 static ssize_t seccomp_write(struct file *file, const char __user *buf,
848 size_t count, loff_t *ppos)
850 struct task_struct *tsk = get_proc_task(file->f_dentry->d_inode);
851 char __buf[20], *end;
852 unsigned int seccomp_mode;
859 /* can set it only once to be even more secure */
861 if (unlikely(tsk->seccomp.mode))
865 memset(__buf, 0, sizeof(__buf));
866 count = min(count, sizeof(__buf) - 1);
867 if (copy_from_user(__buf, buf, count))
870 seccomp_mode = simple_strtoul(__buf, &end, 0);
874 if (seccomp_mode && seccomp_mode <= NR_SECCOMP_MODES) {
875 tsk->seccomp.mode = seccomp_mode;
876 set_tsk_thread_flag(tsk, TIF_SECCOMP);
880 if (unlikely(!(end - __buf)))
882 result = end - __buf;
884 put_task_struct(tsk);
889 static const struct file_operations proc_seccomp_operations = {
890 .read = seccomp_read,
891 .write = seccomp_write,
893 #endif /* CONFIG_SECCOMP */
895 #ifdef CONFIG_FAULT_INJECTION
896 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
897 size_t count, loff_t *ppos)
899 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
900 char buffer[PROC_NUMBUF];
903 loff_t __ppos = *ppos;
907 make_it_fail = task->make_it_fail;
908 put_task_struct(task);
910 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
913 if (count > len-__ppos)
915 if (copy_to_user(buf, buffer + __ppos, count))
917 *ppos = __ppos + count;
921 static ssize_t proc_fault_inject_write(struct file * file,
922 const char __user * buf, size_t count, loff_t *ppos)
924 struct task_struct *task;
925 char buffer[PROC_NUMBUF], *end;
928 if (!capable(CAP_SYS_RESOURCE))
930 memset(buffer, 0, sizeof(buffer));
931 if (count > sizeof(buffer) - 1)
932 count = sizeof(buffer) - 1;
933 if (copy_from_user(buffer, buf, count))
935 make_it_fail = simple_strtol(buffer, &end, 0);
938 task = get_proc_task(file->f_dentry->d_inode);
941 task->make_it_fail = make_it_fail;
942 put_task_struct(task);
943 if (end - buffer == 0)
948 static const struct file_operations proc_fault_inject_operations = {
949 .read = proc_fault_inject_read,
950 .write = proc_fault_inject_write,
954 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
956 struct inode *inode = dentry->d_inode;
959 /* We don't need a base pointer in the /proc filesystem */
962 /* Are we allowed to snoop on the tasks file descriptors? */
963 if (!proc_fd_access_allowed(inode))
966 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
967 nd->last_type = LAST_BIND;
969 return ERR_PTR(error);
972 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
973 char __user *buffer, int buflen)
975 struct inode * inode;
976 char *tmp = (char*)__get_free_page(GFP_KERNEL), *path;
982 inode = dentry->d_inode;
983 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
987 len = tmp + PAGE_SIZE - 1 - path;
991 if (copy_to_user(buffer, path, len))
994 free_page((unsigned long)tmp);
998 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1000 int error = -EACCES;
1001 struct inode *inode = dentry->d_inode;
1003 struct vfsmount *mnt = NULL;
1005 /* Are we allowed to snoop on the tasks file descriptors? */
1006 if (!proc_fd_access_allowed(inode))
1009 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1013 error = do_proc_readlink(de, mnt, buffer, buflen);
1020 static const struct inode_operations proc_pid_link_inode_operations = {
1021 .readlink = proc_pid_readlink,
1022 .follow_link = proc_pid_follow_link,
1023 .setattr = proc_setattr,
1027 /* building an inode */
1029 static int task_dumpable(struct task_struct *task)
1032 struct mm_struct *mm;
1037 dumpable = mm->dumpable;
1045 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1047 struct inode * inode;
1048 struct proc_inode *ei;
1050 /* We need a new inode */
1052 inode = new_inode(sb);
1058 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1059 inode->i_op = &proc_def_inode_operations;
1062 * grab the reference to task.
1064 ei->pid = get_task_pid(task, PIDTYPE_PID);
1070 if (task_dumpable(task)) {
1071 inode->i_uid = task->euid;
1072 inode->i_gid = task->egid;
1074 security_task_to_inode(task, inode);
1084 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1086 struct inode *inode = dentry->d_inode;
1087 struct task_struct *task;
1088 generic_fillattr(inode, stat);
1093 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1095 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1096 task_dumpable(task)) {
1097 stat->uid = task->euid;
1098 stat->gid = task->egid;
1108 * Exceptional case: normally we are not allowed to unhash a busy
1109 * directory. In this case, however, we can do it - no aliasing problems
1110 * due to the way we treat inodes.
1112 * Rewrite the inode's ownerships here because the owning task may have
1113 * performed a setuid(), etc.
1115 * Before the /proc/pid/status file was created the only way to read
1116 * the effective uid of a /process was to stat /proc/pid. Reading
1117 * /proc/pid/status is slow enough that procps and other packages
1118 * kept stating /proc/pid. To keep the rules in /proc simple I have
1119 * made this apply to all per process world readable and executable
1122 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1124 struct inode *inode = dentry->d_inode;
1125 struct task_struct *task = get_proc_task(inode);
1127 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1128 task_dumpable(task)) {
1129 inode->i_uid = task->euid;
1130 inode->i_gid = task->egid;
1135 inode->i_mode &= ~(S_ISUID | S_ISGID);
1136 security_task_to_inode(task, inode);
1137 put_task_struct(task);
1144 static int pid_delete_dentry(struct dentry * dentry)
1146 /* Is the task we represent dead?
1147 * If so, then don't put the dentry on the lru list,
1148 * kill it immediately.
1150 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1153 static struct dentry_operations pid_dentry_operations =
1155 .d_revalidate = pid_revalidate,
1156 .d_delete = pid_delete_dentry,
1161 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1162 struct task_struct *, const void *);
1165 * Fill a directory entry.
1167 * If possible create the dcache entry and derive our inode number and
1168 * file type from dcache entry.
1170 * Since all of the proc inode numbers are dynamically generated, the inode
1171 * numbers do not exist until the inode is cache. This means creating the
1172 * the dcache entry in readdir is necessary to keep the inode numbers
1173 * reported by readdir in sync with the inode numbers reported
1176 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1177 char *name, int len,
1178 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1180 struct dentry *child, *dir = filp->f_path.dentry;
1181 struct inode *inode;
1184 unsigned type = DT_UNKNOWN;
1188 qname.hash = full_name_hash(name, len);
1190 child = d_lookup(dir, &qname);
1193 new = d_alloc(dir, &qname);
1195 child = instantiate(dir->d_inode, new, task, ptr);
1202 if (!child || IS_ERR(child) || !child->d_inode)
1203 goto end_instantiate;
1204 inode = child->d_inode;
1207 type = inode->i_mode >> 12;
1212 ino = find_inode_number(dir, &qname);
1215 return filldir(dirent, name, len, filp->f_pos, ino, type);
1218 static unsigned name_to_int(struct dentry *dentry)
1220 const char *name = dentry->d_name.name;
1221 int len = dentry->d_name.len;
1224 if (len > 1 && *name == '0')
1227 unsigned c = *name++ - '0';
1230 if (n >= (~0U-9)/10)
1240 #define PROC_FDINFO_MAX 64
1242 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1243 struct vfsmount **mnt, char *info)
1245 struct task_struct *task = get_proc_task(inode);
1246 struct files_struct *files = NULL;
1248 int fd = proc_fd(inode);
1251 files = get_files_struct(task);
1252 put_task_struct(task);
1256 * We are not taking a ref to the file structure, so we must
1259 spin_lock(&files->file_lock);
1260 file = fcheck_files(files, fd);
1263 *mnt = mntget(file->f_path.mnt);
1265 *dentry = dget(file->f_path.dentry);
1267 snprintf(info, PROC_FDINFO_MAX,
1270 (long long) file->f_pos,
1272 spin_unlock(&files->file_lock);
1273 put_files_struct(files);
1276 spin_unlock(&files->file_lock);
1277 put_files_struct(files);
1282 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1283 struct vfsmount **mnt)
1285 return proc_fd_info(inode, dentry, mnt, NULL);
1288 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1290 struct inode *inode = dentry->d_inode;
1291 struct task_struct *task = get_proc_task(inode);
1292 int fd = proc_fd(inode);
1293 struct files_struct *files;
1296 files = get_files_struct(task);
1299 if (fcheck_files(files, fd)) {
1301 put_files_struct(files);
1302 if (task_dumpable(task)) {
1303 inode->i_uid = task->euid;
1304 inode->i_gid = task->egid;
1309 inode->i_mode &= ~(S_ISUID | S_ISGID);
1310 security_task_to_inode(task, inode);
1311 put_task_struct(task);
1315 put_files_struct(files);
1317 put_task_struct(task);
1323 static struct dentry_operations tid_fd_dentry_operations =
1325 .d_revalidate = tid_fd_revalidate,
1326 .d_delete = pid_delete_dentry,
1329 static struct dentry *proc_fd_instantiate(struct inode *dir,
1330 struct dentry *dentry, struct task_struct *task, const void *ptr)
1332 unsigned fd = *(const unsigned *)ptr;
1334 struct files_struct *files;
1335 struct inode *inode;
1336 struct proc_inode *ei;
1337 struct dentry *error = ERR_PTR(-ENOENT);
1339 inode = proc_pid_make_inode(dir->i_sb, task);
1344 files = get_files_struct(task);
1347 inode->i_mode = S_IFLNK;
1350 * We are not taking a ref to the file structure, so we must
1353 spin_lock(&files->file_lock);
1354 file = fcheck_files(files, fd);
1357 if (file->f_mode & 1)
1358 inode->i_mode |= S_IRUSR | S_IXUSR;
1359 if (file->f_mode & 2)
1360 inode->i_mode |= S_IWUSR | S_IXUSR;
1361 spin_unlock(&files->file_lock);
1362 put_files_struct(files);
1364 inode->i_op = &proc_pid_link_inode_operations;
1366 ei->op.proc_get_link = proc_fd_link;
1367 dentry->d_op = &tid_fd_dentry_operations;
1368 d_add(dentry, inode);
1369 /* Close the race of the process dying before we return the dentry */
1370 if (tid_fd_revalidate(dentry, NULL))
1376 spin_unlock(&files->file_lock);
1377 put_files_struct(files);
1383 static struct dentry *proc_lookupfd_common(struct inode *dir,
1384 struct dentry *dentry,
1385 instantiate_t instantiate)
1387 struct task_struct *task = get_proc_task(dir);
1388 unsigned fd = name_to_int(dentry);
1389 struct dentry *result = ERR_PTR(-ENOENT);
1396 result = instantiate(dir, dentry, task, &fd);
1398 put_task_struct(task);
1403 static int proc_readfd_common(struct file * filp, void * dirent,
1404 filldir_t filldir, instantiate_t instantiate)
1406 struct dentry *dentry = filp->f_path.dentry;
1407 struct inode *inode = dentry->d_inode;
1408 struct task_struct *p = get_proc_task(inode);
1409 unsigned int fd, tid, ino;
1411 struct files_struct * files;
1412 struct fdtable *fdt;
1423 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1427 ino = parent_ino(dentry);
1428 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1432 files = get_files_struct(p);
1436 fdt = files_fdtable(files);
1437 for (fd = filp->f_pos-2;
1439 fd++, filp->f_pos++) {
1440 char name[PROC_NUMBUF];
1443 if (!fcheck_files(files, fd))
1447 len = snprintf(name, sizeof(name), "%d", fd);
1448 if (proc_fill_cache(filp, dirent, filldir,
1449 name, len, instantiate,
1457 put_files_struct(files);
1465 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1466 struct nameidata *nd)
1468 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1471 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1473 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1476 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1477 size_t len, loff_t *ppos)
1479 char tmp[PROC_FDINFO_MAX];
1480 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1482 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1486 static const struct file_operations proc_fdinfo_file_operations = {
1487 .open = nonseekable_open,
1488 .read = proc_fdinfo_read,
1491 static const struct file_operations proc_fd_operations = {
1492 .read = generic_read_dir,
1493 .readdir = proc_readfd,
1497 * /proc/pid/fd needs a special permission handler so that a process can still
1498 * access /proc/self/fd after it has executed a setuid().
1500 static int proc_fd_permission(struct inode *inode, int mask,
1501 struct nameidata *nd)
1505 rv = generic_permission(inode, mask, NULL);
1508 if (task_pid(current) == proc_pid(inode))
1514 * proc directories can do almost nothing..
1516 static const struct inode_operations proc_fd_inode_operations = {
1517 .lookup = proc_lookupfd,
1518 .permission = proc_fd_permission,
1519 .setattr = proc_setattr,
1522 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1523 struct dentry *dentry, struct task_struct *task, const void *ptr)
1525 unsigned fd = *(unsigned *)ptr;
1526 struct inode *inode;
1527 struct proc_inode *ei;
1528 struct dentry *error = ERR_PTR(-ENOENT);
1530 inode = proc_pid_make_inode(dir->i_sb, task);
1535 inode->i_mode = S_IFREG | S_IRUSR;
1536 inode->i_fop = &proc_fdinfo_file_operations;
1537 dentry->d_op = &tid_fd_dentry_operations;
1538 d_add(dentry, inode);
1539 /* Close the race of the process dying before we return the dentry */
1540 if (tid_fd_revalidate(dentry, NULL))
1547 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1548 struct dentry *dentry,
1549 struct nameidata *nd)
1551 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1554 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1556 return proc_readfd_common(filp, dirent, filldir,
1557 proc_fdinfo_instantiate);
1560 static const struct file_operations proc_fdinfo_operations = {
1561 .read = generic_read_dir,
1562 .readdir = proc_readfdinfo,
1566 * proc directories can do almost nothing..
1568 static const struct inode_operations proc_fdinfo_inode_operations = {
1569 .lookup = proc_lookupfdinfo,
1570 .setattr = proc_setattr,
1574 static struct dentry *proc_pident_instantiate(struct inode *dir,
1575 struct dentry *dentry, struct task_struct *task, const void *ptr)
1577 const struct pid_entry *p = ptr;
1578 struct inode *inode;
1579 struct proc_inode *ei;
1580 struct dentry *error = ERR_PTR(-EINVAL);
1582 inode = proc_pid_make_inode(dir->i_sb, task);
1587 inode->i_mode = p->mode;
1588 if (S_ISDIR(inode->i_mode))
1589 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1591 inode->i_op = p->iop;
1593 inode->i_fop = p->fop;
1595 dentry->d_op = &pid_dentry_operations;
1596 d_add(dentry, inode);
1597 /* Close the race of the process dying before we return the dentry */
1598 if (pid_revalidate(dentry, NULL))
1604 static struct dentry *proc_pident_lookup(struct inode *dir,
1605 struct dentry *dentry,
1606 const struct pid_entry *ents,
1609 struct inode *inode;
1610 struct dentry *error;
1611 struct task_struct *task = get_proc_task(dir);
1612 const struct pid_entry *p, *last;
1614 error = ERR_PTR(-ENOENT);
1621 * Yes, it does not scale. And it should not. Don't add
1622 * new entries into /proc/<tgid>/ without very good reasons.
1624 last = &ents[nents - 1];
1625 for (p = ents; p <= last; p++) {
1626 if (p->len != dentry->d_name.len)
1628 if (!memcmp(dentry->d_name.name, p->name, p->len))
1634 error = proc_pident_instantiate(dir, dentry, task, p);
1636 put_task_struct(task);
1641 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1642 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1644 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1645 proc_pident_instantiate, task, p);
1648 static int proc_pident_readdir(struct file *filp,
1649 void *dirent, filldir_t filldir,
1650 const struct pid_entry *ents, unsigned int nents)
1654 struct dentry *dentry = filp->f_path.dentry;
1655 struct inode *inode = dentry->d_inode;
1656 struct task_struct *task = get_proc_task(inode);
1657 const struct pid_entry *p, *last;
1671 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1677 ino = parent_ino(dentry);
1678 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1690 last = &ents[nents - 1];
1692 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1701 put_task_struct(task);
1706 #ifdef CONFIG_SECURITY
1707 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1708 size_t count, loff_t *ppos)
1710 struct inode * inode = file->f_path.dentry->d_inode;
1713 struct task_struct *task = get_proc_task(inode);
1718 length = security_getprocattr(task,
1719 (char*)file->f_path.dentry->d_name.name,
1721 put_task_struct(task);
1723 length = simple_read_from_buffer(buf, count, ppos, p, length);
1728 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1729 size_t count, loff_t *ppos)
1731 struct inode * inode = file->f_path.dentry->d_inode;
1734 struct task_struct *task = get_proc_task(inode);
1739 if (count > PAGE_SIZE)
1742 /* No partial writes. */
1748 page = (char*)__get_free_page(GFP_USER);
1753 if (copy_from_user(page, buf, count))
1756 length = security_setprocattr(task,
1757 (char*)file->f_path.dentry->d_name.name,
1758 (void*)page, count);
1760 free_page((unsigned long) page);
1762 put_task_struct(task);
1767 static const struct file_operations proc_pid_attr_operations = {
1768 .read = proc_pid_attr_read,
1769 .write = proc_pid_attr_write,
1772 static const struct pid_entry attr_dir_stuff[] = {
1773 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1774 REG("prev", S_IRUGO, pid_attr),
1775 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1776 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1777 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1778 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1781 static int proc_attr_dir_readdir(struct file * filp,
1782 void * dirent, filldir_t filldir)
1784 return proc_pident_readdir(filp,dirent,filldir,
1785 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1788 static const struct file_operations proc_attr_dir_operations = {
1789 .read = generic_read_dir,
1790 .readdir = proc_attr_dir_readdir,
1793 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1794 struct dentry *dentry, struct nameidata *nd)
1796 return proc_pident_lookup(dir, dentry,
1797 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1800 static const struct inode_operations proc_attr_dir_inode_operations = {
1801 .lookup = proc_attr_dir_lookup,
1802 .getattr = pid_getattr,
1803 .setattr = proc_setattr,
1811 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1814 char tmp[PROC_NUMBUF];
1815 sprintf(tmp, "%d", current->tgid);
1816 return vfs_readlink(dentry,buffer,buflen,tmp);
1819 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1821 char tmp[PROC_NUMBUF];
1822 sprintf(tmp, "%d", current->tgid);
1823 return ERR_PTR(vfs_follow_link(nd,tmp));
1826 static const struct inode_operations proc_self_inode_operations = {
1827 .readlink = proc_self_readlink,
1828 .follow_link = proc_self_follow_link,
1834 * These are the directory entries in the root directory of /proc
1835 * that properly belong to the /proc filesystem, as they describe
1836 * describe something that is process related.
1838 static const struct pid_entry proc_base_stuff[] = {
1839 NOD("self", S_IFLNK|S_IRWXUGO,
1840 &proc_self_inode_operations, NULL, {}),
1844 * Exceptional case: normally we are not allowed to unhash a busy
1845 * directory. In this case, however, we can do it - no aliasing problems
1846 * due to the way we treat inodes.
1848 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1850 struct inode *inode = dentry->d_inode;
1851 struct task_struct *task = get_proc_task(inode);
1853 put_task_struct(task);
1860 static struct dentry_operations proc_base_dentry_operations =
1862 .d_revalidate = proc_base_revalidate,
1863 .d_delete = pid_delete_dentry,
1866 static struct dentry *proc_base_instantiate(struct inode *dir,
1867 struct dentry *dentry, struct task_struct *task, const void *ptr)
1869 const struct pid_entry *p = ptr;
1870 struct inode *inode;
1871 struct proc_inode *ei;
1872 struct dentry *error = ERR_PTR(-EINVAL);
1874 /* Allocate the inode */
1875 error = ERR_PTR(-ENOMEM);
1876 inode = new_inode(dir->i_sb);
1880 /* Initialize the inode */
1882 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1885 * grab the reference to the task.
1887 ei->pid = get_task_pid(task, PIDTYPE_PID);
1893 inode->i_mode = p->mode;
1894 if (S_ISDIR(inode->i_mode))
1896 if (S_ISLNK(inode->i_mode))
1899 inode->i_op = p->iop;
1901 inode->i_fop = p->fop;
1903 dentry->d_op = &proc_base_dentry_operations;
1904 d_add(dentry, inode);
1913 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
1915 struct dentry *error;
1916 struct task_struct *task = get_proc_task(dir);
1917 const struct pid_entry *p, *last;
1919 error = ERR_PTR(-ENOENT);
1924 /* Lookup the directory entry */
1925 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
1926 for (p = proc_base_stuff; p <= last; p++) {
1927 if (p->len != dentry->d_name.len)
1929 if (!memcmp(dentry->d_name.name, p->name, p->len))
1935 error = proc_base_instantiate(dir, dentry, task, p);
1938 put_task_struct(task);
1943 static int proc_base_fill_cache(struct file *filp, void *dirent,
1944 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1946 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1947 proc_base_instantiate, task, p);
1950 #ifdef CONFIG_TASK_IO_ACCOUNTING
1951 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
1953 return sprintf(buffer,
1954 #ifdef CONFIG_TASK_XACCT
1960 "read_bytes: %llu\n"
1961 "write_bytes: %llu\n"
1962 "cancelled_write_bytes: %llu\n",
1963 #ifdef CONFIG_TASK_XACCT
1964 (unsigned long long)task->rchar,
1965 (unsigned long long)task->wchar,
1966 (unsigned long long)task->syscr,
1967 (unsigned long long)task->syscw,
1969 (unsigned long long)task->ioac.read_bytes,
1970 (unsigned long long)task->ioac.write_bytes,
1971 (unsigned long long)task->ioac.cancelled_write_bytes);
1978 static const struct file_operations proc_task_operations;
1979 static const struct inode_operations proc_task_inode_operations;
1981 static const struct pid_entry tgid_base_stuff[] = {
1982 DIR("task", S_IRUGO|S_IXUGO, task),
1983 DIR("fd", S_IRUSR|S_IXUSR, fd),
1984 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
1985 INF("environ", S_IRUSR, pid_environ),
1986 INF("auxv", S_IRUSR, pid_auxv),
1987 INF("status", S_IRUGO, pid_status),
1988 INF("cmdline", S_IRUGO, pid_cmdline),
1989 INF("stat", S_IRUGO, tgid_stat),
1990 INF("statm", S_IRUGO, pid_statm),
1991 REG("maps", S_IRUGO, maps),
1993 REG("numa_maps", S_IRUGO, numa_maps),
1995 REG("mem", S_IRUSR|S_IWUSR, mem),
1996 #ifdef CONFIG_SECCOMP
1997 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2002 REG("mounts", S_IRUGO, mounts),
2003 REG("mountstats", S_IRUSR, mountstats),
2005 REG("clear_refs", S_IWUSR, clear_refs),
2006 REG("smaps", S_IRUGO, smaps),
2008 #ifdef CONFIG_SECURITY
2009 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2011 #ifdef CONFIG_KALLSYMS
2012 INF("wchan", S_IRUGO, pid_wchan),
2014 #ifdef CONFIG_SCHEDSTATS
2015 INF("schedstat", S_IRUGO, pid_schedstat),
2017 #ifdef CONFIG_CPUSETS
2018 REG("cpuset", S_IRUGO, cpuset),
2020 INF("oom_score", S_IRUGO, oom_score),
2021 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2022 #ifdef CONFIG_AUDITSYSCALL
2023 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2025 #ifdef CONFIG_FAULT_INJECTION
2026 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2028 #ifdef CONFIG_TASK_IO_ACCOUNTING
2029 INF("io", S_IRUGO, pid_io_accounting),
2033 static int proc_tgid_base_readdir(struct file * filp,
2034 void * dirent, filldir_t filldir)
2036 return proc_pident_readdir(filp,dirent,filldir,
2037 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2040 static const struct file_operations proc_tgid_base_operations = {
2041 .read = generic_read_dir,
2042 .readdir = proc_tgid_base_readdir,
2045 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2046 return proc_pident_lookup(dir, dentry,
2047 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2050 static const struct inode_operations proc_tgid_base_inode_operations = {
2051 .lookup = proc_tgid_base_lookup,
2052 .getattr = pid_getattr,
2053 .setattr = proc_setattr,
2057 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2059 * @task: task that should be flushed.
2061 * Looks in the dcache for
2063 * /proc/@tgid/task/@pid
2064 * if either directory is present flushes it and all of it'ts children
2067 * It is safe and reasonable to cache /proc entries for a task until
2068 * that task exits. After that they just clog up the dcache with
2069 * useless entries, possibly causing useful dcache entries to be
2070 * flushed instead. This routine is proved to flush those useless
2071 * dcache entries at process exit time.
2073 * NOTE: This routine is just an optimization so it does not guarantee
2074 * that no dcache entries will exist at process exit time it
2075 * just makes it very unlikely that any will persist.
2077 void proc_flush_task(struct task_struct *task)
2079 struct dentry *dentry, *leader, *dir;
2080 char buf[PROC_NUMBUF];
2084 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2085 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2087 shrink_dcache_parent(dentry);
2092 if (thread_group_leader(task))
2096 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2097 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2102 name.len = strlen(name.name);
2103 dir = d_hash_and_lookup(leader, &name);
2105 goto out_put_leader;
2108 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2109 dentry = d_hash_and_lookup(dir, &name);
2111 shrink_dcache_parent(dentry);
2123 static struct dentry *proc_pid_instantiate(struct inode *dir,
2124 struct dentry * dentry,
2125 struct task_struct *task, const void *ptr)
2127 struct dentry *error = ERR_PTR(-ENOENT);
2128 struct inode *inode;
2130 inode = proc_pid_make_inode(dir->i_sb, task);
2134 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2135 inode->i_op = &proc_tgid_base_inode_operations;
2136 inode->i_fop = &proc_tgid_base_operations;
2137 inode->i_flags|=S_IMMUTABLE;
2139 #ifdef CONFIG_SECURITY
2140 inode->i_nlink += 1;
2143 dentry->d_op = &pid_dentry_operations;
2145 d_add(dentry, inode);
2146 /* Close the race of the process dying before we return the dentry */
2147 if (pid_revalidate(dentry, NULL))
2153 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2155 struct dentry *result = ERR_PTR(-ENOENT);
2156 struct task_struct *task;
2159 result = proc_base_lookup(dir, dentry);
2160 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2163 tgid = name_to_int(dentry);
2168 task = find_task_by_pid(tgid);
2170 get_task_struct(task);
2175 result = proc_pid_instantiate(dir, dentry, task, NULL);
2176 put_task_struct(task);
2182 * Find the first task with tgid >= tgid
2185 static struct task_struct *next_tgid(unsigned int tgid)
2187 struct task_struct *task;
2193 pid = find_ge_pid(tgid);
2196 task = pid_task(pid, PIDTYPE_PID);
2197 /* What we to know is if the pid we have find is the
2198 * pid of a thread_group_leader. Testing for task
2199 * being a thread_group_leader is the obvious thing
2200 * todo but there is a window when it fails, due to
2201 * the pid transfer logic in de_thread.
2203 * So we perform the straight forward test of seeing
2204 * if the pid we have found is the pid of a thread
2205 * group leader, and don't worry if the task we have
2206 * found doesn't happen to be a thread group leader.
2207 * As we don't care in the case of readdir.
2209 if (!task || !has_group_leader_pid(task))
2211 get_task_struct(task);
2217 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2219 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2220 struct task_struct *task, int tgid)
2222 char name[PROC_NUMBUF];
2223 int len = snprintf(name, sizeof(name), "%d", tgid);
2224 return proc_fill_cache(filp, dirent, filldir, name, len,
2225 proc_pid_instantiate, task, NULL);
2228 /* for the /proc/ directory itself, after non-process stuff has been done */
2229 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2231 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2232 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2233 struct task_struct *task;
2239 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2240 const struct pid_entry *p = &proc_base_stuff[nr];
2241 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2245 tgid = filp->f_pos - TGID_OFFSET;
2246 for (task = next_tgid(tgid);
2248 put_task_struct(task), task = next_tgid(tgid + 1)) {
2250 filp->f_pos = tgid + TGID_OFFSET;
2251 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2252 put_task_struct(task);
2256 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2258 put_task_struct(reaper);
2266 static const struct pid_entry tid_base_stuff[] = {
2267 DIR("fd", S_IRUSR|S_IXUSR, fd),
2268 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2269 INF("environ", S_IRUSR, pid_environ),
2270 INF("auxv", S_IRUSR, pid_auxv),
2271 INF("status", S_IRUGO, pid_status),
2272 INF("cmdline", S_IRUGO, pid_cmdline),
2273 INF("stat", S_IRUGO, tid_stat),
2274 INF("statm", S_IRUGO, pid_statm),
2275 REG("maps", S_IRUGO, maps),
2277 REG("numa_maps", S_IRUGO, numa_maps),
2279 REG("mem", S_IRUSR|S_IWUSR, mem),
2280 #ifdef CONFIG_SECCOMP
2281 REG("seccomp", S_IRUSR|S_IWUSR, seccomp),
2286 REG("mounts", S_IRUGO, mounts),
2288 REG("clear_refs", S_IWUSR, clear_refs),
2289 REG("smaps", S_IRUGO, smaps),
2291 #ifdef CONFIG_SECURITY
2292 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2294 #ifdef CONFIG_KALLSYMS
2295 INF("wchan", S_IRUGO, pid_wchan),
2297 #ifdef CONFIG_SCHEDSTATS
2298 INF("schedstat", S_IRUGO, pid_schedstat),
2300 #ifdef CONFIG_CPUSETS
2301 REG("cpuset", S_IRUGO, cpuset),
2303 INF("oom_score", S_IRUGO, oom_score),
2304 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2305 #ifdef CONFIG_AUDITSYSCALL
2306 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2308 #ifdef CONFIG_FAULT_INJECTION
2309 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2313 static int proc_tid_base_readdir(struct file * filp,
2314 void * dirent, filldir_t filldir)
2316 return proc_pident_readdir(filp,dirent,filldir,
2317 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2320 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2321 return proc_pident_lookup(dir, dentry,
2322 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2325 static const struct file_operations proc_tid_base_operations = {
2326 .read = generic_read_dir,
2327 .readdir = proc_tid_base_readdir,
2330 static const struct inode_operations proc_tid_base_inode_operations = {
2331 .lookup = proc_tid_base_lookup,
2332 .getattr = pid_getattr,
2333 .setattr = proc_setattr,
2336 static struct dentry *proc_task_instantiate(struct inode *dir,
2337 struct dentry *dentry, struct task_struct *task, const void *ptr)
2339 struct dentry *error = ERR_PTR(-ENOENT);
2340 struct inode *inode;
2341 inode = proc_pid_make_inode(dir->i_sb, task);
2345 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2346 inode->i_op = &proc_tid_base_inode_operations;
2347 inode->i_fop = &proc_tid_base_operations;
2348 inode->i_flags|=S_IMMUTABLE;
2350 #ifdef CONFIG_SECURITY
2351 inode->i_nlink += 1;
2354 dentry->d_op = &pid_dentry_operations;
2356 d_add(dentry, inode);
2357 /* Close the race of the process dying before we return the dentry */
2358 if (pid_revalidate(dentry, NULL))
2364 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2366 struct dentry *result = ERR_PTR(-ENOENT);
2367 struct task_struct *task;
2368 struct task_struct *leader = get_proc_task(dir);
2374 tid = name_to_int(dentry);
2379 task = find_task_by_pid(tid);
2381 get_task_struct(task);
2385 if (leader->tgid != task->tgid)
2388 result = proc_task_instantiate(dir, dentry, task, NULL);
2390 put_task_struct(task);
2392 put_task_struct(leader);
2398 * Find the first tid of a thread group to return to user space.
2400 * Usually this is just the thread group leader, but if the users
2401 * buffer was too small or there was a seek into the middle of the
2402 * directory we have more work todo.
2404 * In the case of a short read we start with find_task_by_pid.
2406 * In the case of a seek we start with the leader and walk nr
2409 static struct task_struct *first_tid(struct task_struct *leader,
2412 struct task_struct *pos;
2415 /* Attempt to start with the pid of a thread */
2416 if (tid && (nr > 0)) {
2417 pos = find_task_by_pid(tid);
2418 if (pos && (pos->group_leader == leader))
2422 /* If nr exceeds the number of threads there is nothing todo */
2424 if (nr && nr >= get_nr_threads(leader))
2427 /* If we haven't found our starting place yet start
2428 * with the leader and walk nr threads forward.
2430 for (pos = leader; nr > 0; --nr) {
2431 pos = next_thread(pos);
2432 if (pos == leader) {
2438 get_task_struct(pos);
2445 * Find the next thread in the thread list.
2446 * Return NULL if there is an error or no next thread.
2448 * The reference to the input task_struct is released.
2450 static struct task_struct *next_tid(struct task_struct *start)
2452 struct task_struct *pos = NULL;
2454 if (pid_alive(start)) {
2455 pos = next_thread(start);
2456 if (thread_group_leader(pos))
2459 get_task_struct(pos);
2462 put_task_struct(start);
2466 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2467 struct task_struct *task, int tid)
2469 char name[PROC_NUMBUF];
2470 int len = snprintf(name, sizeof(name), "%d", tid);
2471 return proc_fill_cache(filp, dirent, filldir, name, len,
2472 proc_task_instantiate, task, NULL);
2475 /* for the /proc/TGID/task/ directories */
2476 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2478 struct dentry *dentry = filp->f_path.dentry;
2479 struct inode *inode = dentry->d_inode;
2480 struct task_struct *leader = NULL;
2481 struct task_struct *task;
2482 int retval = -ENOENT;
2485 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2487 task = get_proc_task(inode);
2491 if (pid_alive(task)) {
2492 leader = task->group_leader;
2493 get_task_struct(leader);
2496 put_task_struct(task);
2504 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2509 ino = parent_ino(dentry);
2510 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2516 /* f_version caches the tgid value that the last readdir call couldn't
2517 * return. lseek aka telldir automagically resets f_version to 0.
2519 tid = filp->f_version;
2520 filp->f_version = 0;
2521 for (task = first_tid(leader, tid, pos - 2);
2523 task = next_tid(task), pos++) {
2525 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2526 /* returning this tgid failed, save it as the first
2527 * pid for the next readir call */
2528 filp->f_version = tid;
2529 put_task_struct(task);
2535 put_task_struct(leader);
2540 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2542 struct inode *inode = dentry->d_inode;
2543 struct task_struct *p = get_proc_task(inode);
2544 generic_fillattr(inode, stat);
2548 stat->nlink += get_nr_threads(p);
2556 static const struct inode_operations proc_task_inode_operations = {
2557 .lookup = proc_task_lookup,
2558 .getattr = proc_task_getattr,
2559 .setattr = proc_setattr,
2562 static const struct file_operations proc_task_operations = {
2563 .read = generic_read_dir,
2564 .readdir = proc_task_readdir,