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/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.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 struct inode_operations
*iop
;
97 struct file_operations
*fop
;
101 #define NOD(NAME, MODE, IOP, FOP, OP) { \
102 .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 } )
126 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
128 struct fs_struct
*fs
;
132 atomic_inc(&fs
->count
);
137 static int get_nr_threads(struct task_struct
*tsk
)
139 /* Must be called with the rcu_read_lock held */
143 if (lock_task_sighand(tsk
, &flags
)) {
144 count
= atomic_read(&tsk
->signal
->count
);
145 unlock_task_sighand(tsk
, &flags
);
150 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
152 struct task_struct
*task
= get_proc_task(inode
);
153 struct fs_struct
*fs
= NULL
;
154 int result
= -ENOENT
;
157 fs
= get_fs_struct(task
);
158 put_task_struct(task
);
161 read_lock(&fs
->lock
);
162 *mnt
= mntget(fs
->pwdmnt
);
163 *dentry
= dget(fs
->pwd
);
164 read_unlock(&fs
->lock
);
171 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
173 struct task_struct
*task
= get_proc_task(inode
);
174 struct fs_struct
*fs
= NULL
;
175 int result
= -ENOENT
;
178 fs
= get_fs_struct(task
);
179 put_task_struct(task
);
182 read_lock(&fs
->lock
);
183 *mnt
= mntget(fs
->rootmnt
);
184 *dentry
= dget(fs
->root
);
185 read_unlock(&fs
->lock
);
192 #define MAY_PTRACE(task) \
193 (task == current || \
194 (task->parent == current && \
195 (task->ptrace & PT_PTRACED) && \
196 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
197 security_ptrace(current,task) == 0))
199 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
202 struct mm_struct
*mm
= get_task_mm(task
);
204 unsigned int len
= mm
->env_end
- mm
->env_start
;
207 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
208 if (!ptrace_may_attach(task
))
215 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
219 struct mm_struct
*mm
= get_task_mm(task
);
223 goto out_mm
; /* Shh! No looking before we're done */
225 len
= mm
->arg_end
- mm
->arg_start
;
230 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
232 // If the nul at the end of args has been overwritten, then
233 // assume application is using setproctitle(3).
234 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
235 len
= strnlen(buffer
, res
);
239 len
= mm
->env_end
- mm
->env_start
;
240 if (len
> PAGE_SIZE
- res
)
241 len
= PAGE_SIZE
- res
;
242 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
243 res
= strnlen(buffer
, res
);
252 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
255 struct mm_struct
*mm
= get_task_mm(task
);
257 unsigned int nwords
= 0;
260 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
261 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
264 memcpy(buffer
, mm
->saved_auxv
, res
);
271 #ifdef CONFIG_KALLSYMS
273 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
274 * Returns the resolved symbol. If that fails, simply return the address.
276 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
279 const char *sym_name
;
280 unsigned long wchan
, size
, offset
;
281 char namebuf
[KSYM_NAME_LEN
+1];
283 wchan
= get_wchan(task
);
285 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
287 return sprintf(buffer
, "%s", sym_name
);
288 return sprintf(buffer
, "%lu", wchan
);
290 #endif /* CONFIG_KALLSYMS */
292 #ifdef CONFIG_SCHEDSTATS
294 * Provides /proc/PID/schedstat
296 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
298 return sprintf(buffer
, "%lu %lu %lu\n",
299 task
->sched_info
.cpu_time
,
300 task
->sched_info
.run_delay
,
301 task
->sched_info
.pcnt
);
305 /* The badness from the OOM killer */
306 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
307 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
309 unsigned long points
;
310 struct timespec uptime
;
312 do_posix_clock_monotonic_gettime(&uptime
);
313 points
= badness(task
, uptime
.tv_sec
);
314 return sprintf(buffer
, "%lu\n", points
);
317 /************************************************************************/
318 /* Here the fs part begins */
319 /************************************************************************/
321 /* permission checks */
322 static int proc_fd_access_allowed(struct inode
*inode
)
324 struct task_struct
*task
;
326 /* Allow access to a task's file descriptors if it is us or we
327 * may use ptrace attach to the process and find out that
330 task
= get_proc_task(inode
);
332 allowed
= ptrace_may_attach(task
);
333 put_task_struct(task
);
338 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
341 struct inode
*inode
= dentry
->d_inode
;
343 if (attr
->ia_valid
& ATTR_MODE
)
346 error
= inode_change_ok(inode
, attr
);
348 error
= security_inode_setattr(dentry
, attr
);
350 error
= inode_setattr(inode
, attr
);
355 static struct inode_operations proc_def_inode_operations
= {
356 .setattr
= proc_setattr
,
359 extern struct seq_operations mounts_op
;
365 static int mounts_open(struct inode
*inode
, struct file
*file
)
367 struct task_struct
*task
= get_proc_task(inode
);
368 struct namespace *namespace = NULL
;
369 struct proc_mounts
*p
;
374 namespace = task
->nsproxy
->namespace;
376 get_namespace(namespace);
378 put_task_struct(task
);
383 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
385 file
->private_data
= &p
->m
;
386 ret
= seq_open(file
, &mounts_op
);
388 p
->m
.private = namespace;
389 p
->event
= namespace->event
;
394 put_namespace(namespace);
399 static int mounts_release(struct inode
*inode
, struct file
*file
)
401 struct seq_file
*m
= file
->private_data
;
402 struct namespace *namespace = m
->private;
403 put_namespace(namespace);
404 return seq_release(inode
, file
);
407 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
409 struct proc_mounts
*p
= file
->private_data
;
410 struct namespace *ns
= p
->m
.private;
413 poll_wait(file
, &ns
->poll
, wait
);
415 spin_lock(&vfsmount_lock
);
416 if (p
->event
!= ns
->event
) {
417 p
->event
= ns
->event
;
420 spin_unlock(&vfsmount_lock
);
425 static struct file_operations proc_mounts_operations
= {
429 .release
= mounts_release
,
433 extern struct seq_operations mountstats_op
;
434 static int mountstats_open(struct inode
*inode
, struct file
*file
)
436 int ret
= seq_open(file
, &mountstats_op
);
439 struct seq_file
*m
= file
->private_data
;
440 struct namespace *namespace = NULL
;
441 struct task_struct
*task
= get_proc_task(inode
);
446 namespace = task
->nsproxy
->namespace;
448 get_namespace(namespace);
450 put_task_struct(task
);
454 m
->private = namespace;
456 seq_release(inode
, file
);
463 static struct file_operations proc_mountstats_operations
= {
464 .open
= mountstats_open
,
467 .release
= mounts_release
,
470 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
472 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
473 size_t count
, loff_t
*ppos
)
475 struct inode
* inode
= file
->f_dentry
->d_inode
;
478 struct task_struct
*task
= get_proc_task(inode
);
484 if (count
> PROC_BLOCK_SIZE
)
485 count
= PROC_BLOCK_SIZE
;
488 if (!(page
= __get_free_page(GFP_KERNEL
)))
491 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
494 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
497 put_task_struct(task
);
502 static struct file_operations proc_info_file_operations
= {
503 .read
= proc_info_read
,
506 static int mem_open(struct inode
* inode
, struct file
* file
)
508 file
->private_data
= (void*)((long)current
->self_exec_id
);
512 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
513 size_t count
, loff_t
*ppos
)
515 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
517 unsigned long src
= *ppos
;
519 struct mm_struct
*mm
;
524 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
528 page
= (char *)__get_free_page(GFP_USER
);
534 mm
= get_task_mm(task
);
540 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
546 int this_len
, retval
;
548 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
549 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
550 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
556 if (copy_to_user(buf
, page
, retval
)) {
571 free_page((unsigned long) page
);
573 put_task_struct(task
);
578 #define mem_write NULL
581 /* This is a security hazard */
582 static ssize_t
mem_write(struct file
* file
, const char * buf
,
583 size_t count
, loff_t
*ppos
)
587 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
588 unsigned long dst
= *ppos
;
594 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
598 page
= (char *)__get_free_page(GFP_USER
);
604 int this_len
, retval
;
606 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
607 if (copy_from_user(page
, buf
, this_len
)) {
611 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
623 free_page((unsigned long) page
);
625 put_task_struct(task
);
631 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
635 file
->f_pos
= offset
;
638 file
->f_pos
+= offset
;
643 force_successful_syscall_return();
647 static struct file_operations proc_mem_operations
= {
654 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
655 size_t count
, loff_t
*ppos
)
657 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
658 char buffer
[PROC_NUMBUF
];
661 loff_t __ppos
= *ppos
;
665 oom_adjust
= task
->oomkilladj
;
666 put_task_struct(task
);
668 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", oom_adjust
);
671 if (count
> len
-__ppos
)
673 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
675 *ppos
= __ppos
+ count
;
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 if (!capable(CAP_SYS_RESOURCE
))
688 memset(buffer
, 0, sizeof(buffer
));
689 if (count
> sizeof(buffer
) - 1)
690 count
= sizeof(buffer
) - 1;
691 if (copy_from_user(buffer
, buf
, count
))
693 oom_adjust
= simple_strtol(buffer
, &end
, 0);
694 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
695 && oom_adjust
!= OOM_DISABLE_NOINHERIT
)
699 task
= get_proc_task(file
->f_dentry
->d_inode
);
702 task
->oomkilladj
= oom_adjust
;
703 put_task_struct(task
);
704 if (end
- buffer
== 0)
709 static struct file_operations proc_oom_adjust_operations
= {
710 .read
= oom_adjust_read
,
711 .write
= oom_adjust_write
,
714 #ifdef CONFIG_AUDITSYSCALL
716 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
717 size_t count
, loff_t
*ppos
)
719 struct inode
* inode
= file
->f_dentry
->d_inode
;
720 struct task_struct
*task
= get_proc_task(inode
);
722 char tmpbuf
[TMPBUFLEN
];
726 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
727 audit_get_loginuid(task
->audit_context
));
728 put_task_struct(task
);
729 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
732 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
733 size_t count
, loff_t
*ppos
)
735 struct inode
* inode
= file
->f_dentry
->d_inode
;
740 if (!capable(CAP_AUDIT_CONTROL
))
743 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
))
746 if (count
>= PAGE_SIZE
)
747 count
= PAGE_SIZE
- 1;
750 /* No partial writes. */
753 page
= (char*)__get_free_page(GFP_USER
);
757 if (copy_from_user(page
, buf
, count
))
761 loginuid
= simple_strtoul(page
, &tmp
, 10);
767 length
= audit_set_loginuid(current
, loginuid
);
768 if (likely(length
== 0))
772 free_page((unsigned long) page
);
776 static struct file_operations proc_loginuid_operations
= {
777 .read
= proc_loginuid_read
,
778 .write
= proc_loginuid_write
,
782 #ifdef CONFIG_SECCOMP
783 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
784 size_t count
, loff_t
*ppos
)
786 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
788 loff_t __ppos
= *ppos
;
793 /* no need to print the trailing zero, so use only len */
794 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
795 put_task_struct(tsk
);
798 if (count
> len
- __ppos
)
799 count
= len
- __ppos
;
800 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
802 *ppos
= __ppos
+ count
;
806 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
807 size_t count
, loff_t
*ppos
)
809 struct task_struct
*tsk
= get_proc_task(file
->f_dentry
->d_inode
);
810 char __buf
[20], *end
;
811 unsigned int seccomp_mode
;
818 /* can set it only once to be even more secure */
820 if (unlikely(tsk
->seccomp
.mode
))
824 memset(__buf
, 0, sizeof(__buf
));
825 count
= min(count
, sizeof(__buf
) - 1);
826 if (copy_from_user(__buf
, buf
, count
))
829 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
833 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
834 tsk
->seccomp
.mode
= seccomp_mode
;
835 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
839 if (unlikely(!(end
- __buf
)))
841 result
= end
- __buf
;
843 put_task_struct(tsk
);
848 static struct file_operations proc_seccomp_operations
= {
849 .read
= seccomp_read
,
850 .write
= seccomp_write
,
852 #endif /* CONFIG_SECCOMP */
854 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
856 struct inode
*inode
= dentry
->d_inode
;
859 /* We don't need a base pointer in the /proc filesystem */
862 /* Are we allowed to snoop on the tasks file descriptors? */
863 if (!proc_fd_access_allowed(inode
))
866 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
867 nd
->last_type
= LAST_BIND
;
869 return ERR_PTR(error
);
872 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
873 char __user
*buffer
, int buflen
)
875 struct inode
* inode
;
876 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
882 inode
= dentry
->d_inode
;
883 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
887 len
= tmp
+ PAGE_SIZE
- 1 - path
;
891 if (copy_to_user(buffer
, path
, len
))
894 free_page((unsigned long)tmp
);
898 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
901 struct inode
*inode
= dentry
->d_inode
;
903 struct vfsmount
*mnt
= NULL
;
905 /* Are we allowed to snoop on the tasks file descriptors? */
906 if (!proc_fd_access_allowed(inode
))
909 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
913 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
920 static struct inode_operations proc_pid_link_inode_operations
= {
921 .readlink
= proc_pid_readlink
,
922 .follow_link
= proc_pid_follow_link
,
923 .setattr
= proc_setattr
,
927 /* building an inode */
929 static int task_dumpable(struct task_struct
*task
)
932 struct mm_struct
*mm
;
937 dumpable
= mm
->dumpable
;
945 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
947 struct inode
* inode
;
948 struct proc_inode
*ei
;
950 /* We need a new inode */
952 inode
= new_inode(sb
);
958 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
959 inode
->i_op
= &proc_def_inode_operations
;
962 * grab the reference to task.
964 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
970 if (task_dumpable(task
)) {
971 inode
->i_uid
= task
->euid
;
972 inode
->i_gid
= task
->egid
;
974 security_task_to_inode(task
, inode
);
984 static int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
986 struct inode
*inode
= dentry
->d_inode
;
987 struct task_struct
*task
;
988 generic_fillattr(inode
, stat
);
993 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
995 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
996 task_dumpable(task
)) {
997 stat
->uid
= task
->euid
;
998 stat
->gid
= task
->egid
;
1008 * Exceptional case: normally we are not allowed to unhash a busy
1009 * directory. In this case, however, we can do it - no aliasing problems
1010 * due to the way we treat inodes.
1012 * Rewrite the inode's ownerships here because the owning task may have
1013 * performed a setuid(), etc.
1015 * Before the /proc/pid/status file was created the only way to read
1016 * the effective uid of a /process was to stat /proc/pid. Reading
1017 * /proc/pid/status is slow enough that procps and other packages
1018 * kept stating /proc/pid. To keep the rules in /proc simple I have
1019 * made this apply to all per process world readable and executable
1022 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1024 struct inode
*inode
= dentry
->d_inode
;
1025 struct task_struct
*task
= get_proc_task(inode
);
1027 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1028 task_dumpable(task
)) {
1029 inode
->i_uid
= task
->euid
;
1030 inode
->i_gid
= task
->egid
;
1035 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1036 security_task_to_inode(task
, inode
);
1037 put_task_struct(task
);
1044 static int pid_delete_dentry(struct dentry
* dentry
)
1046 /* Is the task we represent dead?
1047 * If so, then don't put the dentry on the lru list,
1048 * kill it immediately.
1050 return !proc_pid(dentry
->d_inode
)->tasks
[PIDTYPE_PID
].first
;
1053 static struct dentry_operations pid_dentry_operations
=
1055 .d_revalidate
= pid_revalidate
,
1056 .d_delete
= pid_delete_dentry
,
1061 typedef struct dentry
*instantiate_t(struct inode
*, struct dentry
*, struct task_struct
*, void *);
1064 * Fill a directory entry.
1066 * If possible create the dcache entry and derive our inode number and
1067 * file type from dcache entry.
1069 * Since all of the proc inode numbers are dynamically generated, the inode
1070 * numbers do not exist until the inode is cache. This means creating the
1071 * the dcache entry in readdir is necessary to keep the inode numbers
1072 * reported by readdir in sync with the inode numbers reported
1075 static int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1076 char *name
, int len
,
1077 instantiate_t instantiate
, struct task_struct
*task
, void *ptr
)
1079 struct dentry
*child
, *dir
= filp
->f_dentry
;
1080 struct inode
*inode
;
1083 unsigned type
= DT_UNKNOWN
;
1087 qname
.hash
= full_name_hash(name
, len
);
1089 child
= d_lookup(dir
, &qname
);
1092 new = d_alloc(dir
, &qname
);
1094 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1101 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1102 goto end_instantiate
;
1103 inode
= child
->d_inode
;
1106 type
= inode
->i_mode
>> 12;
1111 ino
= find_inode_number(dir
, &qname
);
1114 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1117 static unsigned name_to_int(struct dentry
*dentry
)
1119 const char *name
= dentry
->d_name
.name
;
1120 int len
= dentry
->d_name
.len
;
1123 if (len
> 1 && *name
== '0')
1126 unsigned c
= *name
++ - '0';
1129 if (n
>= (~0U-9)/10)
1139 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
1141 struct task_struct
*task
= get_proc_task(inode
);
1142 struct files_struct
*files
= NULL
;
1144 int fd
= proc_fd(inode
);
1147 files
= get_files_struct(task
);
1148 put_task_struct(task
);
1152 * We are not taking a ref to the file structure, so we must
1155 spin_lock(&files
->file_lock
);
1156 file
= fcheck_files(files
, fd
);
1158 *mnt
= mntget(file
->f_vfsmnt
);
1159 *dentry
= dget(file
->f_dentry
);
1160 spin_unlock(&files
->file_lock
);
1161 put_files_struct(files
);
1164 spin_unlock(&files
->file_lock
);
1165 put_files_struct(files
);
1170 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1172 struct inode
*inode
= dentry
->d_inode
;
1173 struct task_struct
*task
= get_proc_task(inode
);
1174 int fd
= proc_fd(inode
);
1175 struct files_struct
*files
;
1178 files
= get_files_struct(task
);
1181 if (fcheck_files(files
, fd
)) {
1183 put_files_struct(files
);
1184 if (task_dumpable(task
)) {
1185 inode
->i_uid
= task
->euid
;
1186 inode
->i_gid
= task
->egid
;
1191 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1192 security_task_to_inode(task
, inode
);
1193 put_task_struct(task
);
1197 put_files_struct(files
);
1199 put_task_struct(task
);
1205 static struct dentry_operations tid_fd_dentry_operations
=
1207 .d_revalidate
= tid_fd_revalidate
,
1208 .d_delete
= pid_delete_dentry
,
1211 static struct dentry
*proc_fd_instantiate(struct inode
*dir
,
1212 struct dentry
*dentry
, struct task_struct
*task
, void *ptr
)
1214 unsigned fd
= *(unsigned *)ptr
;
1216 struct files_struct
*files
;
1217 struct inode
*inode
;
1218 struct proc_inode
*ei
;
1219 struct dentry
*error
= ERR_PTR(-ENOENT
);
1221 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1226 files
= get_files_struct(task
);
1229 inode
->i_mode
= S_IFLNK
;
1232 * We are not taking a ref to the file structure, so we must
1235 spin_lock(&files
->file_lock
);
1236 file
= fcheck_files(files
, fd
);
1239 if (file
->f_mode
& 1)
1240 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1241 if (file
->f_mode
& 2)
1242 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1243 spin_unlock(&files
->file_lock
);
1244 put_files_struct(files
);
1246 inode
->i_op
= &proc_pid_link_inode_operations
;
1248 ei
->op
.proc_get_link
= proc_fd_link
;
1249 dentry
->d_op
= &tid_fd_dentry_operations
;
1250 d_add(dentry
, inode
);
1251 /* Close the race of the process dying before we return the dentry */
1252 if (tid_fd_revalidate(dentry
, NULL
))
1258 spin_unlock(&files
->file_lock
);
1259 put_files_struct(files
);
1265 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1267 struct task_struct
*task
= get_proc_task(dir
);
1268 unsigned fd
= name_to_int(dentry
);
1269 struct dentry
*result
= ERR_PTR(-ENOENT
);
1276 result
= proc_fd_instantiate(dir
, dentry
, task
, &fd
);
1278 put_task_struct(task
);
1283 static int proc_fd_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1284 struct task_struct
*task
, int fd
)
1286 char name
[PROC_NUMBUF
];
1287 int len
= snprintf(name
, sizeof(name
), "%d", fd
);
1288 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
1289 proc_fd_instantiate
, task
, &fd
);
1292 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1294 struct dentry
*dentry
= filp
->f_dentry
;
1295 struct inode
*inode
= dentry
->d_inode
;
1296 struct task_struct
*p
= get_proc_task(inode
);
1297 unsigned int fd
, tid
, ino
;
1299 struct files_struct
* files
;
1300 struct fdtable
*fdt
;
1311 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1315 ino
= parent_ino(dentry
);
1316 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1320 files
= get_files_struct(p
);
1324 fdt
= files_fdtable(files
);
1325 for (fd
= filp
->f_pos
-2;
1327 fd
++, filp
->f_pos
++) {
1329 if (!fcheck_files(files
, fd
))
1333 if (proc_fd_fill_cache(filp
, dirent
, filldir
, p
, fd
) < 0) {
1340 put_files_struct(files
);
1348 static struct file_operations proc_fd_operations
= {
1349 .read
= generic_read_dir
,
1350 .readdir
= proc_readfd
,
1354 * proc directories can do almost nothing..
1356 static struct inode_operations proc_fd_inode_operations
= {
1357 .lookup
= proc_lookupfd
,
1358 .setattr
= proc_setattr
,
1361 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
1362 struct dentry
*dentry
, struct task_struct
*task
, void *ptr
)
1364 struct pid_entry
*p
= ptr
;
1365 struct inode
*inode
;
1366 struct proc_inode
*ei
;
1367 struct dentry
*error
= ERR_PTR(-EINVAL
);
1369 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1374 inode
->i_mode
= p
->mode
;
1375 if (S_ISDIR(inode
->i_mode
))
1376 inode
->i_nlink
= 2; /* Use getattr to fix if necessary */
1378 inode
->i_op
= p
->iop
;
1380 inode
->i_fop
= p
->fop
;
1382 dentry
->d_op
= &pid_dentry_operations
;
1383 d_add(dentry
, inode
);
1384 /* Close the race of the process dying before we return the dentry */
1385 if (pid_revalidate(dentry
, NULL
))
1391 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1392 struct dentry
*dentry
,
1393 struct pid_entry
*ents
,
1396 struct inode
*inode
;
1397 struct dentry
*error
;
1398 struct task_struct
*task
= get_proc_task(dir
);
1399 struct pid_entry
*p
, *last
;
1401 error
= ERR_PTR(-ENOENT
);
1408 * Yes, it does not scale. And it should not. Don't add
1409 * new entries into /proc/<tgid>/ without very good reasons.
1411 last
= &ents
[nents
- 1];
1412 for (p
= ents
; p
<= last
; p
++) {
1413 if (p
->len
!= dentry
->d_name
.len
)
1415 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1421 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
1423 put_task_struct(task
);
1428 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1429 struct task_struct
*task
, struct pid_entry
*p
)
1431 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1432 proc_pident_instantiate
, task
, p
);
1435 static int proc_pident_readdir(struct file
*filp
,
1436 void *dirent
, filldir_t filldir
,
1437 struct pid_entry
*ents
, unsigned int nents
)
1441 struct dentry
*dentry
= filp
->f_dentry
;
1442 struct inode
*inode
= dentry
->d_inode
;
1443 struct task_struct
*task
= get_proc_task(inode
);
1444 struct pid_entry
*p
, *last
;
1458 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1464 ino
= parent_ino(dentry
);
1465 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1477 last
= &ents
[nents
- 1];
1479 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
1488 put_task_struct(task
);
1493 #ifdef CONFIG_SECURITY
1494 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1495 size_t count
, loff_t
*ppos
)
1497 struct inode
* inode
= file
->f_dentry
->d_inode
;
1500 struct task_struct
*task
= get_proc_task(inode
);
1506 if (count
> PAGE_SIZE
)
1509 if (!(page
= __get_free_page(GFP_KERNEL
)))
1512 length
= security_getprocattr(task
,
1513 (char*)file
->f_dentry
->d_name
.name
,
1514 (void*)page
, count
);
1516 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1519 put_task_struct(task
);
1524 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1525 size_t count
, loff_t
*ppos
)
1527 struct inode
* inode
= file
->f_dentry
->d_inode
;
1530 struct task_struct
*task
= get_proc_task(inode
);
1535 if (count
> PAGE_SIZE
)
1538 /* No partial writes. */
1544 page
= (char*)__get_free_page(GFP_USER
);
1549 if (copy_from_user(page
, buf
, count
))
1552 length
= security_setprocattr(task
,
1553 (char*)file
->f_dentry
->d_name
.name
,
1554 (void*)page
, count
);
1556 free_page((unsigned long) page
);
1558 put_task_struct(task
);
1563 static struct file_operations proc_pid_attr_operations
= {
1564 .read
= proc_pid_attr_read
,
1565 .write
= proc_pid_attr_write
,
1568 static struct pid_entry attr_dir_stuff
[] = {
1569 REG("current", S_IRUGO
|S_IWUGO
, pid_attr
),
1570 REG("prev", S_IRUGO
, pid_attr
),
1571 REG("exec", S_IRUGO
|S_IWUGO
, pid_attr
),
1572 REG("fscreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1573 REG("keycreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1574 REG("sockcreate", S_IRUGO
|S_IWUGO
, pid_attr
),
1577 static int proc_attr_dir_readdir(struct file
* filp
,
1578 void * dirent
, filldir_t filldir
)
1580 return proc_pident_readdir(filp
,dirent
,filldir
,
1581 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
1584 static struct file_operations proc_attr_dir_operations
= {
1585 .read
= generic_read_dir
,
1586 .readdir
= proc_attr_dir_readdir
,
1589 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
1590 struct dentry
*dentry
, struct nameidata
*nd
)
1592 return proc_pident_lookup(dir
, dentry
,
1593 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
1596 static struct inode_operations proc_attr_dir_inode_operations
= {
1597 .lookup
= proc_attr_dir_lookup
,
1598 .getattr
= pid_getattr
,
1599 .setattr
= proc_setattr
,
1607 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1610 char tmp
[PROC_NUMBUF
];
1611 sprintf(tmp
, "%d", current
->tgid
);
1612 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1615 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1617 char tmp
[PROC_NUMBUF
];
1618 sprintf(tmp
, "%d", current
->tgid
);
1619 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1622 static struct inode_operations proc_self_inode_operations
= {
1623 .readlink
= proc_self_readlink
,
1624 .follow_link
= proc_self_follow_link
,
1630 * These are the directory entries in the root directory of /proc
1631 * that properly belong to the /proc filesystem, as they describe
1632 * describe something that is process related.
1634 static struct pid_entry proc_base_stuff
[] = {
1635 NOD("self", S_IFLNK
|S_IRWXUGO
,
1636 &proc_self_inode_operations
, NULL
, {}),
1640 * Exceptional case: normally we are not allowed to unhash a busy
1641 * directory. In this case, however, we can do it - no aliasing problems
1642 * due to the way we treat inodes.
1644 static int proc_base_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1646 struct inode
*inode
= dentry
->d_inode
;
1647 struct task_struct
*task
= get_proc_task(inode
);
1649 put_task_struct(task
);
1656 static struct dentry_operations proc_base_dentry_operations
=
1658 .d_revalidate
= proc_base_revalidate
,
1659 .d_delete
= pid_delete_dentry
,
1662 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
1663 struct dentry
*dentry
, struct task_struct
*task
, void *ptr
)
1665 struct pid_entry
*p
= ptr
;
1666 struct inode
*inode
;
1667 struct proc_inode
*ei
;
1668 struct dentry
*error
= ERR_PTR(-EINVAL
);
1670 /* Allocate the inode */
1671 error
= ERR_PTR(-ENOMEM
);
1672 inode
= new_inode(dir
->i_sb
);
1676 /* Initialize the inode */
1678 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1681 * grab the reference to the task.
1683 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1689 inode
->i_mode
= p
->mode
;
1690 if (S_ISDIR(inode
->i_mode
))
1692 if (S_ISLNK(inode
->i_mode
))
1695 inode
->i_op
= p
->iop
;
1697 inode
->i_fop
= p
->fop
;
1699 dentry
->d_op
= &proc_base_dentry_operations
;
1700 d_add(dentry
, inode
);
1709 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
1711 struct dentry
*error
;
1712 struct task_struct
*task
= get_proc_task(dir
);
1713 struct pid_entry
*p
, *last
;
1715 error
= ERR_PTR(-ENOENT
);
1720 /* Lookup the directory entry */
1721 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
1722 for (p
= proc_base_stuff
; p
<= last
; p
++) {
1723 if (p
->len
!= dentry
->d_name
.len
)
1725 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1731 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
1734 put_task_struct(task
);
1739 static int proc_base_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1740 struct task_struct
*task
, struct pid_entry
*p
)
1742 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
1743 proc_base_instantiate
, task
, p
);
1749 static struct file_operations proc_task_operations
;
1750 static struct inode_operations proc_task_inode_operations
;
1752 static struct pid_entry tgid_base_stuff
[] = {
1753 DIR("task", S_IRUGO
|S_IXUGO
, task
),
1754 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
1755 INF("environ", S_IRUSR
, pid_environ
),
1756 INF("auxv", S_IRUSR
, pid_auxv
),
1757 INF("status", S_IRUGO
, pid_status
),
1758 INF("cmdline", S_IRUGO
, pid_cmdline
),
1759 INF("stat", S_IRUGO
, tgid_stat
),
1760 INF("statm", S_IRUGO
, pid_statm
),
1761 REG("maps", S_IRUGO
, maps
),
1763 REG("numa_maps", S_IRUGO
, numa_maps
),
1765 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
1766 #ifdef CONFIG_SECCOMP
1767 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
1772 REG("mounts", S_IRUGO
, mounts
),
1773 REG("mountstats", S_IRUSR
, mountstats
),
1775 REG("smaps", S_IRUGO
, smaps
),
1777 #ifdef CONFIG_SECURITY
1778 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
1780 #ifdef CONFIG_KALLSYMS
1781 INF("wchan", S_IRUGO
, pid_wchan
),
1783 #ifdef CONFIG_SCHEDSTATS
1784 INF("schedstat", S_IRUGO
, pid_schedstat
),
1786 #ifdef CONFIG_CPUSETS
1787 REG("cpuset", S_IRUGO
, cpuset
),
1789 INF("oom_score", S_IRUGO
, oom_score
),
1790 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
1791 #ifdef CONFIG_AUDITSYSCALL
1792 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
1796 static int proc_tgid_base_readdir(struct file
* filp
,
1797 void * dirent
, filldir_t filldir
)
1799 return proc_pident_readdir(filp
,dirent
,filldir
,
1800 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1803 static struct file_operations proc_tgid_base_operations
= {
1804 .read
= generic_read_dir
,
1805 .readdir
= proc_tgid_base_readdir
,
1808 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1809 return proc_pident_lookup(dir
, dentry
,
1810 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
1813 static struct inode_operations proc_tgid_base_inode_operations
= {
1814 .lookup
= proc_tgid_base_lookup
,
1815 .getattr
= pid_getattr
,
1816 .setattr
= proc_setattr
,
1820 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1822 * @task: task that should be flushed.
1824 * Looks in the dcache for
1826 * /proc/@tgid/task/@pid
1827 * if either directory is present flushes it and all of it'ts children
1830 * It is safe and reasonable to cache /proc entries for a task until
1831 * that task exits. After that they just clog up the dcache with
1832 * useless entries, possibly causing useful dcache entries to be
1833 * flushed instead. This routine is proved to flush those useless
1834 * dcache entries at process exit time.
1836 * NOTE: This routine is just an optimization so it does not guarantee
1837 * that no dcache entries will exist at process exit time it
1838 * just makes it very unlikely that any will persist.
1840 void proc_flush_task(struct task_struct
*task
)
1842 struct dentry
*dentry
, *leader
, *dir
;
1843 char buf
[PROC_NUMBUF
];
1847 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
1848 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
1850 shrink_dcache_parent(dentry
);
1855 if (thread_group_leader(task
))
1859 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
1860 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
1865 name
.len
= strlen(name
.name
);
1866 dir
= d_hash_and_lookup(leader
, &name
);
1868 goto out_put_leader
;
1871 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
1872 dentry
= d_hash_and_lookup(dir
, &name
);
1874 shrink_dcache_parent(dentry
);
1886 struct dentry
*proc_pid_instantiate(struct inode
*dir
,
1887 struct dentry
* dentry
, struct task_struct
*task
, void *ptr
)
1889 struct dentry
*error
= ERR_PTR(-ENOENT
);
1890 struct inode
*inode
;
1892 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1896 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1897 inode
->i_op
= &proc_tgid_base_inode_operations
;
1898 inode
->i_fop
= &proc_tgid_base_operations
;
1899 inode
->i_flags
|=S_IMMUTABLE
;
1901 #ifdef CONFIG_SECURITY
1902 inode
->i_nlink
+= 1;
1905 dentry
->d_op
= &pid_dentry_operations
;
1907 d_add(dentry
, inode
);
1908 /* Close the race of the process dying before we return the dentry */
1909 if (pid_revalidate(dentry
, NULL
))
1915 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1917 struct dentry
*result
= ERR_PTR(-ENOENT
);
1918 struct task_struct
*task
;
1921 result
= proc_base_lookup(dir
, dentry
);
1922 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
1925 tgid
= name_to_int(dentry
);
1930 task
= find_task_by_pid(tgid
);
1932 get_task_struct(task
);
1937 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
1938 put_task_struct(task
);
1944 * Find the first task with tgid >= tgid
1947 static struct task_struct
*next_tgid(unsigned int tgid
)
1949 struct task_struct
*task
;
1955 pid
= find_ge_pid(tgid
);
1958 task
= pid_task(pid
, PIDTYPE_PID
);
1959 /* What we to know is if the pid we have find is the
1960 * pid of a thread_group_leader. Testing for task
1961 * being a thread_group_leader is the obvious thing
1962 * todo but there is a window when it fails, due to
1963 * the pid transfer logic in de_thread.
1965 * So we perform the straight forward test of seeing
1966 * if the pid we have found is the pid of a thread
1967 * group leader, and don't worry if the task we have
1968 * found doesn't happen to be a thread group leader.
1969 * As we don't care in the case of readdir.
1971 if (!task
|| !has_group_leader_pid(task
))
1973 get_task_struct(task
);
1979 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
1981 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1982 struct task_struct
*task
, int tgid
)
1984 char name
[PROC_NUMBUF
];
1985 int len
= snprintf(name
, sizeof(name
), "%d", tgid
);
1986 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
1987 proc_pid_instantiate
, task
, NULL
);
1990 /* for the /proc/ directory itself, after non-process stuff has been done */
1991 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
1993 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
1994 struct task_struct
*reaper
= get_proc_task(filp
->f_dentry
->d_inode
);
1995 struct task_struct
*task
;
2001 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2002 struct pid_entry
*p
= &proc_base_stuff
[nr
];
2003 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2007 tgid
= filp
->f_pos
- TGID_OFFSET
;
2008 for (task
= next_tgid(tgid
);
2010 put_task_struct(task
), task
= next_tgid(tgid
+ 1)) {
2012 filp
->f_pos
= tgid
+ TGID_OFFSET
;
2013 if (proc_pid_fill_cache(filp
, dirent
, filldir
, task
, tgid
) < 0) {
2014 put_task_struct(task
);
2018 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2020 put_task_struct(reaper
);
2028 static struct pid_entry tid_base_stuff
[] = {
2029 DIR("fd", S_IRUSR
|S_IXUSR
, fd
),
2030 INF("environ", S_IRUSR
, pid_environ
),
2031 INF("auxv", S_IRUSR
, pid_auxv
),
2032 INF("status", S_IRUGO
, pid_status
),
2033 INF("cmdline", S_IRUGO
, pid_cmdline
),
2034 INF("stat", S_IRUGO
, tid_stat
),
2035 INF("statm", S_IRUGO
, pid_statm
),
2036 REG("maps", S_IRUGO
, maps
),
2038 REG("numa_maps", S_IRUGO
, numa_maps
),
2040 REG("mem", S_IRUSR
|S_IWUSR
, mem
),
2041 #ifdef CONFIG_SECCOMP
2042 REG("seccomp", S_IRUSR
|S_IWUSR
, seccomp
),
2047 REG("mounts", S_IRUGO
, mounts
),
2049 REG("smaps", S_IRUGO
, smaps
),
2051 #ifdef CONFIG_SECURITY
2052 DIR("attr", S_IRUGO
|S_IXUGO
, attr_dir
),
2054 #ifdef CONFIG_KALLSYMS
2055 INF("wchan", S_IRUGO
, pid_wchan
),
2057 #ifdef CONFIG_SCHEDSTATS
2058 INF("schedstat", S_IRUGO
, pid_schedstat
),
2060 #ifdef CONFIG_CPUSETS
2061 REG("cpuset", S_IRUGO
, cpuset
),
2063 INF("oom_score", S_IRUGO
, oom_score
),
2064 REG("oom_adj", S_IRUGO
|S_IWUSR
, oom_adjust
),
2065 #ifdef CONFIG_AUDITSYSCALL
2066 REG("loginuid", S_IWUSR
|S_IRUGO
, loginuid
),
2070 static int proc_tid_base_readdir(struct file
* filp
,
2071 void * dirent
, filldir_t filldir
)
2073 return proc_pident_readdir(filp
,dirent
,filldir
,
2074 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2077 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
2078 return proc_pident_lookup(dir
, dentry
,
2079 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2082 static struct file_operations proc_tid_base_operations
= {
2083 .read
= generic_read_dir
,
2084 .readdir
= proc_tid_base_readdir
,
2087 static struct inode_operations proc_tid_base_inode_operations
= {
2088 .lookup
= proc_tid_base_lookup
,
2089 .getattr
= pid_getattr
,
2090 .setattr
= proc_setattr
,
2093 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2094 struct dentry
*dentry
, struct task_struct
*task
, void *ptr
)
2096 struct dentry
*error
= ERR_PTR(-ENOENT
);
2097 struct inode
*inode
;
2098 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2102 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2103 inode
->i_op
= &proc_tid_base_inode_operations
;
2104 inode
->i_fop
= &proc_tid_base_operations
;
2105 inode
->i_flags
|=S_IMMUTABLE
;
2107 #ifdef CONFIG_SECURITY
2108 inode
->i_nlink
+= 1;
2111 dentry
->d_op
= &pid_dentry_operations
;
2113 d_add(dentry
, inode
);
2114 /* Close the race of the process dying before we return the dentry */
2115 if (pid_revalidate(dentry
, NULL
))
2121 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2123 struct dentry
*result
= ERR_PTR(-ENOENT
);
2124 struct task_struct
*task
;
2125 struct task_struct
*leader
= get_proc_task(dir
);
2131 tid
= name_to_int(dentry
);
2136 task
= find_task_by_pid(tid
);
2138 get_task_struct(task
);
2142 if (leader
->tgid
!= task
->tgid
)
2145 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
2147 put_task_struct(task
);
2149 put_task_struct(leader
);
2155 * Find the first tid of a thread group to return to user space.
2157 * Usually this is just the thread group leader, but if the users
2158 * buffer was too small or there was a seek into the middle of the
2159 * directory we have more work todo.
2161 * In the case of a short read we start with find_task_by_pid.
2163 * In the case of a seek we start with the leader and walk nr
2166 static struct task_struct
*first_tid(struct task_struct
*leader
,
2169 struct task_struct
*pos
;
2172 /* Attempt to start with the pid of a thread */
2173 if (tid
&& (nr
> 0)) {
2174 pos
= find_task_by_pid(tid
);
2175 if (pos
&& (pos
->group_leader
== leader
))
2179 /* If nr exceeds the number of threads there is nothing todo */
2181 if (nr
&& nr
>= get_nr_threads(leader
))
2184 /* If we haven't found our starting place yet start
2185 * with the leader and walk nr threads forward.
2187 for (pos
= leader
; nr
> 0; --nr
) {
2188 pos
= next_thread(pos
);
2189 if (pos
== leader
) {
2195 get_task_struct(pos
);
2202 * Find the next thread in the thread list.
2203 * Return NULL if there is an error or no next thread.
2205 * The reference to the input task_struct is released.
2207 static struct task_struct
*next_tid(struct task_struct
*start
)
2209 struct task_struct
*pos
= NULL
;
2211 if (pid_alive(start
)) {
2212 pos
= next_thread(start
);
2213 if (thread_group_leader(pos
))
2216 get_task_struct(pos
);
2219 put_task_struct(start
);
2223 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2224 struct task_struct
*task
, int tid
)
2226 char name
[PROC_NUMBUF
];
2227 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
2228 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2229 proc_task_instantiate
, task
, NULL
);
2232 /* for the /proc/TGID/task/ directories */
2233 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2235 struct dentry
*dentry
= filp
->f_dentry
;
2236 struct inode
*inode
= dentry
->d_inode
;
2237 struct task_struct
*leader
= get_proc_task(inode
);
2238 struct task_struct
*task
;
2239 int retval
= -ENOENT
;
2242 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2251 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2256 ino
= parent_ino(dentry
);
2257 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2263 /* f_version caches the tgid value that the last readdir call couldn't
2264 * return. lseek aka telldir automagically resets f_version to 0.
2266 tid
= filp
->f_version
;
2267 filp
->f_version
= 0;
2268 for (task
= first_tid(leader
, tid
, pos
- 2);
2270 task
= next_tid(task
), pos
++) {
2272 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
2273 /* returning this tgid failed, save it as the first
2274 * pid for the next readir call */
2275 filp
->f_version
= tid
;
2276 put_task_struct(task
);
2282 put_task_struct(leader
);
2287 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2289 struct inode
*inode
= dentry
->d_inode
;
2290 struct task_struct
*p
= get_proc_task(inode
);
2291 generic_fillattr(inode
, stat
);
2295 stat
->nlink
+= get_nr_threads(p
);
2303 static struct inode_operations proc_task_inode_operations
= {
2304 .lookup
= proc_task_lookup
,
2305 .getattr
= proc_task_getattr
,
2306 .setattr
= proc_setattr
,
2309 static struct file_operations proc_task_operations
= {
2310 .read
= generic_read_dir
,
2311 .readdir
= proc_task_readdir
,