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/errno.h>
53 #include <linux/time.h>
54 #include <linux/proc_fs.h>
55 #include <linux/stat.h>
56 #include <linux/task_io_accounting_ops.h>
57 #include <linux/init.h>
58 #include <linux/capability.h>
59 #include <linux/file.h>
60 #include <linux/fdtable.h>
61 #include <linux/string.h>
62 #include <linux/seq_file.h>
63 #include <linux/namei.h>
64 #include <linux/mnt_namespace.h>
66 #include <linux/swap.h>
67 #include <linux/rcupdate.h>
68 #include <linux/kallsyms.h>
69 #include <linux/stacktrace.h>
70 #include <linux/resource.h>
71 #include <linux/module.h>
72 #include <linux/mount.h>
73 #include <linux/security.h>
74 #include <linux/ptrace.h>
75 #include <linux/tracehook.h>
76 #include <linux/printk.h>
77 #include <linux/cgroup.h>
78 #include <linux/cpuset.h>
79 #include <linux/audit.h>
80 #include <linux/poll.h>
81 #include <linux/nsproxy.h>
82 #include <linux/oom.h>
83 #include <linux/elf.h>
84 #include <linux/pid_namespace.h>
85 #include <linux/user_namespace.h>
86 #include <linux/fs_struct.h>
87 #include <linux/slab.h>
88 #include <linux/flex_array.h>
89 #include <linux/posix-timers.h>
90 #ifdef CONFIG_HARDWALL
91 #include <asm/hardwall.h>
93 #include <trace/events/oom.h>
97 #include "../../lib/kstrtox.h"
100 * Implementing inode permission operations in /proc is almost
101 * certainly an error. Permission checks need to happen during
102 * each system call not at open time. The reason is that most of
103 * what we wish to check for permissions in /proc varies at runtime.
105 * The classic example of a problem is opening file descriptors
106 * in /proc for a task before it execs a suid executable.
113 const struct inode_operations
*iop
;
114 const struct file_operations
*fop
;
118 #define NOD(NAME, MODE, IOP, FOP, OP) { \
120 .len = sizeof(NAME) - 1, \
127 #define DIR(NAME, MODE, iops, fops) \
128 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
129 #define LNK(NAME, get_link) \
130 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
131 &proc_pid_link_inode_operations, NULL, \
132 { .proc_get_link = get_link } )
133 #define REG(NAME, MODE, fops) \
134 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
135 #define ONE(NAME, MODE, show) \
136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
138 { .proc_show = show } )
141 * Count the number of hardlinks for the pid_entry table, excluding the .
144 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
151 for (i
= 0; i
< n
; ++i
) {
152 if (S_ISDIR(entries
[i
].mode
))
159 static int get_task_root(struct task_struct
*task
, struct path
*root
)
161 int result
= -ENOENT
;
165 get_fs_root(task
->fs
, root
);
172 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
174 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
175 int result
= -ENOENT
;
180 get_fs_pwd(task
->fs
, path
);
184 put_task_struct(task
);
189 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
191 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
192 int result
= -ENOENT
;
195 result
= get_task_root(task
, path
);
196 put_task_struct(task
);
201 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
202 size_t _count
, loff_t
*pos
)
204 struct task_struct
*tsk
;
205 struct mm_struct
*mm
;
207 unsigned long count
= _count
;
208 unsigned long arg_start
, arg_end
, env_start
, env_end
;
209 unsigned long len1
, len2
, len
;
216 tsk
= get_proc_task(file_inode(file
));
219 mm
= get_task_mm(tsk
);
220 put_task_struct(tsk
);
223 /* Check if process spawned far enough to have cmdline. */
229 page
= (char *)__get_free_page(GFP_TEMPORARY
);
235 down_read(&mm
->mmap_sem
);
236 arg_start
= mm
->arg_start
;
237 arg_end
= mm
->arg_end
;
238 env_start
= mm
->env_start
;
239 env_end
= mm
->env_end
;
240 up_read(&mm
->mmap_sem
);
242 BUG_ON(arg_start
> arg_end
);
243 BUG_ON(env_start
> env_end
);
245 len1
= arg_end
- arg_start
;
246 len2
= env_end
- env_start
;
254 * Inherently racy -- command line shares address space
255 * with code and data.
257 rv
= access_remote_vm(mm
, arg_end
- 1, &c
, 1, 0);
264 /* Command line (set of strings) occupies whole ARGV. */
268 p
= arg_start
+ *pos
;
270 while (count
> 0 && len
> 0) {
274 _count
= min3(count
, len
, PAGE_SIZE
);
275 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
281 if (copy_to_user(buf
, page
, nr_read
)) {
294 * Command line (1 string) occupies ARGV and maybe
297 if (len1
+ len2
<= *pos
)
302 p
= arg_start
+ *pos
;
304 while (count
> 0 && len
> 0) {
305 unsigned int _count
, l
;
309 _count
= min3(count
, len
, PAGE_SIZE
);
310 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
317 * Command line can be shorter than whole ARGV
318 * even if last "marker" byte says it is not.
321 l
= strnlen(page
, nr_read
);
327 if (copy_to_user(buf
, page
, nr_read
)) {
343 * Command line (1 string) occupies ARGV and
347 p
= env_start
+ *pos
- len1
;
348 len
= len1
+ len2
- *pos
;
353 while (count
> 0 && len
> 0) {
354 unsigned int _count
, l
;
358 _count
= min3(count
, len
, PAGE_SIZE
);
359 nr_read
= access_remote_vm(mm
, p
, page
, _count
, 0);
367 l
= strnlen(page
, nr_read
);
373 if (copy_to_user(buf
, page
, nr_read
)) {
392 free_page((unsigned long)page
);
400 static const struct file_operations proc_pid_cmdline_ops
= {
401 .read
= proc_pid_cmdline_read
,
402 .llseek
= generic_file_llseek
,
405 static int proc_pid_auxv(struct seq_file
*m
, struct pid_namespace
*ns
,
406 struct pid
*pid
, struct task_struct
*task
)
408 struct mm_struct
*mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
409 if (mm
&& !IS_ERR(mm
)) {
410 unsigned int nwords
= 0;
413 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
414 seq_write(m
, mm
->saved_auxv
, nwords
* sizeof(mm
->saved_auxv
[0]));
422 #ifdef CONFIG_KALLSYMS
424 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
425 * Returns the resolved symbol. If that fails, simply return the address.
427 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
428 struct pid
*pid
, struct task_struct
*task
)
431 char symname
[KSYM_NAME_LEN
];
433 wchan
= get_wchan(task
);
435 if (wchan
&& ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)
436 && !lookup_symbol_name(wchan
, symname
))
437 seq_printf(m
, "%s", symname
);
443 #endif /* CONFIG_KALLSYMS */
445 static int lock_trace(struct task_struct
*task
)
447 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
450 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
451 mutex_unlock(&task
->signal
->cred_guard_mutex
);
457 static void unlock_trace(struct task_struct
*task
)
459 mutex_unlock(&task
->signal
->cred_guard_mutex
);
462 #ifdef CONFIG_STACKTRACE
464 #define MAX_STACK_TRACE_DEPTH 64
466 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
467 struct pid
*pid
, struct task_struct
*task
)
469 struct stack_trace trace
;
470 unsigned long *entries
;
475 * The ability to racily run the kernel stack unwinder on a running task
476 * and then observe the unwinder output is scary; while it is useful for
477 * debugging kernel issues, it can also allow an attacker to leak kernel
479 * Doing this in a manner that is at least safe from races would require
480 * some work to ensure that the remote task can not be scheduled; and
481 * even then, this would still expose the unwinder as local attack
483 * Therefore, this interface is restricted to root.
485 if (!file_ns_capable(m
->file
, &init_user_ns
, CAP_SYS_ADMIN
))
488 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
492 trace
.nr_entries
= 0;
493 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
494 trace
.entries
= entries
;
497 err
= lock_trace(task
);
499 save_stack_trace_tsk(task
, &trace
);
501 for (i
= 0; i
< trace
.nr_entries
; i
++) {
502 seq_printf(m
, "[<%pK>] %pS\n",
503 (void *)entries
[i
], (void *)entries
[i
]);
513 #ifdef CONFIG_SCHED_INFO
515 * Provides /proc/PID/schedstat
517 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
518 struct pid
*pid
, struct task_struct
*task
)
520 if (unlikely(!sched_info_on()))
521 seq_printf(m
, "0 0 0\n");
523 seq_printf(m
, "%llu %llu %lu\n",
524 (unsigned long long)task
->se
.sum_exec_runtime
,
525 (unsigned long long)task
->sched_info
.run_delay
,
526 task
->sched_info
.pcount
);
532 #ifdef CONFIG_LATENCYTOP
533 static int lstats_show_proc(struct seq_file
*m
, void *v
)
536 struct inode
*inode
= m
->private;
537 struct task_struct
*task
= get_proc_task(inode
);
541 seq_puts(m
, "Latency Top version : v0.1\n");
542 for (i
= 0; i
< 32; i
++) {
543 struct latency_record
*lr
= &task
->latency_record
[i
];
544 if (lr
->backtrace
[0]) {
546 seq_printf(m
, "%i %li %li",
547 lr
->count
, lr
->time
, lr
->max
);
548 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
549 unsigned long bt
= lr
->backtrace
[q
];
554 seq_printf(m
, " %ps", (void *)bt
);
560 put_task_struct(task
);
564 static int lstats_open(struct inode
*inode
, struct file
*file
)
566 return single_open(file
, lstats_show_proc
, inode
);
569 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
570 size_t count
, loff_t
*offs
)
572 struct task_struct
*task
= get_proc_task(file_inode(file
));
576 clear_all_latency_tracing(task
);
577 put_task_struct(task
);
582 static const struct file_operations proc_lstats_operations
= {
585 .write
= lstats_write
,
587 .release
= single_release
,
592 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
593 struct pid
*pid
, struct task_struct
*task
)
595 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
596 unsigned long points
= 0;
598 read_lock(&tasklist_lock
);
600 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
602 read_unlock(&tasklist_lock
);
603 seq_printf(m
, "%lu\n", points
);
613 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
614 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
615 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
616 [RLIMIT_DATA
] = {"Max data size", "bytes"},
617 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
618 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
619 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
620 [RLIMIT_NPROC
] = {"Max processes", "processes"},
621 [RLIMIT_NOFILE
] = {"Max open files", "files"},
622 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
623 [RLIMIT_AS
] = {"Max address space", "bytes"},
624 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
625 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
626 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
627 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
628 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
629 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
632 /* Display limits for a process */
633 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
634 struct pid
*pid
, struct task_struct
*task
)
639 struct rlimit rlim
[RLIM_NLIMITS
];
641 if (!lock_task_sighand(task
, &flags
))
643 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
644 unlock_task_sighand(task
, &flags
);
647 * print the file header
649 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
650 "Limit", "Soft Limit", "Hard Limit", "Units");
652 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
653 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
654 seq_printf(m
, "%-25s %-20s ",
655 lnames
[i
].name
, "unlimited");
657 seq_printf(m
, "%-25s %-20lu ",
658 lnames
[i
].name
, rlim
[i
].rlim_cur
);
660 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
661 seq_printf(m
, "%-20s ", "unlimited");
663 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
666 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
674 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
675 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
676 struct pid
*pid
, struct task_struct
*task
)
679 unsigned long args
[6], sp
, pc
;
682 res
= lock_trace(task
);
686 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
687 seq_puts(m
, "running\n");
689 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
692 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
694 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
700 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
702 /************************************************************************/
703 /* Here the fs part begins */
704 /************************************************************************/
706 /* permission checks */
707 static int proc_fd_access_allowed(struct inode
*inode
)
709 struct task_struct
*task
;
711 /* Allow access to a task's file descriptors if it is us or we
712 * may use ptrace attach to the process and find out that
715 task
= get_proc_task(inode
);
717 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
718 put_task_struct(task
);
723 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
726 struct inode
*inode
= d_inode(dentry
);
728 if (attr
->ia_valid
& ATTR_MODE
)
731 error
= inode_change_ok(inode
, attr
);
735 setattr_copy(inode
, attr
);
736 mark_inode_dirty(inode
);
741 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
742 * or euid/egid (for hide_pid_min=2)?
744 static bool has_pid_permissions(struct pid_namespace
*pid
,
745 struct task_struct
*task
,
748 if (pid
->hide_pid
< hide_pid_min
)
750 if (in_group_p(pid
->pid_gid
))
752 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
756 static int proc_pid_permission(struct inode
*inode
, int mask
)
758 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
759 struct task_struct
*task
;
762 task
= get_proc_task(inode
);
765 has_perms
= has_pid_permissions(pid
, task
, 1);
766 put_task_struct(task
);
769 if (pid
->hide_pid
== 2) {
771 * Let's make getdents(), stat(), and open()
772 * consistent with each other. If a process
773 * may not stat() a file, it shouldn't be seen
781 return generic_permission(inode
, mask
);
786 static const struct inode_operations proc_def_inode_operations
= {
787 .setattr
= proc_setattr
,
790 static int proc_single_show(struct seq_file
*m
, void *v
)
792 struct inode
*inode
= m
->private;
793 struct pid_namespace
*ns
;
795 struct task_struct
*task
;
798 ns
= inode
->i_sb
->s_fs_info
;
799 pid
= proc_pid(inode
);
800 task
= get_pid_task(pid
, PIDTYPE_PID
);
804 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
806 put_task_struct(task
);
810 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
812 return single_open(filp
, proc_single_show
, inode
);
815 static const struct file_operations proc_single_file_operations
= {
816 .open
= proc_single_open
,
819 .release
= single_release
,
823 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
825 struct task_struct
*task
= get_proc_task(inode
);
826 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
829 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
830 put_task_struct(task
);
832 if (!IS_ERR_OR_NULL(mm
)) {
833 /* ensure this mm_struct can't be freed */
834 atomic_inc(&mm
->mm_count
);
835 /* but do not pin its memory */
843 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
845 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
850 file
->private_data
= mm
;
854 static int mem_open(struct inode
*inode
, struct file
*file
)
856 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
858 /* OK to pass negative loff_t, we can catch out-of-range */
859 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
864 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
865 size_t count
, loff_t
*ppos
, int write
)
867 struct mm_struct
*mm
= file
->private_data
;
868 unsigned long addr
= *ppos
;
875 page
= (char *)__get_free_page(GFP_TEMPORARY
);
880 if (!atomic_inc_not_zero(&mm
->mm_users
))
884 int this_len
= min_t(int, count
, PAGE_SIZE
);
886 if (write
&& copy_from_user(page
, buf
, this_len
)) {
891 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
898 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
912 free_page((unsigned long) page
);
916 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
917 size_t count
, loff_t
*ppos
)
919 return mem_rw(file
, buf
, count
, ppos
, 0);
922 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
923 size_t count
, loff_t
*ppos
)
925 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
928 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
932 file
->f_pos
= offset
;
935 file
->f_pos
+= offset
;
940 force_successful_syscall_return();
944 static int mem_release(struct inode
*inode
, struct file
*file
)
946 struct mm_struct
*mm
= file
->private_data
;
952 static const struct file_operations proc_mem_operations
= {
957 .release
= mem_release
,
960 static int environ_open(struct inode
*inode
, struct file
*file
)
962 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
965 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
966 size_t count
, loff_t
*ppos
)
969 unsigned long src
= *ppos
;
971 struct mm_struct
*mm
= file
->private_data
;
972 unsigned long env_start
, env_end
;
974 /* Ensure the process spawned far enough to have an environment. */
975 if (!mm
|| !mm
->env_end
)
978 page
= (char *)__get_free_page(GFP_TEMPORARY
);
983 if (!atomic_inc_not_zero(&mm
->mm_users
))
986 down_read(&mm
->mmap_sem
);
987 env_start
= mm
->env_start
;
988 env_end
= mm
->env_end
;
989 up_read(&mm
->mmap_sem
);
992 size_t this_len
, max_len
;
995 if (src
>= (env_end
- env_start
))
998 this_len
= env_end
- (env_start
+ src
);
1000 max_len
= min_t(size_t, PAGE_SIZE
, count
);
1001 this_len
= min(max_len
, this_len
);
1003 retval
= access_remote_vm(mm
, (env_start
+ src
),
1011 if (copy_to_user(buf
, page
, retval
)) {
1025 free_page((unsigned long) page
);
1029 static const struct file_operations proc_environ_operations
= {
1030 .open
= environ_open
,
1031 .read
= environ_read
,
1032 .llseek
= generic_file_llseek
,
1033 .release
= mem_release
,
1036 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
1039 struct task_struct
*task
= get_proc_task(file_inode(file
));
1040 char buffer
[PROC_NUMBUF
];
1041 int oom_adj
= OOM_ADJUST_MIN
;
1043 unsigned long flags
;
1047 if (lock_task_sighand(task
, &flags
)) {
1048 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
1049 oom_adj
= OOM_ADJUST_MAX
;
1051 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
1053 unlock_task_sighand(task
, &flags
);
1055 put_task_struct(task
);
1056 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
1057 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1061 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1062 * kernels. The effective policy is defined by oom_score_adj, which has a
1063 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1064 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1065 * Processes that become oom disabled via oom_adj will still be oom disabled
1066 * with this implementation.
1068 * oom_adj cannot be removed since existing userspace binaries use it.
1070 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1071 size_t count
, loff_t
*ppos
)
1073 struct task_struct
*task
;
1074 char buffer
[PROC_NUMBUF
];
1076 unsigned long flags
;
1079 memset(buffer
, 0, sizeof(buffer
));
1080 if (count
> sizeof(buffer
) - 1)
1081 count
= sizeof(buffer
) - 1;
1082 if (copy_from_user(buffer
, buf
, count
)) {
1087 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1090 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1091 oom_adj
!= OOM_DISABLE
) {
1096 task
= get_proc_task(file_inode(file
));
1108 if (!lock_task_sighand(task
, &flags
)) {
1114 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1115 * value is always attainable.
1117 if (oom_adj
== OOM_ADJUST_MAX
)
1118 oom_adj
= OOM_SCORE_ADJ_MAX
;
1120 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1122 if (oom_adj
< task
->signal
->oom_score_adj
&&
1123 !capable(CAP_SYS_RESOURCE
)) {
1129 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1130 * /proc/pid/oom_score_adj instead.
1132 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1133 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1136 task
->signal
->oom_score_adj
= oom_adj
;
1137 trace_oom_score_adj_update(task
);
1139 unlock_task_sighand(task
, &flags
);
1142 put_task_struct(task
);
1144 return err
< 0 ? err
: count
;
1147 static const struct file_operations proc_oom_adj_operations
= {
1148 .read
= oom_adj_read
,
1149 .write
= oom_adj_write
,
1150 .llseek
= generic_file_llseek
,
1153 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1154 size_t count
, loff_t
*ppos
)
1156 struct task_struct
*task
= get_proc_task(file_inode(file
));
1157 char buffer
[PROC_NUMBUF
];
1158 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1159 unsigned long flags
;
1164 if (lock_task_sighand(task
, &flags
)) {
1165 oom_score_adj
= task
->signal
->oom_score_adj
;
1166 unlock_task_sighand(task
, &flags
);
1168 put_task_struct(task
);
1169 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1170 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1173 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1174 size_t count
, loff_t
*ppos
)
1176 struct task_struct
*task
;
1177 char buffer
[PROC_NUMBUF
];
1178 unsigned long flags
;
1182 memset(buffer
, 0, sizeof(buffer
));
1183 if (count
> sizeof(buffer
) - 1)
1184 count
= sizeof(buffer
) - 1;
1185 if (copy_from_user(buffer
, buf
, count
)) {
1190 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1193 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1194 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1199 task
= get_proc_task(file_inode(file
));
1211 if (!lock_task_sighand(task
, &flags
)) {
1216 if ((short)oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1217 !capable(CAP_SYS_RESOURCE
)) {
1222 task
->signal
->oom_score_adj
= (short)oom_score_adj
;
1223 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1224 task
->signal
->oom_score_adj_min
= (short)oom_score_adj
;
1225 trace_oom_score_adj_update(task
);
1228 unlock_task_sighand(task
, &flags
);
1231 put_task_struct(task
);
1233 return err
< 0 ? err
: count
;
1236 static const struct file_operations proc_oom_score_adj_operations
= {
1237 .read
= oom_score_adj_read
,
1238 .write
= oom_score_adj_write
,
1239 .llseek
= default_llseek
,
1242 #ifdef CONFIG_AUDITSYSCALL
1243 #define TMPBUFLEN 21
1244 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1245 size_t count
, loff_t
*ppos
)
1247 struct inode
* inode
= file_inode(file
);
1248 struct task_struct
*task
= get_proc_task(inode
);
1250 char tmpbuf
[TMPBUFLEN
];
1254 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1255 from_kuid(file
->f_cred
->user_ns
,
1256 audit_get_loginuid(task
)));
1257 put_task_struct(task
);
1258 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1261 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1262 size_t count
, loff_t
*ppos
)
1264 struct inode
* inode
= file_inode(file
);
1270 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1277 /* No partial writes. */
1281 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1285 /* is userspace tring to explicitly UNSET the loginuid? */
1286 if (loginuid
== AUDIT_UID_UNSET
) {
1287 kloginuid
= INVALID_UID
;
1289 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1290 if (!uid_valid(kloginuid
))
1294 rv
= audit_set_loginuid(kloginuid
);
1300 static const struct file_operations proc_loginuid_operations
= {
1301 .read
= proc_loginuid_read
,
1302 .write
= proc_loginuid_write
,
1303 .llseek
= generic_file_llseek
,
1306 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1307 size_t count
, loff_t
*ppos
)
1309 struct inode
* inode
= file_inode(file
);
1310 struct task_struct
*task
= get_proc_task(inode
);
1312 char tmpbuf
[TMPBUFLEN
];
1316 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1317 audit_get_sessionid(task
));
1318 put_task_struct(task
);
1319 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1322 static const struct file_operations proc_sessionid_operations
= {
1323 .read
= proc_sessionid_read
,
1324 .llseek
= generic_file_llseek
,
1328 #ifdef CONFIG_FAULT_INJECTION
1329 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1330 size_t count
, loff_t
*ppos
)
1332 struct task_struct
*task
= get_proc_task(file_inode(file
));
1333 char buffer
[PROC_NUMBUF
];
1339 make_it_fail
= task
->make_it_fail
;
1340 put_task_struct(task
);
1342 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1344 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1347 static ssize_t
proc_fault_inject_write(struct file
* file
,
1348 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1350 struct task_struct
*task
;
1351 char buffer
[PROC_NUMBUF
];
1355 if (!capable(CAP_SYS_RESOURCE
))
1357 memset(buffer
, 0, sizeof(buffer
));
1358 if (count
> sizeof(buffer
) - 1)
1359 count
= sizeof(buffer
) - 1;
1360 if (copy_from_user(buffer
, buf
, count
))
1362 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1365 if (make_it_fail
< 0 || make_it_fail
> 1)
1368 task
= get_proc_task(file_inode(file
));
1371 task
->make_it_fail
= make_it_fail
;
1372 put_task_struct(task
);
1377 static const struct file_operations proc_fault_inject_operations
= {
1378 .read
= proc_fault_inject_read
,
1379 .write
= proc_fault_inject_write
,
1380 .llseek
= generic_file_llseek
,
1385 #ifdef CONFIG_SCHED_DEBUG
1387 * Print out various scheduling related per-task fields:
1389 static int sched_show(struct seq_file
*m
, void *v
)
1391 struct inode
*inode
= m
->private;
1392 struct task_struct
*p
;
1394 p
= get_proc_task(inode
);
1397 proc_sched_show_task(p
, m
);
1405 sched_write(struct file
*file
, const char __user
*buf
,
1406 size_t count
, loff_t
*offset
)
1408 struct inode
*inode
= file_inode(file
);
1409 struct task_struct
*p
;
1411 p
= get_proc_task(inode
);
1414 proc_sched_set_task(p
);
1421 static int sched_open(struct inode
*inode
, struct file
*filp
)
1423 return single_open(filp
, sched_show
, inode
);
1426 static const struct file_operations proc_pid_sched_operations
= {
1429 .write
= sched_write
,
1430 .llseek
= seq_lseek
,
1431 .release
= single_release
,
1436 #ifdef CONFIG_SCHED_AUTOGROUP
1438 * Print out autogroup related information:
1440 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1442 struct inode
*inode
= m
->private;
1443 struct task_struct
*p
;
1445 p
= get_proc_task(inode
);
1448 proc_sched_autogroup_show_task(p
, m
);
1456 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1457 size_t count
, loff_t
*offset
)
1459 struct inode
*inode
= file_inode(file
);
1460 struct task_struct
*p
;
1461 char buffer
[PROC_NUMBUF
];
1465 memset(buffer
, 0, sizeof(buffer
));
1466 if (count
> sizeof(buffer
) - 1)
1467 count
= sizeof(buffer
) - 1;
1468 if (copy_from_user(buffer
, buf
, count
))
1471 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1475 p
= get_proc_task(inode
);
1479 err
= proc_sched_autogroup_set_nice(p
, nice
);
1488 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1492 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1494 struct seq_file
*m
= filp
->private_data
;
1501 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1502 .open
= sched_autogroup_open
,
1504 .write
= sched_autogroup_write
,
1505 .llseek
= seq_lseek
,
1506 .release
= single_release
,
1509 #endif /* CONFIG_SCHED_AUTOGROUP */
1511 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1512 size_t count
, loff_t
*offset
)
1514 struct inode
*inode
= file_inode(file
);
1515 struct task_struct
*p
;
1516 char buffer
[TASK_COMM_LEN
];
1517 const size_t maxlen
= sizeof(buffer
) - 1;
1519 memset(buffer
, 0, sizeof(buffer
));
1520 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1523 p
= get_proc_task(inode
);
1527 if (same_thread_group(current
, p
))
1528 set_task_comm(p
, buffer
);
1537 static int comm_show(struct seq_file
*m
, void *v
)
1539 struct inode
*inode
= m
->private;
1540 struct task_struct
*p
;
1542 p
= get_proc_task(inode
);
1547 seq_printf(m
, "%s\n", p
->comm
);
1555 static int comm_open(struct inode
*inode
, struct file
*filp
)
1557 return single_open(filp
, comm_show
, inode
);
1560 static const struct file_operations proc_pid_set_comm_operations
= {
1563 .write
= comm_write
,
1564 .llseek
= seq_lseek
,
1565 .release
= single_release
,
1568 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1570 struct task_struct
*task
;
1571 struct file
*exe_file
;
1573 task
= get_proc_task(d_inode(dentry
));
1576 exe_file
= get_task_exe_file(task
);
1577 put_task_struct(task
);
1579 *exe_path
= exe_file
->f_path
;
1580 path_get(&exe_file
->f_path
);
1587 static const char *proc_pid_follow_link(struct dentry
*dentry
, void **cookie
)
1589 struct inode
*inode
= d_inode(dentry
);
1591 int error
= -EACCES
;
1593 /* Are we allowed to snoop on the tasks file descriptors? */
1594 if (!proc_fd_access_allowed(inode
))
1597 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1601 nd_jump_link(&path
);
1604 return ERR_PTR(error
);
1607 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1609 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1616 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1617 len
= PTR_ERR(pathname
);
1618 if (IS_ERR(pathname
))
1620 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1624 if (copy_to_user(buffer
, pathname
, len
))
1627 free_page((unsigned long)tmp
);
1631 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1633 int error
= -EACCES
;
1634 struct inode
*inode
= d_inode(dentry
);
1637 /* Are we allowed to snoop on the tasks file descriptors? */
1638 if (!proc_fd_access_allowed(inode
))
1641 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1645 error
= do_proc_readlink(&path
, buffer
, buflen
);
1651 const struct inode_operations proc_pid_link_inode_operations
= {
1652 .readlink
= proc_pid_readlink
,
1653 .follow_link
= proc_pid_follow_link
,
1654 .setattr
= proc_setattr
,
1658 /* building an inode */
1660 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1662 struct inode
* inode
;
1663 struct proc_inode
*ei
;
1664 const struct cred
*cred
;
1666 /* We need a new inode */
1668 inode
= new_inode(sb
);
1674 inode
->i_ino
= get_next_ino();
1675 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1676 inode
->i_op
= &proc_def_inode_operations
;
1679 * grab the reference to task.
1681 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1685 if (task_dumpable(task
)) {
1687 cred
= __task_cred(task
);
1688 inode
->i_uid
= cred
->euid
;
1689 inode
->i_gid
= cred
->egid
;
1692 security_task_to_inode(task
, inode
);
1702 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1704 struct inode
*inode
= d_inode(dentry
);
1705 struct task_struct
*task
;
1706 const struct cred
*cred
;
1707 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1709 generic_fillattr(inode
, stat
);
1712 stat
->uid
= GLOBAL_ROOT_UID
;
1713 stat
->gid
= GLOBAL_ROOT_GID
;
1714 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1716 if (!has_pid_permissions(pid
, task
, 2)) {
1719 * This doesn't prevent learning whether PID exists,
1720 * it only makes getattr() consistent with readdir().
1724 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1725 task_dumpable(task
)) {
1726 cred
= __task_cred(task
);
1727 stat
->uid
= cred
->euid
;
1728 stat
->gid
= cred
->egid
;
1738 * Exceptional case: normally we are not allowed to unhash a busy
1739 * directory. In this case, however, we can do it - no aliasing problems
1740 * due to the way we treat inodes.
1742 * Rewrite the inode's ownerships here because the owning task may have
1743 * performed a setuid(), etc.
1745 * Before the /proc/pid/status file was created the only way to read
1746 * the effective uid of a /process was to stat /proc/pid. Reading
1747 * /proc/pid/status is slow enough that procps and other packages
1748 * kept stating /proc/pid. To keep the rules in /proc simple I have
1749 * made this apply to all per process world readable and executable
1752 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1754 struct inode
*inode
;
1755 struct task_struct
*task
;
1756 const struct cred
*cred
;
1758 if (flags
& LOOKUP_RCU
)
1761 inode
= d_inode(dentry
);
1762 task
= get_proc_task(inode
);
1765 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1766 task_dumpable(task
)) {
1768 cred
= __task_cred(task
);
1769 inode
->i_uid
= cred
->euid
;
1770 inode
->i_gid
= cred
->egid
;
1773 inode
->i_uid
= GLOBAL_ROOT_UID
;
1774 inode
->i_gid
= GLOBAL_ROOT_GID
;
1776 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1777 security_task_to_inode(task
, inode
);
1778 put_task_struct(task
);
1784 static inline bool proc_inode_is_dead(struct inode
*inode
)
1786 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1789 int pid_delete_dentry(const struct dentry
*dentry
)
1791 /* Is the task we represent dead?
1792 * If so, then don't put the dentry on the lru list,
1793 * kill it immediately.
1795 return proc_inode_is_dead(d_inode(dentry
));
1798 const struct dentry_operations pid_dentry_operations
=
1800 .d_revalidate
= pid_revalidate
,
1801 .d_delete
= pid_delete_dentry
,
1807 * Fill a directory entry.
1809 * If possible create the dcache entry and derive our inode number and
1810 * file type from dcache entry.
1812 * Since all of the proc inode numbers are dynamically generated, the inode
1813 * numbers do not exist until the inode is cache. This means creating the
1814 * the dcache entry in readdir is necessary to keep the inode numbers
1815 * reported by readdir in sync with the inode numbers reported
1818 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1819 const char *name
, int len
,
1820 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1822 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1823 struct qstr qname
= QSTR_INIT(name
, len
);
1824 struct inode
*inode
;
1828 child
= d_hash_and_lookup(dir
, &qname
);
1830 child
= d_alloc(dir
, &qname
);
1832 goto end_instantiate
;
1833 if (instantiate(d_inode(dir
), child
, task
, ptr
) < 0) {
1835 goto end_instantiate
;
1838 inode
= d_inode(child
);
1840 type
= inode
->i_mode
>> 12;
1842 return dir_emit(ctx
, name
, len
, ino
, type
);
1845 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1849 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1850 * which represent vma start and end addresses.
1852 static int dname_to_vma_addr(struct dentry
*dentry
,
1853 unsigned long *start
, unsigned long *end
)
1855 const char *str
= dentry
->d_name
.name
;
1856 unsigned long long sval
, eval
;
1859 len
= _parse_integer(str
, 16, &sval
);
1860 if (len
& KSTRTOX_OVERFLOW
)
1862 if (sval
!= (unsigned long)sval
)
1870 len
= _parse_integer(str
, 16, &eval
);
1871 if (len
& KSTRTOX_OVERFLOW
)
1873 if (eval
!= (unsigned long)eval
)
1886 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1888 unsigned long vm_start
, vm_end
;
1889 bool exact_vma_exists
= false;
1890 struct mm_struct
*mm
= NULL
;
1891 struct task_struct
*task
;
1892 const struct cred
*cred
;
1893 struct inode
*inode
;
1896 if (flags
& LOOKUP_RCU
)
1899 inode
= d_inode(dentry
);
1900 task
= get_proc_task(inode
);
1904 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
1905 if (IS_ERR_OR_NULL(mm
))
1908 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1909 down_read(&mm
->mmap_sem
);
1910 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1911 up_read(&mm
->mmap_sem
);
1916 if (exact_vma_exists
) {
1917 if (task_dumpable(task
)) {
1919 cred
= __task_cred(task
);
1920 inode
->i_uid
= cred
->euid
;
1921 inode
->i_gid
= cred
->egid
;
1924 inode
->i_uid
= GLOBAL_ROOT_UID
;
1925 inode
->i_gid
= GLOBAL_ROOT_GID
;
1927 security_task_to_inode(task
, inode
);
1932 put_task_struct(task
);
1938 static const struct dentry_operations tid_map_files_dentry_operations
= {
1939 .d_revalidate
= map_files_d_revalidate
,
1940 .d_delete
= pid_delete_dentry
,
1943 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1945 unsigned long vm_start
, vm_end
;
1946 struct vm_area_struct
*vma
;
1947 struct task_struct
*task
;
1948 struct mm_struct
*mm
;
1952 task
= get_proc_task(d_inode(dentry
));
1956 mm
= get_task_mm(task
);
1957 put_task_struct(task
);
1961 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1966 down_read(&mm
->mmap_sem
);
1967 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1968 if (vma
&& vma
->vm_file
) {
1969 *path
= vma
->vm_file
->f_path
;
1973 up_read(&mm
->mmap_sem
);
1981 struct map_files_info
{
1984 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1988 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
1989 * symlinks may be used to bypass permissions on ancestor directories in the
1990 * path to the file in question.
1993 proc_map_files_follow_link(struct dentry
*dentry
, void **cookie
)
1995 if (!capable(CAP_SYS_ADMIN
))
1996 return ERR_PTR(-EPERM
);
1998 return proc_pid_follow_link(dentry
, NULL
);
2002 * Identical to proc_pid_link_inode_operations except for follow_link()
2004 static const struct inode_operations proc_map_files_link_inode_operations
= {
2005 .readlink
= proc_pid_readlink
,
2006 .follow_link
= proc_map_files_follow_link
,
2007 .setattr
= proc_setattr
,
2011 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2012 struct task_struct
*task
, const void *ptr
)
2014 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2015 struct proc_inode
*ei
;
2016 struct inode
*inode
;
2018 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2023 ei
->op
.proc_get_link
= proc_map_files_get_link
;
2025 inode
->i_op
= &proc_map_files_link_inode_operations
;
2027 inode
->i_mode
= S_IFLNK
;
2029 if (mode
& FMODE_READ
)
2030 inode
->i_mode
|= S_IRUSR
;
2031 if (mode
& FMODE_WRITE
)
2032 inode
->i_mode
|= S_IWUSR
;
2034 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2035 d_add(dentry
, inode
);
2040 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2041 struct dentry
*dentry
, unsigned int flags
)
2043 unsigned long vm_start
, vm_end
;
2044 struct vm_area_struct
*vma
;
2045 struct task_struct
*task
;
2047 struct mm_struct
*mm
;
2050 task
= get_proc_task(dir
);
2055 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2059 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2062 mm
= get_task_mm(task
);
2066 down_read(&mm
->mmap_sem
);
2067 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2072 result
= proc_map_files_instantiate(dir
, dentry
, task
,
2073 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2076 up_read(&mm
->mmap_sem
);
2079 put_task_struct(task
);
2081 return ERR_PTR(result
);
2084 static const struct inode_operations proc_map_files_inode_operations
= {
2085 .lookup
= proc_map_files_lookup
,
2086 .permission
= proc_fd_permission
,
2087 .setattr
= proc_setattr
,
2091 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2093 struct vm_area_struct
*vma
;
2094 struct task_struct
*task
;
2095 struct mm_struct
*mm
;
2096 unsigned long nr_files
, pos
, i
;
2097 struct flex_array
*fa
= NULL
;
2098 struct map_files_info info
;
2099 struct map_files_info
*p
;
2103 task
= get_proc_task(file_inode(file
));
2108 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2112 if (!dir_emit_dots(file
, ctx
))
2115 mm
= get_task_mm(task
);
2118 down_read(&mm
->mmap_sem
);
2123 * We need two passes here:
2125 * 1) Collect vmas of mapped files with mmap_sem taken
2126 * 2) Release mmap_sem and instantiate entries
2128 * otherwise we get lockdep complained, since filldir()
2129 * routine might require mmap_sem taken in might_fault().
2132 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2133 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2138 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2140 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2144 flex_array_free(fa
);
2145 up_read(&mm
->mmap_sem
);
2149 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2150 vma
= vma
->vm_next
) {
2153 if (++pos
<= ctx
->pos
)
2156 info
.mode
= vma
->vm_file
->f_mode
;
2157 info
.len
= snprintf(info
.name
,
2158 sizeof(info
.name
), "%lx-%lx",
2159 vma
->vm_start
, vma
->vm_end
);
2160 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2164 up_read(&mm
->mmap_sem
);
2166 for (i
= 0; i
< nr_files
; i
++) {
2167 p
= flex_array_get(fa
, i
);
2168 if (!proc_fill_cache(file
, ctx
,
2170 proc_map_files_instantiate
,
2172 (void *)(unsigned long)p
->mode
))
2177 flex_array_free(fa
);
2181 put_task_struct(task
);
2186 static const struct file_operations proc_map_files_operations
= {
2187 .read
= generic_read_dir
,
2188 .iterate
= proc_map_files_readdir
,
2189 .llseek
= default_llseek
,
2192 struct timers_private
{
2194 struct task_struct
*task
;
2195 struct sighand_struct
*sighand
;
2196 struct pid_namespace
*ns
;
2197 unsigned long flags
;
2200 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2202 struct timers_private
*tp
= m
->private;
2204 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2206 return ERR_PTR(-ESRCH
);
2208 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2210 return ERR_PTR(-ESRCH
);
2212 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2215 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2217 struct timers_private
*tp
= m
->private;
2218 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2221 static void timers_stop(struct seq_file
*m
, void *v
)
2223 struct timers_private
*tp
= m
->private;
2226 unlock_task_sighand(tp
->task
, &tp
->flags
);
2231 put_task_struct(tp
->task
);
2236 static int show_timer(struct seq_file
*m
, void *v
)
2238 struct k_itimer
*timer
;
2239 struct timers_private
*tp
= m
->private;
2241 static const char * const nstr
[] = {
2242 [SIGEV_SIGNAL
] = "signal",
2243 [SIGEV_NONE
] = "none",
2244 [SIGEV_THREAD
] = "thread",
2247 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2248 notify
= timer
->it_sigev_notify
;
2250 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2251 seq_printf(m
, "signal: %d/%p\n",
2252 timer
->sigq
->info
.si_signo
,
2253 timer
->sigq
->info
.si_value
.sival_ptr
);
2254 seq_printf(m
, "notify: %s/%s.%d\n",
2255 nstr
[notify
& ~SIGEV_THREAD_ID
],
2256 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2257 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2258 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2263 static const struct seq_operations proc_timers_seq_ops
= {
2264 .start
= timers_start
,
2265 .next
= timers_next
,
2266 .stop
= timers_stop
,
2270 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2272 struct timers_private
*tp
;
2274 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2275 sizeof(struct timers_private
));
2279 tp
->pid
= proc_pid(inode
);
2280 tp
->ns
= inode
->i_sb
->s_fs_info
;
2284 static const struct file_operations proc_timers_operations
= {
2285 .open
= proc_timers_open
,
2287 .llseek
= seq_lseek
,
2288 .release
= seq_release_private
,
2291 static int proc_pident_instantiate(struct inode
*dir
,
2292 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2294 const struct pid_entry
*p
= ptr
;
2295 struct inode
*inode
;
2296 struct proc_inode
*ei
;
2298 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2303 inode
->i_mode
= p
->mode
;
2304 if (S_ISDIR(inode
->i_mode
))
2305 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2307 inode
->i_op
= p
->iop
;
2309 inode
->i_fop
= p
->fop
;
2311 d_set_d_op(dentry
, &pid_dentry_operations
);
2312 d_add(dentry
, inode
);
2313 /* Close the race of the process dying before we return the dentry */
2314 if (pid_revalidate(dentry
, 0))
2320 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2321 struct dentry
*dentry
,
2322 const struct pid_entry
*ents
,
2326 struct task_struct
*task
= get_proc_task(dir
);
2327 const struct pid_entry
*p
, *last
;
2335 * Yes, it does not scale. And it should not. Don't add
2336 * new entries into /proc/<tgid>/ without very good reasons.
2338 last
= &ents
[nents
- 1];
2339 for (p
= ents
; p
<= last
; p
++) {
2340 if (p
->len
!= dentry
->d_name
.len
)
2342 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2348 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2350 put_task_struct(task
);
2352 return ERR_PTR(error
);
2355 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2356 const struct pid_entry
*ents
, unsigned int nents
)
2358 struct task_struct
*task
= get_proc_task(file_inode(file
));
2359 const struct pid_entry
*p
;
2364 if (!dir_emit_dots(file
, ctx
))
2367 if (ctx
->pos
>= nents
+ 2)
2370 for (p
= ents
+ (ctx
->pos
- 2); p
<= ents
+ nents
- 1; p
++) {
2371 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2372 proc_pident_instantiate
, task
, p
))
2377 put_task_struct(task
);
2381 #ifdef CONFIG_SECURITY
2382 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2383 size_t count
, loff_t
*ppos
)
2385 struct inode
* inode
= file_inode(file
);
2388 struct task_struct
*task
= get_proc_task(inode
);
2393 length
= security_getprocattr(task
,
2394 (char*)file
->f_path
.dentry
->d_name
.name
,
2396 put_task_struct(task
);
2398 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2403 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2404 size_t count
, loff_t
*ppos
)
2406 struct inode
* inode
= file_inode(file
);
2409 struct task_struct
*task
= get_proc_task(inode
);
2414 if (count
> PAGE_SIZE
)
2417 /* No partial writes. */
2423 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2428 if (copy_from_user(page
, buf
, count
))
2431 /* Guard against adverse ptrace interaction */
2432 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2436 length
= security_setprocattr(task
,
2437 (char*)file
->f_path
.dentry
->d_name
.name
,
2438 (void*)page
, count
);
2439 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2441 free_page((unsigned long) page
);
2443 put_task_struct(task
);
2448 static const struct file_operations proc_pid_attr_operations
= {
2449 .read
= proc_pid_attr_read
,
2450 .write
= proc_pid_attr_write
,
2451 .llseek
= generic_file_llseek
,
2454 static const struct pid_entry attr_dir_stuff
[] = {
2455 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2456 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2457 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2458 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2459 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2460 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2463 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2465 return proc_pident_readdir(file
, ctx
,
2466 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2469 static const struct file_operations proc_attr_dir_operations
= {
2470 .read
= generic_read_dir
,
2471 .iterate
= proc_attr_dir_readdir
,
2472 .llseek
= default_llseek
,
2475 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2476 struct dentry
*dentry
, unsigned int flags
)
2478 return proc_pident_lookup(dir
, dentry
,
2479 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2482 static const struct inode_operations proc_attr_dir_inode_operations
= {
2483 .lookup
= proc_attr_dir_lookup
,
2484 .getattr
= pid_getattr
,
2485 .setattr
= proc_setattr
,
2490 #ifdef CONFIG_ELF_CORE
2491 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2492 size_t count
, loff_t
*ppos
)
2494 struct task_struct
*task
= get_proc_task(file_inode(file
));
2495 struct mm_struct
*mm
;
2496 char buffer
[PROC_NUMBUF
];
2504 mm
= get_task_mm(task
);
2506 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2507 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2508 MMF_DUMP_FILTER_SHIFT
));
2510 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2513 put_task_struct(task
);
2518 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2519 const char __user
*buf
,
2523 struct task_struct
*task
;
2524 struct mm_struct
*mm
;
2530 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2535 task
= get_proc_task(file_inode(file
));
2539 mm
= get_task_mm(task
);
2544 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2546 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2548 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2553 put_task_struct(task
);
2560 static const struct file_operations proc_coredump_filter_operations
= {
2561 .read
= proc_coredump_filter_read
,
2562 .write
= proc_coredump_filter_write
,
2563 .llseek
= generic_file_llseek
,
2567 #ifdef CONFIG_TASK_IO_ACCOUNTING
2568 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2570 struct task_io_accounting acct
= task
->ioac
;
2571 unsigned long flags
;
2574 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2578 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2583 if (whole
&& lock_task_sighand(task
, &flags
)) {
2584 struct task_struct
*t
= task
;
2586 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2587 while_each_thread(task
, t
)
2588 task_io_accounting_add(&acct
, &t
->ioac
);
2590 unlock_task_sighand(task
, &flags
);
2597 "read_bytes: %llu\n"
2598 "write_bytes: %llu\n"
2599 "cancelled_write_bytes: %llu\n",
2600 (unsigned long long)acct
.rchar
,
2601 (unsigned long long)acct
.wchar
,
2602 (unsigned long long)acct
.syscr
,
2603 (unsigned long long)acct
.syscw
,
2604 (unsigned long long)acct
.read_bytes
,
2605 (unsigned long long)acct
.write_bytes
,
2606 (unsigned long long)acct
.cancelled_write_bytes
);
2610 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2614 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2615 struct pid
*pid
, struct task_struct
*task
)
2617 return do_io_accounting(task
, m
, 0);
2620 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2621 struct pid
*pid
, struct task_struct
*task
)
2623 return do_io_accounting(task
, m
, 1);
2625 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2627 #ifdef CONFIG_USER_NS
2628 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2629 const struct seq_operations
*seq_ops
)
2631 struct user_namespace
*ns
= NULL
;
2632 struct task_struct
*task
;
2633 struct seq_file
*seq
;
2636 task
= get_proc_task(inode
);
2639 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2641 put_task_struct(task
);
2646 ret
= seq_open(file
, seq_ops
);
2650 seq
= file
->private_data
;
2660 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2662 struct seq_file
*seq
= file
->private_data
;
2663 struct user_namespace
*ns
= seq
->private;
2665 return seq_release(inode
, file
);
2668 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2670 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2673 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2675 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2678 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2680 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2683 static const struct file_operations proc_uid_map_operations
= {
2684 .open
= proc_uid_map_open
,
2685 .write
= proc_uid_map_write
,
2687 .llseek
= seq_lseek
,
2688 .release
= proc_id_map_release
,
2691 static const struct file_operations proc_gid_map_operations
= {
2692 .open
= proc_gid_map_open
,
2693 .write
= proc_gid_map_write
,
2695 .llseek
= seq_lseek
,
2696 .release
= proc_id_map_release
,
2699 static const struct file_operations proc_projid_map_operations
= {
2700 .open
= proc_projid_map_open
,
2701 .write
= proc_projid_map_write
,
2703 .llseek
= seq_lseek
,
2704 .release
= proc_id_map_release
,
2707 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2709 struct user_namespace
*ns
= NULL
;
2710 struct task_struct
*task
;
2714 task
= get_proc_task(inode
);
2717 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2719 put_task_struct(task
);
2724 if (file
->f_mode
& FMODE_WRITE
) {
2726 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2730 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2741 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2743 struct seq_file
*seq
= file
->private_data
;
2744 struct user_namespace
*ns
= seq
->private;
2745 int ret
= single_release(inode
, file
);
2750 static const struct file_operations proc_setgroups_operations
= {
2751 .open
= proc_setgroups_open
,
2752 .write
= proc_setgroups_write
,
2754 .llseek
= seq_lseek
,
2755 .release
= proc_setgroups_release
,
2757 #endif /* CONFIG_USER_NS */
2759 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2760 struct pid
*pid
, struct task_struct
*task
)
2762 int err
= lock_trace(task
);
2764 seq_printf(m
, "%08x\n", task
->personality
);
2773 static const struct file_operations proc_task_operations
;
2774 static const struct inode_operations proc_task_inode_operations
;
2776 static const struct pid_entry tgid_base_stuff
[] = {
2777 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2778 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2779 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2780 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2781 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2783 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2785 REG("environ", S_IRUSR
, proc_environ_operations
),
2786 ONE("auxv", S_IRUSR
, proc_pid_auxv
),
2787 ONE("status", S_IRUGO
, proc_pid_status
),
2788 ONE("personality", S_IRUSR
, proc_pid_personality
),
2789 ONE("limits", S_IRUGO
, proc_pid_limits
),
2790 #ifdef CONFIG_SCHED_DEBUG
2791 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2793 #ifdef CONFIG_SCHED_AUTOGROUP
2794 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2796 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2797 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2798 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2800 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
2801 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2802 ONE("statm", S_IRUGO
, proc_pid_statm
),
2803 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2805 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2807 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2808 LNK("cwd", proc_cwd_link
),
2809 LNK("root", proc_root_link
),
2810 LNK("exe", proc_exe_link
),
2811 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2812 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2813 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2814 #ifdef CONFIG_PROC_PAGE_MONITOR
2815 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2816 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2817 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2819 #ifdef CONFIG_SECURITY
2820 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2822 #ifdef CONFIG_KALLSYMS
2823 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2825 #ifdef CONFIG_STACKTRACE
2826 ONE("stack", S_IRUSR
, proc_pid_stack
),
2828 #ifdef CONFIG_SCHED_INFO
2829 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2831 #ifdef CONFIG_LATENCYTOP
2832 REG("latency", S_IRUGO
, proc_lstats_operations
),
2834 #ifdef CONFIG_PROC_PID_CPUSET
2835 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
2837 #ifdef CONFIG_CGROUPS
2838 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
2840 ONE("oom_score", S_IRUGO
, proc_oom_score
),
2841 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2842 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2843 #ifdef CONFIG_AUDITSYSCALL
2844 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2845 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2847 #ifdef CONFIG_FAULT_INJECTION
2848 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2850 #ifdef CONFIG_ELF_CORE
2851 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2853 #ifdef CONFIG_TASK_IO_ACCOUNTING
2854 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
2856 #ifdef CONFIG_HARDWALL
2857 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
2859 #ifdef CONFIG_USER_NS
2860 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2861 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2862 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2863 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2865 #ifdef CONFIG_CHECKPOINT_RESTORE
2866 REG("timers", S_IRUGO
, proc_timers_operations
),
2870 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2872 return proc_pident_readdir(file
, ctx
,
2873 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2876 static const struct file_operations proc_tgid_base_operations
= {
2877 .read
= generic_read_dir
,
2878 .iterate
= proc_tgid_base_readdir
,
2879 .llseek
= default_llseek
,
2882 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2884 return proc_pident_lookup(dir
, dentry
,
2885 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2888 static const struct inode_operations proc_tgid_base_inode_operations
= {
2889 .lookup
= proc_tgid_base_lookup
,
2890 .getattr
= pid_getattr
,
2891 .setattr
= proc_setattr
,
2892 .permission
= proc_pid_permission
,
2895 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2897 struct dentry
*dentry
, *leader
, *dir
;
2898 char buf
[PROC_NUMBUF
];
2902 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2903 /* no ->d_hash() rejects on procfs */
2904 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2906 d_invalidate(dentry
);
2914 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2915 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2920 name
.len
= strlen(name
.name
);
2921 dir
= d_hash_and_lookup(leader
, &name
);
2923 goto out_put_leader
;
2926 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2927 dentry
= d_hash_and_lookup(dir
, &name
);
2929 d_invalidate(dentry
);
2941 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2942 * @task: task that should be flushed.
2944 * When flushing dentries from proc, one needs to flush them from global
2945 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2946 * in. This call is supposed to do all of this job.
2948 * Looks in the dcache for
2950 * /proc/@tgid/task/@pid
2951 * if either directory is present flushes it and all of it'ts children
2954 * It is safe and reasonable to cache /proc entries for a task until
2955 * that task exits. After that they just clog up the dcache with
2956 * useless entries, possibly causing useful dcache entries to be
2957 * flushed instead. This routine is proved to flush those useless
2958 * dcache entries at process exit time.
2960 * NOTE: This routine is just an optimization so it does not guarantee
2961 * that no dcache entries will exist at process exit time it
2962 * just makes it very unlikely that any will persist.
2965 void proc_flush_task(struct task_struct
*task
)
2968 struct pid
*pid
, *tgid
;
2971 pid
= task_pid(task
);
2972 tgid
= task_tgid(task
);
2974 for (i
= 0; i
<= pid
->level
; i
++) {
2975 upid
= &pid
->numbers
[i
];
2976 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2977 tgid
->numbers
[i
].nr
);
2981 static int proc_pid_instantiate(struct inode
*dir
,
2982 struct dentry
* dentry
,
2983 struct task_struct
*task
, const void *ptr
)
2985 struct inode
*inode
;
2987 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2991 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2992 inode
->i_op
= &proc_tgid_base_inode_operations
;
2993 inode
->i_fop
= &proc_tgid_base_operations
;
2994 inode
->i_flags
|=S_IMMUTABLE
;
2996 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2997 ARRAY_SIZE(tgid_base_stuff
)));
2999 d_set_d_op(dentry
, &pid_dentry_operations
);
3001 d_add(dentry
, inode
);
3002 /* Close the race of the process dying before we return the dentry */
3003 if (pid_revalidate(dentry
, 0))
3009 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3011 int result
= -ENOENT
;
3012 struct task_struct
*task
;
3014 struct pid_namespace
*ns
;
3016 tgid
= name_to_int(&dentry
->d_name
);
3020 ns
= dentry
->d_sb
->s_fs_info
;
3022 task
= find_task_by_pid_ns(tgid
, ns
);
3024 get_task_struct(task
);
3029 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3030 put_task_struct(task
);
3032 return ERR_PTR(result
);
3036 * Find the first task with tgid >= tgid
3041 struct task_struct
*task
;
3043 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3048 put_task_struct(iter
.task
);
3052 pid
= find_ge_pid(iter
.tgid
, ns
);
3054 iter
.tgid
= pid_nr_ns(pid
, ns
);
3055 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3056 /* What we to know is if the pid we have find is the
3057 * pid of a thread_group_leader. Testing for task
3058 * being a thread_group_leader is the obvious thing
3059 * todo but there is a window when it fails, due to
3060 * the pid transfer logic in de_thread.
3062 * So we perform the straight forward test of seeing
3063 * if the pid we have found is the pid of a thread
3064 * group leader, and don't worry if the task we have
3065 * found doesn't happen to be a thread group leader.
3066 * As we don't care in the case of readdir.
3068 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3072 get_task_struct(iter
.task
);
3078 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3080 /* for the /proc/ directory itself, after non-process stuff has been done */
3081 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3083 struct tgid_iter iter
;
3084 struct pid_namespace
*ns
= file_inode(file
)->i_sb
->s_fs_info
;
3085 loff_t pos
= ctx
->pos
;
3087 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3090 if (pos
== TGID_OFFSET
- 2) {
3091 struct inode
*inode
= d_inode(ns
->proc_self
);
3092 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3094 ctx
->pos
= pos
= pos
+ 1;
3096 if (pos
== TGID_OFFSET
- 1) {
3097 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3098 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3100 ctx
->pos
= pos
= pos
+ 1;
3102 iter
.tgid
= pos
- TGID_OFFSET
;
3104 for (iter
= next_tgid(ns
, iter
);
3106 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3107 char name
[PROC_NUMBUF
];
3111 if (!has_pid_permissions(ns
, iter
.task
, 2))
3114 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3115 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3116 if (!proc_fill_cache(file
, ctx
, name
, len
,
3117 proc_pid_instantiate
, iter
.task
, NULL
)) {
3118 put_task_struct(iter
.task
);
3122 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3127 * proc_tid_comm_permission is a special permission function exclusively
3128 * used for the node /proc/<pid>/task/<tid>/comm.
3129 * It bypasses generic permission checks in the case where a task of the same
3130 * task group attempts to access the node.
3131 * The rationale behind this is that glibc and bionic access this node for
3132 * cross thread naming (pthread_set/getname_np(!self)). However, if
3133 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3134 * which locks out the cross thread naming implementation.
3135 * This function makes sure that the node is always accessible for members of
3136 * same thread group.
3138 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3140 bool is_same_tgroup
;
3141 struct task_struct
*task
;
3143 task
= get_proc_task(inode
);
3146 is_same_tgroup
= same_thread_group(current
, task
);
3147 put_task_struct(task
);
3149 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3150 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3151 * read or written by the members of the corresponding
3157 return generic_permission(inode
, mask
);
3160 static const struct inode_operations proc_tid_comm_inode_operations
= {
3161 .permission
= proc_tid_comm_permission
,
3167 static const struct pid_entry tid_base_stuff
[] = {
3168 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3169 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3170 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3172 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3174 REG("environ", S_IRUSR
, proc_environ_operations
),
3175 ONE("auxv", S_IRUSR
, proc_pid_auxv
),
3176 ONE("status", S_IRUGO
, proc_pid_status
),
3177 ONE("personality", S_IRUSR
, proc_pid_personality
),
3178 ONE("limits", S_IRUGO
, proc_pid_limits
),
3179 #ifdef CONFIG_SCHED_DEBUG
3180 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3182 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3183 &proc_tid_comm_inode_operations
,
3184 &proc_pid_set_comm_operations
, {}),
3185 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3186 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3188 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3189 ONE("stat", S_IRUGO
, proc_tid_stat
),
3190 ONE("statm", S_IRUGO
, proc_pid_statm
),
3191 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3192 #ifdef CONFIG_PROC_CHILDREN
3193 REG("children", S_IRUGO
, proc_tid_children_operations
),
3196 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3198 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3199 LNK("cwd", proc_cwd_link
),
3200 LNK("root", proc_root_link
),
3201 LNK("exe", proc_exe_link
),
3202 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3203 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3204 #ifdef CONFIG_PROC_PAGE_MONITOR
3205 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3206 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3207 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3209 #ifdef CONFIG_SECURITY
3210 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3212 #ifdef CONFIG_KALLSYMS
3213 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3215 #ifdef CONFIG_STACKTRACE
3216 ONE("stack", S_IRUSR
, proc_pid_stack
),
3218 #ifdef CONFIG_SCHED_INFO
3219 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3221 #ifdef CONFIG_LATENCYTOP
3222 REG("latency", S_IRUGO
, proc_lstats_operations
),
3224 #ifdef CONFIG_PROC_PID_CPUSET
3225 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3227 #ifdef CONFIG_CGROUPS
3228 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3230 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3231 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3232 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3233 #ifdef CONFIG_AUDITSYSCALL
3234 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3235 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3237 #ifdef CONFIG_FAULT_INJECTION
3238 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3240 #ifdef CONFIG_TASK_IO_ACCOUNTING
3241 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3243 #ifdef CONFIG_HARDWALL
3244 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3246 #ifdef CONFIG_USER_NS
3247 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3248 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3249 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3250 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3254 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3256 return proc_pident_readdir(file
, ctx
,
3257 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3260 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3262 return proc_pident_lookup(dir
, dentry
,
3263 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3266 static const struct file_operations proc_tid_base_operations
= {
3267 .read
= generic_read_dir
,
3268 .iterate
= proc_tid_base_readdir
,
3269 .llseek
= default_llseek
,
3272 static const struct inode_operations proc_tid_base_inode_operations
= {
3273 .lookup
= proc_tid_base_lookup
,
3274 .getattr
= pid_getattr
,
3275 .setattr
= proc_setattr
,
3278 static int proc_task_instantiate(struct inode
*dir
,
3279 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3281 struct inode
*inode
;
3282 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3286 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3287 inode
->i_op
= &proc_tid_base_inode_operations
;
3288 inode
->i_fop
= &proc_tid_base_operations
;
3289 inode
->i_flags
|=S_IMMUTABLE
;
3291 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3292 ARRAY_SIZE(tid_base_stuff
)));
3294 d_set_d_op(dentry
, &pid_dentry_operations
);
3296 d_add(dentry
, inode
);
3297 /* Close the race of the process dying before we return the dentry */
3298 if (pid_revalidate(dentry
, 0))
3304 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3306 int result
= -ENOENT
;
3307 struct task_struct
*task
;
3308 struct task_struct
*leader
= get_proc_task(dir
);
3310 struct pid_namespace
*ns
;
3315 tid
= name_to_int(&dentry
->d_name
);
3319 ns
= dentry
->d_sb
->s_fs_info
;
3321 task
= find_task_by_pid_ns(tid
, ns
);
3323 get_task_struct(task
);
3327 if (!same_thread_group(leader
, task
))
3330 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3332 put_task_struct(task
);
3334 put_task_struct(leader
);
3336 return ERR_PTR(result
);
3340 * Find the first tid of a thread group to return to user space.
3342 * Usually this is just the thread group leader, but if the users
3343 * buffer was too small or there was a seek into the middle of the
3344 * directory we have more work todo.
3346 * In the case of a short read we start with find_task_by_pid.
3348 * In the case of a seek we start with the leader and walk nr
3351 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3352 struct pid_namespace
*ns
)
3354 struct task_struct
*pos
, *task
;
3355 unsigned long nr
= f_pos
;
3357 if (nr
!= f_pos
) /* 32bit overflow? */
3361 task
= pid_task(pid
, PIDTYPE_PID
);
3365 /* Attempt to start with the tid of a thread */
3367 pos
= find_task_by_pid_ns(tid
, ns
);
3368 if (pos
&& same_thread_group(pos
, task
))
3372 /* If nr exceeds the number of threads there is nothing todo */
3373 if (nr
>= get_nr_threads(task
))
3376 /* If we haven't found our starting place yet start
3377 * with the leader and walk nr threads forward.
3379 pos
= task
= task
->group_leader
;
3383 } while_each_thread(task
, pos
);
3388 get_task_struct(pos
);
3395 * Find the next thread in the thread list.
3396 * Return NULL if there is an error or no next thread.
3398 * The reference to the input task_struct is released.
3400 static struct task_struct
*next_tid(struct task_struct
*start
)
3402 struct task_struct
*pos
= NULL
;
3404 if (pid_alive(start
)) {
3405 pos
= next_thread(start
);
3406 if (thread_group_leader(pos
))
3409 get_task_struct(pos
);
3412 put_task_struct(start
);
3416 /* for the /proc/TGID/task/ directories */
3417 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3419 struct inode
*inode
= file_inode(file
);
3420 struct task_struct
*task
;
3421 struct pid_namespace
*ns
;
3424 if (proc_inode_is_dead(inode
))
3427 if (!dir_emit_dots(file
, ctx
))
3430 /* f_version caches the tgid value that the last readdir call couldn't
3431 * return. lseek aka telldir automagically resets f_version to 0.
3433 ns
= inode
->i_sb
->s_fs_info
;
3434 tid
= (int)file
->f_version
;
3435 file
->f_version
= 0;
3436 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3438 task
= next_tid(task
), ctx
->pos
++) {
3439 char name
[PROC_NUMBUF
];
3441 tid
= task_pid_nr_ns(task
, ns
);
3442 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3443 if (!proc_fill_cache(file
, ctx
, name
, len
,
3444 proc_task_instantiate
, task
, NULL
)) {
3445 /* returning this tgid failed, save it as the first
3446 * pid for the next readir call */
3447 file
->f_version
= (u64
)tid
;
3448 put_task_struct(task
);
3456 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3458 struct inode
*inode
= d_inode(dentry
);
3459 struct task_struct
*p
= get_proc_task(inode
);
3460 generic_fillattr(inode
, stat
);
3463 stat
->nlink
+= get_nr_threads(p
);
3470 static const struct inode_operations proc_task_inode_operations
= {
3471 .lookup
= proc_task_lookup
,
3472 .getattr
= proc_task_getattr
,
3473 .setattr
= proc_setattr
,
3474 .permission
= proc_pid_permission
,
3477 static const struct file_operations proc_task_operations
= {
3478 .read
= generic_read_dir
,
3479 .iterate
= proc_task_readdir
,
3480 .llseek
= default_llseek
,