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 ONE(NAME, MODE, show) \
134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
136 { .proc_show = show } )
139 * Count the number of hardlinks for the pid_entry table, excluding the .
142 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
149 for (i
= 0; i
< n
; ++i
) {
150 if (S_ISDIR(entries
[i
].mode
))
157 static int get_task_root(struct task_struct
*task
, struct path
*root
)
159 int result
= -ENOENT
;
163 get_fs_root(task
->fs
, root
);
170 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
172 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
173 int result
= -ENOENT
;
178 get_fs_pwd(task
->fs
, path
);
182 put_task_struct(task
);
187 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
189 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
190 int result
= -ENOENT
;
193 result
= get_task_root(task
, path
);
194 put_task_struct(task
);
199 static int proc_pid_cmdline(struct seq_file
*m
, struct pid_namespace
*ns
,
200 struct pid
*pid
, struct task_struct
*task
)
203 * Rely on struct seq_operations::show() being called once
204 * per internal buffer allocation. See single_open(), traverse().
206 BUG_ON(m
->size
< PAGE_SIZE
);
207 m
->count
+= get_cmdline(task
, m
->buf
, PAGE_SIZE
);
211 static int proc_pid_auxv(struct seq_file
*m
, struct pid_namespace
*ns
,
212 struct pid
*pid
, struct task_struct
*task
)
214 struct mm_struct
*mm
= mm_access(task
, PTRACE_MODE_READ
);
215 if (mm
&& !IS_ERR(mm
)) {
216 unsigned int nwords
= 0;
219 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
220 seq_write(m
, mm
->saved_auxv
, nwords
* sizeof(mm
->saved_auxv
[0]));
228 #ifdef CONFIG_KALLSYMS
230 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
231 * Returns the resolved symbol. If that fails, simply return the address.
233 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
234 struct pid
*pid
, struct task_struct
*task
)
237 char symname
[KSYM_NAME_LEN
];
239 wchan
= get_wchan(task
);
241 if (wchan
&& ptrace_may_access(task
, PTRACE_MODE_READ
) && !lookup_symbol_name(wchan
, symname
))
242 seq_printf(m
, "%s", symname
);
248 #endif /* CONFIG_KALLSYMS */
250 static int lock_trace(struct task_struct
*task
)
252 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
255 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
256 mutex_unlock(&task
->signal
->cred_guard_mutex
);
262 static void unlock_trace(struct task_struct
*task
)
264 mutex_unlock(&task
->signal
->cred_guard_mutex
);
267 #ifdef CONFIG_STACKTRACE
269 #define MAX_STACK_TRACE_DEPTH 64
271 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
272 struct pid
*pid
, struct task_struct
*task
)
274 struct stack_trace trace
;
275 unsigned long *entries
;
279 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
283 trace
.nr_entries
= 0;
284 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
285 trace
.entries
= entries
;
288 err
= lock_trace(task
);
290 save_stack_trace_tsk(task
, &trace
);
292 for (i
= 0; i
< trace
.nr_entries
; i
++) {
293 seq_printf(m
, "[<%pK>] %pS\n",
294 (void *)entries
[i
], (void *)entries
[i
]);
304 #ifdef CONFIG_SCHEDSTATS
306 * Provides /proc/PID/schedstat
308 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
309 struct pid
*pid
, struct task_struct
*task
)
311 seq_printf(m
, "%llu %llu %lu\n",
312 (unsigned long long)task
->se
.sum_exec_runtime
,
313 (unsigned long long)task
->sched_info
.run_delay
,
314 task
->sched_info
.pcount
);
320 #ifdef CONFIG_LATENCYTOP
321 static int lstats_show_proc(struct seq_file
*m
, void *v
)
324 struct inode
*inode
= m
->private;
325 struct task_struct
*task
= get_proc_task(inode
);
329 seq_puts(m
, "Latency Top version : v0.1\n");
330 for (i
= 0; i
< 32; i
++) {
331 struct latency_record
*lr
= &task
->latency_record
[i
];
332 if (lr
->backtrace
[0]) {
334 seq_printf(m
, "%i %li %li",
335 lr
->count
, lr
->time
, lr
->max
);
336 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
337 unsigned long bt
= lr
->backtrace
[q
];
342 seq_printf(m
, " %ps", (void *)bt
);
348 put_task_struct(task
);
352 static int lstats_open(struct inode
*inode
, struct file
*file
)
354 return single_open(file
, lstats_show_proc
, inode
);
357 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
358 size_t count
, loff_t
*offs
)
360 struct task_struct
*task
= get_proc_task(file_inode(file
));
364 clear_all_latency_tracing(task
);
365 put_task_struct(task
);
370 static const struct file_operations proc_lstats_operations
= {
373 .write
= lstats_write
,
375 .release
= single_release
,
380 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
381 struct pid
*pid
, struct task_struct
*task
)
383 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
384 unsigned long points
= 0;
386 read_lock(&tasklist_lock
);
388 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
390 read_unlock(&tasklist_lock
);
391 seq_printf(m
, "%lu\n", points
);
401 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
402 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
403 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
404 [RLIMIT_DATA
] = {"Max data size", "bytes"},
405 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
406 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
407 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
408 [RLIMIT_NPROC
] = {"Max processes", "processes"},
409 [RLIMIT_NOFILE
] = {"Max open files", "files"},
410 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
411 [RLIMIT_AS
] = {"Max address space", "bytes"},
412 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
413 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
414 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
415 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
416 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
417 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
420 /* Display limits for a process */
421 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
422 struct pid
*pid
, struct task_struct
*task
)
427 struct rlimit rlim
[RLIM_NLIMITS
];
429 if (!lock_task_sighand(task
, &flags
))
431 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
432 unlock_task_sighand(task
, &flags
);
435 * print the file header
437 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
438 "Limit", "Soft Limit", "Hard Limit", "Units");
440 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
441 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
442 seq_printf(m
, "%-25s %-20s ",
443 lnames
[i
].name
, "unlimited");
445 seq_printf(m
, "%-25s %-20lu ",
446 lnames
[i
].name
, rlim
[i
].rlim_cur
);
448 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
449 seq_printf(m
, "%-20s ", "unlimited");
451 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
454 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
462 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
463 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
464 struct pid
*pid
, struct task_struct
*task
)
467 unsigned long args
[6], sp
, pc
;
470 res
= lock_trace(task
);
474 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
475 seq_puts(m
, "running\n");
477 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
480 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
482 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
488 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
490 /************************************************************************/
491 /* Here the fs part begins */
492 /************************************************************************/
494 /* permission checks */
495 static int proc_fd_access_allowed(struct inode
*inode
)
497 struct task_struct
*task
;
499 /* Allow access to a task's file descriptors if it is us or we
500 * may use ptrace attach to the process and find out that
503 task
= get_proc_task(inode
);
505 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
506 put_task_struct(task
);
511 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
514 struct inode
*inode
= d_inode(dentry
);
516 if (attr
->ia_valid
& ATTR_MODE
)
519 error
= inode_change_ok(inode
, attr
);
523 setattr_copy(inode
, attr
);
524 mark_inode_dirty(inode
);
529 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
530 * or euid/egid (for hide_pid_min=2)?
532 static bool has_pid_permissions(struct pid_namespace
*pid
,
533 struct task_struct
*task
,
536 if (pid
->hide_pid
< hide_pid_min
)
538 if (in_group_p(pid
->pid_gid
))
540 return ptrace_may_access(task
, PTRACE_MODE_READ
);
544 static int proc_pid_permission(struct inode
*inode
, int mask
)
546 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
547 struct task_struct
*task
;
550 task
= get_proc_task(inode
);
553 has_perms
= has_pid_permissions(pid
, task
, 1);
554 put_task_struct(task
);
557 if (pid
->hide_pid
== 2) {
559 * Let's make getdents(), stat(), and open()
560 * consistent with each other. If a process
561 * may not stat() a file, it shouldn't be seen
569 return generic_permission(inode
, mask
);
574 static const struct inode_operations proc_def_inode_operations
= {
575 .setattr
= proc_setattr
,
578 static int proc_single_show(struct seq_file
*m
, void *v
)
580 struct inode
*inode
= m
->private;
581 struct pid_namespace
*ns
;
583 struct task_struct
*task
;
586 ns
= inode
->i_sb
->s_fs_info
;
587 pid
= proc_pid(inode
);
588 task
= get_pid_task(pid
, PIDTYPE_PID
);
592 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
594 put_task_struct(task
);
598 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
600 return single_open(filp
, proc_single_show
, inode
);
603 static const struct file_operations proc_single_file_operations
= {
604 .open
= proc_single_open
,
607 .release
= single_release
,
611 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
613 struct task_struct
*task
= get_proc_task(inode
);
614 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
617 mm
= mm_access(task
, mode
);
618 put_task_struct(task
);
620 if (!IS_ERR_OR_NULL(mm
)) {
621 /* ensure this mm_struct can't be freed */
622 atomic_inc(&mm
->mm_count
);
623 /* but do not pin its memory */
631 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
633 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
638 file
->private_data
= mm
;
642 static int mem_open(struct inode
*inode
, struct file
*file
)
644 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
646 /* OK to pass negative loff_t, we can catch out-of-range */
647 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
652 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
653 size_t count
, loff_t
*ppos
, int write
)
655 struct mm_struct
*mm
= file
->private_data
;
656 unsigned long addr
= *ppos
;
663 page
= (char *)__get_free_page(GFP_TEMPORARY
);
668 if (!atomic_inc_not_zero(&mm
->mm_users
))
672 int this_len
= min_t(int, count
, PAGE_SIZE
);
674 if (write
&& copy_from_user(page
, buf
, this_len
)) {
679 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
686 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
700 free_page((unsigned long) page
);
704 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
705 size_t count
, loff_t
*ppos
)
707 return mem_rw(file
, buf
, count
, ppos
, 0);
710 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
711 size_t count
, loff_t
*ppos
)
713 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
716 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
720 file
->f_pos
= offset
;
723 file
->f_pos
+= offset
;
728 force_successful_syscall_return();
732 static int mem_release(struct inode
*inode
, struct file
*file
)
734 struct mm_struct
*mm
= file
->private_data
;
740 static const struct file_operations proc_mem_operations
= {
745 .release
= mem_release
,
748 static int environ_open(struct inode
*inode
, struct file
*file
)
750 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
753 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
754 size_t count
, loff_t
*ppos
)
757 unsigned long src
= *ppos
;
759 struct mm_struct
*mm
= file
->private_data
;
764 page
= (char *)__get_free_page(GFP_TEMPORARY
);
769 if (!atomic_inc_not_zero(&mm
->mm_users
))
772 size_t this_len
, max_len
;
775 if (src
>= (mm
->env_end
- mm
->env_start
))
778 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
780 max_len
= min_t(size_t, PAGE_SIZE
, count
);
781 this_len
= min(max_len
, this_len
);
783 retval
= access_remote_vm(mm
, (mm
->env_start
+ src
),
791 if (copy_to_user(buf
, page
, retval
)) {
805 free_page((unsigned long) page
);
809 static const struct file_operations proc_environ_operations
= {
810 .open
= environ_open
,
811 .read
= environ_read
,
812 .llseek
= generic_file_llseek
,
813 .release
= mem_release
,
816 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
819 struct task_struct
*task
= get_proc_task(file_inode(file
));
820 char buffer
[PROC_NUMBUF
];
821 int oom_adj
= OOM_ADJUST_MIN
;
827 if (lock_task_sighand(task
, &flags
)) {
828 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
829 oom_adj
= OOM_ADJUST_MAX
;
831 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
833 unlock_task_sighand(task
, &flags
);
835 put_task_struct(task
);
836 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
837 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
840 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
841 size_t count
, loff_t
*ppos
)
843 struct task_struct
*task
;
844 char buffer
[PROC_NUMBUF
];
849 memset(buffer
, 0, sizeof(buffer
));
850 if (count
> sizeof(buffer
) - 1)
851 count
= sizeof(buffer
) - 1;
852 if (copy_from_user(buffer
, buf
, count
)) {
857 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
860 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
861 oom_adj
!= OOM_DISABLE
) {
866 task
= get_proc_task(file_inode(file
));
878 if (!lock_task_sighand(task
, &flags
)) {
884 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
885 * value is always attainable.
887 if (oom_adj
== OOM_ADJUST_MAX
)
888 oom_adj
= OOM_SCORE_ADJ_MAX
;
890 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
892 if (oom_adj
< task
->signal
->oom_score_adj
&&
893 !capable(CAP_SYS_RESOURCE
)) {
899 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
900 * /proc/pid/oom_score_adj instead.
902 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
903 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
906 task
->signal
->oom_score_adj
= oom_adj
;
907 trace_oom_score_adj_update(task
);
909 unlock_task_sighand(task
, &flags
);
912 put_task_struct(task
);
914 return err
< 0 ? err
: count
;
917 static const struct file_operations proc_oom_adj_operations
= {
918 .read
= oom_adj_read
,
919 .write
= oom_adj_write
,
920 .llseek
= generic_file_llseek
,
923 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
924 size_t count
, loff_t
*ppos
)
926 struct task_struct
*task
= get_proc_task(file_inode(file
));
927 char buffer
[PROC_NUMBUF
];
928 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
934 if (lock_task_sighand(task
, &flags
)) {
935 oom_score_adj
= task
->signal
->oom_score_adj
;
936 unlock_task_sighand(task
, &flags
);
938 put_task_struct(task
);
939 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
940 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
943 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
944 size_t count
, loff_t
*ppos
)
946 struct task_struct
*task
;
947 char buffer
[PROC_NUMBUF
];
952 memset(buffer
, 0, sizeof(buffer
));
953 if (count
> sizeof(buffer
) - 1)
954 count
= sizeof(buffer
) - 1;
955 if (copy_from_user(buffer
, buf
, count
)) {
960 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
963 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
964 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
969 task
= get_proc_task(file_inode(file
));
981 if (!lock_task_sighand(task
, &flags
)) {
986 if ((short)oom_score_adj
< task
->signal
->oom_score_adj_min
&&
987 !capable(CAP_SYS_RESOURCE
)) {
992 task
->signal
->oom_score_adj
= (short)oom_score_adj
;
993 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
994 task
->signal
->oom_score_adj_min
= (short)oom_score_adj
;
995 trace_oom_score_adj_update(task
);
998 unlock_task_sighand(task
, &flags
);
1001 put_task_struct(task
);
1003 return err
< 0 ? err
: count
;
1006 static const struct file_operations proc_oom_score_adj_operations
= {
1007 .read
= oom_score_adj_read
,
1008 .write
= oom_score_adj_write
,
1009 .llseek
= default_llseek
,
1012 #ifdef CONFIG_AUDITSYSCALL
1013 #define TMPBUFLEN 21
1014 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1015 size_t count
, loff_t
*ppos
)
1017 struct inode
* inode
= file_inode(file
);
1018 struct task_struct
*task
= get_proc_task(inode
);
1020 char tmpbuf
[TMPBUFLEN
];
1024 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1025 from_kuid(file
->f_cred
->user_ns
,
1026 audit_get_loginuid(task
)));
1027 put_task_struct(task
);
1028 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1031 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1032 size_t count
, loff_t
*ppos
)
1034 struct inode
* inode
= file_inode(file
);
1041 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1047 if (count
>= PAGE_SIZE
)
1048 count
= PAGE_SIZE
- 1;
1051 /* No partial writes. */
1054 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1058 if (copy_from_user(page
, buf
, count
))
1062 loginuid
= simple_strtoul(page
, &tmp
, 10);
1069 /* is userspace tring to explicitly UNSET the loginuid? */
1070 if (loginuid
== AUDIT_UID_UNSET
) {
1071 kloginuid
= INVALID_UID
;
1073 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1074 if (!uid_valid(kloginuid
)) {
1080 length
= audit_set_loginuid(kloginuid
);
1081 if (likely(length
== 0))
1085 free_page((unsigned long) page
);
1089 static const struct file_operations proc_loginuid_operations
= {
1090 .read
= proc_loginuid_read
,
1091 .write
= proc_loginuid_write
,
1092 .llseek
= generic_file_llseek
,
1095 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1096 size_t count
, loff_t
*ppos
)
1098 struct inode
* inode
= file_inode(file
);
1099 struct task_struct
*task
= get_proc_task(inode
);
1101 char tmpbuf
[TMPBUFLEN
];
1105 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1106 audit_get_sessionid(task
));
1107 put_task_struct(task
);
1108 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1111 static const struct file_operations proc_sessionid_operations
= {
1112 .read
= proc_sessionid_read
,
1113 .llseek
= generic_file_llseek
,
1117 #ifdef CONFIG_FAULT_INJECTION
1118 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1119 size_t count
, loff_t
*ppos
)
1121 struct task_struct
*task
= get_proc_task(file_inode(file
));
1122 char buffer
[PROC_NUMBUF
];
1128 make_it_fail
= task
->make_it_fail
;
1129 put_task_struct(task
);
1131 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1133 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1136 static ssize_t
proc_fault_inject_write(struct file
* file
,
1137 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1139 struct task_struct
*task
;
1140 char buffer
[PROC_NUMBUF
], *end
;
1143 if (!capable(CAP_SYS_RESOURCE
))
1145 memset(buffer
, 0, sizeof(buffer
));
1146 if (count
> sizeof(buffer
) - 1)
1147 count
= sizeof(buffer
) - 1;
1148 if (copy_from_user(buffer
, buf
, count
))
1150 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1153 if (make_it_fail
< 0 || make_it_fail
> 1)
1156 task
= get_proc_task(file_inode(file
));
1159 task
->make_it_fail
= make_it_fail
;
1160 put_task_struct(task
);
1165 static const struct file_operations proc_fault_inject_operations
= {
1166 .read
= proc_fault_inject_read
,
1167 .write
= proc_fault_inject_write
,
1168 .llseek
= generic_file_llseek
,
1173 #ifdef CONFIG_SCHED_DEBUG
1175 * Print out various scheduling related per-task fields:
1177 static int sched_show(struct seq_file
*m
, void *v
)
1179 struct inode
*inode
= m
->private;
1180 struct task_struct
*p
;
1182 p
= get_proc_task(inode
);
1185 proc_sched_show_task(p
, m
);
1193 sched_write(struct file
*file
, const char __user
*buf
,
1194 size_t count
, loff_t
*offset
)
1196 struct inode
*inode
= file_inode(file
);
1197 struct task_struct
*p
;
1199 p
= get_proc_task(inode
);
1202 proc_sched_set_task(p
);
1209 static int sched_open(struct inode
*inode
, struct file
*filp
)
1211 return single_open(filp
, sched_show
, inode
);
1214 static const struct file_operations proc_pid_sched_operations
= {
1217 .write
= sched_write
,
1218 .llseek
= seq_lseek
,
1219 .release
= single_release
,
1224 #ifdef CONFIG_SCHED_AUTOGROUP
1226 * Print out autogroup related information:
1228 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1230 struct inode
*inode
= m
->private;
1231 struct task_struct
*p
;
1233 p
= get_proc_task(inode
);
1236 proc_sched_autogroup_show_task(p
, m
);
1244 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1245 size_t count
, loff_t
*offset
)
1247 struct inode
*inode
= file_inode(file
);
1248 struct task_struct
*p
;
1249 char buffer
[PROC_NUMBUF
];
1253 memset(buffer
, 0, sizeof(buffer
));
1254 if (count
> sizeof(buffer
) - 1)
1255 count
= sizeof(buffer
) - 1;
1256 if (copy_from_user(buffer
, buf
, count
))
1259 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1263 p
= get_proc_task(inode
);
1267 err
= proc_sched_autogroup_set_nice(p
, nice
);
1276 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1280 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1282 struct seq_file
*m
= filp
->private_data
;
1289 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1290 .open
= sched_autogroup_open
,
1292 .write
= sched_autogroup_write
,
1293 .llseek
= seq_lseek
,
1294 .release
= single_release
,
1297 #endif /* CONFIG_SCHED_AUTOGROUP */
1299 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1300 size_t count
, loff_t
*offset
)
1302 struct inode
*inode
= file_inode(file
);
1303 struct task_struct
*p
;
1304 char buffer
[TASK_COMM_LEN
];
1305 const size_t maxlen
= sizeof(buffer
) - 1;
1307 memset(buffer
, 0, sizeof(buffer
));
1308 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1311 p
= get_proc_task(inode
);
1315 if (same_thread_group(current
, p
))
1316 set_task_comm(p
, buffer
);
1325 static int comm_show(struct seq_file
*m
, void *v
)
1327 struct inode
*inode
= m
->private;
1328 struct task_struct
*p
;
1330 p
= get_proc_task(inode
);
1335 seq_printf(m
, "%s\n", p
->comm
);
1343 static int comm_open(struct inode
*inode
, struct file
*filp
)
1345 return single_open(filp
, comm_show
, inode
);
1348 static const struct file_operations proc_pid_set_comm_operations
= {
1351 .write
= comm_write
,
1352 .llseek
= seq_lseek
,
1353 .release
= single_release
,
1356 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1358 struct task_struct
*task
;
1359 struct mm_struct
*mm
;
1360 struct file
*exe_file
;
1362 task
= get_proc_task(d_inode(dentry
));
1365 mm
= get_task_mm(task
);
1366 put_task_struct(task
);
1369 exe_file
= get_mm_exe_file(mm
);
1372 *exe_path
= exe_file
->f_path
;
1373 path_get(&exe_file
->f_path
);
1380 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1382 struct inode
*inode
= d_inode(dentry
);
1384 int error
= -EACCES
;
1386 /* Are we allowed to snoop on the tasks file descriptors? */
1387 if (!proc_fd_access_allowed(inode
))
1390 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1394 nd_jump_link(nd
, &path
);
1397 return ERR_PTR(error
);
1400 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1402 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1409 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1410 len
= PTR_ERR(pathname
);
1411 if (IS_ERR(pathname
))
1413 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1417 if (copy_to_user(buffer
, pathname
, len
))
1420 free_page((unsigned long)tmp
);
1424 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1426 int error
= -EACCES
;
1427 struct inode
*inode
= d_inode(dentry
);
1430 /* Are we allowed to snoop on the tasks file descriptors? */
1431 if (!proc_fd_access_allowed(inode
))
1434 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1438 error
= do_proc_readlink(&path
, buffer
, buflen
);
1444 const struct inode_operations proc_pid_link_inode_operations
= {
1445 .readlink
= proc_pid_readlink
,
1446 .follow_link
= proc_pid_follow_link
,
1447 .setattr
= proc_setattr
,
1451 /* building an inode */
1453 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1455 struct inode
* inode
;
1456 struct proc_inode
*ei
;
1457 const struct cred
*cred
;
1459 /* We need a new inode */
1461 inode
= new_inode(sb
);
1467 inode
->i_ino
= get_next_ino();
1468 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1469 inode
->i_op
= &proc_def_inode_operations
;
1472 * grab the reference to task.
1474 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1478 if (task_dumpable(task
)) {
1480 cred
= __task_cred(task
);
1481 inode
->i_uid
= cred
->euid
;
1482 inode
->i_gid
= cred
->egid
;
1485 security_task_to_inode(task
, inode
);
1495 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1497 struct inode
*inode
= d_inode(dentry
);
1498 struct task_struct
*task
;
1499 const struct cred
*cred
;
1500 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1502 generic_fillattr(inode
, stat
);
1505 stat
->uid
= GLOBAL_ROOT_UID
;
1506 stat
->gid
= GLOBAL_ROOT_GID
;
1507 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1509 if (!has_pid_permissions(pid
, task
, 2)) {
1512 * This doesn't prevent learning whether PID exists,
1513 * it only makes getattr() consistent with readdir().
1517 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1518 task_dumpable(task
)) {
1519 cred
= __task_cred(task
);
1520 stat
->uid
= cred
->euid
;
1521 stat
->gid
= cred
->egid
;
1531 * Exceptional case: normally we are not allowed to unhash a busy
1532 * directory. In this case, however, we can do it - no aliasing problems
1533 * due to the way we treat inodes.
1535 * Rewrite the inode's ownerships here because the owning task may have
1536 * performed a setuid(), etc.
1538 * Before the /proc/pid/status file was created the only way to read
1539 * the effective uid of a /process was to stat /proc/pid. Reading
1540 * /proc/pid/status is slow enough that procps and other packages
1541 * kept stating /proc/pid. To keep the rules in /proc simple I have
1542 * made this apply to all per process world readable and executable
1545 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1547 struct inode
*inode
;
1548 struct task_struct
*task
;
1549 const struct cred
*cred
;
1551 if (flags
& LOOKUP_RCU
)
1554 inode
= d_inode(dentry
);
1555 task
= get_proc_task(inode
);
1558 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1559 task_dumpable(task
)) {
1561 cred
= __task_cred(task
);
1562 inode
->i_uid
= cred
->euid
;
1563 inode
->i_gid
= cred
->egid
;
1566 inode
->i_uid
= GLOBAL_ROOT_UID
;
1567 inode
->i_gid
= GLOBAL_ROOT_GID
;
1569 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1570 security_task_to_inode(task
, inode
);
1571 put_task_struct(task
);
1577 static inline bool proc_inode_is_dead(struct inode
*inode
)
1579 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1582 int pid_delete_dentry(const struct dentry
*dentry
)
1584 /* Is the task we represent dead?
1585 * If so, then don't put the dentry on the lru list,
1586 * kill it immediately.
1588 return proc_inode_is_dead(d_inode(dentry
));
1591 const struct dentry_operations pid_dentry_operations
=
1593 .d_revalidate
= pid_revalidate
,
1594 .d_delete
= pid_delete_dentry
,
1600 * Fill a directory entry.
1602 * If possible create the dcache entry and derive our inode number and
1603 * file type from dcache entry.
1605 * Since all of the proc inode numbers are dynamically generated, the inode
1606 * numbers do not exist until the inode is cache. This means creating the
1607 * the dcache entry in readdir is necessary to keep the inode numbers
1608 * reported by readdir in sync with the inode numbers reported
1611 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1612 const char *name
, int len
,
1613 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1615 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1616 struct qstr qname
= QSTR_INIT(name
, len
);
1617 struct inode
*inode
;
1621 child
= d_hash_and_lookup(dir
, &qname
);
1623 child
= d_alloc(dir
, &qname
);
1625 goto end_instantiate
;
1626 if (instantiate(d_inode(dir
), child
, task
, ptr
) < 0) {
1628 goto end_instantiate
;
1631 inode
= d_inode(child
);
1633 type
= inode
->i_mode
>> 12;
1635 return dir_emit(ctx
, name
, len
, ino
, type
);
1638 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1641 #ifdef CONFIG_CHECKPOINT_RESTORE
1644 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1645 * which represent vma start and end addresses.
1647 static int dname_to_vma_addr(struct dentry
*dentry
,
1648 unsigned long *start
, unsigned long *end
)
1650 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1656 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1658 unsigned long vm_start
, vm_end
;
1659 bool exact_vma_exists
= false;
1660 struct mm_struct
*mm
= NULL
;
1661 struct task_struct
*task
;
1662 const struct cred
*cred
;
1663 struct inode
*inode
;
1666 if (flags
& LOOKUP_RCU
)
1669 if (!capable(CAP_SYS_ADMIN
)) {
1674 inode
= d_inode(dentry
);
1675 task
= get_proc_task(inode
);
1679 mm
= mm_access(task
, PTRACE_MODE_READ
);
1680 if (IS_ERR_OR_NULL(mm
))
1683 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1684 down_read(&mm
->mmap_sem
);
1685 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1686 up_read(&mm
->mmap_sem
);
1691 if (exact_vma_exists
) {
1692 if (task_dumpable(task
)) {
1694 cred
= __task_cred(task
);
1695 inode
->i_uid
= cred
->euid
;
1696 inode
->i_gid
= cred
->egid
;
1699 inode
->i_uid
= GLOBAL_ROOT_UID
;
1700 inode
->i_gid
= GLOBAL_ROOT_GID
;
1702 security_task_to_inode(task
, inode
);
1707 put_task_struct(task
);
1713 static const struct dentry_operations tid_map_files_dentry_operations
= {
1714 .d_revalidate
= map_files_d_revalidate
,
1715 .d_delete
= pid_delete_dentry
,
1718 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1720 unsigned long vm_start
, vm_end
;
1721 struct vm_area_struct
*vma
;
1722 struct task_struct
*task
;
1723 struct mm_struct
*mm
;
1727 task
= get_proc_task(d_inode(dentry
));
1731 mm
= get_task_mm(task
);
1732 put_task_struct(task
);
1736 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1741 down_read(&mm
->mmap_sem
);
1742 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1743 if (vma
&& vma
->vm_file
) {
1744 *path
= vma
->vm_file
->f_path
;
1748 up_read(&mm
->mmap_sem
);
1756 struct map_files_info
{
1759 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1763 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
1764 struct task_struct
*task
, const void *ptr
)
1766 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
1767 struct proc_inode
*ei
;
1768 struct inode
*inode
;
1770 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1775 ei
->op
.proc_get_link
= proc_map_files_get_link
;
1777 inode
->i_op
= &proc_pid_link_inode_operations
;
1779 inode
->i_mode
= S_IFLNK
;
1781 if (mode
& FMODE_READ
)
1782 inode
->i_mode
|= S_IRUSR
;
1783 if (mode
& FMODE_WRITE
)
1784 inode
->i_mode
|= S_IWUSR
;
1786 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
1787 d_add(dentry
, inode
);
1792 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
1793 struct dentry
*dentry
, unsigned int flags
)
1795 unsigned long vm_start
, vm_end
;
1796 struct vm_area_struct
*vma
;
1797 struct task_struct
*task
;
1799 struct mm_struct
*mm
;
1802 if (!capable(CAP_SYS_ADMIN
))
1806 task
= get_proc_task(dir
);
1811 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1815 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
1818 mm
= get_task_mm(task
);
1822 down_read(&mm
->mmap_sem
);
1823 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1828 result
= proc_map_files_instantiate(dir
, dentry
, task
,
1829 (void *)(unsigned long)vma
->vm_file
->f_mode
);
1832 up_read(&mm
->mmap_sem
);
1835 put_task_struct(task
);
1837 return ERR_PTR(result
);
1840 static const struct inode_operations proc_map_files_inode_operations
= {
1841 .lookup
= proc_map_files_lookup
,
1842 .permission
= proc_fd_permission
,
1843 .setattr
= proc_setattr
,
1847 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
1849 struct vm_area_struct
*vma
;
1850 struct task_struct
*task
;
1851 struct mm_struct
*mm
;
1852 unsigned long nr_files
, pos
, i
;
1853 struct flex_array
*fa
= NULL
;
1854 struct map_files_info info
;
1855 struct map_files_info
*p
;
1859 if (!capable(CAP_SYS_ADMIN
))
1863 task
= get_proc_task(file_inode(file
));
1868 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1872 if (!dir_emit_dots(file
, ctx
))
1875 mm
= get_task_mm(task
);
1878 down_read(&mm
->mmap_sem
);
1883 * We need two passes here:
1885 * 1) Collect vmas of mapped files with mmap_sem taken
1886 * 2) Release mmap_sem and instantiate entries
1888 * otherwise we get lockdep complained, since filldir()
1889 * routine might require mmap_sem taken in might_fault().
1892 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
1893 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
1898 fa
= flex_array_alloc(sizeof(info
), nr_files
,
1900 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
1904 flex_array_free(fa
);
1905 up_read(&mm
->mmap_sem
);
1909 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
1910 vma
= vma
->vm_next
) {
1913 if (++pos
<= ctx
->pos
)
1916 info
.mode
= vma
->vm_file
->f_mode
;
1917 info
.len
= snprintf(info
.name
,
1918 sizeof(info
.name
), "%lx-%lx",
1919 vma
->vm_start
, vma
->vm_end
);
1920 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
1924 up_read(&mm
->mmap_sem
);
1926 for (i
= 0; i
< nr_files
; i
++) {
1927 p
= flex_array_get(fa
, i
);
1928 if (!proc_fill_cache(file
, ctx
,
1930 proc_map_files_instantiate
,
1932 (void *)(unsigned long)p
->mode
))
1937 flex_array_free(fa
);
1941 put_task_struct(task
);
1946 static const struct file_operations proc_map_files_operations
= {
1947 .read
= generic_read_dir
,
1948 .iterate
= proc_map_files_readdir
,
1949 .llseek
= default_llseek
,
1952 struct timers_private
{
1954 struct task_struct
*task
;
1955 struct sighand_struct
*sighand
;
1956 struct pid_namespace
*ns
;
1957 unsigned long flags
;
1960 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
1962 struct timers_private
*tp
= m
->private;
1964 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
1966 return ERR_PTR(-ESRCH
);
1968 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
1970 return ERR_PTR(-ESRCH
);
1972 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
1975 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
1977 struct timers_private
*tp
= m
->private;
1978 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
1981 static void timers_stop(struct seq_file
*m
, void *v
)
1983 struct timers_private
*tp
= m
->private;
1986 unlock_task_sighand(tp
->task
, &tp
->flags
);
1991 put_task_struct(tp
->task
);
1996 static int show_timer(struct seq_file
*m
, void *v
)
1998 struct k_itimer
*timer
;
1999 struct timers_private
*tp
= m
->private;
2001 static const char * const nstr
[] = {
2002 [SIGEV_SIGNAL
] = "signal",
2003 [SIGEV_NONE
] = "none",
2004 [SIGEV_THREAD
] = "thread",
2007 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2008 notify
= timer
->it_sigev_notify
;
2010 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2011 seq_printf(m
, "signal: %d/%p\n",
2012 timer
->sigq
->info
.si_signo
,
2013 timer
->sigq
->info
.si_value
.sival_ptr
);
2014 seq_printf(m
, "notify: %s/%s.%d\n",
2015 nstr
[notify
& ~SIGEV_THREAD_ID
],
2016 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2017 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2018 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2023 static const struct seq_operations proc_timers_seq_ops
= {
2024 .start
= timers_start
,
2025 .next
= timers_next
,
2026 .stop
= timers_stop
,
2030 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2032 struct timers_private
*tp
;
2034 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2035 sizeof(struct timers_private
));
2039 tp
->pid
= proc_pid(inode
);
2040 tp
->ns
= inode
->i_sb
->s_fs_info
;
2044 static const struct file_operations proc_timers_operations
= {
2045 .open
= proc_timers_open
,
2047 .llseek
= seq_lseek
,
2048 .release
= seq_release_private
,
2050 #endif /* CONFIG_CHECKPOINT_RESTORE */
2052 static int proc_pident_instantiate(struct inode
*dir
,
2053 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2055 const struct pid_entry
*p
= ptr
;
2056 struct inode
*inode
;
2057 struct proc_inode
*ei
;
2059 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2064 inode
->i_mode
= p
->mode
;
2065 if (S_ISDIR(inode
->i_mode
))
2066 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2068 inode
->i_op
= p
->iop
;
2070 inode
->i_fop
= p
->fop
;
2072 d_set_d_op(dentry
, &pid_dentry_operations
);
2073 d_add(dentry
, inode
);
2074 /* Close the race of the process dying before we return the dentry */
2075 if (pid_revalidate(dentry
, 0))
2081 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2082 struct dentry
*dentry
,
2083 const struct pid_entry
*ents
,
2087 struct task_struct
*task
= get_proc_task(dir
);
2088 const struct pid_entry
*p
, *last
;
2096 * Yes, it does not scale. And it should not. Don't add
2097 * new entries into /proc/<tgid>/ without very good reasons.
2099 last
= &ents
[nents
- 1];
2100 for (p
= ents
; p
<= last
; p
++) {
2101 if (p
->len
!= dentry
->d_name
.len
)
2103 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2109 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2111 put_task_struct(task
);
2113 return ERR_PTR(error
);
2116 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2117 const struct pid_entry
*ents
, unsigned int nents
)
2119 struct task_struct
*task
= get_proc_task(file_inode(file
));
2120 const struct pid_entry
*p
;
2125 if (!dir_emit_dots(file
, ctx
))
2128 if (ctx
->pos
>= nents
+ 2)
2131 for (p
= ents
+ (ctx
->pos
- 2); p
<= ents
+ nents
- 1; p
++) {
2132 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2133 proc_pident_instantiate
, task
, p
))
2138 put_task_struct(task
);
2142 #ifdef CONFIG_SECURITY
2143 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2144 size_t count
, loff_t
*ppos
)
2146 struct inode
* inode
= file_inode(file
);
2149 struct task_struct
*task
= get_proc_task(inode
);
2154 length
= security_getprocattr(task
,
2155 (char*)file
->f_path
.dentry
->d_name
.name
,
2157 put_task_struct(task
);
2159 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2164 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2165 size_t count
, loff_t
*ppos
)
2167 struct inode
* inode
= file_inode(file
);
2170 struct task_struct
*task
= get_proc_task(inode
);
2175 if (count
> PAGE_SIZE
)
2178 /* No partial writes. */
2184 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2189 if (copy_from_user(page
, buf
, count
))
2192 /* Guard against adverse ptrace interaction */
2193 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2197 length
= security_setprocattr(task
,
2198 (char*)file
->f_path
.dentry
->d_name
.name
,
2199 (void*)page
, count
);
2200 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2202 free_page((unsigned long) page
);
2204 put_task_struct(task
);
2209 static const struct file_operations proc_pid_attr_operations
= {
2210 .read
= proc_pid_attr_read
,
2211 .write
= proc_pid_attr_write
,
2212 .llseek
= generic_file_llseek
,
2215 static const struct pid_entry attr_dir_stuff
[] = {
2216 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2217 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2218 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2219 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2220 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2221 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2224 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2226 return proc_pident_readdir(file
, ctx
,
2227 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2230 static const struct file_operations proc_attr_dir_operations
= {
2231 .read
= generic_read_dir
,
2232 .iterate
= proc_attr_dir_readdir
,
2233 .llseek
= default_llseek
,
2236 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2237 struct dentry
*dentry
, unsigned int flags
)
2239 return proc_pident_lookup(dir
, dentry
,
2240 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2243 static const struct inode_operations proc_attr_dir_inode_operations
= {
2244 .lookup
= proc_attr_dir_lookup
,
2245 .getattr
= pid_getattr
,
2246 .setattr
= proc_setattr
,
2251 #ifdef CONFIG_ELF_CORE
2252 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2253 size_t count
, loff_t
*ppos
)
2255 struct task_struct
*task
= get_proc_task(file_inode(file
));
2256 struct mm_struct
*mm
;
2257 char buffer
[PROC_NUMBUF
];
2265 mm
= get_task_mm(task
);
2267 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2268 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2269 MMF_DUMP_FILTER_SHIFT
));
2271 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2274 put_task_struct(task
);
2279 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2280 const char __user
*buf
,
2284 struct task_struct
*task
;
2285 struct mm_struct
*mm
;
2286 char buffer
[PROC_NUMBUF
], *end
;
2293 memset(buffer
, 0, sizeof(buffer
));
2294 if (count
> sizeof(buffer
) - 1)
2295 count
= sizeof(buffer
) - 1;
2296 if (copy_from_user(buffer
, buf
, count
))
2300 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2303 if (end
- buffer
== 0)
2307 task
= get_proc_task(file_inode(file
));
2312 mm
= get_task_mm(task
);
2316 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2318 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2320 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2325 put_task_struct(task
);
2330 static const struct file_operations proc_coredump_filter_operations
= {
2331 .read
= proc_coredump_filter_read
,
2332 .write
= proc_coredump_filter_write
,
2333 .llseek
= generic_file_llseek
,
2337 #ifdef CONFIG_TASK_IO_ACCOUNTING
2338 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2340 struct task_io_accounting acct
= task
->ioac
;
2341 unsigned long flags
;
2344 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2348 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2353 if (whole
&& lock_task_sighand(task
, &flags
)) {
2354 struct task_struct
*t
= task
;
2356 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2357 while_each_thread(task
, t
)
2358 task_io_accounting_add(&acct
, &t
->ioac
);
2360 unlock_task_sighand(task
, &flags
);
2367 "read_bytes: %llu\n"
2368 "write_bytes: %llu\n"
2369 "cancelled_write_bytes: %llu\n",
2370 (unsigned long long)acct
.rchar
,
2371 (unsigned long long)acct
.wchar
,
2372 (unsigned long long)acct
.syscr
,
2373 (unsigned long long)acct
.syscw
,
2374 (unsigned long long)acct
.read_bytes
,
2375 (unsigned long long)acct
.write_bytes
,
2376 (unsigned long long)acct
.cancelled_write_bytes
);
2380 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2384 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2385 struct pid
*pid
, struct task_struct
*task
)
2387 return do_io_accounting(task
, m
, 0);
2390 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2391 struct pid
*pid
, struct task_struct
*task
)
2393 return do_io_accounting(task
, m
, 1);
2395 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2397 #ifdef CONFIG_USER_NS
2398 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2399 const struct seq_operations
*seq_ops
)
2401 struct user_namespace
*ns
= NULL
;
2402 struct task_struct
*task
;
2403 struct seq_file
*seq
;
2406 task
= get_proc_task(inode
);
2409 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2411 put_task_struct(task
);
2416 ret
= seq_open(file
, seq_ops
);
2420 seq
= file
->private_data
;
2430 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2432 struct seq_file
*seq
= file
->private_data
;
2433 struct user_namespace
*ns
= seq
->private;
2435 return seq_release(inode
, file
);
2438 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2440 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2443 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2445 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2448 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2450 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2453 static const struct file_operations proc_uid_map_operations
= {
2454 .open
= proc_uid_map_open
,
2455 .write
= proc_uid_map_write
,
2457 .llseek
= seq_lseek
,
2458 .release
= proc_id_map_release
,
2461 static const struct file_operations proc_gid_map_operations
= {
2462 .open
= proc_gid_map_open
,
2463 .write
= proc_gid_map_write
,
2465 .llseek
= seq_lseek
,
2466 .release
= proc_id_map_release
,
2469 static const struct file_operations proc_projid_map_operations
= {
2470 .open
= proc_projid_map_open
,
2471 .write
= proc_projid_map_write
,
2473 .llseek
= seq_lseek
,
2474 .release
= proc_id_map_release
,
2477 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2479 struct user_namespace
*ns
= NULL
;
2480 struct task_struct
*task
;
2484 task
= get_proc_task(inode
);
2487 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2489 put_task_struct(task
);
2494 if (file
->f_mode
& FMODE_WRITE
) {
2496 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2500 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2511 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2513 struct seq_file
*seq
= file
->private_data
;
2514 struct user_namespace
*ns
= seq
->private;
2515 int ret
= single_release(inode
, file
);
2520 static const struct file_operations proc_setgroups_operations
= {
2521 .open
= proc_setgroups_open
,
2522 .write
= proc_setgroups_write
,
2524 .llseek
= seq_lseek
,
2525 .release
= proc_setgroups_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 ONE("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 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2572 ONE("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 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2597 #ifdef CONFIG_STACKTRACE
2598 ONE("stack", S_IRUSR
, proc_pid_stack
),
2600 #ifdef CONFIG_SCHEDSTATS
2601 ONE("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 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
2609 #ifdef CONFIG_CGROUPS
2610 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
2612 ONE("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 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
2628 #ifdef CONFIG_HARDWALL
2629 ONE("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
),
2635 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2637 #ifdef CONFIG_CHECKPOINT_RESTORE
2638 REG("timers", S_IRUGO
, proc_timers_operations
),
2642 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2644 return proc_pident_readdir(file
, ctx
,
2645 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2648 static const struct file_operations proc_tgid_base_operations
= {
2649 .read
= generic_read_dir
,
2650 .iterate
= proc_tgid_base_readdir
,
2651 .llseek
= default_llseek
,
2654 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2656 return proc_pident_lookup(dir
, dentry
,
2657 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2660 static const struct inode_operations proc_tgid_base_inode_operations
= {
2661 .lookup
= proc_tgid_base_lookup
,
2662 .getattr
= pid_getattr
,
2663 .setattr
= proc_setattr
,
2664 .permission
= proc_pid_permission
,
2667 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2669 struct dentry
*dentry
, *leader
, *dir
;
2670 char buf
[PROC_NUMBUF
];
2674 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2675 /* no ->d_hash() rejects on procfs */
2676 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2678 d_invalidate(dentry
);
2686 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2687 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2692 name
.len
= strlen(name
.name
);
2693 dir
= d_hash_and_lookup(leader
, &name
);
2695 goto out_put_leader
;
2698 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2699 dentry
= d_hash_and_lookup(dir
, &name
);
2701 d_invalidate(dentry
);
2713 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2714 * @task: task that should be flushed.
2716 * When flushing dentries from proc, one needs to flush them from global
2717 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2718 * in. This call is supposed to do all of this job.
2720 * Looks in the dcache for
2722 * /proc/@tgid/task/@pid
2723 * if either directory is present flushes it and all of it'ts children
2726 * It is safe and reasonable to cache /proc entries for a task until
2727 * that task exits. After that they just clog up the dcache with
2728 * useless entries, possibly causing useful dcache entries to be
2729 * flushed instead. This routine is proved to flush those useless
2730 * dcache entries at process exit time.
2732 * NOTE: This routine is just an optimization so it does not guarantee
2733 * that no dcache entries will exist at process exit time it
2734 * just makes it very unlikely that any will persist.
2737 void proc_flush_task(struct task_struct
*task
)
2740 struct pid
*pid
, *tgid
;
2743 pid
= task_pid(task
);
2744 tgid
= task_tgid(task
);
2746 for (i
= 0; i
<= pid
->level
; i
++) {
2747 upid
= &pid
->numbers
[i
];
2748 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2749 tgid
->numbers
[i
].nr
);
2753 static int proc_pid_instantiate(struct inode
*dir
,
2754 struct dentry
* dentry
,
2755 struct task_struct
*task
, const void *ptr
)
2757 struct inode
*inode
;
2759 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2763 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2764 inode
->i_op
= &proc_tgid_base_inode_operations
;
2765 inode
->i_fop
= &proc_tgid_base_operations
;
2766 inode
->i_flags
|=S_IMMUTABLE
;
2768 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2769 ARRAY_SIZE(tgid_base_stuff
)));
2771 d_set_d_op(dentry
, &pid_dentry_operations
);
2773 d_add(dentry
, inode
);
2774 /* Close the race of the process dying before we return the dentry */
2775 if (pid_revalidate(dentry
, 0))
2781 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2783 int result
= -ENOENT
;
2784 struct task_struct
*task
;
2786 struct pid_namespace
*ns
;
2788 tgid
= name_to_int(&dentry
->d_name
);
2792 ns
= dentry
->d_sb
->s_fs_info
;
2794 task
= find_task_by_pid_ns(tgid
, ns
);
2796 get_task_struct(task
);
2801 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2802 put_task_struct(task
);
2804 return ERR_PTR(result
);
2808 * Find the first task with tgid >= tgid
2813 struct task_struct
*task
;
2815 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2820 put_task_struct(iter
.task
);
2824 pid
= find_ge_pid(iter
.tgid
, ns
);
2826 iter
.tgid
= pid_nr_ns(pid
, ns
);
2827 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2828 /* What we to know is if the pid we have find is the
2829 * pid of a thread_group_leader. Testing for task
2830 * being a thread_group_leader is the obvious thing
2831 * todo but there is a window when it fails, due to
2832 * the pid transfer logic in de_thread.
2834 * So we perform the straight forward test of seeing
2835 * if the pid we have found is the pid of a thread
2836 * group leader, and don't worry if the task we have
2837 * found doesn't happen to be a thread group leader.
2838 * As we don't care in the case of readdir.
2840 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2844 get_task_struct(iter
.task
);
2850 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
2852 /* for the /proc/ directory itself, after non-process stuff has been done */
2853 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
2855 struct tgid_iter iter
;
2856 struct pid_namespace
*ns
= file_inode(file
)->i_sb
->s_fs_info
;
2857 loff_t pos
= ctx
->pos
;
2859 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
2862 if (pos
== TGID_OFFSET
- 2) {
2863 struct inode
*inode
= d_inode(ns
->proc_self
);
2864 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
2866 ctx
->pos
= pos
= pos
+ 1;
2868 if (pos
== TGID_OFFSET
- 1) {
2869 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
2870 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
2872 ctx
->pos
= pos
= pos
+ 1;
2874 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
),
2904 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2906 REG("environ", S_IRUSR
, proc_environ_operations
),
2907 ONE("auxv", S_IRUSR
, proc_pid_auxv
),
2908 ONE("status", S_IRUGO
, proc_pid_status
),
2909 ONE("personality", S_IRUSR
, proc_pid_personality
),
2910 ONE("limits", S_IRUGO
, proc_pid_limits
),
2911 #ifdef CONFIG_SCHED_DEBUG
2912 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2914 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2915 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2916 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2918 ONE("cmdline", S_IRUGO
, proc_pid_cmdline
),
2919 ONE("stat", S_IRUGO
, proc_tid_stat
),
2920 ONE("statm", S_IRUGO
, proc_pid_statm
),
2921 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
2922 #ifdef CONFIG_CHECKPOINT_RESTORE
2923 REG("children", S_IRUGO
, proc_tid_children_operations
),
2926 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
2928 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2929 LNK("cwd", proc_cwd_link
),
2930 LNK("root", proc_root_link
),
2931 LNK("exe", proc_exe_link
),
2932 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2933 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2934 #ifdef CONFIG_PROC_PAGE_MONITOR
2935 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2936 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
2937 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2939 #ifdef CONFIG_SECURITY
2940 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2942 #ifdef CONFIG_KALLSYMS
2943 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2945 #ifdef CONFIG_STACKTRACE
2946 ONE("stack", S_IRUSR
, proc_pid_stack
),
2948 #ifdef CONFIG_SCHEDSTATS
2949 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2951 #ifdef CONFIG_LATENCYTOP
2952 REG("latency", S_IRUGO
, proc_lstats_operations
),
2954 #ifdef CONFIG_PROC_PID_CPUSET
2955 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
2957 #ifdef CONFIG_CGROUPS
2958 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
2960 ONE("oom_score", S_IRUGO
, proc_oom_score
),
2961 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2962 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2963 #ifdef CONFIG_AUDITSYSCALL
2964 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2965 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2967 #ifdef CONFIG_FAULT_INJECTION
2968 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2970 #ifdef CONFIG_TASK_IO_ACCOUNTING
2971 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
2973 #ifdef CONFIG_HARDWALL
2974 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
2976 #ifdef CONFIG_USER_NS
2977 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2978 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2979 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2980 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
2984 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
2986 return proc_pident_readdir(file
, ctx
,
2987 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2990 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2992 return proc_pident_lookup(dir
, dentry
,
2993 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2996 static const struct file_operations proc_tid_base_operations
= {
2997 .read
= generic_read_dir
,
2998 .iterate
= proc_tid_base_readdir
,
2999 .llseek
= default_llseek
,
3002 static const struct inode_operations proc_tid_base_inode_operations
= {
3003 .lookup
= proc_tid_base_lookup
,
3004 .getattr
= pid_getattr
,
3005 .setattr
= proc_setattr
,
3008 static int proc_task_instantiate(struct inode
*dir
,
3009 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3011 struct inode
*inode
;
3012 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3016 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3017 inode
->i_op
= &proc_tid_base_inode_operations
;
3018 inode
->i_fop
= &proc_tid_base_operations
;
3019 inode
->i_flags
|=S_IMMUTABLE
;
3021 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3022 ARRAY_SIZE(tid_base_stuff
)));
3024 d_set_d_op(dentry
, &pid_dentry_operations
);
3026 d_add(dentry
, inode
);
3027 /* Close the race of the process dying before we return the dentry */
3028 if (pid_revalidate(dentry
, 0))
3034 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3036 int result
= -ENOENT
;
3037 struct task_struct
*task
;
3038 struct task_struct
*leader
= get_proc_task(dir
);
3040 struct pid_namespace
*ns
;
3045 tid
= name_to_int(&dentry
->d_name
);
3049 ns
= dentry
->d_sb
->s_fs_info
;
3051 task
= find_task_by_pid_ns(tid
, ns
);
3053 get_task_struct(task
);
3057 if (!same_thread_group(leader
, task
))
3060 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3062 put_task_struct(task
);
3064 put_task_struct(leader
);
3066 return ERR_PTR(result
);
3070 * Find the first tid of a thread group to return to user space.
3072 * Usually this is just the thread group leader, but if the users
3073 * buffer was too small or there was a seek into the middle of the
3074 * directory we have more work todo.
3076 * In the case of a short read we start with find_task_by_pid.
3078 * In the case of a seek we start with the leader and walk nr
3081 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3082 struct pid_namespace
*ns
)
3084 struct task_struct
*pos
, *task
;
3085 unsigned long nr
= f_pos
;
3087 if (nr
!= f_pos
) /* 32bit overflow? */
3091 task
= pid_task(pid
, PIDTYPE_PID
);
3095 /* Attempt to start with the tid of a thread */
3097 pos
= find_task_by_pid_ns(tid
, ns
);
3098 if (pos
&& same_thread_group(pos
, task
))
3102 /* If nr exceeds the number of threads there is nothing todo */
3103 if (nr
>= get_nr_threads(task
))
3106 /* If we haven't found our starting place yet start
3107 * with the leader and walk nr threads forward.
3109 pos
= task
= task
->group_leader
;
3113 } while_each_thread(task
, pos
);
3118 get_task_struct(pos
);
3125 * Find the next thread in the thread list.
3126 * Return NULL if there is an error or no next thread.
3128 * The reference to the input task_struct is released.
3130 static struct task_struct
*next_tid(struct task_struct
*start
)
3132 struct task_struct
*pos
= NULL
;
3134 if (pid_alive(start
)) {
3135 pos
= next_thread(start
);
3136 if (thread_group_leader(pos
))
3139 get_task_struct(pos
);
3142 put_task_struct(start
);
3146 /* for the /proc/TGID/task/ directories */
3147 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3149 struct inode
*inode
= file_inode(file
);
3150 struct task_struct
*task
;
3151 struct pid_namespace
*ns
;
3154 if (proc_inode_is_dead(inode
))
3157 if (!dir_emit_dots(file
, ctx
))
3160 /* f_version caches the tgid value that the last readdir call couldn't
3161 * return. lseek aka telldir automagically resets f_version to 0.
3163 ns
= inode
->i_sb
->s_fs_info
;
3164 tid
= (int)file
->f_version
;
3165 file
->f_version
= 0;
3166 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3168 task
= next_tid(task
), ctx
->pos
++) {
3169 char name
[PROC_NUMBUF
];
3171 tid
= task_pid_nr_ns(task
, ns
);
3172 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3173 if (!proc_fill_cache(file
, ctx
, name
, len
,
3174 proc_task_instantiate
, task
, NULL
)) {
3175 /* returning this tgid failed, save it as the first
3176 * pid for the next readir call */
3177 file
->f_version
= (u64
)tid
;
3178 put_task_struct(task
);
3186 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3188 struct inode
*inode
= d_inode(dentry
);
3189 struct task_struct
*p
= get_proc_task(inode
);
3190 generic_fillattr(inode
, stat
);
3193 stat
->nlink
+= get_nr_threads(p
);
3200 static const struct inode_operations proc_task_inode_operations
= {
3201 .lookup
= proc_task_lookup
,
3202 .getattr
= proc_task_getattr
,
3203 .setattr
= proc_setattr
,
3204 .permission
= proc_pid_permission
,
3207 static const struct file_operations proc_task_operations
= {
3208 .read
= generic_read_dir
,
3209 .iterate
= proc_task_readdir
,
3210 .llseek
= default_llseek
,