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/cpuset.h>
71 #include <linux/audit.h>
72 #include <linux/poll.h>
73 #include <linux/nsproxy.h>
74 #include <linux/oom.h>
78 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
88 /* Worst case buffer size needed for holding an integer. */
89 #define PROC_NUMBUF 13
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 } )
126 EXPORT_SYMBOL(maps_protect
);
128 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
130 struct fs_struct
*fs
;
134 atomic_inc(&fs
->count
);
139 static int get_nr_threads(struct task_struct
*tsk
)
141 /* Must be called with the rcu_read_lock held */
145 if (lock_task_sighand(tsk
, &flags
)) {
146 count
= atomic_read(&tsk
->signal
->count
);
147 unlock_task_sighand(tsk
, &flags
);
152 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
154 struct task_struct
*task
= get_proc_task(inode
);
155 struct fs_struct
*fs
= NULL
;
156 int result
= -ENOENT
;
159 fs
= get_fs_struct(task
);
160 put_task_struct(task
);
163 read_lock(&fs
->lock
);
164 *mnt
= mntget(fs
->pwdmnt
);
165 *dentry
= dget(fs
->pwd
);
166 read_unlock(&fs
->lock
);
173 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
175 struct task_struct
*task
= get_proc_task(inode
);
176 struct fs_struct
*fs
= NULL
;
177 int result
= -ENOENT
;
180 fs
= get_fs_struct(task
);
181 put_task_struct(task
);
184 read_lock(&fs
->lock
);
185 *mnt
= mntget(fs
->rootmnt
);
186 *dentry
= dget(fs
->root
);
187 read_unlock(&fs
->lock
);
194 #define MAY_PTRACE(task) \
195 (task == current || \
196 (task->parent == current && \
197 (task->ptrace & PT_PTRACED) && \
198 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
199 security_ptrace(current,task) == 0))
201 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
204 struct mm_struct
*mm
= get_task_mm(task
);
209 if (!ptrace_may_attach(task
))
212 len
= mm
->env_end
- mm
->env_start
;
215 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
222 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
226 struct mm_struct
*mm
= get_task_mm(task
);
230 goto out_mm
; /* Shh! No looking before we're done */
232 len
= mm
->arg_end
- mm
->arg_start
;
237 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
239 // If the nul at the end of args has been overwritten, then
240 // assume application is using setproctitle(3).
241 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
242 len
= strnlen(buffer
, res
);
246 len
= mm
->env_end
- mm
->env_start
;
247 if (len
> PAGE_SIZE
- res
)
248 len
= PAGE_SIZE
- res
;
249 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
250 res
= strnlen(buffer
, res
);
259 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
262 struct mm_struct
*mm
= get_task_mm(task
);
264 unsigned int nwords
= 0;
267 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
268 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
271 memcpy(buffer
, mm
->saved_auxv
, res
);
278 #ifdef CONFIG_KALLSYMS
280 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
281 * Returns the resolved symbol. If that fails, simply return the address.
283 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
286 char symname
[KSYM_NAME_LEN
];
288 wchan
= get_wchan(task
);
290 if (lookup_symbol_name(wchan
, symname
) < 0)
291 return sprintf(buffer
, "%lu", wchan
);
293 return sprintf(buffer
, "%s", symname
);
295 #endif /* CONFIG_KALLSYMS */
297 #ifdef CONFIG_SCHEDSTATS
299 * Provides /proc/PID/schedstat
301 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
303 return sprintf(buffer
, "%llu %llu %lu\n",
304 task
->sched_info
.cpu_time
,
305 task
->sched_info
.run_delay
,
306 task
->sched_info
.pcnt
);
310 /* The badness from the OOM killer */
311 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
312 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
314 unsigned long points
;
315 struct timespec uptime
;
317 do_posix_clock_monotonic_gettime(&uptime
);
318 read_lock(&tasklist_lock
);
319 points
= badness(task
, uptime
.tv_sec
);
320 read_unlock(&tasklist_lock
);
321 return sprintf(buffer
, "%lu\n", points
);
324 /************************************************************************/
325 /* Here the fs part begins */
326 /************************************************************************/
328 /* permission checks */
329 static int proc_fd_access_allowed(struct inode
*inode
)
331 struct task_struct
*task
;
333 /* Allow access to a task's file descriptors if it is us or we
334 * may use ptrace attach to the process and find out that
337 task
= get_proc_task(inode
);
339 allowed
= ptrace_may_attach(task
);
340 put_task_struct(task
);
345 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
348 struct inode
*inode
= dentry
->d_inode
;
350 if (attr
->ia_valid
& ATTR_MODE
)
353 error
= inode_change_ok(inode
, attr
);
355 error
= inode_setattr(inode
, attr
);
359 static const struct inode_operations proc_def_inode_operations
= {
360 .setattr
= proc_setattr
,
363 extern struct seq_operations mounts_op
;
369 static int mounts_open(struct inode
*inode
, struct file
*file
)
371 struct task_struct
*task
= get_proc_task(inode
);
372 struct mnt_namespace
*ns
= NULL
;
373 struct proc_mounts
*p
;
379 ns
= task
->nsproxy
->mnt_ns
;
384 put_task_struct(task
);
389 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
391 file
->private_data
= &p
->m
;
392 ret
= seq_open(file
, &mounts_op
);
395 p
->event
= ns
->event
;
405 static int mounts_release(struct inode
*inode
, struct file
*file
)
407 struct seq_file
*m
= file
->private_data
;
408 struct mnt_namespace
*ns
= m
->private;
410 return seq_release(inode
, file
);
413 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
415 struct proc_mounts
*p
= file
->private_data
;
416 struct mnt_namespace
*ns
= p
->m
.private;
419 poll_wait(file
, &ns
->poll
, wait
);
421 spin_lock(&vfsmount_lock
);
422 if (p
->event
!= ns
->event
) {
423 p
->event
= ns
->event
;
426 spin_unlock(&vfsmount_lock
);
431 static const struct file_operations proc_mounts_operations
= {
435 .release
= mounts_release
,
439 extern struct seq_operations mountstats_op
;
440 static int mountstats_open(struct inode
*inode
, struct file
*file
)
442 int ret
= seq_open(file
, &mountstats_op
);
445 struct seq_file
*m
= file
->private_data
;
446 struct mnt_namespace
*mnt_ns
= NULL
;
447 struct task_struct
*task
= get_proc_task(inode
);
452 mnt_ns
= task
->nsproxy
->mnt_ns
;
456 put_task_struct(task
);
462 seq_release(inode
, file
);
469 static const struct file_operations proc_mountstats_operations
= {
470 .open
= mountstats_open
,
473 .release
= mounts_release
,
476 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
478 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
479 size_t count
, loff_t
*ppos
)
481 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
484 struct task_struct
*task
= get_proc_task(inode
);
490 if (count
> PROC_BLOCK_SIZE
)
491 count
= PROC_BLOCK_SIZE
;
494 if (!(page
= __get_free_page(GFP_KERNEL
)))
497 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
500 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
503 put_task_struct(task
);
508 static const struct file_operations proc_info_file_operations
= {
509 .read
= proc_info_read
,
512 static int mem_open(struct inode
* inode
, struct file
* file
)
514 file
->private_data
= (void*)((long)current
->self_exec_id
);
518 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
519 size_t count
, loff_t
*ppos
)
521 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
523 unsigned long src
= *ppos
;
525 struct mm_struct
*mm
;
530 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
534 page
= (char *)__get_free_page(GFP_USER
);
540 mm
= get_task_mm(task
);
546 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
552 int this_len
, retval
;
554 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
555 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
556 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
562 if (copy_to_user(buf
, page
, retval
)) {
577 free_page((unsigned long) page
);
579 put_task_struct(task
);
584 #define mem_write NULL
587 /* This is a security hazard */
588 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
589 size_t count
, loff_t
*ppos
)
593 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
594 unsigned long dst
= *ppos
;
600 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
604 page
= (char *)__get_free_page(GFP_USER
);
610 int this_len
, retval
;
612 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
613 if (copy_from_user(page
, buf
, this_len
)) {
617 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
629 free_page((unsigned long) page
);
631 put_task_struct(task
);
637 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
641 file
->f_pos
= offset
;
644 file
->f_pos
+= offset
;
649 force_successful_syscall_return();
653 static const struct file_operations proc_mem_operations
= {
660 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
661 size_t count
, loff_t
*ppos
)
663 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
664 char buffer
[PROC_NUMBUF
];
670 oom_adjust
= task
->oomkilladj
;
671 put_task_struct(task
);
673 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
675 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
678 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
679 size_t count
, loff_t
*ppos
)
681 struct task_struct
*task
;
682 char buffer
[PROC_NUMBUF
], *end
;
685 memset(buffer
, 0, sizeof(buffer
));
686 if (count
> sizeof(buffer
) - 1)
687 count
= sizeof(buffer
) - 1;
688 if (copy_from_user(buffer
, buf
, count
))
690 oom_adjust
= simple_strtol(buffer
, &end
, 0);
691 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
692 oom_adjust
!= OOM_DISABLE
)
696 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
699 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
700 put_task_struct(task
);
703 task
->oomkilladj
= oom_adjust
;
704 put_task_struct(task
);
705 if (end
- buffer
== 0)
710 static const struct file_operations proc_oom_adjust_operations
= {
711 .read
= oom_adjust_read
,
712 .write
= oom_adjust_write
,
716 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
717 size_t count
, loff_t
*ppos
)
719 struct task_struct
*task
;
720 char buffer
[PROC_NUMBUF
], *end
;
721 struct mm_struct
*mm
;
723 memset(buffer
, 0, sizeof(buffer
));
724 if (count
> sizeof(buffer
) - 1)
725 count
= sizeof(buffer
) - 1;
726 if (copy_from_user(buffer
, buf
, count
))
728 if (!simple_strtol(buffer
, &end
, 0))
732 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
735 mm
= get_task_mm(task
);
740 put_task_struct(task
);
741 if (end
- buffer
== 0)
746 static struct file_operations proc_clear_refs_operations
= {
747 .write
= clear_refs_write
,
751 #ifdef CONFIG_AUDITSYSCALL
753 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
754 size_t count
, loff_t
*ppos
)
756 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
757 struct task_struct
*task
= get_proc_task(inode
);
759 char tmpbuf
[TMPBUFLEN
];
763 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
764 audit_get_loginuid(task
->audit_context
));
765 put_task_struct(task
);
766 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
769 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
770 size_t count
, loff_t
*ppos
)
772 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
777 if (!capable(CAP_AUDIT_CONTROL
))
780 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
783 if (count
>= PAGE_SIZE
)
784 count
= PAGE_SIZE
- 1;
787 /* No partial writes. */
790 page
= (char*)__get_free_page(GFP_USER
);
794 if (copy_from_user(page
, buf
, count
))
798 loginuid
= simple_strtoul(page
, &tmp
, 10);
804 length
= audit_set_loginuid(current
, loginuid
);
805 if (likely(length
== 0))
809 free_page((unsigned long) page
);
813 static const struct file_operations proc_loginuid_operations
= {
814 .read
= proc_loginuid_read
,
815 .write
= proc_loginuid_write
,
819 #ifdef CONFIG_FAULT_INJECTION
820 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
821 size_t count
, loff_t
*ppos
)
823 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
824 char buffer
[PROC_NUMBUF
];
830 make_it_fail
= task
->make_it_fail
;
831 put_task_struct(task
);
833 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
835 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
838 static ssize_t
proc_fault_inject_write(struct file
* file
,
839 const char __user
* buf
, size_t count
, loff_t
*ppos
)
841 struct task_struct
*task
;
842 char buffer
[PROC_NUMBUF
], *end
;
845 if (!capable(CAP_SYS_RESOURCE
))
847 memset(buffer
, 0, sizeof(buffer
));
848 if (count
> sizeof(buffer
) - 1)
849 count
= sizeof(buffer
) - 1;
850 if (copy_from_user(buffer
, buf
, count
))
852 make_it_fail
= simple_strtol(buffer
, &end
, 0);
855 task
= get_proc_task(file
->f_dentry
->d_inode
);
858 task
->make_it_fail
= make_it_fail
;
859 put_task_struct(task
);
860 if (end
- buffer
== 0)
865 static const struct file_operations proc_fault_inject_operations
= {
866 .read
= proc_fault_inject_read
,
867 .write
= proc_fault_inject_write
,
871 #ifdef CONFIG_SCHED_DEBUG
873 * Print out various scheduling related per-task fields:
875 static int sched_show(struct seq_file
*m
, void *v
)
877 struct inode
*inode
= m
->private;
878 struct task_struct
*p
;
882 p
= get_proc_task(inode
);
885 proc_sched_show_task(p
, m
);
893 sched_write(struct file
*file
, const char __user
*buf
,
894 size_t count
, loff_t
*offset
)
896 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
897 struct task_struct
*p
;
901 p
= get_proc_task(inode
);
904 proc_sched_set_task(p
);
911 static int sched_open(struct inode
*inode
, struct file
*filp
)
915 ret
= single_open(filp
, sched_show
, NULL
);
917 struct seq_file
*m
= filp
->private_data
;
924 static const struct file_operations proc_pid_sched_operations
= {
927 .write
= sched_write
,
929 .release
= seq_release
,
934 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
936 struct inode
*inode
= dentry
->d_inode
;
939 /* We don't need a base pointer in the /proc filesystem */
942 /* Are we allowed to snoop on the tasks file descriptors? */
943 if (!proc_fd_access_allowed(inode
))
946 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
947 nd
->last_type
= LAST_BIND
;
949 return ERR_PTR(error
);
952 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
953 char __user
*buffer
, int buflen
)
955 struct inode
* inode
;
956 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
962 inode
= dentry
->d_inode
;
963 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
967 len
= tmp
+ PAGE_SIZE
- 1 - path
;
971 if (copy_to_user(buffer
, path
, len
))
974 free_page((unsigned long)tmp
);
978 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
981 struct inode
*inode
= dentry
->d_inode
;
983 struct vfsmount
*mnt
= NULL
;
985 /* Are we allowed to snoop on the tasks file descriptors? */
986 if (!proc_fd_access_allowed(inode
))
989 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
993 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1000 static const struct inode_operations proc_pid_link_inode_operations
= {
1001 .readlink
= proc_pid_readlink
,
1002 .follow_link
= proc_pid_follow_link
,
1003 .setattr
= proc_setattr
,
1007 /* building an inode */
1009 static int task_dumpable(struct task_struct
*task
)
1012 struct mm_struct
*mm
;
1017 dumpable
= mm
->dumpable
;
1025 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1027 struct inode
* inode
;
1028 struct proc_inode
*ei
;
1030 /* We need a new inode */
1032 inode
= new_inode(sb
);
1038 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1039 inode
->i_op
= &proc_def_inode_operations
;
1042 * grab the reference to task.
1044 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1050 if (task_dumpable(task
)) {
1051 inode
->i_uid
= task
->euid
;
1052 inode
->i_gid
= task
->egid
;
1054 security_task_to_inode(task
, inode
);
1064 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1066 struct inode
*inode
= dentry
->d_inode
;
1067 struct task_struct
*task
;
1068 generic_fillattr(inode
, stat
);
1073 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1075 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1076 task_dumpable(task
)) {
1077 stat
->uid
= task
->euid
;
1078 stat
->gid
= task
->egid
;
1088 * Exceptional case: normally we are not allowed to unhash a busy
1089 * directory. In this case, however, we can do it - no aliasing problems
1090 * due to the way we treat inodes.
1092 * Rewrite the inode's ownerships here because the owning task may have
1093 * performed a setuid(), etc.
1095 * Before the /proc/pid/status file was created the only way to read
1096 * the effective uid of a /process was to stat /proc/pid. Reading
1097 * /proc/pid/status is slow enough that procps and other packages
1098 * kept stating /proc/pid. To keep the rules in /proc simple I have
1099 * made this apply to all per process world readable and executable
1102 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1104 struct inode
*inode
= dentry
->d_inode
;
1105 struct task_struct
*task
= get_proc_task(inode
);
1107 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1108 task_dumpable(task
)) {
1109 inode
->i_uid
= task
->euid
;
1110 inode
->i_gid
= task
->egid
;
1115 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1116 security_task_to_inode(task
, inode
);
1117 put_task_struct(task
);
1124 static int pid_delete_dentry(struct dentry
* dentry
)
1126 /* Is the task we represent dead?
1127 * If so, then don't put the dentry on the lru list,
1128 * kill it immediately.
1130 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1133 static struct dentry_operations pid_dentry_operations
=
1135 .d_revalidate
= pid_revalidate
,
1136 .d_delete
= pid_delete_dentry
,
1141 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1142 struct task_struct
*, const void *);
1145 * Fill a directory entry.
1147 * If possible create the dcache entry and derive our inode number and
1148 * file type from dcache entry.
1150 * Since all of the proc inode numbers are dynamically generated, the inode
1151 * numbers do not exist until the inode is cache. This means creating the
1152 * the dcache entry in readdir is necessary to keep the inode numbers
1153 * reported by readdir in sync with the inode numbers reported
1156 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1157 char *name
, int len
,
1158 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1160 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1161 struct inode
*inode
;
1164 unsigned type
= DT_UNKNOWN
;
1168 qname
.hash
= full_name_hash(name
, len
);
1170 child
= d_lookup(dir
, &qname
);
1173 new = d_alloc(dir
, &qname
);
1175 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1182 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1183 goto end_instantiate
;
1184 inode
= child
->d_inode
;
1187 type
= inode
->i_mode
>> 12;
1192 ino
= find_inode_number(dir
, &qname
);
1195 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1198 static unsigned name_to_int(struct dentry
*dentry
)
1200 const char *name
= dentry
->d_name
.name
;
1201 int len
= dentry
->d_name
.len
;
1204 if (len
> 1 && *name
== '0')
1207 unsigned c
= *name
++ - '0';
1210 if (n
>= (~0U-9)/10)
1220 #define PROC_FDINFO_MAX 64
1222 static int proc_fd_info(struct inode
*inode
, struct dentry
**dentry
,
1223 struct vfsmount
**mnt
, char *info
)
1225 struct task_struct
*task
= get_proc_task(inode
);
1226 struct files_struct
*files
= NULL
;
1228 int fd
= proc_fd(inode
);
1231 files
= get_files_struct(task
);
1232 put_task_struct(task
);
1236 * We are not taking a ref to the file structure, so we must
1239 spin_lock(&files
->file_lock
);
1240 file
= fcheck_files(files
, fd
);
1243 *mnt
= mntget(file
->f_path
.mnt
);
1245 *dentry
= dget(file
->f_path
.dentry
);
1247 snprintf(info
, PROC_FDINFO_MAX
,
1250 (long long) file
->f_pos
,
1252 spin_unlock(&files
->file_lock
);
1253 put_files_struct(files
);
1256 spin_unlock(&files
->file_lock
);
1257 put_files_struct(files
);
1262 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
,
1263 struct vfsmount
**mnt
)
1265 return proc_fd_info(inode
, dentry
, mnt
, NULL
);
1268 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1270 struct inode
*inode
= dentry
->d_inode
;
1271 struct task_struct
*task
= get_proc_task(inode
);
1272 int fd
= proc_fd(inode
);
1273 struct files_struct
*files
;
1276 files
= get_files_struct(task
);
1279 if (fcheck_files(files
, fd
)) {
1281 put_files_struct(files
);
1282 if (task_dumpable(task
)) {
1283 inode
->i_uid
= task
->euid
;
1284 inode
->i_gid
= task
->egid
;
1289 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1290 security_task_to_inode(task
, inode
);
1291 put_task_struct(task
);
1295 put_files_struct(files
);
1297 put_task_struct(task
);
1303 static struct dentry_operations tid_fd_dentry_operations
=
1305 .d_revalidate
= tid_fd_revalidate
,
1306 .d_delete
= pid_delete_dentry
,
1309 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1310 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1312 unsigned fd
= *(const unsigned *)ptr
;
1314 struct files_struct
*files
;
1315 struct inode
*inode
;
1316 struct proc_inode
*ei
;
1317 struct dentry
*error
= ERR_PTR(-ENOENT
);
1319 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1324 files
= get_files_struct(task
);
1327 inode
->i_mode
= S_IFLNK
;
1330 * We are not taking a ref to the file structure, so we must
1333 spin_lock(&files
->file_lock
);
1334 file
= fcheck_files(files
, fd
);
1337 if (file
->f_mode
& 1)
1338 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1339 if (file
->f_mode
& 2)
1340 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1341 spin_unlock(&files
->file_lock
);
1342 put_files_struct(files
);
1344 inode
->i_op
= &proc_pid_link_inode_operations
;
1346 ei
->op
.proc_get_link
= proc_fd_link
;
1347 dentry
->d_op
= &tid_fd_dentry_operations
;
1348 d_add(dentry
, inode
);
1349 /* Close the race of the process dying before we return the dentry */
1350 if (tid_fd_revalidate(dentry
, NULL
))
1356 spin_unlock(&files
->file_lock
);
1357 put_files_struct(files
);
1363 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1364 struct dentry
*dentry
,
1365 instantiate_t instantiate
)
1367 struct task_struct
*task
= get_proc_task(dir
);
1368 unsigned fd
= name_to_int(dentry
);
1369 struct dentry
*result
= ERR_PTR(-ENOENT
);
1376 result
= instantiate(dir
, dentry
, task
, &fd
);
1378 put_task_struct(task
);
1383 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1384 filldir_t filldir
, instantiate_t instantiate
)
1386 struct dentry
*dentry
= filp
->f_path
.dentry
;
1387 struct inode
*inode
= dentry
->d_inode
;
1388 struct task_struct
*p
= get_proc_task(inode
);
1389 unsigned int fd
, tid
, ino
;
1391 struct files_struct
* files
;
1392 struct fdtable
*fdt
;
1403 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1407 ino
= parent_ino(dentry
);
1408 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1412 files
= get_files_struct(p
);
1416 fdt
= files_fdtable(files
);
1417 for (fd
= filp
->f_pos
-2;
1419 fd
++, filp
->f_pos
++) {
1420 char name
[PROC_NUMBUF
];
1423 if (!fcheck_files(files
, fd
))
1427 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1428 if (proc_fill_cache(filp
, dirent
, filldir
,
1429 name
, len
, instantiate
,
1437 put_files_struct(files
);
1445 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1446 struct nameidata
*nd
)
1448 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1451 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1453 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1456 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1457 size_t len
, loff_t
*ppos
)
1459 char tmp
[PROC_FDINFO_MAX
];
1460 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, NULL
, tmp
);
1462 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1466 static const struct file_operations proc_fdinfo_file_operations
= {
1467 .open
= nonseekable_open
,
1468 .read
= proc_fdinfo_read
,
1471 static const struct file_operations proc_fd_operations
= {
1472 .read
= generic_read_dir
,
1473 .readdir
= proc_readfd
,
1477 * /proc/pid/fd needs a special permission handler so that a process can still
1478 * access /proc/self/fd after it has executed a setuid().
1480 static int proc_fd_permission(struct inode
*inode
, int mask
,
1481 struct nameidata
*nd
)
1485 rv
= generic_permission(inode
, mask
, NULL
);
1488 if (task_pid(current
) == proc_pid(inode
))
1494 * proc directories can do almost nothing..
1496 static const struct inode_operations proc_fd_inode_operations
= {
1497 .lookup
= proc_lookupfd
,
1498 .permission
= proc_fd_permission
,
1499 .setattr
= proc_setattr
,
1502 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1503 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1505 unsigned fd
= *(unsigned *)ptr
;
1506 struct inode
*inode
;
1507 struct proc_inode
*ei
;
1508 struct dentry
*error
= ERR_PTR(-ENOENT
);
1510 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1515 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1516 inode
->i_fop
= &proc_fdinfo_file_operations
;
1517 dentry
->d_op
= &tid_fd_dentry_operations
;
1518 d_add(dentry
, inode
);
1519 /* Close the race of the process dying before we return the dentry */
1520 if (tid_fd_revalidate(dentry
, NULL
))
1527 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1528 struct dentry
*dentry
,
1529 struct nameidata
*nd
)
1531 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1534 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1536 return proc_readfd_common(filp
, dirent
, filldir
,
1537 proc_fdinfo_instantiate
);
1540 static const struct file_operations proc_fdinfo_operations
= {
1541 .read
= generic_read_dir
,
1542 .readdir
= proc_readfdinfo
,
1546 * proc directories can do almost nothing..
1548 static const struct inode_operations proc_fdinfo_inode_operations
= {
1549 .lookup
= proc_lookupfdinfo
,
1550 .setattr
= proc_setattr
,
1554 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1555 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1557 const struct pid_entry
*p
= ptr
;
1558 struct inode
*inode
;
1559 struct proc_inode
*ei
;
1560 struct dentry
*error
= ERR_PTR(-EINVAL
);
1562 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1567 inode
->i_mode
= p
->mode
;
1568 if (S_ISDIR(inode
->i_mode
))
1569 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1571 inode
->i_op
= p
->iop
;
1573 inode
->i_fop
= p
->fop
;
1575 dentry
->d_op
= &pid_dentry_operations
;
1576 d_add(dentry
, inode
);
1577 /* Close the race of the process dying before we return the dentry */
1578 if (pid_revalidate(dentry
, NULL
))
1584 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1585 struct dentry
*dentry
,
1586 const struct pid_entry
*ents
,
1589 struct inode
*inode
;
1590 struct dentry
*error
;
1591 struct task_struct
*task
= get_proc_task(dir
);
1592 const struct pid_entry
*p
, *last
;
1594 error
= ERR_PTR(-ENOENT
);
1601 * Yes, it does not scale. And it should not. Don't add
1602 * new entries into /proc/<tgid>/ without very good reasons.
1604 last
= &ents
[nents
- 1];
1605 for (p
= ents
; p
<= last
; p
++) {
1606 if (p
->len
!= dentry
->d_name
.len
)
1608 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1614 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1616 put_task_struct(task
);
1621 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1622 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1624 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1625 proc_pident_instantiate
, task
, p
);
1628 static int proc_pident_readdir(struct file
*filp
,
1629 void *dirent
, filldir_t filldir
,
1630 const struct pid_entry
*ents
, unsigned int nents
)
1634 struct dentry
*dentry
= filp
->f_path
.dentry
;
1635 struct inode
*inode
= dentry
->d_inode
;
1636 struct task_struct
*task
= get_proc_task(inode
);
1637 const struct pid_entry
*p
, *last
;
1651 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1657 ino
= parent_ino(dentry
);
1658 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1670 last
= &ents
[nents
- 1];
1672 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
1681 put_task_struct(task
);
1686 #ifdef CONFIG_SECURITY
1687 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1688 size_t count
, loff_t
*ppos
)
1690 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1693 struct task_struct
*task
= get_proc_task(inode
);
1698 length
= security_getprocattr(task
,
1699 (char*)file
->f_path
.dentry
->d_name
.name
,
1701 put_task_struct(task
);
1703 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
1708 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1709 size_t count
, loff_t
*ppos
)
1711 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1714 struct task_struct
*task
= get_proc_task(inode
);
1719 if (count
> PAGE_SIZE
)
1722 /* No partial writes. */
1728 page
= (char*)__get_free_page(GFP_USER
);
1733 if (copy_from_user(page
, buf
, count
))
1736 length
= security_setprocattr(task
,
1737 (char*)file
->f_path
.dentry
->d_name
.name
,
1738 (void*)page
, count
);
1740 free_page((unsigned long) page
);
1742 put_task_struct(task
);
1747 static const struct file_operations proc_pid_attr_operations
= {
1748 .read
= proc_pid_attr_read
,
1749 .write
= proc_pid_attr_write
,
1752 static const struct pid_entry attr_dir_stuff
[] = {
1753 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
1754 REG("prev", S_IRUGO
, pid_attr
),
1755 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
1756 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1757 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1758 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1761 static int proc_attr_dir_readdir(struct file
* filp
,
1762 void * dirent
, filldir_t filldir
)
1764 return proc_pident_readdir(filp
,dirent
,filldir
,
1765 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
1768 static const struct file_operations proc_attr_dir_operations
= {
1769 .read
= generic_read_dir
,
1770 .readdir
= proc_attr_dir_readdir
,
1773 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
1774 struct dentry
*dentry
, struct nameidata
*nd
)
1776 return proc_pident_lookup(dir
, dentry
,
1777 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
1780 static const struct inode_operations proc_attr_dir_inode_operations
= {
1781 .lookup
= proc_attr_dir_lookup
,
1782 .getattr
= pid_getattr
,
1783 .setattr
= proc_setattr
,
1791 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1794 char tmp
[PROC_NUMBUF
];
1795 sprintf(tmp
, "%d", current
->tgid
);
1796 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1799 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1801 char tmp
[PROC_NUMBUF
];
1802 sprintf(tmp
, "%d", current
->tgid
);
1803 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1806 static const struct inode_operations proc_self_inode_operations
= {
1807 .readlink
= proc_self_readlink
,
1808 .follow_link
= proc_self_follow_link
,
1814 * These are the directory entries in the root directory of /proc
1815 * that properly belong to the /proc filesystem, as they describe
1816 * describe something that is process related.
1818 static const struct pid_entry proc_base_stuff
[] = {
1819 NOD("self", S_IFLNK
|S_IRWXUGO
,
1820 &proc_self_inode_operations
, NULL
, {}),
1824 * Exceptional case: normally we are not allowed to unhash a busy
1825 * directory. In this case, however, we can do it - no aliasing problems
1826 * due to the way we treat inodes.
1828 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1830 struct inode
*inode
= dentry
->d_inode
;
1831 struct task_struct
*task
= get_proc_task(inode
);
1833 put_task_struct(task
);
1840 static struct dentry_operations proc_base_dentry_operations
=
1842 .d_revalidate
= proc_base_revalidate
,
1843 .d_delete
= pid_delete_dentry
,
1846 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
1847 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1849 const struct pid_entry
*p
= ptr
;
1850 struct inode
*inode
;
1851 struct proc_inode
*ei
;
1852 struct dentry
*error
= ERR_PTR(-EINVAL
);
1854 /* Allocate the inode */
1855 error
= ERR_PTR(-ENOMEM
);
1856 inode
= new_inode(dir
->i_sb
);
1860 /* Initialize the inode */
1862 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1865 * grab the reference to the task.
1867 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1873 inode
->i_mode
= p
->mode
;
1874 if (S_ISDIR(inode
->i_mode
))
1876 if (S_ISLNK(inode
->i_mode
))
1879 inode
->i_op
= p
->iop
;
1881 inode
->i_fop
= p
->fop
;
1883 dentry
->d_op
= &proc_base_dentry_operations
;
1884 d_add(dentry
, inode
);
1893 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
1895 struct dentry
*error
;
1896 struct task_struct
*task
= get_proc_task(dir
);
1897 const struct pid_entry
*p
, *last
;
1899 error
= ERR_PTR(-ENOENT
);
1904 /* Lookup the directory entry */
1905 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
1906 for (p
= proc_base_stuff
; p
<= last
; p
++) {
1907 if (p
->len
!= dentry
->d_name
.len
)
1909 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1915 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
1918 put_task_struct(task
);
1923 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
1924 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1926 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1927 proc_base_instantiate
, task
, p
);
1930 #ifdef CONFIG_TASK_IO_ACCOUNTING
1931 static int proc_pid_io_accounting(struct task_struct
*task
, char *buffer
)
1933 return sprintf(buffer
,
1934 #ifdef CONFIG_TASK_XACCT
1940 "read_bytes: %llu\n"
1941 "write_bytes: %llu\n"
1942 "cancelled_write_bytes: %llu\n",
1943 #ifdef CONFIG_TASK_XACCT
1944 (unsigned long long)task
->rchar
,
1945 (unsigned long long)task
->wchar
,
1946 (unsigned long long)task
->syscr
,
1947 (unsigned long long)task
->syscw
,
1949 (unsigned long long)task
->ioac
.read_bytes
,
1950 (unsigned long long)task
->ioac
.write_bytes
,
1951 (unsigned long long)task
->ioac
.cancelled_write_bytes
);
1958 static const struct file_operations proc_task_operations
;
1959 static const struct inode_operations proc_task_inode_operations
;
1961 static const struct pid_entry tgid_base_stuff
[] = {
1962 DIR("task", S_IRUGO
|S_IXUGO
, task
),
1963 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
1964 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
1965 INF("environ", S_IRUSR
, pid_environ
),
1966 INF("auxv", S_IRUSR
, pid_auxv
),
1967 INF("status", S_IRUGO
, pid_status
),
1968 #ifdef CONFIG_SCHED_DEBUG
1969 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
1971 INF("cmdline", S_IRUGO
, pid_cmdline
),
1972 INF("stat", S_IRUGO
, tgid_stat
),
1973 INF("statm", S_IRUGO
, pid_statm
),
1974 REG("maps", S_IRUGO
, maps
),
1976 REG("numa_maps", S_IRUGO
, numa_maps
),
1978 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
1982 REG("mounts", S_IRUGO
, mounts
),
1983 REG("mountstats", S_IRUSR
, mountstats
),
1985 REG("clear_refs", S_IWUSR
, clear_refs
),
1986 REG("smaps", S_IRUGO
, smaps
),
1988 #ifdef CONFIG_SECURITY
1989 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
1991 #ifdef CONFIG_KALLSYMS
1992 INF("wchan", S_IRUGO
, pid_wchan
),
1994 #ifdef CONFIG_SCHEDSTATS
1995 INF("schedstat", S_IRUGO
, pid_schedstat
),
1997 #ifdef CONFIG_CPUSETS
1998 REG("cpuset", S_IRUGO
, cpuset
),
2000 INF("oom_score", S_IRUGO
, oom_score
),
2001 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2002 #ifdef CONFIG_AUDITSYSCALL
2003 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2005 #ifdef CONFIG_FAULT_INJECTION
2006 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2008 #ifdef CONFIG_TASK_IO_ACCOUNTING
2009 INF("io", S_IRUGO
, pid_io_accounting
),
2013 static int proc_tgid_base_readdir(struct file
* filp
,
2014 void * dirent
, filldir_t filldir
)
2016 return proc_pident_readdir(filp
,dirent
,filldir
,
2017 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2020 static const struct file_operations proc_tgid_base_operations
= {
2021 .read
= generic_read_dir
,
2022 .readdir
= proc_tgid_base_readdir
,
2025 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2026 return proc_pident_lookup(dir
, dentry
,
2027 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2030 static const struct inode_operations proc_tgid_base_inode_operations
= {
2031 .lookup
= proc_tgid_base_lookup
,
2032 .getattr
= pid_getattr
,
2033 .setattr
= proc_setattr
,
2037 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2039 * @task: task that should be flushed.
2041 * Looks in the dcache for
2043 * /proc/@tgid/task/@pid
2044 * if either directory is present flushes it and all of it'ts children
2047 * It is safe and reasonable to cache /proc entries for a task until
2048 * that task exits. After that they just clog up the dcache with
2049 * useless entries, possibly causing useful dcache entries to be
2050 * flushed instead. This routine is proved to flush those useless
2051 * dcache entries at process exit time.
2053 * NOTE: This routine is just an optimization so it does not guarantee
2054 * that no dcache entries will exist at process exit time it
2055 * just makes it very unlikely that any will persist.
2057 void proc_flush_task(struct task_struct
*task
)
2059 struct dentry
*dentry
, *leader
, *dir
;
2060 char buf
[PROC_NUMBUF
];
2064 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2065 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2067 shrink_dcache_parent(dentry
);
2072 if (thread_group_leader(task
))
2076 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
2077 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2082 name
.len
= strlen(name
.name
);
2083 dir
= d_hash_and_lookup(leader
, &name
);
2085 goto out_put_leader
;
2088 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2089 dentry
= d_hash_and_lookup(dir
, &name
);
2091 shrink_dcache_parent(dentry
);
2103 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2104 struct dentry
* dentry
,
2105 struct task_struct
*task
, const void *ptr
)
2107 struct dentry
*error
= ERR_PTR(-ENOENT
);
2108 struct inode
*inode
;
2110 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2114 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2115 inode
->i_op
= &proc_tgid_base_inode_operations
;
2116 inode
->i_fop
= &proc_tgid_base_operations
;
2117 inode
->i_flags
|=S_IMMUTABLE
;
2119 #ifdef CONFIG_SECURITY
2120 inode
->i_nlink
+= 1;
2123 dentry
->d_op
= &pid_dentry_operations
;
2125 d_add(dentry
, inode
);
2126 /* Close the race of the process dying before we return the dentry */
2127 if (pid_revalidate(dentry
, NULL
))
2133 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2135 struct dentry
*result
= ERR_PTR(-ENOENT
);
2136 struct task_struct
*task
;
2139 result
= proc_base_lookup(dir
, dentry
);
2140 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2143 tgid
= name_to_int(dentry
);
2148 task
= find_task_by_pid(tgid
);
2150 get_task_struct(task
);
2155 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2156 put_task_struct(task
);
2162 * Find the first task with tgid >= tgid
2165 static struct task_struct
*next_tgid(unsigned int tgid
)
2167 struct task_struct
*task
;
2173 pid
= find_ge_pid(tgid
);
2176 task
= pid_task(pid
, PIDTYPE_PID
);
2177 /* What we to know is if the pid we have find is the
2178 * pid of a thread_group_leader. Testing for task
2179 * being a thread_group_leader is the obvious thing
2180 * todo but there is a window when it fails, due to
2181 * the pid transfer logic in de_thread.
2183 * So we perform the straight forward test of seeing
2184 * if the pid we have found is the pid of a thread
2185 * group leader, and don't worry if the task we have
2186 * found doesn't happen to be a thread group leader.
2187 * As we don't care in the case of readdir.
2189 if (!task
|| !has_group_leader_pid(task
))
2191 get_task_struct(task
);
2197 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2199 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2200 struct task_struct
*task
, int tgid
)
2202 char name
[PROC_NUMBUF
];
2203 int len
= snprintf(name
, sizeof(name
), "%d", tgid
);
2204 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2205 proc_pid_instantiate
, task
, NULL
);
2208 /* for the /proc/ directory itself, after non-process stuff has been done */
2209 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2211 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2212 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2213 struct task_struct
*task
;
2219 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2220 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2221 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2225 tgid
= filp
->f_pos
- TGID_OFFSET
;
2226 for (task
= next_tgid(tgid
);
2228 put_task_struct(task
), task
= next_tgid(tgid
+ 1)) {
2230 filp
->f_pos
= tgid
+ TGID_OFFSET
;
2231 if (proc_pid_fill_cache(filp
, dirent
, filldir
, task
, tgid
) < 0) {
2232 put_task_struct(task
);
2236 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2238 put_task_struct(reaper
);
2246 static const struct pid_entry tid_base_stuff
[] = {
2247 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2248 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2249 INF("environ", S_IRUSR
, pid_environ
),
2250 INF("auxv", S_IRUSR
, pid_auxv
),
2251 INF("status", S_IRUGO
, pid_status
),
2252 #ifdef CONFIG_SCHED_DEBUG
2253 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2255 INF("cmdline", S_IRUGO
, pid_cmdline
),
2256 INF("stat", S_IRUGO
, tid_stat
),
2257 INF("statm", S_IRUGO
, pid_statm
),
2258 REG("maps", S_IRUGO
, maps
),
2260 REG("numa_maps", S_IRUGO
, numa_maps
),
2262 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2266 REG("mounts", S_IRUGO
, mounts
),
2268 REG("clear_refs", S_IWUSR
, clear_refs
),
2269 REG("smaps", S_IRUGO
, smaps
),
2271 #ifdef CONFIG_SECURITY
2272 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2274 #ifdef CONFIG_KALLSYMS
2275 INF("wchan", S_IRUGO
, pid_wchan
),
2277 #ifdef CONFIG_SCHEDSTATS
2278 INF("schedstat", S_IRUGO
, pid_schedstat
),
2280 #ifdef CONFIG_CPUSETS
2281 REG("cpuset", S_IRUGO
, cpuset
),
2283 INF("oom_score", S_IRUGO
, oom_score
),
2284 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2285 #ifdef CONFIG_AUDITSYSCALL
2286 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2288 #ifdef CONFIG_FAULT_INJECTION
2289 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2293 static int proc_tid_base_readdir(struct file
* filp
,
2294 void * dirent
, filldir_t filldir
)
2296 return proc_pident_readdir(filp
,dirent
,filldir
,
2297 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2300 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2301 return proc_pident_lookup(dir
, dentry
,
2302 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2305 static const struct file_operations proc_tid_base_operations
= {
2306 .read
= generic_read_dir
,
2307 .readdir
= proc_tid_base_readdir
,
2310 static const struct inode_operations proc_tid_base_inode_operations
= {
2311 .lookup
= proc_tid_base_lookup
,
2312 .getattr
= pid_getattr
,
2313 .setattr
= proc_setattr
,
2316 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2317 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2319 struct dentry
*error
= ERR_PTR(-ENOENT
);
2320 struct inode
*inode
;
2321 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2325 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2326 inode
->i_op
= &proc_tid_base_inode_operations
;
2327 inode
->i_fop
= &proc_tid_base_operations
;
2328 inode
->i_flags
|=S_IMMUTABLE
;
2330 #ifdef CONFIG_SECURITY
2331 inode
->i_nlink
+= 1;
2334 dentry
->d_op
= &pid_dentry_operations
;
2336 d_add(dentry
, inode
);
2337 /* Close the race of the process dying before we return the dentry */
2338 if (pid_revalidate(dentry
, NULL
))
2344 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2346 struct dentry
*result
= ERR_PTR(-ENOENT
);
2347 struct task_struct
*task
;
2348 struct task_struct
*leader
= get_proc_task(dir
);
2354 tid
= name_to_int(dentry
);
2359 task
= find_task_by_pid(tid
);
2361 get_task_struct(task
);
2365 if (leader
->tgid
!= task
->tgid
)
2368 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2370 put_task_struct(task
);
2372 put_task_struct(leader
);
2378 * Find the first tid of a thread group to return to user space.
2380 * Usually this is just the thread group leader, but if the users
2381 * buffer was too small or there was a seek into the middle of the
2382 * directory we have more work todo.
2384 * In the case of a short read we start with find_task_by_pid.
2386 * In the case of a seek we start with the leader and walk nr
2389 static struct task_struct
*first_tid(struct task_struct
*leader
,
2392 struct task_struct
*pos
;
2395 /* Attempt to start with the pid of a thread */
2396 if (tid
&& (nr
> 0)) {
2397 pos
= find_task_by_pid(tid
);
2398 if (pos
&& (pos
->group_leader
== leader
))
2402 /* If nr exceeds the number of threads there is nothing todo */
2404 if (nr
&& nr
>= get_nr_threads(leader
))
2407 /* If we haven't found our starting place yet start
2408 * with the leader and walk nr threads forward.
2410 for (pos
= leader
; nr
> 0; --nr
) {
2411 pos
= next_thread(pos
);
2412 if (pos
== leader
) {
2418 get_task_struct(pos
);
2425 * Find the next thread in the thread list.
2426 * Return NULL if there is an error or no next thread.
2428 * The reference to the input task_struct is released.
2430 static struct task_struct
*next_tid(struct task_struct
*start
)
2432 struct task_struct
*pos
= NULL
;
2434 if (pid_alive(start
)) {
2435 pos
= next_thread(start
);
2436 if (thread_group_leader(pos
))
2439 get_task_struct(pos
);
2442 put_task_struct(start
);
2446 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2447 struct task_struct
*task
, int tid
)
2449 char name
[PROC_NUMBUF
];
2450 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2451 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2452 proc_task_instantiate
, task
, NULL
);
2455 /* for the /proc/TGID/task/ directories */
2456 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2458 struct dentry
*dentry
= filp
->f_path
.dentry
;
2459 struct inode
*inode
= dentry
->d_inode
;
2460 struct task_struct
*leader
= NULL
;
2461 struct task_struct
*task
;
2462 int retval
= -ENOENT
;
2465 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2467 task
= get_proc_task(inode
);
2471 if (pid_alive(task
)) {
2472 leader
= task
->group_leader
;
2473 get_task_struct(leader
);
2476 put_task_struct(task
);
2484 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2489 ino
= parent_ino(dentry
);
2490 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2496 /* f_version caches the tgid value that the last readdir call couldn't
2497 * return. lseek aka telldir automagically resets f_version to 0.
2499 tid
= filp
->f_version
;
2500 filp
->f_version
= 0;
2501 for (task
= first_tid(leader
, tid
, pos
- 2);
2503 task
= next_tid(task
), pos
++) {
2505 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
2506 /* returning this tgid failed, save it as the first
2507 * pid for the next readir call */
2508 filp
->f_version
= tid
;
2509 put_task_struct(task
);
2515 put_task_struct(leader
);
2520 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2522 struct inode
*inode
= dentry
->d_inode
;
2523 struct task_struct
*p
= get_proc_task(inode
);
2524 generic_fillattr(inode
, stat
);
2528 stat
->nlink
+= get_nr_threads(p
);
2536 static const struct inode_operations proc_task_inode_operations
= {
2537 .lookup
= proc_task_lookup
,
2538 .getattr
= proc_task_getattr
,
2539 .setattr
= proc_setattr
,
2542 static const struct file_operations proc_task_operations
= {
2543 .read
= generic_read_dir
,
2544 .readdir
= proc_task_readdir
,