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/file.h>
59 #include <linux/string.h>
60 #include <linux/seq_file.h>
61 #include <linux/namei.h>
62 #include <linux/namespace.h>
64 #include <linux/smp_lock.h>
65 #include <linux/rcupdate.h>
66 #include <linux/kallsyms.h>
67 #include <linux/mount.h>
68 #include <linux/security.h>
69 #include <linux/ptrace.h>
70 #include <linux/seccomp.h>
71 #include <linux/cpuset.h>
72 #include <linux/audit.h>
76 * For hysterical raisins we keep the same inumbers as in the old procfs.
77 * Feel free to change the macro below - just keep the range distinct from
78 * inumbers of the rest of procfs (currently those are in 0x0000--0xffff).
79 * As soon as we'll get a separate superblock we will be able to forget
80 * about magical ranges too.
83 #define fake_ino(pid,ino) (((pid)<<16)|(ino))
85 enum pid_directory_inos
{
109 #ifdef CONFIG_SCHEDSTATS
112 #ifdef CONFIG_CPUSETS
115 #ifdef CONFIG_SECURITY
117 PROC_TGID_ATTR_CURRENT
,
120 PROC_TGID_ATTR_FSCREATE
,
122 #ifdef CONFIG_AUDITSYSCALL
126 PROC_TGID_OOM_ADJUST
,
130 #ifdef CONFIG_SECCOMP
149 #ifdef CONFIG_SCHEDSTATS
152 #ifdef CONFIG_CPUSETS
155 #ifdef CONFIG_SECURITY
157 PROC_TID_ATTR_CURRENT
,
160 PROC_TID_ATTR_FSCREATE
,
162 #ifdef CONFIG_AUDITSYSCALL
168 /* Add new entries before this */
169 PROC_TID_FD_DIR
= 0x8000, /* 0x8000-0xffff */
179 #define E(type,name,mode) {(type),sizeof(name)-1,(name),(mode)}
181 static struct pid_entry tgid_base_stuff
[] = {
182 E(PROC_TGID_TASK
, "task", S_IFDIR
|S_IRUGO
|S_IXUGO
),
183 E(PROC_TGID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
184 E(PROC_TGID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
185 E(PROC_TGID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
186 E(PROC_TGID_STATUS
, "status", S_IFREG
|S_IRUGO
),
187 E(PROC_TGID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
188 E(PROC_TGID_STAT
, "stat", S_IFREG
|S_IRUGO
),
189 E(PROC_TGID_STATM
, "statm", S_IFREG
|S_IRUGO
),
190 E(PROC_TGID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
192 E(PROC_TGID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
194 E(PROC_TGID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
195 #ifdef CONFIG_SECCOMP
196 E(PROC_TGID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
198 E(PROC_TGID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
199 E(PROC_TGID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
200 E(PROC_TGID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
201 E(PROC_TGID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
203 E(PROC_TGID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
205 #ifdef CONFIG_SECURITY
206 E(PROC_TGID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
208 #ifdef CONFIG_KALLSYMS
209 E(PROC_TGID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
211 #ifdef CONFIG_SCHEDSTATS
212 E(PROC_TGID_SCHEDSTAT
, "schedstat", S_IFREG
|S_IRUGO
),
214 #ifdef CONFIG_CPUSETS
215 E(PROC_TGID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
217 E(PROC_TGID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
218 E(PROC_TGID_OOM_ADJUST
,"oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
219 #ifdef CONFIG_AUDITSYSCALL
220 E(PROC_TGID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
224 static struct pid_entry tid_base_stuff
[] = {
225 E(PROC_TID_FD
, "fd", S_IFDIR
|S_IRUSR
|S_IXUSR
),
226 E(PROC_TID_ENVIRON
, "environ", S_IFREG
|S_IRUSR
),
227 E(PROC_TID_AUXV
, "auxv", S_IFREG
|S_IRUSR
),
228 E(PROC_TID_STATUS
, "status", S_IFREG
|S_IRUGO
),
229 E(PROC_TID_CMDLINE
, "cmdline", S_IFREG
|S_IRUGO
),
230 E(PROC_TID_STAT
, "stat", S_IFREG
|S_IRUGO
),
231 E(PROC_TID_STATM
, "statm", S_IFREG
|S_IRUGO
),
232 E(PROC_TID_MAPS
, "maps", S_IFREG
|S_IRUGO
),
234 E(PROC_TID_NUMA_MAPS
, "numa_maps", S_IFREG
|S_IRUGO
),
236 E(PROC_TID_MEM
, "mem", S_IFREG
|S_IRUSR
|S_IWUSR
),
237 #ifdef CONFIG_SECCOMP
238 E(PROC_TID_SECCOMP
, "seccomp", S_IFREG
|S_IRUSR
|S_IWUSR
),
240 E(PROC_TID_CWD
, "cwd", S_IFLNK
|S_IRWXUGO
),
241 E(PROC_TID_ROOT
, "root", S_IFLNK
|S_IRWXUGO
),
242 E(PROC_TID_EXE
, "exe", S_IFLNK
|S_IRWXUGO
),
243 E(PROC_TID_MOUNTS
, "mounts", S_IFREG
|S_IRUGO
),
245 E(PROC_TID_SMAPS
, "smaps", S_IFREG
|S_IRUGO
),
247 #ifdef CONFIG_SECURITY
248 E(PROC_TID_ATTR
, "attr", S_IFDIR
|S_IRUGO
|S_IXUGO
),
250 #ifdef CONFIG_KALLSYMS
251 E(PROC_TID_WCHAN
, "wchan", S_IFREG
|S_IRUGO
),
253 #ifdef CONFIG_SCHEDSTATS
254 E(PROC_TID_SCHEDSTAT
, "schedstat",S_IFREG
|S_IRUGO
),
256 #ifdef CONFIG_CPUSETS
257 E(PROC_TID_CPUSET
, "cpuset", S_IFREG
|S_IRUGO
),
259 E(PROC_TID_OOM_SCORE
, "oom_score",S_IFREG
|S_IRUGO
),
260 E(PROC_TID_OOM_ADJUST
, "oom_adj", S_IFREG
|S_IRUGO
|S_IWUSR
),
261 #ifdef CONFIG_AUDITSYSCALL
262 E(PROC_TID_LOGINUID
, "loginuid", S_IFREG
|S_IWUSR
|S_IRUGO
),
267 #ifdef CONFIG_SECURITY
268 static struct pid_entry tgid_attr_stuff
[] = {
269 E(PROC_TGID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
270 E(PROC_TGID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
271 E(PROC_TGID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
272 E(PROC_TGID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
275 static struct pid_entry tid_attr_stuff
[] = {
276 E(PROC_TID_ATTR_CURRENT
, "current", S_IFREG
|S_IRUGO
|S_IWUGO
),
277 E(PROC_TID_ATTR_PREV
, "prev", S_IFREG
|S_IRUGO
),
278 E(PROC_TID_ATTR_EXEC
, "exec", S_IFREG
|S_IRUGO
|S_IWUGO
),
279 E(PROC_TID_ATTR_FSCREATE
, "fscreate", S_IFREG
|S_IRUGO
|S_IWUGO
),
286 static int proc_fd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
288 struct task_struct
*task
= proc_task(inode
);
289 struct files_struct
*files
;
291 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
293 files
= get_files_struct(task
);
296 file
= fcheck_files(files
, fd
);
298 *mnt
= mntget(file
->f_vfsmnt
);
299 *dentry
= dget(file
->f_dentry
);
301 put_files_struct(files
);
305 put_files_struct(files
);
310 static struct fs_struct
*get_fs_struct(struct task_struct
*task
)
312 struct fs_struct
*fs
;
316 atomic_inc(&fs
->count
);
321 static int proc_cwd_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
323 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
324 int result
= -ENOENT
;
326 read_lock(&fs
->lock
);
327 *mnt
= mntget(fs
->pwdmnt
);
328 *dentry
= dget(fs
->pwd
);
329 read_unlock(&fs
->lock
);
336 static int proc_root_link(struct inode
*inode
, struct dentry
**dentry
, struct vfsmount
**mnt
)
338 struct fs_struct
*fs
= get_fs_struct(proc_task(inode
));
339 int result
= -ENOENT
;
341 read_lock(&fs
->lock
);
342 *mnt
= mntget(fs
->rootmnt
);
343 *dentry
= dget(fs
->root
);
344 read_unlock(&fs
->lock
);
352 /* Same as proc_root_link, but this addionally tries to get fs from other
353 * threads in the group */
354 static int proc_task_root_link(struct inode
*inode
, struct dentry
**dentry
,
355 struct vfsmount
**mnt
)
357 struct fs_struct
*fs
;
358 int result
= -ENOENT
;
359 struct task_struct
*leader
= proc_task(inode
);
364 atomic_inc(&fs
->count
);
367 /* Try to get fs from other threads */
369 read_lock(&tasklist_lock
);
370 if (pid_alive(leader
)) {
371 struct task_struct
*task
= leader
;
373 while ((task
= next_thread(task
)) != leader
) {
377 atomic_inc(&fs
->count
);
384 read_unlock(&tasklist_lock
);
388 read_lock(&fs
->lock
);
389 *mnt
= mntget(fs
->rootmnt
);
390 *dentry
= dget(fs
->root
);
391 read_unlock(&fs
->lock
);
399 #define MAY_PTRACE(task) \
400 (task == current || \
401 (task->parent == current && \
402 (task->ptrace & PT_PTRACED) && \
403 (task->state == TASK_STOPPED || task->state == TASK_TRACED) && \
404 security_ptrace(current,task) == 0))
406 static int proc_pid_environ(struct task_struct
*task
, char * buffer
)
409 struct mm_struct
*mm
= get_task_mm(task
);
411 unsigned int len
= mm
->env_end
- mm
->env_start
;
414 res
= access_process_vm(task
, mm
->env_start
, buffer
, len
, 0);
415 if (!ptrace_may_attach(task
))
422 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
426 struct mm_struct
*mm
= get_task_mm(task
);
430 goto out_mm
; /* Shh! No looking before we're done */
432 len
= mm
->arg_end
- mm
->arg_start
;
437 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
439 // If the nul at the end of args has been overwritten, then
440 // assume application is using setproctitle(3).
441 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
442 len
= strnlen(buffer
, res
);
446 len
= mm
->env_end
- mm
->env_start
;
447 if (len
> PAGE_SIZE
- res
)
448 len
= PAGE_SIZE
- res
;
449 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
450 res
= strnlen(buffer
, res
);
459 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
462 struct mm_struct
*mm
= get_task_mm(task
);
464 unsigned int nwords
= 0;
467 while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
468 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
471 memcpy(buffer
, mm
->saved_auxv
, res
);
478 #ifdef CONFIG_KALLSYMS
480 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
481 * Returns the resolved symbol. If that fails, simply return the address.
483 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
486 const char *sym_name
;
487 unsigned long wchan
, size
, offset
;
488 char namebuf
[KSYM_NAME_LEN
+1];
490 wchan
= get_wchan(task
);
492 sym_name
= kallsyms_lookup(wchan
, &size
, &offset
, &modname
, namebuf
);
494 return sprintf(buffer
, "%s", sym_name
);
495 return sprintf(buffer
, "%lu", wchan
);
497 #endif /* CONFIG_KALLSYMS */
499 #ifdef CONFIG_SCHEDSTATS
501 * Provides /proc/PID/schedstat
503 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
505 return sprintf(buffer
, "%lu %lu %lu\n",
506 task
->sched_info
.cpu_time
,
507 task
->sched_info
.run_delay
,
508 task
->sched_info
.pcnt
);
512 /* The badness from the OOM killer */
513 unsigned long badness(struct task_struct
*p
, unsigned long uptime
);
514 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
516 unsigned long points
;
517 struct timespec uptime
;
519 do_posix_clock_monotonic_gettime(&uptime
);
520 points
= badness(task
, uptime
.tv_sec
);
521 return sprintf(buffer
, "%lu\n", points
);
524 /************************************************************************/
525 /* Here the fs part begins */
526 /************************************************************************/
528 /* permission checks */
530 /* If the process being read is separated by chroot from the reading process,
531 * don't let the reader access the threads.
533 static int proc_check_chroot(struct dentry
*root
, struct vfsmount
*vfsmnt
)
535 struct dentry
*de
, *base
;
536 struct vfsmount
*our_vfsmnt
, *mnt
;
538 read_lock(¤t
->fs
->lock
);
539 our_vfsmnt
= mntget(current
->fs
->rootmnt
);
540 base
= dget(current
->fs
->root
);
541 read_unlock(¤t
->fs
->lock
);
543 spin_lock(&vfsmount_lock
);
547 while (vfsmnt
!= our_vfsmnt
) {
548 if (vfsmnt
== vfsmnt
->mnt_parent
)
550 de
= vfsmnt
->mnt_mountpoint
;
551 vfsmnt
= vfsmnt
->mnt_parent
;
554 if (!is_subdir(de
, base
))
556 spin_unlock(&vfsmount_lock
);
565 spin_unlock(&vfsmount_lock
);
570 static int proc_check_root(struct inode
*inode
)
573 struct vfsmount
*vfsmnt
;
575 if (proc_root_link(inode
, &root
, &vfsmnt
)) /* Ewww... */
577 return proc_check_chroot(root
, vfsmnt
);
580 static int proc_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
582 if (generic_permission(inode
, mask
, NULL
) != 0)
584 return proc_check_root(inode
);
587 static int proc_task_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
590 struct vfsmount
*vfsmnt
;
592 if (generic_permission(inode
, mask
, NULL
) != 0)
595 if (proc_task_root_link(inode
, &root
, &vfsmnt
))
598 return proc_check_chroot(root
, vfsmnt
);
601 extern struct seq_operations proc_pid_maps_op
;
602 static int maps_open(struct inode
*inode
, struct file
*file
)
604 struct task_struct
*task
= proc_task(inode
);
605 int ret
= seq_open(file
, &proc_pid_maps_op
);
607 struct seq_file
*m
= file
->private_data
;
613 static struct file_operations proc_maps_operations
= {
617 .release
= seq_release
,
621 extern struct seq_operations proc_pid_numa_maps_op
;
622 static int numa_maps_open(struct inode
*inode
, struct file
*file
)
624 struct task_struct
*task
= proc_task(inode
);
625 int ret
= seq_open(file
, &proc_pid_numa_maps_op
);
627 struct seq_file
*m
= file
->private_data
;
633 static struct file_operations proc_numa_maps_operations
= {
634 .open
= numa_maps_open
,
637 .release
= seq_release
,
642 extern struct seq_operations proc_pid_smaps_op
;
643 static int smaps_open(struct inode
*inode
, struct file
*file
)
645 struct task_struct
*task
= proc_task(inode
);
646 int ret
= seq_open(file
, &proc_pid_smaps_op
);
648 struct seq_file
*m
= file
->private_data
;
654 static struct file_operations proc_smaps_operations
= {
658 .release
= seq_release
,
662 extern struct seq_operations mounts_op
;
663 static int mounts_open(struct inode
*inode
, struct file
*file
)
665 struct task_struct
*task
= proc_task(inode
);
666 int ret
= seq_open(file
, &mounts_op
);
669 struct seq_file
*m
= file
->private_data
;
670 struct namespace *namespace;
672 namespace = task
->namespace;
674 get_namespace(namespace);
678 m
->private = namespace;
680 seq_release(inode
, file
);
687 static int mounts_release(struct inode
*inode
, struct file
*file
)
689 struct seq_file
*m
= file
->private_data
;
690 struct namespace *namespace = m
->private;
691 put_namespace(namespace);
692 return seq_release(inode
, file
);
695 static struct file_operations proc_mounts_operations
= {
699 .release
= mounts_release
,
702 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
704 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
705 size_t count
, loff_t
*ppos
)
707 struct inode
* inode
= file
->f_dentry
->d_inode
;
710 struct task_struct
*task
= proc_task(inode
);
712 if (count
> PROC_BLOCK_SIZE
)
713 count
= PROC_BLOCK_SIZE
;
714 if (!(page
= __get_free_page(GFP_KERNEL
)))
717 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
720 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
725 static struct file_operations proc_info_file_operations
= {
726 .read
= proc_info_read
,
729 static int mem_open(struct inode
* inode
, struct file
* file
)
731 file
->private_data
= (void*)((long)current
->self_exec_id
);
735 static ssize_t
mem_read(struct file
* file
, char __user
* buf
,
736 size_t count
, loff_t
*ppos
)
738 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
740 unsigned long src
= *ppos
;
742 struct mm_struct
*mm
;
744 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
748 page
= (char *)__get_free_page(GFP_USER
);
754 mm
= get_task_mm(task
);
760 if (file
->private_data
!= (void*)((long)current
->self_exec_id
))
766 int this_len
, retval
;
768 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
769 retval
= access_process_vm(task
, src
, page
, this_len
, 0);
770 if (!retval
|| !MAY_PTRACE(task
) || !ptrace_may_attach(task
)) {
776 if (copy_to_user(buf
, page
, retval
)) {
791 free_page((unsigned long) page
);
796 #define mem_write NULL
799 /* This is a security hazard */
800 static ssize_t
mem_write(struct file
* file
, const char * buf
,
801 size_t count
, loff_t
*ppos
)
805 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
806 unsigned long dst
= *ppos
;
808 if (!MAY_PTRACE(task
) || !ptrace_may_attach(task
))
811 page
= (char *)__get_free_page(GFP_USER
);
816 int this_len
, retval
;
818 this_len
= (count
> PAGE_SIZE
) ? PAGE_SIZE
: count
;
819 if (copy_from_user(page
, buf
, this_len
)) {
823 retval
= access_process_vm(task
, dst
, page
, this_len
, 1);
835 free_page((unsigned long) page
);
840 static loff_t
mem_lseek(struct file
* file
, loff_t offset
, int orig
)
844 file
->f_pos
= offset
;
847 file
->f_pos
+= offset
;
852 force_successful_syscall_return();
856 static struct file_operations proc_mem_operations
= {
863 static ssize_t
oom_adjust_read(struct file
*file
, char __user
*buf
,
864 size_t count
, loff_t
*ppos
)
866 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
869 int oom_adjust
= task
->oomkilladj
;
870 loff_t __ppos
= *ppos
;
872 len
= sprintf(buffer
, "%i\n", oom_adjust
);
875 if (count
> len
-__ppos
)
877 if (copy_to_user(buf
, buffer
+ __ppos
, count
))
879 *ppos
= __ppos
+ count
;
883 static ssize_t
oom_adjust_write(struct file
*file
, const char __user
*buf
,
884 size_t count
, loff_t
*ppos
)
886 struct task_struct
*task
= proc_task(file
->f_dentry
->d_inode
);
887 char buffer
[8], *end
;
890 if (!capable(CAP_SYS_RESOURCE
))
892 memset(buffer
, 0, 8);
895 if (copy_from_user(buffer
, buf
, count
))
897 oom_adjust
= simple_strtol(buffer
, &end
, 0);
898 if ((oom_adjust
< -16 || oom_adjust
> 15) && oom_adjust
!= OOM_DISABLE
)
902 task
->oomkilladj
= oom_adjust
;
903 if (end
- buffer
== 0)
908 static struct file_operations proc_oom_adjust_operations
= {
909 .read
= oom_adjust_read
,
910 .write
= oom_adjust_write
,
913 static struct inode_operations proc_mem_inode_operations
= {
914 .permission
= proc_permission
,
917 #ifdef CONFIG_AUDITSYSCALL
919 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
920 size_t count
, loff_t
*ppos
)
922 struct inode
* inode
= file
->f_dentry
->d_inode
;
923 struct task_struct
*task
= proc_task(inode
);
925 char tmpbuf
[TMPBUFLEN
];
927 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
928 audit_get_loginuid(task
->audit_context
));
929 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
932 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
933 size_t count
, loff_t
*ppos
)
935 struct inode
* inode
= file
->f_dentry
->d_inode
;
938 struct task_struct
*task
= proc_task(inode
);
941 if (!capable(CAP_AUDIT_CONTROL
))
947 if (count
> PAGE_SIZE
)
951 /* No partial writes. */
954 page
= (char*)__get_free_page(GFP_USER
);
958 if (copy_from_user(page
, buf
, count
))
961 loginuid
= simple_strtoul(page
, &tmp
, 10);
967 length
= audit_set_loginuid(task
, loginuid
);
968 if (likely(length
== 0))
972 free_page((unsigned long) page
);
976 static struct file_operations proc_loginuid_operations
= {
977 .read
= proc_loginuid_read
,
978 .write
= proc_loginuid_write
,
982 #ifdef CONFIG_SECCOMP
983 static ssize_t
seccomp_read(struct file
*file
, char __user
*buf
,
984 size_t count
, loff_t
*ppos
)
986 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
988 loff_t __ppos
= *ppos
;
991 /* no need to print the trailing zero, so use only len */
992 len
= sprintf(__buf
, "%u\n", tsk
->seccomp
.mode
);
995 if (count
> len
- __ppos
)
996 count
= len
- __ppos
;
997 if (copy_to_user(buf
, __buf
+ __ppos
, count
))
999 *ppos
= __ppos
+ count
;
1003 static ssize_t
seccomp_write(struct file
*file
, const char __user
*buf
,
1004 size_t count
, loff_t
*ppos
)
1006 struct task_struct
*tsk
= proc_task(file
->f_dentry
->d_inode
);
1007 char __buf
[20], *end
;
1008 unsigned int seccomp_mode
;
1010 /* can set it only once to be even more secure */
1011 if (unlikely(tsk
->seccomp
.mode
))
1014 memset(__buf
, 0, sizeof(__buf
));
1015 count
= min(count
, sizeof(__buf
) - 1);
1016 if (copy_from_user(__buf
, buf
, count
))
1018 seccomp_mode
= simple_strtoul(__buf
, &end
, 0);
1021 if (seccomp_mode
&& seccomp_mode
<= NR_SECCOMP_MODES
) {
1022 tsk
->seccomp
.mode
= seccomp_mode
;
1023 set_tsk_thread_flag(tsk
, TIF_SECCOMP
);
1026 if (unlikely(!(end
- __buf
)))
1031 static struct file_operations proc_seccomp_operations
= {
1032 .read
= seccomp_read
,
1033 .write
= seccomp_write
,
1035 #endif /* CONFIG_SECCOMP */
1037 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1039 struct inode
*inode
= dentry
->d_inode
;
1040 int error
= -EACCES
;
1042 /* We don't need a base pointer in the /proc filesystem */
1045 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1047 error
= proc_check_root(inode
);
1051 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &nd
->dentry
, &nd
->mnt
);
1052 nd
->last_type
= LAST_BIND
;
1054 return ERR_PTR(error
);
1057 static int do_proc_readlink(struct dentry
*dentry
, struct vfsmount
*mnt
,
1058 char __user
*buffer
, int buflen
)
1060 struct inode
* inode
;
1061 char *tmp
= (char*)__get_free_page(GFP_KERNEL
), *path
;
1067 inode
= dentry
->d_inode
;
1068 path
= d_path(dentry
, mnt
, tmp
, PAGE_SIZE
);
1069 len
= PTR_ERR(path
);
1072 len
= tmp
+ PAGE_SIZE
- 1 - path
;
1076 if (copy_to_user(buffer
, path
, len
))
1079 free_page((unsigned long)tmp
);
1083 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1085 int error
= -EACCES
;
1086 struct inode
*inode
= dentry
->d_inode
;
1088 struct vfsmount
*mnt
= NULL
;
1092 if (current
->fsuid
!= inode
->i_uid
&& !capable(CAP_DAC_OVERRIDE
))
1094 error
= proc_check_root(inode
);
1098 error
= PROC_I(inode
)->op
.proc_get_link(inode
, &de
, &mnt
);
1102 error
= do_proc_readlink(de
, mnt
, buffer
, buflen
);
1110 static struct inode_operations proc_pid_link_inode_operations
= {
1111 .readlink
= proc_pid_readlink
,
1112 .follow_link
= proc_pid_follow_link
1117 static int proc_readfd(struct file
* filp
, void * dirent
, filldir_t filldir
)
1119 struct inode
*inode
= filp
->f_dentry
->d_inode
;
1120 struct task_struct
*p
= proc_task(inode
);
1121 unsigned int fd
, tid
, ino
;
1124 struct files_struct
* files
;
1125 struct fdtable
*fdt
;
1136 if (filldir(dirent
, ".", 1, 0, inode
->i_ino
, DT_DIR
) < 0)
1140 ino
= fake_ino(tid
, PROC_TID_INO
);
1141 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1145 files
= get_files_struct(p
);
1149 fdt
= files_fdtable(files
);
1150 for (fd
= filp
->f_pos
-2;
1152 fd
++, filp
->f_pos
++) {
1155 if (!fcheck_files(files
, fd
))
1163 buf
[j
] = '0' + (i
% 10);
1167 ino
= fake_ino(tid
, PROC_TID_FD_DIR
+ fd
);
1168 if (filldir(dirent
, buf
+j
, NUMBUF
-j
, fd
+2, ino
, DT_LNK
) < 0) {
1175 put_files_struct(files
);
1181 static int proc_pident_readdir(struct file
*filp
,
1182 void *dirent
, filldir_t filldir
,
1183 struct pid_entry
*ents
, unsigned int nents
)
1187 struct dentry
*dentry
= filp
->f_dentry
;
1188 struct inode
*inode
= dentry
->d_inode
;
1189 struct pid_entry
*p
;
1194 if (!pid_alive(proc_task(inode
)))
1198 pid
= proc_task(inode
)->pid
;
1203 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
1209 ino
= parent_ino(dentry
);
1210 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
1223 if (filldir(dirent
, p
->name
, p
->len
, filp
->f_pos
,
1224 fake_ino(pid
, p
->type
), p
->mode
>> 12) < 0)
1236 static int proc_tgid_base_readdir(struct file
* filp
,
1237 void * dirent
, filldir_t filldir
)
1239 return proc_pident_readdir(filp
,dirent
,filldir
,
1240 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
1243 static int proc_tid_base_readdir(struct file
* filp
,
1244 void * dirent
, filldir_t filldir
)
1246 return proc_pident_readdir(filp
,dirent
,filldir
,
1247 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
1250 /* building an inode */
1252 static int task_dumpable(struct task_struct
*task
)
1255 struct mm_struct
*mm
;
1260 dumpable
= mm
->dumpable
;
1268 static struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
, int ino
)
1270 struct inode
* inode
;
1271 struct proc_inode
*ei
;
1273 /* We need a new inode */
1275 inode
= new_inode(sb
);
1282 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1283 inode
->i_ino
= fake_ino(task
->pid
, ino
);
1285 if (!pid_alive(task
))
1289 * grab the reference to task.
1291 get_task_struct(task
);
1296 if (ino
== PROC_TGID_INO
|| ino
== PROC_TID_INO
|| task_dumpable(task
)) {
1297 inode
->i_uid
= task
->euid
;
1298 inode
->i_gid
= task
->egid
;
1300 security_task_to_inode(task
, inode
);
1314 * Exceptional case: normally we are not allowed to unhash a busy
1315 * directory. In this case, however, we can do it - no aliasing problems
1316 * due to the way we treat inodes.
1318 * Rewrite the inode's ownerships here because the owning task may have
1319 * performed a setuid(), etc.
1321 static int pid_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1323 struct inode
*inode
= dentry
->d_inode
;
1324 struct task_struct
*task
= proc_task(inode
);
1325 if (pid_alive(task
)) {
1326 if (proc_type(inode
) == PROC_TGID_INO
|| proc_type(inode
) == PROC_TID_INO
|| task_dumpable(task
)) {
1327 inode
->i_uid
= task
->euid
;
1328 inode
->i_gid
= task
->egid
;
1333 security_task_to_inode(task
, inode
);
1340 static int tid_fd_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
1342 struct inode
*inode
= dentry
->d_inode
;
1343 struct task_struct
*task
= proc_task(inode
);
1344 int fd
= proc_type(inode
) - PROC_TID_FD_DIR
;
1345 struct files_struct
*files
;
1347 files
= get_files_struct(task
);
1350 if (fcheck_files(files
, fd
)) {
1352 put_files_struct(files
);
1353 if (task_dumpable(task
)) {
1354 inode
->i_uid
= task
->euid
;
1355 inode
->i_gid
= task
->egid
;
1360 security_task_to_inode(task
, inode
);
1364 put_files_struct(files
);
1370 static void pid_base_iput(struct dentry
*dentry
, struct inode
*inode
)
1372 struct task_struct
*task
= proc_task(inode
);
1373 spin_lock(&task
->proc_lock
);
1374 if (task
->proc_dentry
== dentry
)
1375 task
->proc_dentry
= NULL
;
1376 spin_unlock(&task
->proc_lock
);
1380 static int pid_delete_dentry(struct dentry
* dentry
)
1382 /* Is the task we represent dead?
1383 * If so, then don't put the dentry on the lru list,
1384 * kill it immediately.
1386 return !pid_alive(proc_task(dentry
->d_inode
));
1389 static struct dentry_operations tid_fd_dentry_operations
=
1391 .d_revalidate
= tid_fd_revalidate
,
1392 .d_delete
= pid_delete_dentry
,
1395 static struct dentry_operations pid_dentry_operations
=
1397 .d_revalidate
= pid_revalidate
,
1398 .d_delete
= pid_delete_dentry
,
1401 static struct dentry_operations pid_base_dentry_operations
=
1403 .d_revalidate
= pid_revalidate
,
1404 .d_iput
= pid_base_iput
,
1405 .d_delete
= pid_delete_dentry
,
1410 static unsigned name_to_int(struct dentry
*dentry
)
1412 const char *name
= dentry
->d_name
.name
;
1413 int len
= dentry
->d_name
.len
;
1416 if (len
> 1 && *name
== '0')
1419 unsigned c
= *name
++ - '0';
1422 if (n
>= (~0U-9)/10)
1433 static struct dentry
*proc_lookupfd(struct inode
* dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1435 struct task_struct
*task
= proc_task(dir
);
1436 unsigned fd
= name_to_int(dentry
);
1438 struct files_struct
* files
;
1439 struct inode
*inode
;
1440 struct proc_inode
*ei
;
1444 if (!pid_alive(task
))
1447 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_FD_DIR
+fd
);
1451 files
= get_files_struct(task
);
1454 inode
->i_mode
= S_IFLNK
;
1456 file
= fcheck_files(files
, fd
);
1459 if (file
->f_mode
& 1)
1460 inode
->i_mode
|= S_IRUSR
| S_IXUSR
;
1461 if (file
->f_mode
& 2)
1462 inode
->i_mode
|= S_IWUSR
| S_IXUSR
;
1464 put_files_struct(files
);
1465 inode
->i_op
= &proc_pid_link_inode_operations
;
1467 ei
->op
.proc_get_link
= proc_fd_link
;
1468 dentry
->d_op
= &tid_fd_dentry_operations
;
1469 d_add(dentry
, inode
);
1474 put_files_struct(files
);
1478 return ERR_PTR(-ENOENT
);
1481 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
1482 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
);
1484 static struct file_operations proc_fd_operations
= {
1485 .read
= generic_read_dir
,
1486 .readdir
= proc_readfd
,
1489 static struct file_operations proc_task_operations
= {
1490 .read
= generic_read_dir
,
1491 .readdir
= proc_task_readdir
,
1495 * proc directories can do almost nothing..
1497 static struct inode_operations proc_fd_inode_operations
= {
1498 .lookup
= proc_lookupfd
,
1499 .permission
= proc_permission
,
1502 static struct inode_operations proc_task_inode_operations
= {
1503 .lookup
= proc_task_lookup
,
1504 .permission
= proc_task_permission
,
1507 #ifdef CONFIG_SECURITY
1508 static ssize_t
proc_pid_attr_read(struct file
* file
, 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
)
1518 if (!(page
= __get_free_page(GFP_KERNEL
)))
1521 length
= security_getprocattr(task
,
1522 (char*)file
->f_dentry
->d_name
.name
,
1523 (void*)page
, count
);
1525 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
1530 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
1531 size_t count
, loff_t
*ppos
)
1533 struct inode
* inode
= file
->f_dentry
->d_inode
;
1536 struct task_struct
*task
= proc_task(inode
);
1538 if (count
> PAGE_SIZE
)
1541 /* No partial writes. */
1544 page
= (char*)__get_free_page(GFP_USER
);
1548 if (copy_from_user(page
, buf
, count
))
1551 length
= security_setprocattr(task
,
1552 (char*)file
->f_dentry
->d_name
.name
,
1553 (void*)page
, count
);
1555 free_page((unsigned long) page
);
1559 static struct file_operations proc_pid_attr_operations
= {
1560 .read
= proc_pid_attr_read
,
1561 .write
= proc_pid_attr_write
,
1564 static struct file_operations proc_tid_attr_operations
;
1565 static struct inode_operations proc_tid_attr_inode_operations
;
1566 static struct file_operations proc_tgid_attr_operations
;
1567 static struct inode_operations proc_tgid_attr_inode_operations
;
1570 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
);
1573 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
1574 struct dentry
*dentry
,
1575 struct pid_entry
*ents
)
1577 struct inode
*inode
;
1579 struct task_struct
*task
= proc_task(dir
);
1580 struct pid_entry
*p
;
1581 struct proc_inode
*ei
;
1586 if (!pid_alive(task
))
1589 for (p
= ents
; p
->name
; p
++) {
1590 if (p
->len
!= dentry
->d_name
.len
)
1592 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
1599 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->type
);
1604 inode
->i_mode
= p
->mode
;
1606 * Yes, it does not scale. And it should not. Don't add
1607 * new entries into /proc/<tgid>/ without very good reasons.
1610 case PROC_TGID_TASK
:
1611 inode
->i_nlink
= 2 + get_tid_list(2, NULL
, dir
);
1612 inode
->i_op
= &proc_task_inode_operations
;
1613 inode
->i_fop
= &proc_task_operations
;
1618 inode
->i_op
= &proc_fd_inode_operations
;
1619 inode
->i_fop
= &proc_fd_operations
;
1623 inode
->i_op
= &proc_pid_link_inode_operations
;
1624 ei
->op
.proc_get_link
= proc_exe_link
;
1628 inode
->i_op
= &proc_pid_link_inode_operations
;
1629 ei
->op
.proc_get_link
= proc_cwd_link
;
1632 case PROC_TGID_ROOT
:
1633 inode
->i_op
= &proc_pid_link_inode_operations
;
1634 ei
->op
.proc_get_link
= proc_root_link
;
1636 case PROC_TID_ENVIRON
:
1637 case PROC_TGID_ENVIRON
:
1638 inode
->i_fop
= &proc_info_file_operations
;
1639 ei
->op
.proc_read
= proc_pid_environ
;
1642 case PROC_TGID_AUXV
:
1643 inode
->i_fop
= &proc_info_file_operations
;
1644 ei
->op
.proc_read
= proc_pid_auxv
;
1646 case PROC_TID_STATUS
:
1647 case PROC_TGID_STATUS
:
1648 inode
->i_fop
= &proc_info_file_operations
;
1649 ei
->op
.proc_read
= proc_pid_status
;
1652 inode
->i_fop
= &proc_info_file_operations
;
1653 ei
->op
.proc_read
= proc_tid_stat
;
1655 case PROC_TGID_STAT
:
1656 inode
->i_fop
= &proc_info_file_operations
;
1657 ei
->op
.proc_read
= proc_tgid_stat
;
1659 case PROC_TID_CMDLINE
:
1660 case PROC_TGID_CMDLINE
:
1661 inode
->i_fop
= &proc_info_file_operations
;
1662 ei
->op
.proc_read
= proc_pid_cmdline
;
1664 case PROC_TID_STATM
:
1665 case PROC_TGID_STATM
:
1666 inode
->i_fop
= &proc_info_file_operations
;
1667 ei
->op
.proc_read
= proc_pid_statm
;
1670 case PROC_TGID_MAPS
:
1671 inode
->i_fop
= &proc_maps_operations
;
1674 case PROC_TID_NUMA_MAPS
:
1675 case PROC_TGID_NUMA_MAPS
:
1676 inode
->i_fop
= &proc_numa_maps_operations
;
1681 inode
->i_op
= &proc_mem_inode_operations
;
1682 inode
->i_fop
= &proc_mem_operations
;
1684 #ifdef CONFIG_SECCOMP
1685 case PROC_TID_SECCOMP
:
1686 case PROC_TGID_SECCOMP
:
1687 inode
->i_fop
= &proc_seccomp_operations
;
1689 #endif /* CONFIG_SECCOMP */
1690 case PROC_TID_MOUNTS
:
1691 case PROC_TGID_MOUNTS
:
1692 inode
->i_fop
= &proc_mounts_operations
;
1695 case PROC_TID_SMAPS
:
1696 case PROC_TGID_SMAPS
:
1697 inode
->i_fop
= &proc_smaps_operations
;
1700 #ifdef CONFIG_SECURITY
1703 inode
->i_op
= &proc_tid_attr_inode_operations
;
1704 inode
->i_fop
= &proc_tid_attr_operations
;
1706 case PROC_TGID_ATTR
:
1708 inode
->i_op
= &proc_tgid_attr_inode_operations
;
1709 inode
->i_fop
= &proc_tgid_attr_operations
;
1711 case PROC_TID_ATTR_CURRENT
:
1712 case PROC_TGID_ATTR_CURRENT
:
1713 case PROC_TID_ATTR_PREV
:
1714 case PROC_TGID_ATTR_PREV
:
1715 case PROC_TID_ATTR_EXEC
:
1716 case PROC_TGID_ATTR_EXEC
:
1717 case PROC_TID_ATTR_FSCREATE
:
1718 case PROC_TGID_ATTR_FSCREATE
:
1719 inode
->i_fop
= &proc_pid_attr_operations
;
1722 #ifdef CONFIG_KALLSYMS
1723 case PROC_TID_WCHAN
:
1724 case PROC_TGID_WCHAN
:
1725 inode
->i_fop
= &proc_info_file_operations
;
1726 ei
->op
.proc_read
= proc_pid_wchan
;
1729 #ifdef CONFIG_SCHEDSTATS
1730 case PROC_TID_SCHEDSTAT
:
1731 case PROC_TGID_SCHEDSTAT
:
1732 inode
->i_fop
= &proc_info_file_operations
;
1733 ei
->op
.proc_read
= proc_pid_schedstat
;
1736 #ifdef CONFIG_CPUSETS
1737 case PROC_TID_CPUSET
:
1738 case PROC_TGID_CPUSET
:
1739 inode
->i_fop
= &proc_cpuset_operations
;
1742 case PROC_TID_OOM_SCORE
:
1743 case PROC_TGID_OOM_SCORE
:
1744 inode
->i_fop
= &proc_info_file_operations
;
1745 ei
->op
.proc_read
= proc_oom_score
;
1747 case PROC_TID_OOM_ADJUST
:
1748 case PROC_TGID_OOM_ADJUST
:
1749 inode
->i_fop
= &proc_oom_adjust_operations
;
1751 #ifdef CONFIG_AUDITSYSCALL
1752 case PROC_TID_LOGINUID
:
1753 case PROC_TGID_LOGINUID
:
1754 inode
->i_fop
= &proc_loginuid_operations
;
1758 printk("procfs: impossible type (%d)",p
->type
);
1760 return ERR_PTR(-EINVAL
);
1762 dentry
->d_op
= &pid_dentry_operations
;
1763 d_add(dentry
, inode
);
1767 return ERR_PTR(error
);
1770 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1771 return proc_pident_lookup(dir
, dentry
, tgid_base_stuff
);
1774 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
){
1775 return proc_pident_lookup(dir
, dentry
, tid_base_stuff
);
1778 static struct file_operations proc_tgid_base_operations
= {
1779 .read
= generic_read_dir
,
1780 .readdir
= proc_tgid_base_readdir
,
1783 static struct file_operations proc_tid_base_operations
= {
1784 .read
= generic_read_dir
,
1785 .readdir
= proc_tid_base_readdir
,
1788 static struct inode_operations proc_tgid_base_inode_operations
= {
1789 .lookup
= proc_tgid_base_lookup
,
1792 static struct inode_operations proc_tid_base_inode_operations
= {
1793 .lookup
= proc_tid_base_lookup
,
1796 #ifdef CONFIG_SECURITY
1797 static int proc_tgid_attr_readdir(struct file
* filp
,
1798 void * dirent
, filldir_t filldir
)
1800 return proc_pident_readdir(filp
,dirent
,filldir
,
1801 tgid_attr_stuff
,ARRAY_SIZE(tgid_attr_stuff
));
1804 static int proc_tid_attr_readdir(struct file
* filp
,
1805 void * dirent
, filldir_t filldir
)
1807 return proc_pident_readdir(filp
,dirent
,filldir
,
1808 tid_attr_stuff
,ARRAY_SIZE(tid_attr_stuff
));
1811 static struct file_operations proc_tgid_attr_operations
= {
1812 .read
= generic_read_dir
,
1813 .readdir
= proc_tgid_attr_readdir
,
1816 static struct file_operations proc_tid_attr_operations
= {
1817 .read
= generic_read_dir
,
1818 .readdir
= proc_tid_attr_readdir
,
1821 static struct dentry
*proc_tgid_attr_lookup(struct inode
*dir
,
1822 struct dentry
*dentry
, struct nameidata
*nd
)
1824 return proc_pident_lookup(dir
, dentry
, tgid_attr_stuff
);
1827 static struct dentry
*proc_tid_attr_lookup(struct inode
*dir
,
1828 struct dentry
*dentry
, struct nameidata
*nd
)
1830 return proc_pident_lookup(dir
, dentry
, tid_attr_stuff
);
1833 static struct inode_operations proc_tgid_attr_inode_operations
= {
1834 .lookup
= proc_tgid_attr_lookup
,
1837 static struct inode_operations proc_tid_attr_inode_operations
= {
1838 .lookup
= proc_tid_attr_lookup
,
1845 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
1849 sprintf(tmp
, "%d", current
->tgid
);
1850 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
1853 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1856 sprintf(tmp
, "%d", current
->tgid
);
1857 return ERR_PTR(vfs_follow_link(nd
,tmp
));
1860 static struct inode_operations proc_self_inode_operations
= {
1861 .readlink
= proc_self_readlink
,
1862 .follow_link
= proc_self_follow_link
,
1866 * proc_pid_unhash - Unhash /proc/@pid entry from the dcache.
1867 * @p: task that should be flushed.
1869 * Drops the /proc/@pid dcache entry from the hash chains.
1871 * Dropping /proc/@pid entries and detach_pid must be synchroneous,
1872 * otherwise e.g. /proc/@pid/exe might point to the wrong executable,
1873 * if the pid value is immediately reused. This is enforced by
1874 * - caller must acquire spin_lock(p->proc_lock)
1875 * - must be called before detach_pid()
1876 * - proc_pid_lookup acquires proc_lock, and checks that
1877 * the target is not dead by looking at the attach count
1881 struct dentry
*proc_pid_unhash(struct task_struct
*p
)
1883 struct dentry
*proc_dentry
;
1885 proc_dentry
= p
->proc_dentry
;
1886 if (proc_dentry
!= NULL
) {
1888 spin_lock(&dcache_lock
);
1889 spin_lock(&proc_dentry
->d_lock
);
1890 if (!d_unhashed(proc_dentry
)) {
1891 dget_locked(proc_dentry
);
1892 __d_drop(proc_dentry
);
1893 spin_unlock(&proc_dentry
->d_lock
);
1895 spin_unlock(&proc_dentry
->d_lock
);
1898 spin_unlock(&dcache_lock
);
1904 * proc_pid_flush - recover memory used by stale /proc/@pid/x entries
1905 * @proc_dentry: directoy to prune.
1907 * Shrink the /proc directory that was used by the just killed thread.
1910 void proc_pid_flush(struct dentry
*proc_dentry
)
1913 if(proc_dentry
!= NULL
) {
1914 shrink_dcache_parent(proc_dentry
);
1920 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1922 struct task_struct
*task
;
1923 struct inode
*inode
;
1924 struct proc_inode
*ei
;
1928 if (dentry
->d_name
.len
== 4 && !memcmp(dentry
->d_name
.name
,"self",4)) {
1929 inode
= new_inode(dir
->i_sb
);
1931 return ERR_PTR(-ENOMEM
);
1933 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1934 inode
->i_ino
= fake_ino(0, PROC_TGID_INO
);
1936 inode
->i_mode
= S_IFLNK
|S_IRWXUGO
;
1937 inode
->i_uid
= inode
->i_gid
= 0;
1939 inode
->i_op
= &proc_self_inode_operations
;
1940 d_add(dentry
, inode
);
1943 tgid
= name_to_int(dentry
);
1947 read_lock(&tasklist_lock
);
1948 task
= find_task_by_pid(tgid
);
1950 get_task_struct(task
);
1951 read_unlock(&tasklist_lock
);
1955 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TGID_INO
);
1959 put_task_struct(task
);
1962 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
1963 inode
->i_op
= &proc_tgid_base_inode_operations
;
1964 inode
->i_fop
= &proc_tgid_base_operations
;
1965 inode
->i_flags
|=S_IMMUTABLE
;
1966 #ifdef CONFIG_SECURITY
1972 dentry
->d_op
= &pid_base_dentry_operations
;
1975 d_add(dentry
, inode
);
1976 spin_lock(&task
->proc_lock
);
1977 task
->proc_dentry
= dentry
;
1978 if (!pid_alive(task
)) {
1979 dentry
= proc_pid_unhash(task
);
1982 spin_unlock(&task
->proc_lock
);
1984 put_task_struct(task
);
1986 proc_pid_flush(dentry
);
1991 return ERR_PTR(-ENOENT
);
1995 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, struct nameidata
*nd
)
1997 struct task_struct
*task
;
1998 struct task_struct
*leader
= proc_task(dir
);
1999 struct inode
*inode
;
2002 tid
= name_to_int(dentry
);
2006 read_lock(&tasklist_lock
);
2007 task
= find_task_by_pid(tid
);
2009 get_task_struct(task
);
2010 read_unlock(&tasklist_lock
);
2013 if (leader
->tgid
!= task
->tgid
)
2016 inode
= proc_pid_make_inode(dir
->i_sb
, task
, PROC_TID_INO
);
2021 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2022 inode
->i_op
= &proc_tid_base_inode_operations
;
2023 inode
->i_fop
= &proc_tid_base_operations
;
2024 inode
->i_flags
|=S_IMMUTABLE
;
2025 #ifdef CONFIG_SECURITY
2031 dentry
->d_op
= &pid_base_dentry_operations
;
2033 d_add(dentry
, inode
);
2035 put_task_struct(task
);
2038 put_task_struct(task
);
2040 return ERR_PTR(-ENOENT
);
2043 #define PROC_NUMBUF 10
2044 #define PROC_MAXPIDS 20
2047 * Get a few tgid's to return for filldir - we need to hold the
2048 * tasklist lock while doing this, and we must release it before
2049 * we actually do the filldir itself, so we use a temp buffer..
2051 static int get_tgid_list(int index
, unsigned long version
, unsigned int *tgids
)
2053 struct task_struct
*p
;
2057 read_lock(&tasklist_lock
);
2060 p
= find_task_by_pid(version
);
2061 if (p
&& !thread_group_leader(p
))
2068 p
= next_task(&init_task
);
2070 for ( ; p
!= &init_task
; p
= next_task(p
)) {
2076 tgids
[nr_tgids
] = tgid
;
2078 if (nr_tgids
>= PROC_MAXPIDS
)
2081 read_unlock(&tasklist_lock
);
2086 * Get a few tid's to return for filldir - we need to hold the
2087 * tasklist lock while doing this, and we must release it before
2088 * we actually do the filldir itself, so we use a temp buffer..
2090 static int get_tid_list(int index
, unsigned int *tids
, struct inode
*dir
)
2092 struct task_struct
*leader_task
= proc_task(dir
);
2093 struct task_struct
*task
= leader_task
;
2097 read_lock(&tasklist_lock
);
2099 * The starting point task (leader_task) might be an already
2100 * unlinked task, which cannot be used to access the task-list
2101 * via next_thread().
2103 if (pid_alive(task
)) do {
2104 int tid
= task
->pid
;
2109 tids
[nr_tids
] = tid
;
2111 if (nr_tids
>= PROC_MAXPIDS
)
2113 } while ((task
= next_thread(task
)) != leader_task
);
2114 read_unlock(&tasklist_lock
);
2118 /* for the /proc/ directory itself, after non-process stuff has been done */
2119 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2121 unsigned int tgid_array
[PROC_MAXPIDS
];
2122 char buf
[PROC_NUMBUF
];
2123 unsigned int nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2124 unsigned int nr_tgids
, i
;
2128 ino_t ino
= fake_ino(0,PROC_TGID_INO
);
2129 if (filldir(dirent
, "self", 4, filp
->f_pos
, ino
, DT_LNK
) < 0)
2135 /* f_version caches the tgid value that the last readdir call couldn't
2136 * return. lseek aka telldir automagically resets f_version to 0.
2138 next_tgid
= filp
->f_version
;
2139 filp
->f_version
= 0;
2141 nr_tgids
= get_tgid_list(nr
, next_tgid
, tgid_array
);
2143 /* no more entries ! */
2148 /* do not use the last found pid, reserve it for next_tgid */
2149 if (nr_tgids
== PROC_MAXPIDS
) {
2151 next_tgid
= tgid_array
[nr_tgids
];
2154 for (i
=0;i
<nr_tgids
;i
++) {
2155 int tgid
= tgid_array
[i
];
2156 ino_t ino
= fake_ino(tgid
,PROC_TGID_INO
);
2157 unsigned long j
= PROC_NUMBUF
;
2160 buf
[--j
] = '0' + (tgid
% 10);
2161 while ((tgid
/= 10) != 0);
2163 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, filp
->f_pos
, ino
, DT_DIR
) < 0) {
2164 /* returning this tgid failed, save it as the first
2165 * pid for the next readir call */
2166 filp
->f_version
= tgid_array
[i
];
2177 /* for the /proc/TGID/task/ directories */
2178 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2180 unsigned int tid_array
[PROC_MAXPIDS
];
2181 char buf
[PROC_NUMBUF
];
2182 unsigned int nr_tids
, i
;
2183 struct dentry
*dentry
= filp
->f_dentry
;
2184 struct inode
*inode
= dentry
->d_inode
;
2185 int retval
= -ENOENT
;
2187 unsigned long pos
= filp
->f_pos
; /* avoiding "long long" filp->f_pos */
2189 if (!pid_alive(proc_task(inode
)))
2196 if (filldir(dirent
, ".", 1, pos
, ino
, DT_DIR
) < 0)
2201 ino
= parent_ino(dentry
);
2202 if (filldir(dirent
, "..", 2, pos
, ino
, DT_DIR
) < 0)
2208 nr_tids
= get_tid_list(pos
, tid_array
, inode
);
2209 inode
->i_nlink
= pos
+ nr_tids
;
2211 for (i
= 0; i
< nr_tids
; i
++) {
2212 unsigned long j
= PROC_NUMBUF
;
2213 int tid
= tid_array
[i
];
2215 ino
= fake_ino(tid
,PROC_TID_INO
);
2218 buf
[--j
] = '0' + (tid
% 10);
2219 while ((tid
/= 10) != 0);
2221 if (filldir(dirent
, buf
+j
, PROC_NUMBUF
-j
, pos
, ino
, DT_DIR
) < 0)