1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1991, 1992 Linus Torvalds
7 * proc base directory handling functions
9 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
10 * Instead of using magical inumbers to determine the kind of object
11 * we allocate and fill in-core inodes upon lookup. They don't even
12 * go into icache. We cache the reference to task_struct upon lookup too.
13 * Eventually it should become a filesystem in its own. We don't use the
14 * rest of procfs anymore.
20 * Bruna Moreira <bruna.moreira@indt.org.br>
21 * Edjard Mota <edjard.mota@indt.org.br>
22 * Ilias Biris <ilias.biris@indt.org.br>
23 * Mauricio Lin <mauricio.lin@indt.org.br>
25 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
27 * A new process specific entry (smaps) included in /proc. It shows the
28 * size of rss for each memory area. The maps entry lacks information
29 * about physical memory size (rss) for each mapped file, i.e.,
30 * rss information for executables and library files.
31 * This additional information is useful for any tools that need to know
32 * about physical memory consumption for a process specific library.
36 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
37 * Pud inclusion in the page table walking.
41 * 10LE Instituto Nokia de Tecnologia - INdT:
42 * A better way to walks through the page table as suggested by Hugh Dickins.
44 * Simo Piiroinen <simo.piiroinen@nokia.com>:
45 * Smaps information related to shared, private, clean and dirty pages.
47 * Paul Mundt <paul.mundt@nokia.com>:
48 * Overall revision about smaps.
51 #include <linux/uaccess.h>
53 #include <linux/errno.h>
54 #include <linux/time.h>
55 #include <linux/proc_fs.h>
56 #include <linux/stat.h>
57 #include <linux/task_io_accounting_ops.h>
58 #include <linux/init.h>
59 #include <linux/capability.h>
60 #include <linux/file.h>
61 #include <linux/fdtable.h>
62 #include <linux/string.h>
63 #include <linux/seq_file.h>
64 #include <linux/namei.h>
65 #include <linux/mnt_namespace.h>
67 #include <linux/swap.h>
68 #include <linux/rcupdate.h>
69 #include <linux/kallsyms.h>
70 #include <linux/stacktrace.h>
71 #include <linux/resource.h>
72 #include <linux/module.h>
73 #include <linux/mount.h>
74 #include <linux/security.h>
75 #include <linux/ptrace.h>
76 #include <linux/tracehook.h>
77 #include <linux/printk.h>
78 #include <linux/cgroup.h>
79 #include <linux/cpuset.h>
80 #include <linux/audit.h>
81 #include <linux/poll.h>
82 #include <linux/nsproxy.h>
83 #include <linux/oom.h>
84 #include <linux/elf.h>
85 #include <linux/pid_namespace.h>
86 #include <linux/user_namespace.h>
87 #include <linux/fs_struct.h>
88 #include <linux/slab.h>
89 #include <linux/sched/autogroup.h>
90 #include <linux/sched/mm.h>
91 #include <linux/sched/coredump.h>
92 #include <linux/sched/debug.h>
93 #include <linux/sched/stat.h>
94 #include <linux/flex_array.h>
95 #include <linux/posix-timers.h>
96 #ifdef CONFIG_HARDWALL
97 #include <asm/hardwall.h>
99 #include <trace/events/oom.h>
100 #include "internal.h"
103 #include "../../lib/kstrtox.h"
106 * Implementing inode permission operations in /proc is almost
107 * certainly an error. Permission checks need to happen during
108 * each system call not at open time. The reason is that most of
109 * what we wish to check for permissions in /proc varies at runtime.
111 * The classic example of a problem is opening file descriptors
112 * in /proc for a task before it execs a suid executable.
116 static u8 nlink_tgid
;
122 const struct inode_operations
*iop
;
123 const struct file_operations
*fop
;
127 #define NOD(NAME, MODE, IOP, FOP, OP) { \
129 .len = sizeof(NAME) - 1, \
136 #define DIR(NAME, MODE, iops, fops) \
137 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
138 #define LNK(NAME, get_link) \
139 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
140 &proc_pid_link_inode_operations, NULL, \
141 { .proc_get_link = get_link } )
142 #define REG(NAME, MODE, fops) \
143 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
144 #define ONE(NAME, MODE, show) \
145 NOD(NAME, (S_IFREG|(MODE)), \
146 NULL, &proc_single_file_operations, \
147 { .proc_show = show } )
150 * Count the number of hardlinks for the pid_entry table, excluding the .
153 static unsigned int __init
pid_entry_nlink(const struct pid_entry
*entries
,
160 for (i
= 0; i
< n
; ++i
) {
161 if (S_ISDIR(entries
[i
].mode
))
168 static int get_task_root(struct task_struct
*task
, struct path
*root
)
170 int result
= -ENOENT
;
174 get_fs_root(task
->fs
, root
);
181 static int proc_cwd_link(struct dentry
*dentry
, struct path
*path
)
183 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
184 int result
= -ENOENT
;
189 get_fs_pwd(task
->fs
, path
);
193 put_task_struct(task
);
198 static int proc_root_link(struct dentry
*dentry
, struct path
*path
)
200 struct task_struct
*task
= get_proc_task(d_inode(dentry
));
201 int result
= -ENOENT
;
204 result
= get_task_root(task
, path
);
205 put_task_struct(task
);
210 static ssize_t
proc_pid_cmdline_read(struct file
*file
, char __user
*buf
,
211 size_t _count
, loff_t
*pos
)
213 struct task_struct
*tsk
;
214 struct mm_struct
*mm
;
216 unsigned long count
= _count
;
217 unsigned long arg_start
, arg_end
, env_start
, env_end
;
218 unsigned long len1
, len2
, len
;
225 tsk
= get_proc_task(file_inode(file
));
228 mm
= get_task_mm(tsk
);
229 put_task_struct(tsk
);
232 /* Check if process spawned far enough to have cmdline. */
238 page
= (char *)__get_free_page(GFP_KERNEL
);
244 down_read(&mm
->mmap_sem
);
245 arg_start
= mm
->arg_start
;
246 arg_end
= mm
->arg_end
;
247 env_start
= mm
->env_start
;
248 env_end
= mm
->env_end
;
249 up_read(&mm
->mmap_sem
);
251 BUG_ON(arg_start
> arg_end
);
252 BUG_ON(env_start
> env_end
);
254 len1
= arg_end
- arg_start
;
255 len2
= env_end
- env_start
;
263 * Inherently racy -- command line shares address space
264 * with code and data.
266 rv
= access_remote_vm(mm
, arg_end
- 1, &c
, 1, FOLL_ANON
);
273 /* Command line (set of strings) occupies whole ARGV. */
277 p
= arg_start
+ *pos
;
279 while (count
> 0 && len
> 0) {
283 _count
= min3(count
, len
, PAGE_SIZE
);
284 nr_read
= access_remote_vm(mm
, p
, page
, _count
, FOLL_ANON
);
290 if (copy_to_user(buf
, page
, nr_read
)) {
303 * Command line (1 string) occupies ARGV and
310 { .p
= arg_start
, .len
= len1
},
311 { .p
= env_start
, .len
= len2
},
317 while (i
< 2 && pos1
>= cmdline
[i
].len
) {
318 pos1
-= cmdline
[i
].len
;
322 p
= cmdline
[i
].p
+ pos1
;
323 len
= cmdline
[i
].len
- pos1
;
324 while (count
> 0 && len
> 0) {
325 unsigned int _count
, l
;
329 _count
= min3(count
, len
, PAGE_SIZE
);
330 nr_read
= access_remote_vm(mm
, p
, page
, _count
, FOLL_ANON
);
337 * Command line can be shorter than whole ARGV
338 * even if last "marker" byte says it is not.
341 l
= strnlen(page
, nr_read
);
347 if (copy_to_user(buf
, page
, nr_read
)) {
362 /* Only first chunk can be read partially. */
369 free_page((unsigned long)page
);
377 static const struct file_operations proc_pid_cmdline_ops
= {
378 .read
= proc_pid_cmdline_read
,
379 .llseek
= generic_file_llseek
,
382 #ifdef CONFIG_KALLSYMS
384 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
385 * Returns the resolved symbol. If that fails, simply return the address.
387 static int proc_pid_wchan(struct seq_file
*m
, struct pid_namespace
*ns
,
388 struct pid
*pid
, struct task_struct
*task
)
391 char symname
[KSYM_NAME_LEN
];
393 wchan
= get_wchan(task
);
395 if (wchan
&& ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)
396 && !lookup_symbol_name(wchan
, symname
))
397 seq_printf(m
, "%s", symname
);
403 #endif /* CONFIG_KALLSYMS */
405 static int lock_trace(struct task_struct
*task
)
407 int err
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
410 if (!ptrace_may_access(task
, PTRACE_MODE_ATTACH_FSCREDS
)) {
411 mutex_unlock(&task
->signal
->cred_guard_mutex
);
417 static void unlock_trace(struct task_struct
*task
)
419 mutex_unlock(&task
->signal
->cred_guard_mutex
);
422 #ifdef CONFIG_STACKTRACE
424 #define MAX_STACK_TRACE_DEPTH 64
426 static int proc_pid_stack(struct seq_file
*m
, struct pid_namespace
*ns
,
427 struct pid
*pid
, struct task_struct
*task
)
429 struct stack_trace trace
;
430 unsigned long *entries
;
435 * The ability to racily run the kernel stack unwinder on a running task
436 * and then observe the unwinder output is scary; while it is useful for
437 * debugging kernel issues, it can also allow an attacker to leak kernel
439 * Doing this in a manner that is at least safe from races would require
440 * some work to ensure that the remote task can not be scheduled; and
441 * even then, this would still expose the unwinder as local attack
443 * Therefore, this interface is restricted to root.
445 if (!file_ns_capable(m
->file
, &init_user_ns
, CAP_SYS_ADMIN
))
448 entries
= kmalloc(MAX_STACK_TRACE_DEPTH
* sizeof(*entries
), GFP_KERNEL
);
452 trace
.nr_entries
= 0;
453 trace
.max_entries
= MAX_STACK_TRACE_DEPTH
;
454 trace
.entries
= entries
;
457 err
= lock_trace(task
);
459 save_stack_trace_tsk(task
, &trace
);
461 for (i
= 0; i
< trace
.nr_entries
; i
++) {
462 seq_printf(m
, "[<%pK>] %pB\n",
463 (void *)entries
[i
], (void *)entries
[i
]);
473 #ifdef CONFIG_SCHED_INFO
475 * Provides /proc/PID/schedstat
477 static int proc_pid_schedstat(struct seq_file
*m
, struct pid_namespace
*ns
,
478 struct pid
*pid
, struct task_struct
*task
)
480 if (unlikely(!sched_info_on()))
481 seq_printf(m
, "0 0 0\n");
483 seq_printf(m
, "%llu %llu %lu\n",
484 (unsigned long long)task
->se
.sum_exec_runtime
,
485 (unsigned long long)task
->sched_info
.run_delay
,
486 task
->sched_info
.pcount
);
492 #ifdef CONFIG_LATENCYTOP
493 static int lstats_show_proc(struct seq_file
*m
, void *v
)
496 struct inode
*inode
= m
->private;
497 struct task_struct
*task
= get_proc_task(inode
);
501 seq_puts(m
, "Latency Top version : v0.1\n");
502 for (i
= 0; i
< 32; i
++) {
503 struct latency_record
*lr
= &task
->latency_record
[i
];
504 if (lr
->backtrace
[0]) {
506 seq_printf(m
, "%i %li %li",
507 lr
->count
, lr
->time
, lr
->max
);
508 for (q
= 0; q
< LT_BACKTRACEDEPTH
; q
++) {
509 unsigned long bt
= lr
->backtrace
[q
];
514 seq_printf(m
, " %ps", (void *)bt
);
520 put_task_struct(task
);
524 static int lstats_open(struct inode
*inode
, struct file
*file
)
526 return single_open(file
, lstats_show_proc
, inode
);
529 static ssize_t
lstats_write(struct file
*file
, const char __user
*buf
,
530 size_t count
, loff_t
*offs
)
532 struct task_struct
*task
= get_proc_task(file_inode(file
));
536 clear_all_latency_tracing(task
);
537 put_task_struct(task
);
542 static const struct file_operations proc_lstats_operations
= {
545 .write
= lstats_write
,
547 .release
= single_release
,
552 static int proc_oom_score(struct seq_file
*m
, struct pid_namespace
*ns
,
553 struct pid
*pid
, struct task_struct
*task
)
555 unsigned long totalpages
= totalram_pages
+ total_swap_pages
;
556 unsigned long points
= 0;
558 points
= oom_badness(task
, NULL
, NULL
, totalpages
) *
560 seq_printf(m
, "%lu\n", points
);
570 static const struct limit_names lnames
[RLIM_NLIMITS
] = {
571 [RLIMIT_CPU
] = {"Max cpu time", "seconds"},
572 [RLIMIT_FSIZE
] = {"Max file size", "bytes"},
573 [RLIMIT_DATA
] = {"Max data size", "bytes"},
574 [RLIMIT_STACK
] = {"Max stack size", "bytes"},
575 [RLIMIT_CORE
] = {"Max core file size", "bytes"},
576 [RLIMIT_RSS
] = {"Max resident set", "bytes"},
577 [RLIMIT_NPROC
] = {"Max processes", "processes"},
578 [RLIMIT_NOFILE
] = {"Max open files", "files"},
579 [RLIMIT_MEMLOCK
] = {"Max locked memory", "bytes"},
580 [RLIMIT_AS
] = {"Max address space", "bytes"},
581 [RLIMIT_LOCKS
] = {"Max file locks", "locks"},
582 [RLIMIT_SIGPENDING
] = {"Max pending signals", "signals"},
583 [RLIMIT_MSGQUEUE
] = {"Max msgqueue size", "bytes"},
584 [RLIMIT_NICE
] = {"Max nice priority", NULL
},
585 [RLIMIT_RTPRIO
] = {"Max realtime priority", NULL
},
586 [RLIMIT_RTTIME
] = {"Max realtime timeout", "us"},
589 /* Display limits for a process */
590 static int proc_pid_limits(struct seq_file
*m
, struct pid_namespace
*ns
,
591 struct pid
*pid
, struct task_struct
*task
)
596 struct rlimit rlim
[RLIM_NLIMITS
];
598 if (!lock_task_sighand(task
, &flags
))
600 memcpy(rlim
, task
->signal
->rlim
, sizeof(struct rlimit
) * RLIM_NLIMITS
);
601 unlock_task_sighand(task
, &flags
);
604 * print the file header
606 seq_printf(m
, "%-25s %-20s %-20s %-10s\n",
607 "Limit", "Soft Limit", "Hard Limit", "Units");
609 for (i
= 0; i
< RLIM_NLIMITS
; i
++) {
610 if (rlim
[i
].rlim_cur
== RLIM_INFINITY
)
611 seq_printf(m
, "%-25s %-20s ",
612 lnames
[i
].name
, "unlimited");
614 seq_printf(m
, "%-25s %-20lu ",
615 lnames
[i
].name
, rlim
[i
].rlim_cur
);
617 if (rlim
[i
].rlim_max
== RLIM_INFINITY
)
618 seq_printf(m
, "%-20s ", "unlimited");
620 seq_printf(m
, "%-20lu ", rlim
[i
].rlim_max
);
623 seq_printf(m
, "%-10s\n", lnames
[i
].unit
);
631 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
632 static int proc_pid_syscall(struct seq_file
*m
, struct pid_namespace
*ns
,
633 struct pid
*pid
, struct task_struct
*task
)
636 unsigned long args
[6], sp
, pc
;
639 res
= lock_trace(task
);
643 if (task_current_syscall(task
, &nr
, args
, 6, &sp
, &pc
))
644 seq_puts(m
, "running\n");
646 seq_printf(m
, "%ld 0x%lx 0x%lx\n", nr
, sp
, pc
);
649 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
651 args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
657 #endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
659 /************************************************************************/
660 /* Here the fs part begins */
661 /************************************************************************/
663 /* permission checks */
664 static int proc_fd_access_allowed(struct inode
*inode
)
666 struct task_struct
*task
;
668 /* Allow access to a task's file descriptors if it is us or we
669 * may use ptrace attach to the process and find out that
672 task
= get_proc_task(inode
);
674 allowed
= ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
675 put_task_struct(task
);
680 int proc_setattr(struct dentry
*dentry
, struct iattr
*attr
)
683 struct inode
*inode
= d_inode(dentry
);
685 if (attr
->ia_valid
& ATTR_MODE
)
688 error
= setattr_prepare(dentry
, attr
);
692 setattr_copy(inode
, attr
);
693 mark_inode_dirty(inode
);
698 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
699 * or euid/egid (for hide_pid_min=2)?
701 static bool has_pid_permissions(struct pid_namespace
*pid
,
702 struct task_struct
*task
,
705 if (pid
->hide_pid
< hide_pid_min
)
707 if (in_group_p(pid
->pid_gid
))
709 return ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
);
713 static int proc_pid_permission(struct inode
*inode
, int mask
)
715 struct pid_namespace
*pid
= inode
->i_sb
->s_fs_info
;
716 struct task_struct
*task
;
719 task
= get_proc_task(inode
);
722 has_perms
= has_pid_permissions(pid
, task
, HIDEPID_NO_ACCESS
);
723 put_task_struct(task
);
726 if (pid
->hide_pid
== HIDEPID_INVISIBLE
) {
728 * Let's make getdents(), stat(), and open()
729 * consistent with each other. If a process
730 * may not stat() a file, it shouldn't be seen
738 return generic_permission(inode
, mask
);
743 static const struct inode_operations proc_def_inode_operations
= {
744 .setattr
= proc_setattr
,
747 static int proc_single_show(struct seq_file
*m
, void *v
)
749 struct inode
*inode
= m
->private;
750 struct pid_namespace
*ns
;
752 struct task_struct
*task
;
755 ns
= inode
->i_sb
->s_fs_info
;
756 pid
= proc_pid(inode
);
757 task
= get_pid_task(pid
, PIDTYPE_PID
);
761 ret
= PROC_I(inode
)->op
.proc_show(m
, ns
, pid
, task
);
763 put_task_struct(task
);
767 static int proc_single_open(struct inode
*inode
, struct file
*filp
)
769 return single_open(filp
, proc_single_show
, inode
);
772 static const struct file_operations proc_single_file_operations
= {
773 .open
= proc_single_open
,
776 .release
= single_release
,
780 struct mm_struct
*proc_mem_open(struct inode
*inode
, unsigned int mode
)
782 struct task_struct
*task
= get_proc_task(inode
);
783 struct mm_struct
*mm
= ERR_PTR(-ESRCH
);
786 mm
= mm_access(task
, mode
| PTRACE_MODE_FSCREDS
);
787 put_task_struct(task
);
789 if (!IS_ERR_OR_NULL(mm
)) {
790 /* ensure this mm_struct can't be freed */
792 /* but do not pin its memory */
800 static int __mem_open(struct inode
*inode
, struct file
*file
, unsigned int mode
)
802 struct mm_struct
*mm
= proc_mem_open(inode
, mode
);
807 file
->private_data
= mm
;
811 static int mem_open(struct inode
*inode
, struct file
*file
)
813 int ret
= __mem_open(inode
, file
, PTRACE_MODE_ATTACH
);
815 /* OK to pass negative loff_t, we can catch out-of-range */
816 file
->f_mode
|= FMODE_UNSIGNED_OFFSET
;
821 static ssize_t
mem_rw(struct file
*file
, char __user
*buf
,
822 size_t count
, loff_t
*ppos
, int write
)
824 struct mm_struct
*mm
= file
->private_data
;
825 unsigned long addr
= *ppos
;
833 page
= (char *)__get_free_page(GFP_KERNEL
);
838 if (!mmget_not_zero(mm
))
841 flags
= FOLL_FORCE
| (write
? FOLL_WRITE
: 0);
844 int this_len
= min_t(int, count
, PAGE_SIZE
);
846 if (write
&& copy_from_user(page
, buf
, this_len
)) {
851 this_len
= access_remote_vm(mm
, addr
, page
, this_len
, flags
);
858 if (!write
&& copy_to_user(buf
, page
, this_len
)) {
872 free_page((unsigned long) page
);
876 static ssize_t
mem_read(struct file
*file
, char __user
*buf
,
877 size_t count
, loff_t
*ppos
)
879 return mem_rw(file
, buf
, count
, ppos
, 0);
882 static ssize_t
mem_write(struct file
*file
, const char __user
*buf
,
883 size_t count
, loff_t
*ppos
)
885 return mem_rw(file
, (char __user
*)buf
, count
, ppos
, 1);
888 loff_t
mem_lseek(struct file
*file
, loff_t offset
, int orig
)
892 file
->f_pos
= offset
;
895 file
->f_pos
+= offset
;
900 force_successful_syscall_return();
904 static int mem_release(struct inode
*inode
, struct file
*file
)
906 struct mm_struct
*mm
= file
->private_data
;
912 static const struct file_operations proc_mem_operations
= {
917 .release
= mem_release
,
920 static int environ_open(struct inode
*inode
, struct file
*file
)
922 return __mem_open(inode
, file
, PTRACE_MODE_READ
);
925 static ssize_t
environ_read(struct file
*file
, char __user
*buf
,
926 size_t count
, loff_t
*ppos
)
929 unsigned long src
= *ppos
;
931 struct mm_struct
*mm
= file
->private_data
;
932 unsigned long env_start
, env_end
;
934 /* Ensure the process spawned far enough to have an environment. */
935 if (!mm
|| !mm
->env_end
)
938 page
= (char *)__get_free_page(GFP_KERNEL
);
943 if (!mmget_not_zero(mm
))
946 down_read(&mm
->mmap_sem
);
947 env_start
= mm
->env_start
;
948 env_end
= mm
->env_end
;
949 up_read(&mm
->mmap_sem
);
952 size_t this_len
, max_len
;
955 if (src
>= (env_end
- env_start
))
958 this_len
= env_end
- (env_start
+ src
);
960 max_len
= min_t(size_t, PAGE_SIZE
, count
);
961 this_len
= min(max_len
, this_len
);
963 retval
= access_remote_vm(mm
, (env_start
+ src
), page
, this_len
, FOLL_ANON
);
970 if (copy_to_user(buf
, page
, retval
)) {
984 free_page((unsigned long) page
);
988 static const struct file_operations proc_environ_operations
= {
989 .open
= environ_open
,
990 .read
= environ_read
,
991 .llseek
= generic_file_llseek
,
992 .release
= mem_release
,
995 static int auxv_open(struct inode
*inode
, struct file
*file
)
997 return __mem_open(inode
, file
, PTRACE_MODE_READ_FSCREDS
);
1000 static ssize_t
auxv_read(struct file
*file
, char __user
*buf
,
1001 size_t count
, loff_t
*ppos
)
1003 struct mm_struct
*mm
= file
->private_data
;
1004 unsigned int nwords
= 0;
1010 } while (mm
->saved_auxv
[nwords
- 2] != 0); /* AT_NULL */
1011 return simple_read_from_buffer(buf
, count
, ppos
, mm
->saved_auxv
,
1012 nwords
* sizeof(mm
->saved_auxv
[0]));
1015 static const struct file_operations proc_auxv_operations
= {
1018 .llseek
= generic_file_llseek
,
1019 .release
= mem_release
,
1022 static ssize_t
oom_adj_read(struct file
*file
, char __user
*buf
, size_t count
,
1025 struct task_struct
*task
= get_proc_task(file_inode(file
));
1026 char buffer
[PROC_NUMBUF
];
1027 int oom_adj
= OOM_ADJUST_MIN
;
1032 if (task
->signal
->oom_score_adj
== OOM_SCORE_ADJ_MAX
)
1033 oom_adj
= OOM_ADJUST_MAX
;
1035 oom_adj
= (task
->signal
->oom_score_adj
* -OOM_DISABLE
) /
1037 put_task_struct(task
);
1038 len
= snprintf(buffer
, sizeof(buffer
), "%d\n", oom_adj
);
1039 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1042 static int __set_oom_adj(struct file
*file
, int oom_adj
, bool legacy
)
1044 struct mm_struct
*mm
= NULL
;
1045 struct task_struct
*task
;
1048 task
= get_proc_task(file_inode(file
));
1052 mutex_lock(&oom_adj_mutex
);
1054 if (oom_adj
< task
->signal
->oom_score_adj
&&
1055 !capable(CAP_SYS_RESOURCE
)) {
1060 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
1061 * /proc/pid/oom_score_adj instead.
1063 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
1064 current
->comm
, task_pid_nr(current
), task_pid_nr(task
),
1067 if ((short)oom_adj
< task
->signal
->oom_score_adj_min
&&
1068 !capable(CAP_SYS_RESOURCE
)) {
1075 * Make sure we will check other processes sharing the mm if this is
1076 * not vfrok which wants its own oom_score_adj.
1077 * pin the mm so it doesn't go away and get reused after task_unlock
1079 if (!task
->vfork_done
) {
1080 struct task_struct
*p
= find_lock_task_mm(task
);
1083 if (test_bit(MMF_MULTIPROCESS
, &p
->mm
->flags
)) {
1091 task
->signal
->oom_score_adj
= oom_adj
;
1092 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1093 task
->signal
->oom_score_adj_min
= (short)oom_adj
;
1094 trace_oom_score_adj_update(task
);
1097 struct task_struct
*p
;
1100 for_each_process(p
) {
1101 if (same_thread_group(task
, p
))
1104 /* do not touch kernel threads or the global init */
1105 if (p
->flags
& PF_KTHREAD
|| is_global_init(p
))
1109 if (!p
->vfork_done
&& process_shares_mm(p
, mm
)) {
1110 p
->signal
->oom_score_adj
= oom_adj
;
1111 if (!legacy
&& has_capability_noaudit(current
, CAP_SYS_RESOURCE
))
1112 p
->signal
->oom_score_adj_min
= (short)oom_adj
;
1120 mutex_unlock(&oom_adj_mutex
);
1121 put_task_struct(task
);
1126 * /proc/pid/oom_adj exists solely for backwards compatibility with previous
1127 * kernels. The effective policy is defined by oom_score_adj, which has a
1128 * different scale: oom_adj grew exponentially and oom_score_adj grows linearly.
1129 * Values written to oom_adj are simply mapped linearly to oom_score_adj.
1130 * Processes that become oom disabled via oom_adj will still be oom disabled
1131 * with this implementation.
1133 * oom_adj cannot be removed since existing userspace binaries use it.
1135 static ssize_t
oom_adj_write(struct file
*file
, const char __user
*buf
,
1136 size_t count
, loff_t
*ppos
)
1138 char buffer
[PROC_NUMBUF
];
1142 memset(buffer
, 0, sizeof(buffer
));
1143 if (count
> sizeof(buffer
) - 1)
1144 count
= sizeof(buffer
) - 1;
1145 if (copy_from_user(buffer
, buf
, count
)) {
1150 err
= kstrtoint(strstrip(buffer
), 0, &oom_adj
);
1153 if ((oom_adj
< OOM_ADJUST_MIN
|| oom_adj
> OOM_ADJUST_MAX
) &&
1154 oom_adj
!= OOM_DISABLE
) {
1160 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1161 * value is always attainable.
1163 if (oom_adj
== OOM_ADJUST_MAX
)
1164 oom_adj
= OOM_SCORE_ADJ_MAX
;
1166 oom_adj
= (oom_adj
* OOM_SCORE_ADJ_MAX
) / -OOM_DISABLE
;
1168 err
= __set_oom_adj(file
, oom_adj
, true);
1170 return err
< 0 ? err
: count
;
1173 static const struct file_operations proc_oom_adj_operations
= {
1174 .read
= oom_adj_read
,
1175 .write
= oom_adj_write
,
1176 .llseek
= generic_file_llseek
,
1179 static ssize_t
oom_score_adj_read(struct file
*file
, char __user
*buf
,
1180 size_t count
, loff_t
*ppos
)
1182 struct task_struct
*task
= get_proc_task(file_inode(file
));
1183 char buffer
[PROC_NUMBUF
];
1184 short oom_score_adj
= OOM_SCORE_ADJ_MIN
;
1189 oom_score_adj
= task
->signal
->oom_score_adj
;
1190 put_task_struct(task
);
1191 len
= snprintf(buffer
, sizeof(buffer
), "%hd\n", oom_score_adj
);
1192 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1195 static ssize_t
oom_score_adj_write(struct file
*file
, const char __user
*buf
,
1196 size_t count
, loff_t
*ppos
)
1198 char buffer
[PROC_NUMBUF
];
1202 memset(buffer
, 0, sizeof(buffer
));
1203 if (count
> sizeof(buffer
) - 1)
1204 count
= sizeof(buffer
) - 1;
1205 if (copy_from_user(buffer
, buf
, count
)) {
1210 err
= kstrtoint(strstrip(buffer
), 0, &oom_score_adj
);
1213 if (oom_score_adj
< OOM_SCORE_ADJ_MIN
||
1214 oom_score_adj
> OOM_SCORE_ADJ_MAX
) {
1219 err
= __set_oom_adj(file
, oom_score_adj
, false);
1221 return err
< 0 ? err
: count
;
1224 static const struct file_operations proc_oom_score_adj_operations
= {
1225 .read
= oom_score_adj_read
,
1226 .write
= oom_score_adj_write
,
1227 .llseek
= default_llseek
,
1230 #ifdef CONFIG_AUDITSYSCALL
1231 #define TMPBUFLEN 11
1232 static ssize_t
proc_loginuid_read(struct file
* file
, char __user
* buf
,
1233 size_t count
, loff_t
*ppos
)
1235 struct inode
* inode
= file_inode(file
);
1236 struct task_struct
*task
= get_proc_task(inode
);
1238 char tmpbuf
[TMPBUFLEN
];
1242 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1243 from_kuid(file
->f_cred
->user_ns
,
1244 audit_get_loginuid(task
)));
1245 put_task_struct(task
);
1246 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1249 static ssize_t
proc_loginuid_write(struct file
* file
, const char __user
* buf
,
1250 size_t count
, loff_t
*ppos
)
1252 struct inode
* inode
= file_inode(file
);
1258 if (current
!= pid_task(proc_pid(inode
), PIDTYPE_PID
)) {
1265 /* No partial writes. */
1269 rv
= kstrtou32_from_user(buf
, count
, 10, &loginuid
);
1273 /* is userspace tring to explicitly UNSET the loginuid? */
1274 if (loginuid
== AUDIT_UID_UNSET
) {
1275 kloginuid
= INVALID_UID
;
1277 kloginuid
= make_kuid(file
->f_cred
->user_ns
, loginuid
);
1278 if (!uid_valid(kloginuid
))
1282 rv
= audit_set_loginuid(kloginuid
);
1288 static const struct file_operations proc_loginuid_operations
= {
1289 .read
= proc_loginuid_read
,
1290 .write
= proc_loginuid_write
,
1291 .llseek
= generic_file_llseek
,
1294 static ssize_t
proc_sessionid_read(struct file
* file
, char __user
* buf
,
1295 size_t count
, loff_t
*ppos
)
1297 struct inode
* inode
= file_inode(file
);
1298 struct task_struct
*task
= get_proc_task(inode
);
1300 char tmpbuf
[TMPBUFLEN
];
1304 length
= scnprintf(tmpbuf
, TMPBUFLEN
, "%u",
1305 audit_get_sessionid(task
));
1306 put_task_struct(task
);
1307 return simple_read_from_buffer(buf
, count
, ppos
, tmpbuf
, length
);
1310 static const struct file_operations proc_sessionid_operations
= {
1311 .read
= proc_sessionid_read
,
1312 .llseek
= generic_file_llseek
,
1316 #ifdef CONFIG_FAULT_INJECTION
1317 static ssize_t
proc_fault_inject_read(struct file
* file
, char __user
* buf
,
1318 size_t count
, loff_t
*ppos
)
1320 struct task_struct
*task
= get_proc_task(file_inode(file
));
1321 char buffer
[PROC_NUMBUF
];
1327 make_it_fail
= task
->make_it_fail
;
1328 put_task_struct(task
);
1330 len
= snprintf(buffer
, sizeof(buffer
), "%i\n", make_it_fail
);
1332 return simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
1335 static ssize_t
proc_fault_inject_write(struct file
* file
,
1336 const char __user
* buf
, size_t count
, loff_t
*ppos
)
1338 struct task_struct
*task
;
1339 char buffer
[PROC_NUMBUF
];
1343 if (!capable(CAP_SYS_RESOURCE
))
1345 memset(buffer
, 0, sizeof(buffer
));
1346 if (count
> sizeof(buffer
) - 1)
1347 count
= sizeof(buffer
) - 1;
1348 if (copy_from_user(buffer
, buf
, count
))
1350 rv
= kstrtoint(strstrip(buffer
), 0, &make_it_fail
);
1353 if (make_it_fail
< 0 || make_it_fail
> 1)
1356 task
= get_proc_task(file_inode(file
));
1359 task
->make_it_fail
= make_it_fail
;
1360 put_task_struct(task
);
1365 static const struct file_operations proc_fault_inject_operations
= {
1366 .read
= proc_fault_inject_read
,
1367 .write
= proc_fault_inject_write
,
1368 .llseek
= generic_file_llseek
,
1371 static ssize_t
proc_fail_nth_write(struct file
*file
, const char __user
*buf
,
1372 size_t count
, loff_t
*ppos
)
1374 struct task_struct
*task
;
1378 err
= kstrtouint_from_user(buf
, count
, 0, &n
);
1382 task
= get_proc_task(file_inode(file
));
1385 WRITE_ONCE(task
->fail_nth
, n
);
1386 put_task_struct(task
);
1391 static ssize_t
proc_fail_nth_read(struct file
*file
, char __user
*buf
,
1392 size_t count
, loff_t
*ppos
)
1394 struct task_struct
*task
;
1395 char numbuf
[PROC_NUMBUF
];
1398 task
= get_proc_task(file_inode(file
));
1401 len
= snprintf(numbuf
, sizeof(numbuf
), "%u\n",
1402 READ_ONCE(task
->fail_nth
));
1403 len
= simple_read_from_buffer(buf
, count
, ppos
, numbuf
, len
);
1404 put_task_struct(task
);
1409 static const struct file_operations proc_fail_nth_operations
= {
1410 .read
= proc_fail_nth_read
,
1411 .write
= proc_fail_nth_write
,
1416 #ifdef CONFIG_SCHED_DEBUG
1418 * Print out various scheduling related per-task fields:
1420 static int sched_show(struct seq_file
*m
, void *v
)
1422 struct inode
*inode
= m
->private;
1423 struct pid_namespace
*ns
= inode
->i_sb
->s_fs_info
;
1424 struct task_struct
*p
;
1426 p
= get_proc_task(inode
);
1429 proc_sched_show_task(p
, ns
, m
);
1437 sched_write(struct file
*file
, const char __user
*buf
,
1438 size_t count
, loff_t
*offset
)
1440 struct inode
*inode
= file_inode(file
);
1441 struct task_struct
*p
;
1443 p
= get_proc_task(inode
);
1446 proc_sched_set_task(p
);
1453 static int sched_open(struct inode
*inode
, struct file
*filp
)
1455 return single_open(filp
, sched_show
, inode
);
1458 static const struct file_operations proc_pid_sched_operations
= {
1461 .write
= sched_write
,
1462 .llseek
= seq_lseek
,
1463 .release
= single_release
,
1468 #ifdef CONFIG_SCHED_AUTOGROUP
1470 * Print out autogroup related information:
1472 static int sched_autogroup_show(struct seq_file
*m
, void *v
)
1474 struct inode
*inode
= m
->private;
1475 struct task_struct
*p
;
1477 p
= get_proc_task(inode
);
1480 proc_sched_autogroup_show_task(p
, m
);
1488 sched_autogroup_write(struct file
*file
, const char __user
*buf
,
1489 size_t count
, loff_t
*offset
)
1491 struct inode
*inode
= file_inode(file
);
1492 struct task_struct
*p
;
1493 char buffer
[PROC_NUMBUF
];
1497 memset(buffer
, 0, sizeof(buffer
));
1498 if (count
> sizeof(buffer
) - 1)
1499 count
= sizeof(buffer
) - 1;
1500 if (copy_from_user(buffer
, buf
, count
))
1503 err
= kstrtoint(strstrip(buffer
), 0, &nice
);
1507 p
= get_proc_task(inode
);
1511 err
= proc_sched_autogroup_set_nice(p
, nice
);
1520 static int sched_autogroup_open(struct inode
*inode
, struct file
*filp
)
1524 ret
= single_open(filp
, sched_autogroup_show
, NULL
);
1526 struct seq_file
*m
= filp
->private_data
;
1533 static const struct file_operations proc_pid_sched_autogroup_operations
= {
1534 .open
= sched_autogroup_open
,
1536 .write
= sched_autogroup_write
,
1537 .llseek
= seq_lseek
,
1538 .release
= single_release
,
1541 #endif /* CONFIG_SCHED_AUTOGROUP */
1543 static ssize_t
comm_write(struct file
*file
, const char __user
*buf
,
1544 size_t count
, loff_t
*offset
)
1546 struct inode
*inode
= file_inode(file
);
1547 struct task_struct
*p
;
1548 char buffer
[TASK_COMM_LEN
];
1549 const size_t maxlen
= sizeof(buffer
) - 1;
1551 memset(buffer
, 0, sizeof(buffer
));
1552 if (copy_from_user(buffer
, buf
, count
> maxlen
? maxlen
: count
))
1555 p
= get_proc_task(inode
);
1559 if (same_thread_group(current
, p
))
1560 set_task_comm(p
, buffer
);
1569 static int comm_show(struct seq_file
*m
, void *v
)
1571 struct inode
*inode
= m
->private;
1572 struct task_struct
*p
;
1574 p
= get_proc_task(inode
);
1579 seq_printf(m
, "%s\n", p
->comm
);
1587 static int comm_open(struct inode
*inode
, struct file
*filp
)
1589 return single_open(filp
, comm_show
, inode
);
1592 static const struct file_operations proc_pid_set_comm_operations
= {
1595 .write
= comm_write
,
1596 .llseek
= seq_lseek
,
1597 .release
= single_release
,
1600 static int proc_exe_link(struct dentry
*dentry
, struct path
*exe_path
)
1602 struct task_struct
*task
;
1603 struct file
*exe_file
;
1605 task
= get_proc_task(d_inode(dentry
));
1608 exe_file
= get_task_exe_file(task
);
1609 put_task_struct(task
);
1611 *exe_path
= exe_file
->f_path
;
1612 path_get(&exe_file
->f_path
);
1619 static const char *proc_pid_get_link(struct dentry
*dentry
,
1620 struct inode
*inode
,
1621 struct delayed_call
*done
)
1624 int error
= -EACCES
;
1627 return ERR_PTR(-ECHILD
);
1629 /* Are we allowed to snoop on the tasks file descriptors? */
1630 if (!proc_fd_access_allowed(inode
))
1633 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1637 nd_jump_link(&path
);
1640 return ERR_PTR(error
);
1643 static int do_proc_readlink(struct path
*path
, char __user
*buffer
, int buflen
)
1645 char *tmp
= (char *)__get_free_page(GFP_KERNEL
);
1652 pathname
= d_path(path
, tmp
, PAGE_SIZE
);
1653 len
= PTR_ERR(pathname
);
1654 if (IS_ERR(pathname
))
1656 len
= tmp
+ PAGE_SIZE
- 1 - pathname
;
1660 if (copy_to_user(buffer
, pathname
, len
))
1663 free_page((unsigned long)tmp
);
1667 static int proc_pid_readlink(struct dentry
* dentry
, char __user
* buffer
, int buflen
)
1669 int error
= -EACCES
;
1670 struct inode
*inode
= d_inode(dentry
);
1673 /* Are we allowed to snoop on the tasks file descriptors? */
1674 if (!proc_fd_access_allowed(inode
))
1677 error
= PROC_I(inode
)->op
.proc_get_link(dentry
, &path
);
1681 error
= do_proc_readlink(&path
, buffer
, buflen
);
1687 const struct inode_operations proc_pid_link_inode_operations
= {
1688 .readlink
= proc_pid_readlink
,
1689 .get_link
= proc_pid_get_link
,
1690 .setattr
= proc_setattr
,
1694 /* building an inode */
1696 void task_dump_owner(struct task_struct
*task
, mode_t mode
,
1697 kuid_t
*ruid
, kgid_t
*rgid
)
1699 /* Depending on the state of dumpable compute who should own a
1700 * proc file for a task.
1702 const struct cred
*cred
;
1706 if (unlikely(task
->flags
& PF_KTHREAD
)) {
1707 *ruid
= GLOBAL_ROOT_UID
;
1708 *rgid
= GLOBAL_ROOT_GID
;
1712 /* Default to the tasks effective ownership */
1714 cred
= __task_cred(task
);
1720 * Before the /proc/pid/status file was created the only way to read
1721 * the effective uid of a /process was to stat /proc/pid. Reading
1722 * /proc/pid/status is slow enough that procps and other packages
1723 * kept stating /proc/pid. To keep the rules in /proc simple I have
1724 * made this apply to all per process world readable and executable
1727 if (mode
!= (S_IFDIR
|S_IRUGO
|S_IXUGO
)) {
1728 struct mm_struct
*mm
;
1731 /* Make non-dumpable tasks owned by some root */
1733 if (get_dumpable(mm
) != SUID_DUMP_USER
) {
1734 struct user_namespace
*user_ns
= mm
->user_ns
;
1736 uid
= make_kuid(user_ns
, 0);
1737 if (!uid_valid(uid
))
1738 uid
= GLOBAL_ROOT_UID
;
1740 gid
= make_kgid(user_ns
, 0);
1741 if (!gid_valid(gid
))
1742 gid
= GLOBAL_ROOT_GID
;
1745 uid
= GLOBAL_ROOT_UID
;
1746 gid
= GLOBAL_ROOT_GID
;
1754 struct inode
*proc_pid_make_inode(struct super_block
* sb
,
1755 struct task_struct
*task
, umode_t mode
)
1757 struct inode
* inode
;
1758 struct proc_inode
*ei
;
1760 /* We need a new inode */
1762 inode
= new_inode(sb
);
1768 inode
->i_mode
= mode
;
1769 inode
->i_ino
= get_next_ino();
1770 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= current_time(inode
);
1771 inode
->i_op
= &proc_def_inode_operations
;
1774 * grab the reference to task.
1776 ei
->pid
= get_task_pid(task
, PIDTYPE_PID
);
1780 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1781 security_task_to_inode(task
, inode
);
1791 int pid_getattr(const struct path
*path
, struct kstat
*stat
,
1792 u32 request_mask
, unsigned int query_flags
)
1794 struct inode
*inode
= d_inode(path
->dentry
);
1795 struct task_struct
*task
;
1796 struct pid_namespace
*pid
= path
->dentry
->d_sb
->s_fs_info
;
1798 generic_fillattr(inode
, stat
);
1801 stat
->uid
= GLOBAL_ROOT_UID
;
1802 stat
->gid
= GLOBAL_ROOT_GID
;
1803 task
= pid_task(proc_pid(inode
), PIDTYPE_PID
);
1805 if (!has_pid_permissions(pid
, task
, HIDEPID_INVISIBLE
)) {
1808 * This doesn't prevent learning whether PID exists,
1809 * it only makes getattr() consistent with readdir().
1813 task_dump_owner(task
, inode
->i_mode
, &stat
->uid
, &stat
->gid
);
1822 * Exceptional case: normally we are not allowed to unhash a busy
1823 * directory. In this case, however, we can do it - no aliasing problems
1824 * due to the way we treat inodes.
1826 * Rewrite the inode's ownerships here because the owning task may have
1827 * performed a setuid(), etc.
1830 int pid_revalidate(struct dentry
*dentry
, unsigned int flags
)
1832 struct inode
*inode
;
1833 struct task_struct
*task
;
1835 if (flags
& LOOKUP_RCU
)
1838 inode
= d_inode(dentry
);
1839 task
= get_proc_task(inode
);
1842 task_dump_owner(task
, inode
->i_mode
, &inode
->i_uid
, &inode
->i_gid
);
1844 inode
->i_mode
&= ~(S_ISUID
| S_ISGID
);
1845 security_task_to_inode(task
, inode
);
1846 put_task_struct(task
);
1852 static inline bool proc_inode_is_dead(struct inode
*inode
)
1854 return !proc_pid(inode
)->tasks
[PIDTYPE_PID
].first
;
1857 int pid_delete_dentry(const struct dentry
*dentry
)
1859 /* Is the task we represent dead?
1860 * If so, then don't put the dentry on the lru list,
1861 * kill it immediately.
1863 return proc_inode_is_dead(d_inode(dentry
));
1866 const struct dentry_operations pid_dentry_operations
=
1868 .d_revalidate
= pid_revalidate
,
1869 .d_delete
= pid_delete_dentry
,
1875 * Fill a directory entry.
1877 * If possible create the dcache entry and derive our inode number and
1878 * file type from dcache entry.
1880 * Since all of the proc inode numbers are dynamically generated, the inode
1881 * numbers do not exist until the inode is cache. This means creating the
1882 * the dcache entry in readdir is necessary to keep the inode numbers
1883 * reported by readdir in sync with the inode numbers reported
1886 bool proc_fill_cache(struct file
*file
, struct dir_context
*ctx
,
1887 const char *name
, int len
,
1888 instantiate_t instantiate
, struct task_struct
*task
, const void *ptr
)
1890 struct dentry
*child
, *dir
= file
->f_path
.dentry
;
1891 struct qstr qname
= QSTR_INIT(name
, len
);
1892 struct inode
*inode
;
1896 child
= d_hash_and_lookup(dir
, &qname
);
1898 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq
);
1899 child
= d_alloc_parallel(dir
, &qname
, &wq
);
1901 goto end_instantiate
;
1902 if (d_in_lookup(child
)) {
1903 int err
= instantiate(d_inode(dir
), child
, task
, ptr
);
1904 d_lookup_done(child
);
1907 goto end_instantiate
;
1911 inode
= d_inode(child
);
1913 type
= inode
->i_mode
>> 12;
1915 return dir_emit(ctx
, name
, len
, ino
, type
);
1918 return dir_emit(ctx
, name
, len
, 1, DT_UNKNOWN
);
1922 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1923 * which represent vma start and end addresses.
1925 static int dname_to_vma_addr(struct dentry
*dentry
,
1926 unsigned long *start
, unsigned long *end
)
1928 const char *str
= dentry
->d_name
.name
;
1929 unsigned long long sval
, eval
;
1932 len
= _parse_integer(str
, 16, &sval
);
1933 if (len
& KSTRTOX_OVERFLOW
)
1935 if (sval
!= (unsigned long)sval
)
1943 len
= _parse_integer(str
, 16, &eval
);
1944 if (len
& KSTRTOX_OVERFLOW
)
1946 if (eval
!= (unsigned long)eval
)
1959 static int map_files_d_revalidate(struct dentry
*dentry
, unsigned int flags
)
1961 unsigned long vm_start
, vm_end
;
1962 bool exact_vma_exists
= false;
1963 struct mm_struct
*mm
= NULL
;
1964 struct task_struct
*task
;
1965 struct inode
*inode
;
1968 if (flags
& LOOKUP_RCU
)
1971 inode
= d_inode(dentry
);
1972 task
= get_proc_task(inode
);
1976 mm
= mm_access(task
, PTRACE_MODE_READ_FSCREDS
);
1977 if (IS_ERR_OR_NULL(mm
))
1980 if (!dname_to_vma_addr(dentry
, &vm_start
, &vm_end
)) {
1981 down_read(&mm
->mmap_sem
);
1982 exact_vma_exists
= !!find_exact_vma(mm
, vm_start
, vm_end
);
1983 up_read(&mm
->mmap_sem
);
1988 if (exact_vma_exists
) {
1989 task_dump_owner(task
, 0, &inode
->i_uid
, &inode
->i_gid
);
1991 security_task_to_inode(task
, inode
);
1996 put_task_struct(task
);
2002 static const struct dentry_operations tid_map_files_dentry_operations
= {
2003 .d_revalidate
= map_files_d_revalidate
,
2004 .d_delete
= pid_delete_dentry
,
2007 static int map_files_get_link(struct dentry
*dentry
, struct path
*path
)
2009 unsigned long vm_start
, vm_end
;
2010 struct vm_area_struct
*vma
;
2011 struct task_struct
*task
;
2012 struct mm_struct
*mm
;
2016 task
= get_proc_task(d_inode(dentry
));
2020 mm
= get_task_mm(task
);
2021 put_task_struct(task
);
2025 rc
= dname_to_vma_addr(dentry
, &vm_start
, &vm_end
);
2030 down_read(&mm
->mmap_sem
);
2031 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2032 if (vma
&& vma
->vm_file
) {
2033 *path
= vma
->vm_file
->f_path
;
2037 up_read(&mm
->mmap_sem
);
2045 struct map_files_info
{
2048 unsigned char name
[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
2052 * Only allow CAP_SYS_ADMIN to follow the links, due to concerns about how the
2053 * symlinks may be used to bypass permissions on ancestor directories in the
2054 * path to the file in question.
2057 proc_map_files_get_link(struct dentry
*dentry
,
2058 struct inode
*inode
,
2059 struct delayed_call
*done
)
2061 if (!capable(CAP_SYS_ADMIN
))
2062 return ERR_PTR(-EPERM
);
2064 return proc_pid_get_link(dentry
, inode
, done
);
2068 * Identical to proc_pid_link_inode_operations except for get_link()
2070 static const struct inode_operations proc_map_files_link_inode_operations
= {
2071 .readlink
= proc_pid_readlink
,
2072 .get_link
= proc_map_files_get_link
,
2073 .setattr
= proc_setattr
,
2077 proc_map_files_instantiate(struct inode
*dir
, struct dentry
*dentry
,
2078 struct task_struct
*task
, const void *ptr
)
2080 fmode_t mode
= (fmode_t
)(unsigned long)ptr
;
2081 struct proc_inode
*ei
;
2082 struct inode
*inode
;
2084 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFLNK
|
2085 ((mode
& FMODE_READ
) ? S_IRUSR
: 0) |
2086 ((mode
& FMODE_WRITE
) ? S_IWUSR
: 0));
2091 ei
->op
.proc_get_link
= map_files_get_link
;
2093 inode
->i_op
= &proc_map_files_link_inode_operations
;
2096 d_set_d_op(dentry
, &tid_map_files_dentry_operations
);
2097 d_add(dentry
, inode
);
2102 static struct dentry
*proc_map_files_lookup(struct inode
*dir
,
2103 struct dentry
*dentry
, unsigned int flags
)
2105 unsigned long vm_start
, vm_end
;
2106 struct vm_area_struct
*vma
;
2107 struct task_struct
*task
;
2109 struct mm_struct
*mm
;
2112 task
= get_proc_task(dir
);
2117 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2121 if (dname_to_vma_addr(dentry
, &vm_start
, &vm_end
))
2124 mm
= get_task_mm(task
);
2128 down_read(&mm
->mmap_sem
);
2129 vma
= find_exact_vma(mm
, vm_start
, vm_end
);
2134 result
= proc_map_files_instantiate(dir
, dentry
, task
,
2135 (void *)(unsigned long)vma
->vm_file
->f_mode
);
2138 up_read(&mm
->mmap_sem
);
2141 put_task_struct(task
);
2143 return ERR_PTR(result
);
2146 static const struct inode_operations proc_map_files_inode_operations
= {
2147 .lookup
= proc_map_files_lookup
,
2148 .permission
= proc_fd_permission
,
2149 .setattr
= proc_setattr
,
2153 proc_map_files_readdir(struct file
*file
, struct dir_context
*ctx
)
2155 struct vm_area_struct
*vma
;
2156 struct task_struct
*task
;
2157 struct mm_struct
*mm
;
2158 unsigned long nr_files
, pos
, i
;
2159 struct flex_array
*fa
= NULL
;
2160 struct map_files_info info
;
2161 struct map_files_info
*p
;
2165 task
= get_proc_task(file_inode(file
));
2170 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
))
2174 if (!dir_emit_dots(file
, ctx
))
2177 mm
= get_task_mm(task
);
2180 down_read(&mm
->mmap_sem
);
2185 * We need two passes here:
2187 * 1) Collect vmas of mapped files with mmap_sem taken
2188 * 2) Release mmap_sem and instantiate entries
2190 * otherwise we get lockdep complained, since filldir()
2191 * routine might require mmap_sem taken in might_fault().
2194 for (vma
= mm
->mmap
, pos
= 2; vma
; vma
= vma
->vm_next
) {
2195 if (vma
->vm_file
&& ++pos
> ctx
->pos
)
2200 fa
= flex_array_alloc(sizeof(info
), nr_files
,
2202 if (!fa
|| flex_array_prealloc(fa
, 0, nr_files
,
2206 flex_array_free(fa
);
2207 up_read(&mm
->mmap_sem
);
2211 for (i
= 0, vma
= mm
->mmap
, pos
= 2; vma
;
2212 vma
= vma
->vm_next
) {
2215 if (++pos
<= ctx
->pos
)
2218 info
.mode
= vma
->vm_file
->f_mode
;
2219 info
.len
= snprintf(info
.name
,
2220 sizeof(info
.name
), "%lx-%lx",
2221 vma
->vm_start
, vma
->vm_end
);
2222 if (flex_array_put(fa
, i
++, &info
, GFP_KERNEL
))
2226 up_read(&mm
->mmap_sem
);
2228 for (i
= 0; i
< nr_files
; i
++) {
2229 p
= flex_array_get(fa
, i
);
2230 if (!proc_fill_cache(file
, ctx
,
2232 proc_map_files_instantiate
,
2234 (void *)(unsigned long)p
->mode
))
2239 flex_array_free(fa
);
2243 put_task_struct(task
);
2248 static const struct file_operations proc_map_files_operations
= {
2249 .read
= generic_read_dir
,
2250 .iterate_shared
= proc_map_files_readdir
,
2251 .llseek
= generic_file_llseek
,
2254 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
2255 struct timers_private
{
2257 struct task_struct
*task
;
2258 struct sighand_struct
*sighand
;
2259 struct pid_namespace
*ns
;
2260 unsigned long flags
;
2263 static void *timers_start(struct seq_file
*m
, loff_t
*pos
)
2265 struct timers_private
*tp
= m
->private;
2267 tp
->task
= get_pid_task(tp
->pid
, PIDTYPE_PID
);
2269 return ERR_PTR(-ESRCH
);
2271 tp
->sighand
= lock_task_sighand(tp
->task
, &tp
->flags
);
2273 return ERR_PTR(-ESRCH
);
2275 return seq_list_start(&tp
->task
->signal
->posix_timers
, *pos
);
2278 static void *timers_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
2280 struct timers_private
*tp
= m
->private;
2281 return seq_list_next(v
, &tp
->task
->signal
->posix_timers
, pos
);
2284 static void timers_stop(struct seq_file
*m
, void *v
)
2286 struct timers_private
*tp
= m
->private;
2289 unlock_task_sighand(tp
->task
, &tp
->flags
);
2294 put_task_struct(tp
->task
);
2299 static int show_timer(struct seq_file
*m
, void *v
)
2301 struct k_itimer
*timer
;
2302 struct timers_private
*tp
= m
->private;
2304 static const char * const nstr
[] = {
2305 [SIGEV_SIGNAL
] = "signal",
2306 [SIGEV_NONE
] = "none",
2307 [SIGEV_THREAD
] = "thread",
2310 timer
= list_entry((struct list_head
*)v
, struct k_itimer
, list
);
2311 notify
= timer
->it_sigev_notify
;
2313 seq_printf(m
, "ID: %d\n", timer
->it_id
);
2314 seq_printf(m
, "signal: %d/%p\n",
2315 timer
->sigq
->info
.si_signo
,
2316 timer
->sigq
->info
.si_value
.sival_ptr
);
2317 seq_printf(m
, "notify: %s/%s.%d\n",
2318 nstr
[notify
& ~SIGEV_THREAD_ID
],
2319 (notify
& SIGEV_THREAD_ID
) ? "tid" : "pid",
2320 pid_nr_ns(timer
->it_pid
, tp
->ns
));
2321 seq_printf(m
, "ClockID: %d\n", timer
->it_clock
);
2326 static const struct seq_operations proc_timers_seq_ops
= {
2327 .start
= timers_start
,
2328 .next
= timers_next
,
2329 .stop
= timers_stop
,
2333 static int proc_timers_open(struct inode
*inode
, struct file
*file
)
2335 struct timers_private
*tp
;
2337 tp
= __seq_open_private(file
, &proc_timers_seq_ops
,
2338 sizeof(struct timers_private
));
2342 tp
->pid
= proc_pid(inode
);
2343 tp
->ns
= inode
->i_sb
->s_fs_info
;
2347 static const struct file_operations proc_timers_operations
= {
2348 .open
= proc_timers_open
,
2350 .llseek
= seq_lseek
,
2351 .release
= seq_release_private
,
2355 static ssize_t
timerslack_ns_write(struct file
*file
, const char __user
*buf
,
2356 size_t count
, loff_t
*offset
)
2358 struct inode
*inode
= file_inode(file
);
2359 struct task_struct
*p
;
2363 err
= kstrtoull_from_user(buf
, count
, 10, &slack_ns
);
2367 p
= get_proc_task(inode
);
2372 if (!capable(CAP_SYS_NICE
)) {
2377 err
= security_task_setscheduler(p
);
2386 p
->timer_slack_ns
= p
->default_timer_slack_ns
;
2388 p
->timer_slack_ns
= slack_ns
;
2397 static int timerslack_ns_show(struct seq_file
*m
, void *v
)
2399 struct inode
*inode
= m
->private;
2400 struct task_struct
*p
;
2403 p
= get_proc_task(inode
);
2409 if (!capable(CAP_SYS_NICE
)) {
2413 err
= security_task_getscheduler(p
);
2419 seq_printf(m
, "%llu\n", p
->timer_slack_ns
);
2428 static int timerslack_ns_open(struct inode
*inode
, struct file
*filp
)
2430 return single_open(filp
, timerslack_ns_show
, inode
);
2433 static const struct file_operations proc_pid_set_timerslack_ns_operations
= {
2434 .open
= timerslack_ns_open
,
2436 .write
= timerslack_ns_write
,
2437 .llseek
= seq_lseek
,
2438 .release
= single_release
,
2441 static int proc_pident_instantiate(struct inode
*dir
,
2442 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
2444 const struct pid_entry
*p
= ptr
;
2445 struct inode
*inode
;
2446 struct proc_inode
*ei
;
2448 inode
= proc_pid_make_inode(dir
->i_sb
, task
, p
->mode
);
2453 if (S_ISDIR(inode
->i_mode
))
2454 set_nlink(inode
, 2); /* Use getattr to fix if necessary */
2456 inode
->i_op
= p
->iop
;
2458 inode
->i_fop
= p
->fop
;
2460 d_set_d_op(dentry
, &pid_dentry_operations
);
2461 d_add(dentry
, inode
);
2462 /* Close the race of the process dying before we return the dentry */
2463 if (pid_revalidate(dentry
, 0))
2469 static struct dentry
*proc_pident_lookup(struct inode
*dir
,
2470 struct dentry
*dentry
,
2471 const struct pid_entry
*ents
,
2475 struct task_struct
*task
= get_proc_task(dir
);
2476 const struct pid_entry
*p
, *last
;
2484 * Yes, it does not scale. And it should not. Don't add
2485 * new entries into /proc/<tgid>/ without very good reasons.
2487 last
= &ents
[nents
];
2488 for (p
= ents
; p
< last
; p
++) {
2489 if (p
->len
!= dentry
->d_name
.len
)
2491 if (!memcmp(dentry
->d_name
.name
, p
->name
, p
->len
))
2497 error
= proc_pident_instantiate(dir
, dentry
, task
, p
);
2499 put_task_struct(task
);
2501 return ERR_PTR(error
);
2504 static int proc_pident_readdir(struct file
*file
, struct dir_context
*ctx
,
2505 const struct pid_entry
*ents
, unsigned int nents
)
2507 struct task_struct
*task
= get_proc_task(file_inode(file
));
2508 const struct pid_entry
*p
;
2513 if (!dir_emit_dots(file
, ctx
))
2516 if (ctx
->pos
>= nents
+ 2)
2519 for (p
= ents
+ (ctx
->pos
- 2); p
< ents
+ nents
; p
++) {
2520 if (!proc_fill_cache(file
, ctx
, p
->name
, p
->len
,
2521 proc_pident_instantiate
, task
, p
))
2526 put_task_struct(task
);
2530 #ifdef CONFIG_SECURITY
2531 static ssize_t
proc_pid_attr_read(struct file
* file
, char __user
* buf
,
2532 size_t count
, loff_t
*ppos
)
2534 struct inode
* inode
= file_inode(file
);
2537 struct task_struct
*task
= get_proc_task(inode
);
2542 length
= security_getprocattr(task
,
2543 (char*)file
->f_path
.dentry
->d_name
.name
,
2545 put_task_struct(task
);
2547 length
= simple_read_from_buffer(buf
, count
, ppos
, p
, length
);
2552 static ssize_t
proc_pid_attr_write(struct file
* file
, const char __user
* buf
,
2553 size_t count
, loff_t
*ppos
)
2555 struct inode
* inode
= file_inode(file
);
2558 struct task_struct
*task
= get_proc_task(inode
);
2564 /* A task may only write its own attributes. */
2566 if (current
!= task
)
2569 if (count
> PAGE_SIZE
)
2572 /* No partial writes. */
2577 page
= memdup_user(buf
, count
);
2579 length
= PTR_ERR(page
);
2583 /* Guard against adverse ptrace interaction */
2584 length
= mutex_lock_interruptible(¤t
->signal
->cred_guard_mutex
);
2588 length
= security_setprocattr(file
->f_path
.dentry
->d_name
.name
,
2590 mutex_unlock(¤t
->signal
->cred_guard_mutex
);
2594 put_task_struct(task
);
2599 static const struct file_operations proc_pid_attr_operations
= {
2600 .read
= proc_pid_attr_read
,
2601 .write
= proc_pid_attr_write
,
2602 .llseek
= generic_file_llseek
,
2605 static const struct pid_entry attr_dir_stuff
[] = {
2606 REG("current", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2607 REG("prev", S_IRUGO
, proc_pid_attr_operations
),
2608 REG("exec", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2609 REG("fscreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2610 REG("keycreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2611 REG("sockcreate", S_IRUGO
|S_IWUGO
, proc_pid_attr_operations
),
2614 static int proc_attr_dir_readdir(struct file
*file
, struct dir_context
*ctx
)
2616 return proc_pident_readdir(file
, ctx
,
2617 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2620 static const struct file_operations proc_attr_dir_operations
= {
2621 .read
= generic_read_dir
,
2622 .iterate_shared
= proc_attr_dir_readdir
,
2623 .llseek
= generic_file_llseek
,
2626 static struct dentry
*proc_attr_dir_lookup(struct inode
*dir
,
2627 struct dentry
*dentry
, unsigned int flags
)
2629 return proc_pident_lookup(dir
, dentry
,
2630 attr_dir_stuff
, ARRAY_SIZE(attr_dir_stuff
));
2633 static const struct inode_operations proc_attr_dir_inode_operations
= {
2634 .lookup
= proc_attr_dir_lookup
,
2635 .getattr
= pid_getattr
,
2636 .setattr
= proc_setattr
,
2641 #ifdef CONFIG_ELF_CORE
2642 static ssize_t
proc_coredump_filter_read(struct file
*file
, char __user
*buf
,
2643 size_t count
, loff_t
*ppos
)
2645 struct task_struct
*task
= get_proc_task(file_inode(file
));
2646 struct mm_struct
*mm
;
2647 char buffer
[PROC_NUMBUF
];
2655 mm
= get_task_mm(task
);
2657 len
= snprintf(buffer
, sizeof(buffer
), "%08lx\n",
2658 ((mm
->flags
& MMF_DUMP_FILTER_MASK
) >>
2659 MMF_DUMP_FILTER_SHIFT
));
2661 ret
= simple_read_from_buffer(buf
, count
, ppos
, buffer
, len
);
2664 put_task_struct(task
);
2669 static ssize_t
proc_coredump_filter_write(struct file
*file
,
2670 const char __user
*buf
,
2674 struct task_struct
*task
;
2675 struct mm_struct
*mm
;
2681 ret
= kstrtouint_from_user(buf
, count
, 0, &val
);
2686 task
= get_proc_task(file_inode(file
));
2690 mm
= get_task_mm(task
);
2695 for (i
= 0, mask
= 1; i
< MMF_DUMP_FILTER_BITS
; i
++, mask
<<= 1) {
2697 set_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2699 clear_bit(i
+ MMF_DUMP_FILTER_SHIFT
, &mm
->flags
);
2704 put_task_struct(task
);
2711 static const struct file_operations proc_coredump_filter_operations
= {
2712 .read
= proc_coredump_filter_read
,
2713 .write
= proc_coredump_filter_write
,
2714 .llseek
= generic_file_llseek
,
2718 #ifdef CONFIG_TASK_IO_ACCOUNTING
2719 static int do_io_accounting(struct task_struct
*task
, struct seq_file
*m
, int whole
)
2721 struct task_io_accounting acct
= task
->ioac
;
2722 unsigned long flags
;
2725 result
= mutex_lock_killable(&task
->signal
->cred_guard_mutex
);
2729 if (!ptrace_may_access(task
, PTRACE_MODE_READ_FSCREDS
)) {
2734 if (whole
&& lock_task_sighand(task
, &flags
)) {
2735 struct task_struct
*t
= task
;
2737 task_io_accounting_add(&acct
, &task
->signal
->ioac
);
2738 while_each_thread(task
, t
)
2739 task_io_accounting_add(&acct
, &t
->ioac
);
2741 unlock_task_sighand(task
, &flags
);
2748 "read_bytes: %llu\n"
2749 "write_bytes: %llu\n"
2750 "cancelled_write_bytes: %llu\n",
2751 (unsigned long long)acct
.rchar
,
2752 (unsigned long long)acct
.wchar
,
2753 (unsigned long long)acct
.syscr
,
2754 (unsigned long long)acct
.syscw
,
2755 (unsigned long long)acct
.read_bytes
,
2756 (unsigned long long)acct
.write_bytes
,
2757 (unsigned long long)acct
.cancelled_write_bytes
);
2761 mutex_unlock(&task
->signal
->cred_guard_mutex
);
2765 static int proc_tid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2766 struct pid
*pid
, struct task_struct
*task
)
2768 return do_io_accounting(task
, m
, 0);
2771 static int proc_tgid_io_accounting(struct seq_file
*m
, struct pid_namespace
*ns
,
2772 struct pid
*pid
, struct task_struct
*task
)
2774 return do_io_accounting(task
, m
, 1);
2776 #endif /* CONFIG_TASK_IO_ACCOUNTING */
2778 #ifdef CONFIG_USER_NS
2779 static int proc_id_map_open(struct inode
*inode
, struct file
*file
,
2780 const struct seq_operations
*seq_ops
)
2782 struct user_namespace
*ns
= NULL
;
2783 struct task_struct
*task
;
2784 struct seq_file
*seq
;
2787 task
= get_proc_task(inode
);
2790 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2792 put_task_struct(task
);
2797 ret
= seq_open(file
, seq_ops
);
2801 seq
= file
->private_data
;
2811 static int proc_id_map_release(struct inode
*inode
, struct file
*file
)
2813 struct seq_file
*seq
= file
->private_data
;
2814 struct user_namespace
*ns
= seq
->private;
2816 return seq_release(inode
, file
);
2819 static int proc_uid_map_open(struct inode
*inode
, struct file
*file
)
2821 return proc_id_map_open(inode
, file
, &proc_uid_seq_operations
);
2824 static int proc_gid_map_open(struct inode
*inode
, struct file
*file
)
2826 return proc_id_map_open(inode
, file
, &proc_gid_seq_operations
);
2829 static int proc_projid_map_open(struct inode
*inode
, struct file
*file
)
2831 return proc_id_map_open(inode
, file
, &proc_projid_seq_operations
);
2834 static const struct file_operations proc_uid_map_operations
= {
2835 .open
= proc_uid_map_open
,
2836 .write
= proc_uid_map_write
,
2838 .llseek
= seq_lseek
,
2839 .release
= proc_id_map_release
,
2842 static const struct file_operations proc_gid_map_operations
= {
2843 .open
= proc_gid_map_open
,
2844 .write
= proc_gid_map_write
,
2846 .llseek
= seq_lseek
,
2847 .release
= proc_id_map_release
,
2850 static const struct file_operations proc_projid_map_operations
= {
2851 .open
= proc_projid_map_open
,
2852 .write
= proc_projid_map_write
,
2854 .llseek
= seq_lseek
,
2855 .release
= proc_id_map_release
,
2858 static int proc_setgroups_open(struct inode
*inode
, struct file
*file
)
2860 struct user_namespace
*ns
= NULL
;
2861 struct task_struct
*task
;
2865 task
= get_proc_task(inode
);
2868 ns
= get_user_ns(task_cred_xxx(task
, user_ns
));
2870 put_task_struct(task
);
2875 if (file
->f_mode
& FMODE_WRITE
) {
2877 if (!ns_capable(ns
, CAP_SYS_ADMIN
))
2881 ret
= single_open(file
, &proc_setgroups_show
, ns
);
2892 static int proc_setgroups_release(struct inode
*inode
, struct file
*file
)
2894 struct seq_file
*seq
= file
->private_data
;
2895 struct user_namespace
*ns
= seq
->private;
2896 int ret
= single_release(inode
, file
);
2901 static const struct file_operations proc_setgroups_operations
= {
2902 .open
= proc_setgroups_open
,
2903 .write
= proc_setgroups_write
,
2905 .llseek
= seq_lseek
,
2906 .release
= proc_setgroups_release
,
2908 #endif /* CONFIG_USER_NS */
2910 static int proc_pid_personality(struct seq_file
*m
, struct pid_namespace
*ns
,
2911 struct pid
*pid
, struct task_struct
*task
)
2913 int err
= lock_trace(task
);
2915 seq_printf(m
, "%08x\n", task
->personality
);
2921 #ifdef CONFIG_LIVEPATCH
2922 static int proc_pid_patch_state(struct seq_file
*m
, struct pid_namespace
*ns
,
2923 struct pid
*pid
, struct task_struct
*task
)
2925 seq_printf(m
, "%d\n", task
->patch_state
);
2928 #endif /* CONFIG_LIVEPATCH */
2933 static const struct file_operations proc_task_operations
;
2934 static const struct inode_operations proc_task_inode_operations
;
2936 static const struct pid_entry tgid_base_stuff
[] = {
2937 DIR("task", S_IRUGO
|S_IXUGO
, proc_task_inode_operations
, proc_task_operations
),
2938 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
2939 DIR("map_files", S_IRUSR
|S_IXUSR
, proc_map_files_inode_operations
, proc_map_files_operations
),
2940 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
2941 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
2943 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
2945 REG("environ", S_IRUSR
, proc_environ_operations
),
2946 REG("auxv", S_IRUSR
, proc_auxv_operations
),
2947 ONE("status", S_IRUGO
, proc_pid_status
),
2948 ONE("personality", S_IRUSR
, proc_pid_personality
),
2949 ONE("limits", S_IRUGO
, proc_pid_limits
),
2950 #ifdef CONFIG_SCHED_DEBUG
2951 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
2953 #ifdef CONFIG_SCHED_AUTOGROUP
2954 REG("autogroup", S_IRUGO
|S_IWUSR
, proc_pid_sched_autogroup_operations
),
2956 REG("comm", S_IRUGO
|S_IWUSR
, proc_pid_set_comm_operations
),
2957 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2958 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
2960 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
2961 ONE("stat", S_IRUGO
, proc_tgid_stat
),
2962 ONE("statm", S_IRUGO
, proc_pid_statm
),
2963 REG("maps", S_IRUGO
, proc_pid_maps_operations
),
2965 REG("numa_maps", S_IRUGO
, proc_pid_numa_maps_operations
),
2967 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
2968 LNK("cwd", proc_cwd_link
),
2969 LNK("root", proc_root_link
),
2970 LNK("exe", proc_exe_link
),
2971 REG("mounts", S_IRUGO
, proc_mounts_operations
),
2972 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
2973 REG("mountstats", S_IRUSR
, proc_mountstats_operations
),
2974 #ifdef CONFIG_PROC_PAGE_MONITOR
2975 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
2976 REG("smaps", S_IRUGO
, proc_pid_smaps_operations
),
2977 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
2978 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
2980 #ifdef CONFIG_SECURITY
2981 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
2983 #ifdef CONFIG_KALLSYMS
2984 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
2986 #ifdef CONFIG_STACKTRACE
2987 ONE("stack", S_IRUSR
, proc_pid_stack
),
2989 #ifdef CONFIG_SCHED_INFO
2990 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
2992 #ifdef CONFIG_LATENCYTOP
2993 REG("latency", S_IRUGO
, proc_lstats_operations
),
2995 #ifdef CONFIG_PROC_PID_CPUSET
2996 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
2998 #ifdef CONFIG_CGROUPS
2999 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3001 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3002 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3003 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3004 #ifdef CONFIG_AUDITSYSCALL
3005 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3006 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3008 #ifdef CONFIG_FAULT_INJECTION
3009 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3010 REG("fail-nth", 0644, proc_fail_nth_operations
),
3012 #ifdef CONFIG_ELF_CORE
3013 REG("coredump_filter", S_IRUGO
|S_IWUSR
, proc_coredump_filter_operations
),
3015 #ifdef CONFIG_TASK_IO_ACCOUNTING
3016 ONE("io", S_IRUSR
, proc_tgid_io_accounting
),
3018 #ifdef CONFIG_HARDWALL
3019 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3021 #ifdef CONFIG_USER_NS
3022 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3023 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3024 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3025 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3027 #if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
3028 REG("timers", S_IRUGO
, proc_timers_operations
),
3030 REG("timerslack_ns", S_IRUGO
|S_IWUGO
, proc_pid_set_timerslack_ns_operations
),
3031 #ifdef CONFIG_LIVEPATCH
3032 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3036 static int proc_tgid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3038 return proc_pident_readdir(file
, ctx
,
3039 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3042 static const struct file_operations proc_tgid_base_operations
= {
3043 .read
= generic_read_dir
,
3044 .iterate_shared
= proc_tgid_base_readdir
,
3045 .llseek
= generic_file_llseek
,
3048 static struct dentry
*proc_tgid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3050 return proc_pident_lookup(dir
, dentry
,
3051 tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));
3054 static const struct inode_operations proc_tgid_base_inode_operations
= {
3055 .lookup
= proc_tgid_base_lookup
,
3056 .getattr
= pid_getattr
,
3057 .setattr
= proc_setattr
,
3058 .permission
= proc_pid_permission
,
3061 static void proc_flush_task_mnt(struct vfsmount
*mnt
, pid_t pid
, pid_t tgid
)
3063 struct dentry
*dentry
, *leader
, *dir
;
3064 char buf
[PROC_NUMBUF
];
3068 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3069 /* no ->d_hash() rejects on procfs */
3070 dentry
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3072 d_invalidate(dentry
);
3080 name
.len
= snprintf(buf
, sizeof(buf
), "%d", tgid
);
3081 leader
= d_hash_and_lookup(mnt
->mnt_root
, &name
);
3086 name
.len
= strlen(name
.name
);
3087 dir
= d_hash_and_lookup(leader
, &name
);
3089 goto out_put_leader
;
3092 name
.len
= snprintf(buf
, sizeof(buf
), "%d", pid
);
3093 dentry
= d_hash_and_lookup(dir
, &name
);
3095 d_invalidate(dentry
);
3107 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
3108 * @task: task that should be flushed.
3110 * When flushing dentries from proc, one needs to flush them from global
3111 * proc (proc_mnt) and from all the namespaces' procs this task was seen
3112 * in. This call is supposed to do all of this job.
3114 * Looks in the dcache for
3116 * /proc/@tgid/task/@pid
3117 * if either directory is present flushes it and all of it'ts children
3120 * It is safe and reasonable to cache /proc entries for a task until
3121 * that task exits. After that they just clog up the dcache with
3122 * useless entries, possibly causing useful dcache entries to be
3123 * flushed instead. This routine is proved to flush those useless
3124 * dcache entries at process exit time.
3126 * NOTE: This routine is just an optimization so it does not guarantee
3127 * that no dcache entries will exist at process exit time it
3128 * just makes it very unlikely that any will persist.
3131 void proc_flush_task(struct task_struct
*task
)
3134 struct pid
*pid
, *tgid
;
3137 pid
= task_pid(task
);
3138 tgid
= task_tgid(task
);
3140 for (i
= 0; i
<= pid
->level
; i
++) {
3141 upid
= &pid
->numbers
[i
];
3142 proc_flush_task_mnt(upid
->ns
->proc_mnt
, upid
->nr
,
3143 tgid
->numbers
[i
].nr
);
3147 static int proc_pid_instantiate(struct inode
*dir
,
3148 struct dentry
* dentry
,
3149 struct task_struct
*task
, const void *ptr
)
3151 struct inode
*inode
;
3153 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3157 inode
->i_op
= &proc_tgid_base_inode_operations
;
3158 inode
->i_fop
= &proc_tgid_base_operations
;
3159 inode
->i_flags
|=S_IMMUTABLE
;
3161 set_nlink(inode
, nlink_tgid
);
3163 d_set_d_op(dentry
, &pid_dentry_operations
);
3165 d_add(dentry
, inode
);
3166 /* Close the race of the process dying before we return the dentry */
3167 if (pid_revalidate(dentry
, 0))
3173 struct dentry
*proc_pid_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3175 int result
= -ENOENT
;
3176 struct task_struct
*task
;
3178 struct pid_namespace
*ns
;
3180 tgid
= name_to_int(&dentry
->d_name
);
3184 ns
= dentry
->d_sb
->s_fs_info
;
3186 task
= find_task_by_pid_ns(tgid
, ns
);
3188 get_task_struct(task
);
3193 result
= proc_pid_instantiate(dir
, dentry
, task
, NULL
);
3194 put_task_struct(task
);
3196 return ERR_PTR(result
);
3200 * Find the first task with tgid >= tgid
3205 struct task_struct
*task
;
3207 static struct tgid_iter
next_tgid(struct pid_namespace
*ns
, struct tgid_iter iter
)
3212 put_task_struct(iter
.task
);
3216 pid
= find_ge_pid(iter
.tgid
, ns
);
3218 iter
.tgid
= pid_nr_ns(pid
, ns
);
3219 iter
.task
= pid_task(pid
, PIDTYPE_PID
);
3220 /* What we to know is if the pid we have find is the
3221 * pid of a thread_group_leader. Testing for task
3222 * being a thread_group_leader is the obvious thing
3223 * todo but there is a window when it fails, due to
3224 * the pid transfer logic in de_thread.
3226 * So we perform the straight forward test of seeing
3227 * if the pid we have found is the pid of a thread
3228 * group leader, and don't worry if the task we have
3229 * found doesn't happen to be a thread group leader.
3230 * As we don't care in the case of readdir.
3232 if (!iter
.task
|| !has_group_leader_pid(iter
.task
)) {
3236 get_task_struct(iter
.task
);
3242 #define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
3244 /* for the /proc/ directory itself, after non-process stuff has been done */
3245 int proc_pid_readdir(struct file
*file
, struct dir_context
*ctx
)
3247 struct tgid_iter iter
;
3248 struct pid_namespace
*ns
= file_inode(file
)->i_sb
->s_fs_info
;
3249 loff_t pos
= ctx
->pos
;
3251 if (pos
>= PID_MAX_LIMIT
+ TGID_OFFSET
)
3254 if (pos
== TGID_OFFSET
- 2) {
3255 struct inode
*inode
= d_inode(ns
->proc_self
);
3256 if (!dir_emit(ctx
, "self", 4, inode
->i_ino
, DT_LNK
))
3258 ctx
->pos
= pos
= pos
+ 1;
3260 if (pos
== TGID_OFFSET
- 1) {
3261 struct inode
*inode
= d_inode(ns
->proc_thread_self
);
3262 if (!dir_emit(ctx
, "thread-self", 11, inode
->i_ino
, DT_LNK
))
3264 ctx
->pos
= pos
= pos
+ 1;
3266 iter
.tgid
= pos
- TGID_OFFSET
;
3268 for (iter
= next_tgid(ns
, iter
);
3270 iter
.tgid
+= 1, iter
= next_tgid(ns
, iter
)) {
3271 char name
[PROC_NUMBUF
];
3275 if (!has_pid_permissions(ns
, iter
.task
, HIDEPID_INVISIBLE
))
3278 len
= snprintf(name
, sizeof(name
), "%d", iter
.tgid
);
3279 ctx
->pos
= iter
.tgid
+ TGID_OFFSET
;
3280 if (!proc_fill_cache(file
, ctx
, name
, len
,
3281 proc_pid_instantiate
, iter
.task
, NULL
)) {
3282 put_task_struct(iter
.task
);
3286 ctx
->pos
= PID_MAX_LIMIT
+ TGID_OFFSET
;
3291 * proc_tid_comm_permission is a special permission function exclusively
3292 * used for the node /proc/<pid>/task/<tid>/comm.
3293 * It bypasses generic permission checks in the case where a task of the same
3294 * task group attempts to access the node.
3295 * The rationale behind this is that glibc and bionic access this node for
3296 * cross thread naming (pthread_set/getname_np(!self)). However, if
3297 * PR_SET_DUMPABLE gets set to 0 this node among others becomes uid=0 gid=0,
3298 * which locks out the cross thread naming implementation.
3299 * This function makes sure that the node is always accessible for members of
3300 * same thread group.
3302 static int proc_tid_comm_permission(struct inode
*inode
, int mask
)
3304 bool is_same_tgroup
;
3305 struct task_struct
*task
;
3307 task
= get_proc_task(inode
);
3310 is_same_tgroup
= same_thread_group(current
, task
);
3311 put_task_struct(task
);
3313 if (likely(is_same_tgroup
&& !(mask
& MAY_EXEC
))) {
3314 /* This file (/proc/<pid>/task/<tid>/comm) can always be
3315 * read or written by the members of the corresponding
3321 return generic_permission(inode
, mask
);
3324 static const struct inode_operations proc_tid_comm_inode_operations
= {
3325 .permission
= proc_tid_comm_permission
,
3331 static const struct pid_entry tid_base_stuff
[] = {
3332 DIR("fd", S_IRUSR
|S_IXUSR
, proc_fd_inode_operations
, proc_fd_operations
),
3333 DIR("fdinfo", S_IRUSR
|S_IXUSR
, proc_fdinfo_inode_operations
, proc_fdinfo_operations
),
3334 DIR("ns", S_IRUSR
|S_IXUGO
, proc_ns_dir_inode_operations
, proc_ns_dir_operations
),
3336 DIR("net", S_IRUGO
|S_IXUGO
, proc_net_inode_operations
, proc_net_operations
),
3338 REG("environ", S_IRUSR
, proc_environ_operations
),
3339 REG("auxv", S_IRUSR
, proc_auxv_operations
),
3340 ONE("status", S_IRUGO
, proc_pid_status
),
3341 ONE("personality", S_IRUSR
, proc_pid_personality
),
3342 ONE("limits", S_IRUGO
, proc_pid_limits
),
3343 #ifdef CONFIG_SCHED_DEBUG
3344 REG("sched", S_IRUGO
|S_IWUSR
, proc_pid_sched_operations
),
3346 NOD("comm", S_IFREG
|S_IRUGO
|S_IWUSR
,
3347 &proc_tid_comm_inode_operations
,
3348 &proc_pid_set_comm_operations
, {}),
3349 #ifdef CONFIG_HAVE_ARCH_TRACEHOOK
3350 ONE("syscall", S_IRUSR
, proc_pid_syscall
),
3352 REG("cmdline", S_IRUGO
, proc_pid_cmdline_ops
),
3353 ONE("stat", S_IRUGO
, proc_tid_stat
),
3354 ONE("statm", S_IRUGO
, proc_pid_statm
),
3355 REG("maps", S_IRUGO
, proc_tid_maps_operations
),
3356 #ifdef CONFIG_PROC_CHILDREN
3357 REG("children", S_IRUGO
, proc_tid_children_operations
),
3360 REG("numa_maps", S_IRUGO
, proc_tid_numa_maps_operations
),
3362 REG("mem", S_IRUSR
|S_IWUSR
, proc_mem_operations
),
3363 LNK("cwd", proc_cwd_link
),
3364 LNK("root", proc_root_link
),
3365 LNK("exe", proc_exe_link
),
3366 REG("mounts", S_IRUGO
, proc_mounts_operations
),
3367 REG("mountinfo", S_IRUGO
, proc_mountinfo_operations
),
3368 #ifdef CONFIG_PROC_PAGE_MONITOR
3369 REG("clear_refs", S_IWUSR
, proc_clear_refs_operations
),
3370 REG("smaps", S_IRUGO
, proc_tid_smaps_operations
),
3371 REG("smaps_rollup", S_IRUGO
, proc_pid_smaps_rollup_operations
),
3372 REG("pagemap", S_IRUSR
, proc_pagemap_operations
),
3374 #ifdef CONFIG_SECURITY
3375 DIR("attr", S_IRUGO
|S_IXUGO
, proc_attr_dir_inode_operations
, proc_attr_dir_operations
),
3377 #ifdef CONFIG_KALLSYMS
3378 ONE("wchan", S_IRUGO
, proc_pid_wchan
),
3380 #ifdef CONFIG_STACKTRACE
3381 ONE("stack", S_IRUSR
, proc_pid_stack
),
3383 #ifdef CONFIG_SCHED_INFO
3384 ONE("schedstat", S_IRUGO
, proc_pid_schedstat
),
3386 #ifdef CONFIG_LATENCYTOP
3387 REG("latency", S_IRUGO
, proc_lstats_operations
),
3389 #ifdef CONFIG_PROC_PID_CPUSET
3390 ONE("cpuset", S_IRUGO
, proc_cpuset_show
),
3392 #ifdef CONFIG_CGROUPS
3393 ONE("cgroup", S_IRUGO
, proc_cgroup_show
),
3395 ONE("oom_score", S_IRUGO
, proc_oom_score
),
3396 REG("oom_adj", S_IRUGO
|S_IWUSR
, proc_oom_adj_operations
),
3397 REG("oom_score_adj", S_IRUGO
|S_IWUSR
, proc_oom_score_adj_operations
),
3398 #ifdef CONFIG_AUDITSYSCALL
3399 REG("loginuid", S_IWUSR
|S_IRUGO
, proc_loginuid_operations
),
3400 REG("sessionid", S_IRUGO
, proc_sessionid_operations
),
3402 #ifdef CONFIG_FAULT_INJECTION
3403 REG("make-it-fail", S_IRUGO
|S_IWUSR
, proc_fault_inject_operations
),
3404 REG("fail-nth", 0644, proc_fail_nth_operations
),
3406 #ifdef CONFIG_TASK_IO_ACCOUNTING
3407 ONE("io", S_IRUSR
, proc_tid_io_accounting
),
3409 #ifdef CONFIG_HARDWALL
3410 ONE("hardwall", S_IRUGO
, proc_pid_hardwall
),
3412 #ifdef CONFIG_USER_NS
3413 REG("uid_map", S_IRUGO
|S_IWUSR
, proc_uid_map_operations
),
3414 REG("gid_map", S_IRUGO
|S_IWUSR
, proc_gid_map_operations
),
3415 REG("projid_map", S_IRUGO
|S_IWUSR
, proc_projid_map_operations
),
3416 REG("setgroups", S_IRUGO
|S_IWUSR
, proc_setgroups_operations
),
3418 #ifdef CONFIG_LIVEPATCH
3419 ONE("patch_state", S_IRUSR
, proc_pid_patch_state
),
3423 static int proc_tid_base_readdir(struct file
*file
, struct dir_context
*ctx
)
3425 return proc_pident_readdir(file
, ctx
,
3426 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3429 static struct dentry
*proc_tid_base_lookup(struct inode
*dir
, struct dentry
*dentry
, unsigned int flags
)
3431 return proc_pident_lookup(dir
, dentry
,
3432 tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3435 static const struct file_operations proc_tid_base_operations
= {
3436 .read
= generic_read_dir
,
3437 .iterate_shared
= proc_tid_base_readdir
,
3438 .llseek
= generic_file_llseek
,
3441 static const struct inode_operations proc_tid_base_inode_operations
= {
3442 .lookup
= proc_tid_base_lookup
,
3443 .getattr
= pid_getattr
,
3444 .setattr
= proc_setattr
,
3447 static int proc_task_instantiate(struct inode
*dir
,
3448 struct dentry
*dentry
, struct task_struct
*task
, const void *ptr
)
3450 struct inode
*inode
;
3451 inode
= proc_pid_make_inode(dir
->i_sb
, task
, S_IFDIR
| S_IRUGO
| S_IXUGO
);
3455 inode
->i_op
= &proc_tid_base_inode_operations
;
3456 inode
->i_fop
= &proc_tid_base_operations
;
3457 inode
->i_flags
|=S_IMMUTABLE
;
3459 set_nlink(inode
, nlink_tid
);
3461 d_set_d_op(dentry
, &pid_dentry_operations
);
3463 d_add(dentry
, inode
);
3464 /* Close the race of the process dying before we return the dentry */
3465 if (pid_revalidate(dentry
, 0))
3471 static struct dentry
*proc_task_lookup(struct inode
*dir
, struct dentry
* dentry
, unsigned int flags
)
3473 int result
= -ENOENT
;
3474 struct task_struct
*task
;
3475 struct task_struct
*leader
= get_proc_task(dir
);
3477 struct pid_namespace
*ns
;
3482 tid
= name_to_int(&dentry
->d_name
);
3486 ns
= dentry
->d_sb
->s_fs_info
;
3488 task
= find_task_by_pid_ns(tid
, ns
);
3490 get_task_struct(task
);
3494 if (!same_thread_group(leader
, task
))
3497 result
= proc_task_instantiate(dir
, dentry
, task
, NULL
);
3499 put_task_struct(task
);
3501 put_task_struct(leader
);
3503 return ERR_PTR(result
);
3507 * Find the first tid of a thread group to return to user space.
3509 * Usually this is just the thread group leader, but if the users
3510 * buffer was too small or there was a seek into the middle of the
3511 * directory we have more work todo.
3513 * In the case of a short read we start with find_task_by_pid.
3515 * In the case of a seek we start with the leader and walk nr
3518 static struct task_struct
*first_tid(struct pid
*pid
, int tid
, loff_t f_pos
,
3519 struct pid_namespace
*ns
)
3521 struct task_struct
*pos
, *task
;
3522 unsigned long nr
= f_pos
;
3524 if (nr
!= f_pos
) /* 32bit overflow? */
3528 task
= pid_task(pid
, PIDTYPE_PID
);
3532 /* Attempt to start with the tid of a thread */
3534 pos
= find_task_by_pid_ns(tid
, ns
);
3535 if (pos
&& same_thread_group(pos
, task
))
3539 /* If nr exceeds the number of threads there is nothing todo */
3540 if (nr
>= get_nr_threads(task
))
3543 /* If we haven't found our starting place yet start
3544 * with the leader and walk nr threads forward.
3546 pos
= task
= task
->group_leader
;
3550 } while_each_thread(task
, pos
);
3555 get_task_struct(pos
);
3562 * Find the next thread in the thread list.
3563 * Return NULL if there is an error or no next thread.
3565 * The reference to the input task_struct is released.
3567 static struct task_struct
*next_tid(struct task_struct
*start
)
3569 struct task_struct
*pos
= NULL
;
3571 if (pid_alive(start
)) {
3572 pos
= next_thread(start
);
3573 if (thread_group_leader(pos
))
3576 get_task_struct(pos
);
3579 put_task_struct(start
);
3583 /* for the /proc/TGID/task/ directories */
3584 static int proc_task_readdir(struct file
*file
, struct dir_context
*ctx
)
3586 struct inode
*inode
= file_inode(file
);
3587 struct task_struct
*task
;
3588 struct pid_namespace
*ns
;
3591 if (proc_inode_is_dead(inode
))
3594 if (!dir_emit_dots(file
, ctx
))
3597 /* f_version caches the tgid value that the last readdir call couldn't
3598 * return. lseek aka telldir automagically resets f_version to 0.
3600 ns
= inode
->i_sb
->s_fs_info
;
3601 tid
= (int)file
->f_version
;
3602 file
->f_version
= 0;
3603 for (task
= first_tid(proc_pid(inode
), tid
, ctx
->pos
- 2, ns
);
3605 task
= next_tid(task
), ctx
->pos
++) {
3606 char name
[PROC_NUMBUF
];
3608 tid
= task_pid_nr_ns(task
, ns
);
3609 len
= snprintf(name
, sizeof(name
), "%d", tid
);
3610 if (!proc_fill_cache(file
, ctx
, name
, len
,
3611 proc_task_instantiate
, task
, NULL
)) {
3612 /* returning this tgid failed, save it as the first
3613 * pid for the next readir call */
3614 file
->f_version
= (u64
)tid
;
3615 put_task_struct(task
);
3623 static int proc_task_getattr(const struct path
*path
, struct kstat
*stat
,
3624 u32 request_mask
, unsigned int query_flags
)
3626 struct inode
*inode
= d_inode(path
->dentry
);
3627 struct task_struct
*p
= get_proc_task(inode
);
3628 generic_fillattr(inode
, stat
);
3631 stat
->nlink
+= get_nr_threads(p
);
3638 static const struct inode_operations proc_task_inode_operations
= {
3639 .lookup
= proc_task_lookup
,
3640 .getattr
= proc_task_getattr
,
3641 .setattr
= proc_setattr
,
3642 .permission
= proc_pid_permission
,
3645 static const struct file_operations proc_task_operations
= {
3646 .read
= generic_read_dir
,
3647 .iterate_shared
= proc_task_readdir
,
3648 .llseek
= generic_file_llseek
,
3651 void __init
set_proc_pid_nlink(void)
3653 nlink_tid
= pid_entry_nlink(tid_base_stuff
, ARRAY_SIZE(tid_base_stuff
));
3654 nlink_tgid
= pid_entry_nlink(tgid_base_stuff
, ARRAY_SIZE(tgid_base_stuff
));