blob: 4bb8d34819707946536fd74ce5e235c3f56e7d93 [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
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700203static int proc_pid_cmdline(struct seq_file *m, struct pid_namespace *ns,
204 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -0700206 /*
207 * Rely on struct seq_operations::show() being called once
208 * per internal buffer allocation. See single_open(), traverse().
209 */
210 BUG_ON(m->size < PAGE_SIZE);
211 m->count += get_cmdline(task, m->buf, PAGE_SIZE);
212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213}
214
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700215static int proc_pid_auxv(struct seq_file *m, struct pid_namespace *ns,
216 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Cong Wange7dcd992012-05-31 16:26:17 -0700218 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500219 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300221 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300223 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700224 seq_write(m, mm->saved_auxv, nwords * sizeof(mm->saved_auxv[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 mmput(mm);
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -0700226 return 0;
227 } else
228 return PTR_ERR(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231
232#ifdef CONFIG_KALLSYMS
233/*
234 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
235 * Returns the resolved symbol. If that fails, simply return the address.
236 */
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700237static int proc_pid_wchan(struct seq_file *m, struct pid_namespace *ns,
238 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700240 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700241 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 wchan = get_wchan(task);
244
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700245 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600246 if (!ptrace_may_access(task, PTRACE_MODE_READ))
247 return 0;
248 else
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700249 return seq_printf(m, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700250 else
Alexey Dobriyanedfcd602014-08-08 14:21:44 -0700251 return seq_printf(m, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253#endif /* CONFIG_KALLSYMS */
254
Al Viroa9712bc2011-03-23 15:52:50 -0400255static int lock_trace(struct task_struct *task)
256{
257 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
258 if (err)
259 return err;
260 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
261 mutex_unlock(&task->signal->cred_guard_mutex);
262 return -EPERM;
263 }
264 return 0;
265}
266
267static void unlock_trace(struct task_struct *task)
268{
269 mutex_unlock(&task->signal->cred_guard_mutex);
270}
271
Ken Chen2ec220e2008-11-10 11:26:08 +0300272#ifdef CONFIG_STACKTRACE
273
274#define MAX_STACK_TRACE_DEPTH 64
275
276static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
277 struct pid *pid, struct task_struct *task)
278{
279 struct stack_trace trace;
280 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400281 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300282 int i;
283
284 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
285 if (!entries)
286 return -ENOMEM;
287
288 trace.nr_entries = 0;
289 trace.max_entries = MAX_STACK_TRACE_DEPTH;
290 trace.entries = entries;
291 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300292
Al Viroa9712bc2011-03-23 15:52:50 -0400293 err = lock_trace(task);
294 if (!err) {
295 save_stack_trace_tsk(task, &trace);
296
297 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700298 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400299 (void *)entries[i], (void *)entries[i]);
300 }
301 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300302 }
303 kfree(entries);
304
Al Viroa9712bc2011-03-23 15:52:50 -0400305 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300306}
307#endif
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#ifdef CONFIG_SCHEDSTATS
310/*
311 * Provides /proc/PID/schedstat
312 */
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700313static int proc_pid_schedstat(struct seq_file *m, struct pid_namespace *ns,
314 struct pid *pid, struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -0700316 return seq_printf(m, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100317 (unsigned long long)task->se.sum_exec_runtime,
318 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200319 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321#endif
322
Arjan van de Ven97455122008-01-25 21:08:34 +0100323#ifdef CONFIG_LATENCYTOP
324static int lstats_show_proc(struct seq_file *m, void *v)
325{
326 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800327 struct inode *inode = m->private;
328 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100329
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800330 if (!task)
331 return -ESRCH;
332 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100333 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800334 struct latency_record *lr = &task->latency_record[i];
335 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100336 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800337 seq_printf(m, "%i %li %li",
338 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100339 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800340 unsigned long bt = lr->backtrace[q];
341 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100342 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800343 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100344 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800345 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100346 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800347 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100348 }
349
350 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800351 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100352 return 0;
353}
354
355static int lstats_open(struct inode *inode, struct file *file)
356{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800357 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800358}
359
Arjan van de Ven97455122008-01-25 21:08:34 +0100360static ssize_t lstats_write(struct file *file, const char __user *buf,
361 size_t count, loff_t *offs)
362{
Al Viro496ad9a2013-01-23 17:07:38 -0500363 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100364
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800365 if (!task)
366 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100367 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800368 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100369
370 return count;
371}
372
373static const struct file_operations proc_lstats_operations = {
374 .open = lstats_open,
375 .read = seq_read,
376 .write = lstats_write,
377 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800378 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100379};
380
381#endif
382
Al Viro8d8b97b2013-04-19 23:11:24 -0400383#ifdef CONFIG_CGROUPS
384static int cgroup_open(struct inode *inode, struct file *file)
385{
386 struct pid *pid = PROC_I(inode)->pid;
387 return single_open(file, proc_cgroup_show, pid);
388}
389
390static const struct file_operations proc_cgroup_operations = {
391 .open = cgroup_open,
392 .read = seq_read,
393 .llseek = seq_lseek,
394 .release = single_release,
395};
396#endif
397
398#ifdef CONFIG_PROC_PID_CPUSET
399
400static int cpuset_open(struct inode *inode, struct file *file)
401{
402 struct pid *pid = PROC_I(inode)->pid;
403 return single_open(file, proc_cpuset_show, pid);
404}
405
406static const struct file_operations proc_cpuset_operations = {
407 .open = cpuset_open,
408 .read = seq_read,
409 .llseek = seq_lseek,
410 .release = single_release,
411};
412#endif
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414static int proc_oom_score(struct task_struct *task, char *buffer)
415{
David Rientjesa7f638f2012-05-29 15:06:47 -0700416 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200417 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700419 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200420 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700421 points = oom_badness(task, NULL, NULL, totalpages) *
422 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700423 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return sprintf(buffer, "%lu\n", points);
425}
426
Neil Hormand85f50d2007-10-18 23:40:37 -0700427struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700428 const char *name;
429 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700430};
431
432static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700433 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700434 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
435 [RLIMIT_DATA] = {"Max data size", "bytes"},
436 [RLIMIT_STACK] = {"Max stack size", "bytes"},
437 [RLIMIT_CORE] = {"Max core file size", "bytes"},
438 [RLIMIT_RSS] = {"Max resident set", "bytes"},
439 [RLIMIT_NPROC] = {"Max processes", "processes"},
440 [RLIMIT_NOFILE] = {"Max open files", "files"},
441 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
442 [RLIMIT_AS] = {"Max address space", "bytes"},
443 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
444 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
445 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
446 [RLIMIT_NICE] = {"Max nice priority", NULL},
447 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800448 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700449};
450
451/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700452static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
453 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700454{
455 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700456 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700457
458 struct rlimit rlim[RLIM_NLIMITS];
459
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400460 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700461 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700462 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
463 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700464
465 /*
466 * print the file header
467 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700468 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700469 "Limit", "Soft Limit", "Hard Limit", "Units");
470
471 for (i = 0; i < RLIM_NLIMITS; i++) {
472 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700473 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700474 lnames[i].name, "unlimited");
475 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700476 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700477 lnames[i].name, rlim[i].rlim_cur);
478
479 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700480 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700481 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700482 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700483
484 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700485 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700486 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700487 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700488 }
489
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700490 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700491}
492
Roland McGrathebcb6732008-07-25 19:46:00 -0700493#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700494static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
495 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700496{
497 long nr;
498 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400499 int res = lock_trace(task);
500 if (res)
501 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700502
503 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700504 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400505 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700506 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400507 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700508 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700509 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
510 nr,
511 args[0], args[1], args[2], args[3], args[4], args[5],
512 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400513 unlock_trace(task);
514 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700515}
516#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/************************************************************************/
519/* Here the fs part begins */
520/************************************************************************/
521
522/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700523static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700525 struct task_struct *task;
526 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700527 /* Allow access to a task's file descriptors if it is us or we
528 * may use ptrace attach to the process and find out that
529 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700530 */
531 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700532 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400533 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700534 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700535 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700536 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800539int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700540{
541 int error;
542 struct inode *inode = dentry->d_inode;
543
544 if (attr->ia_valid & ATTR_MODE)
545 return -EPERM;
546
547 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200548 if (error)
549 return error;
550
Christoph Hellwig10257742010-06-04 11:30:02 +0200551 setattr_copy(inode, attr);
552 mark_inode_dirty(inode);
553 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700554}
555
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800556/*
557 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
558 * or euid/egid (for hide_pid_min=2)?
559 */
560static bool has_pid_permissions(struct pid_namespace *pid,
561 struct task_struct *task,
562 int hide_pid_min)
563{
564 if (pid->hide_pid < hide_pid_min)
565 return true;
566 if (in_group_p(pid->pid_gid))
567 return true;
568 return ptrace_may_access(task, PTRACE_MODE_READ);
569}
570
571
572static int proc_pid_permission(struct inode *inode, int mask)
573{
574 struct pid_namespace *pid = inode->i_sb->s_fs_info;
575 struct task_struct *task;
576 bool has_perms;
577
578 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800579 if (!task)
580 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800581 has_perms = has_pid_permissions(pid, task, 1);
582 put_task_struct(task);
583
584 if (!has_perms) {
585 if (pid->hide_pid == 2) {
586 /*
587 * Let's make getdents(), stat(), and open()
588 * consistent with each other. If a process
589 * may not stat() a file, it shouldn't be seen
590 * in procfs at all.
591 */
592 return -ENOENT;
593 }
594
595 return -EPERM;
596 }
597 return generic_permission(inode, mask);
598}
599
600
601
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800602static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700603 .setattr = proc_setattr,
604};
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
607
608static ssize_t proc_info_read(struct file * file, char __user * buf,
609 size_t count, loff_t *ppos)
610{
Al Viro496ad9a2013-01-23 17:07:38 -0500611 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned long page;
613 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700614 struct task_struct *task = get_proc_task(inode);
615
616 length = -ESRCH;
617 if (!task)
618 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (count > PROC_BLOCK_SIZE)
621 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700622
623 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700624 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 length = PROC_I(inode)->op.proc_read(task, (char*)page);
628
629 if (length >= 0)
630 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
631 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700632out:
633 put_task_struct(task);
634out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return length;
636}
637
Arjan van de Ven00977a52007-02-12 00:55:34 -0800638static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100640 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641};
642
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800643static int proc_single_show(struct seq_file *m, void *v)
644{
645 struct inode *inode = m->private;
646 struct pid_namespace *ns;
647 struct pid *pid;
648 struct task_struct *task;
649 int ret;
650
651 ns = inode->i_sb->s_fs_info;
652 pid = proc_pid(inode);
653 task = get_pid_task(pid, PIDTYPE_PID);
654 if (!task)
655 return -ESRCH;
656
657 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
658
659 put_task_struct(task);
660 return ret;
661}
662
663static int proc_single_open(struct inode *inode, struct file *filp)
664{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800665 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800666}
667
668static const struct file_operations proc_single_file_operations = {
669 .open = proc_single_open,
670 .read = seq_read,
671 .llseek = seq_lseek,
672 .release = single_release,
673};
674
Cong Wangb409e572012-05-31 16:26:17 -0700675static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Al Viro496ad9a2013-01-23 17:07:38 -0500677 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800678 struct mm_struct *mm;
679
680 if (!task)
681 return -ESRCH;
682
Cong Wangb409e572012-05-31 16:26:17 -0700683 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800684 put_task_struct(task);
685
686 if (IS_ERR(mm))
687 return PTR_ERR(mm);
688
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100689 if (mm) {
690 /* ensure this mm_struct can't be freed */
691 atomic_inc(&mm->mm_count);
692 /* but do not pin its memory */
693 mmput(mm);
694 }
695
Linus Torvaldse2683372012-01-17 15:21:19 -0800696 file->private_data = mm;
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
699}
700
Cong Wangb409e572012-05-31 16:26:17 -0700701static int mem_open(struct inode *inode, struct file *file)
702{
Djalal Harounibc452b42012-07-30 14:42:28 -0700703 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
704
705 /* OK to pass negative loff_t, we can catch out-of-range */
706 file->f_mode |= FMODE_UNSIGNED_OFFSET;
707
708 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700709}
710
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100711static ssize_t mem_rw(struct file *file, char __user *buf,
712 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Linus Torvaldse2683372012-01-17 15:21:19 -0800714 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100715 unsigned long addr = *ppos;
716 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Linus Torvaldse2683372012-01-17 15:21:19 -0800719 if (!mm)
720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Mel Gormane12ba742007-10-16 01:25:52 -0700722 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800724 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700726 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100727 if (!atomic_inc_not_zero(&mm->mm_users))
728 goto free;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100731 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100733 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 copied = -EFAULT;
735 break;
736 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100737
738 this_len = access_remote_vm(mm, addr, page, this_len, write);
739 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!copied)
741 copied = -EIO;
742 break;
743 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100744
745 if (!write && copy_to_user(buf, page, this_len)) {
746 copied = -EFAULT;
747 break;
748 }
749
750 buf += this_len;
751 addr += this_len;
752 copied += this_len;
753 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100755 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700756
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100757 mmput(mm);
758free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700759 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return copied;
761}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100763static ssize_t mem_read(struct file *file, char __user *buf,
764 size_t count, loff_t *ppos)
765{
766 return mem_rw(file, buf, count, ppos, 0);
767}
768
769static ssize_t mem_write(struct file *file, const char __user *buf,
770 size_t count, loff_t *ppos)
771{
772 return mem_rw(file, (char __user*)buf, count, ppos, 1);
773}
774
Matt Mackall85863e42008-02-04 22:29:04 -0800775loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 switch (orig) {
778 case 0:
779 file->f_pos = offset;
780 break;
781 case 1:
782 file->f_pos += offset;
783 break;
784 default:
785 return -EINVAL;
786 }
787 force_successful_syscall_return();
788 return file->f_pos;
789}
790
Linus Torvaldse2683372012-01-17 15:21:19 -0800791static int mem_release(struct inode *inode, struct file *file)
792{
793 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100794 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100795 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800796 return 0;
797}
798
Arjan van de Ven00977a52007-02-12 00:55:34 -0800799static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 .llseek = mem_lseek,
801 .read = mem_read,
802 .write = mem_write,
803 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800804 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805};
806
Cong Wangb409e572012-05-31 16:26:17 -0700807static int environ_open(struct inode *inode, struct file *file)
808{
809 return __mem_open(inode, file, PTRACE_MODE_READ);
810}
811
James Pearson315e28c2007-10-16 23:30:17 -0700812static ssize_t environ_read(struct file *file, char __user *buf,
813 size_t count, loff_t *ppos)
814{
James Pearson315e28c2007-10-16 23:30:17 -0700815 char *page;
816 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700817 int ret = 0;
818 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700819
Cong Wangb409e572012-05-31 16:26:17 -0700820 if (!mm)
821 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700822
James Pearson315e28c2007-10-16 23:30:17 -0700823 page = (char *)__get_free_page(GFP_TEMPORARY);
824 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700825 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700826
Al Virod6f64b82011-02-15 22:26:01 -0500827 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700828 if (!atomic_inc_not_zero(&mm->mm_users))
829 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700830 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700831 size_t this_len, max_len;
832 int retval;
833
834 if (src >= (mm->env_end - mm->env_start))
835 break;
James Pearson315e28c2007-10-16 23:30:17 -0700836
837 this_len = mm->env_end - (mm->env_start + src);
838
Djalal Harounie8905ec2012-07-30 14:42:26 -0700839 max_len = min_t(size_t, PAGE_SIZE, count);
840 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700841
Cong Wangb409e572012-05-31 16:26:17 -0700842 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700843 page, this_len, 0);
844
845 if (retval <= 0) {
846 ret = retval;
847 break;
848 }
849
850 if (copy_to_user(buf, page, retval)) {
851 ret = -EFAULT;
852 break;
853 }
854
855 ret += retval;
856 src += retval;
857 buf += retval;
858 count -= retval;
859 }
860 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700861 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700862
863free:
James Pearson315e28c2007-10-16 23:30:17 -0700864 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700865 return ret;
866}
867
868static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700869 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700870 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100871 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700872 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700873};
874
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800875static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
876 loff_t *ppos)
877{
Al Viro496ad9a2013-01-23 17:07:38 -0500878 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800879 char buffer[PROC_NUMBUF];
880 int oom_adj = OOM_ADJUST_MIN;
881 size_t len;
882 unsigned long flags;
883
884 if (!task)
885 return -ESRCH;
886 if (lock_task_sighand(task, &flags)) {
887 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
888 oom_adj = OOM_ADJUST_MAX;
889 else
890 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
891 OOM_SCORE_ADJ_MAX;
892 unlock_task_sighand(task, &flags);
893 }
894 put_task_struct(task);
895 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
896 return simple_read_from_buffer(buf, count, ppos, buffer, len);
897}
898
899static ssize_t oom_adj_write(struct file *file, const char __user *buf,
900 size_t count, loff_t *ppos)
901{
902 struct task_struct *task;
903 char buffer[PROC_NUMBUF];
904 int oom_adj;
905 unsigned long flags;
906 int err;
907
908 memset(buffer, 0, sizeof(buffer));
909 if (count > sizeof(buffer) - 1)
910 count = sizeof(buffer) - 1;
911 if (copy_from_user(buffer, buf, count)) {
912 err = -EFAULT;
913 goto out;
914 }
915
916 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
917 if (err)
918 goto out;
919 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
920 oom_adj != OOM_DISABLE) {
921 err = -EINVAL;
922 goto out;
923 }
924
Al Viro496ad9a2013-01-23 17:07:38 -0500925 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800926 if (!task) {
927 err = -ESRCH;
928 goto out;
929 }
930
931 task_lock(task);
932 if (!task->mm) {
933 err = -EINVAL;
934 goto err_task_lock;
935 }
936
937 if (!lock_task_sighand(task, &flags)) {
938 err = -ESRCH;
939 goto err_task_lock;
940 }
941
942 /*
943 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
944 * value is always attainable.
945 */
946 if (oom_adj == OOM_ADJUST_MAX)
947 oom_adj = OOM_SCORE_ADJ_MAX;
948 else
949 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
950
951 if (oom_adj < task->signal->oom_score_adj &&
952 !capable(CAP_SYS_RESOURCE)) {
953 err = -EACCES;
954 goto err_sighand;
955 }
956
957 /*
958 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
959 * /proc/pid/oom_score_adj instead.
960 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800961 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 -0800962 current->comm, task_pid_nr(current), task_pid_nr(task),
963 task_pid_nr(task));
964
965 task->signal->oom_score_adj = oom_adj;
966 trace_oom_score_adj_update(task);
967err_sighand:
968 unlock_task_sighand(task, &flags);
969err_task_lock:
970 task_unlock(task);
971 put_task_struct(task);
972out:
973 return err < 0 ? err : count;
974}
975
976static const struct file_operations proc_oom_adj_operations = {
977 .read = oom_adj_read,
978 .write = oom_adj_write,
979 .llseek = generic_file_llseek,
980};
981
David Rientjesa63d83f2010-08-09 17:19:46 -0700982static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
983 size_t count, loff_t *ppos)
984{
Al Viro496ad9a2013-01-23 17:07:38 -0500985 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700986 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800987 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700988 unsigned long flags;
989 size_t len;
990
991 if (!task)
992 return -ESRCH;
993 if (lock_task_sighand(task, &flags)) {
994 oom_score_adj = task->signal->oom_score_adj;
995 unlock_task_sighand(task, &flags);
996 }
997 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800998 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700999 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1000}
1001
1002static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1003 size_t count, loff_t *ppos)
1004{
1005 struct task_struct *task;
1006 char buffer[PROC_NUMBUF];
1007 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001008 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001009 int err;
1010
1011 memset(buffer, 0, sizeof(buffer));
1012 if (count > sizeof(buffer) - 1)
1013 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001014 if (copy_from_user(buffer, buf, count)) {
1015 err = -EFAULT;
1016 goto out;
1017 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001018
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001019 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001020 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001021 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001022 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001023 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1024 err = -EINVAL;
1025 goto out;
1026 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001027
Al Viro496ad9a2013-01-23 17:07:38 -05001028 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001029 if (!task) {
1030 err = -ESRCH;
1031 goto out;
1032 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001033
Ying Han3d5992d2010-10-26 14:21:23 -07001034 task_lock(task);
1035 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001036 err = -EINVAL;
1037 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001038 }
David Rientjesd19d5472010-10-26 14:21:26 -07001039
1040 if (!lock_task_sighand(task, &flags)) {
1041 err = -ESRCH;
1042 goto err_task_lock;
1043 }
1044
David Rientjesa9c58b902012-12-11 16:02:54 -08001045 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001046 !capable(CAP_SYS_RESOURCE)) {
1047 err = -EACCES;
1048 goto err_sighand;
1049 }
1050
David Rientjesa9c58b902012-12-11 16:02:54 -08001051 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f632011-01-13 15:46:05 -08001052 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001053 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001054 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001055
David Rientjes723548b2010-10-26 14:21:25 -07001056err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001057 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001058err_task_lock:
1059 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001060 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001061out:
1062 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001063}
1064
1065static const struct file_operations proc_oom_score_adj_operations = {
1066 .read = oom_score_adj_read,
1067 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001068 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001069};
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071#ifdef CONFIG_AUDITSYSCALL
1072#define TMPBUFLEN 21
1073static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1074 size_t count, loff_t *ppos)
1075{
Al Viro496ad9a2013-01-23 17:07:38 -05001076 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001077 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 ssize_t length;
1079 char tmpbuf[TMPBUFLEN];
1080
Eric W. Biederman99f89552006-06-26 00:25:55 -07001081 if (!task)
1082 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001084 from_kuid(file->f_cred->user_ns,
1085 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001086 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1088}
1089
1090static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1091 size_t count, loff_t *ppos)
1092{
Al Viro496ad9a2013-01-23 17:07:38 -05001093 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 char *page, *tmp;
1095 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001097 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001099 rcu_read_lock();
1100 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1101 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001103 }
1104 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Al Viroe0182902006-05-18 08:28:02 -04001106 if (count >= PAGE_SIZE)
1107 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (*ppos != 0) {
1110 /* No partial writes. */
1111 return -EINVAL;
1112 }
Mel Gormane12ba742007-10-16 01:25:52 -07001113 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (!page)
1115 return -ENOMEM;
1116 length = -EFAULT;
1117 if (copy_from_user(page, buf, count))
1118 goto out_free_page;
1119
Al Viroe0182902006-05-18 08:28:02 -04001120 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 loginuid = simple_strtoul(page, &tmp, 10);
1122 if (tmp == page) {
1123 length = -EINVAL;
1124 goto out_free_page;
1125
1126 }
Eric Paris81407c82013-05-24 09:49:14 -04001127
1128 /* is userspace tring to explicitly UNSET the loginuid? */
1129 if (loginuid == AUDIT_UID_UNSET) {
1130 kloginuid = INVALID_UID;
1131 } else {
1132 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1133 if (!uid_valid(kloginuid)) {
1134 length = -EINVAL;
1135 goto out_free_page;
1136 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001137 }
1138
1139 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (likely(length == 0))
1141 length = count;
1142
1143out_free_page:
1144 free_page((unsigned long) page);
1145 return length;
1146}
1147
Arjan van de Ven00977a52007-02-12 00:55:34 -08001148static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 .read = proc_loginuid_read,
1150 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001151 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152};
Eric Paris1e0bd752008-03-13 08:15:31 -04001153
1154static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1155 size_t count, loff_t *ppos)
1156{
Al Viro496ad9a2013-01-23 17:07:38 -05001157 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001158 struct task_struct *task = get_proc_task(inode);
1159 ssize_t length;
1160 char tmpbuf[TMPBUFLEN];
1161
1162 if (!task)
1163 return -ESRCH;
1164 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1165 audit_get_sessionid(task));
1166 put_task_struct(task);
1167 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1168}
1169
1170static const struct file_operations proc_sessionid_operations = {
1171 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001172 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001173};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174#endif
1175
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001176#ifdef CONFIG_FAULT_INJECTION
1177static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1178 size_t count, loff_t *ppos)
1179{
Al Viro496ad9a2013-01-23 17:07:38 -05001180 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001181 char buffer[PROC_NUMBUF];
1182 size_t len;
1183 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001184
1185 if (!task)
1186 return -ESRCH;
1187 make_it_fail = task->make_it_fail;
1188 put_task_struct(task);
1189
1190 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001191
1192 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001193}
1194
1195static ssize_t proc_fault_inject_write(struct file * file,
1196 const char __user * buf, size_t count, loff_t *ppos)
1197{
1198 struct task_struct *task;
1199 char buffer[PROC_NUMBUF], *end;
1200 int make_it_fail;
1201
1202 if (!capable(CAP_SYS_RESOURCE))
1203 return -EPERM;
1204 memset(buffer, 0, sizeof(buffer));
1205 if (count > sizeof(buffer) - 1)
1206 count = sizeof(buffer) - 1;
1207 if (copy_from_user(buffer, buf, count))
1208 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001209 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1210 if (*end)
1211 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001212 if (make_it_fail < 0 || make_it_fail > 1)
1213 return -EINVAL;
1214
Al Viro496ad9a2013-01-23 17:07:38 -05001215 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001216 if (!task)
1217 return -ESRCH;
1218 task->make_it_fail = make_it_fail;
1219 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001220
1221 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001222}
1223
Arjan van de Ven00977a52007-02-12 00:55:34 -08001224static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001225 .read = proc_fault_inject_read,
1226 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001227 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001228};
1229#endif
1230
Arjan van de Ven97455122008-01-25 21:08:34 +01001231
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001232#ifdef CONFIG_SCHED_DEBUG
1233/*
1234 * Print out various scheduling related per-task fields:
1235 */
1236static int sched_show(struct seq_file *m, void *v)
1237{
1238 struct inode *inode = m->private;
1239 struct task_struct *p;
1240
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001241 p = get_proc_task(inode);
1242 if (!p)
1243 return -ESRCH;
1244 proc_sched_show_task(p, m);
1245
1246 put_task_struct(p);
1247
1248 return 0;
1249}
1250
1251static ssize_t
1252sched_write(struct file *file, const char __user *buf,
1253 size_t count, loff_t *offset)
1254{
Al Viro496ad9a2013-01-23 17:07:38 -05001255 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001256 struct task_struct *p;
1257
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001258 p = get_proc_task(inode);
1259 if (!p)
1260 return -ESRCH;
1261 proc_sched_set_task(p);
1262
1263 put_task_struct(p);
1264
1265 return count;
1266}
1267
1268static int sched_open(struct inode *inode, struct file *filp)
1269{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001270 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001271}
1272
1273static const struct file_operations proc_pid_sched_operations = {
1274 .open = sched_open,
1275 .read = seq_read,
1276 .write = sched_write,
1277 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001278 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001279};
1280
1281#endif
1282
Mike Galbraith5091faa2010-11-30 14:18:03 +01001283#ifdef CONFIG_SCHED_AUTOGROUP
1284/*
1285 * Print out autogroup related information:
1286 */
1287static int sched_autogroup_show(struct seq_file *m, void *v)
1288{
1289 struct inode *inode = m->private;
1290 struct task_struct *p;
1291
1292 p = get_proc_task(inode);
1293 if (!p)
1294 return -ESRCH;
1295 proc_sched_autogroup_show_task(p, m);
1296
1297 put_task_struct(p);
1298
1299 return 0;
1300}
1301
1302static ssize_t
1303sched_autogroup_write(struct file *file, const char __user *buf,
1304 size_t count, loff_t *offset)
1305{
Al Viro496ad9a2013-01-23 17:07:38 -05001306 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001307 struct task_struct *p;
1308 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001309 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001310 int err;
1311
1312 memset(buffer, 0, sizeof(buffer));
1313 if (count > sizeof(buffer) - 1)
1314 count = sizeof(buffer) - 1;
1315 if (copy_from_user(buffer, buf, count))
1316 return -EFAULT;
1317
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001318 err = kstrtoint(strstrip(buffer), 0, &nice);
1319 if (err < 0)
1320 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001321
1322 p = get_proc_task(inode);
1323 if (!p)
1324 return -ESRCH;
1325
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001326 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001327 if (err)
1328 count = err;
1329
1330 put_task_struct(p);
1331
1332 return count;
1333}
1334
1335static int sched_autogroup_open(struct inode *inode, struct file *filp)
1336{
1337 int ret;
1338
1339 ret = single_open(filp, sched_autogroup_show, NULL);
1340 if (!ret) {
1341 struct seq_file *m = filp->private_data;
1342
1343 m->private = inode;
1344 }
1345 return ret;
1346}
1347
1348static const struct file_operations proc_pid_sched_autogroup_operations = {
1349 .open = sched_autogroup_open,
1350 .read = seq_read,
1351 .write = sched_autogroup_write,
1352 .llseek = seq_lseek,
1353 .release = single_release,
1354};
1355
1356#endif /* CONFIG_SCHED_AUTOGROUP */
1357
john stultz4614a696b2009-12-14 18:00:05 -08001358static ssize_t comm_write(struct file *file, const char __user *buf,
1359 size_t count, loff_t *offset)
1360{
Al Viro496ad9a2013-01-23 17:07:38 -05001361 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001362 struct task_struct *p;
1363 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001364 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001365
1366 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001367 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001368 return -EFAULT;
1369
1370 p = get_proc_task(inode);
1371 if (!p)
1372 return -ESRCH;
1373
1374 if (same_thread_group(current, p))
1375 set_task_comm(p, buffer);
1376 else
1377 count = -EINVAL;
1378
1379 put_task_struct(p);
1380
1381 return count;
1382}
1383
1384static int comm_show(struct seq_file *m, void *v)
1385{
1386 struct inode *inode = m->private;
1387 struct task_struct *p;
1388
1389 p = get_proc_task(inode);
1390 if (!p)
1391 return -ESRCH;
1392
1393 task_lock(p);
1394 seq_printf(m, "%s\n", p->comm);
1395 task_unlock(p);
1396
1397 put_task_struct(p);
1398
1399 return 0;
1400}
1401
1402static int comm_open(struct inode *inode, struct file *filp)
1403{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001404 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001405}
1406
1407static const struct file_operations proc_pid_set_comm_operations = {
1408 .open = comm_open,
1409 .read = seq_read,
1410 .write = comm_write,
1411 .llseek = seq_lseek,
1412 .release = single_release,
1413};
1414
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001415static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001416{
1417 struct task_struct *task;
1418 struct mm_struct *mm;
1419 struct file *exe_file;
1420
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001421 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001422 if (!task)
1423 return -ENOENT;
1424 mm = get_task_mm(task);
1425 put_task_struct(task);
1426 if (!mm)
1427 return -ENOENT;
1428 exe_file = get_mm_exe_file(mm);
1429 mmput(mm);
1430 if (exe_file) {
1431 *exe_path = exe_file->f_path;
1432 path_get(&exe_file->f_path);
1433 fput(exe_file);
1434 return 0;
1435 } else
1436 return -ENOENT;
1437}
1438
Al Viro008b1502005-08-20 00:17:39 +01001439static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
1441 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001442 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 int error = -EACCES;
1444
Eric W. Biederman778c1142006-06-26 00:25:58 -07001445 /* Are we allowed to snoop on the tasks file descriptors? */
1446 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Christoph Hellwig408ef012012-06-18 10:47:03 -04001449 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1450 if (error)
1451 goto out;
1452
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001453 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001454 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455out:
Al Viro008b1502005-08-20 00:17:39 +01001456 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001459static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Mel Gormane12ba742007-10-16 01:25:52 -07001461 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001462 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 int len;
1464
1465 if (!tmp)
1466 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001467
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001468 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001469 len = PTR_ERR(pathname);
1470 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001472 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 if (len > buflen)
1475 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001476 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 len = -EFAULT;
1478 out:
1479 free_page((unsigned long)tmp);
1480 return len;
1481}
1482
1483static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1484{
1485 int error = -EACCES;
1486 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001487 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Eric W. Biederman778c1142006-06-26 00:25:58 -07001489 /* Are we allowed to snoop on the tasks file descriptors? */
1490 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001493 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (error)
1495 goto out;
1496
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001497 error = do_proc_readlink(&path, buffer, buflen);
1498 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return error;
1501}
1502
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001503const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001505 .follow_link = proc_pid_follow_link,
1506 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507};
1508
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001509
1510/* building an inode */
1511
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001512struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001513{
1514 struct inode * inode;
1515 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001516 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001517
1518 /* We need a new inode */
1519
1520 inode = new_inode(sb);
1521 if (!inode)
1522 goto out;
1523
1524 /* Common stuff */
1525 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001526 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001528 inode->i_op = &proc_def_inode_operations;
1529
1530 /*
1531 * grab the reference to task.
1532 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001533 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001534 if (!ei->pid)
1535 goto out_unlock;
1536
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001537 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001538 rcu_read_lock();
1539 cred = __task_cred(task);
1540 inode->i_uid = cred->euid;
1541 inode->i_gid = cred->egid;
1542 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001543 }
1544 security_task_to_inode(task, inode);
1545
1546out:
1547 return inode;
1548
1549out_unlock:
1550 iput(inode);
1551 return NULL;
1552}
1553
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001554int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555{
1556 struct inode *inode = dentry->d_inode;
1557 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001558 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001559 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001560
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001561 generic_fillattr(inode, stat);
1562
1563 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001564 stat->uid = GLOBAL_ROOT_UID;
1565 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001566 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1567 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001568 if (!has_pid_permissions(pid, task, 2)) {
1569 rcu_read_unlock();
1570 /*
1571 * This doesn't prevent learning whether PID exists,
1572 * it only makes getattr() consistent with readdir().
1573 */
1574 return -ENOENT;
1575 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001576 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1577 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001578 cred = __task_cred(task);
1579 stat->uid = cred->euid;
1580 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001581 }
1582 }
1583 rcu_read_unlock();
1584 return 0;
1585}
1586
1587/* dentry stuff */
1588
1589/*
1590 * Exceptional case: normally we are not allowed to unhash a busy
1591 * directory. In this case, however, we can do it - no aliasing problems
1592 * due to the way we treat inodes.
1593 *
1594 * Rewrite the inode's ownerships here because the owning task may have
1595 * performed a setuid(), etc.
1596 *
1597 * Before the /proc/pid/status file was created the only way to read
1598 * the effective uid of a /process was to stat /proc/pid. Reading
1599 * /proc/pid/status is slow enough that procps and other packages
1600 * kept stating /proc/pid. To keep the rules in /proc simple I have
1601 * made this apply to all per process world readable and executable
1602 * directories.
1603 */
Al Viro0b728e12012-06-10 16:03:43 -04001604int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001605{
Nick Piggin34286d62011-01-07 17:49:57 +11001606 struct inode *inode;
1607 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001608 const struct cred *cred;
1609
Al Viro0b728e12012-06-10 16:03:43 -04001610 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001611 return -ECHILD;
1612
1613 inode = dentry->d_inode;
1614 task = get_proc_task(inode);
1615
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001616 if (task) {
1617 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1618 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001619 rcu_read_lock();
1620 cred = __task_cred(task);
1621 inode->i_uid = cred->euid;
1622 inode->i_gid = cred->egid;
1623 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001624 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001625 inode->i_uid = GLOBAL_ROOT_UID;
1626 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001627 }
1628 inode->i_mode &= ~(S_ISUID | S_ISGID);
1629 security_task_to_inode(task, inode);
1630 put_task_struct(task);
1631 return 1;
1632 }
1633 d_drop(dentry);
1634 return 0;
1635}
1636
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001637static inline bool proc_inode_is_dead(struct inode *inode)
1638{
1639 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1640}
1641
David Howells1dd704b2013-04-12 01:08:50 +01001642int pid_delete_dentry(const struct dentry *dentry)
1643{
1644 /* Is the task we represent dead?
1645 * If so, then don't put the dentry on the lru list,
1646 * kill it immediately.
1647 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001648 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001649}
1650
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001651const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001652{
1653 .d_revalidate = pid_revalidate,
1654 .d_delete = pid_delete_dentry,
1655};
1656
1657/* Lookups */
1658
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001659/*
1660 * Fill a directory entry.
1661 *
1662 * If possible create the dcache entry and derive our inode number and
1663 * file type from dcache entry.
1664 *
1665 * Since all of the proc inode numbers are dynamically generated, the inode
1666 * numbers do not exist until the inode is cache. This means creating the
1667 * the dcache entry in readdir is necessary to keep the inode numbers
1668 * reported by readdir in sync with the inode numbers reported
1669 * by stat.
1670 */
Al Virof0c3b502013-05-16 12:07:31 -04001671bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001672 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001673 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001674{
Al Virof0c3b502013-05-16 12:07:31 -04001675 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001676 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001677 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001678 unsigned type;
1679 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001680
Al Viro1df98b82013-06-15 11:33:10 +04001681 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001682 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001683 child = d_alloc(dir, &qname);
1684 if (!child)
1685 goto end_instantiate;
1686 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1687 dput(child);
1688 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001689 }
1690 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001691 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001692 ino = inode->i_ino;
1693 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001694 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001695 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001696
1697end_instantiate:
1698 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001699}
1700
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001701#ifdef CONFIG_CHECKPOINT_RESTORE
1702
1703/*
1704 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1705 * which represent vma start and end addresses.
1706 */
1707static int dname_to_vma_addr(struct dentry *dentry,
1708 unsigned long *start, unsigned long *end)
1709{
1710 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1711 return -EINVAL;
1712
1713 return 0;
1714}
1715
Al Viro0b728e12012-06-10 16:03:43 -04001716static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001717{
1718 unsigned long vm_start, vm_end;
1719 bool exact_vma_exists = false;
1720 struct mm_struct *mm = NULL;
1721 struct task_struct *task;
1722 const struct cred *cred;
1723 struct inode *inode;
1724 int status = 0;
1725
Al Viro0b728e12012-06-10 16:03:43 -04001726 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001727 return -ECHILD;
1728
1729 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001730 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001731 goto out_notask;
1732 }
1733
1734 inode = dentry->d_inode;
1735 task = get_proc_task(inode);
1736 if (!task)
1737 goto out_notask;
1738
Cong Wang2344bec2012-05-31 16:26:18 -07001739 mm = mm_access(task, PTRACE_MODE_READ);
1740 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001741 goto out;
1742
1743 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1744 down_read(&mm->mmap_sem);
1745 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1746 up_read(&mm->mmap_sem);
1747 }
1748
1749 mmput(mm);
1750
1751 if (exact_vma_exists) {
1752 if (task_dumpable(task)) {
1753 rcu_read_lock();
1754 cred = __task_cred(task);
1755 inode->i_uid = cred->euid;
1756 inode->i_gid = cred->egid;
1757 rcu_read_unlock();
1758 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001759 inode->i_uid = GLOBAL_ROOT_UID;
1760 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001761 }
1762 security_task_to_inode(task, inode);
1763 status = 1;
1764 }
1765
1766out:
1767 put_task_struct(task);
1768
1769out_notask:
1770 if (status <= 0)
1771 d_drop(dentry);
1772
1773 return status;
1774}
1775
1776static const struct dentry_operations tid_map_files_dentry_operations = {
1777 .d_revalidate = map_files_d_revalidate,
1778 .d_delete = pid_delete_dentry,
1779};
1780
1781static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1782{
1783 unsigned long vm_start, vm_end;
1784 struct vm_area_struct *vma;
1785 struct task_struct *task;
1786 struct mm_struct *mm;
1787 int rc;
1788
1789 rc = -ENOENT;
1790 task = get_proc_task(dentry->d_inode);
1791 if (!task)
1792 goto out;
1793
1794 mm = get_task_mm(task);
1795 put_task_struct(task);
1796 if (!mm)
1797 goto out;
1798
1799 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1800 if (rc)
1801 goto out_mmput;
1802
Artem Fetishev70335ab2014-03-10 15:49:45 -07001803 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001804 down_read(&mm->mmap_sem);
1805 vma = find_exact_vma(mm, vm_start, vm_end);
1806 if (vma && vma->vm_file) {
1807 *path = vma->vm_file->f_path;
1808 path_get(path);
1809 rc = 0;
1810 }
1811 up_read(&mm->mmap_sem);
1812
1813out_mmput:
1814 mmput(mm);
1815out:
1816 return rc;
1817}
1818
1819struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001820 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001821 unsigned long len;
1822 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1823};
1824
Al Viroc52a47a2013-06-15 11:15:20 +04001825static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001826proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1827 struct task_struct *task, const void *ptr)
1828{
Al Viro7b540d02012-08-27 14:55:26 -04001829 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001830 struct proc_inode *ei;
1831 struct inode *inode;
1832
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001833 inode = proc_pid_make_inode(dir->i_sb, task);
1834 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001835 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001836
1837 ei = PROC_I(inode);
1838 ei->op.proc_get_link = proc_map_files_get_link;
1839
1840 inode->i_op = &proc_pid_link_inode_operations;
1841 inode->i_size = 64;
1842 inode->i_mode = S_IFLNK;
1843
Al Viro7b540d02012-08-27 14:55:26 -04001844 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001845 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001846 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001847 inode->i_mode |= S_IWUSR;
1848
1849 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1850 d_add(dentry, inode);
1851
Al Viroc52a47a2013-06-15 11:15:20 +04001852 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001853}
1854
1855static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001856 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001857{
1858 unsigned long vm_start, vm_end;
1859 struct vm_area_struct *vma;
1860 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001861 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001862 struct mm_struct *mm;
1863
Al Viroc52a47a2013-06-15 11:15:20 +04001864 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001865 if (!capable(CAP_SYS_ADMIN))
1866 goto out;
1867
Al Viroc52a47a2013-06-15 11:15:20 +04001868 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001869 task = get_proc_task(dir);
1870 if (!task)
1871 goto out;
1872
Al Viroc52a47a2013-06-15 11:15:20 +04001873 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001874 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001875 goto out_put_task;
1876
Al Viroc52a47a2013-06-15 11:15:20 +04001877 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001878 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001879 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001880
1881 mm = get_task_mm(task);
1882 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001883 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001884
1885 down_read(&mm->mmap_sem);
1886 vma = find_exact_vma(mm, vm_start, vm_end);
1887 if (!vma)
1888 goto out_no_vma;
1889
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001890 if (vma->vm_file)
1891 result = proc_map_files_instantiate(dir, dentry, task,
1892 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001893
1894out_no_vma:
1895 up_read(&mm->mmap_sem);
1896 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001897out_put_task:
1898 put_task_struct(task);
1899out:
Al Viroc52a47a2013-06-15 11:15:20 +04001900 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001901}
1902
1903static const struct inode_operations proc_map_files_inode_operations = {
1904 .lookup = proc_map_files_lookup,
1905 .permission = proc_fd_permission,
1906 .setattr = proc_setattr,
1907};
1908
1909static int
Al Virof0c3b502013-05-16 12:07:31 -04001910proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001911{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001912 struct vm_area_struct *vma;
1913 struct task_struct *task;
1914 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001915 unsigned long nr_files, pos, i;
1916 struct flex_array *fa = NULL;
1917 struct map_files_info info;
1918 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001919 int ret;
1920
Zhao Hongjiang41735812013-02-20 13:13:55 +11001921 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001922 if (!capable(CAP_SYS_ADMIN))
1923 goto out;
1924
1925 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001926 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001927 if (!task)
1928 goto out;
1929
1930 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001931 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001932 goto out_put_task;
1933
1934 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001935 if (!dir_emit_dots(file, ctx))
1936 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001937
Al Virof0c3b502013-05-16 12:07:31 -04001938 mm = get_task_mm(task);
1939 if (!mm)
1940 goto out_put_task;
1941 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001942
Al Virof0c3b502013-05-16 12:07:31 -04001943 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001944
Al Virof0c3b502013-05-16 12:07:31 -04001945 /*
1946 * We need two passes here:
1947 *
1948 * 1) Collect vmas of mapped files with mmap_sem taken
1949 * 2) Release mmap_sem and instantiate entries
1950 *
1951 * otherwise we get lockdep complained, since filldir()
1952 * routine might require mmap_sem taken in might_fault().
1953 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001954
Al Virof0c3b502013-05-16 12:07:31 -04001955 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1956 if (vma->vm_file && ++pos > ctx->pos)
1957 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001958 }
Al Virof0c3b502013-05-16 12:07:31 -04001959
1960 if (nr_files) {
1961 fa = flex_array_alloc(sizeof(info), nr_files,
1962 GFP_KERNEL);
1963 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1964 GFP_KERNEL)) {
1965 ret = -ENOMEM;
1966 if (fa)
1967 flex_array_free(fa);
1968 up_read(&mm->mmap_sem);
1969 mmput(mm);
1970 goto out_put_task;
1971 }
1972 for (i = 0, vma = mm->mmap, pos = 2; vma;
1973 vma = vma->vm_next) {
1974 if (!vma->vm_file)
1975 continue;
1976 if (++pos <= ctx->pos)
1977 continue;
1978
1979 info.mode = vma->vm_file->f_mode;
1980 info.len = snprintf(info.name,
1981 sizeof(info.name), "%lx-%lx",
1982 vma->vm_start, vma->vm_end);
1983 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1984 BUG();
1985 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001986 }
Al Virof0c3b502013-05-16 12:07:31 -04001987 up_read(&mm->mmap_sem);
1988
1989 for (i = 0; i < nr_files; i++) {
1990 p = flex_array_get(fa, i);
1991 if (!proc_fill_cache(file, ctx,
1992 p->name, p->len,
1993 proc_map_files_instantiate,
1994 task,
1995 (void *)(unsigned long)p->mode))
1996 break;
1997 ctx->pos++;
1998 }
1999 if (fa)
2000 flex_array_free(fa);
2001 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002002
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002003out_put_task:
2004 put_task_struct(task);
2005out:
2006 return ret;
2007}
2008
2009static const struct file_operations proc_map_files_operations = {
2010 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002011 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002012 .llseek = default_llseek,
2013};
2014
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002015struct timers_private {
2016 struct pid *pid;
2017 struct task_struct *task;
2018 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002019 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002020 unsigned long flags;
2021};
2022
2023static void *timers_start(struct seq_file *m, loff_t *pos)
2024{
2025 struct timers_private *tp = m->private;
2026
2027 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2028 if (!tp->task)
2029 return ERR_PTR(-ESRCH);
2030
2031 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2032 if (!tp->sighand)
2033 return ERR_PTR(-ESRCH);
2034
2035 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2036}
2037
2038static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2039{
2040 struct timers_private *tp = m->private;
2041 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2042}
2043
2044static void timers_stop(struct seq_file *m, void *v)
2045{
2046 struct timers_private *tp = m->private;
2047
2048 if (tp->sighand) {
2049 unlock_task_sighand(tp->task, &tp->flags);
2050 tp->sighand = NULL;
2051 }
2052
2053 if (tp->task) {
2054 put_task_struct(tp->task);
2055 tp->task = NULL;
2056 }
2057}
2058
2059static int show_timer(struct seq_file *m, void *v)
2060{
2061 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002062 struct timers_private *tp = m->private;
2063 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002064 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002065 [SIGEV_SIGNAL] = "signal",
2066 [SIGEV_NONE] = "none",
2067 [SIGEV_THREAD] = "thread",
2068 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002069
2070 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002071 notify = timer->it_sigev_notify;
2072
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002073 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002074 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2075 timer->sigq->info.si_value.sival_ptr);
2076 seq_printf(m, "notify: %s/%s.%d\n",
2077 nstr[notify & ~SIGEV_THREAD_ID],
2078 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2079 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002080 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002081
2082 return 0;
2083}
2084
2085static const struct seq_operations proc_timers_seq_ops = {
2086 .start = timers_start,
2087 .next = timers_next,
2088 .stop = timers_stop,
2089 .show = show_timer,
2090};
2091
2092static int proc_timers_open(struct inode *inode, struct file *file)
2093{
2094 struct timers_private *tp;
2095
2096 tp = __seq_open_private(file, &proc_timers_seq_ops,
2097 sizeof(struct timers_private));
2098 if (!tp)
2099 return -ENOMEM;
2100
2101 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002102 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002103 return 0;
2104}
2105
2106static const struct file_operations proc_timers_operations = {
2107 .open = proc_timers_open,
2108 .read = seq_read,
2109 .llseek = seq_lseek,
2110 .release = seq_release_private,
2111};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112#endif /* CONFIG_CHECKPOINT_RESTORE */
2113
Al Viroc52a47a2013-06-15 11:15:20 +04002114static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002115 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002116{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002117 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002118 struct inode *inode;
2119 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002120
Eric W. Biederman61a28782006-10-02 02:18:49 -07002121 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002122 if (!inode)
2123 goto out;
2124
2125 ei = PROC_I(inode);
2126 inode->i_mode = p->mode;
2127 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002128 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002129 if (p->iop)
2130 inode->i_op = p->iop;
2131 if (p->fop)
2132 inode->i_fop = p->fop;
2133 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002134 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002135 d_add(dentry, inode);
2136 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002137 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002138 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002139out:
Al Viroc52a47a2013-06-15 11:15:20 +04002140 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002141}
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143static struct dentry *proc_pident_lookup(struct inode *dir,
2144 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002145 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002146 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
Al Viroc52a47a2013-06-15 11:15:20 +04002148 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002149 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002150 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Al Viroc52a47a2013-06-15 11:15:20 +04002152 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Eric W. Biederman99f89552006-06-26 00:25:55 -07002154 if (!task)
2155 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002157 /*
2158 * Yes, it does not scale. And it should not. Don't add
2159 * new entries into /proc/<tgid>/ without very good reasons.
2160 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002161 last = &ents[nents - 1];
2162 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (p->len != dentry->d_name.len)
2164 continue;
2165 if (!memcmp(dentry->d_name.name, p->name, p->len))
2166 break;
2167 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002168 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto out;
2170
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002171 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002173 put_task_struct(task);
2174out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002175 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Al Virof0c3b502013-05-16 12:07:31 -04002178static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002179 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002180{
Al Virof0c3b502013-05-16 12:07:31 -04002181 struct task_struct *task = get_proc_task(file_inode(file));
2182 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002183
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002184 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002185 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002186
Al Virof0c3b502013-05-16 12:07:31 -04002187 if (!dir_emit_dots(file, ctx))
2188 goto out;
2189
2190 if (ctx->pos >= nents + 2)
2191 goto out;
2192
2193 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2194 if (!proc_fill_cache(file, ctx, p->name, p->len,
2195 proc_pident_instantiate, task, p))
2196 break;
2197 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002198 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002199out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002200 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2206 size_t count, loff_t *ppos)
2207{
Al Viro496ad9a2013-01-23 17:07:38 -05002208 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002209 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002210 ssize_t length;
2211 struct task_struct *task = get_proc_task(inode);
2212
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002213 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002214 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002215
2216 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002217 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002218 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002219 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002220 if (length > 0)
2221 length = simple_read_from_buffer(buf, count, ppos, p, length);
2222 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002223 return length;
2224}
2225
2226static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2227 size_t count, loff_t *ppos)
2228{
Al Viro496ad9a2013-01-23 17:07:38 -05002229 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002230 char *page;
2231 ssize_t length;
2232 struct task_struct *task = get_proc_task(inode);
2233
2234 length = -ESRCH;
2235 if (!task)
2236 goto out_no_task;
2237 if (count > PAGE_SIZE)
2238 count = PAGE_SIZE;
2239
2240 /* No partial writes. */
2241 length = -EINVAL;
2242 if (*ppos != 0)
2243 goto out;
2244
2245 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002246 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002247 if (!page)
2248 goto out;
2249
2250 length = -EFAULT;
2251 if (copy_from_user(page, buf, count))
2252 goto out_free;
2253
David Howells107db7c2009-05-08 13:55:27 +01002254 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002255 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002256 if (length < 0)
2257 goto out_free;
2258
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002259 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002260 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002261 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002262 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002263out_free:
2264 free_page((unsigned long) page);
2265out:
2266 put_task_struct(task);
2267out_no_task:
2268 return length;
2269}
2270
Arjan van de Ven00977a52007-02-12 00:55:34 -08002271static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002272 .read = proc_pid_attr_read,
2273 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002274 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002275};
2276
Eric Dumazetc5141e62007-05-08 00:26:15 -07002277static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002278 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2279 REG("prev", S_IRUGO, proc_pid_attr_operations),
2280 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2281 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2282 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2283 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002284};
2285
Al Virof0c3b502013-05-16 12:07:31 -04002286static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Al Virof0c3b502013-05-16 12:07:31 -04002288 return proc_pident_readdir(file, ctx,
2289 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290}
2291
Arjan van de Ven00977a52007-02-12 00:55:34 -08002292static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002294 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002295 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296};
2297
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002298static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002299 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002301 return proc_pident_lookup(dir, dentry,
2302 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
2304
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002305static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002306 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002307 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002308 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309};
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311#endif
2312
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002313#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002314static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2315 size_t count, loff_t *ppos)
2316{
Al Viro496ad9a2013-01-23 17:07:38 -05002317 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002318 struct mm_struct *mm;
2319 char buffer[PROC_NUMBUF];
2320 size_t len;
2321 int ret;
2322
2323 if (!task)
2324 return -ESRCH;
2325
2326 ret = 0;
2327 mm = get_task_mm(task);
2328 if (mm) {
2329 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2330 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2331 MMF_DUMP_FILTER_SHIFT));
2332 mmput(mm);
2333 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2334 }
2335
2336 put_task_struct(task);
2337
2338 return ret;
2339}
2340
2341static ssize_t proc_coredump_filter_write(struct file *file,
2342 const char __user *buf,
2343 size_t count,
2344 loff_t *ppos)
2345{
2346 struct task_struct *task;
2347 struct mm_struct *mm;
2348 char buffer[PROC_NUMBUF], *end;
2349 unsigned int val;
2350 int ret;
2351 int i;
2352 unsigned long mask;
2353
2354 ret = -EFAULT;
2355 memset(buffer, 0, sizeof(buffer));
2356 if (count > sizeof(buffer) - 1)
2357 count = sizeof(buffer) - 1;
2358 if (copy_from_user(buffer, buf, count))
2359 goto out_no_task;
2360
2361 ret = -EINVAL;
2362 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2363 if (*end == '\n')
2364 end++;
2365 if (end - buffer == 0)
2366 goto out_no_task;
2367
2368 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002369 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002370 if (!task)
2371 goto out_no_task;
2372
2373 ret = end - buffer;
2374 mm = get_task_mm(task);
2375 if (!mm)
2376 goto out_no_mm;
2377
2378 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2379 if (val & mask)
2380 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2381 else
2382 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2383 }
2384
2385 mmput(mm);
2386 out_no_mm:
2387 put_task_struct(task);
2388 out_no_task:
2389 return ret;
2390}
2391
2392static const struct file_operations proc_coredump_filter_operations = {
2393 .read = proc_coredump_filter_read,
2394 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002395 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002396};
2397#endif
2398
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002399#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002400static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002401{
Andrea Righi940389b2008-07-28 00:48:12 +02002402 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002403 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002404 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002405
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002406 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2407 if (result)
2408 return result;
2409
2410 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2411 result = -EACCES;
2412 goto out_unlock;
2413 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002414
Andrea Righi59954772008-07-27 17:29:15 +02002415 if (whole && lock_task_sighand(task, &flags)) {
2416 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002417
Andrea Righi59954772008-07-27 17:29:15 +02002418 task_io_accounting_add(&acct, &task->signal->ioac);
2419 while_each_thread(task, t)
2420 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002421
Andrea Righi59954772008-07-27 17:29:15 +02002422 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002423 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002424 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002425 "rchar: %llu\n"
2426 "wchar: %llu\n"
2427 "syscr: %llu\n"
2428 "syscw: %llu\n"
2429 "read_bytes: %llu\n"
2430 "write_bytes: %llu\n"
2431 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002432 (unsigned long long)acct.rchar,
2433 (unsigned long long)acct.wchar,
2434 (unsigned long long)acct.syscr,
2435 (unsigned long long)acct.syscw,
2436 (unsigned long long)acct.read_bytes,
2437 (unsigned long long)acct.write_bytes,
2438 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002439out_unlock:
2440 mutex_unlock(&task->signal->cred_guard_mutex);
2441 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002442}
Andrea Righi297c5d92008-07-25 01:48:49 -07002443
2444static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2445{
2446 return do_io_accounting(task, buffer, 0);
2447}
2448
2449static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2450{
2451 return do_io_accounting(task, buffer, 1);
2452}
2453#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002454
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002455#ifdef CONFIG_USER_NS
2456static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002457 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002458{
2459 struct user_namespace *ns = NULL;
2460 struct task_struct *task;
2461 struct seq_file *seq;
2462 int ret = -EINVAL;
2463
2464 task = get_proc_task(inode);
2465 if (task) {
2466 rcu_read_lock();
2467 ns = get_user_ns(task_cred_xxx(task, user_ns));
2468 rcu_read_unlock();
2469 put_task_struct(task);
2470 }
2471 if (!ns)
2472 goto err;
2473
2474 ret = seq_open(file, seq_ops);
2475 if (ret)
2476 goto err_put_ns;
2477
2478 seq = file->private_data;
2479 seq->private = ns;
2480
2481 return 0;
2482err_put_ns:
2483 put_user_ns(ns);
2484err:
2485 return ret;
2486}
2487
2488static int proc_id_map_release(struct inode *inode, struct file *file)
2489{
2490 struct seq_file *seq = file->private_data;
2491 struct user_namespace *ns = seq->private;
2492 put_user_ns(ns);
2493 return seq_release(inode, file);
2494}
2495
2496static int proc_uid_map_open(struct inode *inode, struct file *file)
2497{
2498 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2499}
2500
2501static int proc_gid_map_open(struct inode *inode, struct file *file)
2502{
2503 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2504}
2505
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002506static int proc_projid_map_open(struct inode *inode, struct file *file)
2507{
2508 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2509}
2510
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002511static const struct file_operations proc_uid_map_operations = {
2512 .open = proc_uid_map_open,
2513 .write = proc_uid_map_write,
2514 .read = seq_read,
2515 .llseek = seq_lseek,
2516 .release = proc_id_map_release,
2517};
2518
2519static const struct file_operations proc_gid_map_operations = {
2520 .open = proc_gid_map_open,
2521 .write = proc_gid_map_write,
2522 .read = seq_read,
2523 .llseek = seq_lseek,
2524 .release = proc_id_map_release,
2525};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002526
2527static const struct file_operations proc_projid_map_operations = {
2528 .open = proc_projid_map_open,
2529 .write = proc_projid_map_write,
2530 .read = seq_read,
2531 .llseek = seq_lseek,
2532 .release = proc_id_map_release,
2533};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002534#endif /* CONFIG_USER_NS */
2535
Kees Cook47830722008-10-06 03:11:58 +04002536static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2537 struct pid *pid, struct task_struct *task)
2538{
Al Viroa9712bc2011-03-23 15:52:50 -04002539 int err = lock_trace(task);
2540 if (!err) {
2541 seq_printf(m, "%08x\n", task->personality);
2542 unlock_trace(task);
2543 }
2544 return err;
Kees Cook47830722008-10-06 03:11:58 +04002545}
2546
Eric W. Biederman801199c2006-10-02 02:18:48 -07002547/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002548 * Thread groups
2549 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002550static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002551static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002552
Eric Dumazetc5141e62007-05-08 00:26:15 -07002553static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002554 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2555 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002556#ifdef CONFIG_CHECKPOINT_RESTORE
2557 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2558#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002559 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002560 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002561#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002562 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002563#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002564 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002565 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002566 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002567 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002568 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002569#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002570 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002571#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002572#ifdef CONFIG_SCHED_AUTOGROUP
2573 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2574#endif
john stultz4614a696b2009-12-14 18:00:05 -08002575 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002576#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002577 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002578#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002579 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002580 ONE("stat", S_IRUGO, proc_tgid_stat),
2581 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002582 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002584 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002585#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002586 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2587 LNK("cwd", proc_cwd_link),
2588 LNK("root", proc_root_link),
2589 LNK("exe", proc_exe_link),
2590 REG("mounts", S_IRUGO, proc_mounts_operations),
2591 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2592 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002593#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002594 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002595 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002596 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002597#endif
2598#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002599 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002600#endif
2601#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002602 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002603#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002604#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002605 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002606#endif
2607#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002608 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002609#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002610#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002611 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002612#endif
Paul Menage8793d852007-10-18 23:39:39 -07002613#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002614 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002615#endif
Paul Menagea4243162007-10-18 23:39:35 -07002616#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002617 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002618#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002619 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002620 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002621 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002622#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002623 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2624 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002625#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002626#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002627 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002628#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002629#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002630 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002631#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002632#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002633 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002634#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002635#ifdef CONFIG_HARDWALL
2636 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2637#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002638#ifdef CONFIG_USER_NS
2639 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2640 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002641 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002642#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002643#ifdef CONFIG_CHECKPOINT_RESTORE
2644 REG("timers", S_IRUGO, proc_timers_operations),
2645#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002646};
2647
Al Virof0c3b502013-05-16 12:07:31 -04002648static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002649{
Al Virof0c3b502013-05-16 12:07:31 -04002650 return proc_pident_readdir(file, ctx,
2651 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002652}
2653
Arjan van de Ven00977a52007-02-12 00:55:34 -08002654static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002655 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002656 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002657 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002658};
2659
Al Viro00cd8dd2012-06-10 17:13:09 -04002660static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2661{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002662 return proc_pident_lookup(dir, dentry,
2663 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002664}
2665
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002666static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002667 .lookup = proc_tgid_base_lookup,
2668 .getattr = pid_getattr,
2669 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002670 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002671};
2672
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002673static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002675 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002676 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002677 struct qstr name;
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", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002681 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002682 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002683 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002684 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002685 d_drop(dentry);
2686 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Eric W. Biederman48e64842006-06-26 00:25:48 -07002689 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002690 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2691 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002692 if (!leader)
2693 goto out;
2694
2695 name.name = "task";
2696 name.len = strlen(name.name);
2697 dir = d_hash_and_lookup(leader, &name);
2698 if (!dir)
2699 goto out_put_leader;
2700
2701 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002702 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002703 dentry = d_hash_and_lookup(dir, &name);
2704 if (dentry) {
2705 shrink_dcache_parent(dentry);
2706 d_drop(dentry);
2707 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002709
2710 dput(dir);
2711out_put_leader:
2712 dput(leader);
2713out:
2714 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715}
2716
Randy Dunlap0895e912007-10-21 21:00:10 -07002717/**
2718 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2719 * @task: task that should be flushed.
2720 *
2721 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002722 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002723 * in. This call is supposed to do all of this job.
2724 *
2725 * Looks in the dcache for
2726 * /proc/@pid
2727 * /proc/@tgid/task/@pid
2728 * if either directory is present flushes it and all of it'ts children
2729 * from the dcache.
2730 *
2731 * It is safe and reasonable to cache /proc entries for a task until
2732 * that task exits. After that they just clog up the dcache with
2733 * useless entries, possibly causing useful dcache entries to be
2734 * flushed instead. This routine is proved to flush those useless
2735 * dcache entries at process exit time.
2736 *
2737 * NOTE: This routine is just an optimization so it does not guarantee
2738 * that no dcache entries will exist at process exit time it
2739 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002740 */
2741
2742void proc_flush_task(struct task_struct *task)
2743{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002744 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002745 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002746 struct upid *upid;
2747
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002748 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002749 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002750
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002751 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002752 upid = &pid->numbers[i];
2753 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002754 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002755 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002756}
2757
Al Viroc52a47a2013-06-15 11:15:20 +04002758static int proc_pid_instantiate(struct inode *dir,
2759 struct dentry * dentry,
2760 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002761{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002762 struct inode *inode;
2763
Eric W. Biederman61a28782006-10-02 02:18:49 -07002764 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002765 if (!inode)
2766 goto out;
2767
2768 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2769 inode->i_op = &proc_tgid_base_inode_operations;
2770 inode->i_fop = &proc_tgid_base_operations;
2771 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002772
Miklos Szeredibfe86842011-10-28 14:13:29 +02002773 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2774 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002775
Nick Pigginfb045ad2011-01-07 17:49:55 +11002776 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002777
2778 d_add(dentry, inode);
2779 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002780 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002781 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002782out:
Al Viroc52a47a2013-06-15 11:15:20 +04002783 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002784}
2785
Al Viro00cd8dd2012-06-10 17:13:09 -04002786struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002788 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002791 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002793 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (tgid == ~0U)
2795 goto out;
2796
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002797 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002798 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002799 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 if (task)
2801 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002802 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 if (!task)
2804 goto out;
2805
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002806 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002807 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808out:
Al Viroc52a47a2013-06-15 11:15:20 +04002809 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810}
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002813 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002814 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002816struct tgid_iter {
2817 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002818 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002819};
2820static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2821{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002822 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002824 if (iter.task)
2825 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002826 rcu_read_lock();
2827retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002828 iter.task = NULL;
2829 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002830 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002831 iter.tgid = pid_nr_ns(pid, ns);
2832 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002833 /* What we to know is if the pid we have find is the
2834 * pid of a thread_group_leader. Testing for task
2835 * being a thread_group_leader is the obvious thing
2836 * todo but there is a window when it fails, due to
2837 * the pid transfer logic in de_thread.
2838 *
2839 * So we perform the straight forward test of seeing
2840 * if the pid we have found is the pid of a thread
2841 * group leader, and don't worry if the task we have
2842 * found doesn't happen to be a thread group leader.
2843 * As we don't care in the case of readdir.
2844 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002845 if (!iter.task || !has_group_leader_pid(iter.task)) {
2846 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002847 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002848 }
2849 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002851 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002852 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
Al Viro021ada72013-03-29 19:27:05 -04002855#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002858int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002860 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002861 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002862 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Al Viro021ada72013-03-29 19:27:05 -04002864 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002865 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Al Viro021ada72013-03-29 19:27:05 -04002867 if (pos == TGID_OFFSET - 1) {
Al Virodb963162013-06-15 10:45:10 +04002868 struct inode *inode = ns->proc_self->d_inode;
2869 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002870 return 0;
Al Viro021ada72013-03-29 19:27:05 -04002871 iter.tgid = 0;
2872 } else {
2873 iter.tgid = pos - TGID_OFFSET;
2874 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002875 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002876 for (iter = next_tgid(ns, iter);
2877 iter.task;
2878 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002879 char name[PROC_NUMBUF];
2880 int len;
2881 if (!has_pid_permissions(ns, iter.task, 2))
2882 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002883
Al Virof0c3b502013-05-16 12:07:31 -04002884 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2885 ctx->pos = iter.tgid + TGID_OFFSET;
2886 if (!proc_fill_cache(file, ctx, name, len,
2887 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002888 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002889 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
Al Virof0c3b502013-05-16 12:07:31 -04002892 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return 0;
2894}
2895
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002896/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002897 * Tasks
2898 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002899static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002900 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002901 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002902 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002903 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002904 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002905 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002906 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002907 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002908#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002909 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002910#endif
john stultz4614a696b2009-12-14 18:00:05 -08002911 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002912#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002913 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002914#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002915 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002916 ONE("stat", S_IRUGO, proc_tid_stat),
2917 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002918 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002919#ifdef CONFIG_CHECKPOINT_RESTORE
2920 REG("children", S_IRUGO, proc_tid_children_operations),
2921#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002922#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002923 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002924#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002925 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2926 LNK("cwd", proc_cwd_link),
2927 LNK("root", proc_root_link),
2928 LNK("exe", proc_exe_link),
2929 REG("mounts", S_IRUGO, proc_mounts_operations),
2930 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002931#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002932 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002933 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002934 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935#endif
2936#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002937 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002938#endif
2939#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002940 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002942#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002943 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#endif
2945#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyanf6e826c2014-08-08 14:21:46 -07002946 ONE("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002947#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002948#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002949 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002950#endif
Paul Menage8793d852007-10-18 23:39:39 -07002951#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002952 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002953#endif
Paul Menagea4243162007-10-18 23:39:35 -07002954#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002955 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002956#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002957 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002958 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002959 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002961 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002962 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002964#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002965 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002966#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002967#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002968 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002969#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002970#ifdef CONFIG_HARDWALL
2971 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2972#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002973#ifdef CONFIG_USER_NS
2974 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2975 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002976 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002977#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002978};
2979
Al Virof0c3b502013-05-16 12:07:31 -04002980static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002981{
Al Virof0c3b502013-05-16 12:07:31 -04002982 return proc_pident_readdir(file, ctx,
2983 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002984}
2985
Al Viro00cd8dd2012-06-10 17:13:09 -04002986static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2987{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002988 return proc_pident_lookup(dir, dentry,
2989 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990}
2991
Arjan van de Ven00977a52007-02-12 00:55:34 -08002992static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002994 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002995 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996};
2997
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002998static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002999 .lookup = proc_tid_base_lookup,
3000 .getattr = pid_getattr,
3001 .setattr = proc_setattr,
3002};
3003
Al Viroc52a47a2013-06-15 11:15:20 +04003004static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003005 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003006{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003007 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003008 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003009
3010 if (!inode)
3011 goto out;
3012 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3013 inode->i_op = &proc_tid_base_inode_operations;
3014 inode->i_fop = &proc_tid_base_operations;
3015 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003016
Miklos Szeredibfe86842011-10-28 14:13:29 +02003017 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3018 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003019
Nick Pigginfb045ad2011-01-07 17:49:55 +11003020 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003021
3022 d_add(dentry, inode);
3023 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003024 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003025 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003026out:
Al Viroc52a47a2013-06-15 11:15:20 +04003027 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003028}
3029
Al Viro00cd8dd2012-06-10 17:13:09 -04003030static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031{
Al Viroc52a47a2013-06-15 11:15:20 +04003032 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033 struct task_struct *task;
3034 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003035 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003036 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003037
3038 if (!leader)
3039 goto out_no_task;
3040
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003041 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003042 if (tid == ~0U)
3043 goto out;
3044
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003045 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003046 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003047 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003048 if (task)
3049 get_task_struct(task);
3050 rcu_read_unlock();
3051 if (!task)
3052 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003053 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003054 goto out_drop_task;
3055
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003056 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003057out_drop_task:
3058 put_task_struct(task);
3059out:
3060 put_task_struct(leader);
3061out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003062 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003063}
3064
3065/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003066 * Find the first tid of a thread group to return to user space.
3067 *
3068 * Usually this is just the thread group leader, but if the users
3069 * buffer was too small or there was a seek into the middle of the
3070 * directory we have more work todo.
3071 *
3072 * In the case of a short read we start with find_task_by_pid.
3073 *
3074 * In the case of a seek we start with the leader and walk nr
3075 * threads past it.
3076 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003077static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3078 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003079{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003080 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003081 unsigned long nr = f_pos;
3082
3083 if (nr != f_pos) /* 32bit overflow? */
3084 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003085
Eric W. Biedermancc288732006-06-26 00:26:01 -07003086 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003087 task = pid_task(pid, PIDTYPE_PID);
3088 if (!task)
3089 goto fail;
3090
3091 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003092 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003093 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003094 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003095 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003096 }
3097
3098 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003099 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003100 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003101
3102 /* If we haven't found our starting place yet start
3103 * with the leader and walk nr threads forward.
3104 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003105 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003106 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003107 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003108 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003109 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003110fail:
3111 pos = NULL;
3112 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003113found:
3114 get_task_struct(pos);
3115out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003116 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003117 return pos;
3118}
3119
3120/*
3121 * Find the next thread in the thread list.
3122 * Return NULL if there is an error or no next thread.
3123 *
3124 * The reference to the input task_struct is released.
3125 */
3126static struct task_struct *next_tid(struct task_struct *start)
3127{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003128 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003129 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003130 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003131 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003132 if (thread_group_leader(pos))
3133 pos = NULL;
3134 else
3135 get_task_struct(pos);
3136 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003137 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003138 put_task_struct(start);
3139 return pos;
3140}
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003143static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003145 struct inode *inode = file_inode(file);
3146 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003147 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003148 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003150 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003151 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Al Virof0c3b502013-05-16 12:07:31 -04003153 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003156 /* f_version caches the tgid value that the last readdir call couldn't
3157 * return. lseek aka telldir automagically resets f_version to 0.
3158 */
Al Virof0c3b502013-05-16 12:07:31 -04003159 ns = file->f_dentry->d_sb->s_fs_info;
3160 tid = (int)file->f_version;
3161 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003162 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003163 task;
Al Virof0c3b502013-05-16 12:07:31 -04003164 task = next_tid(task), ctx->pos++) {
3165 char name[PROC_NUMBUF];
3166 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003167 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003168 len = snprintf(name, sizeof(name), "%d", tid);
3169 if (!proc_fill_cache(file, ctx, name, len,
3170 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003171 /* returning this tgid failed, save it as the first
3172 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003173 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003174 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003178
Al Virof0c3b502013-05-16 12:07:31 -04003179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003181
3182static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3183{
3184 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003185 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003186 generic_fillattr(inode, stat);
3187
Eric W. Biederman99f89552006-06-26 00:25:55 -07003188 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003189 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003190 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003191 }
3192
3193 return 0;
3194}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003195
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003196static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003197 .lookup = proc_task_lookup,
3198 .getattr = proc_task_getattr,
3199 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003200 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003201};
3202
Arjan van de Ven00977a52007-02-12 00:55:34 -08003203static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003204 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003205 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003206 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003207};