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
)
1023 count
= PAGE_SIZE
- 1;
1026 /* No partial writes. */
1029 page
= (char*)__get_free_page(GFP_USER
);
1033 if (copy_from_user(page
, buf
, count
))
1037 loginuid
= simple_strtoul(page
, &tmp
, 10);
1043 length
= audit_set_loginuid(task
, loginuid
);
1044 if (likely(length
== 0))
1048 free_page((unsigned long) page
);
1052 static struct file_operations proc_loginuid_operations
= {
1053 .read
= proc_loginuid_read
,
1054 .write
= proc_loginuid_write
,
1058 #ifdef CONFIG_SECCOMP
1059 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
1060 size_t count
, loff_t
*ppos
)
1062 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1064 loff_t __ppos
= *ppos
;
1067 /* no need to print the trailing zero, so use only len */
1068 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
1071 if (count
> len
- __ppos
)
1072 count
= len
- __ppos
;
1073 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
1075 *ppos
= __ppos
+ count
;
1079 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
1080 size_t count
, loff_t
*ppos
)
1082 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1083 char __buf
[20], *end
;
1084 unsigned int seccomp_mode
;
1086 /* can set it only once to be even more secure */
1087 if (unlikely(tsk
->seccomp
.mode
))
1090 memset(__buf
, 0, sizeof(__buf
));
1091 count
= min(count
, sizeof(__buf
) - 1);
1092 if (copy_from_user(__buf
, buf
, count
))
1094 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
1097 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
1098 tsk
->seccomp
.mode
= seccomp_mode
;
1099 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
1102 if (unlikely(!(end
- __buf
)))
1107 static struct file_operations proc_seccomp_operations
= {
1108 .read
= seccomp_read
,
1109 .write
= seccomp_write
,
1111 #endif /* CONFIG_SECCOMP */
1113 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1115 struct inode
*inode
= dentry
->d_inode
;
1116 int error
= -EACCES
;
1118 /* We don't need a base pointer in the /proc filesystem */
1121 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1123 error
= proc_check_root(inode
);
1127 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1128 nd
->last_type
= LAST_BIND
;
1130 return ERR_PTR(error
);
1133 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1134 char __user
*buffer
, int buflen
)
1136 struct inode
* inode
;
1137 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1143 inode
= dentry
->d_inode
;
1144 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1145 len
= PTR_ERR(path
);
1148 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1152 if (copy_to_user(buffer
, path
, len
))
1155 free_page((unsigned long)tmp
);
1159 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1161 int error
= -EACCES
;
1162 struct inode
*inode
= dentry
->d_inode
;
1164 struct vfsmount
*mnt
= NULL
;
1168 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1170 error
= proc_check_root(inode
);
1174 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1178 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1186 static struct inode_operations proc_pid_link_inode_operations
= {
1187 .readlink
= proc_pid_readlink
,
1188 .follow_link
= proc_pid_follow_link
1193 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1195 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1196 struct task_struct
*p
= proc_task(inode
);
1197 unsigned int fd
, tid
, ino
;
1200 struct files_struct
* files
;
1201 struct fdtable
*fdt
;
1212 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1216 ino
= fake_ino(tid
, PROC_TID_INO
);
1217 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1221 files
= get_files_struct(p
);
1225 fdt
= files_fdtable(files
);
1226 for (fd
= filp
->f_pos
-2;
1228 fd
++, filp
->f_pos
++) {
1231 if (!fcheck_files(files
, fd
))
1239 buf
[j
] = '0' + (i
% 10);
1243 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1244 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1251 put_files_struct(files
);
1257 static int proc_pident_readdir(struct file
*filp
,
1258 void *dirent
, filldir_t filldir
,
1259 struct pid_entry
*ents
, unsigned int nents
)
1263 struct dentry
*dentry
= filp
->f_dentry
;
1264 struct inode
*inode
= dentry
->d_inode
;
1265 struct pid_entry
*p
;
1270 if (!pid_alive(proc_task(inode
)))
1274 pid
= proc_task(inode
)->pid
;
1279 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1285 ino
= parent_ino(dentry
);
1286 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1299 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1300 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1312 static int proc_tgid_base_readdir(struct file
* filp
,
1313 void * dirent
, filldir_t filldir
)
1315 return proc_pident_readdir(filp
,dirent
,filldir
,
1316 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1319 static int proc_tid_base_readdir(struct file
* filp
,
1320 void * dirent
, filldir_t filldir
)
1322 return proc_pident_readdir(filp
,dirent
,filldir
,
1323 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1326 /* building an inode */
1328 static int task_dumpable(struct task_struct
*task
)
1331 struct mm_struct
*mm
;
1336 dumpable
= mm
->dumpable
;
1344 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1346 struct inode
* inode
;
1347 struct proc_inode
*ei
;
1349 /* We need a new inode */
1351 inode
= new_inode(sb
);
1358 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1359 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1361 if (!pid_alive(task
))
1365 * grab the reference to task.
1367 get_task_struct(task
);
1372 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1373 inode
->i_uid
= task
->euid
;
1374 inode
->i_gid
= task
->egid
;
1376 security_task_to_inode(task
, inode
);
1390 * Exceptional case: normally we are not allowed to unhash a busy
1391 * directory. In this case, however, we can do it - no aliasing problems
1392 * due to the way we treat inodes.
1394 * Rewrite the inode's ownerships here because the owning task may have
1395 * performed a setuid(), etc.
1397 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1399 struct inode
*inode
= dentry
->d_inode
;
1400 struct task_struct
*task
= proc_task(inode
);
1401 if (pid_alive(task
)) {
1402 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1403 inode
->i_uid
= task
->euid
;
1404 inode
->i_gid
= task
->egid
;
1409 security_task_to_inode(task
, inode
);
1416 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1418 struct inode
*inode
= dentry
->d_inode
;
1419 struct task_struct
*task
= proc_task(inode
);
1420 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1421 struct files_struct
*files
;
1423 files
= get_files_struct(task
);
1426 if (fcheck_files(files
, fd
)) {
1428 put_files_struct(files
);
1429 if (task_dumpable(task
)) {
1430 inode
->i_uid
= task
->euid
;
1431 inode
->i_gid
= task
->egid
;
1436 security_task_to_inode(task
, inode
);
1440 put_files_struct(files
);
1446 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1448 struct task_struct
*task
= proc_task(inode
);
1449 spin_lock(&task
->proc_lock
);
1450 if (task
->proc_dentry
== dentry
)
1451 task
->proc_dentry
= NULL
;
1452 spin_unlock(&task
->proc_lock
);
1456 static int pid_delete_dentry(struct dentry
* dentry
)
1458 /* Is the task we represent dead?
1459 * If so, then don't put the dentry on the lru list,
1460 * kill it immediately.
1462 return !pid_alive(proc_task(dentry
->d_inode
));
1465 static struct dentry_operations tid_fd_dentry_operations
=
1467 .d_revalidate
= tid_fd_revalidate
,
1468 .d_delete
= pid_delete_dentry
,
1471 static struct dentry_operations pid_dentry_operations
=
1473 .d_revalidate
= pid_revalidate
,
1474 .d_delete
= pid_delete_dentry
,
1477 static struct dentry_operations pid_base_dentry_operations
=
1479 .d_revalidate
= pid_revalidate
,
1480 .d_iput
= pid_base_iput
,
1481 .d_delete
= pid_delete_dentry
,
1486 static unsigned name_to_int(struct dentry
*dentry
)
1488 const char *name
= dentry
->d_name
.name
;
1489 int len
= dentry
->d_name
.len
;
1492 if (len
> 1 && *name
== '0')
1495 unsigned c
= *name
++ - '0';
1498 if (n
>= (~0U-9)/10)
1509 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1511 struct task_struct
*task
= proc_task(dir
);
1512 unsigned fd
= name_to_int(dentry
);
1514 struct files_struct
* files
;
1515 struct inode
*inode
;
1516 struct proc_inode
*ei
;
1520 if (!pid_alive(task
))
1523 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1527 files
= get_files_struct(task
);
1530 inode
->i_mode
= S_IFLNK
;
1533 * We are not taking a ref to the file structure, so we must
1536 spin_lock(&files
->file_lock
);
1537 file
= fcheck_files(files
, fd
);
1540 if (file
->f_mode
& 1)
1541 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1542 if (file
->f_mode
& 2)
1543 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1544 spin_unlock(&files
->file_lock
);
1545 put_files_struct(files
);
1546 inode
->i_op
= &proc_pid_link_inode_operations
;
1548 ei
->op
.proc_get_link
= proc_fd_link
;
1549 dentry
->d_op
= &tid_fd_dentry_operations
;
1550 d_add(dentry
, inode
);
1554 spin_unlock(&files
->file_lock
);
1555 put_files_struct(files
);
1559 return ERR_PTR(-ENOENT
);
1562 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1563 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1565 static struct file_operations proc_fd_operations
= {
1566 .read
= generic_read_dir
,
1567 .readdir
= proc_readfd
,
1570 static struct file_operations proc_task_operations
= {
1571 .read
= generic_read_dir
,
1572 .readdir
= proc_task_readdir
,
1576 * proc directories can do almost nothing..
1578 static struct inode_operations proc_fd_inode_operations
= {
1579 .lookup
= proc_lookupfd
,
1580 .permission
= proc_permission
,
1583 static struct inode_operations proc_task_inode_operations
= {
1584 .lookup
= proc_task_lookup
,
1585 .permission
= proc_task_permission
,
1588 #ifdef CONFIG_SECURITY
1589 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1590 size_t count
, loff_t
*ppos
)
1592 struct inode
* inode
= file
->f_dentry
->d_inode
;
1595 struct task_struct
*task
= proc_task(inode
);
1597 if (count
> PAGE_SIZE
)
1599 if (!(page
= __get_free_page(GFP_KERNEL
)))
1602 length
= security_getprocattr(task
,
1603 (char*)file
->f_dentry
->d_name
.name
,
1604 (void*)page
, count
);
1606 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1611 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1612 size_t count
, loff_t
*ppos
)
1614 struct inode
* inode
= file
->f_dentry
->d_inode
;
1617 struct task_struct
*task
= proc_task(inode
);
1619 if (count
> PAGE_SIZE
)
1622 /* No partial writes. */
1625 page
= (char*)__get_free_page(GFP_USER
);
1629 if (copy_from_user(page
, buf
, count
))
1632 length
= security_setprocattr(task
,
1633 (char*)file
->f_dentry
->d_name
.name
,
1634 (void*)page
, count
);
1636 free_page((unsigned long) page
);
1640 static struct file_operations proc_pid_attr_operations
= {
1641 .read
= proc_pid_attr_read
,
1642 .write
= proc_pid_attr_write
,
1645 static struct file_operations proc_tid_attr_operations
;
1646 static struct inode_operations proc_tid_attr_inode_operations
;
1647 static struct file_operations proc_tgid_attr_operations
;
1648 static struct inode_operations proc_tgid_attr_inode_operations
;
1651 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1654 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1655 struct dentry
*dentry
,
1656 struct pid_entry
*ents
)
1658 struct inode
*inode
;
1660 struct task_struct
*task
= proc_task(dir
);
1661 struct pid_entry
*p
;
1662 struct proc_inode
*ei
;
1667 if (!pid_alive(task
))
1670 for (p
= ents
; p
->name
; p
++) {
1671 if (p
->len
!= dentry
->d_name
.len
)
1673 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1680 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1685 inode
->i_mode
= p
->mode
;
1687 * Yes, it does not scale. And it should not. Don't add
1688 * new entries into /proc/<tgid>/ without very good reasons.
1691 case PROC_TGID_TASK
:
1692 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1693 inode
->i_op
= &proc_task_inode_operations
;
1694 inode
->i_fop
= &proc_task_operations
;
1699 inode
->i_op
= &proc_fd_inode_operations
;
1700 inode
->i_fop
= &proc_fd_operations
;
1704 inode
->i_op
= &proc_pid_link_inode_operations
;
1705 ei
->op
.proc_get_link
= proc_exe_link
;
1709 inode
->i_op
= &proc_pid_link_inode_operations
;
1710 ei
->op
.proc_get_link
= proc_cwd_link
;
1713 case PROC_TGID_ROOT
:
1714 inode
->i_op
= &proc_pid_link_inode_operations
;
1715 ei
->op
.proc_get_link
= proc_root_link
;
1717 case PROC_TID_ENVIRON
:
1718 case PROC_TGID_ENVIRON
:
1719 inode
->i_fop
= &proc_info_file_operations
;
1720 ei
->op
.proc_read
= proc_pid_environ
;
1723 case PROC_TGID_AUXV
:
1724 inode
->i_fop
= &proc_info_file_operations
;
1725 ei
->op
.proc_read
= proc_pid_auxv
;
1727 case PROC_TID_STATUS
:
1728 case PROC_TGID_STATUS
:
1729 inode
->i_fop
= &proc_info_file_operations
;
1730 ei
->op
.proc_read
= proc_pid_status
;
1733 inode
->i_fop
= &proc_info_file_operations
;
1734 ei
->op
.proc_read
= proc_tid_stat
;
1736 case PROC_TGID_STAT
:
1737 inode
->i_fop
= &proc_info_file_operations
;
1738 ei
->op
.proc_read
= proc_tgid_stat
;
1740 case PROC_TID_CMDLINE
:
1741 case PROC_TGID_CMDLINE
:
1742 inode
->i_fop
= &proc_info_file_operations
;
1743 ei
->op
.proc_read
= proc_pid_cmdline
;
1745 case PROC_TID_STATM
:
1746 case PROC_TGID_STATM
:
1747 inode
->i_fop
= &proc_info_file_operations
;
1748 ei
->op
.proc_read
= proc_pid_statm
;
1751 case PROC_TGID_MAPS
:
1752 inode
->i_fop
= &proc_maps_operations
;
1755 case PROC_TID_NUMA_MAPS
:
1756 case PROC_TGID_NUMA_MAPS
:
1757 inode
->i_fop
= &proc_numa_maps_operations
;
1762 inode
->i_op
= &proc_mem_inode_operations
;
1763 inode
->i_fop
= &proc_mem_operations
;
1765 #ifdef CONFIG_SECCOMP
1766 case PROC_TID_SECCOMP
:
1767 case PROC_TGID_SECCOMP
:
1768 inode
->i_fop
= &proc_seccomp_operations
;
1770 #endif /* CONFIG_SECCOMP */
1771 case PROC_TID_MOUNTS
:
1772 case PROC_TGID_MOUNTS
:
1773 inode
->i_fop
= &proc_mounts_operations
;
1776 case PROC_TID_SMAPS
:
1777 case PROC_TGID_SMAPS
:
1778 inode
->i_fop
= &proc_smaps_operations
;
1781 case PROC_TID_MOUNTSTATS
:
1782 case PROC_TGID_MOUNTSTATS
:
1783 inode
->i_fop
= &proc_mountstats_operations
;
1785 #ifdef CONFIG_SECURITY
1788 inode
->i_op
= &proc_tid_attr_inode_operations
;
1789 inode
->i_fop
= &proc_tid_attr_operations
;
1791 case PROC_TGID_ATTR
:
1793 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1794 inode
->i_fop
= &proc_tgid_attr_operations
;
1796 case PROC_TID_ATTR_CURRENT
:
1797 case PROC_TGID_ATTR_CURRENT
:
1798 case PROC_TID_ATTR_PREV
:
1799 case PROC_TGID_ATTR_PREV
:
1800 case PROC_TID_ATTR_EXEC
:
1801 case PROC_TGID_ATTR_EXEC
:
1802 case PROC_TID_ATTR_FSCREATE
:
1803 case PROC_TGID_ATTR_FSCREATE
:
1804 inode
->i_fop
= &proc_pid_attr_operations
;
1807 #ifdef CONFIG_KALLSYMS
1808 case PROC_TID_WCHAN
:
1809 case PROC_TGID_WCHAN
:
1810 inode
->i_fop
= &proc_info_file_operations
;
1811 ei
->op
.proc_read
= proc_pid_wchan
;
1814 #ifdef CONFIG_SCHEDSTATS
1815 case PROC_TID_SCHEDSTAT
:
1816 case PROC_TGID_SCHEDSTAT
:
1817 inode
->i_fop
= &proc_info_file_operations
;
1818 ei
->op
.proc_read
= proc_pid_schedstat
;
1821 #ifdef CONFIG_CPUSETS
1822 case PROC_TID_CPUSET
:
1823 case PROC_TGID_CPUSET
:
1824 inode
->i_fop
= &proc_cpuset_operations
;
1827 case PROC_TID_OOM_SCORE
:
1828 case PROC_TGID_OOM_SCORE
:
1829 inode
->i_fop
= &proc_info_file_operations
;
1830 ei
->op
.proc_read
= proc_oom_score
;
1832 case PROC_TID_OOM_ADJUST
:
1833 case PROC_TGID_OOM_ADJUST
:
1834 inode
->i_fop
= &proc_oom_adjust_operations
;
1836 #ifdef CONFIG_AUDITSYSCALL
1837 case PROC_TID_LOGINUID
:
1838 case PROC_TGID_LOGINUID
:
1839 inode
->i_fop
= &proc_loginuid_operations
;
1843 printk("procfs: impossible type (%d)",p
->type
);
1845 return ERR_PTR(-EINVAL
);
1847 dentry
->d_op
= &pid_dentry_operations
;
1848 d_add(dentry
, inode
);
1852 return ERR_PTR(error
);
1855 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1856 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1859 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1860 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1863 static struct file_operations proc_tgid_base_operations
= {
1864 .read
= generic_read_dir
,
1865 .readdir
= proc_tgid_base_readdir
,
1868 static struct file_operations proc_tid_base_operations
= {
1869 .read
= generic_read_dir
,
1870 .readdir
= proc_tid_base_readdir
,
1873 static struct inode_operations proc_tgid_base_inode_operations
= {
1874 .lookup
= proc_tgid_base_lookup
,
1877 static struct inode_operations proc_tid_base_inode_operations
= {
1878 .lookup
= proc_tid_base_lookup
,
1881 #ifdef CONFIG_SECURITY
1882 static int proc_tgid_attr_readdir(struct file
* filp
,
1883 void * dirent
, filldir_t filldir
)
1885 return proc_pident_readdir(filp
,dirent
,filldir
,
1886 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1889 static int proc_tid_attr_readdir(struct file
* filp
,
1890 void * dirent
, filldir_t filldir
)
1892 return proc_pident_readdir(filp
,dirent
,filldir
,
1893 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1896 static struct file_operations proc_tgid_attr_operations
= {
1897 .read
= generic_read_dir
,
1898 .readdir
= proc_tgid_attr_readdir
,
1901 static struct file_operations proc_tid_attr_operations
= {
1902 .read
= generic_read_dir
,
1903 .readdir
= proc_tid_attr_readdir
,
1906 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1907 struct dentry
*dentry
, struct nameidata
*nd
)
1909 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1912 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1913 struct dentry
*dentry
, struct nameidata
*nd
)
1915 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1918 static struct inode_operations proc_tgid_attr_inode_operations
= {
1919 .lookup
= proc_tgid_attr_lookup
,
1922 static struct inode_operations proc_tid_attr_inode_operations
= {
1923 .lookup
= proc_tid_attr_lookup
,
1930 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1934 sprintf(tmp
, "%d", current
->tgid
);
1935 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1938 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1941 sprintf(tmp
, "%d", current
->tgid
);
1942 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1945 static struct inode_operations proc_self_inode_operations
= {
1946 .readlink
= proc_self_readlink
,
1947 .follow_link
= proc_self_follow_link
,
1951 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1952 * @p: task that should be flushed.
1954 * Drops the /proc/@pid dcache entry from the hash chains.
1956 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1957 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1958 * if the pid value is immediately reused. This is enforced by
1959 * - caller must acquire spin_lock(p->proc_lock)
1960 * - must be called before detach_pid()
1961 * - proc_pid_lookup acquires proc_lock, and checks that
1962 * the target is not dead by looking at the attach count
1966 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1968 struct dentry
*proc_dentry
;
1970 proc_dentry
= p
->proc_dentry
;
1971 if (proc_dentry
!= NULL
) {
1973 spin_lock(&dcache_lock
);
1974 spin_lock(&proc_dentry
->d_lock
);
1975 if (!d_unhashed(proc_dentry
)) {
1976 dget_locked(proc_dentry
);
1977 __d_drop(proc_dentry
);
1978 spin_unlock(&proc_dentry
->d_lock
);
1980 spin_unlock(&proc_dentry
->d_lock
);
1983 spin_unlock(&dcache_lock
);
1989 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1990 * @proc_dentry: directoy to prune.
1992 * Shrink the /proc directory that was used by the just killed thread.
1995 void proc_pid_flush(struct dentry
*proc_dentry
)
1998 if(proc_dentry
!= NULL
) {
1999 shrink_dcache_parent(proc_dentry
);
2005 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2007 struct task_struct
*task
;
2008 struct inode
*inode
;
2009 struct proc_inode
*ei
;
2013 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
2014 inode
= new_inode(dir
->i_sb
);
2016 return ERR_PTR(-ENOMEM
);
2018 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2019 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
2021 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
2022 inode
->i_uid
= inode
->i_gid
= 0;
2024 inode
->i_op
= &proc_self_inode_operations
;
2025 d_add(dentry
, inode
);
2028 tgid
= name_to_int(dentry
);
2032 read_lock(&tasklist_lock
);
2033 task
= find_task_by_pid(tgid
);
2035 get_task_struct(task
);
2036 read_unlock(&tasklist_lock
);
2040 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
2044 put_task_struct(task
);
2047 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2048 inode
->i_op
= &proc_tgid_base_inode_operations
;
2049 inode
->i_fop
= &proc_tgid_base_operations
;
2050 inode
->i_flags
|=S_IMMUTABLE
;
2051 #ifdef CONFIG_SECURITY
2057 dentry
->d_op
= &pid_base_dentry_operations
;
2060 d_add(dentry
, inode
);
2061 spin_lock(&task
->proc_lock
);
2062 task
->proc_dentry
= dentry
;
2063 if (!pid_alive(task
)) {
2064 dentry
= proc_pid_unhash(task
);
2067 spin_unlock(&task
->proc_lock
);
2069 put_task_struct(task
);
2071 proc_pid_flush(dentry
);
2076 return ERR_PTR(-ENOENT
);
2080 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
2082 struct task_struct
*task
;
2083 struct task_struct
*leader
= proc_task(dir
);
2084 struct inode
*inode
;
2087 tid
= name_to_int(dentry
);
2091 read_lock(&tasklist_lock
);
2092 task
= find_task_by_pid(tid
);
2094 get_task_struct(task
);
2095 read_unlock(&tasklist_lock
);
2098 if (leader
->tgid
!= task
->tgid
)
2101 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2106 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2107 inode
->i_op
= &proc_tid_base_inode_operations
;
2108 inode
->i_fop
= &proc_tid_base_operations
;
2109 inode
->i_flags
|=S_IMMUTABLE
;
2110 #ifdef CONFIG_SECURITY
2116 dentry
->d_op
= &pid_base_dentry_operations
;
2118 d_add(dentry
, inode
);
2120 put_task_struct(task
);
2123 put_task_struct(task
);
2125 return ERR_PTR(-ENOENT
);
2128 #define PROC_NUMBUF 10
2129 #define PROC_MAXPIDS 20
2132 * Get a few tgid's to return for filldir - we need to hold the
2133 * tasklist lock while doing this, and we must release it before
2134 * we actually do the filldir itself, so we use a temp buffer..
2136 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
2138 struct task_struct
*p
;
2142 read_lock(&tasklist_lock
);
2145 p
= find_task_by_pid(version
);
2146 if (p
&& !thread_group_leader(p
))
2153 p
= next_task(&init_task
);
2155 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2161 tgids
[nr_tgids
] = tgid
;
2163 if (nr_tgids
>= PROC_MAXPIDS
)
2166 read_unlock(&tasklist_lock
);
2171 * Get a few tid's to return for filldir - we need to hold the
2172 * tasklist lock while doing this, and we must release it before
2173 * we actually do the filldir itself, so we use a temp buffer..
2175 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2177 struct task_struct
*leader_task
= proc_task(dir
);
2178 struct task_struct
*task
= leader_task
;
2182 read_lock(&tasklist_lock
);
2184 * The starting point task (leader_task) might be an already
2185 * unlinked task, which cannot be used to access the task-list
2186 * via next_thread().
2188 if (pid_alive(task
)) do {
2189 int tid
= task
->pid
;
2194 tids
[nr_tids
] = tid
;
2196 if (nr_tids
>= PROC_MAXPIDS
)
2198 } while ((task
= next_thread(task
)) != leader_task
);
2199 read_unlock(&tasklist_lock
);
2203 /* for the /proc/ directory itself, after non-process stuff has been done */
2204 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2206 unsigned int tgid_array
[PROC_MAXPIDS
];
2207 char buf
[PROC_NUMBUF
];
2208 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2209 unsigned int nr_tgids
, i
;
2213 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2214 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2220 /* f_version caches the tgid value that the last readdir call couldn't
2221 * return. lseek aka telldir automagically resets f_version to 0.
2223 next_tgid
= filp
->f_version
;
2224 filp
->f_version
= 0;
2226 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2228 /* no more entries ! */
2233 /* do not use the last found pid, reserve it for next_tgid */
2234 if (nr_tgids
== PROC_MAXPIDS
) {
2236 next_tgid
= tgid_array
[nr_tgids
];
2239 for (i
=0;i
<nr_tgids
;i
++) {
2240 int tgid
= tgid_array
[i
];
2241 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2242 unsigned long j
= PROC_NUMBUF
;
2245 buf
[--j
] = '0' + (tgid
% 10);
2246 while ((tgid
/= 10) != 0);
2248 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2249 /* returning this tgid failed, save it as the first
2250 * pid for the next readir call */
2251 filp
->f_version
= tgid_array
[i
];
2262 /* for the /proc/TGID/task/ directories */
2263 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2265 unsigned int tid_array
[PROC_MAXPIDS
];
2266 char buf
[PROC_NUMBUF
];
2267 unsigned int nr_tids
, i
;
2268 struct dentry
*dentry
= filp
->f_dentry
;
2269 struct inode
*inode
= dentry
->d_inode
;
2270 int retval
= -ENOENT
;
2272 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2274 if (!pid_alive(proc_task(inode
)))
2281 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2286 ino
= parent_ino(dentry
);
2287 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2293 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2294 inode
->i_nlink
= pos
+ nr_tids
;
2296 for (i
= 0; i
< nr_tids
; i
++) {
2297 unsigned long j
= PROC_NUMBUF
;
2298 int tid
= tid_array
[i
];
2300 ino
= fake_ino(tid
,PROC_TID_INO
);
2303 buf
[--j
] = '0' + (tid
% 10);
2304 while ((tid
/= 10) != 0);
2306 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)