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/cgroup.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>
76 #include <linux/elf.h>
80 * Implementing inode permission operations in /proc is almost
81 * certainly an error. Permission checks need to happen during
82 * each system call not at open time. The reason is that most of
83 * what we wish to check for permissions in /proc varies at runtime.
85 * The classic example of a problem is opening file descriptors
86 * in /proc for a task before it execs a suid executable.
90 /* Worst case buffer size needed for holding an integer. */
91 #define PROC_NUMBUF 13
97 const struct inode_operations *iop;
98 const struct file_operations *fop;
102 #define NOD(NAME, MODE, IOP, FOP, OP) { \
104 .len = sizeof(NAME) - 1, \
111 #define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 #define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
119 #define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
121 &proc_##OTYPE##_operations, {})
122 #define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
128 EXPORT_SYMBOL(maps_protect);
130 static struct fs_struct *get_fs_struct(struct task_struct *task)
132 struct fs_struct *fs;
136 atomic_inc(&fs->count);
141 static int get_nr_threads(struct task_struct *tsk)
143 /* Must be called with the rcu_read_lock held */
147 if (lock_task_sighand(tsk, &flags)) {
148 count = atomic_read(&tsk->signal->count);
149 unlock_task_sighand(tsk, &flags);
154 static int proc_cwd_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
156 struct task_struct *task = get_proc_task(inode);
157 struct fs_struct *fs = NULL;
158 int result = -ENOENT;
161 fs = get_fs_struct(task);
162 put_task_struct(task);
165 read_lock(&fs->lock);
166 *mnt = mntget(fs->pwdmnt);
167 *dentry = dget(fs->pwd);
168 read_unlock(&fs->lock);
175 static int proc_root_link(struct inode *inode, struct dentry **dentry, struct vfsmount **mnt)
177 struct task_struct *task = get_proc_task(inode);
178 struct fs_struct *fs = NULL;
179 int result = -ENOENT;
182 fs = get_fs_struct(task);
183 put_task_struct(task);
186 read_lock(&fs->lock);
187 *mnt = mntget(fs->rootmnt);
188 *dentry = dget(fs->root);
189 read_unlock(&fs->lock);
196 #define MAY_PTRACE(task) \
197 (task == current || \
198 (task->parent == current && \
199 (task->ptrace & PT_PTRACED) && \
200 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
201 security_ptrace(current,task) == 0))
203 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
207 struct mm_struct *mm = get_task_mm(task);
211 goto out_mm; /* Shh! No looking before we're done */
213 len = mm->arg_end - mm->arg_start;
218 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
223 len = strnlen(buffer, res);
227 len = mm->env_end - mm->env_start;
228 if (len > PAGE_SIZE - res)
229 len = PAGE_SIZE - res;
230 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
231 res = strnlen(buffer, res);
240 static int proc_pid_auxv(struct task_struct *task, char *buffer)
243 struct mm_struct *mm = get_task_mm(task);
245 unsigned int nwords = 0;
248 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
249 res = nwords * sizeof(mm->saved_auxv[0]);
252 memcpy(buffer, mm->saved_auxv, res);
259 #ifdef CONFIG_KALLSYMS
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
264 static int proc_pid_wchan(struct task_struct *task, char *buffer)
267 char symname[KSYM_NAME_LEN];
269 wchan = get_wchan(task);
271 if (lookup_symbol_name(wchan, symname) < 0)
272 return sprintf(buffer, "%lu", wchan);
274 return sprintf(buffer, "%s", symname);
276 #endif /* CONFIG_KALLSYMS */
278 #ifdef CONFIG_SCHEDSTATS
280 * Provides /proc/PID/schedstat
282 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
284 return sprintf(buffer, "%llu %llu %lu\n",
285 task->sched_info.cpu_time,
286 task->sched_info.run_delay,
287 task->sched_info.pcount);
291 /* The badness from the OOM killer */
292 unsigned long badness(struct task_struct *p, unsigned long uptime);
293 static int proc_oom_score(struct task_struct *task, char *buffer)
295 unsigned long points;
296 struct timespec uptime;
298 do_posix_clock_monotonic_gettime(&uptime);
299 read_lock(&tasklist_lock);
300 points = badness(task, uptime.tv_sec);
301 read_unlock(&tasklist_lock);
302 return sprintf(buffer, "%lu\n", points);
305 /************************************************************************/
306 /* Here the fs part begins */
307 /************************************************************************/
309 /* permission checks */
310 static int proc_fd_access_allowed(struct inode *inode)
312 struct task_struct *task;
314 /* Allow access to a task's file descriptors if it is us or we
315 * may use ptrace attach to the process and find out that
318 task = get_proc_task(inode);
320 allowed = ptrace_may_attach(task);
321 put_task_struct(task);
326 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
329 struct inode *inode = dentry->d_inode;
331 if (attr->ia_valid & ATTR_MODE)
334 error = inode_change_ok(inode, attr);
336 error = inode_setattr(inode, attr);
340 static const struct inode_operations proc_def_inode_operations = {
341 .setattr = proc_setattr,
344 extern struct seq_operations mounts_op;
350 static int mounts_open(struct inode *inode, struct file *file)
352 struct task_struct *task = get_proc_task(inode);
353 struct mnt_namespace *ns = NULL;
354 struct proc_mounts *p;
360 ns = task->nsproxy->mnt_ns;
365 put_task_struct(task);
370 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
372 file->private_data = &p->m;
373 ret = seq_open(file, &mounts_op);
376 p->event = ns->event;
386 static int mounts_release(struct inode *inode, struct file *file)
388 struct seq_file *m = file->private_data;
389 struct mnt_namespace *ns = m->private;
391 return seq_release(inode, file);
394 static unsigned mounts_poll(struct file *file, poll_table *wait)
396 struct proc_mounts *p = file->private_data;
397 struct mnt_namespace *ns = p->m.private;
400 poll_wait(file, &ns->poll, wait);
402 spin_lock(&vfsmount_lock);
403 if (p->event != ns->event) {
404 p->event = ns->event;
407 spin_unlock(&vfsmount_lock);
412 static const struct file_operations proc_mounts_operations = {
416 .release = mounts_release,
420 extern struct seq_operations mountstats_op;
421 static int mountstats_open(struct inode *inode, struct file *file)
423 int ret = seq_open(file, &mountstats_op);
426 struct seq_file *m = file->private_data;
427 struct mnt_namespace *mnt_ns = NULL;
428 struct task_struct *task = get_proc_task(inode);
433 mnt_ns = task->nsproxy->mnt_ns;
437 put_task_struct(task);
443 seq_release(inode, file);
450 static const struct file_operations proc_mountstats_operations = {
451 .open = mountstats_open,
454 .release = mounts_release,
457 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
459 static ssize_t proc_info_read(struct file * file, char __user * buf,
460 size_t count, loff_t *ppos)
462 struct inode * inode = file->f_path.dentry->d_inode;
465 struct task_struct *task = get_proc_task(inode);
471 if (count > PROC_BLOCK_SIZE)
472 count = PROC_BLOCK_SIZE;
475 if (!(page = __get_free_page(GFP_TEMPORARY)))
478 length = PROC_I(inode)->op.proc_read(task, (char*)page);
481 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
484 put_task_struct(task);
489 static const struct file_operations proc_info_file_operations = {
490 .read = proc_info_read,
493 static int mem_open(struct inode* inode, struct file* file)
495 file->private_data = (void*)((long)current->self_exec_id);
499 static ssize_t mem_read(struct file * file, char __user * buf,
500 size_t count, loff_t *ppos)
502 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
504 unsigned long src = *ppos;
506 struct mm_struct *mm;
511 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
515 page = (char *)__get_free_page(GFP_TEMPORARY);
521 mm = get_task_mm(task);
527 if (file->private_data != (void*)((long)current->self_exec_id))
533 int this_len, retval;
535 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
536 retval = access_process_vm(task, src, page, this_len, 0);
537 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
543 if (copy_to_user(buf, page, retval)) {
558 free_page((unsigned long) page);
560 put_task_struct(task);
565 #define mem_write NULL
568 /* This is a security hazard */
569 static ssize_t mem_write(struct file * file, const char __user *buf,
570 size_t count, loff_t *ppos)
574 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
575 unsigned long dst = *ppos;
581 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
585 page = (char *)__get_free_page(GFP_TEMPORARY);
591 int this_len, retval;
593 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
594 if (copy_from_user(page, buf, this_len)) {
598 retval = access_process_vm(task, dst, page, this_len, 1);
610 free_page((unsigned long) page);
612 put_task_struct(task);
618 static loff_t mem_lseek(struct file * file, loff_t offset, int orig)
622 file->f_pos = offset;
625 file->f_pos += offset;
630 force_successful_syscall_return();
634 static const struct file_operations proc_mem_operations = {
641 static ssize_t environ_read(struct file *file, char __user *buf,
642 size_t count, loff_t *ppos)
644 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
646 unsigned long src = *ppos;
648 struct mm_struct *mm;
653 if (!ptrace_may_attach(task))
657 page = (char *)__get_free_page(GFP_TEMPORARY);
663 mm = get_task_mm(task);
668 int this_len, retval, max_len;
670 this_len = mm->env_end - (mm->env_start + src);
675 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
676 this_len = (this_len > max_len) ? max_len : this_len;
678 retval = access_process_vm(task, (mm->env_start + src),
686 if (copy_to_user(buf, page, retval)) {
700 free_page((unsigned long) page);
702 put_task_struct(task);
707 static const struct file_operations proc_environ_operations = {
708 .read = environ_read,
711 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
712 size_t count, loff_t *ppos)
714 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
715 char buffer[PROC_NUMBUF];
721 oom_adjust = task->oomkilladj;
722 put_task_struct(task);
724 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
726 return simple_read_from_buffer(buf, count, ppos, buffer, len);
729 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
730 size_t count, loff_t *ppos)
732 struct task_struct *task;
733 char buffer[PROC_NUMBUF], *end;
736 memset(buffer, 0, sizeof(buffer));
737 if (count > sizeof(buffer) - 1)
738 count = sizeof(buffer) - 1;
739 if (copy_from_user(buffer, buf, count))
741 oom_adjust = simple_strtol(buffer, &end, 0);
742 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
743 oom_adjust != OOM_DISABLE)
747 task = get_proc_task(file->f_path.dentry->d_inode);
750 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
751 put_task_struct(task);
754 task->oomkilladj = oom_adjust;
755 put_task_struct(task);
756 if (end - buffer == 0)
761 static const struct file_operations proc_oom_adjust_operations = {
762 .read = oom_adjust_read,
763 .write = oom_adjust_write,
767 static ssize_t clear_refs_write(struct file *file, const char __user *buf,
768 size_t count, loff_t *ppos)
770 struct task_struct *task;
771 char buffer[PROC_NUMBUF], *end;
772 struct mm_struct *mm;
774 memset(buffer, 0, sizeof(buffer));
775 if (count > sizeof(buffer) - 1)
776 count = sizeof(buffer) - 1;
777 if (copy_from_user(buffer, buf, count))
779 if (!simple_strtol(buffer, &end, 0))
783 task = get_proc_task(file->f_path.dentry->d_inode);
786 mm = get_task_mm(task);
791 put_task_struct(task);
792 if (end - buffer == 0)
797 static struct file_operations proc_clear_refs_operations = {
798 .write = clear_refs_write,
802 #ifdef CONFIG_AUDITSYSCALL
804 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
805 size_t count, loff_t *ppos)
807 struct inode * inode = file->f_path.dentry->d_inode;
808 struct task_struct *task = get_proc_task(inode);
810 char tmpbuf[TMPBUFLEN];
814 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
815 audit_get_loginuid(task->audit_context));
816 put_task_struct(task);
817 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
820 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
821 size_t count, loff_t *ppos)
823 struct inode * inode = file->f_path.dentry->d_inode;
828 if (!capable(CAP_AUDIT_CONTROL))
831 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
834 if (count >= PAGE_SIZE)
835 count = PAGE_SIZE - 1;
838 /* No partial writes. */
841 page = (char*)__get_free_page(GFP_TEMPORARY);
845 if (copy_from_user(page, buf, count))
849 loginuid = simple_strtoul(page, &tmp, 10);
855 length = audit_set_loginuid(current, loginuid);
856 if (likely(length == 0))
860 free_page((unsigned long) page);
864 static const struct file_operations proc_loginuid_operations = {
865 .read = proc_loginuid_read,
866 .write = proc_loginuid_write,
870 #ifdef CONFIG_FAULT_INJECTION
871 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
872 size_t count, loff_t *ppos)
874 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
875 char buffer[PROC_NUMBUF];
881 make_it_fail = task->make_it_fail;
882 put_task_struct(task);
884 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
886 return simple_read_from_buffer(buf, count, ppos, buffer, len);
889 static ssize_t proc_fault_inject_write(struct file * file,
890 const char __user * buf, size_t count, loff_t *ppos)
892 struct task_struct *task;
893 char buffer[PROC_NUMBUF], *end;
896 if (!capable(CAP_SYS_RESOURCE))
898 memset(buffer, 0, sizeof(buffer));
899 if (count > sizeof(buffer) - 1)
900 count = sizeof(buffer) - 1;
901 if (copy_from_user(buffer, buf, count))
903 make_it_fail = simple_strtol(buffer, &end, 0);
906 task = get_proc_task(file->f_dentry->d_inode);
909 task->make_it_fail = make_it_fail;
910 put_task_struct(task);
911 if (end - buffer == 0)
916 static const struct file_operations proc_fault_inject_operations = {
917 .read = proc_fault_inject_read,
918 .write = proc_fault_inject_write,
922 #ifdef CONFIG_SCHED_DEBUG
924 * Print out various scheduling related per-task fields:
926 static int sched_show(struct seq_file *m, void *v)
928 struct inode *inode = m->private;
929 struct task_struct *p;
933 p = get_proc_task(inode);
936 proc_sched_show_task(p, m);
944 sched_write(struct file *file, const char __user *buf,
945 size_t count, loff_t *offset)
947 struct inode *inode = file->f_path.dentry->d_inode;
948 struct task_struct *p;
952 p = get_proc_task(inode);
955 proc_sched_set_task(p);
962 static int sched_open(struct inode *inode, struct file *filp)
966 ret = single_open(filp, sched_show, NULL);
968 struct seq_file *m = filp->private_data;
975 static const struct file_operations proc_pid_sched_operations = {
978 .write = sched_write,
980 .release = single_release,
985 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
987 struct inode *inode = dentry->d_inode;
990 /* We don't need a base pointer in the /proc filesystem */
993 /* Are we allowed to snoop on the tasks file descriptors? */
994 if (!proc_fd_access_allowed(inode))
997 error = PROC_I(inode)->op.proc_get_link(inode, &nd->dentry, &nd->mnt);
998 nd->last_type = LAST_BIND;
1000 return ERR_PTR(error);
1003 static int do_proc_readlink(struct dentry *dentry, struct vfsmount *mnt,
1004 char __user *buffer, int buflen)
1006 struct inode * inode;
1007 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1014 inode = dentry->d_inode;
1015 path = d_path(dentry, mnt, tmp, PAGE_SIZE);
1016 len = PTR_ERR(path);
1019 len = tmp + PAGE_SIZE - 1 - path;
1023 if (copy_to_user(buffer, path, len))
1026 free_page((unsigned long)tmp);
1030 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1032 int error = -EACCES;
1033 struct inode *inode = dentry->d_inode;
1035 struct vfsmount *mnt = NULL;
1037 /* Are we allowed to snoop on the tasks file descriptors? */
1038 if (!proc_fd_access_allowed(inode))
1041 error = PROC_I(inode)->op.proc_get_link(inode, &de, &mnt);
1045 error = do_proc_readlink(de, mnt, buffer, buflen);
1052 static const struct inode_operations proc_pid_link_inode_operations = {
1053 .readlink = proc_pid_readlink,
1054 .follow_link = proc_pid_follow_link,
1055 .setattr = proc_setattr,
1059 /* building an inode */
1061 static int task_dumpable(struct task_struct *task)
1064 struct mm_struct *mm;
1069 dumpable = get_dumpable(mm);
1077 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1079 struct inode * inode;
1080 struct proc_inode *ei;
1082 /* We need a new inode */
1084 inode = new_inode(sb);
1090 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1091 inode->i_op = &proc_def_inode_operations;
1094 * grab the reference to task.
1096 ei->pid = get_task_pid(task, PIDTYPE_PID);
1102 if (task_dumpable(task)) {
1103 inode->i_uid = task->euid;
1104 inode->i_gid = task->egid;
1106 security_task_to_inode(task, inode);
1116 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1118 struct inode *inode = dentry->d_inode;
1119 struct task_struct *task;
1120 generic_fillattr(inode, stat);
1125 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1127 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1128 task_dumpable(task)) {
1129 stat->uid = task->euid;
1130 stat->gid = task->egid;
1140 * Exceptional case: normally we are not allowed to unhash a busy
1141 * directory. In this case, however, we can do it - no aliasing problems
1142 * due to the way we treat inodes.
1144 * Rewrite the inode's ownerships here because the owning task may have
1145 * performed a setuid(), etc.
1147 * Before the /proc/pid/status file was created the only way to read
1148 * the effective uid of a /process was to stat /proc/pid. Reading
1149 * /proc/pid/status is slow enough that procps and other packages
1150 * kept stating /proc/pid. To keep the rules in /proc simple I have
1151 * made this apply to all per process world readable and executable
1154 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1156 struct inode *inode = dentry->d_inode;
1157 struct task_struct *task = get_proc_task(inode);
1159 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1160 task_dumpable(task)) {
1161 inode->i_uid = task->euid;
1162 inode->i_gid = task->egid;
1167 inode->i_mode &= ~(S_ISUID | S_ISGID);
1168 security_task_to_inode(task, inode);
1169 put_task_struct(task);
1176 static int pid_delete_dentry(struct dentry * dentry)
1178 /* Is the task we represent dead?
1179 * If so, then don't put the dentry on the lru list,
1180 * kill it immediately.
1182 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1185 static struct dentry_operations pid_dentry_operations =
1187 .d_revalidate = pid_revalidate,
1188 .d_delete = pid_delete_dentry,
1193 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1194 struct task_struct *, const void *);
1197 * Fill a directory entry.
1199 * If possible create the dcache entry and derive our inode number and
1200 * file type from dcache entry.
1202 * Since all of the proc inode numbers are dynamically generated, the inode
1203 * numbers do not exist until the inode is cache. This means creating the
1204 * the dcache entry in readdir is necessary to keep the inode numbers
1205 * reported by readdir in sync with the inode numbers reported
1208 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1209 char *name, int len,
1210 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1212 struct dentry *child, *dir = filp->f_path.dentry;
1213 struct inode *inode;
1216 unsigned type = DT_UNKNOWN;
1220 qname.hash = full_name_hash(name, len);
1222 child = d_lookup(dir, &qname);
1225 new = d_alloc(dir, &qname);
1227 child = instantiate(dir->d_inode, new, task, ptr);
1234 if (!child || IS_ERR(child) || !child->d_inode)
1235 goto end_instantiate;
1236 inode = child->d_inode;
1239 type = inode->i_mode >> 12;
1244 ino = find_inode_number(dir, &qname);
1247 return filldir(dirent, name, len, filp->f_pos, ino, type);
1250 static unsigned name_to_int(struct dentry *dentry)
1252 const char *name = dentry->d_name.name;
1253 int len = dentry->d_name.len;
1256 if (len > 1 && *name == '0')
1259 unsigned c = *name++ - '0';
1262 if (n >= (~0U-9)/10)
1272 #define PROC_FDINFO_MAX 64
1274 static int proc_fd_info(struct inode *inode, struct dentry **dentry,
1275 struct vfsmount **mnt, char *info)
1277 struct task_struct *task = get_proc_task(inode);
1278 struct files_struct *files = NULL;
1280 int fd = proc_fd(inode);
1283 files = get_files_struct(task);
1284 put_task_struct(task);
1288 * We are not taking a ref to the file structure, so we must
1291 spin_lock(&files->file_lock);
1292 file = fcheck_files(files, fd);
1295 *mnt = mntget(file->f_path.mnt);
1297 *dentry = dget(file->f_path.dentry);
1299 snprintf(info, PROC_FDINFO_MAX,
1302 (long long) file->f_pos,
1304 spin_unlock(&files->file_lock);
1305 put_files_struct(files);
1308 spin_unlock(&files->file_lock);
1309 put_files_struct(files);
1314 static int proc_fd_link(struct inode *inode, struct dentry **dentry,
1315 struct vfsmount **mnt)
1317 return proc_fd_info(inode, dentry, mnt, NULL);
1320 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1322 struct inode *inode = dentry->d_inode;
1323 struct task_struct *task = get_proc_task(inode);
1324 int fd = proc_fd(inode);
1325 struct files_struct *files;
1328 files = get_files_struct(task);
1331 if (fcheck_files(files, fd)) {
1333 put_files_struct(files);
1334 if (task_dumpable(task)) {
1335 inode->i_uid = task->euid;
1336 inode->i_gid = task->egid;
1341 inode->i_mode &= ~(S_ISUID | S_ISGID);
1342 security_task_to_inode(task, inode);
1343 put_task_struct(task);
1347 put_files_struct(files);
1349 put_task_struct(task);
1355 static struct dentry_operations tid_fd_dentry_operations =
1357 .d_revalidate = tid_fd_revalidate,
1358 .d_delete = pid_delete_dentry,
1361 static struct dentry *proc_fd_instantiate(struct inode *dir,
1362 struct dentry *dentry, struct task_struct *task, const void *ptr)
1364 unsigned fd = *(const unsigned *)ptr;
1366 struct files_struct *files;
1367 struct inode *inode;
1368 struct proc_inode *ei;
1369 struct dentry *error = ERR_PTR(-ENOENT);
1371 inode = proc_pid_make_inode(dir->i_sb, task);
1376 files = get_files_struct(task);
1379 inode->i_mode = S_IFLNK;
1382 * We are not taking a ref to the file structure, so we must
1385 spin_lock(&files->file_lock);
1386 file = fcheck_files(files, fd);
1389 if (file->f_mode & 1)
1390 inode->i_mode |= S_IRUSR | S_IXUSR;
1391 if (file->f_mode & 2)
1392 inode->i_mode |= S_IWUSR | S_IXUSR;
1393 spin_unlock(&files->file_lock);
1394 put_files_struct(files);
1396 inode->i_op = &proc_pid_link_inode_operations;
1398 ei->op.proc_get_link = proc_fd_link;
1399 dentry->d_op = &tid_fd_dentry_operations;
1400 d_add(dentry, inode);
1401 /* Close the race of the process dying before we return the dentry */
1402 if (tid_fd_revalidate(dentry, NULL))
1408 spin_unlock(&files->file_lock);
1409 put_files_struct(files);
1415 static struct dentry *proc_lookupfd_common(struct inode *dir,
1416 struct dentry *dentry,
1417 instantiate_t instantiate)
1419 struct task_struct *task = get_proc_task(dir);
1420 unsigned fd = name_to_int(dentry);
1421 struct dentry *result = ERR_PTR(-ENOENT);
1428 result = instantiate(dir, dentry, task, &fd);
1430 put_task_struct(task);
1435 static int proc_readfd_common(struct file * filp, void * dirent,
1436 filldir_t filldir, instantiate_t instantiate)
1438 struct dentry *dentry = filp->f_path.dentry;
1439 struct inode *inode = dentry->d_inode;
1440 struct task_struct *p = get_proc_task(inode);
1441 unsigned int fd, tid, ino;
1443 struct files_struct * files;
1444 struct fdtable *fdt;
1455 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1459 ino = parent_ino(dentry);
1460 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1464 files = get_files_struct(p);
1468 fdt = files_fdtable(files);
1469 for (fd = filp->f_pos-2;
1471 fd++, filp->f_pos++) {
1472 char name[PROC_NUMBUF];
1475 if (!fcheck_files(files, fd))
1479 len = snprintf(name, sizeof(name), "%d", fd);
1480 if (proc_fill_cache(filp, dirent, filldir,
1481 name, len, instantiate,
1489 put_files_struct(files);
1497 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1498 struct nameidata *nd)
1500 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1503 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1505 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1508 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1509 size_t len, loff_t *ppos)
1511 char tmp[PROC_FDINFO_MAX];
1512 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, NULL, tmp);
1514 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1518 static const struct file_operations proc_fdinfo_file_operations = {
1519 .open = nonseekable_open,
1520 .read = proc_fdinfo_read,
1523 static const struct file_operations proc_fd_operations = {
1524 .read = generic_read_dir,
1525 .readdir = proc_readfd,
1529 * /proc/pid/fd needs a special permission handler so that a process can still
1530 * access /proc/self/fd after it has executed a setuid().
1532 static int proc_fd_permission(struct inode *inode, int mask,
1533 struct nameidata *nd)
1537 rv = generic_permission(inode, mask, NULL);
1540 if (task_pid(current) == proc_pid(inode))
1546 * proc directories can do almost nothing..
1548 static const struct inode_operations proc_fd_inode_operations = {
1549 .lookup = proc_lookupfd,
1550 .permission = proc_fd_permission,
1551 .setattr = proc_setattr,
1554 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1555 struct dentry *dentry, struct task_struct *task, const void *ptr)
1557 unsigned fd = *(unsigned *)ptr;
1558 struct inode *inode;
1559 struct proc_inode *ei;
1560 struct dentry *error = ERR_PTR(-ENOENT);
1562 inode = proc_pid_make_inode(dir->i_sb, task);
1567 inode->i_mode = S_IFREG | S_IRUSR;
1568 inode->i_fop = &proc_fdinfo_file_operations;
1569 dentry->d_op = &tid_fd_dentry_operations;
1570 d_add(dentry, inode);
1571 /* Close the race of the process dying before we return the dentry */
1572 if (tid_fd_revalidate(dentry, NULL))
1579 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1580 struct dentry *dentry,
1581 struct nameidata *nd)
1583 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1586 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1588 return proc_readfd_common(filp, dirent, filldir,
1589 proc_fdinfo_instantiate);
1592 static const struct file_operations proc_fdinfo_operations = {
1593 .read = generic_read_dir,
1594 .readdir = proc_readfdinfo,
1598 * proc directories can do almost nothing..
1600 static const struct inode_operations proc_fdinfo_inode_operations = {
1601 .lookup = proc_lookupfdinfo,
1602 .setattr = proc_setattr,
1606 static struct dentry *proc_pident_instantiate(struct inode *dir,
1607 struct dentry *dentry, struct task_struct *task, const void *ptr)
1609 const struct pid_entry *p = ptr;
1610 struct inode *inode;
1611 struct proc_inode *ei;
1612 struct dentry *error = ERR_PTR(-EINVAL);
1614 inode = proc_pid_make_inode(dir->i_sb, task);
1619 inode->i_mode = p->mode;
1620 if (S_ISDIR(inode->i_mode))
1621 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1623 inode->i_op = p->iop;
1625 inode->i_fop = p->fop;
1627 dentry->d_op = &pid_dentry_operations;
1628 d_add(dentry, inode);
1629 /* Close the race of the process dying before we return the dentry */
1630 if (pid_revalidate(dentry, NULL))
1636 static struct dentry *proc_pident_lookup(struct inode *dir,
1637 struct dentry *dentry,
1638 const struct pid_entry *ents,
1641 struct inode *inode;
1642 struct dentry *error;
1643 struct task_struct *task = get_proc_task(dir);
1644 const struct pid_entry *p, *last;
1646 error = ERR_PTR(-ENOENT);
1653 * Yes, it does not scale. And it should not. Don't add
1654 * new entries into /proc/<tgid>/ without very good reasons.
1656 last = &ents[nents - 1];
1657 for (p = ents; p <= last; p++) {
1658 if (p->len != dentry->d_name.len)
1660 if (!memcmp(dentry->d_name.name, p->name, p->len))
1666 error = proc_pident_instantiate(dir, dentry, task, p);
1668 put_task_struct(task);
1673 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1674 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1676 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1677 proc_pident_instantiate, task, p);
1680 static int proc_pident_readdir(struct file *filp,
1681 void *dirent, filldir_t filldir,
1682 const struct pid_entry *ents, unsigned int nents)
1686 struct dentry *dentry = filp->f_path.dentry;
1687 struct inode *inode = dentry->d_inode;
1688 struct task_struct *task = get_proc_task(inode);
1689 const struct pid_entry *p, *last;
1703 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1709 ino = parent_ino(dentry);
1710 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1722 last = &ents[nents - 1];
1724 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1733 put_task_struct(task);
1738 #ifdef CONFIG_SECURITY
1739 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1740 size_t count, loff_t *ppos)
1742 struct inode * inode = file->f_path.dentry->d_inode;
1745 struct task_struct *task = get_proc_task(inode);
1750 length = security_getprocattr(task,
1751 (char*)file->f_path.dentry->d_name.name,
1753 put_task_struct(task);
1755 length = simple_read_from_buffer(buf, count, ppos, p, length);
1760 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1761 size_t count, loff_t *ppos)
1763 struct inode * inode = file->f_path.dentry->d_inode;
1766 struct task_struct *task = get_proc_task(inode);
1771 if (count > PAGE_SIZE)
1774 /* No partial writes. */
1780 page = (char*)__get_free_page(GFP_TEMPORARY);
1785 if (copy_from_user(page, buf, count))
1788 length = security_setprocattr(task,
1789 (char*)file->f_path.dentry->d_name.name,
1790 (void*)page, count);
1792 free_page((unsigned long) page);
1794 put_task_struct(task);
1799 static const struct file_operations proc_pid_attr_operations = {
1800 .read = proc_pid_attr_read,
1801 .write = proc_pid_attr_write,
1804 static const struct pid_entry attr_dir_stuff[] = {
1805 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1806 REG("prev", S_IRUGO, pid_attr),
1807 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1808 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1809 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1810 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1813 static int proc_attr_dir_readdir(struct file * filp,
1814 void * dirent, filldir_t filldir)
1816 return proc_pident_readdir(filp,dirent,filldir,
1817 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1820 static const struct file_operations proc_attr_dir_operations = {
1821 .read = generic_read_dir,
1822 .readdir = proc_attr_dir_readdir,
1825 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
1826 struct dentry *dentry, struct nameidata *nd)
1828 return proc_pident_lookup(dir, dentry,
1829 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
1832 static const struct inode_operations proc_attr_dir_inode_operations = {
1833 .lookup = proc_attr_dir_lookup,
1834 .getattr = pid_getattr,
1835 .setattr = proc_setattr,
1840 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1841 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
1842 size_t count, loff_t *ppos)
1844 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1845 struct mm_struct *mm;
1846 char buffer[PROC_NUMBUF];
1854 mm = get_task_mm(task);
1856 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
1857 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
1858 MMF_DUMP_FILTER_SHIFT));
1860 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
1863 put_task_struct(task);
1868 static ssize_t proc_coredump_filter_write(struct file *file,
1869 const char __user *buf,
1873 struct task_struct *task;
1874 struct mm_struct *mm;
1875 char buffer[PROC_NUMBUF], *end;
1882 memset(buffer, 0, sizeof(buffer));
1883 if (count > sizeof(buffer) - 1)
1884 count = sizeof(buffer) - 1;
1885 if (copy_from_user(buffer, buf, count))
1889 val = (unsigned int)simple_strtoul(buffer, &end, 0);
1892 if (end - buffer == 0)
1896 task = get_proc_task(file->f_dentry->d_inode);
1901 mm = get_task_mm(task);
1905 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
1907 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1909 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
1914 put_task_struct(task);
1919 static const struct file_operations proc_coredump_filter_operations = {
1920 .read = proc_coredump_filter_read,
1921 .write = proc_coredump_filter_write,
1928 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
1931 char tmp[PROC_NUMBUF];
1932 sprintf(tmp, "%d", current->tgid);
1933 return vfs_readlink(dentry,buffer,buflen,tmp);
1936 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
1938 char tmp[PROC_NUMBUF];
1939 sprintf(tmp, "%d", current->tgid);
1940 return ERR_PTR(vfs_follow_link(nd,tmp));
1943 static const struct inode_operations proc_self_inode_operations = {
1944 .readlink = proc_self_readlink,
1945 .follow_link = proc_self_follow_link,
1951 * These are the directory entries in the root directory of /proc
1952 * that properly belong to the /proc filesystem, as they describe
1953 * describe something that is process related.
1955 static const struct pid_entry proc_base_stuff[] = {
1956 NOD("self", S_IFLNK|S_IRWXUGO,
1957 &proc_self_inode_operations, NULL, {}),
1961 * Exceptional case: normally we are not allowed to unhash a busy
1962 * directory. In this case, however, we can do it - no aliasing problems
1963 * due to the way we treat inodes.
1965 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
1967 struct inode *inode = dentry->d_inode;
1968 struct task_struct *task = get_proc_task(inode);
1970 put_task_struct(task);
1977 static struct dentry_operations proc_base_dentry_operations =
1979 .d_revalidate = proc_base_revalidate,
1980 .d_delete = pid_delete_dentry,
1983 static struct dentry *proc_base_instantiate(struct inode *dir,
1984 struct dentry *dentry, struct task_struct *task, const void *ptr)
1986 const struct pid_entry *p = ptr;
1987 struct inode *inode;
1988 struct proc_inode *ei;
1989 struct dentry *error = ERR_PTR(-EINVAL);
1991 /* Allocate the inode */
1992 error = ERR_PTR(-ENOMEM);
1993 inode = new_inode(dir->i_sb);
1997 /* Initialize the inode */
1999 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2002 * grab the reference to the task.
2004 ei->pid = get_task_pid(task, PIDTYPE_PID);
2010 inode->i_mode = p->mode;
2011 if (S_ISDIR(inode->i_mode))
2013 if (S_ISLNK(inode->i_mode))
2016 inode->i_op = p->iop;
2018 inode->i_fop = p->fop;
2020 dentry->d_op = &proc_base_dentry_operations;
2021 d_add(dentry, inode);
2030 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2032 struct dentry *error;
2033 struct task_struct *task = get_proc_task(dir);
2034 const struct pid_entry *p, *last;
2036 error = ERR_PTR(-ENOENT);
2041 /* Lookup the directory entry */
2042 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2043 for (p = proc_base_stuff; p <= last; p++) {
2044 if (p->len != dentry->d_name.len)
2046 if (!memcmp(dentry->d_name.name, p->name, p->len))
2052 error = proc_base_instantiate(dir, dentry, task, p);
2055 put_task_struct(task);
2060 static int proc_base_fill_cache(struct file *filp, void *dirent,
2061 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2063 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2064 proc_base_instantiate, task, p);
2067 #ifdef CONFIG_TASK_IO_ACCOUNTING
2068 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2070 return sprintf(buffer,
2071 #ifdef CONFIG_TASK_XACCT
2077 "read_bytes: %llu\n"
2078 "write_bytes: %llu\n"
2079 "cancelled_write_bytes: %llu\n",
2080 #ifdef CONFIG_TASK_XACCT
2081 (unsigned long long)task->rchar,
2082 (unsigned long long)task->wchar,
2083 (unsigned long long)task->syscr,
2084 (unsigned long long)task->syscw,
2086 (unsigned long long)task->ioac.read_bytes,
2087 (unsigned long long)task->ioac.write_bytes,
2088 (unsigned long long)task->ioac.cancelled_write_bytes);
2095 static const struct file_operations proc_task_operations;
2096 static const struct inode_operations proc_task_inode_operations;
2098 static const struct pid_entry tgid_base_stuff[] = {
2099 DIR("task", S_IRUGO|S_IXUGO, task),
2100 DIR("fd", S_IRUSR|S_IXUSR, fd),
2101 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2102 REG("environ", S_IRUSR, environ),
2103 INF("auxv", S_IRUSR, pid_auxv),
2104 INF("status", S_IRUGO, pid_status),
2105 #ifdef CONFIG_SCHED_DEBUG
2106 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2108 INF("cmdline", S_IRUGO, pid_cmdline),
2109 INF("stat", S_IRUGO, tgid_stat),
2110 INF("statm", S_IRUGO, pid_statm),
2111 REG("maps", S_IRUGO, maps),
2113 REG("numa_maps", S_IRUGO, numa_maps),
2115 REG("mem", S_IRUSR|S_IWUSR, mem),
2119 REG("mounts", S_IRUGO, mounts),
2120 REG("mountstats", S_IRUSR, mountstats),
2122 REG("clear_refs", S_IWUSR, clear_refs),
2123 REG("smaps", S_IRUGO, smaps),
2125 #ifdef CONFIG_SECURITY
2126 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2128 #ifdef CONFIG_KALLSYMS
2129 INF("wchan", S_IRUGO, pid_wchan),
2131 #ifdef CONFIG_SCHEDSTATS
2132 INF("schedstat", S_IRUGO, pid_schedstat),
2134 #ifdef CONFIG_CPUSETS
2135 REG("cpuset", S_IRUGO, cpuset),
2137 #ifdef CONFIG_CGROUPS
2138 REG("cgroup", S_IRUGO, cgroup),
2140 INF("oom_score", S_IRUGO, oom_score),
2141 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2142 #ifdef CONFIG_AUDITSYSCALL
2143 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2145 #ifdef CONFIG_FAULT_INJECTION
2146 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2148 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2149 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2151 #ifdef CONFIG_TASK_IO_ACCOUNTING
2152 INF("io", S_IRUGO, pid_io_accounting),
2156 static int proc_tgid_base_readdir(struct file * filp,
2157 void * dirent, filldir_t filldir)
2159 return proc_pident_readdir(filp,dirent,filldir,
2160 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2163 static const struct file_operations proc_tgid_base_operations = {
2164 .read = generic_read_dir,
2165 .readdir = proc_tgid_base_readdir,
2168 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2169 return proc_pident_lookup(dir, dentry,
2170 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2173 static const struct inode_operations proc_tgid_base_inode_operations = {
2174 .lookup = proc_tgid_base_lookup,
2175 .getattr = pid_getattr,
2176 .setattr = proc_setattr,
2180 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2182 * @task: task that should be flushed.
2184 * Looks in the dcache for
2186 * /proc/@tgid/task/@pid
2187 * if either directory is present flushes it and all of it'ts children
2190 * It is safe and reasonable to cache /proc entries for a task until
2191 * that task exits. After that they just clog up the dcache with
2192 * useless entries, possibly causing useful dcache entries to be
2193 * flushed instead. This routine is proved to flush those useless
2194 * dcache entries at process exit time.
2196 * NOTE: This routine is just an optimization so it does not guarantee
2197 * that no dcache entries will exist at process exit time it
2198 * just makes it very unlikely that any will persist.
2200 void proc_flush_task(struct task_struct *task)
2202 struct dentry *dentry, *leader, *dir;
2203 char buf[PROC_NUMBUF];
2207 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2208 dentry = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2210 shrink_dcache_parent(dentry);
2215 if (thread_group_leader(task))
2219 name.len = snprintf(buf, sizeof(buf), "%d", task->tgid);
2220 leader = d_hash_and_lookup(proc_mnt->mnt_root, &name);
2225 name.len = strlen(name.name);
2226 dir = d_hash_and_lookup(leader, &name);
2228 goto out_put_leader;
2231 name.len = snprintf(buf, sizeof(buf), "%d", task->pid);
2232 dentry = d_hash_and_lookup(dir, &name);
2234 shrink_dcache_parent(dentry);
2246 static struct dentry *proc_pid_instantiate(struct inode *dir,
2247 struct dentry * dentry,
2248 struct task_struct *task, const void *ptr)
2250 struct dentry *error = ERR_PTR(-ENOENT);
2251 struct inode *inode;
2253 inode = proc_pid_make_inode(dir->i_sb, task);
2257 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2258 inode->i_op = &proc_tgid_base_inode_operations;
2259 inode->i_fop = &proc_tgid_base_operations;
2260 inode->i_flags|=S_IMMUTABLE;
2262 #ifdef CONFIG_SECURITY
2263 inode->i_nlink += 1;
2266 dentry->d_op = &pid_dentry_operations;
2268 d_add(dentry, inode);
2269 /* Close the race of the process dying before we return the dentry */
2270 if (pid_revalidate(dentry, NULL))
2276 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2278 struct dentry *result = ERR_PTR(-ENOENT);
2279 struct task_struct *task;
2282 result = proc_base_lookup(dir, dentry);
2283 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2286 tgid = name_to_int(dentry);
2291 task = find_task_by_pid(tgid);
2293 get_task_struct(task);
2298 result = proc_pid_instantiate(dir, dentry, task, NULL);
2299 put_task_struct(task);
2305 * Find the first task with tgid >= tgid
2308 static struct task_struct *next_tgid(unsigned int tgid)
2310 struct task_struct *task;
2316 pid = find_ge_pid(tgid);
2319 task = pid_task(pid, PIDTYPE_PID);
2320 /* What we to know is if the pid we have find is the
2321 * pid of a thread_group_leader. Testing for task
2322 * being a thread_group_leader is the obvious thing
2323 * todo but there is a window when it fails, due to
2324 * the pid transfer logic in de_thread.
2326 * So we perform the straight forward test of seeing
2327 * if the pid we have found is the pid of a thread
2328 * group leader, and don't worry if the task we have
2329 * found doesn't happen to be a thread group leader.
2330 * As we don't care in the case of readdir.
2332 if (!task || !has_group_leader_pid(task))
2334 get_task_struct(task);
2340 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2342 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2343 struct task_struct *task, int tgid)
2345 char name[PROC_NUMBUF];
2346 int len = snprintf(name, sizeof(name), "%d", tgid);
2347 return proc_fill_cache(filp, dirent, filldir, name, len,
2348 proc_pid_instantiate, task, NULL);
2351 /* for the /proc/ directory itself, after non-process stuff has been done */
2352 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2354 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2355 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2356 struct task_struct *task;
2362 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2363 const struct pid_entry *p = &proc_base_stuff[nr];
2364 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2368 tgid = filp->f_pos - TGID_OFFSET;
2369 for (task = next_tgid(tgid);
2371 put_task_struct(task), task = next_tgid(tgid + 1)) {
2373 filp->f_pos = tgid + TGID_OFFSET;
2374 if (proc_pid_fill_cache(filp, dirent, filldir, task, tgid) < 0) {
2375 put_task_struct(task);
2379 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2381 put_task_struct(reaper);
2389 static const struct pid_entry tid_base_stuff[] = {
2390 DIR("fd", S_IRUSR|S_IXUSR, fd),
2391 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2392 REG("environ", S_IRUSR, environ),
2393 INF("auxv", S_IRUSR, pid_auxv),
2394 INF("status", S_IRUGO, pid_status),
2395 #ifdef CONFIG_SCHED_DEBUG
2396 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2398 INF("cmdline", S_IRUGO, pid_cmdline),
2399 INF("stat", S_IRUGO, tid_stat),
2400 INF("statm", S_IRUGO, pid_statm),
2401 REG("maps", S_IRUGO, maps),
2403 REG("numa_maps", S_IRUGO, numa_maps),
2405 REG("mem", S_IRUSR|S_IWUSR, mem),
2409 REG("mounts", S_IRUGO, mounts),
2411 REG("clear_refs", S_IWUSR, clear_refs),
2412 REG("smaps", S_IRUGO, smaps),
2414 #ifdef CONFIG_SECURITY
2415 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2417 #ifdef CONFIG_KALLSYMS
2418 INF("wchan", S_IRUGO, pid_wchan),
2420 #ifdef CONFIG_SCHEDSTATS
2421 INF("schedstat", S_IRUGO, pid_schedstat),
2423 #ifdef CONFIG_CPUSETS
2424 REG("cpuset", S_IRUGO, cpuset),
2426 #ifdef CONFIG_CGROUPS
2427 REG("cgroup", S_IRUGO, cgroup),
2429 INF("oom_score", S_IRUGO, oom_score),
2430 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2431 #ifdef CONFIG_AUDITSYSCALL
2432 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2434 #ifdef CONFIG_FAULT_INJECTION
2435 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2439 static int proc_tid_base_readdir(struct file * filp,
2440 void * dirent, filldir_t filldir)
2442 return proc_pident_readdir(filp,dirent,filldir,
2443 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2446 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2447 return proc_pident_lookup(dir, dentry,
2448 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2451 static const struct file_operations proc_tid_base_operations = {
2452 .read = generic_read_dir,
2453 .readdir = proc_tid_base_readdir,
2456 static const struct inode_operations proc_tid_base_inode_operations = {
2457 .lookup = proc_tid_base_lookup,
2458 .getattr = pid_getattr,
2459 .setattr = proc_setattr,
2462 static struct dentry *proc_task_instantiate(struct inode *dir,
2463 struct dentry *dentry, struct task_struct *task, const void *ptr)
2465 struct dentry *error = ERR_PTR(-ENOENT);
2466 struct inode *inode;
2467 inode = proc_pid_make_inode(dir->i_sb, task);
2471 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2472 inode->i_op = &proc_tid_base_inode_operations;
2473 inode->i_fop = &proc_tid_base_operations;
2474 inode->i_flags|=S_IMMUTABLE;
2476 #ifdef CONFIG_SECURITY
2477 inode->i_nlink += 1;
2480 dentry->d_op = &pid_dentry_operations;
2482 d_add(dentry, inode);
2483 /* Close the race of the process dying before we return the dentry */
2484 if (pid_revalidate(dentry, NULL))
2490 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2492 struct dentry *result = ERR_PTR(-ENOENT);
2493 struct task_struct *task;
2494 struct task_struct *leader = get_proc_task(dir);
2500 tid = name_to_int(dentry);
2505 task = find_task_by_pid(tid);
2507 get_task_struct(task);
2511 if (leader->tgid != task->tgid)
2514 result = proc_task_instantiate(dir, dentry, task, NULL);
2516 put_task_struct(task);
2518 put_task_struct(leader);
2524 * Find the first tid of a thread group to return to user space.
2526 * Usually this is just the thread group leader, but if the users
2527 * buffer was too small or there was a seek into the middle of the
2528 * directory we have more work todo.
2530 * In the case of a short read we start with find_task_by_pid.
2532 * In the case of a seek we start with the leader and walk nr
2535 static struct task_struct *first_tid(struct task_struct *leader,
2538 struct task_struct *pos;
2541 /* Attempt to start with the pid of a thread */
2542 if (tid && (nr > 0)) {
2543 pos = find_task_by_pid(tid);
2544 if (pos && (pos->group_leader == leader))
2548 /* If nr exceeds the number of threads there is nothing todo */
2550 if (nr && nr >= get_nr_threads(leader))
2553 /* If we haven't found our starting place yet start
2554 * with the leader and walk nr threads forward.
2556 for (pos = leader; nr > 0; --nr) {
2557 pos = next_thread(pos);
2558 if (pos == leader) {
2564 get_task_struct(pos);
2571 * Find the next thread in the thread list.
2572 * Return NULL if there is an error or no next thread.
2574 * The reference to the input task_struct is released.
2576 static struct task_struct *next_tid(struct task_struct *start)
2578 struct task_struct *pos = NULL;
2580 if (pid_alive(start)) {
2581 pos = next_thread(start);
2582 if (thread_group_leader(pos))
2585 get_task_struct(pos);
2588 put_task_struct(start);
2592 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2593 struct task_struct *task, int tid)
2595 char name[PROC_NUMBUF];
2596 int len = snprintf(name, sizeof(name), "%d", tid);
2597 return proc_fill_cache(filp, dirent, filldir, name, len,
2598 proc_task_instantiate, task, NULL);
2601 /* for the /proc/TGID/task/ directories */
2602 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2604 struct dentry *dentry = filp->f_path.dentry;
2605 struct inode *inode = dentry->d_inode;
2606 struct task_struct *leader = NULL;
2607 struct task_struct *task;
2608 int retval = -ENOENT;
2611 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2613 task = get_proc_task(inode);
2617 if (pid_alive(task)) {
2618 leader = task->group_leader;
2619 get_task_struct(leader);
2622 put_task_struct(task);
2630 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2635 ino = parent_ino(dentry);
2636 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2642 /* f_version caches the tgid value that the last readdir call couldn't
2643 * return. lseek aka telldir automagically resets f_version to 0.
2645 tid = (int)filp->f_version;
2646 filp->f_version = 0;
2647 for (task = first_tid(leader, tid, pos - 2);
2649 task = next_tid(task), pos++) {
2651 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2652 /* returning this tgid failed, save it as the first
2653 * pid for the next readir call */
2654 filp->f_version = (u64)tid;
2655 put_task_struct(task);
2661 put_task_struct(leader);
2666 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2668 struct inode *inode = dentry->d_inode;
2669 struct task_struct *p = get_proc_task(inode);
2670 generic_fillattr(inode, stat);
2674 stat->nlink += get_nr_threads(p);
2682 static const struct inode_operations proc_task_inode_operations = {
2683 .lookup = proc_task_lookup,
2684 .getattr = proc_task_getattr,
2685 .setattr = proc_setattr,
2688 static const struct file_operations proc_task_operations = {
2689 .read = generic_read_dir,
2690 .readdir = proc_task_readdir,