blob: 4c542b9077547357d2cf973a75055488b197a981 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
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.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
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>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
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.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040090#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080093#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070097/* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct pid_entry {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700108 const char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 int len;
Al Virod161a132011-07-24 03:36:29 -0400110 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800111 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800112 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700118 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 .mode = MODE, \
120 .iop = IOP, \
121 .fop = FOP, \
122 .op = OP, \
123}
124
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300125#define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700129 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300130 { .proc_get_link = get_link } )
131#define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300133#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800134 NOD(NAME, (S_IFREG|(MODE)), \
135 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Vegard Nossumaed54172008-06-05 22:46:53 -0700138/*
139 * Count the number of hardlinks for the pid_entry table, excluding the .
140 * and .. links.
141 */
142static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
143 unsigned int n)
144{
145 unsigned int i;
146 unsigned int count;
147
148 count = 0;
149 for (i = 0; i < n; ++i) {
150 if (S_ISDIR(entries[i].mode))
151 ++count;
152 }
153
154 return count;
155}
156
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200157static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000159 int result = -ENOENT;
160
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200162 if (task->fs) {
163 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000164 result = 0;
165 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168}
169
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800170static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800172 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700174
175 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200176 task_lock(task);
177 if (task->fs) {
178 get_fs_pwd(task->fs, path);
179 result = 0;
180 }
181 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return result;
185}
186
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800187static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800189 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700191
192 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200193 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194 put_task_struct(task);
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return result;
197}
198
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700199static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
200 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700202 /*
203 * Rely on struct seq_operations::show() being called once
204 * per internal buffer allocation. See single_open(), traverse().
205 */
206 BUG_ON(m->size < PAGE_SIZE);
207 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700211static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
212 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Cong Wange7dcd992012-05-31 16:26:17 -0700214 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500215 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300217 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300219 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700220 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 mmput(mm);
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700222 return 0;
223 } else
224 return PTR_ERR(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227
228#ifdef CONFIG_KALLSYMS
229/*
230 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
231 * Returns the resolved symbol. If that fails, simply return the address.
232 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700233static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
234 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700236 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700237 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 wchan = get_wchan(task);
240
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700241 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600242 if (!ptrace_may_access(task, PTRACE_MODE_READ))
243 return 0;
244 else
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700245 return seq_printf(m, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700246 else
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700247 return seq_printf(m, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249#endif /* CONFIG_KALLSYMS */
250
Al Viroa9712bc2011-03-23 15:52:50 -0400251static int lock_trace(struct task_struct *task)
252{
253 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
254 if (err)
255 return err;
256 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
257 mutex_unlock(&task->signal->cred_guard_mutex);
258 return -EPERM;
259 }
260 return 0;
261}
262
263static void unlock_trace(struct task_struct *task)
264{
265 mutex_unlock(&task->signal->cred_guard_mutex);
266}
267
Ken Chen2ec220e2008-11-10 11:26:08 +0300268#ifdef CONFIG_STACKTRACE
269
270#define MAX_STACK_TRACE_DEPTH 64
271
272static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
273 struct pid *pid, struct task_struct *task)
274{
275 struct stack_trace trace;
276 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400277 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300278 int i;
279
280 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
281 if (!entries)
282 return -ENOMEM;
283
284 trace.nr_entries = 0;
285 trace.max_entries = MAX_STACK_TRACE_DEPTH;
286 trace.entries = entries;
287 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300288
Al Viroa9712bc2011-03-23 15:52:50 -0400289 err = lock_trace(task);
290 if (!err) {
291 save_stack_trace_tsk(task, &trace);
292
293 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700294 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400295 (void *)entries[i], (void *)entries[i]);
296 }
297 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300298 }
299 kfree(entries);
300
Al Viroa9712bc2011-03-23 15:52:50 -0400301 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300302}
303#endif
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#ifdef CONFIG_SCHEDSTATS
306/*
307 * Provides /proc/PID/schedstat
308 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700309static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
310 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700312 return seq_printf(m, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100313 (unsigned long long)task->se.sum_exec_runtime,
314 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200315 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317#endif
318
Arjan van de Ven97455122008-01-25 21:08:34 +0100319#ifdef CONFIG_LATENCYTOP
320static int lstats_show_proc(struct seq_file *m, void *v)
321{
322 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800323 struct inode *inode = m->private;
324 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100325
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800326 if (!task)
327 return -ESRCH;
328 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100329 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800330 struct latency_record *lr = &task->latency_record[i];
331 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100332 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800333 seq_printf(m, "%i %li %li",
334 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100335 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800336 unsigned long bt = lr->backtrace[q];
337 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100338 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800339 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100340 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800341 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100342 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800343 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100344 }
345
346 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800347 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100348 return 0;
349}
350
351static int lstats_open(struct inode *inode, struct file *file)
352{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800353 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800354}
355
Arjan van de Ven97455122008-01-25 21:08:34 +0100356static ssize_t lstats_write(struct file *file, const char __user *buf,
357 size_t count, loff_t *offs)
358{
Al Viro496ad9a2013-01-23 17:07:38 -0500359 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100360
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800361 if (!task)
362 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100363 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800364 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100365
366 return count;
367}
368
369static const struct file_operations proc_lstats_operations = {
370 .open = lstats_open,
371 .read = seq_read,
372 .write = lstats_write,
373 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100375};
376
377#endif
378
Al Viro8d8b97b2013-04-19 23:11:24 -0400379#ifdef CONFIG_CGROUPS
380static int cgroup_open(struct inode *inode, struct file *file)
381{
382 struct pid *pid = PROC_I(inode)->pid;
383 return single_open(file, proc_cgroup_show, pid);
384}
385
386static const struct file_operations proc_cgroup_operations = {
387 .open = cgroup_open,
388 .read = seq_read,
389 .llseek = seq_lseek,
390 .release = single_release,
391};
392#endif
393
394#ifdef CONFIG_PROC_PID_CPUSET
395
396static int cpuset_open(struct inode *inode, struct file *file)
397{
398 struct pid *pid = PROC_I(inode)->pid;
399 return single_open(file, proc_cpuset_show, pid);
400}
401
402static const struct file_operations proc_cpuset_operations = {
403 .open = cpuset_open,
404 .read = seq_read,
405 .llseek = seq_lseek,
406 .release = single_release,
407};
408#endif
409
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700410static int proc_oom_score(struct seq_file *m, struct pid_namespace *ns,
411 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
David Rientjesa7f638f2012-05-29 15:06:47 -0700413 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200414 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700416 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200417 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700418 points = oom_badness(task, NULL, NULL, totalpages) *
419 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700420 read_unlock(&tasklist_lock);
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -0700421 return seq_printf(m, "%lu\n", points);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Neil Hormand85f50d2007-10-18 23:40:37 -0700424struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700425 const char *name;
426 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700427};
428
429static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700430 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700431 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
432 [RLIMIT_DATA] = {"Max data size", "bytes"},
433 [RLIMIT_STACK] = {"Max stack size", "bytes"},
434 [RLIMIT_CORE] = {"Max core file size", "bytes"},
435 [RLIMIT_RSS] = {"Max resident set", "bytes"},
436 [RLIMIT_NPROC] = {"Max processes", "processes"},
437 [RLIMIT_NOFILE] = {"Max open files", "files"},
438 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
439 [RLIMIT_AS] = {"Max address space", "bytes"},
440 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
441 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
442 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
443 [RLIMIT_NICE] = {"Max nice priority", NULL},
444 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800445 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700446};
447
448/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700449static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
450 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700451{
452 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700453 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700454
455 struct rlimit rlim[RLIM_NLIMITS];
456
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400457 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700458 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700459 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
460 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700461
462 /*
463 * print the file header
464 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700465 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700466 "Limit", "Soft Limit", "Hard Limit", "Units");
467
468 for (i = 0; i < RLIM_NLIMITS; i++) {
469 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700470 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700471 lnames[i].name, "unlimited");
472 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700473 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700474 lnames[i].name, rlim[i].rlim_cur);
475
476 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700477 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700478 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700479 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700480
481 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700482 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700483 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700484 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700485 }
486
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700487 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700488}
489
Roland McGrathebcb6732008-07-25 19:46:00 -0700490#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700491static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
492 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700493{
494 long nr;
495 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400496 int res = lock_trace(task);
497 if (res)
498 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700499
500 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700501 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400502 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700503 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400504 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700505 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700506 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
507 nr,
508 args[0], args[1], args[2], args[3], args[4], args[5],
509 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400510 unlock_trace(task);
511 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700512}
513#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/************************************************************************/
516/* Here the fs part begins */
517/************************************************************************/
518
519/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700520static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700522 struct task_struct *task;
523 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700524 /* Allow access to a task's file descriptors if it is us or we
525 * may use ptrace attach to the process and find out that
526 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700527 */
528 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700529 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400530 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700531 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700532 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700533 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800536int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700537{
538 int error;
539 struct inode *inode = dentry->d_inode;
540
541 if (attr->ia_valid & ATTR_MODE)
542 return -EPERM;
543
544 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200545 if (error)
546 return error;
547
Christoph Hellwig10257742010-06-04 11:30:02 +0200548 setattr_copy(inode, attr);
549 mark_inode_dirty(inode);
550 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700551}
552
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800553/*
554 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
555 * or euid/egid (for hide_pid_min=2)?
556 */
557static bool has_pid_permissions(struct pid_namespace *pid,
558 struct task_struct *task,
559 int hide_pid_min)
560{
561 if (pid->hide_pid < hide_pid_min)
562 return true;
563 if (in_group_p(pid->pid_gid))
564 return true;
565 return ptrace_may_access(task, PTRACE_MODE_READ);
566}
567
568
569static int proc_pid_permission(struct inode *inode, int mask)
570{
571 struct pid_namespace *pid = inode->i_sb->s_fs_info;
572 struct task_struct *task;
573 bool has_perms;
574
575 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800576 if (!task)
577 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800578 has_perms = has_pid_permissions(pid, task, 1);
579 put_task_struct(task);
580
581 if (!has_perms) {
582 if (pid->hide_pid == 2) {
583 /*
584 * Let's make getdents(), stat(), and open()
585 * consistent with each other. If a process
586 * may not stat() a file, it shouldn't be seen
587 * in procfs at all.
588 */
589 return -ENOENT;
590 }
591
592 return -EPERM;
593 }
594 return generic_permission(inode, mask);
595}
596
597
598
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800599static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700600 .setattr = proc_setattr,
601};
602
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800603static int proc_single_show(struct seq_file *m, void *v)
604{
605 struct inode *inode = m->private;
606 struct pid_namespace *ns;
607 struct pid *pid;
608 struct task_struct *task;
609 int ret;
610
611 ns = inode->i_sb->s_fs_info;
612 pid = proc_pid(inode);
613 task = get_pid_task(pid, PIDTYPE_PID);
614 if (!task)
615 return -ESRCH;
616
617 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
618
619 put_task_struct(task);
620 return ret;
621}
622
623static int proc_single_open(struct inode *inode, struct file *filp)
624{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800625 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800626}
627
628static const struct file_operations proc_single_file_operations = {
629 .open = proc_single_open,
630 .read = seq_read,
631 .llseek = seq_lseek,
632 .release = single_release,
633};
634
Oleg Nesterov5381e162014-10-09 15:25:24 -0700635
636struct mm_struct *proc_mem_open(struct inode *inode, unsigned int mode)
637{
638 struct task_struct *task = get_proc_task(inode);
639 struct mm_struct *mm = ERR_PTR(-ESRCH);
640
641 if (task) {
642 mm = mm_access(task, mode);
643 put_task_struct(task);
644
645 if (!IS_ERR_OR_NULL(mm)) {
646 /* ensure this mm_struct can't be freed */
647 atomic_inc(&mm->mm_count);
648 /* but do not pin its memory */
649 mmput(mm);
650 }
651 }
652
653 return mm;
654}
655
Cong Wangb409e572012-05-31 16:26:17 -0700656static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Oleg Nesterov5381e162014-10-09 15:25:24 -0700658 struct mm_struct *mm = proc_mem_open(inode, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800659
660 if (IS_ERR(mm))
661 return PTR_ERR(mm);
662
Linus Torvaldse2683372012-01-17 15:21:19 -0800663 file->private_data = mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
665}
666
Cong Wangb409e572012-05-31 16:26:17 -0700667static int mem_open(struct inode *inode, struct file *file)
668{
Djalal Harounibc452b4b2012-07-30 14:42:28 -0700669 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
670
671 /* OK to pass negative loff_t, we can catch out-of-range */
672 file->f_mode |= FMODE_UNSIGNED_OFFSET;
673
674 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700675}
676
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100677static ssize_t mem_rw(struct file *file, char __user *buf,
678 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Linus Torvaldse2683372012-01-17 15:21:19 -0800680 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100681 unsigned long addr = *ppos;
682 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvaldse2683372012-01-17 15:21:19 -0800685 if (!mm)
686 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Mel Gormane12ba742007-10-16 01:25:52 -0700688 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800690 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700692 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100693 if (!atomic_inc_not_zero(&mm->mm_users))
694 goto free;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100697 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100699 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 copied = -EFAULT;
701 break;
702 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100703
704 this_len = access_remote_vm(mm, addr, page, this_len, write);
705 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (!copied)
707 copied = -EIO;
708 break;
709 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100710
711 if (!write && copy_to_user(buf, page, this_len)) {
712 copied = -EFAULT;
713 break;
714 }
715
716 buf += this_len;
717 addr += this_len;
718 copied += this_len;
719 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100721 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700722
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100723 mmput(mm);
724free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700725 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return copied;
727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100729static ssize_t mem_read(struct file *file, char __user *buf,
730 size_t count, loff_t *ppos)
731{
732 return mem_rw(file, buf, count, ppos, 0);
733}
734
735static ssize_t mem_write(struct file *file, const char __user *buf,
736 size_t count, loff_t *ppos)
737{
738 return mem_rw(file, (char __user*)buf, count, ppos, 1);
739}
740
Matt Mackall85863e42008-02-04 22:29:04 -0800741loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 switch (orig) {
744 case 0:
745 file->f_pos = offset;
746 break;
747 case 1:
748 file->f_pos += offset;
749 break;
750 default:
751 return -EINVAL;
752 }
753 force_successful_syscall_return();
754 return file->f_pos;
755}
756
Linus Torvaldse2683372012-01-17 15:21:19 -0800757static int mem_release(struct inode *inode, struct file *file)
758{
759 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100760 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100761 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800762 return 0;
763}
764
Arjan van de Ven00977a52007-02-12 00:55:34 -0800765static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 .llseek = mem_lseek,
767 .read = mem_read,
768 .write = mem_write,
769 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800770 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771};
772
Cong Wangb409e572012-05-31 16:26:17 -0700773static int environ_open(struct inode *inode, struct file *file)
774{
775 return __mem_open(inode, file, PTRACE_MODE_READ);
776}
777
James Pearson315e28c2007-10-16 23:30:17 -0700778static ssize_t environ_read(struct file *file, char __user *buf,
779 size_t count, loff_t *ppos)
780{
James Pearson315e28c2007-10-16 23:30:17 -0700781 char *page;
782 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700783 int ret = 0;
784 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700785
Cong Wangb409e572012-05-31 16:26:17 -0700786 if (!mm)
787 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700788
James Pearson315e28c2007-10-16 23:30:17 -0700789 page = (char *)__get_free_page(GFP_TEMPORARY);
790 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700791 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700792
Al Virod6f64b82011-02-15 22:26:01 -0500793 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700794 if (!atomic_inc_not_zero(&mm->mm_users))
795 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700796 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700797 size_t this_len, max_len;
798 int retval;
799
800 if (src >= (mm->env_end - mm->env_start))
801 break;
James Pearson315e28c2007-10-16 23:30:17 -0700802
803 this_len = mm->env_end - (mm->env_start + src);
804
Djalal Harounie8905ec2012-07-30 14:42:26 -0700805 max_len = min_t(size_t, PAGE_SIZE, count);
806 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700807
Cong Wangb409e572012-05-31 16:26:17 -0700808 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700809 page, this_len, 0);
810
811 if (retval <= 0) {
812 ret = retval;
813 break;
814 }
815
816 if (copy_to_user(buf, page, retval)) {
817 ret = -EFAULT;
818 break;
819 }
820
821 ret += retval;
822 src += retval;
823 buf += retval;
824 count -= retval;
825 }
826 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700827 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700828
829free:
James Pearson315e28c2007-10-16 23:30:17 -0700830 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700831 return ret;
832}
833
834static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700835 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700836 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100837 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700838 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700839};
840
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800841static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
842 loff_t *ppos)
843{
Al Viro496ad9a2013-01-23 17:07:38 -0500844 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800845 char buffer[PROC_NUMBUF];
846 int oom_adj = OOM_ADJUST_MIN;
847 size_t len;
848 unsigned long flags;
849
850 if (!task)
851 return -ESRCH;
852 if (lock_task_sighand(task, &flags)) {
853 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
854 oom_adj = OOM_ADJUST_MAX;
855 else
856 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
857 OOM_SCORE_ADJ_MAX;
858 unlock_task_sighand(task, &flags);
859 }
860 put_task_struct(task);
861 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
862 return simple_read_from_buffer(buf, count, ppos, buffer, len);
863}
864
865static ssize_t oom_adj_write(struct file *file, const char __user *buf,
866 size_t count, loff_t *ppos)
867{
868 struct task_struct *task;
869 char buffer[PROC_NUMBUF];
870 int oom_adj;
871 unsigned long flags;
872 int err;
873
874 memset(buffer, 0, sizeof(buffer));
875 if (count > sizeof(buffer) - 1)
876 count = sizeof(buffer) - 1;
877 if (copy_from_user(buffer, buf, count)) {
878 err = -EFAULT;
879 goto out;
880 }
881
882 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
883 if (err)
884 goto out;
885 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
886 oom_adj != OOM_DISABLE) {
887 err = -EINVAL;
888 goto out;
889 }
890
Al Viro496ad9a2013-01-23 17:07:38 -0500891 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800892 if (!task) {
893 err = -ESRCH;
894 goto out;
895 }
896
897 task_lock(task);
898 if (!task->mm) {
899 err = -EINVAL;
900 goto err_task_lock;
901 }
902
903 if (!lock_task_sighand(task, &flags)) {
904 err = -ESRCH;
905 goto err_task_lock;
906 }
907
908 /*
909 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
910 * value is always attainable.
911 */
912 if (oom_adj == OOM_ADJUST_MAX)
913 oom_adj = OOM_SCORE_ADJ_MAX;
914 else
915 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
916
917 if (oom_adj < task->signal->oom_score_adj &&
918 !capable(CAP_SYS_RESOURCE)) {
919 err = -EACCES;
920 goto err_sighand;
921 }
922
923 /*
924 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
925 * /proc/pid/oom_score_adj instead.
926 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800927 pr_warn_once("%s (%d): /proc/%d/oom_adj is deprecated, please use /proc/%d/oom_score_adj instead.\n",
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800928 current->comm, task_pid_nr(current), task_pid_nr(task),
929 task_pid_nr(task));
930
931 task->signal->oom_score_adj = oom_adj;
932 trace_oom_score_adj_update(task);
933err_sighand:
934 unlock_task_sighand(task, &flags);
935err_task_lock:
936 task_unlock(task);
937 put_task_struct(task);
938out:
939 return err < 0 ? err : count;
940}
941
942static const struct file_operations proc_oom_adj_operations = {
943 .read = oom_adj_read,
944 .write = oom_adj_write,
945 .llseek = generic_file_llseek,
946};
947
David Rientjesa63d83f2010-08-09 17:19:46 -0700948static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
949 size_t count, loff_t *ppos)
950{
Al Viro496ad9a2013-01-23 17:07:38 -0500951 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700952 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800953 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700954 unsigned long flags;
955 size_t len;
956
957 if (!task)
958 return -ESRCH;
959 if (lock_task_sighand(task, &flags)) {
960 oom_score_adj = task->signal->oom_score_adj;
961 unlock_task_sighand(task, &flags);
962 }
963 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800964 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700965 return simple_read_from_buffer(buf, count, ppos, buffer, len);
966}
967
968static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
969 size_t count, loff_t *ppos)
970{
971 struct task_struct *task;
972 char buffer[PROC_NUMBUF];
973 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700974 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -0700975 int err;
976
977 memset(buffer, 0, sizeof(buffer));
978 if (count > sizeof(buffer) - 1)
979 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -0700980 if (copy_from_user(buffer, buf, count)) {
981 err = -EFAULT;
982 goto out;
983 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700984
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700985 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700986 if (err)
David Rientjes723548b2010-10-26 14:21:25 -0700987 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -0700988 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -0700989 oom_score_adj > OOM_SCORE_ADJ_MAX) {
990 err = -EINVAL;
991 goto out;
992 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700993
Al Viro496ad9a2013-01-23 17:07:38 -0500994 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -0700995 if (!task) {
996 err = -ESRCH;
997 goto out;
998 }
David Rientjesa63d83f2010-08-09 17:19:46 -0700999
Ying Han3d5992d2010-10-26 14:21:23 -07001000 task_lock(task);
1001 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001002 err = -EINVAL;
1003 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001004 }
David Rientjesd19d5472010-10-26 14:21:26 -07001005
1006 if (!lock_task_sighand(task, &flags)) {
1007 err = -ESRCH;
1008 goto err_task_lock;
1009 }
1010
David Rientjesa9c58b902012-12-11 16:02:54 -08001011 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001012 !capable(CAP_SYS_RESOURCE)) {
1013 err = -EACCES;
1014 goto err_sighand;
1015 }
1016
David Rientjesa9c58b902012-12-11 16:02:54 -08001017 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001018 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001019 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001020 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001021
David Rientjes723548b2010-10-26 14:21:25 -07001022err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001023 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001024err_task_lock:
1025 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001026 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001027out:
1028 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001029}
1030
1031static const struct file_operations proc_oom_score_adj_operations = {
1032 .read = oom_score_adj_read,
1033 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001034 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001035};
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037#ifdef CONFIG_AUDITSYSCALL
1038#define TMPBUFLEN 21
1039static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1040 size_t count, loff_t *ppos)
1041{
Al Viro496ad9a2013-01-23 17:07:38 -05001042 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001043 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 ssize_t length;
1045 char tmpbuf[TMPBUFLEN];
1046
Eric W. Biederman99f89552006-06-26 00:25:55 -07001047 if (!task)
1048 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001050 from_kuid(file->f_cred->user_ns,
1051 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001052 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1054}
1055
1056static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1057 size_t count, loff_t *ppos)
1058{
Al Viro496ad9a2013-01-23 17:07:38 -05001059 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 char *page, *tmp;
1061 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001063 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001065 rcu_read_lock();
1066 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1067 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001069 }
1070 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Al Viroe0182902006-05-18 08:28:02 -04001072 if (count >= PAGE_SIZE)
1073 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 if (*ppos != 0) {
1076 /* No partial writes. */
1077 return -EINVAL;
1078 }
Mel Gormane12ba742007-10-16 01:25:52 -07001079 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (!page)
1081 return -ENOMEM;
1082 length = -EFAULT;
1083 if (copy_from_user(page, buf, count))
1084 goto out_free_page;
1085
Al Viroe0182902006-05-18 08:28:02 -04001086 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 loginuid = simple_strtoul(page, &tmp, 10);
1088 if (tmp == page) {
1089 length = -EINVAL;
1090 goto out_free_page;
1091
1092 }
Eric Paris81407c82013-05-24 09:49:14 -04001093
1094 /* is userspace tring to explicitly UNSET the loginuid? */
1095 if (loginuid == AUDIT_UID_UNSET) {
1096 kloginuid = INVALID_UID;
1097 } else {
1098 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1099 if (!uid_valid(kloginuid)) {
1100 length = -EINVAL;
1101 goto out_free_page;
1102 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001103 }
1104
1105 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (likely(length == 0))
1107 length = count;
1108
1109out_free_page:
1110 free_page((unsigned long) page);
1111 return length;
1112}
1113
Arjan van de Ven00977a52007-02-12 00:55:34 -08001114static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 .read = proc_loginuid_read,
1116 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001117 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118};
Eric Paris1e0bd752008-03-13 08:15:31 -04001119
1120static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1121 size_t count, loff_t *ppos)
1122{
Al Viro496ad9a2013-01-23 17:07:38 -05001123 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001124 struct task_struct *task = get_proc_task(inode);
1125 ssize_t length;
1126 char tmpbuf[TMPBUFLEN];
1127
1128 if (!task)
1129 return -ESRCH;
1130 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1131 audit_get_sessionid(task));
1132 put_task_struct(task);
1133 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1134}
1135
1136static const struct file_operations proc_sessionid_operations = {
1137 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001138 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001139};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#endif
1141
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001142#ifdef CONFIG_FAULT_INJECTION
1143static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1144 size_t count, loff_t *ppos)
1145{
Al Viro496ad9a2013-01-23 17:07:38 -05001146 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001147 char buffer[PROC_NUMBUF];
1148 size_t len;
1149 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001150
1151 if (!task)
1152 return -ESRCH;
1153 make_it_fail = task->make_it_fail;
1154 put_task_struct(task);
1155
1156 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001157
1158 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001159}
1160
1161static ssize_t proc_fault_inject_write(struct file * file,
1162 const char __user * buf, size_t count, loff_t *ppos)
1163{
1164 struct task_struct *task;
1165 char buffer[PROC_NUMBUF], *end;
1166 int make_it_fail;
1167
1168 if (!capable(CAP_SYS_RESOURCE))
1169 return -EPERM;
1170 memset(buffer, 0, sizeof(buffer));
1171 if (count > sizeof(buffer) - 1)
1172 count = sizeof(buffer) - 1;
1173 if (copy_from_user(buffer, buf, count))
1174 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001175 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1176 if (*end)
1177 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001178 if (make_it_fail < 0 || make_it_fail > 1)
1179 return -EINVAL;
1180
Al Viro496ad9a2013-01-23 17:07:38 -05001181 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001182 if (!task)
1183 return -ESRCH;
1184 task->make_it_fail = make_it_fail;
1185 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001186
1187 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001188}
1189
Arjan van de Ven00977a52007-02-12 00:55:34 -08001190static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001191 .read = proc_fault_inject_read,
1192 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001193 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001194};
1195#endif
1196
Arjan van de Ven97455122008-01-25 21:08:34 +01001197
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001198#ifdef CONFIG_SCHED_DEBUG
1199/*
1200 * Print out various scheduling related per-task fields:
1201 */
1202static int sched_show(struct seq_file *m, void *v)
1203{
1204 struct inode *inode = m->private;
1205 struct task_struct *p;
1206
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001207 p = get_proc_task(inode);
1208 if (!p)
1209 return -ESRCH;
1210 proc_sched_show_task(p, m);
1211
1212 put_task_struct(p);
1213
1214 return 0;
1215}
1216
1217static ssize_t
1218sched_write(struct file *file, const char __user *buf,
1219 size_t count, loff_t *offset)
1220{
Al Viro496ad9a2013-01-23 17:07:38 -05001221 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001222 struct task_struct *p;
1223
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001224 p = get_proc_task(inode);
1225 if (!p)
1226 return -ESRCH;
1227 proc_sched_set_task(p);
1228
1229 put_task_struct(p);
1230
1231 return count;
1232}
1233
1234static int sched_open(struct inode *inode, struct file *filp)
1235{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001236 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001237}
1238
1239static const struct file_operations proc_pid_sched_operations = {
1240 .open = sched_open,
1241 .read = seq_read,
1242 .write = sched_write,
1243 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001244 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001245};
1246
1247#endif
1248
Mike Galbraith5091faa2010-11-30 14:18:03 +01001249#ifdef CONFIG_SCHED_AUTOGROUP
1250/*
1251 * Print out autogroup related information:
1252 */
1253static int sched_autogroup_show(struct seq_file *m, void *v)
1254{
1255 struct inode *inode = m->private;
1256 struct task_struct *p;
1257
1258 p = get_proc_task(inode);
1259 if (!p)
1260 return -ESRCH;
1261 proc_sched_autogroup_show_task(p, m);
1262
1263 put_task_struct(p);
1264
1265 return 0;
1266}
1267
1268static ssize_t
1269sched_autogroup_write(struct file *file, const char __user *buf,
1270 size_t count, loff_t *offset)
1271{
Al Viro496ad9a2013-01-23 17:07:38 -05001272 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001273 struct task_struct *p;
1274 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001275 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001276 int err;
1277
1278 memset(buffer, 0, sizeof(buffer));
1279 if (count > sizeof(buffer) - 1)
1280 count = sizeof(buffer) - 1;
1281 if (copy_from_user(buffer, buf, count))
1282 return -EFAULT;
1283
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001284 err = kstrtoint(strstrip(buffer), 0, &nice);
1285 if (err < 0)
1286 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001287
1288 p = get_proc_task(inode);
1289 if (!p)
1290 return -ESRCH;
1291
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001292 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001293 if (err)
1294 count = err;
1295
1296 put_task_struct(p);
1297
1298 return count;
1299}
1300
1301static int sched_autogroup_open(struct inode *inode, struct file *filp)
1302{
1303 int ret;
1304
1305 ret = single_open(filp, sched_autogroup_show, NULL);
1306 if (!ret) {
1307 struct seq_file *m = filp->private_data;
1308
1309 m->private = inode;
1310 }
1311 return ret;
1312}
1313
1314static const struct file_operations proc_pid_sched_autogroup_operations = {
1315 .open = sched_autogroup_open,
1316 .read = seq_read,
1317 .write = sched_autogroup_write,
1318 .llseek = seq_lseek,
1319 .release = single_release,
1320};
1321
1322#endif /* CONFIG_SCHED_AUTOGROUP */
1323
john stultz4614a696b2009-12-14 18:00:05 -08001324static ssize_t comm_write(struct file *file, const char __user *buf,
1325 size_t count, loff_t *offset)
1326{
Al Viro496ad9a2013-01-23 17:07:38 -05001327 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001328 struct task_struct *p;
1329 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001330 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001331
1332 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001333 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001334 return -EFAULT;
1335
1336 p = get_proc_task(inode);
1337 if (!p)
1338 return -ESRCH;
1339
1340 if (same_thread_group(current, p))
1341 set_task_comm(p, buffer);
1342 else
1343 count = -EINVAL;
1344
1345 put_task_struct(p);
1346
1347 return count;
1348}
1349
1350static int comm_show(struct seq_file *m, void *v)
1351{
1352 struct inode *inode = m->private;
1353 struct task_struct *p;
1354
1355 p = get_proc_task(inode);
1356 if (!p)
1357 return -ESRCH;
1358
1359 task_lock(p);
1360 seq_printf(m, "%s\n", p->comm);
1361 task_unlock(p);
1362
1363 put_task_struct(p);
1364
1365 return 0;
1366}
1367
1368static int comm_open(struct inode *inode, struct file *filp)
1369{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001370 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001371}
1372
1373static const struct file_operations proc_pid_set_comm_operations = {
1374 .open = comm_open,
1375 .read = seq_read,
1376 .write = comm_write,
1377 .llseek = seq_lseek,
1378 .release = single_release,
1379};
1380
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001381static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001382{
1383 struct task_struct *task;
1384 struct mm_struct *mm;
1385 struct file *exe_file;
1386
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001387 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001388 if (!task)
1389 return -ENOENT;
1390 mm = get_task_mm(task);
1391 put_task_struct(task);
1392 if (!mm)
1393 return -ENOENT;
1394 exe_file = get_mm_exe_file(mm);
1395 mmput(mm);
1396 if (exe_file) {
1397 *exe_path = exe_file->f_path;
1398 path_get(&exe_file->f_path);
1399 fput(exe_file);
1400 return 0;
1401 } else
1402 return -ENOENT;
1403}
1404
Al Viro008b1502005-08-20 00:17:39 +01001405static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001408 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 int error = -EACCES;
1410
Eric W. Biederman778c1142006-06-26 00:25:58 -07001411 /* Are we allowed to snoop on the tasks file descriptors? */
1412 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Christoph Hellwig408ef012012-06-18 10:47:03 -04001415 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1416 if (error)
1417 goto out;
1418
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001419 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001420 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421out:
Al Viro008b1502005-08-20 00:17:39 +01001422 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001425static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Mel Gormane12ba742007-10-16 01:25:52 -07001427 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001428 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 int len;
1430
1431 if (!tmp)
1432 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001433
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001434 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001435 len = PTR_ERR(pathname);
1436 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001438 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 if (len > buflen)
1441 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001442 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 len = -EFAULT;
1444 out:
1445 free_page((unsigned long)tmp);
1446 return len;
1447}
1448
1449static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1450{
1451 int error = -EACCES;
1452 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001453 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Eric W. Biederman778c1142006-06-26 00:25:58 -07001455 /* Are we allowed to snoop on the tasks file descriptors? */
1456 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001459 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (error)
1461 goto out;
1462
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001463 error = do_proc_readlink(&path, buffer, buflen);
1464 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 return error;
1467}
1468
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001469const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001471 .follow_link = proc_pid_follow_link,
1472 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473};
1474
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001475
1476/* building an inode */
1477
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001478struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001479{
1480 struct inode * inode;
1481 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001482 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001483
1484 /* We need a new inode */
1485
1486 inode = new_inode(sb);
1487 if (!inode)
1488 goto out;
1489
1490 /* Common stuff */
1491 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001492 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001493 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001494 inode->i_op = &proc_def_inode_operations;
1495
1496 /*
1497 * grab the reference to task.
1498 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001499 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001500 if (!ei->pid)
1501 goto out_unlock;
1502
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001503 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001504 rcu_read_lock();
1505 cred = __task_cred(task);
1506 inode->i_uid = cred->euid;
1507 inode->i_gid = cred->egid;
1508 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001509 }
1510 security_task_to_inode(task, inode);
1511
1512out:
1513 return inode;
1514
1515out_unlock:
1516 iput(inode);
1517 return NULL;
1518}
1519
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001520int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001521{
1522 struct inode *inode = dentry->d_inode;
1523 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001524 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001525 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001526
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527 generic_fillattr(inode, stat);
1528
1529 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001530 stat->uid = GLOBAL_ROOT_UID;
1531 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001532 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1533 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001534 if (!has_pid_permissions(pid, task, 2)) {
1535 rcu_read_unlock();
1536 /*
1537 * This doesn't prevent learning whether PID exists,
1538 * it only makes getattr() consistent with readdir().
1539 */
1540 return -ENOENT;
1541 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001542 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1543 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001544 cred = __task_cred(task);
1545 stat->uid = cred->euid;
1546 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001547 }
1548 }
1549 rcu_read_unlock();
1550 return 0;
1551}
1552
1553/* dentry stuff */
1554
1555/*
1556 * Exceptional case: normally we are not allowed to unhash a busy
1557 * directory. In this case, however, we can do it - no aliasing problems
1558 * due to the way we treat inodes.
1559 *
1560 * Rewrite the inode's ownerships here because the owning task may have
1561 * performed a setuid(), etc.
1562 *
1563 * Before the /proc/pid/status file was created the only way to read
1564 * the effective uid of a /process was to stat /proc/pid. Reading
1565 * /proc/pid/status is slow enough that procps and other packages
1566 * kept stating /proc/pid. To keep the rules in /proc simple I have
1567 * made this apply to all per process world readable and executable
1568 * directories.
1569 */
Al Viro0b728e12012-06-10 16:03:43 -04001570int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001571{
Nick Piggin34286d62011-01-07 17:49:57 +11001572 struct inode *inode;
1573 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001574 const struct cred *cred;
1575
Al Viro0b728e12012-06-10 16:03:43 -04001576 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001577 return -ECHILD;
1578
1579 inode = dentry->d_inode;
1580 task = get_proc_task(inode);
1581
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001582 if (task) {
1583 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1584 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001585 rcu_read_lock();
1586 cred = __task_cred(task);
1587 inode->i_uid = cred->euid;
1588 inode->i_gid = cred->egid;
1589 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001590 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001591 inode->i_uid = GLOBAL_ROOT_UID;
1592 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001593 }
1594 inode->i_mode &= ~(S_ISUID | S_ISGID);
1595 security_task_to_inode(task, inode);
1596 put_task_struct(task);
1597 return 1;
1598 }
1599 d_drop(dentry);
1600 return 0;
1601}
1602
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001603static inline bool proc_inode_is_dead(struct inode *inode)
1604{
1605 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1606}
1607
David Howells1dd704b2013-04-12 01:08:50 +01001608int pid_delete_dentry(const struct dentry *dentry)
1609{
1610 /* Is the task we represent dead?
1611 * If so, then don't put the dentry on the lru list,
1612 * kill it immediately.
1613 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001614 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001615}
1616
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001617const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001618{
1619 .d_revalidate = pid_revalidate,
1620 .d_delete = pid_delete_dentry,
1621};
1622
1623/* Lookups */
1624
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001625/*
1626 * Fill a directory entry.
1627 *
1628 * If possible create the dcache entry and derive our inode number and
1629 * file type from dcache entry.
1630 *
1631 * Since all of the proc inode numbers are dynamically generated, the inode
1632 * numbers do not exist until the inode is cache. This means creating the
1633 * the dcache entry in readdir is necessary to keep the inode numbers
1634 * reported by readdir in sync with the inode numbers reported
1635 * by stat.
1636 */
Al Virof0c3b502013-05-16 12:07:31 -04001637bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001638 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001639 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001640{
Al Virof0c3b502013-05-16 12:07:31 -04001641 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001642 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001643 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001644 unsigned type;
1645 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001646
Al Viro1df98b82013-06-15 11:33:10 +04001647 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001648 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001649 child = d_alloc(dir, &qname);
1650 if (!child)
1651 goto end_instantiate;
1652 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1653 dput(child);
1654 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001655 }
1656 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001657 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001658 ino = inode->i_ino;
1659 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001660 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001661 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001662
1663end_instantiate:
1664 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001665}
1666
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001667#ifdef CONFIG_CHECKPOINT_RESTORE
1668
1669/*
1670 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1671 * which represent vma start and end addresses.
1672 */
1673static int dname_to_vma_addr(struct dentry *dentry,
1674 unsigned long *start, unsigned long *end)
1675{
1676 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1677 return -EINVAL;
1678
1679 return 0;
1680}
1681
Al Viro0b728e12012-06-10 16:03:43 -04001682static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001683{
1684 unsigned long vm_start, vm_end;
1685 bool exact_vma_exists = false;
1686 struct mm_struct *mm = NULL;
1687 struct task_struct *task;
1688 const struct cred *cred;
1689 struct inode *inode;
1690 int status = 0;
1691
Al Viro0b728e12012-06-10 16:03:43 -04001692 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001693 return -ECHILD;
1694
1695 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001696 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001697 goto out_notask;
1698 }
1699
1700 inode = dentry->d_inode;
1701 task = get_proc_task(inode);
1702 if (!task)
1703 goto out_notask;
1704
Cong Wang2344bec2012-05-31 16:26:18 -07001705 mm = mm_access(task, PTRACE_MODE_READ);
1706 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001707 goto out;
1708
1709 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1710 down_read(&mm->mmap_sem);
1711 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1712 up_read(&mm->mmap_sem);
1713 }
1714
1715 mmput(mm);
1716
1717 if (exact_vma_exists) {
1718 if (task_dumpable(task)) {
1719 rcu_read_lock();
1720 cred = __task_cred(task);
1721 inode->i_uid = cred->euid;
1722 inode->i_gid = cred->egid;
1723 rcu_read_unlock();
1724 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001725 inode->i_uid = GLOBAL_ROOT_UID;
1726 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001727 }
1728 security_task_to_inode(task, inode);
1729 status = 1;
1730 }
1731
1732out:
1733 put_task_struct(task);
1734
1735out_notask:
1736 if (status <= 0)
1737 d_drop(dentry);
1738
1739 return status;
1740}
1741
1742static const struct dentry_operations tid_map_files_dentry_operations = {
1743 .d_revalidate = map_files_d_revalidate,
1744 .d_delete = pid_delete_dentry,
1745};
1746
1747static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1748{
1749 unsigned long vm_start, vm_end;
1750 struct vm_area_struct *vma;
1751 struct task_struct *task;
1752 struct mm_struct *mm;
1753 int rc;
1754
1755 rc = -ENOENT;
1756 task = get_proc_task(dentry->d_inode);
1757 if (!task)
1758 goto out;
1759
1760 mm = get_task_mm(task);
1761 put_task_struct(task);
1762 if (!mm)
1763 goto out;
1764
1765 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1766 if (rc)
1767 goto out_mmput;
1768
Artem Fetishev70335ab2014-03-10 15:49:45 -07001769 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001770 down_read(&mm->mmap_sem);
1771 vma = find_exact_vma(mm, vm_start, vm_end);
1772 if (vma && vma->vm_file) {
1773 *path = vma->vm_file->f_path;
1774 path_get(path);
1775 rc = 0;
1776 }
1777 up_read(&mm->mmap_sem);
1778
1779out_mmput:
1780 mmput(mm);
1781out:
1782 return rc;
1783}
1784
1785struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001786 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001787 unsigned long len;
1788 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1789};
1790
Al Viroc52a47a2013-06-15 11:15:20 +04001791static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001792proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1793 struct task_struct *task, const void *ptr)
1794{
Al Viro7b540d02012-08-27 14:55:26 -04001795 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001796 struct proc_inode *ei;
1797 struct inode *inode;
1798
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001799 inode = proc_pid_make_inode(dir->i_sb, task);
1800 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001801 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001802
1803 ei = PROC_I(inode);
1804 ei->op.proc_get_link = proc_map_files_get_link;
1805
1806 inode->i_op = &proc_pid_link_inode_operations;
1807 inode->i_size = 64;
1808 inode->i_mode = S_IFLNK;
1809
Al Viro7b540d02012-08-27 14:55:26 -04001810 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001811 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001812 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001813 inode->i_mode |= S_IWUSR;
1814
1815 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1816 d_add(dentry, inode);
1817
Al Viroc52a47a2013-06-15 11:15:20 +04001818 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001819}
1820
1821static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001822 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001823{
1824 unsigned long vm_start, vm_end;
1825 struct vm_area_struct *vma;
1826 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001827 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001828 struct mm_struct *mm;
1829
Al Viroc52a47a2013-06-15 11:15:20 +04001830 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001831 if (!capable(CAP_SYS_ADMIN))
1832 goto out;
1833
Al Viroc52a47a2013-06-15 11:15:20 +04001834 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001835 task = get_proc_task(dir);
1836 if (!task)
1837 goto out;
1838
Al Viroc52a47a2013-06-15 11:15:20 +04001839 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001840 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001841 goto out_put_task;
1842
Al Viroc52a47a2013-06-15 11:15:20 +04001843 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001844 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001845 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001846
1847 mm = get_task_mm(task);
1848 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001849 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001850
1851 down_read(&mm->mmap_sem);
1852 vma = find_exact_vma(mm, vm_start, vm_end);
1853 if (!vma)
1854 goto out_no_vma;
1855
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001856 if (vma->vm_file)
1857 result = proc_map_files_instantiate(dir, dentry, task,
1858 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001859
1860out_no_vma:
1861 up_read(&mm->mmap_sem);
1862 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001863out_put_task:
1864 put_task_struct(task);
1865out:
Al Viroc52a47a2013-06-15 11:15:20 +04001866 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001867}
1868
1869static const struct inode_operations proc_map_files_inode_operations = {
1870 .lookup = proc_map_files_lookup,
1871 .permission = proc_fd_permission,
1872 .setattr = proc_setattr,
1873};
1874
1875static int
Al Virof0c3b502013-05-16 12:07:31 -04001876proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001877{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001878 struct vm_area_struct *vma;
1879 struct task_struct *task;
1880 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001881 unsigned long nr_files, pos, i;
1882 struct flex_array *fa = NULL;
1883 struct map_files_info info;
1884 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001885 int ret;
1886
Zhao Hongjiang41735812013-02-20 13:13:55 +11001887 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001888 if (!capable(CAP_SYS_ADMIN))
1889 goto out;
1890
1891 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001892 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001893 if (!task)
1894 goto out;
1895
1896 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001897 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001898 goto out_put_task;
1899
1900 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001901 if (!dir_emit_dots(file, ctx))
1902 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001903
Al Virof0c3b502013-05-16 12:07:31 -04001904 mm = get_task_mm(task);
1905 if (!mm)
1906 goto out_put_task;
1907 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001908
Al Virof0c3b502013-05-16 12:07:31 -04001909 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001910
Al Virof0c3b502013-05-16 12:07:31 -04001911 /*
1912 * We need two passes here:
1913 *
1914 * 1) Collect vmas of mapped files with mmap_sem taken
1915 * 2) Release mmap_sem and instantiate entries
1916 *
1917 * otherwise we get lockdep complained, since filldir()
1918 * routine might require mmap_sem taken in might_fault().
1919 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001920
Al Virof0c3b502013-05-16 12:07:31 -04001921 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1922 if (vma->vm_file && ++pos > ctx->pos)
1923 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001924 }
Al Virof0c3b502013-05-16 12:07:31 -04001925
1926 if (nr_files) {
1927 fa = flex_array_alloc(sizeof(info), nr_files,
1928 GFP_KERNEL);
1929 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1930 GFP_KERNEL)) {
1931 ret = -ENOMEM;
1932 if (fa)
1933 flex_array_free(fa);
1934 up_read(&mm->mmap_sem);
1935 mmput(mm);
1936 goto out_put_task;
1937 }
1938 for (i = 0, vma = mm->mmap, pos = 2; vma;
1939 vma = vma->vm_next) {
1940 if (!vma->vm_file)
1941 continue;
1942 if (++pos <= ctx->pos)
1943 continue;
1944
1945 info.mode = vma->vm_file->f_mode;
1946 info.len = snprintf(info.name,
1947 sizeof(info.name), "%lx-%lx",
1948 vma->vm_start, vma->vm_end);
1949 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1950 BUG();
1951 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001952 }
Al Virof0c3b502013-05-16 12:07:31 -04001953 up_read(&mm->mmap_sem);
1954
1955 for (i = 0; i < nr_files; i++) {
1956 p = flex_array_get(fa, i);
1957 if (!proc_fill_cache(file, ctx,
1958 p->name, p->len,
1959 proc_map_files_instantiate,
1960 task,
1961 (void *)(unsigned long)p->mode))
1962 break;
1963 ctx->pos++;
1964 }
1965 if (fa)
1966 flex_array_free(fa);
1967 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001968
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001969out_put_task:
1970 put_task_struct(task);
1971out:
1972 return ret;
1973}
1974
1975static const struct file_operations proc_map_files_operations = {
1976 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04001977 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001978 .llseek = default_llseek,
1979};
1980
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001981struct timers_private {
1982 struct pid *pid;
1983 struct task_struct *task;
1984 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04001985 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04001986 unsigned long flags;
1987};
1988
1989static void *timers_start(struct seq_file *m, loff_t *pos)
1990{
1991 struct timers_private *tp = m->private;
1992
1993 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
1994 if (!tp->task)
1995 return ERR_PTR(-ESRCH);
1996
1997 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
1998 if (!tp->sighand)
1999 return ERR_PTR(-ESRCH);
2000
2001 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2002}
2003
2004static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2005{
2006 struct timers_private *tp = m->private;
2007 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2008}
2009
2010static void timers_stop(struct seq_file *m, void *v)
2011{
2012 struct timers_private *tp = m->private;
2013
2014 if (tp->sighand) {
2015 unlock_task_sighand(tp->task, &tp->flags);
2016 tp->sighand = NULL;
2017 }
2018
2019 if (tp->task) {
2020 put_task_struct(tp->task);
2021 tp->task = NULL;
2022 }
2023}
2024
2025static int show_timer(struct seq_file *m, void *v)
2026{
2027 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002028 struct timers_private *tp = m->private;
2029 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002030 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002031 [SIGEV_SIGNAL] = "signal",
2032 [SIGEV_NONE] = "none",
2033 [SIGEV_THREAD] = "thread",
2034 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002035
2036 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002037 notify = timer->it_sigev_notify;
2038
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002039 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002040 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2041 timer->sigq->info.si_value.sival_ptr);
2042 seq_printf(m, "notify: %s/%s.%d\n",
2043 nstr[notify & ~SIGEV_THREAD_ID],
2044 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2045 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002046 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002047
2048 return 0;
2049}
2050
2051static const struct seq_operations proc_timers_seq_ops = {
2052 .start = timers_start,
2053 .next = timers_next,
2054 .stop = timers_stop,
2055 .show = show_timer,
2056};
2057
2058static int proc_timers_open(struct inode *inode, struct file *file)
2059{
2060 struct timers_private *tp;
2061
2062 tp = __seq_open_private(file, &proc_timers_seq_ops,
2063 sizeof(struct timers_private));
2064 if (!tp)
2065 return -ENOMEM;
2066
2067 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002068 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002069 return 0;
2070}
2071
2072static const struct file_operations proc_timers_operations = {
2073 .open = proc_timers_open,
2074 .read = seq_read,
2075 .llseek = seq_lseek,
2076 .release = seq_release_private,
2077};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002078#endif /* CONFIG_CHECKPOINT_RESTORE */
2079
Al Viroc52a47a2013-06-15 11:15:20 +04002080static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002081 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002082{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002083 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002084 struct inode *inode;
2085 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002086
Eric W. Biederman61a28782006-10-02 02:18:49 -07002087 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002088 if (!inode)
2089 goto out;
2090
2091 ei = PROC_I(inode);
2092 inode->i_mode = p->mode;
2093 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002094 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002095 if (p->iop)
2096 inode->i_op = p->iop;
2097 if (p->fop)
2098 inode->i_fop = p->fop;
2099 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002100 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002101 d_add(dentry, inode);
2102 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002103 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002104 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002105out:
Al Viroc52a47a2013-06-15 11:15:20 +04002106 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002107}
2108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109static struct dentry *proc_pident_lookup(struct inode *dir,
2110 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002111 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002112 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
Al Viroc52a47a2013-06-15 11:15:20 +04002114 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002115 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002116 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Al Viroc52a47a2013-06-15 11:15:20 +04002118 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Eric W. Biederman99f89552006-06-26 00:25:55 -07002120 if (!task)
2121 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002123 /*
2124 * Yes, it does not scale. And it should not. Don't add
2125 * new entries into /proc/<tgid>/ without very good reasons.
2126 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002127 last = &ents[nents - 1];
2128 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (p->len != dentry->d_name.len)
2130 continue;
2131 if (!memcmp(dentry->d_name.name, p->name, p->len))
2132 break;
2133 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002134 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 goto out;
2136
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002137 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002139 put_task_struct(task);
2140out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002141 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Al Virof0c3b502013-05-16 12:07:31 -04002144static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002145 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002146{
Al Virof0c3b502013-05-16 12:07:31 -04002147 struct task_struct *task = get_proc_task(file_inode(file));
2148 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002149
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002150 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002151 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002152
Al Virof0c3b502013-05-16 12:07:31 -04002153 if (!dir_emit_dots(file, ctx))
2154 goto out;
2155
2156 if (ctx->pos >= nents + 2)
2157 goto out;
2158
2159 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2160 if (!proc_fill_cache(file, ctx, p->name, p->len,
2161 proc_pident_instantiate, task, p))
2162 break;
2163 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002164 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002165out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002166 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002171static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2172 size_t count, loff_t *ppos)
2173{
Al Viro496ad9a2013-01-23 17:07:38 -05002174 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002175 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002176 ssize_t length;
2177 struct task_struct *task = get_proc_task(inode);
2178
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002179 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002180 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002181
2182 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002183 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002184 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002185 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002186 if (length > 0)
2187 length = simple_read_from_buffer(buf, count, ppos, p, length);
2188 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002189 return length;
2190}
2191
2192static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2193 size_t count, loff_t *ppos)
2194{
Al Viro496ad9a2013-01-23 17:07:38 -05002195 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002196 char *page;
2197 ssize_t length;
2198 struct task_struct *task = get_proc_task(inode);
2199
2200 length = -ESRCH;
2201 if (!task)
2202 goto out_no_task;
2203 if (count > PAGE_SIZE)
2204 count = PAGE_SIZE;
2205
2206 /* No partial writes. */
2207 length = -EINVAL;
2208 if (*ppos != 0)
2209 goto out;
2210
2211 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002212 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002213 if (!page)
2214 goto out;
2215
2216 length = -EFAULT;
2217 if (copy_from_user(page, buf, count))
2218 goto out_free;
2219
David Howells107db7c2009-05-08 13:55:27 +01002220 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002221 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002222 if (length < 0)
2223 goto out_free;
2224
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002225 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002226 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002227 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002228 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002229out_free:
2230 free_page((unsigned long) page);
2231out:
2232 put_task_struct(task);
2233out_no_task:
2234 return length;
2235}
2236
Arjan van de Ven00977a52007-02-12 00:55:34 -08002237static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002238 .read = proc_pid_attr_read,
2239 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002240 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002241};
2242
Eric Dumazetc5141e62007-05-08 00:26:15 -07002243static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002244 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2245 REG("prev", S_IRUGO, proc_pid_attr_operations),
2246 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2247 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2248 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2249 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002250};
2251
Al Virof0c3b502013-05-16 12:07:31 -04002252static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Al Virof0c3b502013-05-16 12:07:31 -04002254 return proc_pident_readdir(file, ctx,
2255 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256}
2257
Arjan van de Ven00977a52007-02-12 00:55:34 -08002258static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002260 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002261 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262};
2263
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002264static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002265 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002267 return proc_pident_lookup(dir, dentry,
2268 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002271static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002272 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002273 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002274 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275};
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277#endif
2278
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002279#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002280static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2281 size_t count, loff_t *ppos)
2282{
Al Viro496ad9a2013-01-23 17:07:38 -05002283 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002284 struct mm_struct *mm;
2285 char buffer[PROC_NUMBUF];
2286 size_t len;
2287 int ret;
2288
2289 if (!task)
2290 return -ESRCH;
2291
2292 ret = 0;
2293 mm = get_task_mm(task);
2294 if (mm) {
2295 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2296 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2297 MMF_DUMP_FILTER_SHIFT));
2298 mmput(mm);
2299 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2300 }
2301
2302 put_task_struct(task);
2303
2304 return ret;
2305}
2306
2307static ssize_t proc_coredump_filter_write(struct file *file,
2308 const char __user *buf,
2309 size_t count,
2310 loff_t *ppos)
2311{
2312 struct task_struct *task;
2313 struct mm_struct *mm;
2314 char buffer[PROC_NUMBUF], *end;
2315 unsigned int val;
2316 int ret;
2317 int i;
2318 unsigned long mask;
2319
2320 ret = -EFAULT;
2321 memset(buffer, 0, sizeof(buffer));
2322 if (count > sizeof(buffer) - 1)
2323 count = sizeof(buffer) - 1;
2324 if (copy_from_user(buffer, buf, count))
2325 goto out_no_task;
2326
2327 ret = -EINVAL;
2328 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2329 if (*end == '\n')
2330 end++;
2331 if (end - buffer == 0)
2332 goto out_no_task;
2333
2334 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002335 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002336 if (!task)
2337 goto out_no_task;
2338
2339 ret = end - buffer;
2340 mm = get_task_mm(task);
2341 if (!mm)
2342 goto out_no_mm;
2343
2344 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2345 if (val & mask)
2346 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2347 else
2348 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2349 }
2350
2351 mmput(mm);
2352 out_no_mm:
2353 put_task_struct(task);
2354 out_no_task:
2355 return ret;
2356}
2357
2358static const struct file_operations proc_coredump_filter_operations = {
2359 .read = proc_coredump_filter_read,
2360 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002361 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002362};
2363#endif
2364
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002365#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002366static int do_io_accounting(struct task_struct *task, struct seq_file *m, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002367{
Andrea Righi940389b2008-07-28 00:48:12 +02002368 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002369 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002370 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002371
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002372 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2373 if (result)
2374 return result;
2375
2376 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2377 result = -EACCES;
2378 goto out_unlock;
2379 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002380
Andrea Righi59954772008-07-27 17:29:15 +02002381 if (whole && lock_task_sighand(task, &flags)) {
2382 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002383
Andrea Righi59954772008-07-27 17:29:15 +02002384 task_io_accounting_add(&acct, &task->signal->ioac);
2385 while_each_thread(task, t)
2386 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002387
Andrea Righi59954772008-07-27 17:29:15 +02002388 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002389 }
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002390 result = seq_printf(m,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002391 "rchar: %llu\n"
2392 "wchar: %llu\n"
2393 "syscr: %llu\n"
2394 "syscw: %llu\n"
2395 "read_bytes: %llu\n"
2396 "write_bytes: %llu\n"
2397 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002398 (unsigned long long)acct.rchar,
2399 (unsigned long long)acct.wchar,
2400 (unsigned long long)acct.syscr,
2401 (unsigned long long)acct.syscw,
2402 (unsigned long long)acct.read_bytes,
2403 (unsigned long long)acct.write_bytes,
2404 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002405out_unlock:
2406 mutex_unlock(&task->signal->cred_guard_mutex);
2407 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002408}
Andrea Righi297c5d92008-07-25 01:48:49 -07002409
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002410static int proc_tid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2411 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002412{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002413 return do_io_accounting(task, m, 0);
Andrea Righi297c5d92008-07-25 01:48:49 -07002414}
2415
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002416static int proc_tgid_io_accounting(struct seq_file *m, struct pid_namespace *ns,
2417 struct pid *pid, struct task_struct *task)
Andrea Righi297c5d92008-07-25 01:48:49 -07002418{
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002419 return do_io_accounting(task, m, 1);
Andrea Righi297c5d92008-07-25 01:48:49 -07002420}
2421#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002422
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002423#ifdef CONFIG_USER_NS
2424static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002425 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002426{
2427 struct user_namespace *ns = NULL;
2428 struct task_struct *task;
2429 struct seq_file *seq;
2430 int ret = -EINVAL;
2431
2432 task = get_proc_task(inode);
2433 if (task) {
2434 rcu_read_lock();
2435 ns = get_user_ns(task_cred_xxx(task, user_ns));
2436 rcu_read_unlock();
2437 put_task_struct(task);
2438 }
2439 if (!ns)
2440 goto err;
2441
2442 ret = seq_open(file, seq_ops);
2443 if (ret)
2444 goto err_put_ns;
2445
2446 seq = file->private_data;
2447 seq->private = ns;
2448
2449 return 0;
2450err_put_ns:
2451 put_user_ns(ns);
2452err:
2453 return ret;
2454}
2455
2456static int proc_id_map_release(struct inode *inode, struct file *file)
2457{
2458 struct seq_file *seq = file->private_data;
2459 struct user_namespace *ns = seq->private;
2460 put_user_ns(ns);
2461 return seq_release(inode, file);
2462}
2463
2464static int proc_uid_map_open(struct inode *inode, struct file *file)
2465{
2466 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2467}
2468
2469static int proc_gid_map_open(struct inode *inode, struct file *file)
2470{
2471 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2472}
2473
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002474static int proc_projid_map_open(struct inode *inode, struct file *file)
2475{
2476 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2477}
2478
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002479static const struct file_operations proc_uid_map_operations = {
2480 .open = proc_uid_map_open,
2481 .write = proc_uid_map_write,
2482 .read = seq_read,
2483 .llseek = seq_lseek,
2484 .release = proc_id_map_release,
2485};
2486
2487static const struct file_operations proc_gid_map_operations = {
2488 .open = proc_gid_map_open,
2489 .write = proc_gid_map_write,
2490 .read = seq_read,
2491 .llseek = seq_lseek,
2492 .release = proc_id_map_release,
2493};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002494
2495static const struct file_operations proc_projid_map_operations = {
2496 .open = proc_projid_map_open,
2497 .write = proc_projid_map_write,
2498 .read = seq_read,
2499 .llseek = seq_lseek,
2500 .release = proc_id_map_release,
2501};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002502#endif /* CONFIG_USER_NS */
2503
Kees Cook47830722008-10-06 03:11:58 +04002504static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2505 struct pid *pid, struct task_struct *task)
2506{
Al Viroa9712bc2011-03-23 15:52:50 -04002507 int err = lock_trace(task);
2508 if (!err) {
2509 seq_printf(m, "%08x\n", task->personality);
2510 unlock_trace(task);
2511 }
2512 return err;
Kees Cook47830722008-10-06 03:11:58 +04002513}
2514
Eric W. Biederman801199c2006-10-02 02:18:48 -07002515/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516 * Thread groups
2517 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002518static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002519static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002520
Eric Dumazetc5141e62007-05-08 00:26:15 -07002521static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002522 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2523 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002524#ifdef CONFIG_CHECKPOINT_RESTORE
2525 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2526#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002527 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002528 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002529#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002530 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002531#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002532 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002533 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002534 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002535 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002536 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002537#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002538 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002539#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002540#ifdef CONFIG_SCHED_AUTOGROUP
2541 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2542#endif
john stultz4614a696b2009-12-14 18:00:05 -08002543 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002544#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002545 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002546#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002547 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002548 ONE("stat", S_IRUGO, proc_tgid_stat),
2549 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002550 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002552 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002554 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2555 LNK("cwd", proc_cwd_link),
2556 LNK("root", proc_root_link),
2557 LNK("exe", proc_exe_link),
2558 REG("mounts", S_IRUGO, proc_mounts_operations),
2559 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2560 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002561#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002562 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002563 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002564 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002565#endif
2566#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002567 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002568#endif
2569#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002570 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002571#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002572#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002573 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002574#endif
2575#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002576 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002577#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002578#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002579 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002580#endif
Paul Menage8793d852007-10-18 23:39:39 -07002581#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002582 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583#endif
Paul Menagea4243162007-10-18 23:39:35 -07002584#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002585 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002586#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002587 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002588 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002589 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002591 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2592 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002593#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002594#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002595 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002596#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002597#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002598 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002599#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002600#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002601 ONE("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002602#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002603#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002604 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002605#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002606#ifdef CONFIG_USER_NS
2607 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2608 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002609 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002610#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002611#ifdef CONFIG_CHECKPOINT_RESTORE
2612 REG("timers", S_IRUGO, proc_timers_operations),
2613#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614};
2615
Al Virof0c3b502013-05-16 12:07:31 -04002616static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002617{
Al Virof0c3b502013-05-16 12:07:31 -04002618 return proc_pident_readdir(file, ctx,
2619 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002620}
2621
Arjan van de Ven00977a52007-02-12 00:55:34 -08002622static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002623 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002624 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002625 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002626};
2627
Al Viro00cd8dd2012-06-10 17:13:09 -04002628static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2629{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002630 return proc_pident_lookup(dir, dentry,
2631 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002632}
2633
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002634static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002635 .lookup = proc_tgid_base_lookup,
2636 .getattr = pid_getattr,
2637 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002638 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002639};
2640
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002641static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002643 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002644 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002645 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Eric W. Biederman48e64842006-06-26 00:25:48 -07002647 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002648 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002649 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002650 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002651 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002652 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002653 d_drop(dentry);
2654 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Eric W. Biederman48e64842006-06-26 00:25:48 -07002657 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002658 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2659 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002660 if (!leader)
2661 goto out;
2662
2663 name.name = "task";
2664 name.len = strlen(name.name);
2665 dir = d_hash_and_lookup(leader, &name);
2666 if (!dir)
2667 goto out_put_leader;
2668
2669 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002670 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002671 dentry = d_hash_and_lookup(dir, &name);
2672 if (dentry) {
2673 shrink_dcache_parent(dentry);
2674 d_drop(dentry);
2675 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002677
2678 dput(dir);
2679out_put_leader:
2680 dput(leader);
2681out:
2682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
2684
Randy Dunlap0895e912007-10-21 21:00:10 -07002685/**
2686 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2687 * @task: task that should be flushed.
2688 *
2689 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002690 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002691 * in. This call is supposed to do all of this job.
2692 *
2693 * Looks in the dcache for
2694 * /proc/@pid
2695 * /proc/@tgid/task/@pid
2696 * if either directory is present flushes it and all of it'ts children
2697 * from the dcache.
2698 *
2699 * It is safe and reasonable to cache /proc entries for a task until
2700 * that task exits. After that they just clog up the dcache with
2701 * useless entries, possibly causing useful dcache entries to be
2702 * flushed instead. This routine is proved to flush those useless
2703 * dcache entries at process exit time.
2704 *
2705 * NOTE: This routine is just an optimization so it does not guarantee
2706 * that no dcache entries will exist at process exit time it
2707 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002708 */
2709
2710void proc_flush_task(struct task_struct *task)
2711{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002712 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002713 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002714 struct upid *upid;
2715
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002716 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002717 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002718
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002719 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002720 upid = &pid->numbers[i];
2721 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002722 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002723 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002724}
2725
Al Viroc52a47a2013-06-15 11:15:20 +04002726static int proc_pid_instantiate(struct inode *dir,
2727 struct dentry * dentry,
2728 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002729{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002730 struct inode *inode;
2731
Eric W. Biederman61a28782006-10-02 02:18:49 -07002732 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002733 if (!inode)
2734 goto out;
2735
2736 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2737 inode->i_op = &proc_tgid_base_inode_operations;
2738 inode->i_fop = &proc_tgid_base_operations;
2739 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002740
Miklos Szeredibfe86842011-10-28 14:13:29 +02002741 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2742 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002743
Nick Pigginfb045ad2011-01-07 17:49:55 +11002744 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002745
2746 d_add(dentry, inode);
2747 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002748 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002749 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002750out:
Al Viroc52a47a2013-06-15 11:15:20 +04002751 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002752}
2753
Al Viro00cd8dd2012-06-10 17:13:09 -04002754struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002756 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002759 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002761 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (tgid == ~0U)
2763 goto out;
2764
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002765 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002766 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002767 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (task)
2769 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002770 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (!task)
2772 goto out;
2773
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002774 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002775 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776out:
Al Viroc52a47a2013-06-15 11:15:20 +04002777 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002781 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002784struct tgid_iter {
2785 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002786 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002787};
2788static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2789{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002790 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002792 if (iter.task)
2793 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002794 rcu_read_lock();
2795retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002796 iter.task = NULL;
2797 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002798 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002799 iter.tgid = pid_nr_ns(pid, ns);
2800 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002801 /* What we to know is if the pid we have find is the
2802 * pid of a thread_group_leader. Testing for task
2803 * being a thread_group_leader is the obvious thing
2804 * todo but there is a window when it fails, due to
2805 * the pid transfer logic in de_thread.
2806 *
2807 * So we perform the straight forward test of seeing
2808 * if the pid we have found is the pid of a thread
2809 * group leader, and don't worry if the task we have
2810 * found doesn't happen to be a thread group leader.
2811 * As we don't care in the case of readdir.
2812 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002813 if (!iter.task || !has_group_leader_pid(iter.task)) {
2814 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002815 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002816 }
2817 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002819 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002820 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
Eric W. Biederman00978752014-07-31 03:10:50 -07002823#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002826int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002828 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002829 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002830 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Al Viro021ada72013-03-29 19:27:05 -04002832 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Eric W. Biederman00978752014-07-31 03:10:50 -07002835 if (pos == TGID_OFFSET - 2) {
Al Virodb963162013-06-15 10:45:10 +04002836 struct inode *inode = ns->proc_self->d_inode;
2837 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002838 return 0;
Eric W. Biederman00978752014-07-31 03:10:50 -07002839 ctx->pos = pos = pos + 1;
Al Viro021ada72013-03-29 19:27:05 -04002840 }
Eric W. Biederman00978752014-07-31 03:10:50 -07002841 if (pos == TGID_OFFSET - 1) {
2842 struct inode *inode = ns->proc_thread_self->d_inode;
2843 if (!dir_emit(ctx, "thread-self", 11, inode->i_ino, DT_LNK))
2844 return 0;
2845 ctx->pos = pos = pos + 1;
2846 }
2847 iter.tgid = pos - TGID_OFFSET;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002848 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002849 for (iter = next_tgid(ns, iter);
2850 iter.task;
2851 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002852 char name[PROC_NUMBUF];
2853 int len;
2854 if (!has_pid_permissions(ns, iter.task, 2))
2855 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002856
Al Virof0c3b502013-05-16 12:07:31 -04002857 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2858 ctx->pos = iter.tgid + TGID_OFFSET;
2859 if (!proc_fill_cache(file, ctx, name, len,
2860 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002861 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002862 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
Al Virof0c3b502013-05-16 12:07:31 -04002865 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return 0;
2867}
2868
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002869/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002870 * Tasks
2871 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002872static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002873 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002874 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002875 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Eric W. Biederman6ba8ed72014-07-31 16:27:08 -07002876#ifdef CONFIG_NET
2877 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
2878#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002879 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002880 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002881 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002882 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002883 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002884#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002885 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002886#endif
john stultz4614a696b2009-12-14 18:00:05 -08002887 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002888#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002889 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002890#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002891 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002892 ONE("stat", S_IRUGO, proc_tid_stat),
2893 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002894 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002895#ifdef CONFIG_CHECKPOINT_RESTORE
2896 REG("children", S_IRUGO, proc_tid_children_operations),
2897#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002898#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002899 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002900#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002901 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2902 LNK("cwd", proc_cwd_link),
2903 LNK("root", proc_root_link),
2904 LNK("exe", proc_exe_link),
2905 REG("mounts", S_IRUGO, proc_mounts_operations),
2906 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002907#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002908 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002909 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002910 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002911#endif
2912#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002913 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002914#endif
2915#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002916 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002918#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002919 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002920#endif
2921#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002922 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002923#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002924#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002925 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002926#endif
Paul Menage8793d852007-10-18 23:39:39 -07002927#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002928 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002929#endif
Paul Menagea4243162007-10-18 23:39:35 -07002930#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002932#endif
Alexey Dobriyan6ba51e32014-08-08 14:21:48 -07002933 ONE("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002934 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002935 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002936#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002937 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002938 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002940#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002941 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002942#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002943#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan19aadc92014-08-08 14:21:50 -07002944 ONE("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002945#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002946#ifdef CONFIG_HARDWALL
Alexey Dobriyand962c142014-08-08 14:21:52 -07002947 ONE("hardwall", S_IRUGO, proc_pid_hardwall),
Chris Metcalff133ecc2011-05-26 12:40:09 -04002948#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002949#ifdef CONFIG_USER_NS
2950 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2951 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002952 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002953#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954};
2955
Al Virof0c3b502013-05-16 12:07:31 -04002956static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002957{
Al Virof0c3b502013-05-16 12:07:31 -04002958 return proc_pident_readdir(file, ctx,
2959 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960}
2961
Al Viro00cd8dd2012-06-10 17:13:09 -04002962static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2963{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002964 return proc_pident_lookup(dir, dentry,
2965 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002966}
2967
Arjan van de Ven00977a52007-02-12 00:55:34 -08002968static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002969 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002970 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002971 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002972};
2973
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002974static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002975 .lookup = proc_tid_base_lookup,
2976 .getattr = pid_getattr,
2977 .setattr = proc_setattr,
2978};
2979
Al Viroc52a47a2013-06-15 11:15:20 +04002980static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002981 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002982{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002983 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002984 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002985
2986 if (!inode)
2987 goto out;
2988 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2989 inode->i_op = &proc_tid_base_inode_operations;
2990 inode->i_fop = &proc_tid_base_operations;
2991 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002992
Miklos Szeredibfe86842011-10-28 14:13:29 +02002993 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
2994 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002995
Nick Pigginfb045ad2011-01-07 17:49:55 +11002996 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002997
2998 d_add(dentry, inode);
2999 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003000 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003001 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003002out:
Al Viroc52a47a2013-06-15 11:15:20 +04003003 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003004}
3005
Al Viro00cd8dd2012-06-10 17:13:09 -04003006static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003007{
Al Viroc52a47a2013-06-15 11:15:20 +04003008 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009 struct task_struct *task;
3010 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003011 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003012 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003013
3014 if (!leader)
3015 goto out_no_task;
3016
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003017 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018 if (tid == ~0U)
3019 goto out;
3020
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003021 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003022 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003023 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003024 if (task)
3025 get_task_struct(task);
3026 rcu_read_unlock();
3027 if (!task)
3028 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003029 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003030 goto out_drop_task;
3031
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003032 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033out_drop_task:
3034 put_task_struct(task);
3035out:
3036 put_task_struct(leader);
3037out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003038 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003039}
3040
3041/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003042 * Find the first tid of a thread group to return to user space.
3043 *
3044 * Usually this is just the thread group leader, but if the users
3045 * buffer was too small or there was a seek into the middle of the
3046 * directory we have more work todo.
3047 *
3048 * In the case of a short read we start with find_task_by_pid.
3049 *
3050 * In the case of a seek we start with the leader and walk nr
3051 * threads past it.
3052 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003053static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3054 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003055{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003056 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003057 unsigned long nr = f_pos;
3058
3059 if (nr != f_pos) /* 32bit overflow? */
3060 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003061
Eric W. Biedermancc288732006-06-26 00:26:01 -07003062 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003063 task = pid_task(pid, PIDTYPE_PID);
3064 if (!task)
3065 goto fail;
3066
3067 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003068 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003069 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003070 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003071 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003072 }
3073
3074 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003075 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003076 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003077
3078 /* If we haven't found our starting place yet start
3079 * with the leader and walk nr threads forward.
3080 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003081 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003082 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003083 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003084 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003085 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003086fail:
3087 pos = NULL;
3088 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003089found:
3090 get_task_struct(pos);
3091out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003092 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003093 return pos;
3094}
3095
3096/*
3097 * Find the next thread in the thread list.
3098 * Return NULL if there is an error or no next thread.
3099 *
3100 * The reference to the input task_struct is released.
3101 */
3102static struct task_struct *next_tid(struct task_struct *start)
3103{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003104 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003105 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003106 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003107 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003108 if (thread_group_leader(pos))
3109 pos = NULL;
3110 else
3111 get_task_struct(pos);
3112 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003113 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003114 put_task_struct(start);
3115 return pos;
3116}
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003119static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003121 struct inode *inode = file_inode(file);
3122 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003123 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003124 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003126 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003127 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Al Virof0c3b502013-05-16 12:07:31 -04003129 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003132 /* f_version caches the tgid value that the last readdir call couldn't
3133 * return. lseek aka telldir automagically resets f_version to 0.
3134 */
Al Virof0c3b502013-05-16 12:07:31 -04003135 ns = file->f_dentry->d_sb->s_fs_info;
3136 tid = (int)file->f_version;
3137 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003138 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003139 task;
Al Virof0c3b502013-05-16 12:07:31 -04003140 task = next_tid(task), ctx->pos++) {
3141 char name[PROC_NUMBUF];
3142 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003143 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003144 len = snprintf(name, sizeof(name), "%d", tid);
3145 if (!proc_fill_cache(file, ctx, name, len,
3146 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003147 /* returning this tgid failed, save it as the first
3148 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003149 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003150 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003154
Al Virof0c3b502013-05-16 12:07:31 -04003155 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003157
3158static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3159{
3160 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003161 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003162 generic_fillattr(inode, stat);
3163
Eric W. Biederman99f89552006-06-26 00:25:55 -07003164 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003165 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003166 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003167 }
3168
3169 return 0;
3170}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003171
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003172static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003173 .lookup = proc_task_lookup,
3174 .getattr = proc_task_getattr,
3175 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003176 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003177};
3178
Arjan van de Ven00977a52007-02-12 00:55:34 -08003179static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003180 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003181 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003182 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003183};