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
{
107 PROC_TGID_MOUNTSTATS
,
112 #ifdef CONFIG_SCHEDSTATS
115 #ifdef CONFIG_CPUSETS
118 #ifdef CONFIG_SECURITY
120 PROC_TGID_ATTR_CURRENT
,
123 PROC_TGID_ATTR_FSCREATE
,
125 #ifdef CONFIG_AUDITSYSCALL
129 PROC_TGID_OOM_ADJUST
,
133 #ifdef CONFIG_SECCOMP
153 #ifdef CONFIG_SCHEDSTATS
156 #ifdef CONFIG_CPUSETS
159 #ifdef CONFIG_SECURITY
161 PROC_TID_ATTR_CURRENT
,
164 PROC_TID_ATTR_FSCREATE
,
166 #ifdef CONFIG_AUDITSYSCALL
172 /* Add new entries before this */
173 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
183 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
185 static struct pid_entry tgid_base_stuff
[] = {
186 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
187 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
188 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
189 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
190 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
191 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
192 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
193 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
194 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
196 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
198 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
199 #ifdef CONFIG_SECCOMP
200 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
202 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
203 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
204 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
205 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
206 E(PROC_TGID_MOUNTSTATS
, "mountstats", S_IFREG
|S_IRUSR
),
208 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
210 #ifdef CONFIG_SECURITY
211 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
213 #ifdef CONFIG_KALLSYMS
214 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
216 #ifdef CONFIG_SCHEDSTATS
217 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
219 #ifdef CONFIG_CPUSETS
220 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
222 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
223 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
224 #ifdef CONFIG_AUDITSYSCALL
225 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
229 static struct pid_entry tid_base_stuff
[] = {
230 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
231 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
232 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
233 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
234 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
235 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
236 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
237 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
239 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
241 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
242 #ifdef CONFIG_SECCOMP
243 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
245 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
246 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
247 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
248 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
250 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
252 #ifdef CONFIG_SECURITY
253 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
255 #ifdef CONFIG_KALLSYMS
256 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
258 #ifdef CONFIG_SCHEDSTATS
259 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
261 #ifdef CONFIG_CPUSETS
262 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
264 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
265 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
266 #ifdef CONFIG_AUDITSYSCALL
267 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
272 #ifdef CONFIG_SECURITY
273 static struct pid_entry tgid_attr_stuff
[] = {
274 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
275 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
276 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
277 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
280 static struct pid_entry tid_attr_stuff
[] = {
281 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
282 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
283 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
284 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
291 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
293 struct task_struct
*task
= proc_task(inode
);
294 struct files_struct
*files
;
296 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
298 files
= get_files_struct(task
);
301 * We are not taking a ref to the file structure, so we must
304 spin_lock(&files
->file_lock
);
305 file
= fcheck_files(files
, fd
);
307 *mnt
= mntget(file
->f_vfsmnt
);
308 *dentry
= dget(file
->f_dentry
);
309 spin_unlock(&files
->file_lock
);
310 put_files_struct(files
);
313 spin_unlock(&files
->file_lock
);
314 put_files_struct(files
);
319 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
321 struct fs_struct
*fs
;
325 atomic_inc(&fs
->count
);
330 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
332 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
333 int result
= -ENOENT
;
335 read_lock(&fs
->lock
);
336 *mnt
= mntget(fs
->pwdmnt
);
337 *dentry
= dget(fs
->pwd
);
338 read_unlock(&fs
->lock
);
345 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
347 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
348 int result
= -ENOENT
;
350 read_lock(&fs
->lock
);
351 *mnt
= mntget(fs
->rootmnt
);
352 *dentry
= dget(fs
->root
);
353 read_unlock(&fs
->lock
);
361 /* Same as proc_root_link, but this addionally tries to get fs from other
362 * threads in the group */
363 static int proc_task_root_link(struct inode
*inode
, struct dentry
**dentry
,
364 struct vfsmount
**mnt
)
366 struct fs_struct
*fs
;
367 int result
= -ENOENT
;
368 struct task_struct
*leader
= proc_task(inode
);
373 atomic_inc(&fs
->count
);
376 /* Try to get fs from other threads */
378 read_lock(&tasklist_lock
);
379 if (pid_alive(leader
)) {
380 struct task_struct
*task
= leader
;
382 while ((task
= next_thread(task
)) != leader
) {
386 atomic_inc(&fs
->count
);
393 read_unlock(&tasklist_lock
);
397 read_lock(&fs
->lock
);
398 *mnt
= mntget(fs
->rootmnt
);
399 *dentry
= dget(fs
->root
);
400 read_unlock(&fs
->lock
);
408 #define MAY_PTRACE(task) \
409 (task == current || \
410 (task->parent == current && \
411 (task->ptrace & PT_PTRACED) && \
412 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
413 security_ptrace(current,task) == 0))
415 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
418 struct mm_struct
*mm
= get_task_mm(task
);
420 unsigned int len
= mm
->env_end
- mm
->env_start
;
423 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
424 if (!ptrace_may_attach(task
))
431 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
435 struct mm_struct
*mm
= get_task_mm(task
);
439 goto out_mm
; /* Shh! No looking before we're done */
441 len
= mm
->arg_end
- mm
->arg_start
;
446 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
448 // If the nul at the end of args has been overwritten, then
449 // assume application is using setproctitle(3).
450 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
451 len
= strnlen(buffer
, res
);
455 len
= mm
->env_end
- mm
->env_start
;
456 if (len
> PAGE_SIZE
- res
)
457 len
= PAGE_SIZE
- res
;
458 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
459 res
= strnlen(buffer
, res
);
468 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
471 struct mm_struct
*mm
= get_task_mm(task
);
473 unsigned int nwords
= 0;
476 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
477 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
480 memcpy(buffer
, mm
->saved_auxv
, res
);
487 #ifdef CONFIG_KALLSYMS
489 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
490 * Returns the resolved symbol. If that fails, simply return the address.
492 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
495 const char *sym_name
;
496 unsigned long wchan
, size
, offset
;
497 char namebuf
[KSYM_NAME_LEN
+1];
499 wchan
= get_wchan(task
);
501 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
503 return sprintf(buffer
, "%s", sym_name
);
504 return sprintf(buffer
, "%lu", wchan
);
506 #endif /* CONFIG_KALLSYMS */
508 #ifdef CONFIG_SCHEDSTATS
510 * Provides /proc/PID/schedstat
512 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
514 return sprintf(buffer
, "%lu %lu %lu\n",
515 task
->sched_info
.cpu_time
,
516 task
->sched_info
.run_delay
,
517 task
->sched_info
.pcnt
);
521 /* The badness from the OOM killer */
522 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
523 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
525 unsigned long points
;
526 struct timespec uptime
;
528 do_posix_clock_monotonic_gettime(&uptime
);
529 points
= badness(task
, uptime
.tv_sec
);
530 return sprintf(buffer
, "%lu\n", points
);
533 /************************************************************************/
534 /* Here the fs part begins */
535 /************************************************************************/
537 /* permission checks */
539 /* If the process being read is separated by chroot from the reading process,
540 * don't let the reader access the threads.
542 * note: this does dput(root) and mntput(vfsmnt) on exit.
544 static int proc_check_chroot(struct dentry
*root
, struct vfsmount
*vfsmnt
)
546 struct dentry
*de
, *base
;
547 struct vfsmount
*our_vfsmnt
, *mnt
;
550 read_lock(¤t
->fs
->lock
);
551 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
552 base
= dget(current
->fs
->root
);
553 read_unlock(¤t
->fs
->lock
);
555 spin_lock(&vfsmount_lock
);
559 while (mnt
!= our_vfsmnt
) {
560 if (mnt
== mnt
->mnt_parent
)
562 de
= mnt
->mnt_mountpoint
;
563 mnt
= mnt
->mnt_parent
;
566 if (!is_subdir(de
, base
))
568 spin_unlock(&vfsmount_lock
);
577 spin_unlock(&vfsmount_lock
);
582 static int proc_check_root(struct inode
*inode
)
585 struct vfsmount
*vfsmnt
;
587 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
589 return proc_check_chroot(root
, vfsmnt
);
592 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
594 if (generic_permission(inode
, mask
, NULL
) != 0)
596 return proc_check_root(inode
);
599 static int proc_task_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
602 struct vfsmount
*vfsmnt
;
604 if (generic_permission(inode
, mask
, NULL
) != 0)
607 if (proc_task_root_link(inode
, &root
, &vfsmnt
))
610 return proc_check_chroot(root
, vfsmnt
);
613 extern struct seq_operations proc_pid_maps_op
;
614 static int maps_open(struct inode
*inode
, struct file
*file
)
616 struct task_struct
*task
= proc_task(inode
);
617 int ret
= seq_open(file
, &proc_pid_maps_op
);
619 struct seq_file
*m
= file
->private_data
;
625 static struct file_operations proc_maps_operations
= {
629 .release
= seq_release
,
633 extern struct seq_operations proc_pid_numa_maps_op
;
634 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
636 struct task_struct
*task
= proc_task(inode
);
637 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
639 struct seq_file
*m
= file
->private_data
;
645 static struct file_operations proc_numa_maps_operations
= {
646 .open
= numa_maps_open
,
649 .release
= seq_release
,
654 extern struct seq_operations proc_pid_smaps_op
;
655 static int smaps_open(struct inode
*inode
, struct file
*file
)
657 struct task_struct
*task
= proc_task(inode
);
658 int ret
= seq_open(file
, &proc_pid_smaps_op
);
660 struct seq_file
*m
= file
->private_data
;
666 static struct file_operations proc_smaps_operations
= {
670 .release
= seq_release
,
674 extern struct seq_operations mounts_op
;
680 static int mounts_open(struct inode
*inode
, struct file
*file
)
682 struct task_struct
*task
= proc_task(inode
);
683 struct namespace *namespace;
684 struct proc_mounts
*p
;
688 namespace = task
->namespace;
690 get_namespace(namespace);
695 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
697 file
->private_data
= &p
->m
;
698 ret
= seq_open(file
, &mounts_op
);
700 p
->m
.private = namespace;
701 p
->event
= namespace->event
;
706 put_namespace(namespace);
711 static int mounts_release(struct inode
*inode
, struct file
*file
)
713 struct seq_file
*m
= file
->private_data
;
714 struct namespace *namespace = m
->private;
715 put_namespace(namespace);
716 return seq_release(inode
, file
);
719 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
721 struct proc_mounts
*p
= file
->private_data
;
722 struct namespace *ns
= p
->m
.private;
725 poll_wait(file
, &ns
->poll
, wait
);
727 spin_lock(&vfsmount_lock
);
728 if (p
->event
!= ns
->event
) {
729 p
->event
= ns
->event
;
732 spin_unlock(&vfsmount_lock
);
737 static struct file_operations proc_mounts_operations
= {
741 .release
= mounts_release
,
745 extern struct seq_operations mountstats_op
;
746 static int mountstats_open(struct inode
*inode
, struct file
*file
)
748 struct task_struct
*task
= proc_task(inode
);
749 int ret
= seq_open(file
, &mountstats_op
);
752 struct seq_file
*m
= file
->private_data
;
753 struct namespace *namespace;
755 namespace = task
->namespace;
757 get_namespace(namespace);
761 m
->private = namespace;
763 seq_release(inode
, file
);
770 static struct file_operations proc_mountstats_operations
= {
771 .open
= mountstats_open
,
774 .release
= mounts_release
,
777 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
779 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
780 size_t count
, loff_t
*ppos
)
782 struct inode
* inode
= file
->f_dentry
->d_inode
;
785 struct task_struct
*task
= proc_task(inode
);
787 if (count
> PROC_BLOCK_SIZE
)
788 count
= PROC_BLOCK_SIZE
;
789 if (!(page
= __get_free_page(GFP_KERNEL
)))
792 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
795 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
800 static struct file_operations proc_info_file_operations
= {
801 .read
= proc_info_read
,
804 static int mem_open(struct inode
* inode
, struct file
* file
)
806 file
->private_data
= (void*)((long)current
->self_exec_id
);
810 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
811 size_t count
, loff_t
*ppos
)
813 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
815 unsigned long src
= *ppos
;
817 struct mm_struct
*mm
;
819 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
823 page
= (char *)__get_free_page(GFP_USER
);
829 mm
= get_task_mm(task
);
835 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
841 int this_len
, retval
;
843 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
844 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
845 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
851 if (copy_to_user(buf
, page
, retval
)) {
866 free_page((unsigned long) page
);
871 #define mem_write NULL
874 /* This is a security hazard */
875 static ssize_t
mem_write(struct file
* file
, const char * buf
,
876 size_t count
, loff_t
*ppos
)
880 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
881 unsigned long dst
= *ppos
;
883 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
886 page
= (char *)__get_free_page(GFP_USER
);
891 int this_len
, retval
;
893 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
894 if (copy_from_user(page
, buf
, this_len
)) {
898 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
910 free_page((unsigned long) page
);
915 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
919 file
->f_pos
= offset
;
922 file
->f_pos
+= offset
;
927 force_successful_syscall_return();
931 static struct file_operations proc_mem_operations
= {
938 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
939 size_t count
, loff_t
*ppos
)
941 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
944 int oom_adjust
= task
->oomkilladj
;
945 loff_t __ppos
= *ppos
;
947 len
= sprintf(buffer
, "%i\n", oom_adjust
);
950 if (count
> len
-__ppos
)
952 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
954 *ppos
= __ppos
+ count
;
958 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
959 size_t count
, loff_t
*ppos
)
961 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
962 char buffer
[8], *end
;
965 if (!capable(CAP_SYS_RESOURCE
))
967 memset(buffer
, 0, 8);
970 if (copy_from_user(buffer
, buf
, count
))
972 oom_adjust
= simple_strtol(buffer
, &end
, 0);
973 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
977 task
->oomkilladj
= oom_adjust
;
978 if (end
- buffer
== 0)
983 static struct file_operations proc_oom_adjust_operations
= {
984 .read
= oom_adjust_read
,
985 .write
= oom_adjust_write
,
988 static struct inode_operations proc_mem_inode_operations
= {
989 .permission
= proc_permission
,
992 #ifdef CONFIG_AUDITSYSCALL
994 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
995 size_t count
, loff_t
*ppos
)
997 struct inode
* inode
= file
->f_dentry
->d_inode
;
998 struct task_struct
*task
= proc_task(inode
);
1000 char tmpbuf
[TMPBUFLEN
];
1002 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1003 audit_get_loginuid(task
->audit_context
));
1004 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1007 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1008 size_t count
, loff_t
*ppos
)
1010 struct inode
* inode
= file
->f_dentry
->d_inode
;
1013 struct task_struct
*task
= proc_task(inode
);
1016 if (!capable(CAP_AUDIT_CONTROL
))
1019 if (current
!= task
)
1022 if (count
> PAGE_SIZE
)
1026 /* No partial writes. */
1029 page
= (char*)__get_free_page(GFP_USER
);
1033 if (copy_from_user(page
, buf
, count
))
1036 loginuid
= simple_strtoul(page
, &tmp
, 10);
1042 length
= audit_set_loginuid(task
, loginuid
);
1043 if (likely(length
== 0))
1047 free_page((unsigned long) page
);
1051 static struct file_operations proc_loginuid_operations
= {
1052 .read
= proc_loginuid_read
,
1053 .write
= proc_loginuid_write
,
1057 #ifdef CONFIG_SECCOMP
1058 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
1059 size_t count
, loff_t
*ppos
)
1061 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1063 loff_t __ppos
= *ppos
;
1066 /* no need to print the trailing zero, so use only len */
1067 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
1070 if (count
> len
- __ppos
)
1071 count
= len
- __ppos
;
1072 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
1074 *ppos
= __ppos
+ count
;
1078 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
1079 size_t count
, loff_t
*ppos
)
1081 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1082 char __buf
[20], *end
;
1083 unsigned int seccomp_mode
;
1085 /* can set it only once to be even more secure */
1086 if (unlikely(tsk
->seccomp
.mode
))
1089 memset(__buf
, 0, sizeof(__buf
));
1090 count
= min(count
, sizeof(__buf
) - 1);
1091 if (copy_from_user(__buf
, buf
, count
))
1093 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
1096 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
1097 tsk
->seccomp
.mode
= seccomp_mode
;
1098 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
1101 if (unlikely(!(end
- __buf
)))
1106 static struct file_operations proc_seccomp_operations
= {
1107 .read
= seccomp_read
,
1108 .write
= seccomp_write
,
1110 #endif /* CONFIG_SECCOMP */
1112 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1114 struct inode
*inode
= dentry
->d_inode
;
1115 int error
= -EACCES
;
1117 /* We don't need a base pointer in the /proc filesystem */
1120 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1122 error
= proc_check_root(inode
);
1126 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1127 nd
->last_type
= LAST_BIND
;
1129 return ERR_PTR(error
);
1132 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1133 char __user
*buffer
, int buflen
)
1135 struct inode
* inode
;
1136 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1142 inode
= dentry
->d_inode
;
1143 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1144 len
= PTR_ERR(path
);
1147 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1151 if (copy_to_user(buffer
, path
, len
))
1154 free_page((unsigned long)tmp
);
1158 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1160 int error
= -EACCES
;
1161 struct inode
*inode
= dentry
->d_inode
;
1163 struct vfsmount
*mnt
= NULL
;
1167 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1169 error
= proc_check_root(inode
);
1173 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1177 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1185 static struct inode_operations proc_pid_link_inode_operations
= {
1186 .readlink
= proc_pid_readlink
,
1187 .follow_link
= proc_pid_follow_link
1192 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1194 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1195 struct task_struct
*p
= proc_task(inode
);
1196 unsigned int fd
, tid
, ino
;
1199 struct files_struct
* files
;
1200 struct fdtable
*fdt
;
1211 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1215 ino
= fake_ino(tid
, PROC_TID_INO
);
1216 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1220 files
= get_files_struct(p
);
1224 fdt
= files_fdtable(files
);
1225 for (fd
= filp
->f_pos
-2;
1227 fd
++, filp
->f_pos
++) {
1230 if (!fcheck_files(files
, fd
))
1238 buf
[j
] = '0' + (i
% 10);
1242 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1243 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1250 put_files_struct(files
);
1256 static int proc_pident_readdir(struct file
*filp
,
1257 void *dirent
, filldir_t filldir
,
1258 struct pid_entry
*ents
, unsigned int nents
)
1262 struct dentry
*dentry
= filp
->f_dentry
;
1263 struct inode
*inode
= dentry
->d_inode
;
1264 struct pid_entry
*p
;
1269 if (!pid_alive(proc_task(inode
)))
1273 pid
= proc_task(inode
)->pid
;
1278 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1284 ino
= parent_ino(dentry
);
1285 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1298 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1299 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1311 static int proc_tgid_base_readdir(struct file
* filp
,
1312 void * dirent
, filldir_t filldir
)
1314 return proc_pident_readdir(filp
,dirent
,filldir
,
1315 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1318 static int proc_tid_base_readdir(struct file
* filp
,
1319 void * dirent
, filldir_t filldir
)
1321 return proc_pident_readdir(filp
,dirent
,filldir
,
1322 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1325 /* building an inode */
1327 static int task_dumpable(struct task_struct
*task
)
1330 struct mm_struct
*mm
;
1335 dumpable
= mm
->dumpable
;
1343 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1345 struct inode
* inode
;
1346 struct proc_inode
*ei
;
1348 /* We need a new inode */
1350 inode
= new_inode(sb
);
1357 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1358 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1360 if (!pid_alive(task
))
1364 * grab the reference to task.
1366 get_task_struct(task
);
1371 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1372 inode
->i_uid
= task
->euid
;
1373 inode
->i_gid
= task
->egid
;
1375 security_task_to_inode(task
, inode
);
1389 * Exceptional case: normally we are not allowed to unhash a busy
1390 * directory. In this case, however, we can do it - no aliasing problems
1391 * due to the way we treat inodes.
1393 * Rewrite the inode's ownerships here because the owning task may have
1394 * performed a setuid(), etc.
1396 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1398 struct inode
*inode
= dentry
->d_inode
;
1399 struct task_struct
*task
= proc_task(inode
);
1400 if (pid_alive(task
)) {
1401 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1402 inode
->i_uid
= task
->euid
;
1403 inode
->i_gid
= task
->egid
;
1408 security_task_to_inode(task
, inode
);
1415 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1417 struct inode
*inode
= dentry
->d_inode
;
1418 struct task_struct
*task
= proc_task(inode
);
1419 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1420 struct files_struct
*files
;
1422 files
= get_files_struct(task
);
1425 if (fcheck_files(files
, fd
)) {
1427 put_files_struct(files
);
1428 if (task_dumpable(task
)) {
1429 inode
->i_uid
= task
->euid
;
1430 inode
->i_gid
= task
->egid
;
1435 security_task_to_inode(task
, inode
);
1439 put_files_struct(files
);
1445 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1447 struct task_struct
*task
= proc_task(inode
);
1448 spin_lock(&task
->proc_lock
);
1449 if (task
->proc_dentry
== dentry
)
1450 task
->proc_dentry
= NULL
;
1451 spin_unlock(&task
->proc_lock
);
1455 static int pid_delete_dentry(struct dentry
* dentry
)
1457 /* Is the task we represent dead?
1458 * If so, then don't put the dentry on the lru list,
1459 * kill it immediately.
1461 return !pid_alive(proc_task(dentry
->d_inode
));
1464 static struct dentry_operations tid_fd_dentry_operations
=
1466 .d_revalidate
= tid_fd_revalidate
,
1467 .d_delete
= pid_delete_dentry
,
1470 static struct dentry_operations pid_dentry_operations
=
1472 .d_revalidate
= pid_revalidate
,
1473 .d_delete
= pid_delete_dentry
,
1476 static struct dentry_operations pid_base_dentry_operations
=
1478 .d_revalidate
= pid_revalidate
,
1479 .d_iput
= pid_base_iput
,
1480 .d_delete
= pid_delete_dentry
,
1485 static unsigned name_to_int(struct dentry
*dentry
)
1487 const char *name
= dentry
->d_name
.name
;
1488 int len
= dentry
->d_name
.len
;
1491 if (len
> 1 && *name
== '0')
1494 unsigned c
= *name
++ - '0';
1497 if (n
>= (~0U-9)/10)
1508 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1510 struct task_struct
*task
= proc_task(dir
);
1511 unsigned fd
= name_to_int(dentry
);
1513 struct files_struct
* files
;
1514 struct inode
*inode
;
1515 struct proc_inode
*ei
;
1519 if (!pid_alive(task
))
1522 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1526 files
= get_files_struct(task
);
1529 inode
->i_mode
= S_IFLNK
;
1532 * We are not taking a ref to the file structure, so we must
1535 spin_lock(&files
->file_lock
);
1536 file
= fcheck_files(files
, fd
);
1539 if (file
->f_mode
& 1)
1540 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1541 if (file
->f_mode
& 2)
1542 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1543 spin_unlock(&files
->file_lock
);
1544 put_files_struct(files
);
1545 inode
->i_op
= &proc_pid_link_inode_operations
;
1547 ei
->op
.proc_get_link
= proc_fd_link
;
1548 dentry
->d_op
= &tid_fd_dentry_operations
;
1549 d_add(dentry
, inode
);
1553 spin_unlock(&files
->file_lock
);
1554 put_files_struct(files
);
1558 return ERR_PTR(-ENOENT
);
1561 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1562 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1564 static struct file_operations proc_fd_operations
= {
1565 .read
= generic_read_dir
,
1566 .readdir
= proc_readfd
,
1569 static struct file_operations proc_task_operations
= {
1570 .read
= generic_read_dir
,
1571 .readdir
= proc_task_readdir
,
1575 * proc directories can do almost nothing..
1577 static struct inode_operations proc_fd_inode_operations
= {
1578 .lookup
= proc_lookupfd
,
1579 .permission
= proc_permission
,
1582 static struct inode_operations proc_task_inode_operations
= {
1583 .lookup
= proc_task_lookup
,
1584 .permission
= proc_task_permission
,
1587 #ifdef CONFIG_SECURITY
1588 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1589 size_t count
, loff_t
*ppos
)
1591 struct inode
* inode
= file
->f_dentry
->d_inode
;
1594 struct task_struct
*task
= proc_task(inode
);
1596 if (count
> PAGE_SIZE
)
1598 if (!(page
= __get_free_page(GFP_KERNEL
)))
1601 length
= security_getprocattr(task
,
1602 (char*)file
->f_dentry
->d_name
.name
,
1603 (void*)page
, count
);
1605 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1610 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1611 size_t count
, loff_t
*ppos
)
1613 struct inode
* inode
= file
->f_dentry
->d_inode
;
1616 struct task_struct
*task
= proc_task(inode
);
1618 if (count
> PAGE_SIZE
)
1621 /* No partial writes. */
1624 page
= (char*)__get_free_page(GFP_USER
);
1628 if (copy_from_user(page
, buf
, count
))
1631 length
= security_setprocattr(task
,
1632 (char*)file
->f_dentry
->d_name
.name
,
1633 (void*)page
, count
);
1635 free_page((unsigned long) page
);
1639 static struct file_operations proc_pid_attr_operations
= {
1640 .read
= proc_pid_attr_read
,
1641 .write
= proc_pid_attr_write
,
1644 static struct file_operations proc_tid_attr_operations
;
1645 static struct inode_operations proc_tid_attr_inode_operations
;
1646 static struct file_operations proc_tgid_attr_operations
;
1647 static struct inode_operations proc_tgid_attr_inode_operations
;
1650 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1653 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1654 struct dentry
*dentry
,
1655 struct pid_entry
*ents
)
1657 struct inode
*inode
;
1659 struct task_struct
*task
= proc_task(dir
);
1660 struct pid_entry
*p
;
1661 struct proc_inode
*ei
;
1666 if (!pid_alive(task
))
1669 for (p
= ents
; p
->name
; p
++) {
1670 if (p
->len
!= dentry
->d_name
.len
)
1672 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1679 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1684 inode
->i_mode
= p
->mode
;
1686 * Yes, it does not scale. And it should not. Don't add
1687 * new entries into /proc/<tgid>/ without very good reasons.
1690 case PROC_TGID_TASK
:
1691 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1692 inode
->i_op
= &proc_task_inode_operations
;
1693 inode
->i_fop
= &proc_task_operations
;
1698 inode
->i_op
= &proc_fd_inode_operations
;
1699 inode
->i_fop
= &proc_fd_operations
;
1703 inode
->i_op
= &proc_pid_link_inode_operations
;
1704 ei
->op
.proc_get_link
= proc_exe_link
;
1708 inode
->i_op
= &proc_pid_link_inode_operations
;
1709 ei
->op
.proc_get_link
= proc_cwd_link
;
1712 case PROC_TGID_ROOT
:
1713 inode
->i_op
= &proc_pid_link_inode_operations
;
1714 ei
->op
.proc_get_link
= proc_root_link
;
1716 case PROC_TID_ENVIRON
:
1717 case PROC_TGID_ENVIRON
:
1718 inode
->i_fop
= &proc_info_file_operations
;
1719 ei
->op
.proc_read
= proc_pid_environ
;
1722 case PROC_TGID_AUXV
:
1723 inode
->i_fop
= &proc_info_file_operations
;
1724 ei
->op
.proc_read
= proc_pid_auxv
;
1726 case PROC_TID_STATUS
:
1727 case PROC_TGID_STATUS
:
1728 inode
->i_fop
= &proc_info_file_operations
;
1729 ei
->op
.proc_read
= proc_pid_status
;
1732 inode
->i_fop
= &proc_info_file_operations
;
1733 ei
->op
.proc_read
= proc_tid_stat
;
1735 case PROC_TGID_STAT
:
1736 inode
->i_fop
= &proc_info_file_operations
;
1737 ei
->op
.proc_read
= proc_tgid_stat
;
1739 case PROC_TID_CMDLINE
:
1740 case PROC_TGID_CMDLINE
:
1741 inode
->i_fop
= &proc_info_file_operations
;
1742 ei
->op
.proc_read
= proc_pid_cmdline
;
1744 case PROC_TID_STATM
:
1745 case PROC_TGID_STATM
:
1746 inode
->i_fop
= &proc_info_file_operations
;
1747 ei
->op
.proc_read
= proc_pid_statm
;
1750 case PROC_TGID_MAPS
:
1751 inode
->i_fop
= &proc_maps_operations
;
1754 case PROC_TID_NUMA_MAPS
:
1755 case PROC_TGID_NUMA_MAPS
:
1756 inode
->i_fop
= &proc_numa_maps_operations
;
1761 inode
->i_op
= &proc_mem_inode_operations
;
1762 inode
->i_fop
= &proc_mem_operations
;
1764 #ifdef CONFIG_SECCOMP
1765 case PROC_TID_SECCOMP
:
1766 case PROC_TGID_SECCOMP
:
1767 inode
->i_fop
= &proc_seccomp_operations
;
1769 #endif /* CONFIG_SECCOMP */
1770 case PROC_TID_MOUNTS
:
1771 case PROC_TGID_MOUNTS
:
1772 inode
->i_fop
= &proc_mounts_operations
;
1775 case PROC_TID_SMAPS
:
1776 case PROC_TGID_SMAPS
:
1777 inode
->i_fop
= &proc_smaps_operations
;
1780 case PROC_TID_MOUNTSTATS
:
1781 case PROC_TGID_MOUNTSTATS
:
1782 inode
->i_fop
= &proc_mountstats_operations
;
1784 #ifdef CONFIG_SECURITY
1787 inode
->i_op
= &proc_tid_attr_inode_operations
;
1788 inode
->i_fop
= &proc_tid_attr_operations
;
1790 case PROC_TGID_ATTR
:
1792 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1793 inode
->i_fop
= &proc_tgid_attr_operations
;
1795 case PROC_TID_ATTR_CURRENT
:
1796 case PROC_TGID_ATTR_CURRENT
:
1797 case PROC_TID_ATTR_PREV
:
1798 case PROC_TGID_ATTR_PREV
:
1799 case PROC_TID_ATTR_EXEC
:
1800 case PROC_TGID_ATTR_EXEC
:
1801 case PROC_TID_ATTR_FSCREATE
:
1802 case PROC_TGID_ATTR_FSCREATE
:
1803 inode
->i_fop
= &proc_pid_attr_operations
;
1806 #ifdef CONFIG_KALLSYMS
1807 case PROC_TID_WCHAN
:
1808 case PROC_TGID_WCHAN
:
1809 inode
->i_fop
= &proc_info_file_operations
;
1810 ei
->op
.proc_read
= proc_pid_wchan
;
1813 #ifdef CONFIG_SCHEDSTATS
1814 case PROC_TID_SCHEDSTAT
:
1815 case PROC_TGID_SCHEDSTAT
:
1816 inode
->i_fop
= &proc_info_file_operations
;
1817 ei
->op
.proc_read
= proc_pid_schedstat
;
1820 #ifdef CONFIG_CPUSETS
1821 case PROC_TID_CPUSET
:
1822 case PROC_TGID_CPUSET
:
1823 inode
->i_fop
= &proc_cpuset_operations
;
1826 case PROC_TID_OOM_SCORE
:
1827 case PROC_TGID_OOM_SCORE
:
1828 inode
->i_fop
= &proc_info_file_operations
;
1829 ei
->op
.proc_read
= proc_oom_score
;
1831 case PROC_TID_OOM_ADJUST
:
1832 case PROC_TGID_OOM_ADJUST
:
1833 inode
->i_fop
= &proc_oom_adjust_operations
;
1835 #ifdef CONFIG_AUDITSYSCALL
1836 case PROC_TID_LOGINUID
:
1837 case PROC_TGID_LOGINUID
:
1838 inode
->i_fop
= &proc_loginuid_operations
;
1842 printk("procfs: impossible type (%d)",p
->type
);
1844 return ERR_PTR(-EINVAL
);
1846 dentry
->d_op
= &pid_dentry_operations
;
1847 d_add(dentry
, inode
);
1851 return ERR_PTR(error
);
1854 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1855 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1858 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1859 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1862 static struct file_operations proc_tgid_base_operations
= {
1863 .read
= generic_read_dir
,
1864 .readdir
= proc_tgid_base_readdir
,
1867 static struct file_operations proc_tid_base_operations
= {
1868 .read
= generic_read_dir
,
1869 .readdir
= proc_tid_base_readdir
,
1872 static struct inode_operations proc_tgid_base_inode_operations
= {
1873 .lookup
= proc_tgid_base_lookup
,
1876 static struct inode_operations proc_tid_base_inode_operations
= {
1877 .lookup
= proc_tid_base_lookup
,
1880 #ifdef CONFIG_SECURITY
1881 static int proc_tgid_attr_readdir(struct file
* filp
,
1882 void * dirent
, filldir_t filldir
)
1884 return proc_pident_readdir(filp
,dirent
,filldir
,
1885 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1888 static int proc_tid_attr_readdir(struct file
* filp
,
1889 void * dirent
, filldir_t filldir
)
1891 return proc_pident_readdir(filp
,dirent
,filldir
,
1892 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1895 static struct file_operations proc_tgid_attr_operations
= {
1896 .read
= generic_read_dir
,
1897 .readdir
= proc_tgid_attr_readdir
,
1900 static struct file_operations proc_tid_attr_operations
= {
1901 .read
= generic_read_dir
,
1902 .readdir
= proc_tid_attr_readdir
,
1905 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1906 struct dentry
*dentry
, struct nameidata
*nd
)
1908 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1911 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1912 struct dentry
*dentry
, struct nameidata
*nd
)
1914 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1917 static struct inode_operations proc_tgid_attr_inode_operations
= {
1918 .lookup
= proc_tgid_attr_lookup
,
1921 static struct inode_operations proc_tid_attr_inode_operations
= {
1922 .lookup
= proc_tid_attr_lookup
,
1929 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1933 sprintf(tmp
, "%d", current
->tgid
);
1934 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1937 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1940 sprintf(tmp
, "%d", current
->tgid
);
1941 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1944 static struct inode_operations proc_self_inode_operations
= {
1945 .readlink
= proc_self_readlink
,
1946 .follow_link
= proc_self_follow_link
,
1950 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1951 * @p: task that should be flushed.
1953 * Drops the /proc/@pid dcache entry from the hash chains.
1955 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1956 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1957 * if the pid value is immediately reused. This is enforced by
1958 * - caller must acquire spin_lock(p->proc_lock)
1959 * - must be called before detach_pid()
1960 * - proc_pid_lookup acquires proc_lock, and checks that
1961 * the target is not dead by looking at the attach count
1965 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1967 struct dentry
*proc_dentry
;
1969 proc_dentry
= p
->proc_dentry
;
1970 if (proc_dentry
!= NULL
) {
1972 spin_lock(&dcache_lock
);
1973 spin_lock(&proc_dentry
->d_lock
);
1974 if (!d_unhashed(proc_dentry
)) {
1975 dget_locked(proc_dentry
);
1976 __d_drop(proc_dentry
);
1977 spin_unlock(&proc_dentry
->d_lock
);
1979 spin_unlock(&proc_dentry
->d_lock
);
1982 spin_unlock(&dcache_lock
);
1988 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1989 * @proc_dentry: directoy to prune.
1991 * Shrink the /proc directory that was used by the just killed thread.
1994 void proc_pid_flush(struct dentry
*proc_dentry
)
1997 if(proc_dentry
!= NULL
) {
1998 shrink_dcache_parent(proc_dentry
);
2004 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2006 struct task_struct
*task
;
2007 struct inode
*inode
;
2008 struct proc_inode
*ei
;
2012 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
2013 inode
= new_inode(dir
->i_sb
);
2015 return ERR_PTR(-ENOMEM
);
2017 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2018 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
2020 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
2021 inode
->i_uid
= inode
->i_gid
= 0;
2023 inode
->i_op
= &proc_self_inode_operations
;
2024 d_add(dentry
, inode
);
2027 tgid
= name_to_int(dentry
);
2031 read_lock(&tasklist_lock
);
2032 task
= find_task_by_pid(tgid
);
2034 get_task_struct(task
);
2035 read_unlock(&tasklist_lock
);
2039 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
2043 put_task_struct(task
);
2046 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2047 inode
->i_op
= &proc_tgid_base_inode_operations
;
2048 inode
->i_fop
= &proc_tgid_base_operations
;
2049 inode
->i_flags
|=S_IMMUTABLE
;
2050 #ifdef CONFIG_SECURITY
2056 dentry
->d_op
= &pid_base_dentry_operations
;
2059 d_add(dentry
, inode
);
2060 spin_lock(&task
->proc_lock
);
2061 task
->proc_dentry
= dentry
;
2062 if (!pid_alive(task
)) {
2063 dentry
= proc_pid_unhash(task
);
2066 spin_unlock(&task
->proc_lock
);
2068 put_task_struct(task
);
2070 proc_pid_flush(dentry
);
2075 return ERR_PTR(-ENOENT
);
2079 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2081 struct task_struct
*task
;
2082 struct task_struct
*leader
= proc_task(dir
);
2083 struct inode
*inode
;
2086 tid
= name_to_int(dentry
);
2090 read_lock(&tasklist_lock
);
2091 task
= find_task_by_pid(tid
);
2093 get_task_struct(task
);
2094 read_unlock(&tasklist_lock
);
2097 if (leader
->tgid
!= task
->tgid
)
2100 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2105 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2106 inode
->i_op
= &proc_tid_base_inode_operations
;
2107 inode
->i_fop
= &proc_tid_base_operations
;
2108 inode
->i_flags
|=S_IMMUTABLE
;
2109 #ifdef CONFIG_SECURITY
2115 dentry
->d_op
= &pid_base_dentry_operations
;
2117 d_add(dentry
, inode
);
2119 put_task_struct(task
);
2122 put_task_struct(task
);
2124 return ERR_PTR(-ENOENT
);
2127 #define PROC_NUMBUF 10
2128 #define PROC_MAXPIDS 20
2131 * Get a few tgid's to return for filldir - we need to hold the
2132 * tasklist lock while doing this, and we must release it before
2133 * we actually do the filldir itself, so we use a temp buffer..
2135 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
2137 struct task_struct
*p
;
2141 read_lock(&tasklist_lock
);
2144 p
= find_task_by_pid(version
);
2145 if (p
&& !thread_group_leader(p
))
2152 p
= next_task(&init_task
);
2154 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2160 tgids
[nr_tgids
] = tgid
;
2162 if (nr_tgids
>= PROC_MAXPIDS
)
2165 read_unlock(&tasklist_lock
);
2170 * Get a few tid's to return for filldir - we need to hold the
2171 * tasklist lock while doing this, and we must release it before
2172 * we actually do the filldir itself, so we use a temp buffer..
2174 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2176 struct task_struct
*leader_task
= proc_task(dir
);
2177 struct task_struct
*task
= leader_task
;
2181 read_lock(&tasklist_lock
);
2183 * The starting point task (leader_task) might be an already
2184 * unlinked task, which cannot be used to access the task-list
2185 * via next_thread().
2187 if (pid_alive(task
)) do {
2188 int tid
= task
->pid
;
2193 tids
[nr_tids
] = tid
;
2195 if (nr_tids
>= PROC_MAXPIDS
)
2197 } while ((task
= next_thread(task
)) != leader_task
);
2198 read_unlock(&tasklist_lock
);
2202 /* for the /proc/ directory itself, after non-process stuff has been done */
2203 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2205 unsigned int tgid_array
[PROC_MAXPIDS
];
2206 char buf
[PROC_NUMBUF
];
2207 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2208 unsigned int nr_tgids
, i
;
2212 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2213 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2219 /* f_version caches the tgid value that the last readdir call couldn't
2220 * return. lseek aka telldir automagically resets f_version to 0.
2222 next_tgid
= filp
->f_version
;
2223 filp
->f_version
= 0;
2225 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2227 /* no more entries ! */
2232 /* do not use the last found pid, reserve it for next_tgid */
2233 if (nr_tgids
== PROC_MAXPIDS
) {
2235 next_tgid
= tgid_array
[nr_tgids
];
2238 for (i
=0;i
<nr_tgids
;i
++) {
2239 int tgid
= tgid_array
[i
];
2240 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2241 unsigned long j
= PROC_NUMBUF
;
2244 buf
[--j
] = '0' + (tgid
% 10);
2245 while ((tgid
/= 10) != 0);
2247 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2248 /* returning this tgid failed, save it as the first
2249 * pid for the next readir call */
2250 filp
->f_version
= tgid_array
[i
];
2261 /* for the /proc/TGID/task/ directories */
2262 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2264 unsigned int tid_array
[PROC_MAXPIDS
];
2265 char buf
[PROC_NUMBUF
];
2266 unsigned int nr_tids
, i
;
2267 struct dentry
*dentry
= filp
->f_dentry
;
2268 struct inode
*inode
= dentry
->d_inode
;
2269 int retval
= -ENOENT
;
2271 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2273 if (!pid_alive(proc_task(inode
)))
2280 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2285 ino
= parent_ino(dentry
);
2286 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2292 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2293 inode
->i_nlink
= pos
+ nr_tids
;
2295 for (i
= 0; i
< nr_tids
; i
++) {
2296 unsigned long j
= PROC_NUMBUF
;
2297 int tid
= tid_array
[i
];
2299 ino
= fake_ino(tid
,PROC_TID_INO
);
2302 buf
[--j
] = '0' + (tid
% 10);
2303 while ((tid
/= 10) != 0);
2305 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)