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_inode(file
));
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 setattr_copy(inode
, attr
);
546 mark_inode_dirty(inode
);
551 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
552 * or euid/egid (for hide_pid_min=2)?
554 static bool has_pid_permissions(struct pid_namespace
*pid
,
555 struct task_struct
*task
,
558 if (pid
->hide_pid
< hide_pid_min
)
560 if (in_group_p(pid
->pid_gid
))
562 return ptrace_may_access(task
, PTRACE_MODE_READ
);
566 static int proc_pid_permission(struct inode
*inode
, int mask
)
568 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
569 struct task_struct
*task
;
572 task
= get_proc_task(inode
);
575 has_perms
= has_pid_permissions(pid
, task
, 1);
576 put_task_struct(task
);
579 if (pid
->hide_pid
== 2) {
581 * Let's make getdents(), stat(), and open()
582 * consistent with each other. If a process
583 * may not stat() a file, it shouldn't be seen
591 return generic_permission(inode
, mask
);
596 static const struct inode_operations proc_def_inode_operations
= {
597 .setattr
= proc_setattr
,
600 #define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
602 static ssize_t
proc_info_read(struct file
* file
, char __user
* buf
,
603 size_t count
, loff_t
*ppos
)
605 struct inode
* inode
= file_inode(file
);
608 struct task_struct
*task
= get_proc_task(inode
);
614 if (count
> PROC_BLOCK_SIZE
)
615 count
= PROC_BLOCK_SIZE
;
618 if (!(page
= __get_free_page(GFP_TEMPORARY
)))
621 length
= PROC_I(inode
)->op
.proc_read(task
, (char*)page
);
624 length
= simple_read_from_buffer(buf
, count
, ppos
, (char *)page
, length
);
627 put_task_struct(task
);
632 static const struct file_operations proc_info_file_operations
= {
633 .read
= proc_info_read
,
634 .llseek
= generic_file_llseek
,
637 static int proc_single_show(struct seq_file
*m
, void *v
)
639 struct inode
*inode
= m
->private;
640 struct pid_namespace
*ns
;
642 struct task_struct
*task
;
645 ns
= inode
->i_sb
->s_fs_info
;
646 pid
= proc_pid(inode
);
647 task
= get_pid_task(pid
, PIDTYPE_PID
);
651 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
653 put_task_struct(task
);
657 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
659 return single_open(filp
, proc_single_show
, inode
);
662 static const struct file_operations proc_single_file_operations
= {
663 .open
= proc_single_open
,
666 .release
= single_release
,
669 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
671 struct task_struct
*task
= get_proc_task(file_inode(file
));
672 struct mm_struct
*mm
;
677 mm
= mm_access(task
, mode
);
678 put_task_struct(task
);
684 /* ensure this mm_struct can't be freed */
685 atomic_inc(&mm
->mm_count
);
686 /* but do not pin its memory */
690 file
->private_data
= mm
;
695 static int mem_open(struct inode
*inode
, struct file
*file
)
697 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
699 /* OK to pass negative loff_t, we can catch out-of-range */
700 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
705 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
706 size_t count
, loff_t
*ppos
, int write
)
708 struct mm_struct
*mm
= file
->private_data
;
709 unsigned long addr
= *ppos
;
716 page
= (char *)__get_free_page(GFP_TEMPORARY
);
721 if (!atomic_inc_not_zero(&mm
->mm_users
))
725 int this_len
= min_t(int, count
, PAGE_SIZE
);
727 if (write
&& copy_from_user(page
, buf
, this_len
)) {
732 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, write
);
739 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
753 free_page((unsigned long) page
);
757 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
758 size_t count
, loff_t
*ppos
)
760 return mem_rw(file
, buf
, count
, ppos
, 0);
763 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
764 size_t count
, loff_t
*ppos
)
766 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
769 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
773 file
->f_pos
= offset
;
776 file
->f_pos
+= offset
;
781 force_successful_syscall_return();
785 static int mem_release(struct inode
*inode
, struct file
*file
)
787 struct mm_struct
*mm
= file
->private_data
;
793 static const struct file_operations proc_mem_operations
= {
798 .release
= mem_release
,
801 static int environ_open(struct inode
*inode
, struct file
*file
)
803 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
806 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
807 size_t count
, loff_t
*ppos
)
810 unsigned long src
= *ppos
;
812 struct mm_struct
*mm
= file
->private_data
;
817 page
= (char *)__get_free_page(GFP_TEMPORARY
);
822 if (!atomic_inc_not_zero(&mm
->mm_users
))
825 size_t this_len
, max_len
;
828 if (src
>= (mm
->env_end
- mm
->env_start
))
831 this_len
= mm
->env_end
- (mm
->env_start
+ src
);
833 max_len
= min_t(size_t, PAGE_SIZE
, count
);
834 this_len
= min(max_len
, this_len
);
836 retval
= access_remote_vm(mm
, (mm
->env_start
+ src
),
844 if (copy_to_user(buf
, page
, retval
)) {
858 free_page((unsigned long) page
);
862 static const struct file_operations proc_environ_operations
= {
863 .open
= environ_open
,
864 .read
= environ_read
,
865 .llseek
= generic_file_llseek
,
866 .release
= mem_release
,
869 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
872 struct task_struct
*task
= get_proc_task(file_inode(file
));
873 char buffer
[PROC_NUMBUF
];
874 int oom_adj
= OOM_ADJUST_MIN
;
880 if (lock_task_sighand(task
, &flags
)) {
881 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
882 oom_adj
= OOM_ADJUST_MAX
;
884 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
886 unlock_task_sighand(task
, &flags
);
888 put_task_struct(task
);
889 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
890 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
893 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
894 size_t count
, loff_t
*ppos
)
896 struct task_struct
*task
;
897 char buffer
[PROC_NUMBUF
];
902 memset(buffer
, 0, sizeof(buffer
));
903 if (count
> sizeof(buffer
) - 1)
904 count
= sizeof(buffer
) - 1;
905 if (copy_from_user(buffer
, buf
, count
)) {
910 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
913 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
914 oom_adj
!= OOM_DISABLE
) {
919 task
= get_proc_task(file_inode(file
));
931 if (!lock_task_sighand(task
, &flags
)) {
937 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
938 * value is always attainable.
940 if (oom_adj
== OOM_ADJUST_MAX
)
941 oom_adj
= OOM_SCORE_ADJ_MAX
;
943 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
945 if (oom_adj
< task
->signal
->oom_score_adj
&&
946 !capable(CAP_SYS_RESOURCE
)) {
952 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
953 * /proc/pid/oom_score_adj instead.
955 printk_once(KERN_WARNING
"%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
956 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
959 task
->signal
->oom_score_adj
= oom_adj
;
960 trace_oom_score_adj_update(task
);
962 unlock_task_sighand(task
, &flags
);
965 put_task_struct(task
);
967 return err
< 0 ? err
: count
;
970 static const struct file_operations proc_oom_adj_operations
= {
971 .read
= oom_adj_read
,
972 .write
= oom_adj_write
,
973 .llseek
= generic_file_llseek
,
976 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
977 size_t count
, loff_t
*ppos
)
979 struct task_struct
*task
= get_proc_task(file_inode(file
));
980 char buffer
[PROC_NUMBUF
];
981 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
987 if (lock_task_sighand(task
, &flags
)) {
988 oom_score_adj
= task
->signal
->oom_score_adj
;
989 unlock_task_sighand(task
, &flags
);
991 put_task_struct(task
);
992 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
993 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
996 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
997 size_t count
, loff_t
*ppos
)
999 struct task_struct
*task
;
1000 char buffer
[PROC_NUMBUF
];
1001 unsigned long flags
;
1005 memset(buffer
, 0, sizeof(buffer
));
1006 if (count
> sizeof(buffer
) - 1)
1007 count
= sizeof(buffer
) - 1;
1008 if (copy_from_user(buffer
, buf
, count
)) {
1013 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1016 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1017 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1022 task
= get_proc_task(file_inode(file
));
1034 if (!lock_task_sighand(task
, &flags
)) {
1039 if ((short)oom_score_adj
< task
->signal
->oom_score_adj_min
&&
1040 !capable(CAP_SYS_RESOURCE
)) {
1045 task
->signal
->oom_score_adj
= (short)oom_score_adj
;
1046 if (has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1047 task
->signal
->oom_score_adj_min
= (short)oom_score_adj
;
1048 trace_oom_score_adj_update(task
);
1051 unlock_task_sighand(task
, &flags
);
1054 put_task_struct(task
);
1056 return err
< 0 ? err
: count
;
1059 static const struct file_operations proc_oom_score_adj_operations
= {
1060 .read
= oom_score_adj_read
,
1061 .write
= oom_score_adj_write
,
1062 .llseek
= default_llseek
,
1065 #ifdef CONFIG_AUDITSYSCALL
1066 #define TMPBUFLEN 21
1067 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1068 size_t count
, loff_t
*ppos
)
1070 struct inode
* inode
= file_inode(file
);
1071 struct task_struct
*task
= get_proc_task(inode
);
1073 char tmpbuf
[TMPBUFLEN
];
1077 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1078 from_kuid(file
->f_cred
->user_ns
,
1079 audit_get_loginuid(task
)));
1080 put_task_struct(task
);
1081 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1084 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1085 size_t count
, loff_t
*ppos
)
1087 struct inode
* inode
= file_inode(file
);
1094 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1100 if (count
>= PAGE_SIZE
)
1101 count
= PAGE_SIZE
- 1;
1104 /* No partial writes. */
1107 page
= (char*)__get_free_page(GFP_TEMPORARY
);
1111 if (copy_from_user(page
, buf
, count
))
1115 loginuid
= simple_strtoul(page
, &tmp
, 10);
1121 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1122 if (!uid_valid(kloginuid
)) {
1127 length
= audit_set_loginuid(kloginuid
);
1128 if (likely(length
== 0))
1132 free_page((unsigned long) page
);
1136 static const struct file_operations proc_loginuid_operations
= {
1137 .read
= proc_loginuid_read
,
1138 .write
= proc_loginuid_write
,
1139 .llseek
= generic_file_llseek
,
1142 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1143 size_t count
, loff_t
*ppos
)
1145 struct inode
* inode
= file_inode(file
);
1146 struct task_struct
*task
= get_proc_task(inode
);
1148 char tmpbuf
[TMPBUFLEN
];
1152 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1153 audit_get_sessionid(task
));
1154 put_task_struct(task
);
1155 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1158 static const struct file_operations proc_sessionid_operations
= {
1159 .read
= proc_sessionid_read
,
1160 .llseek
= generic_file_llseek
,
1164 #ifdef CONFIG_FAULT_INJECTION
1165 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1166 size_t count
, loff_t
*ppos
)
1168 struct task_struct
*task
= get_proc_task(file_inode(file
));
1169 char buffer
[PROC_NUMBUF
];
1175 make_it_fail
= task
->make_it_fail
;
1176 put_task_struct(task
);
1178 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1180 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1183 static ssize_t
proc_fault_inject_write(struct file
* file
,
1184 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1186 struct task_struct
*task
;
1187 char buffer
[PROC_NUMBUF
], *end
;
1190 if (!capable(CAP_SYS_RESOURCE
))
1192 memset(buffer
, 0, sizeof(buffer
));
1193 if (count
> sizeof(buffer
) - 1)
1194 count
= sizeof(buffer
) - 1;
1195 if (copy_from_user(buffer
, buf
, count
))
1197 make_it_fail
= simple_strtol(strstrip(buffer
), &end
, 0);
1200 task
= get_proc_task(file_inode(file
));
1203 task
->make_it_fail
= make_it_fail
;
1204 put_task_struct(task
);
1209 static const struct file_operations proc_fault_inject_operations
= {
1210 .read
= proc_fault_inject_read
,
1211 .write
= proc_fault_inject_write
,
1212 .llseek
= generic_file_llseek
,
1217 #ifdef CONFIG_SCHED_DEBUG
1219 * Print out various scheduling related per-task fields:
1221 static int sched_show(struct seq_file
*m
, void *v
)
1223 struct inode
*inode
= m
->private;
1224 struct task_struct
*p
;
1226 p
= get_proc_task(inode
);
1229 proc_sched_show_task(p
, m
);
1237 sched_write(struct file
*file
, const char __user
*buf
,
1238 size_t count
, loff_t
*offset
)
1240 struct inode
*inode
= file_inode(file
);
1241 struct task_struct
*p
;
1243 p
= get_proc_task(inode
);
1246 proc_sched_set_task(p
);
1253 static int sched_open(struct inode
*inode
, struct file
*filp
)
1255 return single_open(filp
, sched_show
, inode
);
1258 static const struct file_operations proc_pid_sched_operations
= {
1261 .write
= sched_write
,
1262 .llseek
= seq_lseek
,
1263 .release
= single_release
,
1268 #ifdef CONFIG_SCHED_AUTOGROUP
1270 * Print out autogroup related information:
1272 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1274 struct inode
*inode
= m
->private;
1275 struct task_struct
*p
;
1277 p
= get_proc_task(inode
);
1280 proc_sched_autogroup_show_task(p
, m
);
1288 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1289 size_t count
, loff_t
*offset
)
1291 struct inode
*inode
= file_inode(file
);
1292 struct task_struct
*p
;
1293 char buffer
[PROC_NUMBUF
];
1297 memset(buffer
, 0, sizeof(buffer
));
1298 if (count
> sizeof(buffer
) - 1)
1299 count
= sizeof(buffer
) - 1;
1300 if (copy_from_user(buffer
, buf
, count
))
1303 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1307 p
= get_proc_task(inode
);
1311 err
= proc_sched_autogroup_set_nice(p
, nice
);
1320 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1324 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1326 struct seq_file
*m
= filp
->private_data
;
1333 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1334 .open
= sched_autogroup_open
,
1336 .write
= sched_autogroup_write
,
1337 .llseek
= seq_lseek
,
1338 .release
= single_release
,
1341 #endif /* CONFIG_SCHED_AUTOGROUP */
1343 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1344 size_t count
, loff_t
*offset
)
1346 struct inode
*inode
= file_inode(file
);
1347 struct task_struct
*p
;
1348 char buffer
[TASK_COMM_LEN
];
1350 memset(buffer
, 0, sizeof(buffer
));
1351 if (count
> sizeof(buffer
) - 1)
1352 count
= sizeof(buffer
) - 1;
1353 if (copy_from_user(buffer
, buf
, count
))
1356 p
= get_proc_task(inode
);
1360 if (same_thread_group(current
, p
))
1361 set_task_comm(p
, buffer
);
1370 static int comm_show(struct seq_file
*m
, void *v
)
1372 struct inode
*inode
= m
->private;
1373 struct task_struct
*p
;
1375 p
= get_proc_task(inode
);
1380 seq_printf(m
, "%s\n", p
->comm
);
1388 static int comm_open(struct inode
*inode
, struct file
*filp
)
1390 return single_open(filp
, comm_show
, inode
);
1393 static const struct file_operations proc_pid_set_comm_operations
= {
1396 .write
= comm_write
,
1397 .llseek
= seq_lseek
,
1398 .release
= single_release
,
1401 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1403 struct task_struct
*task
;
1404 struct mm_struct
*mm
;
1405 struct file
*exe_file
;
1407 task
= get_proc_task(dentry
->d_inode
);
1410 mm
= get_task_mm(task
);
1411 put_task_struct(task
);
1414 exe_file
= get_mm_exe_file(mm
);
1417 *exe_path
= exe_file
->f_path
;
1418 path_get(&exe_file
->f_path
);
1425 static void *proc_pid_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
1427 struct inode
*inode
= dentry
->d_inode
;
1429 int error
= -EACCES
;
1431 /* Are we allowed to snoop on the tasks file descriptors? */
1432 if (!proc_fd_access_allowed(inode
))
1435 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1439 nd_jump_link(nd
, &path
);
1442 return ERR_PTR(error
);
1445 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1447 char *tmp
= (char*)__get_free_page(GFP_TEMPORARY
);
1454 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1455 len
= PTR_ERR(pathname
);
1456 if (IS_ERR(pathname
))
1458 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1462 if (copy_to_user(buffer
, pathname
, len
))
1465 free_page((unsigned long)tmp
);
1469 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1471 int error
= -EACCES
;
1472 struct inode
*inode
= dentry
->d_inode
;
1475 /* Are we allowed to snoop on the tasks file descriptors? */
1476 if (!proc_fd_access_allowed(inode
))
1479 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1483 error
= do_proc_readlink(&path
, buffer
, buflen
);
1489 const struct inode_operations proc_pid_link_inode_operations
= {
1490 .readlink
= proc_pid_readlink
,
1491 .follow_link
= proc_pid_follow_link
,
1492 .setattr
= proc_setattr
,
1496 /* building an inode */
1498 struct inode
*proc_pid_make_inode(struct super_block
* sb
, struct task_struct
*task
)
1500 struct inode
* inode
;
1501 struct proc_inode
*ei
;
1502 const struct cred
*cred
;
1504 /* We need a new inode */
1506 inode
= new_inode(sb
);
1512 inode
->i_ino
= get_next_ino();
1513 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
1514 inode
->i_op
= &proc_def_inode_operations
;
1517 * grab the reference to task.
1519 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1523 if (task_dumpable(task
)) {
1525 cred
= __task_cred(task
);
1526 inode
->i_uid
= cred
->euid
;
1527 inode
->i_gid
= cred
->egid
;
1530 security_task_to_inode(task
, inode
);
1540 int pid_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
1542 struct inode
*inode
= dentry
->d_inode
;
1543 struct task_struct
*task
;
1544 const struct cred
*cred
;
1545 struct pid_namespace
*pid
= dentry
->d_sb
->s_fs_info
;
1547 generic_fillattr(inode
, stat
);
1550 stat
->uid
= GLOBAL_ROOT_UID
;
1551 stat
->gid
= GLOBAL_ROOT_GID
;
1552 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1554 if (!has_pid_permissions(pid
, task
, 2)) {
1557 * This doesn't prevent learning whether PID exists,
1558 * it only makes getattr() consistent with readdir().
1562 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1563 task_dumpable(task
)) {
1564 cred
= __task_cred(task
);
1565 stat
->uid
= cred
->euid
;
1566 stat
->gid
= cred
->egid
;
1576 * Exceptional case: normally we are not allowed to unhash a busy
1577 * directory. In this case, however, we can do it - no aliasing problems
1578 * due to the way we treat inodes.
1580 * Rewrite the inode's ownerships here because the owning task may have
1581 * performed a setuid(), etc.
1583 * Before the /proc/pid/status file was created the only way to read
1584 * the effective uid of a /process was to stat /proc/pid. Reading
1585 * /proc/pid/status is slow enough that procps and other packages
1586 * kept stating /proc/pid. To keep the rules in /proc simple I have
1587 * made this apply to all per process world readable and executable
1590 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1592 struct inode
*inode
;
1593 struct task_struct
*task
;
1594 const struct cred
*cred
;
1596 if (flags
& LOOKUP_RCU
)
1599 inode
= dentry
->d_inode
;
1600 task
= get_proc_task(inode
);
1603 if ((inode
->i_mode
== (S_IFDIR
|S_IRUGO
|S_IXUGO
)) ||
1604 task_dumpable(task
)) {
1606 cred
= __task_cred(task
);
1607 inode
->i_uid
= cred
->euid
;
1608 inode
->i_gid
= cred
->egid
;
1611 inode
->i_uid
= GLOBAL_ROOT_UID
;
1612 inode
->i_gid
= GLOBAL_ROOT_GID
;
1614 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1615 security_task_to_inode(task
, inode
);
1616 put_task_struct(task
);
1623 const struct dentry_operations pid_dentry_operations
=
1625 .d_revalidate
= pid_revalidate
,
1626 .d_delete
= pid_delete_dentry
,
1632 * Fill a directory entry.
1634 * If possible create the dcache entry and derive our inode number and
1635 * file type from dcache entry.
1637 * Since all of the proc inode numbers are dynamically generated, the inode
1638 * numbers do not exist until the inode is cache. This means creating the
1639 * the dcache entry in readdir is necessary to keep the inode numbers
1640 * reported by readdir in sync with the inode numbers reported
1643 int proc_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
1644 const char *name
, int len
,
1645 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1647 struct dentry
*child
, *dir
= filp
->f_path
.dentry
;
1648 struct inode
*inode
;
1651 unsigned type
= DT_UNKNOWN
;
1655 qname
.hash
= full_name_hash(name
, len
);
1657 child
= d_lookup(dir
, &qname
);
1660 new = d_alloc(dir
, &qname
);
1662 child
= instantiate(dir
->d_inode
, new, task
, ptr
);
1669 if (!child
|| IS_ERR(child
) || !child
->d_inode
)
1670 goto end_instantiate
;
1671 inode
= child
->d_inode
;
1674 type
= inode
->i_mode
>> 12;
1679 ino
= find_inode_number(dir
, &qname
);
1682 return filldir(dirent
, name
, len
, filp
->f_pos
, ino
, type
);
1685 #ifdef CONFIG_CHECKPOINT_RESTORE
1688 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1689 * which represent vma start and end addresses.
1691 static int dname_to_vma_addr(struct dentry
*dentry
,
1692 unsigned long *start
, unsigned long *end
)
1694 if (sscanf(dentry
->d_name
.name
, "%lx-%lx", start
, end
) != 2)
1700 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1702 unsigned long vm_start
, vm_end
;
1703 bool exact_vma_exists
= false;
1704 struct mm_struct
*mm
= NULL
;
1705 struct task_struct
*task
;
1706 const struct cred
*cred
;
1707 struct inode
*inode
;
1710 if (flags
& LOOKUP_RCU
)
1713 if (!capable(CAP_SYS_ADMIN
)) {
1718 inode
= dentry
->d_inode
;
1719 task
= get_proc_task(inode
);
1723 mm
= mm_access(task
, PTRACE_MODE_READ
);
1724 if (IS_ERR_OR_NULL(mm
))
1727 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1728 down_read(&mm
->mmap_sem
);
1729 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1730 up_read(&mm
->mmap_sem
);
1735 if (exact_vma_exists
) {
1736 if (task_dumpable(task
)) {
1738 cred
= __task_cred(task
);
1739 inode
->i_uid
= cred
->euid
;
1740 inode
->i_gid
= cred
->egid
;
1743 inode
->i_uid
= GLOBAL_ROOT_UID
;
1744 inode
->i_gid
= GLOBAL_ROOT_GID
;
1746 security_task_to_inode(task
, inode
);
1751 put_task_struct(task
);
1760 static const struct dentry_operations tid_map_files_dentry_operations
= {
1761 .d_revalidate
= map_files_d_revalidate
,
1762 .d_delete
= pid_delete_dentry
,
1765 static int proc_map_files_get_link(struct dentry
*dentry
, struct path
*path
)
1767 unsigned long vm_start
, vm_end
;
1768 struct vm_area_struct
*vma
;
1769 struct task_struct
*task
;
1770 struct mm_struct
*mm
;
1774 task
= get_proc_task(dentry
->d_inode
);
1778 mm
= get_task_mm(task
);
1779 put_task_struct(task
);
1783 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
1787 down_read(&mm
->mmap_sem
);
1788 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1789 if (vma
&& vma
->vm_file
) {
1790 *path
= vma
->vm_file
->f_path
;
1794 up_read(&mm
->mmap_sem
);
1802 struct map_files_info
{
1805 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1808 static struct dentry
*
1809 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
1810 struct task_struct
*task
, const void *ptr
)
1812 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
1813 struct proc_inode
*ei
;
1814 struct inode
*inode
;
1816 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
1818 return ERR_PTR(-ENOENT
);
1821 ei
->op
.proc_get_link
= proc_map_files_get_link
;
1823 inode
->i_op
= &proc_pid_link_inode_operations
;
1825 inode
->i_mode
= S_IFLNK
;
1827 if (mode
& FMODE_READ
)
1828 inode
->i_mode
|= S_IRUSR
;
1829 if (mode
& FMODE_WRITE
)
1830 inode
->i_mode
|= S_IWUSR
;
1832 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
1833 d_add(dentry
, inode
);
1838 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
1839 struct dentry
*dentry
, unsigned int flags
)
1841 unsigned long vm_start
, vm_end
;
1842 struct vm_area_struct
*vma
;
1843 struct task_struct
*task
;
1844 struct dentry
*result
;
1845 struct mm_struct
*mm
;
1847 result
= ERR_PTR(-EACCES
);
1848 if (!capable(CAP_SYS_ADMIN
))
1851 result
= ERR_PTR(-ENOENT
);
1852 task
= get_proc_task(dir
);
1856 result
= ERR_PTR(-EACCES
);
1857 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1860 result
= ERR_PTR(-ENOENT
);
1861 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
1864 mm
= get_task_mm(task
);
1868 down_read(&mm
->mmap_sem
);
1869 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
1874 result
= proc_map_files_instantiate(dir
, dentry
, task
,
1875 (void *)(unsigned long)vma
->vm_file
->f_mode
);
1878 up_read(&mm
->mmap_sem
);
1881 put_task_struct(task
);
1886 static const struct inode_operations proc_map_files_inode_operations
= {
1887 .lookup
= proc_map_files_lookup
,
1888 .permission
= proc_fd_permission
,
1889 .setattr
= proc_setattr
,
1893 proc_map_files_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
1895 struct dentry
*dentry
= filp
->f_path
.dentry
;
1896 struct inode
*inode
= dentry
->d_inode
;
1897 struct vm_area_struct
*vma
;
1898 struct task_struct
*task
;
1899 struct mm_struct
*mm
;
1904 if (!capable(CAP_SYS_ADMIN
))
1908 task
= get_proc_task(inode
);
1913 if (!ptrace_may_access(task
, PTRACE_MODE_READ
))
1917 switch (filp
->f_pos
) {
1920 if (filldir(dirent
, ".", 1, 0, ino
, DT_DIR
) < 0)
1924 ino
= parent_ino(dentry
);
1925 if (filldir(dirent
, "..", 2, 1, ino
, DT_DIR
) < 0)
1930 unsigned long nr_files
, pos
, i
;
1931 struct flex_array
*fa
= NULL
;
1932 struct map_files_info info
;
1933 struct map_files_info
*p
;
1935 mm
= get_task_mm(task
);
1938 down_read(&mm
->mmap_sem
);
1943 * We need two passes here:
1945 * 1) Collect vmas of mapped files with mmap_sem taken
1946 * 2) Release mmap_sem and instantiate entries
1948 * otherwise we get lockdep complained, since filldir()
1949 * routine might require mmap_sem taken in might_fault().
1952 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
1953 if (vma
->vm_file
&& ++pos
> filp
->f_pos
)
1958 fa
= flex_array_alloc(sizeof(info
), nr_files
,
1960 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
1964 flex_array_free(fa
);
1965 up_read(&mm
->mmap_sem
);
1969 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
1970 vma
= vma
->vm_next
) {
1973 if (++pos
<= filp
->f_pos
)
1976 info
.mode
= vma
->vm_file
->f_mode
;
1977 info
.len
= snprintf(info
.name
,
1978 sizeof(info
.name
), "%lx-%lx",
1979 vma
->vm_start
, vma
->vm_end
);
1980 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
1984 up_read(&mm
->mmap_sem
);
1986 for (i
= 0; i
< nr_files
; i
++) {
1987 p
= flex_array_get(fa
, i
);
1988 ret
= proc_fill_cache(filp
, dirent
, filldir
,
1990 proc_map_files_instantiate
,
1992 (void *)(unsigned long)p
->mode
);
1998 flex_array_free(fa
);
2004 put_task_struct(task
);
2009 static const struct file_operations proc_map_files_operations
= {
2010 .read
= generic_read_dir
,
2011 .readdir
= proc_map_files_readdir
,
2012 .llseek
= default_llseek
,
2015 #endif /* CONFIG_CHECKPOINT_RESTORE */
2017 static struct dentry
*proc_pident_instantiate(struct inode
*dir
,
2018 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2020 const struct pid_entry
*p
= ptr
;
2021 struct inode
*inode
;
2022 struct proc_inode
*ei
;
2023 struct dentry
*error
= ERR_PTR(-ENOENT
);
2025 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2030 inode
->i_mode
= p
->mode
;
2031 if (S_ISDIR(inode
->i_mode
))
2032 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2034 inode
->i_op
= p
->iop
;
2036 inode
->i_fop
= p
->fop
;
2038 d_set_d_op(dentry
, &pid_dentry_operations
);
2039 d_add(dentry
, inode
);
2040 /* Close the race of the process dying before we return the dentry */
2041 if (pid_revalidate(dentry
, 0))
2047 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2048 struct dentry
*dentry
,
2049 const struct pid_entry
*ents
,
2052 struct dentry
*error
;
2053 struct task_struct
*task
= get_proc_task(dir
);
2054 const struct pid_entry
*p
, *last
;
2056 error
= ERR_PTR(-ENOENT
);
2062 * Yes, it does not scale. And it should not. Don't add
2063 * new entries into /proc/<tgid>/ without very good reasons.
2065 last
= &ents
[nents
- 1];
2066 for (p
= ents
; p
<= last
; p
++) {
2067 if (p
->len
!= dentry
->d_name
.len
)
2069 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2075 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2077 put_task_struct(task
);
2082 static int proc_pident_fill_cache(struct file
*filp
, void *dirent
,
2083 filldir_t filldir
, struct task_struct
*task
, const struct pid_entry
*p
)
2085 return proc_fill_cache(filp
, dirent
, filldir
, p
->name
, p
->len
,
2086 proc_pident_instantiate
, task
, p
);
2089 static int proc_pident_readdir(struct file
*filp
,
2090 void *dirent
, filldir_t filldir
,
2091 const struct pid_entry
*ents
, unsigned int nents
)
2094 struct dentry
*dentry
= filp
->f_path
.dentry
;
2095 struct inode
*inode
= dentry
->d_inode
;
2096 struct task_struct
*task
= get_proc_task(inode
);
2097 const struct pid_entry
*p
, *last
;
2110 if (filldir(dirent
, ".", 1, i
, ino
, DT_DIR
) < 0)
2116 ino
= parent_ino(dentry
);
2117 if (filldir(dirent
, "..", 2, i
, ino
, DT_DIR
) < 0)
2129 last
= &ents
[nents
- 1];
2131 if (proc_pident_fill_cache(filp
, dirent
, filldir
, task
, p
) < 0)
2140 put_task_struct(task
);
2145 #ifdef CONFIG_SECURITY
2146 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2147 size_t count
, loff_t
*ppos
)
2149 struct inode
* inode
= file_inode(file
);
2152 struct task_struct
*task
= get_proc_task(inode
);
2157 length
= security_getprocattr(task
,
2158 (char*)file
->f_path
.dentry
->d_name
.name
,
2160 put_task_struct(task
);
2162 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2167 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2168 size_t count
, loff_t
*ppos
)
2170 struct inode
* inode
= file_inode(file
);
2173 struct task_struct
*task
= get_proc_task(inode
);
2178 if (count
> PAGE_SIZE
)
2181 /* No partial writes. */
2187 page
= (char*)__get_free_page(GFP_TEMPORARY
);
2192 if (copy_from_user(page
, buf
, count
))
2195 /* Guard against adverse ptrace interaction */
2196 length
= mutex_lock_interruptible(&task
->signal
->cred_guard_mutex
);
2200 length
= security_setprocattr(task
,
2201 (char*)file
->f_path
.dentry
->d_name
.name
,
2202 (void*)page
, count
);
2203 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2205 free_page((unsigned long) page
);
2207 put_task_struct(task
);
2212 static const struct file_operations proc_pid_attr_operations
= {
2213 .read
= proc_pid_attr_read
,
2214 .write
= proc_pid_attr_write
,
2215 .llseek
= generic_file_llseek
,
2218 static const struct pid_entry attr_dir_stuff
[] = {
2219 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2220 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2221 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2222 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2223 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2224 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2227 static int proc_attr_dir_readdir(struct file
* filp
,
2228 void * dirent
, filldir_t filldir
)
2230 return proc_pident_readdir(filp
,dirent
,filldir
,
2231 attr_dir_stuff
,ARRAY_SIZE(attr_dir_stuff
));
2234 static const struct file_operations proc_attr_dir_operations
= {
2235 .read
= generic_read_dir
,
2236 .readdir
= proc_attr_dir_readdir
,
2237 .llseek
= default_llseek
,
2240 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2241 struct dentry
*dentry
, unsigned int flags
)
2243 return proc_pident_lookup(dir
, dentry
,
2244 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2247 static const struct inode_operations proc_attr_dir_inode_operations
= {
2248 .lookup
= proc_attr_dir_lookup
,
2249 .getattr
= pid_getattr
,
2250 .setattr
= proc_setattr
,
2255 #ifdef CONFIG_ELF_CORE
2256 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2257 size_t count
, loff_t
*ppos
)
2259 struct task_struct
*task
= get_proc_task(file_inode(file
));
2260 struct mm_struct
*mm
;
2261 char buffer
[PROC_NUMBUF
];
2269 mm
= get_task_mm(task
);
2271 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2272 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2273 MMF_DUMP_FILTER_SHIFT
));
2275 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2278 put_task_struct(task
);
2283 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2284 const char __user
*buf
,
2288 struct task_struct
*task
;
2289 struct mm_struct
*mm
;
2290 char buffer
[PROC_NUMBUF
], *end
;
2297 memset(buffer
, 0, sizeof(buffer
));
2298 if (count
> sizeof(buffer
) - 1)
2299 count
= sizeof(buffer
) - 1;
2300 if (copy_from_user(buffer
, buf
, count
))
2304 val
= (unsigned int)simple_strtoul(buffer
, &end
, 0);
2307 if (end
- buffer
== 0)
2311 task
= get_proc_task(file_inode(file
));
2316 mm
= get_task_mm(task
);
2320 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2322 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2324 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2329 put_task_struct(task
);
2334 static const struct file_operations proc_coredump_filter_operations
= {
2335 .read
= proc_coredump_filter_read
,
2336 .write
= proc_coredump_filter_write
,
2337 .llseek
= generic_file_llseek
,
2341 #ifdef CONFIG_TASK_IO_ACCOUNTING
2342 static int do_io_accounting(struct task_struct
*task
, char *buffer
, int whole
)
2344 struct task_io_accounting acct
= task
->ioac
;
2345 unsigned long flags
;
2348 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2352 if (!ptrace_may_access(task
, PTRACE_MODE_READ
)) {
2357 if (whole
&& lock_task_sighand(task
, &flags
)) {
2358 struct task_struct
*t
= task
;
2360 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2361 while_each_thread(task
, t
)
2362 task_io_accounting_add(&acct
, &t
->ioac
);
2364 unlock_task_sighand(task
, &flags
);
2366 result
= sprintf(buffer
,
2371 "read_bytes: %llu\n"
2372 "write_bytes: %llu\n"
2373 "cancelled_write_bytes: %llu\n",
2374 (unsigned long long)acct
.rchar
,
2375 (unsigned long long)acct
.wchar
,
2376 (unsigned long long)acct
.syscr
,
2377 (unsigned long long)acct
.syscw
,
2378 (unsigned long long)acct
.read_bytes
,
2379 (unsigned long long)acct
.write_bytes
,
2380 (unsigned long long)acct
.cancelled_write_bytes
);
2382 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2386 static int proc_tid_io_accounting(struct task_struct
*task
, char *buffer
)
2388 return do_io_accounting(task
, buffer
, 0);
2391 static int proc_tgid_io_accounting(struct task_struct
*task
, char *buffer
)
2393 return do_io_accounting(task
, buffer
, 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 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
,
2476 #endif /* CONFIG_USER_NS */
2478 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2479 struct pid
*pid
, struct task_struct
*task
)
2481 int err
= lock_trace(task
);
2483 seq_printf(m
, "%08x\n", task
->personality
);
2492 static const struct file_operations proc_task_operations
;
2493 static const struct inode_operations proc_task_inode_operations
;
2495 static const struct pid_entry tgid_base_stuff
[] = {
2496 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2497 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2498 #ifdef CONFIG_CHECKPOINT_RESTORE
2499 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2501 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2502 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2504 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2506 REG("environ", S_IRUSR
, proc_environ_operations
),
2507 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2508 ONE("status", S_IRUGO
, proc_pid_status
),
2509 ONE("personality", S_IRUGO
, proc_pid_personality
),
2510 INF("limits", S_IRUGO
, proc_pid_limits
),
2511 #ifdef CONFIG_SCHED_DEBUG
2512 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2514 #ifdef CONFIG_SCHED_AUTOGROUP
2515 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2517 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2518 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2519 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2521 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2522 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2523 ONE("statm", S_IRUGO
, proc_pid_statm
),
2524 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2526 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2528 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2529 LNK("cwd", proc_cwd_link
),
2530 LNK("root", proc_root_link
),
2531 LNK("exe", proc_exe_link
),
2532 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2533 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2534 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2535 #ifdef CONFIG_PROC_PAGE_MONITOR
2536 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2537 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2538 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2540 #ifdef CONFIG_SECURITY
2541 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2543 #ifdef CONFIG_KALLSYMS
2544 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2546 #ifdef CONFIG_STACKTRACE
2547 ONE("stack", S_IRUGO
, proc_pid_stack
),
2549 #ifdef CONFIG_SCHEDSTATS
2550 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2552 #ifdef CONFIG_LATENCYTOP
2553 REG("latency", S_IRUGO
, proc_lstats_operations
),
2555 #ifdef CONFIG_PROC_PID_CPUSET
2556 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2558 #ifdef CONFIG_CGROUPS
2559 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2561 INF("oom_score", S_IRUGO
, proc_oom_score
),
2562 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2563 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2564 #ifdef CONFIG_AUDITSYSCALL
2565 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2566 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2568 #ifdef CONFIG_FAULT_INJECTION
2569 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2571 #ifdef CONFIG_ELF_CORE
2572 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
2574 #ifdef CONFIG_TASK_IO_ACCOUNTING
2575 INF("io", S_IRUSR
, proc_tgid_io_accounting
),
2577 #ifdef CONFIG_HARDWALL
2578 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2580 #ifdef CONFIG_USER_NS
2581 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2582 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2583 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2587 static int proc_tgid_base_readdir(struct file
* filp
,
2588 void * dirent
, filldir_t filldir
)
2590 return proc_pident_readdir(filp
,dirent
,filldir
,
2591 tgid_base_stuff
,ARRAY_SIZE(tgid_base_stuff
));
2594 static const struct file_operations proc_tgid_base_operations
= {
2595 .read
= generic_read_dir
,
2596 .readdir
= proc_tgid_base_readdir
,
2597 .llseek
= default_llseek
,
2600 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2602 return proc_pident_lookup(dir
, dentry
,
2603 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
2606 static const struct inode_operations proc_tgid_base_inode_operations
= {
2607 .lookup
= proc_tgid_base_lookup
,
2608 .getattr
= pid_getattr
,
2609 .setattr
= proc_setattr
,
2610 .permission
= proc_pid_permission
,
2613 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
2615 struct dentry
*dentry
, *leader
, *dir
;
2616 char buf
[PROC_NUMBUF
];
2620 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2621 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2623 shrink_dcache_parent(dentry
);
2629 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
2630 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
2635 name
.len
= strlen(name
.name
);
2636 dir
= d_hash_and_lookup(leader
, &name
);
2638 goto out_put_leader
;
2641 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
2642 dentry
= d_hash_and_lookup(dir
, &name
);
2644 shrink_dcache_parent(dentry
);
2657 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2658 * @task: task that should be flushed.
2660 * When flushing dentries from proc, one needs to flush them from global
2661 * proc (proc_mnt) and from all the namespaces' procs this task was seen
2662 * in. This call is supposed to do all of this job.
2664 * Looks in the dcache for
2666 * /proc/@tgid/task/@pid
2667 * if either directory is present flushes it and all of it'ts children
2670 * It is safe and reasonable to cache /proc entries for a task until
2671 * that task exits. After that they just clog up the dcache with
2672 * useless entries, possibly causing useful dcache entries to be
2673 * flushed instead. This routine is proved to flush those useless
2674 * dcache entries at process exit time.
2676 * NOTE: This routine is just an optimization so it does not guarantee
2677 * that no dcache entries will exist at process exit time it
2678 * just makes it very unlikely that any will persist.
2681 void proc_flush_task(struct task_struct
*task
)
2684 struct pid
*pid
, *tgid
;
2687 pid
= task_pid(task
);
2688 tgid
= task_tgid(task
);
2690 for (i
= 0; i
<= pid
->level
; i
++) {
2691 upid
= &pid
->numbers
[i
];
2692 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
2693 tgid
->numbers
[i
].nr
);
2697 static struct dentry
*proc_pid_instantiate(struct inode
*dir
,
2698 struct dentry
* dentry
,
2699 struct task_struct
*task
, const void *ptr
)
2701 struct dentry
*error
= ERR_PTR(-ENOENT
);
2702 struct inode
*inode
;
2704 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2708 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2709 inode
->i_op
= &proc_tgid_base_inode_operations
;
2710 inode
->i_fop
= &proc_tgid_base_operations
;
2711 inode
->i_flags
|=S_IMMUTABLE
;
2713 set_nlink(inode
, 2 + pid_entry_count_dirs(tgid_base_stuff
,
2714 ARRAY_SIZE(tgid_base_stuff
)));
2716 d_set_d_op(dentry
, &pid_dentry_operations
);
2718 d_add(dentry
, inode
);
2719 /* Close the race of the process dying before we return the dentry */
2720 if (pid_revalidate(dentry
, 0))
2726 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2728 struct dentry
*result
= NULL
;
2729 struct task_struct
*task
;
2731 struct pid_namespace
*ns
;
2733 tgid
= name_to_int(dentry
);
2737 ns
= dentry
->d_sb
->s_fs_info
;
2739 task
= find_task_by_pid_ns(tgid
, ns
);
2741 get_task_struct(task
);
2746 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
2747 put_task_struct(task
);
2753 * Find the first task with tgid >= tgid
2758 struct task_struct
*task
;
2760 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
2765 put_task_struct(iter
.task
);
2769 pid
= find_ge_pid(iter
.tgid
, ns
);
2771 iter
.tgid
= pid_nr_ns(pid
, ns
);
2772 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
2773 /* What we to know is if the pid we have find is the
2774 * pid of a thread_group_leader. Testing for task
2775 * being a thread_group_leader is the obvious thing
2776 * todo but there is a window when it fails, due to
2777 * the pid transfer logic in de_thread.
2779 * So we perform the straight forward test of seeing
2780 * if the pid we have found is the pid of a thread
2781 * group leader, and don't worry if the task we have
2782 * found doesn't happen to be a thread group leader.
2783 * As we don't care in the case of readdir.
2785 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
2789 get_task_struct(iter
.task
);
2795 #define TGID_OFFSET (FIRST_PROCESS_ENTRY)
2797 static int proc_pid_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
2798 struct tgid_iter iter
)
2800 char name
[PROC_NUMBUF
];
2801 int len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
2802 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
2803 proc_pid_instantiate
, iter
.task
, NULL
);
2806 static int fake_filldir(void *buf
, const char *name
, int namelen
,
2807 loff_t offset
, u64 ino
, unsigned d_type
)
2812 /* for the /proc/ directory itself, after non-process stuff has been done */
2813 int proc_pid_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
2815 struct tgid_iter iter
;
2816 struct pid_namespace
*ns
;
2817 filldir_t __filldir
;
2819 if (filp
->f_pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
2822 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
2824 iter
.tgid
= filp
->f_pos
- TGID_OFFSET
;
2825 for (iter
= next_tgid(ns
, iter
);
2827 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
2828 if (has_pid_permissions(ns
, iter
.task
, 2))
2829 __filldir
= filldir
;
2831 __filldir
= fake_filldir
;
2833 filp
->f_pos
= iter
.tgid
+ TGID_OFFSET
;
2834 if (proc_pid_fill_cache(filp
, dirent
, __filldir
, iter
) < 0) {
2835 put_task_struct(iter
.task
);
2839 filp
->f_pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
2847 static const struct pid_entry tid_base_stuff
[] = {
2848 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2849 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2850 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2851 REG("environ", S_IRUSR
, proc_environ_operations
),
2852 INF("auxv", S_IRUSR
, proc_pid_auxv
),
2853 ONE("status", S_IRUGO
, proc_pid_status
),
2854 ONE("personality", S_IRUGO
, proc_pid_personality
),
2855 INF("limits", S_IRUGO
, proc_pid_limits
),
2856 #ifdef CONFIG_SCHED_DEBUG
2857 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2859 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2860 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2861 INF("syscall", S_IRUGO
, proc_pid_syscall
),
2863 INF("cmdline", S_IRUGO
, proc_pid_cmdline
),
2864 ONE("stat", S_IRUGO
, proc_tid_stat
),
2865 ONE("statm", S_IRUGO
, proc_pid_statm
),
2866 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
2867 #ifdef CONFIG_CHECKPOINT_RESTORE
2868 REG("children", S_IRUGO
, proc_tid_children_operations
),
2871 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
2873 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2874 LNK("cwd", proc_cwd_link
),
2875 LNK("root", proc_root_link
),
2876 LNK("exe", proc_exe_link
),
2877 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2878 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2879 #ifdef CONFIG_PROC_PAGE_MONITOR
2880 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2881 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
2882 REG("pagemap", S_IRUGO
, proc_pagemap_operations
),
2884 #ifdef CONFIG_SECURITY
2885 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2887 #ifdef CONFIG_KALLSYMS
2888 INF("wchan", S_IRUGO
, proc_pid_wchan
),
2890 #ifdef CONFIG_STACKTRACE
2891 ONE("stack", S_IRUGO
, proc_pid_stack
),
2893 #ifdef CONFIG_SCHEDSTATS
2894 INF("schedstat", S_IRUGO
, proc_pid_schedstat
),
2896 #ifdef CONFIG_LATENCYTOP
2897 REG("latency", S_IRUGO
, proc_lstats_operations
),
2899 #ifdef CONFIG_PROC_PID_CPUSET
2900 REG("cpuset", S_IRUGO
, proc_cpuset_operations
),
2902 #ifdef CONFIG_CGROUPS
2903 REG("cgroup", S_IRUGO
, proc_cgroup_operations
),
2905 INF("oom_score", S_IRUGO
, proc_oom_score
),
2906 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
2907 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
2908 #ifdef CONFIG_AUDITSYSCALL
2909 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
2910 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
2912 #ifdef CONFIG_FAULT_INJECTION
2913 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
2915 #ifdef CONFIG_TASK_IO_ACCOUNTING
2916 INF("io", S_IRUSR
, proc_tid_io_accounting
),
2918 #ifdef CONFIG_HARDWALL
2919 INF("hardwall", S_IRUGO
, proc_pid_hardwall
),
2921 #ifdef CONFIG_USER_NS
2922 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
2923 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
2924 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
2928 static int proc_tid_base_readdir(struct file
* filp
,
2929 void * dirent
, filldir_t filldir
)
2931 return proc_pident_readdir(filp
,dirent
,filldir
,
2932 tid_base_stuff
,ARRAY_SIZE(tid_base_stuff
));
2935 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
2937 return proc_pident_lookup(dir
, dentry
,
2938 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
2941 static const struct file_operations proc_tid_base_operations
= {
2942 .read
= generic_read_dir
,
2943 .readdir
= proc_tid_base_readdir
,
2944 .llseek
= default_llseek
,
2947 static const struct inode_operations proc_tid_base_inode_operations
= {
2948 .lookup
= proc_tid_base_lookup
,
2949 .getattr
= pid_getattr
,
2950 .setattr
= proc_setattr
,
2953 static struct dentry
*proc_task_instantiate(struct inode
*dir
,
2954 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2956 struct dentry
*error
= ERR_PTR(-ENOENT
);
2957 struct inode
*inode
;
2958 inode
= proc_pid_make_inode(dir
->i_sb
, task
);
2962 inode
->i_mode
= S_IFDIR
|S_IRUGO
|S_IXUGO
;
2963 inode
->i_op
= &proc_tid_base_inode_operations
;
2964 inode
->i_fop
= &proc_tid_base_operations
;
2965 inode
->i_flags
|=S_IMMUTABLE
;
2967 set_nlink(inode
, 2 + pid_entry_count_dirs(tid_base_stuff
,
2968 ARRAY_SIZE(tid_base_stuff
)));
2970 d_set_d_op(dentry
, &pid_dentry_operations
);
2972 d_add(dentry
, inode
);
2973 /* Close the race of the process dying before we return the dentry */
2974 if (pid_revalidate(dentry
, 0))
2980 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
2982 struct dentry
*result
= ERR_PTR(-ENOENT
);
2983 struct task_struct
*task
;
2984 struct task_struct
*leader
= get_proc_task(dir
);
2986 struct pid_namespace
*ns
;
2991 tid
= name_to_int(dentry
);
2995 ns
= dentry
->d_sb
->s_fs_info
;
2997 task
= find_task_by_pid_ns(tid
, ns
);
2999 get_task_struct(task
);
3003 if (!same_thread_group(leader
, task
))
3006 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3008 put_task_struct(task
);
3010 put_task_struct(leader
);
3016 * Find the first tid of a thread group to return to user space.
3018 * Usually this is just the thread group leader, but if the users
3019 * buffer was too small or there was a seek into the middle of the
3020 * directory we have more work todo.
3022 * In the case of a short read we start with find_task_by_pid.
3024 * In the case of a seek we start with the leader and walk nr
3027 static struct task_struct
*first_tid(struct task_struct
*leader
,
3028 int tid
, int nr
, struct pid_namespace
*ns
)
3030 struct task_struct
*pos
;
3033 /* Attempt to start with the pid of a thread */
3034 if (tid
&& (nr
> 0)) {
3035 pos
= find_task_by_pid_ns(tid
, ns
);
3036 if (pos
&& (pos
->group_leader
== leader
))
3040 /* If nr exceeds the number of threads there is nothing todo */
3042 if (nr
&& nr
>= get_nr_threads(leader
))
3045 /* If we haven't found our starting place yet start
3046 * with the leader and walk nr threads forward.
3048 for (pos
= leader
; nr
> 0; --nr
) {
3049 pos
= next_thread(pos
);
3050 if (pos
== leader
) {
3056 get_task_struct(pos
);
3063 * Find the next thread in the thread list.
3064 * Return NULL if there is an error or no next thread.
3066 * The reference to the input task_struct is released.
3068 static struct task_struct
*next_tid(struct task_struct
*start
)
3070 struct task_struct
*pos
= NULL
;
3072 if (pid_alive(start
)) {
3073 pos
= next_thread(start
);
3074 if (thread_group_leader(pos
))
3077 get_task_struct(pos
);
3080 put_task_struct(start
);
3084 static int proc_task_fill_cache(struct file
*filp
, void *dirent
, filldir_t filldir
,
3085 struct task_struct
*task
, int tid
)
3087 char name
[PROC_NUMBUF
];
3088 int len
= snprintf(name
, sizeof(name
), "%d", tid
);
3089 return proc_fill_cache(filp
, dirent
, filldir
, name
, len
,
3090 proc_task_instantiate
, task
, NULL
);
3093 /* for the /proc/TGID/task/ directories */
3094 static int proc_task_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
)
3096 struct dentry
*dentry
= filp
->f_path
.dentry
;
3097 struct inode
*inode
= dentry
->d_inode
;
3098 struct task_struct
*leader
= NULL
;
3099 struct task_struct
*task
;
3100 int retval
= -ENOENT
;
3103 struct pid_namespace
*ns
;
3105 task
= get_proc_task(inode
);
3109 if (pid_alive(task
)) {
3110 leader
= task
->group_leader
;
3111 get_task_struct(leader
);
3114 put_task_struct(task
);
3119 switch ((unsigned long)filp
->f_pos
) {
3122 if (filldir(dirent
, ".", 1, filp
->f_pos
, ino
, DT_DIR
) < 0)
3127 ino
= parent_ino(dentry
);
3128 if (filldir(dirent
, "..", 2, filp
->f_pos
, ino
, DT_DIR
) < 0)
3134 /* f_version caches the tgid value that the last readdir call couldn't
3135 * return. lseek aka telldir automagically resets f_version to 0.
3137 ns
= filp
->f_dentry
->d_sb
->s_fs_info
;
3138 tid
= (int)filp
->f_version
;
3139 filp
->f_version
= 0;
3140 for (task
= first_tid(leader
, tid
, filp
->f_pos
- 2, ns
);
3142 task
= next_tid(task
), filp
->f_pos
++) {
3143 tid
= task_pid_nr_ns(task
, ns
);
3144 if (proc_task_fill_cache(filp
, dirent
, filldir
, task
, tid
) < 0) {
3145 /* returning this tgid failed, save it as the first
3146 * pid for the next readir call */
3147 filp
->f_version
= (u64
)tid
;
3148 put_task_struct(task
);
3153 put_task_struct(leader
);
3158 static int proc_task_getattr(struct vfsmount
*mnt
, struct dentry
*dentry
, struct kstat
*stat
)
3160 struct inode
*inode
= dentry
->d_inode
;
3161 struct task_struct
*p
= get_proc_task(inode
);
3162 generic_fillattr(inode
, stat
);
3165 stat
->nlink
+= get_nr_threads(p
);
3172 static const struct inode_operations proc_task_inode_operations
= {
3173 .lookup
= proc_task_lookup
,
3174 .getattr
= proc_task_getattr
,
3175 .setattr
= proc_setattr
,
3176 .permission
= proc_pid_permission
,
3179 static const struct file_operations proc_task_operations
= {
3180 .read
= generic_read_dir
,
3181 .readdir
= proc_task_readdir
,
3182 .llseek
= default_llseek
,