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
];
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
= 0;
311 struct timespec uptime
;
313 do_posix_clock_monotonic_gettime(&uptime
);
314 read_lock(&tasklist_lock
);
316 points
= badness(task
, uptime
.tv_sec
);
317 read_unlock(&tasklist_lock
);
318 return sprintf(buffer
, "%lu\n", points
);
321 /************************************************************************/
322 /* Here the fs part begins */
323 /************************************************************************/
325 /* permission checks */
326 static int proc_fd_access_allowed(struct inode
*inode
)
328 struct task_struct
*task
;
330 /* Allow access to a task's file descriptors if it is us or we
331 * may use ptrace attach to the process and find out that
334 task
= get_proc_task(inode
);
336 allowed
= ptrace_may_attach(task
);
337 put_task_struct(task
);
342 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
345 struct inode
*inode
= dentry
->d_inode
;
347 if (attr
->ia_valid
& ATTR_MODE
)
350 error
= inode_change_ok(inode
, attr
);
352 error
= inode_setattr(inode
, attr
);
356 static const struct inode_operations proc_def_inode_operations
= {
357 .setattr
= proc_setattr
,
360 extern struct seq_operations mounts_op
;
366 static int mounts_open(struct inode
*inode
, struct file
*file
)
368 struct task_struct
*task
= get_proc_task(inode
);
369 struct mnt_namespace
*ns
= NULL
;
370 struct proc_mounts
*p
;
376 ns
= task
->nsproxy
->mnt_ns
;
381 put_task_struct(task
);
386 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
388 file
->private_data
= &p
->m
;
389 ret
= seq_open(file
, &mounts_op
);
392 p
->event
= ns
->event
;
402 static int mounts_release(struct inode
*inode
, struct file
*file
)
404 struct seq_file
*m
= file
->private_data
;
405 struct mnt_namespace
*ns
= m
->private;
407 return seq_release(inode
, file
);
410 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
412 struct proc_mounts
*p
= file
->private_data
;
413 struct mnt_namespace
*ns
= p
->m
.private;
416 poll_wait(file
, &ns
->poll
, wait
);
418 spin_lock(&vfsmount_lock
);
419 if (p
->event
!= ns
->event
) {
420 p
->event
= ns
->event
;
423 spin_unlock(&vfsmount_lock
);
428 static const struct file_operations proc_mounts_operations
= {
432 .release
= mounts_release
,
436 extern struct seq_operations mountstats_op
;
437 static int mountstats_open(struct inode
*inode
, struct file
*file
)
439 int ret
= seq_open(file
, &mountstats_op
);
442 struct seq_file
*m
= file
->private_data
;
443 struct mnt_namespace
*mnt_ns
= NULL
;
444 struct task_struct
*task
= get_proc_task(inode
);
449 mnt_ns
= task
->nsproxy
->mnt_ns
;
453 put_task_struct(task
);
459 seq_release(inode
, file
);
466 static const struct file_operations proc_mountstats_operations
= {
467 .open
= mountstats_open
,
470 .release
= mounts_release
,
473 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
475 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
476 size_t count
, loff_t
*ppos
)
478 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
481 struct task_struct
*task
= get_proc_task(inode
);
487 if (count
> PROC_BLOCK_SIZE
)
488 count
= PROC_BLOCK_SIZE
;
491 if (!(page
= __get_free_page(GFP_KERNEL
)))
494 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
497 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
500 put_task_struct(task
);
505 static const struct file_operations proc_info_file_operations
= {
506 .read
= proc_info_read
,
509 static int mem_open(struct inode
* inode
, struct file
* file
)
511 file
->private_data
= (void*)((long)current
->self_exec_id
);
515 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
516 size_t count
, loff_t
*ppos
)
518 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
520 unsigned long src
= *ppos
;
522 struct mm_struct
*mm
;
527 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
531 page
= (char *)__get_free_page(GFP_USER
);
537 mm
= get_task_mm(task
);
543 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
549 int this_len
, retval
;
551 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
552 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
553 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
559 if (copy_to_user(buf
, page
, retval
)) {
574 free_page((unsigned long) page
);
576 put_task_struct(task
);
581 #define mem_write NULL
584 /* This is a security hazard */
585 static ssize_t
mem_write(struct file
* file
, const char __user
*buf
,
586 size_t count
, loff_t
*ppos
)
590 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
591 unsigned long dst
= *ppos
;
597 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
601 page
= (char *)__get_free_page(GFP_USER
);
607 int this_len
, retval
;
609 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
610 if (copy_from_user(page
, buf
, this_len
)) {
614 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
626 free_page((unsigned long) page
);
628 put_task_struct(task
);
634 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
638 file
->f_pos
= offset
;
641 file
->f_pos
+= offset
;
646 force_successful_syscall_return();
650 static const struct file_operations proc_mem_operations
= {
657 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
658 size_t count
, loff_t
*ppos
)
660 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
661 char buffer
[PROC_NUMBUF
];
667 oom_adjust
= task
->oomkilladj
;
668 put_task_struct(task
);
670 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
672 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
675 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
676 size_t count
, loff_t
*ppos
)
678 struct task_struct
*task
;
679 char buffer
[PROC_NUMBUF
], *end
;
682 memset(buffer
, 0, sizeof(buffer
));
683 if (count
> sizeof(buffer
) - 1)
684 count
= sizeof(buffer
) - 1;
685 if (copy_from_user(buffer
, buf
, count
))
687 oom_adjust
= simple_strtol(buffer
, &end
, 0);
688 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
689 oom_adjust
!= OOM_DISABLE
)
693 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
696 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
697 put_task_struct(task
);
700 task
->oomkilladj
= oom_adjust
;
701 put_task_struct(task
);
702 if (end
- buffer
== 0)
707 static const struct file_operations proc_oom_adjust_operations
= {
708 .read
= oom_adjust_read
,
709 .write
= oom_adjust_write
,
713 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
714 size_t count
, loff_t
*ppos
)
716 struct task_struct
*task
;
717 char buffer
[PROC_NUMBUF
], *end
;
718 struct mm_struct
*mm
;
720 memset(buffer
, 0, sizeof(buffer
));
721 if (count
> sizeof(buffer
) - 1)
722 count
= sizeof(buffer
) - 1;
723 if (copy_from_user(buffer
, buf
, count
))
725 if (!simple_strtol(buffer
, &end
, 0))
729 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
732 mm
= get_task_mm(task
);
737 put_task_struct(task
);
738 if (end
- buffer
== 0)
743 static struct file_operations proc_clear_refs_operations
= {
744 .write
= clear_refs_write
,
748 #ifdef CONFIG_AUDITSYSCALL
750 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
751 size_t count
, loff_t
*ppos
)
753 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
754 struct task_struct
*task
= get_proc_task(inode
);
756 char tmpbuf
[TMPBUFLEN
];
760 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
761 audit_get_loginuid(task
->audit_context
));
762 put_task_struct(task
);
763 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
766 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
767 size_t count
, loff_t
*ppos
)
769 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
774 if (!capable(CAP_AUDIT_CONTROL
))
777 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
780 if (count
>= PAGE_SIZE
)
781 count
= PAGE_SIZE
- 1;
784 /* No partial writes. */
787 page
= (char*)__get_free_page(GFP_USER
);
791 if (copy_from_user(page
, buf
, count
))
795 loginuid
= simple_strtoul(page
, &tmp
, 10);
801 length
= audit_set_loginuid(current
, loginuid
);
802 if (likely(length
== 0))
806 free_page((unsigned long) page
);
810 static const struct file_operations proc_loginuid_operations
= {
811 .read
= proc_loginuid_read
,
812 .write
= proc_loginuid_write
,
816 #ifdef CONFIG_SECCOMP
817 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
818 size_t count
, loff_t
*ppos
)
820 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
826 /* no need to print the trailing zero, so use only len */
827 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
828 put_task_struct(tsk
);
830 return simple_read_from_buffer(buf
, count
, ppos
, __buf
, len
);
833 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
834 size_t count
, loff_t
*ppos
)
836 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
837 char __buf
[20], *end
;
838 unsigned int seccomp_mode
;
845 /* can set it only once to be even more secure */
847 if (unlikely(tsk
->seccomp
.mode
))
851 memset(__buf
, 0, sizeof(__buf
));
852 count
= min(count
, sizeof(__buf
) - 1);
853 if (copy_from_user(__buf
, buf
, count
))
856 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
860 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
861 tsk
->seccomp
.mode
= seccomp_mode
;
862 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
866 if (unlikely(!(end
- __buf
)))
868 result
= end
- __buf
;
870 put_task_struct(tsk
);
875 static const struct file_operations proc_seccomp_operations
= {
876 .read
= seccomp_read
,
877 .write
= seccomp_write
,
879 #endif /* CONFIG_SECCOMP */
881 #ifdef CONFIG_FAULT_INJECTION
882 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
883 size_t count
, loff_t
*ppos
)
885 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
886 char buffer
[PROC_NUMBUF
];
892 make_it_fail
= task
->make_it_fail
;
893 put_task_struct(task
);
895 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
897 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
900 static ssize_t
proc_fault_inject_write(struct file
* file
,
901 const char __user
* buf
, size_t count
, loff_t
*ppos
)
903 struct task_struct
*task
;
904 char buffer
[PROC_NUMBUF
], *end
;
907 if (!capable(CAP_SYS_RESOURCE
))
909 memset(buffer
, 0, sizeof(buffer
));
910 if (count
> sizeof(buffer
) - 1)
911 count
= sizeof(buffer
) - 1;
912 if (copy_from_user(buffer
, buf
, count
))
914 make_it_fail
= simple_strtol(buffer
, &end
, 0);
917 task
= get_proc_task(file
->f_dentry
->d_inode
);
920 task
->make_it_fail
= make_it_fail
;
921 put_task_struct(task
);
922 if (end
- buffer
== 0)
927 static const struct file_operations proc_fault_inject_operations
= {
928 .read
= proc_fault_inject_read
,
929 .write
= proc_fault_inject_write
,
933 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
935 struct inode
*inode
= dentry
->d_inode
;
938 /* We don't need a base pointer in the /proc filesystem */
941 /* Are we allowed to snoop on the tasks file descriptors? */
942 if (!proc_fd_access_allowed(inode
))
945 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
946 nd
->last_type
= LAST_BIND
;
948 return ERR_PTR(error
);
951 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
952 char __user
*buffer
, int buflen
)
954 struct inode
* inode
;
955 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
961 inode
= dentry
->d_inode
;
962 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
966 len
= tmp
+ PAGE_SIZE
- 1 - path
;
970 if (copy_to_user(buffer
, path
, len
))
973 free_page((unsigned long)tmp
);
977 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
980 struct inode
*inode
= dentry
->d_inode
;
982 struct vfsmount
*mnt
= NULL
;
984 /* Are we allowed to snoop on the tasks file descriptors? */
985 if (!proc_fd_access_allowed(inode
))
988 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
992 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
999 static const struct inode_operations proc_pid_link_inode_operations
= {
1000 .readlink
= proc_pid_readlink
,
1001 .follow_link
= proc_pid_follow_link
,
1002 .setattr
= proc_setattr
,
1006 /* building an inode */
1008 static int task_dumpable(struct task_struct
*task
)
1011 struct mm_struct
*mm
;
1016 dumpable
= mm
->dumpable
;
1024 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1026 struct inode
* inode
;
1027 struct proc_inode
*ei
;
1029 /* We need a new inode */
1031 inode
= new_inode(sb
);
1037 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1038 inode
->i_op
= &proc_def_inode_operations
;
1041 * grab the reference to task.
1043 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1049 if (task_dumpable(task
)) {
1050 inode
->i_uid
= task
->euid
;
1051 inode
->i_gid
= task
->egid
;
1053 security_task_to_inode(task
, inode
);
1063 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1065 struct inode
*inode
= dentry
->d_inode
;
1066 struct task_struct
*task
;
1067 generic_fillattr(inode
, stat
);
1072 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1074 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1075 task_dumpable(task
)) {
1076 stat
->uid
= task
->euid
;
1077 stat
->gid
= task
->egid
;
1087 * Exceptional case: normally we are not allowed to unhash a busy
1088 * directory. In this case, however, we can do it - no aliasing problems
1089 * due to the way we treat inodes.
1091 * Rewrite the inode's ownerships here because the owning task may have
1092 * performed a setuid(), etc.
1094 * Before the /proc/pid/status file was created the only way to read
1095 * the effective uid of a /process was to stat /proc/pid. Reading
1096 * /proc/pid/status is slow enough that procps and other packages
1097 * kept stating /proc/pid. To keep the rules in /proc simple I have
1098 * made this apply to all per process world readable and executable
1101 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1103 struct inode
*inode
= dentry
->d_inode
;
1104 struct task_struct
*task
= get_proc_task(inode
);
1106 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1107 task_dumpable(task
)) {
1108 inode
->i_uid
= task
->euid
;
1109 inode
->i_gid
= task
->egid
;
1114 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1115 security_task_to_inode(task
, inode
);
1116 put_task_struct(task
);
1123 static int pid_delete_dentry(struct dentry
* dentry
)
1125 /* Is the task we represent dead?
1126 * If so, then don't put the dentry on the lru list,
1127 * kill it immediately.
1129 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1132 static struct dentry_operations pid_dentry_operations
=
1134 .d_revalidate
= pid_revalidate
,
1135 .d_delete
= pid_delete_dentry
,
1140 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1141 struct task_struct
*, const void *);
1144 * Fill a directory entry.
1146 * If possible create the dcache entry and derive our inode number and
1147 * file type from dcache entry.
1149 * Since all of the proc inode numbers are dynamically generated, the inode
1150 * numbers do not exist until the inode is cache. This means creating the
1151 * the dcache entry in readdir is necessary to keep the inode numbers
1152 * reported by readdir in sync with the inode numbers reported
1155 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1156 char *name
, int len
,
1157 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1159 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1160 struct inode
*inode
;
1163 unsigned type
= DT_UNKNOWN
;
1167 qname
.hash
= full_name_hash(name
, len
);
1169 child
= d_lookup(dir
, &qname
);
1172 new = d_alloc(dir
, &qname
);
1174 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1181 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1182 goto end_instantiate
;
1183 inode
= child
->d_inode
;
1186 type
= inode
->i_mode
>> 12;
1191 ino
= find_inode_number(dir
, &qname
);
1194 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1197 static unsigned name_to_int(struct dentry
*dentry
)
1199 const char *name
= dentry
->d_name
.name
;
1200 int len
= dentry
->d_name
.len
;
1203 if (len
> 1 && *name
== '0')
1206 unsigned c
= *name
++ - '0';
1209 if (n
>= (~0U-9)/10)
1219 #define PROC_FDINFO_MAX 64
1221 static int proc_fd_info(struct inode
*inode
, struct dentry
**dentry
,
1222 struct vfsmount
**mnt
, char *info
)
1224 struct task_struct
*task
= get_proc_task(inode
);
1225 struct files_struct
*files
= NULL
;
1227 int fd
= proc_fd(inode
);
1230 files
= get_files_struct(task
);
1231 put_task_struct(task
);
1235 * We are not taking a ref to the file structure, so we must
1238 spin_lock(&files
->file_lock
);
1239 file
= fcheck_files(files
, fd
);
1241 unsigned int f_flags
;
1242 struct fdtable
*fdt
;
1244 fdt
= files_fdtable(files
);
1245 f_flags
= file
->f_flags
& ~O_CLOEXEC
;
1246 if (FD_ISSET(fd
, fdt
->close_on_exec
))
1247 f_flags
|= O_CLOEXEC
;
1250 *mnt
= mntget(file
->f_path
.mnt
);
1252 *dentry
= dget(file
->f_path
.dentry
);
1254 snprintf(info
, PROC_FDINFO_MAX
,
1257 (long long) file
->f_pos
,
1259 spin_unlock(&files
->file_lock
);
1260 put_files_struct(files
);
1263 spin_unlock(&files
->file_lock
);
1264 put_files_struct(files
);
1269 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
,
1270 struct vfsmount
**mnt
)
1272 return proc_fd_info(inode
, dentry
, mnt
, NULL
);
1275 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1277 struct inode
*inode
= dentry
->d_inode
;
1278 struct task_struct
*task
= get_proc_task(inode
);
1279 int fd
= proc_fd(inode
);
1280 struct files_struct
*files
;
1283 files
= get_files_struct(task
);
1286 if (fcheck_files(files
, fd
)) {
1288 put_files_struct(files
);
1289 if (task_dumpable(task
)) {
1290 inode
->i_uid
= task
->euid
;
1291 inode
->i_gid
= task
->egid
;
1296 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1297 security_task_to_inode(task
, inode
);
1298 put_task_struct(task
);
1302 put_files_struct(files
);
1304 put_task_struct(task
);
1310 static struct dentry_operations tid_fd_dentry_operations
=
1312 .d_revalidate
= tid_fd_revalidate
,
1313 .d_delete
= pid_delete_dentry
,
1316 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1317 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1319 unsigned fd
= *(const unsigned *)ptr
;
1321 struct files_struct
*files
;
1322 struct inode
*inode
;
1323 struct proc_inode
*ei
;
1324 struct dentry
*error
= ERR_PTR(-ENOENT
);
1326 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1331 files
= get_files_struct(task
);
1334 inode
->i_mode
= S_IFLNK
;
1337 * We are not taking a ref to the file structure, so we must
1340 spin_lock(&files
->file_lock
);
1341 file
= fcheck_files(files
, fd
);
1344 if (file
->f_mode
& 1)
1345 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1346 if (file
->f_mode
& 2)
1347 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1348 spin_unlock(&files
->file_lock
);
1349 put_files_struct(files
);
1351 inode
->i_op
= &proc_pid_link_inode_operations
;
1353 ei
->op
.proc_get_link
= proc_fd_link
;
1354 dentry
->d_op
= &tid_fd_dentry_operations
;
1355 d_add(dentry
, inode
);
1356 /* Close the race of the process dying before we return the dentry */
1357 if (tid_fd_revalidate(dentry
, NULL
))
1363 spin_unlock(&files
->file_lock
);
1364 put_files_struct(files
);
1370 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1371 struct dentry
*dentry
,
1372 instantiate_t instantiate
)
1374 struct task_struct
*task
= get_proc_task(dir
);
1375 unsigned fd
= name_to_int(dentry
);
1376 struct dentry
*result
= ERR_PTR(-ENOENT
);
1383 result
= instantiate(dir
, dentry
, task
, &fd
);
1385 put_task_struct(task
);
1390 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1391 filldir_t filldir
, instantiate_t instantiate
)
1393 struct dentry
*dentry
= filp
->f_path
.dentry
;
1394 struct inode
*inode
= dentry
->d_inode
;
1395 struct task_struct
*p
= get_proc_task(inode
);
1396 unsigned int fd
, tid
, ino
;
1398 struct files_struct
* files
;
1399 struct fdtable
*fdt
;
1410 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1414 ino
= parent_ino(dentry
);
1415 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1419 files
= get_files_struct(p
);
1423 fdt
= files_fdtable(files
);
1424 for (fd
= filp
->f_pos
-2;
1426 fd
++, filp
->f_pos
++) {
1427 char name
[PROC_NUMBUF
];
1430 if (!fcheck_files(files
, fd
))
1434 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1435 if (proc_fill_cache(filp
, dirent
, filldir
,
1436 name
, len
, instantiate
,
1444 put_files_struct(files
);
1452 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1453 struct nameidata
*nd
)
1455 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1458 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1460 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1463 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1464 size_t len
, loff_t
*ppos
)
1466 char tmp
[PROC_FDINFO_MAX
];
1467 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, NULL
, tmp
);
1469 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1473 static const struct file_operations proc_fdinfo_file_operations
= {
1474 .open
= nonseekable_open
,
1475 .read
= proc_fdinfo_read
,
1478 static const struct file_operations proc_fd_operations
= {
1479 .read
= generic_read_dir
,
1480 .readdir
= proc_readfd
,
1484 * /proc/pid/fd needs a special permission handler so that a process can still
1485 * access /proc/self/fd after it has executed a setuid().
1487 static int proc_fd_permission(struct inode
*inode
, int mask
,
1488 struct nameidata
*nd
)
1492 rv
= generic_permission(inode
, mask
, NULL
);
1495 if (task_pid(current
) == proc_pid(inode
))
1501 * proc directories can do almost nothing..
1503 static const struct inode_operations proc_fd_inode_operations
= {
1504 .lookup
= proc_lookupfd
,
1505 .permission
= proc_fd_permission
,
1506 .setattr
= proc_setattr
,
1509 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1510 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1512 unsigned fd
= *(unsigned *)ptr
;
1513 struct inode
*inode
;
1514 struct proc_inode
*ei
;
1515 struct dentry
*error
= ERR_PTR(-ENOENT
);
1517 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1522 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1523 inode
->i_fop
= &proc_fdinfo_file_operations
;
1524 dentry
->d_op
= &tid_fd_dentry_operations
;
1525 d_add(dentry
, inode
);
1526 /* Close the race of the process dying before we return the dentry */
1527 if (tid_fd_revalidate(dentry
, NULL
))
1534 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1535 struct dentry
*dentry
,
1536 struct nameidata
*nd
)
1538 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1541 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1543 return proc_readfd_common(filp
, dirent
, filldir
,
1544 proc_fdinfo_instantiate
);
1547 static const struct file_operations proc_fdinfo_operations
= {
1548 .read
= generic_read_dir
,
1549 .readdir
= proc_readfdinfo
,
1553 * proc directories can do almost nothing..
1555 static const struct inode_operations proc_fdinfo_inode_operations
= {
1556 .lookup
= proc_lookupfdinfo
,
1557 .setattr
= proc_setattr
,
1561 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1562 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1564 const struct pid_entry
*p
= ptr
;
1565 struct inode
*inode
;
1566 struct proc_inode
*ei
;
1567 struct dentry
*error
= ERR_PTR(-EINVAL
);
1569 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1574 inode
->i_mode
= p
->mode
;
1575 if (S_ISDIR(inode
->i_mode
))
1576 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1578 inode
->i_op
= p
->iop
;
1580 inode
->i_fop
= p
->fop
;
1582 dentry
->d_op
= &pid_dentry_operations
;
1583 d_add(dentry
, inode
);
1584 /* Close the race of the process dying before we return the dentry */
1585 if (pid_revalidate(dentry
, NULL
))
1591 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1592 struct dentry
*dentry
,
1593 const struct pid_entry
*ents
,
1596 struct inode
*inode
;
1597 struct dentry
*error
;
1598 struct task_struct
*task
= get_proc_task(dir
);
1599 const struct pid_entry
*p
, *last
;
1601 error
= ERR_PTR(-ENOENT
);
1608 * Yes, it does not scale. And it should not. Don't add
1609 * new entries into /proc/<tgid>/ without very good reasons.
1611 last
= &ents
[nents
- 1];
1612 for (p
= ents
; p
<= last
; p
++) {
1613 if (p
->len
!= dentry
->d_name
.len
)
1615 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1621 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1623 put_task_struct(task
);
1628 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1629 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1631 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1632 proc_pident_instantiate
, task
, p
);
1635 static int proc_pident_readdir(struct file
*filp
,
1636 void *dirent
, filldir_t filldir
,
1637 const struct pid_entry
*ents
, unsigned int nents
)
1641 struct dentry
*dentry
= filp
->f_path
.dentry
;
1642 struct inode
*inode
= dentry
->d_inode
;
1643 struct task_struct
*task
= get_proc_task(inode
);
1644 const struct pid_entry
*p
, *last
;
1658 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1664 ino
= parent_ino(dentry
);
1665 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1677 last
= &ents
[nents
- 1];
1679 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
1688 put_task_struct(task
);
1693 #ifdef CONFIG_SECURITY
1694 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1695 size_t count
, loff_t
*ppos
)
1697 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1700 struct task_struct
*task
= get_proc_task(inode
);
1705 length
= security_getprocattr(task
,
1706 (char*)file
->f_path
.dentry
->d_name
.name
,
1708 put_task_struct(task
);
1710 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
1715 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1716 size_t count
, loff_t
*ppos
)
1718 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1721 struct task_struct
*task
= get_proc_task(inode
);
1726 if (count
> PAGE_SIZE
)
1729 /* No partial writes. */
1735 page
= (char*)__get_free_page(GFP_USER
);
1740 if (copy_from_user(page
, buf
, count
))
1743 length
= security_setprocattr(task
,
1744 (char*)file
->f_path
.dentry
->d_name
.name
,
1745 (void*)page
, count
);
1747 free_page((unsigned long) page
);
1749 put_task_struct(task
);
1754 static const struct file_operations proc_pid_attr_operations
= {
1755 .read
= proc_pid_attr_read
,
1756 .write
= proc_pid_attr_write
,
1759 static const struct pid_entry attr_dir_stuff
[] = {
1760 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
1761 REG("prev", S_IRUGO
, pid_attr
),
1762 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
1763 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1764 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1765 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1768 static int proc_attr_dir_readdir(struct file
* filp
,
1769 void * dirent
, filldir_t filldir
)
1771 return proc_pident_readdir(filp
,dirent
,filldir
,
1772 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
1775 static const struct file_operations proc_attr_dir_operations
= {
1776 .read
= generic_read_dir
,
1777 .readdir
= proc_attr_dir_readdir
,
1780 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
1781 struct dentry
*dentry
, struct nameidata
*nd
)
1783 return proc_pident_lookup(dir
, dentry
,
1784 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
1787 static const struct inode_operations proc_attr_dir_inode_operations
= {
1788 .lookup
= proc_attr_dir_lookup
,
1789 .getattr
= pid_getattr
,
1790 .setattr
= proc_setattr
,
1798 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1801 char tmp
[PROC_NUMBUF
];
1802 sprintf(tmp
, "%d", current
->tgid
);
1803 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1806 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1808 char tmp
[PROC_NUMBUF
];
1809 sprintf(tmp
, "%d", current
->tgid
);
1810 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1813 static const struct inode_operations proc_self_inode_operations
= {
1814 .readlink
= proc_self_readlink
,
1815 .follow_link
= proc_self_follow_link
,
1821 * These are the directory entries in the root directory of /proc
1822 * that properly belong to the /proc filesystem, as they describe
1823 * describe something that is process related.
1825 static const struct pid_entry proc_base_stuff
[] = {
1826 NOD("self", S_IFLNK
|S_IRWXUGO
,
1827 &proc_self_inode_operations
, NULL
, {}),
1831 * Exceptional case: normally we are not allowed to unhash a busy
1832 * directory. In this case, however, we can do it - no aliasing problems
1833 * due to the way we treat inodes.
1835 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1837 struct inode
*inode
= dentry
->d_inode
;
1838 struct task_struct
*task
= get_proc_task(inode
);
1840 put_task_struct(task
);
1847 static struct dentry_operations proc_base_dentry_operations
=
1849 .d_revalidate
= proc_base_revalidate
,
1850 .d_delete
= pid_delete_dentry
,
1853 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
1854 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1856 const struct pid_entry
*p
= ptr
;
1857 struct inode
*inode
;
1858 struct proc_inode
*ei
;
1859 struct dentry
*error
= ERR_PTR(-EINVAL
);
1861 /* Allocate the inode */
1862 error
= ERR_PTR(-ENOMEM
);
1863 inode
= new_inode(dir
->i_sb
);
1867 /* Initialize the inode */
1869 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1872 * grab the reference to the task.
1874 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1880 inode
->i_mode
= p
->mode
;
1881 if (S_ISDIR(inode
->i_mode
))
1883 if (S_ISLNK(inode
->i_mode
))
1886 inode
->i_op
= p
->iop
;
1888 inode
->i_fop
= p
->fop
;
1890 dentry
->d_op
= &proc_base_dentry_operations
;
1891 d_add(dentry
, inode
);
1900 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
1902 struct dentry
*error
;
1903 struct task_struct
*task
= get_proc_task(dir
);
1904 const struct pid_entry
*p
, *last
;
1906 error
= ERR_PTR(-ENOENT
);
1911 /* Lookup the directory entry */
1912 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
1913 for (p
= proc_base_stuff
; p
<= last
; p
++) {
1914 if (p
->len
!= dentry
->d_name
.len
)
1916 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1922 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
1925 put_task_struct(task
);
1930 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
1931 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1933 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1934 proc_base_instantiate
, task
, p
);
1937 #ifdef CONFIG_TASK_IO_ACCOUNTING
1938 static int proc_pid_io_accounting(struct task_struct
*task
, char *buffer
)
1940 return sprintf(buffer
,
1941 #ifdef CONFIG_TASK_XACCT
1947 "read_bytes: %llu\n"
1948 "write_bytes: %llu\n"
1949 "cancelled_write_bytes: %llu\n",
1950 #ifdef CONFIG_TASK_XACCT
1951 (unsigned long long)task
->rchar
,
1952 (unsigned long long)task
->wchar
,
1953 (unsigned long long)task
->syscr
,
1954 (unsigned long long)task
->syscw
,
1956 (unsigned long long)task
->ioac
.read_bytes
,
1957 (unsigned long long)task
->ioac
.write_bytes
,
1958 (unsigned long long)task
->ioac
.cancelled_write_bytes
);
1965 static const struct file_operations proc_task_operations
;
1966 static const struct inode_operations proc_task_inode_operations
;
1968 static const struct pid_entry tgid_base_stuff
[] = {
1969 DIR("task", S_IRUGO
|S_IXUGO
, task
),
1970 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
1971 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
1972 INF("environ", S_IRUSR
, pid_environ
),
1973 INF("auxv", S_IRUSR
, pid_auxv
),
1974 INF("status", S_IRUGO
, pid_status
),
1975 INF("cmdline", S_IRUGO
, pid_cmdline
),
1976 INF("stat", S_IRUGO
, tgid_stat
),
1977 INF("statm", S_IRUGO
, pid_statm
),
1978 REG("maps", S_IRUGO
, maps
),
1980 REG("numa_maps", S_IRUGO
, numa_maps
),
1982 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
1983 #ifdef CONFIG_SECCOMP
1984 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
1989 REG("mounts", S_IRUGO
, mounts
),
1990 REG("mountstats", S_IRUSR
, mountstats
),
1992 REG("clear_refs", S_IWUSR
, clear_refs
),
1993 REG("smaps", S_IRUGO
, smaps
),
1995 #ifdef CONFIG_SECURITY
1996 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
1998 #ifdef CONFIG_KALLSYMS
1999 INF("wchan", S_IRUGO
, pid_wchan
),
2001 #ifdef CONFIG_SCHEDSTATS
2002 INF("schedstat", S_IRUGO
, pid_schedstat
),
2004 #ifdef CONFIG_CPUSETS
2005 REG("cpuset", S_IRUGO
, cpuset
),
2007 INF("oom_score", S_IRUGO
, oom_score
),
2008 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2009 #ifdef CONFIG_AUDITSYSCALL
2010 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2012 #ifdef CONFIG_FAULT_INJECTION
2013 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2015 #ifdef CONFIG_TASK_IO_ACCOUNTING
2016 INF("io", S_IRUGO
, pid_io_accounting
),
2020 static int proc_tgid_base_readdir(struct file
* filp
,
2021 void * dirent
, filldir_t filldir
)
2023 return proc_pident_readdir(filp
,dirent
,filldir
,
2024 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2027 static const struct file_operations proc_tgid_base_operations
= {
2028 .read
= generic_read_dir
,
2029 .readdir
= proc_tgid_base_readdir
,
2032 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2033 return proc_pident_lookup(dir
, dentry
,
2034 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2037 static const struct inode_operations proc_tgid_base_inode_operations
= {
2038 .lookup
= proc_tgid_base_lookup
,
2039 .getattr
= pid_getattr
,
2040 .setattr
= proc_setattr
,
2044 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2046 * @task: task that should be flushed.
2048 * Looks in the dcache for
2050 * /proc/@tgid/task/@pid
2051 * if either directory is present flushes it and all of it'ts children
2054 * It is safe and reasonable to cache /proc entries for a task until
2055 * that task exits. After that they just clog up the dcache with
2056 * useless entries, possibly causing useful dcache entries to be
2057 * flushed instead. This routine is proved to flush those useless
2058 * dcache entries at process exit time.
2060 * NOTE: This routine is just an optimization so it does not guarantee
2061 * that no dcache entries will exist at process exit time it
2062 * just makes it very unlikely that any will persist.
2064 void proc_flush_task(struct task_struct
*task
)
2066 struct dentry
*dentry
, *leader
, *dir
;
2067 char buf
[PROC_NUMBUF
];
2071 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2072 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2074 shrink_dcache_parent(dentry
);
2079 if (thread_group_leader(task
))
2083 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
2084 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2089 name
.len
= strlen(name
.name
);
2090 dir
= d_hash_and_lookup(leader
, &name
);
2092 goto out_put_leader
;
2095 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2096 dentry
= d_hash_and_lookup(dir
, &name
);
2098 shrink_dcache_parent(dentry
);
2110 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2111 struct dentry
* dentry
,
2112 struct task_struct
*task
, const void *ptr
)
2114 struct dentry
*error
= ERR_PTR(-ENOENT
);
2115 struct inode
*inode
;
2117 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2121 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2122 inode
->i_op
= &proc_tgid_base_inode_operations
;
2123 inode
->i_fop
= &proc_tgid_base_operations
;
2124 inode
->i_flags
|=S_IMMUTABLE
;
2126 #ifdef CONFIG_SECURITY
2127 inode
->i_nlink
+= 1;
2130 dentry
->d_op
= &pid_dentry_operations
;
2132 d_add(dentry
, inode
);
2133 /* Close the race of the process dying before we return the dentry */
2134 if (pid_revalidate(dentry
, NULL
))
2140 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2142 struct dentry
*result
= ERR_PTR(-ENOENT
);
2143 struct task_struct
*task
;
2146 result
= proc_base_lookup(dir
, dentry
);
2147 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2150 tgid
= name_to_int(dentry
);
2155 task
= find_task_by_pid(tgid
);
2157 get_task_struct(task
);
2162 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2163 put_task_struct(task
);
2169 * Find the first task with tgid >= tgid
2172 static struct task_struct
*next_tgid(unsigned int tgid
)
2174 struct task_struct
*task
;
2180 pid
= find_ge_pid(tgid
);
2183 task
= pid_task(pid
, PIDTYPE_PID
);
2184 /* What we to know is if the pid we have find is the
2185 * pid of a thread_group_leader. Testing for task
2186 * being a thread_group_leader is the obvious thing
2187 * todo but there is a window when it fails, due to
2188 * the pid transfer logic in de_thread.
2190 * So we perform the straight forward test of seeing
2191 * if the pid we have found is the pid of a thread
2192 * group leader, and don't worry if the task we have
2193 * found doesn't happen to be a thread group leader.
2194 * As we don't care in the case of readdir.
2196 if (!task
|| !has_group_leader_pid(task
))
2198 get_task_struct(task
);
2204 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2206 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2207 struct task_struct
*task
, int tgid
)
2209 char name
[PROC_NUMBUF
];
2210 int len
= snprintf(name
, sizeof(name
), "%d", tgid
);
2211 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2212 proc_pid_instantiate
, task
, NULL
);
2215 /* for the /proc/ directory itself, after non-process stuff has been done */
2216 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2218 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2219 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2220 struct task_struct
*task
;
2226 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2227 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2228 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2232 tgid
= filp
->f_pos
- TGID_OFFSET
;
2233 for (task
= next_tgid(tgid
);
2235 put_task_struct(task
), task
= next_tgid(tgid
+ 1)) {
2237 filp
->f_pos
= tgid
+ TGID_OFFSET
;
2238 if (proc_pid_fill_cache(filp
, dirent
, filldir
, task
, tgid
) < 0) {
2239 put_task_struct(task
);
2243 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2245 put_task_struct(reaper
);
2253 static const struct pid_entry tid_base_stuff
[] = {
2254 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2255 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2256 INF("environ", S_IRUSR
, pid_environ
),
2257 INF("auxv", S_IRUSR
, pid_auxv
),
2258 INF("status", S_IRUGO
, pid_status
),
2259 INF("cmdline", S_IRUGO
, pid_cmdline
),
2260 INF("stat", S_IRUGO
, tid_stat
),
2261 INF("statm", S_IRUGO
, pid_statm
),
2262 REG("maps", S_IRUGO
, maps
),
2264 REG("numa_maps", S_IRUGO
, numa_maps
),
2266 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2267 #ifdef CONFIG_SECCOMP
2268 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
2273 REG("mounts", S_IRUGO
, mounts
),
2275 REG("clear_refs", S_IWUSR
, clear_refs
),
2276 REG("smaps", S_IRUGO
, smaps
),
2278 #ifdef CONFIG_SECURITY
2279 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2281 #ifdef CONFIG_KALLSYMS
2282 INF("wchan", S_IRUGO
, pid_wchan
),
2284 #ifdef CONFIG_SCHEDSTATS
2285 INF("schedstat", S_IRUGO
, pid_schedstat
),
2287 #ifdef CONFIG_CPUSETS
2288 REG("cpuset", S_IRUGO
, cpuset
),
2290 INF("oom_score", S_IRUGO
, oom_score
),
2291 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2292 #ifdef CONFIG_AUDITSYSCALL
2293 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2295 #ifdef CONFIG_FAULT_INJECTION
2296 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2300 static int proc_tid_base_readdir(struct file
* filp
,
2301 void * dirent
, filldir_t filldir
)
2303 return proc_pident_readdir(filp
,dirent
,filldir
,
2304 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2307 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2308 return proc_pident_lookup(dir
, dentry
,
2309 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2312 static const struct file_operations proc_tid_base_operations
= {
2313 .read
= generic_read_dir
,
2314 .readdir
= proc_tid_base_readdir
,
2317 static const struct inode_operations proc_tid_base_inode_operations
= {
2318 .lookup
= proc_tid_base_lookup
,
2319 .getattr
= pid_getattr
,
2320 .setattr
= proc_setattr
,
2323 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2324 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2326 struct dentry
*error
= ERR_PTR(-ENOENT
);
2327 struct inode
*inode
;
2328 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2332 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2333 inode
->i_op
= &proc_tid_base_inode_operations
;
2334 inode
->i_fop
= &proc_tid_base_operations
;
2335 inode
->i_flags
|=S_IMMUTABLE
;
2337 #ifdef CONFIG_SECURITY
2338 inode
->i_nlink
+= 1;
2341 dentry
->d_op
= &pid_dentry_operations
;
2343 d_add(dentry
, inode
);
2344 /* Close the race of the process dying before we return the dentry */
2345 if (pid_revalidate(dentry
, NULL
))
2351 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2353 struct dentry
*result
= ERR_PTR(-ENOENT
);
2354 struct task_struct
*task
;
2355 struct task_struct
*leader
= get_proc_task(dir
);
2361 tid
= name_to_int(dentry
);
2366 task
= find_task_by_pid(tid
);
2368 get_task_struct(task
);
2372 if (leader
->tgid
!= task
->tgid
)
2375 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2377 put_task_struct(task
);
2379 put_task_struct(leader
);
2385 * Find the first tid of a thread group to return to user space.
2387 * Usually this is just the thread group leader, but if the users
2388 * buffer was too small or there was a seek into the middle of the
2389 * directory we have more work todo.
2391 * In the case of a short read we start with find_task_by_pid.
2393 * In the case of a seek we start with the leader and walk nr
2396 static struct task_struct
*first_tid(struct task_struct
*leader
,
2399 struct task_struct
*pos
;
2402 /* Attempt to start with the pid of a thread */
2403 if (tid
&& (nr
> 0)) {
2404 pos
= find_task_by_pid(tid
);
2405 if (pos
&& (pos
->group_leader
== leader
))
2409 /* If nr exceeds the number of threads there is nothing todo */
2411 if (nr
&& nr
>= get_nr_threads(leader
))
2414 /* If we haven't found our starting place yet start
2415 * with the leader and walk nr threads forward.
2417 for (pos
= leader
; nr
> 0; --nr
) {
2418 pos
= next_thread(pos
);
2419 if (pos
== leader
) {
2425 get_task_struct(pos
);
2432 * Find the next thread in the thread list.
2433 * Return NULL if there is an error or no next thread.
2435 * The reference to the input task_struct is released.
2437 static struct task_struct
*next_tid(struct task_struct
*start
)
2439 struct task_struct
*pos
= NULL
;
2441 if (pid_alive(start
)) {
2442 pos
= next_thread(start
);
2443 if (thread_group_leader(pos
))
2446 get_task_struct(pos
);
2449 put_task_struct(start
);
2453 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2454 struct task_struct
*task
, int tid
)
2456 char name
[PROC_NUMBUF
];
2457 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2458 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2459 proc_task_instantiate
, task
, NULL
);
2462 /* for the /proc/TGID/task/ directories */
2463 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2465 struct dentry
*dentry
= filp
->f_path
.dentry
;
2466 struct inode
*inode
= dentry
->d_inode
;
2467 struct task_struct
*leader
= NULL
;
2468 struct task_struct
*task
;
2469 int retval
= -ENOENT
;
2472 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2474 task
= get_proc_task(inode
);
2478 if (pid_alive(task
)) {
2479 leader
= task
->group_leader
;
2480 get_task_struct(leader
);
2483 put_task_struct(task
);
2491 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2496 ino
= parent_ino(dentry
);
2497 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2503 /* f_version caches the tgid value that the last readdir call couldn't
2504 * return. lseek aka telldir automagically resets f_version to 0.
2506 tid
= filp
->f_version
;
2507 filp
->f_version
= 0;
2508 for (task
= first_tid(leader
, tid
, pos
- 2);
2510 task
= next_tid(task
), pos
++) {
2512 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
2513 /* returning this tgid failed, save it as the first
2514 * pid for the next readir call */
2515 filp
->f_version
= tid
;
2516 put_task_struct(task
);
2522 put_task_struct(leader
);
2527 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2529 struct inode
*inode
= dentry
->d_inode
;
2530 struct task_struct
*p
= get_proc_task(inode
);
2531 generic_fillattr(inode
, stat
);
2535 stat
->nlink
+= get_nr_threads(p
);
2543 static const struct inode_operations proc_task_inode_operations
= {
2544 .lookup
= proc_task_lookup
,
2545 .getattr
= proc_task_getattr
,
2546 .setattr
= proc_setattr
,
2549 static const struct file_operations proc_task_operations
= {
2550 .read
= generic_read_dir
,
2551 .readdir
= proc_task_readdir
,