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/config.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/string.h>
61 #include <linux/seq_file.h>
62 #include <linux/namei.h>
63 #include <linux/namespace.h>
65 #include <linux/smp_lock.h>
66 #include <linux/rcupdate.h>
67 #include <linux/kallsyms.h>
68 #include <linux/mount.h>
69 #include <linux/security.h>
70 #include <linux/ptrace.h>
71 #include <linux/seccomp.h>
72 #include <linux/cpuset.h>
73 #include <linux/audit.h>
74 #include <linux/poll.h>
78 * For hysterical raisins we keep the same inumbers as in the old procfs.
79 * Feel free to change the macro below - just keep the range distinct from
80 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
81 * As soon as we'll get a separate superblock we will be able to forget
82 * about magical ranges too.
85 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
87 enum pid_directory_inos
{
111 #ifdef CONFIG_SCHEDSTATS
114 #ifdef CONFIG_CPUSETS
117 #ifdef CONFIG_SECURITY
119 PROC_TGID_ATTR_CURRENT
,
122 PROC_TGID_ATTR_FSCREATE
,
124 #ifdef CONFIG_AUDITSYSCALL
128 PROC_TGID_OOM_ADJUST
,
132 #ifdef CONFIG_SECCOMP
151 #ifdef CONFIG_SCHEDSTATS
154 #ifdef CONFIG_CPUSETS
157 #ifdef CONFIG_SECURITY
159 PROC_TID_ATTR_CURRENT
,
162 PROC_TID_ATTR_FSCREATE
,
164 #ifdef CONFIG_AUDITSYSCALL
170 /* Add new entries before this */
171 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
181 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
183 static struct pid_entry tgid_base_stuff
[] = {
184 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
185 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
186 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
187 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
188 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
189 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
190 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
191 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
192 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
194 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
196 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
197 #ifdef CONFIG_SECCOMP
198 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
200 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
201 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
202 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
203 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
205 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
207 #ifdef CONFIG_SECURITY
208 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
210 #ifdef CONFIG_KALLSYMS
211 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
213 #ifdef CONFIG_SCHEDSTATS
214 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
216 #ifdef CONFIG_CPUSETS
217 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
219 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
220 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
221 #ifdef CONFIG_AUDITSYSCALL
222 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
226 static struct pid_entry tid_base_stuff
[] = {
227 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
228 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
229 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
230 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
231 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
232 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
233 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
234 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
236 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
238 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
239 #ifdef CONFIG_SECCOMP
240 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
242 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
243 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
244 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
245 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
247 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
249 #ifdef CONFIG_SECURITY
250 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
252 #ifdef CONFIG_KALLSYMS
253 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
255 #ifdef CONFIG_SCHEDSTATS
256 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
258 #ifdef CONFIG_CPUSETS
259 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
261 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
262 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
263 #ifdef CONFIG_AUDITSYSCALL
264 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
269 #ifdef CONFIG_SECURITY
270 static struct pid_entry tgid_attr_stuff
[] = {
271 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
272 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
273 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
274 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
277 static struct pid_entry tid_attr_stuff
[] = {
278 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
279 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
280 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
281 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
288 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
290 struct task_struct
*task
= proc_task(inode
);
291 struct files_struct
*files
;
293 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
295 files
= get_files_struct(task
);
298 * We are not taking a ref to the file structure, so we must
301 spin_lock(&files
->file_lock
);
302 file
= fcheck_files(files
, fd
);
304 *mnt
= mntget(file
->f_vfsmnt
);
305 *dentry
= dget(file
->f_dentry
);
306 spin_unlock(&files
->file_lock
);
307 put_files_struct(files
);
310 spin_unlock(&files
->file_lock
);
311 put_files_struct(files
);
316 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
318 struct fs_struct
*fs
;
322 atomic_inc(&fs
->count
);
327 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
329 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
330 int result
= -ENOENT
;
332 read_lock(&fs
->lock
);
333 *mnt
= mntget(fs
->pwdmnt
);
334 *dentry
= dget(fs
->pwd
);
335 read_unlock(&fs
->lock
);
342 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
344 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
345 int result
= -ENOENT
;
347 read_lock(&fs
->lock
);
348 *mnt
= mntget(fs
->rootmnt
);
349 *dentry
= dget(fs
->root
);
350 read_unlock(&fs
->lock
);
358 /* Same as proc_root_link, but this addionally tries to get fs from other
359 * threads in the group */
360 static int proc_task_root_link(struct inode
*inode
, struct dentry
**dentry
,
361 struct vfsmount
**mnt
)
363 struct fs_struct
*fs
;
364 int result
= -ENOENT
;
365 struct task_struct
*leader
= proc_task(inode
);
370 atomic_inc(&fs
->count
);
373 /* Try to get fs from other threads */
375 read_lock(&tasklist_lock
);
376 if (pid_alive(leader
)) {
377 struct task_struct
*task
= leader
;
379 while ((task
= next_thread(task
)) != leader
) {
383 atomic_inc(&fs
->count
);
390 read_unlock(&tasklist_lock
);
394 read_lock(&fs
->lock
);
395 *mnt
= mntget(fs
->rootmnt
);
396 *dentry
= dget(fs
->root
);
397 read_unlock(&fs
->lock
);
405 #define MAY_PTRACE(task) \
406 (task == current || \
407 (task->parent == current && \
408 (task->ptrace & PT_PTRACED) && \
409 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
410 security_ptrace(current,task) == 0))
412 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
415 struct mm_struct
*mm
= get_task_mm(task
);
417 unsigned int len
= mm
->env_end
- mm
->env_start
;
420 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
421 if (!ptrace_may_attach(task
))
428 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
432 struct mm_struct
*mm
= get_task_mm(task
);
436 goto out_mm
; /* Shh! No looking before we're done */
438 len
= mm
->arg_end
- mm
->arg_start
;
443 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
445 // If the nul at the end of args has been overwritten, then
446 // assume application is using setproctitle(3).
447 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
448 len
= strnlen(buffer
, res
);
452 len
= mm
->env_end
- mm
->env_start
;
453 if (len
> PAGE_SIZE
- res
)
454 len
= PAGE_SIZE
- res
;
455 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
456 res
= strnlen(buffer
, res
);
465 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
468 struct mm_struct
*mm
= get_task_mm(task
);
470 unsigned int nwords
= 0;
473 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
474 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
477 memcpy(buffer
, mm
->saved_auxv
, res
);
484 #ifdef CONFIG_KALLSYMS
486 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
487 * Returns the resolved symbol. If that fails, simply return the address.
489 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
492 const char *sym_name
;
493 unsigned long wchan
, size
, offset
;
494 char namebuf
[KSYM_NAME_LEN
+1];
496 wchan
= get_wchan(task
);
498 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
500 return sprintf(buffer
, "%s", sym_name
);
501 return sprintf(buffer
, "%lu", wchan
);
503 #endif /* CONFIG_KALLSYMS */
505 #ifdef CONFIG_SCHEDSTATS
507 * Provides /proc/PID/schedstat
509 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
511 return sprintf(buffer
, "%lu %lu %lu\n",
512 task
->sched_info
.cpu_time
,
513 task
->sched_info
.run_delay
,
514 task
->sched_info
.pcnt
);
518 /* The badness from the OOM killer */
519 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
520 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
522 unsigned long points
;
523 struct timespec uptime
;
525 do_posix_clock_monotonic_gettime(&uptime
);
526 points
= badness(task
, uptime
.tv_sec
);
527 return sprintf(buffer
, "%lu\n", points
);
530 /************************************************************************/
531 /* Here the fs part begins */
532 /************************************************************************/
534 /* permission checks */
536 /* If the process being read is separated by chroot from the reading process,
537 * don't let the reader access the threads.
539 static int proc_check_chroot(struct dentry
*root
, struct vfsmount
*vfsmnt
)
541 struct dentry
*de
, *base
;
542 struct vfsmount
*our_vfsmnt
, *mnt
;
544 read_lock(¤t
->fs
->lock
);
545 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
546 base
= dget(current
->fs
->root
);
547 read_unlock(¤t
->fs
->lock
);
549 spin_lock(&vfsmount_lock
);
553 while (vfsmnt
!= our_vfsmnt
) {
554 if (vfsmnt
== vfsmnt
->mnt_parent
)
556 de
= vfsmnt
->mnt_mountpoint
;
557 vfsmnt
= vfsmnt
->mnt_parent
;
560 if (!is_subdir(de
, base
))
562 spin_unlock(&vfsmount_lock
);
571 spin_unlock(&vfsmount_lock
);
576 static int proc_check_root(struct inode
*inode
)
579 struct vfsmount
*vfsmnt
;
581 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
583 return proc_check_chroot(root
, vfsmnt
);
586 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
588 if (generic_permission(inode
, mask
, NULL
) != 0)
590 return proc_check_root(inode
);
593 static int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
596 struct inode
*inode
= dentry
->d_inode
;
598 if (attr
->ia_valid
& ATTR_MODE
)
601 error
= inode_change_ok(inode
, attr
);
603 error
= security_inode_setattr(dentry
, attr
);
605 error
= inode_setattr(inode
, attr
);
610 static struct inode_operations proc_def_inode_operations
= {
611 .setattr
= proc_setattr
,
614 static int proc_task_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
617 struct vfsmount
*vfsmnt
;
619 if (generic_permission(inode
, mask
, NULL
) != 0)
622 if (proc_task_root_link(inode
, &root
, &vfsmnt
))
625 return proc_check_chroot(root
, vfsmnt
);
628 extern struct seq_operations proc_pid_maps_op
;
629 static int maps_open(struct inode
*inode
, struct file
*file
)
631 struct task_struct
*task
= proc_task(inode
);
632 int ret
= seq_open(file
, &proc_pid_maps_op
);
634 struct seq_file
*m
= file
->private_data
;
640 static struct file_operations proc_maps_operations
= {
644 .release
= seq_release
,
648 extern struct seq_operations proc_pid_numa_maps_op
;
649 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
651 struct task_struct
*task
= proc_task(inode
);
652 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
654 struct seq_file
*m
= file
->private_data
;
660 static struct file_operations proc_numa_maps_operations
= {
661 .open
= numa_maps_open
,
664 .release
= seq_release
,
669 extern struct seq_operations proc_pid_smaps_op
;
670 static int smaps_open(struct inode
*inode
, struct file
*file
)
672 struct task_struct
*task
= proc_task(inode
);
673 int ret
= seq_open(file
, &proc_pid_smaps_op
);
675 struct seq_file
*m
= file
->private_data
;
681 static struct file_operations proc_smaps_operations
= {
685 .release
= seq_release
,
689 extern struct seq_operations mounts_op
;
695 static int mounts_open(struct inode
*inode
, struct file
*file
)
697 struct task_struct
*task
= proc_task(inode
);
698 struct namespace *namespace;
699 struct proc_mounts
*p
;
703 namespace = task
->namespace;
705 get_namespace(namespace);
710 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
712 file
->private_data
= &p
->m
;
713 ret
= seq_open(file
, &mounts_op
);
715 p
->m
.private = namespace;
716 p
->event
= namespace->event
;
721 put_namespace(namespace);
726 static int mounts_release(struct inode
*inode
, struct file
*file
)
728 struct seq_file
*m
= file
->private_data
;
729 struct namespace *namespace = m
->private;
730 put_namespace(namespace);
731 return seq_release(inode
, file
);
734 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
736 struct proc_mounts
*p
= file
->private_data
;
737 struct namespace *ns
= p
->m
.private;
740 poll_wait(file
, &ns
->poll
, wait
);
742 spin_lock(&vfsmount_lock
);
743 if (p
->event
!= ns
->event
) {
744 p
->event
= ns
->event
;
747 spin_unlock(&vfsmount_lock
);
752 static struct file_operations proc_mounts_operations
= {
756 .release
= mounts_release
,
760 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
762 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
763 size_t count
, loff_t
*ppos
)
765 struct inode
* inode
= file
->f_dentry
->d_inode
;
768 struct task_struct
*task
= proc_task(inode
);
770 if (count
> PROC_BLOCK_SIZE
)
771 count
= PROC_BLOCK_SIZE
;
772 if (!(page
= __get_free_page(GFP_KERNEL
)))
775 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
778 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
783 static struct file_operations proc_info_file_operations
= {
784 .read
= proc_info_read
,
787 static int mem_open(struct inode
* inode
, struct file
* file
)
789 file
->private_data
= (void*)((long)current
->self_exec_id
);
793 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
794 size_t count
, loff_t
*ppos
)
796 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
798 unsigned long src
= *ppos
;
800 struct mm_struct
*mm
;
802 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
806 page
= (char *)__get_free_page(GFP_USER
);
812 mm
= get_task_mm(task
);
818 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
824 int this_len
, retval
;
826 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
827 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
828 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
834 if (copy_to_user(buf
, page
, retval
)) {
849 free_page((unsigned long) page
);
854 #define mem_write NULL
857 /* This is a security hazard */
858 static ssize_t
mem_write(struct file
* file
, const char * buf
,
859 size_t count
, loff_t
*ppos
)
863 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
864 unsigned long dst
= *ppos
;
866 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
869 page
= (char *)__get_free_page(GFP_USER
);
874 int this_len
, retval
;
876 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
877 if (copy_from_user(page
, buf
, this_len
)) {
881 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
893 free_page((unsigned long) page
);
898 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
902 file
->f_pos
= offset
;
905 file
->f_pos
+= offset
;
910 force_successful_syscall_return();
914 static struct file_operations proc_mem_operations
= {
921 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
922 size_t count
, loff_t
*ppos
)
924 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
927 int oom_adjust
= task
->oomkilladj
;
928 loff_t __ppos
= *ppos
;
930 len
= sprintf(buffer
, "%i\n", oom_adjust
);
933 if (count
> len
-__ppos
)
935 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
937 *ppos
= __ppos
+ count
;
941 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
942 size_t count
, loff_t
*ppos
)
944 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
945 char buffer
[8], *end
;
948 if (!capable(CAP_SYS_RESOURCE
))
950 memset(buffer
, 0, 8);
953 if (copy_from_user(buffer
, buf
, count
))
955 oom_adjust
= simple_strtol(buffer
, &end
, 0);
956 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
960 task
->oomkilladj
= oom_adjust
;
961 if (end
- buffer
== 0)
966 static struct file_operations proc_oom_adjust_operations
= {
967 .read
= oom_adjust_read
,
968 .write
= oom_adjust_write
,
971 static struct inode_operations proc_mem_inode_operations
= {
972 .permission
= proc_permission
,
973 .setattr
= proc_setattr
,
976 #ifdef CONFIG_AUDITSYSCALL
978 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
979 size_t count
, loff_t
*ppos
)
981 struct inode
* inode
= file
->f_dentry
->d_inode
;
982 struct task_struct
*task
= proc_task(inode
);
984 char tmpbuf
[TMPBUFLEN
];
986 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
987 audit_get_loginuid(task
->audit_context
));
988 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
991 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
992 size_t count
, loff_t
*ppos
)
994 struct inode
* inode
= file
->f_dentry
->d_inode
;
997 struct task_struct
*task
= proc_task(inode
);
1000 if (!capable(CAP_AUDIT_CONTROL
))
1003 if (current
!= task
)
1006 if (count
> PAGE_SIZE
)
1010 /* No partial writes. */
1013 page
= (char*)__get_free_page(GFP_USER
);
1017 if (copy_from_user(page
, buf
, count
))
1020 loginuid
= simple_strtoul(page
, &tmp
, 10);
1026 length
= audit_set_loginuid(task
, loginuid
);
1027 if (likely(length
== 0))
1031 free_page((unsigned long) page
);
1035 static struct file_operations proc_loginuid_operations
= {
1036 .read
= proc_loginuid_read
,
1037 .write
= proc_loginuid_write
,
1041 #ifdef CONFIG_SECCOMP
1042 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
1043 size_t count
, loff_t
*ppos
)
1045 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1047 loff_t __ppos
= *ppos
;
1050 /* no need to print the trailing zero, so use only len */
1051 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
1054 if (count
> len
- __ppos
)
1055 count
= len
- __ppos
;
1056 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
1058 *ppos
= __ppos
+ count
;
1062 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
1063 size_t count
, loff_t
*ppos
)
1065 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1066 char __buf
[20], *end
;
1067 unsigned int seccomp_mode
;
1069 /* can set it only once to be even more secure */
1070 if (unlikely(tsk
->seccomp
.mode
))
1073 memset(__buf
, 0, sizeof(__buf
));
1074 count
= min(count
, sizeof(__buf
) - 1);
1075 if (copy_from_user(__buf
, buf
, count
))
1077 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
1080 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
1081 tsk
->seccomp
.mode
= seccomp_mode
;
1082 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
1085 if (unlikely(!(end
- __buf
)))
1090 static struct file_operations proc_seccomp_operations
= {
1091 .read
= seccomp_read
,
1092 .write
= seccomp_write
,
1094 #endif /* CONFIG_SECCOMP */
1096 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1098 struct inode
*inode
= dentry
->d_inode
;
1099 int error
= -EACCES
;
1101 /* We don't need a base pointer in the /proc filesystem */
1104 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1106 error
= proc_check_root(inode
);
1110 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1111 nd
->last_type
= LAST_BIND
;
1113 return ERR_PTR(error
);
1116 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1117 char __user
*buffer
, int buflen
)
1119 struct inode
* inode
;
1120 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1126 inode
= dentry
->d_inode
;
1127 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1128 len
= PTR_ERR(path
);
1131 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1135 if (copy_to_user(buffer
, path
, len
))
1138 free_page((unsigned long)tmp
);
1142 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1144 int error
= -EACCES
;
1145 struct inode
*inode
= dentry
->d_inode
;
1147 struct vfsmount
*mnt
= NULL
;
1151 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1153 error
= proc_check_root(inode
);
1157 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1161 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1169 static struct inode_operations proc_pid_link_inode_operations
= {
1170 .readlink
= proc_pid_readlink
,
1171 .follow_link
= proc_pid_follow_link
,
1172 .setattr
= proc_setattr
,
1177 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1179 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1180 struct task_struct
*p
= proc_task(inode
);
1181 unsigned int fd
, tid
, ino
;
1184 struct files_struct
* files
;
1185 struct fdtable
*fdt
;
1196 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1200 ino
= fake_ino(tid
, PROC_TID_INO
);
1201 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1205 files
= get_files_struct(p
);
1209 fdt
= files_fdtable(files
);
1210 for (fd
= filp
->f_pos
-2;
1212 fd
++, filp
->f_pos
++) {
1215 if (!fcheck_files(files
, fd
))
1223 buf
[j
] = '0' + (i
% 10);
1227 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1228 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1235 put_files_struct(files
);
1241 static int proc_pident_readdir(struct file
*filp
,
1242 void *dirent
, filldir_t filldir
,
1243 struct pid_entry
*ents
, unsigned int nents
)
1247 struct dentry
*dentry
= filp
->f_dentry
;
1248 struct inode
*inode
= dentry
->d_inode
;
1249 struct pid_entry
*p
;
1254 if (!pid_alive(proc_task(inode
)))
1258 pid
= proc_task(inode
)->pid
;
1263 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1269 ino
= parent_ino(dentry
);
1270 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1283 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1284 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1296 static int proc_tgid_base_readdir(struct file
* filp
,
1297 void * dirent
, filldir_t filldir
)
1299 return proc_pident_readdir(filp
,dirent
,filldir
,
1300 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1303 static int proc_tid_base_readdir(struct file
* filp
,
1304 void * dirent
, filldir_t filldir
)
1306 return proc_pident_readdir(filp
,dirent
,filldir
,
1307 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1310 /* building an inode */
1312 static int task_dumpable(struct task_struct
*task
)
1315 struct mm_struct
*mm
;
1320 dumpable
= mm
->dumpable
;
1328 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1330 struct inode
* inode
;
1331 struct proc_inode
*ei
;
1333 /* We need a new inode */
1335 inode
= new_inode(sb
);
1342 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1343 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1344 inode
->i_op
= &proc_def_inode_operations
;
1346 if (!pid_alive(task
))
1350 * grab the reference to task.
1352 get_task_struct(task
);
1357 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1358 inode
->i_uid
= task
->euid
;
1359 inode
->i_gid
= task
->egid
;
1361 security_task_to_inode(task
, inode
);
1375 * Exceptional case: normally we are not allowed to unhash a busy
1376 * directory. In this case, however, we can do it - no aliasing problems
1377 * due to the way we treat inodes.
1379 * Rewrite the inode's ownerships here because the owning task may have
1380 * performed a setuid(), etc.
1382 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1384 struct inode
*inode
= dentry
->d_inode
;
1385 struct task_struct
*task
= proc_task(inode
);
1386 if (pid_alive(task
)) {
1387 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1388 inode
->i_uid
= task
->euid
;
1389 inode
->i_gid
= task
->egid
;
1394 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1395 security_task_to_inode(task
, inode
);
1402 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1404 struct inode
*inode
= dentry
->d_inode
;
1405 struct task_struct
*task
= proc_task(inode
);
1406 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1407 struct files_struct
*files
;
1409 files
= get_files_struct(task
);
1412 if (fcheck_files(files
, fd
)) {
1414 put_files_struct(files
);
1415 if (task_dumpable(task
)) {
1416 inode
->i_uid
= task
->euid
;
1417 inode
->i_gid
= task
->egid
;
1422 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1423 security_task_to_inode(task
, inode
);
1427 put_files_struct(files
);
1433 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1435 struct task_struct
*task
= proc_task(inode
);
1436 spin_lock(&task
->proc_lock
);
1437 if (task
->proc_dentry
== dentry
)
1438 task
->proc_dentry
= NULL
;
1439 spin_unlock(&task
->proc_lock
);
1443 static int pid_delete_dentry(struct dentry
* dentry
)
1445 /* Is the task we represent dead?
1446 * If so, then don't put the dentry on the lru list,
1447 * kill it immediately.
1449 return !pid_alive(proc_task(dentry
->d_inode
));
1452 static struct dentry_operations tid_fd_dentry_operations
=
1454 .d_revalidate
= tid_fd_revalidate
,
1455 .d_delete
= pid_delete_dentry
,
1458 static struct dentry_operations pid_dentry_operations
=
1460 .d_revalidate
= pid_revalidate
,
1461 .d_delete
= pid_delete_dentry
,
1464 static struct dentry_operations pid_base_dentry_operations
=
1466 .d_revalidate
= pid_revalidate
,
1467 .d_iput
= pid_base_iput
,
1468 .d_delete
= pid_delete_dentry
,
1473 static unsigned name_to_int(struct dentry
*dentry
)
1475 const char *name
= dentry
->d_name
.name
;
1476 int len
= dentry
->d_name
.len
;
1479 if (len
> 1 && *name
== '0')
1482 unsigned c
= *name
++ - '0';
1485 if (n
>= (~0U-9)/10)
1496 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1498 struct task_struct
*task
= proc_task(dir
);
1499 unsigned fd
= name_to_int(dentry
);
1501 struct files_struct
* files
;
1502 struct inode
*inode
;
1503 struct proc_inode
*ei
;
1507 if (!pid_alive(task
))
1510 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1514 files
= get_files_struct(task
);
1517 inode
->i_mode
= S_IFLNK
;
1520 * We are not taking a ref to the file structure, so we must
1523 spin_lock(&files
->file_lock
);
1524 file
= fcheck_files(files
, fd
);
1527 if (file
->f_mode
& 1)
1528 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1529 if (file
->f_mode
& 2)
1530 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1531 spin_unlock(&files
->file_lock
);
1532 put_files_struct(files
);
1533 inode
->i_op
= &proc_pid_link_inode_operations
;
1535 ei
->op
.proc_get_link
= proc_fd_link
;
1536 dentry
->d_op
= &tid_fd_dentry_operations
;
1537 d_add(dentry
, inode
);
1541 spin_unlock(&files
->file_lock
);
1542 put_files_struct(files
);
1546 return ERR_PTR(-ENOENT
);
1549 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1550 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1552 static struct file_operations proc_fd_operations
= {
1553 .read
= generic_read_dir
,
1554 .readdir
= proc_readfd
,
1557 static struct file_operations proc_task_operations
= {
1558 .read
= generic_read_dir
,
1559 .readdir
= proc_task_readdir
,
1563 * proc directories can do almost nothing..
1565 static struct inode_operations proc_fd_inode_operations
= {
1566 .lookup
= proc_lookupfd
,
1567 .permission
= proc_permission
,
1568 .setattr
= proc_setattr
,
1571 static struct inode_operations proc_task_inode_operations
= {
1572 .lookup
= proc_task_lookup
,
1573 .permission
= proc_task_permission
,
1574 .setattr
= proc_setattr
,
1577 #ifdef CONFIG_SECURITY
1578 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1579 size_t count
, loff_t
*ppos
)
1581 struct inode
* inode
= file
->f_dentry
->d_inode
;
1584 struct task_struct
*task
= proc_task(inode
);
1586 if (count
> PAGE_SIZE
)
1588 if (!(page
= __get_free_page(GFP_KERNEL
)))
1591 length
= security_getprocattr(task
,
1592 (char*)file
->f_dentry
->d_name
.name
,
1593 (void*)page
, count
);
1595 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1600 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1601 size_t count
, loff_t
*ppos
)
1603 struct inode
* inode
= file
->f_dentry
->d_inode
;
1606 struct task_struct
*task
= proc_task(inode
);
1608 if (count
> PAGE_SIZE
)
1611 /* No partial writes. */
1614 page
= (char*)__get_free_page(GFP_USER
);
1618 if (copy_from_user(page
, buf
, count
))
1621 length
= security_setprocattr(task
,
1622 (char*)file
->f_dentry
->d_name
.name
,
1623 (void*)page
, count
);
1625 free_page((unsigned long) page
);
1629 static struct file_operations proc_pid_attr_operations
= {
1630 .read
= proc_pid_attr_read
,
1631 .write
= proc_pid_attr_write
,
1634 static struct file_operations proc_tid_attr_operations
;
1635 static struct inode_operations proc_tid_attr_inode_operations
;
1636 static struct file_operations proc_tgid_attr_operations
;
1637 static struct inode_operations proc_tgid_attr_inode_operations
;
1640 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1643 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1644 struct dentry
*dentry
,
1645 struct pid_entry
*ents
)
1647 struct inode
*inode
;
1649 struct task_struct
*task
= proc_task(dir
);
1650 struct pid_entry
*p
;
1651 struct proc_inode
*ei
;
1656 if (!pid_alive(task
))
1659 for (p
= ents
; p
->name
; p
++) {
1660 if (p
->len
!= dentry
->d_name
.len
)
1662 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1669 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1674 inode
->i_mode
= p
->mode
;
1676 * Yes, it does not scale. And it should not. Don't add
1677 * new entries into /proc/<tgid>/ without very good reasons.
1680 case PROC_TGID_TASK
:
1681 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1682 inode
->i_op
= &proc_task_inode_operations
;
1683 inode
->i_fop
= &proc_task_operations
;
1688 inode
->i_op
= &proc_fd_inode_operations
;
1689 inode
->i_fop
= &proc_fd_operations
;
1693 inode
->i_op
= &proc_pid_link_inode_operations
;
1694 ei
->op
.proc_get_link
= proc_exe_link
;
1698 inode
->i_op
= &proc_pid_link_inode_operations
;
1699 ei
->op
.proc_get_link
= proc_cwd_link
;
1702 case PROC_TGID_ROOT
:
1703 inode
->i_op
= &proc_pid_link_inode_operations
;
1704 ei
->op
.proc_get_link
= proc_root_link
;
1706 case PROC_TID_ENVIRON
:
1707 case PROC_TGID_ENVIRON
:
1708 inode
->i_fop
= &proc_info_file_operations
;
1709 ei
->op
.proc_read
= proc_pid_environ
;
1712 case PROC_TGID_AUXV
:
1713 inode
->i_fop
= &proc_info_file_operations
;
1714 ei
->op
.proc_read
= proc_pid_auxv
;
1716 case PROC_TID_STATUS
:
1717 case PROC_TGID_STATUS
:
1718 inode
->i_fop
= &proc_info_file_operations
;
1719 ei
->op
.proc_read
= proc_pid_status
;
1722 inode
->i_fop
= &proc_info_file_operations
;
1723 ei
->op
.proc_read
= proc_tid_stat
;
1725 case PROC_TGID_STAT
:
1726 inode
->i_fop
= &proc_info_file_operations
;
1727 ei
->op
.proc_read
= proc_tgid_stat
;
1729 case PROC_TID_CMDLINE
:
1730 case PROC_TGID_CMDLINE
:
1731 inode
->i_fop
= &proc_info_file_operations
;
1732 ei
->op
.proc_read
= proc_pid_cmdline
;
1734 case PROC_TID_STATM
:
1735 case PROC_TGID_STATM
:
1736 inode
->i_fop
= &proc_info_file_operations
;
1737 ei
->op
.proc_read
= proc_pid_statm
;
1740 case PROC_TGID_MAPS
:
1741 inode
->i_fop
= &proc_maps_operations
;
1744 case PROC_TID_NUMA_MAPS
:
1745 case PROC_TGID_NUMA_MAPS
:
1746 inode
->i_fop
= &proc_numa_maps_operations
;
1751 inode
->i_op
= &proc_mem_inode_operations
;
1752 inode
->i_fop
= &proc_mem_operations
;
1754 #ifdef CONFIG_SECCOMP
1755 case PROC_TID_SECCOMP
:
1756 case PROC_TGID_SECCOMP
:
1757 inode
->i_fop
= &proc_seccomp_operations
;
1759 #endif /* CONFIG_SECCOMP */
1760 case PROC_TID_MOUNTS
:
1761 case PROC_TGID_MOUNTS
:
1762 inode
->i_fop
= &proc_mounts_operations
;
1765 case PROC_TID_SMAPS
:
1766 case PROC_TGID_SMAPS
:
1767 inode
->i_fop
= &proc_smaps_operations
;
1770 #ifdef CONFIG_SECURITY
1773 inode
->i_op
= &proc_tid_attr_inode_operations
;
1774 inode
->i_fop
= &proc_tid_attr_operations
;
1776 case PROC_TGID_ATTR
:
1778 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1779 inode
->i_fop
= &proc_tgid_attr_operations
;
1781 case PROC_TID_ATTR_CURRENT
:
1782 case PROC_TGID_ATTR_CURRENT
:
1783 case PROC_TID_ATTR_PREV
:
1784 case PROC_TGID_ATTR_PREV
:
1785 case PROC_TID_ATTR_EXEC
:
1786 case PROC_TGID_ATTR_EXEC
:
1787 case PROC_TID_ATTR_FSCREATE
:
1788 case PROC_TGID_ATTR_FSCREATE
:
1789 inode
->i_fop
= &proc_pid_attr_operations
;
1792 #ifdef CONFIG_KALLSYMS
1793 case PROC_TID_WCHAN
:
1794 case PROC_TGID_WCHAN
:
1795 inode
->i_fop
= &proc_info_file_operations
;
1796 ei
->op
.proc_read
= proc_pid_wchan
;
1799 #ifdef CONFIG_SCHEDSTATS
1800 case PROC_TID_SCHEDSTAT
:
1801 case PROC_TGID_SCHEDSTAT
:
1802 inode
->i_fop
= &proc_info_file_operations
;
1803 ei
->op
.proc_read
= proc_pid_schedstat
;
1806 #ifdef CONFIG_CPUSETS
1807 case PROC_TID_CPUSET
:
1808 case PROC_TGID_CPUSET
:
1809 inode
->i_fop
= &proc_cpuset_operations
;
1812 case PROC_TID_OOM_SCORE
:
1813 case PROC_TGID_OOM_SCORE
:
1814 inode
->i_fop
= &proc_info_file_operations
;
1815 ei
->op
.proc_read
= proc_oom_score
;
1817 case PROC_TID_OOM_ADJUST
:
1818 case PROC_TGID_OOM_ADJUST
:
1819 inode
->i_fop
= &proc_oom_adjust_operations
;
1821 #ifdef CONFIG_AUDITSYSCALL
1822 case PROC_TID_LOGINUID
:
1823 case PROC_TGID_LOGINUID
:
1824 inode
->i_fop
= &proc_loginuid_operations
;
1828 printk("procfs: impossible type (%d)",p
->type
);
1830 return ERR_PTR(-EINVAL
);
1832 dentry
->d_op
= &pid_dentry_operations
;
1833 d_add(dentry
, inode
);
1837 return ERR_PTR(error
);
1840 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1841 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1844 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1845 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1848 static struct file_operations proc_tgid_base_operations
= {
1849 .read
= generic_read_dir
,
1850 .readdir
= proc_tgid_base_readdir
,
1853 static struct file_operations proc_tid_base_operations
= {
1854 .read
= generic_read_dir
,
1855 .readdir
= proc_tid_base_readdir
,
1858 static struct inode_operations proc_tgid_base_inode_operations
= {
1859 .lookup
= proc_tgid_base_lookup
,
1860 .setattr
= proc_setattr
,
1863 static struct inode_operations proc_tid_base_inode_operations
= {
1864 .lookup
= proc_tid_base_lookup
,
1865 .setattr
= proc_setattr
,
1868 #ifdef CONFIG_SECURITY
1869 static int proc_tgid_attr_readdir(struct file
* filp
,
1870 void * dirent
, filldir_t filldir
)
1872 return proc_pident_readdir(filp
,dirent
,filldir
,
1873 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1876 static int proc_tid_attr_readdir(struct file
* filp
,
1877 void * dirent
, filldir_t filldir
)
1879 return proc_pident_readdir(filp
,dirent
,filldir
,
1880 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1883 static struct file_operations proc_tgid_attr_operations
= {
1884 .read
= generic_read_dir
,
1885 .readdir
= proc_tgid_attr_readdir
,
1888 static struct file_operations proc_tid_attr_operations
= {
1889 .read
= generic_read_dir
,
1890 .readdir
= proc_tid_attr_readdir
,
1893 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1894 struct dentry
*dentry
, struct nameidata
*nd
)
1896 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1899 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1900 struct dentry
*dentry
, struct nameidata
*nd
)
1902 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1905 static struct inode_operations proc_tgid_attr_inode_operations
= {
1906 .lookup
= proc_tgid_attr_lookup
,
1907 .setattr
= proc_setattr
,
1910 static struct inode_operations proc_tid_attr_inode_operations
= {
1911 .lookup
= proc_tid_attr_lookup
,
1912 .setattr
= proc_setattr
,
1919 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1923 sprintf(tmp
, "%d", current
->tgid
);
1924 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1927 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1930 sprintf(tmp
, "%d", current
->tgid
);
1931 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1934 static struct inode_operations proc_self_inode_operations
= {
1935 .readlink
= proc_self_readlink
,
1936 .follow_link
= proc_self_follow_link
,
1937 .setattr
= proc_setattr
,
1941 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1942 * @p: task that should be flushed.
1944 * Drops the /proc/@pid dcache entry from the hash chains.
1946 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1947 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1948 * if the pid value is immediately reused. This is enforced by
1949 * - caller must acquire spin_lock(p->proc_lock)
1950 * - must be called before detach_pid()
1951 * - proc_pid_lookup acquires proc_lock, and checks that
1952 * the target is not dead by looking at the attach count
1956 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1958 struct dentry
*proc_dentry
;
1960 proc_dentry
= p
->proc_dentry
;
1961 if (proc_dentry
!= NULL
) {
1963 spin_lock(&dcache_lock
);
1964 spin_lock(&proc_dentry
->d_lock
);
1965 if (!d_unhashed(proc_dentry
)) {
1966 dget_locked(proc_dentry
);
1967 __d_drop(proc_dentry
);
1968 spin_unlock(&proc_dentry
->d_lock
);
1970 spin_unlock(&proc_dentry
->d_lock
);
1973 spin_unlock(&dcache_lock
);
1979 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1980 * @proc_dentry: directoy to prune.
1982 * Shrink the /proc directory that was used by the just killed thread.
1985 void proc_pid_flush(struct dentry
*proc_dentry
)
1988 if(proc_dentry
!= NULL
) {
1989 shrink_dcache_parent(proc_dentry
);
1995 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1997 struct task_struct
*task
;
1998 struct inode
*inode
;
1999 struct proc_inode
*ei
;
2003 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
2004 inode
= new_inode(dir
->i_sb
);
2006 return ERR_PTR(-ENOMEM
);
2008 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2009 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
2011 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
2012 inode
->i_uid
= inode
->i_gid
= 0;
2014 inode
->i_op
= &proc_self_inode_operations
;
2015 d_add(dentry
, inode
);
2018 tgid
= name_to_int(dentry
);
2022 read_lock(&tasklist_lock
);
2023 task
= find_task_by_pid(tgid
);
2025 get_task_struct(task
);
2026 read_unlock(&tasklist_lock
);
2030 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
2034 put_task_struct(task
);
2037 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2038 inode
->i_op
= &proc_tgid_base_inode_operations
;
2039 inode
->i_fop
= &proc_tgid_base_operations
;
2040 inode
->i_flags
|=S_IMMUTABLE
;
2041 #ifdef CONFIG_SECURITY
2047 dentry
->d_op
= &pid_base_dentry_operations
;
2050 d_add(dentry
, inode
);
2051 spin_lock(&task
->proc_lock
);
2052 task
->proc_dentry
= dentry
;
2053 if (!pid_alive(task
)) {
2054 dentry
= proc_pid_unhash(task
);
2057 spin_unlock(&task
->proc_lock
);
2059 put_task_struct(task
);
2061 proc_pid_flush(dentry
);
2066 return ERR_PTR(-ENOENT
);
2070 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2072 struct task_struct
*task
;
2073 struct task_struct
*leader
= proc_task(dir
);
2074 struct inode
*inode
;
2077 tid
= name_to_int(dentry
);
2081 read_lock(&tasklist_lock
);
2082 task
= find_task_by_pid(tid
);
2084 get_task_struct(task
);
2085 read_unlock(&tasklist_lock
);
2088 if (leader
->tgid
!= task
->tgid
)
2091 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2096 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2097 inode
->i_op
= &proc_tid_base_inode_operations
;
2098 inode
->i_fop
= &proc_tid_base_operations
;
2099 inode
->i_flags
|=S_IMMUTABLE
;
2100 #ifdef CONFIG_SECURITY
2106 dentry
->d_op
= &pid_base_dentry_operations
;
2108 d_add(dentry
, inode
);
2110 put_task_struct(task
);
2113 put_task_struct(task
);
2115 return ERR_PTR(-ENOENT
);
2118 #define PROC_NUMBUF 10
2119 #define PROC_MAXPIDS 20
2122 * Get a few tgid's to return for filldir - we need to hold the
2123 * tasklist lock while doing this, and we must release it before
2124 * we actually do the filldir itself, so we use a temp buffer..
2126 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
2128 struct task_struct
*p
;
2132 read_lock(&tasklist_lock
);
2135 p
= find_task_by_pid(version
);
2136 if (p
&& !thread_group_leader(p
))
2143 p
= next_task(&init_task
);
2145 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2151 tgids
[nr_tgids
] = tgid
;
2153 if (nr_tgids
>= PROC_MAXPIDS
)
2156 read_unlock(&tasklist_lock
);
2161 * Get a few tid's to return for filldir - we need to hold the
2162 * tasklist lock while doing this, and we must release it before
2163 * we actually do the filldir itself, so we use a temp buffer..
2165 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2167 struct task_struct
*leader_task
= proc_task(dir
);
2168 struct task_struct
*task
= leader_task
;
2172 read_lock(&tasklist_lock
);
2174 * The starting point task (leader_task) might be an already
2175 * unlinked task, which cannot be used to access the task-list
2176 * via next_thread().
2178 if (pid_alive(task
)) do {
2179 int tid
= task
->pid
;
2184 tids
[nr_tids
] = tid
;
2186 if (nr_tids
>= PROC_MAXPIDS
)
2188 } while ((task
= next_thread(task
)) != leader_task
);
2189 read_unlock(&tasklist_lock
);
2193 /* for the /proc/ directory itself, after non-process stuff has been done */
2194 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2196 unsigned int tgid_array
[PROC_MAXPIDS
];
2197 char buf
[PROC_NUMBUF
];
2198 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2199 unsigned int nr_tgids
, i
;
2203 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2204 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2210 /* f_version caches the tgid value that the last readdir call couldn't
2211 * return. lseek aka telldir automagically resets f_version to 0.
2213 next_tgid
= filp
->f_version
;
2214 filp
->f_version
= 0;
2216 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2218 /* no more entries ! */
2223 /* do not use the last found pid, reserve it for next_tgid */
2224 if (nr_tgids
== PROC_MAXPIDS
) {
2226 next_tgid
= tgid_array
[nr_tgids
];
2229 for (i
=0;i
<nr_tgids
;i
++) {
2230 int tgid
= tgid_array
[i
];
2231 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2232 unsigned long j
= PROC_NUMBUF
;
2235 buf
[--j
] = '0' + (tgid
% 10);
2236 while ((tgid
/= 10) != 0);
2238 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2239 /* returning this tgid failed, save it as the first
2240 * pid for the next readir call */
2241 filp
->f_version
= tgid_array
[i
];
2252 /* for the /proc/TGID/task/ directories */
2253 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2255 unsigned int tid_array
[PROC_MAXPIDS
];
2256 char buf
[PROC_NUMBUF
];
2257 unsigned int nr_tids
, i
;
2258 struct dentry
*dentry
= filp
->f_dentry
;
2259 struct inode
*inode
= dentry
->d_inode
;
2260 int retval
= -ENOENT
;
2262 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2264 if (!pid_alive(proc_task(inode
)))
2271 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2276 ino
= parent_ino(dentry
);
2277 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2283 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2284 inode
->i_nlink
= pos
+ nr_tids
;
2286 for (i
= 0; i
< nr_tids
; i
++) {
2287 unsigned long j
= PROC_NUMBUF
;
2288 int tid
= tid_array
[i
];
2290 ino
= fake_ino(tid
,PROC_TID_INO
);
2293 buf
[--j
] = '0' + (tid
% 10);
2294 while ((tid
/= 10) != 0);
2296 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)