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 * Implementing inode permission operations in /proc is almost
79 * certainly an error. Permission checks need to happen during
80 * each system call not at open time. The reason is that most of
81 * what we wish to check for permissions in /proc varies at runtime.
83 * The classic example of a problem is opening file descriptors
84 * in /proc for a task before it execs a suid executable.
88 * For hysterical raisins we keep the same inumbers as in the old procfs.
89 * Feel free to change the macro below - just keep the range distinct from
90 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
91 * As soon as we'll get a separate superblock we will be able to forget
92 * about magical ranges too.
95 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
97 enum pid_directory_inos
{
102 #ifdef CONFIG_SECCOMP
117 PROC_TGID_MOUNTSTATS
,
122 #ifdef CONFIG_SCHEDSTATS
125 #ifdef CONFIG_CPUSETS
128 #ifdef CONFIG_SECURITY
130 PROC_TGID_ATTR_CURRENT
,
133 PROC_TGID_ATTR_FSCREATE
,
134 PROC_TGID_ATTR_KEYCREATE
,
136 #ifdef CONFIG_AUDITSYSCALL
140 PROC_TGID_OOM_ADJUST
,
144 #ifdef CONFIG_SECCOMP
164 #ifdef CONFIG_SCHEDSTATS
167 #ifdef CONFIG_CPUSETS
170 #ifdef CONFIG_SECURITY
172 PROC_TID_ATTR_CURRENT
,
175 PROC_TID_ATTR_FSCREATE
,
176 PROC_TID_ATTR_KEYCREATE
,
178 #ifdef CONFIG_AUDITSYSCALL
184 /* Add new entries before this */
185 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
195 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
197 static struct pid_entry tgid_base_stuff
[] = {
198 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
199 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
200 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
201 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
202 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
203 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
204 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
205 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
206 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
208 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
210 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
211 #ifdef CONFIG_SECCOMP
212 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
214 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
215 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
216 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
217 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
218 E(PROC_TGID_MOUNTSTATS
, "mountstats", S_IFREG
|S_IRUSR
),
220 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
222 #ifdef CONFIG_SECURITY
223 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
225 #ifdef CONFIG_KALLSYMS
226 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
228 #ifdef CONFIG_SCHEDSTATS
229 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
231 #ifdef CONFIG_CPUSETS
232 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
234 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
235 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
236 #ifdef CONFIG_AUDITSYSCALL
237 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
241 static struct pid_entry tid_base_stuff
[] = {
242 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
243 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
244 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
245 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
246 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
247 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
248 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
249 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
251 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
253 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
254 #ifdef CONFIG_SECCOMP
255 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
257 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
258 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
259 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
260 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
262 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
264 #ifdef CONFIG_SECURITY
265 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
267 #ifdef CONFIG_KALLSYMS
268 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
270 #ifdef CONFIG_SCHEDSTATS
271 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
273 #ifdef CONFIG_CPUSETS
274 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
276 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
277 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
278 #ifdef CONFIG_AUDITSYSCALL
279 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
284 #ifdef CONFIG_SECURITY
285 static struct pid_entry tgid_attr_stuff
[] = {
286 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
287 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
288 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
289 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
290 E(PROC_TGID_ATTR_KEYCREATE
, "keycreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
293 static struct pid_entry tid_attr_stuff
[] = {
294 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
295 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
296 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
297 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
298 E(PROC_TID_ATTR_KEYCREATE
, "keycreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
305 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
307 struct task_struct
*task
= proc_task(inode
);
308 struct files_struct
*files
;
310 int fd
= proc_fd(inode
);
312 files
= get_files_struct(task
);
315 * We are not taking a ref to the file structure, so we must
318 spin_lock(&files
->file_lock
);
319 file
= fcheck_files(files
, fd
);
321 *mnt
= mntget(file
->f_vfsmnt
);
322 *dentry
= dget(file
->f_dentry
);
323 spin_unlock(&files
->file_lock
);
324 put_files_struct(files
);
327 spin_unlock(&files
->file_lock
);
328 put_files_struct(files
);
333 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
335 struct fs_struct
*fs
;
339 atomic_inc(&fs
->count
);
344 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
346 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
347 int result
= -ENOENT
;
349 read_lock(&fs
->lock
);
350 *mnt
= mntget(fs
->pwdmnt
);
351 *dentry
= dget(fs
->pwd
);
352 read_unlock(&fs
->lock
);
359 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
361 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
362 int result
= -ENOENT
;
364 read_lock(&fs
->lock
);
365 *mnt
= mntget(fs
->rootmnt
);
366 *dentry
= dget(fs
->root
);
367 read_unlock(&fs
->lock
);
374 #define MAY_PTRACE(task) \
375 (task == current || \
376 (task->parent == current && \
377 (task->ptrace & PT_PTRACED) && \
378 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
379 security_ptrace(current,task) == 0))
381 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
384 struct mm_struct
*mm
= get_task_mm(task
);
386 unsigned int len
= mm
->env_end
- mm
->env_start
;
389 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
390 if (!ptrace_may_attach(task
))
397 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
401 struct mm_struct
*mm
= get_task_mm(task
);
405 goto out_mm
; /* Shh! No looking before we're done */
407 len
= mm
->arg_end
- mm
->arg_start
;
412 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
414 // If the nul at the end of args has been overwritten, then
415 // assume application is using setproctitle(3).
416 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
417 len
= strnlen(buffer
, res
);
421 len
= mm
->env_end
- mm
->env_start
;
422 if (len
> PAGE_SIZE
- res
)
423 len
= PAGE_SIZE
- res
;
424 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
425 res
= strnlen(buffer
, res
);
434 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
437 struct mm_struct
*mm
= get_task_mm(task
);
439 unsigned int nwords
= 0;
442 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
443 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
446 memcpy(buffer
, mm
->saved_auxv
, res
);
453 #ifdef CONFIG_KALLSYMS
455 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
456 * Returns the resolved symbol. If that fails, simply return the address.
458 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
461 const char *sym_name
;
462 unsigned long wchan
, size
, offset
;
463 char namebuf
[KSYM_NAME_LEN
+1];
465 wchan
= get_wchan(task
);
467 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
469 return sprintf(buffer
, "%s", sym_name
);
470 return sprintf(buffer
, "%lu", wchan
);
472 #endif /* CONFIG_KALLSYMS */
474 #ifdef CONFIG_SCHEDSTATS
476 * Provides /proc/PID/schedstat
478 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
480 return sprintf(buffer
, "%lu %lu %lu\n",
481 task
->sched_info
.cpu_time
,
482 task
->sched_info
.run_delay
,
483 task
->sched_info
.pcnt
);
487 /* The badness from the OOM killer */
488 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
489 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
491 unsigned long points
;
492 struct timespec uptime
;
494 do_posix_clock_monotonic_gettime(&uptime
);
495 points
= badness(task
, uptime
.tv_sec
);
496 return sprintf(buffer
, "%lu\n", points
);
499 /************************************************************************/
500 /* Here the fs part begins */
501 /************************************************************************/
503 /* permission checks */
505 /* If the process being read is separated by chroot from the reading process,
506 * don't let the reader access the threads.
508 static int proc_check_chroot(struct dentry
*de
, struct vfsmount
*mnt
)
511 struct vfsmount
*our_vfsmnt
;
514 read_lock(¤t
->fs
->lock
);
515 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
516 base
= dget(current
->fs
->root
);
517 read_unlock(¤t
->fs
->lock
);
519 spin_lock(&vfsmount_lock
);
521 while (mnt
!= our_vfsmnt
) {
522 if (mnt
== mnt
->mnt_parent
)
524 de
= mnt
->mnt_mountpoint
;
525 mnt
= mnt
->mnt_parent
;
528 if (!is_subdir(de
, base
))
530 spin_unlock(&vfsmount_lock
);
537 spin_unlock(&vfsmount_lock
);
542 extern struct seq_operations mounts_op
;
548 static int mounts_open(struct inode
*inode
, struct file
*file
)
550 struct task_struct
*task
= proc_task(inode
);
551 struct namespace *namespace;
552 struct proc_mounts
*p
;
556 namespace = task
->namespace;
558 get_namespace(namespace);
563 p
= kmalloc(sizeof(struct proc_mounts
), GFP_KERNEL
);
565 file
->private_data
= &p
->m
;
566 ret
= seq_open(file
, &mounts_op
);
568 p
->m
.private = namespace;
569 p
->event
= namespace->event
;
574 put_namespace(namespace);
579 static int mounts_release(struct inode
*inode
, struct file
*file
)
581 struct seq_file
*m
= file
->private_data
;
582 struct namespace *namespace = m
->private;
583 put_namespace(namespace);
584 return seq_release(inode
, file
);
587 static unsigned mounts_poll(struct file
*file
, poll_table
*wait
)
589 struct proc_mounts
*p
= file
->private_data
;
590 struct namespace *ns
= p
->m
.private;
593 poll_wait(file
, &ns
->poll
, wait
);
595 spin_lock(&vfsmount_lock
);
596 if (p
->event
!= ns
->event
) {
597 p
->event
= ns
->event
;
600 spin_unlock(&vfsmount_lock
);
605 static struct file_operations proc_mounts_operations
= {
609 .release
= mounts_release
,
613 extern struct seq_operations mountstats_op
;
614 static int mountstats_open(struct inode
*inode
, struct file
*file
)
616 struct task_struct
*task
= proc_task(inode
);
617 int ret
= seq_open(file
, &mountstats_op
);
620 struct seq_file
*m
= file
->private_data
;
621 struct namespace *namespace;
623 namespace = task
->namespace;
625 get_namespace(namespace);
629 m
->private = namespace;
631 seq_release(inode
, file
);
638 static struct file_operations proc_mountstats_operations
= {
639 .open
= mountstats_open
,
642 .release
= mounts_release
,
645 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
647 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
648 size_t count
, loff_t
*ppos
)
650 struct inode
* inode
= file
->f_dentry
->d_inode
;
653 struct task_struct
*task
= proc_task(inode
);
655 if (count
> PROC_BLOCK_SIZE
)
656 count
= PROC_BLOCK_SIZE
;
657 if (!(page
= __get_free_page(GFP_KERNEL
)))
660 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
663 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
668 static struct file_operations proc_info_file_operations
= {
669 .read
= proc_info_read
,
672 static int mem_open(struct inode
* inode
, struct file
* file
)
674 file
->private_data
= (void*)((long)current
->self_exec_id
);
678 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
679 size_t count
, loff_t
*ppos
)
681 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
683 unsigned long src
= *ppos
;
685 struct mm_struct
*mm
;
687 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
691 page
= (char *)__get_free_page(GFP_USER
);
697 mm
= get_task_mm(task
);
703 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
709 int this_len
, retval
;
711 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
712 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
713 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
719 if (copy_to_user(buf
, page
, retval
)) {
734 free_page((unsigned long) page
);
739 #define mem_write NULL
742 /* This is a security hazard */
743 static ssize_t
mem_write(struct file
* file
, const char * buf
,
744 size_t count
, loff_t
*ppos
)
748 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
749 unsigned long dst
= *ppos
;
751 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
754 page
= (char *)__get_free_page(GFP_USER
);
759 int this_len
, retval
;
761 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
762 if (copy_from_user(page
, buf
, this_len
)) {
766 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
778 free_page((unsigned long) page
);
783 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
787 file
->f_pos
= offset
;
790 file
->f_pos
+= offset
;
795 force_successful_syscall_return();
799 static struct file_operations proc_mem_operations
= {
806 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
807 size_t count
, loff_t
*ppos
)
809 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
812 int oom_adjust
= task
->oomkilladj
;
813 loff_t __ppos
= *ppos
;
815 len
= sprintf(buffer
, "%i\n", oom_adjust
);
818 if (count
> len
-__ppos
)
820 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
822 *ppos
= __ppos
+ count
;
826 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
827 size_t count
, loff_t
*ppos
)
829 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
830 char buffer
[8], *end
;
833 if (!capable(CAP_SYS_RESOURCE
))
835 memset(buffer
, 0, 8);
838 if (copy_from_user(buffer
, buf
, count
))
840 oom_adjust
= simple_strtol(buffer
, &end
, 0);
841 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
845 task
->oomkilladj
= oom_adjust
;
846 if (end
- buffer
== 0)
851 static struct file_operations proc_oom_adjust_operations
= {
852 .read
= oom_adjust_read
,
853 .write
= oom_adjust_write
,
856 #ifdef CONFIG_AUDITSYSCALL
858 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
859 size_t count
, loff_t
*ppos
)
861 struct inode
* inode
= file
->f_dentry
->d_inode
;
862 struct task_struct
*task
= proc_task(inode
);
864 char tmpbuf
[TMPBUFLEN
];
866 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
867 audit_get_loginuid(task
->audit_context
));
868 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
871 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
872 size_t count
, loff_t
*ppos
)
874 struct inode
* inode
= file
->f_dentry
->d_inode
;
877 struct task_struct
*task
= proc_task(inode
);
880 if (!capable(CAP_AUDIT_CONTROL
))
886 if (count
>= PAGE_SIZE
)
887 count
= PAGE_SIZE
- 1;
890 /* No partial writes. */
893 page
= (char*)__get_free_page(GFP_USER
);
897 if (copy_from_user(page
, buf
, count
))
901 loginuid
= simple_strtoul(page
, &tmp
, 10);
907 length
= audit_set_loginuid(task
, loginuid
);
908 if (likely(length
== 0))
912 free_page((unsigned long) page
);
916 static struct file_operations proc_loginuid_operations
= {
917 .read
= proc_loginuid_read
,
918 .write
= proc_loginuid_write
,
922 #ifdef CONFIG_SECCOMP
923 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
924 size_t count
, loff_t
*ppos
)
926 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
928 loff_t __ppos
= *ppos
;
931 /* no need to print the trailing zero, so use only len */
932 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
935 if (count
> len
- __ppos
)
936 count
= len
- __ppos
;
937 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
939 *ppos
= __ppos
+ count
;
943 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
944 size_t count
, loff_t
*ppos
)
946 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
947 char __buf
[20], *end
;
948 unsigned int seccomp_mode
;
950 /* can set it only once to be even more secure */
951 if (unlikely(tsk
->seccomp
.mode
))
954 memset(__buf
, 0, sizeof(__buf
));
955 count
= min(count
, sizeof(__buf
) - 1);
956 if (copy_from_user(__buf
, buf
, count
))
958 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
961 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
962 tsk
->seccomp
.mode
= seccomp_mode
;
963 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
966 if (unlikely(!(end
- __buf
)))
971 static struct file_operations proc_seccomp_operations
= {
972 .read
= seccomp_read
,
973 .write
= seccomp_write
,
975 #endif /* CONFIG_SECCOMP */
977 static int proc_check_dentry_visible(struct inode
*inode
,
978 struct dentry
*dentry
, struct vfsmount
*mnt
)
980 /* Verify that the current process can already see the
981 * file pointed at by the file descriptor.
982 * This prevents /proc from being an accidental information leak.
984 * This prevents access to files that are not visible do to
985 * being on the otherside of a chroot, in a different
986 * namespace, or are simply process local (like pipes).
988 struct task_struct
*task
;
989 struct files_struct
*task_files
, *files
;
992 /* See if the the two tasks share a commone set of
993 * file descriptors. If so everything is visible.
995 task
= proc_task(inode
);
998 files
= get_files_struct(current
);
999 task_files
= get_files_struct(task
);
1000 if (files
&& task_files
&& (files
== task_files
))
1003 put_files_struct(task_files
);
1005 put_files_struct(files
);
1009 /* If the two tasks don't share a common set of file
1010 * descriptors see if the destination dentry is already
1011 * visible in the current tasks filesystem namespace.
1013 error
= proc_check_chroot(dentry
, mnt
);
1019 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1021 struct inode
*inode
= dentry
->d_inode
;
1022 int error
= -EACCES
;
1024 /* We don't need a base pointer in the /proc filesystem */
1027 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1030 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1031 nd
->last_type
= LAST_BIND
;
1035 /* Only return files this task can already see */
1036 error
= proc_check_dentry_visible(inode
, nd
->dentry
, nd
->mnt
);
1040 return ERR_PTR(error
);
1043 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1044 char __user
*buffer
, int buflen
)
1046 struct inode
* inode
;
1047 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1053 inode
= dentry
->d_inode
;
1054 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1055 len
= PTR_ERR(path
);
1058 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1062 if (copy_to_user(buffer
, path
, len
))
1065 free_page((unsigned long)tmp
);
1069 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1071 int error
= -EACCES
;
1072 struct inode
*inode
= dentry
->d_inode
;
1074 struct vfsmount
*mnt
= NULL
;
1077 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1080 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1084 /* Only return files this task can already see */
1085 error
= proc_check_dentry_visible(inode
, de
, mnt
);
1089 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1097 static struct inode_operations proc_pid_link_inode_operations
= {
1098 .readlink
= proc_pid_readlink
,
1099 .follow_link
= proc_pid_follow_link
1104 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1106 struct dentry
*dentry
= filp
->f_dentry
;
1107 struct inode
*inode
= dentry
->d_inode
;
1108 struct task_struct
*p
= proc_task(inode
);
1109 unsigned int fd
, tid
, ino
;
1112 struct files_struct
* files
;
1113 struct fdtable
*fdt
;
1124 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1128 ino
= parent_ino(dentry
);
1129 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1133 files
= get_files_struct(p
);
1137 fdt
= files_fdtable(files
);
1138 for (fd
= filp
->f_pos
-2;
1140 fd
++, filp
->f_pos
++) {
1143 if (!fcheck_files(files
, fd
))
1151 buf
[j
] = '0' + (i
% 10);
1155 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1156 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1163 put_files_struct(files
);
1169 static int proc_pident_readdir(struct file
*filp
,
1170 void *dirent
, filldir_t filldir
,
1171 struct pid_entry
*ents
, unsigned int nents
)
1175 struct dentry
*dentry
= filp
->f_dentry
;
1176 struct inode
*inode
= dentry
->d_inode
;
1177 struct pid_entry
*p
;
1182 if (!pid_alive(proc_task(inode
)))
1186 pid
= proc_task(inode
)->pid
;
1191 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1197 ino
= parent_ino(dentry
);
1198 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1211 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1212 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1224 static int proc_tgid_base_readdir(struct file
* filp
,
1225 void * dirent
, filldir_t filldir
)
1227 return proc_pident_readdir(filp
,dirent
,filldir
,
1228 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1231 static int proc_tid_base_readdir(struct file
* filp
,
1232 void * dirent
, filldir_t filldir
)
1234 return proc_pident_readdir(filp
,dirent
,filldir
,
1235 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1238 /* building an inode */
1240 static int task_dumpable(struct task_struct
*task
)
1243 struct mm_struct
*mm
;
1248 dumpable
= mm
->dumpable
;
1256 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1258 struct inode
* inode
;
1259 struct proc_inode
*ei
;
1261 /* We need a new inode */
1263 inode
= new_inode(sb
);
1269 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1270 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1272 if (!pid_alive(task
))
1276 * grab the reference to task.
1278 get_task_struct(task
);
1282 if (task_dumpable(task
)) {
1283 inode
->i_uid
= task
->euid
;
1284 inode
->i_gid
= task
->egid
;
1286 security_task_to_inode(task
, inode
);
1299 * Exceptional case: normally we are not allowed to unhash a busy
1300 * directory. In this case, however, we can do it - no aliasing problems
1301 * due to the way we treat inodes.
1303 * Rewrite the inode's ownerships here because the owning task may have
1304 * performed a setuid(), etc.
1306 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1308 struct inode
*inode
= dentry
->d_inode
;
1309 struct task_struct
*task
= proc_task(inode
);
1310 if (pid_alive(task
)) {
1311 if (task_dumpable(task
)) {
1312 inode
->i_uid
= task
->euid
;
1313 inode
->i_gid
= task
->egid
;
1318 security_task_to_inode(task
, inode
);
1325 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1327 struct inode
*inode
= dentry
->d_inode
;
1328 struct task_struct
*task
= proc_task(inode
);
1329 int fd
= proc_fd(inode
);
1330 struct files_struct
*files
;
1332 files
= get_files_struct(task
);
1335 if (fcheck_files(files
, fd
)) {
1337 put_files_struct(files
);
1338 if (task_dumpable(task
)) {
1339 inode
->i_uid
= task
->euid
;
1340 inode
->i_gid
= task
->egid
;
1345 security_task_to_inode(task
, inode
);
1349 put_files_struct(files
);
1355 static int pid_delete_dentry(struct dentry
* dentry
)
1357 /* Is the task we represent dead?
1358 * If so, then don't put the dentry on the lru list,
1359 * kill it immediately.
1361 return !pid_alive(proc_task(dentry
->d_inode
));
1364 static struct dentry_operations tid_fd_dentry_operations
=
1366 .d_revalidate
= tid_fd_revalidate
,
1367 .d_delete
= pid_delete_dentry
,
1370 static struct dentry_operations pid_dentry_operations
=
1372 .d_revalidate
= pid_revalidate
,
1373 .d_delete
= pid_delete_dentry
,
1378 static unsigned name_to_int(struct dentry
*dentry
)
1380 const char *name
= dentry
->d_name
.name
;
1381 int len
= dentry
->d_name
.len
;
1384 if (len
> 1 && *name
== '0')
1387 unsigned c
= *name
++ - '0';
1390 if (n
>= (~0U-9)/10)
1401 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1403 struct task_struct
*task
= proc_task(dir
);
1404 unsigned fd
= name_to_int(dentry
);
1405 struct dentry
*result
= ERR_PTR(-ENOENT
);
1407 struct files_struct
* files
;
1408 struct inode
*inode
;
1409 struct proc_inode
*ei
;
1413 if (!pid_alive(task
))
1416 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1421 files
= get_files_struct(task
);
1424 inode
->i_mode
= S_IFLNK
;
1427 * We are not taking a ref to the file structure, so we must
1430 spin_lock(&files
->file_lock
);
1431 file
= fcheck_files(files
, fd
);
1434 if (file
->f_mode
& 1)
1435 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1436 if (file
->f_mode
& 2)
1437 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1438 spin_unlock(&files
->file_lock
);
1439 put_files_struct(files
);
1440 inode
->i_op
= &proc_pid_link_inode_operations
;
1442 ei
->op
.proc_get_link
= proc_fd_link
;
1443 dentry
->d_op
= &tid_fd_dentry_operations
;
1444 d_add(dentry
, inode
);
1445 /* Close the race of the process dying before we return the dentry */
1446 if (tid_fd_revalidate(dentry
, NULL
))
1452 spin_unlock(&files
->file_lock
);
1453 put_files_struct(files
);
1459 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1460 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1461 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
);
1463 static struct file_operations proc_fd_operations
= {
1464 .read
= generic_read_dir
,
1465 .readdir
= proc_readfd
,
1468 static struct file_operations proc_task_operations
= {
1469 .read
= generic_read_dir
,
1470 .readdir
= proc_task_readdir
,
1474 * proc directories can do almost nothing..
1476 static struct inode_operations proc_fd_inode_operations
= {
1477 .lookup
= proc_lookupfd
,
1480 static struct inode_operations proc_task_inode_operations
= {
1481 .lookup
= proc_task_lookup
,
1482 .getattr
= proc_task_getattr
,
1485 #ifdef CONFIG_SECURITY
1486 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
1487 size_t count
, loff_t
*ppos
)
1489 struct inode
* inode
= file
->f_dentry
->d_inode
;
1492 struct task_struct
*task
= proc_task(inode
);
1494 if (count
> PAGE_SIZE
)
1496 if (!(page
= __get_free_page(GFP_KERNEL
)))
1499 length
= security_getprocattr(task
,
1500 (char*)file
->f_dentry
->d_name
.name
,
1501 (void*)page
, count
);
1503 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1508 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1509 size_t count
, loff_t
*ppos
)
1511 struct inode
* inode
= file
->f_dentry
->d_inode
;
1514 struct task_struct
*task
= proc_task(inode
);
1516 if (count
> PAGE_SIZE
)
1519 /* No partial writes. */
1522 page
= (char*)__get_free_page(GFP_USER
);
1526 if (copy_from_user(page
, buf
, count
))
1529 length
= security_setprocattr(task
,
1530 (char*)file
->f_dentry
->d_name
.name
,
1531 (void*)page
, count
);
1533 free_page((unsigned long) page
);
1537 static struct file_operations proc_pid_attr_operations
= {
1538 .read
= proc_pid_attr_read
,
1539 .write
= proc_pid_attr_write
,
1542 static struct file_operations proc_tid_attr_operations
;
1543 static struct inode_operations proc_tid_attr_inode_operations
;
1544 static struct file_operations proc_tgid_attr_operations
;
1545 static struct inode_operations proc_tgid_attr_inode_operations
;
1549 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1550 struct dentry
*dentry
,
1551 struct pid_entry
*ents
)
1553 struct inode
*inode
;
1554 struct dentry
*error
;
1555 struct task_struct
*task
= proc_task(dir
);
1556 struct pid_entry
*p
;
1557 struct proc_inode
*ei
;
1559 error
= ERR_PTR(-ENOENT
);
1562 if (!pid_alive(task
))
1565 for (p
= ents
; p
->name
; p
++) {
1566 if (p
->len
!= dentry
->d_name
.len
)
1568 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1574 error
= ERR_PTR(-EINVAL
);
1575 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1580 inode
->i_mode
= p
->mode
;
1582 * Yes, it does not scale. And it should not. Don't add
1583 * new entries into /proc/<tgid>/ without very good reasons.
1586 case PROC_TGID_TASK
:
1588 inode
->i_op
= &proc_task_inode_operations
;
1589 inode
->i_fop
= &proc_task_operations
;
1594 inode
->i_op
= &proc_fd_inode_operations
;
1595 inode
->i_fop
= &proc_fd_operations
;
1599 inode
->i_op
= &proc_pid_link_inode_operations
;
1600 ei
->op
.proc_get_link
= proc_exe_link
;
1604 inode
->i_op
= &proc_pid_link_inode_operations
;
1605 ei
->op
.proc_get_link
= proc_cwd_link
;
1608 case PROC_TGID_ROOT
:
1609 inode
->i_op
= &proc_pid_link_inode_operations
;
1610 ei
->op
.proc_get_link
= proc_root_link
;
1612 case PROC_TID_ENVIRON
:
1613 case PROC_TGID_ENVIRON
:
1614 inode
->i_fop
= &proc_info_file_operations
;
1615 ei
->op
.proc_read
= proc_pid_environ
;
1618 case PROC_TGID_AUXV
:
1619 inode
->i_fop
= &proc_info_file_operations
;
1620 ei
->op
.proc_read
= proc_pid_auxv
;
1622 case PROC_TID_STATUS
:
1623 case PROC_TGID_STATUS
:
1624 inode
->i_fop
= &proc_info_file_operations
;
1625 ei
->op
.proc_read
= proc_pid_status
;
1628 inode
->i_fop
= &proc_info_file_operations
;
1629 ei
->op
.proc_read
= proc_tid_stat
;
1631 case PROC_TGID_STAT
:
1632 inode
->i_fop
= &proc_info_file_operations
;
1633 ei
->op
.proc_read
= proc_tgid_stat
;
1635 case PROC_TID_CMDLINE
:
1636 case PROC_TGID_CMDLINE
:
1637 inode
->i_fop
= &proc_info_file_operations
;
1638 ei
->op
.proc_read
= proc_pid_cmdline
;
1640 case PROC_TID_STATM
:
1641 case PROC_TGID_STATM
:
1642 inode
->i_fop
= &proc_info_file_operations
;
1643 ei
->op
.proc_read
= proc_pid_statm
;
1646 case PROC_TGID_MAPS
:
1647 inode
->i_fop
= &proc_maps_operations
;
1650 case PROC_TID_NUMA_MAPS
:
1651 case PROC_TGID_NUMA_MAPS
:
1652 inode
->i_fop
= &proc_numa_maps_operations
;
1657 inode
->i_fop
= &proc_mem_operations
;
1659 #ifdef CONFIG_SECCOMP
1660 case PROC_TID_SECCOMP
:
1661 case PROC_TGID_SECCOMP
:
1662 inode
->i_fop
= &proc_seccomp_operations
;
1664 #endif /* CONFIG_SECCOMP */
1665 case PROC_TID_MOUNTS
:
1666 case PROC_TGID_MOUNTS
:
1667 inode
->i_fop
= &proc_mounts_operations
;
1670 case PROC_TID_SMAPS
:
1671 case PROC_TGID_SMAPS
:
1672 inode
->i_fop
= &proc_smaps_operations
;
1675 case PROC_TID_MOUNTSTATS
:
1676 case PROC_TGID_MOUNTSTATS
:
1677 inode
->i_fop
= &proc_mountstats_operations
;
1679 #ifdef CONFIG_SECURITY
1682 inode
->i_op
= &proc_tid_attr_inode_operations
;
1683 inode
->i_fop
= &proc_tid_attr_operations
;
1685 case PROC_TGID_ATTR
:
1687 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1688 inode
->i_fop
= &proc_tgid_attr_operations
;
1690 case PROC_TID_ATTR_CURRENT
:
1691 case PROC_TGID_ATTR_CURRENT
:
1692 case PROC_TID_ATTR_PREV
:
1693 case PROC_TGID_ATTR_PREV
:
1694 case PROC_TID_ATTR_EXEC
:
1695 case PROC_TGID_ATTR_EXEC
:
1696 case PROC_TID_ATTR_FSCREATE
:
1697 case PROC_TGID_ATTR_FSCREATE
:
1698 case PROC_TID_ATTR_KEYCREATE
:
1699 case PROC_TGID_ATTR_KEYCREATE
:
1700 inode
->i_fop
= &proc_pid_attr_operations
;
1703 #ifdef CONFIG_KALLSYMS
1704 case PROC_TID_WCHAN
:
1705 case PROC_TGID_WCHAN
:
1706 inode
->i_fop
= &proc_info_file_operations
;
1707 ei
->op
.proc_read
= proc_pid_wchan
;
1710 #ifdef CONFIG_SCHEDSTATS
1711 case PROC_TID_SCHEDSTAT
:
1712 case PROC_TGID_SCHEDSTAT
:
1713 inode
->i_fop
= &proc_info_file_operations
;
1714 ei
->op
.proc_read
= proc_pid_schedstat
;
1717 #ifdef CONFIG_CPUSETS
1718 case PROC_TID_CPUSET
:
1719 case PROC_TGID_CPUSET
:
1720 inode
->i_fop
= &proc_cpuset_operations
;
1723 case PROC_TID_OOM_SCORE
:
1724 case PROC_TGID_OOM_SCORE
:
1725 inode
->i_fop
= &proc_info_file_operations
;
1726 ei
->op
.proc_read
= proc_oom_score
;
1728 case PROC_TID_OOM_ADJUST
:
1729 case PROC_TGID_OOM_ADJUST
:
1730 inode
->i_fop
= &proc_oom_adjust_operations
;
1732 #ifdef CONFIG_AUDITSYSCALL
1733 case PROC_TID_LOGINUID
:
1734 case PROC_TGID_LOGINUID
:
1735 inode
->i_fop
= &proc_loginuid_operations
;
1739 printk("procfs: impossible type (%d)",p
->type
);
1741 error
= ERR_PTR(-EINVAL
);
1744 dentry
->d_op
= &pid_dentry_operations
;
1745 d_add(dentry
, inode
);
1746 /* Close the race of the process dying before we return the dentry */
1747 if (pid_revalidate(dentry
, NULL
))
1753 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1754 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1757 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1758 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1761 static struct file_operations proc_tgid_base_operations
= {
1762 .read
= generic_read_dir
,
1763 .readdir
= proc_tgid_base_readdir
,
1766 static struct file_operations proc_tid_base_operations
= {
1767 .read
= generic_read_dir
,
1768 .readdir
= proc_tid_base_readdir
,
1771 static struct inode_operations proc_tgid_base_inode_operations
= {
1772 .lookup
= proc_tgid_base_lookup
,
1775 static struct inode_operations proc_tid_base_inode_operations
= {
1776 .lookup
= proc_tid_base_lookup
,
1779 #ifdef CONFIG_SECURITY
1780 static int proc_tgid_attr_readdir(struct file
* filp
,
1781 void * dirent
, filldir_t filldir
)
1783 return proc_pident_readdir(filp
,dirent
,filldir
,
1784 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1787 static int proc_tid_attr_readdir(struct file
* filp
,
1788 void * dirent
, filldir_t filldir
)
1790 return proc_pident_readdir(filp
,dirent
,filldir
,
1791 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1794 static struct file_operations proc_tgid_attr_operations
= {
1795 .read
= generic_read_dir
,
1796 .readdir
= proc_tgid_attr_readdir
,
1799 static struct file_operations proc_tid_attr_operations
= {
1800 .read
= generic_read_dir
,
1801 .readdir
= proc_tid_attr_readdir
,
1804 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1805 struct dentry
*dentry
, struct nameidata
*nd
)
1807 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1810 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1811 struct dentry
*dentry
, struct nameidata
*nd
)
1813 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1816 static struct inode_operations proc_tgid_attr_inode_operations
= {
1817 .lookup
= proc_tgid_attr_lookup
,
1820 static struct inode_operations proc_tid_attr_inode_operations
= {
1821 .lookup
= proc_tid_attr_lookup
,
1828 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1832 sprintf(tmp
, "%d", current
->tgid
);
1833 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1836 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1839 sprintf(tmp
, "%d", current
->tgid
);
1840 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1843 static struct inode_operations proc_self_inode_operations
= {
1844 .readlink
= proc_self_readlink
,
1845 .follow_link
= proc_self_follow_link
,
1849 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
1851 * @task: task that should be flushed.
1853 * Looks in the dcache for
1855 * /proc/@tgid/task/@pid
1856 * if either directory is present flushes it and all of it'ts children
1859 * It is safe and reasonable to cache /proc entries for a task until
1860 * that task exits. After that they just clog up the dcache with
1861 * useless entries, possibly causing useful dcache entries to be
1862 * flushed instead. This routine is proved to flush those useless
1863 * dcache entries at process exit time.
1865 * NOTE: This routine is just an optimization so it does not guarantee
1866 * that no dcache entries will exist at process exit time it
1867 * just makes it very unlikely that any will persist.
1869 void proc_flush_task(struct task_struct
*task
)
1871 struct dentry
*dentry
, *leader
, *dir
;
1876 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
1877 dentry
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
1879 shrink_dcache_parent(dentry
);
1884 if (thread_group_leader(task
))
1888 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->tgid
);
1889 leader
= d_hash_and_lookup(proc_mnt
->mnt_root
, &name
);
1894 name
.len
= strlen(name
.name
);
1895 dir
= d_hash_and_lookup(leader
, &name
);
1897 goto out_put_leader
;
1900 name
.len
= snprintf(buf
, sizeof(buf
), "%d", task
->pid
);
1901 dentry
= d_hash_and_lookup(dir
, &name
);
1903 shrink_dcache_parent(dentry
);
1916 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1918 struct dentry
*result
= ERR_PTR(-ENOENT
);
1919 struct task_struct
*task
;
1920 struct inode
*inode
;
1921 struct proc_inode
*ei
;
1924 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
1925 inode
= new_inode(dir
->i_sb
);
1927 return ERR_PTR(-ENOMEM
);
1929 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1930 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
1932 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
1933 inode
->i_uid
= inode
->i_gid
= 0;
1935 inode
->i_op
= &proc_self_inode_operations
;
1936 d_add(dentry
, inode
);
1939 tgid
= name_to_int(dentry
);
1944 task
= find_task_by_pid(tgid
);
1946 get_task_struct(task
);
1951 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
1955 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1956 inode
->i_op
= &proc_tgid_base_inode_operations
;
1957 inode
->i_fop
= &proc_tgid_base_operations
;
1958 inode
->i_flags
|=S_IMMUTABLE
;
1959 #ifdef CONFIG_SECURITY
1965 dentry
->d_op
= &pid_dentry_operations
;
1967 d_add(dentry
, inode
);
1968 /* Close the race of the process dying before we return the dentry */
1969 if (pid_revalidate(dentry
, NULL
))
1973 put_task_struct(task
);
1979 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1981 struct dentry
*result
= ERR_PTR(-ENOENT
);
1982 struct task_struct
*task
;
1983 struct task_struct
*leader
= proc_task(dir
);
1984 struct inode
*inode
;
1987 tid
= name_to_int(dentry
);
1992 task
= find_task_by_pid(tid
);
1994 get_task_struct(task
);
1998 if (leader
->tgid
!= task
->tgid
)
2001 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2006 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2007 inode
->i_op
= &proc_tid_base_inode_operations
;
2008 inode
->i_fop
= &proc_tid_base_operations
;
2009 inode
->i_flags
|=S_IMMUTABLE
;
2010 #ifdef CONFIG_SECURITY
2016 dentry
->d_op
= &pid_dentry_operations
;
2018 d_add(dentry
, inode
);
2019 /* Close the race of the process dying before we return the dentry */
2020 if (pid_revalidate(dentry
, NULL
))
2024 put_task_struct(task
);
2029 #define PROC_NUMBUF 10
2032 * Find the first tgid to return to user space.
2034 * Usually this is just whatever follows &init_task, but if the users
2035 * buffer was too small to hold the full list or there was a seek into
2036 * the middle of the directory we have more work to do.
2038 * In the case of a short read we start with find_task_by_pid.
2040 * In the case of a seek we start with &init_task and walk nr
2043 static struct task_struct
*first_tgid(int tgid
, int nr
)
2045 struct task_struct
*pos
= NULL
;
2048 pos
= find_task_by_pid(tgid
);
2049 if (pos
&& !thread_group_leader(pos
))
2054 /* If nr exceeds the number of processes get out quickly */
2055 if (nr
&& nr
>= nr_processes())
2058 /* If we haven't found our starting place yet start with
2059 * the init_task and walk nr tasks forward.
2061 if (!pos
&& (nr
>= 0))
2062 pos
= next_task(&init_task
);
2064 for (; pos
&& pid_alive(pos
); pos
= next_task(pos
)) {
2067 get_task_struct(pos
);
2077 * Find the next task in the task list.
2078 * Return NULL if we loop or there is any error.
2080 * The reference to the input task_struct is released.
2082 static struct task_struct
*next_tgid(struct task_struct
*start
)
2084 struct task_struct
*pos
;
2087 if (pid_alive(start
))
2088 pos
= next_task(start
);
2089 if (pid_alive(pos
) && (pos
!= &init_task
)) {
2090 get_task_struct(pos
);
2096 put_task_struct(start
);
2100 /* for the /proc/ directory itself, after non-process stuff has been done */
2101 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2103 char buf
[PROC_NUMBUF
];
2104 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2105 struct task_struct
*task
;
2109 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2110 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2117 /* f_version caches the tgid value that the last readdir call couldn't
2118 * return. lseek aka telldir automagically resets f_version to 0.
2120 tgid
= filp
->f_version
;
2121 filp
->f_version
= 0;
2122 for (task
= first_tgid(tgid
, nr
);
2124 task
= next_tgid(task
), filp
->f_pos
++) {
2128 len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2129 ino
= fake_ino(tgid
, PROC_TGID_INO
);
2130 if (filldir(dirent
, buf
, len
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2131 /* returning this tgid failed, save it as the first
2132 * pid for the next readir call */
2133 filp
->f_version
= tgid
;
2134 put_task_struct(task
);
2142 * Find the first tid of a thread group to return to user space.
2144 * Usually this is just the thread group leader, but if the users
2145 * buffer was too small or there was a seek into the middle of the
2146 * directory we have more work todo.
2148 * In the case of a short read we start with find_task_by_pid.
2150 * In the case of a seek we start with the leader and walk nr
2153 static struct task_struct
*first_tid(struct task_struct
*leader
, int tid
, int nr
)
2155 struct task_struct
*pos
= NULL
;
2156 read_lock(&tasklist_lock
);
2158 /* Attempt to start with the pid of a thread */
2159 if (tid
&& (nr
> 0)) {
2160 pos
= find_task_by_pid(tid
);
2161 if (pos
&& (pos
->group_leader
!= leader
))
2167 /* If nr exceeds the number of threads there is nothing todo */
2172 threads
= atomic_read(&leader
->signal
->count
);
2173 task_unlock(leader
);
2178 /* If we haven't found our starting place yet start with the
2179 * leader and walk nr threads forward.
2181 if (!pos
&& (nr
>= 0))
2184 for (; pos
&& pid_alive(pos
); pos
= next_thread(pos
)) {
2187 get_task_struct(pos
);
2192 read_unlock(&tasklist_lock
);
2197 * Find the next thread in the thread list.
2198 * Return NULL if there is an error or no next thread.
2200 * The reference to the input task_struct is released.
2202 static struct task_struct
*next_tid(struct task_struct
*start
)
2204 struct task_struct
*pos
;
2205 read_lock(&tasklist_lock
);
2207 if (pid_alive(start
))
2208 pos
= next_thread(start
);
2209 if (pid_alive(pos
) && (pos
!= start
->group_leader
))
2210 get_task_struct(pos
);
2213 read_unlock(&tasklist_lock
);
2214 put_task_struct(start
);
2218 /* for the /proc/TGID/task/ directories */
2219 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2221 char buf
[PROC_NUMBUF
];
2222 struct dentry
*dentry
= filp
->f_dentry
;
2223 struct inode
*inode
= dentry
->d_inode
;
2224 struct task_struct
*leader
= proc_task(inode
);
2225 struct task_struct
*task
;
2226 int retval
= -ENOENT
;
2229 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2231 if (!pid_alive(leader
))
2238 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2243 ino
= parent_ino(dentry
);
2244 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2250 /* f_version caches the tgid value that the last readdir call couldn't
2251 * return. lseek aka telldir automagically resets f_version to 0.
2253 tid
= filp
->f_version
;
2254 filp
->f_version
= 0;
2255 for (task
= first_tid(leader
, tid
, pos
- 2);
2257 task
= next_tid(task
), pos
++) {
2260 len
= snprintf(buf
, sizeof(buf
), "%d", tid
);
2261 ino
= fake_ino(tid
, PROC_TID_INO
);
2262 if (filldir(dirent
, buf
, len
, pos
, ino
, DT_DIR
< 0)) {
2263 /* returning this tgid failed, save it as the first
2264 * pid for the next readir call */
2265 filp
->f_version
= tid
;
2266 put_task_struct(task
);
2275 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
2277 struct inode
*inode
= dentry
->d_inode
;
2278 struct task_struct
*p
= proc_task(inode
);
2279 generic_fillattr(inode
, stat
);
2284 stat
->nlink
+= atomic_read(&p
->signal
->count
);