blob: 1dd692132ede0be8879e2e9ed3b18f6383755774 [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, {})
133#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700134 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700135 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_read = read } )
137#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300140 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Vegard Nossumaed54172008-06-05 22:46:53 -0700142/*
143 * Count the number of hardlinks for the pid_entry table, excluding the .
144 * and .. links.
145 */
146static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 unsigned int n)
148{
149 unsigned int i;
150 unsigned int count;
151
152 count = 0;
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
155 ++count;
156 }
157
158 return count;
159}
160
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200161static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000163 int result = -ENOENT;
164
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700165 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200166 if (task->fs) {
167 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000168 result = 0;
169 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000171 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700172}
173
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800174static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800176 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700177 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700178
179 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200180 task_lock(task);
181 if (task->fs) {
182 get_fs_pwd(task->fs, path);
183 result = 0;
184 }
185 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186 put_task_struct(task);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return result;
189}
190
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800191static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800193 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700195
196 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200197 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700198 put_task_struct(task);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return result;
201}
202
William Roberts21a64572014-02-11 10:12:00 -0800203static int proc_pid_cmdline(struct task_struct *task, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
William Roberts21a64572014-02-11 10:12:00 -0800205 return get_cmdline(task, buffer, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700208static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
209 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Cong Wange7dcd992012-05-31 16:26:17 -0700211 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500212 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300214 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300216 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700217 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 mmput(mm);
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700219 return 0;
220 } else
221 return PTR_ERR(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224
225#ifdef CONFIG_KALLSYMS
226/*
227 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
228 * Returns the resolved symbol. If that fails, simply return the address.
229 */
230static int proc_pid_wchan(struct task_struct *task, char *buffer)
231{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700232 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700233 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 wchan = get_wchan(task);
236
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700237 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600238 if (!ptrace_may_access(task, PTRACE_MODE_READ))
239 return 0;
240 else
241 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700242 else
243 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245#endif /* CONFIG_KALLSYMS */
246
Al Viroa9712bc2011-03-23 15:52:50 -0400247static int lock_trace(struct task_struct *task)
248{
249 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
250 if (err)
251 return err;
252 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
253 mutex_unlock(&task->signal->cred_guard_mutex);
254 return -EPERM;
255 }
256 return 0;
257}
258
259static void unlock_trace(struct task_struct *task)
260{
261 mutex_unlock(&task->signal->cred_guard_mutex);
262}
263
Ken Chen2ec220e2008-11-10 11:26:08 +0300264#ifdef CONFIG_STACKTRACE
265
266#define MAX_STACK_TRACE_DEPTH 64
267
268static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
269 struct pid *pid, struct task_struct *task)
270{
271 struct stack_trace trace;
272 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400273 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300274 int i;
275
276 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
277 if (!entries)
278 return -ENOMEM;
279
280 trace.nr_entries = 0;
281 trace.max_entries = MAX_STACK_TRACE_DEPTH;
282 trace.entries = entries;
283 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300284
Al Viroa9712bc2011-03-23 15:52:50 -0400285 err = lock_trace(task);
286 if (!err) {
287 save_stack_trace_tsk(task, &trace);
288
289 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700290 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400291 (void *)entries[i], (void *)entries[i]);
292 }
293 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300294 }
295 kfree(entries);
296
Al Viroa9712bc2011-03-23 15:52:50 -0400297 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300298}
299#endif
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#ifdef CONFIG_SCHEDSTATS
302/*
303 * Provides /proc/PID/schedstat
304 */
305static int proc_pid_schedstat(struct task_struct *task, char *buffer)
306{
Balbir Singh172ba842007-07-09 18:52:00 +0200307 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100308 (unsigned long long)task->se.sum_exec_runtime,
309 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200310 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312#endif
313
Arjan van de Ven97455122008-01-25 21:08:34 +0100314#ifdef CONFIG_LATENCYTOP
315static int lstats_show_proc(struct seq_file *m, void *v)
316{
317 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800318 struct inode *inode = m->private;
319 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100320
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800321 if (!task)
322 return -ESRCH;
323 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100324 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800325 struct latency_record *lr = &task->latency_record[i];
326 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100327 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800328 seq_printf(m, "%i %li %li",
329 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100330 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800331 unsigned long bt = lr->backtrace[q];
332 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100333 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800334 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100335 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800336 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100337 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800338 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100339 }
340
341 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800342 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100343 return 0;
344}
345
346static int lstats_open(struct inode *inode, struct file *file)
347{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800348 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800349}
350
Arjan van de Ven97455122008-01-25 21:08:34 +0100351static ssize_t lstats_write(struct file *file, const char __user *buf,
352 size_t count, loff_t *offs)
353{
Al Viro496ad9a2013-01-23 17:07:38 -0500354 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100355
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800356 if (!task)
357 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100358 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800359 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100360
361 return count;
362}
363
364static const struct file_operations proc_lstats_operations = {
365 .open = lstats_open,
366 .read = seq_read,
367 .write = lstats_write,
368 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800369 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100370};
371
372#endif
373
Al Viro8d8b97b2013-04-19 23:11:24 -0400374#ifdef CONFIG_CGROUPS
375static int cgroup_open(struct inode *inode, struct file *file)
376{
377 struct pid *pid = PROC_I(inode)->pid;
378 return single_open(file, proc_cgroup_show, pid);
379}
380
381static const struct file_operations proc_cgroup_operations = {
382 .open = cgroup_open,
383 .read = seq_read,
384 .llseek = seq_lseek,
385 .release = single_release,
386};
387#endif
388
389#ifdef CONFIG_PROC_PID_CPUSET
390
391static int cpuset_open(struct inode *inode, struct file *file)
392{
393 struct pid *pid = PROC_I(inode)->pid;
394 return single_open(file, proc_cpuset_show, pid);
395}
396
397static const struct file_operations proc_cpuset_operations = {
398 .open = cpuset_open,
399 .read = seq_read,
400 .llseek = seq_lseek,
401 .release = single_release,
402};
403#endif
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405static int proc_oom_score(struct task_struct *task, char *buffer)
406{
David Rientjesa7f638f2012-05-29 15:06:47 -0700407 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200408 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700410 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200411 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700412 points = oom_badness(task, NULL, NULL, totalpages) *
413 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700414 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return sprintf(buffer, "%lu\n", points);
416}
417
Neil Hormand85f50d2007-10-18 23:40:37 -0700418struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700419 const char *name;
420 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700421};
422
423static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700424 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700425 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
426 [RLIMIT_DATA] = {"Max data size", "bytes"},
427 [RLIMIT_STACK] = {"Max stack size", "bytes"},
428 [RLIMIT_CORE] = {"Max core file size", "bytes"},
429 [RLIMIT_RSS] = {"Max resident set", "bytes"},
430 [RLIMIT_NPROC] = {"Max processes", "processes"},
431 [RLIMIT_NOFILE] = {"Max open files", "files"},
432 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
433 [RLIMIT_AS] = {"Max address space", "bytes"},
434 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
435 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
436 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
437 [RLIMIT_NICE] = {"Max nice priority", NULL},
438 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800439 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700440};
441
442/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700443static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
444 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700445{
446 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700447 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700448
449 struct rlimit rlim[RLIM_NLIMITS];
450
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400451 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700452 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700453 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700455
456 /*
457 * print the file header
458 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700459 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700460 "Limit", "Soft Limit", "Hard Limit", "Units");
461
462 for (i = 0; i < RLIM_NLIMITS; i++) {
463 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700464 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700465 lnames[i].name, "unlimited");
466 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700467 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700468 lnames[i].name, rlim[i].rlim_cur);
469
470 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700471 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700472 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700473 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700474
475 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700476 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700477 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700478 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700479 }
480
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700481 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700482}
483
Roland McGrathebcb6732008-07-25 19:46:00 -0700484#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
485static int proc_pid_syscall(struct task_struct *task, char *buffer)
486{
487 long nr;
488 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400489 int res = lock_trace(task);
490 if (res)
491 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700492
493 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400494 res = sprintf(buffer, "running\n");
495 else if (nr < 0)
496 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
497 else
498 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700499 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
500 nr,
501 args[0], args[1], args[2], args[3], args[4], args[5],
502 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400503 unlock_trace(task);
504 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700505}
506#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/************************************************************************/
509/* Here the fs part begins */
510/************************************************************************/
511
512/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700513static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700515 struct task_struct *task;
516 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700517 /* Allow access to a task's file descriptors if it is us or we
518 * may use ptrace attach to the process and find out that
519 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700520 */
521 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700522 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400523 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700524 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700525 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700526 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800529int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700530{
531 int error;
532 struct inode *inode = dentry->d_inode;
533
534 if (attr->ia_valid & ATTR_MODE)
535 return -EPERM;
536
537 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200538 if (error)
539 return error;
540
Christoph Hellwig10257742010-06-04 11:30:02 +0200541 setattr_copy(inode, attr);
542 mark_inode_dirty(inode);
543 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700544}
545
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800546/*
547 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
548 * or euid/egid (for hide_pid_min=2)?
549 */
550static bool has_pid_permissions(struct pid_namespace *pid,
551 struct task_struct *task,
552 int hide_pid_min)
553{
554 if (pid->hide_pid < hide_pid_min)
555 return true;
556 if (in_group_p(pid->pid_gid))
557 return true;
558 return ptrace_may_access(task, PTRACE_MODE_READ);
559}
560
561
562static int proc_pid_permission(struct inode *inode, int mask)
563{
564 struct pid_namespace *pid = inode->i_sb->s_fs_info;
565 struct task_struct *task;
566 bool has_perms;
567
568 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800569 if (!task)
570 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800571 has_perms = has_pid_permissions(pid, task, 1);
572 put_task_struct(task);
573
574 if (!has_perms) {
575 if (pid->hide_pid == 2) {
576 /*
577 * Let's make getdents(), stat(), and open()
578 * consistent with each other. If a process
579 * may not stat() a file, it shouldn't be seen
580 * in procfs at all.
581 */
582 return -ENOENT;
583 }
584
585 return -EPERM;
586 }
587 return generic_permission(inode, mask);
588}
589
590
591
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800592static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700593 .setattr = proc_setattr,
594};
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
597
598static ssize_t proc_info_read(struct file * file, char __user * buf,
599 size_t count, loff_t *ppos)
600{
Al Viro496ad9a2013-01-23 17:07:38 -0500601 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unsigned long page;
603 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700604 struct task_struct *task = get_proc_task(inode);
605
606 length = -ESRCH;
607 if (!task)
608 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 if (count > PROC_BLOCK_SIZE)
611 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700612
613 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700614 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700615 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 length = PROC_I(inode)->op.proc_read(task, (char*)page);
618
619 if (length >= 0)
620 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
621 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700622out:
623 put_task_struct(task);
624out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return length;
626}
627
Arjan van de Ven00977a52007-02-12 00:55:34 -0800628static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100630 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631};
632
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800633static int proc_single_show(struct seq_file *m, void *v)
634{
635 struct inode *inode = m->private;
636 struct pid_namespace *ns;
637 struct pid *pid;
638 struct task_struct *task;
639 int ret;
640
641 ns = inode->i_sb->s_fs_info;
642 pid = proc_pid(inode);
643 task = get_pid_task(pid, PIDTYPE_PID);
644 if (!task)
645 return -ESRCH;
646
647 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
648
649 put_task_struct(task);
650 return ret;
651}
652
653static int proc_single_open(struct inode *inode, struct file *filp)
654{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800655 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800656}
657
658static const struct file_operations proc_single_file_operations = {
659 .open = proc_single_open,
660 .read = seq_read,
661 .llseek = seq_lseek,
662 .release = single_release,
663};
664
Cong Wangb409e572012-05-31 16:26:17 -0700665static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Al Viro496ad9a2013-01-23 17:07:38 -0500667 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800668 struct mm_struct *mm;
669
670 if (!task)
671 return -ESRCH;
672
Cong Wangb409e572012-05-31 16:26:17 -0700673 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800674 put_task_struct(task);
675
676 if (IS_ERR(mm))
677 return PTR_ERR(mm);
678
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100679 if (mm) {
680 /* ensure this mm_struct can't be freed */
681 atomic_inc(&mm->mm_count);
682 /* but do not pin its memory */
683 mmput(mm);
684 }
685
Linus Torvaldse2683372012-01-17 15:21:19 -0800686 file->private_data = mm;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 return 0;
689}
690
Cong Wangb409e572012-05-31 16:26:17 -0700691static int mem_open(struct inode *inode, struct file *file)
692{
Djalal Harounibc452b42012-07-30 14:42:28 -0700693 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
694
695 /* OK to pass negative loff_t, we can catch out-of-range */
696 file->f_mode |= FMODE_UNSIGNED_OFFSET;
697
698 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700699}
700
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100701static ssize_t mem_rw(struct file *file, char __user *buf,
702 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Linus Torvaldse2683372012-01-17 15:21:19 -0800704 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100705 unsigned long addr = *ppos;
706 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Linus Torvaldse2683372012-01-17 15:21:19 -0800709 if (!mm)
710 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Mel Gormane12ba742007-10-16 01:25:52 -0700712 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800714 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700716 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100717 if (!atomic_inc_not_zero(&mm->mm_users))
718 goto free;
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100721 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100723 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 copied = -EFAULT;
725 break;
726 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100727
728 this_len = access_remote_vm(mm, addr, page, this_len, write);
729 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (!copied)
731 copied = -EIO;
732 break;
733 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100734
735 if (!write && copy_to_user(buf, page, this_len)) {
736 copied = -EFAULT;
737 break;
738 }
739
740 buf += this_len;
741 addr += this_len;
742 copied += this_len;
743 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100745 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700746
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100747 mmput(mm);
748free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700749 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return copied;
751}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100753static ssize_t mem_read(struct file *file, char __user *buf,
754 size_t count, loff_t *ppos)
755{
756 return mem_rw(file, buf, count, ppos, 0);
757}
758
759static ssize_t mem_write(struct file *file, const char __user *buf,
760 size_t count, loff_t *ppos)
761{
762 return mem_rw(file, (char __user*)buf, count, ppos, 1);
763}
764
Matt Mackall85863e42008-02-04 22:29:04 -0800765loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 switch (orig) {
768 case 0:
769 file->f_pos = offset;
770 break;
771 case 1:
772 file->f_pos += offset;
773 break;
774 default:
775 return -EINVAL;
776 }
777 force_successful_syscall_return();
778 return file->f_pos;
779}
780
Linus Torvaldse2683372012-01-17 15:21:19 -0800781static int mem_release(struct inode *inode, struct file *file)
782{
783 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100784 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100785 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800786 return 0;
787}
788
Arjan van de Ven00977a52007-02-12 00:55:34 -0800789static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 .llseek = mem_lseek,
791 .read = mem_read,
792 .write = mem_write,
793 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800794 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
796
Cong Wangb409e572012-05-31 16:26:17 -0700797static int environ_open(struct inode *inode, struct file *file)
798{
799 return __mem_open(inode, file, PTRACE_MODE_READ);
800}
801
James Pearson315e28c2007-10-16 23:30:17 -0700802static ssize_t environ_read(struct file *file, char __user *buf,
803 size_t count, loff_t *ppos)
804{
James Pearson315e28c2007-10-16 23:30:17 -0700805 char *page;
806 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700807 int ret = 0;
808 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700809
Cong Wangb409e572012-05-31 16:26:17 -0700810 if (!mm)
811 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700812
James Pearson315e28c2007-10-16 23:30:17 -0700813 page = (char *)__get_free_page(GFP_TEMPORARY);
814 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700815 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700816
Al Virod6f64b82011-02-15 22:26:01 -0500817 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700818 if (!atomic_inc_not_zero(&mm->mm_users))
819 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700820 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700821 size_t this_len, max_len;
822 int retval;
823
824 if (src >= (mm->env_end - mm->env_start))
825 break;
James Pearson315e28c2007-10-16 23:30:17 -0700826
827 this_len = mm->env_end - (mm->env_start + src);
828
Djalal Harounie8905ec2012-07-30 14:42:26 -0700829 max_len = min_t(size_t, PAGE_SIZE, count);
830 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700831
Cong Wangb409e572012-05-31 16:26:17 -0700832 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700833 page, this_len, 0);
834
835 if (retval <= 0) {
836 ret = retval;
837 break;
838 }
839
840 if (copy_to_user(buf, page, retval)) {
841 ret = -EFAULT;
842 break;
843 }
844
845 ret += retval;
846 src += retval;
847 buf += retval;
848 count -= retval;
849 }
850 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700851 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700852
853free:
James Pearson315e28c2007-10-16 23:30:17 -0700854 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700855 return ret;
856}
857
858static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700859 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700860 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100861 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700862 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700863};
864
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800865static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
866 loff_t *ppos)
867{
Al Viro496ad9a2013-01-23 17:07:38 -0500868 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800869 char buffer[PROC_NUMBUF];
870 int oom_adj = OOM_ADJUST_MIN;
871 size_t len;
872 unsigned long flags;
873
874 if (!task)
875 return -ESRCH;
876 if (lock_task_sighand(task, &flags)) {
877 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
878 oom_adj = OOM_ADJUST_MAX;
879 else
880 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
881 OOM_SCORE_ADJ_MAX;
882 unlock_task_sighand(task, &flags);
883 }
884 put_task_struct(task);
885 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
886 return simple_read_from_buffer(buf, count, ppos, buffer, len);
887}
888
889static ssize_t oom_adj_write(struct file *file, const char __user *buf,
890 size_t count, loff_t *ppos)
891{
892 struct task_struct *task;
893 char buffer[PROC_NUMBUF];
894 int oom_adj;
895 unsigned long flags;
896 int err;
897
898 memset(buffer, 0, sizeof(buffer));
899 if (count > sizeof(buffer) - 1)
900 count = sizeof(buffer) - 1;
901 if (copy_from_user(buffer, buf, count)) {
902 err = -EFAULT;
903 goto out;
904 }
905
906 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
907 if (err)
908 goto out;
909 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
910 oom_adj != OOM_DISABLE) {
911 err = -EINVAL;
912 goto out;
913 }
914
Al Viro496ad9a2013-01-23 17:07:38 -0500915 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800916 if (!task) {
917 err = -ESRCH;
918 goto out;
919 }
920
921 task_lock(task);
922 if (!task->mm) {
923 err = -EINVAL;
924 goto err_task_lock;
925 }
926
927 if (!lock_task_sighand(task, &flags)) {
928 err = -ESRCH;
929 goto err_task_lock;
930 }
931
932 /*
933 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
934 * value is always attainable.
935 */
936 if (oom_adj == OOM_ADJUST_MAX)
937 oom_adj = OOM_SCORE_ADJ_MAX;
938 else
939 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
940
941 if (oom_adj < task->signal->oom_score_adj &&
942 !capable(CAP_SYS_RESOURCE)) {
943 err = -EACCES;
944 goto err_sighand;
945 }
946
947 /*
948 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
949 * /proc/pid/oom_score_adj instead.
950 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800951 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 -0800952 current->comm, task_pid_nr(current), task_pid_nr(task),
953 task_pid_nr(task));
954
955 task->signal->oom_score_adj = oom_adj;
956 trace_oom_score_adj_update(task);
957err_sighand:
958 unlock_task_sighand(task, &flags);
959err_task_lock:
960 task_unlock(task);
961 put_task_struct(task);
962out:
963 return err < 0 ? err : count;
964}
965
966static const struct file_operations proc_oom_adj_operations = {
967 .read = oom_adj_read,
968 .write = oom_adj_write,
969 .llseek = generic_file_llseek,
970};
971
David Rientjesa63d83f2010-08-09 17:19:46 -0700972static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
973 size_t count, loff_t *ppos)
974{
Al Viro496ad9a2013-01-23 17:07:38 -0500975 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700976 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800977 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700978 unsigned long flags;
979 size_t len;
980
981 if (!task)
982 return -ESRCH;
983 if (lock_task_sighand(task, &flags)) {
984 oom_score_adj = task->signal->oom_score_adj;
985 unlock_task_sighand(task, &flags);
986 }
987 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800988 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700989 return simple_read_from_buffer(buf, count, ppos, buffer, len);
990}
991
992static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
993 size_t count, loff_t *ppos)
994{
995 struct task_struct *task;
996 char buffer[PROC_NUMBUF];
997 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -0700998 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -0700999 int err;
1000
1001 memset(buffer, 0, sizeof(buffer));
1002 if (count > sizeof(buffer) - 1)
1003 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001004 if (copy_from_user(buffer, buf, count)) {
1005 err = -EFAULT;
1006 goto out;
1007 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001008
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001009 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001010 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001011 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001012 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001013 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1014 err = -EINVAL;
1015 goto out;
1016 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001017
Al Viro496ad9a2013-01-23 17:07:38 -05001018 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001019 if (!task) {
1020 err = -ESRCH;
1021 goto out;
1022 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001023
Ying Han3d5992d2010-10-26 14:21:23 -07001024 task_lock(task);
1025 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001026 err = -EINVAL;
1027 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001028 }
David Rientjesd19d5472010-10-26 14:21:26 -07001029
1030 if (!lock_task_sighand(task, &flags)) {
1031 err = -ESRCH;
1032 goto err_task_lock;
1033 }
1034
David Rientjesa9c58b902012-12-11 16:02:54 -08001035 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001036 !capable(CAP_SYS_RESOURCE)) {
1037 err = -EACCES;
1038 goto err_sighand;
1039 }
1040
David Rientjesa9c58b902012-12-11 16:02:54 -08001041 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f632011-01-13 15:46:05 -08001042 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001043 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001044 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001045
David Rientjes723548b2010-10-26 14:21:25 -07001046err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001047 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001048err_task_lock:
1049 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001050 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001051out:
1052 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001053}
1054
1055static const struct file_operations proc_oom_score_adj_operations = {
1056 .read = oom_score_adj_read,
1057 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001058 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001059};
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061#ifdef CONFIG_AUDITSYSCALL
1062#define TMPBUFLEN 21
1063static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1064 size_t count, loff_t *ppos)
1065{
Al Viro496ad9a2013-01-23 17:07:38 -05001066 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001067 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ssize_t length;
1069 char tmpbuf[TMPBUFLEN];
1070
Eric W. Biederman99f89552006-06-26 00:25:55 -07001071 if (!task)
1072 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001074 from_kuid(file->f_cred->user_ns,
1075 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001076 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1078}
1079
1080static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1081 size_t count, loff_t *ppos)
1082{
Al Viro496ad9a2013-01-23 17:07:38 -05001083 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 char *page, *tmp;
1085 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001087 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001089 rcu_read_lock();
1090 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1091 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001093 }
1094 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Al Viroe0182902006-05-18 08:28:02 -04001096 if (count >= PAGE_SIZE)
1097 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (*ppos != 0) {
1100 /* No partial writes. */
1101 return -EINVAL;
1102 }
Mel Gormane12ba742007-10-16 01:25:52 -07001103 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!page)
1105 return -ENOMEM;
1106 length = -EFAULT;
1107 if (copy_from_user(page, buf, count))
1108 goto out_free_page;
1109
Al Viroe0182902006-05-18 08:28:02 -04001110 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 loginuid = simple_strtoul(page, &tmp, 10);
1112 if (tmp == page) {
1113 length = -EINVAL;
1114 goto out_free_page;
1115
1116 }
Eric Paris81407c82013-05-24 09:49:14 -04001117
1118 /* is userspace tring to explicitly UNSET the loginuid? */
1119 if (loginuid == AUDIT_UID_UNSET) {
1120 kloginuid = INVALID_UID;
1121 } else {
1122 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1123 if (!uid_valid(kloginuid)) {
1124 length = -EINVAL;
1125 goto out_free_page;
1126 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001127 }
1128
1129 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (likely(length == 0))
1131 length = count;
1132
1133out_free_page:
1134 free_page((unsigned long) page);
1135 return length;
1136}
1137
Arjan van de Ven00977a52007-02-12 00:55:34 -08001138static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 .read = proc_loginuid_read,
1140 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001141 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};
Eric Paris1e0bd752008-03-13 08:15:31 -04001143
1144static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1145 size_t count, loff_t *ppos)
1146{
Al Viro496ad9a2013-01-23 17:07:38 -05001147 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001148 struct task_struct *task = get_proc_task(inode);
1149 ssize_t length;
1150 char tmpbuf[TMPBUFLEN];
1151
1152 if (!task)
1153 return -ESRCH;
1154 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1155 audit_get_sessionid(task));
1156 put_task_struct(task);
1157 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1158}
1159
1160static const struct file_operations proc_sessionid_operations = {
1161 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001162 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001163};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164#endif
1165
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001166#ifdef CONFIG_FAULT_INJECTION
1167static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1168 size_t count, loff_t *ppos)
1169{
Al Viro496ad9a2013-01-23 17:07:38 -05001170 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001171 char buffer[PROC_NUMBUF];
1172 size_t len;
1173 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001174
1175 if (!task)
1176 return -ESRCH;
1177 make_it_fail = task->make_it_fail;
1178 put_task_struct(task);
1179
1180 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001181
1182 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001183}
1184
1185static ssize_t proc_fault_inject_write(struct file * file,
1186 const char __user * buf, size_t count, loff_t *ppos)
1187{
1188 struct task_struct *task;
1189 char buffer[PROC_NUMBUF], *end;
1190 int make_it_fail;
1191
1192 if (!capable(CAP_SYS_RESOURCE))
1193 return -EPERM;
1194 memset(buffer, 0, sizeof(buffer));
1195 if (count > sizeof(buffer) - 1)
1196 count = sizeof(buffer) - 1;
1197 if (copy_from_user(buffer, buf, count))
1198 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001199 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1200 if (*end)
1201 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001202 if (make_it_fail < 0 || make_it_fail > 1)
1203 return -EINVAL;
1204
Al Viro496ad9a2013-01-23 17:07:38 -05001205 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001206 if (!task)
1207 return -ESRCH;
1208 task->make_it_fail = make_it_fail;
1209 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001210
1211 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001212}
1213
Arjan van de Ven00977a52007-02-12 00:55:34 -08001214static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001215 .read = proc_fault_inject_read,
1216 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001217 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001218};
1219#endif
1220
Arjan van de Ven97455122008-01-25 21:08:34 +01001221
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001222#ifdef CONFIG_SCHED_DEBUG
1223/*
1224 * Print out various scheduling related per-task fields:
1225 */
1226static int sched_show(struct seq_file *m, void *v)
1227{
1228 struct inode *inode = m->private;
1229 struct task_struct *p;
1230
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001231 p = get_proc_task(inode);
1232 if (!p)
1233 return -ESRCH;
1234 proc_sched_show_task(p, m);
1235
1236 put_task_struct(p);
1237
1238 return 0;
1239}
1240
1241static ssize_t
1242sched_write(struct file *file, const char __user *buf,
1243 size_t count, loff_t *offset)
1244{
Al Viro496ad9a2013-01-23 17:07:38 -05001245 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001246 struct task_struct *p;
1247
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001248 p = get_proc_task(inode);
1249 if (!p)
1250 return -ESRCH;
1251 proc_sched_set_task(p);
1252
1253 put_task_struct(p);
1254
1255 return count;
1256}
1257
1258static int sched_open(struct inode *inode, struct file *filp)
1259{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001260 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001261}
1262
1263static const struct file_operations proc_pid_sched_operations = {
1264 .open = sched_open,
1265 .read = seq_read,
1266 .write = sched_write,
1267 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001268 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001269};
1270
1271#endif
1272
Mike Galbraith5091faa2010-11-30 14:18:03 +01001273#ifdef CONFIG_SCHED_AUTOGROUP
1274/*
1275 * Print out autogroup related information:
1276 */
1277static int sched_autogroup_show(struct seq_file *m, void *v)
1278{
1279 struct inode *inode = m->private;
1280 struct task_struct *p;
1281
1282 p = get_proc_task(inode);
1283 if (!p)
1284 return -ESRCH;
1285 proc_sched_autogroup_show_task(p, m);
1286
1287 put_task_struct(p);
1288
1289 return 0;
1290}
1291
1292static ssize_t
1293sched_autogroup_write(struct file *file, const char __user *buf,
1294 size_t count, loff_t *offset)
1295{
Al Viro496ad9a2013-01-23 17:07:38 -05001296 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001297 struct task_struct *p;
1298 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001299 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001300 int err;
1301
1302 memset(buffer, 0, sizeof(buffer));
1303 if (count > sizeof(buffer) - 1)
1304 count = sizeof(buffer) - 1;
1305 if (copy_from_user(buffer, buf, count))
1306 return -EFAULT;
1307
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001308 err = kstrtoint(strstrip(buffer), 0, &nice);
1309 if (err < 0)
1310 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001311
1312 p = get_proc_task(inode);
1313 if (!p)
1314 return -ESRCH;
1315
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001316 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001317 if (err)
1318 count = err;
1319
1320 put_task_struct(p);
1321
1322 return count;
1323}
1324
1325static int sched_autogroup_open(struct inode *inode, struct file *filp)
1326{
1327 int ret;
1328
1329 ret = single_open(filp, sched_autogroup_show, NULL);
1330 if (!ret) {
1331 struct seq_file *m = filp->private_data;
1332
1333 m->private = inode;
1334 }
1335 return ret;
1336}
1337
1338static const struct file_operations proc_pid_sched_autogroup_operations = {
1339 .open = sched_autogroup_open,
1340 .read = seq_read,
1341 .write = sched_autogroup_write,
1342 .llseek = seq_lseek,
1343 .release = single_release,
1344};
1345
1346#endif /* CONFIG_SCHED_AUTOGROUP */
1347
john stultz4614a696b2009-12-14 18:00:05 -08001348static ssize_t comm_write(struct file *file, const char __user *buf,
1349 size_t count, loff_t *offset)
1350{
Al Viro496ad9a2013-01-23 17:07:38 -05001351 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001352 struct task_struct *p;
1353 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001354 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001355
1356 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001357 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001358 return -EFAULT;
1359
1360 p = get_proc_task(inode);
1361 if (!p)
1362 return -ESRCH;
1363
1364 if (same_thread_group(current, p))
1365 set_task_comm(p, buffer);
1366 else
1367 count = -EINVAL;
1368
1369 put_task_struct(p);
1370
1371 return count;
1372}
1373
1374static int comm_show(struct seq_file *m, void *v)
1375{
1376 struct inode *inode = m->private;
1377 struct task_struct *p;
1378
1379 p = get_proc_task(inode);
1380 if (!p)
1381 return -ESRCH;
1382
1383 task_lock(p);
1384 seq_printf(m, "%s\n", p->comm);
1385 task_unlock(p);
1386
1387 put_task_struct(p);
1388
1389 return 0;
1390}
1391
1392static int comm_open(struct inode *inode, struct file *filp)
1393{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001394 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001395}
1396
1397static const struct file_operations proc_pid_set_comm_operations = {
1398 .open = comm_open,
1399 .read = seq_read,
1400 .write = comm_write,
1401 .llseek = seq_lseek,
1402 .release = single_release,
1403};
1404
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001405static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001406{
1407 struct task_struct *task;
1408 struct mm_struct *mm;
1409 struct file *exe_file;
1410
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001411 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001412 if (!task)
1413 return -ENOENT;
1414 mm = get_task_mm(task);
1415 put_task_struct(task);
1416 if (!mm)
1417 return -ENOENT;
1418 exe_file = get_mm_exe_file(mm);
1419 mmput(mm);
1420 if (exe_file) {
1421 *exe_path = exe_file->f_path;
1422 path_get(&exe_file->f_path);
1423 fput(exe_file);
1424 return 0;
1425 } else
1426 return -ENOENT;
1427}
1428
Al Viro008b1502005-08-20 00:17:39 +01001429static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001432 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int error = -EACCES;
1434
Eric W. Biederman778c1142006-06-26 00:25:58 -07001435 /* Are we allowed to snoop on the tasks file descriptors? */
1436 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Christoph Hellwig408ef012012-06-18 10:47:03 -04001439 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1440 if (error)
1441 goto out;
1442
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001443 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001444 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445out:
Al Viro008b1502005-08-20 00:17:39 +01001446 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001449static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Mel Gormane12ba742007-10-16 01:25:52 -07001451 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001452 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 int len;
1454
1455 if (!tmp)
1456 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001457
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001458 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001459 len = PTR_ERR(pathname);
1460 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001462 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 if (len > buflen)
1465 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001466 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 len = -EFAULT;
1468 out:
1469 free_page((unsigned long)tmp);
1470 return len;
1471}
1472
1473static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1474{
1475 int error = -EACCES;
1476 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001477 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Eric W. Biederman778c1142006-06-26 00:25:58 -07001479 /* Are we allowed to snoop on the tasks file descriptors? */
1480 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001483 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (error)
1485 goto out;
1486
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001487 error = do_proc_readlink(&path, buffer, buflen);
1488 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return error;
1491}
1492
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001493const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001495 .follow_link = proc_pid_follow_link,
1496 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497};
1498
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001499
1500/* building an inode */
1501
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001502struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001503{
1504 struct inode * inode;
1505 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001506 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001507
1508 /* We need a new inode */
1509
1510 inode = new_inode(sb);
1511 if (!inode)
1512 goto out;
1513
1514 /* Common stuff */
1515 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001516 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001517 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001518 inode->i_op = &proc_def_inode_operations;
1519
1520 /*
1521 * grab the reference to task.
1522 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001523 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001524 if (!ei->pid)
1525 goto out_unlock;
1526
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001528 rcu_read_lock();
1529 cred = __task_cred(task);
1530 inode->i_uid = cred->euid;
1531 inode->i_gid = cred->egid;
1532 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001533 }
1534 security_task_to_inode(task, inode);
1535
1536out:
1537 return inode;
1538
1539out_unlock:
1540 iput(inode);
1541 return NULL;
1542}
1543
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001544int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001545{
1546 struct inode *inode = dentry->d_inode;
1547 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001548 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001549 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001550
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001551 generic_fillattr(inode, stat);
1552
1553 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001554 stat->uid = GLOBAL_ROOT_UID;
1555 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001556 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1557 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001558 if (!has_pid_permissions(pid, task, 2)) {
1559 rcu_read_unlock();
1560 /*
1561 * This doesn't prevent learning whether PID exists,
1562 * it only makes getattr() consistent with readdir().
1563 */
1564 return -ENOENT;
1565 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001566 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1567 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001568 cred = __task_cred(task);
1569 stat->uid = cred->euid;
1570 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001571 }
1572 }
1573 rcu_read_unlock();
1574 return 0;
1575}
1576
1577/* dentry stuff */
1578
1579/*
1580 * Exceptional case: normally we are not allowed to unhash a busy
1581 * directory. In this case, however, we can do it - no aliasing problems
1582 * due to the way we treat inodes.
1583 *
1584 * Rewrite the inode's ownerships here because the owning task may have
1585 * performed a setuid(), etc.
1586 *
1587 * Before the /proc/pid/status file was created the only way to read
1588 * the effective uid of a /process was to stat /proc/pid. Reading
1589 * /proc/pid/status is slow enough that procps and other packages
1590 * kept stating /proc/pid. To keep the rules in /proc simple I have
1591 * made this apply to all per process world readable and executable
1592 * directories.
1593 */
Al Viro0b728e12012-06-10 16:03:43 -04001594int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001595{
Nick Piggin34286d62011-01-07 17:49:57 +11001596 struct inode *inode;
1597 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001598 const struct cred *cred;
1599
Al Viro0b728e12012-06-10 16:03:43 -04001600 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001601 return -ECHILD;
1602
1603 inode = dentry->d_inode;
1604 task = get_proc_task(inode);
1605
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001606 if (task) {
1607 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1608 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001609 rcu_read_lock();
1610 cred = __task_cred(task);
1611 inode->i_uid = cred->euid;
1612 inode->i_gid = cred->egid;
1613 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001615 inode->i_uid = GLOBAL_ROOT_UID;
1616 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001617 }
1618 inode->i_mode &= ~(S_ISUID | S_ISGID);
1619 security_task_to_inode(task, inode);
1620 put_task_struct(task);
1621 return 1;
1622 }
1623 d_drop(dentry);
1624 return 0;
1625}
1626
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001627static inline bool proc_inode_is_dead(struct inode *inode)
1628{
1629 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1630}
1631
David Howells1dd704b2013-04-12 01:08:50 +01001632int pid_delete_dentry(const struct dentry *dentry)
1633{
1634 /* Is the task we represent dead?
1635 * If so, then don't put the dentry on the lru list,
1636 * kill it immediately.
1637 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001638 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001639}
1640
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001641const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001642{
1643 .d_revalidate = pid_revalidate,
1644 .d_delete = pid_delete_dentry,
1645};
1646
1647/* Lookups */
1648
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001649/*
1650 * Fill a directory entry.
1651 *
1652 * If possible create the dcache entry and derive our inode number and
1653 * file type from dcache entry.
1654 *
1655 * Since all of the proc inode numbers are dynamically generated, the inode
1656 * numbers do not exist until the inode is cache. This means creating the
1657 * the dcache entry in readdir is necessary to keep the inode numbers
1658 * reported by readdir in sync with the inode numbers reported
1659 * by stat.
1660 */
Al Virof0c3b502013-05-16 12:07:31 -04001661bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001662 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001663 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001664{
Al Virof0c3b502013-05-16 12:07:31 -04001665 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001666 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001667 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001668 unsigned type;
1669 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001670
Al Viro1df98b82013-06-15 11:33:10 +04001671 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001672 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001673 child = d_alloc(dir, &qname);
1674 if (!child)
1675 goto end_instantiate;
1676 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1677 dput(child);
1678 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001679 }
1680 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001681 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001682 ino = inode->i_ino;
1683 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001684 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001685 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001686
1687end_instantiate:
1688 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001689}
1690
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001691#ifdef CONFIG_CHECKPOINT_RESTORE
1692
1693/*
1694 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1695 * which represent vma start and end addresses.
1696 */
1697static int dname_to_vma_addr(struct dentry *dentry,
1698 unsigned long *start, unsigned long *end)
1699{
1700 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1701 return -EINVAL;
1702
1703 return 0;
1704}
1705
Al Viro0b728e12012-06-10 16:03:43 -04001706static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001707{
1708 unsigned long vm_start, vm_end;
1709 bool exact_vma_exists = false;
1710 struct mm_struct *mm = NULL;
1711 struct task_struct *task;
1712 const struct cred *cred;
1713 struct inode *inode;
1714 int status = 0;
1715
Al Viro0b728e12012-06-10 16:03:43 -04001716 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001717 return -ECHILD;
1718
1719 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001720 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001721 goto out_notask;
1722 }
1723
1724 inode = dentry->d_inode;
1725 task = get_proc_task(inode);
1726 if (!task)
1727 goto out_notask;
1728
Cong Wang2344bec2012-05-31 16:26:18 -07001729 mm = mm_access(task, PTRACE_MODE_READ);
1730 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001731 goto out;
1732
1733 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1734 down_read(&mm->mmap_sem);
1735 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1736 up_read(&mm->mmap_sem);
1737 }
1738
1739 mmput(mm);
1740
1741 if (exact_vma_exists) {
1742 if (task_dumpable(task)) {
1743 rcu_read_lock();
1744 cred = __task_cred(task);
1745 inode->i_uid = cred->euid;
1746 inode->i_gid = cred->egid;
1747 rcu_read_unlock();
1748 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001749 inode->i_uid = GLOBAL_ROOT_UID;
1750 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001751 }
1752 security_task_to_inode(task, inode);
1753 status = 1;
1754 }
1755
1756out:
1757 put_task_struct(task);
1758
1759out_notask:
1760 if (status <= 0)
1761 d_drop(dentry);
1762
1763 return status;
1764}
1765
1766static const struct dentry_operations tid_map_files_dentry_operations = {
1767 .d_revalidate = map_files_d_revalidate,
1768 .d_delete = pid_delete_dentry,
1769};
1770
1771static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1772{
1773 unsigned long vm_start, vm_end;
1774 struct vm_area_struct *vma;
1775 struct task_struct *task;
1776 struct mm_struct *mm;
1777 int rc;
1778
1779 rc = -ENOENT;
1780 task = get_proc_task(dentry->d_inode);
1781 if (!task)
1782 goto out;
1783
1784 mm = get_task_mm(task);
1785 put_task_struct(task);
1786 if (!mm)
1787 goto out;
1788
1789 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1790 if (rc)
1791 goto out_mmput;
1792
Artem Fetishev70335ab2014-03-10 15:49:45 -07001793 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001794 down_read(&mm->mmap_sem);
1795 vma = find_exact_vma(mm, vm_start, vm_end);
1796 if (vma && vma->vm_file) {
1797 *path = vma->vm_file->f_path;
1798 path_get(path);
1799 rc = 0;
1800 }
1801 up_read(&mm->mmap_sem);
1802
1803out_mmput:
1804 mmput(mm);
1805out:
1806 return rc;
1807}
1808
1809struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001810 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001811 unsigned long len;
1812 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1813};
1814
Al Viroc52a47a2013-06-15 11:15:20 +04001815static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001816proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1817 struct task_struct *task, const void *ptr)
1818{
Al Viro7b540d02012-08-27 14:55:26 -04001819 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001820 struct proc_inode *ei;
1821 struct inode *inode;
1822
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001823 inode = proc_pid_make_inode(dir->i_sb, task);
1824 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001825 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001826
1827 ei = PROC_I(inode);
1828 ei->op.proc_get_link = proc_map_files_get_link;
1829
1830 inode->i_op = &proc_pid_link_inode_operations;
1831 inode->i_size = 64;
1832 inode->i_mode = S_IFLNK;
1833
Al Viro7b540d02012-08-27 14:55:26 -04001834 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001835 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001836 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001837 inode->i_mode |= S_IWUSR;
1838
1839 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1840 d_add(dentry, inode);
1841
Al Viroc52a47a2013-06-15 11:15:20 +04001842 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001843}
1844
1845static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001846 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001847{
1848 unsigned long vm_start, vm_end;
1849 struct vm_area_struct *vma;
1850 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001851 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001852 struct mm_struct *mm;
1853
Al Viroc52a47a2013-06-15 11:15:20 +04001854 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001855 if (!capable(CAP_SYS_ADMIN))
1856 goto out;
1857
Al Viroc52a47a2013-06-15 11:15:20 +04001858 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001859 task = get_proc_task(dir);
1860 if (!task)
1861 goto out;
1862
Al Viroc52a47a2013-06-15 11:15:20 +04001863 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001864 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001865 goto out_put_task;
1866
Al Viroc52a47a2013-06-15 11:15:20 +04001867 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001868 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001869 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001870
1871 mm = get_task_mm(task);
1872 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001873 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001874
1875 down_read(&mm->mmap_sem);
1876 vma = find_exact_vma(mm, vm_start, vm_end);
1877 if (!vma)
1878 goto out_no_vma;
1879
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001880 if (vma->vm_file)
1881 result = proc_map_files_instantiate(dir, dentry, task,
1882 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001883
1884out_no_vma:
1885 up_read(&mm->mmap_sem);
1886 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001887out_put_task:
1888 put_task_struct(task);
1889out:
Al Viroc52a47a2013-06-15 11:15:20 +04001890 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001891}
1892
1893static const struct inode_operations proc_map_files_inode_operations = {
1894 .lookup = proc_map_files_lookup,
1895 .permission = proc_fd_permission,
1896 .setattr = proc_setattr,
1897};
1898
1899static int
Al Virof0c3b502013-05-16 12:07:31 -04001900proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001901{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001902 struct vm_area_struct *vma;
1903 struct task_struct *task;
1904 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001905 unsigned long nr_files, pos, i;
1906 struct flex_array *fa = NULL;
1907 struct map_files_info info;
1908 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001909 int ret;
1910
Zhao Hongjiang41735812013-02-20 13:13:55 +11001911 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001912 if (!capable(CAP_SYS_ADMIN))
1913 goto out;
1914
1915 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001916 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001917 if (!task)
1918 goto out;
1919
1920 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001921 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001922 goto out_put_task;
1923
1924 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001925 if (!dir_emit_dots(file, ctx))
1926 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001927
Al Virof0c3b502013-05-16 12:07:31 -04001928 mm = get_task_mm(task);
1929 if (!mm)
1930 goto out_put_task;
1931 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001932
Al Virof0c3b502013-05-16 12:07:31 -04001933 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934
Al Virof0c3b502013-05-16 12:07:31 -04001935 /*
1936 * We need two passes here:
1937 *
1938 * 1) Collect vmas of mapped files with mmap_sem taken
1939 * 2) Release mmap_sem and instantiate entries
1940 *
1941 * otherwise we get lockdep complained, since filldir()
1942 * routine might require mmap_sem taken in might_fault().
1943 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001944
Al Virof0c3b502013-05-16 12:07:31 -04001945 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1946 if (vma->vm_file && ++pos > ctx->pos)
1947 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001948 }
Al Virof0c3b502013-05-16 12:07:31 -04001949
1950 if (nr_files) {
1951 fa = flex_array_alloc(sizeof(info), nr_files,
1952 GFP_KERNEL);
1953 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1954 GFP_KERNEL)) {
1955 ret = -ENOMEM;
1956 if (fa)
1957 flex_array_free(fa);
1958 up_read(&mm->mmap_sem);
1959 mmput(mm);
1960 goto out_put_task;
1961 }
1962 for (i = 0, vma = mm->mmap, pos = 2; vma;
1963 vma = vma->vm_next) {
1964 if (!vma->vm_file)
1965 continue;
1966 if (++pos <= ctx->pos)
1967 continue;
1968
1969 info.mode = vma->vm_file->f_mode;
1970 info.len = snprintf(info.name,
1971 sizeof(info.name), "%lx-%lx",
1972 vma->vm_start, vma->vm_end);
1973 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1974 BUG();
1975 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001976 }
Al Virof0c3b502013-05-16 12:07:31 -04001977 up_read(&mm->mmap_sem);
1978
1979 for (i = 0; i < nr_files; i++) {
1980 p = flex_array_get(fa, i);
1981 if (!proc_fill_cache(file, ctx,
1982 p->name, p->len,
1983 proc_map_files_instantiate,
1984 task,
1985 (void *)(unsigned long)p->mode))
1986 break;
1987 ctx->pos++;
1988 }
1989 if (fa)
1990 flex_array_free(fa);
1991 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001992
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001993out_put_task:
1994 put_task_struct(task);
1995out:
1996 return ret;
1997}
1998
1999static const struct file_operations proc_map_files_operations = {
2000 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002001 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002002 .llseek = default_llseek,
2003};
2004
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002005struct timers_private {
2006 struct pid *pid;
2007 struct task_struct *task;
2008 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002009 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002010 unsigned long flags;
2011};
2012
2013static void *timers_start(struct seq_file *m, loff_t *pos)
2014{
2015 struct timers_private *tp = m->private;
2016
2017 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2018 if (!tp->task)
2019 return ERR_PTR(-ESRCH);
2020
2021 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2022 if (!tp->sighand)
2023 return ERR_PTR(-ESRCH);
2024
2025 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2026}
2027
2028static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2029{
2030 struct timers_private *tp = m->private;
2031 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2032}
2033
2034static void timers_stop(struct seq_file *m, void *v)
2035{
2036 struct timers_private *tp = m->private;
2037
2038 if (tp->sighand) {
2039 unlock_task_sighand(tp->task, &tp->flags);
2040 tp->sighand = NULL;
2041 }
2042
2043 if (tp->task) {
2044 put_task_struct(tp->task);
2045 tp->task = NULL;
2046 }
2047}
2048
2049static int show_timer(struct seq_file *m, void *v)
2050{
2051 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002052 struct timers_private *tp = m->private;
2053 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002054 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002055 [SIGEV_SIGNAL] = "signal",
2056 [SIGEV_NONE] = "none",
2057 [SIGEV_THREAD] = "thread",
2058 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002059
2060 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002061 notify = timer->it_sigev_notify;
2062
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002063 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002064 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2065 timer->sigq->info.si_value.sival_ptr);
2066 seq_printf(m, "notify: %s/%s.%d\n",
2067 nstr[notify & ~SIGEV_THREAD_ID],
2068 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2069 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002070 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002071
2072 return 0;
2073}
2074
2075static const struct seq_operations proc_timers_seq_ops = {
2076 .start = timers_start,
2077 .next = timers_next,
2078 .stop = timers_stop,
2079 .show = show_timer,
2080};
2081
2082static int proc_timers_open(struct inode *inode, struct file *file)
2083{
2084 struct timers_private *tp;
2085
2086 tp = __seq_open_private(file, &proc_timers_seq_ops,
2087 sizeof(struct timers_private));
2088 if (!tp)
2089 return -ENOMEM;
2090
2091 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002092 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002093 return 0;
2094}
2095
2096static const struct file_operations proc_timers_operations = {
2097 .open = proc_timers_open,
2098 .read = seq_read,
2099 .llseek = seq_lseek,
2100 .release = seq_release_private,
2101};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002102#endif /* CONFIG_CHECKPOINT_RESTORE */
2103
Al Viroc52a47a2013-06-15 11:15:20 +04002104static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002105 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002106{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002107 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002108 struct inode *inode;
2109 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002110
Eric W. Biederman61a28782006-10-02 02:18:49 -07002111 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002112 if (!inode)
2113 goto out;
2114
2115 ei = PROC_I(inode);
2116 inode->i_mode = p->mode;
2117 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002118 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002119 if (p->iop)
2120 inode->i_op = p->iop;
2121 if (p->fop)
2122 inode->i_fop = p->fop;
2123 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002124 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002125 d_add(dentry, inode);
2126 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002127 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002128 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002129out:
Al Viroc52a47a2013-06-15 11:15:20 +04002130 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002131}
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133static struct dentry *proc_pident_lookup(struct inode *dir,
2134 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002135 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002136 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Al Viroc52a47a2013-06-15 11:15:20 +04002138 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002139 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002140 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Al Viroc52a47a2013-06-15 11:15:20 +04002142 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Eric W. Biederman99f89552006-06-26 00:25:55 -07002144 if (!task)
2145 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002147 /*
2148 * Yes, it does not scale. And it should not. Don't add
2149 * new entries into /proc/<tgid>/ without very good reasons.
2150 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002151 last = &ents[nents - 1];
2152 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (p->len != dentry->d_name.len)
2154 continue;
2155 if (!memcmp(dentry->d_name.name, p->name, p->len))
2156 break;
2157 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002158 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 goto out;
2160
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002161 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002163 put_task_struct(task);
2164out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002165 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Al Virof0c3b502013-05-16 12:07:31 -04002168static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002169 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002170{
Al Virof0c3b502013-05-16 12:07:31 -04002171 struct task_struct *task = get_proc_task(file_inode(file));
2172 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002173
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002174 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002175 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002176
Al Virof0c3b502013-05-16 12:07:31 -04002177 if (!dir_emit_dots(file, ctx))
2178 goto out;
2179
2180 if (ctx->pos >= nents + 2)
2181 goto out;
2182
2183 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2184 if (!proc_fill_cache(file, ctx, p->name, p->len,
2185 proc_pident_instantiate, task, p))
2186 break;
2187 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002188 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002189out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002190 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002195static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2196 size_t count, loff_t *ppos)
2197{
Al Viro496ad9a2013-01-23 17:07:38 -05002198 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002199 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002200 ssize_t length;
2201 struct task_struct *task = get_proc_task(inode);
2202
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002203 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002204 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205
2206 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002207 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002208 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002209 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002210 if (length > 0)
2211 length = simple_read_from_buffer(buf, count, ppos, p, length);
2212 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002213 return length;
2214}
2215
2216static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2217 size_t count, loff_t *ppos)
2218{
Al Viro496ad9a2013-01-23 17:07:38 -05002219 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002220 char *page;
2221 ssize_t length;
2222 struct task_struct *task = get_proc_task(inode);
2223
2224 length = -ESRCH;
2225 if (!task)
2226 goto out_no_task;
2227 if (count > PAGE_SIZE)
2228 count = PAGE_SIZE;
2229
2230 /* No partial writes. */
2231 length = -EINVAL;
2232 if (*ppos != 0)
2233 goto out;
2234
2235 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002236 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002237 if (!page)
2238 goto out;
2239
2240 length = -EFAULT;
2241 if (copy_from_user(page, buf, count))
2242 goto out_free;
2243
David Howells107db7c2009-05-08 13:55:27 +01002244 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002245 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002246 if (length < 0)
2247 goto out_free;
2248
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002249 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002250 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002251 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002252 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002253out_free:
2254 free_page((unsigned long) page);
2255out:
2256 put_task_struct(task);
2257out_no_task:
2258 return length;
2259}
2260
Arjan van de Ven00977a52007-02-12 00:55:34 -08002261static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002262 .read = proc_pid_attr_read,
2263 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002264 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002265};
2266
Eric Dumazetc5141e62007-05-08 00:26:15 -07002267static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002268 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2269 REG("prev", S_IRUGO, proc_pid_attr_operations),
2270 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2271 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2272 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2273 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002274};
2275
Al Virof0c3b502013-05-16 12:07:31 -04002276static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Al Virof0c3b502013-05-16 12:07:31 -04002278 return proc_pident_readdir(file, ctx,
2279 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
Arjan van de Ven00977a52007-02-12 00:55:34 -08002282static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002284 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002285 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286};
2287
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002288static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002289 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002291 return proc_pident_lookup(dir, dentry,
2292 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002295static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002296 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002297 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002298 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299};
2300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301#endif
2302
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002303#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002304static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2305 size_t count, loff_t *ppos)
2306{
Al Viro496ad9a2013-01-23 17:07:38 -05002307 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002308 struct mm_struct *mm;
2309 char buffer[PROC_NUMBUF];
2310 size_t len;
2311 int ret;
2312
2313 if (!task)
2314 return -ESRCH;
2315
2316 ret = 0;
2317 mm = get_task_mm(task);
2318 if (mm) {
2319 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2320 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2321 MMF_DUMP_FILTER_SHIFT));
2322 mmput(mm);
2323 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2324 }
2325
2326 put_task_struct(task);
2327
2328 return ret;
2329}
2330
2331static ssize_t proc_coredump_filter_write(struct file *file,
2332 const char __user *buf,
2333 size_t count,
2334 loff_t *ppos)
2335{
2336 struct task_struct *task;
2337 struct mm_struct *mm;
2338 char buffer[PROC_NUMBUF], *end;
2339 unsigned int val;
2340 int ret;
2341 int i;
2342 unsigned long mask;
2343
2344 ret = -EFAULT;
2345 memset(buffer, 0, sizeof(buffer));
2346 if (count > sizeof(buffer) - 1)
2347 count = sizeof(buffer) - 1;
2348 if (copy_from_user(buffer, buf, count))
2349 goto out_no_task;
2350
2351 ret = -EINVAL;
2352 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2353 if (*end == '\n')
2354 end++;
2355 if (end - buffer == 0)
2356 goto out_no_task;
2357
2358 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002359 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002360 if (!task)
2361 goto out_no_task;
2362
2363 ret = end - buffer;
2364 mm = get_task_mm(task);
2365 if (!mm)
2366 goto out_no_mm;
2367
2368 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2369 if (val & mask)
2370 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2371 else
2372 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2373 }
2374
2375 mmput(mm);
2376 out_no_mm:
2377 put_task_struct(task);
2378 out_no_task:
2379 return ret;
2380}
2381
2382static const struct file_operations proc_coredump_filter_operations = {
2383 .read = proc_coredump_filter_read,
2384 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002385 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002386};
2387#endif
2388
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002389#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002390static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002391{
Andrea Righi940389b2008-07-28 00:48:12 +02002392 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002393 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002394 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002395
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002396 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2397 if (result)
2398 return result;
2399
2400 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2401 result = -EACCES;
2402 goto out_unlock;
2403 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002404
Andrea Righi59954772008-07-27 17:29:15 +02002405 if (whole && lock_task_sighand(task, &flags)) {
2406 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002407
Andrea Righi59954772008-07-27 17:29:15 +02002408 task_io_accounting_add(&acct, &task->signal->ioac);
2409 while_each_thread(task, t)
2410 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002411
Andrea Righi59954772008-07-27 17:29:15 +02002412 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002413 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002414 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002415 "rchar: %llu\n"
2416 "wchar: %llu\n"
2417 "syscr: %llu\n"
2418 "syscw: %llu\n"
2419 "read_bytes: %llu\n"
2420 "write_bytes: %llu\n"
2421 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002422 (unsigned long long)acct.rchar,
2423 (unsigned long long)acct.wchar,
2424 (unsigned long long)acct.syscr,
2425 (unsigned long long)acct.syscw,
2426 (unsigned long long)acct.read_bytes,
2427 (unsigned long long)acct.write_bytes,
2428 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002429out_unlock:
2430 mutex_unlock(&task->signal->cred_guard_mutex);
2431 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002432}
Andrea Righi297c5d92008-07-25 01:48:49 -07002433
2434static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2435{
2436 return do_io_accounting(task, buffer, 0);
2437}
2438
2439static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2440{
2441 return do_io_accounting(task, buffer, 1);
2442}
2443#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002444
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002445#ifdef CONFIG_USER_NS
2446static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002447 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002448{
2449 struct user_namespace *ns = NULL;
2450 struct task_struct *task;
2451 struct seq_file *seq;
2452 int ret = -EINVAL;
2453
2454 task = get_proc_task(inode);
2455 if (task) {
2456 rcu_read_lock();
2457 ns = get_user_ns(task_cred_xxx(task, user_ns));
2458 rcu_read_unlock();
2459 put_task_struct(task);
2460 }
2461 if (!ns)
2462 goto err;
2463
2464 ret = seq_open(file, seq_ops);
2465 if (ret)
2466 goto err_put_ns;
2467
2468 seq = file->private_data;
2469 seq->private = ns;
2470
2471 return 0;
2472err_put_ns:
2473 put_user_ns(ns);
2474err:
2475 return ret;
2476}
2477
2478static int proc_id_map_release(struct inode *inode, struct file *file)
2479{
2480 struct seq_file *seq = file->private_data;
2481 struct user_namespace *ns = seq->private;
2482 put_user_ns(ns);
2483 return seq_release(inode, file);
2484}
2485
2486static int proc_uid_map_open(struct inode *inode, struct file *file)
2487{
2488 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2489}
2490
2491static int proc_gid_map_open(struct inode *inode, struct file *file)
2492{
2493 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2494}
2495
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002496static int proc_projid_map_open(struct inode *inode, struct file *file)
2497{
2498 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2499}
2500
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002501static const struct file_operations proc_uid_map_operations = {
2502 .open = proc_uid_map_open,
2503 .write = proc_uid_map_write,
2504 .read = seq_read,
2505 .llseek = seq_lseek,
2506 .release = proc_id_map_release,
2507};
2508
2509static const struct file_operations proc_gid_map_operations = {
2510 .open = proc_gid_map_open,
2511 .write = proc_gid_map_write,
2512 .read = seq_read,
2513 .llseek = seq_lseek,
2514 .release = proc_id_map_release,
2515};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002516
2517static const struct file_operations proc_projid_map_operations = {
2518 .open = proc_projid_map_open,
2519 .write = proc_projid_map_write,
2520 .read = seq_read,
2521 .llseek = seq_lseek,
2522 .release = proc_id_map_release,
2523};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002524#endif /* CONFIG_USER_NS */
2525
Kees Cook47830722008-10-06 03:11:58 +04002526static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2527 struct pid *pid, struct task_struct *task)
2528{
Al Viroa9712bc2011-03-23 15:52:50 -04002529 int err = lock_trace(task);
2530 if (!err) {
2531 seq_printf(m, "%08x\n", task->personality);
2532 unlock_trace(task);
2533 }
2534 return err;
Kees Cook47830722008-10-06 03:11:58 +04002535}
2536
Eric W. Biederman801199c2006-10-02 02:18:48 -07002537/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002538 * Thread groups
2539 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002540static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002541static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002542
Eric Dumazetc5141e62007-05-08 00:26:15 -07002543static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002544 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2545 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002546#ifdef CONFIG_CHECKPOINT_RESTORE
2547 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2548#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002549 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002550 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002551#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002552 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002553#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002554 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002555 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002556 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002557 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002558 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002559#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002560 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002561#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002562#ifdef CONFIG_SCHED_AUTOGROUP
2563 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2564#endif
john stultz4614a696b2009-12-14 18:00:05 -08002565 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002566#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Djalal Harouni35a35042014-04-07 15:38:36 -07002567 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002568#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002569 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2570 ONE("stat", S_IRUGO, proc_tgid_stat),
2571 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002572 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002573#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002574 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002575#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002576 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2577 LNK("cwd", proc_cwd_link),
2578 LNK("root", proc_root_link),
2579 LNK("exe", proc_exe_link),
2580 REG("mounts", S_IRUGO, proc_mounts_operations),
2581 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2582 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002583#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002584 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002585 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002586 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002587#endif
2588#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002589 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002590#endif
2591#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002592 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002593#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002594#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002595 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002596#endif
2597#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002598 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002599#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002600#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002601 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002602#endif
Paul Menage8793d852007-10-18 23:39:39 -07002603#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002604 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605#endif
Paul Menagea4243162007-10-18 23:39:35 -07002606#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002607 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002608#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002609 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002610 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002611 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002612#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002613 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2614 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002615#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002616#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002617 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002618#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002619#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002620 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002621#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002622#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002623 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002624#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002625#ifdef CONFIG_HARDWALL
2626 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2627#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002628#ifdef CONFIG_USER_NS
2629 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2630 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002631 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002632#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002633#ifdef CONFIG_CHECKPOINT_RESTORE
2634 REG("timers", S_IRUGO, proc_timers_operations),
2635#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002636};
2637
Al Virof0c3b502013-05-16 12:07:31 -04002638static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002639{
Al Virof0c3b502013-05-16 12:07:31 -04002640 return proc_pident_readdir(file, ctx,
2641 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002642}
2643
Arjan van de Ven00977a52007-02-12 00:55:34 -08002644static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002645 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002646 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002647 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648};
2649
Al Viro00cd8dd2012-06-10 17:13:09 -04002650static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2651{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002652 return proc_pident_lookup(dir, dentry,
2653 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002654}
2655
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002656static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657 .lookup = proc_tgid_base_lookup,
2658 .getattr = pid_getattr,
2659 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002660 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002661};
2662
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002663static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002665 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002666 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002667 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Eric W. Biederman48e64842006-06-26 00:25:48 -07002669 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002670 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002671 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002672 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002673 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002674 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002675 d_drop(dentry);
2676 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Eric W. Biederman48e64842006-06-26 00:25:48 -07002679 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002680 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2681 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002682 if (!leader)
2683 goto out;
2684
2685 name.name = "task";
2686 name.len = strlen(name.name);
2687 dir = d_hash_and_lookup(leader, &name);
2688 if (!dir)
2689 goto out_put_leader;
2690
2691 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002692 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002693 dentry = d_hash_and_lookup(dir, &name);
2694 if (dentry) {
2695 shrink_dcache_parent(dentry);
2696 d_drop(dentry);
2697 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002699
2700 dput(dir);
2701out_put_leader:
2702 dput(leader);
2703out:
2704 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Randy Dunlap0895e912007-10-21 21:00:10 -07002707/**
2708 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2709 * @task: task that should be flushed.
2710 *
2711 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002712 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002713 * in. This call is supposed to do all of this job.
2714 *
2715 * Looks in the dcache for
2716 * /proc/@pid
2717 * /proc/@tgid/task/@pid
2718 * if either directory is present flushes it and all of it'ts children
2719 * from the dcache.
2720 *
2721 * It is safe and reasonable to cache /proc entries for a task until
2722 * that task exits. After that they just clog up the dcache with
2723 * useless entries, possibly causing useful dcache entries to be
2724 * flushed instead. This routine is proved to flush those useless
2725 * dcache entries at process exit time.
2726 *
2727 * NOTE: This routine is just an optimization so it does not guarantee
2728 * that no dcache entries will exist at process exit time it
2729 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002730 */
2731
2732void proc_flush_task(struct task_struct *task)
2733{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002734 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002735 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002736 struct upid *upid;
2737
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002738 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002739 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002740
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002741 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002742 upid = &pid->numbers[i];
2743 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002744 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002745 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002746}
2747
Al Viroc52a47a2013-06-15 11:15:20 +04002748static int proc_pid_instantiate(struct inode *dir,
2749 struct dentry * dentry,
2750 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002751{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002752 struct inode *inode;
2753
Eric W. Biederman61a28782006-10-02 02:18:49 -07002754 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002755 if (!inode)
2756 goto out;
2757
2758 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2759 inode->i_op = &proc_tgid_base_inode_operations;
2760 inode->i_fop = &proc_tgid_base_operations;
2761 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002762
Miklos Szeredibfe86842011-10-28 14:13:29 +02002763 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2764 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002765
Nick Pigginfb045ad2011-01-07 17:49:55 +11002766 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002767
2768 d_add(dentry, inode);
2769 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002770 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002771 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002772out:
Al Viroc52a47a2013-06-15 11:15:20 +04002773 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002774}
2775
Al Viro00cd8dd2012-06-10 17:13:09 -04002776struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002778 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002781 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002783 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (tgid == ~0U)
2785 goto out;
2786
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002787 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002788 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002789 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (task)
2791 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002792 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (!task)
2794 goto out;
2795
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002796 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002797 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798out:
Al Viroc52a47a2013-06-15 11:15:20 +04002799 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002803 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002804 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002806struct tgid_iter {
2807 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002808 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002809};
2810static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2811{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002812 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002814 if (iter.task)
2815 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002816 rcu_read_lock();
2817retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002818 iter.task = NULL;
2819 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002820 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002821 iter.tgid = pid_nr_ns(pid, ns);
2822 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002823 /* What we to know is if the pid we have find is the
2824 * pid of a thread_group_leader. Testing for task
2825 * being a thread_group_leader is the obvious thing
2826 * todo but there is a window when it fails, due to
2827 * the pid transfer logic in de_thread.
2828 *
2829 * So we perform the straight forward test of seeing
2830 * if the pid we have found is the pid of a thread
2831 * group leader, and don't worry if the task we have
2832 * found doesn't happen to be a thread group leader.
2833 * As we don't care in the case of readdir.
2834 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002835 if (!iter.task || !has_group_leader_pid(iter.task)) {
2836 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002837 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002838 }
2839 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002841 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002842 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
Al Viro021ada72013-03-29 19:27:05 -04002845#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002848int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002850 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002851 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002852 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Al Viro021ada72013-03-29 19:27:05 -04002854 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Al Viro021ada72013-03-29 19:27:05 -04002857 if (pos == TGID_OFFSET - 1) {
Al Virodb963162013-06-15 10:45:10 +04002858 struct inode *inode = ns->proc_self->d_inode;
2859 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002860 return 0;
Al Viro021ada72013-03-29 19:27:05 -04002861 iter.tgid = 0;
2862 } else {
2863 iter.tgid = pos - TGID_OFFSET;
2864 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002865 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002866 for (iter = next_tgid(ns, iter);
2867 iter.task;
2868 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002869 char name[PROC_NUMBUF];
2870 int len;
2871 if (!has_pid_permissions(ns, iter.task, 2))
2872 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002873
Al Virof0c3b502013-05-16 12:07:31 -04002874 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2875 ctx->pos = iter.tgid + TGID_OFFSET;
2876 if (!proc_fill_cache(file, ctx, name, len,
2877 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002878 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
Al Virof0c3b502013-05-16 12:07:31 -04002882 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return 0;
2884}
2885
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002886/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002887 * Tasks
2888 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002889static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002890 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002891 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002892 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002893 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002894 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002895 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002896 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002897 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002898#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002899 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002900#endif
john stultz4614a696b2009-12-14 18:00:05 -08002901 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002902#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Djalal Harouni35a35042014-04-07 15:38:36 -07002903 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002904#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002905 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2906 ONE("stat", S_IRUGO, proc_tid_stat),
2907 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002908 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002909#ifdef CONFIG_CHECKPOINT_RESTORE
2910 REG("children", S_IRUGO, proc_tid_children_operations),
2911#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002912#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002913 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002914#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002915 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2916 LNK("cwd", proc_cwd_link),
2917 LNK("root", proc_root_link),
2918 LNK("exe", proc_exe_link),
2919 REG("mounts", S_IRUGO, proc_mounts_operations),
2920 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002921#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002922 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002923 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002924 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002925#endif
2926#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002927 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002928#endif
2929#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002930 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002931#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002932#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002933 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002934#endif
2935#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002936 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002937#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002938#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002939 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002940#endif
Paul Menage8793d852007-10-18 23:39:39 -07002941#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002942 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002943#endif
Paul Menagea4243162007-10-18 23:39:35 -07002944#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002945 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002946#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002948 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002949 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002950#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002951 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002952 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002953#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002954#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002955 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002956#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002957#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002958 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002959#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002960#ifdef CONFIG_HARDWALL
2961 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2962#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002963#ifdef CONFIG_USER_NS
2964 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2965 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002966 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002967#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002968};
2969
Al Virof0c3b502013-05-16 12:07:31 -04002970static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002971{
Al Virof0c3b502013-05-16 12:07:31 -04002972 return proc_pident_readdir(file, ctx,
2973 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002974}
2975
Al Viro00cd8dd2012-06-10 17:13:09 -04002976static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2977{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002978 return proc_pident_lookup(dir, dentry,
2979 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002980}
2981
Arjan van de Ven00977a52007-02-12 00:55:34 -08002982static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002983 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002984 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002985 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002986};
2987
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002988static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002989 .lookup = proc_tid_base_lookup,
2990 .getattr = pid_getattr,
2991 .setattr = proc_setattr,
2992};
2993
Al Viroc52a47a2013-06-15 11:15:20 +04002994static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002995 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002996{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002997 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002998 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002999
3000 if (!inode)
3001 goto out;
3002 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3003 inode->i_op = &proc_tid_base_inode_operations;
3004 inode->i_fop = &proc_tid_base_operations;
3005 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003006
Miklos Szeredibfe86842011-10-28 14:13:29 +02003007 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3008 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003009
Nick Pigginfb045ad2011-01-07 17:49:55 +11003010 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003011
3012 d_add(dentry, inode);
3013 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003014 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003015 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003016out:
Al Viroc52a47a2013-06-15 11:15:20 +04003017 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003018}
3019
Al Viro00cd8dd2012-06-10 17:13:09 -04003020static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003021{
Al Viroc52a47a2013-06-15 11:15:20 +04003022 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003023 struct task_struct *task;
3024 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003025 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003026 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027
3028 if (!leader)
3029 goto out_no_task;
3030
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003031 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003032 if (tid == ~0U)
3033 goto out;
3034
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003035 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003037 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003038 if (task)
3039 get_task_struct(task);
3040 rcu_read_unlock();
3041 if (!task)
3042 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003043 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003044 goto out_drop_task;
3045
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003046 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003047out_drop_task:
3048 put_task_struct(task);
3049out:
3050 put_task_struct(leader);
3051out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003052 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003053}
3054
3055/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003056 * Find the first tid of a thread group to return to user space.
3057 *
3058 * Usually this is just the thread group leader, but if the users
3059 * buffer was too small or there was a seek into the middle of the
3060 * directory we have more work todo.
3061 *
3062 * In the case of a short read we start with find_task_by_pid.
3063 *
3064 * In the case of a seek we start with the leader and walk nr
3065 * threads past it.
3066 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003067static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3068 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003069{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003070 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003071 unsigned long nr = f_pos;
3072
3073 if (nr != f_pos) /* 32bit overflow? */
3074 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003075
Eric W. Biedermancc288732006-06-26 00:26:01 -07003076 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003077 task = pid_task(pid, PIDTYPE_PID);
3078 if (!task)
3079 goto fail;
3080
3081 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003082 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003083 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003084 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003085 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003086 }
3087
3088 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003089 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003090 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003091
3092 /* If we haven't found our starting place yet start
3093 * with the leader and walk nr threads forward.
3094 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003095 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003096 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003097 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003098 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003099 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003100fail:
3101 pos = NULL;
3102 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003103found:
3104 get_task_struct(pos);
3105out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003106 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003107 return pos;
3108}
3109
3110/*
3111 * Find the next thread in the thread list.
3112 * Return NULL if there is an error or no next thread.
3113 *
3114 * The reference to the input task_struct is released.
3115 */
3116static struct task_struct *next_tid(struct task_struct *start)
3117{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003118 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003119 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003120 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003121 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003122 if (thread_group_leader(pos))
3123 pos = NULL;
3124 else
3125 get_task_struct(pos);
3126 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003127 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003128 put_task_struct(start);
3129 return pos;
3130}
3131
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003133static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003135 struct inode *inode = file_inode(file);
3136 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003137 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003138 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003140 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003141 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Al Virof0c3b502013-05-16 12:07:31 -04003143 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003146 /* f_version caches the tgid value that the last readdir call couldn't
3147 * return. lseek aka telldir automagically resets f_version to 0.
3148 */
Al Virof0c3b502013-05-16 12:07:31 -04003149 ns = file->f_dentry->d_sb->s_fs_info;
3150 tid = (int)file->f_version;
3151 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003152 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003153 task;
Al Virof0c3b502013-05-16 12:07:31 -04003154 task = next_tid(task), ctx->pos++) {
3155 char name[PROC_NUMBUF];
3156 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003157 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003158 len = snprintf(name, sizeof(name), "%d", tid);
3159 if (!proc_fill_cache(file, ctx, name, len,
3160 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003161 /* returning this tgid failed, save it as the first
3162 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003163 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003164 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003168
Al Virof0c3b502013-05-16 12:07:31 -04003169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003171
3172static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3173{
3174 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003175 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003176 generic_fillattr(inode, stat);
3177
Eric W. Biederman99f89552006-06-26 00:25:55 -07003178 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003179 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003180 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003181 }
3182
3183 return 0;
3184}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003185
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003186static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003187 .lookup = proc_task_lookup,
3188 .getattr = proc_task_getattr,
3189 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003190 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003191};
3192
Arjan van de Ven00977a52007-02-12 00:55:34 -08003193static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003194 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003195 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003196 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003197};