blob: ac1f7a8232049fff830c1379e9e0f715366ecc41 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int proc_pid_cmdline(struct task_struct *task, char * buffer)
204{
205 int res = 0;
206 unsigned int len;
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 goto out;
210 if (!mm->arg_end)
211 goto out_mm; /* Shh! No looking before we're done */
212
213 len = mm->arg_end - mm->arg_start;
214
215 if (len > PAGE_SIZE)
216 len = PAGE_SIZE;
217
218 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
219
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
223 len = strnlen(buffer, res);
224 if (len < res) {
225 res = len;
226 } else {
227 len = mm->env_end - mm->env_start;
228 if (len > PAGE_SIZE - res)
229 len = PAGE_SIZE - res;
230 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
231 res = strnlen(buffer, res);
232 }
233 }
234out_mm:
235 mmput(mm);
236out:
237 return res;
238}
239
240static int proc_pid_auxv(struct task_struct *task, char *buffer)
241{
Cong Wange7dcd992012-05-31 16:26:17 -0700242 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500243 int res = PTR_ERR(mm);
244 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300246 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300248 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 res = nwords * sizeof(mm->saved_auxv[0]);
250 if (res > PAGE_SIZE)
251 res = PAGE_SIZE;
252 memcpy(buffer, mm->saved_auxv, res);
253 mmput(mm);
254 }
255 return res;
256}
257
258
259#ifdef CONFIG_KALLSYMS
260/*
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
263 */
264static int proc_pid_wchan(struct task_struct *task, char *buffer)
265{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700266 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700267 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 wchan = get_wchan(task);
270
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700271 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600272 if (!ptrace_may_access(task, PTRACE_MODE_READ))
273 return 0;
274 else
275 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700276 else
277 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279#endif /* CONFIG_KALLSYMS */
280
Al Viroa9712bc2011-03-23 15:52:50 -0400281static int lock_trace(struct task_struct *task)
282{
283 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
284 if (err)
285 return err;
286 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
287 mutex_unlock(&task->signal->cred_guard_mutex);
288 return -EPERM;
289 }
290 return 0;
291}
292
293static void unlock_trace(struct task_struct *task)
294{
295 mutex_unlock(&task->signal->cred_guard_mutex);
296}
297
Ken Chen2ec220e2008-11-10 11:26:08 +0300298#ifdef CONFIG_STACKTRACE
299
300#define MAX_STACK_TRACE_DEPTH 64
301
302static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
303 struct pid *pid, struct task_struct *task)
304{
305 struct stack_trace trace;
306 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400307 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300308 int i;
309
310 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
311 if (!entries)
312 return -ENOMEM;
313
314 trace.nr_entries = 0;
315 trace.max_entries = MAX_STACK_TRACE_DEPTH;
316 trace.entries = entries;
317 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300318
Al Viroa9712bc2011-03-23 15:52:50 -0400319 err = lock_trace(task);
320 if (!err) {
321 save_stack_trace_tsk(task, &trace);
322
323 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700324 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400325 (void *)entries[i], (void *)entries[i]);
326 }
327 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300328 }
329 kfree(entries);
330
Al Viroa9712bc2011-03-23 15:52:50 -0400331 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300332}
333#endif
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#ifdef CONFIG_SCHEDSTATS
336/*
337 * Provides /proc/PID/schedstat
338 */
339static int proc_pid_schedstat(struct task_struct *task, char *buffer)
340{
Balbir Singh172ba842007-07-09 18:52:00 +0200341 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100342 (unsigned long long)task->se.sum_exec_runtime,
343 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200344 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346#endif
347
Arjan van de Ven97455122008-01-25 21:08:34 +0100348#ifdef CONFIG_LATENCYTOP
349static int lstats_show_proc(struct seq_file *m, void *v)
350{
351 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800352 struct inode *inode = m->private;
353 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100354
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800355 if (!task)
356 return -ESRCH;
357 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100358 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800359 struct latency_record *lr = &task->latency_record[i];
360 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100361 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800362 seq_printf(m, "%i %li %li",
363 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100364 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800365 unsigned long bt = lr->backtrace[q];
366 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100367 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800368 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100369 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800370 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100371 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800372 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100373 }
374
375 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800376 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100377 return 0;
378}
379
380static int lstats_open(struct inode *inode, struct file *file)
381{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800382 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800383}
384
Arjan van de Ven97455122008-01-25 21:08:34 +0100385static ssize_t lstats_write(struct file *file, const char __user *buf,
386 size_t count, loff_t *offs)
387{
Al Viro496ad9a2013-01-23 17:07:38 -0500388 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100389
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800390 if (!task)
391 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100392 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800393 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100394
395 return count;
396}
397
398static const struct file_operations proc_lstats_operations = {
399 .open = lstats_open,
400 .read = seq_read,
401 .write = lstats_write,
402 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800403 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100404};
405
406#endif
407
Al Viro8d8b97b2013-04-19 23:11:24 -0400408#ifdef CONFIG_CGROUPS
409static int cgroup_open(struct inode *inode, struct file *file)
410{
411 struct pid *pid = PROC_I(inode)->pid;
412 return single_open(file, proc_cgroup_show, pid);
413}
414
415static const struct file_operations proc_cgroup_operations = {
416 .open = cgroup_open,
417 .read = seq_read,
418 .llseek = seq_lseek,
419 .release = single_release,
420};
421#endif
422
423#ifdef CONFIG_PROC_PID_CPUSET
424
425static int cpuset_open(struct inode *inode, struct file *file)
426{
427 struct pid *pid = PROC_I(inode)->pid;
428 return single_open(file, proc_cpuset_show, pid);
429}
430
431static const struct file_operations proc_cpuset_operations = {
432 .open = cpuset_open,
433 .read = seq_read,
434 .llseek = seq_lseek,
435 .release = single_release,
436};
437#endif
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static int proc_oom_score(struct task_struct *task, char *buffer)
440{
David Rientjesa7f638f2012-05-29 15:06:47 -0700441 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200442 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700444 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200445 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700446 points = oom_badness(task, NULL, NULL, totalpages) *
447 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700448 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return sprintf(buffer, "%lu\n", points);
450}
451
Neil Hormand85f50d2007-10-18 23:40:37 -0700452struct limit_names {
453 char *name;
454 char *unit;
455};
456
457static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700458 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700459 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
460 [RLIMIT_DATA] = {"Max data size", "bytes"},
461 [RLIMIT_STACK] = {"Max stack size", "bytes"},
462 [RLIMIT_CORE] = {"Max core file size", "bytes"},
463 [RLIMIT_RSS] = {"Max resident set", "bytes"},
464 [RLIMIT_NPROC] = {"Max processes", "processes"},
465 [RLIMIT_NOFILE] = {"Max open files", "files"},
466 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
467 [RLIMIT_AS] = {"Max address space", "bytes"},
468 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
469 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
470 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
471 [RLIMIT_NICE] = {"Max nice priority", NULL},
472 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800473 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700474};
475
476/* Display limits for a process */
477static int proc_pid_limits(struct task_struct *task, char *buffer)
478{
479 unsigned int i;
480 int count = 0;
481 unsigned long flags;
482 char *bufptr = buffer;
483
484 struct rlimit rlim[RLIM_NLIMITS];
485
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400486 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700487 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700488 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
489 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700490
491 /*
492 * print the file header
493 */
494 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
495 "Limit", "Soft Limit", "Hard Limit", "Units");
496
497 for (i = 0; i < RLIM_NLIMITS; i++) {
498 if (rlim[i].rlim_cur == RLIM_INFINITY)
499 count += sprintf(&bufptr[count], "%-25s %-20s ",
500 lnames[i].name, "unlimited");
501 else
502 count += sprintf(&bufptr[count], "%-25s %-20lu ",
503 lnames[i].name, rlim[i].rlim_cur);
504
505 if (rlim[i].rlim_max == RLIM_INFINITY)
506 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
507 else
508 count += sprintf(&bufptr[count], "%-20lu ",
509 rlim[i].rlim_max);
510
511 if (lnames[i].unit)
512 count += sprintf(&bufptr[count], "%-10s\n",
513 lnames[i].unit);
514 else
515 count += sprintf(&bufptr[count], "\n");
516 }
517
518 return count;
519}
520
Roland McGrathebcb6732008-07-25 19:46:00 -0700521#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
522static int proc_pid_syscall(struct task_struct *task, char *buffer)
523{
524 long nr;
525 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400526 int res = lock_trace(task);
527 if (res)
528 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700529
530 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400531 res = sprintf(buffer, "running\n");
532 else if (nr < 0)
533 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
534 else
535 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700536 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
537 nr,
538 args[0], args[1], args[2], args[3], args[4], args[5],
539 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400540 unlock_trace(task);
541 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700542}
543#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/************************************************************************/
546/* Here the fs part begins */
547/************************************************************************/
548
549/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700550static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700552 struct task_struct *task;
553 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700554 /* Allow access to a task's file descriptors if it is us or we
555 * may use ptrace attach to the process and find out that
556 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700557 */
558 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700559 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400560 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700561 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700562 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700563 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800566int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700567{
568 int error;
569 struct inode *inode = dentry->d_inode;
570
571 if (attr->ia_valid & ATTR_MODE)
572 return -EPERM;
573
574 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200575 if (error)
576 return error;
577
Christoph Hellwig10257742010-06-04 11:30:02 +0200578 setattr_copy(inode, attr);
579 mark_inode_dirty(inode);
580 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700581}
582
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800583/*
584 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
585 * or euid/egid (for hide_pid_min=2)?
586 */
587static bool has_pid_permissions(struct pid_namespace *pid,
588 struct task_struct *task,
589 int hide_pid_min)
590{
591 if (pid->hide_pid < hide_pid_min)
592 return true;
593 if (in_group_p(pid->pid_gid))
594 return true;
595 return ptrace_may_access(task, PTRACE_MODE_READ);
596}
597
598
599static int proc_pid_permission(struct inode *inode, int mask)
600{
601 struct pid_namespace *pid = inode->i_sb->s_fs_info;
602 struct task_struct *task;
603 bool has_perms;
604
605 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800606 if (!task)
607 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800608 has_perms = has_pid_permissions(pid, task, 1);
609 put_task_struct(task);
610
611 if (!has_perms) {
612 if (pid->hide_pid == 2) {
613 /*
614 * Let's make getdents(), stat(), and open()
615 * consistent with each other. If a process
616 * may not stat() a file, it shouldn't be seen
617 * in procfs at all.
618 */
619 return -ENOENT;
620 }
621
622 return -EPERM;
623 }
624 return generic_permission(inode, mask);
625}
626
627
628
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800629static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700630 .setattr = proc_setattr,
631};
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
634
635static ssize_t proc_info_read(struct file * file, char __user * buf,
636 size_t count, loff_t *ppos)
637{
Al Viro496ad9a2013-01-23 17:07:38 -0500638 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned long page;
640 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700641 struct task_struct *task = get_proc_task(inode);
642
643 length = -ESRCH;
644 if (!task)
645 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 if (count > PROC_BLOCK_SIZE)
648 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700649
650 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700651 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700652 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 length = PROC_I(inode)->op.proc_read(task, (char*)page);
655
656 if (length >= 0)
657 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
658 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700659out:
660 put_task_struct(task);
661out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return length;
663}
664
Arjan van de Ven00977a52007-02-12 00:55:34 -0800665static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100667 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668};
669
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800670static int proc_single_show(struct seq_file *m, void *v)
671{
672 struct inode *inode = m->private;
673 struct pid_namespace *ns;
674 struct pid *pid;
675 struct task_struct *task;
676 int ret;
677
678 ns = inode->i_sb->s_fs_info;
679 pid = proc_pid(inode);
680 task = get_pid_task(pid, PIDTYPE_PID);
681 if (!task)
682 return -ESRCH;
683
684 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
685
686 put_task_struct(task);
687 return ret;
688}
689
690static int proc_single_open(struct inode *inode, struct file *filp)
691{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800692 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800693}
694
695static const struct file_operations proc_single_file_operations = {
696 .open = proc_single_open,
697 .read = seq_read,
698 .llseek = seq_lseek,
699 .release = single_release,
700};
701
Cong Wangb409e572012-05-31 16:26:17 -0700702static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Al Viro496ad9a2013-01-23 17:07:38 -0500704 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800705 struct mm_struct *mm;
706
707 if (!task)
708 return -ESRCH;
709
Cong Wangb409e572012-05-31 16:26:17 -0700710 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800711 put_task_struct(task);
712
713 if (IS_ERR(mm))
714 return PTR_ERR(mm);
715
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100716 if (mm) {
717 /* ensure this mm_struct can't be freed */
718 atomic_inc(&mm->mm_count);
719 /* but do not pin its memory */
720 mmput(mm);
721 }
722
Linus Torvaldse2683372012-01-17 15:21:19 -0800723 file->private_data = mm;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
727
Cong Wangb409e572012-05-31 16:26:17 -0700728static int mem_open(struct inode *inode, struct file *file)
729{
Djalal Harounibc452b42012-07-30 14:42:28 -0700730 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
731
732 /* OK to pass negative loff_t, we can catch out-of-range */
733 file->f_mode |= FMODE_UNSIGNED_OFFSET;
734
735 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700736}
737
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100738static ssize_t mem_rw(struct file *file, char __user *buf,
739 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Linus Torvaldse2683372012-01-17 15:21:19 -0800741 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100742 unsigned long addr = *ppos;
743 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Linus Torvaldse2683372012-01-17 15:21:19 -0800746 if (!mm)
747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Mel Gormane12ba742007-10-16 01:25:52 -0700749 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800751 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700753 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100754 if (!atomic_inc_not_zero(&mm->mm_users))
755 goto free;
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100758 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100760 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 copied = -EFAULT;
762 break;
763 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100764
765 this_len = access_remote_vm(mm, addr, page, this_len, write);
766 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!copied)
768 copied = -EIO;
769 break;
770 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100771
772 if (!write && copy_to_user(buf, page, this_len)) {
773 copied = -EFAULT;
774 break;
775 }
776
777 buf += this_len;
778 addr += this_len;
779 copied += this_len;
780 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100782 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700783
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100784 mmput(mm);
785free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700786 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return copied;
788}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100790static ssize_t mem_read(struct file *file, char __user *buf,
791 size_t count, loff_t *ppos)
792{
793 return mem_rw(file, buf, count, ppos, 0);
794}
795
796static ssize_t mem_write(struct file *file, const char __user *buf,
797 size_t count, loff_t *ppos)
798{
799 return mem_rw(file, (char __user*)buf, count, ppos, 1);
800}
801
Matt Mackall85863e42008-02-04 22:29:04 -0800802loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 switch (orig) {
805 case 0:
806 file->f_pos = offset;
807 break;
808 case 1:
809 file->f_pos += offset;
810 break;
811 default:
812 return -EINVAL;
813 }
814 force_successful_syscall_return();
815 return file->f_pos;
816}
817
Linus Torvaldse2683372012-01-17 15:21:19 -0800818static int mem_release(struct inode *inode, struct file *file)
819{
820 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100821 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100822 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800823 return 0;
824}
825
Arjan van de Ven00977a52007-02-12 00:55:34 -0800826static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 .llseek = mem_lseek,
828 .read = mem_read,
829 .write = mem_write,
830 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800831 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832};
833
Cong Wangb409e572012-05-31 16:26:17 -0700834static int environ_open(struct inode *inode, struct file *file)
835{
836 return __mem_open(inode, file, PTRACE_MODE_READ);
837}
838
James Pearson315e28c2007-10-16 23:30:17 -0700839static ssize_t environ_read(struct file *file, char __user *buf,
840 size_t count, loff_t *ppos)
841{
James Pearson315e28c2007-10-16 23:30:17 -0700842 char *page;
843 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700844 int ret = 0;
845 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700846
Cong Wangb409e572012-05-31 16:26:17 -0700847 if (!mm)
848 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700849
James Pearson315e28c2007-10-16 23:30:17 -0700850 page = (char *)__get_free_page(GFP_TEMPORARY);
851 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700852 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700853
Al Virod6f64b82011-02-15 22:26:01 -0500854 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700855 if (!atomic_inc_not_zero(&mm->mm_users))
856 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700857 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700858 size_t this_len, max_len;
859 int retval;
860
861 if (src >= (mm->env_end - mm->env_start))
862 break;
James Pearson315e28c2007-10-16 23:30:17 -0700863
864 this_len = mm->env_end - (mm->env_start + src);
865
Djalal Harounie8905ec2012-07-30 14:42:26 -0700866 max_len = min_t(size_t, PAGE_SIZE, count);
867 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700868
Cong Wangb409e572012-05-31 16:26:17 -0700869 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700870 page, this_len, 0);
871
872 if (retval <= 0) {
873 ret = retval;
874 break;
875 }
876
877 if (copy_to_user(buf, page, retval)) {
878 ret = -EFAULT;
879 break;
880 }
881
882 ret += retval;
883 src += retval;
884 buf += retval;
885 count -= retval;
886 }
887 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700888 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700889
890free:
James Pearson315e28c2007-10-16 23:30:17 -0700891 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700892 return ret;
893}
894
895static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700896 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700897 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100898 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700899 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700900};
901
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800902static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
903 loff_t *ppos)
904{
Al Viro496ad9a2013-01-23 17:07:38 -0500905 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800906 char buffer[PROC_NUMBUF];
907 int oom_adj = OOM_ADJUST_MIN;
908 size_t len;
909 unsigned long flags;
910
911 if (!task)
912 return -ESRCH;
913 if (lock_task_sighand(task, &flags)) {
914 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
915 oom_adj = OOM_ADJUST_MAX;
916 else
917 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
918 OOM_SCORE_ADJ_MAX;
919 unlock_task_sighand(task, &flags);
920 }
921 put_task_struct(task);
922 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
923 return simple_read_from_buffer(buf, count, ppos, buffer, len);
924}
925
926static ssize_t oom_adj_write(struct file *file, const char __user *buf,
927 size_t count, loff_t *ppos)
928{
929 struct task_struct *task;
930 char buffer[PROC_NUMBUF];
931 int oom_adj;
932 unsigned long flags;
933 int err;
934
935 memset(buffer, 0, sizeof(buffer));
936 if (count > sizeof(buffer) - 1)
937 count = sizeof(buffer) - 1;
938 if (copy_from_user(buffer, buf, count)) {
939 err = -EFAULT;
940 goto out;
941 }
942
943 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
944 if (err)
945 goto out;
946 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
947 oom_adj != OOM_DISABLE) {
948 err = -EINVAL;
949 goto out;
950 }
951
Al Viro496ad9a2013-01-23 17:07:38 -0500952 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800953 if (!task) {
954 err = -ESRCH;
955 goto out;
956 }
957
958 task_lock(task);
959 if (!task->mm) {
960 err = -EINVAL;
961 goto err_task_lock;
962 }
963
964 if (!lock_task_sighand(task, &flags)) {
965 err = -ESRCH;
966 goto err_task_lock;
967 }
968
969 /*
970 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
971 * value is always attainable.
972 */
973 if (oom_adj == OOM_ADJUST_MAX)
974 oom_adj = OOM_SCORE_ADJ_MAX;
975 else
976 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
977
978 if (oom_adj < task->signal->oom_score_adj &&
979 !capable(CAP_SYS_RESOURCE)) {
980 err = -EACCES;
981 goto err_sighand;
982 }
983
984 /*
985 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
986 * /proc/pid/oom_score_adj instead.
987 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800988 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 -0800989 current->comm, task_pid_nr(current), task_pid_nr(task),
990 task_pid_nr(task));
991
992 task->signal->oom_score_adj = oom_adj;
993 trace_oom_score_adj_update(task);
994err_sighand:
995 unlock_task_sighand(task, &flags);
996err_task_lock:
997 task_unlock(task);
998 put_task_struct(task);
999out:
1000 return err < 0 ? err : count;
1001}
1002
1003static const struct file_operations proc_oom_adj_operations = {
1004 .read = oom_adj_read,
1005 .write = oom_adj_write,
1006 .llseek = generic_file_llseek,
1007};
1008
David Rientjesa63d83f2010-08-09 17:19:46 -07001009static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1010 size_t count, loff_t *ppos)
1011{
Al Viro496ad9a2013-01-23 17:07:38 -05001012 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001013 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001014 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001015 unsigned long flags;
1016 size_t len;
1017
1018 if (!task)
1019 return -ESRCH;
1020 if (lock_task_sighand(task, &flags)) {
1021 oom_score_adj = task->signal->oom_score_adj;
1022 unlock_task_sighand(task, &flags);
1023 }
1024 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001025 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001026 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1027}
1028
1029static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1030 size_t count, loff_t *ppos)
1031{
1032 struct task_struct *task;
1033 char buffer[PROC_NUMBUF];
1034 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001035 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001036 int err;
1037
1038 memset(buffer, 0, sizeof(buffer));
1039 if (count > sizeof(buffer) - 1)
1040 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001041 if (copy_from_user(buffer, buf, count)) {
1042 err = -EFAULT;
1043 goto out;
1044 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001045
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001046 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001047 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001048 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001049 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001050 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1051 err = -EINVAL;
1052 goto out;
1053 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001054
Al Viro496ad9a2013-01-23 17:07:38 -05001055 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001056 if (!task) {
1057 err = -ESRCH;
1058 goto out;
1059 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001060
Ying Han3d5992d2010-10-26 14:21:23 -07001061 task_lock(task);
1062 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001063 err = -EINVAL;
1064 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001065 }
David Rientjesd19d5472010-10-26 14:21:26 -07001066
1067 if (!lock_task_sighand(task, &flags)) {
1068 err = -ESRCH;
1069 goto err_task_lock;
1070 }
1071
David Rientjesa9c58b902012-12-11 16:02:54 -08001072 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001073 !capable(CAP_SYS_RESOURCE)) {
1074 err = -EACCES;
1075 goto err_sighand;
1076 }
1077
David Rientjesa9c58b902012-12-11 16:02:54 -08001078 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001079 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001080 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001081 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001082
David Rientjes723548b2010-10-26 14:21:25 -07001083err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001084 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001085err_task_lock:
1086 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001087 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001088out:
1089 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001090}
1091
1092static const struct file_operations proc_oom_score_adj_operations = {
1093 .read = oom_score_adj_read,
1094 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001095 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001096};
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098#ifdef CONFIG_AUDITSYSCALL
1099#define TMPBUFLEN 21
1100static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1101 size_t count, loff_t *ppos)
1102{
Al Viro496ad9a2013-01-23 17:07:38 -05001103 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001104 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 ssize_t length;
1106 char tmpbuf[TMPBUFLEN];
1107
Eric W. Biederman99f89552006-06-26 00:25:55 -07001108 if (!task)
1109 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001111 from_kuid(file->f_cred->user_ns,
1112 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001113 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1115}
1116
1117static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1118 size_t count, loff_t *ppos)
1119{
Al Viro496ad9a2013-01-23 17:07:38 -05001120 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 char *page, *tmp;
1122 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001124 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001126 rcu_read_lock();
1127 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1128 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001130 }
1131 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Al Viroe0182902006-05-18 08:28:02 -04001133 if (count >= PAGE_SIZE)
1134 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 if (*ppos != 0) {
1137 /* No partial writes. */
1138 return -EINVAL;
1139 }
Mel Gormane12ba742007-10-16 01:25:52 -07001140 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!page)
1142 return -ENOMEM;
1143 length = -EFAULT;
1144 if (copy_from_user(page, buf, count))
1145 goto out_free_page;
1146
Al Viroe0182902006-05-18 08:28:02 -04001147 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 loginuid = simple_strtoul(page, &tmp, 10);
1149 if (tmp == page) {
1150 length = -EINVAL;
1151 goto out_free_page;
1152
1153 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001154 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1155 if (!uid_valid(kloginuid)) {
1156 length = -EINVAL;
1157 goto out_free_page;
1158 }
1159
1160 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (likely(length == 0))
1162 length = count;
1163
1164out_free_page:
1165 free_page((unsigned long) page);
1166 return length;
1167}
1168
Arjan van de Ven00977a52007-02-12 00:55:34 -08001169static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 .read = proc_loginuid_read,
1171 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001172 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173};
Eric Paris1e0bd752008-03-13 08:15:31 -04001174
1175static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1176 size_t count, loff_t *ppos)
1177{
Al Viro496ad9a2013-01-23 17:07:38 -05001178 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001179 struct task_struct *task = get_proc_task(inode);
1180 ssize_t length;
1181 char tmpbuf[TMPBUFLEN];
1182
1183 if (!task)
1184 return -ESRCH;
1185 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1186 audit_get_sessionid(task));
1187 put_task_struct(task);
1188 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1189}
1190
1191static const struct file_operations proc_sessionid_operations = {
1192 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001193 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001194};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195#endif
1196
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001197#ifdef CONFIG_FAULT_INJECTION
1198static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1199 size_t count, loff_t *ppos)
1200{
Al Viro496ad9a2013-01-23 17:07:38 -05001201 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001202 char buffer[PROC_NUMBUF];
1203 size_t len;
1204 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001205
1206 if (!task)
1207 return -ESRCH;
1208 make_it_fail = task->make_it_fail;
1209 put_task_struct(task);
1210
1211 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001212
1213 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001214}
1215
1216static ssize_t proc_fault_inject_write(struct file * file,
1217 const char __user * buf, size_t count, loff_t *ppos)
1218{
1219 struct task_struct *task;
1220 char buffer[PROC_NUMBUF], *end;
1221 int make_it_fail;
1222
1223 if (!capable(CAP_SYS_RESOURCE))
1224 return -EPERM;
1225 memset(buffer, 0, sizeof(buffer));
1226 if (count > sizeof(buffer) - 1)
1227 count = sizeof(buffer) - 1;
1228 if (copy_from_user(buffer, buf, count))
1229 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001230 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1231 if (*end)
1232 return -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001233 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001234 if (!task)
1235 return -ESRCH;
1236 task->make_it_fail = make_it_fail;
1237 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001238
1239 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001240}
1241
Arjan van de Ven00977a52007-02-12 00:55:34 -08001242static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001243 .read = proc_fault_inject_read,
1244 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001245 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001246};
1247#endif
1248
Arjan van de Ven97455122008-01-25 21:08:34 +01001249
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001250#ifdef CONFIG_SCHED_DEBUG
1251/*
1252 * Print out various scheduling related per-task fields:
1253 */
1254static int sched_show(struct seq_file *m, void *v)
1255{
1256 struct inode *inode = m->private;
1257 struct task_struct *p;
1258
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001259 p = get_proc_task(inode);
1260 if (!p)
1261 return -ESRCH;
1262 proc_sched_show_task(p, m);
1263
1264 put_task_struct(p);
1265
1266 return 0;
1267}
1268
1269static ssize_t
1270sched_write(struct file *file, const char __user *buf,
1271 size_t count, loff_t *offset)
1272{
Al Viro496ad9a2013-01-23 17:07:38 -05001273 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001274 struct task_struct *p;
1275
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001276 p = get_proc_task(inode);
1277 if (!p)
1278 return -ESRCH;
1279 proc_sched_set_task(p);
1280
1281 put_task_struct(p);
1282
1283 return count;
1284}
1285
1286static int sched_open(struct inode *inode, struct file *filp)
1287{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001288 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001289}
1290
1291static const struct file_operations proc_pid_sched_operations = {
1292 .open = sched_open,
1293 .read = seq_read,
1294 .write = sched_write,
1295 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001296 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001297};
1298
1299#endif
1300
Mike Galbraith5091faa2010-11-30 14:18:03 +01001301#ifdef CONFIG_SCHED_AUTOGROUP
1302/*
1303 * Print out autogroup related information:
1304 */
1305static int sched_autogroup_show(struct seq_file *m, void *v)
1306{
1307 struct inode *inode = m->private;
1308 struct task_struct *p;
1309
1310 p = get_proc_task(inode);
1311 if (!p)
1312 return -ESRCH;
1313 proc_sched_autogroup_show_task(p, m);
1314
1315 put_task_struct(p);
1316
1317 return 0;
1318}
1319
1320static ssize_t
1321sched_autogroup_write(struct file *file, const char __user *buf,
1322 size_t count, loff_t *offset)
1323{
Al Viro496ad9a2013-01-23 17:07:38 -05001324 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001325 struct task_struct *p;
1326 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001327 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001328 int err;
1329
1330 memset(buffer, 0, sizeof(buffer));
1331 if (count > sizeof(buffer) - 1)
1332 count = sizeof(buffer) - 1;
1333 if (copy_from_user(buffer, buf, count))
1334 return -EFAULT;
1335
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001336 err = kstrtoint(strstrip(buffer), 0, &nice);
1337 if (err < 0)
1338 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001339
1340 p = get_proc_task(inode);
1341 if (!p)
1342 return -ESRCH;
1343
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001344 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001345 if (err)
1346 count = err;
1347
1348 put_task_struct(p);
1349
1350 return count;
1351}
1352
1353static int sched_autogroup_open(struct inode *inode, struct file *filp)
1354{
1355 int ret;
1356
1357 ret = single_open(filp, sched_autogroup_show, NULL);
1358 if (!ret) {
1359 struct seq_file *m = filp->private_data;
1360
1361 m->private = inode;
1362 }
1363 return ret;
1364}
1365
1366static const struct file_operations proc_pid_sched_autogroup_operations = {
1367 .open = sched_autogroup_open,
1368 .read = seq_read,
1369 .write = sched_autogroup_write,
1370 .llseek = seq_lseek,
1371 .release = single_release,
1372};
1373
1374#endif /* CONFIG_SCHED_AUTOGROUP */
1375
john stultz4614a696b2009-12-14 18:00:05 -08001376static ssize_t comm_write(struct file *file, const char __user *buf,
1377 size_t count, loff_t *offset)
1378{
Al Viro496ad9a2013-01-23 17:07:38 -05001379 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001380 struct task_struct *p;
1381 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001382 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001383
1384 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001385 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001386 return -EFAULT;
1387
1388 p = get_proc_task(inode);
1389 if (!p)
1390 return -ESRCH;
1391
1392 if (same_thread_group(current, p))
1393 set_task_comm(p, buffer);
1394 else
1395 count = -EINVAL;
1396
1397 put_task_struct(p);
1398
1399 return count;
1400}
1401
1402static int comm_show(struct seq_file *m, void *v)
1403{
1404 struct inode *inode = m->private;
1405 struct task_struct *p;
1406
1407 p = get_proc_task(inode);
1408 if (!p)
1409 return -ESRCH;
1410
1411 task_lock(p);
1412 seq_printf(m, "%s\n", p->comm);
1413 task_unlock(p);
1414
1415 put_task_struct(p);
1416
1417 return 0;
1418}
1419
1420static int comm_open(struct inode *inode, struct file *filp)
1421{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001422 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001423}
1424
1425static const struct file_operations proc_pid_set_comm_operations = {
1426 .open = comm_open,
1427 .read = seq_read,
1428 .write = comm_write,
1429 .llseek = seq_lseek,
1430 .release = single_release,
1431};
1432
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001433static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001434{
1435 struct task_struct *task;
1436 struct mm_struct *mm;
1437 struct file *exe_file;
1438
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001439 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001440 if (!task)
1441 return -ENOENT;
1442 mm = get_task_mm(task);
1443 put_task_struct(task);
1444 if (!mm)
1445 return -ENOENT;
1446 exe_file = get_mm_exe_file(mm);
1447 mmput(mm);
1448 if (exe_file) {
1449 *exe_path = exe_file->f_path;
1450 path_get(&exe_file->f_path);
1451 fput(exe_file);
1452 return 0;
1453 } else
1454 return -ENOENT;
1455}
1456
Al Viro008b1502005-08-20 00:17:39 +01001457static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001460 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int error = -EACCES;
1462
Eric W. Biederman778c1142006-06-26 00:25:58 -07001463 /* Are we allowed to snoop on the tasks file descriptors? */
1464 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Christoph Hellwig408ef012012-06-18 10:47:03 -04001467 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1468 if (error)
1469 goto out;
1470
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001471 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001472 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473out:
Al Viro008b1502005-08-20 00:17:39 +01001474 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001477static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Mel Gormane12ba742007-10-16 01:25:52 -07001479 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001480 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 int len;
1482
1483 if (!tmp)
1484 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001485
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001486 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001487 len = PTR_ERR(pathname);
1488 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001490 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (len > buflen)
1493 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001494 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 len = -EFAULT;
1496 out:
1497 free_page((unsigned long)tmp);
1498 return len;
1499}
1500
1501static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1502{
1503 int error = -EACCES;
1504 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001505 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Eric W. Biederman778c1142006-06-26 00:25:58 -07001507 /* Are we allowed to snoop on the tasks file descriptors? */
1508 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001511 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (error)
1513 goto out;
1514
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001515 error = do_proc_readlink(&path, buffer, buflen);
1516 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return error;
1519}
1520
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001521const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001523 .follow_link = proc_pid_follow_link,
1524 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525};
1526
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527
1528/* building an inode */
1529
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001530struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001531{
1532 struct inode * inode;
1533 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001534 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001535
1536 /* We need a new inode */
1537
1538 inode = new_inode(sb);
1539 if (!inode)
1540 goto out;
1541
1542 /* Common stuff */
1543 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001544 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001545 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001546 inode->i_op = &proc_def_inode_operations;
1547
1548 /*
1549 * grab the reference to task.
1550 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001551 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552 if (!ei->pid)
1553 goto out_unlock;
1554
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001556 rcu_read_lock();
1557 cred = __task_cred(task);
1558 inode->i_uid = cred->euid;
1559 inode->i_gid = cred->egid;
1560 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001561 }
1562 security_task_to_inode(task, inode);
1563
1564out:
1565 return inode;
1566
1567out_unlock:
1568 iput(inode);
1569 return NULL;
1570}
1571
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001572int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001573{
1574 struct inode *inode = dentry->d_inode;
1575 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001576 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001577 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001578
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001579 generic_fillattr(inode, stat);
1580
1581 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001582 stat->uid = GLOBAL_ROOT_UID;
1583 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001584 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1585 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001586 if (!has_pid_permissions(pid, task, 2)) {
1587 rcu_read_unlock();
1588 /*
1589 * This doesn't prevent learning whether PID exists,
1590 * it only makes getattr() consistent with readdir().
1591 */
1592 return -ENOENT;
1593 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001594 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1595 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001596 cred = __task_cred(task);
1597 stat->uid = cred->euid;
1598 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001599 }
1600 }
1601 rcu_read_unlock();
1602 return 0;
1603}
1604
1605/* dentry stuff */
1606
1607/*
1608 * Exceptional case: normally we are not allowed to unhash a busy
1609 * directory. In this case, however, we can do it - no aliasing problems
1610 * due to the way we treat inodes.
1611 *
1612 * Rewrite the inode's ownerships here because the owning task may have
1613 * performed a setuid(), etc.
1614 *
1615 * Before the /proc/pid/status file was created the only way to read
1616 * the effective uid of a /process was to stat /proc/pid. Reading
1617 * /proc/pid/status is slow enough that procps and other packages
1618 * kept stating /proc/pid. To keep the rules in /proc simple I have
1619 * made this apply to all per process world readable and executable
1620 * directories.
1621 */
Al Viro0b728e12012-06-10 16:03:43 -04001622int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001623{
Nick Piggin34286d62011-01-07 17:49:57 +11001624 struct inode *inode;
1625 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001626 const struct cred *cred;
1627
Al Viro0b728e12012-06-10 16:03:43 -04001628 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001629 return -ECHILD;
1630
1631 inode = dentry->d_inode;
1632 task = get_proc_task(inode);
1633
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001634 if (task) {
1635 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1636 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001637 rcu_read_lock();
1638 cred = __task_cred(task);
1639 inode->i_uid = cred->euid;
1640 inode->i_gid = cred->egid;
1641 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001642 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001643 inode->i_uid = GLOBAL_ROOT_UID;
1644 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001645 }
1646 inode->i_mode &= ~(S_ISUID | S_ISGID);
1647 security_task_to_inode(task, inode);
1648 put_task_struct(task);
1649 return 1;
1650 }
1651 d_drop(dentry);
1652 return 0;
1653}
1654
David Howells1dd704b2013-04-12 01:08:50 +01001655int pid_delete_dentry(const struct dentry *dentry)
1656{
1657 /* Is the task we represent dead?
1658 * If so, then don't put the dentry on the lru list,
1659 * kill it immediately.
1660 */
1661 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1662}
1663
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001664const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001665{
1666 .d_revalidate = pid_revalidate,
1667 .d_delete = pid_delete_dentry,
1668};
1669
1670/* Lookups */
1671
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001672/*
1673 * Fill a directory entry.
1674 *
1675 * If possible create the dcache entry and derive our inode number and
1676 * file type from dcache entry.
1677 *
1678 * Since all of the proc inode numbers are dynamically generated, the inode
1679 * numbers do not exist until the inode is cache. This means creating the
1680 * the dcache entry in readdir is necessary to keep the inode numbers
1681 * reported by readdir in sync with the inode numbers reported
1682 * by stat.
1683 */
Al Virof0c3b502013-05-16 12:07:31 -04001684bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001685 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001686 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001687{
Al Virof0c3b502013-05-16 12:07:31 -04001688 struct dentry *child, *dir = file->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001689 struct inode *inode;
1690 struct qstr qname;
1691 ino_t ino = 0;
1692 unsigned type = DT_UNKNOWN;
1693
1694 qname.name = name;
1695 qname.len = len;
1696 qname.hash = full_name_hash(name, len);
1697
1698 child = d_lookup(dir, &qname);
1699 if (!child) {
1700 struct dentry *new;
1701 new = d_alloc(dir, &qname);
1702 if (new) {
Al Viroc52a47a2013-06-15 11:15:20 +04001703 child = ERR_PTR(instantiate(dir->d_inode, new, task, ptr));
Eric W. Biederman61a28782006-10-02 02:18:49 -07001704 if (child)
1705 dput(new);
1706 else
1707 child = new;
1708 }
1709 }
1710 if (!child || IS_ERR(child) || !child->d_inode)
1711 goto end_instantiate;
1712 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001713 ino = inode->i_ino;
1714 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001715 dput(child);
1716end_instantiate:
1717 if (!ino)
1718 ino = find_inode_number(dir, &qname);
1719 if (!ino)
1720 ino = 1;
Al Virof0c3b502013-05-16 12:07:31 -04001721 return dir_emit(ctx, name, len, ino, type);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001722}
1723
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001724#ifdef CONFIG_CHECKPOINT_RESTORE
1725
1726/*
1727 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1728 * which represent vma start and end addresses.
1729 */
1730static int dname_to_vma_addr(struct dentry *dentry,
1731 unsigned long *start, unsigned long *end)
1732{
1733 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1734 return -EINVAL;
1735
1736 return 0;
1737}
1738
Al Viro0b728e12012-06-10 16:03:43 -04001739static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001740{
1741 unsigned long vm_start, vm_end;
1742 bool exact_vma_exists = false;
1743 struct mm_struct *mm = NULL;
1744 struct task_struct *task;
1745 const struct cred *cred;
1746 struct inode *inode;
1747 int status = 0;
1748
Al Viro0b728e12012-06-10 16:03:43 -04001749 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001750 return -ECHILD;
1751
1752 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001753 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001754 goto out_notask;
1755 }
1756
1757 inode = dentry->d_inode;
1758 task = get_proc_task(inode);
1759 if (!task)
1760 goto out_notask;
1761
Cong Wang2344bec2012-05-31 16:26:18 -07001762 mm = mm_access(task, PTRACE_MODE_READ);
1763 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001764 goto out;
1765
1766 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1767 down_read(&mm->mmap_sem);
1768 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1769 up_read(&mm->mmap_sem);
1770 }
1771
1772 mmput(mm);
1773
1774 if (exact_vma_exists) {
1775 if (task_dumpable(task)) {
1776 rcu_read_lock();
1777 cred = __task_cred(task);
1778 inode->i_uid = cred->euid;
1779 inode->i_gid = cred->egid;
1780 rcu_read_unlock();
1781 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001782 inode->i_uid = GLOBAL_ROOT_UID;
1783 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001784 }
1785 security_task_to_inode(task, inode);
1786 status = 1;
1787 }
1788
1789out:
1790 put_task_struct(task);
1791
1792out_notask:
1793 if (status <= 0)
1794 d_drop(dentry);
1795
1796 return status;
1797}
1798
1799static const struct dentry_operations tid_map_files_dentry_operations = {
1800 .d_revalidate = map_files_d_revalidate,
1801 .d_delete = pid_delete_dentry,
1802};
1803
1804static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1805{
1806 unsigned long vm_start, vm_end;
1807 struct vm_area_struct *vma;
1808 struct task_struct *task;
1809 struct mm_struct *mm;
1810 int rc;
1811
1812 rc = -ENOENT;
1813 task = get_proc_task(dentry->d_inode);
1814 if (!task)
1815 goto out;
1816
1817 mm = get_task_mm(task);
1818 put_task_struct(task);
1819 if (!mm)
1820 goto out;
1821
1822 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1823 if (rc)
1824 goto out_mmput;
1825
1826 down_read(&mm->mmap_sem);
1827 vma = find_exact_vma(mm, vm_start, vm_end);
1828 if (vma && vma->vm_file) {
1829 *path = vma->vm_file->f_path;
1830 path_get(path);
1831 rc = 0;
1832 }
1833 up_read(&mm->mmap_sem);
1834
1835out_mmput:
1836 mmput(mm);
1837out:
1838 return rc;
1839}
1840
1841struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001842 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001843 unsigned long len;
1844 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1845};
1846
Al Viroc52a47a2013-06-15 11:15:20 +04001847static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001848proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1849 struct task_struct *task, const void *ptr)
1850{
Al Viro7b540d02012-08-27 14:55:26 -04001851 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001852 struct proc_inode *ei;
1853 struct inode *inode;
1854
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001855 inode = proc_pid_make_inode(dir->i_sb, task);
1856 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001857 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001858
1859 ei = PROC_I(inode);
1860 ei->op.proc_get_link = proc_map_files_get_link;
1861
1862 inode->i_op = &proc_pid_link_inode_operations;
1863 inode->i_size = 64;
1864 inode->i_mode = S_IFLNK;
1865
Al Viro7b540d02012-08-27 14:55:26 -04001866 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001867 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001868 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001869 inode->i_mode |= S_IWUSR;
1870
1871 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1872 d_add(dentry, inode);
1873
Al Viroc52a47a2013-06-15 11:15:20 +04001874 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001875}
1876
1877static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001878 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001879{
1880 unsigned long vm_start, vm_end;
1881 struct vm_area_struct *vma;
1882 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001883 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001884 struct mm_struct *mm;
1885
Al Viroc52a47a2013-06-15 11:15:20 +04001886 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001887 if (!capable(CAP_SYS_ADMIN))
1888 goto out;
1889
Al Viroc52a47a2013-06-15 11:15:20 +04001890 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001891 task = get_proc_task(dir);
1892 if (!task)
1893 goto out;
1894
Al Viroc52a47a2013-06-15 11:15:20 +04001895 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001896 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001897 goto out_put_task;
1898
Al Viroc52a47a2013-06-15 11:15:20 +04001899 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001900 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001901 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001902
1903 mm = get_task_mm(task);
1904 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001905 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001906
1907 down_read(&mm->mmap_sem);
1908 vma = find_exact_vma(mm, vm_start, vm_end);
1909 if (!vma)
1910 goto out_no_vma;
1911
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001912 if (vma->vm_file)
1913 result = proc_map_files_instantiate(dir, dentry, task,
1914 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001915
1916out_no_vma:
1917 up_read(&mm->mmap_sem);
1918 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001919out_put_task:
1920 put_task_struct(task);
1921out:
Al Viroc52a47a2013-06-15 11:15:20 +04001922 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001923}
1924
1925static const struct inode_operations proc_map_files_inode_operations = {
1926 .lookup = proc_map_files_lookup,
1927 .permission = proc_fd_permission,
1928 .setattr = proc_setattr,
1929};
1930
1931static int
Al Virof0c3b502013-05-16 12:07:31 -04001932proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001933{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001934 struct vm_area_struct *vma;
1935 struct task_struct *task;
1936 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001937 unsigned long nr_files, pos, i;
1938 struct flex_array *fa = NULL;
1939 struct map_files_info info;
1940 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001941 int ret;
1942
Zhao Hongjiang41735812013-02-20 13:13:55 +11001943 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001944 if (!capable(CAP_SYS_ADMIN))
1945 goto out;
1946
1947 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001948 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001949 if (!task)
1950 goto out;
1951
1952 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001953 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001954 goto out_put_task;
1955
1956 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001957 if (!dir_emit_dots(file, ctx))
1958 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001959
Al Virof0c3b502013-05-16 12:07:31 -04001960 mm = get_task_mm(task);
1961 if (!mm)
1962 goto out_put_task;
1963 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001964
Al Virof0c3b502013-05-16 12:07:31 -04001965 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001966
Al Virof0c3b502013-05-16 12:07:31 -04001967 /*
1968 * We need two passes here:
1969 *
1970 * 1) Collect vmas of mapped files with mmap_sem taken
1971 * 2) Release mmap_sem and instantiate entries
1972 *
1973 * otherwise we get lockdep complained, since filldir()
1974 * routine might require mmap_sem taken in might_fault().
1975 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001976
Al Virof0c3b502013-05-16 12:07:31 -04001977 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1978 if (vma->vm_file && ++pos > ctx->pos)
1979 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001980 }
Al Virof0c3b502013-05-16 12:07:31 -04001981
1982 if (nr_files) {
1983 fa = flex_array_alloc(sizeof(info), nr_files,
1984 GFP_KERNEL);
1985 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1986 GFP_KERNEL)) {
1987 ret = -ENOMEM;
1988 if (fa)
1989 flex_array_free(fa);
1990 up_read(&mm->mmap_sem);
1991 mmput(mm);
1992 goto out_put_task;
1993 }
1994 for (i = 0, vma = mm->mmap, pos = 2; vma;
1995 vma = vma->vm_next) {
1996 if (!vma->vm_file)
1997 continue;
1998 if (++pos <= ctx->pos)
1999 continue;
2000
2001 info.mode = vma->vm_file->f_mode;
2002 info.len = snprintf(info.name,
2003 sizeof(info.name), "%lx-%lx",
2004 vma->vm_start, vma->vm_end);
2005 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2006 BUG();
2007 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002008 }
Al Virof0c3b502013-05-16 12:07:31 -04002009 up_read(&mm->mmap_sem);
2010
2011 for (i = 0; i < nr_files; i++) {
2012 p = flex_array_get(fa, i);
2013 if (!proc_fill_cache(file, ctx,
2014 p->name, p->len,
2015 proc_map_files_instantiate,
2016 task,
2017 (void *)(unsigned long)p->mode))
2018 break;
2019 ctx->pos++;
2020 }
2021 if (fa)
2022 flex_array_free(fa);
2023 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002024
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002025out_put_task:
2026 put_task_struct(task);
2027out:
2028 return ret;
2029}
2030
2031static const struct file_operations proc_map_files_operations = {
2032 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002033 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002034 .llseek = default_llseek,
2035};
2036
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002037struct timers_private {
2038 struct pid *pid;
2039 struct task_struct *task;
2040 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002041 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002042 unsigned long flags;
2043};
2044
2045static void *timers_start(struct seq_file *m, loff_t *pos)
2046{
2047 struct timers_private *tp = m->private;
2048
2049 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2050 if (!tp->task)
2051 return ERR_PTR(-ESRCH);
2052
2053 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2054 if (!tp->sighand)
2055 return ERR_PTR(-ESRCH);
2056
2057 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2058}
2059
2060static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2061{
2062 struct timers_private *tp = m->private;
2063 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2064}
2065
2066static void timers_stop(struct seq_file *m, void *v)
2067{
2068 struct timers_private *tp = m->private;
2069
2070 if (tp->sighand) {
2071 unlock_task_sighand(tp->task, &tp->flags);
2072 tp->sighand = NULL;
2073 }
2074
2075 if (tp->task) {
2076 put_task_struct(tp->task);
2077 tp->task = NULL;
2078 }
2079}
2080
2081static int show_timer(struct seq_file *m, void *v)
2082{
2083 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002084 struct timers_private *tp = m->private;
2085 int notify;
2086 static char *nstr[] = {
2087 [SIGEV_SIGNAL] = "signal",
2088 [SIGEV_NONE] = "none",
2089 [SIGEV_THREAD] = "thread",
2090 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002091
2092 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002093 notify = timer->it_sigev_notify;
2094
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002095 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002096 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2097 timer->sigq->info.si_value.sival_ptr);
2098 seq_printf(m, "notify: %s/%s.%d\n",
2099 nstr[notify & ~SIGEV_THREAD_ID],
2100 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2101 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002102 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002103
2104 return 0;
2105}
2106
2107static const struct seq_operations proc_timers_seq_ops = {
2108 .start = timers_start,
2109 .next = timers_next,
2110 .stop = timers_stop,
2111 .show = show_timer,
2112};
2113
2114static int proc_timers_open(struct inode *inode, struct file *file)
2115{
2116 struct timers_private *tp;
2117
2118 tp = __seq_open_private(file, &proc_timers_seq_ops,
2119 sizeof(struct timers_private));
2120 if (!tp)
2121 return -ENOMEM;
2122
2123 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002124 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002125 return 0;
2126}
2127
2128static const struct file_operations proc_timers_operations = {
2129 .open = proc_timers_open,
2130 .read = seq_read,
2131 .llseek = seq_lseek,
2132 .release = seq_release_private,
2133};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002134#endif /* CONFIG_CHECKPOINT_RESTORE */
2135
Al Viroc52a47a2013-06-15 11:15:20 +04002136static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002137 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002138{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002139 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002140 struct inode *inode;
2141 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002142
Eric W. Biederman61a28782006-10-02 02:18:49 -07002143 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002144 if (!inode)
2145 goto out;
2146
2147 ei = PROC_I(inode);
2148 inode->i_mode = p->mode;
2149 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002150 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002151 if (p->iop)
2152 inode->i_op = p->iop;
2153 if (p->fop)
2154 inode->i_fop = p->fop;
2155 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002156 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002157 d_add(dentry, inode);
2158 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002159 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002160 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002161out:
Al Viroc52a47a2013-06-15 11:15:20 +04002162 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002163}
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165static struct dentry *proc_pident_lookup(struct inode *dir,
2166 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002167 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002168 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Al Viroc52a47a2013-06-15 11:15:20 +04002170 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002171 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002172 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Al Viroc52a47a2013-06-15 11:15:20 +04002174 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Eric W. Biederman99f89552006-06-26 00:25:55 -07002176 if (!task)
2177 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002179 /*
2180 * Yes, it does not scale. And it should not. Don't add
2181 * new entries into /proc/<tgid>/ without very good reasons.
2182 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002183 last = &ents[nents - 1];
2184 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (p->len != dentry->d_name.len)
2186 continue;
2187 if (!memcmp(dentry->d_name.name, p->name, p->len))
2188 break;
2189 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002190 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 goto out;
2192
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002193 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002195 put_task_struct(task);
2196out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002197 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
Al Virof0c3b502013-05-16 12:07:31 -04002200static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002201 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002202{
Al Virof0c3b502013-05-16 12:07:31 -04002203 struct task_struct *task = get_proc_task(file_inode(file));
2204 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002206 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002207 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002208
Al Virof0c3b502013-05-16 12:07:31 -04002209 if (!dir_emit_dots(file, ctx))
2210 goto out;
2211
2212 if (ctx->pos >= nents + 2)
2213 goto out;
2214
2215 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2216 if (!proc_fill_cache(file, ctx, p->name, p->len,
2217 proc_pident_instantiate, task, p))
2218 break;
2219 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002220 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002221out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002222 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002227static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2228 size_t count, loff_t *ppos)
2229{
Al Viro496ad9a2013-01-23 17:07:38 -05002230 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002231 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002232 ssize_t length;
2233 struct task_struct *task = get_proc_task(inode);
2234
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002235 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002236 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002237
2238 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002239 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002240 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002241 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002242 if (length > 0)
2243 length = simple_read_from_buffer(buf, count, ppos, p, length);
2244 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002245 return length;
2246}
2247
2248static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2249 size_t count, loff_t *ppos)
2250{
Al Viro496ad9a2013-01-23 17:07:38 -05002251 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002252 char *page;
2253 ssize_t length;
2254 struct task_struct *task = get_proc_task(inode);
2255
2256 length = -ESRCH;
2257 if (!task)
2258 goto out_no_task;
2259 if (count > PAGE_SIZE)
2260 count = PAGE_SIZE;
2261
2262 /* No partial writes. */
2263 length = -EINVAL;
2264 if (*ppos != 0)
2265 goto out;
2266
2267 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002268 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002269 if (!page)
2270 goto out;
2271
2272 length = -EFAULT;
2273 if (copy_from_user(page, buf, count))
2274 goto out_free;
2275
David Howells107db7c2009-05-08 13:55:27 +01002276 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002277 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002278 if (length < 0)
2279 goto out_free;
2280
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002281 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002282 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002283 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002284 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002285out_free:
2286 free_page((unsigned long) page);
2287out:
2288 put_task_struct(task);
2289out_no_task:
2290 return length;
2291}
2292
Arjan van de Ven00977a52007-02-12 00:55:34 -08002293static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002294 .read = proc_pid_attr_read,
2295 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002296 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002297};
2298
Eric Dumazetc5141e62007-05-08 00:26:15 -07002299static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002300 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2301 REG("prev", S_IRUGO, proc_pid_attr_operations),
2302 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2303 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2304 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2305 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002306};
2307
Al Virof0c3b502013-05-16 12:07:31 -04002308static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Al Virof0c3b502013-05-16 12:07:31 -04002310 return proc_pident_readdir(file, ctx,
2311 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
Arjan van de Ven00977a52007-02-12 00:55:34 -08002314static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002316 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002317 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318};
2319
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002320static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002321 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002323 return proc_pident_lookup(dir, dentry,
2324 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325}
2326
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002327static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002328 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002329 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002330 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331};
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333#endif
2334
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002335#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002336static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2337 size_t count, loff_t *ppos)
2338{
Al Viro496ad9a2013-01-23 17:07:38 -05002339 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002340 struct mm_struct *mm;
2341 char buffer[PROC_NUMBUF];
2342 size_t len;
2343 int ret;
2344
2345 if (!task)
2346 return -ESRCH;
2347
2348 ret = 0;
2349 mm = get_task_mm(task);
2350 if (mm) {
2351 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2352 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2353 MMF_DUMP_FILTER_SHIFT));
2354 mmput(mm);
2355 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2356 }
2357
2358 put_task_struct(task);
2359
2360 return ret;
2361}
2362
2363static ssize_t proc_coredump_filter_write(struct file *file,
2364 const char __user *buf,
2365 size_t count,
2366 loff_t *ppos)
2367{
2368 struct task_struct *task;
2369 struct mm_struct *mm;
2370 char buffer[PROC_NUMBUF], *end;
2371 unsigned int val;
2372 int ret;
2373 int i;
2374 unsigned long mask;
2375
2376 ret = -EFAULT;
2377 memset(buffer, 0, sizeof(buffer));
2378 if (count > sizeof(buffer) - 1)
2379 count = sizeof(buffer) - 1;
2380 if (copy_from_user(buffer, buf, count))
2381 goto out_no_task;
2382
2383 ret = -EINVAL;
2384 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2385 if (*end == '\n')
2386 end++;
2387 if (end - buffer == 0)
2388 goto out_no_task;
2389
2390 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002391 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002392 if (!task)
2393 goto out_no_task;
2394
2395 ret = end - buffer;
2396 mm = get_task_mm(task);
2397 if (!mm)
2398 goto out_no_mm;
2399
2400 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2401 if (val & mask)
2402 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2403 else
2404 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2405 }
2406
2407 mmput(mm);
2408 out_no_mm:
2409 put_task_struct(task);
2410 out_no_task:
2411 return ret;
2412}
2413
2414static const struct file_operations proc_coredump_filter_operations = {
2415 .read = proc_coredump_filter_read,
2416 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002417 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002418};
2419#endif
2420
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002421#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002422static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002423{
Andrea Righi940389b2008-07-28 00:48:12 +02002424 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002425 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002426 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002427
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002428 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2429 if (result)
2430 return result;
2431
2432 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2433 result = -EACCES;
2434 goto out_unlock;
2435 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002436
Andrea Righi59954772008-07-27 17:29:15 +02002437 if (whole && lock_task_sighand(task, &flags)) {
2438 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002439
Andrea Righi59954772008-07-27 17:29:15 +02002440 task_io_accounting_add(&acct, &task->signal->ioac);
2441 while_each_thread(task, t)
2442 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002443
Andrea Righi59954772008-07-27 17:29:15 +02002444 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002445 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002446 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002447 "rchar: %llu\n"
2448 "wchar: %llu\n"
2449 "syscr: %llu\n"
2450 "syscw: %llu\n"
2451 "read_bytes: %llu\n"
2452 "write_bytes: %llu\n"
2453 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002454 (unsigned long long)acct.rchar,
2455 (unsigned long long)acct.wchar,
2456 (unsigned long long)acct.syscr,
2457 (unsigned long long)acct.syscw,
2458 (unsigned long long)acct.read_bytes,
2459 (unsigned long long)acct.write_bytes,
2460 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002461out_unlock:
2462 mutex_unlock(&task->signal->cred_guard_mutex);
2463 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002464}
Andrea Righi297c5d92008-07-25 01:48:49 -07002465
2466static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2467{
2468 return do_io_accounting(task, buffer, 0);
2469}
2470
2471static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2472{
2473 return do_io_accounting(task, buffer, 1);
2474}
2475#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002476
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002477#ifdef CONFIG_USER_NS
2478static int proc_id_map_open(struct inode *inode, struct file *file,
2479 struct seq_operations *seq_ops)
2480{
2481 struct user_namespace *ns = NULL;
2482 struct task_struct *task;
2483 struct seq_file *seq;
2484 int ret = -EINVAL;
2485
2486 task = get_proc_task(inode);
2487 if (task) {
2488 rcu_read_lock();
2489 ns = get_user_ns(task_cred_xxx(task, user_ns));
2490 rcu_read_unlock();
2491 put_task_struct(task);
2492 }
2493 if (!ns)
2494 goto err;
2495
2496 ret = seq_open(file, seq_ops);
2497 if (ret)
2498 goto err_put_ns;
2499
2500 seq = file->private_data;
2501 seq->private = ns;
2502
2503 return 0;
2504err_put_ns:
2505 put_user_ns(ns);
2506err:
2507 return ret;
2508}
2509
2510static int proc_id_map_release(struct inode *inode, struct file *file)
2511{
2512 struct seq_file *seq = file->private_data;
2513 struct user_namespace *ns = seq->private;
2514 put_user_ns(ns);
2515 return seq_release(inode, file);
2516}
2517
2518static int proc_uid_map_open(struct inode *inode, struct file *file)
2519{
2520 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2521}
2522
2523static int proc_gid_map_open(struct inode *inode, struct file *file)
2524{
2525 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2526}
2527
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002528static int proc_projid_map_open(struct inode *inode, struct file *file)
2529{
2530 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2531}
2532
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002533static const struct file_operations proc_uid_map_operations = {
2534 .open = proc_uid_map_open,
2535 .write = proc_uid_map_write,
2536 .read = seq_read,
2537 .llseek = seq_lseek,
2538 .release = proc_id_map_release,
2539};
2540
2541static const struct file_operations proc_gid_map_operations = {
2542 .open = proc_gid_map_open,
2543 .write = proc_gid_map_write,
2544 .read = seq_read,
2545 .llseek = seq_lseek,
2546 .release = proc_id_map_release,
2547};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002548
2549static const struct file_operations proc_projid_map_operations = {
2550 .open = proc_projid_map_open,
2551 .write = proc_projid_map_write,
2552 .read = seq_read,
2553 .llseek = seq_lseek,
2554 .release = proc_id_map_release,
2555};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002556#endif /* CONFIG_USER_NS */
2557
Kees Cook47830722008-10-06 03:11:58 +04002558static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2559 struct pid *pid, struct task_struct *task)
2560{
Al Viroa9712bc2011-03-23 15:52:50 -04002561 int err = lock_trace(task);
2562 if (!err) {
2563 seq_printf(m, "%08x\n", task->personality);
2564 unlock_trace(task);
2565 }
2566 return err;
Kees Cook47830722008-10-06 03:11:58 +04002567}
2568
Eric W. Biederman801199c2006-10-02 02:18:48 -07002569/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002570 * Thread groups
2571 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002572static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002573static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002574
Eric Dumazetc5141e62007-05-08 00:26:15 -07002575static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002576 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2577 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002578#ifdef CONFIG_CHECKPOINT_RESTORE
2579 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2580#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002581 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002582 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002583#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002584 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002585#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002586 REG("environ", S_IRUSR, proc_environ_operations),
2587 INF("auxv", S_IRUSR, proc_pid_auxv),
2588 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002589 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002590 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002591#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002592 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002593#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002594#ifdef CONFIG_SCHED_AUTOGROUP
2595 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2596#endif
john stultz4614a696b2009-12-14 18:00:05 -08002597 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002598#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002599 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002600#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002601 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2602 ONE("stat", S_IRUGO, proc_tgid_stat),
2603 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002604 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002606 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002607#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002608 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2609 LNK("cwd", proc_cwd_link),
2610 LNK("root", proc_root_link),
2611 LNK("exe", proc_exe_link),
2612 REG("mounts", S_IRUGO, proc_mounts_operations),
2613 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2614 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002615#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002616 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002617 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002618 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002619#endif
2620#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002621 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002622#endif
2623#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002624 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002625#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002626#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002627 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002628#endif
2629#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002630 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002631#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002632#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002633 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002634#endif
Paul Menage8793d852007-10-18 23:39:39 -07002635#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002636 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002637#endif
Paul Menagea4243162007-10-18 23:39:35 -07002638#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002639 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002640#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002641 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002642 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002643 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002645 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2646 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002647#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002648#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002649 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002650#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002651#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002652 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002653#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002654#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002655 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002656#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002657#ifdef CONFIG_HARDWALL
2658 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2659#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002660#ifdef CONFIG_USER_NS
2661 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2662 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002663 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002664#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002665#ifdef CONFIG_CHECKPOINT_RESTORE
2666 REG("timers", S_IRUGO, proc_timers_operations),
2667#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002668};
2669
Al Virof0c3b502013-05-16 12:07:31 -04002670static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002671{
Al Virof0c3b502013-05-16 12:07:31 -04002672 return proc_pident_readdir(file, ctx,
2673 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002674}
2675
Arjan van de Ven00977a52007-02-12 00:55:34 -08002676static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002677 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002678 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002679 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002680};
2681
Al Viro00cd8dd2012-06-10 17:13:09 -04002682static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2683{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002684 return proc_pident_lookup(dir, dentry,
2685 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002686}
2687
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002688static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002689 .lookup = proc_tgid_base_lookup,
2690 .getattr = pid_getattr,
2691 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002692 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002693};
2694
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002695static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002697 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002698 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002699 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Eric W. Biederman48e64842006-06-26 00:25:48 -07002701 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002702 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002703 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002704 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002705 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002706 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002707 d_drop(dentry);
2708 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Eric W. Biederman48e64842006-06-26 00:25:48 -07002711 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002712 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2713 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002714 if (!leader)
2715 goto out;
2716
2717 name.name = "task";
2718 name.len = strlen(name.name);
2719 dir = d_hash_and_lookup(leader, &name);
2720 if (!dir)
2721 goto out_put_leader;
2722
2723 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002724 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002725 dentry = d_hash_and_lookup(dir, &name);
2726 if (dentry) {
2727 shrink_dcache_parent(dentry);
2728 d_drop(dentry);
2729 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002731
2732 dput(dir);
2733out_put_leader:
2734 dput(leader);
2735out:
2736 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
Randy Dunlap0895e912007-10-21 21:00:10 -07002739/**
2740 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2741 * @task: task that should be flushed.
2742 *
2743 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002744 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002745 * in. This call is supposed to do all of this job.
2746 *
2747 * Looks in the dcache for
2748 * /proc/@pid
2749 * /proc/@tgid/task/@pid
2750 * if either directory is present flushes it and all of it'ts children
2751 * from the dcache.
2752 *
2753 * It is safe and reasonable to cache /proc entries for a task until
2754 * that task exits. After that they just clog up the dcache with
2755 * useless entries, possibly causing useful dcache entries to be
2756 * flushed instead. This routine is proved to flush those useless
2757 * dcache entries at process exit time.
2758 *
2759 * NOTE: This routine is just an optimization so it does not guarantee
2760 * that no dcache entries will exist at process exit time it
2761 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002762 */
2763
2764void proc_flush_task(struct task_struct *task)
2765{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002766 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002767 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002768 struct upid *upid;
2769
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002770 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002771 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002772
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002773 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002774 upid = &pid->numbers[i];
2775 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002776 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002777 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002778}
2779
Al Viroc52a47a2013-06-15 11:15:20 +04002780static int proc_pid_instantiate(struct inode *dir,
2781 struct dentry * dentry,
2782 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002783{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002784 struct inode *inode;
2785
Eric W. Biederman61a28782006-10-02 02:18:49 -07002786 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002787 if (!inode)
2788 goto out;
2789
2790 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2791 inode->i_op = &proc_tgid_base_inode_operations;
2792 inode->i_fop = &proc_tgid_base_operations;
2793 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002794
Miklos Szeredibfe86842011-10-28 14:13:29 +02002795 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2796 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002797
Nick Pigginfb045ad2011-01-07 17:49:55 +11002798 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002799
2800 d_add(dentry, inode);
2801 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002802 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002803 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002804out:
Al Viroc52a47a2013-06-15 11:15:20 +04002805 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002806}
2807
Al Viro00cd8dd2012-06-10 17:13:09 -04002808struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Al Viroc52a47a2013-06-15 11:15:20 +04002810 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002813 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 tgid = name_to_int(dentry);
2816 if (tgid == ~0U)
2817 goto out;
2818
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002819 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002820 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002821 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (task)
2823 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002824 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (!task)
2826 goto out;
2827
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002828 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002829 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830out:
Al Viroc52a47a2013-06-15 11:15:20 +04002831 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002835 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002836 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002838struct tgid_iter {
2839 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002840 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002841};
2842static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2843{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002844 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002846 if (iter.task)
2847 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002848 rcu_read_lock();
2849retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002850 iter.task = NULL;
2851 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002852 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002853 iter.tgid = pid_nr_ns(pid, ns);
2854 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002855 /* What we to know is if the pid we have find is the
2856 * pid of a thread_group_leader. Testing for task
2857 * being a thread_group_leader is the obvious thing
2858 * todo but there is a window when it fails, due to
2859 * the pid transfer logic in de_thread.
2860 *
2861 * So we perform the straight forward test of seeing
2862 * if the pid we have found is the pid of a thread
2863 * group leader, and don't worry if the task we have
2864 * found doesn't happen to be a thread group leader.
2865 * As we don't care in the case of readdir.
2866 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002867 if (!iter.task || !has_group_leader_pid(iter.task)) {
2868 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002869 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002870 }
2871 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002873 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002874 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875}
2876
Al Viro021ada72013-03-29 19:27:05 -04002877#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002880int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002882 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002883 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002884 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Al Viro021ada72013-03-29 19:27:05 -04002886 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Al Viro021ada72013-03-29 19:27:05 -04002889 if (pos == TGID_OFFSET - 1) {
Al Virodb963162013-06-15 10:45:10 +04002890 struct inode *inode = ns->proc_self->d_inode;
2891 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002892 return 0;
Al Viro021ada72013-03-29 19:27:05 -04002893 iter.tgid = 0;
2894 } else {
2895 iter.tgid = pos - TGID_OFFSET;
2896 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002897 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002898 for (iter = next_tgid(ns, iter);
2899 iter.task;
2900 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002901 char name[PROC_NUMBUF];
2902 int len;
2903 if (!has_pid_permissions(ns, iter.task, 2))
2904 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002905
Al Virof0c3b502013-05-16 12:07:31 -04002906 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2907 ctx->pos = iter.tgid + TGID_OFFSET;
2908 if (!proc_fill_cache(file, ctx, name, len,
2909 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002910 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
Al Virof0c3b502013-05-16 12:07:31 -04002914 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return 0;
2916}
2917
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002918/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002919 * Tasks
2920 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002921static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002922 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002923 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002924 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002925 REG("environ", S_IRUSR, proc_environ_operations),
2926 INF("auxv", S_IRUSR, proc_pid_auxv),
2927 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002928 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002929 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002930#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002931 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002932#endif
john stultz4614a696b2009-12-14 18:00:05 -08002933 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002934#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002935 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002936#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002937 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2938 ONE("stat", S_IRUGO, proc_tid_stat),
2939 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002940 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002941#ifdef CONFIG_CHECKPOINT_RESTORE
2942 REG("children", S_IRUGO, proc_tid_children_operations),
2943#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002945 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002946#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2948 LNK("cwd", proc_cwd_link),
2949 LNK("root", proc_root_link),
2950 LNK("exe", proc_exe_link),
2951 REG("mounts", S_IRUGO, proc_mounts_operations),
2952 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002953#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002954 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002955 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002956 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002957#endif
2958#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002959 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002960#endif
2961#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002962 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002963#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002964#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002965 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002966#endif
2967#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002968 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002969#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002970#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002971 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002972#endif
Paul Menage8793d852007-10-18 23:39:39 -07002973#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002974 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002975#endif
Paul Menagea4243162007-10-18 23:39:35 -07002976#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002977 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002978#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002979 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002980 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002981 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002983 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002984 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002985#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002986#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002987 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002988#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002989#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002990 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002991#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002992#ifdef CONFIG_HARDWALL
2993 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2994#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002995#ifdef CONFIG_USER_NS
2996 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2997 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002998 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002999#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003000};
3001
Al Virof0c3b502013-05-16 12:07:31 -04003002static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003003{
Al Virof0c3b502013-05-16 12:07:31 -04003004 return proc_pident_readdir(file, ctx,
3005 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003006}
3007
Al Viro00cd8dd2012-06-10 17:13:09 -04003008static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3009{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003010 return proc_pident_lookup(dir, dentry,
3011 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012}
3013
Arjan van de Ven00977a52007-02-12 00:55:34 -08003014static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003015 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003016 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003017 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018};
3019
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003020static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003021 .lookup = proc_tid_base_lookup,
3022 .getattr = pid_getattr,
3023 .setattr = proc_setattr,
3024};
3025
Al Viroc52a47a2013-06-15 11:15:20 +04003026static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003027 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003028{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003029 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003030 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003031
3032 if (!inode)
3033 goto out;
3034 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3035 inode->i_op = &proc_tid_base_inode_operations;
3036 inode->i_fop = &proc_tid_base_operations;
3037 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003038
Miklos Szeredibfe86842011-10-28 14:13:29 +02003039 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3040 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003041
Nick Pigginfb045ad2011-01-07 17:49:55 +11003042 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003043
3044 d_add(dentry, inode);
3045 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003046 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003047 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003048out:
Al Viroc52a47a2013-06-15 11:15:20 +04003049 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003050}
3051
Al Viro00cd8dd2012-06-10 17:13:09 -04003052static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003053{
Al Viroc52a47a2013-06-15 11:15:20 +04003054 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003055 struct task_struct *task;
3056 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003057 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003058 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003059
3060 if (!leader)
3061 goto out_no_task;
3062
3063 tid = name_to_int(dentry);
3064 if (tid == ~0U)
3065 goto out;
3066
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003067 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003068 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003069 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003070 if (task)
3071 get_task_struct(task);
3072 rcu_read_unlock();
3073 if (!task)
3074 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003075 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003076 goto out_drop_task;
3077
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003078 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003079out_drop_task:
3080 put_task_struct(task);
3081out:
3082 put_task_struct(leader);
3083out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003084 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003085}
3086
3087/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003088 * Find the first tid of a thread group to return to user space.
3089 *
3090 * Usually this is just the thread group leader, but if the users
3091 * buffer was too small or there was a seek into the middle of the
3092 * directory we have more work todo.
3093 *
3094 * In the case of a short read we start with find_task_by_pid.
3095 *
3096 * In the case of a seek we start with the leader and walk nr
3097 * threads past it.
3098 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003099static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003100 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003101{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003102 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003103
Eric W. Biedermancc288732006-06-26 00:26:01 -07003104 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003105 /* Attempt to start with the pid of a thread */
3106 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003107 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003108 if (pos && (pos->group_leader == leader))
3109 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003110 }
3111
3112 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003113 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003114 if (nr && nr >= get_nr_threads(leader))
3115 goto out;
3116
3117 /* If we haven't found our starting place yet start
3118 * with the leader and walk nr threads forward.
3119 */
3120 for (pos = leader; nr > 0; --nr) {
3121 pos = next_thread(pos);
3122 if (pos == leader) {
3123 pos = NULL;
3124 goto out;
3125 }
3126 }
3127found:
3128 get_task_struct(pos);
3129out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003130 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003131 return pos;
3132}
3133
3134/*
3135 * Find the next thread in the thread list.
3136 * Return NULL if there is an error or no next thread.
3137 *
3138 * The reference to the input task_struct is released.
3139 */
3140static struct task_struct *next_tid(struct task_struct *start)
3141{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003142 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003143 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003144 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003145 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003146 if (thread_group_leader(pos))
3147 pos = NULL;
3148 else
3149 get_task_struct(pos);
3150 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003151 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003152 put_task_struct(start);
3153 return pos;
3154}
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003157static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003159 struct task_struct *leader = NULL;
Al Virof0c3b502013-05-16 12:07:31 -04003160 struct task_struct *task = get_proc_task(file_inode(file));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003161 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003162 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003164 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04003165 return -ENOENT;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003166 rcu_read_lock();
3167 if (pid_alive(task)) {
3168 leader = task->group_leader;
3169 get_task_struct(leader);
3170 }
3171 rcu_read_unlock();
3172 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003173 if (!leader)
Al Virof0c3b502013-05-16 12:07:31 -04003174 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Al Virof0c3b502013-05-16 12:07:31 -04003176 if (!dir_emit_dots(file, ctx))
3177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003179 /* f_version caches the tgid value that the last readdir call couldn't
3180 * return. lseek aka telldir automagically resets f_version to 0.
3181 */
Al Virof0c3b502013-05-16 12:07:31 -04003182 ns = file->f_dentry->d_sb->s_fs_info;
3183 tid = (int)file->f_version;
3184 file->f_version = 0;
3185 for (task = first_tid(leader, tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003186 task;
Al Virof0c3b502013-05-16 12:07:31 -04003187 task = next_tid(task), ctx->pos++) {
3188 char name[PROC_NUMBUF];
3189 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003190 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003191 len = snprintf(name, sizeof(name), "%d", tid);
3192 if (!proc_fill_cache(file, ctx, name, len,
3193 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003194 /* returning this tgid failed, save it as the first
3195 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003196 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003197 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
3201out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003202 put_task_struct(leader);
Al Virof0c3b502013-05-16 12:07:31 -04003203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003205
3206static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3207{
3208 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003209 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003210 generic_fillattr(inode, stat);
3211
Eric W. Biederman99f89552006-06-26 00:25:55 -07003212 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003213 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003214 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003215 }
3216
3217 return 0;
3218}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003219
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003220static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003221 .lookup = proc_task_lookup,
3222 .getattr = proc_task_getattr,
3223 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003224 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003225};
3226
Arjan van de Ven00977a52007-02-12 00:55:34 -08003227static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003228 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003229 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003230 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003231};