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 const char *sym_name
;
283 unsigned long wchan
, size
, offset
;
284 char namebuf
[KSYM_NAME_LEN
+1];
286 wchan
= get_wchan(task
);
288 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
290 return sprintf(buffer
, "%s", sym_name
);
291 return sprintf(buffer
, "%lu", wchan
);
293 #endif /* CONFIG_KALLSYMS */
295 #ifdef CONFIG_SCHEDSTATS
297 * Provides /proc/PID/schedstat
299 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
301 return sprintf(buffer
, "%lu %lu %lu\n",
302 task
->sched_info
.cpu_time
,
303 task
->sched_info
.run_delay
,
304 task
->sched_info
.pcnt
);
308 /* The badness from the OOM killer */
309 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
310 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
312 unsigned long points
;
313 struct timespec uptime
;
315 do_posix_clock_monotonic_gettime(&uptime
);
316 read_lock(&tasklist_lock
);
317 points
= badness(task
, uptime
.tv_sec
);
318 read_unlock(&tasklist_lock
);
319 return sprintf(buffer
, "%lu\n", points
);
322 /************************************************************************/
323 /* Here the fs part begins */
324 /************************************************************************/
326 /* permission checks */
327 static int proc_fd_access_allowed(struct inode
*inode
)
329 struct task_struct
*task
;
331 /* Allow access to a task's file descriptors if it is us or we
332 * may use ptrace attach to the process and find out that
335 task
= get_proc_task(inode
);
337 allowed
= ptrace_may_attach(task
);
338 put_task_struct(task
);
343 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
346 struct inode
*inode
= dentry
->d_inode
;
348 if (attr
->ia_valid
& ATTR_MODE
)
351 error
= inode_change_ok(inode
, attr
);
353 error
= security_inode_setattr(dentry
, attr
);
355 error
= inode_setattr(inode
, attr
);
360 static const struct inode_operations proc_def_inode_operations
= {
361 .setattr
= proc_setattr
,
364 extern struct seq_operations mounts_op
;
370 static int mounts_open(struct inode
*inode
, struct file
*file
)
372 struct task_struct
*task
= get_proc_task(inode
);
373 struct mnt_namespace
*ns
= NULL
;
374 struct proc_mounts
*p
;
380 ns
= task
->nsproxy
->mnt_ns
;
385 put_task_struct(task
);
390 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
392 file
->private_data
= &p
->m
;
393 ret
= seq_open(file
, &mounts_op
);
396 p
->event
= ns
->event
;
406 static int mounts_release(struct inode
*inode
, struct file
*file
)
408 struct seq_file
*m
= file
->private_data
;
409 struct mnt_namespace
*ns
= m
->private;
411 return seq_release(inode
, file
);
414 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
416 struct proc_mounts
*p
= file
->private_data
;
417 struct mnt_namespace
*ns
= p
->m
.private;
420 poll_wait(file
, &ns
->poll
, wait
);
422 spin_lock(&vfsmount_lock
);
423 if (p
->event
!= ns
->event
) {
424 p
->event
= ns
->event
;
427 spin_unlock(&vfsmount_lock
);
432 static const struct file_operations proc_mounts_operations
= {
436 .release
= mounts_release
,
440 extern struct seq_operations mountstats_op
;
441 static int mountstats_open(struct inode
*inode
, struct file
*file
)
443 int ret
= seq_open(file
, &mountstats_op
);
446 struct seq_file
*m
= file
->private_data
;
447 struct mnt_namespace
*mnt_ns
= NULL
;
448 struct task_struct
*task
= get_proc_task(inode
);
453 mnt_ns
= task
->nsproxy
->mnt_ns
;
457 put_task_struct(task
);
463 seq_release(inode
, file
);
470 static const struct file_operations proc_mountstats_operations
= {
471 .open
= mountstats_open
,
474 .release
= mounts_release
,
477 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
479 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
480 size_t count
, loff_t
*ppos
)
482 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
485 struct task_struct
*task
= get_proc_task(inode
);
491 if (count
> PROC_BLOCK_SIZE
)
492 count
= PROC_BLOCK_SIZE
;
495 if (!(page
= __get_free_page(GFP_KERNEL
)))
498 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
501 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
504 put_task_struct(task
);
509 static const struct file_operations proc_info_file_operations
= {
510 .read
= proc_info_read
,
513 static int mem_open(struct inode
* inode
, struct file
* file
)
515 file
->private_data
= (void*)((long)current
->self_exec_id
);
519 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
520 size_t count
, loff_t
*ppos
)
522 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
524 unsigned long src
= *ppos
;
526 struct mm_struct
*mm
;
531 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
535 page
= (char *)__get_free_page(GFP_USER
);
541 mm
= get_task_mm(task
);
547 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
553 int this_len
, retval
;
555 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
556 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
557 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
563 if (copy_to_user(buf
, page
, retval
)) {
578 free_page((unsigned long) page
);
580 put_task_struct(task
);
585 #define mem_write NULL
588 /* This is a security hazard */
589 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
590 size_t count
, loff_t
*ppos
)
594 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
595 unsigned long dst
= *ppos
;
601 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
605 page
= (char *)__get_free_page(GFP_USER
);
611 int this_len
, retval
;
613 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
614 if (copy_from_user(page
, buf
, this_len
)) {
618 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
630 free_page((unsigned long) page
);
632 put_task_struct(task
);
638 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
642 file
->f_pos
= offset
;
645 file
->f_pos
+= offset
;
650 force_successful_syscall_return();
654 static const struct file_operations proc_mem_operations
= {
661 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
662 size_t count
, loff_t
*ppos
)
664 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
665 char buffer
[PROC_NUMBUF
];
668 loff_t __ppos
= *ppos
;
672 oom_adjust
= task
->oomkilladj
;
673 put_task_struct(task
);
675 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
678 if (count
> len
-__ppos
)
680 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
682 *ppos
= __ppos
+ count
;
686 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
687 size_t count
, loff_t
*ppos
)
689 struct task_struct
*task
;
690 char buffer
[PROC_NUMBUF
], *end
;
693 memset(buffer
, 0, sizeof(buffer
));
694 if (count
> sizeof(buffer
) - 1)
695 count
= sizeof(buffer
) - 1;
696 if (copy_from_user(buffer
, buf
, count
))
698 oom_adjust
= simple_strtol(buffer
, &end
, 0);
699 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
700 oom_adjust
!= OOM_DISABLE
)
704 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
707 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
708 put_task_struct(task
);
711 task
->oomkilladj
= oom_adjust
;
712 put_task_struct(task
);
713 if (end
- buffer
== 0)
718 static const struct file_operations proc_oom_adjust_operations
= {
719 .read
= oom_adjust_read
,
720 .write
= oom_adjust_write
,
723 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
724 size_t count
, loff_t
*ppos
)
726 struct task_struct
*task
;
727 char buffer
[PROC_NUMBUF
], *end
;
728 struct mm_struct
*mm
;
730 memset(buffer
, 0, sizeof(buffer
));
731 if (count
> sizeof(buffer
) - 1)
732 count
= sizeof(buffer
) - 1;
733 if (copy_from_user(buffer
, buf
, count
))
735 if (!simple_strtol(buffer
, &end
, 0))
739 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
742 mm
= get_task_mm(task
);
747 put_task_struct(task
);
748 if (end
- buffer
== 0)
753 static struct file_operations proc_clear_refs_operations
= {
754 .write
= clear_refs_write
,
757 #ifdef CONFIG_AUDITSYSCALL
759 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
760 size_t count
, loff_t
*ppos
)
762 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
763 struct task_struct
*task
= get_proc_task(inode
);
765 char tmpbuf
[TMPBUFLEN
];
769 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
770 audit_get_loginuid(task
->audit_context
));
771 put_task_struct(task
);
772 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
775 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
776 size_t count
, loff_t
*ppos
)
778 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
783 if (!capable(CAP_AUDIT_CONTROL
))
786 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
789 if (count
>= PAGE_SIZE
)
790 count
= PAGE_SIZE
- 1;
793 /* No partial writes. */
796 page
= (char*)__get_free_page(GFP_USER
);
800 if (copy_from_user(page
, buf
, count
))
804 loginuid
= simple_strtoul(page
, &tmp
, 10);
810 length
= audit_set_loginuid(current
, loginuid
);
811 if (likely(length
== 0))
815 free_page((unsigned long) page
);
819 static const struct file_operations proc_loginuid_operations
= {
820 .read
= proc_loginuid_read
,
821 .write
= proc_loginuid_write
,
825 #ifdef CONFIG_SECCOMP
826 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
827 size_t count
, loff_t
*ppos
)
829 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
831 loff_t __ppos
= *ppos
;
836 /* no need to print the trailing zero, so use only len */
837 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
838 put_task_struct(tsk
);
841 if (count
> len
- __ppos
)
842 count
= len
- __ppos
;
843 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
845 *ppos
= __ppos
+ count
;
849 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
850 size_t count
, loff_t
*ppos
)
852 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
853 char __buf
[20], *end
;
854 unsigned int seccomp_mode
;
861 /* can set it only once to be even more secure */
863 if (unlikely(tsk
->seccomp
.mode
))
867 memset(__buf
, 0, sizeof(__buf
));
868 count
= min(count
, sizeof(__buf
) - 1);
869 if (copy_from_user(__buf
, buf
, count
))
872 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
876 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
877 tsk
->seccomp
.mode
= seccomp_mode
;
878 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
882 if (unlikely(!(end
- __buf
)))
884 result
= end
- __buf
;
886 put_task_struct(tsk
);
891 static const struct file_operations proc_seccomp_operations
= {
892 .read
= seccomp_read
,
893 .write
= seccomp_write
,
895 #endif /* CONFIG_SECCOMP */
897 #ifdef CONFIG_FAULT_INJECTION
898 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
899 size_t count
, loff_t
*ppos
)
901 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
902 char buffer
[PROC_NUMBUF
];
905 loff_t __ppos
= *ppos
;
909 make_it_fail
= task
->make_it_fail
;
910 put_task_struct(task
);
912 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
915 if (count
> len
-__ppos
)
917 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
919 *ppos
= __ppos
+ count
;
923 static ssize_t
proc_fault_inject_write(struct file
* file
,
924 const char __user
* buf
, size_t count
, loff_t
*ppos
)
926 struct task_struct
*task
;
927 char buffer
[PROC_NUMBUF
], *end
;
930 if (!capable(CAP_SYS_RESOURCE
))
932 memset(buffer
, 0, sizeof(buffer
));
933 if (count
> sizeof(buffer
) - 1)
934 count
= sizeof(buffer
) - 1;
935 if (copy_from_user(buffer
, buf
, count
))
937 make_it_fail
= simple_strtol(buffer
, &end
, 0);
940 task
= get_proc_task(file
->f_dentry
->d_inode
);
943 task
->make_it_fail
= make_it_fail
;
944 put_task_struct(task
);
945 if (end
- buffer
== 0)
950 static const struct file_operations proc_fault_inject_operations
= {
951 .read
= proc_fault_inject_read
,
952 .write
= proc_fault_inject_write
,
956 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
958 struct inode
*inode
= dentry
->d_inode
;
961 /* We don't need a base pointer in the /proc filesystem */
964 /* Are we allowed to snoop on the tasks file descriptors? */
965 if (!proc_fd_access_allowed(inode
))
968 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
969 nd
->last_type
= LAST_BIND
;
971 return ERR_PTR(error
);
974 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
975 char __user
*buffer
, int buflen
)
977 struct inode
* inode
;
978 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
984 inode
= dentry
->d_inode
;
985 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
989 len
= tmp
+ PAGE_SIZE
- 1 - path
;
993 if (copy_to_user(buffer
, path
, len
))
996 free_page((unsigned long)tmp
);
1000 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1002 int error
= -EACCES
;
1003 struct inode
*inode
= dentry
->d_inode
;
1005 struct vfsmount
*mnt
= NULL
;
1007 /* Are we allowed to snoop on the tasks file descriptors? */
1008 if (!proc_fd_access_allowed(inode
))
1011 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1015 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1022 static const struct inode_operations proc_pid_link_inode_operations
= {
1023 .readlink
= proc_pid_readlink
,
1024 .follow_link
= proc_pid_follow_link
,
1025 .setattr
= proc_setattr
,
1029 /* building an inode */
1031 static int task_dumpable(struct task_struct
*task
)
1034 struct mm_struct
*mm
;
1039 dumpable
= mm
->dumpable
;
1047 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1049 struct inode
* inode
;
1050 struct proc_inode
*ei
;
1052 /* We need a new inode */
1054 inode
= new_inode(sb
);
1060 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1061 inode
->i_op
= &proc_def_inode_operations
;
1064 * grab the reference to task.
1066 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1072 if (task_dumpable(task
)) {
1073 inode
->i_uid
= task
->euid
;
1074 inode
->i_gid
= task
->egid
;
1076 security_task_to_inode(task
, inode
);
1086 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1088 struct inode
*inode
= dentry
->d_inode
;
1089 struct task_struct
*task
;
1090 generic_fillattr(inode
, stat
);
1095 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1097 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1098 task_dumpable(task
)) {
1099 stat
->uid
= task
->euid
;
1100 stat
->gid
= task
->egid
;
1110 * Exceptional case: normally we are not allowed to unhash a busy
1111 * directory. In this case, however, we can do it - no aliasing problems
1112 * due to the way we treat inodes.
1114 * Rewrite the inode's ownerships here because the owning task may have
1115 * performed a setuid(), etc.
1117 * Before the /proc/pid/status file was created the only way to read
1118 * the effective uid of a /process was to stat /proc/pid. Reading
1119 * /proc/pid/status is slow enough that procps and other packages
1120 * kept stating /proc/pid. To keep the rules in /proc simple I have
1121 * made this apply to all per process world readable and executable
1124 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1126 struct inode
*inode
= dentry
->d_inode
;
1127 struct task_struct
*task
= get_proc_task(inode
);
1129 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1130 task_dumpable(task
)) {
1131 inode
->i_uid
= task
->euid
;
1132 inode
->i_gid
= task
->egid
;
1137 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1138 security_task_to_inode(task
, inode
);
1139 put_task_struct(task
);
1146 static int pid_delete_dentry(struct dentry
* dentry
)
1148 /* Is the task we represent dead?
1149 * If so, then don't put the dentry on the lru list,
1150 * kill it immediately.
1152 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1155 static struct dentry_operations pid_dentry_operations
=
1157 .d_revalidate
= pid_revalidate
,
1158 .d_delete
= pid_delete_dentry
,
1163 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1164 struct task_struct
*, const void *);
1167 * Fill a directory entry.
1169 * If possible create the dcache entry and derive our inode number and
1170 * file type from dcache entry.
1172 * Since all of the proc inode numbers are dynamically generated, the inode
1173 * numbers do not exist until the inode is cache. This means creating the
1174 * the dcache entry in readdir is necessary to keep the inode numbers
1175 * reported by readdir in sync with the inode numbers reported
1178 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1179 char *name
, int len
,
1180 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1182 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1183 struct inode
*inode
;
1186 unsigned type
= DT_UNKNOWN
;
1190 qname
.hash
= full_name_hash(name
, len
);
1192 child
= d_lookup(dir
, &qname
);
1195 new = d_alloc(dir
, &qname
);
1197 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1204 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1205 goto end_instantiate
;
1206 inode
= child
->d_inode
;
1209 type
= inode
->i_mode
>> 12;
1214 ino
= find_inode_number(dir
, &qname
);
1217 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1220 static unsigned name_to_int(struct dentry
*dentry
)
1222 const char *name
= dentry
->d_name
.name
;
1223 int len
= dentry
->d_name
.len
;
1226 if (len
> 1 && *name
== '0')
1229 unsigned c
= *name
++ - '0';
1232 if (n
>= (~0U-9)/10)
1242 #define PROC_FDINFO_MAX 64
1244 static int proc_fd_info(struct inode
*inode
, struct dentry
**dentry
,
1245 struct vfsmount
**mnt
, char *info
)
1247 struct task_struct
*task
= get_proc_task(inode
);
1248 struct files_struct
*files
= NULL
;
1250 int fd
= proc_fd(inode
);
1253 files
= get_files_struct(task
);
1254 put_task_struct(task
);
1258 * We are not taking a ref to the file structure, so we must
1261 spin_lock(&files
->file_lock
);
1262 file
= fcheck_files(files
, fd
);
1265 *mnt
= mntget(file
->f_path
.mnt
);
1267 *dentry
= dget(file
->f_path
.dentry
);
1269 snprintf(info
, PROC_FDINFO_MAX
,
1272 (long long) file
->f_pos
,
1274 spin_unlock(&files
->file_lock
);
1275 put_files_struct(files
);
1278 spin_unlock(&files
->file_lock
);
1279 put_files_struct(files
);
1284 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
,
1285 struct vfsmount
**mnt
)
1287 return proc_fd_info(inode
, dentry
, mnt
, NULL
);
1290 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1292 struct inode
*inode
= dentry
->d_inode
;
1293 struct task_struct
*task
= get_proc_task(inode
);
1294 int fd
= proc_fd(inode
);
1295 struct files_struct
*files
;
1298 files
= get_files_struct(task
);
1301 if (fcheck_files(files
, fd
)) {
1303 put_files_struct(files
);
1304 if (task_dumpable(task
)) {
1305 inode
->i_uid
= task
->euid
;
1306 inode
->i_gid
= task
->egid
;
1311 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1312 security_task_to_inode(task
, inode
);
1313 put_task_struct(task
);
1317 put_files_struct(files
);
1319 put_task_struct(task
);
1325 static struct dentry_operations tid_fd_dentry_operations
=
1327 .d_revalidate
= tid_fd_revalidate
,
1328 .d_delete
= pid_delete_dentry
,
1331 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1332 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1334 unsigned fd
= *(const unsigned *)ptr
;
1336 struct files_struct
*files
;
1337 struct inode
*inode
;
1338 struct proc_inode
*ei
;
1339 struct dentry
*error
= ERR_PTR(-ENOENT
);
1341 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1346 files
= get_files_struct(task
);
1349 inode
->i_mode
= S_IFLNK
;
1352 * We are not taking a ref to the file structure, so we must
1355 spin_lock(&files
->file_lock
);
1356 file
= fcheck_files(files
, fd
);
1359 if (file
->f_mode
& 1)
1360 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1361 if (file
->f_mode
& 2)
1362 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1363 spin_unlock(&files
->file_lock
);
1364 put_files_struct(files
);
1366 inode
->i_op
= &proc_pid_link_inode_operations
;
1368 ei
->op
.proc_get_link
= proc_fd_link
;
1369 dentry
->d_op
= &tid_fd_dentry_operations
;
1370 d_add(dentry
, inode
);
1371 /* Close the race of the process dying before we return the dentry */
1372 if (tid_fd_revalidate(dentry
, NULL
))
1378 spin_unlock(&files
->file_lock
);
1379 put_files_struct(files
);
1385 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1386 struct dentry
*dentry
,
1387 instantiate_t instantiate
)
1389 struct task_struct
*task
= get_proc_task(dir
);
1390 unsigned fd
= name_to_int(dentry
);
1391 struct dentry
*result
= ERR_PTR(-ENOENT
);
1398 result
= instantiate(dir
, dentry
, task
, &fd
);
1400 put_task_struct(task
);
1405 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1406 filldir_t filldir
, instantiate_t instantiate
)
1408 struct dentry
*dentry
= filp
->f_path
.dentry
;
1409 struct inode
*inode
= dentry
->d_inode
;
1410 struct task_struct
*p
= get_proc_task(inode
);
1411 unsigned int fd
, tid
, ino
;
1413 struct files_struct
* files
;
1414 struct fdtable
*fdt
;
1425 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1429 ino
= parent_ino(dentry
);
1430 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1434 files
= get_files_struct(p
);
1438 fdt
= files_fdtable(files
);
1439 for (fd
= filp
->f_pos
-2;
1441 fd
++, filp
->f_pos
++) {
1442 char name
[PROC_NUMBUF
];
1445 if (!fcheck_files(files
, fd
))
1449 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1450 if (proc_fill_cache(filp
, dirent
, filldir
,
1451 name
, len
, instantiate
,
1459 put_files_struct(files
);
1467 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1468 struct nameidata
*nd
)
1470 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1473 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1475 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1478 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1479 size_t len
, loff_t
*ppos
)
1481 char tmp
[PROC_FDINFO_MAX
];
1482 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, NULL
, tmp
);
1484 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1488 static const struct file_operations proc_fdinfo_file_operations
= {
1489 .open
= nonseekable_open
,
1490 .read
= proc_fdinfo_read
,
1493 static const struct file_operations proc_fd_operations
= {
1494 .read
= generic_read_dir
,
1495 .readdir
= proc_readfd
,
1499 * /proc/pid/fd needs a special permission handler so that a process can still
1500 * access /proc/self/fd after it has executed a setuid().
1502 static int proc_fd_permission(struct inode
*inode
, int mask
,
1503 struct nameidata
*nd
)
1507 rv
= generic_permission(inode
, mask
, NULL
);
1510 if (task_pid(current
) == proc_pid(inode
))
1516 * proc directories can do almost nothing..
1518 static const struct inode_operations proc_fd_inode_operations
= {
1519 .lookup
= proc_lookupfd
,
1520 .permission
= proc_fd_permission
,
1521 .setattr
= proc_setattr
,
1524 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1525 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1527 unsigned fd
= *(unsigned *)ptr
;
1528 struct inode
*inode
;
1529 struct proc_inode
*ei
;
1530 struct dentry
*error
= ERR_PTR(-ENOENT
);
1532 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1537 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1538 inode
->i_fop
= &proc_fdinfo_file_operations
;
1539 dentry
->d_op
= &tid_fd_dentry_operations
;
1540 d_add(dentry
, inode
);
1541 /* Close the race of the process dying before we return the dentry */
1542 if (tid_fd_revalidate(dentry
, NULL
))
1549 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1550 struct dentry
*dentry
,
1551 struct nameidata
*nd
)
1553 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1556 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1558 return proc_readfd_common(filp
, dirent
, filldir
,
1559 proc_fdinfo_instantiate
);
1562 static const struct file_operations proc_fdinfo_operations
= {
1563 .read
= generic_read_dir
,
1564 .readdir
= proc_readfdinfo
,
1568 * proc directories can do almost nothing..
1570 static const struct inode_operations proc_fdinfo_inode_operations
= {
1571 .lookup
= proc_lookupfdinfo
,
1572 .setattr
= proc_setattr
,
1576 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1577 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1579 const struct pid_entry
*p
= ptr
;
1580 struct inode
*inode
;
1581 struct proc_inode
*ei
;
1582 struct dentry
*error
= ERR_PTR(-EINVAL
);
1584 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1589 inode
->i_mode
= p
->mode
;
1590 if (S_ISDIR(inode
->i_mode
))
1591 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1593 inode
->i_op
= p
->iop
;
1595 inode
->i_fop
= p
->fop
;
1597 dentry
->d_op
= &pid_dentry_operations
;
1598 d_add(dentry
, inode
);
1599 /* Close the race of the process dying before we return the dentry */
1600 if (pid_revalidate(dentry
, NULL
))
1606 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1607 struct dentry
*dentry
,
1608 const struct pid_entry
*ents
,
1611 struct inode
*inode
;
1612 struct dentry
*error
;
1613 struct task_struct
*task
= get_proc_task(dir
);
1614 const struct pid_entry
*p
, *last
;
1616 error
= ERR_PTR(-ENOENT
);
1623 * Yes, it does not scale. And it should not. Don't add
1624 * new entries into /proc/<tgid>/ without very good reasons.
1626 last
= &ents
[nents
- 1];
1627 for (p
= ents
; p
<= last
; p
++) {
1628 if (p
->len
!= dentry
->d_name
.len
)
1630 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1636 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1638 put_task_struct(task
);
1643 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1644 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1646 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1647 proc_pident_instantiate
, task
, p
);
1650 static int proc_pident_readdir(struct file
*filp
,
1651 void *dirent
, filldir_t filldir
,
1652 const struct pid_entry
*ents
, unsigned int nents
)
1656 struct dentry
*dentry
= filp
->f_path
.dentry
;
1657 struct inode
*inode
= dentry
->d_inode
;
1658 struct task_struct
*task
= get_proc_task(inode
);
1659 const struct pid_entry
*p
, *last
;
1673 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1679 ino
= parent_ino(dentry
);
1680 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1692 last
= &ents
[nents
- 1];
1694 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
1703 put_task_struct(task
);
1708 #ifdef CONFIG_SECURITY
1709 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1710 size_t count
, loff_t
*ppos
)
1712 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1715 struct task_struct
*task
= get_proc_task(inode
);
1720 length
= security_getprocattr(task
,
1721 (char*)file
->f_path
.dentry
->d_name
.name
,
1723 put_task_struct(task
);
1725 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
1730 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1731 size_t count
, loff_t
*ppos
)
1733 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1736 struct task_struct
*task
= get_proc_task(inode
);
1741 if (count
> PAGE_SIZE
)
1744 /* No partial writes. */
1750 page
= (char*)__get_free_page(GFP_USER
);
1755 if (copy_from_user(page
, buf
, count
))
1758 length
= security_setprocattr(task
,
1759 (char*)file
->f_path
.dentry
->d_name
.name
,
1760 (void*)page
, count
);
1762 free_page((unsigned long) page
);
1764 put_task_struct(task
);
1769 static const struct file_operations proc_pid_attr_operations
= {
1770 .read
= proc_pid_attr_read
,
1771 .write
= proc_pid_attr_write
,
1774 static const struct pid_entry attr_dir_stuff
[] = {
1775 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
1776 REG("prev", S_IRUGO
, pid_attr
),
1777 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
1778 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1779 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1780 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1783 static int proc_attr_dir_readdir(struct file
* filp
,
1784 void * dirent
, filldir_t filldir
)
1786 return proc_pident_readdir(filp
,dirent
,filldir
,
1787 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
1790 static const struct file_operations proc_attr_dir_operations
= {
1791 .read
= generic_read_dir
,
1792 .readdir
= proc_attr_dir_readdir
,
1795 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
1796 struct dentry
*dentry
, struct nameidata
*nd
)
1798 return proc_pident_lookup(dir
, dentry
,
1799 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
1802 static const struct inode_operations proc_attr_dir_inode_operations
= {
1803 .lookup
= proc_attr_dir_lookup
,
1804 .getattr
= pid_getattr
,
1805 .setattr
= proc_setattr
,
1813 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1816 char tmp
[PROC_NUMBUF
];
1817 sprintf(tmp
, "%d", current
->tgid
);
1818 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1821 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1823 char tmp
[PROC_NUMBUF
];
1824 sprintf(tmp
, "%d", current
->tgid
);
1825 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1828 static const struct inode_operations proc_self_inode_operations
= {
1829 .readlink
= proc_self_readlink
,
1830 .follow_link
= proc_self_follow_link
,
1836 * These are the directory entries in the root directory of /proc
1837 * that properly belong to the /proc filesystem, as they describe
1838 * describe something that is process related.
1840 static const struct pid_entry proc_base_stuff
[] = {
1841 NOD("self", S_IFLNK
|S_IRWXUGO
,
1842 &proc_self_inode_operations
, NULL
, {}),
1846 * Exceptional case: normally we are not allowed to unhash a busy
1847 * directory. In this case, however, we can do it - no aliasing problems
1848 * due to the way we treat inodes.
1850 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1852 struct inode
*inode
= dentry
->d_inode
;
1853 struct task_struct
*task
= get_proc_task(inode
);
1855 put_task_struct(task
);
1862 static struct dentry_operations proc_base_dentry_operations
=
1864 .d_revalidate
= proc_base_revalidate
,
1865 .d_delete
= pid_delete_dentry
,
1868 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
1869 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1871 const struct pid_entry
*p
= ptr
;
1872 struct inode
*inode
;
1873 struct proc_inode
*ei
;
1874 struct dentry
*error
= ERR_PTR(-EINVAL
);
1876 /* Allocate the inode */
1877 error
= ERR_PTR(-ENOMEM
);
1878 inode
= new_inode(dir
->i_sb
);
1882 /* Initialize the inode */
1884 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1887 * grab the reference to the task.
1889 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1895 inode
->i_mode
= p
->mode
;
1896 if (S_ISDIR(inode
->i_mode
))
1898 if (S_ISLNK(inode
->i_mode
))
1901 inode
->i_op
= p
->iop
;
1903 inode
->i_fop
= p
->fop
;
1905 dentry
->d_op
= &proc_base_dentry_operations
;
1906 d_add(dentry
, inode
);
1915 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
1917 struct dentry
*error
;
1918 struct task_struct
*task
= get_proc_task(dir
);
1919 const struct pid_entry
*p
, *last
;
1921 error
= ERR_PTR(-ENOENT
);
1926 /* Lookup the directory entry */
1927 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
1928 for (p
= proc_base_stuff
; p
<= last
; p
++) {
1929 if (p
->len
!= dentry
->d_name
.len
)
1931 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1937 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
1940 put_task_struct(task
);
1945 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
1946 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1948 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1949 proc_base_instantiate
, task
, p
);
1952 #ifdef CONFIG_TASK_IO_ACCOUNTING
1953 static int proc_pid_io_accounting(struct task_struct
*task
, char *buffer
)
1955 return sprintf(buffer
,
1956 #ifdef CONFIG_TASK_XACCT
1962 "read_bytes: %llu\n"
1963 "write_bytes: %llu\n"
1964 "cancelled_write_bytes: %llu\n",
1965 #ifdef CONFIG_TASK_XACCT
1966 (unsigned long long)task
->rchar
,
1967 (unsigned long long)task
->wchar
,
1968 (unsigned long long)task
->syscr
,
1969 (unsigned long long)task
->syscw
,
1971 (unsigned long long)task
->ioac
.read_bytes
,
1972 (unsigned long long)task
->ioac
.write_bytes
,
1973 (unsigned long long)task
->ioac
.cancelled_write_bytes
);
1980 static const struct file_operations proc_task_operations
;
1981 static const struct inode_operations proc_task_inode_operations
;
1983 static const struct pid_entry tgid_base_stuff
[] = {
1984 DIR("task", S_IRUGO
|S_IXUGO
, task
),
1985 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
1986 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
1987 INF("environ", S_IRUSR
, pid_environ
),
1988 INF("auxv", S_IRUSR
, pid_auxv
),
1989 INF("status", S_IRUGO
, pid_status
),
1990 INF("cmdline", S_IRUGO
, pid_cmdline
),
1991 INF("stat", S_IRUGO
, tgid_stat
),
1992 INF("statm", S_IRUGO
, pid_statm
),
1993 REG("maps", S_IRUGO
, maps
),
1995 REG("numa_maps", S_IRUGO
, numa_maps
),
1997 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
1998 #ifdef CONFIG_SECCOMP
1999 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
2004 REG("mounts", S_IRUGO
, mounts
),
2005 REG("mountstats", S_IRUSR
, mountstats
),
2007 REG("clear_refs", S_IWUSR
, clear_refs
),
2008 REG("smaps", S_IRUGO
, smaps
),
2010 #ifdef CONFIG_SECURITY
2011 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2013 #ifdef CONFIG_KALLSYMS
2014 INF("wchan", S_IRUGO
, pid_wchan
),
2016 #ifdef CONFIG_SCHEDSTATS
2017 INF("schedstat", S_IRUGO
, pid_schedstat
),
2019 #ifdef CONFIG_CPUSETS
2020 REG("cpuset", S_IRUGO
, cpuset
),
2022 INF("oom_score", S_IRUGO
, oom_score
),
2023 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2024 #ifdef CONFIG_AUDITSYSCALL
2025 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2027 #ifdef CONFIG_FAULT_INJECTION
2028 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2030 #ifdef CONFIG_TASK_IO_ACCOUNTING
2031 INF("io", S_IRUGO
, pid_io_accounting
),
2035 static int proc_tgid_base_readdir(struct file
* filp
,
2036 void * dirent
, filldir_t filldir
)
2038 return proc_pident_readdir(filp
,dirent
,filldir
,
2039 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2042 static const struct file_operations proc_tgid_base_operations
= {
2043 .read
= generic_read_dir
,
2044 .readdir
= proc_tgid_base_readdir
,
2047 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2048 return proc_pident_lookup(dir
, dentry
,
2049 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2052 static const struct inode_operations proc_tgid_base_inode_operations
= {
2053 .lookup
= proc_tgid_base_lookup
,
2054 .getattr
= pid_getattr
,
2055 .setattr
= proc_setattr
,
2059 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2061 * @task: task that should be flushed.
2063 * Looks in the dcache for
2065 * /proc/@tgid/task/@pid
2066 * if either directory is present flushes it and all of it'ts children
2069 * It is safe and reasonable to cache /proc entries for a task until
2070 * that task exits. After that they just clog up the dcache with
2071 * useless entries, possibly causing useful dcache entries to be
2072 * flushed instead. This routine is proved to flush those useless
2073 * dcache entries at process exit time.
2075 * NOTE: This routine is just an optimization so it does not guarantee
2076 * that no dcache entries will exist at process exit time it
2077 * just makes it very unlikely that any will persist.
2079 void proc_flush_task(struct task_struct
*task
)
2081 struct dentry
*dentry
, *leader
, *dir
;
2082 char buf
[PROC_NUMBUF
];
2086 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2087 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2089 shrink_dcache_parent(dentry
);
2094 if (thread_group_leader(task
))
2098 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
2099 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2104 name
.len
= strlen(name
.name
);
2105 dir
= d_hash_and_lookup(leader
, &name
);
2107 goto out_put_leader
;
2110 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2111 dentry
= d_hash_and_lookup(dir
, &name
);
2113 shrink_dcache_parent(dentry
);
2125 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2126 struct dentry
* dentry
,
2127 struct task_struct
*task
, const void *ptr
)
2129 struct dentry
*error
= ERR_PTR(-ENOENT
);
2130 struct inode
*inode
;
2132 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2136 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2137 inode
->i_op
= &proc_tgid_base_inode_operations
;
2138 inode
->i_fop
= &proc_tgid_base_operations
;
2139 inode
->i_flags
|=S_IMMUTABLE
;
2141 #ifdef CONFIG_SECURITY
2142 inode
->i_nlink
+= 1;
2145 dentry
->d_op
= &pid_dentry_operations
;
2147 d_add(dentry
, inode
);
2148 /* Close the race of the process dying before we return the dentry */
2149 if (pid_revalidate(dentry
, NULL
))
2155 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2157 struct dentry
*result
= ERR_PTR(-ENOENT
);
2158 struct task_struct
*task
;
2161 result
= proc_base_lookup(dir
, dentry
);
2162 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2165 tgid
= name_to_int(dentry
);
2170 task
= find_task_by_pid(tgid
);
2172 get_task_struct(task
);
2177 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2178 put_task_struct(task
);
2184 * Find the first task with tgid >= tgid
2187 static struct task_struct
*next_tgid(unsigned int tgid
)
2189 struct task_struct
*task
;
2195 pid
= find_ge_pid(tgid
);
2198 task
= pid_task(pid
, PIDTYPE_PID
);
2199 /* What we to know is if the pid we have find is the
2200 * pid of a thread_group_leader. Testing for task
2201 * being a thread_group_leader is the obvious thing
2202 * todo but there is a window when it fails, due to
2203 * the pid transfer logic in de_thread.
2205 * So we perform the straight forward test of seeing
2206 * if the pid we have found is the pid of a thread
2207 * group leader, and don't worry if the task we have
2208 * found doesn't happen to be a thread group leader.
2209 * As we don't care in the case of readdir.
2211 if (!task
|| !has_group_leader_pid(task
))
2213 get_task_struct(task
);
2219 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2221 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2222 struct task_struct
*task
, int tgid
)
2224 char name
[PROC_NUMBUF
];
2225 int len
= snprintf(name
, sizeof(name
), "%d", tgid
);
2226 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2227 proc_pid_instantiate
, task
, NULL
);
2230 /* for the /proc/ directory itself, after non-process stuff has been done */
2231 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2233 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2234 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2235 struct task_struct
*task
;
2241 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2242 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2243 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2247 tgid
= filp
->f_pos
- TGID_OFFSET
;
2248 for (task
= next_tgid(tgid
);
2250 put_task_struct(task
), task
= next_tgid(tgid
+ 1)) {
2252 filp
->f_pos
= tgid
+ TGID_OFFSET
;
2253 if (proc_pid_fill_cache(filp
, dirent
, filldir
, task
, tgid
) < 0) {
2254 put_task_struct(task
);
2258 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2260 put_task_struct(reaper
);
2268 static const struct pid_entry tid_base_stuff
[] = {
2269 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2270 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2271 INF("environ", S_IRUSR
, pid_environ
),
2272 INF("auxv", S_IRUSR
, pid_auxv
),
2273 INF("status", S_IRUGO
, pid_status
),
2274 INF("cmdline", S_IRUGO
, pid_cmdline
),
2275 INF("stat", S_IRUGO
, tid_stat
),
2276 INF("statm", S_IRUGO
, pid_statm
),
2277 REG("maps", S_IRUGO
, maps
),
2279 REG("numa_maps", S_IRUGO
, numa_maps
),
2281 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2282 #ifdef CONFIG_SECCOMP
2283 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
2288 REG("mounts", S_IRUGO
, mounts
),
2290 REG("clear_refs", S_IWUSR
, clear_refs
),
2291 REG("smaps", S_IRUGO
, smaps
),
2293 #ifdef CONFIG_SECURITY
2294 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2296 #ifdef CONFIG_KALLSYMS
2297 INF("wchan", S_IRUGO
, pid_wchan
),
2299 #ifdef CONFIG_SCHEDSTATS
2300 INF("schedstat", S_IRUGO
, pid_schedstat
),
2302 #ifdef CONFIG_CPUSETS
2303 REG("cpuset", S_IRUGO
, cpuset
),
2305 INF("oom_score", S_IRUGO
, oom_score
),
2306 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2307 #ifdef CONFIG_AUDITSYSCALL
2308 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2310 #ifdef CONFIG_FAULT_INJECTION
2311 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2315 static int proc_tid_base_readdir(struct file
* filp
,
2316 void * dirent
, filldir_t filldir
)
2318 return proc_pident_readdir(filp
,dirent
,filldir
,
2319 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2322 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2323 return proc_pident_lookup(dir
, dentry
,
2324 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2327 static const struct file_operations proc_tid_base_operations
= {
2328 .read
= generic_read_dir
,
2329 .readdir
= proc_tid_base_readdir
,
2332 static const struct inode_operations proc_tid_base_inode_operations
= {
2333 .lookup
= proc_tid_base_lookup
,
2334 .getattr
= pid_getattr
,
2335 .setattr
= proc_setattr
,
2338 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2339 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2341 struct dentry
*error
= ERR_PTR(-ENOENT
);
2342 struct inode
*inode
;
2343 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2347 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2348 inode
->i_op
= &proc_tid_base_inode_operations
;
2349 inode
->i_fop
= &proc_tid_base_operations
;
2350 inode
->i_flags
|=S_IMMUTABLE
;
2352 #ifdef CONFIG_SECURITY
2353 inode
->i_nlink
+= 1;
2356 dentry
->d_op
= &pid_dentry_operations
;
2358 d_add(dentry
, inode
);
2359 /* Close the race of the process dying before we return the dentry */
2360 if (pid_revalidate(dentry
, NULL
))
2366 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2368 struct dentry
*result
= ERR_PTR(-ENOENT
);
2369 struct task_struct
*task
;
2370 struct task_struct
*leader
= get_proc_task(dir
);
2376 tid
= name_to_int(dentry
);
2381 task
= find_task_by_pid(tid
);
2383 get_task_struct(task
);
2387 if (leader
->tgid
!= task
->tgid
)
2390 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2392 put_task_struct(task
);
2394 put_task_struct(leader
);
2400 * Find the first tid of a thread group to return to user space.
2402 * Usually this is just the thread group leader, but if the users
2403 * buffer was too small or there was a seek into the middle of the
2404 * directory we have more work todo.
2406 * In the case of a short read we start with find_task_by_pid.
2408 * In the case of a seek we start with the leader and walk nr
2411 static struct task_struct
*first_tid(struct task_struct
*leader
,
2414 struct task_struct
*pos
;
2417 /* Attempt to start with the pid of a thread */
2418 if (tid
&& (nr
> 0)) {
2419 pos
= find_task_by_pid(tid
);
2420 if (pos
&& (pos
->group_leader
== leader
))
2424 /* If nr exceeds the number of threads there is nothing todo */
2426 if (nr
&& nr
>= get_nr_threads(leader
))
2429 /* If we haven't found our starting place yet start
2430 * with the leader and walk nr threads forward.
2432 for (pos
= leader
; nr
> 0; --nr
) {
2433 pos
= next_thread(pos
);
2434 if (pos
== leader
) {
2440 get_task_struct(pos
);
2447 * Find the next thread in the thread list.
2448 * Return NULL if there is an error or no next thread.
2450 * The reference to the input task_struct is released.
2452 static struct task_struct
*next_tid(struct task_struct
*start
)
2454 struct task_struct
*pos
= NULL
;
2456 if (pid_alive(start
)) {
2457 pos
= next_thread(start
);
2458 if (thread_group_leader(pos
))
2461 get_task_struct(pos
);
2464 put_task_struct(start
);
2468 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2469 struct task_struct
*task
, int tid
)
2471 char name
[PROC_NUMBUF
];
2472 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2473 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2474 proc_task_instantiate
, task
, NULL
);
2477 /* for the /proc/TGID/task/ directories */
2478 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2480 struct dentry
*dentry
= filp
->f_path
.dentry
;
2481 struct inode
*inode
= dentry
->d_inode
;
2482 struct task_struct
*leader
= NULL
;
2483 struct task_struct
*task
;
2484 int retval
= -ENOENT
;
2487 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2489 task
= get_proc_task(inode
);
2493 if (pid_alive(task
)) {
2494 leader
= task
->group_leader
;
2495 get_task_struct(leader
);
2498 put_task_struct(task
);
2506 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2511 ino
= parent_ino(dentry
);
2512 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2518 /* f_version caches the tgid value that the last readdir call couldn't
2519 * return. lseek aka telldir automagically resets f_version to 0.
2521 tid
= filp
->f_version
;
2522 filp
->f_version
= 0;
2523 for (task
= first_tid(leader
, tid
, pos
- 2);
2525 task
= next_tid(task
), pos
++) {
2527 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
2528 /* returning this tgid failed, save it as the first
2529 * pid for the next readir call */
2530 filp
->f_version
= tid
;
2531 put_task_struct(task
);
2537 put_task_struct(leader
);
2542 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2544 struct inode
*inode
= dentry
->d_inode
;
2545 struct task_struct
*p
= get_proc_task(inode
);
2546 generic_fillattr(inode
, stat
);
2550 stat
->nlink
+= get_nr_threads(p
);
2558 static const struct inode_operations proc_task_inode_operations
= {
2559 .lookup
= proc_task_lookup
,
2560 .getattr
= proc_task_getattr
,
2561 .setattr
= proc_setattr
,
2564 static const struct file_operations proc_task_operations
= {
2565 .read
= generic_read_dir
,
2566 .readdir
= proc_task_readdir
,