4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
64 #include <linux/rcupdate.h>
65 #include <linux/kallsyms.h>
66 #include <linux/module.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
73 #include <linux/poll.h>
74 #include <linux/nsproxy.h>
75 #include <linux/oom.h>
79 * Implementing inode permission operations in /proc is almost
80 * certainly an error. Permission checks need to happen during
81 * each system call not at open time. The reason is that most of
82 * what we wish to check for permissions in /proc varies at runtime.
84 * The classic example of a problem is opening file descriptors
85 * in /proc for a task before it execs a suid executable.
89 /* Worst case buffer size needed for holding an integer. */
90 #define PROC_NUMBUF 13
96 const struct inode_operations
*iop
;
97 const struct file_operations
*fop
;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
103 .len = sizeof(NAME) - 1, \
110 #define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 #define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
118 #define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
120 &proc_##OTYPE##_operations, {})
121 #define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
127 EXPORT_SYMBOL(maps_protect
);
129 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
131 struct fs_struct
*fs
;
135 atomic_inc(&fs
->count
);
140 static int get_nr_threads(struct task_struct
*tsk
)
142 /* Must be called with the rcu_read_lock held */
146 if (lock_task_sighand(tsk
, &flags
)) {
147 count
= atomic_read(&tsk
->signal
->count
);
148 unlock_task_sighand(tsk
, &flags
);
153 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
155 struct task_struct
*task
= get_proc_task(inode
);
156 struct fs_struct
*fs
= NULL
;
157 int result
= -ENOENT
;
160 fs
= get_fs_struct(task
);
161 put_task_struct(task
);
164 read_lock(&fs
->lock
);
165 *mnt
= mntget(fs
->pwdmnt
);
166 *dentry
= dget(fs
->pwd
);
167 read_unlock(&fs
->lock
);
174 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
176 struct task_struct
*task
= get_proc_task(inode
);
177 struct fs_struct
*fs
= NULL
;
178 int result
= -ENOENT
;
181 fs
= get_fs_struct(task
);
182 put_task_struct(task
);
185 read_lock(&fs
->lock
);
186 *mnt
= mntget(fs
->rootmnt
);
187 *dentry
= dget(fs
->root
);
188 read_unlock(&fs
->lock
);
195 #define MAY_PTRACE(task) \
196 (task == current || \
197 (task->parent == current && \
198 (task->ptrace & PT_PTRACED) && \
199 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
200 security_ptrace(current,task) == 0))
202 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
205 struct mm_struct
*mm
= get_task_mm(task
);
207 unsigned int len
= mm
->env_end
- mm
->env_start
;
210 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
211 if (!ptrace_may_attach(task
))
218 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
222 struct mm_struct
*mm
= get_task_mm(task
);
226 goto out_mm
; /* Shh! No looking before we're done */
228 len
= mm
->arg_end
- mm
->arg_start
;
233 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
235 // If the nul at the end of args has been overwritten, then
236 // assume application is using setproctitle(3).
237 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
238 len
= strnlen(buffer
, res
);
242 len
= mm
->env_end
- mm
->env_start
;
243 if (len
> PAGE_SIZE
- res
)
244 len
= PAGE_SIZE
- res
;
245 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
246 res
= strnlen(buffer
, res
);
255 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
258 struct mm_struct
*mm
= get_task_mm(task
);
260 unsigned int nwords
= 0;
263 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
264 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
267 memcpy(buffer
, mm
->saved_auxv
, res
);
274 #ifdef CONFIG_KALLSYMS
276 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
277 * Returns the resolved symbol. If that fails, simply return the address.
279 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
282 char symname
[KSYM_NAME_LEN
+1];
284 wchan
= get_wchan(task
);
286 if (lookup_symbol_name(wchan
, symname
) < 0)
287 return sprintf(buffer
, "%lu", wchan
);
289 return sprintf(buffer
, "%s", symname
);
291 #endif /* CONFIG_KALLSYMS */
293 #ifdef CONFIG_SCHEDSTATS
295 * Provides /proc/PID/schedstat
297 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
299 return sprintf(buffer
, "%lu %lu %lu\n",
300 task
->sched_info
.cpu_time
,
301 task
->sched_info
.run_delay
,
302 task
->sched_info
.pcnt
);
306 /* The badness from the OOM killer */
307 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
308 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
310 unsigned long points
;
311 struct timespec uptime
;
313 do_posix_clock_monotonic_gettime(&uptime
);
314 read_lock(&tasklist_lock
);
315 points
= badness(task
, uptime
.tv_sec
);
316 read_unlock(&tasklist_lock
);
317 return sprintf(buffer
, "%lu\n", points
);
320 /************************************************************************/
321 /* Here the fs part begins */
322 /************************************************************************/
324 /* permission checks */
325 static int proc_fd_access_allowed(struct inode
*inode
)
327 struct task_struct
*task
;
329 /* Allow access to a task's file descriptors if it is us or we
330 * may use ptrace attach to the process and find out that
333 task
= get_proc_task(inode
);
335 allowed
= ptrace_may_attach(task
);
336 put_task_struct(task
);
341 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
344 struct inode
*inode
= dentry
->d_inode
;
346 if (attr
->ia_valid
& ATTR_MODE
)
349 error
= inode_change_ok(inode
, attr
);
351 error
= inode_setattr(inode
, attr
);
355 static const struct inode_operations proc_def_inode_operations
= {
356 .setattr
= proc_setattr
,
359 extern struct seq_operations mounts_op
;
365 static int mounts_open(struct inode
*inode
, struct file
*file
)
367 struct task_struct
*task
= get_proc_task(inode
);
368 struct mnt_namespace
*ns
= NULL
;
369 struct proc_mounts
*p
;
375 ns
= task
->nsproxy
->mnt_ns
;
380 put_task_struct(task
);
385 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
387 file
->private_data
= &p
->m
;
388 ret
= seq_open(file
, &mounts_op
);
391 p
->event
= ns
->event
;
401 static int mounts_release(struct inode
*inode
, struct file
*file
)
403 struct seq_file
*m
= file
->private_data
;
404 struct mnt_namespace
*ns
= m
->private;
406 return seq_release(inode
, file
);
409 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
411 struct proc_mounts
*p
= file
->private_data
;
412 struct mnt_namespace
*ns
= p
->m
.private;
415 poll_wait(file
, &ns
->poll
, wait
);
417 spin_lock(&vfsmount_lock
);
418 if (p
->event
!= ns
->event
) {
419 p
->event
= ns
->event
;
422 spin_unlock(&vfsmount_lock
);
427 static const struct file_operations proc_mounts_operations
= {
431 .release
= mounts_release
,
435 extern struct seq_operations mountstats_op
;
436 static int mountstats_open(struct inode
*inode
, struct file
*file
)
438 int ret
= seq_open(file
, &mountstats_op
);
441 struct seq_file
*m
= file
->private_data
;
442 struct mnt_namespace
*mnt_ns
= NULL
;
443 struct task_struct
*task
= get_proc_task(inode
);
448 mnt_ns
= task
->nsproxy
->mnt_ns
;
452 put_task_struct(task
);
458 seq_release(inode
, file
);
465 static const struct file_operations proc_mountstats_operations
= {
466 .open
= mountstats_open
,
469 .release
= mounts_release
,
472 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
474 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
475 size_t count
, loff_t
*ppos
)
477 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
480 struct task_struct
*task
= get_proc_task(inode
);
486 if (count
> PROC_BLOCK_SIZE
)
487 count
= PROC_BLOCK_SIZE
;
490 if (!(page
= __get_free_page(GFP_KERNEL
)))
493 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
496 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
499 put_task_struct(task
);
504 static const struct file_operations proc_info_file_operations
= {
505 .read
= proc_info_read
,
508 static int mem_open(struct inode
* inode
, struct file
* file
)
510 file
->private_data
= (void*)((long)current
->self_exec_id
);
514 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
515 size_t count
, loff_t
*ppos
)
517 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
519 unsigned long src
= *ppos
;
521 struct mm_struct
*mm
;
526 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
530 page
= (char *)__get_free_page(GFP_USER
);
536 mm
= get_task_mm(task
);
542 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
548 int this_len
, retval
;
550 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
551 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
552 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
558 if (copy_to_user(buf
, page
, retval
)) {
573 free_page((unsigned long) page
);
575 put_task_struct(task
);
580 #define mem_write NULL
583 /* This is a security hazard */
584 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
585 size_t count
, loff_t
*ppos
)
589 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
590 unsigned long dst
= *ppos
;
596 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
600 page
= (char *)__get_free_page(GFP_USER
);
606 int this_len
, retval
;
608 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
609 if (copy_from_user(page
, buf
, this_len
)) {
613 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
625 free_page((unsigned long) page
);
627 put_task_struct(task
);
633 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
637 file
->f_pos
= offset
;
640 file
->f_pos
+= offset
;
645 force_successful_syscall_return();
649 static const struct file_operations proc_mem_operations
= {
656 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
657 size_t count
, loff_t
*ppos
)
659 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
660 char buffer
[PROC_NUMBUF
];
666 oom_adjust
= task
->oomkilladj
;
667 put_task_struct(task
);
669 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
671 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
674 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
675 size_t count
, loff_t
*ppos
)
677 struct task_struct
*task
;
678 char buffer
[PROC_NUMBUF
], *end
;
681 memset(buffer
, 0, sizeof(buffer
));
682 if (count
> sizeof(buffer
) - 1)
683 count
= sizeof(buffer
) - 1;
684 if (copy_from_user(buffer
, buf
, count
))
686 oom_adjust
= simple_strtol(buffer
, &end
, 0);
687 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
688 oom_adjust
!= OOM_DISABLE
)
692 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
695 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
696 put_task_struct(task
);
699 task
->oomkilladj
= oom_adjust
;
700 put_task_struct(task
);
701 if (end
- buffer
== 0)
706 static const struct file_operations proc_oom_adjust_operations
= {
707 .read
= oom_adjust_read
,
708 .write
= oom_adjust_write
,
712 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
713 size_t count
, loff_t
*ppos
)
715 struct task_struct
*task
;
716 char buffer
[PROC_NUMBUF
], *end
;
717 struct mm_struct
*mm
;
719 memset(buffer
, 0, sizeof(buffer
));
720 if (count
> sizeof(buffer
) - 1)
721 count
= sizeof(buffer
) - 1;
722 if (copy_from_user(buffer
, buf
, count
))
724 if (!simple_strtol(buffer
, &end
, 0))
728 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
731 mm
= get_task_mm(task
);
736 put_task_struct(task
);
737 if (end
- buffer
== 0)
742 static struct file_operations proc_clear_refs_operations
= {
743 .write
= clear_refs_write
,
747 #ifdef CONFIG_AUDITSYSCALL
749 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
750 size_t count
, loff_t
*ppos
)
752 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
753 struct task_struct
*task
= get_proc_task(inode
);
755 char tmpbuf
[TMPBUFLEN
];
759 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
760 audit_get_loginuid(task
->audit_context
));
761 put_task_struct(task
);
762 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
765 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
766 size_t count
, loff_t
*ppos
)
768 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
773 if (!capable(CAP_AUDIT_CONTROL
))
776 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
779 if (count
>= PAGE_SIZE
)
780 count
= PAGE_SIZE
- 1;
783 /* No partial writes. */
786 page
= (char*)__get_free_page(GFP_USER
);
790 if (copy_from_user(page
, buf
, count
))
794 loginuid
= simple_strtoul(page
, &tmp
, 10);
800 length
= audit_set_loginuid(current
, loginuid
);
801 if (likely(length
== 0))
805 free_page((unsigned long) page
);
809 static const struct file_operations proc_loginuid_operations
= {
810 .read
= proc_loginuid_read
,
811 .write
= proc_loginuid_write
,
815 #ifdef CONFIG_SECCOMP
816 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
817 size_t count
, loff_t
*ppos
)
819 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
825 /* no need to print the trailing zero, so use only len */
826 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
827 put_task_struct(tsk
);
829 return simple_read_from_buffer(buf
, count
, ppos
, __buf
, len
);
832 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
833 size_t count
, loff_t
*ppos
)
835 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
836 char __buf
[20], *end
;
837 unsigned int seccomp_mode
;
844 /* can set it only once to be even more secure */
846 if (unlikely(tsk
->seccomp
.mode
))
850 memset(__buf
, 0, sizeof(__buf
));
851 count
= min(count
, sizeof(__buf
) - 1);
852 if (copy_from_user(__buf
, buf
, count
))
855 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
859 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
860 tsk
->seccomp
.mode
= seccomp_mode
;
861 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
865 if (unlikely(!(end
- __buf
)))
867 result
= end
- __buf
;
869 put_task_struct(tsk
);
874 static const struct file_operations proc_seccomp_operations
= {
875 .read
= seccomp_read
,
876 .write
= seccomp_write
,
878 #endif /* CONFIG_SECCOMP */
880 #ifdef CONFIG_FAULT_INJECTION
881 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
882 size_t count
, loff_t
*ppos
)
884 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
885 char buffer
[PROC_NUMBUF
];
891 make_it_fail
= task
->make_it_fail
;
892 put_task_struct(task
);
894 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
896 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
899 static ssize_t
proc_fault_inject_write(struct file
* file
,
900 const char __user
* buf
, size_t count
, loff_t
*ppos
)
902 struct task_struct
*task
;
903 char buffer
[PROC_NUMBUF
], *end
;
906 if (!capable(CAP_SYS_RESOURCE
))
908 memset(buffer
, 0, sizeof(buffer
));
909 if (count
> sizeof(buffer
) - 1)
910 count
= sizeof(buffer
) - 1;
911 if (copy_from_user(buffer
, buf
, count
))
913 make_it_fail
= simple_strtol(buffer
, &end
, 0);
916 task
= get_proc_task(file
->f_dentry
->d_inode
);
919 task
->make_it_fail
= make_it_fail
;
920 put_task_struct(task
);
921 if (end
- buffer
== 0)
926 static const struct file_operations proc_fault_inject_operations
= {
927 .read
= proc_fault_inject_read
,
928 .write
= proc_fault_inject_write
,
932 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
934 struct inode
*inode
= dentry
->d_inode
;
937 /* We don't need a base pointer in the /proc filesystem */
940 /* Are we allowed to snoop on the tasks file descriptors? */
941 if (!proc_fd_access_allowed(inode
))
944 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
945 nd
->last_type
= LAST_BIND
;
947 return ERR_PTR(error
);
950 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
951 char __user
*buffer
, int buflen
)
953 struct inode
* inode
;
954 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
960 inode
= dentry
->d_inode
;
961 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
965 len
= tmp
+ PAGE_SIZE
- 1 - path
;
969 if (copy_to_user(buffer
, path
, len
))
972 free_page((unsigned long)tmp
);
976 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
979 struct inode
*inode
= dentry
->d_inode
;
981 struct vfsmount
*mnt
= NULL
;
983 /* Are we allowed to snoop on the tasks file descriptors? */
984 if (!proc_fd_access_allowed(inode
))
987 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
991 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
998 static const struct inode_operations proc_pid_link_inode_operations
= {
999 .readlink
= proc_pid_readlink
,
1000 .follow_link
= proc_pid_follow_link
,
1001 .setattr
= proc_setattr
,
1005 /* building an inode */
1007 static int task_dumpable(struct task_struct
*task
)
1010 struct mm_struct
*mm
;
1015 dumpable
= mm
->dumpable
;
1023 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1025 struct inode
* inode
;
1026 struct proc_inode
*ei
;
1028 /* We need a new inode */
1030 inode
= new_inode(sb
);
1036 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1037 inode
->i_op
= &proc_def_inode_operations
;
1040 * grab the reference to task.
1042 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1048 if (task_dumpable(task
)) {
1049 inode
->i_uid
= task
->euid
;
1050 inode
->i_gid
= task
->egid
;
1052 security_task_to_inode(task
, inode
);
1062 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1064 struct inode
*inode
= dentry
->d_inode
;
1065 struct task_struct
*task
;
1066 generic_fillattr(inode
, stat
);
1071 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1073 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1074 task_dumpable(task
)) {
1075 stat
->uid
= task
->euid
;
1076 stat
->gid
= task
->egid
;
1086 * Exceptional case: normally we are not allowed to unhash a busy
1087 * directory. In this case, however, we can do it - no aliasing problems
1088 * due to the way we treat inodes.
1090 * Rewrite the inode's ownerships here because the owning task may have
1091 * performed a setuid(), etc.
1093 * Before the /proc/pid/status file was created the only way to read
1094 * the effective uid of a /process was to stat /proc/pid. Reading
1095 * /proc/pid/status is slow enough that procps and other packages
1096 * kept stating /proc/pid. To keep the rules in /proc simple I have
1097 * made this apply to all per process world readable and executable
1100 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1102 struct inode
*inode
= dentry
->d_inode
;
1103 struct task_struct
*task
= get_proc_task(inode
);
1105 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1106 task_dumpable(task
)) {
1107 inode
->i_uid
= task
->euid
;
1108 inode
->i_gid
= task
->egid
;
1113 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1114 security_task_to_inode(task
, inode
);
1115 put_task_struct(task
);
1122 static int pid_delete_dentry(struct dentry
* dentry
)
1124 /* Is the task we represent dead?
1125 * If so, then don't put the dentry on the lru list,
1126 * kill it immediately.
1128 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1131 static struct dentry_operations pid_dentry_operations
=
1133 .d_revalidate
= pid_revalidate
,
1134 .d_delete
= pid_delete_dentry
,
1139 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1140 struct task_struct
*, const void *);
1143 * Fill a directory entry.
1145 * If possible create the dcache entry and derive our inode number and
1146 * file type from dcache entry.
1148 * Since all of the proc inode numbers are dynamically generated, the inode
1149 * numbers do not exist until the inode is cache. This means creating the
1150 * the dcache entry in readdir is necessary to keep the inode numbers
1151 * reported by readdir in sync with the inode numbers reported
1154 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1155 char *name
, int len
,
1156 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1158 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1159 struct inode
*inode
;
1162 unsigned type
= DT_UNKNOWN
;
1166 qname
.hash
= full_name_hash(name
, len
);
1168 child
= d_lookup(dir
, &qname
);
1171 new = d_alloc(dir
, &qname
);
1173 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1180 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1181 goto end_instantiate
;
1182 inode
= child
->d_inode
;
1185 type
= inode
->i_mode
>> 12;
1190 ino
= find_inode_number(dir
, &qname
);
1193 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1196 static unsigned name_to_int(struct dentry
*dentry
)
1198 const char *name
= dentry
->d_name
.name
;
1199 int len
= dentry
->d_name
.len
;
1202 if (len
> 1 && *name
== '0')
1205 unsigned c
= *name
++ - '0';
1208 if (n
>= (~0U-9)/10)
1218 #define PROC_FDINFO_MAX 64
1220 static int proc_fd_info(struct inode
*inode
, struct dentry
**dentry
,
1221 struct vfsmount
**mnt
, char *info
)
1223 struct task_struct
*task
= get_proc_task(inode
);
1224 struct files_struct
*files
= NULL
;
1226 int fd
= proc_fd(inode
);
1229 files
= get_files_struct(task
);
1230 put_task_struct(task
);
1234 * We are not taking a ref to the file structure, so we must
1237 spin_lock(&files
->file_lock
);
1238 file
= fcheck_files(files
, fd
);
1241 *mnt
= mntget(file
->f_path
.mnt
);
1243 *dentry
= dget(file
->f_path
.dentry
);
1245 snprintf(info
, PROC_FDINFO_MAX
,
1248 (long long) file
->f_pos
,
1250 spin_unlock(&files
->file_lock
);
1251 put_files_struct(files
);
1254 spin_unlock(&files
->file_lock
);
1255 put_files_struct(files
);
1260 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
,
1261 struct vfsmount
**mnt
)
1263 return proc_fd_info(inode
, dentry
, mnt
, NULL
);
1266 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1268 struct inode
*inode
= dentry
->d_inode
;
1269 struct task_struct
*task
= get_proc_task(inode
);
1270 int fd
= proc_fd(inode
);
1271 struct files_struct
*files
;
1274 files
= get_files_struct(task
);
1277 if (fcheck_files(files
, fd
)) {
1279 put_files_struct(files
);
1280 if (task_dumpable(task
)) {
1281 inode
->i_uid
= task
->euid
;
1282 inode
->i_gid
= task
->egid
;
1287 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1288 security_task_to_inode(task
, inode
);
1289 put_task_struct(task
);
1293 put_files_struct(files
);
1295 put_task_struct(task
);
1301 static struct dentry_operations tid_fd_dentry_operations
=
1303 .d_revalidate
= tid_fd_revalidate
,
1304 .d_delete
= pid_delete_dentry
,
1307 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1308 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1310 unsigned fd
= *(const unsigned *)ptr
;
1312 struct files_struct
*files
;
1313 struct inode
*inode
;
1314 struct proc_inode
*ei
;
1315 struct dentry
*error
= ERR_PTR(-ENOENT
);
1317 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1322 files
= get_files_struct(task
);
1325 inode
->i_mode
= S_IFLNK
;
1328 * We are not taking a ref to the file structure, so we must
1331 spin_lock(&files
->file_lock
);
1332 file
= fcheck_files(files
, fd
);
1335 if (file
->f_mode
& 1)
1336 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1337 if (file
->f_mode
& 2)
1338 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1339 spin_unlock(&files
->file_lock
);
1340 put_files_struct(files
);
1342 inode
->i_op
= &proc_pid_link_inode_operations
;
1344 ei
->op
.proc_get_link
= proc_fd_link
;
1345 dentry
->d_op
= &tid_fd_dentry_operations
;
1346 d_add(dentry
, inode
);
1347 /* Close the race of the process dying before we return the dentry */
1348 if (tid_fd_revalidate(dentry
, NULL
))
1354 spin_unlock(&files
->file_lock
);
1355 put_files_struct(files
);
1361 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1362 struct dentry
*dentry
,
1363 instantiate_t instantiate
)
1365 struct task_struct
*task
= get_proc_task(dir
);
1366 unsigned fd
= name_to_int(dentry
);
1367 struct dentry
*result
= ERR_PTR(-ENOENT
);
1374 result
= instantiate(dir
, dentry
, task
, &fd
);
1376 put_task_struct(task
);
1381 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1382 filldir_t filldir
, instantiate_t instantiate
)
1384 struct dentry
*dentry
= filp
->f_path
.dentry
;
1385 struct inode
*inode
= dentry
->d_inode
;
1386 struct task_struct
*p
= get_proc_task(inode
);
1387 unsigned int fd
, tid
, ino
;
1389 struct files_struct
* files
;
1390 struct fdtable
*fdt
;
1401 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1405 ino
= parent_ino(dentry
);
1406 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1410 files
= get_files_struct(p
);
1414 fdt
= files_fdtable(files
);
1415 for (fd
= filp
->f_pos
-2;
1417 fd
++, filp
->f_pos
++) {
1418 char name
[PROC_NUMBUF
];
1421 if (!fcheck_files(files
, fd
))
1425 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1426 if (proc_fill_cache(filp
, dirent
, filldir
,
1427 name
, len
, instantiate
,
1435 put_files_struct(files
);
1443 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1444 struct nameidata
*nd
)
1446 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1449 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1451 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1454 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1455 size_t len
, loff_t
*ppos
)
1457 char tmp
[PROC_FDINFO_MAX
];
1458 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, NULL
, tmp
);
1460 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1464 static const struct file_operations proc_fdinfo_file_operations
= {
1465 .open
= nonseekable_open
,
1466 .read
= proc_fdinfo_read
,
1469 static const struct file_operations proc_fd_operations
= {
1470 .read
= generic_read_dir
,
1471 .readdir
= proc_readfd
,
1475 * /proc/pid/fd needs a special permission handler so that a process can still
1476 * access /proc/self/fd after it has executed a setuid().
1478 static int proc_fd_permission(struct inode
*inode
, int mask
,
1479 struct nameidata
*nd
)
1483 rv
= generic_permission(inode
, mask
, NULL
);
1486 if (task_pid(current
) == proc_pid(inode
))
1492 * proc directories can do almost nothing..
1494 static const struct inode_operations proc_fd_inode_operations
= {
1495 .lookup
= proc_lookupfd
,
1496 .permission
= proc_fd_permission
,
1497 .setattr
= proc_setattr
,
1500 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1501 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1503 unsigned fd
= *(unsigned *)ptr
;
1504 struct inode
*inode
;
1505 struct proc_inode
*ei
;
1506 struct dentry
*error
= ERR_PTR(-ENOENT
);
1508 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1513 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1514 inode
->i_fop
= &proc_fdinfo_file_operations
;
1515 dentry
->d_op
= &tid_fd_dentry_operations
;
1516 d_add(dentry
, inode
);
1517 /* Close the race of the process dying before we return the dentry */
1518 if (tid_fd_revalidate(dentry
, NULL
))
1525 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1526 struct dentry
*dentry
,
1527 struct nameidata
*nd
)
1529 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1532 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1534 return proc_readfd_common(filp
, dirent
, filldir
,
1535 proc_fdinfo_instantiate
);
1538 static const struct file_operations proc_fdinfo_operations
= {
1539 .read
= generic_read_dir
,
1540 .readdir
= proc_readfdinfo
,
1544 * proc directories can do almost nothing..
1546 static const struct inode_operations proc_fdinfo_inode_operations
= {
1547 .lookup
= proc_lookupfdinfo
,
1548 .setattr
= proc_setattr
,
1552 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1553 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1555 const struct pid_entry
*p
= ptr
;
1556 struct inode
*inode
;
1557 struct proc_inode
*ei
;
1558 struct dentry
*error
= ERR_PTR(-EINVAL
);
1560 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1565 inode
->i_mode
= p
->mode
;
1566 if (S_ISDIR(inode
->i_mode
))
1567 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1569 inode
->i_op
= p
->iop
;
1571 inode
->i_fop
= p
->fop
;
1573 dentry
->d_op
= &pid_dentry_operations
;
1574 d_add(dentry
, inode
);
1575 /* Close the race of the process dying before we return the dentry */
1576 if (pid_revalidate(dentry
, NULL
))
1582 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1583 struct dentry
*dentry
,
1584 const struct pid_entry
*ents
,
1587 struct inode
*inode
;
1588 struct dentry
*error
;
1589 struct task_struct
*task
= get_proc_task(dir
);
1590 const struct pid_entry
*p
, *last
;
1592 error
= ERR_PTR(-ENOENT
);
1599 * Yes, it does not scale. And it should not. Don't add
1600 * new entries into /proc/<tgid>/ without very good reasons.
1602 last
= &ents
[nents
- 1];
1603 for (p
= ents
; p
<= last
; p
++) {
1604 if (p
->len
!= dentry
->d_name
.len
)
1606 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1612 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1614 put_task_struct(task
);
1619 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1620 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1622 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1623 proc_pident_instantiate
, task
, p
);
1626 static int proc_pident_readdir(struct file
*filp
,
1627 void *dirent
, filldir_t filldir
,
1628 const struct pid_entry
*ents
, unsigned int nents
)
1632 struct dentry
*dentry
= filp
->f_path
.dentry
;
1633 struct inode
*inode
= dentry
->d_inode
;
1634 struct task_struct
*task
= get_proc_task(inode
);
1635 const struct pid_entry
*p
, *last
;
1649 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1655 ino
= parent_ino(dentry
);
1656 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1668 last
= &ents
[nents
- 1];
1670 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
1679 put_task_struct(task
);
1684 #ifdef CONFIG_SECURITY
1685 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1686 size_t count
, loff_t
*ppos
)
1688 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1691 struct task_struct
*task
= get_proc_task(inode
);
1696 length
= security_getprocattr(task
,
1697 (char*)file
->f_path
.dentry
->d_name
.name
,
1699 put_task_struct(task
);
1701 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
1706 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1707 size_t count
, loff_t
*ppos
)
1709 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1712 struct task_struct
*task
= get_proc_task(inode
);
1717 if (count
> PAGE_SIZE
)
1720 /* No partial writes. */
1726 page
= (char*)__get_free_page(GFP_USER
);
1731 if (copy_from_user(page
, buf
, count
))
1734 length
= security_setprocattr(task
,
1735 (char*)file
->f_path
.dentry
->d_name
.name
,
1736 (void*)page
, count
);
1738 free_page((unsigned long) page
);
1740 put_task_struct(task
);
1745 static const struct file_operations proc_pid_attr_operations
= {
1746 .read
= proc_pid_attr_read
,
1747 .write
= proc_pid_attr_write
,
1750 static const struct pid_entry attr_dir_stuff
[] = {
1751 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
1752 REG("prev", S_IRUGO
, pid_attr
),
1753 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
1754 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1755 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1756 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1759 static int proc_attr_dir_readdir(struct file
* filp
,
1760 void * dirent
, filldir_t filldir
)
1762 return proc_pident_readdir(filp
,dirent
,filldir
,
1763 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
1766 static const struct file_operations proc_attr_dir_operations
= {
1767 .read
= generic_read_dir
,
1768 .readdir
= proc_attr_dir_readdir
,
1771 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
1772 struct dentry
*dentry
, struct nameidata
*nd
)
1774 return proc_pident_lookup(dir
, dentry
,
1775 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
1778 static const struct inode_operations proc_attr_dir_inode_operations
= {
1779 .lookup
= proc_attr_dir_lookup
,
1780 .getattr
= pid_getattr
,
1781 .setattr
= proc_setattr
,
1789 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1792 char tmp
[PROC_NUMBUF
];
1793 sprintf(tmp
, "%d", current
->tgid
);
1794 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1797 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1799 char tmp
[PROC_NUMBUF
];
1800 sprintf(tmp
, "%d", current
->tgid
);
1801 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1804 static const struct inode_operations proc_self_inode_operations
= {
1805 .readlink
= proc_self_readlink
,
1806 .follow_link
= proc_self_follow_link
,
1812 * These are the directory entries in the root directory of /proc
1813 * that properly belong to the /proc filesystem, as they describe
1814 * describe something that is process related.
1816 static const struct pid_entry proc_base_stuff
[] = {
1817 NOD("self", S_IFLNK
|S_IRWXUGO
,
1818 &proc_self_inode_operations
, NULL
, {}),
1822 * Exceptional case: normally we are not allowed to unhash a busy
1823 * directory. In this case, however, we can do it - no aliasing problems
1824 * due to the way we treat inodes.
1826 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1828 struct inode
*inode
= dentry
->d_inode
;
1829 struct task_struct
*task
= get_proc_task(inode
);
1831 put_task_struct(task
);
1838 static struct dentry_operations proc_base_dentry_operations
=
1840 .d_revalidate
= proc_base_revalidate
,
1841 .d_delete
= pid_delete_dentry
,
1844 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
1845 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1847 const struct pid_entry
*p
= ptr
;
1848 struct inode
*inode
;
1849 struct proc_inode
*ei
;
1850 struct dentry
*error
= ERR_PTR(-EINVAL
);
1852 /* Allocate the inode */
1853 error
= ERR_PTR(-ENOMEM
);
1854 inode
= new_inode(dir
->i_sb
);
1858 /* Initialize the inode */
1860 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1863 * grab the reference to the task.
1865 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1871 inode
->i_mode
= p
->mode
;
1872 if (S_ISDIR(inode
->i_mode
))
1874 if (S_ISLNK(inode
->i_mode
))
1877 inode
->i_op
= p
->iop
;
1879 inode
->i_fop
= p
->fop
;
1881 dentry
->d_op
= &proc_base_dentry_operations
;
1882 d_add(dentry
, inode
);
1891 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
1893 struct dentry
*error
;
1894 struct task_struct
*task
= get_proc_task(dir
);
1895 const struct pid_entry
*p
, *last
;
1897 error
= ERR_PTR(-ENOENT
);
1902 /* Lookup the directory entry */
1903 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
1904 for (p
= proc_base_stuff
; p
<= last
; p
++) {
1905 if (p
->len
!= dentry
->d_name
.len
)
1907 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1913 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
1916 put_task_struct(task
);
1921 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
1922 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1924 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1925 proc_base_instantiate
, task
, p
);
1928 #ifdef CONFIG_TASK_IO_ACCOUNTING
1929 static int proc_pid_io_accounting(struct task_struct
*task
, char *buffer
)
1931 return sprintf(buffer
,
1932 #ifdef CONFIG_TASK_XACCT
1938 "read_bytes: %llu\n"
1939 "write_bytes: %llu\n"
1940 "cancelled_write_bytes: %llu\n",
1941 #ifdef CONFIG_TASK_XACCT
1942 (unsigned long long)task
->rchar
,
1943 (unsigned long long)task
->wchar
,
1944 (unsigned long long)task
->syscr
,
1945 (unsigned long long)task
->syscw
,
1947 (unsigned long long)task
->ioac
.read_bytes
,
1948 (unsigned long long)task
->ioac
.write_bytes
,
1949 (unsigned long long)task
->ioac
.cancelled_write_bytes
);
1956 static const struct file_operations proc_task_operations
;
1957 static const struct inode_operations proc_task_inode_operations
;
1959 static const struct pid_entry tgid_base_stuff
[] = {
1960 DIR("task", S_IRUGO
|S_IXUGO
, task
),
1961 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
1962 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
1963 INF("environ", S_IRUSR
, pid_environ
),
1964 INF("auxv", S_IRUSR
, pid_auxv
),
1965 INF("status", S_IRUGO
, pid_status
),
1966 INF("cmdline", S_IRUGO
, pid_cmdline
),
1967 INF("stat", S_IRUGO
, tgid_stat
),
1968 INF("statm", S_IRUGO
, pid_statm
),
1969 REG("maps", S_IRUGO
, maps
),
1971 REG("numa_maps", S_IRUGO
, numa_maps
),
1973 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
1974 #ifdef CONFIG_SECCOMP
1975 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
1980 REG("mounts", S_IRUGO
, mounts
),
1981 REG("mountstats", S_IRUSR
, mountstats
),
1983 REG("clear_refs", S_IWUSR
, clear_refs
),
1984 REG("smaps", S_IRUGO
, smaps
),
1986 #ifdef CONFIG_SECURITY
1987 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
1989 #ifdef CONFIG_KALLSYMS
1990 INF("wchan", S_IRUGO
, pid_wchan
),
1992 #ifdef CONFIG_SCHEDSTATS
1993 INF("schedstat", S_IRUGO
, pid_schedstat
),
1995 #ifdef CONFIG_CPUSETS
1996 REG("cpuset", S_IRUGO
, cpuset
),
1998 INF("oom_score", S_IRUGO
, oom_score
),
1999 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2000 #ifdef CONFIG_AUDITSYSCALL
2001 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2003 #ifdef CONFIG_FAULT_INJECTION
2004 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2006 #ifdef CONFIG_TASK_IO_ACCOUNTING
2007 INF("io", S_IRUGO
, pid_io_accounting
),
2011 static int proc_tgid_base_readdir(struct file
* filp
,
2012 void * dirent
, filldir_t filldir
)
2014 return proc_pident_readdir(filp
,dirent
,filldir
,
2015 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2018 static const struct file_operations proc_tgid_base_operations
= {
2019 .read
= generic_read_dir
,
2020 .readdir
= proc_tgid_base_readdir
,
2023 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2024 return proc_pident_lookup(dir
, dentry
,
2025 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2028 static const struct inode_operations proc_tgid_base_inode_operations
= {
2029 .lookup
= proc_tgid_base_lookup
,
2030 .getattr
= pid_getattr
,
2031 .setattr
= proc_setattr
,
2035 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2037 * @task: task that should be flushed.
2039 * Looks in the dcache for
2041 * /proc/@tgid/task/@pid
2042 * if either directory is present flushes it and all of it'ts children
2045 * It is safe and reasonable to cache /proc entries for a task until
2046 * that task exits. After that they just clog up the dcache with
2047 * useless entries, possibly causing useful dcache entries to be
2048 * flushed instead. This routine is proved to flush those useless
2049 * dcache entries at process exit time.
2051 * NOTE: This routine is just an optimization so it does not guarantee
2052 * that no dcache entries will exist at process exit time it
2053 * just makes it very unlikely that any will persist.
2055 void proc_flush_task(struct task_struct
*task
)
2057 struct dentry
*dentry
, *leader
, *dir
;
2058 char buf
[PROC_NUMBUF
];
2062 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2063 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2065 shrink_dcache_parent(dentry
);
2070 if (thread_group_leader(task
))
2074 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
2075 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2080 name
.len
= strlen(name
.name
);
2081 dir
= d_hash_and_lookup(leader
, &name
);
2083 goto out_put_leader
;
2086 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2087 dentry
= d_hash_and_lookup(dir
, &name
);
2089 shrink_dcache_parent(dentry
);
2101 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2102 struct dentry
* dentry
,
2103 struct task_struct
*task
, const void *ptr
)
2105 struct dentry
*error
= ERR_PTR(-ENOENT
);
2106 struct inode
*inode
;
2108 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2112 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2113 inode
->i_op
= &proc_tgid_base_inode_operations
;
2114 inode
->i_fop
= &proc_tgid_base_operations
;
2115 inode
->i_flags
|=S_IMMUTABLE
;
2117 #ifdef CONFIG_SECURITY
2118 inode
->i_nlink
+= 1;
2121 dentry
->d_op
= &pid_dentry_operations
;
2123 d_add(dentry
, inode
);
2124 /* Close the race of the process dying before we return the dentry */
2125 if (pid_revalidate(dentry
, NULL
))
2131 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2133 struct dentry
*result
= ERR_PTR(-ENOENT
);
2134 struct task_struct
*task
;
2137 result
= proc_base_lookup(dir
, dentry
);
2138 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2141 tgid
= name_to_int(dentry
);
2146 task
= find_task_by_pid(tgid
);
2148 get_task_struct(task
);
2153 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2154 put_task_struct(task
);
2160 * Find the first task with tgid >= tgid
2163 static struct task_struct
*next_tgid(unsigned int tgid
)
2165 struct task_struct
*task
;
2171 pid
= find_ge_pid(tgid
);
2174 task
= pid_task(pid
, PIDTYPE_PID
);
2175 /* What we to know is if the pid we have find is the
2176 * pid of a thread_group_leader. Testing for task
2177 * being a thread_group_leader is the obvious thing
2178 * todo but there is a window when it fails, due to
2179 * the pid transfer logic in de_thread.
2181 * So we perform the straight forward test of seeing
2182 * if the pid we have found is the pid of a thread
2183 * group leader, and don't worry if the task we have
2184 * found doesn't happen to be a thread group leader.
2185 * As we don't care in the case of readdir.
2187 if (!task
|| !has_group_leader_pid(task
))
2189 get_task_struct(task
);
2195 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2197 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2198 struct task_struct
*task
, int tgid
)
2200 char name
[PROC_NUMBUF
];
2201 int len
= snprintf(name
, sizeof(name
), "%d", tgid
);
2202 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2203 proc_pid_instantiate
, task
, NULL
);
2206 /* for the /proc/ directory itself, after non-process stuff has been done */
2207 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2209 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2210 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2211 struct task_struct
*task
;
2217 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2218 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2219 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2223 tgid
= filp
->f_pos
- TGID_OFFSET
;
2224 for (task
= next_tgid(tgid
);
2226 put_task_struct(task
), task
= next_tgid(tgid
+ 1)) {
2228 filp
->f_pos
= tgid
+ TGID_OFFSET
;
2229 if (proc_pid_fill_cache(filp
, dirent
, filldir
, task
, tgid
) < 0) {
2230 put_task_struct(task
);
2234 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2236 put_task_struct(reaper
);
2244 static const struct pid_entry tid_base_stuff
[] = {
2245 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2246 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2247 INF("environ", S_IRUSR
, pid_environ
),
2248 INF("auxv", S_IRUSR
, pid_auxv
),
2249 INF("status", S_IRUGO
, pid_status
),
2250 INF("cmdline", S_IRUGO
, pid_cmdline
),
2251 INF("stat", S_IRUGO
, tid_stat
),
2252 INF("statm", S_IRUGO
, pid_statm
),
2253 REG("maps", S_IRUGO
, maps
),
2255 REG("numa_maps", S_IRUGO
, numa_maps
),
2257 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2258 #ifdef CONFIG_SECCOMP
2259 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
2264 REG("mounts", S_IRUGO
, mounts
),
2266 REG("clear_refs", S_IWUSR
, clear_refs
),
2267 REG("smaps", S_IRUGO
, smaps
),
2269 #ifdef CONFIG_SECURITY
2270 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2272 #ifdef CONFIG_KALLSYMS
2273 INF("wchan", S_IRUGO
, pid_wchan
),
2275 #ifdef CONFIG_SCHEDSTATS
2276 INF("schedstat", S_IRUGO
, pid_schedstat
),
2278 #ifdef CONFIG_CPUSETS
2279 REG("cpuset", S_IRUGO
, cpuset
),
2281 INF("oom_score", S_IRUGO
, oom_score
),
2282 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2283 #ifdef CONFIG_AUDITSYSCALL
2284 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2286 #ifdef CONFIG_FAULT_INJECTION
2287 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2291 static int proc_tid_base_readdir(struct file
* filp
,
2292 void * dirent
, filldir_t filldir
)
2294 return proc_pident_readdir(filp
,dirent
,filldir
,
2295 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2298 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2299 return proc_pident_lookup(dir
, dentry
,
2300 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2303 static const struct file_operations proc_tid_base_operations
= {
2304 .read
= generic_read_dir
,
2305 .readdir
= proc_tid_base_readdir
,
2308 static const struct inode_operations proc_tid_base_inode_operations
= {
2309 .lookup
= proc_tid_base_lookup
,
2310 .getattr
= pid_getattr
,
2311 .setattr
= proc_setattr
,
2314 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2315 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2317 struct dentry
*error
= ERR_PTR(-ENOENT
);
2318 struct inode
*inode
;
2319 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2323 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2324 inode
->i_op
= &proc_tid_base_inode_operations
;
2325 inode
->i_fop
= &proc_tid_base_operations
;
2326 inode
->i_flags
|=S_IMMUTABLE
;
2328 #ifdef CONFIG_SECURITY
2329 inode
->i_nlink
+= 1;
2332 dentry
->d_op
= &pid_dentry_operations
;
2334 d_add(dentry
, inode
);
2335 /* Close the race of the process dying before we return the dentry */
2336 if (pid_revalidate(dentry
, NULL
))
2342 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2344 struct dentry
*result
= ERR_PTR(-ENOENT
);
2345 struct task_struct
*task
;
2346 struct task_struct
*leader
= get_proc_task(dir
);
2352 tid
= name_to_int(dentry
);
2357 task
= find_task_by_pid(tid
);
2359 get_task_struct(task
);
2363 if (leader
->tgid
!= task
->tgid
)
2366 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2368 put_task_struct(task
);
2370 put_task_struct(leader
);
2376 * Find the first tid of a thread group to return to user space.
2378 * Usually this is just the thread group leader, but if the users
2379 * buffer was too small or there was a seek into the middle of the
2380 * directory we have more work todo.
2382 * In the case of a short read we start with find_task_by_pid.
2384 * In the case of a seek we start with the leader and walk nr
2387 static struct task_struct
*first_tid(struct task_struct
*leader
,
2390 struct task_struct
*pos
;
2393 /* Attempt to start with the pid of a thread */
2394 if (tid
&& (nr
> 0)) {
2395 pos
= find_task_by_pid(tid
);
2396 if (pos
&& (pos
->group_leader
== leader
))
2400 /* If nr exceeds the number of threads there is nothing todo */
2402 if (nr
&& nr
>= get_nr_threads(leader
))
2405 /* If we haven't found our starting place yet start
2406 * with the leader and walk nr threads forward.
2408 for (pos
= leader
; nr
> 0; --nr
) {
2409 pos
= next_thread(pos
);
2410 if (pos
== leader
) {
2416 get_task_struct(pos
);
2423 * Find the next thread in the thread list.
2424 * Return NULL if there is an error or no next thread.
2426 * The reference to the input task_struct is released.
2428 static struct task_struct
*next_tid(struct task_struct
*start
)
2430 struct task_struct
*pos
= NULL
;
2432 if (pid_alive(start
)) {
2433 pos
= next_thread(start
);
2434 if (thread_group_leader(pos
))
2437 get_task_struct(pos
);
2440 put_task_struct(start
);
2444 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2445 struct task_struct
*task
, int tid
)
2447 char name
[PROC_NUMBUF
];
2448 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2449 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2450 proc_task_instantiate
, task
, NULL
);
2453 /* for the /proc/TGID/task/ directories */
2454 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2456 struct dentry
*dentry
= filp
->f_path
.dentry
;
2457 struct inode
*inode
= dentry
->d_inode
;
2458 struct task_struct
*leader
= NULL
;
2459 struct task_struct
*task
;
2460 int retval
= -ENOENT
;
2463 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2465 task
= get_proc_task(inode
);
2469 if (pid_alive(task
)) {
2470 leader
= task
->group_leader
;
2471 get_task_struct(leader
);
2474 put_task_struct(task
);
2482 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2487 ino
= parent_ino(dentry
);
2488 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2494 /* f_version caches the tgid value that the last readdir call couldn't
2495 * return. lseek aka telldir automagically resets f_version to 0.
2497 tid
= filp
->f_version
;
2498 filp
->f_version
= 0;
2499 for (task
= first_tid(leader
, tid
, pos
- 2);
2501 task
= next_tid(task
), pos
++) {
2503 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
2504 /* returning this tgid failed, save it as the first
2505 * pid for the next readir call */
2506 filp
->f_version
= tid
;
2507 put_task_struct(task
);
2513 put_task_struct(leader
);
2518 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2520 struct inode
*inode
= dentry
->d_inode
;
2521 struct task_struct
*p
= get_proc_task(inode
);
2522 generic_fillattr(inode
, stat
);
2526 stat
->nlink
+= get_nr_threads(p
);
2534 static const struct inode_operations proc_task_inode_operations
= {
2535 .lookup
= proc_task_lookup
,
2536 .getattr
= proc_task_getattr
,
2537 .setattr
= proc_setattr
,
2540 static const struct file_operations proc_task_operations
= {
2541 .read
= generic_read_dir
,
2542 .readdir
= proc_task_readdir
,