4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * proc base directory handling functions
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
50 #include <asm/uaccess.h>
52 #include <linux/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/init.h>
57 #include <linux/capability.h>
58 #include <linux/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/mnt_namespace.h>
64 #include <linux/rcupdate.h>
65 #include <linux/kallsyms.h>
66 #include <linux/module.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/cpuset.h>
71 #include <linux/audit.h>
72 #include <linux/poll.h>
73 #include <linux/nsproxy.h>
74 #include <linux/oom.h>
75 #include <linux/elf.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
);
210 if (!ptrace_may_attach(task
))
213 len
= mm
->env_end
- mm
->env_start
;
216 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
223 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
227 struct mm_struct
*mm
= get_task_mm(task
);
231 goto out_mm
; /* Shh! No looking before we're done */
233 len
= mm
->arg_end
- mm
->arg_start
;
238 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
240 // If the nul at the end of args has been overwritten, then
241 // assume application is using setproctitle(3).
242 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
243 len
= strnlen(buffer
, res
);
247 len
= mm
->env_end
- mm
->env_start
;
248 if (len
> PAGE_SIZE
- res
)
249 len
= PAGE_SIZE
- res
;
250 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
251 res
= strnlen(buffer
, res
);
260 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
263 struct mm_struct
*mm
= get_task_mm(task
);
265 unsigned int nwords
= 0;
268 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
269 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
272 memcpy(buffer
, mm
->saved_auxv
, res
);
279 #ifdef CONFIG_KALLSYMS
281 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
282 * Returns the resolved symbol. If that fails, simply return the address.
284 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
287 char symname
[KSYM_NAME_LEN
];
289 wchan
= get_wchan(task
);
291 if (lookup_symbol_name(wchan
, symname
) < 0)
292 return sprintf(buffer
, "%lu", wchan
);
294 return sprintf(buffer
, "%s", symname
);
296 #endif /* CONFIG_KALLSYMS */
298 #ifdef CONFIG_SCHEDSTATS
300 * Provides /proc/PID/schedstat
302 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
304 return sprintf(buffer
, "%llu %llu %lu\n",
305 task
->sched_info
.cpu_time
,
306 task
->sched_info
.run_delay
,
307 task
->sched_info
.pcnt
);
311 /* The badness from the OOM killer */
312 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
313 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
315 unsigned long points
;
316 struct timespec uptime
;
318 do_posix_clock_monotonic_gettime(&uptime
);
319 read_lock(&tasklist_lock
);
320 points
= badness(task
, uptime
.tv_sec
);
321 read_unlock(&tasklist_lock
);
322 return sprintf(buffer
, "%lu\n", points
);
325 /************************************************************************/
326 /* Here the fs part begins */
327 /************************************************************************/
329 /* permission checks */
330 static int proc_fd_access_allowed(struct inode
*inode
)
332 struct task_struct
*task
;
334 /* Allow access to a task's file descriptors if it is us or we
335 * may use ptrace attach to the process and find out that
338 task
= get_proc_task(inode
);
340 allowed
= ptrace_may_attach(task
);
341 put_task_struct(task
);
346 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
349 struct inode
*inode
= dentry
->d_inode
;
351 if (attr
->ia_valid
& ATTR_MODE
)
354 error
= inode_change_ok(inode
, attr
);
356 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
];
671 oom_adjust
= task
->oomkilladj
;
672 put_task_struct(task
);
674 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
676 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
679 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
680 size_t count
, loff_t
*ppos
)
682 struct task_struct
*task
;
683 char buffer
[PROC_NUMBUF
], *end
;
686 memset(buffer
, 0, sizeof(buffer
));
687 if (count
> sizeof(buffer
) - 1)
688 count
= sizeof(buffer
) - 1;
689 if (copy_from_user(buffer
, buf
, count
))
691 oom_adjust
= simple_strtol(buffer
, &end
, 0);
692 if ((oom_adjust
< OOM_ADJUST_MIN
|| oom_adjust
> OOM_ADJUST_MAX
) &&
693 oom_adjust
!= OOM_DISABLE
)
697 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
700 if (oom_adjust
< task
->oomkilladj
&& !capable(CAP_SYS_RESOURCE
)) {
701 put_task_struct(task
);
704 task
->oomkilladj
= oom_adjust
;
705 put_task_struct(task
);
706 if (end
- buffer
== 0)
711 static const struct file_operations proc_oom_adjust_operations
= {
712 .read
= oom_adjust_read
,
713 .write
= oom_adjust_write
,
717 static ssize_t
clear_refs_write(struct file
*file
, const char __user
*buf
,
718 size_t count
, loff_t
*ppos
)
720 struct task_struct
*task
;
721 char buffer
[PROC_NUMBUF
], *end
;
722 struct mm_struct
*mm
;
724 memset(buffer
, 0, sizeof(buffer
));
725 if (count
> sizeof(buffer
) - 1)
726 count
= sizeof(buffer
) - 1;
727 if (copy_from_user(buffer
, buf
, count
))
729 if (!simple_strtol(buffer
, &end
, 0))
733 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
736 mm
= get_task_mm(task
);
741 put_task_struct(task
);
742 if (end
- buffer
== 0)
747 static struct file_operations proc_clear_refs_operations
= {
748 .write
= clear_refs_write
,
752 #ifdef CONFIG_AUDITSYSCALL
754 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
755 size_t count
, loff_t
*ppos
)
757 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
758 struct task_struct
*task
= get_proc_task(inode
);
760 char tmpbuf
[TMPBUFLEN
];
764 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
765 audit_get_loginuid(task
->audit_context
));
766 put_task_struct(task
);
767 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
770 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
771 size_t count
, loff_t
*ppos
)
773 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
778 if (!capable(CAP_AUDIT_CONTROL
))
781 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
784 if (count
>= PAGE_SIZE
)
785 count
= PAGE_SIZE
- 1;
788 /* No partial writes. */
791 page
= (char*)__get_free_page(GFP_USER
);
795 if (copy_from_user(page
, buf
, count
))
799 loginuid
= simple_strtoul(page
, &tmp
, 10);
805 length
= audit_set_loginuid(current
, loginuid
);
806 if (likely(length
== 0))
810 free_page((unsigned long) page
);
814 static const struct file_operations proc_loginuid_operations
= {
815 .read
= proc_loginuid_read
,
816 .write
= proc_loginuid_write
,
820 #ifdef CONFIG_FAULT_INJECTION
821 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
822 size_t count
, loff_t
*ppos
)
824 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
825 char buffer
[PROC_NUMBUF
];
831 make_it_fail
= task
->make_it_fail
;
832 put_task_struct(task
);
834 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
836 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
839 static ssize_t
proc_fault_inject_write(struct file
* file
,
840 const char __user
* buf
, size_t count
, loff_t
*ppos
)
842 struct task_struct
*task
;
843 char buffer
[PROC_NUMBUF
], *end
;
846 if (!capable(CAP_SYS_RESOURCE
))
848 memset(buffer
, 0, sizeof(buffer
));
849 if (count
> sizeof(buffer
) - 1)
850 count
= sizeof(buffer
) - 1;
851 if (copy_from_user(buffer
, buf
, count
))
853 make_it_fail
= simple_strtol(buffer
, &end
, 0);
856 task
= get_proc_task(file
->f_dentry
->d_inode
);
859 task
->make_it_fail
= make_it_fail
;
860 put_task_struct(task
);
861 if (end
- buffer
== 0)
866 static const struct file_operations proc_fault_inject_operations
= {
867 .read
= proc_fault_inject_read
,
868 .write
= proc_fault_inject_write
,
872 #ifdef CONFIG_SCHED_DEBUG
874 * Print out various scheduling related per-task fields:
876 static int sched_show(struct seq_file
*m
, void *v
)
878 struct inode
*inode
= m
->private;
879 struct task_struct
*p
;
883 p
= get_proc_task(inode
);
886 proc_sched_show_task(p
, m
);
894 sched_write(struct file
*file
, const char __user
*buf
,
895 size_t count
, loff_t
*offset
)
897 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
898 struct task_struct
*p
;
902 p
= get_proc_task(inode
);
905 proc_sched_set_task(p
);
912 static int sched_open(struct inode
*inode
, struct file
*filp
)
916 ret
= single_open(filp
, sched_show
, NULL
);
918 struct seq_file
*m
= filp
->private_data
;
925 static const struct file_operations proc_pid_sched_operations
= {
928 .write
= sched_write
,
930 .release
= seq_release
,
935 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
937 struct inode
*inode
= dentry
->d_inode
;
940 /* We don't need a base pointer in the /proc filesystem */
943 /* Are we allowed to snoop on the tasks file descriptors? */
944 if (!proc_fd_access_allowed(inode
))
947 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
948 nd
->last_type
= LAST_BIND
;
950 return ERR_PTR(error
);
953 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
954 char __user
*buffer
, int buflen
)
956 struct inode
* inode
;
957 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
963 inode
= dentry
->d_inode
;
964 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
968 len
= tmp
+ PAGE_SIZE
- 1 - path
;
972 if (copy_to_user(buffer
, path
, len
))
975 free_page((unsigned long)tmp
);
979 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
982 struct inode
*inode
= dentry
->d_inode
;
984 struct vfsmount
*mnt
= NULL
;
986 /* Are we allowed to snoop on the tasks file descriptors? */
987 if (!proc_fd_access_allowed(inode
))
990 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
994 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1001 static const struct inode_operations proc_pid_link_inode_operations
= {
1002 .readlink
= proc_pid_readlink
,
1003 .follow_link
= proc_pid_follow_link
,
1004 .setattr
= proc_setattr
,
1008 /* building an inode */
1010 static int task_dumpable(struct task_struct
*task
)
1013 struct mm_struct
*mm
;
1018 dumpable
= get_dumpable(mm
);
1026 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1028 struct inode
* inode
;
1029 struct proc_inode
*ei
;
1031 /* We need a new inode */
1033 inode
= new_inode(sb
);
1039 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1040 inode
->i_op
= &proc_def_inode_operations
;
1043 * grab the reference to task.
1045 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1051 if (task_dumpable(task
)) {
1052 inode
->i_uid
= task
->euid
;
1053 inode
->i_gid
= task
->egid
;
1055 security_task_to_inode(task
, inode
);
1065 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1067 struct inode
*inode
= dentry
->d_inode
;
1068 struct task_struct
*task
;
1069 generic_fillattr(inode
, stat
);
1074 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1076 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1077 task_dumpable(task
)) {
1078 stat
->uid
= task
->euid
;
1079 stat
->gid
= task
->egid
;
1089 * Exceptional case: normally we are not allowed to unhash a busy
1090 * directory. In this case, however, we can do it - no aliasing problems
1091 * due to the way we treat inodes.
1093 * Rewrite the inode's ownerships here because the owning task may have
1094 * performed a setuid(), etc.
1096 * Before the /proc/pid/status file was created the only way to read
1097 * the effective uid of a /process was to stat /proc/pid. Reading
1098 * /proc/pid/status is slow enough that procps and other packages
1099 * kept stating /proc/pid. To keep the rules in /proc simple I have
1100 * made this apply to all per process world readable and executable
1103 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1105 struct inode
*inode
= dentry
->d_inode
;
1106 struct task_struct
*task
= get_proc_task(inode
);
1108 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1109 task_dumpable(task
)) {
1110 inode
->i_uid
= task
->euid
;
1111 inode
->i_gid
= task
->egid
;
1116 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1117 security_task_to_inode(task
, inode
);
1118 put_task_struct(task
);
1125 static int pid_delete_dentry(struct dentry
* dentry
)
1127 /* Is the task we represent dead?
1128 * If so, then don't put the dentry on the lru list,
1129 * kill it immediately.
1131 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1134 static struct dentry_operations pid_dentry_operations
=
1136 .d_revalidate
= pid_revalidate
,
1137 .d_delete
= pid_delete_dentry
,
1142 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*,
1143 struct task_struct
*, const void *);
1146 * Fill a directory entry.
1148 * If possible create the dcache entry and derive our inode number and
1149 * file type from dcache entry.
1151 * Since all of the proc inode numbers are dynamically generated, the inode
1152 * numbers do not exist until the inode is cache. This means creating the
1153 * the dcache entry in readdir is necessary to keep the inode numbers
1154 * reported by readdir in sync with the inode numbers reported
1157 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1158 char *name
, int len
,
1159 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1161 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1162 struct inode
*inode
;
1165 unsigned type
= DT_UNKNOWN
;
1169 qname
.hash
= full_name_hash(name
, len
);
1171 child
= d_lookup(dir
, &qname
);
1174 new = d_alloc(dir
, &qname
);
1176 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1183 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1184 goto end_instantiate
;
1185 inode
= child
->d_inode
;
1188 type
= inode
->i_mode
>> 12;
1193 ino
= find_inode_number(dir
, &qname
);
1196 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1199 static unsigned name_to_int(struct dentry
*dentry
)
1201 const char *name
= dentry
->d_name
.name
;
1202 int len
= dentry
->d_name
.len
;
1205 if (len
> 1 && *name
== '0')
1208 unsigned c
= *name
++ - '0';
1211 if (n
>= (~0U-9)/10)
1221 #define PROC_FDINFO_MAX 64
1223 static int proc_fd_info(struct inode
*inode
, struct dentry
**dentry
,
1224 struct vfsmount
**mnt
, char *info
)
1226 struct task_struct
*task
= get_proc_task(inode
);
1227 struct files_struct
*files
= NULL
;
1229 int fd
= proc_fd(inode
);
1232 files
= get_files_struct(task
);
1233 put_task_struct(task
);
1237 * We are not taking a ref to the file structure, so we must
1240 spin_lock(&files
->file_lock
);
1241 file
= fcheck_files(files
, fd
);
1244 *mnt
= mntget(file
->f_path
.mnt
);
1246 *dentry
= dget(file
->f_path
.dentry
);
1248 snprintf(info
, PROC_FDINFO_MAX
,
1251 (long long) file
->f_pos
,
1253 spin_unlock(&files
->file_lock
);
1254 put_files_struct(files
);
1257 spin_unlock(&files
->file_lock
);
1258 put_files_struct(files
);
1263 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
,
1264 struct vfsmount
**mnt
)
1266 return proc_fd_info(inode
, dentry
, mnt
, NULL
);
1269 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1271 struct inode
*inode
= dentry
->d_inode
;
1272 struct task_struct
*task
= get_proc_task(inode
);
1273 int fd
= proc_fd(inode
);
1274 struct files_struct
*files
;
1277 files
= get_files_struct(task
);
1280 if (fcheck_files(files
, fd
)) {
1282 put_files_struct(files
);
1283 if (task_dumpable(task
)) {
1284 inode
->i_uid
= task
->euid
;
1285 inode
->i_gid
= task
->egid
;
1290 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1291 security_task_to_inode(task
, inode
);
1292 put_task_struct(task
);
1296 put_files_struct(files
);
1298 put_task_struct(task
);
1304 static struct dentry_operations tid_fd_dentry_operations
=
1306 .d_revalidate
= tid_fd_revalidate
,
1307 .d_delete
= pid_delete_dentry
,
1310 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1311 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1313 unsigned fd
= *(const unsigned *)ptr
;
1315 struct files_struct
*files
;
1316 struct inode
*inode
;
1317 struct proc_inode
*ei
;
1318 struct dentry
*error
= ERR_PTR(-ENOENT
);
1320 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1325 files
= get_files_struct(task
);
1328 inode
->i_mode
= S_IFLNK
;
1331 * We are not taking a ref to the file structure, so we must
1334 spin_lock(&files
->file_lock
);
1335 file
= fcheck_files(files
, fd
);
1338 if (file
->f_mode
& 1)
1339 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1340 if (file
->f_mode
& 2)
1341 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1342 spin_unlock(&files
->file_lock
);
1343 put_files_struct(files
);
1345 inode
->i_op
= &proc_pid_link_inode_operations
;
1347 ei
->op
.proc_get_link
= proc_fd_link
;
1348 dentry
->d_op
= &tid_fd_dentry_operations
;
1349 d_add(dentry
, inode
);
1350 /* Close the race of the process dying before we return the dentry */
1351 if (tid_fd_revalidate(dentry
, NULL
))
1357 spin_unlock(&files
->file_lock
);
1358 put_files_struct(files
);
1364 static struct dentry
*proc_lookupfd_common(struct inode
*dir
,
1365 struct dentry
*dentry
,
1366 instantiate_t instantiate
)
1368 struct task_struct
*task
= get_proc_task(dir
);
1369 unsigned fd
= name_to_int(dentry
);
1370 struct dentry
*result
= ERR_PTR(-ENOENT
);
1377 result
= instantiate(dir
, dentry
, task
, &fd
);
1379 put_task_struct(task
);
1384 static int proc_readfd_common(struct file
* filp
, void * dirent
,
1385 filldir_t filldir
, instantiate_t instantiate
)
1387 struct dentry
*dentry
= filp
->f_path
.dentry
;
1388 struct inode
*inode
= dentry
->d_inode
;
1389 struct task_struct
*p
= get_proc_task(inode
);
1390 unsigned int fd
, tid
, ino
;
1392 struct files_struct
* files
;
1393 struct fdtable
*fdt
;
1404 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1408 ino
= parent_ino(dentry
);
1409 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1413 files
= get_files_struct(p
);
1417 fdt
= files_fdtable(files
);
1418 for (fd
= filp
->f_pos
-2;
1420 fd
++, filp
->f_pos
++) {
1421 char name
[PROC_NUMBUF
];
1424 if (!fcheck_files(files
, fd
))
1428 len
= snprintf(name
, sizeof(name
), "%d", fd
);
1429 if (proc_fill_cache(filp
, dirent
, filldir
,
1430 name
, len
, instantiate
,
1438 put_files_struct(files
);
1446 static struct dentry
*proc_lookupfd(struct inode
*dir
, struct dentry
*dentry
,
1447 struct nameidata
*nd
)
1449 return proc_lookupfd_common(dir
, dentry
, proc_fd_instantiate
);
1452 static int proc_readfd(struct file
*filp
, void *dirent
, filldir_t filldir
)
1454 return proc_readfd_common(filp
, dirent
, filldir
, proc_fd_instantiate
);
1457 static ssize_t
proc_fdinfo_read(struct file
*file
, char __user
*buf
,
1458 size_t len
, loff_t
*ppos
)
1460 char tmp
[PROC_FDINFO_MAX
];
1461 int err
= proc_fd_info(file
->f_path
.dentry
->d_inode
, NULL
, NULL
, tmp
);
1463 err
= simple_read_from_buffer(buf
, len
, ppos
, tmp
, strlen(tmp
));
1467 static const struct file_operations proc_fdinfo_file_operations
= {
1468 .open
= nonseekable_open
,
1469 .read
= proc_fdinfo_read
,
1472 static const struct file_operations proc_fd_operations
= {
1473 .read
= generic_read_dir
,
1474 .readdir
= proc_readfd
,
1478 * /proc/pid/fd needs a special permission handler so that a process can still
1479 * access /proc/self/fd after it has executed a setuid().
1481 static int proc_fd_permission(struct inode
*inode
, int mask
,
1482 struct nameidata
*nd
)
1486 rv
= generic_permission(inode
, mask
, NULL
);
1489 if (task_pid(current
) == proc_pid(inode
))
1495 * proc directories can do almost nothing..
1497 static const struct inode_operations proc_fd_inode_operations
= {
1498 .lookup
= proc_lookupfd
,
1499 .permission
= proc_fd_permission
,
1500 .setattr
= proc_setattr
,
1503 static struct dentry
*proc_fdinfo_instantiate(struct inode
*dir
,
1504 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1506 unsigned fd
= *(unsigned *)ptr
;
1507 struct inode
*inode
;
1508 struct proc_inode
*ei
;
1509 struct dentry
*error
= ERR_PTR(-ENOENT
);
1511 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1516 inode
->i_mode
= S_IFREG
| S_IRUSR
;
1517 inode
->i_fop
= &proc_fdinfo_file_operations
;
1518 dentry
->d_op
= &tid_fd_dentry_operations
;
1519 d_add(dentry
, inode
);
1520 /* Close the race of the process dying before we return the dentry */
1521 if (tid_fd_revalidate(dentry
, NULL
))
1528 static struct dentry
*proc_lookupfdinfo(struct inode
*dir
,
1529 struct dentry
*dentry
,
1530 struct nameidata
*nd
)
1532 return proc_lookupfd_common(dir
, dentry
, proc_fdinfo_instantiate
);
1535 static int proc_readfdinfo(struct file
*filp
, void *dirent
, filldir_t filldir
)
1537 return proc_readfd_common(filp
, dirent
, filldir
,
1538 proc_fdinfo_instantiate
);
1541 static const struct file_operations proc_fdinfo_operations
= {
1542 .read
= generic_read_dir
,
1543 .readdir
= proc_readfdinfo
,
1547 * proc directories can do almost nothing..
1549 static const struct inode_operations proc_fdinfo_inode_operations
= {
1550 .lookup
= proc_lookupfdinfo
,
1551 .setattr
= proc_setattr
,
1555 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1556 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1558 const struct pid_entry
*p
= ptr
;
1559 struct inode
*inode
;
1560 struct proc_inode
*ei
;
1561 struct dentry
*error
= ERR_PTR(-EINVAL
);
1563 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1568 inode
->i_mode
= p
->mode
;
1569 if (S_ISDIR(inode
->i_mode
))
1570 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1572 inode
->i_op
= p
->iop
;
1574 inode
->i_fop
= p
->fop
;
1576 dentry
->d_op
= &pid_dentry_operations
;
1577 d_add(dentry
, inode
);
1578 /* Close the race of the process dying before we return the dentry */
1579 if (pid_revalidate(dentry
, NULL
))
1585 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1586 struct dentry
*dentry
,
1587 const struct pid_entry
*ents
,
1590 struct inode
*inode
;
1591 struct dentry
*error
;
1592 struct task_struct
*task
= get_proc_task(dir
);
1593 const struct pid_entry
*p
, *last
;
1595 error
= ERR_PTR(-ENOENT
);
1602 * Yes, it does not scale. And it should not. Don't add
1603 * new entries into /proc/<tgid>/ without very good reasons.
1605 last
= &ents
[nents
- 1];
1606 for (p
= ents
; p
<= last
; p
++) {
1607 if (p
->len
!= dentry
->d_name
.len
)
1609 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1615 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1617 put_task_struct(task
);
1622 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
1623 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
1625 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1626 proc_pident_instantiate
, task
, p
);
1629 static int proc_pident_readdir(struct file
*filp
,
1630 void *dirent
, filldir_t filldir
,
1631 const struct pid_entry
*ents
, unsigned int nents
)
1635 struct dentry
*dentry
= filp
->f_path
.dentry
;
1636 struct inode
*inode
= dentry
->d_inode
;
1637 struct task_struct
*task
= get_proc_task(inode
);
1638 const struct pid_entry
*p
, *last
;
1652 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1658 ino
= parent_ino(dentry
);
1659 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1671 last
= &ents
[nents
- 1];
1673 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
1682 put_task_struct(task
);
1687 #ifdef CONFIG_SECURITY
1688 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1689 size_t count
, loff_t
*ppos
)
1691 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1694 struct task_struct
*task
= get_proc_task(inode
);
1699 length
= security_getprocattr(task
,
1700 (char*)file
->f_path
.dentry
->d_name
.name
,
1702 put_task_struct(task
);
1704 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
1709 static ssize_t
proc_pid_attr_write(struct file
* file
, const 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 if (count
> PAGE_SIZE
)
1723 /* No partial writes. */
1729 page
= (char*)__get_free_page(GFP_USER
);
1734 if (copy_from_user(page
, buf
, count
))
1737 length
= security_setprocattr(task
,
1738 (char*)file
->f_path
.dentry
->d_name
.name
,
1739 (void*)page
, count
);
1741 free_page((unsigned long) page
);
1743 put_task_struct(task
);
1748 static const struct file_operations proc_pid_attr_operations
= {
1749 .read
= proc_pid_attr_read
,
1750 .write
= proc_pid_attr_write
,
1753 static const struct pid_entry attr_dir_stuff
[] = {
1754 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
1755 REG("prev", S_IRUGO
, pid_attr
),
1756 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
1757 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1758 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1759 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1762 static int proc_attr_dir_readdir(struct file
* filp
,
1763 void * dirent
, filldir_t filldir
)
1765 return proc_pident_readdir(filp
,dirent
,filldir
,
1766 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
1769 static const struct file_operations proc_attr_dir_operations
= {
1770 .read
= generic_read_dir
,
1771 .readdir
= proc_attr_dir_readdir
,
1774 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
1775 struct dentry
*dentry
, struct nameidata
*nd
)
1777 return proc_pident_lookup(dir
, dentry
,
1778 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
1781 static const struct inode_operations proc_attr_dir_inode_operations
= {
1782 .lookup
= proc_attr_dir_lookup
,
1783 .getattr
= pid_getattr
,
1784 .setattr
= proc_setattr
,
1789 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
1790 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
1791 size_t count
, loff_t
*ppos
)
1793 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1794 struct mm_struct
*mm
;
1795 char buffer
[PROC_NUMBUF
];
1803 mm
= get_task_mm(task
);
1805 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
1806 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
1807 MMF_DUMP_FILTER_SHIFT
));
1809 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1812 put_task_struct(task
);
1817 static ssize_t
proc_coredump_filter_write(struct file
*file
,
1818 const char __user
*buf
,
1822 struct task_struct
*task
;
1823 struct mm_struct
*mm
;
1824 char buffer
[PROC_NUMBUF
], *end
;
1831 memset(buffer
, 0, sizeof(buffer
));
1832 if (count
> sizeof(buffer
) - 1)
1833 count
= sizeof(buffer
) - 1;
1834 if (copy_from_user(buffer
, buf
, count
))
1838 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
1841 if (end
- buffer
== 0)
1845 task
= get_proc_task(file
->f_dentry
->d_inode
);
1850 mm
= get_task_mm(task
);
1854 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
1856 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
1858 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
1863 put_task_struct(task
);
1868 static const struct file_operations proc_coredump_filter_operations
= {
1869 .read
= proc_coredump_filter_read
,
1870 .write
= proc_coredump_filter_write
,
1877 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1880 char tmp
[PROC_NUMBUF
];
1881 sprintf(tmp
, "%d", current
->tgid
);
1882 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1885 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1887 char tmp
[PROC_NUMBUF
];
1888 sprintf(tmp
, "%d", current
->tgid
);
1889 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1892 static const struct inode_operations proc_self_inode_operations
= {
1893 .readlink
= proc_self_readlink
,
1894 .follow_link
= proc_self_follow_link
,
1900 * These are the directory entries in the root directory of /proc
1901 * that properly belong to the /proc filesystem, as they describe
1902 * describe something that is process related.
1904 static const struct pid_entry proc_base_stuff
[] = {
1905 NOD("self", S_IFLNK
|S_IRWXUGO
,
1906 &proc_self_inode_operations
, NULL
, {}),
1910 * Exceptional case: normally we are not allowed to unhash a busy
1911 * directory. In this case, however, we can do it - no aliasing problems
1912 * due to the way we treat inodes.
1914 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1916 struct inode
*inode
= dentry
->d_inode
;
1917 struct task_struct
*task
= get_proc_task(inode
);
1919 put_task_struct(task
);
1926 static struct dentry_operations proc_base_dentry_operations
=
1928 .d_revalidate
= proc_base_revalidate
,
1929 .d_delete
= pid_delete_dentry
,
1932 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
1933 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
1935 const struct pid_entry
*p
= ptr
;
1936 struct inode
*inode
;
1937 struct proc_inode
*ei
;
1938 struct dentry
*error
= ERR_PTR(-EINVAL
);
1940 /* Allocate the inode */
1941 error
= ERR_PTR(-ENOMEM
);
1942 inode
= new_inode(dir
->i_sb
);
1946 /* Initialize the inode */
1948 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1951 * grab the reference to the task.
1953 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1959 inode
->i_mode
= p
->mode
;
1960 if (S_ISDIR(inode
->i_mode
))
1962 if (S_ISLNK(inode
->i_mode
))
1965 inode
->i_op
= p
->iop
;
1967 inode
->i_fop
= p
->fop
;
1969 dentry
->d_op
= &proc_base_dentry_operations
;
1970 d_add(dentry
, inode
);
1979 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
1981 struct dentry
*error
;
1982 struct task_struct
*task
= get_proc_task(dir
);
1983 const struct pid_entry
*p
, *last
;
1985 error
= ERR_PTR(-ENOENT
);
1990 /* Lookup the directory entry */
1991 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
1992 for (p
= proc_base_stuff
; p
<= last
; p
++) {
1993 if (p
->len
!= dentry
->d_name
.len
)
1995 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2001 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2004 put_task_struct(task
);
2009 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2010 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2012 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2013 proc_base_instantiate
, task
, p
);
2016 #ifdef CONFIG_TASK_IO_ACCOUNTING
2017 static int proc_pid_io_accounting(struct task_struct
*task
, char *buffer
)
2019 return sprintf(buffer
,
2020 #ifdef CONFIG_TASK_XACCT
2026 "read_bytes: %llu\n"
2027 "write_bytes: %llu\n"
2028 "cancelled_write_bytes: %llu\n",
2029 #ifdef CONFIG_TASK_XACCT
2030 (unsigned long long)task
->rchar
,
2031 (unsigned long long)task
->wchar
,
2032 (unsigned long long)task
->syscr
,
2033 (unsigned long long)task
->syscw
,
2035 (unsigned long long)task
->ioac
.read_bytes
,
2036 (unsigned long long)task
->ioac
.write_bytes
,
2037 (unsigned long long)task
->ioac
.cancelled_write_bytes
);
2044 static const struct file_operations proc_task_operations
;
2045 static const struct inode_operations proc_task_inode_operations
;
2047 static const struct pid_entry tgid_base_stuff
[] = {
2048 DIR("task", S_IRUGO
|S_IXUGO
, task
),
2049 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2050 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2051 INF("environ", S_IRUSR
, pid_environ
),
2052 INF("auxv", S_IRUSR
, pid_auxv
),
2053 INF("status", S_IRUGO
, pid_status
),
2054 #ifdef CONFIG_SCHED_DEBUG
2055 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2057 INF("cmdline", S_IRUGO
, pid_cmdline
),
2058 INF("stat", S_IRUGO
, tgid_stat
),
2059 INF("statm", S_IRUGO
, pid_statm
),
2060 REG("maps", S_IRUGO
, maps
),
2062 REG("numa_maps", S_IRUGO
, numa_maps
),
2064 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2068 REG("mounts", S_IRUGO
, mounts
),
2069 REG("mountstats", S_IRUSR
, mountstats
),
2071 REG("clear_refs", S_IWUSR
, clear_refs
),
2072 REG("smaps", S_IRUGO
, smaps
),
2074 #ifdef CONFIG_SECURITY
2075 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2077 #ifdef CONFIG_KALLSYMS
2078 INF("wchan", S_IRUGO
, pid_wchan
),
2080 #ifdef CONFIG_SCHEDSTATS
2081 INF("schedstat", S_IRUGO
, pid_schedstat
),
2083 #ifdef CONFIG_CPUSETS
2084 REG("cpuset", S_IRUGO
, cpuset
),
2086 INF("oom_score", S_IRUGO
, oom_score
),
2087 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2088 #ifdef CONFIG_AUDITSYSCALL
2089 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2091 #ifdef CONFIG_FAULT_INJECTION
2092 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2094 #if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2095 REG("coredump_filter", S_IRUGO
|S_IWUSR
, coredump_filter
),
2097 #ifdef CONFIG_TASK_IO_ACCOUNTING
2098 INF("io", S_IRUGO
, pid_io_accounting
),
2102 static int proc_tgid_base_readdir(struct file
* filp
,
2103 void * dirent
, filldir_t filldir
)
2105 return proc_pident_readdir(filp
,dirent
,filldir
,
2106 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2109 static const struct file_operations proc_tgid_base_operations
= {
2110 .read
= generic_read_dir
,
2111 .readdir
= proc_tgid_base_readdir
,
2114 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2115 return proc_pident_lookup(dir
, dentry
,
2116 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2119 static const struct inode_operations proc_tgid_base_inode_operations
= {
2120 .lookup
= proc_tgid_base_lookup
,
2121 .getattr
= pid_getattr
,
2122 .setattr
= proc_setattr
,
2126 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2128 * @task: task that should be flushed.
2130 * Looks in the dcache for
2132 * /proc/@tgid/task/@pid
2133 * if either directory is present flushes it and all of it'ts children
2136 * It is safe and reasonable to cache /proc entries for a task until
2137 * that task exits. After that they just clog up the dcache with
2138 * useless entries, possibly causing useful dcache entries to be
2139 * flushed instead. This routine is proved to flush those useless
2140 * dcache entries at process exit time.
2142 * NOTE: This routine is just an optimization so it does not guarantee
2143 * that no dcache entries will exist at process exit time it
2144 * just makes it very unlikely that any will persist.
2146 void proc_flush_task(struct task_struct
*task
)
2148 struct dentry
*dentry
, *leader
, *dir
;
2149 char buf
[PROC_NUMBUF
];
2153 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2154 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2156 shrink_dcache_parent(dentry
);
2161 if (thread_group_leader(task
))
2165 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
2166 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
2171 name
.len
= strlen(name
.name
);
2172 dir
= d_hash_and_lookup(leader
, &name
);
2174 goto out_put_leader
;
2177 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
2178 dentry
= d_hash_and_lookup(dir
, &name
);
2180 shrink_dcache_parent(dentry
);
2192 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2193 struct dentry
* dentry
,
2194 struct task_struct
*task
, const void *ptr
)
2196 struct dentry
*error
= ERR_PTR(-ENOENT
);
2197 struct inode
*inode
;
2199 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2203 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2204 inode
->i_op
= &proc_tgid_base_inode_operations
;
2205 inode
->i_fop
= &proc_tgid_base_operations
;
2206 inode
->i_flags
|=S_IMMUTABLE
;
2208 #ifdef CONFIG_SECURITY
2209 inode
->i_nlink
+= 1;
2212 dentry
->d_op
= &pid_dentry_operations
;
2214 d_add(dentry
, inode
);
2215 /* Close the race of the process dying before we return the dentry */
2216 if (pid_revalidate(dentry
, NULL
))
2222 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2224 struct dentry
*result
= ERR_PTR(-ENOENT
);
2225 struct task_struct
*task
;
2228 result
= proc_base_lookup(dir
, dentry
);
2229 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2232 tgid
= name_to_int(dentry
);
2237 task
= find_task_by_pid(tgid
);
2239 get_task_struct(task
);
2244 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2245 put_task_struct(task
);
2251 * Find the first task with tgid >= tgid
2254 static struct task_struct
*next_tgid(unsigned int tgid
)
2256 struct task_struct
*task
;
2262 pid
= find_ge_pid(tgid
);
2265 task
= pid_task(pid
, PIDTYPE_PID
);
2266 /* What we to know is if the pid we have find is the
2267 * pid of a thread_group_leader. Testing for task
2268 * being a thread_group_leader is the obvious thing
2269 * todo but there is a window when it fails, due to
2270 * the pid transfer logic in de_thread.
2272 * So we perform the straight forward test of seeing
2273 * if the pid we have found is the pid of a thread
2274 * group leader, and don't worry if the task we have
2275 * found doesn't happen to be a thread group leader.
2276 * As we don't care in the case of readdir.
2278 if (!task
|| !has_group_leader_pid(task
))
2280 get_task_struct(task
);
2286 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2288 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2289 struct task_struct
*task
, int tgid
)
2291 char name
[PROC_NUMBUF
];
2292 int len
= snprintf(name
, sizeof(name
), "%d", tgid
);
2293 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2294 proc_pid_instantiate
, task
, NULL
);
2297 /* for the /proc/ directory itself, after non-process stuff has been done */
2298 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2300 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2301 struct task_struct
*reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2302 struct task_struct
*task
;
2308 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2309 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2310 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2314 tgid
= filp
->f_pos
- TGID_OFFSET
;
2315 for (task
= next_tgid(tgid
);
2317 put_task_struct(task
), task
= next_tgid(tgid
+ 1)) {
2319 filp
->f_pos
= tgid
+ TGID_OFFSET
;
2320 if (proc_pid_fill_cache(filp
, dirent
, filldir
, task
, tgid
) < 0) {
2321 put_task_struct(task
);
2325 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2327 put_task_struct(reaper
);
2335 static const struct pid_entry tid_base_stuff
[] = {
2336 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2337 DIR("fdinfo", S_IRUSR
|S_IXUSR
, fdinfo
),
2338 INF("environ", S_IRUSR
, pid_environ
),
2339 INF("auxv", S_IRUSR
, pid_auxv
),
2340 INF("status", S_IRUGO
, pid_status
),
2341 #ifdef CONFIG_SCHED_DEBUG
2342 REG("sched", S_IRUGO
|S_IWUSR
, pid_sched
),
2344 INF("cmdline", S_IRUGO
, pid_cmdline
),
2345 INF("stat", S_IRUGO
, tid_stat
),
2346 INF("statm", S_IRUGO
, pid_statm
),
2347 REG("maps", S_IRUGO
, maps
),
2349 REG("numa_maps", S_IRUGO
, numa_maps
),
2351 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2355 REG("mounts", S_IRUGO
, mounts
),
2357 REG("clear_refs", S_IWUSR
, clear_refs
),
2358 REG("smaps", S_IRUGO
, smaps
),
2360 #ifdef CONFIG_SECURITY
2361 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2363 #ifdef CONFIG_KALLSYMS
2364 INF("wchan", S_IRUGO
, pid_wchan
),
2366 #ifdef CONFIG_SCHEDSTATS
2367 INF("schedstat", S_IRUGO
, pid_schedstat
),
2369 #ifdef CONFIG_CPUSETS
2370 REG("cpuset", S_IRUGO
, cpuset
),
2372 INF("oom_score", S_IRUGO
, oom_score
),
2373 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2374 #ifdef CONFIG_AUDITSYSCALL
2375 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2377 #ifdef CONFIG_FAULT_INJECTION
2378 REG("make-it-fail", S_IRUGO
|S_IWUSR
, fault_inject
),
2382 static int proc_tid_base_readdir(struct file
* filp
,
2383 void * dirent
, filldir_t filldir
)
2385 return proc_pident_readdir(filp
,dirent
,filldir
,
2386 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2389 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2390 return proc_pident_lookup(dir
, dentry
,
2391 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2394 static const struct file_operations proc_tid_base_operations
= {
2395 .read
= generic_read_dir
,
2396 .readdir
= proc_tid_base_readdir
,
2399 static const struct inode_operations proc_tid_base_inode_operations
= {
2400 .lookup
= proc_tid_base_lookup
,
2401 .getattr
= pid_getattr
,
2402 .setattr
= proc_setattr
,
2405 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2406 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2408 struct dentry
*error
= ERR_PTR(-ENOENT
);
2409 struct inode
*inode
;
2410 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2414 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2415 inode
->i_op
= &proc_tid_base_inode_operations
;
2416 inode
->i_fop
= &proc_tid_base_operations
;
2417 inode
->i_flags
|=S_IMMUTABLE
;
2419 #ifdef CONFIG_SECURITY
2420 inode
->i_nlink
+= 1;
2423 dentry
->d_op
= &pid_dentry_operations
;
2425 d_add(dentry
, inode
);
2426 /* Close the race of the process dying before we return the dentry */
2427 if (pid_revalidate(dentry
, NULL
))
2433 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2435 struct dentry
*result
= ERR_PTR(-ENOENT
);
2436 struct task_struct
*task
;
2437 struct task_struct
*leader
= get_proc_task(dir
);
2443 tid
= name_to_int(dentry
);
2448 task
= find_task_by_pid(tid
);
2450 get_task_struct(task
);
2454 if (leader
->tgid
!= task
->tgid
)
2457 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2459 put_task_struct(task
);
2461 put_task_struct(leader
);
2467 * Find the first tid of a thread group to return to user space.
2469 * Usually this is just the thread group leader, but if the users
2470 * buffer was too small or there was a seek into the middle of the
2471 * directory we have more work todo.
2473 * In the case of a short read we start with find_task_by_pid.
2475 * In the case of a seek we start with the leader and walk nr
2478 static struct task_struct
*first_tid(struct task_struct
*leader
,
2481 struct task_struct
*pos
;
2484 /* Attempt to start with the pid of a thread */
2485 if (tid
&& (nr
> 0)) {
2486 pos
= find_task_by_pid(tid
);
2487 if (pos
&& (pos
->group_leader
== leader
))
2491 /* If nr exceeds the number of threads there is nothing todo */
2493 if (nr
&& nr
>= get_nr_threads(leader
))
2496 /* If we haven't found our starting place yet start
2497 * with the leader and walk nr threads forward.
2499 for (pos
= leader
; nr
> 0; --nr
) {
2500 pos
= next_thread(pos
);
2501 if (pos
== leader
) {
2507 get_task_struct(pos
);
2514 * Find the next thread in the thread list.
2515 * Return NULL if there is an error or no next thread.
2517 * The reference to the input task_struct is released.
2519 static struct task_struct
*next_tid(struct task_struct
*start
)
2521 struct task_struct
*pos
= NULL
;
2523 if (pid_alive(start
)) {
2524 pos
= next_thread(start
);
2525 if (thread_group_leader(pos
))
2528 get_task_struct(pos
);
2531 put_task_struct(start
);
2535 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2536 struct task_struct
*task
, int tid
)
2538 char name
[PROC_NUMBUF
];
2539 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2540 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2541 proc_task_instantiate
, task
, NULL
);
2544 /* for the /proc/TGID/task/ directories */
2545 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2547 struct dentry
*dentry
= filp
->f_path
.dentry
;
2548 struct inode
*inode
= dentry
->d_inode
;
2549 struct task_struct
*leader
= NULL
;
2550 struct task_struct
*task
;
2551 int retval
= -ENOENT
;
2554 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2556 task
= get_proc_task(inode
);
2560 if (pid_alive(task
)) {
2561 leader
= task
->group_leader
;
2562 get_task_struct(leader
);
2565 put_task_struct(task
);
2573 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2578 ino
= parent_ino(dentry
);
2579 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2585 /* f_version caches the tgid value that the last readdir call couldn't
2586 * return. lseek aka telldir automagically resets f_version to 0.
2588 tid
= filp
->f_version
;
2589 filp
->f_version
= 0;
2590 for (task
= first_tid(leader
, tid
, pos
- 2);
2592 task
= next_tid(task
), pos
++) {
2594 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
2595 /* returning this tgid failed, save it as the first
2596 * pid for the next readir call */
2597 filp
->f_version
= tid
;
2598 put_task_struct(task
);
2604 put_task_struct(leader
);
2609 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2611 struct inode
*inode
= dentry
->d_inode
;
2612 struct task_struct
*p
= get_proc_task(inode
);
2613 generic_fillattr(inode
, stat
);
2617 stat
->nlink
+= get_nr_threads(p
);
2625 static const struct inode_operations proc_task_inode_operations
= {
2626 .lookup
= proc_task_lookup
,
2627 .getattr
= proc_task_getattr
,
2628 .setattr
= proc_setattr
,
2631 static const struct file_operations proc_task_operations
= {
2632 .read
= generic_read_dir
,
2633 .readdir
= proc_task_readdir
,