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/cgroup.h>
77 #include <linux/cpuset.h>
78 #include <linux/audit.h>
79 #include <linux/poll.h>
80 #include <linux/nsproxy.h>
81 #include <linux/oom.h>
82 #include <linux/elf.h>
83 #include <linux/pid_namespace.h>
84 #include <linux/user_namespace.h>
85 #include <linux/fs_struct.h>
86 #include <linux/slab.h>
87 #include <linux/flex_array.h>
88 #ifdef CONFIG_HARDWALL
89 #include <asm/hardwall.h>
91 #include <trace/events/oom.h>
96 * Implementing inode permission operations in /proc is almost
97 * certainly an error. Permission checks need to happen during
98 * each system call not at open time. The reason is that most of
99 * what we wish to check for permissions in /proc varies at runtime.
101 * The classic example of a problem is opening file descriptors
102 * in /proc for a task before it execs a suid executable.
109 const struct inode_operations
*iop
;
110 const struct file_operations
*fop
;
114 #define NOD(NAME, MODE, IOP, FOP, OP) { \
116 .len = sizeof(NAME) - 1, \
123 #define DIR(NAME, MODE, iops, fops) \
124 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
125 #define LNK(NAME, get_link) \
126 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
127 &proc_pid_link_inode_operations, NULL, \
128 { .proc_get_link = get_link } )
129 #define REG(NAME, MODE, fops) \
130 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
131 #define INF(NAME, MODE, read) \
132 NOD(NAME, (S_IFREG|(MODE)), \
133 NULL, &proc_info_file_operations, \
134 { .proc_read = read } )
135 #define ONE(NAME, MODE, show) \
136 NOD(NAME, (S_IFREG|(MODE)), \
137 NULL, &proc_single_file_operations, \
138 { .proc_show = show } )
141 * Count the number of hardlinks for the pid_entry table, excluding the .
144 static unsigned int pid_entry_count_dirs(const struct pid_entry
*entries
,
151 for (i
= 0; i
< n
; ++i
) {
152 if (S_ISDIR(entries
[i
].mode
))
159 static int get_task_root(struct task_struct
*task
, struct path
*root
)
161 int result
= -ENOENT
;
165 get_fs_root(task
->fs
, root
);
172 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
174 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
175 int result
= -ENOENT
;
180 get_fs_pwd(task
->fs
, path
);
184 put_task_struct(task
);
189 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
191 struct task_struct
*task
= get_proc_task(dentry
->d_inode
);
192 int result
= -ENOENT
;
195 result
= get_task_root(task
, path
);
196 put_task_struct(task
);
201 static int proc_pid_cmdline(struct task_struct
*task
, char * buffer
)
205 struct mm_struct
*mm
= get_task_mm(task
);
209 goto out_mm
; /* Shh! No looking before we're done */
211 len
= mm
->arg_end
- mm
->arg_start
;
216 res
= access_process_vm(task
, mm
->arg_start
, buffer
, len
, 0);
218 // If the nul at the end of args has been overwritten, then
219 // assume application is using setproctitle(3).
220 if (res
> 0 && buffer
[res
-1] != '\0' && len
< PAGE_SIZE
) {
221 len
= strnlen(buffer
, res
);
225 len
= mm
->env_end
- mm
->env_start
;
226 if (len
> PAGE_SIZE
- res
)
227 len
= PAGE_SIZE
- res
;
228 res
+= access_process_vm(task
, mm
->env_start
, buffer
+res
, len
, 0);
229 res
= strnlen(buffer
, res
);
238 static int proc_pid_auxv(struct task_struct
*task
, char *buffer
)
240 struct mm_struct
*mm
= mm_access(task
, PTRACE_MODE_READ
);
241 int res
= PTR_ERR(mm
);
242 if (mm
&& !IS_ERR(mm
)) {
243 unsigned int nwords
= 0;
246 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
247 res
= nwords
* sizeof(mm
->saved_auxv
[0]);
250 memcpy(buffer
, mm
->saved_auxv
, res
);
257 #ifdef CONFIG_KALLSYMS
259 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
260 * Returns the resolved symbol. If that fails, simply return the address.
262 static int proc_pid_wchan(struct task_struct
*task
, char *buffer
)
265 char symname
[KSYM_NAME_LEN
];
267 wchan
= get_wchan(task
);
269 if (lookup_symbol_name(wchan
, symname
) < 0)
270 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
273 return sprintf(buffer
, "%lu", wchan
);
275 return sprintf(buffer
, "%s", symname
);
277 #endif /* CONFIG_KALLSYMS */
279 static int lock_trace(struct task_struct
*task
)
281 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
284 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH
)) {
285 mutex_unlock(&task
->signal
->cred_guard_mutex
);
291 static void unlock_trace(struct task_struct
*task
)
293 mutex_unlock(&task
->signal
->cred_guard_mutex
);
296 #ifdef CONFIG_STACKTRACE
298 #define MAX_STACK_TRACE_DEPTH 64
300 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
301 struct pid
*pid
, struct task_struct
*task
)
303 struct stack_trace trace
;
304 unsigned long *entries
;
308 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
312 trace
.nr_entries
= 0;
313 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
314 trace
.entries
= entries
;
317 err
= lock_trace(task
);
319 save_stack_trace_tsk(task
, &trace
);
321 for (i
= 0; i
< trace
.nr_entries
; i
++) {
322 seq_printf(m
, "[<%pK>] %pS\n",
323 (void *)entries
[i
], (void *)entries
[i
]);
333 #ifdef CONFIG_SCHEDSTATS
335 * Provides /proc/PID/schedstat
337 static int proc_pid_schedstat(struct task_struct
*task
, char *buffer
)
339 return sprintf(buffer
, "%llu %llu %lu\n",
340 (unsigned long long)task
->se
.sum_exec_runtime
,
341 (unsigned long long)task
->sched_info
.run_delay
,
342 task
->sched_info
.pcount
);
346 #ifdef CONFIG_LATENCYTOP
347 static int lstats_show_proc(struct seq_file
*m
, void *v
)
350 struct inode
*inode
= m
->private;
351 struct task_struct
*task
= get_proc_task(inode
);
355 seq_puts(m
, "Latency Top version : v0.1\n");
356 for (i
= 0; i
< 32; i
++) {
357 struct latency_record
*lr
= &task
->latency_record
[i
];
358 if (lr
->backtrace
[0]) {
360 seq_printf(m
, "%i %li %li",
361 lr
->count
, lr
->time
, lr
->max
);
362 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
363 unsigned long bt
= lr
->backtrace
[q
];
368 seq_printf(m
, " %ps", (void *)bt
);
374 put_task_struct(task
);
378 static int lstats_open(struct inode
*inode
, struct file
*file
)
380 return single_open(file
, lstats_show_proc
, inode
);
383 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
384 size_t count
, loff_t
*offs
)
386 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
390 clear_all_latency_tracing(task
);
391 put_task_struct(task
);
396 static const struct file_operations proc_lstats_operations
= {
399 .write
= lstats_write
,
401 .release
= single_release
,
406 static int proc_oom_score(struct task_struct
*task
, char *buffer
)
408 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
409 unsigned long points
= 0;
411 read_lock(&tasklist_lock
);
413 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
415 read_unlock(&tasklist_lock
);
416 return sprintf(buffer
, "%lu\n", points
);
424 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
425 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
426 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
427 [RLIMIT_DATA
] = {"Max data size", "bytes"},
428 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
429 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
430 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
431 [RLIMIT_NPROC
] = {"Max processes", "processes"},
432 [RLIMIT_NOFILE
] = {"Max open files", "files"},
433 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
434 [RLIMIT_AS
] = {"Max address space", "bytes"},
435 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
436 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
437 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
438 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
439 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
440 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
443 /* Display limits for a process */
444 static int proc_pid_limits(struct task_struct
*task
, char *buffer
)
449 char *bufptr
= buffer
;
451 struct rlimit rlim
[RLIM_NLIMITS
];
453 if (!lock_task_sighand(task
, &flags
))
455 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
456 unlock_task_sighand(task
, &flags
);
459 * print the file header
461 count
+= sprintf(&bufptr
[count
], "%-25s %-20s %-20s %-10s\n",
462 "Limit", "Soft Limit", "Hard Limit", "Units");
464 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
465 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
466 count
+= sprintf(&bufptr
[count
], "%-25s %-20s ",
467 lnames
[i
].name
, "unlimited");
469 count
+= sprintf(&bufptr
[count
], "%-25s %-20lu ",
470 lnames
[i
].name
, rlim
[i
].rlim_cur
);
472 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
473 count
+= sprintf(&bufptr
[count
], "%-20s ", "unlimited");
475 count
+= sprintf(&bufptr
[count
], "%-20lu ",
479 count
+= sprintf(&bufptr
[count
], "%-10s\n",
482 count
+= sprintf(&bufptr
[count
], "\n");
488 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
489 static int proc_pid_syscall(struct task_struct
*task
, char *buffer
)
492 unsigned long args
[6], sp
, pc
;
493 int res
= lock_trace(task
);
497 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
498 res
= sprintf(buffer
, "running\n");
500 res
= sprintf(buffer
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
502 res
= sprintf(buffer
,
503 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
505 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
510 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
512 /************************************************************************/
513 /* Here the fs part begins */
514 /************************************************************************/
516 /* permission checks */
517 static int proc_fd_access_allowed(struct inode
*inode
)
519 struct task_struct
*task
;
521 /* Allow access to a task's file descriptors if it is us or we
522 * may use ptrace attach to the process and find out that
525 task
= get_proc_task(inode
);
527 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ
);
528 put_task_struct(task
);
533 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
536 struct inode
*inode
= dentry
->d_inode
;
538 if (attr
->ia_valid
& ATTR_MODE
)
541 error
= inode_change_ok(inode
, attr
);
545 if ((attr
->ia_valid
& ATTR_SIZE
) &&
546 attr
->ia_size
!= i_size_read(inode
)) {
547 error
= vmtruncate(inode
, attr
->ia_size
);
552 setattr_copy(inode
, attr
);
553 mark_inode_dirty(inode
);
558 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
559 * or euid/egid (for hide_pid_min=2)?
561 static bool has_pid_permissions(struct pid_namespace
*pid
,
562 struct task_struct
*task
,
565 if (pid
->hide_pid
< hide_pid_min
)
567 if (in_group_p(pid
->pid_gid
))
569 return ptrace_may_access(task
, PTRACE_MODE_READ
);
573 static int proc_pid_permission(struct inode
*inode
, int mask
)
575 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
576 struct task_struct
*task
;
579 task
= get_proc_task(inode
);
582 has_perms
= has_pid_permissions(pid
, task
, 1);
583 put_task_struct(task
);
586 if (pid
->hide_pid
== 2) {
588 * Let's make getdents(), stat(), and open()
589 * consistent with each other. If a process
590 * may not stat() a file, it shouldn't be seen
598 return generic_permission(inode
, mask
);
603 static const struct inode_operations proc_def_inode_operations
= {
604 .setattr
= proc_setattr
,
607 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
609 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
610 size_t count
, loff_t
*ppos
)
612 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
615 struct task_struct
*task
= get_proc_task(inode
);
621 if (count
> PROC_BLOCK_SIZE
)
622 count
= PROC_BLOCK_SIZE
;
625 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
628 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
631 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
634 put_task_struct(task
);
639 static const struct file_operations proc_info_file_operations
= {
640 .read
= proc_info_read
,
641 .llseek
= generic_file_llseek
,
644 static int proc_single_show(struct seq_file
*m
, void *v
)
646 struct inode
*inode
= m
->private;
647 struct pid_namespace
*ns
;
649 struct task_struct
*task
;
652 ns
= inode
->i_sb
->s_fs_info
;
653 pid
= proc_pid(inode
);
654 task
= get_pid_task(pid
, PIDTYPE_PID
);
658 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
660 put_task_struct(task
);
664 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
666 return single_open(filp
, proc_single_show
, inode
);
669 static const struct file_operations proc_single_file_operations
= {
670 .open
= proc_single_open
,
673 .release
= single_release
,
676 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
678 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
679 struct mm_struct
*mm
;
684 mm
= mm_access(task
, mode
);
685 put_task_struct(task
);
691 /* ensure this mm_struct can't be freed */
692 atomic_inc(&mm
->mm_count
);
693 /* but do not pin its memory */
697 file
->private_data
= mm
;
702 static int mem_open(struct inode
*inode
, struct file
*file
)
704 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
706 /* OK to pass negative loff_t, we can catch out-of-range */
707 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
712 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
713 size_t count
, loff_t
*ppos
, int write
)
715 struct mm_struct
*mm
= file
->private_data
;
716 unsigned long addr
= *ppos
;
723 page
= (char *)__get_free_page(GFP_TEMPORARY
);
728 if (!atomic_inc_not_zero(&mm
->mm_users
))
732 int this_len
= min_t(int, count
, PAGE_SIZE
);
734 if (write
&& copy_from_user(page
, buf
, this_len
)) {
739 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
746 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
760 free_page((unsigned long) page
);
764 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
765 size_t count
, loff_t
*ppos
)
767 return mem_rw(file
, buf
, count
, ppos
, 0);
770 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
771 size_t count
, loff_t
*ppos
)
773 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
776 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
780 file
->f_pos
= offset
;
783 file
->f_pos
+= offset
;
788 force_successful_syscall_return();
792 static int mem_release(struct inode
*inode
, struct file
*file
)
794 struct mm_struct
*mm
= file
->private_data
;
800 static const struct file_operations proc_mem_operations
= {
805 .release
= mem_release
,
808 static int environ_open(struct inode
*inode
, struct file
*file
)
810 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
813 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
814 size_t count
, loff_t
*ppos
)
817 unsigned long src
= *ppos
;
819 struct mm_struct
*mm
= file
->private_data
;
824 page
= (char *)__get_free_page(GFP_TEMPORARY
);
829 if (!atomic_inc_not_zero(&mm
->mm_users
))
832 size_t this_len
, max_len
;
835 if (src
>= (mm
->env_end
- mm
->env_start
))
838 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
840 max_len
= min_t(size_t, PAGE_SIZE
, count
);
841 this_len
= min(max_len
, this_len
);
843 retval
= access_remote_vm(mm
, (mm
->env_start
+ src
),
851 if (copy_to_user(buf
, page
, retval
)) {
865 free_page((unsigned long) page
);
869 static const struct file_operations proc_environ_operations
= {
870 .open
= environ_open
,
871 .read
= environ_read
,
872 .llseek
= generic_file_llseek
,
873 .release
= mem_release
,
876 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
879 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
880 char buffer
[PROC_NUMBUF
];
881 int oom_adj
= OOM_ADJUST_MIN
;
887 if (lock_task_sighand(task
, &flags
)) {
888 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
889 oom_adj
= OOM_ADJUST_MAX
;
891 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
893 unlock_task_sighand(task
, &flags
);
895 put_task_struct(task
);
896 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
897 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
900 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
901 size_t count
, loff_t
*ppos
)
903 struct task_struct
*task
;
904 char buffer
[PROC_NUMBUF
];
909 memset(buffer
, 0, sizeof(buffer
));
910 if (count
> sizeof(buffer
) - 1)
911 count
= sizeof(buffer
) - 1;
912 if (copy_from_user(buffer
, buf
, count
)) {
917 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
920 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
921 oom_adj
!= OOM_DISABLE
) {
926 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
938 if (!lock_task_sighand(task
, &flags
)) {
944 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
945 * value is always attainable.
947 if (oom_adj
== OOM_ADJUST_MAX
)
948 oom_adj
= OOM_SCORE_ADJ_MAX
;
950 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
952 if (oom_adj
< task
->signal
->oom_score_adj
&&
953 !capable(CAP_SYS_RESOURCE
)) {
959 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
960 * /proc/pid/oom_score_adj instead.
962 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
963 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
966 task
->signal
->oom_score_adj
= oom_adj
;
967 trace_oom_score_adj_update(task
);
969 unlock_task_sighand(task
, &flags
);
972 put_task_struct(task
);
974 return err
< 0 ? err
: count
;
977 static const struct file_operations proc_oom_adj_operations
= {
978 .read
= oom_adj_read
,
979 .write
= oom_adj_write
,
980 .llseek
= generic_file_llseek
,
983 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
984 size_t count
, loff_t
*ppos
)
986 struct task_struct
*task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
987 char buffer
[PROC_NUMBUF
];
988 int oom_score_adj
= OOM_SCORE_ADJ_MIN
;
994 if (lock_task_sighand(task
, &flags
)) {
995 oom_score_adj
= task
->signal
->oom_score_adj
;
996 unlock_task_sighand(task
, &flags
);
998 put_task_struct(task
);
999 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_score_adj
);
1000 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1003 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1004 size_t count
, loff_t
*ppos
)
1006 struct task_struct
*task
;
1007 char buffer
[PROC_NUMBUF
];
1008 unsigned long flags
;
1012 memset(buffer
, 0, sizeof(buffer
));
1013 if (count
> sizeof(buffer
) - 1)
1014 count
= sizeof(buffer
) - 1;
1015 if (copy_from_user(buffer
, buf
, count
)) {
1020 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1023 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1024 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1029 task
= get_proc_task(file
->f_path
.dentry
->d_inode
);
1041 if (!lock_task_sighand(task
, &flags
)) {
1046 if (oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1047 !capable(CAP_SYS_RESOURCE
)) {
1052 task
->signal
->oom_score_adj
= oom_score_adj
;
1053 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1054 task
->signal
->oom_score_adj_min
= oom_score_adj
;
1055 trace_oom_score_adj_update(task
);
1058 unlock_task_sighand(task
, &flags
);
1061 put_task_struct(task
);
1063 return err
< 0 ? err
: count
;
1066 static const struct file_operations proc_oom_score_adj_operations
= {
1067 .read
= oom_score_adj_read
,
1068 .write
= oom_score_adj_write
,
1069 .llseek
= default_llseek
,
1072 #ifdef CONFIG_AUDITSYSCALL
1073 #define TMPBUFLEN 21
1074 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1075 size_t count
, loff_t
*ppos
)
1077 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1078 struct task_struct
*task
= get_proc_task(inode
);
1080 char tmpbuf
[TMPBUFLEN
];
1084 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1085 from_kuid(file
->f_cred
->user_ns
,
1086 audit_get_loginuid(task
)));
1087 put_task_struct(task
);
1088 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1091 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1092 size_t count
, loff_t
*ppos
)
1094 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1101 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1107 if (count
>= PAGE_SIZE
)
1108 count
= PAGE_SIZE
- 1;
1111 /* No partial writes. */
1114 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1118 if (copy_from_user(page
, buf
, count
))
1122 loginuid
= simple_strtoul(page
, &tmp
, 10);
1128 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1129 if (!uid_valid(kloginuid
)) {
1134 length
= audit_set_loginuid(kloginuid
);
1135 if (likely(length
== 0))
1139 free_page((unsigned long) page
);
1143 static const struct file_operations proc_loginuid_operations
= {
1144 .read
= proc_loginuid_read
,
1145 .write
= proc_loginuid_write
,
1146 .llseek
= generic_file_llseek
,
1149 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1150 size_t count
, loff_t
*ppos
)
1152 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
1153 struct task_struct
*task
= get_proc_task(inode
);
1155 char tmpbuf
[TMPBUFLEN
];
1159 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1160 audit_get_sessionid(task
));
1161 put_task_struct(task
);
1162 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1165 static const struct file_operations proc_sessionid_operations
= {
1166 .read
= proc_sessionid_read
,
1167 .llseek
= generic_file_llseek
,
1171 #ifdef CONFIG_FAULT_INJECTION
1172 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1173 size_t count
, loff_t
*ppos
)
1175 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
1176 char buffer
[PROC_NUMBUF
];
1182 make_it_fail
= task
->make_it_fail
;
1183 put_task_struct(task
);
1185 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1187 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1190 static ssize_t
proc_fault_inject_write(struct file
* file
,
1191 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1193 struct task_struct
*task
;
1194 char buffer
[PROC_NUMBUF
], *end
;
1197 if (!capable(CAP_SYS_RESOURCE
))
1199 memset(buffer
, 0, sizeof(buffer
));
1200 if (count
> sizeof(buffer
) - 1)
1201 count
= sizeof(buffer
) - 1;
1202 if (copy_from_user(buffer
, buf
, count
))
1204 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1207 task
= get_proc_task(file
->f_dentry
->d_inode
);
1210 task
->make_it_fail
= make_it_fail
;
1211 put_task_struct(task
);
1216 static const struct file_operations proc_fault_inject_operations
= {
1217 .read
= proc_fault_inject_read
,
1218 .write
= proc_fault_inject_write
,
1219 .llseek
= generic_file_llseek
,
1224 #ifdef CONFIG_SCHED_DEBUG
1226 * Print out various scheduling related per-task fields:
1228 static int sched_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_show_task(p
, m
);
1244 sched_write(struct file
*file
, const char __user
*buf
,
1245 size_t count
, loff_t
*offset
)
1247 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1248 struct task_struct
*p
;
1250 p
= get_proc_task(inode
);
1253 proc_sched_set_task(p
);
1260 static int sched_open(struct inode
*inode
, struct file
*filp
)
1262 return single_open(filp
, sched_show
, inode
);
1265 static const struct file_operations proc_pid_sched_operations
= {
1268 .write
= sched_write
,
1269 .llseek
= seq_lseek
,
1270 .release
= single_release
,
1275 #ifdef CONFIG_SCHED_AUTOGROUP
1277 * Print out autogroup related information:
1279 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1281 struct inode
*inode
= m
->private;
1282 struct task_struct
*p
;
1284 p
= get_proc_task(inode
);
1287 proc_sched_autogroup_show_task(p
, m
);
1295 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1296 size_t count
, loff_t
*offset
)
1298 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1299 struct task_struct
*p
;
1300 char buffer
[PROC_NUMBUF
];
1304 memset(buffer
, 0, sizeof(buffer
));
1305 if (count
> sizeof(buffer
) - 1)
1306 count
= sizeof(buffer
) - 1;
1307 if (copy_from_user(buffer
, buf
, count
))
1310 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1314 p
= get_proc_task(inode
);
1318 err
= proc_sched_autogroup_set_nice(p
, nice
);
1327 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1331 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1333 struct seq_file
*m
= filp
->private_data
;
1340 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1341 .open
= sched_autogroup_open
,
1343 .write
= sched_autogroup_write
,
1344 .llseek
= seq_lseek
,
1345 .release
= single_release
,
1348 #endif /* CONFIG_SCHED_AUTOGROUP */
1350 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1351 size_t count
, loff_t
*offset
)
1353 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1354 struct task_struct
*p
;
1355 char buffer
[TASK_COMM_LEN
];
1357 memset(buffer
, 0, sizeof(buffer
));
1358 if (count
> sizeof(buffer
) - 1)
1359 count
= sizeof(buffer
) - 1;
1360 if (copy_from_user(buffer
, buf
, count
))
1363 p
= get_proc_task(inode
);
1367 if (same_thread_group(current
, p
))
1368 set_task_comm(p
, buffer
);
1377 static int comm_show(struct seq_file
*m
, void *v
)
1379 struct inode
*inode
= m
->private;
1380 struct task_struct
*p
;
1382 p
= get_proc_task(inode
);
1387 seq_printf(m
, "%s\n", p
->comm
);
1395 static int comm_open(struct inode
*inode
, struct file
*filp
)
1397 return single_open(filp
, comm_show
, inode
);
1400 static const struct file_operations proc_pid_set_comm_operations
= {
1403 .write
= comm_write
,
1404 .llseek
= seq_lseek
,
1405 .release
= single_release
,
1408 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1410 struct task_struct
*task
;
1411 struct mm_struct
*mm
;
1412 struct file
*exe_file
;
1414 task
= get_proc_task(dentry
->d_inode
);
1417 mm
= get_task_mm(task
);
1418 put_task_struct(task
);
1421 exe_file
= get_mm_exe_file(mm
);
1424 *exe_path
= exe_file
->f_path
;
1425 path_get(&exe_file
->f_path
);
1432 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1434 struct inode
*inode
= dentry
->d_inode
;
1436 int error
= -EACCES
;
1438 /* Are we allowed to snoop on the tasks file descriptors? */
1439 if (!proc_fd_access_allowed(inode
))
1442 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1446 nd_jump_link(nd
, &path
);
1449 return ERR_PTR(error
);
1452 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1454 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1461 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1462 len
= PTR_ERR(pathname
);
1463 if (IS_ERR(pathname
))
1465 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1469 if (copy_to_user(buffer
, pathname
, len
))
1472 free_page((unsigned long)tmp
);
1476 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1478 int error
= -EACCES
;
1479 struct inode
*inode
= dentry
->d_inode
;
1482 /* Are we allowed to snoop on the tasks file descriptors? */
1483 if (!proc_fd_access_allowed(inode
))
1486 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1490 error
= do_proc_readlink(&path
, buffer
, buflen
);
1496 const struct inode_operations proc_pid_link_inode_operations
= {
1497 .readlink
= proc_pid_readlink
,
1498 .follow_link
= proc_pid_follow_link
,
1499 .setattr
= proc_setattr
,
1503 /* building an inode */
1505 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1507 struct inode
* inode
;
1508 struct proc_inode
*ei
;
1509 const struct cred
*cred
;
1511 /* We need a new inode */
1513 inode
= new_inode(sb
);
1519 inode
->i_ino
= get_next_ino();
1520 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1521 inode
->i_op
= &proc_def_inode_operations
;
1524 * grab the reference to task.
1526 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1530 if (task_dumpable(task
)) {
1532 cred
= __task_cred(task
);
1533 inode
->i_uid
= cred
->euid
;
1534 inode
->i_gid
= cred
->egid
;
1537 security_task_to_inode(task
, inode
);
1547 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1549 struct inode
*inode
= dentry
->d_inode
;
1550 struct task_struct
*task
;
1551 const struct cred
*cred
;
1552 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1554 generic_fillattr(inode
, stat
);
1557 stat
->uid
= GLOBAL_ROOT_UID
;
1558 stat
->gid
= GLOBAL_ROOT_GID
;
1559 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1561 if (!has_pid_permissions(pid
, task
, 2)) {
1564 * This doesn't prevent learning whether PID exists,
1565 * it only makes getattr() consistent with readdir().
1569 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1570 task_dumpable(task
)) {
1571 cred
= __task_cred(task
);
1572 stat
->uid
= cred
->euid
;
1573 stat
->gid
= cred
->egid
;
1583 * Exceptional case: normally we are not allowed to unhash a busy
1584 * directory. In this case, however, we can do it - no aliasing problems
1585 * due to the way we treat inodes.
1587 * Rewrite the inode's ownerships here because the owning task may have
1588 * performed a setuid(), etc.
1590 * Before the /proc/pid/status file was created the only way to read
1591 * the effective uid of a /process was to stat /proc/pid. Reading
1592 * /proc/pid/status is slow enough that procps and other packages
1593 * kept stating /proc/pid. To keep the rules in /proc simple I have
1594 * made this apply to all per process world readable and executable
1597 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1599 struct inode
*inode
;
1600 struct task_struct
*task
;
1601 const struct cred
*cred
;
1603 if (flags
& LOOKUP_RCU
)
1606 inode
= dentry
->d_inode
;
1607 task
= get_proc_task(inode
);
1610 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1611 task_dumpable(task
)) {
1613 cred
= __task_cred(task
);
1614 inode
->i_uid
= cred
->euid
;
1615 inode
->i_gid
= cred
->egid
;
1618 inode
->i_uid
= GLOBAL_ROOT_UID
;
1619 inode
->i_gid
= GLOBAL_ROOT_GID
;
1621 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1622 security_task_to_inode(task
, inode
);
1623 put_task_struct(task
);
1630 const struct dentry_operations pid_dentry_operations
=
1632 .d_revalidate
= pid_revalidate
,
1633 .d_delete
= pid_delete_dentry
,
1639 * Fill a directory entry.
1641 * If possible create the dcache entry and derive our inode number and
1642 * file type from dcache entry.
1644 * Since all of the proc inode numbers are dynamically generated, the inode
1645 * numbers do not exist until the inode is cache. This means creating the
1646 * the dcache entry in readdir is necessary to keep the inode numbers
1647 * reported by readdir in sync with the inode numbers reported
1650 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1651 const char *name
, int len
,
1652 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1654 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1655 struct inode
*inode
;
1658 unsigned type
= DT_UNKNOWN
;
1662 qname
.hash
= full_name_hash(name
, len
);
1664 child
= d_lookup(dir
, &qname
);
1667 new = d_alloc(dir
, &qname
);
1669 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1676 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1677 goto end_instantiate
;
1678 inode
= child
->d_inode
;
1681 type
= inode
->i_mode
>> 12;
1686 ino
= find_inode_number(dir
, &qname
);
1689 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1692 #ifdef CONFIG_CHECKPOINT_RESTORE
1695 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1696 * which represent vma start and end addresses.
1698 static int dname_to_vma_addr(struct dentry
*dentry
,
1699 unsigned long *start
, unsigned long *end
)
1701 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1707 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1709 unsigned long vm_start
, vm_end
;
1710 bool exact_vma_exists
= false;
1711 struct mm_struct
*mm
= NULL
;
1712 struct task_struct
*task
;
1713 const struct cred
*cred
;
1714 struct inode
*inode
;
1717 if (flags
& LOOKUP_RCU
)
1720 if (!capable(CAP_SYS_ADMIN
)) {
1725 inode
= dentry
->d_inode
;
1726 task
= get_proc_task(inode
);
1730 mm
= mm_access(task
, PTRACE_MODE_READ
);
1731 if (IS_ERR_OR_NULL(mm
))
1734 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1735 down_read(&mm
->mmap_sem
);
1736 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1737 up_read(&mm
->mmap_sem
);
1742 if (exact_vma_exists
) {
1743 if (task_dumpable(task
)) {
1745 cred
= __task_cred(task
);
1746 inode
->i_uid
= cred
->euid
;
1747 inode
->i_gid
= cred
->egid
;
1750 inode
->i_uid
= GLOBAL_ROOT_UID
;
1751 inode
->i_gid
= GLOBAL_ROOT_GID
;
1753 security_task_to_inode(task
, inode
);
1758 put_task_struct(task
);
1767 static const struct dentry_operations tid_map_files_dentry_operations
= {
1768 .d_revalidate
= map_files_d_revalidate
,
1769 .d_delete
= pid_delete_dentry
,
1772 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1774 unsigned long vm_start
, vm_end
;
1775 struct vm_area_struct
*vma
;
1776 struct task_struct
*task
;
1777 struct mm_struct
*mm
;
1781 task
= get_proc_task(dentry
->d_inode
);
1785 mm
= get_task_mm(task
);
1786 put_task_struct(task
);
1790 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1794 down_read(&mm
->mmap_sem
);
1795 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1796 if (vma
&& vma
->vm_file
) {
1797 *path
= vma
->vm_file
->f_path
;
1801 up_read(&mm
->mmap_sem
);
1809 struct map_files_info
{
1812 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1815 static struct dentry
*
1816 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
1817 struct task_struct
*task
, const void *ptr
)
1819 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
1820 struct proc_inode
*ei
;
1821 struct inode
*inode
;
1823 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1825 return ERR_PTR(-ENOENT
);
1828 ei
->op
.proc_get_link
= proc_map_files_get_link
;
1830 inode
->i_op
= &proc_pid_link_inode_operations
;
1832 inode
->i_mode
= S_IFLNK
;
1834 if (mode
& FMODE_READ
)
1835 inode
->i_mode
|= S_IRUSR
;
1836 if (mode
& FMODE_WRITE
)
1837 inode
->i_mode
|= S_IWUSR
;
1839 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
1840 d_add(dentry
, inode
);
1845 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
1846 struct dentry
*dentry
, unsigned int flags
)
1848 unsigned long vm_start
, vm_end
;
1849 struct vm_area_struct
*vma
;
1850 struct task_struct
*task
;
1851 struct dentry
*result
;
1852 struct mm_struct
*mm
;
1854 result
= ERR_PTR(-EACCES
);
1855 if (!capable(CAP_SYS_ADMIN
))
1858 result
= ERR_PTR(-ENOENT
);
1859 task
= get_proc_task(dir
);
1863 result
= ERR_PTR(-EACCES
);
1864 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1867 result
= ERR_PTR(-ENOENT
);
1868 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
1871 mm
= get_task_mm(task
);
1875 down_read(&mm
->mmap_sem
);
1876 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1880 result
= proc_map_files_instantiate(dir
, dentry
, task
,
1881 (void *)(unsigned long)vma
->vm_file
->f_mode
);
1884 up_read(&mm
->mmap_sem
);
1887 put_task_struct(task
);
1892 static const struct inode_operations proc_map_files_inode_operations
= {
1893 .lookup
= proc_map_files_lookup
,
1894 .permission
= proc_fd_permission
,
1895 .setattr
= proc_setattr
,
1899 proc_map_files_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1901 struct dentry
*dentry
= filp
->f_path
.dentry
;
1902 struct inode
*inode
= dentry
->d_inode
;
1903 struct vm_area_struct
*vma
;
1904 struct task_struct
*task
;
1905 struct mm_struct
*mm
;
1910 if (!capable(CAP_SYS_ADMIN
))
1914 task
= get_proc_task(inode
);
1919 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1923 switch (filp
->f_pos
) {
1926 if (filldir(dirent
, ".", 1, 0, ino
, DT_DIR
) < 0)
1930 ino
= parent_ino(dentry
);
1931 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1936 unsigned long nr_files
, pos
, i
;
1937 struct flex_array
*fa
= NULL
;
1938 struct map_files_info info
;
1939 struct map_files_info
*p
;
1941 mm
= get_task_mm(task
);
1944 down_read(&mm
->mmap_sem
);
1949 * We need two passes here:
1951 * 1) Collect vmas of mapped files with mmap_sem taken
1952 * 2) Release mmap_sem and instantiate entries
1954 * otherwise we get lockdep complained, since filldir()
1955 * routine might require mmap_sem taken in might_fault().
1958 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
1959 if (vma
->vm_file
&& ++pos
> filp
->f_pos
)
1964 fa
= flex_array_alloc(sizeof(info
), nr_files
,
1966 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
1970 flex_array_free(fa
);
1971 up_read(&mm
->mmap_sem
);
1975 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
1976 vma
= vma
->vm_next
) {
1979 if (++pos
<= filp
->f_pos
)
1982 info
.mode
= vma
->vm_file
->f_mode
;
1983 info
.len
= snprintf(info
.name
,
1984 sizeof(info
.name
), "%lx-%lx",
1985 vma
->vm_start
, vma
->vm_end
);
1986 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
1990 up_read(&mm
->mmap_sem
);
1992 for (i
= 0; i
< nr_files
; i
++) {
1993 p
= flex_array_get(fa
, i
);
1994 ret
= proc_fill_cache(filp
, dirent
, filldir
,
1996 proc_map_files_instantiate
,
1998 (void *)(unsigned long)p
->mode
);
2004 flex_array_free(fa
);
2010 put_task_struct(task
);
2015 static const struct file_operations proc_map_files_operations
= {
2016 .read
= generic_read_dir
,
2017 .readdir
= proc_map_files_readdir
,
2018 .llseek
= default_llseek
,
2021 #endif /* CONFIG_CHECKPOINT_RESTORE */
2023 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2024 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2026 const struct pid_entry
*p
= ptr
;
2027 struct inode
*inode
;
2028 struct proc_inode
*ei
;
2029 struct dentry
*error
= ERR_PTR(-ENOENT
);
2031 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2036 inode
->i_mode
= p
->mode
;
2037 if (S_ISDIR(inode
->i_mode
))
2038 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2040 inode
->i_op
= p
->iop
;
2042 inode
->i_fop
= p
->fop
;
2044 d_set_d_op(dentry
, &pid_dentry_operations
);
2045 d_add(dentry
, inode
);
2046 /* Close the race of the process dying before we return the dentry */
2047 if (pid_revalidate(dentry
, 0))
2053 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2054 struct dentry
*dentry
,
2055 const struct pid_entry
*ents
,
2058 struct dentry
*error
;
2059 struct task_struct
*task
= get_proc_task(dir
);
2060 const struct pid_entry
*p
, *last
;
2062 error
= ERR_PTR(-ENOENT
);
2068 * Yes, it does not scale. And it should not. Don't add
2069 * new entries into /proc/<tgid>/ without very good reasons.
2071 last
= &ents
[nents
- 1];
2072 for (p
= ents
; p
<= last
; p
++) {
2073 if (p
->len
!= dentry
->d_name
.len
)
2075 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2081 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2083 put_task_struct(task
);
2088 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2089 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2091 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2092 proc_pident_instantiate
, task
, p
);
2095 static int proc_pident_readdir(struct file
*filp
,
2096 void *dirent
, filldir_t filldir
,
2097 const struct pid_entry
*ents
, unsigned int nents
)
2100 struct dentry
*dentry
= filp
->f_path
.dentry
;
2101 struct inode
*inode
= dentry
->d_inode
;
2102 struct task_struct
*task
= get_proc_task(inode
);
2103 const struct pid_entry
*p
, *last
;
2116 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2122 ino
= parent_ino(dentry
);
2123 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2135 last
= &ents
[nents
- 1];
2137 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2146 put_task_struct(task
);
2151 #ifdef CONFIG_SECURITY
2152 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2153 size_t count
, loff_t
*ppos
)
2155 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2158 struct task_struct
*task
= get_proc_task(inode
);
2163 length
= security_getprocattr(task
,
2164 (char*)file
->f_path
.dentry
->d_name
.name
,
2166 put_task_struct(task
);
2168 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2173 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2174 size_t count
, loff_t
*ppos
)
2176 struct inode
* inode
= file
->f_path
.dentry
->d_inode
;
2179 struct task_struct
*task
= get_proc_task(inode
);
2184 if (count
> PAGE_SIZE
)
2187 /* No partial writes. */
2193 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2198 if (copy_from_user(page
, buf
, count
))
2201 /* Guard against adverse ptrace interaction */
2202 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2206 length
= security_setprocattr(task
,
2207 (char*)file
->f_path
.dentry
->d_name
.name
,
2208 (void*)page
, count
);
2209 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2211 free_page((unsigned long) page
);
2213 put_task_struct(task
);
2218 static const struct file_operations proc_pid_attr_operations
= {
2219 .read
= proc_pid_attr_read
,
2220 .write
= proc_pid_attr_write
,
2221 .llseek
= generic_file_llseek
,
2224 static const struct pid_entry attr_dir_stuff
[] = {
2225 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2226 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2227 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2228 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2229 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2230 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2233 static int proc_attr_dir_readdir(struct file
* filp
,
2234 void * dirent
, filldir_t filldir
)
2236 return proc_pident_readdir(filp
,dirent
,filldir
,
2237 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2240 static const struct file_operations proc_attr_dir_operations
= {
2241 .read
= generic_read_dir
,
2242 .readdir
= proc_attr_dir_readdir
,
2243 .llseek
= default_llseek
,
2246 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2247 struct dentry
*dentry
, unsigned int flags
)
2249 return proc_pident_lookup(dir
, dentry
,
2250 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2253 static const struct inode_operations proc_attr_dir_inode_operations
= {
2254 .lookup
= proc_attr_dir_lookup
,
2255 .getattr
= pid_getattr
,
2256 .setattr
= proc_setattr
,
2261 #ifdef CONFIG_ELF_CORE
2262 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2263 size_t count
, loff_t
*ppos
)
2265 struct task_struct
*task
= get_proc_task(file
->f_dentry
->d_inode
);
2266 struct mm_struct
*mm
;
2267 char buffer
[PROC_NUMBUF
];
2275 mm
= get_task_mm(task
);
2277 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2278 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2279 MMF_DUMP_FILTER_SHIFT
));
2281 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2284 put_task_struct(task
);
2289 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2290 const char __user
*buf
,
2294 struct task_struct
*task
;
2295 struct mm_struct
*mm
;
2296 char buffer
[PROC_NUMBUF
], *end
;
2303 memset(buffer
, 0, sizeof(buffer
));
2304 if (count
> sizeof(buffer
) - 1)
2305 count
= sizeof(buffer
) - 1;
2306 if (copy_from_user(buffer
, buf
, count
))
2310 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2313 if (end
- buffer
== 0)
2317 task
= get_proc_task(file
->f_dentry
->d_inode
);
2322 mm
= get_task_mm(task
);
2326 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2328 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2330 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2335 put_task_struct(task
);
2340 static const struct file_operations proc_coredump_filter_operations
= {
2341 .read
= proc_coredump_filter_read
,
2342 .write
= proc_coredump_filter_write
,
2343 .llseek
= generic_file_llseek
,
2350 static int proc_self_readlink(struct dentry
*dentry
, char __user
*buffer
,
2353 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2354 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2355 char tmp
[PROC_NUMBUF
];
2358 sprintf(tmp
, "%d", tgid
);
2359 return vfs_readlink(dentry
,buffer
,buflen
,tmp
);
2362 static void *proc_self_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
2364 struct pid_namespace
*ns
= dentry
->d_sb
->s_fs_info
;
2365 pid_t tgid
= task_tgid_nr_ns(current
, ns
);
2366 char *name
= ERR_PTR(-ENOENT
);
2368 /* 11 for max length of signed int in decimal + NULL term */
2369 name
= kmalloc(12, GFP_KERNEL
);
2371 name
= ERR_PTR(-ENOMEM
);
2373 sprintf(name
, "%d", tgid
);
2375 nd_set_link(nd
, name
);
2379 static void proc_self_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
2382 char *s
= nd_get_link(nd
);
2387 static const struct inode_operations proc_self_inode_operations
= {
2388 .readlink
= proc_self_readlink
,
2389 .follow_link
= proc_self_follow_link
,
2390 .put_link
= proc_self_put_link
,
2396 * These are the directory entries in the root directory of /proc
2397 * that properly belong to the /proc filesystem, as they describe
2398 * describe something that is process related.
2400 static const struct pid_entry proc_base_stuff
[] = {
2401 NOD("self", S_IFLNK
|S_IRWXUGO
,
2402 &proc_self_inode_operations
, NULL
, {}),
2405 static struct dentry
*proc_base_instantiate(struct inode
*dir
,
2406 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2408 const struct pid_entry
*p
= ptr
;
2409 struct inode
*inode
;
2410 struct proc_inode
*ei
;
2411 struct dentry
*error
;
2413 /* Allocate the inode */
2414 error
= ERR_PTR(-ENOMEM
);
2415 inode
= new_inode(dir
->i_sb
);
2419 /* Initialize the inode */
2421 inode
->i_ino
= get_next_ino();
2422 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
2425 * grab the reference to the task.
2427 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
2431 inode
->i_mode
= p
->mode
;
2432 if (S_ISDIR(inode
->i_mode
))
2433 set_nlink(inode
, 2);
2434 if (S_ISLNK(inode
->i_mode
))
2437 inode
->i_op
= p
->iop
;
2439 inode
->i_fop
= p
->fop
;
2441 d_add(dentry
, inode
);
2450 static struct dentry
*proc_base_lookup(struct inode
*dir
, struct dentry
*dentry
)
2452 struct dentry
*error
;
2453 struct task_struct
*task
= get_proc_task(dir
);
2454 const struct pid_entry
*p
, *last
;
2456 error
= ERR_PTR(-ENOENT
);
2461 /* Lookup the directory entry */
2462 last
= &proc_base_stuff
[ARRAY_SIZE(proc_base_stuff
) - 1];
2463 for (p
= proc_base_stuff
; p
<= last
; p
++) {
2464 if (p
->len
!= dentry
->d_name
.len
)
2466 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2472 error
= proc_base_instantiate(dir
, dentry
, task
, p
);
2475 put_task_struct(task
);
2480 static int proc_base_fill_cache(struct file
*filp
, void *dirent
,
2481 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2483 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2484 proc_base_instantiate
, task
, p
);
2487 #ifdef CONFIG_TASK_IO_ACCOUNTING
2488 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2490 struct task_io_accounting acct
= task
->ioac
;
2491 unsigned long flags
;
2494 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2498 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2503 if (whole
&& lock_task_sighand(task
, &flags
)) {
2504 struct task_struct
*t
= task
;
2506 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2507 while_each_thread(task
, t
)
2508 task_io_accounting_add(&acct
, &t
->ioac
);
2510 unlock_task_sighand(task
, &flags
);
2512 result
= sprintf(buffer
,
2517 "read_bytes: %llu\n"
2518 "write_bytes: %llu\n"
2519 "cancelled_write_bytes: %llu\n",
2520 (unsigned long long)acct
.rchar
,
2521 (unsigned long long)acct
.wchar
,
2522 (unsigned long long)acct
.syscr
,
2523 (unsigned long long)acct
.syscw
,
2524 (unsigned long long)acct
.read_bytes
,
2525 (unsigned long long)acct
.write_bytes
,
2526 (unsigned long long)acct
.cancelled_write_bytes
);
2528 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2532 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2534 return do_io_accounting(task
, buffer
, 0);
2537 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2539 return do_io_accounting(task
, buffer
, 1);
2541 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2543 #ifdef CONFIG_USER_NS
2544 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2545 struct seq_operations
*seq_ops
)
2547 struct user_namespace
*ns
= NULL
;
2548 struct task_struct
*task
;
2549 struct seq_file
*seq
;
2552 task
= get_proc_task(inode
);
2555 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2557 put_task_struct(task
);
2562 ret
= seq_open(file
, seq_ops
);
2566 seq
= file
->private_data
;
2576 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2578 struct seq_file
*seq
= file
->private_data
;
2579 struct user_namespace
*ns
= seq
->private;
2581 return seq_release(inode
, file
);
2584 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2586 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2589 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2591 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2594 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2596 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2599 static const struct file_operations proc_uid_map_operations
= {
2600 .open
= proc_uid_map_open
,
2601 .write
= proc_uid_map_write
,
2603 .llseek
= seq_lseek
,
2604 .release
= proc_id_map_release
,
2607 static const struct file_operations proc_gid_map_operations
= {
2608 .open
= proc_gid_map_open
,
2609 .write
= proc_gid_map_write
,
2611 .llseek
= seq_lseek
,
2612 .release
= proc_id_map_release
,
2615 static const struct file_operations proc_projid_map_operations
= {
2616 .open
= proc_projid_map_open
,
2617 .write
= proc_projid_map_write
,
2619 .llseek
= seq_lseek
,
2620 .release
= proc_id_map_release
,
2622 #endif /* CONFIG_USER_NS */
2624 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2625 struct pid
*pid
, struct task_struct
*task
)
2627 int err
= lock_trace(task
);
2629 seq_printf(m
, "%08x\n", task
->personality
);
2638 static const struct file_operations proc_task_operations
;
2639 static const struct inode_operations proc_task_inode_operations
;
2641 static const struct pid_entry tgid_base_stuff
[] = {
2642 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2643 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2644 #ifdef CONFIG_CHECKPOINT_RESTORE
2645 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2647 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2648 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2650 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2652 REG("environ", S_IRUSR
, proc_environ_operations
),
2653 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2654 ONE("status", S_IRUGO
, proc_pid_status
),
2655 ONE("personality", S_IRUGO
, proc_pid_personality
),
2656 INF("limits", S_IRUGO
, proc_pid_limits
),
2657 #ifdef CONFIG_SCHED_DEBUG
2658 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2660 #ifdef CONFIG_SCHED_AUTOGROUP
2661 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2663 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2664 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2665 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2667 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2668 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2669 ONE("statm", S_IRUGO
, proc_pid_statm
),
2670 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2672 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2674 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2675 LNK("cwd", proc_cwd_link
),
2676 LNK("root", proc_root_link
),
2677 LNK("exe", proc_exe_link
),
2678 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2679 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2680 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2681 #ifdef CONFIG_PROC_PAGE_MONITOR
2682 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2683 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2684 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2686 #ifdef CONFIG_SECURITY
2687 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2689 #ifdef CONFIG_KALLSYMS
2690 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2692 #ifdef CONFIG_STACKTRACE
2693 ONE("stack", S_IRUGO
, proc_pid_stack
),
2695 #ifdef CONFIG_SCHEDSTATS
2696 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2698 #ifdef CONFIG_LATENCYTOP
2699 REG("latency", S_IRUGO
, proc_lstats_operations
),
2701 #ifdef CONFIG_PROC_PID_CPUSET
2702 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2704 #ifdef CONFIG_CGROUPS
2705 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2707 INF("oom_score", S_IRUGO
, proc_oom_score
),
2708 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2709 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2710 #ifdef CONFIG_AUDITSYSCALL
2711 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2712 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2714 #ifdef CONFIG_FAULT_INJECTION
2715 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2717 #ifdef CONFIG_ELF_CORE
2718 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2720 #ifdef CONFIG_TASK_IO_ACCOUNTING
2721 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2723 #ifdef CONFIG_HARDWALL
2724 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2726 #ifdef CONFIG_USER_NS
2727 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2728 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2729 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2733 static int proc_tgid_base_readdir(struct file
* filp
,
2734 void * dirent
, filldir_t filldir
)
2736 return proc_pident_readdir(filp
,dirent
,filldir
,
2737 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2740 static const struct file_operations proc_tgid_base_operations
= {
2741 .read
= generic_read_dir
,
2742 .readdir
= proc_tgid_base_readdir
,
2743 .llseek
= default_llseek
,
2746 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2748 return proc_pident_lookup(dir
, dentry
,
2749 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2752 static const struct inode_operations proc_tgid_base_inode_operations
= {
2753 .lookup
= proc_tgid_base_lookup
,
2754 .getattr
= pid_getattr
,
2755 .setattr
= proc_setattr
,
2756 .permission
= proc_pid_permission
,
2759 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2761 struct dentry
*dentry
, *leader
, *dir
;
2762 char buf
[PROC_NUMBUF
];
2766 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2767 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2769 shrink_dcache_parent(dentry
);
2775 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2776 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2781 name
.len
= strlen(name
.name
);
2782 dir
= d_hash_and_lookup(leader
, &name
);
2784 goto out_put_leader
;
2787 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2788 dentry
= d_hash_and_lookup(dir
, &name
);
2790 shrink_dcache_parent(dentry
);
2803 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2804 * @task: task that should be flushed.
2806 * When flushing dentries from proc, one needs to flush them from global
2807 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2808 * in. This call is supposed to do all of this job.
2810 * Looks in the dcache for
2812 * /proc/@tgid/task/@pid
2813 * if either directory is present flushes it and all of it'ts children
2816 * It is safe and reasonable to cache /proc entries for a task until
2817 * that task exits. After that they just clog up the dcache with
2818 * useless entries, possibly causing useful dcache entries to be
2819 * flushed instead. This routine is proved to flush those useless
2820 * dcache entries at process exit time.
2822 * NOTE: This routine is just an optimization so it does not guarantee
2823 * that no dcache entries will exist at process exit time it
2824 * just makes it very unlikely that any will persist.
2827 void proc_flush_task(struct task_struct
*task
)
2830 struct pid
*pid
, *tgid
;
2833 pid
= task_pid(task
);
2834 tgid
= task_tgid(task
);
2836 for (i
= 0; i
<= pid
->level
; i
++) {
2837 upid
= &pid
->numbers
[i
];
2838 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2839 tgid
->numbers
[i
].nr
);
2842 upid
= &pid
->numbers
[pid
->level
];
2844 pid_ns_release_proc(upid
->ns
);
2847 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2848 struct dentry
* dentry
,
2849 struct task_struct
*task
, const void *ptr
)
2851 struct dentry
*error
= ERR_PTR(-ENOENT
);
2852 struct inode
*inode
;
2854 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2858 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2859 inode
->i_op
= &proc_tgid_base_inode_operations
;
2860 inode
->i_fop
= &proc_tgid_base_operations
;
2861 inode
->i_flags
|=S_IMMUTABLE
;
2863 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2864 ARRAY_SIZE(tgid_base_stuff
)));
2866 d_set_d_op(dentry
, &pid_dentry_operations
);
2868 d_add(dentry
, inode
);
2869 /* Close the race of the process dying before we return the dentry */
2870 if (pid_revalidate(dentry
, 0))
2876 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2878 struct dentry
*result
;
2879 struct task_struct
*task
;
2881 struct pid_namespace
*ns
;
2883 result
= proc_base_lookup(dir
, dentry
);
2884 if (!IS_ERR(result
) || PTR_ERR(result
) != -ENOENT
)
2887 tgid
= name_to_int(dentry
);
2891 ns
= dentry
->d_sb
->s_fs_info
;
2893 task
= find_task_by_pid_ns(tgid
, ns
);
2895 get_task_struct(task
);
2900 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2901 put_task_struct(task
);
2907 * Find the first task with tgid >= tgid
2912 struct task_struct
*task
;
2914 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2919 put_task_struct(iter
.task
);
2923 pid
= find_ge_pid(iter
.tgid
, ns
);
2925 iter
.tgid
= pid_nr_ns(pid
, ns
);
2926 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2927 /* What we to know is if the pid we have find is the
2928 * pid of a thread_group_leader. Testing for task
2929 * being a thread_group_leader is the obvious thing
2930 * todo but there is a window when it fails, due to
2931 * the pid transfer logic in de_thread.
2933 * So we perform the straight forward test of seeing
2934 * if the pid we have found is the pid of a thread
2935 * group leader, and don't worry if the task we have
2936 * found doesn't happen to be a thread group leader.
2937 * As we don't care in the case of readdir.
2939 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2943 get_task_struct(iter
.task
);
2949 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
2951 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2952 struct tgid_iter iter
)
2954 char name
[PROC_NUMBUF
];
2955 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2956 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2957 proc_pid_instantiate
, iter
.task
, NULL
);
2960 static int fake_filldir(void *buf
, const char *name
, int namelen
,
2961 loff_t offset
, u64 ino
, unsigned d_type
)
2966 /* for the /proc/ directory itself, after non-process stuff has been done */
2967 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2970 struct task_struct
*reaper
;
2971 struct tgid_iter iter
;
2972 struct pid_namespace
*ns
;
2973 filldir_t __filldir
;
2975 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
2977 nr
= filp
->f_pos
- FIRST_PROCESS_ENTRY
;
2979 reaper
= get_proc_task(filp
->f_path
.dentry
->d_inode
);
2983 for (; nr
< ARRAY_SIZE(proc_base_stuff
); filp
->f_pos
++, nr
++) {
2984 const struct pid_entry
*p
= &proc_base_stuff
[nr
];
2985 if (proc_base_fill_cache(filp
, dirent
, filldir
, reaper
, p
) < 0)
2989 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2991 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2992 for (iter
= next_tgid(ns
, iter
);
2994 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2995 if (has_pid_permissions(ns
, iter
.task
, 2))
2996 __filldir
= filldir
;
2998 __filldir
= fake_filldir
;
3000 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
3001 if (proc_pid_fill_cache(filp
, dirent
, __filldir
, iter
) < 0) {
3002 put_task_struct(iter
.task
);
3006 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3008 put_task_struct(reaper
);
3016 static const struct pid_entry tid_base_stuff
[] = {
3017 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3018 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3019 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3020 REG("environ", S_IRUSR
, proc_environ_operations
),
3021 INF("auxv", S_IRUSR
, proc_pid_auxv
),
3022 ONE("status", S_IRUGO
, proc_pid_status
),
3023 ONE("personality", S_IRUGO
, proc_pid_personality
),
3024 INF("limits", S_IRUGO
, proc_pid_limits
),
3025 #ifdef CONFIG_SCHED_DEBUG
3026 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3028 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
3029 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3030 INF("syscall", S_IRUGO
, proc_pid_syscall
),
3032 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
3033 ONE("stat", S_IRUGO
, proc_tid_stat
),
3034 ONE("statm", S_IRUGO
, proc_pid_statm
),
3035 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3036 #ifdef CONFIG_CHECKPOINT_RESTORE
3037 REG("children", S_IRUGO
, proc_tid_children_operations
),
3040 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3042 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3043 LNK("cwd", proc_cwd_link
),
3044 LNK("root", proc_root_link
),
3045 LNK("exe", proc_exe_link
),
3046 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3047 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3048 #ifdef CONFIG_PROC_PAGE_MONITOR
3049 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3050 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3051 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
3053 #ifdef CONFIG_SECURITY
3054 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3056 #ifdef CONFIG_KALLSYMS
3057 INF("wchan", S_IRUGO
, proc_pid_wchan
),
3059 #ifdef CONFIG_STACKTRACE
3060 ONE("stack", S_IRUGO
, proc_pid_stack
),
3062 #ifdef CONFIG_SCHEDSTATS
3063 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
3065 #ifdef CONFIG_LATENCYTOP
3066 REG("latency", S_IRUGO
, proc_lstats_operations
),
3068 #ifdef CONFIG_PROC_PID_CPUSET
3069 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
3071 #ifdef CONFIG_CGROUPS
3072 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
3074 INF("oom_score", S_IRUGO
, proc_oom_score
),
3075 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3076 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3077 #ifdef CONFIG_AUDITSYSCALL
3078 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3079 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3081 #ifdef CONFIG_FAULT_INJECTION
3082 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3084 #ifdef CONFIG_TASK_IO_ACCOUNTING
3085 INF("io", S_IRUSR
, proc_tid_io_accounting
),
3087 #ifdef CONFIG_HARDWALL
3088 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
3090 #ifdef CONFIG_USER_NS
3091 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3092 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3093 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3097 static int proc_tid_base_readdir(struct file
* filp
,
3098 void * dirent
, filldir_t filldir
)
3100 return proc_pident_readdir(filp
,dirent
,filldir
,
3101 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
3104 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3106 return proc_pident_lookup(dir
, dentry
,
3107 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3110 static const struct file_operations proc_tid_base_operations
= {
3111 .read
= generic_read_dir
,
3112 .readdir
= proc_tid_base_readdir
,
3113 .llseek
= default_llseek
,
3116 static const struct inode_operations proc_tid_base_inode_operations
= {
3117 .lookup
= proc_tid_base_lookup
,
3118 .getattr
= pid_getattr
,
3119 .setattr
= proc_setattr
,
3122 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
3123 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3125 struct dentry
*error
= ERR_PTR(-ENOENT
);
3126 struct inode
*inode
;
3127 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
3131 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
3132 inode
->i_op
= &proc_tid_base_inode_operations
;
3133 inode
->i_fop
= &proc_tid_base_operations
;
3134 inode
->i_flags
|=S_IMMUTABLE
;
3136 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
3137 ARRAY_SIZE(tid_base_stuff
)));
3139 d_set_d_op(dentry
, &pid_dentry_operations
);
3141 d_add(dentry
, inode
);
3142 /* Close the race of the process dying before we return the dentry */
3143 if (pid_revalidate(dentry
, 0))
3149 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3151 struct dentry
*result
= ERR_PTR(-ENOENT
);
3152 struct task_struct
*task
;
3153 struct task_struct
*leader
= get_proc_task(dir
);
3155 struct pid_namespace
*ns
;
3160 tid
= name_to_int(dentry
);
3164 ns
= dentry
->d_sb
->s_fs_info
;
3166 task
= find_task_by_pid_ns(tid
, ns
);
3168 get_task_struct(task
);
3172 if (!same_thread_group(leader
, task
))
3175 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3177 put_task_struct(task
);
3179 put_task_struct(leader
);
3185 * Find the first tid of a thread group to return to user space.
3187 * Usually this is just the thread group leader, but if the users
3188 * buffer was too small or there was a seek into the middle of the
3189 * directory we have more work todo.
3191 * In the case of a short read we start with find_task_by_pid.
3193 * In the case of a seek we start with the leader and walk nr
3196 static struct task_struct
*first_tid(struct task_struct
*leader
,
3197 int tid
, int nr
, struct pid_namespace
*ns
)
3199 struct task_struct
*pos
;
3202 /* Attempt to start with the pid of a thread */
3203 if (tid
&& (nr
> 0)) {
3204 pos
= find_task_by_pid_ns(tid
, ns
);
3205 if (pos
&& (pos
->group_leader
== leader
))
3209 /* If nr exceeds the number of threads there is nothing todo */
3211 if (nr
&& nr
>= get_nr_threads(leader
))
3214 /* If we haven't found our starting place yet start
3215 * with the leader and walk nr threads forward.
3217 for (pos
= leader
; nr
> 0; --nr
) {
3218 pos
= next_thread(pos
);
3219 if (pos
== leader
) {
3225 get_task_struct(pos
);
3232 * Find the next thread in the thread list.
3233 * Return NULL if there is an error or no next thread.
3235 * The reference to the input task_struct is released.
3237 static struct task_struct
*next_tid(struct task_struct
*start
)
3239 struct task_struct
*pos
= NULL
;
3241 if (pid_alive(start
)) {
3242 pos
= next_thread(start
);
3243 if (thread_group_leader(pos
))
3246 get_task_struct(pos
);
3249 put_task_struct(start
);
3253 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3254 struct task_struct
*task
, int tid
)
3256 char name
[PROC_NUMBUF
];
3257 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3258 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3259 proc_task_instantiate
, task
, NULL
);
3262 /* for the /proc/TGID/task/ directories */
3263 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3265 struct dentry
*dentry
= filp
->f_path
.dentry
;
3266 struct inode
*inode
= dentry
->d_inode
;
3267 struct task_struct
*leader
= NULL
;
3268 struct task_struct
*task
;
3269 int retval
= -ENOENT
;
3272 struct pid_namespace
*ns
;
3274 task
= get_proc_task(inode
);
3278 if (pid_alive(task
)) {
3279 leader
= task
->group_leader
;
3280 get_task_struct(leader
);
3283 put_task_struct(task
);
3288 switch ((unsigned long)filp
->f_pos
) {
3291 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3296 ino
= parent_ino(dentry
);
3297 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3303 /* f_version caches the tgid value that the last readdir call couldn't
3304 * return. lseek aka telldir automagically resets f_version to 0.
3306 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3307 tid
= (int)filp
->f_version
;
3308 filp
->f_version
= 0;
3309 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3311 task
= next_tid(task
), filp
->f_pos
++) {
3312 tid
= task_pid_nr_ns(task
, ns
);
3313 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3314 /* returning this tgid failed, save it as the first
3315 * pid for the next readir call */
3316 filp
->f_version
= (u64
)tid
;
3317 put_task_struct(task
);
3322 put_task_struct(leader
);
3327 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3329 struct inode
*inode
= dentry
->d_inode
;
3330 struct task_struct
*p
= get_proc_task(inode
);
3331 generic_fillattr(inode
, stat
);
3334 stat
->nlink
+= get_nr_threads(p
);
3341 static const struct inode_operations proc_task_inode_operations
= {
3342 .lookup
= proc_task_lookup
,
3343 .getattr
= proc_task_getattr
,
3344 .setattr
= proc_setattr
,
3345 .permission
= proc_pid_permission
,
3348 static const struct file_operations proc_task_operations
= {
3349 .read
= generic_read_dir
,
3350 .readdir
= proc_task_readdir
,
3351 .llseek
= default_llseek
,