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>
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
111 const struct inode_operations
*iop
;
112 const struct file_operations
*fop
;
116 #define NOD(NAME, MODE, IOP, FOP, OP) { \
118 .len = sizeof(NAME) - 1, \
125 #define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127 #define LNK(NAME, get_link) \
128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
129 &proc_pid_link_inode_operations, NULL, \
130 { .proc_get_link = get_link } )
131 #define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133 #define INF(NAME, MODE, read) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_info_file_operations, \
136 { .proc_read = read } )
137 #define ONE(NAME, MODE, show) \
138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
140 { .proc_show = show } )
143 * Count the number of hardlinks for the pid_entry table, excluding the .
146 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
153 for (i
= 0; i
< n
; ++i
) {
154 if (S_ISDIR(entries
[i
].mode
))
161 static int get_task_root(struct task_struct
*task
, struct path
*root
)
163 int result
= -ENOENT
;
167 get_fs_root(task
->fs
, root
);
174 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
176 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
177 int result
= -ENOENT
;
182 get_fs_pwd(task
->fs
, path
);
186 put_task_struct(task
);
191 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
193 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
194 int result
= -ENOENT
;
197 result
= get_task_root(task
, path
);
198 put_task_struct(task
);
203 static int proc_pid_cmdline(struct task_struct
*task
, char *buffer
)
205 return get_cmdline(task
, buffer
, PAGE_SIZE
);
208 static int proc_pid_auxv(struct seq_file
*m
, struct pid_namespace
*ns
,
209 struct pid
*pid
, struct task_struct
*task
)
211 struct mm_struct
*mm
= mm_access(task
, PTRACE_MODE_READ
);
212 if (mm
&& !IS_ERR(mm
)) {
213 unsigned int nwords
= 0;
216 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
217 seq_write(m
, mm
->saved_auxv
, nwords
* sizeof(mm
->saved_auxv
[0]));
225 #ifdef CONFIG_KALLSYMS
227 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
228 * Returns the resolved symbol. If that fails, simply return the address.
230 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
233 char symname
[KSYM_NAME_LEN
];
235 wchan
= get_wchan(task
);
237 if (lookup_symbol_name(wchan
, symname
) < 0)
238 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
241 return sprintf(buffer
, "%lu", wchan
);
243 return sprintf(buffer
, "%s", symname
);
245 #endif /* CONFIG_KALLSYMS */
247 static int lock_trace(struct task_struct
*task
)
249 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
252 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
253 mutex_unlock(&task
->signal
->cred_guard_mutex
);
259 static void unlock_trace(struct task_struct
*task
)
261 mutex_unlock(&task
->signal
->cred_guard_mutex
);
264 #ifdef CONFIG_STACKTRACE
266 #define MAX_STACK_TRACE_DEPTH 64
268 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
269 struct pid
*pid
, struct task_struct
*task
)
271 struct stack_trace trace
;
272 unsigned long *entries
;
276 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
280 trace
.nr_entries
= 0;
281 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
282 trace
.entries
= entries
;
285 err
= lock_trace(task
);
287 save_stack_trace_tsk(task
, &trace
);
289 for (i
= 0; i
< trace
.nr_entries
; i
++) {
290 seq_printf(m
, "[<%pK>] %pS\n",
291 (void *)entries
[i
], (void *)entries
[i
]);
301 #ifdef CONFIG_SCHEDSTATS
303 * Provides /proc/PID/schedstat
305 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
307 return sprintf(buffer
, "%llu %llu %lu\n",
308 (unsigned long long)task
->se
.sum_exec_runtime
,
309 (unsigned long long)task
->sched_info
.run_delay
,
310 task
->sched_info
.pcount
);
314 #ifdef CONFIG_LATENCYTOP
315 static int lstats_show_proc(struct seq_file
*m
, void *v
)
318 struct inode
*inode
= m
->private;
319 struct task_struct
*task
= get_proc_task(inode
);
323 seq_puts(m
, "Latency Top version : v0.1\n");
324 for (i
= 0; i
< 32; i
++) {
325 struct latency_record
*lr
= &task
->latency_record
[i
];
326 if (lr
->backtrace
[0]) {
328 seq_printf(m
, "%i %li %li",
329 lr
->count
, lr
->time
, lr
->max
);
330 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
331 unsigned long bt
= lr
->backtrace
[q
];
336 seq_printf(m
, " %ps", (void *)bt
);
342 put_task_struct(task
);
346 static int lstats_open(struct inode
*inode
, struct file
*file
)
348 return single_open(file
, lstats_show_proc
, inode
);
351 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
352 size_t count
, loff_t
*offs
)
354 struct task_struct
*task
= get_proc_task(file_inode(file
));
358 clear_all_latency_tracing(task
);
359 put_task_struct(task
);
364 static const struct file_operations proc_lstats_operations
= {
367 .write
= lstats_write
,
369 .release
= single_release
,
374 #ifdef CONFIG_CGROUPS
375 static int cgroup_open(struct inode
*inode
, struct file
*file
)
377 struct pid
*pid
= PROC_I(inode
)->pid
;
378 return single_open(file
, proc_cgroup_show
, pid
);
381 static const struct file_operations proc_cgroup_operations
= {
385 .release
= single_release
,
389 #ifdef CONFIG_PROC_PID_CPUSET
391 static int cpuset_open(struct inode
*inode
, struct file
*file
)
393 struct pid
*pid
= PROC_I(inode
)->pid
;
394 return single_open(file
, proc_cpuset_show
, pid
);
397 static const struct file_operations proc_cpuset_operations
= {
401 .release
= single_release
,
405 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
407 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
408 unsigned long points
= 0;
410 read_lock(&tasklist_lock
);
412 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
414 read_unlock(&tasklist_lock
);
415 return sprintf(buffer
, "%lu\n", points
);
423 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
424 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
425 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
426 [RLIMIT_DATA
] = {"Max data size", "bytes"},
427 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
428 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
429 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
430 [RLIMIT_NPROC
] = {"Max processes", "processes"},
431 [RLIMIT_NOFILE
] = {"Max open files", "files"},
432 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
433 [RLIMIT_AS
] = {"Max address space", "bytes"},
434 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
435 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
436 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
437 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
438 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
439 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
442 /* Display limits for a process */
443 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
448 char *bufptr
= buffer
;
450 struct rlimit rlim
[RLIM_NLIMITS
];
452 if (!lock_task_sighand(task
, &flags
))
454 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
455 unlock_task_sighand(task
, &flags
);
458 * print the file header
460 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
461 "Limit", "Soft Limit", "Hard Limit", "Units");
463 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
464 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
465 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
466 lnames
[i
].name
, "unlimited");
468 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
469 lnames
[i
].name
, rlim
[i
].rlim_cur
);
471 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
472 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
474 count
+= sprintf(&bufptr
[count
], "%-20lu ",
478 count
+= sprintf(&bufptr
[count
], "%-10s\n",
481 count
+= sprintf(&bufptr
[count
], "\n");
487 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
488 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
491 unsigned long args
[6], sp
, pc
;
492 int res
= lock_trace(task
);
496 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
497 res
= sprintf(buffer
, "running\n");
499 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
501 res
= sprintf(buffer
,
502 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
504 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
509 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
511 /************************************************************************/
512 /* Here the fs part begins */
513 /************************************************************************/
515 /* permission checks */
516 static int proc_fd_access_allowed(struct inode
*inode
)
518 struct task_struct
*task
;
520 /* Allow access to a task's file descriptors if it is us or we
521 * may use ptrace attach to the process and find out that
524 task
= get_proc_task(inode
);
526 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
527 put_task_struct(task
);
532 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
535 struct inode
*inode
= dentry
->d_inode
;
537 if (attr
->ia_valid
& ATTR_MODE
)
540 error
= inode_change_ok(inode
, attr
);
544 setattr_copy(inode
, attr
);
545 mark_inode_dirty(inode
);
550 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
551 * or euid/egid (for hide_pid_min=2)?
553 static bool has_pid_permissions(struct pid_namespace
*pid
,
554 struct task_struct
*task
,
557 if (pid
->hide_pid
< hide_pid_min
)
559 if (in_group_p(pid
->pid_gid
))
561 return ptrace_may_access(task
, PTRACE_MODE_READ
);
565 static int proc_pid_permission(struct inode
*inode
, int mask
)
567 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
568 struct task_struct
*task
;
571 task
= get_proc_task(inode
);
574 has_perms
= has_pid_permissions(pid
, task
, 1);
575 put_task_struct(task
);
578 if (pid
->hide_pid
== 2) {
580 * Let's make getdents(), stat(), and open()
581 * consistent with each other. If a process
582 * may not stat() a file, it shouldn't be seen
590 return generic_permission(inode
, mask
);
595 static const struct inode_operations proc_def_inode_operations
= {
596 .setattr
= proc_setattr
,
599 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
601 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
602 size_t count
, loff_t
*ppos
)
604 struct inode
* inode
= file_inode(file
);
607 struct task_struct
*task
= get_proc_task(inode
);
613 if (count
> PROC_BLOCK_SIZE
)
614 count
= PROC_BLOCK_SIZE
;
617 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
620 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
623 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
626 put_task_struct(task
);
631 static const struct file_operations proc_info_file_operations
= {
632 .read
= proc_info_read
,
633 .llseek
= generic_file_llseek
,
636 static int proc_single_show(struct seq_file
*m
, void *v
)
638 struct inode
*inode
= m
->private;
639 struct pid_namespace
*ns
;
641 struct task_struct
*task
;
644 ns
= inode
->i_sb
->s_fs_info
;
645 pid
= proc_pid(inode
);
646 task
= get_pid_task(pid
, PIDTYPE_PID
);
650 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
652 put_task_struct(task
);
656 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
658 return single_open(filp
, proc_single_show
, inode
);
661 static const struct file_operations proc_single_file_operations
= {
662 .open
= proc_single_open
,
665 .release
= single_release
,
668 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
670 struct task_struct
*task
= get_proc_task(file_inode(file
));
671 struct mm_struct
*mm
;
676 mm
= mm_access(task
, mode
);
677 put_task_struct(task
);
683 /* ensure this mm_struct can't be freed */
684 atomic_inc(&mm
->mm_count
);
685 /* but do not pin its memory */
689 file
->private_data
= mm
;
694 static int mem_open(struct inode
*inode
, struct file
*file
)
696 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
698 /* OK to pass negative loff_t, we can catch out-of-range */
699 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
704 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
705 size_t count
, loff_t
*ppos
, int write
)
707 struct mm_struct
*mm
= file
->private_data
;
708 unsigned long addr
= *ppos
;
715 page
= (char *)__get_free_page(GFP_TEMPORARY
);
720 if (!atomic_inc_not_zero(&mm
->mm_users
))
724 int this_len
= min_t(int, count
, PAGE_SIZE
);
726 if (write
&& copy_from_user(page
, buf
, this_len
)) {
731 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
738 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
752 free_page((unsigned long) page
);
756 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
757 size_t count
, loff_t
*ppos
)
759 return mem_rw(file
, buf
, count
, ppos
, 0);
762 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
763 size_t count
, loff_t
*ppos
)
765 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
768 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
772 file
->f_pos
= offset
;
775 file
->f_pos
+= offset
;
780 force_successful_syscall_return();
784 static int mem_release(struct inode
*inode
, struct file
*file
)
786 struct mm_struct
*mm
= file
->private_data
;
792 static const struct file_operations proc_mem_operations
= {
797 .release
= mem_release
,
800 static int environ_open(struct inode
*inode
, struct file
*file
)
802 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
805 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
806 size_t count
, loff_t
*ppos
)
809 unsigned long src
= *ppos
;
811 struct mm_struct
*mm
= file
->private_data
;
816 page
= (char *)__get_free_page(GFP_TEMPORARY
);
821 if (!atomic_inc_not_zero(&mm
->mm_users
))
824 size_t this_len
, max_len
;
827 if (src
>= (mm
->env_end
- mm
->env_start
))
830 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
832 max_len
= min_t(size_t, PAGE_SIZE
, count
);
833 this_len
= min(max_len
, this_len
);
835 retval
= access_remote_vm(mm
, (mm
->env_start
+ src
),
843 if (copy_to_user(buf
, page
, retval
)) {
857 free_page((unsigned long) page
);
861 static const struct file_operations proc_environ_operations
= {
862 .open
= environ_open
,
863 .read
= environ_read
,
864 .llseek
= generic_file_llseek
,
865 .release
= mem_release
,
868 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
871 struct task_struct
*task
= get_proc_task(file_inode(file
));
872 char buffer
[PROC_NUMBUF
];
873 int oom_adj
= OOM_ADJUST_MIN
;
879 if (lock_task_sighand(task
, &flags
)) {
880 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
881 oom_adj
= OOM_ADJUST_MAX
;
883 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
885 unlock_task_sighand(task
, &flags
);
887 put_task_struct(task
);
888 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
889 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
892 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
893 size_t count
, loff_t
*ppos
)
895 struct task_struct
*task
;
896 char buffer
[PROC_NUMBUF
];
901 memset(buffer
, 0, sizeof(buffer
));
902 if (count
> sizeof(buffer
) - 1)
903 count
= sizeof(buffer
) - 1;
904 if (copy_from_user(buffer
, buf
, count
)) {
909 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
912 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
913 oom_adj
!= OOM_DISABLE
) {
918 task
= get_proc_task(file_inode(file
));
930 if (!lock_task_sighand(task
, &flags
)) {
936 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
937 * value is always attainable.
939 if (oom_adj
== OOM_ADJUST_MAX
)
940 oom_adj
= OOM_SCORE_ADJ_MAX
;
942 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
944 if (oom_adj
< task
->signal
->oom_score_adj
&&
945 !capable(CAP_SYS_RESOURCE
)) {
951 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
952 * /proc/pid/oom_score_adj instead.
954 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
955 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
958 task
->signal
->oom_score_adj
= oom_adj
;
959 trace_oom_score_adj_update(task
);
961 unlock_task_sighand(task
, &flags
);
964 put_task_struct(task
);
966 return err
< 0 ? err
: count
;
969 static const struct file_operations proc_oom_adj_operations
= {
970 .read
= oom_adj_read
,
971 .write
= oom_adj_write
,
972 .llseek
= generic_file_llseek
,
975 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
976 size_t count
, loff_t
*ppos
)
978 struct task_struct
*task
= get_proc_task(file_inode(file
));
979 char buffer
[PROC_NUMBUF
];
980 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
986 if (lock_task_sighand(task
, &flags
)) {
987 oom_score_adj
= task
->signal
->oom_score_adj
;
988 unlock_task_sighand(task
, &flags
);
990 put_task_struct(task
);
991 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
992 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
995 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
996 size_t count
, loff_t
*ppos
)
998 struct task_struct
*task
;
999 char buffer
[PROC_NUMBUF
];
1000 unsigned long flags
;
1004 memset(buffer
, 0, sizeof(buffer
));
1005 if (count
> sizeof(buffer
) - 1)
1006 count
= sizeof(buffer
) - 1;
1007 if (copy_from_user(buffer
, buf
, count
)) {
1012 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1015 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1016 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1021 task
= get_proc_task(file_inode(file
));
1033 if (!lock_task_sighand(task
, &flags
)) {
1038 if ((short)oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1039 !capable(CAP_SYS_RESOURCE
)) {
1044 task
->signal
->oom_score_adj
= (short)oom_score_adj
;
1045 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1046 task
->signal
->oom_score_adj_min
= (short)oom_score_adj
;
1047 trace_oom_score_adj_update(task
);
1050 unlock_task_sighand(task
, &flags
);
1053 put_task_struct(task
);
1055 return err
< 0 ? err
: count
;
1058 static const struct file_operations proc_oom_score_adj_operations
= {
1059 .read
= oom_score_adj_read
,
1060 .write
= oom_score_adj_write
,
1061 .llseek
= default_llseek
,
1064 #ifdef CONFIG_AUDITSYSCALL
1065 #define TMPBUFLEN 21
1066 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1067 size_t count
, loff_t
*ppos
)
1069 struct inode
* inode
= file_inode(file
);
1070 struct task_struct
*task
= get_proc_task(inode
);
1072 char tmpbuf
[TMPBUFLEN
];
1076 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1077 from_kuid(file
->f_cred
->user_ns
,
1078 audit_get_loginuid(task
)));
1079 put_task_struct(task
);
1080 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1083 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1084 size_t count
, loff_t
*ppos
)
1086 struct inode
* inode
= file_inode(file
);
1093 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1099 if (count
>= PAGE_SIZE
)
1100 count
= PAGE_SIZE
- 1;
1103 /* No partial writes. */
1106 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1110 if (copy_from_user(page
, buf
, count
))
1114 loginuid
= simple_strtoul(page
, &tmp
, 10);
1121 /* is userspace tring to explicitly UNSET the loginuid? */
1122 if (loginuid
== AUDIT_UID_UNSET
) {
1123 kloginuid
= INVALID_UID
;
1125 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1126 if (!uid_valid(kloginuid
)) {
1132 length
= audit_set_loginuid(kloginuid
);
1133 if (likely(length
== 0))
1137 free_page((unsigned long) page
);
1141 static const struct file_operations proc_loginuid_operations
= {
1142 .read
= proc_loginuid_read
,
1143 .write
= proc_loginuid_write
,
1144 .llseek
= generic_file_llseek
,
1147 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1148 size_t count
, loff_t
*ppos
)
1150 struct inode
* inode
= file_inode(file
);
1151 struct task_struct
*task
= get_proc_task(inode
);
1153 char tmpbuf
[TMPBUFLEN
];
1157 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1158 audit_get_sessionid(task
));
1159 put_task_struct(task
);
1160 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1163 static const struct file_operations proc_sessionid_operations
= {
1164 .read
= proc_sessionid_read
,
1165 .llseek
= generic_file_llseek
,
1169 #ifdef CONFIG_FAULT_INJECTION
1170 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1171 size_t count
, loff_t
*ppos
)
1173 struct task_struct
*task
= get_proc_task(file_inode(file
));
1174 char buffer
[PROC_NUMBUF
];
1180 make_it_fail
= task
->make_it_fail
;
1181 put_task_struct(task
);
1183 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1185 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1188 static ssize_t
proc_fault_inject_write(struct file
* file
,
1189 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1191 struct task_struct
*task
;
1192 char buffer
[PROC_NUMBUF
], *end
;
1195 if (!capable(CAP_SYS_RESOURCE
))
1197 memset(buffer
, 0, sizeof(buffer
));
1198 if (count
> sizeof(buffer
) - 1)
1199 count
= sizeof(buffer
) - 1;
1200 if (copy_from_user(buffer
, buf
, count
))
1202 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1205 if (make_it_fail
< 0 || make_it_fail
> 1)
1208 task
= get_proc_task(file_inode(file
));
1211 task
->make_it_fail
= make_it_fail
;
1212 put_task_struct(task
);
1217 static const struct file_operations proc_fault_inject_operations
= {
1218 .read
= proc_fault_inject_read
,
1219 .write
= proc_fault_inject_write
,
1220 .llseek
= generic_file_llseek
,
1225 #ifdef CONFIG_SCHED_DEBUG
1227 * Print out various scheduling related per-task fields:
1229 static int sched_show(struct seq_file
*m
, void *v
)
1231 struct inode
*inode
= m
->private;
1232 struct task_struct
*p
;
1234 p
= get_proc_task(inode
);
1237 proc_sched_show_task(p
, m
);
1245 sched_write(struct file
*file
, const char __user
*buf
,
1246 size_t count
, loff_t
*offset
)
1248 struct inode
*inode
= file_inode(file
);
1249 struct task_struct
*p
;
1251 p
= get_proc_task(inode
);
1254 proc_sched_set_task(p
);
1261 static int sched_open(struct inode
*inode
, struct file
*filp
)
1263 return single_open(filp
, sched_show
, inode
);
1266 static const struct file_operations proc_pid_sched_operations
= {
1269 .write
= sched_write
,
1270 .llseek
= seq_lseek
,
1271 .release
= single_release
,
1276 #ifdef CONFIG_SCHED_AUTOGROUP
1278 * Print out autogroup related information:
1280 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1282 struct inode
*inode
= m
->private;
1283 struct task_struct
*p
;
1285 p
= get_proc_task(inode
);
1288 proc_sched_autogroup_show_task(p
, m
);
1296 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1297 size_t count
, loff_t
*offset
)
1299 struct inode
*inode
= file_inode(file
);
1300 struct task_struct
*p
;
1301 char buffer
[PROC_NUMBUF
];
1305 memset(buffer
, 0, sizeof(buffer
));
1306 if (count
> sizeof(buffer
) - 1)
1307 count
= sizeof(buffer
) - 1;
1308 if (copy_from_user(buffer
, buf
, count
))
1311 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1315 p
= get_proc_task(inode
);
1319 err
= proc_sched_autogroup_set_nice(p
, nice
);
1328 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1332 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1334 struct seq_file
*m
= filp
->private_data
;
1341 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1342 .open
= sched_autogroup_open
,
1344 .write
= sched_autogroup_write
,
1345 .llseek
= seq_lseek
,
1346 .release
= single_release
,
1349 #endif /* CONFIG_SCHED_AUTOGROUP */
1351 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1352 size_t count
, loff_t
*offset
)
1354 struct inode
*inode
= file_inode(file
);
1355 struct task_struct
*p
;
1356 char buffer
[TASK_COMM_LEN
];
1357 const size_t maxlen
= sizeof(buffer
) - 1;
1359 memset(buffer
, 0, sizeof(buffer
));
1360 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1363 p
= get_proc_task(inode
);
1367 if (same_thread_group(current
, p
))
1368 set_task_comm(p
, buffer
);
1377 static int comm_show(struct seq_file
*m
, void *v
)
1379 struct inode
*inode
= m
->private;
1380 struct task_struct
*p
;
1382 p
= get_proc_task(inode
);
1387 seq_printf(m
, "%s\n", p
->comm
);
1395 static int comm_open(struct inode
*inode
, struct file
*filp
)
1397 return single_open(filp
, comm_show
, inode
);
1400 static const struct file_operations proc_pid_set_comm_operations
= {
1403 .write
= comm_write
,
1404 .llseek
= seq_lseek
,
1405 .release
= single_release
,
1408 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1410 struct task_struct
*task
;
1411 struct mm_struct
*mm
;
1412 struct file
*exe_file
;
1414 task
= get_proc_task(dentry
->d_inode
);
1417 mm
= get_task_mm(task
);
1418 put_task_struct(task
);
1421 exe_file
= get_mm_exe_file(mm
);
1424 *exe_path
= exe_file
->f_path
;
1425 path_get(&exe_file
->f_path
);
1432 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1434 struct inode
*inode
= dentry
->d_inode
;
1436 int error
= -EACCES
;
1438 /* Are we allowed to snoop on the tasks file descriptors? */
1439 if (!proc_fd_access_allowed(inode
))
1442 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1446 nd_jump_link(nd
, &path
);
1449 return ERR_PTR(error
);
1452 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1454 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1461 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1462 len
= PTR_ERR(pathname
);
1463 if (IS_ERR(pathname
))
1465 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1469 if (copy_to_user(buffer
, pathname
, len
))
1472 free_page((unsigned long)tmp
);
1476 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1478 int error
= -EACCES
;
1479 struct inode
*inode
= dentry
->d_inode
;
1482 /* Are we allowed to snoop on the tasks file descriptors? */
1483 if (!proc_fd_access_allowed(inode
))
1486 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1490 error
= do_proc_readlink(&path
, buffer
, buflen
);
1496 const struct inode_operations proc_pid_link_inode_operations
= {
1497 .readlink
= proc_pid_readlink
,
1498 .follow_link
= proc_pid_follow_link
,
1499 .setattr
= proc_setattr
,
1503 /* building an inode */
1505 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1507 struct inode
* inode
;
1508 struct proc_inode
*ei
;
1509 const struct cred
*cred
;
1511 /* We need a new inode */
1513 inode
= new_inode(sb
);
1519 inode
->i_ino
= get_next_ino();
1520 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1521 inode
->i_op
= &proc_def_inode_operations
;
1524 * grab the reference to task.
1526 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1530 if (task_dumpable(task
)) {
1532 cred
= __task_cred(task
);
1533 inode
->i_uid
= cred
->euid
;
1534 inode
->i_gid
= cred
->egid
;
1537 security_task_to_inode(task
, inode
);
1547 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1549 struct inode
*inode
= dentry
->d_inode
;
1550 struct task_struct
*task
;
1551 const struct cred
*cred
;
1552 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1554 generic_fillattr(inode
, stat
);
1557 stat
->uid
= GLOBAL_ROOT_UID
;
1558 stat
->gid
= GLOBAL_ROOT_GID
;
1559 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1561 if (!has_pid_permissions(pid
, task
, 2)) {
1564 * This doesn't prevent learning whether PID exists,
1565 * it only makes getattr() consistent with readdir().
1569 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1570 task_dumpable(task
)) {
1571 cred
= __task_cred(task
);
1572 stat
->uid
= cred
->euid
;
1573 stat
->gid
= cred
->egid
;
1583 * Exceptional case: normally we are not allowed to unhash a busy
1584 * directory. In this case, however, we can do it - no aliasing problems
1585 * due to the way we treat inodes.
1587 * Rewrite the inode's ownerships here because the owning task may have
1588 * performed a setuid(), etc.
1590 * Before the /proc/pid/status file was created the only way to read
1591 * the effective uid of a /process was to stat /proc/pid. Reading
1592 * /proc/pid/status is slow enough that procps and other packages
1593 * kept stating /proc/pid. To keep the rules in /proc simple I have
1594 * made this apply to all per process world readable and executable
1597 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1599 struct inode
*inode
;
1600 struct task_struct
*task
;
1601 const struct cred
*cred
;
1603 if (flags
& LOOKUP_RCU
)
1606 inode
= dentry
->d_inode
;
1607 task
= get_proc_task(inode
);
1610 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1611 task_dumpable(task
)) {
1613 cred
= __task_cred(task
);
1614 inode
->i_uid
= cred
->euid
;
1615 inode
->i_gid
= cred
->egid
;
1618 inode
->i_uid
= GLOBAL_ROOT_UID
;
1619 inode
->i_gid
= GLOBAL_ROOT_GID
;
1621 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1622 security_task_to_inode(task
, inode
);
1623 put_task_struct(task
);
1630 static inline bool proc_inode_is_dead(struct inode
*inode
)
1632 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1635 int pid_delete_dentry(const struct dentry
*dentry
)
1637 /* Is the task we represent dead?
1638 * If so, then don't put the dentry on the lru list,
1639 * kill it immediately.
1641 return proc_inode_is_dead(dentry
->d_inode
);
1644 const struct dentry_operations pid_dentry_operations
=
1646 .d_revalidate
= pid_revalidate
,
1647 .d_delete
= pid_delete_dentry
,
1653 * Fill a directory entry.
1655 * If possible create the dcache entry and derive our inode number and
1656 * file type from dcache entry.
1658 * Since all of the proc inode numbers are dynamically generated, the inode
1659 * numbers do not exist until the inode is cache. This means creating the
1660 * the dcache entry in readdir is necessary to keep the inode numbers
1661 * reported by readdir in sync with the inode numbers reported
1664 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1665 const char *name
, int len
,
1666 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1668 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1669 struct qstr qname
= QSTR_INIT(name
, len
);
1670 struct inode
*inode
;
1674 child
= d_hash_and_lookup(dir
, &qname
);
1676 child
= d_alloc(dir
, &qname
);
1678 goto end_instantiate
;
1679 if (instantiate(dir
->d_inode
, child
, task
, ptr
) < 0) {
1681 goto end_instantiate
;
1684 inode
= child
->d_inode
;
1686 type
= inode
->i_mode
>> 12;
1688 return dir_emit(ctx
, name
, len
, ino
, type
);
1691 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1694 #ifdef CONFIG_CHECKPOINT_RESTORE
1697 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1698 * which represent vma start and end addresses.
1700 static int dname_to_vma_addr(struct dentry
*dentry
,
1701 unsigned long *start
, unsigned long *end
)
1703 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1709 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1711 unsigned long vm_start
, vm_end
;
1712 bool exact_vma_exists
= false;
1713 struct mm_struct
*mm
= NULL
;
1714 struct task_struct
*task
;
1715 const struct cred
*cred
;
1716 struct inode
*inode
;
1719 if (flags
& LOOKUP_RCU
)
1722 if (!capable(CAP_SYS_ADMIN
)) {
1727 inode
= dentry
->d_inode
;
1728 task
= get_proc_task(inode
);
1732 mm
= mm_access(task
, PTRACE_MODE_READ
);
1733 if (IS_ERR_OR_NULL(mm
))
1736 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1737 down_read(&mm
->mmap_sem
);
1738 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1739 up_read(&mm
->mmap_sem
);
1744 if (exact_vma_exists
) {
1745 if (task_dumpable(task
)) {
1747 cred
= __task_cred(task
);
1748 inode
->i_uid
= cred
->euid
;
1749 inode
->i_gid
= cred
->egid
;
1752 inode
->i_uid
= GLOBAL_ROOT_UID
;
1753 inode
->i_gid
= GLOBAL_ROOT_GID
;
1755 security_task_to_inode(task
, inode
);
1760 put_task_struct(task
);
1769 static const struct dentry_operations tid_map_files_dentry_operations
= {
1770 .d_revalidate
= map_files_d_revalidate
,
1771 .d_delete
= pid_delete_dentry
,
1774 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1776 unsigned long vm_start
, vm_end
;
1777 struct vm_area_struct
*vma
;
1778 struct task_struct
*task
;
1779 struct mm_struct
*mm
;
1783 task
= get_proc_task(dentry
->d_inode
);
1787 mm
= get_task_mm(task
);
1788 put_task_struct(task
);
1792 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1797 down_read(&mm
->mmap_sem
);
1798 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1799 if (vma
&& vma
->vm_file
) {
1800 *path
= vma
->vm_file
->f_path
;
1804 up_read(&mm
->mmap_sem
);
1812 struct map_files_info
{
1815 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1819 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
1820 struct task_struct
*task
, const void *ptr
)
1822 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
1823 struct proc_inode
*ei
;
1824 struct inode
*inode
;
1826 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1831 ei
->op
.proc_get_link
= proc_map_files_get_link
;
1833 inode
->i_op
= &proc_pid_link_inode_operations
;
1835 inode
->i_mode
= S_IFLNK
;
1837 if (mode
& FMODE_READ
)
1838 inode
->i_mode
|= S_IRUSR
;
1839 if (mode
& FMODE_WRITE
)
1840 inode
->i_mode
|= S_IWUSR
;
1842 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
1843 d_add(dentry
, inode
);
1848 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
1849 struct dentry
*dentry
, unsigned int flags
)
1851 unsigned long vm_start
, vm_end
;
1852 struct vm_area_struct
*vma
;
1853 struct task_struct
*task
;
1855 struct mm_struct
*mm
;
1858 if (!capable(CAP_SYS_ADMIN
))
1862 task
= get_proc_task(dir
);
1867 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1871 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
1874 mm
= get_task_mm(task
);
1878 down_read(&mm
->mmap_sem
);
1879 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1884 result
= proc_map_files_instantiate(dir
, dentry
, task
,
1885 (void *)(unsigned long)vma
->vm_file
->f_mode
);
1888 up_read(&mm
->mmap_sem
);
1891 put_task_struct(task
);
1893 return ERR_PTR(result
);
1896 static const struct inode_operations proc_map_files_inode_operations
= {
1897 .lookup
= proc_map_files_lookup
,
1898 .permission
= proc_fd_permission
,
1899 .setattr
= proc_setattr
,
1903 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
1905 struct vm_area_struct
*vma
;
1906 struct task_struct
*task
;
1907 struct mm_struct
*mm
;
1908 unsigned long nr_files
, pos
, i
;
1909 struct flex_array
*fa
= NULL
;
1910 struct map_files_info info
;
1911 struct map_files_info
*p
;
1915 if (!capable(CAP_SYS_ADMIN
))
1919 task
= get_proc_task(file_inode(file
));
1924 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1928 if (!dir_emit_dots(file
, ctx
))
1931 mm
= get_task_mm(task
);
1934 down_read(&mm
->mmap_sem
);
1939 * We need two passes here:
1941 * 1) Collect vmas of mapped files with mmap_sem taken
1942 * 2) Release mmap_sem and instantiate entries
1944 * otherwise we get lockdep complained, since filldir()
1945 * routine might require mmap_sem taken in might_fault().
1948 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
1949 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
1954 fa
= flex_array_alloc(sizeof(info
), nr_files
,
1956 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
1960 flex_array_free(fa
);
1961 up_read(&mm
->mmap_sem
);
1965 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
1966 vma
= vma
->vm_next
) {
1969 if (++pos
<= ctx
->pos
)
1972 info
.mode
= vma
->vm_file
->f_mode
;
1973 info
.len
= snprintf(info
.name
,
1974 sizeof(info
.name
), "%lx-%lx",
1975 vma
->vm_start
, vma
->vm_end
);
1976 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
1980 up_read(&mm
->mmap_sem
);
1982 for (i
= 0; i
< nr_files
; i
++) {
1983 p
= flex_array_get(fa
, i
);
1984 if (!proc_fill_cache(file
, ctx
,
1986 proc_map_files_instantiate
,
1988 (void *)(unsigned long)p
->mode
))
1993 flex_array_free(fa
);
1997 put_task_struct(task
);
2002 static const struct file_operations proc_map_files_operations
= {
2003 .read
= generic_read_dir
,
2004 .iterate
= proc_map_files_readdir
,
2005 .llseek
= default_llseek
,
2008 struct timers_private
{
2010 struct task_struct
*task
;
2011 struct sighand_struct
*sighand
;
2012 struct pid_namespace
*ns
;
2013 unsigned long flags
;
2016 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2018 struct timers_private
*tp
= m
->private;
2020 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2022 return ERR_PTR(-ESRCH
);
2024 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2026 return ERR_PTR(-ESRCH
);
2028 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2031 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2033 struct timers_private
*tp
= m
->private;
2034 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2037 static void timers_stop(struct seq_file
*m
, void *v
)
2039 struct timers_private
*tp
= m
->private;
2042 unlock_task_sighand(tp
->task
, &tp
->flags
);
2047 put_task_struct(tp
->task
);
2052 static int show_timer(struct seq_file
*m
, void *v
)
2054 struct k_itimer
*timer
;
2055 struct timers_private
*tp
= m
->private;
2057 static const char * const nstr
[] = {
2058 [SIGEV_SIGNAL
] = "signal",
2059 [SIGEV_NONE
] = "none",
2060 [SIGEV_THREAD
] = "thread",
2063 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2064 notify
= timer
->it_sigev_notify
;
2066 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2067 seq_printf(m
, "signal: %d/%p\n", timer
->sigq
->info
.si_signo
,
2068 timer
->sigq
->info
.si_value
.sival_ptr
);
2069 seq_printf(m
, "notify: %s/%s.%d\n",
2070 nstr
[notify
& ~SIGEV_THREAD_ID
],
2071 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2072 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2073 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2078 static const struct seq_operations proc_timers_seq_ops
= {
2079 .start
= timers_start
,
2080 .next
= timers_next
,
2081 .stop
= timers_stop
,
2085 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2087 struct timers_private
*tp
;
2089 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2090 sizeof(struct timers_private
));
2094 tp
->pid
= proc_pid(inode
);
2095 tp
->ns
= inode
->i_sb
->s_fs_info
;
2099 static const struct file_operations proc_timers_operations
= {
2100 .open
= proc_timers_open
,
2102 .llseek
= seq_lseek
,
2103 .release
= seq_release_private
,
2105 #endif /* CONFIG_CHECKPOINT_RESTORE */
2107 static int proc_pident_instantiate(struct inode
*dir
,
2108 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2110 const struct pid_entry
*p
= ptr
;
2111 struct inode
*inode
;
2112 struct proc_inode
*ei
;
2114 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2119 inode
->i_mode
= p
->mode
;
2120 if (S_ISDIR(inode
->i_mode
))
2121 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2123 inode
->i_op
= p
->iop
;
2125 inode
->i_fop
= p
->fop
;
2127 d_set_d_op(dentry
, &pid_dentry_operations
);
2128 d_add(dentry
, inode
);
2129 /* Close the race of the process dying before we return the dentry */
2130 if (pid_revalidate(dentry
, 0))
2136 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2137 struct dentry
*dentry
,
2138 const struct pid_entry
*ents
,
2142 struct task_struct
*task
= get_proc_task(dir
);
2143 const struct pid_entry
*p
, *last
;
2151 * Yes, it does not scale. And it should not. Don't add
2152 * new entries into /proc/<tgid>/ without very good reasons.
2154 last
= &ents
[nents
- 1];
2155 for (p
= ents
; p
<= last
; p
++) {
2156 if (p
->len
!= dentry
->d_name
.len
)
2158 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2164 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2166 put_task_struct(task
);
2168 return ERR_PTR(error
);
2171 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2172 const struct pid_entry
*ents
, unsigned int nents
)
2174 struct task_struct
*task
= get_proc_task(file_inode(file
));
2175 const struct pid_entry
*p
;
2180 if (!dir_emit_dots(file
, ctx
))
2183 if (ctx
->pos
>= nents
+ 2)
2186 for (p
= ents
+ (ctx
->pos
- 2); p
<= ents
+ nents
- 1; p
++) {
2187 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2188 proc_pident_instantiate
, task
, p
))
2193 put_task_struct(task
);
2197 #ifdef CONFIG_SECURITY
2198 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2199 size_t count
, loff_t
*ppos
)
2201 struct inode
* inode
= file_inode(file
);
2204 struct task_struct
*task
= get_proc_task(inode
);
2209 length
= security_getprocattr(task
,
2210 (char*)file
->f_path
.dentry
->d_name
.name
,
2212 put_task_struct(task
);
2214 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2219 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2220 size_t count
, loff_t
*ppos
)
2222 struct inode
* inode
= file_inode(file
);
2225 struct task_struct
*task
= get_proc_task(inode
);
2230 if (count
> PAGE_SIZE
)
2233 /* No partial writes. */
2239 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2244 if (copy_from_user(page
, buf
, count
))
2247 /* Guard against adverse ptrace interaction */
2248 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2252 length
= security_setprocattr(task
,
2253 (char*)file
->f_path
.dentry
->d_name
.name
,
2254 (void*)page
, count
);
2255 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2257 free_page((unsigned long) page
);
2259 put_task_struct(task
);
2264 static const struct file_operations proc_pid_attr_operations
= {
2265 .read
= proc_pid_attr_read
,
2266 .write
= proc_pid_attr_write
,
2267 .llseek
= generic_file_llseek
,
2270 static const struct pid_entry attr_dir_stuff
[] = {
2271 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2272 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2273 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2274 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2275 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2276 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2279 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2281 return proc_pident_readdir(file
, ctx
,
2282 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2285 static const struct file_operations proc_attr_dir_operations
= {
2286 .read
= generic_read_dir
,
2287 .iterate
= proc_attr_dir_readdir
,
2288 .llseek
= default_llseek
,
2291 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2292 struct dentry
*dentry
, unsigned int flags
)
2294 return proc_pident_lookup(dir
, dentry
,
2295 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2298 static const struct inode_operations proc_attr_dir_inode_operations
= {
2299 .lookup
= proc_attr_dir_lookup
,
2300 .getattr
= pid_getattr
,
2301 .setattr
= proc_setattr
,
2306 #ifdef CONFIG_ELF_CORE
2307 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2308 size_t count
, loff_t
*ppos
)
2310 struct task_struct
*task
= get_proc_task(file_inode(file
));
2311 struct mm_struct
*mm
;
2312 char buffer
[PROC_NUMBUF
];
2320 mm
= get_task_mm(task
);
2322 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2323 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2324 MMF_DUMP_FILTER_SHIFT
));
2326 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2329 put_task_struct(task
);
2334 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2335 const char __user
*buf
,
2339 struct task_struct
*task
;
2340 struct mm_struct
*mm
;
2341 char buffer
[PROC_NUMBUF
], *end
;
2348 memset(buffer
, 0, sizeof(buffer
));
2349 if (count
> sizeof(buffer
) - 1)
2350 count
= sizeof(buffer
) - 1;
2351 if (copy_from_user(buffer
, buf
, count
))
2355 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2358 if (end
- buffer
== 0)
2362 task
= get_proc_task(file_inode(file
));
2367 mm
= get_task_mm(task
);
2371 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2373 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2375 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2380 put_task_struct(task
);
2385 static const struct file_operations proc_coredump_filter_operations
= {
2386 .read
= proc_coredump_filter_read
,
2387 .write
= proc_coredump_filter_write
,
2388 .llseek
= generic_file_llseek
,
2392 #ifdef CONFIG_TASK_IO_ACCOUNTING
2393 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2395 struct task_io_accounting acct
= task
->ioac
;
2396 unsigned long flags
;
2399 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2403 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2408 if (whole
&& lock_task_sighand(task
, &flags
)) {
2409 struct task_struct
*t
= task
;
2411 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2412 while_each_thread(task
, t
)
2413 task_io_accounting_add(&acct
, &t
->ioac
);
2415 unlock_task_sighand(task
, &flags
);
2417 result
= sprintf(buffer
,
2422 "read_bytes: %llu\n"
2423 "write_bytes: %llu\n"
2424 "cancelled_write_bytes: %llu\n",
2425 (unsigned long long)acct
.rchar
,
2426 (unsigned long long)acct
.wchar
,
2427 (unsigned long long)acct
.syscr
,
2428 (unsigned long long)acct
.syscw
,
2429 (unsigned long long)acct
.read_bytes
,
2430 (unsigned long long)acct
.write_bytes
,
2431 (unsigned long long)acct
.cancelled_write_bytes
);
2433 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2437 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2439 return do_io_accounting(task
, buffer
, 0);
2442 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2444 return do_io_accounting(task
, buffer
, 1);
2446 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2448 #ifdef CONFIG_USER_NS
2449 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2450 const struct seq_operations
*seq_ops
)
2452 struct user_namespace
*ns
= NULL
;
2453 struct task_struct
*task
;
2454 struct seq_file
*seq
;
2457 task
= get_proc_task(inode
);
2460 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2462 put_task_struct(task
);
2467 ret
= seq_open(file
, seq_ops
);
2471 seq
= file
->private_data
;
2481 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2483 struct seq_file
*seq
= file
->private_data
;
2484 struct user_namespace
*ns
= seq
->private;
2486 return seq_release(inode
, file
);
2489 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2491 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2494 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2496 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2499 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2501 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2504 static const struct file_operations proc_uid_map_operations
= {
2505 .open
= proc_uid_map_open
,
2506 .write
= proc_uid_map_write
,
2508 .llseek
= seq_lseek
,
2509 .release
= proc_id_map_release
,
2512 static const struct file_operations proc_gid_map_operations
= {
2513 .open
= proc_gid_map_open
,
2514 .write
= proc_gid_map_write
,
2516 .llseek
= seq_lseek
,
2517 .release
= proc_id_map_release
,
2520 static const struct file_operations proc_projid_map_operations
= {
2521 .open
= proc_projid_map_open
,
2522 .write
= proc_projid_map_write
,
2524 .llseek
= seq_lseek
,
2525 .release
= proc_id_map_release
,
2527 #endif /* CONFIG_USER_NS */
2529 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2530 struct pid
*pid
, struct task_struct
*task
)
2532 int err
= lock_trace(task
);
2534 seq_printf(m
, "%08x\n", task
->personality
);
2543 static const struct file_operations proc_task_operations
;
2544 static const struct inode_operations proc_task_inode_operations
;
2546 static const struct pid_entry tgid_base_stuff
[] = {
2547 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2548 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2549 #ifdef CONFIG_CHECKPOINT_RESTORE
2550 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2552 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2553 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2555 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2557 REG("environ", S_IRUSR
, proc_environ_operations
),
2558 ONE("auxv", S_IRUSR
, proc_pid_auxv
),
2559 ONE("status", S_IRUGO
, proc_pid_status
),
2560 ONE("personality", S_IRUSR
, proc_pid_personality
),
2561 INF("limits", S_IRUGO
, proc_pid_limits
),
2562 #ifdef CONFIG_SCHED_DEBUG
2563 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2565 #ifdef CONFIG_SCHED_AUTOGROUP
2566 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2568 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2569 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2570 INF("syscall", S_IRUSR
, proc_pid_syscall
),
2572 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2573 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2574 ONE("statm", S_IRUGO
, proc_pid_statm
),
2575 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2577 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2579 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2580 LNK("cwd", proc_cwd_link
),
2581 LNK("root", proc_root_link
),
2582 LNK("exe", proc_exe_link
),
2583 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2584 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2585 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2586 #ifdef CONFIG_PROC_PAGE_MONITOR
2587 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2588 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2589 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2591 #ifdef CONFIG_SECURITY
2592 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2594 #ifdef CONFIG_KALLSYMS
2595 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2597 #ifdef CONFIG_STACKTRACE
2598 ONE("stack", S_IRUSR
, proc_pid_stack
),
2600 #ifdef CONFIG_SCHEDSTATS
2601 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2603 #ifdef CONFIG_LATENCYTOP
2604 REG("latency", S_IRUGO
, proc_lstats_operations
),
2606 #ifdef CONFIG_PROC_PID_CPUSET
2607 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2609 #ifdef CONFIG_CGROUPS
2610 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2612 INF("oom_score", S_IRUGO
, proc_oom_score
),
2613 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2614 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2615 #ifdef CONFIG_AUDITSYSCALL
2616 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2617 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2619 #ifdef CONFIG_FAULT_INJECTION
2620 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2622 #ifdef CONFIG_ELF_CORE
2623 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2625 #ifdef CONFIG_TASK_IO_ACCOUNTING
2626 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2628 #ifdef CONFIG_HARDWALL
2629 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2631 #ifdef CONFIG_USER_NS
2632 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2633 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2634 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2636 #ifdef CONFIG_CHECKPOINT_RESTORE
2637 REG("timers", S_IRUGO
, proc_timers_operations
),
2641 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2643 return proc_pident_readdir(file
, ctx
,
2644 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2647 static const struct file_operations proc_tgid_base_operations
= {
2648 .read
= generic_read_dir
,
2649 .iterate
= proc_tgid_base_readdir
,
2650 .llseek
= default_llseek
,
2653 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2655 return proc_pident_lookup(dir
, dentry
,
2656 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2659 static const struct inode_operations proc_tgid_base_inode_operations
= {
2660 .lookup
= proc_tgid_base_lookup
,
2661 .getattr
= pid_getattr
,
2662 .setattr
= proc_setattr
,
2663 .permission
= proc_pid_permission
,
2666 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2668 struct dentry
*dentry
, *leader
, *dir
;
2669 char buf
[PROC_NUMBUF
];
2673 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2674 /* no ->d_hash() rejects on procfs */
2675 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2677 shrink_dcache_parent(dentry
);
2683 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2684 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2689 name
.len
= strlen(name
.name
);
2690 dir
= d_hash_and_lookup(leader
, &name
);
2692 goto out_put_leader
;
2695 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2696 dentry
= d_hash_and_lookup(dir
, &name
);
2698 shrink_dcache_parent(dentry
);
2711 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2712 * @task: task that should be flushed.
2714 * When flushing dentries from proc, one needs to flush them from global
2715 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2716 * in. This call is supposed to do all of this job.
2718 * Looks in the dcache for
2720 * /proc/@tgid/task/@pid
2721 * if either directory is present flushes it and all of it'ts children
2724 * It is safe and reasonable to cache /proc entries for a task until
2725 * that task exits. After that they just clog up the dcache with
2726 * useless entries, possibly causing useful dcache entries to be
2727 * flushed instead. This routine is proved to flush those useless
2728 * dcache entries at process exit time.
2730 * NOTE: This routine is just an optimization so it does not guarantee
2731 * that no dcache entries will exist at process exit time it
2732 * just makes it very unlikely that any will persist.
2735 void proc_flush_task(struct task_struct
*task
)
2738 struct pid
*pid
, *tgid
;
2741 pid
= task_pid(task
);
2742 tgid
= task_tgid(task
);
2744 for (i
= 0; i
<= pid
->level
; i
++) {
2745 upid
= &pid
->numbers
[i
];
2746 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2747 tgid
->numbers
[i
].nr
);
2751 static int proc_pid_instantiate(struct inode
*dir
,
2752 struct dentry
* dentry
,
2753 struct task_struct
*task
, const void *ptr
)
2755 struct inode
*inode
;
2757 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2761 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2762 inode
->i_op
= &proc_tgid_base_inode_operations
;
2763 inode
->i_fop
= &proc_tgid_base_operations
;
2764 inode
->i_flags
|=S_IMMUTABLE
;
2766 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2767 ARRAY_SIZE(tgid_base_stuff
)));
2769 d_set_d_op(dentry
, &pid_dentry_operations
);
2771 d_add(dentry
, inode
);
2772 /* Close the race of the process dying before we return the dentry */
2773 if (pid_revalidate(dentry
, 0))
2779 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2781 int result
= -ENOENT
;
2782 struct task_struct
*task
;
2784 struct pid_namespace
*ns
;
2786 tgid
= name_to_int(&dentry
->d_name
);
2790 ns
= dentry
->d_sb
->s_fs_info
;
2792 task
= find_task_by_pid_ns(tgid
, ns
);
2794 get_task_struct(task
);
2799 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2800 put_task_struct(task
);
2802 return ERR_PTR(result
);
2806 * Find the first task with tgid >= tgid
2811 struct task_struct
*task
;
2813 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2818 put_task_struct(iter
.task
);
2822 pid
= find_ge_pid(iter
.tgid
, ns
);
2824 iter
.tgid
= pid_nr_ns(pid
, ns
);
2825 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2826 /* What we to know is if the pid we have find is the
2827 * pid of a thread_group_leader. Testing for task
2828 * being a thread_group_leader is the obvious thing
2829 * todo but there is a window when it fails, due to
2830 * the pid transfer logic in de_thread.
2832 * So we perform the straight forward test of seeing
2833 * if the pid we have found is the pid of a thread
2834 * group leader, and don't worry if the task we have
2835 * found doesn't happen to be a thread group leader.
2836 * As we don't care in the case of readdir.
2838 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2842 get_task_struct(iter
.task
);
2848 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2850 /* for the /proc/ directory itself, after non-process stuff has been done */
2851 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
2853 struct tgid_iter iter
;
2854 struct pid_namespace
*ns
= file
->f_dentry
->d_sb
->s_fs_info
;
2855 loff_t pos
= ctx
->pos
;
2857 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
2860 if (pos
== TGID_OFFSET
- 1) {
2861 struct inode
*inode
= ns
->proc_self
->d_inode
;
2862 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
2866 iter
.tgid
= pos
- TGID_OFFSET
;
2869 for (iter
= next_tgid(ns
, iter
);
2871 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2872 char name
[PROC_NUMBUF
];
2874 if (!has_pid_permissions(ns
, iter
.task
, 2))
2877 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2878 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
2879 if (!proc_fill_cache(file
, ctx
, name
, len
,
2880 proc_pid_instantiate
, iter
.task
, NULL
)) {
2881 put_task_struct(iter
.task
);
2885 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2892 static const struct pid_entry tid_base_stuff
[] = {
2893 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2894 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2895 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2896 REG("environ", S_IRUSR
, proc_environ_operations
),
2897 ONE("auxv", S_IRUSR
, proc_pid_auxv
),
2898 ONE("status", S_IRUGO
, proc_pid_status
),
2899 ONE("personality", S_IRUSR
, proc_pid_personality
),
2900 INF("limits", S_IRUGO
, proc_pid_limits
),
2901 #ifdef CONFIG_SCHED_DEBUG
2902 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2904 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2905 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2906 INF("syscall", S_IRUSR
, proc_pid_syscall
),
2908 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2909 ONE("stat", S_IRUGO
, proc_tid_stat
),
2910 ONE("statm", S_IRUGO
, proc_pid_statm
),
2911 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
2912 #ifdef CONFIG_CHECKPOINT_RESTORE
2913 REG("children", S_IRUGO
, proc_tid_children_operations
),
2916 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
2918 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2919 LNK("cwd", proc_cwd_link
),
2920 LNK("root", proc_root_link
),
2921 LNK("exe", proc_exe_link
),
2922 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2923 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2924 #ifdef CONFIG_PROC_PAGE_MONITOR
2925 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2926 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
2927 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2929 #ifdef CONFIG_SECURITY
2930 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2932 #ifdef CONFIG_KALLSYMS
2933 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2935 #ifdef CONFIG_STACKTRACE
2936 ONE("stack", S_IRUSR
, proc_pid_stack
),
2938 #ifdef CONFIG_SCHEDSTATS
2939 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2941 #ifdef CONFIG_LATENCYTOP
2942 REG("latency", S_IRUGO
, proc_lstats_operations
),
2944 #ifdef CONFIG_PROC_PID_CPUSET
2945 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2947 #ifdef CONFIG_CGROUPS
2948 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2950 INF("oom_score", S_IRUGO
, proc_oom_score
),
2951 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2952 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2953 #ifdef CONFIG_AUDITSYSCALL
2954 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2955 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2957 #ifdef CONFIG_FAULT_INJECTION
2958 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2960 #ifdef CONFIG_TASK_IO_ACCOUNTING
2961 INF("io", S_IRUSR
, proc_tid_io_accounting
),
2963 #ifdef CONFIG_HARDWALL
2964 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2966 #ifdef CONFIG_USER_NS
2967 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2968 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2969 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2973 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2975 return proc_pident_readdir(file
, ctx
,
2976 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2979 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2981 return proc_pident_lookup(dir
, dentry
,
2982 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2985 static const struct file_operations proc_tid_base_operations
= {
2986 .read
= generic_read_dir
,
2987 .iterate
= proc_tid_base_readdir
,
2988 .llseek
= default_llseek
,
2991 static const struct inode_operations proc_tid_base_inode_operations
= {
2992 .lookup
= proc_tid_base_lookup
,
2993 .getattr
= pid_getattr
,
2994 .setattr
= proc_setattr
,
2997 static int proc_task_instantiate(struct inode
*dir
,
2998 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3000 struct inode
*inode
;
3001 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3005 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3006 inode
->i_op
= &proc_tid_base_inode_operations
;
3007 inode
->i_fop
= &proc_tid_base_operations
;
3008 inode
->i_flags
|=S_IMMUTABLE
;
3010 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3011 ARRAY_SIZE(tid_base_stuff
)));
3013 d_set_d_op(dentry
, &pid_dentry_operations
);
3015 d_add(dentry
, inode
);
3016 /* Close the race of the process dying before we return the dentry */
3017 if (pid_revalidate(dentry
, 0))
3023 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3025 int result
= -ENOENT
;
3026 struct task_struct
*task
;
3027 struct task_struct
*leader
= get_proc_task(dir
);
3029 struct pid_namespace
*ns
;
3034 tid
= name_to_int(&dentry
->d_name
);
3038 ns
= dentry
->d_sb
->s_fs_info
;
3040 task
= find_task_by_pid_ns(tid
, ns
);
3042 get_task_struct(task
);
3046 if (!same_thread_group(leader
, task
))
3049 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3051 put_task_struct(task
);
3053 put_task_struct(leader
);
3055 return ERR_PTR(result
);
3059 * Find the first tid of a thread group to return to user space.
3061 * Usually this is just the thread group leader, but if the users
3062 * buffer was too small or there was a seek into the middle of the
3063 * directory we have more work todo.
3065 * In the case of a short read we start with find_task_by_pid.
3067 * In the case of a seek we start with the leader and walk nr
3070 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3071 struct pid_namespace
*ns
)
3073 struct task_struct
*pos
, *task
;
3074 unsigned long nr
= f_pos
;
3076 if (nr
!= f_pos
) /* 32bit overflow? */
3080 task
= pid_task(pid
, PIDTYPE_PID
);
3084 /* Attempt to start with the tid of a thread */
3086 pos
= find_task_by_pid_ns(tid
, ns
);
3087 if (pos
&& same_thread_group(pos
, task
))
3091 /* If nr exceeds the number of threads there is nothing todo */
3092 if (nr
>= get_nr_threads(task
))
3095 /* If we haven't found our starting place yet start
3096 * with the leader and walk nr threads forward.
3098 pos
= task
= task
->group_leader
;
3102 } while_each_thread(task
, pos
);
3107 get_task_struct(pos
);
3114 * Find the next thread in the thread list.
3115 * Return NULL if there is an error or no next thread.
3117 * The reference to the input task_struct is released.
3119 static struct task_struct
*next_tid(struct task_struct
*start
)
3121 struct task_struct
*pos
= NULL
;
3123 if (pid_alive(start
)) {
3124 pos
= next_thread(start
);
3125 if (thread_group_leader(pos
))
3128 get_task_struct(pos
);
3131 put_task_struct(start
);
3135 /* for the /proc/TGID/task/ directories */
3136 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3138 struct inode
*inode
= file_inode(file
);
3139 struct task_struct
*task
;
3140 struct pid_namespace
*ns
;
3143 if (proc_inode_is_dead(inode
))
3146 if (!dir_emit_dots(file
, ctx
))
3149 /* f_version caches the tgid value that the last readdir call couldn't
3150 * return. lseek aka telldir automagically resets f_version to 0.
3152 ns
= file
->f_dentry
->d_sb
->s_fs_info
;
3153 tid
= (int)file
->f_version
;
3154 file
->f_version
= 0;
3155 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3157 task
= next_tid(task
), ctx
->pos
++) {
3158 char name
[PROC_NUMBUF
];
3160 tid
= task_pid_nr_ns(task
, ns
);
3161 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3162 if (!proc_fill_cache(file
, ctx
, name
, len
,
3163 proc_task_instantiate
, task
, NULL
)) {
3164 /* returning this tgid failed, save it as the first
3165 * pid for the next readir call */
3166 file
->f_version
= (u64
)tid
;
3167 put_task_struct(task
);
3175 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3177 struct inode
*inode
= dentry
->d_inode
;
3178 struct task_struct
*p
= get_proc_task(inode
);
3179 generic_fillattr(inode
, stat
);
3182 stat
->nlink
+= get_nr_threads(p
);
3189 static const struct inode_operations proc_task_inode_operations
= {
3190 .lookup
= proc_task_lookup
,
3191 .getattr
= proc_task_getattr
,
3192 .setattr
= proc_setattr
,
3193 .permission
= proc_pid_permission
,
3196 static const struct file_operations proc_task_operations
= {
3197 .read
= generic_read_dir
,
3198 .iterate
= proc_task_readdir
,
3199 .llseek
= default_llseek
,