blob: 353f287184141b9e4e21d2f23cf7515b7320c023 [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 */
313static int proc_pid_schedstat(struct task_struct *task, char *buffer)
314{
Balbir Singh172ba842007-07-09 18:52:00 +0200315 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100316 (unsigned long long)task->se.sum_exec_runtime,
317 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200318 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320#endif
321
Arjan van de Ven97455122008-01-25 21:08:34 +0100322#ifdef CONFIG_LATENCYTOP
323static int lstats_show_proc(struct seq_file *m, void *v)
324{
325 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800326 struct inode *inode = m->private;
327 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100328
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800329 if (!task)
330 return -ESRCH;
331 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100332 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800333 struct latency_record *lr = &task->latency_record[i];
334 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100335 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800336 seq_printf(m, "%i %li %li",
337 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100338 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800339 unsigned long bt = lr->backtrace[q];
340 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100341 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800342 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100343 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800344 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100345 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800346 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100347 }
348
349 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800350 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100351 return 0;
352}
353
354static int lstats_open(struct inode *inode, struct file *file)
355{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800356 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800357}
358
Arjan van de Ven97455122008-01-25 21:08:34 +0100359static ssize_t lstats_write(struct file *file, const char __user *buf,
360 size_t count, loff_t *offs)
361{
Al Viro496ad9a2013-01-23 17:07:38 -0500362 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100363
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800364 if (!task)
365 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100366 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800367 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100368
369 return count;
370}
371
372static const struct file_operations proc_lstats_operations = {
373 .open = lstats_open,
374 .read = seq_read,
375 .write = lstats_write,
376 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800377 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100378};
379
380#endif
381
Al Viro8d8b97b2013-04-19 23:11:24 -0400382#ifdef CONFIG_CGROUPS
383static int cgroup_open(struct inode *inode, struct file *file)
384{
385 struct pid *pid = PROC_I(inode)->pid;
386 return single_open(file, proc_cgroup_show, pid);
387}
388
389static const struct file_operations proc_cgroup_operations = {
390 .open = cgroup_open,
391 .read = seq_read,
392 .llseek = seq_lseek,
393 .release = single_release,
394};
395#endif
396
397#ifdef CONFIG_PROC_PID_CPUSET
398
399static int cpuset_open(struct inode *inode, struct file *file)
400{
401 struct pid *pid = PROC_I(inode)->pid;
402 return single_open(file, proc_cpuset_show, pid);
403}
404
405static const struct file_operations proc_cpuset_operations = {
406 .open = cpuset_open,
407 .read = seq_read,
408 .llseek = seq_lseek,
409 .release = single_release,
410};
411#endif
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413static int proc_oom_score(struct task_struct *task, char *buffer)
414{
David Rientjesa7f638f2012-05-29 15:06:47 -0700415 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200416 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700418 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200419 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700420 points = oom_badness(task, NULL, NULL, totalpages) *
421 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700422 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return sprintf(buffer, "%lu\n", points);
424}
425
Neil Hormand85f50d2007-10-18 23:40:37 -0700426struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700427 const char *name;
428 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700429};
430
431static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700432 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700433 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
434 [RLIMIT_DATA] = {"Max data size", "bytes"},
435 [RLIMIT_STACK] = {"Max stack size", "bytes"},
436 [RLIMIT_CORE] = {"Max core file size", "bytes"},
437 [RLIMIT_RSS] = {"Max resident set", "bytes"},
438 [RLIMIT_NPROC] = {"Max processes", "processes"},
439 [RLIMIT_NOFILE] = {"Max open files", "files"},
440 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
441 [RLIMIT_AS] = {"Max address space", "bytes"},
442 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
443 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
444 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
445 [RLIMIT_NICE] = {"Max nice priority", NULL},
446 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800447 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700448};
449
450/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700451static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
452 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700453{
454 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700455 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700456
457 struct rlimit rlim[RLIM_NLIMITS];
458
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400459 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700460 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700461 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
462 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700463
464 /*
465 * print the file header
466 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700467 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700468 "Limit", "Soft Limit", "Hard Limit", "Units");
469
470 for (i = 0; i < RLIM_NLIMITS; i++) {
471 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700472 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700473 lnames[i].name, "unlimited");
474 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700475 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700476 lnames[i].name, rlim[i].rlim_cur);
477
478 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700479 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700480 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700481 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700482
483 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700484 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700485 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700486 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700487 }
488
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700489 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700490}
491
Roland McGrathebcb6732008-07-25 19:46:00 -0700492#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700493static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
494 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700495{
496 long nr;
497 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400498 int res = lock_trace(task);
499 if (res)
500 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700501
502 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700503 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400504 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700505 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400506 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700507 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700508 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
509 nr,
510 args[0], args[1], args[2], args[3], args[4], args[5],
511 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400512 unlock_trace(task);
513 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700514}
515#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/************************************************************************/
518/* Here the fs part begins */
519/************************************************************************/
520
521/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700522static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700524 struct task_struct *task;
525 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700526 /* Allow access to a task's file descriptors if it is us or we
527 * may use ptrace attach to the process and find out that
528 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700529 */
530 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700531 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400532 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700533 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700534 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700535 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800538int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700539{
540 int error;
541 struct inode *inode = dentry->d_inode;
542
543 if (attr->ia_valid & ATTR_MODE)
544 return -EPERM;
545
546 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200547 if (error)
548 return error;
549
Christoph Hellwig10257742010-06-04 11:30:02 +0200550 setattr_copy(inode, attr);
551 mark_inode_dirty(inode);
552 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700553}
554
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800555/*
556 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
557 * or euid/egid (for hide_pid_min=2)?
558 */
559static bool has_pid_permissions(struct pid_namespace *pid,
560 struct task_struct *task,
561 int hide_pid_min)
562{
563 if (pid->hide_pid < hide_pid_min)
564 return true;
565 if (in_group_p(pid->pid_gid))
566 return true;
567 return ptrace_may_access(task, PTRACE_MODE_READ);
568}
569
570
571static int proc_pid_permission(struct inode *inode, int mask)
572{
573 struct pid_namespace *pid = inode->i_sb->s_fs_info;
574 struct task_struct *task;
575 bool has_perms;
576
577 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800578 if (!task)
579 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800580 has_perms = has_pid_permissions(pid, task, 1);
581 put_task_struct(task);
582
583 if (!has_perms) {
584 if (pid->hide_pid == 2) {
585 /*
586 * Let's make getdents(), stat(), and open()
587 * consistent with each other. If a process
588 * may not stat() a file, it shouldn't be seen
589 * in procfs at all.
590 */
591 return -ENOENT;
592 }
593
594 return -EPERM;
595 }
596 return generic_permission(inode, mask);
597}
598
599
600
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800601static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700602 .setattr = proc_setattr,
603};
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
606
607static ssize_t proc_info_read(struct file * file, char __user * buf,
608 size_t count, loff_t *ppos)
609{
Al Viro496ad9a2013-01-23 17:07:38 -0500610 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 unsigned long page;
612 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700613 struct task_struct *task = get_proc_task(inode);
614
615 length = -ESRCH;
616 if (!task)
617 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (count > PROC_BLOCK_SIZE)
620 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700621
622 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700623 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700624 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 length = PROC_I(inode)->op.proc_read(task, (char*)page);
627
628 if (length >= 0)
629 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
630 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700631out:
632 put_task_struct(task);
633out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return length;
635}
636
Arjan van de Ven00977a52007-02-12 00:55:34 -0800637static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100639 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640};
641
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800642static int proc_single_show(struct seq_file *m, void *v)
643{
644 struct inode *inode = m->private;
645 struct pid_namespace *ns;
646 struct pid *pid;
647 struct task_struct *task;
648 int ret;
649
650 ns = inode->i_sb->s_fs_info;
651 pid = proc_pid(inode);
652 task = get_pid_task(pid, PIDTYPE_PID);
653 if (!task)
654 return -ESRCH;
655
656 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
657
658 put_task_struct(task);
659 return ret;
660}
661
662static int proc_single_open(struct inode *inode, struct file *filp)
663{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800664 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800665}
666
667static const struct file_operations proc_single_file_operations = {
668 .open = proc_single_open,
669 .read = seq_read,
670 .llseek = seq_lseek,
671 .release = single_release,
672};
673
Cong Wangb409e572012-05-31 16:26:17 -0700674static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Al Viro496ad9a2013-01-23 17:07:38 -0500676 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800677 struct mm_struct *mm;
678
679 if (!task)
680 return -ESRCH;
681
Cong Wangb409e572012-05-31 16:26:17 -0700682 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800683 put_task_struct(task);
684
685 if (IS_ERR(mm))
686 return PTR_ERR(mm);
687
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100688 if (mm) {
689 /* ensure this mm_struct can't be freed */
690 atomic_inc(&mm->mm_count);
691 /* but do not pin its memory */
692 mmput(mm);
693 }
694
Linus Torvaldse2683372012-01-17 15:21:19 -0800695 file->private_data = mm;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
Cong Wangb409e572012-05-31 16:26:17 -0700700static int mem_open(struct inode *inode, struct file *file)
701{
Djalal Harounibc452b42012-07-30 14:42:28 -0700702 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
703
704 /* OK to pass negative loff_t, we can catch out-of-range */
705 file->f_mode |= FMODE_UNSIGNED_OFFSET;
706
707 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700708}
709
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100710static ssize_t mem_rw(struct file *file, char __user *buf,
711 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Linus Torvaldse2683372012-01-17 15:21:19 -0800713 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100714 unsigned long addr = *ppos;
715 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Linus Torvaldse2683372012-01-17 15:21:19 -0800718 if (!mm)
719 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Mel Gormane12ba742007-10-16 01:25:52 -0700721 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800723 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700725 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100726 if (!atomic_inc_not_zero(&mm->mm_users))
727 goto free;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100730 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100732 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 copied = -EFAULT;
734 break;
735 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100736
737 this_len = access_remote_vm(mm, addr, page, this_len, write);
738 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!copied)
740 copied = -EIO;
741 break;
742 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100743
744 if (!write && copy_to_user(buf, page, this_len)) {
745 copied = -EFAULT;
746 break;
747 }
748
749 buf += this_len;
750 addr += this_len;
751 copied += this_len;
752 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100754 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700755
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100756 mmput(mm);
757free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700758 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return copied;
760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100762static ssize_t mem_read(struct file *file, char __user *buf,
763 size_t count, loff_t *ppos)
764{
765 return mem_rw(file, buf, count, ppos, 0);
766}
767
768static ssize_t mem_write(struct file *file, const char __user *buf,
769 size_t count, loff_t *ppos)
770{
771 return mem_rw(file, (char __user*)buf, count, ppos, 1);
772}
773
Matt Mackall85863e42008-02-04 22:29:04 -0800774loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 switch (orig) {
777 case 0:
778 file->f_pos = offset;
779 break;
780 case 1:
781 file->f_pos += offset;
782 break;
783 default:
784 return -EINVAL;
785 }
786 force_successful_syscall_return();
787 return file->f_pos;
788}
789
Linus Torvaldse2683372012-01-17 15:21:19 -0800790static int mem_release(struct inode *inode, struct file *file)
791{
792 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100793 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100794 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800795 return 0;
796}
797
Arjan van de Ven00977a52007-02-12 00:55:34 -0800798static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .llseek = mem_lseek,
800 .read = mem_read,
801 .write = mem_write,
802 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800803 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804};
805
Cong Wangb409e572012-05-31 16:26:17 -0700806static int environ_open(struct inode *inode, struct file *file)
807{
808 return __mem_open(inode, file, PTRACE_MODE_READ);
809}
810
James Pearson315e28c2007-10-16 23:30:17 -0700811static ssize_t environ_read(struct file *file, char __user *buf,
812 size_t count, loff_t *ppos)
813{
James Pearson315e28c2007-10-16 23:30:17 -0700814 char *page;
815 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700816 int ret = 0;
817 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700818
Cong Wangb409e572012-05-31 16:26:17 -0700819 if (!mm)
820 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700821
James Pearson315e28c2007-10-16 23:30:17 -0700822 page = (char *)__get_free_page(GFP_TEMPORARY);
823 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700824 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700825
Al Virod6f64b82011-02-15 22:26:01 -0500826 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700827 if (!atomic_inc_not_zero(&mm->mm_users))
828 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700829 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700830 size_t this_len, max_len;
831 int retval;
832
833 if (src >= (mm->env_end - mm->env_start))
834 break;
James Pearson315e28c2007-10-16 23:30:17 -0700835
836 this_len = mm->env_end - (mm->env_start + src);
837
Djalal Harounie8905ec2012-07-30 14:42:26 -0700838 max_len = min_t(size_t, PAGE_SIZE, count);
839 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700840
Cong Wangb409e572012-05-31 16:26:17 -0700841 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700842 page, this_len, 0);
843
844 if (retval <= 0) {
845 ret = retval;
846 break;
847 }
848
849 if (copy_to_user(buf, page, retval)) {
850 ret = -EFAULT;
851 break;
852 }
853
854 ret += retval;
855 src += retval;
856 buf += retval;
857 count -= retval;
858 }
859 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700860 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700861
862free:
James Pearson315e28c2007-10-16 23:30:17 -0700863 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700864 return ret;
865}
866
867static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700868 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700869 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100870 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700871 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700872};
873
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800874static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
875 loff_t *ppos)
876{
Al Viro496ad9a2013-01-23 17:07:38 -0500877 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800878 char buffer[PROC_NUMBUF];
879 int oom_adj = OOM_ADJUST_MIN;
880 size_t len;
881 unsigned long flags;
882
883 if (!task)
884 return -ESRCH;
885 if (lock_task_sighand(task, &flags)) {
886 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
887 oom_adj = OOM_ADJUST_MAX;
888 else
889 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
890 OOM_SCORE_ADJ_MAX;
891 unlock_task_sighand(task, &flags);
892 }
893 put_task_struct(task);
894 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
895 return simple_read_from_buffer(buf, count, ppos, buffer, len);
896}
897
898static ssize_t oom_adj_write(struct file *file, const char __user *buf,
899 size_t count, loff_t *ppos)
900{
901 struct task_struct *task;
902 char buffer[PROC_NUMBUF];
903 int oom_adj;
904 unsigned long flags;
905 int err;
906
907 memset(buffer, 0, sizeof(buffer));
908 if (count > sizeof(buffer) - 1)
909 count = sizeof(buffer) - 1;
910 if (copy_from_user(buffer, buf, count)) {
911 err = -EFAULT;
912 goto out;
913 }
914
915 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
916 if (err)
917 goto out;
918 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
919 oom_adj != OOM_DISABLE) {
920 err = -EINVAL;
921 goto out;
922 }
923
Al Viro496ad9a2013-01-23 17:07:38 -0500924 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800925 if (!task) {
926 err = -ESRCH;
927 goto out;
928 }
929
930 task_lock(task);
931 if (!task->mm) {
932 err = -EINVAL;
933 goto err_task_lock;
934 }
935
936 if (!lock_task_sighand(task, &flags)) {
937 err = -ESRCH;
938 goto err_task_lock;
939 }
940
941 /*
942 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
943 * value is always attainable.
944 */
945 if (oom_adj == OOM_ADJUST_MAX)
946 oom_adj = OOM_SCORE_ADJ_MAX;
947 else
948 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
949
950 if (oom_adj < task->signal->oom_score_adj &&
951 !capable(CAP_SYS_RESOURCE)) {
952 err = -EACCES;
953 goto err_sighand;
954 }
955
956 /*
957 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
958 * /proc/pid/oom_score_adj instead.
959 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800960 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 -0800961 current->comm, task_pid_nr(current), task_pid_nr(task),
962 task_pid_nr(task));
963
964 task->signal->oom_score_adj = oom_adj;
965 trace_oom_score_adj_update(task);
966err_sighand:
967 unlock_task_sighand(task, &flags);
968err_task_lock:
969 task_unlock(task);
970 put_task_struct(task);
971out:
972 return err < 0 ? err : count;
973}
974
975static const struct file_operations proc_oom_adj_operations = {
976 .read = oom_adj_read,
977 .write = oom_adj_write,
978 .llseek = generic_file_llseek,
979};
980
David Rientjesa63d83f2010-08-09 17:19:46 -0700981static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
982 size_t count, loff_t *ppos)
983{
Al Viro496ad9a2013-01-23 17:07:38 -0500984 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700985 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800986 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700987 unsigned long flags;
988 size_t len;
989
990 if (!task)
991 return -ESRCH;
992 if (lock_task_sighand(task, &flags)) {
993 oom_score_adj = task->signal->oom_score_adj;
994 unlock_task_sighand(task, &flags);
995 }
996 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800997 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700998 return simple_read_from_buffer(buf, count, ppos, buffer, len);
999}
1000
1001static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1002 size_t count, loff_t *ppos)
1003{
1004 struct task_struct *task;
1005 char buffer[PROC_NUMBUF];
1006 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001007 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001008 int err;
1009
1010 memset(buffer, 0, sizeof(buffer));
1011 if (count > sizeof(buffer) - 1)
1012 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001013 if (copy_from_user(buffer, buf, count)) {
1014 err = -EFAULT;
1015 goto out;
1016 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001017
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001018 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001019 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001020 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001021 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001022 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1023 err = -EINVAL;
1024 goto out;
1025 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001026
Al Viro496ad9a2013-01-23 17:07:38 -05001027 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001028 if (!task) {
1029 err = -ESRCH;
1030 goto out;
1031 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001032
Ying Han3d5992d2010-10-26 14:21:23 -07001033 task_lock(task);
1034 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001035 err = -EINVAL;
1036 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001037 }
David Rientjesd19d5472010-10-26 14:21:26 -07001038
1039 if (!lock_task_sighand(task, &flags)) {
1040 err = -ESRCH;
1041 goto err_task_lock;
1042 }
1043
David Rientjesa9c58b902012-12-11 16:02:54 -08001044 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001045 !capable(CAP_SYS_RESOURCE)) {
1046 err = -EACCES;
1047 goto err_sighand;
1048 }
1049
David Rientjesa9c58b902012-12-11 16:02:54 -08001050 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001051 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001052 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001053 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001054
David Rientjes723548b2010-10-26 14:21:25 -07001055err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001056 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001057err_task_lock:
1058 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001059 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001060out:
1061 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001062}
1063
1064static const struct file_operations proc_oom_score_adj_operations = {
1065 .read = oom_score_adj_read,
1066 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001067 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001068};
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070#ifdef CONFIG_AUDITSYSCALL
1071#define TMPBUFLEN 21
1072static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1073 size_t count, loff_t *ppos)
1074{
Al Viro496ad9a2013-01-23 17:07:38 -05001075 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001076 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 ssize_t length;
1078 char tmpbuf[TMPBUFLEN];
1079
Eric W. Biederman99f89552006-06-26 00:25:55 -07001080 if (!task)
1081 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001083 from_kuid(file->f_cred->user_ns,
1084 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001085 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1087}
1088
1089static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1090 size_t count, loff_t *ppos)
1091{
Al Viro496ad9a2013-01-23 17:07:38 -05001092 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 char *page, *tmp;
1094 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001096 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001098 rcu_read_lock();
1099 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1100 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001102 }
1103 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Al Viroe0182902006-05-18 08:28:02 -04001105 if (count >= PAGE_SIZE)
1106 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 if (*ppos != 0) {
1109 /* No partial writes. */
1110 return -EINVAL;
1111 }
Mel Gormane12ba742007-10-16 01:25:52 -07001112 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (!page)
1114 return -ENOMEM;
1115 length = -EFAULT;
1116 if (copy_from_user(page, buf, count))
1117 goto out_free_page;
1118
Al Viroe0182902006-05-18 08:28:02 -04001119 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 loginuid = simple_strtoul(page, &tmp, 10);
1121 if (tmp == page) {
1122 length = -EINVAL;
1123 goto out_free_page;
1124
1125 }
Eric Paris81407c82013-05-24 09:49:14 -04001126
1127 /* is userspace tring to explicitly UNSET the loginuid? */
1128 if (loginuid == AUDIT_UID_UNSET) {
1129 kloginuid = INVALID_UID;
1130 } else {
1131 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1132 if (!uid_valid(kloginuid)) {
1133 length = -EINVAL;
1134 goto out_free_page;
1135 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001136 }
1137
1138 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (likely(length == 0))
1140 length = count;
1141
1142out_free_page:
1143 free_page((unsigned long) page);
1144 return length;
1145}
1146
Arjan van de Ven00977a52007-02-12 00:55:34 -08001147static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 .read = proc_loginuid_read,
1149 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001150 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151};
Eric Paris1e0bd752008-03-13 08:15:31 -04001152
1153static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1154 size_t count, loff_t *ppos)
1155{
Al Viro496ad9a2013-01-23 17:07:38 -05001156 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001157 struct task_struct *task = get_proc_task(inode);
1158 ssize_t length;
1159 char tmpbuf[TMPBUFLEN];
1160
1161 if (!task)
1162 return -ESRCH;
1163 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1164 audit_get_sessionid(task));
1165 put_task_struct(task);
1166 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1167}
1168
1169static const struct file_operations proc_sessionid_operations = {
1170 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001171 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001172};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173#endif
1174
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001175#ifdef CONFIG_FAULT_INJECTION
1176static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1177 size_t count, loff_t *ppos)
1178{
Al Viro496ad9a2013-01-23 17:07:38 -05001179 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001180 char buffer[PROC_NUMBUF];
1181 size_t len;
1182 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001183
1184 if (!task)
1185 return -ESRCH;
1186 make_it_fail = task->make_it_fail;
1187 put_task_struct(task);
1188
1189 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001190
1191 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001192}
1193
1194static ssize_t proc_fault_inject_write(struct file * file,
1195 const char __user * buf, size_t count, loff_t *ppos)
1196{
1197 struct task_struct *task;
1198 char buffer[PROC_NUMBUF], *end;
1199 int make_it_fail;
1200
1201 if (!capable(CAP_SYS_RESOURCE))
1202 return -EPERM;
1203 memset(buffer, 0, sizeof(buffer));
1204 if (count > sizeof(buffer) - 1)
1205 count = sizeof(buffer) - 1;
1206 if (copy_from_user(buffer, buf, count))
1207 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001208 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1209 if (*end)
1210 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001211 if (make_it_fail < 0 || make_it_fail > 1)
1212 return -EINVAL;
1213
Al Viro496ad9a2013-01-23 17:07:38 -05001214 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001215 if (!task)
1216 return -ESRCH;
1217 task->make_it_fail = make_it_fail;
1218 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001219
1220 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001221}
1222
Arjan van de Ven00977a52007-02-12 00:55:34 -08001223static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001224 .read = proc_fault_inject_read,
1225 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001226 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001227};
1228#endif
1229
Arjan van de Ven97455122008-01-25 21:08:34 +01001230
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001231#ifdef CONFIG_SCHED_DEBUG
1232/*
1233 * Print out various scheduling related per-task fields:
1234 */
1235static int sched_show(struct seq_file *m, void *v)
1236{
1237 struct inode *inode = m->private;
1238 struct task_struct *p;
1239
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001240 p = get_proc_task(inode);
1241 if (!p)
1242 return -ESRCH;
1243 proc_sched_show_task(p, m);
1244
1245 put_task_struct(p);
1246
1247 return 0;
1248}
1249
1250static ssize_t
1251sched_write(struct file *file, const char __user *buf,
1252 size_t count, loff_t *offset)
1253{
Al Viro496ad9a2013-01-23 17:07:38 -05001254 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001255 struct task_struct *p;
1256
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001257 p = get_proc_task(inode);
1258 if (!p)
1259 return -ESRCH;
1260 proc_sched_set_task(p);
1261
1262 put_task_struct(p);
1263
1264 return count;
1265}
1266
1267static int sched_open(struct inode *inode, struct file *filp)
1268{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001269 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001270}
1271
1272static const struct file_operations proc_pid_sched_operations = {
1273 .open = sched_open,
1274 .read = seq_read,
1275 .write = sched_write,
1276 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001277 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001278};
1279
1280#endif
1281
Mike Galbraith5091faa2010-11-30 14:18:03 +01001282#ifdef CONFIG_SCHED_AUTOGROUP
1283/*
1284 * Print out autogroup related information:
1285 */
1286static int sched_autogroup_show(struct seq_file *m, void *v)
1287{
1288 struct inode *inode = m->private;
1289 struct task_struct *p;
1290
1291 p = get_proc_task(inode);
1292 if (!p)
1293 return -ESRCH;
1294 proc_sched_autogroup_show_task(p, m);
1295
1296 put_task_struct(p);
1297
1298 return 0;
1299}
1300
1301static ssize_t
1302sched_autogroup_write(struct file *file, const char __user *buf,
1303 size_t count, loff_t *offset)
1304{
Al Viro496ad9a2013-01-23 17:07:38 -05001305 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001306 struct task_struct *p;
1307 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001308 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001309 int err;
1310
1311 memset(buffer, 0, sizeof(buffer));
1312 if (count > sizeof(buffer) - 1)
1313 count = sizeof(buffer) - 1;
1314 if (copy_from_user(buffer, buf, count))
1315 return -EFAULT;
1316
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001317 err = kstrtoint(strstrip(buffer), 0, &nice);
1318 if (err < 0)
1319 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001320
1321 p = get_proc_task(inode);
1322 if (!p)
1323 return -ESRCH;
1324
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001325 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001326 if (err)
1327 count = err;
1328
1329 put_task_struct(p);
1330
1331 return count;
1332}
1333
1334static int sched_autogroup_open(struct inode *inode, struct file *filp)
1335{
1336 int ret;
1337
1338 ret = single_open(filp, sched_autogroup_show, NULL);
1339 if (!ret) {
1340 struct seq_file *m = filp->private_data;
1341
1342 m->private = inode;
1343 }
1344 return ret;
1345}
1346
1347static const struct file_operations proc_pid_sched_autogroup_operations = {
1348 .open = sched_autogroup_open,
1349 .read = seq_read,
1350 .write = sched_autogroup_write,
1351 .llseek = seq_lseek,
1352 .release = single_release,
1353};
1354
1355#endif /* CONFIG_SCHED_AUTOGROUP */
1356
john stultz4614a696b2009-12-14 18:00:05 -08001357static ssize_t comm_write(struct file *file, const char __user *buf,
1358 size_t count, loff_t *offset)
1359{
Al Viro496ad9a2013-01-23 17:07:38 -05001360 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001361 struct task_struct *p;
1362 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001363 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001364
1365 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001366 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001367 return -EFAULT;
1368
1369 p = get_proc_task(inode);
1370 if (!p)
1371 return -ESRCH;
1372
1373 if (same_thread_group(current, p))
1374 set_task_comm(p, buffer);
1375 else
1376 count = -EINVAL;
1377
1378 put_task_struct(p);
1379
1380 return count;
1381}
1382
1383static int comm_show(struct seq_file *m, void *v)
1384{
1385 struct inode *inode = m->private;
1386 struct task_struct *p;
1387
1388 p = get_proc_task(inode);
1389 if (!p)
1390 return -ESRCH;
1391
1392 task_lock(p);
1393 seq_printf(m, "%s\n", p->comm);
1394 task_unlock(p);
1395
1396 put_task_struct(p);
1397
1398 return 0;
1399}
1400
1401static int comm_open(struct inode *inode, struct file *filp)
1402{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001403 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001404}
1405
1406static const struct file_operations proc_pid_set_comm_operations = {
1407 .open = comm_open,
1408 .read = seq_read,
1409 .write = comm_write,
1410 .llseek = seq_lseek,
1411 .release = single_release,
1412};
1413
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001414static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001415{
1416 struct task_struct *task;
1417 struct mm_struct *mm;
1418 struct file *exe_file;
1419
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001420 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001421 if (!task)
1422 return -ENOENT;
1423 mm = get_task_mm(task);
1424 put_task_struct(task);
1425 if (!mm)
1426 return -ENOENT;
1427 exe_file = get_mm_exe_file(mm);
1428 mmput(mm);
1429 if (exe_file) {
1430 *exe_path = exe_file->f_path;
1431 path_get(&exe_file->f_path);
1432 fput(exe_file);
1433 return 0;
1434 } else
1435 return -ENOENT;
1436}
1437
Al Viro008b1502005-08-20 00:17:39 +01001438static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001441 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 int error = -EACCES;
1443
Eric W. Biederman778c1142006-06-26 00:25:58 -07001444 /* Are we allowed to snoop on the tasks file descriptors? */
1445 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Christoph Hellwig408ef012012-06-18 10:47:03 -04001448 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1449 if (error)
1450 goto out;
1451
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001452 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001453 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454out:
Al Viro008b1502005-08-20 00:17:39 +01001455 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001458static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
Mel Gormane12ba742007-10-16 01:25:52 -07001460 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001461 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int len;
1463
1464 if (!tmp)
1465 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001466
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001467 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001468 len = PTR_ERR(pathname);
1469 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001471 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 if (len > buflen)
1474 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001475 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 len = -EFAULT;
1477 out:
1478 free_page((unsigned long)tmp);
1479 return len;
1480}
1481
1482static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1483{
1484 int error = -EACCES;
1485 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001486 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Eric W. Biederman778c1142006-06-26 00:25:58 -07001488 /* Are we allowed to snoop on the tasks file descriptors? */
1489 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001492 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (error)
1494 goto out;
1495
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001496 error = do_proc_readlink(&path, buffer, buflen);
1497 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 return error;
1500}
1501
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001502const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001504 .follow_link = proc_pid_follow_link,
1505 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506};
1507
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001508
1509/* building an inode */
1510
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001511struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001512{
1513 struct inode * inode;
1514 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001515 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001516
1517 /* We need a new inode */
1518
1519 inode = new_inode(sb);
1520 if (!inode)
1521 goto out;
1522
1523 /* Common stuff */
1524 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001525 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001526 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527 inode->i_op = &proc_def_inode_operations;
1528
1529 /*
1530 * grab the reference to task.
1531 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001532 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001533 if (!ei->pid)
1534 goto out_unlock;
1535
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001536 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001537 rcu_read_lock();
1538 cred = __task_cred(task);
1539 inode->i_uid = cred->euid;
1540 inode->i_gid = cred->egid;
1541 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001542 }
1543 security_task_to_inode(task, inode);
1544
1545out:
1546 return inode;
1547
1548out_unlock:
1549 iput(inode);
1550 return NULL;
1551}
1552
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001553int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001554{
1555 struct inode *inode = dentry->d_inode;
1556 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001557 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001558 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001559
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001560 generic_fillattr(inode, stat);
1561
1562 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001563 stat->uid = GLOBAL_ROOT_UID;
1564 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001565 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1566 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001567 if (!has_pid_permissions(pid, task, 2)) {
1568 rcu_read_unlock();
1569 /*
1570 * This doesn't prevent learning whether PID exists,
1571 * it only makes getattr() consistent with readdir().
1572 */
1573 return -ENOENT;
1574 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001575 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1576 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001577 cred = __task_cred(task);
1578 stat->uid = cred->euid;
1579 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001580 }
1581 }
1582 rcu_read_unlock();
1583 return 0;
1584}
1585
1586/* dentry stuff */
1587
1588/*
1589 * Exceptional case: normally we are not allowed to unhash a busy
1590 * directory. In this case, however, we can do it - no aliasing problems
1591 * due to the way we treat inodes.
1592 *
1593 * Rewrite the inode's ownerships here because the owning task may have
1594 * performed a setuid(), etc.
1595 *
1596 * Before the /proc/pid/status file was created the only way to read
1597 * the effective uid of a /process was to stat /proc/pid. Reading
1598 * /proc/pid/status is slow enough that procps and other packages
1599 * kept stating /proc/pid. To keep the rules in /proc simple I have
1600 * made this apply to all per process world readable and executable
1601 * directories.
1602 */
Al Viro0b728e12012-06-10 16:03:43 -04001603int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001604{
Nick Piggin34286d62011-01-07 17:49:57 +11001605 struct inode *inode;
1606 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001607 const struct cred *cred;
1608
Al Viro0b728e12012-06-10 16:03:43 -04001609 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001610 return -ECHILD;
1611
1612 inode = dentry->d_inode;
1613 task = get_proc_task(inode);
1614
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001615 if (task) {
1616 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1617 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001618 rcu_read_lock();
1619 cred = __task_cred(task);
1620 inode->i_uid = cred->euid;
1621 inode->i_gid = cred->egid;
1622 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001623 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001624 inode->i_uid = GLOBAL_ROOT_UID;
1625 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001626 }
1627 inode->i_mode &= ~(S_ISUID | S_ISGID);
1628 security_task_to_inode(task, inode);
1629 put_task_struct(task);
1630 return 1;
1631 }
1632 d_drop(dentry);
1633 return 0;
1634}
1635
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001636static inline bool proc_inode_is_dead(struct inode *inode)
1637{
1638 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1639}
1640
David Howells1dd704b2013-04-12 01:08:50 +01001641int pid_delete_dentry(const struct dentry *dentry)
1642{
1643 /* Is the task we represent dead?
1644 * If so, then don't put the dentry on the lru list,
1645 * kill it immediately.
1646 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001647 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001648}
1649
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001650const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651{
1652 .d_revalidate = pid_revalidate,
1653 .d_delete = pid_delete_dentry,
1654};
1655
1656/* Lookups */
1657
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001658/*
1659 * Fill a directory entry.
1660 *
1661 * If possible create the dcache entry and derive our inode number and
1662 * file type from dcache entry.
1663 *
1664 * Since all of the proc inode numbers are dynamically generated, the inode
1665 * numbers do not exist until the inode is cache. This means creating the
1666 * the dcache entry in readdir is necessary to keep the inode numbers
1667 * reported by readdir in sync with the inode numbers reported
1668 * by stat.
1669 */
Al Virof0c3b502013-05-16 12:07:31 -04001670bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001671 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001672 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001673{
Al Virof0c3b502013-05-16 12:07:31 -04001674 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001675 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001676 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001677 unsigned type;
1678 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001679
Al Viro1df98b82013-06-15 11:33:10 +04001680 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001681 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001682 child = d_alloc(dir, &qname);
1683 if (!child)
1684 goto end_instantiate;
1685 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1686 dput(child);
1687 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001688 }
1689 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001690 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001691 ino = inode->i_ino;
1692 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001693 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001694 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001695
1696end_instantiate:
1697 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001698}
1699
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001700#ifdef CONFIG_CHECKPOINT_RESTORE
1701
1702/*
1703 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1704 * which represent vma start and end addresses.
1705 */
1706static int dname_to_vma_addr(struct dentry *dentry,
1707 unsigned long *start, unsigned long *end)
1708{
1709 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1710 return -EINVAL;
1711
1712 return 0;
1713}
1714
Al Viro0b728e12012-06-10 16:03:43 -04001715static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001716{
1717 unsigned long vm_start, vm_end;
1718 bool exact_vma_exists = false;
1719 struct mm_struct *mm = NULL;
1720 struct task_struct *task;
1721 const struct cred *cred;
1722 struct inode *inode;
1723 int status = 0;
1724
Al Viro0b728e12012-06-10 16:03:43 -04001725 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001726 return -ECHILD;
1727
1728 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001729 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001730 goto out_notask;
1731 }
1732
1733 inode = dentry->d_inode;
1734 task = get_proc_task(inode);
1735 if (!task)
1736 goto out_notask;
1737
Cong Wang2344bec2012-05-31 16:26:18 -07001738 mm = mm_access(task, PTRACE_MODE_READ);
1739 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001740 goto out;
1741
1742 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1743 down_read(&mm->mmap_sem);
1744 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1745 up_read(&mm->mmap_sem);
1746 }
1747
1748 mmput(mm);
1749
1750 if (exact_vma_exists) {
1751 if (task_dumpable(task)) {
1752 rcu_read_lock();
1753 cred = __task_cred(task);
1754 inode->i_uid = cred->euid;
1755 inode->i_gid = cred->egid;
1756 rcu_read_unlock();
1757 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001758 inode->i_uid = GLOBAL_ROOT_UID;
1759 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001760 }
1761 security_task_to_inode(task, inode);
1762 status = 1;
1763 }
1764
1765out:
1766 put_task_struct(task);
1767
1768out_notask:
1769 if (status <= 0)
1770 d_drop(dentry);
1771
1772 return status;
1773}
1774
1775static const struct dentry_operations tid_map_files_dentry_operations = {
1776 .d_revalidate = map_files_d_revalidate,
1777 .d_delete = pid_delete_dentry,
1778};
1779
1780static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1781{
1782 unsigned long vm_start, vm_end;
1783 struct vm_area_struct *vma;
1784 struct task_struct *task;
1785 struct mm_struct *mm;
1786 int rc;
1787
1788 rc = -ENOENT;
1789 task = get_proc_task(dentry->d_inode);
1790 if (!task)
1791 goto out;
1792
1793 mm = get_task_mm(task);
1794 put_task_struct(task);
1795 if (!mm)
1796 goto out;
1797
1798 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1799 if (rc)
1800 goto out_mmput;
1801
Artem Fetishev70335ab2014-03-10 15:49:45 -07001802 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001803 down_read(&mm->mmap_sem);
1804 vma = find_exact_vma(mm, vm_start, vm_end);
1805 if (vma && vma->vm_file) {
1806 *path = vma->vm_file->f_path;
1807 path_get(path);
1808 rc = 0;
1809 }
1810 up_read(&mm->mmap_sem);
1811
1812out_mmput:
1813 mmput(mm);
1814out:
1815 return rc;
1816}
1817
1818struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001819 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001820 unsigned long len;
1821 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1822};
1823
Al Viroc52a47a2013-06-15 11:15:20 +04001824static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001825proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1826 struct task_struct *task, const void *ptr)
1827{
Al Viro7b540d02012-08-27 14:55:26 -04001828 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001829 struct proc_inode *ei;
1830 struct inode *inode;
1831
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001832 inode = proc_pid_make_inode(dir->i_sb, task);
1833 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001834 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001835
1836 ei = PROC_I(inode);
1837 ei->op.proc_get_link = proc_map_files_get_link;
1838
1839 inode->i_op = &proc_pid_link_inode_operations;
1840 inode->i_size = 64;
1841 inode->i_mode = S_IFLNK;
1842
Al Viro7b540d02012-08-27 14:55:26 -04001843 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001844 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001845 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001846 inode->i_mode |= S_IWUSR;
1847
1848 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1849 d_add(dentry, inode);
1850
Al Viroc52a47a2013-06-15 11:15:20 +04001851 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001852}
1853
1854static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001855 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001856{
1857 unsigned long vm_start, vm_end;
1858 struct vm_area_struct *vma;
1859 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001860 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001861 struct mm_struct *mm;
1862
Al Viroc52a47a2013-06-15 11:15:20 +04001863 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001864 if (!capable(CAP_SYS_ADMIN))
1865 goto out;
1866
Al Viroc52a47a2013-06-15 11:15:20 +04001867 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001868 task = get_proc_task(dir);
1869 if (!task)
1870 goto out;
1871
Al Viroc52a47a2013-06-15 11:15:20 +04001872 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001873 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001874 goto out_put_task;
1875
Al Viroc52a47a2013-06-15 11:15:20 +04001876 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001877 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001878 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001879
1880 mm = get_task_mm(task);
1881 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001882 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001883
1884 down_read(&mm->mmap_sem);
1885 vma = find_exact_vma(mm, vm_start, vm_end);
1886 if (!vma)
1887 goto out_no_vma;
1888
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001889 if (vma->vm_file)
1890 result = proc_map_files_instantiate(dir, dentry, task,
1891 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001892
1893out_no_vma:
1894 up_read(&mm->mmap_sem);
1895 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001896out_put_task:
1897 put_task_struct(task);
1898out:
Al Viroc52a47a2013-06-15 11:15:20 +04001899 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001900}
1901
1902static const struct inode_operations proc_map_files_inode_operations = {
1903 .lookup = proc_map_files_lookup,
1904 .permission = proc_fd_permission,
1905 .setattr = proc_setattr,
1906};
1907
1908static int
Al Virof0c3b502013-05-16 12:07:31 -04001909proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001910{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001911 struct vm_area_struct *vma;
1912 struct task_struct *task;
1913 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001914 unsigned long nr_files, pos, i;
1915 struct flex_array *fa = NULL;
1916 struct map_files_info info;
1917 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001918 int ret;
1919
Zhao Hongjiang41735812013-02-20 13:13:55 +11001920 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921 if (!capable(CAP_SYS_ADMIN))
1922 goto out;
1923
1924 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001925 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001926 if (!task)
1927 goto out;
1928
1929 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001930 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001931 goto out_put_task;
1932
1933 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001934 if (!dir_emit_dots(file, ctx))
1935 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001936
Al Virof0c3b502013-05-16 12:07:31 -04001937 mm = get_task_mm(task);
1938 if (!mm)
1939 goto out_put_task;
1940 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001941
Al Virof0c3b502013-05-16 12:07:31 -04001942 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001943
Al Virof0c3b502013-05-16 12:07:31 -04001944 /*
1945 * We need two passes here:
1946 *
1947 * 1) Collect vmas of mapped files with mmap_sem taken
1948 * 2) Release mmap_sem and instantiate entries
1949 *
1950 * otherwise we get lockdep complained, since filldir()
1951 * routine might require mmap_sem taken in might_fault().
1952 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001953
Al Virof0c3b502013-05-16 12:07:31 -04001954 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1955 if (vma->vm_file && ++pos > ctx->pos)
1956 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001957 }
Al Virof0c3b502013-05-16 12:07:31 -04001958
1959 if (nr_files) {
1960 fa = flex_array_alloc(sizeof(info), nr_files,
1961 GFP_KERNEL);
1962 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1963 GFP_KERNEL)) {
1964 ret = -ENOMEM;
1965 if (fa)
1966 flex_array_free(fa);
1967 up_read(&mm->mmap_sem);
1968 mmput(mm);
1969 goto out_put_task;
1970 }
1971 for (i = 0, vma = mm->mmap, pos = 2; vma;
1972 vma = vma->vm_next) {
1973 if (!vma->vm_file)
1974 continue;
1975 if (++pos <= ctx->pos)
1976 continue;
1977
1978 info.mode = vma->vm_file->f_mode;
1979 info.len = snprintf(info.name,
1980 sizeof(info.name), "%lx-%lx",
1981 vma->vm_start, vma->vm_end);
1982 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1983 BUG();
1984 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001985 }
Al Virof0c3b502013-05-16 12:07:31 -04001986 up_read(&mm->mmap_sem);
1987
1988 for (i = 0; i < nr_files; i++) {
1989 p = flex_array_get(fa, i);
1990 if (!proc_fill_cache(file, ctx,
1991 p->name, p->len,
1992 proc_map_files_instantiate,
1993 task,
1994 (void *)(unsigned long)p->mode))
1995 break;
1996 ctx->pos++;
1997 }
1998 if (fa)
1999 flex_array_free(fa);
2000 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002001
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002002out_put_task:
2003 put_task_struct(task);
2004out:
2005 return ret;
2006}
2007
2008static const struct file_operations proc_map_files_operations = {
2009 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002010 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002011 .llseek = default_llseek,
2012};
2013
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002014struct timers_private {
2015 struct pid *pid;
2016 struct task_struct *task;
2017 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002018 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002019 unsigned long flags;
2020};
2021
2022static void *timers_start(struct seq_file *m, loff_t *pos)
2023{
2024 struct timers_private *tp = m->private;
2025
2026 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2027 if (!tp->task)
2028 return ERR_PTR(-ESRCH);
2029
2030 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2031 if (!tp->sighand)
2032 return ERR_PTR(-ESRCH);
2033
2034 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2035}
2036
2037static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2038{
2039 struct timers_private *tp = m->private;
2040 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2041}
2042
2043static void timers_stop(struct seq_file *m, void *v)
2044{
2045 struct timers_private *tp = m->private;
2046
2047 if (tp->sighand) {
2048 unlock_task_sighand(tp->task, &tp->flags);
2049 tp->sighand = NULL;
2050 }
2051
2052 if (tp->task) {
2053 put_task_struct(tp->task);
2054 tp->task = NULL;
2055 }
2056}
2057
2058static int show_timer(struct seq_file *m, void *v)
2059{
2060 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002061 struct timers_private *tp = m->private;
2062 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002063 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002064 [SIGEV_SIGNAL] = "signal",
2065 [SIGEV_NONE] = "none",
2066 [SIGEV_THREAD] = "thread",
2067 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002068
2069 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002070 notify = timer->it_sigev_notify;
2071
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002072 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002073 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2074 timer->sigq->info.si_value.sival_ptr);
2075 seq_printf(m, "notify: %s/%s.%d\n",
2076 nstr[notify & ~SIGEV_THREAD_ID],
2077 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2078 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002079 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002080
2081 return 0;
2082}
2083
2084static const struct seq_operations proc_timers_seq_ops = {
2085 .start = timers_start,
2086 .next = timers_next,
2087 .stop = timers_stop,
2088 .show = show_timer,
2089};
2090
2091static int proc_timers_open(struct inode *inode, struct file *file)
2092{
2093 struct timers_private *tp;
2094
2095 tp = __seq_open_private(file, &proc_timers_seq_ops,
2096 sizeof(struct timers_private));
2097 if (!tp)
2098 return -ENOMEM;
2099
2100 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002101 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002102 return 0;
2103}
2104
2105static const struct file_operations proc_timers_operations = {
2106 .open = proc_timers_open,
2107 .read = seq_read,
2108 .llseek = seq_lseek,
2109 .release = seq_release_private,
2110};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002111#endif /* CONFIG_CHECKPOINT_RESTORE */
2112
Al Viroc52a47a2013-06-15 11:15:20 +04002113static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002114 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002115{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002116 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002117 struct inode *inode;
2118 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002119
Eric W. Biederman61a28782006-10-02 02:18:49 -07002120 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002121 if (!inode)
2122 goto out;
2123
2124 ei = PROC_I(inode);
2125 inode->i_mode = p->mode;
2126 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002127 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002128 if (p->iop)
2129 inode->i_op = p->iop;
2130 if (p->fop)
2131 inode->i_fop = p->fop;
2132 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002133 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002134 d_add(dentry, inode);
2135 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002136 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002137 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002138out:
Al Viroc52a47a2013-06-15 11:15:20 +04002139 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002140}
2141
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142static struct dentry *proc_pident_lookup(struct inode *dir,
2143 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002144 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002145 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Al Viroc52a47a2013-06-15 11:15:20 +04002147 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002148 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002149 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Al Viroc52a47a2013-06-15 11:15:20 +04002151 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Eric W. Biederman99f89552006-06-26 00:25:55 -07002153 if (!task)
2154 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002156 /*
2157 * Yes, it does not scale. And it should not. Don't add
2158 * new entries into /proc/<tgid>/ without very good reasons.
2159 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002160 last = &ents[nents - 1];
2161 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (p->len != dentry->d_name.len)
2163 continue;
2164 if (!memcmp(dentry->d_name.name, p->name, p->len))
2165 break;
2166 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002167 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 goto out;
2169
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002170 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002172 put_task_struct(task);
2173out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002174 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
Al Virof0c3b502013-05-16 12:07:31 -04002177static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002178 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002179{
Al Virof0c3b502013-05-16 12:07:31 -04002180 struct task_struct *task = get_proc_task(file_inode(file));
2181 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002182
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002183 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002184 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002185
Al Virof0c3b502013-05-16 12:07:31 -04002186 if (!dir_emit_dots(file, ctx))
2187 goto out;
2188
2189 if (ctx->pos >= nents + 2)
2190 goto out;
2191
2192 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2193 if (!proc_fill_cache(file, ctx, p->name, p->len,
2194 proc_pident_instantiate, task, p))
2195 break;
2196 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002197 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002198out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002199 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002204static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2205 size_t count, loff_t *ppos)
2206{
Al Viro496ad9a2013-01-23 17:07:38 -05002207 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002208 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002209 ssize_t length;
2210 struct task_struct *task = get_proc_task(inode);
2211
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002212 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002213 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002214
2215 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002216 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002217 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002218 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002219 if (length > 0)
2220 length = simple_read_from_buffer(buf, count, ppos, p, length);
2221 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002222 return length;
2223}
2224
2225static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2226 size_t count, loff_t *ppos)
2227{
Al Viro496ad9a2013-01-23 17:07:38 -05002228 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002229 char *page;
2230 ssize_t length;
2231 struct task_struct *task = get_proc_task(inode);
2232
2233 length = -ESRCH;
2234 if (!task)
2235 goto out_no_task;
2236 if (count > PAGE_SIZE)
2237 count = PAGE_SIZE;
2238
2239 /* No partial writes. */
2240 length = -EINVAL;
2241 if (*ppos != 0)
2242 goto out;
2243
2244 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002245 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002246 if (!page)
2247 goto out;
2248
2249 length = -EFAULT;
2250 if (copy_from_user(page, buf, count))
2251 goto out_free;
2252
David Howells107db7c2009-05-08 13:55:27 +01002253 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002254 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002255 if (length < 0)
2256 goto out_free;
2257
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002258 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002259 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002260 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002261 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002262out_free:
2263 free_page((unsigned long) page);
2264out:
2265 put_task_struct(task);
2266out_no_task:
2267 return length;
2268}
2269
Arjan van de Ven00977a52007-02-12 00:55:34 -08002270static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002271 .read = proc_pid_attr_read,
2272 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002273 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002274};
2275
Eric Dumazetc5141e62007-05-08 00:26:15 -07002276static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002277 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2278 REG("prev", S_IRUGO, proc_pid_attr_operations),
2279 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2280 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2281 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2282 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002283};
2284
Al Virof0c3b502013-05-16 12:07:31 -04002285static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Al Virof0c3b502013-05-16 12:07:31 -04002287 return proc_pident_readdir(file, ctx,
2288 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Arjan van de Ven00977a52007-02-12 00:55:34 -08002291static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002293 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002294 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295};
2296
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002297static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002298 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002300 return proc_pident_lookup(dir, dentry,
2301 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002304static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002305 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002306 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002307 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308};
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310#endif
2311
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002312#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002313static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2314 size_t count, loff_t *ppos)
2315{
Al Viro496ad9a2013-01-23 17:07:38 -05002316 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002317 struct mm_struct *mm;
2318 char buffer[PROC_NUMBUF];
2319 size_t len;
2320 int ret;
2321
2322 if (!task)
2323 return -ESRCH;
2324
2325 ret = 0;
2326 mm = get_task_mm(task);
2327 if (mm) {
2328 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2329 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2330 MMF_DUMP_FILTER_SHIFT));
2331 mmput(mm);
2332 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2333 }
2334
2335 put_task_struct(task);
2336
2337 return ret;
2338}
2339
2340static ssize_t proc_coredump_filter_write(struct file *file,
2341 const char __user *buf,
2342 size_t count,
2343 loff_t *ppos)
2344{
2345 struct task_struct *task;
2346 struct mm_struct *mm;
2347 char buffer[PROC_NUMBUF], *end;
2348 unsigned int val;
2349 int ret;
2350 int i;
2351 unsigned long mask;
2352
2353 ret = -EFAULT;
2354 memset(buffer, 0, sizeof(buffer));
2355 if (count > sizeof(buffer) - 1)
2356 count = sizeof(buffer) - 1;
2357 if (copy_from_user(buffer, buf, count))
2358 goto out_no_task;
2359
2360 ret = -EINVAL;
2361 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2362 if (*end == '\n')
2363 end++;
2364 if (end - buffer == 0)
2365 goto out_no_task;
2366
2367 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002368 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002369 if (!task)
2370 goto out_no_task;
2371
2372 ret = end - buffer;
2373 mm = get_task_mm(task);
2374 if (!mm)
2375 goto out_no_mm;
2376
2377 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2378 if (val & mask)
2379 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2380 else
2381 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2382 }
2383
2384 mmput(mm);
2385 out_no_mm:
2386 put_task_struct(task);
2387 out_no_task:
2388 return ret;
2389}
2390
2391static const struct file_operations proc_coredump_filter_operations = {
2392 .read = proc_coredump_filter_read,
2393 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002394 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002395};
2396#endif
2397
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002398#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002399static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002400{
Andrea Righi940389b2008-07-28 00:48:12 +02002401 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002402 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002403 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002404
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002405 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2406 if (result)
2407 return result;
2408
2409 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2410 result = -EACCES;
2411 goto out_unlock;
2412 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002413
Andrea Righi59954772008-07-27 17:29:15 +02002414 if (whole && lock_task_sighand(task, &flags)) {
2415 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002416
Andrea Righi59954772008-07-27 17:29:15 +02002417 task_io_accounting_add(&acct, &task->signal->ioac);
2418 while_each_thread(task, t)
2419 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002420
Andrea Righi59954772008-07-27 17:29:15 +02002421 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002422 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002423 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002424 "rchar: %llu\n"
2425 "wchar: %llu\n"
2426 "syscr: %llu\n"
2427 "syscw: %llu\n"
2428 "read_bytes: %llu\n"
2429 "write_bytes: %llu\n"
2430 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002431 (unsigned long long)acct.rchar,
2432 (unsigned long long)acct.wchar,
2433 (unsigned long long)acct.syscr,
2434 (unsigned long long)acct.syscw,
2435 (unsigned long long)acct.read_bytes,
2436 (unsigned long long)acct.write_bytes,
2437 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002438out_unlock:
2439 mutex_unlock(&task->signal->cred_guard_mutex);
2440 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002441}
Andrea Righi297c5d92008-07-25 01:48:49 -07002442
2443static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2444{
2445 return do_io_accounting(task, buffer, 0);
2446}
2447
2448static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2449{
2450 return do_io_accounting(task, buffer, 1);
2451}
2452#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002453
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002454#ifdef CONFIG_USER_NS
2455static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002456 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002457{
2458 struct user_namespace *ns = NULL;
2459 struct task_struct *task;
2460 struct seq_file *seq;
2461 int ret = -EINVAL;
2462
2463 task = get_proc_task(inode);
2464 if (task) {
2465 rcu_read_lock();
2466 ns = get_user_ns(task_cred_xxx(task, user_ns));
2467 rcu_read_unlock();
2468 put_task_struct(task);
2469 }
2470 if (!ns)
2471 goto err;
2472
2473 ret = seq_open(file, seq_ops);
2474 if (ret)
2475 goto err_put_ns;
2476
2477 seq = file->private_data;
2478 seq->private = ns;
2479
2480 return 0;
2481err_put_ns:
2482 put_user_ns(ns);
2483err:
2484 return ret;
2485}
2486
2487static int proc_id_map_release(struct inode *inode, struct file *file)
2488{
2489 struct seq_file *seq = file->private_data;
2490 struct user_namespace *ns = seq->private;
2491 put_user_ns(ns);
2492 return seq_release(inode, file);
2493}
2494
2495static int proc_uid_map_open(struct inode *inode, struct file *file)
2496{
2497 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2498}
2499
2500static int proc_gid_map_open(struct inode *inode, struct file *file)
2501{
2502 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2503}
2504
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002505static int proc_projid_map_open(struct inode *inode, struct file *file)
2506{
2507 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2508}
2509
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002510static const struct file_operations proc_uid_map_operations = {
2511 .open = proc_uid_map_open,
2512 .write = proc_uid_map_write,
2513 .read = seq_read,
2514 .llseek = seq_lseek,
2515 .release = proc_id_map_release,
2516};
2517
2518static const struct file_operations proc_gid_map_operations = {
2519 .open = proc_gid_map_open,
2520 .write = proc_gid_map_write,
2521 .read = seq_read,
2522 .llseek = seq_lseek,
2523 .release = proc_id_map_release,
2524};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002525
2526static const struct file_operations proc_projid_map_operations = {
2527 .open = proc_projid_map_open,
2528 .write = proc_projid_map_write,
2529 .read = seq_read,
2530 .llseek = seq_lseek,
2531 .release = proc_id_map_release,
2532};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002533#endif /* CONFIG_USER_NS */
2534
Kees Cook47830722008-10-06 03:11:58 +04002535static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2536 struct pid *pid, struct task_struct *task)
2537{
Al Viroa9712bc2011-03-23 15:52:50 -04002538 int err = lock_trace(task);
2539 if (!err) {
2540 seq_printf(m, "%08x\n", task->personality);
2541 unlock_trace(task);
2542 }
2543 return err;
Kees Cook47830722008-10-06 03:11:58 +04002544}
2545
Eric W. Biederman801199c2006-10-02 02:18:48 -07002546/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002547 * Thread groups
2548 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002549static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002550static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002551
Eric Dumazetc5141e62007-05-08 00:26:15 -07002552static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002553 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2554 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002555#ifdef CONFIG_CHECKPOINT_RESTORE
2556 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2557#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002558 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002559 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002560#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002561 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002562#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002563 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002564 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002565 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002566 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002567 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002568#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002569 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002570#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002571#ifdef CONFIG_SCHED_AUTOGROUP
2572 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2573#endif
john stultz4614a696b2009-12-14 18:00:05 -08002574 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002575#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002576 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002577#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002578 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002579 ONE("stat", S_IRUGO, proc_tgid_stat),
2580 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002581 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002582#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002583 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002584#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002585 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2586 LNK("cwd", proc_cwd_link),
2587 LNK("root", proc_root_link),
2588 LNK("exe", proc_exe_link),
2589 REG("mounts", S_IRUGO, proc_mounts_operations),
2590 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2591 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002592#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002593 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002594 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002595 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002596#endif
2597#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002598 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002599#endif
2600#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002601 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002602#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002603#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002604 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605#endif
2606#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002607 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002608#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002609#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002610 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002611#endif
Paul Menage8793d852007-10-18 23:39:39 -07002612#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002613 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614#endif
Paul Menagea4243162007-10-18 23:39:35 -07002615#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002616 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002617#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002618 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002619 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002620 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002621#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002622 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2623 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002624#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002625#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002626 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002627#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002628#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002629 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002630#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002631#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002632 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002633#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002634#ifdef CONFIG_HARDWALL
2635 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2636#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002637#ifdef CONFIG_USER_NS
2638 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2639 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002640 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002641#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002642#ifdef CONFIG_CHECKPOINT_RESTORE
2643 REG("timers", S_IRUGO, proc_timers_operations),
2644#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002645};
2646
Al Virof0c3b502013-05-16 12:07:31 -04002647static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648{
Al Virof0c3b502013-05-16 12:07:31 -04002649 return proc_pident_readdir(file, ctx,
2650 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002651}
2652
Arjan van de Ven00977a52007-02-12 00:55:34 -08002653static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002654 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002655 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002656 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657};
2658
Al Viro00cd8dd2012-06-10 17:13:09 -04002659static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2660{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002661 return proc_pident_lookup(dir, dentry,
2662 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002663}
2664
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002665static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002666 .lookup = proc_tgid_base_lookup,
2667 .getattr = pid_getattr,
2668 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002669 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002670};
2671
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002672static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002674 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002675 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002676 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Eric W. Biederman48e64842006-06-26 00:25:48 -07002678 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002679 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002680 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002681 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002682 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002683 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002684 d_drop(dentry);
2685 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Eric W. Biederman48e64842006-06-26 00:25:48 -07002688 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002689 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2690 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002691 if (!leader)
2692 goto out;
2693
2694 name.name = "task";
2695 name.len = strlen(name.name);
2696 dir = d_hash_and_lookup(leader, &name);
2697 if (!dir)
2698 goto out_put_leader;
2699
2700 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002701 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002702 dentry = d_hash_and_lookup(dir, &name);
2703 if (dentry) {
2704 shrink_dcache_parent(dentry);
2705 d_drop(dentry);
2706 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002708
2709 dput(dir);
2710out_put_leader:
2711 dput(leader);
2712out:
2713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Randy Dunlap0895e912007-10-21 21:00:10 -07002716/**
2717 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2718 * @task: task that should be flushed.
2719 *
2720 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002721 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002722 * in. This call is supposed to do all of this job.
2723 *
2724 * Looks in the dcache for
2725 * /proc/@pid
2726 * /proc/@tgid/task/@pid
2727 * if either directory is present flushes it and all of it'ts children
2728 * from the dcache.
2729 *
2730 * It is safe and reasonable to cache /proc entries for a task until
2731 * that task exits. After that they just clog up the dcache with
2732 * useless entries, possibly causing useful dcache entries to be
2733 * flushed instead. This routine is proved to flush those useless
2734 * dcache entries at process exit time.
2735 *
2736 * NOTE: This routine is just an optimization so it does not guarantee
2737 * that no dcache entries will exist at process exit time it
2738 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002739 */
2740
2741void proc_flush_task(struct task_struct *task)
2742{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002743 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002744 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002745 struct upid *upid;
2746
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002747 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002748 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002749
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002750 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002751 upid = &pid->numbers[i];
2752 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002753 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002754 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002755}
2756
Al Viroc52a47a2013-06-15 11:15:20 +04002757static int proc_pid_instantiate(struct inode *dir,
2758 struct dentry * dentry,
2759 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002760{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002761 struct inode *inode;
2762
Eric W. Biederman61a28782006-10-02 02:18:49 -07002763 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002764 if (!inode)
2765 goto out;
2766
2767 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2768 inode->i_op = &proc_tgid_base_inode_operations;
2769 inode->i_fop = &proc_tgid_base_operations;
2770 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002771
Miklos Szeredibfe86842011-10-28 14:13:29 +02002772 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2773 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002774
Nick Pigginfb045ad2011-01-07 17:49:55 +11002775 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002776
2777 d_add(dentry, inode);
2778 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002779 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002780 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002781out:
Al Viroc52a47a2013-06-15 11:15:20 +04002782 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002783}
2784
Al Viro00cd8dd2012-06-10 17:13:09 -04002785struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002787 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002790 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002792 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (tgid == ~0U)
2794 goto out;
2795
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002796 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002797 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002798 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (task)
2800 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002801 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 if (!task)
2803 goto out;
2804
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002805 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002806 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807out:
Al Viroc52a47a2013-06-15 11:15:20 +04002808 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002812 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002815struct tgid_iter {
2816 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002817 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002818};
2819static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2820{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002821 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002823 if (iter.task)
2824 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002825 rcu_read_lock();
2826retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002827 iter.task = NULL;
2828 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002829 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002830 iter.tgid = pid_nr_ns(pid, ns);
2831 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002832 /* What we to know is if the pid we have find is the
2833 * pid of a thread_group_leader. Testing for task
2834 * being a thread_group_leader is the obvious thing
2835 * todo but there is a window when it fails, due to
2836 * the pid transfer logic in de_thread.
2837 *
2838 * So we perform the straight forward test of seeing
2839 * if the pid we have found is the pid of a thread
2840 * group leader, and don't worry if the task we have
2841 * found doesn't happen to be a thread group leader.
2842 * As we don't care in the case of readdir.
2843 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002844 if (!iter.task || !has_group_leader_pid(iter.task)) {
2845 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002846 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002847 }
2848 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002850 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002851 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
Al Viro021ada72013-03-29 19:27:05 -04002854#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
2856/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002857int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002859 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002860 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002861 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Al Viro021ada72013-03-29 19:27:05 -04002863 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Al Viro021ada72013-03-29 19:27:05 -04002866 if (pos == TGID_OFFSET - 1) {
Al Virodb963162013-06-15 10:45:10 +04002867 struct inode *inode = ns->proc_self->d_inode;
2868 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002869 return 0;
Al Viro021ada72013-03-29 19:27:05 -04002870 iter.tgid = 0;
2871 } else {
2872 iter.tgid = pos - TGID_OFFSET;
2873 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002874 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002875 for (iter = next_tgid(ns, iter);
2876 iter.task;
2877 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002878 char name[PROC_NUMBUF];
2879 int len;
2880 if (!has_pid_permissions(ns, iter.task, 2))
2881 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002882
Al Virof0c3b502013-05-16 12:07:31 -04002883 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2884 ctx->pos = iter.tgid + TGID_OFFSET;
2885 if (!proc_fill_cache(file, ctx, name, len,
2886 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002887 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
Al Virof0c3b502013-05-16 12:07:31 -04002891 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return 0;
2893}
2894
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002895/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002896 * Tasks
2897 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002898static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002899 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002900 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002901 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002902 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002903 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002904 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002905 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002906 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002907#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002908 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002909#endif
john stultz4614a696b2009-12-14 18:00:05 -08002910 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002911#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002912 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002913#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002914 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002915 ONE("stat", S_IRUGO, proc_tid_stat),
2916 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002917 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002918#ifdef CONFIG_CHECKPOINT_RESTORE
2919 REG("children", S_IRUGO, proc_tid_children_operations),
2920#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002921#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002922 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002923#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002924 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2925 LNK("cwd", proc_cwd_link),
2926 LNK("root", proc_root_link),
2927 LNK("exe", proc_exe_link),
2928 REG("mounts", S_IRUGO, proc_mounts_operations),
2929 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002930#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002932 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002933 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002934#endif
2935#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002936 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002937#endif
2938#ifdef CONFIG_KALLSYMS
Alexey Dobriyanedfcd602014-08-08 14:21:44 -07002939 ONE("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002940#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002941#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002942 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002943#endif
2944#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002945 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002946#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002947#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002948 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002949#endif
Paul Menage8793d852007-10-18 23:39:39 -07002950#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002951 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002952#endif
Paul Menagea4243162007-10-18 23:39:35 -07002953#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002954 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002955#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002956 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002957 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002958 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002959#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002960 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002961 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002962#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002963#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002964 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002965#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002966#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002967 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002968#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002969#ifdef CONFIG_HARDWALL
2970 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2971#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002972#ifdef CONFIG_USER_NS
2973 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2974 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002975 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002976#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002977};
2978
Al Virof0c3b502013-05-16 12:07:31 -04002979static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002980{
Al Virof0c3b502013-05-16 12:07:31 -04002981 return proc_pident_readdir(file, ctx,
2982 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002983}
2984
Al Viro00cd8dd2012-06-10 17:13:09 -04002985static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2986{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002987 return proc_pident_lookup(dir, dentry,
2988 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002989}
2990
Arjan van de Ven00977a52007-02-12 00:55:34 -08002991static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002992 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002993 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002994 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002995};
2996
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002997static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002998 .lookup = proc_tid_base_lookup,
2999 .getattr = pid_getattr,
3000 .setattr = proc_setattr,
3001};
3002
Al Viroc52a47a2013-06-15 11:15:20 +04003003static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003004 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003005{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003006 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003007 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003008
3009 if (!inode)
3010 goto out;
3011 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3012 inode->i_op = &proc_tid_base_inode_operations;
3013 inode->i_fop = &proc_tid_base_operations;
3014 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003015
Miklos Szeredibfe86842011-10-28 14:13:29 +02003016 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3017 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003018
Nick Pigginfb045ad2011-01-07 17:49:55 +11003019 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003020
3021 d_add(dentry, inode);
3022 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003023 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003024 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003025out:
Al Viroc52a47a2013-06-15 11:15:20 +04003026 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003027}
3028
Al Viro00cd8dd2012-06-10 17:13:09 -04003029static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003030{
Al Viroc52a47a2013-06-15 11:15:20 +04003031 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003032 struct task_struct *task;
3033 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003034 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003035 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036
3037 if (!leader)
3038 goto out_no_task;
3039
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003040 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003041 if (tid == ~0U)
3042 goto out;
3043
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003044 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003045 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003046 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003047 if (task)
3048 get_task_struct(task);
3049 rcu_read_unlock();
3050 if (!task)
3051 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003052 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003053 goto out_drop_task;
3054
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003055 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003056out_drop_task:
3057 put_task_struct(task);
3058out:
3059 put_task_struct(leader);
3060out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003061 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003062}
3063
3064/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003065 * Find the first tid of a thread group to return to user space.
3066 *
3067 * Usually this is just the thread group leader, but if the users
3068 * buffer was too small or there was a seek into the middle of the
3069 * directory we have more work todo.
3070 *
3071 * In the case of a short read we start with find_task_by_pid.
3072 *
3073 * In the case of a seek we start with the leader and walk nr
3074 * threads past it.
3075 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003076static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3077 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003078{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003079 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003080 unsigned long nr = f_pos;
3081
3082 if (nr != f_pos) /* 32bit overflow? */
3083 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003084
Eric W. Biedermancc288732006-06-26 00:26:01 -07003085 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003086 task = pid_task(pid, PIDTYPE_PID);
3087 if (!task)
3088 goto fail;
3089
3090 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003091 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003092 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003093 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003094 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003095 }
3096
3097 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003098 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003099 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003100
3101 /* If we haven't found our starting place yet start
3102 * with the leader and walk nr threads forward.
3103 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003104 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003105 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003106 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003107 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003108 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003109fail:
3110 pos = NULL;
3111 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003112found:
3113 get_task_struct(pos);
3114out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003115 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003116 return pos;
3117}
3118
3119/*
3120 * Find the next thread in the thread list.
3121 * Return NULL if there is an error or no next thread.
3122 *
3123 * The reference to the input task_struct is released.
3124 */
3125static struct task_struct *next_tid(struct task_struct *start)
3126{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003127 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003128 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003129 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003130 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003131 if (thread_group_leader(pos))
3132 pos = NULL;
3133 else
3134 get_task_struct(pos);
3135 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003136 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003137 put_task_struct(start);
3138 return pos;
3139}
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003142static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003144 struct inode *inode = file_inode(file);
3145 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003146 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003147 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003149 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003150 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151
Al Virof0c3b502013-05-16 12:07:31 -04003152 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003155 /* f_version caches the tgid value that the last readdir call couldn't
3156 * return. lseek aka telldir automagically resets f_version to 0.
3157 */
Al Virof0c3b502013-05-16 12:07:31 -04003158 ns = file->f_dentry->d_sb->s_fs_info;
3159 tid = (int)file->f_version;
3160 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003161 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003162 task;
Al Virof0c3b502013-05-16 12:07:31 -04003163 task = next_tid(task), ctx->pos++) {
3164 char name[PROC_NUMBUF];
3165 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003166 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003167 len = snprintf(name, sizeof(name), "%d", tid);
3168 if (!proc_fill_cache(file, ctx, name, len,
3169 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003170 /* returning this tgid failed, save it as the first
3171 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003172 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003173 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003177
Al Virof0c3b502013-05-16 12:07:31 -04003178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003180
3181static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3182{
3183 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003184 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003185 generic_fillattr(inode, stat);
3186
Eric W. Biederman99f89552006-06-26 00:25:55 -07003187 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003188 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003189 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003190 }
3191
3192 return 0;
3193}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003194
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003195static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003196 .lookup = proc_task_lookup,
3197 .getattr = proc_task_getattr,
3198 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003199 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003200};
3201
Arjan van de Ven00977a52007-02-12 00:55:34 -08003202static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003203 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003204 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003205 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003206};