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 seq_file
*m
, struct pid_namespace
*ns
,
204 struct pid
*pid
, struct task_struct
*task
)
207 * Rely on struct seq_operations::show() being called once
208 * per internal buffer allocation. See single_open(), traverse().
210 BUG_ON(m
->size
< PAGE_SIZE
);
211 m
->count
+= get_cmdline(task
, m
->buf
, PAGE_SIZE
);
215 static int proc_pid_auxv(struct seq_file
*m
, struct pid_namespace
*ns
,
216 struct pid
*pid
, struct task_struct
*task
)
218 struct mm_struct
*mm
= mm_access(task
, PTRACE_MODE_READ
);
219 if (mm
&& !IS_ERR(mm
)) {
220 unsigned int nwords
= 0;
223 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
224 seq_write(m
, mm
->saved_auxv
, nwords
* sizeof(mm
->saved_auxv
[0]));
232 #ifdef CONFIG_KALLSYMS
234 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
235 * Returns the resolved symbol. If that fails, simply return the address.
237 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
238 struct pid
*pid
, struct task_struct
*task
)
241 char symname
[KSYM_NAME_LEN
];
243 wchan
= get_wchan(task
);
245 if (lookup_symbol_name(wchan
, symname
) < 0)
246 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
249 return seq_printf(m
, "%lu", wchan
);
251 return seq_printf(m
, "%s", symname
);
253 #endif /* CONFIG_KALLSYMS */
255 static int lock_trace(struct task_struct
*task
)
257 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
260 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
261 mutex_unlock(&task
->signal
->cred_guard_mutex
);
267 static void unlock_trace(struct task_struct
*task
)
269 mutex_unlock(&task
->signal
->cred_guard_mutex
);
272 #ifdef CONFIG_STACKTRACE
274 #define MAX_STACK_TRACE_DEPTH 64
276 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
277 struct pid
*pid
, struct task_struct
*task
)
279 struct stack_trace trace
;
280 unsigned long *entries
;
284 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
288 trace
.nr_entries
= 0;
289 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
290 trace
.entries
= entries
;
293 err
= lock_trace(task
);
295 save_stack_trace_tsk(task
, &trace
);
297 for (i
= 0; i
< trace
.nr_entries
; i
++) {
298 seq_printf(m
, "[<%pK>] %pS\n",
299 (void *)entries
[i
], (void *)entries
[i
]);
309 #ifdef CONFIG_SCHEDSTATS
311 * Provides /proc/PID/schedstat
313 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
314 struct pid
*pid
, struct task_struct
*task
)
316 return seq_printf(m
, "%llu %llu %lu\n",
317 (unsigned long long)task
->se
.sum_exec_runtime
,
318 (unsigned long long)task
->sched_info
.run_delay
,
319 task
->sched_info
.pcount
);
323 #ifdef CONFIG_LATENCYTOP
324 static int lstats_show_proc(struct seq_file
*m
, void *v
)
327 struct inode
*inode
= m
->private;
328 struct task_struct
*task
= get_proc_task(inode
);
332 seq_puts(m
, "Latency Top version : v0.1\n");
333 for (i
= 0; i
< 32; i
++) {
334 struct latency_record
*lr
= &task
->latency_record
[i
];
335 if (lr
->backtrace
[0]) {
337 seq_printf(m
, "%i %li %li",
338 lr
->count
, lr
->time
, lr
->max
);
339 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
340 unsigned long bt
= lr
->backtrace
[q
];
345 seq_printf(m
, " %ps", (void *)bt
);
351 put_task_struct(task
);
355 static int lstats_open(struct inode
*inode
, struct file
*file
)
357 return single_open(file
, lstats_show_proc
, inode
);
360 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
361 size_t count
, loff_t
*offs
)
363 struct task_struct
*task
= get_proc_task(file_inode(file
));
367 clear_all_latency_tracing(task
);
368 put_task_struct(task
);
373 static const struct file_operations proc_lstats_operations
= {
376 .write
= lstats_write
,
378 .release
= single_release
,
383 #ifdef CONFIG_CGROUPS
384 static int cgroup_open(struct inode
*inode
, struct file
*file
)
386 struct pid
*pid
= PROC_I(inode
)->pid
;
387 return single_open(file
, proc_cgroup_show
, pid
);
390 static const struct file_operations proc_cgroup_operations
= {
394 .release
= single_release
,
398 #ifdef CONFIG_PROC_PID_CPUSET
400 static int cpuset_open(struct inode
*inode
, struct file
*file
)
402 struct pid
*pid
= PROC_I(inode
)->pid
;
403 return single_open(file
, proc_cpuset_show
, pid
);
406 static const struct file_operations proc_cpuset_operations
= {
410 .release
= single_release
,
414 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
416 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
417 unsigned long points
= 0;
419 read_lock(&tasklist_lock
);
421 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
423 read_unlock(&tasklist_lock
);
424 return sprintf(buffer
, "%lu\n", points
);
432 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
433 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
434 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
435 [RLIMIT_DATA
] = {"Max data size", "bytes"},
436 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
437 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
438 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
439 [RLIMIT_NPROC
] = {"Max processes", "processes"},
440 [RLIMIT_NOFILE
] = {"Max open files", "files"},
441 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
442 [RLIMIT_AS
] = {"Max address space", "bytes"},
443 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
444 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
445 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
446 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
447 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
448 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
451 /* Display limits for a process */
452 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
453 struct pid
*pid
, struct task_struct
*task
)
458 struct rlimit rlim
[RLIM_NLIMITS
];
460 if (!lock_task_sighand(task
, &flags
))
462 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
463 unlock_task_sighand(task
, &flags
);
466 * print the file header
468 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
469 "Limit", "Soft Limit", "Hard Limit", "Units");
471 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
472 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
473 seq_printf(m
, "%-25s %-20s ",
474 lnames
[i
].name
, "unlimited");
476 seq_printf(m
, "%-25s %-20lu ",
477 lnames
[i
].name
, rlim
[i
].rlim_cur
);
479 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
480 seq_printf(m
, "%-20s ", "unlimited");
482 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
485 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
493 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
494 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
495 struct pid
*pid
, struct task_struct
*task
)
498 unsigned long args
[6], sp
, pc
;
499 int res
= lock_trace(task
);
503 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
504 seq_puts(m
, "running\n");
506 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
509 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
511 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
516 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
518 /************************************************************************/
519 /* Here the fs part begins */
520 /************************************************************************/
522 /* permission checks */
523 static int proc_fd_access_allowed(struct inode
*inode
)
525 struct task_struct
*task
;
527 /* Allow access to a task's file descriptors if it is us or we
528 * may use ptrace attach to the process and find out that
531 task
= get_proc_task(inode
);
533 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
534 put_task_struct(task
);
539 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
542 struct inode
*inode
= dentry
->d_inode
;
544 if (attr
->ia_valid
& ATTR_MODE
)
547 error
= inode_change_ok(inode
, attr
);
551 setattr_copy(inode
, attr
);
552 mark_inode_dirty(inode
);
557 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
558 * or euid/egid (for hide_pid_min=2)?
560 static bool has_pid_permissions(struct pid_namespace
*pid
,
561 struct task_struct
*task
,
564 if (pid
->hide_pid
< hide_pid_min
)
566 if (in_group_p(pid
->pid_gid
))
568 return ptrace_may_access(task
, PTRACE_MODE_READ
);
572 static int proc_pid_permission(struct inode
*inode
, int mask
)
574 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
575 struct task_struct
*task
;
578 task
= get_proc_task(inode
);
581 has_perms
= has_pid_permissions(pid
, task
, 1);
582 put_task_struct(task
);
585 if (pid
->hide_pid
== 2) {
587 * Let's make getdents(), stat(), and open()
588 * consistent with each other. If a process
589 * may not stat() a file, it shouldn't be seen
597 return generic_permission(inode
, mask
);
602 static const struct inode_operations proc_def_inode_operations
= {
603 .setattr
= proc_setattr
,
606 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
608 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
609 size_t count
, loff_t
*ppos
)
611 struct inode
* inode
= file_inode(file
);
614 struct task_struct
*task
= get_proc_task(inode
);
620 if (count
> PROC_BLOCK_SIZE
)
621 count
= PROC_BLOCK_SIZE
;
624 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
627 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
630 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
633 put_task_struct(task
);
638 static const struct file_operations proc_info_file_operations
= {
639 .read
= proc_info_read
,
640 .llseek
= generic_file_llseek
,
643 static int proc_single_show(struct seq_file
*m
, void *v
)
645 struct inode
*inode
= m
->private;
646 struct pid_namespace
*ns
;
648 struct task_struct
*task
;
651 ns
= inode
->i_sb
->s_fs_info
;
652 pid
= proc_pid(inode
);
653 task
= get_pid_task(pid
, PIDTYPE_PID
);
657 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
659 put_task_struct(task
);
663 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
665 return single_open(filp
, proc_single_show
, inode
);
668 static const struct file_operations proc_single_file_operations
= {
669 .open
= proc_single_open
,
672 .release
= single_release
,
675 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
677 struct task_struct
*task
= get_proc_task(file_inode(file
));
678 struct mm_struct
*mm
;
683 mm
= mm_access(task
, mode
);
684 put_task_struct(task
);
690 /* ensure this mm_struct can't be freed */
691 atomic_inc(&mm
->mm_count
);
692 /* but do not pin its memory */
696 file
->private_data
= mm
;
701 static int mem_open(struct inode
*inode
, struct file
*file
)
703 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
705 /* OK to pass negative loff_t, we can catch out-of-range */
706 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
711 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
712 size_t count
, loff_t
*ppos
, int write
)
714 struct mm_struct
*mm
= file
->private_data
;
715 unsigned long addr
= *ppos
;
722 page
= (char *)__get_free_page(GFP_TEMPORARY
);
727 if (!atomic_inc_not_zero(&mm
->mm_users
))
731 int this_len
= min_t(int, count
, PAGE_SIZE
);
733 if (write
&& copy_from_user(page
, buf
, this_len
)) {
738 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
745 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
759 free_page((unsigned long) page
);
763 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
764 size_t count
, loff_t
*ppos
)
766 return mem_rw(file
, buf
, count
, ppos
, 0);
769 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
770 size_t count
, loff_t
*ppos
)
772 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
775 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
779 file
->f_pos
= offset
;
782 file
->f_pos
+= offset
;
787 force_successful_syscall_return();
791 static int mem_release(struct inode
*inode
, struct file
*file
)
793 struct mm_struct
*mm
= file
->private_data
;
799 static const struct file_operations proc_mem_operations
= {
804 .release
= mem_release
,
807 static int environ_open(struct inode
*inode
, struct file
*file
)
809 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
812 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
813 size_t count
, loff_t
*ppos
)
816 unsigned long src
= *ppos
;
818 struct mm_struct
*mm
= file
->private_data
;
823 page
= (char *)__get_free_page(GFP_TEMPORARY
);
828 if (!atomic_inc_not_zero(&mm
->mm_users
))
831 size_t this_len
, max_len
;
834 if (src
>= (mm
->env_end
- mm
->env_start
))
837 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
839 max_len
= min_t(size_t, PAGE_SIZE
, count
);
840 this_len
= min(max_len
, this_len
);
842 retval
= access_remote_vm(mm
, (mm
->env_start
+ src
),
850 if (copy_to_user(buf
, page
, retval
)) {
864 free_page((unsigned long) page
);
868 static const struct file_operations proc_environ_operations
= {
869 .open
= environ_open
,
870 .read
= environ_read
,
871 .llseek
= generic_file_llseek
,
872 .release
= mem_release
,
875 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
878 struct task_struct
*task
= get_proc_task(file_inode(file
));
879 char buffer
[PROC_NUMBUF
];
880 int oom_adj
= OOM_ADJUST_MIN
;
886 if (lock_task_sighand(task
, &flags
)) {
887 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
888 oom_adj
= OOM_ADJUST_MAX
;
890 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
892 unlock_task_sighand(task
, &flags
);
894 put_task_struct(task
);
895 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
896 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
899 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
900 size_t count
, loff_t
*ppos
)
902 struct task_struct
*task
;
903 char buffer
[PROC_NUMBUF
];
908 memset(buffer
, 0, sizeof(buffer
));
909 if (count
> sizeof(buffer
) - 1)
910 count
= sizeof(buffer
) - 1;
911 if (copy_from_user(buffer
, buf
, count
)) {
916 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
919 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
920 oom_adj
!= OOM_DISABLE
) {
925 task
= get_proc_task(file_inode(file
));
937 if (!lock_task_sighand(task
, &flags
)) {
943 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
944 * value is always attainable.
946 if (oom_adj
== OOM_ADJUST_MAX
)
947 oom_adj
= OOM_SCORE_ADJ_MAX
;
949 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
951 if (oom_adj
< task
->signal
->oom_score_adj
&&
952 !capable(CAP_SYS_RESOURCE
)) {
958 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
959 * /proc/pid/oom_score_adj instead.
961 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
962 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
965 task
->signal
->oom_score_adj
= oom_adj
;
966 trace_oom_score_adj_update(task
);
968 unlock_task_sighand(task
, &flags
);
971 put_task_struct(task
);
973 return err
< 0 ? err
: count
;
976 static const struct file_operations proc_oom_adj_operations
= {
977 .read
= oom_adj_read
,
978 .write
= oom_adj_write
,
979 .llseek
= generic_file_llseek
,
982 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
983 size_t count
, loff_t
*ppos
)
985 struct task_struct
*task
= get_proc_task(file_inode(file
));
986 char buffer
[PROC_NUMBUF
];
987 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
993 if (lock_task_sighand(task
, &flags
)) {
994 oom_score_adj
= task
->signal
->oom_score_adj
;
995 unlock_task_sighand(task
, &flags
);
997 put_task_struct(task
);
998 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
999 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1002 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1003 size_t count
, loff_t
*ppos
)
1005 struct task_struct
*task
;
1006 char buffer
[PROC_NUMBUF
];
1007 unsigned long flags
;
1011 memset(buffer
, 0, sizeof(buffer
));
1012 if (count
> sizeof(buffer
) - 1)
1013 count
= sizeof(buffer
) - 1;
1014 if (copy_from_user(buffer
, buf
, count
)) {
1019 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1022 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1023 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1028 task
= get_proc_task(file_inode(file
));
1040 if (!lock_task_sighand(task
, &flags
)) {
1045 if ((short)oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1046 !capable(CAP_SYS_RESOURCE
)) {
1051 task
->signal
->oom_score_adj
= (short)oom_score_adj
;
1052 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1053 task
->signal
->oom_score_adj_min
= (short)oom_score_adj
;
1054 trace_oom_score_adj_update(task
);
1057 unlock_task_sighand(task
, &flags
);
1060 put_task_struct(task
);
1062 return err
< 0 ? err
: count
;
1065 static const struct file_operations proc_oom_score_adj_operations
= {
1066 .read
= oom_score_adj_read
,
1067 .write
= oom_score_adj_write
,
1068 .llseek
= default_llseek
,
1071 #ifdef CONFIG_AUDITSYSCALL
1072 #define TMPBUFLEN 21
1073 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1074 size_t count
, loff_t
*ppos
)
1076 struct inode
* inode
= file_inode(file
);
1077 struct task_struct
*task
= get_proc_task(inode
);
1079 char tmpbuf
[TMPBUFLEN
];
1083 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1084 from_kuid(file
->f_cred
->user_ns
,
1085 audit_get_loginuid(task
)));
1086 put_task_struct(task
);
1087 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1090 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1091 size_t count
, loff_t
*ppos
)
1093 struct inode
* inode
= file_inode(file
);
1100 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1106 if (count
>= PAGE_SIZE
)
1107 count
= PAGE_SIZE
- 1;
1110 /* No partial writes. */
1113 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1117 if (copy_from_user(page
, buf
, count
))
1121 loginuid
= simple_strtoul(page
, &tmp
, 10);
1128 /* is userspace tring to explicitly UNSET the loginuid? */
1129 if (loginuid
== AUDIT_UID_UNSET
) {
1130 kloginuid
= INVALID_UID
;
1132 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1133 if (!uid_valid(kloginuid
)) {
1139 length
= audit_set_loginuid(kloginuid
);
1140 if (likely(length
== 0))
1144 free_page((unsigned long) page
);
1148 static const struct file_operations proc_loginuid_operations
= {
1149 .read
= proc_loginuid_read
,
1150 .write
= proc_loginuid_write
,
1151 .llseek
= generic_file_llseek
,
1154 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1155 size_t count
, loff_t
*ppos
)
1157 struct inode
* inode
= file_inode(file
);
1158 struct task_struct
*task
= get_proc_task(inode
);
1160 char tmpbuf
[TMPBUFLEN
];
1164 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1165 audit_get_sessionid(task
));
1166 put_task_struct(task
);
1167 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1170 static const struct file_operations proc_sessionid_operations
= {
1171 .read
= proc_sessionid_read
,
1172 .llseek
= generic_file_llseek
,
1176 #ifdef CONFIG_FAULT_INJECTION
1177 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1178 size_t count
, loff_t
*ppos
)
1180 struct task_struct
*task
= get_proc_task(file_inode(file
));
1181 char buffer
[PROC_NUMBUF
];
1187 make_it_fail
= task
->make_it_fail
;
1188 put_task_struct(task
);
1190 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1192 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1195 static ssize_t
proc_fault_inject_write(struct file
* file
,
1196 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1198 struct task_struct
*task
;
1199 char buffer
[PROC_NUMBUF
], *end
;
1202 if (!capable(CAP_SYS_RESOURCE
))
1204 memset(buffer
, 0, sizeof(buffer
));
1205 if (count
> sizeof(buffer
) - 1)
1206 count
= sizeof(buffer
) - 1;
1207 if (copy_from_user(buffer
, buf
, count
))
1209 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1212 if (make_it_fail
< 0 || make_it_fail
> 1)
1215 task
= get_proc_task(file_inode(file
));
1218 task
->make_it_fail
= make_it_fail
;
1219 put_task_struct(task
);
1224 static const struct file_operations proc_fault_inject_operations
= {
1225 .read
= proc_fault_inject_read
,
1226 .write
= proc_fault_inject_write
,
1227 .llseek
= generic_file_llseek
,
1232 #ifdef CONFIG_SCHED_DEBUG
1234 * Print out various scheduling related per-task fields:
1236 static int sched_show(struct seq_file
*m
, void *v
)
1238 struct inode
*inode
= m
->private;
1239 struct task_struct
*p
;
1241 p
= get_proc_task(inode
);
1244 proc_sched_show_task(p
, m
);
1252 sched_write(struct file
*file
, const char __user
*buf
,
1253 size_t count
, loff_t
*offset
)
1255 struct inode
*inode
= file_inode(file
);
1256 struct task_struct
*p
;
1258 p
= get_proc_task(inode
);
1261 proc_sched_set_task(p
);
1268 static int sched_open(struct inode
*inode
, struct file
*filp
)
1270 return single_open(filp
, sched_show
, inode
);
1273 static const struct file_operations proc_pid_sched_operations
= {
1276 .write
= sched_write
,
1277 .llseek
= seq_lseek
,
1278 .release
= single_release
,
1283 #ifdef CONFIG_SCHED_AUTOGROUP
1285 * Print out autogroup related information:
1287 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1289 struct inode
*inode
= m
->private;
1290 struct task_struct
*p
;
1292 p
= get_proc_task(inode
);
1295 proc_sched_autogroup_show_task(p
, m
);
1303 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1304 size_t count
, loff_t
*offset
)
1306 struct inode
*inode
= file_inode(file
);
1307 struct task_struct
*p
;
1308 char buffer
[PROC_NUMBUF
];
1312 memset(buffer
, 0, sizeof(buffer
));
1313 if (count
> sizeof(buffer
) - 1)
1314 count
= sizeof(buffer
) - 1;
1315 if (copy_from_user(buffer
, buf
, count
))
1318 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1322 p
= get_proc_task(inode
);
1326 err
= proc_sched_autogroup_set_nice(p
, nice
);
1335 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1339 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1341 struct seq_file
*m
= filp
->private_data
;
1348 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1349 .open
= sched_autogroup_open
,
1351 .write
= sched_autogroup_write
,
1352 .llseek
= seq_lseek
,
1353 .release
= single_release
,
1356 #endif /* CONFIG_SCHED_AUTOGROUP */
1358 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1359 size_t count
, loff_t
*offset
)
1361 struct inode
*inode
= file_inode(file
);
1362 struct task_struct
*p
;
1363 char buffer
[TASK_COMM_LEN
];
1364 const size_t maxlen
= sizeof(buffer
) - 1;
1366 memset(buffer
, 0, sizeof(buffer
));
1367 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1370 p
= get_proc_task(inode
);
1374 if (same_thread_group(current
, p
))
1375 set_task_comm(p
, buffer
);
1384 static int comm_show(struct seq_file
*m
, void *v
)
1386 struct inode
*inode
= m
->private;
1387 struct task_struct
*p
;
1389 p
= get_proc_task(inode
);
1394 seq_printf(m
, "%s\n", p
->comm
);
1402 static int comm_open(struct inode
*inode
, struct file
*filp
)
1404 return single_open(filp
, comm_show
, inode
);
1407 static const struct file_operations proc_pid_set_comm_operations
= {
1410 .write
= comm_write
,
1411 .llseek
= seq_lseek
,
1412 .release
= single_release
,
1415 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1417 struct task_struct
*task
;
1418 struct mm_struct
*mm
;
1419 struct file
*exe_file
;
1421 task
= get_proc_task(dentry
->d_inode
);
1424 mm
= get_task_mm(task
);
1425 put_task_struct(task
);
1428 exe_file
= get_mm_exe_file(mm
);
1431 *exe_path
= exe_file
->f_path
;
1432 path_get(&exe_file
->f_path
);
1439 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1441 struct inode
*inode
= dentry
->d_inode
;
1443 int error
= -EACCES
;
1445 /* Are we allowed to snoop on the tasks file descriptors? */
1446 if (!proc_fd_access_allowed(inode
))
1449 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1453 nd_jump_link(nd
, &path
);
1456 return ERR_PTR(error
);
1459 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1461 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1468 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1469 len
= PTR_ERR(pathname
);
1470 if (IS_ERR(pathname
))
1472 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1476 if (copy_to_user(buffer
, pathname
, len
))
1479 free_page((unsigned long)tmp
);
1483 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1485 int error
= -EACCES
;
1486 struct inode
*inode
= dentry
->d_inode
;
1489 /* Are we allowed to snoop on the tasks file descriptors? */
1490 if (!proc_fd_access_allowed(inode
))
1493 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1497 error
= do_proc_readlink(&path
, buffer
, buflen
);
1503 const struct inode_operations proc_pid_link_inode_operations
= {
1504 .readlink
= proc_pid_readlink
,
1505 .follow_link
= proc_pid_follow_link
,
1506 .setattr
= proc_setattr
,
1510 /* building an inode */
1512 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1514 struct inode
* inode
;
1515 struct proc_inode
*ei
;
1516 const struct cred
*cred
;
1518 /* We need a new inode */
1520 inode
= new_inode(sb
);
1526 inode
->i_ino
= get_next_ino();
1527 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1528 inode
->i_op
= &proc_def_inode_operations
;
1531 * grab the reference to task.
1533 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1537 if (task_dumpable(task
)) {
1539 cred
= __task_cred(task
);
1540 inode
->i_uid
= cred
->euid
;
1541 inode
->i_gid
= cred
->egid
;
1544 security_task_to_inode(task
, inode
);
1554 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1556 struct inode
*inode
= dentry
->d_inode
;
1557 struct task_struct
*task
;
1558 const struct cred
*cred
;
1559 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1561 generic_fillattr(inode
, stat
);
1564 stat
->uid
= GLOBAL_ROOT_UID
;
1565 stat
->gid
= GLOBAL_ROOT_GID
;
1566 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1568 if (!has_pid_permissions(pid
, task
, 2)) {
1571 * This doesn't prevent learning whether PID exists,
1572 * it only makes getattr() consistent with readdir().
1576 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1577 task_dumpable(task
)) {
1578 cred
= __task_cred(task
);
1579 stat
->uid
= cred
->euid
;
1580 stat
->gid
= cred
->egid
;
1590 * Exceptional case: normally we are not allowed to unhash a busy
1591 * directory. In this case, however, we can do it - no aliasing problems
1592 * due to the way we treat inodes.
1594 * Rewrite the inode's ownerships here because the owning task may have
1595 * performed a setuid(), etc.
1597 * Before the /proc/pid/status file was created the only way to read
1598 * the effective uid of a /process was to stat /proc/pid. Reading
1599 * /proc/pid/status is slow enough that procps and other packages
1600 * kept stating /proc/pid. To keep the rules in /proc simple I have
1601 * made this apply to all per process world readable and executable
1604 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1606 struct inode
*inode
;
1607 struct task_struct
*task
;
1608 const struct cred
*cred
;
1610 if (flags
& LOOKUP_RCU
)
1613 inode
= dentry
->d_inode
;
1614 task
= get_proc_task(inode
);
1617 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1618 task_dumpable(task
)) {
1620 cred
= __task_cred(task
);
1621 inode
->i_uid
= cred
->euid
;
1622 inode
->i_gid
= cred
->egid
;
1625 inode
->i_uid
= GLOBAL_ROOT_UID
;
1626 inode
->i_gid
= GLOBAL_ROOT_GID
;
1628 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1629 security_task_to_inode(task
, inode
);
1630 put_task_struct(task
);
1637 static inline bool proc_inode_is_dead(struct inode
*inode
)
1639 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1642 int pid_delete_dentry(const struct dentry
*dentry
)
1644 /* Is the task we represent dead?
1645 * If so, then don't put the dentry on the lru list,
1646 * kill it immediately.
1648 return proc_inode_is_dead(dentry
->d_inode
);
1651 const struct dentry_operations pid_dentry_operations
=
1653 .d_revalidate
= pid_revalidate
,
1654 .d_delete
= pid_delete_dentry
,
1660 * Fill a directory entry.
1662 * If possible create the dcache entry and derive our inode number and
1663 * file type from dcache entry.
1665 * Since all of the proc inode numbers are dynamically generated, the inode
1666 * numbers do not exist until the inode is cache. This means creating the
1667 * the dcache entry in readdir is necessary to keep the inode numbers
1668 * reported by readdir in sync with the inode numbers reported
1671 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1672 const char *name
, int len
,
1673 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1675 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1676 struct qstr qname
= QSTR_INIT(name
, len
);
1677 struct inode
*inode
;
1681 child
= d_hash_and_lookup(dir
, &qname
);
1683 child
= d_alloc(dir
, &qname
);
1685 goto end_instantiate
;
1686 if (instantiate(dir
->d_inode
, child
, task
, ptr
) < 0) {
1688 goto end_instantiate
;
1691 inode
= child
->d_inode
;
1693 type
= inode
->i_mode
>> 12;
1695 return dir_emit(ctx
, name
, len
, ino
, type
);
1698 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1701 #ifdef CONFIG_CHECKPOINT_RESTORE
1704 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1705 * which represent vma start and end addresses.
1707 static int dname_to_vma_addr(struct dentry
*dentry
,
1708 unsigned long *start
, unsigned long *end
)
1710 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1716 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1718 unsigned long vm_start
, vm_end
;
1719 bool exact_vma_exists
= false;
1720 struct mm_struct
*mm
= NULL
;
1721 struct task_struct
*task
;
1722 const struct cred
*cred
;
1723 struct inode
*inode
;
1726 if (flags
& LOOKUP_RCU
)
1729 if (!capable(CAP_SYS_ADMIN
)) {
1734 inode
= dentry
->d_inode
;
1735 task
= get_proc_task(inode
);
1739 mm
= mm_access(task
, PTRACE_MODE_READ
);
1740 if (IS_ERR_OR_NULL(mm
))
1743 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1744 down_read(&mm
->mmap_sem
);
1745 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1746 up_read(&mm
->mmap_sem
);
1751 if (exact_vma_exists
) {
1752 if (task_dumpable(task
)) {
1754 cred
= __task_cred(task
);
1755 inode
->i_uid
= cred
->euid
;
1756 inode
->i_gid
= cred
->egid
;
1759 inode
->i_uid
= GLOBAL_ROOT_UID
;
1760 inode
->i_gid
= GLOBAL_ROOT_GID
;
1762 security_task_to_inode(task
, inode
);
1767 put_task_struct(task
);
1776 static const struct dentry_operations tid_map_files_dentry_operations
= {
1777 .d_revalidate
= map_files_d_revalidate
,
1778 .d_delete
= pid_delete_dentry
,
1781 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1783 unsigned long vm_start
, vm_end
;
1784 struct vm_area_struct
*vma
;
1785 struct task_struct
*task
;
1786 struct mm_struct
*mm
;
1790 task
= get_proc_task(dentry
->d_inode
);
1794 mm
= get_task_mm(task
);
1795 put_task_struct(task
);
1799 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1804 down_read(&mm
->mmap_sem
);
1805 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1806 if (vma
&& vma
->vm_file
) {
1807 *path
= vma
->vm_file
->f_path
;
1811 up_read(&mm
->mmap_sem
);
1819 struct map_files_info
{
1822 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1826 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
1827 struct task_struct
*task
, const void *ptr
)
1829 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
1830 struct proc_inode
*ei
;
1831 struct inode
*inode
;
1833 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1838 ei
->op
.proc_get_link
= proc_map_files_get_link
;
1840 inode
->i_op
= &proc_pid_link_inode_operations
;
1842 inode
->i_mode
= S_IFLNK
;
1844 if (mode
& FMODE_READ
)
1845 inode
->i_mode
|= S_IRUSR
;
1846 if (mode
& FMODE_WRITE
)
1847 inode
->i_mode
|= S_IWUSR
;
1849 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
1850 d_add(dentry
, inode
);
1855 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
1856 struct dentry
*dentry
, unsigned int flags
)
1858 unsigned long vm_start
, vm_end
;
1859 struct vm_area_struct
*vma
;
1860 struct task_struct
*task
;
1862 struct mm_struct
*mm
;
1865 if (!capable(CAP_SYS_ADMIN
))
1869 task
= get_proc_task(dir
);
1874 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1878 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
1881 mm
= get_task_mm(task
);
1885 down_read(&mm
->mmap_sem
);
1886 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1891 result
= proc_map_files_instantiate(dir
, dentry
, task
,
1892 (void *)(unsigned long)vma
->vm_file
->f_mode
);
1895 up_read(&mm
->mmap_sem
);
1898 put_task_struct(task
);
1900 return ERR_PTR(result
);
1903 static const struct inode_operations proc_map_files_inode_operations
= {
1904 .lookup
= proc_map_files_lookup
,
1905 .permission
= proc_fd_permission
,
1906 .setattr
= proc_setattr
,
1910 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
1912 struct vm_area_struct
*vma
;
1913 struct task_struct
*task
;
1914 struct mm_struct
*mm
;
1915 unsigned long nr_files
, pos
, i
;
1916 struct flex_array
*fa
= NULL
;
1917 struct map_files_info info
;
1918 struct map_files_info
*p
;
1922 if (!capable(CAP_SYS_ADMIN
))
1926 task
= get_proc_task(file_inode(file
));
1931 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1935 if (!dir_emit_dots(file
, ctx
))
1938 mm
= get_task_mm(task
);
1941 down_read(&mm
->mmap_sem
);
1946 * We need two passes here:
1948 * 1) Collect vmas of mapped files with mmap_sem taken
1949 * 2) Release mmap_sem and instantiate entries
1951 * otherwise we get lockdep complained, since filldir()
1952 * routine might require mmap_sem taken in might_fault().
1955 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
1956 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
1961 fa
= flex_array_alloc(sizeof(info
), nr_files
,
1963 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
1967 flex_array_free(fa
);
1968 up_read(&mm
->mmap_sem
);
1972 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
1973 vma
= vma
->vm_next
) {
1976 if (++pos
<= ctx
->pos
)
1979 info
.mode
= vma
->vm_file
->f_mode
;
1980 info
.len
= snprintf(info
.name
,
1981 sizeof(info
.name
), "%lx-%lx",
1982 vma
->vm_start
, vma
->vm_end
);
1983 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
1987 up_read(&mm
->mmap_sem
);
1989 for (i
= 0; i
< nr_files
; i
++) {
1990 p
= flex_array_get(fa
, i
);
1991 if (!proc_fill_cache(file
, ctx
,
1993 proc_map_files_instantiate
,
1995 (void *)(unsigned long)p
->mode
))
2000 flex_array_free(fa
);
2004 put_task_struct(task
);
2009 static const struct file_operations proc_map_files_operations
= {
2010 .read
= generic_read_dir
,
2011 .iterate
= proc_map_files_readdir
,
2012 .llseek
= default_llseek
,
2015 struct timers_private
{
2017 struct task_struct
*task
;
2018 struct sighand_struct
*sighand
;
2019 struct pid_namespace
*ns
;
2020 unsigned long flags
;
2023 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2025 struct timers_private
*tp
= m
->private;
2027 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2029 return ERR_PTR(-ESRCH
);
2031 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2033 return ERR_PTR(-ESRCH
);
2035 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2038 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2040 struct timers_private
*tp
= m
->private;
2041 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2044 static void timers_stop(struct seq_file
*m
, void *v
)
2046 struct timers_private
*tp
= m
->private;
2049 unlock_task_sighand(tp
->task
, &tp
->flags
);
2054 put_task_struct(tp
->task
);
2059 static int show_timer(struct seq_file
*m
, void *v
)
2061 struct k_itimer
*timer
;
2062 struct timers_private
*tp
= m
->private;
2064 static const char * const nstr
[] = {
2065 [SIGEV_SIGNAL
] = "signal",
2066 [SIGEV_NONE
] = "none",
2067 [SIGEV_THREAD
] = "thread",
2070 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2071 notify
= timer
->it_sigev_notify
;
2073 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2074 seq_printf(m
, "signal: %d/%p\n", timer
->sigq
->info
.si_signo
,
2075 timer
->sigq
->info
.si_value
.sival_ptr
);
2076 seq_printf(m
, "notify: %s/%s.%d\n",
2077 nstr
[notify
& ~SIGEV_THREAD_ID
],
2078 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2079 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2080 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2085 static const struct seq_operations proc_timers_seq_ops
= {
2086 .start
= timers_start
,
2087 .next
= timers_next
,
2088 .stop
= timers_stop
,
2092 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2094 struct timers_private
*tp
;
2096 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2097 sizeof(struct timers_private
));
2101 tp
->pid
= proc_pid(inode
);
2102 tp
->ns
= inode
->i_sb
->s_fs_info
;
2106 static const struct file_operations proc_timers_operations
= {
2107 .open
= proc_timers_open
,
2109 .llseek
= seq_lseek
,
2110 .release
= seq_release_private
,
2112 #endif /* CONFIG_CHECKPOINT_RESTORE */
2114 static int proc_pident_instantiate(struct inode
*dir
,
2115 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2117 const struct pid_entry
*p
= ptr
;
2118 struct inode
*inode
;
2119 struct proc_inode
*ei
;
2121 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2126 inode
->i_mode
= p
->mode
;
2127 if (S_ISDIR(inode
->i_mode
))
2128 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2130 inode
->i_op
= p
->iop
;
2132 inode
->i_fop
= p
->fop
;
2134 d_set_d_op(dentry
, &pid_dentry_operations
);
2135 d_add(dentry
, inode
);
2136 /* Close the race of the process dying before we return the dentry */
2137 if (pid_revalidate(dentry
, 0))
2143 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2144 struct dentry
*dentry
,
2145 const struct pid_entry
*ents
,
2149 struct task_struct
*task
= get_proc_task(dir
);
2150 const struct pid_entry
*p
, *last
;
2158 * Yes, it does not scale. And it should not. Don't add
2159 * new entries into /proc/<tgid>/ without very good reasons.
2161 last
= &ents
[nents
- 1];
2162 for (p
= ents
; p
<= last
; p
++) {
2163 if (p
->len
!= dentry
->d_name
.len
)
2165 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2171 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2173 put_task_struct(task
);
2175 return ERR_PTR(error
);
2178 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2179 const struct pid_entry
*ents
, unsigned int nents
)
2181 struct task_struct
*task
= get_proc_task(file_inode(file
));
2182 const struct pid_entry
*p
;
2187 if (!dir_emit_dots(file
, ctx
))
2190 if (ctx
->pos
>= nents
+ 2)
2193 for (p
= ents
+ (ctx
->pos
- 2); p
<= ents
+ nents
- 1; p
++) {
2194 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2195 proc_pident_instantiate
, task
, p
))
2200 put_task_struct(task
);
2204 #ifdef CONFIG_SECURITY
2205 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2206 size_t count
, loff_t
*ppos
)
2208 struct inode
* inode
= file_inode(file
);
2211 struct task_struct
*task
= get_proc_task(inode
);
2216 length
= security_getprocattr(task
,
2217 (char*)file
->f_path
.dentry
->d_name
.name
,
2219 put_task_struct(task
);
2221 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2226 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2227 size_t count
, loff_t
*ppos
)
2229 struct inode
* inode
= file_inode(file
);
2232 struct task_struct
*task
= get_proc_task(inode
);
2237 if (count
> PAGE_SIZE
)
2240 /* No partial writes. */
2246 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2251 if (copy_from_user(page
, buf
, count
))
2254 /* Guard against adverse ptrace interaction */
2255 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2259 length
= security_setprocattr(task
,
2260 (char*)file
->f_path
.dentry
->d_name
.name
,
2261 (void*)page
, count
);
2262 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2264 free_page((unsigned long) page
);
2266 put_task_struct(task
);
2271 static const struct file_operations proc_pid_attr_operations
= {
2272 .read
= proc_pid_attr_read
,
2273 .write
= proc_pid_attr_write
,
2274 .llseek
= generic_file_llseek
,
2277 static const struct pid_entry attr_dir_stuff
[] = {
2278 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2279 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2280 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2281 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2282 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2283 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2286 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2288 return proc_pident_readdir(file
, ctx
,
2289 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2292 static const struct file_operations proc_attr_dir_operations
= {
2293 .read
= generic_read_dir
,
2294 .iterate
= proc_attr_dir_readdir
,
2295 .llseek
= default_llseek
,
2298 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2299 struct dentry
*dentry
, unsigned int flags
)
2301 return proc_pident_lookup(dir
, dentry
,
2302 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2305 static const struct inode_operations proc_attr_dir_inode_operations
= {
2306 .lookup
= proc_attr_dir_lookup
,
2307 .getattr
= pid_getattr
,
2308 .setattr
= proc_setattr
,
2313 #ifdef CONFIG_ELF_CORE
2314 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2315 size_t count
, loff_t
*ppos
)
2317 struct task_struct
*task
= get_proc_task(file_inode(file
));
2318 struct mm_struct
*mm
;
2319 char buffer
[PROC_NUMBUF
];
2327 mm
= get_task_mm(task
);
2329 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2330 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2331 MMF_DUMP_FILTER_SHIFT
));
2333 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2336 put_task_struct(task
);
2341 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2342 const char __user
*buf
,
2346 struct task_struct
*task
;
2347 struct mm_struct
*mm
;
2348 char buffer
[PROC_NUMBUF
], *end
;
2355 memset(buffer
, 0, sizeof(buffer
));
2356 if (count
> sizeof(buffer
) - 1)
2357 count
= sizeof(buffer
) - 1;
2358 if (copy_from_user(buffer
, buf
, count
))
2362 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2365 if (end
- buffer
== 0)
2369 task
= get_proc_task(file_inode(file
));
2374 mm
= get_task_mm(task
);
2378 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2380 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2382 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2387 put_task_struct(task
);
2392 static const struct file_operations proc_coredump_filter_operations
= {
2393 .read
= proc_coredump_filter_read
,
2394 .write
= proc_coredump_filter_write
,
2395 .llseek
= generic_file_llseek
,
2399 #ifdef CONFIG_TASK_IO_ACCOUNTING
2400 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2402 struct task_io_accounting acct
= task
->ioac
;
2403 unsigned long flags
;
2406 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2410 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2415 if (whole
&& lock_task_sighand(task
, &flags
)) {
2416 struct task_struct
*t
= task
;
2418 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2419 while_each_thread(task
, t
)
2420 task_io_accounting_add(&acct
, &t
->ioac
);
2422 unlock_task_sighand(task
, &flags
);
2424 result
= sprintf(buffer
,
2429 "read_bytes: %llu\n"
2430 "write_bytes: %llu\n"
2431 "cancelled_write_bytes: %llu\n",
2432 (unsigned long long)acct
.rchar
,
2433 (unsigned long long)acct
.wchar
,
2434 (unsigned long long)acct
.syscr
,
2435 (unsigned long long)acct
.syscw
,
2436 (unsigned long long)acct
.read_bytes
,
2437 (unsigned long long)acct
.write_bytes
,
2438 (unsigned long long)acct
.cancelled_write_bytes
);
2440 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2444 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2446 return do_io_accounting(task
, buffer
, 0);
2449 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2451 return do_io_accounting(task
, buffer
, 1);
2453 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2455 #ifdef CONFIG_USER_NS
2456 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2457 const struct seq_operations
*seq_ops
)
2459 struct user_namespace
*ns
= NULL
;
2460 struct task_struct
*task
;
2461 struct seq_file
*seq
;
2464 task
= get_proc_task(inode
);
2467 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2469 put_task_struct(task
);
2474 ret
= seq_open(file
, seq_ops
);
2478 seq
= file
->private_data
;
2488 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2490 struct seq_file
*seq
= file
->private_data
;
2491 struct user_namespace
*ns
= seq
->private;
2493 return seq_release(inode
, file
);
2496 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2498 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2501 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2503 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2506 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2508 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2511 static const struct file_operations proc_uid_map_operations
= {
2512 .open
= proc_uid_map_open
,
2513 .write
= proc_uid_map_write
,
2515 .llseek
= seq_lseek
,
2516 .release
= proc_id_map_release
,
2519 static const struct file_operations proc_gid_map_operations
= {
2520 .open
= proc_gid_map_open
,
2521 .write
= proc_gid_map_write
,
2523 .llseek
= seq_lseek
,
2524 .release
= proc_id_map_release
,
2527 static const struct file_operations proc_projid_map_operations
= {
2528 .open
= proc_projid_map_open
,
2529 .write
= proc_projid_map_write
,
2531 .llseek
= seq_lseek
,
2532 .release
= proc_id_map_release
,
2534 #endif /* CONFIG_USER_NS */
2536 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2537 struct pid
*pid
, struct task_struct
*task
)
2539 int err
= lock_trace(task
);
2541 seq_printf(m
, "%08x\n", task
->personality
);
2550 static const struct file_operations proc_task_operations
;
2551 static const struct inode_operations proc_task_inode_operations
;
2553 static const struct pid_entry tgid_base_stuff
[] = {
2554 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2555 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2556 #ifdef CONFIG_CHECKPOINT_RESTORE
2557 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2559 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2560 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2562 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2564 REG("environ", S_IRUSR
, proc_environ_operations
),
2565 ONE("auxv", S_IRUSR
, proc_pid_auxv
),
2566 ONE("status", S_IRUGO
, proc_pid_status
),
2567 ONE("personality", S_IRUSR
, proc_pid_personality
),
2568 ONE("limits", S_IRUGO
, proc_pid_limits
),
2569 #ifdef CONFIG_SCHED_DEBUG
2570 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2572 #ifdef CONFIG_SCHED_AUTOGROUP
2573 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2575 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2576 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2577 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2579 ONE("cmdline", S_IRUGO
, proc_pid_cmdline
),
2580 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2581 ONE("statm", S_IRUGO
, proc_pid_statm
),
2582 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2584 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2586 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2587 LNK("cwd", proc_cwd_link
),
2588 LNK("root", proc_root_link
),
2589 LNK("exe", proc_exe_link
),
2590 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2591 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2592 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2593 #ifdef CONFIG_PROC_PAGE_MONITOR
2594 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2595 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2596 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2598 #ifdef CONFIG_SECURITY
2599 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2601 #ifdef CONFIG_KALLSYMS
2602 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2604 #ifdef CONFIG_STACKTRACE
2605 ONE("stack", S_IRUSR
, proc_pid_stack
),
2607 #ifdef CONFIG_SCHEDSTATS
2608 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2610 #ifdef CONFIG_LATENCYTOP
2611 REG("latency", S_IRUGO
, proc_lstats_operations
),
2613 #ifdef CONFIG_PROC_PID_CPUSET
2614 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2616 #ifdef CONFIG_CGROUPS
2617 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2619 INF("oom_score", S_IRUGO
, proc_oom_score
),
2620 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2621 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2622 #ifdef CONFIG_AUDITSYSCALL
2623 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2624 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2626 #ifdef CONFIG_FAULT_INJECTION
2627 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2629 #ifdef CONFIG_ELF_CORE
2630 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2632 #ifdef CONFIG_TASK_IO_ACCOUNTING
2633 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2635 #ifdef CONFIG_HARDWALL
2636 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2638 #ifdef CONFIG_USER_NS
2639 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2640 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2641 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2643 #ifdef CONFIG_CHECKPOINT_RESTORE
2644 REG("timers", S_IRUGO
, proc_timers_operations
),
2648 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2650 return proc_pident_readdir(file
, ctx
,
2651 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2654 static const struct file_operations proc_tgid_base_operations
= {
2655 .read
= generic_read_dir
,
2656 .iterate
= proc_tgid_base_readdir
,
2657 .llseek
= default_llseek
,
2660 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2662 return proc_pident_lookup(dir
, dentry
,
2663 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2666 static const struct inode_operations proc_tgid_base_inode_operations
= {
2667 .lookup
= proc_tgid_base_lookup
,
2668 .getattr
= pid_getattr
,
2669 .setattr
= proc_setattr
,
2670 .permission
= proc_pid_permission
,
2673 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2675 struct dentry
*dentry
, *leader
, *dir
;
2676 char buf
[PROC_NUMBUF
];
2680 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2681 /* no ->d_hash() rejects on procfs */
2682 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2684 shrink_dcache_parent(dentry
);
2690 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2691 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2696 name
.len
= strlen(name
.name
);
2697 dir
= d_hash_and_lookup(leader
, &name
);
2699 goto out_put_leader
;
2702 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2703 dentry
= d_hash_and_lookup(dir
, &name
);
2705 shrink_dcache_parent(dentry
);
2718 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2719 * @task: task that should be flushed.
2721 * When flushing dentries from proc, one needs to flush them from global
2722 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2723 * in. This call is supposed to do all of this job.
2725 * Looks in the dcache for
2727 * /proc/@tgid/task/@pid
2728 * if either directory is present flushes it and all of it'ts children
2731 * It is safe and reasonable to cache /proc entries for a task until
2732 * that task exits. After that they just clog up the dcache with
2733 * useless entries, possibly causing useful dcache entries to be
2734 * flushed instead. This routine is proved to flush those useless
2735 * dcache entries at process exit time.
2737 * NOTE: This routine is just an optimization so it does not guarantee
2738 * that no dcache entries will exist at process exit time it
2739 * just makes it very unlikely that any will persist.
2742 void proc_flush_task(struct task_struct
*task
)
2745 struct pid
*pid
, *tgid
;
2748 pid
= task_pid(task
);
2749 tgid
= task_tgid(task
);
2751 for (i
= 0; i
<= pid
->level
; i
++) {
2752 upid
= &pid
->numbers
[i
];
2753 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2754 tgid
->numbers
[i
].nr
);
2758 static int proc_pid_instantiate(struct inode
*dir
,
2759 struct dentry
* dentry
,
2760 struct task_struct
*task
, const void *ptr
)
2762 struct inode
*inode
;
2764 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2768 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2769 inode
->i_op
= &proc_tgid_base_inode_operations
;
2770 inode
->i_fop
= &proc_tgid_base_operations
;
2771 inode
->i_flags
|=S_IMMUTABLE
;
2773 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2774 ARRAY_SIZE(tgid_base_stuff
)));
2776 d_set_d_op(dentry
, &pid_dentry_operations
);
2778 d_add(dentry
, inode
);
2779 /* Close the race of the process dying before we return the dentry */
2780 if (pid_revalidate(dentry
, 0))
2786 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2788 int result
= -ENOENT
;
2789 struct task_struct
*task
;
2791 struct pid_namespace
*ns
;
2793 tgid
= name_to_int(&dentry
->d_name
);
2797 ns
= dentry
->d_sb
->s_fs_info
;
2799 task
= find_task_by_pid_ns(tgid
, ns
);
2801 get_task_struct(task
);
2806 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2807 put_task_struct(task
);
2809 return ERR_PTR(result
);
2813 * Find the first task with tgid >= tgid
2818 struct task_struct
*task
;
2820 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2825 put_task_struct(iter
.task
);
2829 pid
= find_ge_pid(iter
.tgid
, ns
);
2831 iter
.tgid
= pid_nr_ns(pid
, ns
);
2832 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2833 /* What we to know is if the pid we have find is the
2834 * pid of a thread_group_leader. Testing for task
2835 * being a thread_group_leader is the obvious thing
2836 * todo but there is a window when it fails, due to
2837 * the pid transfer logic in de_thread.
2839 * So we perform the straight forward test of seeing
2840 * if the pid we have found is the pid of a thread
2841 * group leader, and don't worry if the task we have
2842 * found doesn't happen to be a thread group leader.
2843 * As we don't care in the case of readdir.
2845 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2849 get_task_struct(iter
.task
);
2855 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
2857 /* for the /proc/ directory itself, after non-process stuff has been done */
2858 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
2860 struct tgid_iter iter
;
2861 struct pid_namespace
*ns
= file
->f_dentry
->d_sb
->s_fs_info
;
2862 loff_t pos
= ctx
->pos
;
2864 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
2867 if (pos
== TGID_OFFSET
- 1) {
2868 struct inode
*inode
= ns
->proc_self
->d_inode
;
2869 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
2873 iter
.tgid
= pos
- TGID_OFFSET
;
2876 for (iter
= next_tgid(ns
, iter
);
2878 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2879 char name
[PROC_NUMBUF
];
2881 if (!has_pid_permissions(ns
, iter
.task
, 2))
2884 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2885 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
2886 if (!proc_fill_cache(file
, ctx
, name
, len
,
2887 proc_pid_instantiate
, iter
.task
, NULL
)) {
2888 put_task_struct(iter
.task
);
2892 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2899 static const struct pid_entry tid_base_stuff
[] = {
2900 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2901 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2902 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2903 REG("environ", S_IRUSR
, proc_environ_operations
),
2904 ONE("auxv", S_IRUSR
, proc_pid_auxv
),
2905 ONE("status", S_IRUGO
, proc_pid_status
),
2906 ONE("personality", S_IRUSR
, proc_pid_personality
),
2907 ONE("limits", S_IRUGO
, proc_pid_limits
),
2908 #ifdef CONFIG_SCHED_DEBUG
2909 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2911 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2912 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2913 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2915 ONE("cmdline", S_IRUGO
, proc_pid_cmdline
),
2916 ONE("stat", S_IRUGO
, proc_tid_stat
),
2917 ONE("statm", S_IRUGO
, proc_pid_statm
),
2918 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
2919 #ifdef CONFIG_CHECKPOINT_RESTORE
2920 REG("children", S_IRUGO
, proc_tid_children_operations
),
2923 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
2925 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2926 LNK("cwd", proc_cwd_link
),
2927 LNK("root", proc_root_link
),
2928 LNK("exe", proc_exe_link
),
2929 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2930 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2931 #ifdef CONFIG_PROC_PAGE_MONITOR
2932 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2933 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
2934 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2936 #ifdef CONFIG_SECURITY
2937 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2939 #ifdef CONFIG_KALLSYMS
2940 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2942 #ifdef CONFIG_STACKTRACE
2943 ONE("stack", S_IRUSR
, proc_pid_stack
),
2945 #ifdef CONFIG_SCHEDSTATS
2946 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2948 #ifdef CONFIG_LATENCYTOP
2949 REG("latency", S_IRUGO
, proc_lstats_operations
),
2951 #ifdef CONFIG_PROC_PID_CPUSET
2952 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2954 #ifdef CONFIG_CGROUPS
2955 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2957 INF("oom_score", S_IRUGO
, proc_oom_score
),
2958 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2959 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2960 #ifdef CONFIG_AUDITSYSCALL
2961 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2962 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2964 #ifdef CONFIG_FAULT_INJECTION
2965 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2967 #ifdef CONFIG_TASK_IO_ACCOUNTING
2968 INF("io", S_IRUSR
, proc_tid_io_accounting
),
2970 #ifdef CONFIG_HARDWALL
2971 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2973 #ifdef CONFIG_USER_NS
2974 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2975 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2976 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2980 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2982 return proc_pident_readdir(file
, ctx
,
2983 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2986 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2988 return proc_pident_lookup(dir
, dentry
,
2989 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2992 static const struct file_operations proc_tid_base_operations
= {
2993 .read
= generic_read_dir
,
2994 .iterate
= proc_tid_base_readdir
,
2995 .llseek
= default_llseek
,
2998 static const struct inode_operations proc_tid_base_inode_operations
= {
2999 .lookup
= proc_tid_base_lookup
,
3000 .getattr
= pid_getattr
,
3001 .setattr
= proc_setattr
,
3004 static int proc_task_instantiate(struct inode
*dir
,
3005 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3007 struct inode
*inode
;
3008 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3012 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3013 inode
->i_op
= &proc_tid_base_inode_operations
;
3014 inode
->i_fop
= &proc_tid_base_operations
;
3015 inode
->i_flags
|=S_IMMUTABLE
;
3017 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3018 ARRAY_SIZE(tid_base_stuff
)));
3020 d_set_d_op(dentry
, &pid_dentry_operations
);
3022 d_add(dentry
, inode
);
3023 /* Close the race of the process dying before we return the dentry */
3024 if (pid_revalidate(dentry
, 0))
3030 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3032 int result
= -ENOENT
;
3033 struct task_struct
*task
;
3034 struct task_struct
*leader
= get_proc_task(dir
);
3036 struct pid_namespace
*ns
;
3041 tid
= name_to_int(&dentry
->d_name
);
3045 ns
= dentry
->d_sb
->s_fs_info
;
3047 task
= find_task_by_pid_ns(tid
, ns
);
3049 get_task_struct(task
);
3053 if (!same_thread_group(leader
, task
))
3056 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3058 put_task_struct(task
);
3060 put_task_struct(leader
);
3062 return ERR_PTR(result
);
3066 * Find the first tid of a thread group to return to user space.
3068 * Usually this is just the thread group leader, but if the users
3069 * buffer was too small or there was a seek into the middle of the
3070 * directory we have more work todo.
3072 * In the case of a short read we start with find_task_by_pid.
3074 * In the case of a seek we start with the leader and walk nr
3077 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3078 struct pid_namespace
*ns
)
3080 struct task_struct
*pos
, *task
;
3081 unsigned long nr
= f_pos
;
3083 if (nr
!= f_pos
) /* 32bit overflow? */
3087 task
= pid_task(pid
, PIDTYPE_PID
);
3091 /* Attempt to start with the tid of a thread */
3093 pos
= find_task_by_pid_ns(tid
, ns
);
3094 if (pos
&& same_thread_group(pos
, task
))
3098 /* If nr exceeds the number of threads there is nothing todo */
3099 if (nr
>= get_nr_threads(task
))
3102 /* If we haven't found our starting place yet start
3103 * with the leader and walk nr threads forward.
3105 pos
= task
= task
->group_leader
;
3109 } while_each_thread(task
, pos
);
3114 get_task_struct(pos
);
3121 * Find the next thread in the thread list.
3122 * Return NULL if there is an error or no next thread.
3124 * The reference to the input task_struct is released.
3126 static struct task_struct
*next_tid(struct task_struct
*start
)
3128 struct task_struct
*pos
= NULL
;
3130 if (pid_alive(start
)) {
3131 pos
= next_thread(start
);
3132 if (thread_group_leader(pos
))
3135 get_task_struct(pos
);
3138 put_task_struct(start
);
3142 /* for the /proc/TGID/task/ directories */
3143 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3145 struct inode
*inode
= file_inode(file
);
3146 struct task_struct
*task
;
3147 struct pid_namespace
*ns
;
3150 if (proc_inode_is_dead(inode
))
3153 if (!dir_emit_dots(file
, ctx
))
3156 /* f_version caches the tgid value that the last readdir call couldn't
3157 * return. lseek aka telldir automagically resets f_version to 0.
3159 ns
= file
->f_dentry
->d_sb
->s_fs_info
;
3160 tid
= (int)file
->f_version
;
3161 file
->f_version
= 0;
3162 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3164 task
= next_tid(task
), ctx
->pos
++) {
3165 char name
[PROC_NUMBUF
];
3167 tid
= task_pid_nr_ns(task
, ns
);
3168 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3169 if (!proc_fill_cache(file
, ctx
, name
, len
,
3170 proc_task_instantiate
, task
, NULL
)) {
3171 /* returning this tgid failed, save it as the first
3172 * pid for the next readir call */
3173 file
->f_version
= (u64
)tid
;
3174 put_task_struct(task
);
3182 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3184 struct inode
*inode
= dentry
->d_inode
;
3185 struct task_struct
*p
= get_proc_task(inode
);
3186 generic_fillattr(inode
, stat
);
3189 stat
->nlink
+= get_nr_threads(p
);
3196 static const struct inode_operations proc_task_inode_operations
= {
3197 .lookup
= proc_task_lookup
,
3198 .getattr
= proc_task_getattr
,
3199 .setattr
= proc_setattr
,
3200 .permission
= proc_pid_permission
,
3203 static const struct file_operations proc_task_operations
= {
3204 .read
= generic_read_dir
,
3205 .iterate
= proc_task_readdir
,
3206 .llseek
= default_llseek
,