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/resource.h>
67 #include <linux/module.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/cgroup.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
75 #include <linux/nsproxy.h>
76 #include <linux/oom.h>
77 #include <linux/elf.h>
78 #include <linux/pid_namespace.h>
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
95 const struct inode_operations *iop;
96 const struct file_operations *fop;
100 #define NOD(NAME, MODE, IOP, FOP, OP) { \
102 .len = sizeof(NAME) - 1, \
109 #define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 #define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
117 #define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
119 &proc_##OTYPE##_operations, {})
120 #define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
124 #define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &proc_##OTYPE } )
130 EXPORT_SYMBOL(maps_protect);
132 static struct fs_struct *get_fs_struct(struct task_struct *task)
134 struct fs_struct *fs;
138 atomic_inc(&fs->count);
143 static int get_nr_threads(struct task_struct *tsk)
145 /* Must be called with the rcu_read_lock held */
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
156 static int proc_cwd_link(struct inode *inode, struct path *path)
158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
160 int result = -ENOENT;
163 fs = get_fs_struct(task);
164 put_task_struct(task);
167 read_lock(&fs->lock);
170 read_unlock(&fs->lock);
177 static int proc_root_link(struct inode *inode, struct path *path)
179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
181 int result = -ENOENT;
184 fs = get_fs_struct(task);
185 put_task_struct(task);
188 read_lock(&fs->lock);
191 read_unlock(&fs->lock);
198 #define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
202 (task_is_stopped_or_traced(task)) && \
203 security_ptrace(current,task) == 0))
205 struct mm_struct *mm_for_maps(struct task_struct *task)
207 struct mm_struct *mm = get_task_mm(task);
210 down_read(&mm->mmap_sem);
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
220 up_read(&mm->mmap_sem);
225 static int proc_pid_cmdline(struct task_struct *task, char * buffer)
229 struct mm_struct *mm = get_task_mm(task);
233 goto out_mm; /* Shh! No looking before we're done */
235 len = mm->arg_end - mm->arg_start;
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
262 static int proc_pid_auxv(struct task_struct *task, char *buffer)
265 struct mm_struct *mm = get_task_mm(task);
267 unsigned int nwords = 0;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
274 memcpy(buffer, mm->saved_auxv, res);
281 #ifdef CONFIG_KALLSYMS
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
286 static int proc_pid_wchan(struct task_struct *task, char *buffer)
289 char symname[KSYM_NAME_LEN];
291 wchan = get_wchan(task);
293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
296 return sprintf(buffer, "%s", symname);
298 #endif /* CONFIG_KALLSYMS */
300 #ifdef CONFIG_SCHEDSTATS
302 * Provides /proc/PID/schedstat
304 static int proc_pid_schedstat(struct task_struct *task, char *buffer)
306 return sprintf(buffer, "%llu %llu %lu\n",
307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
309 task->sched_info.pcount);
313 #ifdef CONFIG_LATENCYTOP
314 static int lstats_show_proc(struct seq_file *m, void *v)
317 struct task_struct *task = m->private;
318 seq_puts(m, "Latency Top version : v0.1\n");
320 for (i = 0; i < 32; i++) {
321 if (task->latency_record[i].backtrace[0]) {
323 seq_printf(m, "%i %li %li ",
324 task->latency_record[i].count,
325 task->latency_record[i].time,
326 task->latency_record[i].max);
327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
328 char sym[KSYM_NAME_LEN];
330 if (!task->latency_record[i].backtrace[q])
332 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
334 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
335 c = strchr(sym, '+');
338 seq_printf(m, "%s ", sym);
347 static int lstats_open(struct inode *inode, struct file *file)
351 struct task_struct *task = get_proc_task(inode);
355 ret = single_open(file, lstats_show_proc, NULL);
357 m = file->private_data;
363 static int lstats_release(struct inode *inode, struct file *file)
365 struct seq_file *m = file->private_data;
366 struct task_struct *task = m->private;
368 put_task_struct(task);
369 return single_release(inode, file);
372 static ssize_t lstats_write(struct file *file, const char __user *buf,
373 size_t count, loff_t *offs)
376 struct task_struct *task;
378 m = file->private_data;
380 clear_all_latency_tracing(task);
385 static const struct file_operations proc_lstats_operations = {
388 .write = lstats_write,
390 .release = lstats_release,
395 /* The badness from the OOM killer */
396 unsigned long badness(struct task_struct *p, unsigned long uptime);
397 static int proc_oom_score(struct task_struct *task, char *buffer)
399 unsigned long points;
400 struct timespec uptime;
402 do_posix_clock_monotonic_gettime(&uptime);
403 read_lock(&tasklist_lock);
404 points = badness(task, uptime.tv_sec);
405 read_unlock(&tasklist_lock);
406 return sprintf(buffer, "%lu\n", points);
414 static const struct limit_names lnames[RLIM_NLIMITS] = {
415 [RLIMIT_CPU] = {"Max cpu time", "ms"},
416 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
417 [RLIMIT_DATA] = {"Max data size", "bytes"},
418 [RLIMIT_STACK] = {"Max stack size", "bytes"},
419 [RLIMIT_CORE] = {"Max core file size", "bytes"},
420 [RLIMIT_RSS] = {"Max resident set", "bytes"},
421 [RLIMIT_NPROC] = {"Max processes", "processes"},
422 [RLIMIT_NOFILE] = {"Max open files", "files"},
423 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
424 [RLIMIT_AS] = {"Max address space", "bytes"},
425 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
426 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
427 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
428 [RLIMIT_NICE] = {"Max nice priority", NULL},
429 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
430 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
433 /* Display limits for a process */
434 static int proc_pid_limits(struct task_struct *task, char *buffer)
439 char *bufptr = buffer;
441 struct rlimit rlim[RLIM_NLIMITS];
444 if (!lock_task_sighand(task,&flags)) {
448 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
449 unlock_task_sighand(task, &flags);
453 * print the file header
455 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
456 "Limit", "Soft Limit", "Hard Limit", "Units");
458 for (i = 0; i < RLIM_NLIMITS; i++) {
459 if (rlim[i].rlim_cur == RLIM_INFINITY)
460 count += sprintf(&bufptr[count], "%-25s %-20s ",
461 lnames[i].name, "unlimited");
463 count += sprintf(&bufptr[count], "%-25s %-20lu ",
464 lnames[i].name, rlim[i].rlim_cur);
466 if (rlim[i].rlim_max == RLIM_INFINITY)
467 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
469 count += sprintf(&bufptr[count], "%-20lu ",
473 count += sprintf(&bufptr[count], "%-10s\n",
476 count += sprintf(&bufptr[count], "\n");
482 /************************************************************************/
483 /* Here the fs part begins */
484 /************************************************************************/
486 /* permission checks */
487 static int proc_fd_access_allowed(struct inode *inode)
489 struct task_struct *task;
491 /* Allow access to a task's file descriptors if it is us or we
492 * may use ptrace attach to the process and find out that
495 task = get_proc_task(inode);
497 allowed = ptrace_may_attach(task);
498 put_task_struct(task);
503 static int proc_setattr(struct dentry *dentry, struct iattr *attr)
506 struct inode *inode = dentry->d_inode;
508 if (attr->ia_valid & ATTR_MODE)
511 error = inode_change_ok(inode, attr);
513 error = inode_setattr(inode, attr);
517 static const struct inode_operations proc_def_inode_operations = {
518 .setattr = proc_setattr,
521 extern const struct seq_operations mounts_op;
527 static int mounts_open(struct inode *inode, struct file *file)
529 struct task_struct *task = get_proc_task(inode);
531 struct mnt_namespace *ns = NULL;
532 struct proc_mounts *p;
537 nsp = task_nsproxy(task);
545 put_task_struct(task);
550 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
552 file->private_data = &p->m;
553 ret = seq_open(file, &mounts_op);
556 p->event = ns->event;
566 static int mounts_release(struct inode *inode, struct file *file)
568 struct seq_file *m = file->private_data;
569 struct mnt_namespace *ns = m->private;
571 return seq_release(inode, file);
574 static unsigned mounts_poll(struct file *file, poll_table *wait)
576 struct proc_mounts *p = file->private_data;
577 struct mnt_namespace *ns = p->m.private;
580 poll_wait(file, &ns->poll, wait);
582 spin_lock(&vfsmount_lock);
583 if (p->event != ns->event) {
584 p->event = ns->event;
587 spin_unlock(&vfsmount_lock);
592 static const struct file_operations proc_mounts_operations = {
596 .release = mounts_release,
600 extern const struct seq_operations mountstats_op;
601 static int mountstats_open(struct inode *inode, struct file *file)
603 int ret = seq_open(file, &mountstats_op);
606 struct seq_file *m = file->private_data;
608 struct mnt_namespace *mnt_ns = NULL;
609 struct task_struct *task = get_proc_task(inode);
613 nsp = task_nsproxy(task);
615 mnt_ns = nsp->mnt_ns;
621 put_task_struct(task);
627 seq_release(inode, file);
634 static const struct file_operations proc_mountstats_operations = {
635 .open = mountstats_open,
638 .release = mounts_release,
641 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
643 static ssize_t proc_info_read(struct file * file, char __user * buf,
644 size_t count, loff_t *ppos)
646 struct inode * inode = file->f_path.dentry->d_inode;
649 struct task_struct *task = get_proc_task(inode);
655 if (count > PROC_BLOCK_SIZE)
656 count = PROC_BLOCK_SIZE;
659 if (!(page = __get_free_page(GFP_TEMPORARY)))
662 length = PROC_I(inode)->op.proc_read(task, (char*)page);
665 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
668 put_task_struct(task);
673 static const struct file_operations proc_info_file_operations = {
674 .read = proc_info_read,
677 static int proc_single_show(struct seq_file *m, void *v)
679 struct inode *inode = m->private;
680 struct pid_namespace *ns;
682 struct task_struct *task;
685 ns = inode->i_sb->s_fs_info;
686 pid = proc_pid(inode);
687 task = get_pid_task(pid, PIDTYPE_PID);
691 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
693 put_task_struct(task);
697 static int proc_single_open(struct inode *inode, struct file *filp)
700 ret = single_open(filp, proc_single_show, NULL);
702 struct seq_file *m = filp->private_data;
709 static const struct file_operations proc_single_file_operations = {
710 .open = proc_single_open,
713 .release = single_release,
716 static int mem_open(struct inode* inode, struct file* file)
718 file->private_data = (void*)((long)current->self_exec_id);
722 static ssize_t mem_read(struct file * file, char __user * buf,
723 size_t count, loff_t *ppos)
725 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
727 unsigned long src = *ppos;
729 struct mm_struct *mm;
734 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
738 page = (char *)__get_free_page(GFP_TEMPORARY);
744 mm = get_task_mm(task);
750 if (file->private_data != (void*)((long)current->self_exec_id))
756 int this_len, retval;
758 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
759 retval = access_process_vm(task, src, page, this_len, 0);
760 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
766 if (copy_to_user(buf, page, retval)) {
781 free_page((unsigned long) page);
783 put_task_struct(task);
788 #define mem_write NULL
791 /* This is a security hazard */
792 static ssize_t mem_write(struct file * file, const char __user *buf,
793 size_t count, loff_t *ppos)
797 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
798 unsigned long dst = *ppos;
804 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
808 page = (char *)__get_free_page(GFP_TEMPORARY);
814 int this_len, retval;
816 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
817 if (copy_from_user(page, buf, this_len)) {
821 retval = access_process_vm(task, dst, page, this_len, 1);
833 free_page((unsigned long) page);
835 put_task_struct(task);
841 loff_t mem_lseek(struct file *file, loff_t offset, int orig)
845 file->f_pos = offset;
848 file->f_pos += offset;
853 force_successful_syscall_return();
857 static const struct file_operations proc_mem_operations = {
864 static ssize_t environ_read(struct file *file, char __user *buf,
865 size_t count, loff_t *ppos)
867 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
869 unsigned long src = *ppos;
871 struct mm_struct *mm;
876 if (!ptrace_may_attach(task))
880 page = (char *)__get_free_page(GFP_TEMPORARY);
886 mm = get_task_mm(task);
891 int this_len, retval, max_len;
893 this_len = mm->env_end - (mm->env_start + src);
898 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
899 this_len = (this_len > max_len) ? max_len : this_len;
901 retval = access_process_vm(task, (mm->env_start + src),
909 if (copy_to_user(buf, page, retval)) {
923 free_page((unsigned long) page);
925 put_task_struct(task);
930 static const struct file_operations proc_environ_operations = {
931 .read = environ_read,
934 static ssize_t oom_adjust_read(struct file *file, char __user *buf,
935 size_t count, loff_t *ppos)
937 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
938 char buffer[PROC_NUMBUF];
944 oom_adjust = task->oomkilladj;
945 put_task_struct(task);
947 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
949 return simple_read_from_buffer(buf, count, ppos, buffer, len);
952 static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
953 size_t count, loff_t *ppos)
955 struct task_struct *task;
956 char buffer[PROC_NUMBUF], *end;
959 memset(buffer, 0, sizeof(buffer));
960 if (count > sizeof(buffer) - 1)
961 count = sizeof(buffer) - 1;
962 if (copy_from_user(buffer, buf, count))
964 oom_adjust = simple_strtol(buffer, &end, 0);
965 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
966 oom_adjust != OOM_DISABLE)
970 task = get_proc_task(file->f_path.dentry->d_inode);
973 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
974 put_task_struct(task);
977 task->oomkilladj = oom_adjust;
978 put_task_struct(task);
979 if (end - buffer == 0)
984 static const struct file_operations proc_oom_adjust_operations = {
985 .read = oom_adjust_read,
986 .write = oom_adjust_write,
989 #ifdef CONFIG_AUDITSYSCALL
991 static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
992 size_t count, loff_t *ppos)
994 struct inode * inode = file->f_path.dentry->d_inode;
995 struct task_struct *task = get_proc_task(inode);
997 char tmpbuf[TMPBUFLEN];
1001 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1002 audit_get_loginuid(task));
1003 put_task_struct(task);
1004 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1007 static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1008 size_t count, loff_t *ppos)
1010 struct inode * inode = file->f_path.dentry->d_inode;
1015 if (!capable(CAP_AUDIT_CONTROL))
1018 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
1021 if (count >= PAGE_SIZE)
1022 count = PAGE_SIZE - 1;
1025 /* No partial writes. */
1028 page = (char*)__get_free_page(GFP_TEMPORARY);
1032 if (copy_from_user(page, buf, count))
1036 loginuid = simple_strtoul(page, &tmp, 10);
1042 length = audit_set_loginuid(current, loginuid);
1043 if (likely(length == 0))
1047 free_page((unsigned long) page);
1051 static const struct file_operations proc_loginuid_operations = {
1052 .read = proc_loginuid_read,
1053 .write = proc_loginuid_write,
1057 #ifdef CONFIG_FAULT_INJECTION
1058 static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1059 size_t count, loff_t *ppos)
1061 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1062 char buffer[PROC_NUMBUF];
1068 make_it_fail = task->make_it_fail;
1069 put_task_struct(task);
1071 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
1073 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1076 static ssize_t proc_fault_inject_write(struct file * file,
1077 const char __user * buf, size_t count, loff_t *ppos)
1079 struct task_struct *task;
1080 char buffer[PROC_NUMBUF], *end;
1083 if (!capable(CAP_SYS_RESOURCE))
1085 memset(buffer, 0, sizeof(buffer));
1086 if (count > sizeof(buffer) - 1)
1087 count = sizeof(buffer) - 1;
1088 if (copy_from_user(buffer, buf, count))
1090 make_it_fail = simple_strtol(buffer, &end, 0);
1093 task = get_proc_task(file->f_dentry->d_inode);
1096 task->make_it_fail = make_it_fail;
1097 put_task_struct(task);
1098 if (end - buffer == 0)
1100 return end - buffer;
1103 static const struct file_operations proc_fault_inject_operations = {
1104 .read = proc_fault_inject_read,
1105 .write = proc_fault_inject_write,
1110 #ifdef CONFIG_SCHED_DEBUG
1112 * Print out various scheduling related per-task fields:
1114 static int sched_show(struct seq_file *m, void *v)
1116 struct inode *inode = m->private;
1117 struct task_struct *p;
1121 p = get_proc_task(inode);
1124 proc_sched_show_task(p, m);
1132 sched_write(struct file *file, const char __user *buf,
1133 size_t count, loff_t *offset)
1135 struct inode *inode = file->f_path.dentry->d_inode;
1136 struct task_struct *p;
1140 p = get_proc_task(inode);
1143 proc_sched_set_task(p);
1150 static int sched_open(struct inode *inode, struct file *filp)
1154 ret = single_open(filp, sched_show, NULL);
1156 struct seq_file *m = filp->private_data;
1163 static const struct file_operations proc_pid_sched_operations = {
1166 .write = sched_write,
1167 .llseek = seq_lseek,
1168 .release = single_release,
1173 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
1175 struct inode *inode = dentry->d_inode;
1176 int error = -EACCES;
1178 /* We don't need a base pointer in the /proc filesystem */
1179 path_put(&nd->path);
1181 /* Are we allowed to snoop on the tasks file descriptors? */
1182 if (!proc_fd_access_allowed(inode))
1185 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
1186 nd->last_type = LAST_BIND;
1188 return ERR_PTR(error);
1191 static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
1193 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
1200 pathname = d_path(path, tmp, PAGE_SIZE);
1201 len = PTR_ERR(pathname);
1202 if (IS_ERR(pathname))
1204 len = tmp + PAGE_SIZE - 1 - pathname;
1208 if (copy_to_user(buffer, pathname, len))
1211 free_page((unsigned long)tmp);
1215 static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1217 int error = -EACCES;
1218 struct inode *inode = dentry->d_inode;
1221 /* Are we allowed to snoop on the tasks file descriptors? */
1222 if (!proc_fd_access_allowed(inode))
1225 error = PROC_I(inode)->op.proc_get_link(inode, &path);
1229 error = do_proc_readlink(&path, buffer, buflen);
1235 static const struct inode_operations proc_pid_link_inode_operations = {
1236 .readlink = proc_pid_readlink,
1237 .follow_link = proc_pid_follow_link,
1238 .setattr = proc_setattr,
1242 /* building an inode */
1244 static int task_dumpable(struct task_struct *task)
1247 struct mm_struct *mm;
1252 dumpable = get_dumpable(mm);
1260 static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
1262 struct inode * inode;
1263 struct proc_inode *ei;
1265 /* We need a new inode */
1267 inode = new_inode(sb);
1273 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1274 inode->i_op = &proc_def_inode_operations;
1277 * grab the reference to task.
1279 ei->pid = get_task_pid(task, PIDTYPE_PID);
1285 if (task_dumpable(task)) {
1286 inode->i_uid = task->euid;
1287 inode->i_gid = task->egid;
1289 security_task_to_inode(task, inode);
1299 static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1301 struct inode *inode = dentry->d_inode;
1302 struct task_struct *task;
1303 generic_fillattr(inode, stat);
1308 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1310 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1311 task_dumpable(task)) {
1312 stat->uid = task->euid;
1313 stat->gid = task->egid;
1323 * Exceptional case: normally we are not allowed to unhash a busy
1324 * directory. In this case, however, we can do it - no aliasing problems
1325 * due to the way we treat inodes.
1327 * Rewrite the inode's ownerships here because the owning task may have
1328 * performed a setuid(), etc.
1330 * Before the /proc/pid/status file was created the only way to read
1331 * the effective uid of a /process was to stat /proc/pid. Reading
1332 * /proc/pid/status is slow enough that procps and other packages
1333 * kept stating /proc/pid. To keep the rules in /proc simple I have
1334 * made this apply to all per process world readable and executable
1337 static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1339 struct inode *inode = dentry->d_inode;
1340 struct task_struct *task = get_proc_task(inode);
1342 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1343 task_dumpable(task)) {
1344 inode->i_uid = task->euid;
1345 inode->i_gid = task->egid;
1350 inode->i_mode &= ~(S_ISUID | S_ISGID);
1351 security_task_to_inode(task, inode);
1352 put_task_struct(task);
1359 static int pid_delete_dentry(struct dentry * dentry)
1361 /* Is the task we represent dead?
1362 * If so, then don't put the dentry on the lru list,
1363 * kill it immediately.
1365 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1368 static struct dentry_operations pid_dentry_operations =
1370 .d_revalidate = pid_revalidate,
1371 .d_delete = pid_delete_dentry,
1376 typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1377 struct task_struct *, const void *);
1380 * Fill a directory entry.
1382 * If possible create the dcache entry and derive our inode number and
1383 * file type from dcache entry.
1385 * Since all of the proc inode numbers are dynamically generated, the inode
1386 * numbers do not exist until the inode is cache. This means creating the
1387 * the dcache entry in readdir is necessary to keep the inode numbers
1388 * reported by readdir in sync with the inode numbers reported
1391 static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1392 char *name, int len,
1393 instantiate_t instantiate, struct task_struct *task, const void *ptr)
1395 struct dentry *child, *dir = filp->f_path.dentry;
1396 struct inode *inode;
1399 unsigned type = DT_UNKNOWN;
1403 qname.hash = full_name_hash(name, len);
1405 child = d_lookup(dir, &qname);
1408 new = d_alloc(dir, &qname);
1410 child = instantiate(dir->d_inode, new, task, ptr);
1417 if (!child || IS_ERR(child) || !child->d_inode)
1418 goto end_instantiate;
1419 inode = child->d_inode;
1422 type = inode->i_mode >> 12;
1427 ino = find_inode_number(dir, &qname);
1430 return filldir(dirent, name, len, filp->f_pos, ino, type);
1433 static unsigned name_to_int(struct dentry *dentry)
1435 const char *name = dentry->d_name.name;
1436 int len = dentry->d_name.len;
1439 if (len > 1 && *name == '0')
1442 unsigned c = *name++ - '0';
1445 if (n >= (~0U-9)/10)
1455 #define PROC_FDINFO_MAX 64
1457 static int proc_fd_info(struct inode *inode, struct path *path, char *info)
1459 struct task_struct *task = get_proc_task(inode);
1460 struct files_struct *files = NULL;
1462 int fd = proc_fd(inode);
1465 files = get_files_struct(task);
1466 put_task_struct(task);
1470 * We are not taking a ref to the file structure, so we must
1473 spin_lock(&files->file_lock);
1474 file = fcheck_files(files, fd);
1477 *path = file->f_path;
1478 path_get(&file->f_path);
1481 snprintf(info, PROC_FDINFO_MAX,
1484 (long long) file->f_pos,
1486 spin_unlock(&files->file_lock);
1487 put_files_struct(files);
1490 spin_unlock(&files->file_lock);
1491 put_files_struct(files);
1496 static int proc_fd_link(struct inode *inode, struct path *path)
1498 return proc_fd_info(inode, path, NULL);
1501 static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1503 struct inode *inode = dentry->d_inode;
1504 struct task_struct *task = get_proc_task(inode);
1505 int fd = proc_fd(inode);
1506 struct files_struct *files;
1509 files = get_files_struct(task);
1512 if (fcheck_files(files, fd)) {
1514 put_files_struct(files);
1515 if (task_dumpable(task)) {
1516 inode->i_uid = task->euid;
1517 inode->i_gid = task->egid;
1522 inode->i_mode &= ~(S_ISUID | S_ISGID);
1523 security_task_to_inode(task, inode);
1524 put_task_struct(task);
1528 put_files_struct(files);
1530 put_task_struct(task);
1536 static struct dentry_operations tid_fd_dentry_operations =
1538 .d_revalidate = tid_fd_revalidate,
1539 .d_delete = pid_delete_dentry,
1542 static struct dentry *proc_fd_instantiate(struct inode *dir,
1543 struct dentry *dentry, struct task_struct *task, const void *ptr)
1545 unsigned fd = *(const unsigned *)ptr;
1547 struct files_struct *files;
1548 struct inode *inode;
1549 struct proc_inode *ei;
1550 struct dentry *error = ERR_PTR(-ENOENT);
1552 inode = proc_pid_make_inode(dir->i_sb, task);
1557 files = get_files_struct(task);
1560 inode->i_mode = S_IFLNK;
1563 * We are not taking a ref to the file structure, so we must
1566 spin_lock(&files->file_lock);
1567 file = fcheck_files(files, fd);
1570 if (file->f_mode & 1)
1571 inode->i_mode |= S_IRUSR | S_IXUSR;
1572 if (file->f_mode & 2)
1573 inode->i_mode |= S_IWUSR | S_IXUSR;
1574 spin_unlock(&files->file_lock);
1575 put_files_struct(files);
1577 inode->i_op = &proc_pid_link_inode_operations;
1579 ei->op.proc_get_link = proc_fd_link;
1580 dentry->d_op = &tid_fd_dentry_operations;
1581 d_add(dentry, inode);
1582 /* Close the race of the process dying before we return the dentry */
1583 if (tid_fd_revalidate(dentry, NULL))
1589 spin_unlock(&files->file_lock);
1590 put_files_struct(files);
1596 static struct dentry *proc_lookupfd_common(struct inode *dir,
1597 struct dentry *dentry,
1598 instantiate_t instantiate)
1600 struct task_struct *task = get_proc_task(dir);
1601 unsigned fd = name_to_int(dentry);
1602 struct dentry *result = ERR_PTR(-ENOENT);
1609 result = instantiate(dir, dentry, task, &fd);
1611 put_task_struct(task);
1616 static int proc_readfd_common(struct file * filp, void * dirent,
1617 filldir_t filldir, instantiate_t instantiate)
1619 struct dentry *dentry = filp->f_path.dentry;
1620 struct inode *inode = dentry->d_inode;
1621 struct task_struct *p = get_proc_task(inode);
1622 unsigned int fd, ino;
1624 struct files_struct * files;
1625 struct fdtable *fdt;
1635 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1639 ino = parent_ino(dentry);
1640 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1644 files = get_files_struct(p);
1648 fdt = files_fdtable(files);
1649 for (fd = filp->f_pos-2;
1651 fd++, filp->f_pos++) {
1652 char name[PROC_NUMBUF];
1655 if (!fcheck_files(files, fd))
1659 len = snprintf(name, sizeof(name), "%d", fd);
1660 if (proc_fill_cache(filp, dirent, filldir,
1661 name, len, instantiate,
1669 put_files_struct(files);
1677 static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1678 struct nameidata *nd)
1680 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1683 static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1685 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1688 static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1689 size_t len, loff_t *ppos)
1691 char tmp[PROC_FDINFO_MAX];
1692 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
1694 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1698 static const struct file_operations proc_fdinfo_file_operations = {
1699 .open = nonseekable_open,
1700 .read = proc_fdinfo_read,
1703 static const struct file_operations proc_fd_operations = {
1704 .read = generic_read_dir,
1705 .readdir = proc_readfd,
1709 * /proc/pid/fd needs a special permission handler so that a process can still
1710 * access /proc/self/fd after it has executed a setuid().
1712 static int proc_fd_permission(struct inode *inode, int mask,
1713 struct nameidata *nd)
1717 rv = generic_permission(inode, mask, NULL);
1720 if (task_pid(current) == proc_pid(inode))
1726 * proc directories can do almost nothing..
1728 static const struct inode_operations proc_fd_inode_operations = {
1729 .lookup = proc_lookupfd,
1730 .permission = proc_fd_permission,
1731 .setattr = proc_setattr,
1734 static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1735 struct dentry *dentry, struct task_struct *task, const void *ptr)
1737 unsigned fd = *(unsigned *)ptr;
1738 struct inode *inode;
1739 struct proc_inode *ei;
1740 struct dentry *error = ERR_PTR(-ENOENT);
1742 inode = proc_pid_make_inode(dir->i_sb, task);
1747 inode->i_mode = S_IFREG | S_IRUSR;
1748 inode->i_fop = &proc_fdinfo_file_operations;
1749 dentry->d_op = &tid_fd_dentry_operations;
1750 d_add(dentry, inode);
1751 /* Close the race of the process dying before we return the dentry */
1752 if (tid_fd_revalidate(dentry, NULL))
1759 static struct dentry *proc_lookupfdinfo(struct inode *dir,
1760 struct dentry *dentry,
1761 struct nameidata *nd)
1763 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1766 static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1768 return proc_readfd_common(filp, dirent, filldir,
1769 proc_fdinfo_instantiate);
1772 static const struct file_operations proc_fdinfo_operations = {
1773 .read = generic_read_dir,
1774 .readdir = proc_readfdinfo,
1778 * proc directories can do almost nothing..
1780 static const struct inode_operations proc_fdinfo_inode_operations = {
1781 .lookup = proc_lookupfdinfo,
1782 .setattr = proc_setattr,
1786 static struct dentry *proc_pident_instantiate(struct inode *dir,
1787 struct dentry *dentry, struct task_struct *task, const void *ptr)
1789 const struct pid_entry *p = ptr;
1790 struct inode *inode;
1791 struct proc_inode *ei;
1792 struct dentry *error = ERR_PTR(-EINVAL);
1794 inode = proc_pid_make_inode(dir->i_sb, task);
1799 inode->i_mode = p->mode;
1800 if (S_ISDIR(inode->i_mode))
1801 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1803 inode->i_op = p->iop;
1805 inode->i_fop = p->fop;
1807 dentry->d_op = &pid_dentry_operations;
1808 d_add(dentry, inode);
1809 /* Close the race of the process dying before we return the dentry */
1810 if (pid_revalidate(dentry, NULL))
1816 static struct dentry *proc_pident_lookup(struct inode *dir,
1817 struct dentry *dentry,
1818 const struct pid_entry *ents,
1821 struct inode *inode;
1822 struct dentry *error;
1823 struct task_struct *task = get_proc_task(dir);
1824 const struct pid_entry *p, *last;
1826 error = ERR_PTR(-ENOENT);
1833 * Yes, it does not scale. And it should not. Don't add
1834 * new entries into /proc/<tgid>/ without very good reasons.
1836 last = &ents[nents - 1];
1837 for (p = ents; p <= last; p++) {
1838 if (p->len != dentry->d_name.len)
1840 if (!memcmp(dentry->d_name.name, p->name, p->len))
1846 error = proc_pident_instantiate(dir, dentry, task, p);
1848 put_task_struct(task);
1853 static int proc_pident_fill_cache(struct file *filp, void *dirent,
1854 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
1856 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1857 proc_pident_instantiate, task, p);
1860 static int proc_pident_readdir(struct file *filp,
1861 void *dirent, filldir_t filldir,
1862 const struct pid_entry *ents, unsigned int nents)
1865 struct dentry *dentry = filp->f_path.dentry;
1866 struct inode *inode = dentry->d_inode;
1867 struct task_struct *task = get_proc_task(inode);
1868 const struct pid_entry *p, *last;
1881 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1887 ino = parent_ino(dentry);
1888 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1900 last = &ents[nents - 1];
1902 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
1911 put_task_struct(task);
1916 #ifdef CONFIG_SECURITY
1917 static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1918 size_t count, loff_t *ppos)
1920 struct inode * inode = file->f_path.dentry->d_inode;
1923 struct task_struct *task = get_proc_task(inode);
1928 length = security_getprocattr(task,
1929 (char*)file->f_path.dentry->d_name.name,
1931 put_task_struct(task);
1933 length = simple_read_from_buffer(buf, count, ppos, p, length);
1938 static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1939 size_t count, loff_t *ppos)
1941 struct inode * inode = file->f_path.dentry->d_inode;
1944 struct task_struct *task = get_proc_task(inode);
1949 if (count > PAGE_SIZE)
1952 /* No partial writes. */
1958 page = (char*)__get_free_page(GFP_TEMPORARY);
1963 if (copy_from_user(page, buf, count))
1966 length = security_setprocattr(task,
1967 (char*)file->f_path.dentry->d_name.name,
1968 (void*)page, count);
1970 free_page((unsigned long) page);
1972 put_task_struct(task);
1977 static const struct file_operations proc_pid_attr_operations = {
1978 .read = proc_pid_attr_read,
1979 .write = proc_pid_attr_write,
1982 static const struct pid_entry attr_dir_stuff[] = {
1983 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1984 REG("prev", S_IRUGO, pid_attr),
1985 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1986 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1987 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1988 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
1991 static int proc_attr_dir_readdir(struct file * filp,
1992 void * dirent, filldir_t filldir)
1994 return proc_pident_readdir(filp,dirent,filldir,
1995 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
1998 static const struct file_operations proc_attr_dir_operations = {
1999 .read = generic_read_dir,
2000 .readdir = proc_attr_dir_readdir,
2003 static struct dentry *proc_attr_dir_lookup(struct inode *dir,
2004 struct dentry *dentry, struct nameidata *nd)
2006 return proc_pident_lookup(dir, dentry,
2007 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
2010 static const struct inode_operations proc_attr_dir_inode_operations = {
2011 .lookup = proc_attr_dir_lookup,
2012 .getattr = pid_getattr,
2013 .setattr = proc_setattr,
2018 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2019 static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2020 size_t count, loff_t *ppos)
2022 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2023 struct mm_struct *mm;
2024 char buffer[PROC_NUMBUF];
2032 mm = get_task_mm(task);
2034 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2035 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2036 MMF_DUMP_FILTER_SHIFT));
2038 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2041 put_task_struct(task);
2046 static ssize_t proc_coredump_filter_write(struct file *file,
2047 const char __user *buf,
2051 struct task_struct *task;
2052 struct mm_struct *mm;
2053 char buffer[PROC_NUMBUF], *end;
2060 memset(buffer, 0, sizeof(buffer));
2061 if (count > sizeof(buffer) - 1)
2062 count = sizeof(buffer) - 1;
2063 if (copy_from_user(buffer, buf, count))
2067 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2070 if (end - buffer == 0)
2074 task = get_proc_task(file->f_dentry->d_inode);
2079 mm = get_task_mm(task);
2083 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2085 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2087 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2092 put_task_struct(task);
2097 static const struct file_operations proc_coredump_filter_operations = {
2098 .read = proc_coredump_filter_read,
2099 .write = proc_coredump_filter_write,
2106 static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2109 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2110 pid_t tgid = task_tgid_nr_ns(current, ns);
2111 char tmp[PROC_NUMBUF];
2114 sprintf(tmp, "%d", tgid);
2115 return vfs_readlink(dentry,buffer,buflen,tmp);
2118 static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
2120 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
2121 pid_t tgid = task_tgid_nr_ns(current, ns);
2122 char tmp[PROC_NUMBUF];
2124 return ERR_PTR(-ENOENT);
2125 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
2126 return ERR_PTR(vfs_follow_link(nd,tmp));
2129 static const struct inode_operations proc_self_inode_operations = {
2130 .readlink = proc_self_readlink,
2131 .follow_link = proc_self_follow_link,
2137 * These are the directory entries in the root directory of /proc
2138 * that properly belong to the /proc filesystem, as they describe
2139 * describe something that is process related.
2141 static const struct pid_entry proc_base_stuff[] = {
2142 NOD("self", S_IFLNK|S_IRWXUGO,
2143 &proc_self_inode_operations, NULL, {}),
2147 * Exceptional case: normally we are not allowed to unhash a busy
2148 * directory. In this case, however, we can do it - no aliasing problems
2149 * due to the way we treat inodes.
2151 static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2153 struct inode *inode = dentry->d_inode;
2154 struct task_struct *task = get_proc_task(inode);
2156 put_task_struct(task);
2163 static struct dentry_operations proc_base_dentry_operations =
2165 .d_revalidate = proc_base_revalidate,
2166 .d_delete = pid_delete_dentry,
2169 static struct dentry *proc_base_instantiate(struct inode *dir,
2170 struct dentry *dentry, struct task_struct *task, const void *ptr)
2172 const struct pid_entry *p = ptr;
2173 struct inode *inode;
2174 struct proc_inode *ei;
2175 struct dentry *error = ERR_PTR(-EINVAL);
2177 /* Allocate the inode */
2178 error = ERR_PTR(-ENOMEM);
2179 inode = new_inode(dir->i_sb);
2183 /* Initialize the inode */
2185 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2188 * grab the reference to the task.
2190 ei->pid = get_task_pid(task, PIDTYPE_PID);
2196 inode->i_mode = p->mode;
2197 if (S_ISDIR(inode->i_mode))
2199 if (S_ISLNK(inode->i_mode))
2202 inode->i_op = p->iop;
2204 inode->i_fop = p->fop;
2206 dentry->d_op = &proc_base_dentry_operations;
2207 d_add(dentry, inode);
2216 static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2218 struct dentry *error;
2219 struct task_struct *task = get_proc_task(dir);
2220 const struct pid_entry *p, *last;
2222 error = ERR_PTR(-ENOENT);
2227 /* Lookup the directory entry */
2228 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2229 for (p = proc_base_stuff; p <= last; p++) {
2230 if (p->len != dentry->d_name.len)
2232 if (!memcmp(dentry->d_name.name, p->name, p->len))
2238 error = proc_base_instantiate(dir, dentry, task, p);
2241 put_task_struct(task);
2246 static int proc_base_fill_cache(struct file *filp, void *dirent,
2247 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
2249 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2250 proc_base_instantiate, task, p);
2253 #ifdef CONFIG_TASK_IO_ACCOUNTING
2254 static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2256 return sprintf(buffer,
2257 #ifdef CONFIG_TASK_XACCT
2263 "read_bytes: %llu\n"
2264 "write_bytes: %llu\n"
2265 "cancelled_write_bytes: %llu\n",
2266 #ifdef CONFIG_TASK_XACCT
2267 (unsigned long long)task->rchar,
2268 (unsigned long long)task->wchar,
2269 (unsigned long long)task->syscr,
2270 (unsigned long long)task->syscw,
2272 (unsigned long long)task->ioac.read_bytes,
2273 (unsigned long long)task->ioac.write_bytes,
2274 (unsigned long long)task->ioac.cancelled_write_bytes);
2281 static const struct file_operations proc_task_operations;
2282 static const struct inode_operations proc_task_inode_operations;
2284 static const struct pid_entry tgid_base_stuff[] = {
2285 DIR("task", S_IRUGO|S_IXUGO, task),
2286 DIR("fd", S_IRUSR|S_IXUSR, fd),
2287 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2288 REG("environ", S_IRUSR, environ),
2289 INF("auxv", S_IRUSR, pid_auxv),
2290 ONE("status", S_IRUGO, pid_status),
2291 INF("limits", S_IRUSR, pid_limits),
2292 #ifdef CONFIG_SCHED_DEBUG
2293 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2295 INF("cmdline", S_IRUGO, pid_cmdline),
2296 ONE("stat", S_IRUGO, tgid_stat),
2297 ONE("statm", S_IRUGO, pid_statm),
2298 REG("maps", S_IRUGO, maps),
2300 REG("numa_maps", S_IRUGO, numa_maps),
2302 REG("mem", S_IRUSR|S_IWUSR, mem),
2306 REG("mounts", S_IRUGO, mounts),
2307 REG("mountstats", S_IRUSR, mountstats),
2308 #ifdef CONFIG_PROC_PAGE_MONITOR
2309 REG("clear_refs", S_IWUSR, clear_refs),
2310 REG("smaps", S_IRUGO, smaps),
2311 REG("pagemap", S_IRUSR, pagemap),
2313 #ifdef CONFIG_SECURITY
2314 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2316 #ifdef CONFIG_KALLSYMS
2317 INF("wchan", S_IRUGO, pid_wchan),
2319 #ifdef CONFIG_SCHEDSTATS
2320 INF("schedstat", S_IRUGO, pid_schedstat),
2322 #ifdef CONFIG_LATENCYTOP
2323 REG("latency", S_IRUGO, lstats),
2325 #ifdef CONFIG_PROC_PID_CPUSET
2326 REG("cpuset", S_IRUGO, cpuset),
2328 #ifdef CONFIG_CGROUPS
2329 REG("cgroup", S_IRUGO, cgroup),
2331 INF("oom_score", S_IRUGO, oom_score),
2332 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2333 #ifdef CONFIG_AUDITSYSCALL
2334 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2336 #ifdef CONFIG_FAULT_INJECTION
2337 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2339 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2340 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2342 #ifdef CONFIG_TASK_IO_ACCOUNTING
2343 INF("io", S_IRUGO, pid_io_accounting),
2347 static int proc_tgid_base_readdir(struct file * filp,
2348 void * dirent, filldir_t filldir)
2350 return proc_pident_readdir(filp,dirent,filldir,
2351 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2354 static const struct file_operations proc_tgid_base_operations = {
2355 .read = generic_read_dir,
2356 .readdir = proc_tgid_base_readdir,
2359 static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2360 return proc_pident_lookup(dir, dentry,
2361 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
2364 static const struct inode_operations proc_tgid_base_inode_operations = {
2365 .lookup = proc_tgid_base_lookup,
2366 .getattr = pid_getattr,
2367 .setattr = proc_setattr,
2370 static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
2372 struct dentry *dentry, *leader, *dir;
2373 char buf[PROC_NUMBUF];
2377 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2378 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
2380 if (!(current->flags & PF_EXITING))
2381 shrink_dcache_parent(dentry);
2390 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2391 leader = d_hash_and_lookup(mnt->mnt_root, &name);
2396 name.len = strlen(name.name);
2397 dir = d_hash_and_lookup(leader, &name);
2399 goto out_put_leader;
2402 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2403 dentry = d_hash_and_lookup(dir, &name);
2405 shrink_dcache_parent(dentry);
2418 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2419 * @task: task that should be flushed.
2421 * When flushing dentries from proc, one needs to flush them from global
2422 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2423 * in. This call is supposed to do all of this job.
2425 * Looks in the dcache for
2427 * /proc/@tgid/task/@pid
2428 * if either directory is present flushes it and all of it'ts children
2431 * It is safe and reasonable to cache /proc entries for a task until
2432 * that task exits. After that they just clog up the dcache with
2433 * useless entries, possibly causing useful dcache entries to be
2434 * flushed instead. This routine is proved to flush those useless
2435 * dcache entries at process exit time.
2437 * NOTE: This routine is just an optimization so it does not guarantee
2438 * that no dcache entries will exist at process exit time it
2439 * just makes it very unlikely that any will persist.
2442 void proc_flush_task(struct task_struct *task)
2445 struct pid *pid, *tgid = NULL;
2448 pid = task_pid(task);
2449 if (thread_group_leader(task))
2450 tgid = task_tgid(task);
2452 for (i = 0; i <= pid->level; i++) {
2453 upid = &pid->numbers[i];
2454 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
2455 tgid ? tgid->numbers[i].nr : 0);
2458 upid = &pid->numbers[pid->level];
2460 pid_ns_release_proc(upid->ns);
2463 static struct dentry *proc_pid_instantiate(struct inode *dir,
2464 struct dentry * dentry,
2465 struct task_struct *task, const void *ptr)
2467 struct dentry *error = ERR_PTR(-ENOENT);
2468 struct inode *inode;
2470 inode = proc_pid_make_inode(dir->i_sb, task);
2474 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2475 inode->i_op = &proc_tgid_base_inode_operations;
2476 inode->i_fop = &proc_tgid_base_operations;
2477 inode->i_flags|=S_IMMUTABLE;
2479 #ifdef CONFIG_SECURITY
2480 inode->i_nlink += 1;
2483 dentry->d_op = &pid_dentry_operations;
2485 d_add(dentry, inode);
2486 /* Close the race of the process dying before we return the dentry */
2487 if (pid_revalidate(dentry, NULL))
2493 struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2495 struct dentry *result = ERR_PTR(-ENOENT);
2496 struct task_struct *task;
2498 struct pid_namespace *ns;
2500 result = proc_base_lookup(dir, dentry);
2501 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2504 tgid = name_to_int(dentry);
2508 ns = dentry->d_sb->s_fs_info;
2510 task = find_task_by_pid_ns(tgid, ns);
2512 get_task_struct(task);
2517 result = proc_pid_instantiate(dir, dentry, task, NULL);
2518 put_task_struct(task);
2524 * Find the first task with tgid >= tgid
2529 struct task_struct *task;
2531 static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2536 put_task_struct(iter.task);
2540 pid = find_ge_pid(iter.tgid, ns);
2542 iter.tgid = pid_nr_ns(pid, ns);
2543 iter.task = pid_task(pid, PIDTYPE_PID);
2544 /* What we to know is if the pid we have find is the
2545 * pid of a thread_group_leader. Testing for task
2546 * being a thread_group_leader is the obvious thing
2547 * todo but there is a window when it fails, due to
2548 * the pid transfer logic in de_thread.
2550 * So we perform the straight forward test of seeing
2551 * if the pid we have found is the pid of a thread
2552 * group leader, and don't worry if the task we have
2553 * found doesn't happen to be a thread group leader.
2554 * As we don't care in the case of readdir.
2556 if (!iter.task || !has_group_leader_pid(iter.task)) {
2560 get_task_struct(iter.task);
2566 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2568 static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2569 struct tgid_iter iter)
2571 char name[PROC_NUMBUF];
2572 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
2573 return proc_fill_cache(filp, dirent, filldir, name, len,
2574 proc_pid_instantiate, iter.task, NULL);
2577 /* for the /proc/ directory itself, after non-process stuff has been done */
2578 int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2580 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
2581 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
2582 struct tgid_iter iter;
2583 struct pid_namespace *ns;
2588 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
2589 const struct pid_entry *p = &proc_base_stuff[nr];
2590 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
2594 ns = filp->f_dentry->d_sb->s_fs_info;
2596 iter.tgid = filp->f_pos - TGID_OFFSET;
2597 for (iter = next_tgid(ns, iter);
2599 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2600 filp->f_pos = iter.tgid + TGID_OFFSET;
2601 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2602 put_task_struct(iter.task);
2606 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2608 put_task_struct(reaper);
2616 static const struct pid_entry tid_base_stuff[] = {
2617 DIR("fd", S_IRUSR|S_IXUSR, fd),
2618 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
2619 REG("environ", S_IRUSR, environ),
2620 INF("auxv", S_IRUSR, pid_auxv),
2621 ONE("status", S_IRUGO, pid_status),
2622 INF("limits", S_IRUSR, pid_limits),
2623 #ifdef CONFIG_SCHED_DEBUG
2624 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2626 INF("cmdline", S_IRUGO, pid_cmdline),
2627 ONE("stat", S_IRUGO, tid_stat),
2628 ONE("statm", S_IRUGO, pid_statm),
2629 REG("maps", S_IRUGO, maps),
2631 REG("numa_maps", S_IRUGO, numa_maps),
2633 REG("mem", S_IRUSR|S_IWUSR, mem),
2637 REG("mounts", S_IRUGO, mounts),
2638 #ifdef CONFIG_PROC_PAGE_MONITOR
2639 REG("clear_refs", S_IWUSR, clear_refs),
2640 REG("smaps", S_IRUGO, smaps),
2641 REG("pagemap", S_IRUSR, pagemap),
2643 #ifdef CONFIG_SECURITY
2644 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
2646 #ifdef CONFIG_KALLSYMS
2647 INF("wchan", S_IRUGO, pid_wchan),
2649 #ifdef CONFIG_SCHEDSTATS
2650 INF("schedstat", S_IRUGO, pid_schedstat),
2652 #ifdef CONFIG_LATENCYTOP
2653 REG("latency", S_IRUGO, lstats),
2655 #ifdef CONFIG_PROC_PID_CPUSET
2656 REG("cpuset", S_IRUGO, cpuset),
2658 #ifdef CONFIG_CGROUPS
2659 REG("cgroup", S_IRUGO, cgroup),
2661 INF("oom_score", S_IRUGO, oom_score),
2662 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
2663 #ifdef CONFIG_AUDITSYSCALL
2664 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
2666 #ifdef CONFIG_FAULT_INJECTION
2667 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2671 static int proc_tid_base_readdir(struct file * filp,
2672 void * dirent, filldir_t filldir)
2674 return proc_pident_readdir(filp,dirent,filldir,
2675 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2678 static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
2679 return proc_pident_lookup(dir, dentry,
2680 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
2683 static const struct file_operations proc_tid_base_operations = {
2684 .read = generic_read_dir,
2685 .readdir = proc_tid_base_readdir,
2688 static const struct inode_operations proc_tid_base_inode_operations = {
2689 .lookup = proc_tid_base_lookup,
2690 .getattr = pid_getattr,
2691 .setattr = proc_setattr,
2694 static struct dentry *proc_task_instantiate(struct inode *dir,
2695 struct dentry *dentry, struct task_struct *task, const void *ptr)
2697 struct dentry *error = ERR_PTR(-ENOENT);
2698 struct inode *inode;
2699 inode = proc_pid_make_inode(dir->i_sb, task);
2703 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2704 inode->i_op = &proc_tid_base_inode_operations;
2705 inode->i_fop = &proc_tid_base_operations;
2706 inode->i_flags|=S_IMMUTABLE;
2708 #ifdef CONFIG_SECURITY
2709 inode->i_nlink += 1;
2712 dentry->d_op = &pid_dentry_operations;
2714 d_add(dentry, inode);
2715 /* Close the race of the process dying before we return the dentry */
2716 if (pid_revalidate(dentry, NULL))
2722 static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2724 struct dentry *result = ERR_PTR(-ENOENT);
2725 struct task_struct *task;
2726 struct task_struct *leader = get_proc_task(dir);
2728 struct pid_namespace *ns;
2733 tid = name_to_int(dentry);
2737 ns = dentry->d_sb->s_fs_info;
2739 task = find_task_by_pid_ns(tid, ns);
2741 get_task_struct(task);
2745 if (!same_thread_group(leader, task))
2748 result = proc_task_instantiate(dir, dentry, task, NULL);
2750 put_task_struct(task);
2752 put_task_struct(leader);
2758 * Find the first tid of a thread group to return to user space.
2760 * Usually this is just the thread group leader, but if the users
2761 * buffer was too small or there was a seek into the middle of the
2762 * directory we have more work todo.
2764 * In the case of a short read we start with find_task_by_pid.
2766 * In the case of a seek we start with the leader and walk nr
2769 static struct task_struct *first_tid(struct task_struct *leader,
2770 int tid, int nr, struct pid_namespace *ns)
2772 struct task_struct *pos;
2775 /* Attempt to start with the pid of a thread */
2776 if (tid && (nr > 0)) {
2777 pos = find_task_by_pid_ns(tid, ns);
2778 if (pos && (pos->group_leader == leader))
2782 /* If nr exceeds the number of threads there is nothing todo */
2784 if (nr && nr >= get_nr_threads(leader))
2787 /* If we haven't found our starting place yet start
2788 * with the leader and walk nr threads forward.
2790 for (pos = leader; nr > 0; --nr) {
2791 pos = next_thread(pos);
2792 if (pos == leader) {
2798 get_task_struct(pos);
2805 * Find the next thread in the thread list.
2806 * Return NULL if there is an error or no next thread.
2808 * The reference to the input task_struct is released.
2810 static struct task_struct *next_tid(struct task_struct *start)
2812 struct task_struct *pos = NULL;
2814 if (pid_alive(start)) {
2815 pos = next_thread(start);
2816 if (thread_group_leader(pos))
2819 get_task_struct(pos);
2822 put_task_struct(start);
2826 static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2827 struct task_struct *task, int tid)
2829 char name[PROC_NUMBUF];
2830 int len = snprintf(name, sizeof(name), "%d", tid);
2831 return proc_fill_cache(filp, dirent, filldir, name, len,
2832 proc_task_instantiate, task, NULL);
2835 /* for the /proc/TGID/task/ directories */
2836 static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2838 struct dentry *dentry = filp->f_path.dentry;
2839 struct inode *inode = dentry->d_inode;
2840 struct task_struct *leader = NULL;
2841 struct task_struct *task;
2842 int retval = -ENOENT;
2845 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
2846 struct pid_namespace *ns;
2848 task = get_proc_task(inode);
2852 if (pid_alive(task)) {
2853 leader = task->group_leader;
2854 get_task_struct(leader);
2857 put_task_struct(task);
2865 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2870 ino = parent_ino(dentry);
2871 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2877 /* f_version caches the tgid value that the last readdir call couldn't
2878 * return. lseek aka telldir automagically resets f_version to 0.
2880 ns = filp->f_dentry->d_sb->s_fs_info;
2881 tid = (int)filp->f_version;
2882 filp->f_version = 0;
2883 for (task = first_tid(leader, tid, pos - 2, ns);
2885 task = next_tid(task), pos++) {
2886 tid = task_pid_nr_ns(task, ns);
2887 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
2888 /* returning this tgid failed, save it as the first
2889 * pid for the next readir call */
2890 filp->f_version = (u64)tid;
2891 put_task_struct(task);
2897 put_task_struct(leader);
2902 static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2904 struct inode *inode = dentry->d_inode;
2905 struct task_struct *p = get_proc_task(inode);
2906 generic_fillattr(inode, stat);
2910 stat->nlink += get_nr_threads(p);
2918 static const struct inode_operations proc_task_inode_operations = {
2919 .lookup = proc_task_lookup,
2920 .getattr = proc_task_getattr,
2921 .setattr = proc_setattr,
2924 static const struct file_operations proc_task_operations = {
2925 .read = generic_read_dir,
2926 .readdir = proc_task_readdir,