blob: a6d0214c7c95927ca16b3a47ec79e88c0ca7cd4d [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 */
237static int proc_pid_wchan(struct task_struct *task, char *buffer)
238{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700239 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700240 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 wchan = get_wchan(task);
243
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700244 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600245 if (!ptrace_may_access(task, PTRACE_MODE_READ))
246 return 0;
247 else
248 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700249 else
250 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252#endif /* CONFIG_KALLSYMS */
253
Al Viroa9712bc2011-03-23 15:52:50 -0400254static int lock_trace(struct task_struct *task)
255{
256 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
257 if (err)
258 return err;
259 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
260 mutex_unlock(&task->signal->cred_guard_mutex);
261 return -EPERM;
262 }
263 return 0;
264}
265
266static void unlock_trace(struct task_struct *task)
267{
268 mutex_unlock(&task->signal->cred_guard_mutex);
269}
270
Ken Chen2ec220e2008-11-10 11:26:08 +0300271#ifdef CONFIG_STACKTRACE
272
273#define MAX_STACK_TRACE_DEPTH 64
274
275static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
276 struct pid *pid, struct task_struct *task)
277{
278 struct stack_trace trace;
279 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400280 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300281 int i;
282
283 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
284 if (!entries)
285 return -ENOMEM;
286
287 trace.nr_entries = 0;
288 trace.max_entries = MAX_STACK_TRACE_DEPTH;
289 trace.entries = entries;
290 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300291
Al Viroa9712bc2011-03-23 15:52:50 -0400292 err = lock_trace(task);
293 if (!err) {
294 save_stack_trace_tsk(task, &trace);
295
296 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700297 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400298 (void *)entries[i], (void *)entries[i]);
299 }
300 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300301 }
302 kfree(entries);
303
Al Viroa9712bc2011-03-23 15:52:50 -0400304 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300305}
306#endif
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#ifdef CONFIG_SCHEDSTATS
309/*
310 * Provides /proc/PID/schedstat
311 */
312static int proc_pid_schedstat(struct task_struct *task, char *buffer)
313{
Balbir Singh172ba842007-07-09 18:52:00 +0200314 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100315 (unsigned long long)task->se.sum_exec_runtime,
316 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200317 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319#endif
320
Arjan van de Ven97455122008-01-25 21:08:34 +0100321#ifdef CONFIG_LATENCYTOP
322static int lstats_show_proc(struct seq_file *m, void *v)
323{
324 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800325 struct inode *inode = m->private;
326 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100327
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800328 if (!task)
329 return -ESRCH;
330 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100331 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800332 struct latency_record *lr = &task->latency_record[i];
333 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100334 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800335 seq_printf(m, "%i %li %li",
336 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100337 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800338 unsigned long bt = lr->backtrace[q];
339 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100340 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800341 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100342 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800343 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100344 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800345 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100346 }
347
348 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800349 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100350 return 0;
351}
352
353static int lstats_open(struct inode *inode, struct file *file)
354{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800355 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800356}
357
Arjan van de Ven97455122008-01-25 21:08:34 +0100358static ssize_t lstats_write(struct file *file, const char __user *buf,
359 size_t count, loff_t *offs)
360{
Al Viro496ad9a2013-01-23 17:07:38 -0500361 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100362
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800363 if (!task)
364 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100365 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800366 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100367
368 return count;
369}
370
371static const struct file_operations proc_lstats_operations = {
372 .open = lstats_open,
373 .read = seq_read,
374 .write = lstats_write,
375 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800376 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100377};
378
379#endif
380
Al Viro8d8b97b2013-04-19 23:11:24 -0400381#ifdef CONFIG_CGROUPS
382static int cgroup_open(struct inode *inode, struct file *file)
383{
384 struct pid *pid = PROC_I(inode)->pid;
385 return single_open(file, proc_cgroup_show, pid);
386}
387
388static const struct file_operations proc_cgroup_operations = {
389 .open = cgroup_open,
390 .read = seq_read,
391 .llseek = seq_lseek,
392 .release = single_release,
393};
394#endif
395
396#ifdef CONFIG_PROC_PID_CPUSET
397
398static int cpuset_open(struct inode *inode, struct file *file)
399{
400 struct pid *pid = PROC_I(inode)->pid;
401 return single_open(file, proc_cpuset_show, pid);
402}
403
404static const struct file_operations proc_cpuset_operations = {
405 .open = cpuset_open,
406 .read = seq_read,
407 .llseek = seq_lseek,
408 .release = single_release,
409};
410#endif
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412static int proc_oom_score(struct task_struct *task, char *buffer)
413{
David Rientjesa7f638f2012-05-29 15:06:47 -0700414 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200415 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700417 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200418 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700419 points = oom_badness(task, NULL, NULL, totalpages) *
420 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700421 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return sprintf(buffer, "%lu\n", points);
423}
424
Neil Hormand85f50d2007-10-18 23:40:37 -0700425struct limit_names {
Alexey Dobriyancedbcca2014-08-08 14:21:33 -0700426 const char *name;
427 const char *unit;
Neil Hormand85f50d2007-10-18 23:40:37 -0700428};
429
430static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700431 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700432 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
433 [RLIMIT_DATA] = {"Max data size", "bytes"},
434 [RLIMIT_STACK] = {"Max stack size", "bytes"},
435 [RLIMIT_CORE] = {"Max core file size", "bytes"},
436 [RLIMIT_RSS] = {"Max resident set", "bytes"},
437 [RLIMIT_NPROC] = {"Max processes", "processes"},
438 [RLIMIT_NOFILE] = {"Max open files", "files"},
439 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
440 [RLIMIT_AS] = {"Max address space", "bytes"},
441 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
442 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
443 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
444 [RLIMIT_NICE] = {"Max nice priority", NULL},
445 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800446 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700447};
448
449/* Display limits for a process */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700450static int proc_pid_limits(struct seq_file *m, struct pid_namespace *ns,
451 struct pid *pid, struct task_struct *task)
Neil Hormand85f50d2007-10-18 23:40:37 -0700452{
453 unsigned int i;
Neil Hormand85f50d2007-10-18 23:40:37 -0700454 unsigned long flags;
Neil Hormand85f50d2007-10-18 23:40:37 -0700455
456 struct rlimit rlim[RLIM_NLIMITS];
457
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400458 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700459 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700460 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
461 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700462
463 /*
464 * print the file header
465 */
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700466 seq_printf(m, "%-25s %-20s %-20s %-10s\n",
Neil Hormand85f50d2007-10-18 23:40:37 -0700467 "Limit", "Soft Limit", "Hard Limit", "Units");
468
469 for (i = 0; i < RLIM_NLIMITS; i++) {
470 if (rlim[i].rlim_cur == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700471 seq_printf(m, "%-25s %-20s ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700472 lnames[i].name, "unlimited");
473 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700474 seq_printf(m, "%-25s %-20lu ",
Neil Hormand85f50d2007-10-18 23:40:37 -0700475 lnames[i].name, rlim[i].rlim_cur);
476
477 if (rlim[i].rlim_max == RLIM_INFINITY)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700478 seq_printf(m, "%-20s ", "unlimited");
Neil Hormand85f50d2007-10-18 23:40:37 -0700479 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700480 seq_printf(m, "%-20lu ", rlim[i].rlim_max);
Neil Hormand85f50d2007-10-18 23:40:37 -0700481
482 if (lnames[i].unit)
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700483 seq_printf(m, "%-10s\n", lnames[i].unit);
Neil Hormand85f50d2007-10-18 23:40:37 -0700484 else
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700485 seq_putc(m, '\n');
Neil Hormand85f50d2007-10-18 23:40:37 -0700486 }
487
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -0700488 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700489}
490
Roland McGrathebcb6732008-07-25 19:46:00 -0700491#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700492static int proc_pid_syscall(struct seq_file *m, struct pid_namespace *ns,
493 struct pid *pid, struct task_struct *task)
Roland McGrathebcb6732008-07-25 19:46:00 -0700494{
495 long nr;
496 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400497 int res = lock_trace(task);
498 if (res)
499 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700500
501 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700502 seq_puts(m, "running\n");
Al Viroa9712bc2011-03-23 15:52:50 -0400503 else if (nr < 0)
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700504 seq_printf(m, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400505 else
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -0700506 seq_printf(m,
Roland McGrathebcb6732008-07-25 19:46:00 -0700507 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
508 nr,
509 args[0], args[1], args[2], args[3], args[4], args[5],
510 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400511 unlock_trace(task);
512 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700513}
514#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/************************************************************************/
517/* Here the fs part begins */
518/************************************************************************/
519
520/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700521static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700523 struct task_struct *task;
524 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700525 /* Allow access to a task's file descriptors if it is us or we
526 * may use ptrace attach to the process and find out that
527 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700528 */
529 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700530 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400531 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700532 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700533 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700534 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800537int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700538{
539 int error;
540 struct inode *inode = dentry->d_inode;
541
542 if (attr->ia_valid & ATTR_MODE)
543 return -EPERM;
544
545 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200546 if (error)
547 return error;
548
Christoph Hellwig10257742010-06-04 11:30:02 +0200549 setattr_copy(inode, attr);
550 mark_inode_dirty(inode);
551 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700552}
553
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800554/*
555 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
556 * or euid/egid (for hide_pid_min=2)?
557 */
558static bool has_pid_permissions(struct pid_namespace *pid,
559 struct task_struct *task,
560 int hide_pid_min)
561{
562 if (pid->hide_pid < hide_pid_min)
563 return true;
564 if (in_group_p(pid->pid_gid))
565 return true;
566 return ptrace_may_access(task, PTRACE_MODE_READ);
567}
568
569
570static int proc_pid_permission(struct inode *inode, int mask)
571{
572 struct pid_namespace *pid = inode->i_sb->s_fs_info;
573 struct task_struct *task;
574 bool has_perms;
575
576 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800577 if (!task)
578 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800579 has_perms = has_pid_permissions(pid, task, 1);
580 put_task_struct(task);
581
582 if (!has_perms) {
583 if (pid->hide_pid == 2) {
584 /*
585 * Let's make getdents(), stat(), and open()
586 * consistent with each other. If a process
587 * may not stat() a file, it shouldn't be seen
588 * in procfs at all.
589 */
590 return -ENOENT;
591 }
592
593 return -EPERM;
594 }
595 return generic_permission(inode, mask);
596}
597
598
599
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800600static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700601 .setattr = proc_setattr,
602};
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
605
606static ssize_t proc_info_read(struct file * file, char __user * buf,
607 size_t count, loff_t *ppos)
608{
Al Viro496ad9a2013-01-23 17:07:38 -0500609 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 unsigned long page;
611 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700612 struct task_struct *task = get_proc_task(inode);
613
614 length = -ESRCH;
615 if (!task)
616 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (count > PROC_BLOCK_SIZE)
619 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700620
621 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700622 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700623 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 length = PROC_I(inode)->op.proc_read(task, (char*)page);
626
627 if (length >= 0)
628 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
629 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700630out:
631 put_task_struct(task);
632out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return length;
634}
635
Arjan van de Ven00977a52007-02-12 00:55:34 -0800636static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100638 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639};
640
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800641static int proc_single_show(struct seq_file *m, void *v)
642{
643 struct inode *inode = m->private;
644 struct pid_namespace *ns;
645 struct pid *pid;
646 struct task_struct *task;
647 int ret;
648
649 ns = inode->i_sb->s_fs_info;
650 pid = proc_pid(inode);
651 task = get_pid_task(pid, PIDTYPE_PID);
652 if (!task)
653 return -ESRCH;
654
655 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
656
657 put_task_struct(task);
658 return ret;
659}
660
661static int proc_single_open(struct inode *inode, struct file *filp)
662{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800663 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800664}
665
666static const struct file_operations proc_single_file_operations = {
667 .open = proc_single_open,
668 .read = seq_read,
669 .llseek = seq_lseek,
670 .release = single_release,
671};
672
Cong Wangb409e572012-05-31 16:26:17 -0700673static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Al Viro496ad9a2013-01-23 17:07:38 -0500675 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800676 struct mm_struct *mm;
677
678 if (!task)
679 return -ESRCH;
680
Cong Wangb409e572012-05-31 16:26:17 -0700681 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800682 put_task_struct(task);
683
684 if (IS_ERR(mm))
685 return PTR_ERR(mm);
686
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100687 if (mm) {
688 /* ensure this mm_struct can't be freed */
689 atomic_inc(&mm->mm_count);
690 /* but do not pin its memory */
691 mmput(mm);
692 }
693
Linus Torvaldse2683372012-01-17 15:21:19 -0800694 file->private_data = mm;
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697}
698
Cong Wangb409e572012-05-31 16:26:17 -0700699static int mem_open(struct inode *inode, struct file *file)
700{
Djalal Harounibc452b42012-07-30 14:42:28 -0700701 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
702
703 /* OK to pass negative loff_t, we can catch out-of-range */
704 file->f_mode |= FMODE_UNSIGNED_OFFSET;
705
706 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700707}
708
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100709static ssize_t mem_rw(struct file *file, char __user *buf,
710 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Linus Torvaldse2683372012-01-17 15:21:19 -0800712 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100713 unsigned long addr = *ppos;
714 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Linus Torvaldse2683372012-01-17 15:21:19 -0800717 if (!mm)
718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Mel Gormane12ba742007-10-16 01:25:52 -0700720 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800722 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700724 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100725 if (!atomic_inc_not_zero(&mm->mm_users))
726 goto free;
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100729 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100731 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 copied = -EFAULT;
733 break;
734 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100735
736 this_len = access_remote_vm(mm, addr, page, this_len, write);
737 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (!copied)
739 copied = -EIO;
740 break;
741 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100742
743 if (!write && copy_to_user(buf, page, this_len)) {
744 copied = -EFAULT;
745 break;
746 }
747
748 buf += this_len;
749 addr += this_len;
750 copied += this_len;
751 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100753 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700754
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100755 mmput(mm);
756free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700757 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return copied;
759}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100761static ssize_t mem_read(struct file *file, char __user *buf,
762 size_t count, loff_t *ppos)
763{
764 return mem_rw(file, buf, count, ppos, 0);
765}
766
767static ssize_t mem_write(struct file *file, const char __user *buf,
768 size_t count, loff_t *ppos)
769{
770 return mem_rw(file, (char __user*)buf, count, ppos, 1);
771}
772
Matt Mackall85863e42008-02-04 22:29:04 -0800773loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 switch (orig) {
776 case 0:
777 file->f_pos = offset;
778 break;
779 case 1:
780 file->f_pos += offset;
781 break;
782 default:
783 return -EINVAL;
784 }
785 force_successful_syscall_return();
786 return file->f_pos;
787}
788
Linus Torvaldse2683372012-01-17 15:21:19 -0800789static int mem_release(struct inode *inode, struct file *file)
790{
791 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100792 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100793 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800794 return 0;
795}
796
Arjan van de Ven00977a52007-02-12 00:55:34 -0800797static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 .llseek = mem_lseek,
799 .read = mem_read,
800 .write = mem_write,
801 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800802 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803};
804
Cong Wangb409e572012-05-31 16:26:17 -0700805static int environ_open(struct inode *inode, struct file *file)
806{
807 return __mem_open(inode, file, PTRACE_MODE_READ);
808}
809
James Pearson315e28c2007-10-16 23:30:17 -0700810static ssize_t environ_read(struct file *file, char __user *buf,
811 size_t count, loff_t *ppos)
812{
James Pearson315e28c2007-10-16 23:30:17 -0700813 char *page;
814 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700815 int ret = 0;
816 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700817
Cong Wangb409e572012-05-31 16:26:17 -0700818 if (!mm)
819 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700820
James Pearson315e28c2007-10-16 23:30:17 -0700821 page = (char *)__get_free_page(GFP_TEMPORARY);
822 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700823 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700824
Al Virod6f64b82011-02-15 22:26:01 -0500825 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700826 if (!atomic_inc_not_zero(&mm->mm_users))
827 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700828 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700829 size_t this_len, max_len;
830 int retval;
831
832 if (src >= (mm->env_end - mm->env_start))
833 break;
James Pearson315e28c2007-10-16 23:30:17 -0700834
835 this_len = mm->env_end - (mm->env_start + src);
836
Djalal Harounie8905ec2012-07-30 14:42:26 -0700837 max_len = min_t(size_t, PAGE_SIZE, count);
838 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700839
Cong Wangb409e572012-05-31 16:26:17 -0700840 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700841 page, this_len, 0);
842
843 if (retval <= 0) {
844 ret = retval;
845 break;
846 }
847
848 if (copy_to_user(buf, page, retval)) {
849 ret = -EFAULT;
850 break;
851 }
852
853 ret += retval;
854 src += retval;
855 buf += retval;
856 count -= retval;
857 }
858 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700859 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700860
861free:
James Pearson315e28c2007-10-16 23:30:17 -0700862 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700863 return ret;
864}
865
866static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700867 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700868 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100869 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700870 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700871};
872
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800873static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
874 loff_t *ppos)
875{
Al Viro496ad9a2013-01-23 17:07:38 -0500876 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800877 char buffer[PROC_NUMBUF];
878 int oom_adj = OOM_ADJUST_MIN;
879 size_t len;
880 unsigned long flags;
881
882 if (!task)
883 return -ESRCH;
884 if (lock_task_sighand(task, &flags)) {
885 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
886 oom_adj = OOM_ADJUST_MAX;
887 else
888 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
889 OOM_SCORE_ADJ_MAX;
890 unlock_task_sighand(task, &flags);
891 }
892 put_task_struct(task);
893 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
894 return simple_read_from_buffer(buf, count, ppos, buffer, len);
895}
896
897static ssize_t oom_adj_write(struct file *file, const char __user *buf,
898 size_t count, loff_t *ppos)
899{
900 struct task_struct *task;
901 char buffer[PROC_NUMBUF];
902 int oom_adj;
903 unsigned long flags;
904 int err;
905
906 memset(buffer, 0, sizeof(buffer));
907 if (count > sizeof(buffer) - 1)
908 count = sizeof(buffer) - 1;
909 if (copy_from_user(buffer, buf, count)) {
910 err = -EFAULT;
911 goto out;
912 }
913
914 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
915 if (err)
916 goto out;
917 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
918 oom_adj != OOM_DISABLE) {
919 err = -EINVAL;
920 goto out;
921 }
922
Al Viro496ad9a2013-01-23 17:07:38 -0500923 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800924 if (!task) {
925 err = -ESRCH;
926 goto out;
927 }
928
929 task_lock(task);
930 if (!task->mm) {
931 err = -EINVAL;
932 goto err_task_lock;
933 }
934
935 if (!lock_task_sighand(task, &flags)) {
936 err = -ESRCH;
937 goto err_task_lock;
938 }
939
940 /*
941 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
942 * value is always attainable.
943 */
944 if (oom_adj == OOM_ADJUST_MAX)
945 oom_adj = OOM_SCORE_ADJ_MAX;
946 else
947 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
948
949 if (oom_adj < task->signal->oom_score_adj &&
950 !capable(CAP_SYS_RESOURCE)) {
951 err = -EACCES;
952 goto err_sighand;
953 }
954
955 /*
956 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
957 * /proc/pid/oom_score_adj instead.
958 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800959 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 -0800960 current->comm, task_pid_nr(current), task_pid_nr(task),
961 task_pid_nr(task));
962
963 task->signal->oom_score_adj = oom_adj;
964 trace_oom_score_adj_update(task);
965err_sighand:
966 unlock_task_sighand(task, &flags);
967err_task_lock:
968 task_unlock(task);
969 put_task_struct(task);
970out:
971 return err < 0 ? err : count;
972}
973
974static const struct file_operations proc_oom_adj_operations = {
975 .read = oom_adj_read,
976 .write = oom_adj_write,
977 .llseek = generic_file_llseek,
978};
979
David Rientjesa63d83f2010-08-09 17:19:46 -0700980static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
981 size_t count, loff_t *ppos)
982{
Al Viro496ad9a2013-01-23 17:07:38 -0500983 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700984 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800985 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700986 unsigned long flags;
987 size_t len;
988
989 if (!task)
990 return -ESRCH;
991 if (lock_task_sighand(task, &flags)) {
992 oom_score_adj = task->signal->oom_score_adj;
993 unlock_task_sighand(task, &flags);
994 }
995 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800996 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700997 return simple_read_from_buffer(buf, count, ppos, buffer, len);
998}
999
1000static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1001 size_t count, loff_t *ppos)
1002{
1003 struct task_struct *task;
1004 char buffer[PROC_NUMBUF];
1005 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001006 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001007 int err;
1008
1009 memset(buffer, 0, sizeof(buffer));
1010 if (count > sizeof(buffer) - 1)
1011 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001012 if (copy_from_user(buffer, buf, count)) {
1013 err = -EFAULT;
1014 goto out;
1015 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001016
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001017 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001018 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001019 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001020 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001021 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1022 err = -EINVAL;
1023 goto out;
1024 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001025
Al Viro496ad9a2013-01-23 17:07:38 -05001026 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001027 if (!task) {
1028 err = -ESRCH;
1029 goto out;
1030 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001031
Ying Han3d5992d2010-10-26 14:21:23 -07001032 task_lock(task);
1033 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001034 err = -EINVAL;
1035 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001036 }
David Rientjesd19d5472010-10-26 14:21:26 -07001037
1038 if (!lock_task_sighand(task, &flags)) {
1039 err = -ESRCH;
1040 goto err_task_lock;
1041 }
1042
David Rientjesa9c58b902012-12-11 16:02:54 -08001043 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001044 !capable(CAP_SYS_RESOURCE)) {
1045 err = -EACCES;
1046 goto err_sighand;
1047 }
1048
David Rientjesa9c58b902012-12-11 16:02:54 -08001049 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001050 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001051 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001052 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001053
David Rientjes723548b2010-10-26 14:21:25 -07001054err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001055 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001056err_task_lock:
1057 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001058 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001059out:
1060 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001061}
1062
1063static const struct file_operations proc_oom_score_adj_operations = {
1064 .read = oom_score_adj_read,
1065 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001066 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001067};
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069#ifdef CONFIG_AUDITSYSCALL
1070#define TMPBUFLEN 21
1071static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1072 size_t count, loff_t *ppos)
1073{
Al Viro496ad9a2013-01-23 17:07:38 -05001074 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001075 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ssize_t length;
1077 char tmpbuf[TMPBUFLEN];
1078
Eric W. Biederman99f89552006-06-26 00:25:55 -07001079 if (!task)
1080 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001082 from_kuid(file->f_cred->user_ns,
1083 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001084 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1086}
1087
1088static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1089 size_t count, loff_t *ppos)
1090{
Al Viro496ad9a2013-01-23 17:07:38 -05001091 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 char *page, *tmp;
1093 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001095 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001097 rcu_read_lock();
1098 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1099 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001101 }
1102 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Al Viroe0182902006-05-18 08:28:02 -04001104 if (count >= PAGE_SIZE)
1105 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 if (*ppos != 0) {
1108 /* No partial writes. */
1109 return -EINVAL;
1110 }
Mel Gormane12ba742007-10-16 01:25:52 -07001111 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!page)
1113 return -ENOMEM;
1114 length = -EFAULT;
1115 if (copy_from_user(page, buf, count))
1116 goto out_free_page;
1117
Al Viroe0182902006-05-18 08:28:02 -04001118 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 loginuid = simple_strtoul(page, &tmp, 10);
1120 if (tmp == page) {
1121 length = -EINVAL;
1122 goto out_free_page;
1123
1124 }
Eric Paris81407c82013-05-24 09:49:14 -04001125
1126 /* is userspace tring to explicitly UNSET the loginuid? */
1127 if (loginuid == AUDIT_UID_UNSET) {
1128 kloginuid = INVALID_UID;
1129 } else {
1130 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1131 if (!uid_valid(kloginuid)) {
1132 length = -EINVAL;
1133 goto out_free_page;
1134 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001135 }
1136
1137 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (likely(length == 0))
1139 length = count;
1140
1141out_free_page:
1142 free_page((unsigned long) page);
1143 return length;
1144}
1145
Arjan van de Ven00977a52007-02-12 00:55:34 -08001146static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 .read = proc_loginuid_read,
1148 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001149 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150};
Eric Paris1e0bd752008-03-13 08:15:31 -04001151
1152static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1153 size_t count, loff_t *ppos)
1154{
Al Viro496ad9a2013-01-23 17:07:38 -05001155 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001156 struct task_struct *task = get_proc_task(inode);
1157 ssize_t length;
1158 char tmpbuf[TMPBUFLEN];
1159
1160 if (!task)
1161 return -ESRCH;
1162 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1163 audit_get_sessionid(task));
1164 put_task_struct(task);
1165 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1166}
1167
1168static const struct file_operations proc_sessionid_operations = {
1169 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001170 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001171};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172#endif
1173
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001174#ifdef CONFIG_FAULT_INJECTION
1175static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1176 size_t count, loff_t *ppos)
1177{
Al Viro496ad9a2013-01-23 17:07:38 -05001178 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001179 char buffer[PROC_NUMBUF];
1180 size_t len;
1181 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001182
1183 if (!task)
1184 return -ESRCH;
1185 make_it_fail = task->make_it_fail;
1186 put_task_struct(task);
1187
1188 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001189
1190 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001191}
1192
1193static ssize_t proc_fault_inject_write(struct file * file,
1194 const char __user * buf, size_t count, loff_t *ppos)
1195{
1196 struct task_struct *task;
1197 char buffer[PROC_NUMBUF], *end;
1198 int make_it_fail;
1199
1200 if (!capable(CAP_SYS_RESOURCE))
1201 return -EPERM;
1202 memset(buffer, 0, sizeof(buffer));
1203 if (count > sizeof(buffer) - 1)
1204 count = sizeof(buffer) - 1;
1205 if (copy_from_user(buffer, buf, count))
1206 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001207 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1208 if (*end)
1209 return -EINVAL;
Dave Jones16caed32014-04-07 15:39:15 -07001210 if (make_it_fail < 0 || make_it_fail > 1)
1211 return -EINVAL;
1212
Al Viro496ad9a2013-01-23 17:07:38 -05001213 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001214 if (!task)
1215 return -ESRCH;
1216 task->make_it_fail = make_it_fail;
1217 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001218
1219 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001220}
1221
Arjan van de Ven00977a52007-02-12 00:55:34 -08001222static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001223 .read = proc_fault_inject_read,
1224 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001225 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001226};
1227#endif
1228
Arjan van de Ven97455122008-01-25 21:08:34 +01001229
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001230#ifdef CONFIG_SCHED_DEBUG
1231/*
1232 * Print out various scheduling related per-task fields:
1233 */
1234static int sched_show(struct seq_file *m, void *v)
1235{
1236 struct inode *inode = m->private;
1237 struct task_struct *p;
1238
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001239 p = get_proc_task(inode);
1240 if (!p)
1241 return -ESRCH;
1242 proc_sched_show_task(p, m);
1243
1244 put_task_struct(p);
1245
1246 return 0;
1247}
1248
1249static ssize_t
1250sched_write(struct file *file, const char __user *buf,
1251 size_t count, loff_t *offset)
1252{
Al Viro496ad9a2013-01-23 17:07:38 -05001253 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001254 struct task_struct *p;
1255
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001256 p = get_proc_task(inode);
1257 if (!p)
1258 return -ESRCH;
1259 proc_sched_set_task(p);
1260
1261 put_task_struct(p);
1262
1263 return count;
1264}
1265
1266static int sched_open(struct inode *inode, struct file *filp)
1267{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001268 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001269}
1270
1271static const struct file_operations proc_pid_sched_operations = {
1272 .open = sched_open,
1273 .read = seq_read,
1274 .write = sched_write,
1275 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001276 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001277};
1278
1279#endif
1280
Mike Galbraith5091faa2010-11-30 14:18:03 +01001281#ifdef CONFIG_SCHED_AUTOGROUP
1282/*
1283 * Print out autogroup related information:
1284 */
1285static int sched_autogroup_show(struct seq_file *m, void *v)
1286{
1287 struct inode *inode = m->private;
1288 struct task_struct *p;
1289
1290 p = get_proc_task(inode);
1291 if (!p)
1292 return -ESRCH;
1293 proc_sched_autogroup_show_task(p, m);
1294
1295 put_task_struct(p);
1296
1297 return 0;
1298}
1299
1300static ssize_t
1301sched_autogroup_write(struct file *file, const char __user *buf,
1302 size_t count, loff_t *offset)
1303{
Al Viro496ad9a2013-01-23 17:07:38 -05001304 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001305 struct task_struct *p;
1306 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001307 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001308 int err;
1309
1310 memset(buffer, 0, sizeof(buffer));
1311 if (count > sizeof(buffer) - 1)
1312 count = sizeof(buffer) - 1;
1313 if (copy_from_user(buffer, buf, count))
1314 return -EFAULT;
1315
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001316 err = kstrtoint(strstrip(buffer), 0, &nice);
1317 if (err < 0)
1318 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001319
1320 p = get_proc_task(inode);
1321 if (!p)
1322 return -ESRCH;
1323
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001324 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001325 if (err)
1326 count = err;
1327
1328 put_task_struct(p);
1329
1330 return count;
1331}
1332
1333static int sched_autogroup_open(struct inode *inode, struct file *filp)
1334{
1335 int ret;
1336
1337 ret = single_open(filp, sched_autogroup_show, NULL);
1338 if (!ret) {
1339 struct seq_file *m = filp->private_data;
1340
1341 m->private = inode;
1342 }
1343 return ret;
1344}
1345
1346static const struct file_operations proc_pid_sched_autogroup_operations = {
1347 .open = sched_autogroup_open,
1348 .read = seq_read,
1349 .write = sched_autogroup_write,
1350 .llseek = seq_lseek,
1351 .release = single_release,
1352};
1353
1354#endif /* CONFIG_SCHED_AUTOGROUP */
1355
john stultz4614a696b2009-12-14 18:00:05 -08001356static ssize_t comm_write(struct file *file, const char __user *buf,
1357 size_t count, loff_t *offset)
1358{
Al Viro496ad9a2013-01-23 17:07:38 -05001359 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001360 struct task_struct *p;
1361 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001362 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001363
1364 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001365 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001366 return -EFAULT;
1367
1368 p = get_proc_task(inode);
1369 if (!p)
1370 return -ESRCH;
1371
1372 if (same_thread_group(current, p))
1373 set_task_comm(p, buffer);
1374 else
1375 count = -EINVAL;
1376
1377 put_task_struct(p);
1378
1379 return count;
1380}
1381
1382static int comm_show(struct seq_file *m, void *v)
1383{
1384 struct inode *inode = m->private;
1385 struct task_struct *p;
1386
1387 p = get_proc_task(inode);
1388 if (!p)
1389 return -ESRCH;
1390
1391 task_lock(p);
1392 seq_printf(m, "%s\n", p->comm);
1393 task_unlock(p);
1394
1395 put_task_struct(p);
1396
1397 return 0;
1398}
1399
1400static int comm_open(struct inode *inode, struct file *filp)
1401{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001402 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001403}
1404
1405static const struct file_operations proc_pid_set_comm_operations = {
1406 .open = comm_open,
1407 .read = seq_read,
1408 .write = comm_write,
1409 .llseek = seq_lseek,
1410 .release = single_release,
1411};
1412
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001413static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001414{
1415 struct task_struct *task;
1416 struct mm_struct *mm;
1417 struct file *exe_file;
1418
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001419 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001420 if (!task)
1421 return -ENOENT;
1422 mm = get_task_mm(task);
1423 put_task_struct(task);
1424 if (!mm)
1425 return -ENOENT;
1426 exe_file = get_mm_exe_file(mm);
1427 mmput(mm);
1428 if (exe_file) {
1429 *exe_path = exe_file->f_path;
1430 path_get(&exe_file->f_path);
1431 fput(exe_file);
1432 return 0;
1433 } else
1434 return -ENOENT;
1435}
1436
Al Viro008b1502005-08-20 00:17:39 +01001437static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001440 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 int error = -EACCES;
1442
Eric W. Biederman778c1142006-06-26 00:25:58 -07001443 /* Are we allowed to snoop on the tasks file descriptors? */
1444 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Christoph Hellwig408ef012012-06-18 10:47:03 -04001447 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1448 if (error)
1449 goto out;
1450
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001451 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001452 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453out:
Al Viro008b1502005-08-20 00:17:39 +01001454 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001457static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Mel Gormane12ba742007-10-16 01:25:52 -07001459 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001460 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int len;
1462
1463 if (!tmp)
1464 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001465
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001466 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001467 len = PTR_ERR(pathname);
1468 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001470 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (len > buflen)
1473 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001474 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 len = -EFAULT;
1476 out:
1477 free_page((unsigned long)tmp);
1478 return len;
1479}
1480
1481static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1482{
1483 int error = -EACCES;
1484 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001485 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Eric W. Biederman778c1142006-06-26 00:25:58 -07001487 /* Are we allowed to snoop on the tasks file descriptors? */
1488 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001491 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (error)
1493 goto out;
1494
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001495 error = do_proc_readlink(&path, buffer, buflen);
1496 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return error;
1499}
1500
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001501const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001503 .follow_link = proc_pid_follow_link,
1504 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505};
1506
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001507
1508/* building an inode */
1509
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001510struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001511{
1512 struct inode * inode;
1513 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001514 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001515
1516 /* We need a new inode */
1517
1518 inode = new_inode(sb);
1519 if (!inode)
1520 goto out;
1521
1522 /* Common stuff */
1523 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001524 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001525 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001526 inode->i_op = &proc_def_inode_operations;
1527
1528 /*
1529 * grab the reference to task.
1530 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001531 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001532 if (!ei->pid)
1533 goto out_unlock;
1534
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001535 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001536 rcu_read_lock();
1537 cred = __task_cred(task);
1538 inode->i_uid = cred->euid;
1539 inode->i_gid = cred->egid;
1540 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001541 }
1542 security_task_to_inode(task, inode);
1543
1544out:
1545 return inode;
1546
1547out_unlock:
1548 iput(inode);
1549 return NULL;
1550}
1551
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001552int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001553{
1554 struct inode *inode = dentry->d_inode;
1555 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001556 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001557 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001558
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001559 generic_fillattr(inode, stat);
1560
1561 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001562 stat->uid = GLOBAL_ROOT_UID;
1563 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001564 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1565 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001566 if (!has_pid_permissions(pid, task, 2)) {
1567 rcu_read_unlock();
1568 /*
1569 * This doesn't prevent learning whether PID exists,
1570 * it only makes getattr() consistent with readdir().
1571 */
1572 return -ENOENT;
1573 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001574 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1575 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001576 cred = __task_cred(task);
1577 stat->uid = cred->euid;
1578 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001579 }
1580 }
1581 rcu_read_unlock();
1582 return 0;
1583}
1584
1585/* dentry stuff */
1586
1587/*
1588 * Exceptional case: normally we are not allowed to unhash a busy
1589 * directory. In this case, however, we can do it - no aliasing problems
1590 * due to the way we treat inodes.
1591 *
1592 * Rewrite the inode's ownerships here because the owning task may have
1593 * performed a setuid(), etc.
1594 *
1595 * Before the /proc/pid/status file was created the only way to read
1596 * the effective uid of a /process was to stat /proc/pid. Reading
1597 * /proc/pid/status is slow enough that procps and other packages
1598 * kept stating /proc/pid. To keep the rules in /proc simple I have
1599 * made this apply to all per process world readable and executable
1600 * directories.
1601 */
Al Viro0b728e12012-06-10 16:03:43 -04001602int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001603{
Nick Piggin34286d62011-01-07 17:49:57 +11001604 struct inode *inode;
1605 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001606 const struct cred *cred;
1607
Al Viro0b728e12012-06-10 16:03:43 -04001608 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001609 return -ECHILD;
1610
1611 inode = dentry->d_inode;
1612 task = get_proc_task(inode);
1613
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614 if (task) {
1615 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1616 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001617 rcu_read_lock();
1618 cred = __task_cred(task);
1619 inode->i_uid = cred->euid;
1620 inode->i_gid = cred->egid;
1621 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001622 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001623 inode->i_uid = GLOBAL_ROOT_UID;
1624 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001625 }
1626 inode->i_mode &= ~(S_ISUID | S_ISGID);
1627 security_task_to_inode(task, inode);
1628 put_task_struct(task);
1629 return 1;
1630 }
1631 d_drop(dentry);
1632 return 0;
1633}
1634
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001635static inline bool proc_inode_is_dead(struct inode *inode)
1636{
1637 return !proc_pid(inode)->tasks[PIDTYPE_PID].first;
1638}
1639
David Howells1dd704b2013-04-12 01:08:50 +01001640int pid_delete_dentry(const struct dentry *dentry)
1641{
1642 /* Is the task we represent dead?
1643 * If so, then don't put the dentry on the lru list,
1644 * kill it immediately.
1645 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08001646 return proc_inode_is_dead(dentry->d_inode);
David Howells1dd704b2013-04-12 01:08:50 +01001647}
1648
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001649const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001650{
1651 .d_revalidate = pid_revalidate,
1652 .d_delete = pid_delete_dentry,
1653};
1654
1655/* Lookups */
1656
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001657/*
1658 * Fill a directory entry.
1659 *
1660 * If possible create the dcache entry and derive our inode number and
1661 * file type from dcache entry.
1662 *
1663 * Since all of the proc inode numbers are dynamically generated, the inode
1664 * numbers do not exist until the inode is cache. This means creating the
1665 * the dcache entry in readdir is necessary to keep the inode numbers
1666 * reported by readdir in sync with the inode numbers reported
1667 * by stat.
1668 */
Al Virof0c3b502013-05-16 12:07:31 -04001669bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001670 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001671 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001672{
Al Virof0c3b502013-05-16 12:07:31 -04001673 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001674 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001675 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001676 unsigned type;
1677 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001678
Al Viro1df98b82013-06-15 11:33:10 +04001679 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001680 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001681 child = d_alloc(dir, &qname);
1682 if (!child)
1683 goto end_instantiate;
1684 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1685 dput(child);
1686 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001687 }
1688 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001689 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001690 ino = inode->i_ino;
1691 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001692 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001693 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001694
1695end_instantiate:
1696 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001697}
1698
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001699#ifdef CONFIG_CHECKPOINT_RESTORE
1700
1701/*
1702 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1703 * which represent vma start and end addresses.
1704 */
1705static int dname_to_vma_addr(struct dentry *dentry,
1706 unsigned long *start, unsigned long *end)
1707{
1708 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1709 return -EINVAL;
1710
1711 return 0;
1712}
1713
Al Viro0b728e12012-06-10 16:03:43 -04001714static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001715{
1716 unsigned long vm_start, vm_end;
1717 bool exact_vma_exists = false;
1718 struct mm_struct *mm = NULL;
1719 struct task_struct *task;
1720 const struct cred *cred;
1721 struct inode *inode;
1722 int status = 0;
1723
Al Viro0b728e12012-06-10 16:03:43 -04001724 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001725 return -ECHILD;
1726
1727 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001728 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001729 goto out_notask;
1730 }
1731
1732 inode = dentry->d_inode;
1733 task = get_proc_task(inode);
1734 if (!task)
1735 goto out_notask;
1736
Cong Wang2344bec2012-05-31 16:26:18 -07001737 mm = mm_access(task, PTRACE_MODE_READ);
1738 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001739 goto out;
1740
1741 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1742 down_read(&mm->mmap_sem);
1743 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1744 up_read(&mm->mmap_sem);
1745 }
1746
1747 mmput(mm);
1748
1749 if (exact_vma_exists) {
1750 if (task_dumpable(task)) {
1751 rcu_read_lock();
1752 cred = __task_cred(task);
1753 inode->i_uid = cred->euid;
1754 inode->i_gid = cred->egid;
1755 rcu_read_unlock();
1756 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001757 inode->i_uid = GLOBAL_ROOT_UID;
1758 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001759 }
1760 security_task_to_inode(task, inode);
1761 status = 1;
1762 }
1763
1764out:
1765 put_task_struct(task);
1766
1767out_notask:
1768 if (status <= 0)
1769 d_drop(dentry);
1770
1771 return status;
1772}
1773
1774static const struct dentry_operations tid_map_files_dentry_operations = {
1775 .d_revalidate = map_files_d_revalidate,
1776 .d_delete = pid_delete_dentry,
1777};
1778
1779static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1780{
1781 unsigned long vm_start, vm_end;
1782 struct vm_area_struct *vma;
1783 struct task_struct *task;
1784 struct mm_struct *mm;
1785 int rc;
1786
1787 rc = -ENOENT;
1788 task = get_proc_task(dentry->d_inode);
1789 if (!task)
1790 goto out;
1791
1792 mm = get_task_mm(task);
1793 put_task_struct(task);
1794 if (!mm)
1795 goto out;
1796
1797 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1798 if (rc)
1799 goto out_mmput;
1800
Artem Fetishev70335ab2014-03-10 15:49:45 -07001801 rc = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001802 down_read(&mm->mmap_sem);
1803 vma = find_exact_vma(mm, vm_start, vm_end);
1804 if (vma && vma->vm_file) {
1805 *path = vma->vm_file->f_path;
1806 path_get(path);
1807 rc = 0;
1808 }
1809 up_read(&mm->mmap_sem);
1810
1811out_mmput:
1812 mmput(mm);
1813out:
1814 return rc;
1815}
1816
1817struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001818 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001819 unsigned long len;
1820 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1821};
1822
Al Viroc52a47a2013-06-15 11:15:20 +04001823static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001824proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1825 struct task_struct *task, const void *ptr)
1826{
Al Viro7b540d02012-08-27 14:55:26 -04001827 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001828 struct proc_inode *ei;
1829 struct inode *inode;
1830
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001831 inode = proc_pid_make_inode(dir->i_sb, task);
1832 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001833 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001834
1835 ei = PROC_I(inode);
1836 ei->op.proc_get_link = proc_map_files_get_link;
1837
1838 inode->i_op = &proc_pid_link_inode_operations;
1839 inode->i_size = 64;
1840 inode->i_mode = S_IFLNK;
1841
Al Viro7b540d02012-08-27 14:55:26 -04001842 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001843 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001844 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001845 inode->i_mode |= S_IWUSR;
1846
1847 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1848 d_add(dentry, inode);
1849
Al Viroc52a47a2013-06-15 11:15:20 +04001850 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001851}
1852
1853static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001854 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001855{
1856 unsigned long vm_start, vm_end;
1857 struct vm_area_struct *vma;
1858 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001859 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001860 struct mm_struct *mm;
1861
Al Viroc52a47a2013-06-15 11:15:20 +04001862 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001863 if (!capable(CAP_SYS_ADMIN))
1864 goto out;
1865
Al Viroc52a47a2013-06-15 11:15:20 +04001866 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001867 task = get_proc_task(dir);
1868 if (!task)
1869 goto out;
1870
Al Viroc52a47a2013-06-15 11:15:20 +04001871 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001872 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001873 goto out_put_task;
1874
Al Viroc52a47a2013-06-15 11:15:20 +04001875 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001876 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001877 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001878
1879 mm = get_task_mm(task);
1880 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001881 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001882
1883 down_read(&mm->mmap_sem);
1884 vma = find_exact_vma(mm, vm_start, vm_end);
1885 if (!vma)
1886 goto out_no_vma;
1887
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001888 if (vma->vm_file)
1889 result = proc_map_files_instantiate(dir, dentry, task,
1890 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001891
1892out_no_vma:
1893 up_read(&mm->mmap_sem);
1894 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001895out_put_task:
1896 put_task_struct(task);
1897out:
Al Viroc52a47a2013-06-15 11:15:20 +04001898 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001899}
1900
1901static const struct inode_operations proc_map_files_inode_operations = {
1902 .lookup = proc_map_files_lookup,
1903 .permission = proc_fd_permission,
1904 .setattr = proc_setattr,
1905};
1906
1907static int
Al Virof0c3b502013-05-16 12:07:31 -04001908proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001909{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001910 struct vm_area_struct *vma;
1911 struct task_struct *task;
1912 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001913 unsigned long nr_files, pos, i;
1914 struct flex_array *fa = NULL;
1915 struct map_files_info info;
1916 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001917 int ret;
1918
Zhao Hongjiang41735812013-02-20 13:13:55 +11001919 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001920 if (!capable(CAP_SYS_ADMIN))
1921 goto out;
1922
1923 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001924 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001925 if (!task)
1926 goto out;
1927
1928 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001929 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001930 goto out_put_task;
1931
1932 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001933 if (!dir_emit_dots(file, ctx))
1934 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001935
Al Virof0c3b502013-05-16 12:07:31 -04001936 mm = get_task_mm(task);
1937 if (!mm)
1938 goto out_put_task;
1939 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001940
Al Virof0c3b502013-05-16 12:07:31 -04001941 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001942
Al Virof0c3b502013-05-16 12:07:31 -04001943 /*
1944 * We need two passes here:
1945 *
1946 * 1) Collect vmas of mapped files with mmap_sem taken
1947 * 2) Release mmap_sem and instantiate entries
1948 *
1949 * otherwise we get lockdep complained, since filldir()
1950 * routine might require mmap_sem taken in might_fault().
1951 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001952
Al Virof0c3b502013-05-16 12:07:31 -04001953 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1954 if (vma->vm_file && ++pos > ctx->pos)
1955 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001956 }
Al Virof0c3b502013-05-16 12:07:31 -04001957
1958 if (nr_files) {
1959 fa = flex_array_alloc(sizeof(info), nr_files,
1960 GFP_KERNEL);
1961 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1962 GFP_KERNEL)) {
1963 ret = -ENOMEM;
1964 if (fa)
1965 flex_array_free(fa);
1966 up_read(&mm->mmap_sem);
1967 mmput(mm);
1968 goto out_put_task;
1969 }
1970 for (i = 0, vma = mm->mmap, pos = 2; vma;
1971 vma = vma->vm_next) {
1972 if (!vma->vm_file)
1973 continue;
1974 if (++pos <= ctx->pos)
1975 continue;
1976
1977 info.mode = vma->vm_file->f_mode;
1978 info.len = snprintf(info.name,
1979 sizeof(info.name), "%lx-%lx",
1980 vma->vm_start, vma->vm_end);
1981 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1982 BUG();
1983 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001984 }
Al Virof0c3b502013-05-16 12:07:31 -04001985 up_read(&mm->mmap_sem);
1986
1987 for (i = 0; i < nr_files; i++) {
1988 p = flex_array_get(fa, i);
1989 if (!proc_fill_cache(file, ctx,
1990 p->name, p->len,
1991 proc_map_files_instantiate,
1992 task,
1993 (void *)(unsigned long)p->mode))
1994 break;
1995 ctx->pos++;
1996 }
1997 if (fa)
1998 flex_array_free(fa);
1999 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002000
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002001out_put_task:
2002 put_task_struct(task);
2003out:
2004 return ret;
2005}
2006
2007static const struct file_operations proc_map_files_operations = {
2008 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002009 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002010 .llseek = default_llseek,
2011};
2012
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002013struct timers_private {
2014 struct pid *pid;
2015 struct task_struct *task;
2016 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002017 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002018 unsigned long flags;
2019};
2020
2021static void *timers_start(struct seq_file *m, loff_t *pos)
2022{
2023 struct timers_private *tp = m->private;
2024
2025 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2026 if (!tp->task)
2027 return ERR_PTR(-ESRCH);
2028
2029 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2030 if (!tp->sighand)
2031 return ERR_PTR(-ESRCH);
2032
2033 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2034}
2035
2036static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2037{
2038 struct timers_private *tp = m->private;
2039 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2040}
2041
2042static void timers_stop(struct seq_file *m, void *v)
2043{
2044 struct timers_private *tp = m->private;
2045
2046 if (tp->sighand) {
2047 unlock_task_sighand(tp->task, &tp->flags);
2048 tp->sighand = NULL;
2049 }
2050
2051 if (tp->task) {
2052 put_task_struct(tp->task);
2053 tp->task = NULL;
2054 }
2055}
2056
2057static int show_timer(struct seq_file *m, void *v)
2058{
2059 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002060 struct timers_private *tp = m->private;
2061 int notify;
Alexey Dobriyancedbcca2014-08-08 14:21:33 -07002062 static const char * const nstr[] = {
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002063 [SIGEV_SIGNAL] = "signal",
2064 [SIGEV_NONE] = "none",
2065 [SIGEV_THREAD] = "thread",
2066 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002067
2068 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002069 notify = timer->it_sigev_notify;
2070
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002071 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002072 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2073 timer->sigq->info.si_value.sival_ptr);
2074 seq_printf(m, "notify: %s/%s.%d\n",
2075 nstr[notify & ~SIGEV_THREAD_ID],
2076 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2077 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002078 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002079
2080 return 0;
2081}
2082
2083static const struct seq_operations proc_timers_seq_ops = {
2084 .start = timers_start,
2085 .next = timers_next,
2086 .stop = timers_stop,
2087 .show = show_timer,
2088};
2089
2090static int proc_timers_open(struct inode *inode, struct file *file)
2091{
2092 struct timers_private *tp;
2093
2094 tp = __seq_open_private(file, &proc_timers_seq_ops,
2095 sizeof(struct timers_private));
2096 if (!tp)
2097 return -ENOMEM;
2098
2099 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002100 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002101 return 0;
2102}
2103
2104static const struct file_operations proc_timers_operations = {
2105 .open = proc_timers_open,
2106 .read = seq_read,
2107 .llseek = seq_lseek,
2108 .release = seq_release_private,
2109};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002110#endif /* CONFIG_CHECKPOINT_RESTORE */
2111
Al Viroc52a47a2013-06-15 11:15:20 +04002112static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002113 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002114{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002115 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002116 struct inode *inode;
2117 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002118
Eric W. Biederman61a28782006-10-02 02:18:49 -07002119 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002120 if (!inode)
2121 goto out;
2122
2123 ei = PROC_I(inode);
2124 inode->i_mode = p->mode;
2125 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002126 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002127 if (p->iop)
2128 inode->i_op = p->iop;
2129 if (p->fop)
2130 inode->i_fop = p->fop;
2131 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002132 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002133 d_add(dentry, inode);
2134 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002135 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002136 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002137out:
Al Viroc52a47a2013-06-15 11:15:20 +04002138 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002139}
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141static struct dentry *proc_pident_lookup(struct inode *dir,
2142 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002143 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002144 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Al Viroc52a47a2013-06-15 11:15:20 +04002146 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002147 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002148 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Al Viroc52a47a2013-06-15 11:15:20 +04002150 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Eric W. Biederman99f89552006-06-26 00:25:55 -07002152 if (!task)
2153 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002155 /*
2156 * Yes, it does not scale. And it should not. Don't add
2157 * new entries into /proc/<tgid>/ without very good reasons.
2158 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002159 last = &ents[nents - 1];
2160 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (p->len != dentry->d_name.len)
2162 continue;
2163 if (!memcmp(dentry->d_name.name, p->name, p->len))
2164 break;
2165 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002166 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto out;
2168
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002169 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002171 put_task_struct(task);
2172out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002173 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Al Virof0c3b502013-05-16 12:07:31 -04002176static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002177 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002178{
Al Virof0c3b502013-05-16 12:07:31 -04002179 struct task_struct *task = get_proc_task(file_inode(file));
2180 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002181
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002182 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002183 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002184
Al Virof0c3b502013-05-16 12:07:31 -04002185 if (!dir_emit_dots(file, ctx))
2186 goto out;
2187
2188 if (ctx->pos >= nents + 2)
2189 goto out;
2190
2191 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2192 if (!proc_fill_cache(file, ctx, p->name, p->len,
2193 proc_pident_instantiate, task, p))
2194 break;
2195 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002196 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002197out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002198 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002203static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2204 size_t count, loff_t *ppos)
2205{
Al Viro496ad9a2013-01-23 17:07:38 -05002206 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002207 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002208 ssize_t length;
2209 struct task_struct *task = get_proc_task(inode);
2210
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002211 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002212 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002213
2214 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002215 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002216 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002217 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002218 if (length > 0)
2219 length = simple_read_from_buffer(buf, count, ppos, p, length);
2220 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002221 return length;
2222}
2223
2224static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2225 size_t count, loff_t *ppos)
2226{
Al Viro496ad9a2013-01-23 17:07:38 -05002227 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002228 char *page;
2229 ssize_t length;
2230 struct task_struct *task = get_proc_task(inode);
2231
2232 length = -ESRCH;
2233 if (!task)
2234 goto out_no_task;
2235 if (count > PAGE_SIZE)
2236 count = PAGE_SIZE;
2237
2238 /* No partial writes. */
2239 length = -EINVAL;
2240 if (*ppos != 0)
2241 goto out;
2242
2243 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002244 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002245 if (!page)
2246 goto out;
2247
2248 length = -EFAULT;
2249 if (copy_from_user(page, buf, count))
2250 goto out_free;
2251
David Howells107db7c2009-05-08 13:55:27 +01002252 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002253 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002254 if (length < 0)
2255 goto out_free;
2256
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002257 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002258 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002259 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002260 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002261out_free:
2262 free_page((unsigned long) page);
2263out:
2264 put_task_struct(task);
2265out_no_task:
2266 return length;
2267}
2268
Arjan van de Ven00977a52007-02-12 00:55:34 -08002269static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002270 .read = proc_pid_attr_read,
2271 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002272 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002273};
2274
Eric Dumazetc5141e62007-05-08 00:26:15 -07002275static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002276 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2277 REG("prev", S_IRUGO, proc_pid_attr_operations),
2278 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2279 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2280 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2281 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002282};
2283
Al Virof0c3b502013-05-16 12:07:31 -04002284static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Al Virof0c3b502013-05-16 12:07:31 -04002286 return proc_pident_readdir(file, ctx,
2287 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Arjan van de Ven00977a52007-02-12 00:55:34 -08002290static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002292 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002293 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294};
2295
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002296static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002297 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002299 return proc_pident_lookup(dir, dentry,
2300 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002303static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002304 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002305 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002306 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307};
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309#endif
2310
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002311#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002312static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2313 size_t count, loff_t *ppos)
2314{
Al Viro496ad9a2013-01-23 17:07:38 -05002315 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002316 struct mm_struct *mm;
2317 char buffer[PROC_NUMBUF];
2318 size_t len;
2319 int ret;
2320
2321 if (!task)
2322 return -ESRCH;
2323
2324 ret = 0;
2325 mm = get_task_mm(task);
2326 if (mm) {
2327 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2328 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2329 MMF_DUMP_FILTER_SHIFT));
2330 mmput(mm);
2331 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2332 }
2333
2334 put_task_struct(task);
2335
2336 return ret;
2337}
2338
2339static ssize_t proc_coredump_filter_write(struct file *file,
2340 const char __user *buf,
2341 size_t count,
2342 loff_t *ppos)
2343{
2344 struct task_struct *task;
2345 struct mm_struct *mm;
2346 char buffer[PROC_NUMBUF], *end;
2347 unsigned int val;
2348 int ret;
2349 int i;
2350 unsigned long mask;
2351
2352 ret = -EFAULT;
2353 memset(buffer, 0, sizeof(buffer));
2354 if (count > sizeof(buffer) - 1)
2355 count = sizeof(buffer) - 1;
2356 if (copy_from_user(buffer, buf, count))
2357 goto out_no_task;
2358
2359 ret = -EINVAL;
2360 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2361 if (*end == '\n')
2362 end++;
2363 if (end - buffer == 0)
2364 goto out_no_task;
2365
2366 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002367 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002368 if (!task)
2369 goto out_no_task;
2370
2371 ret = end - buffer;
2372 mm = get_task_mm(task);
2373 if (!mm)
2374 goto out_no_mm;
2375
2376 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2377 if (val & mask)
2378 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2379 else
2380 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2381 }
2382
2383 mmput(mm);
2384 out_no_mm:
2385 put_task_struct(task);
2386 out_no_task:
2387 return ret;
2388}
2389
2390static const struct file_operations proc_coredump_filter_operations = {
2391 .read = proc_coredump_filter_read,
2392 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002393 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002394};
2395#endif
2396
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002397#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002398static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002399{
Andrea Righi940389b2008-07-28 00:48:12 +02002400 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002401 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002402 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002403
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002404 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2405 if (result)
2406 return result;
2407
2408 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2409 result = -EACCES;
2410 goto out_unlock;
2411 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002412
Andrea Righi59954772008-07-27 17:29:15 +02002413 if (whole && lock_task_sighand(task, &flags)) {
2414 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002415
Andrea Righi59954772008-07-27 17:29:15 +02002416 task_io_accounting_add(&acct, &task->signal->ioac);
2417 while_each_thread(task, t)
2418 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002419
Andrea Righi59954772008-07-27 17:29:15 +02002420 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002421 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002422 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002423 "rchar: %llu\n"
2424 "wchar: %llu\n"
2425 "syscr: %llu\n"
2426 "syscw: %llu\n"
2427 "read_bytes: %llu\n"
2428 "write_bytes: %llu\n"
2429 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002430 (unsigned long long)acct.rchar,
2431 (unsigned long long)acct.wchar,
2432 (unsigned long long)acct.syscr,
2433 (unsigned long long)acct.syscw,
2434 (unsigned long long)acct.read_bytes,
2435 (unsigned long long)acct.write_bytes,
2436 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002437out_unlock:
2438 mutex_unlock(&task->signal->cred_guard_mutex);
2439 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002440}
Andrea Righi297c5d92008-07-25 01:48:49 -07002441
2442static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2443{
2444 return do_io_accounting(task, buffer, 0);
2445}
2446
2447static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2448{
2449 return do_io_accounting(task, buffer, 1);
2450}
2451#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002452
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002453#ifdef CONFIG_USER_NS
2454static int proc_id_map_open(struct inode *inode, struct file *file,
Fabian Frederickccf94f12014-08-08 14:21:22 -07002455 const struct seq_operations *seq_ops)
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002456{
2457 struct user_namespace *ns = NULL;
2458 struct task_struct *task;
2459 struct seq_file *seq;
2460 int ret = -EINVAL;
2461
2462 task = get_proc_task(inode);
2463 if (task) {
2464 rcu_read_lock();
2465 ns = get_user_ns(task_cred_xxx(task, user_ns));
2466 rcu_read_unlock();
2467 put_task_struct(task);
2468 }
2469 if (!ns)
2470 goto err;
2471
2472 ret = seq_open(file, seq_ops);
2473 if (ret)
2474 goto err_put_ns;
2475
2476 seq = file->private_data;
2477 seq->private = ns;
2478
2479 return 0;
2480err_put_ns:
2481 put_user_ns(ns);
2482err:
2483 return ret;
2484}
2485
2486static int proc_id_map_release(struct inode *inode, struct file *file)
2487{
2488 struct seq_file *seq = file->private_data;
2489 struct user_namespace *ns = seq->private;
2490 put_user_ns(ns);
2491 return seq_release(inode, file);
2492}
2493
2494static int proc_uid_map_open(struct inode *inode, struct file *file)
2495{
2496 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2497}
2498
2499static int proc_gid_map_open(struct inode *inode, struct file *file)
2500{
2501 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2502}
2503
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002504static int proc_projid_map_open(struct inode *inode, struct file *file)
2505{
2506 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2507}
2508
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002509static const struct file_operations proc_uid_map_operations = {
2510 .open = proc_uid_map_open,
2511 .write = proc_uid_map_write,
2512 .read = seq_read,
2513 .llseek = seq_lseek,
2514 .release = proc_id_map_release,
2515};
2516
2517static const struct file_operations proc_gid_map_operations = {
2518 .open = proc_gid_map_open,
2519 .write = proc_gid_map_write,
2520 .read = seq_read,
2521 .llseek = seq_lseek,
2522 .release = proc_id_map_release,
2523};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002524
2525static const struct file_operations proc_projid_map_operations = {
2526 .open = proc_projid_map_open,
2527 .write = proc_projid_map_write,
2528 .read = seq_read,
2529 .llseek = seq_lseek,
2530 .release = proc_id_map_release,
2531};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002532#endif /* CONFIG_USER_NS */
2533
Kees Cook47830722008-10-06 03:11:58 +04002534static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2535 struct pid *pid, struct task_struct *task)
2536{
Al Viroa9712bc2011-03-23 15:52:50 -04002537 int err = lock_trace(task);
2538 if (!err) {
2539 seq_printf(m, "%08x\n", task->personality);
2540 unlock_trace(task);
2541 }
2542 return err;
Kees Cook47830722008-10-06 03:11:58 +04002543}
2544
Eric W. Biederman801199c2006-10-02 02:18:48 -07002545/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002546 * Thread groups
2547 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002548static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002549static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002550
Eric Dumazetc5141e62007-05-08 00:26:15 -07002551static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002552 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2553 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002554#ifdef CONFIG_CHECKPOINT_RESTORE
2555 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2556#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002557 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002558 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002559#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002560 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002561#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002562 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002563 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002564 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002565 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002566 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002567#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002568 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002569#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002570#ifdef CONFIG_SCHED_AUTOGROUP
2571 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2572#endif
john stultz4614a696b2009-12-14 18:00:05 -08002573 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002574#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002575 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002576#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002577 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002578 ONE("stat", S_IRUGO, proc_tgid_stat),
2579 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002580 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002581#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002582 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002584 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2585 LNK("cwd", proc_cwd_link),
2586 LNK("root", proc_root_link),
2587 LNK("exe", proc_exe_link),
2588 REG("mounts", S_IRUGO, proc_mounts_operations),
2589 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2590 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002591#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002592 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002593 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002594 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002595#endif
2596#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002597 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002598#endif
2599#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002600 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002601#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002602#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002603 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002604#endif
2605#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002606 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002607#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002608#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002609 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002610#endif
Paul Menage8793d852007-10-18 23:39:39 -07002611#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002612 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002613#endif
Paul Menagea4243162007-10-18 23:39:35 -07002614#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002615 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002616#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002617 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002618 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002619 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002620#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002621 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2622 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002623#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002624#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002625 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002626#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002627#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002628 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002629#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002630#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002631 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002632#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002633#ifdef CONFIG_HARDWALL
2634 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2635#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002636#ifdef CONFIG_USER_NS
2637 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2638 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002639 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002640#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002641#ifdef CONFIG_CHECKPOINT_RESTORE
2642 REG("timers", S_IRUGO, proc_timers_operations),
2643#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644};
2645
Al Virof0c3b502013-05-16 12:07:31 -04002646static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002647{
Al Virof0c3b502013-05-16 12:07:31 -04002648 return proc_pident_readdir(file, ctx,
2649 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002650}
2651
Arjan van de Ven00977a52007-02-12 00:55:34 -08002652static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002653 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002654 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002655 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002656};
2657
Al Viro00cd8dd2012-06-10 17:13:09 -04002658static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2659{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002660 return proc_pident_lookup(dir, dentry,
2661 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002662}
2663
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002664static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002665 .lookup = proc_tgid_base_lookup,
2666 .getattr = pid_getattr,
2667 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002668 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002669};
2670
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002671static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002673 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002674 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002675 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Eric W. Biederman48e64842006-06-26 00:25:48 -07002677 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002678 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002679 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002680 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002681 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002682 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002683 d_drop(dentry);
2684 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Eric W. Biederman48e64842006-06-26 00:25:48 -07002687 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002688 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2689 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002690 if (!leader)
2691 goto out;
2692
2693 name.name = "task";
2694 name.len = strlen(name.name);
2695 dir = d_hash_and_lookup(leader, &name);
2696 if (!dir)
2697 goto out_put_leader;
2698
2699 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002700 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002701 dentry = d_hash_and_lookup(dir, &name);
2702 if (dentry) {
2703 shrink_dcache_parent(dentry);
2704 d_drop(dentry);
2705 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002707
2708 dput(dir);
2709out_put_leader:
2710 dput(leader);
2711out:
2712 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713}
2714
Randy Dunlap0895e912007-10-21 21:00:10 -07002715/**
2716 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2717 * @task: task that should be flushed.
2718 *
2719 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002720 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002721 * in. This call is supposed to do all of this job.
2722 *
2723 * Looks in the dcache for
2724 * /proc/@pid
2725 * /proc/@tgid/task/@pid
2726 * if either directory is present flushes it and all of it'ts children
2727 * from the dcache.
2728 *
2729 * It is safe and reasonable to cache /proc entries for a task until
2730 * that task exits. After that they just clog up the dcache with
2731 * useless entries, possibly causing useful dcache entries to be
2732 * flushed instead. This routine is proved to flush those useless
2733 * dcache entries at process exit time.
2734 *
2735 * NOTE: This routine is just an optimization so it does not guarantee
2736 * that no dcache entries will exist at process exit time it
2737 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002738 */
2739
2740void proc_flush_task(struct task_struct *task)
2741{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002742 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002743 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002744 struct upid *upid;
2745
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002746 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002747 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002748
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002749 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002750 upid = &pid->numbers[i];
2751 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002752 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002753 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002754}
2755
Al Viroc52a47a2013-06-15 11:15:20 +04002756static int proc_pid_instantiate(struct inode *dir,
2757 struct dentry * dentry,
2758 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002759{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002760 struct inode *inode;
2761
Eric W. Biederman61a28782006-10-02 02:18:49 -07002762 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002763 if (!inode)
2764 goto out;
2765
2766 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2767 inode->i_op = &proc_tgid_base_inode_operations;
2768 inode->i_fop = &proc_tgid_base_operations;
2769 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002770
Miklos Szeredibfe86842011-10-28 14:13:29 +02002771 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2772 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002773
Nick Pigginfb045ad2011-01-07 17:49:55 +11002774 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002775
2776 d_add(dentry, inode);
2777 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002778 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002779 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002780out:
Al Viroc52a47a2013-06-15 11:15:20 +04002781 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002782}
2783
Al Viro00cd8dd2012-06-10 17:13:09 -04002784struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785{
Alexey Dobriyan335eb532014-08-08 14:21:27 -07002786 int result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002789 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07002791 tgid = name_to_int(&dentry->d_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (tgid == ~0U)
2793 goto out;
2794
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002795 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002796 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002797 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (task)
2799 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002800 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 if (!task)
2802 goto out;
2803
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002804 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002805 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806out:
Al Viroc52a47a2013-06-15 11:15:20 +04002807 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002811 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002812 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002814struct tgid_iter {
2815 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002816 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002817};
2818static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2819{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002820 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002822 if (iter.task)
2823 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002824 rcu_read_lock();
2825retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002826 iter.task = NULL;
2827 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002828 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002829 iter.tgid = pid_nr_ns(pid, ns);
2830 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002831 /* What we to know is if the pid we have find is the
2832 * pid of a thread_group_leader. Testing for task
2833 * being a thread_group_leader is the obvious thing
2834 * todo but there is a window when it fails, due to
2835 * the pid transfer logic in de_thread.
2836 *
2837 * So we perform the straight forward test of seeing
2838 * if the pid we have found is the pid of a thread
2839 * group leader, and don't worry if the task we have
2840 * found doesn't happen to be a thread group leader.
2841 * As we don't care in the case of readdir.
2842 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002843 if (!iter.task || !has_group_leader_pid(iter.task)) {
2844 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002845 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002846 }
2847 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002849 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002850 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
Al Viro021ada72013-03-29 19:27:05 -04002853#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002856int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002858 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002859 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002860 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Al Viro021ada72013-03-29 19:27:05 -04002862 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002863 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Al Viro021ada72013-03-29 19:27:05 -04002865 if (pos == TGID_OFFSET - 1) {
Al Virodb963162013-06-15 10:45:10 +04002866 struct inode *inode = ns->proc_self->d_inode;
2867 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002868 return 0;
Al Viro021ada72013-03-29 19:27:05 -04002869 iter.tgid = 0;
2870 } else {
2871 iter.tgid = pos - TGID_OFFSET;
2872 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002873 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002874 for (iter = next_tgid(ns, iter);
2875 iter.task;
2876 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002877 char name[PROC_NUMBUF];
2878 int len;
2879 if (!has_pid_permissions(ns, iter.task, 2))
2880 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002881
Al Virof0c3b502013-05-16 12:07:31 -04002882 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2883 ctx->pos = iter.tgid + TGID_OFFSET;
2884 if (!proc_fill_cache(file, ctx, name, len,
2885 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002886 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
Al Virof0c3b502013-05-16 12:07:31 -04002890 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return 0;
2892}
2893
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002894/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002895 * Tasks
2896 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002897static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002898 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002899 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002900 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002901 REG("environ", S_IRUSR, proc_environ_operations),
Alexey Dobriyanf9ea5362014-08-08 14:21:35 -07002902 ONE("auxv", S_IRUSR, proc_pid_auxv),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002903 ONE("status", S_IRUGO, proc_pid_status),
Djalal Harouni35a35042014-04-07 15:38:36 -07002904 ONE("personality", S_IRUSR, proc_pid_personality),
Alexey Dobriyan1c963eb2014-08-08 14:21:37 -07002905 ONE("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002906#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002907 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002908#endif
john stultz4614a696b2009-12-14 18:00:05 -08002909 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002910#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan09d93bd2014-08-08 14:21:39 -07002911 ONE("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002912#endif
Alexey Dobriyan2ca66ff2014-08-08 14:21:41 -07002913 ONE("cmdline", S_IRUGO, proc_pid_cmdline),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002914 ONE("stat", S_IRUGO, proc_tid_stat),
2915 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002916 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002917#ifdef CONFIG_CHECKPOINT_RESTORE
2918 REG("children", S_IRUGO, proc_tid_children_operations),
2919#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002920#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002921 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002922#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2924 LNK("cwd", proc_cwd_link),
2925 LNK("root", proc_root_link),
2926 LNK("exe", proc_exe_link),
2927 REG("mounts", S_IRUGO, proc_mounts_operations),
2928 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002929#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002930 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002931 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Djalal Harouni32ed74a2014-04-07 15:38:38 -07002932 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933#endif
2934#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002935 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002936#endif
2937#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002938 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002940#ifdef CONFIG_STACKTRACE
Djalal Harouni35a35042014-04-07 15:38:36 -07002941 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002942#endif
2943#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002944 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002946#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002948#endif
Paul Menage8793d852007-10-18 23:39:39 -07002949#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002950 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951#endif
Paul Menagea4243162007-10-18 23:39:35 -07002952#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002953 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002954#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002955 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002956 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002957 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002958#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002959 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002960 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002961#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002962#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002963 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002964#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002965#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002966 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002967#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002968#ifdef CONFIG_HARDWALL
2969 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2970#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002971#ifdef CONFIG_USER_NS
2972 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2973 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002974 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002975#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002976};
2977
Al Virof0c3b502013-05-16 12:07:31 -04002978static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002979{
Al Virof0c3b502013-05-16 12:07:31 -04002980 return proc_pident_readdir(file, ctx,
2981 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982}
2983
Al Viro00cd8dd2012-06-10 17:13:09 -04002984static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2985{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002986 return proc_pident_lookup(dir, dentry,
2987 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002988}
2989
Arjan van de Ven00977a52007-02-12 00:55:34 -08002990static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002991 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002992 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002993 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002994};
2995
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002996static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002997 .lookup = proc_tid_base_lookup,
2998 .getattr = pid_getattr,
2999 .setattr = proc_setattr,
3000};
3001
Al Viroc52a47a2013-06-15 11:15:20 +04003002static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003003 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003004{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003005 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003006 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003007
3008 if (!inode)
3009 goto out;
3010 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3011 inode->i_op = &proc_tid_base_inode_operations;
3012 inode->i_fop = &proc_tid_base_operations;
3013 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003014
Miklos Szeredibfe86842011-10-28 14:13:29 +02003015 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3016 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003017
Nick Pigginfb045ad2011-01-07 17:49:55 +11003018 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003019
3020 d_add(dentry, inode);
3021 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003022 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003023 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003024out:
Al Viroc52a47a2013-06-15 11:15:20 +04003025 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003026}
3027
Al Viro00cd8dd2012-06-10 17:13:09 -04003028static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003029{
Al Viroc52a47a2013-06-15 11:15:20 +04003030 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031 struct task_struct *task;
3032 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003033 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003034 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003035
3036 if (!leader)
3037 goto out_no_task;
3038
Alexey Dobriyandbcdb502014-08-08 14:21:25 -07003039 tid = name_to_int(&dentry->d_name);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003040 if (tid == ~0U)
3041 goto out;
3042
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003043 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003044 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003045 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003046 if (task)
3047 get_task_struct(task);
3048 rcu_read_unlock();
3049 if (!task)
3050 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003051 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003052 goto out_drop_task;
3053
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003054 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003055out_drop_task:
3056 put_task_struct(task);
3057out:
3058 put_task_struct(leader);
3059out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003060 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003061}
3062
3063/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003064 * Find the first tid of a thread group to return to user space.
3065 *
3066 * Usually this is just the thread group leader, but if the users
3067 * buffer was too small or there was a seek into the middle of the
3068 * directory we have more work todo.
3069 *
3070 * In the case of a short read we start with find_task_by_pid.
3071 *
3072 * In the case of a seek we start with the leader and walk nr
3073 * threads past it.
3074 */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003075static struct task_struct *first_tid(struct pid *pid, int tid, loff_t f_pos,
3076 struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003077{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003078 struct task_struct *pos, *task;
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003079 unsigned long nr = f_pos;
3080
3081 if (nr != f_pos) /* 32bit overflow? */
3082 return NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003083
Eric W. Biedermancc288732006-06-26 00:26:01 -07003084 rcu_read_lock();
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003085 task = pid_task(pid, PIDTYPE_PID);
3086 if (!task)
3087 goto fail;
3088
3089 /* Attempt to start with the tid of a thread */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003090 if (tid && nr) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003091 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003092 if (pos && same_thread_group(pos, task))
Oleg Nesterova872ff02006-06-26 00:26:01 -07003093 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003094 }
3095
3096 /* If nr exceeds the number of threads there is nothing todo */
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003097 if (nr >= get_nr_threads(task))
Oleg Nesterovc986c142014-01-23 15:55:38 -08003098 goto fail;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003099
3100 /* If we haven't found our starting place yet start
3101 * with the leader and walk nr threads forward.
3102 */
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003103 pos = task = task->group_leader;
Oleg Nesterovc986c142014-01-23 15:55:38 -08003104 do {
Oleg Nesterov9f6e9632014-01-23 15:55:40 -08003105 if (!nr--)
Oleg Nesterovc986c142014-01-23 15:55:38 -08003106 goto found;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003107 } while_each_thread(task, pos);
Oleg Nesterovc986c142014-01-23 15:55:38 -08003108fail:
3109 pos = NULL;
3110 goto out;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003111found:
3112 get_task_struct(pos);
3113out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003114 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003115 return pos;
3116}
3117
3118/*
3119 * Find the next thread in the thread list.
3120 * Return NULL if there is an error or no next thread.
3121 *
3122 * The reference to the input task_struct is released.
3123 */
3124static struct task_struct *next_tid(struct task_struct *start)
3125{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003126 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003127 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003128 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003129 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003130 if (thread_group_leader(pos))
3131 pos = NULL;
3132 else
3133 get_task_struct(pos);
3134 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003135 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003136 put_task_struct(start);
3137 return pos;
3138}
3139
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003141static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003143 struct inode *inode = file_inode(file);
3144 struct task_struct *task;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003145 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003146 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003148 if (proc_inode_is_dead(inode))
Al Virof0c3b502013-05-16 12:07:31 -04003149 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150
Al Virof0c3b502013-05-16 12:07:31 -04003151 if (!dir_emit_dots(file, ctx))
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003154 /* f_version caches the tgid value that the last readdir call couldn't
3155 * return. lseek aka telldir automagically resets f_version to 0.
3156 */
Al Virof0c3b502013-05-16 12:07:31 -04003157 ns = file->f_dentry->d_sb->s_fs_info;
3158 tid = (int)file->f_version;
3159 file->f_version = 0;
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003160 for (task = first_tid(proc_pid(inode), tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003161 task;
Al Virof0c3b502013-05-16 12:07:31 -04003162 task = next_tid(task), ctx->pos++) {
3163 char name[PROC_NUMBUF];
3164 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003165 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003166 len = snprintf(name, sizeof(name), "%d", tid);
3167 if (!proc_fill_cache(file, ctx, name, len,
3168 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003169 /* returning this tgid failed, save it as the first
3170 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003171 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003172 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
Oleg Nesterovd855a4b2014-01-23 15:55:39 -08003176
Al Virof0c3b502013-05-16 12:07:31 -04003177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003179
3180static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3181{
3182 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003183 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003184 generic_fillattr(inode, stat);
3185
Eric W. Biederman99f89552006-06-26 00:25:55 -07003186 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003187 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003188 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003189 }
3190
3191 return 0;
3192}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003193
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003194static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003195 .lookup = proc_task_lookup,
3196 .getattr = proc_task_getattr,
3197 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003198 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003199};
3200
Arjan van de Ven00977a52007-02-12 00:55:34 -08003201static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003202 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003203 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003204 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003205};