blob: cfd178dc76b2fec211d69228dc3fe3dd475640d1 [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
William Roberts21a64572014-02-11 10:12:00 -0800203static int proc_pid_cmdline(struct task_struct *task, char *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
William Roberts21a64572014-02-11 10:12:00 -0800205 return get_cmdline(task, buffer, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static int proc_pid_auxv(struct task_struct *task, char *buffer)
209{
Cong Wange7dcd992012-05-31 16:26:17 -0700210 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500211 int res = PTR_ERR(mm);
212 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300214 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300216 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 res = nwords * sizeof(mm->saved_auxv[0]);
218 if (res > PAGE_SIZE)
219 res = PAGE_SIZE;
220 memcpy(buffer, mm->saved_auxv, res);
221 mmput(mm);
222 }
223 return res;
224}
225
226
227#ifdef CONFIG_KALLSYMS
228/*
229 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
230 * Returns the resolved symbol. If that fails, simply return the address.
231 */
232static int proc_pid_wchan(struct task_struct *task, char *buffer)
233{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700234 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700235 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 wchan = get_wchan(task);
238
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700239 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600240 if (!ptrace_may_access(task, PTRACE_MODE_READ))
241 return 0;
242 else
243 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700244 else
245 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247#endif /* CONFIG_KALLSYMS */
248
Al Viroa9712bc2011-03-23 15:52:50 -0400249static int lock_trace(struct task_struct *task)
250{
251 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
252 if (err)
253 return err;
254 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
255 mutex_unlock(&task->signal->cred_guard_mutex);
256 return -EPERM;
257 }
258 return 0;
259}
260
261static void unlock_trace(struct task_struct *task)
262{
263 mutex_unlock(&task->signal->cred_guard_mutex);
264}
265
Ken Chen2ec220e2008-11-10 11:26:08 +0300266#ifdef CONFIG_STACKTRACE
267
268#define MAX_STACK_TRACE_DEPTH 64
269
270static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
271 struct pid *pid, struct task_struct *task)
272{
273 struct stack_trace trace;
274 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400275 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300276 int i;
277
278 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
279 if (!entries)
280 return -ENOMEM;
281
282 trace.nr_entries = 0;
283 trace.max_entries = MAX_STACK_TRACE_DEPTH;
284 trace.entries = entries;
285 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300286
Al Viroa9712bc2011-03-23 15:52:50 -0400287 err = lock_trace(task);
288 if (!err) {
289 save_stack_trace_tsk(task, &trace);
290
291 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700292 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400293 (void *)entries[i], (void *)entries[i]);
294 }
295 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300296 }
297 kfree(entries);
298
Al Viroa9712bc2011-03-23 15:52:50 -0400299 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300300}
301#endif
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#ifdef CONFIG_SCHEDSTATS
304/*
305 * Provides /proc/PID/schedstat
306 */
307static int proc_pid_schedstat(struct task_struct *task, char *buffer)
308{
Balbir Singh172ba842007-07-09 18:52:00 +0200309 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100310 (unsigned long long)task->se.sum_exec_runtime,
311 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200312 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314#endif
315
Arjan van de Ven97455122008-01-25 21:08:34 +0100316#ifdef CONFIG_LATENCYTOP
317static int lstats_show_proc(struct seq_file *m, void *v)
318{
319 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800320 struct inode *inode = m->private;
321 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100322
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800323 if (!task)
324 return -ESRCH;
325 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100326 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800327 struct latency_record *lr = &task->latency_record[i];
328 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100329 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800330 seq_printf(m, "%i %li %li",
331 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100332 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800333 unsigned long bt = lr->backtrace[q];
334 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100335 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800336 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100337 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800338 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100339 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800340 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100341 }
342
343 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800344 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100345 return 0;
346}
347
348static int lstats_open(struct inode *inode, struct file *file)
349{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800350 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800351}
352
Arjan van de Ven97455122008-01-25 21:08:34 +0100353static ssize_t lstats_write(struct file *file, const char __user *buf,
354 size_t count, loff_t *offs)
355{
Al Viro496ad9a2013-01-23 17:07:38 -0500356 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100357
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800358 if (!task)
359 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100360 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800361 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100362
363 return count;
364}
365
366static const struct file_operations proc_lstats_operations = {
367 .open = lstats_open,
368 .read = seq_read,
369 .write = lstats_write,
370 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800371 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100372};
373
374#endif
375
Al Viro8d8b97b2013-04-19 23:11:24 -0400376#ifdef CONFIG_CGROUPS
377static int cgroup_open(struct inode *inode, struct file *file)
378{
379 struct pid *pid = PROC_I(inode)->pid;
380 return single_open(file, proc_cgroup_show, pid);
381}
382
383static const struct file_operations proc_cgroup_operations = {
384 .open = cgroup_open,
385 .read = seq_read,
386 .llseek = seq_lseek,
387 .release = single_release,
388};
389#endif
390
391#ifdef CONFIG_PROC_PID_CPUSET
392
393static int cpuset_open(struct inode *inode, struct file *file)
394{
395 struct pid *pid = PROC_I(inode)->pid;
396 return single_open(file, proc_cpuset_show, pid);
397}
398
399static const struct file_operations proc_cpuset_operations = {
400 .open = cpuset_open,
401 .read = seq_read,
402 .llseek = seq_lseek,
403 .release = single_release,
404};
405#endif
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static int proc_oom_score(struct task_struct *task, char *buffer)
408{
David Rientjesa7f638f2012-05-29 15:06:47 -0700409 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200410 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700412 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200413 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700414 points = oom_badness(task, NULL, NULL, totalpages) *
415 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700416 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return sprintf(buffer, "%lu\n", points);
418}
419
Neil Hormand85f50d2007-10-18 23:40:37 -0700420struct limit_names {
421 char *name;
422 char *unit;
423};
424
425static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700426 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700427 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
428 [RLIMIT_DATA] = {"Max data size", "bytes"},
429 [RLIMIT_STACK] = {"Max stack size", "bytes"},
430 [RLIMIT_CORE] = {"Max core file size", "bytes"},
431 [RLIMIT_RSS] = {"Max resident set", "bytes"},
432 [RLIMIT_NPROC] = {"Max processes", "processes"},
433 [RLIMIT_NOFILE] = {"Max open files", "files"},
434 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
435 [RLIMIT_AS] = {"Max address space", "bytes"},
436 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
437 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
438 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
439 [RLIMIT_NICE] = {"Max nice priority", NULL},
440 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800441 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700442};
443
444/* Display limits for a process */
445static int proc_pid_limits(struct task_struct *task, char *buffer)
446{
447 unsigned int i;
448 int count = 0;
449 unsigned long flags;
450 char *bufptr = buffer;
451
452 struct rlimit rlim[RLIM_NLIMITS];
453
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400454 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700455 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700456 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
457 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700458
459 /*
460 * print the file header
461 */
462 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
463 "Limit", "Soft Limit", "Hard Limit", "Units");
464
465 for (i = 0; i < RLIM_NLIMITS; i++) {
466 if (rlim[i].rlim_cur == RLIM_INFINITY)
467 count += sprintf(&bufptr[count], "%-25s %-20s ",
468 lnames[i].name, "unlimited");
469 else
470 count += sprintf(&bufptr[count], "%-25s %-20lu ",
471 lnames[i].name, rlim[i].rlim_cur);
472
473 if (rlim[i].rlim_max == RLIM_INFINITY)
474 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
475 else
476 count += sprintf(&bufptr[count], "%-20lu ",
477 rlim[i].rlim_max);
478
479 if (lnames[i].unit)
480 count += sprintf(&bufptr[count], "%-10s\n",
481 lnames[i].unit);
482 else
483 count += sprintf(&bufptr[count], "\n");
484 }
485
486 return count;
487}
488
Roland McGrathebcb6732008-07-25 19:46:00 -0700489#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
490static int proc_pid_syscall(struct task_struct *task, char *buffer)
491{
492 long nr;
493 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400494 int res = lock_trace(task);
495 if (res)
496 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700497
498 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400499 res = sprintf(buffer, "running\n");
500 else if (nr < 0)
501 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
502 else
503 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700504 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
505 nr,
506 args[0], args[1], args[2], args[3], args[4], args[5],
507 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400508 unlock_trace(task);
509 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700510}
511#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/************************************************************************/
514/* Here the fs part begins */
515/************************************************************************/
516
517/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700518static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700520 struct task_struct *task;
521 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700522 /* Allow access to a task's file descriptors if it is us or we
523 * may use ptrace attach to the process and find out that
524 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700525 */
526 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700527 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400528 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700529 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700530 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700531 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800534int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700535{
536 int error;
537 struct inode *inode = dentry->d_inode;
538
539 if (attr->ia_valid & ATTR_MODE)
540 return -EPERM;
541
542 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200543 if (error)
544 return error;
545
Christoph Hellwig10257742010-06-04 11:30:02 +0200546 setattr_copy(inode, attr);
547 mark_inode_dirty(inode);
548 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700549}
550
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800551/*
552 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
553 * or euid/egid (for hide_pid_min=2)?
554 */
555static bool has_pid_permissions(struct pid_namespace *pid,
556 struct task_struct *task,
557 int hide_pid_min)
558{
559 if (pid->hide_pid < hide_pid_min)
560 return true;
561 if (in_group_p(pid->pid_gid))
562 return true;
563 return ptrace_may_access(task, PTRACE_MODE_READ);
564}
565
566
567static int proc_pid_permission(struct inode *inode, int mask)
568{
569 struct pid_namespace *pid = inode->i_sb->s_fs_info;
570 struct task_struct *task;
571 bool has_perms;
572
573 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800574 if (!task)
575 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800576 has_perms = has_pid_permissions(pid, task, 1);
577 put_task_struct(task);
578
579 if (!has_perms) {
580 if (pid->hide_pid == 2) {
581 /*
582 * Let's make getdents(), stat(), and open()
583 * consistent with each other. If a process
584 * may not stat() a file, it shouldn't be seen
585 * in procfs at all.
586 */
587 return -ENOENT;
588 }
589
590 return -EPERM;
591 }
592 return generic_permission(inode, mask);
593}
594
595
596
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800597static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700598 .setattr = proc_setattr,
599};
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
602
603static ssize_t proc_info_read(struct file * file, char __user * buf,
604 size_t count, loff_t *ppos)
605{
Al Viro496ad9a2013-01-23 17:07:38 -0500606 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 unsigned long page;
608 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700609 struct task_struct *task = get_proc_task(inode);
610
611 length = -ESRCH;
612 if (!task)
613 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (count > PROC_BLOCK_SIZE)
616 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700617
618 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700619 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 length = PROC_I(inode)->op.proc_read(task, (char*)page);
623
624 if (length >= 0)
625 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
626 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700627out:
628 put_task_struct(task);
629out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return length;
631}
632
Arjan van de Ven00977a52007-02-12 00:55:34 -0800633static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100635 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636};
637
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800638static int proc_single_show(struct seq_file *m, void *v)
639{
640 struct inode *inode = m->private;
641 struct pid_namespace *ns;
642 struct pid *pid;
643 struct task_struct *task;
644 int ret;
645
646 ns = inode->i_sb->s_fs_info;
647 pid = proc_pid(inode);
648 task = get_pid_task(pid, PIDTYPE_PID);
649 if (!task)
650 return -ESRCH;
651
652 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
653
654 put_task_struct(task);
655 return ret;
656}
657
658static int proc_single_open(struct inode *inode, struct file *filp)
659{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800660 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800661}
662
663static const struct file_operations proc_single_file_operations = {
664 .open = proc_single_open,
665 .read = seq_read,
666 .llseek = seq_lseek,
667 .release = single_release,
668};
669
Cong Wangb409e572012-05-31 16:26:17 -0700670static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Al Viro496ad9a2013-01-23 17:07:38 -0500672 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800673 struct mm_struct *mm;
674
675 if (!task)
676 return -ESRCH;
677
Cong Wangb409e572012-05-31 16:26:17 -0700678 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800679 put_task_struct(task);
680
681 if (IS_ERR(mm))
682 return PTR_ERR(mm);
683
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100684 if (mm) {
685 /* ensure this mm_struct can't be freed */
686 atomic_inc(&mm->mm_count);
687 /* but do not pin its memory */
688 mmput(mm);
689 }
690
Linus Torvaldse2683372012-01-17 15:21:19 -0800691 file->private_data = mm;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return 0;
694}
695
Cong Wangb409e572012-05-31 16:26:17 -0700696static int mem_open(struct inode *inode, struct file *file)
697{
Djalal Harounibc452b42012-07-30 14:42:28 -0700698 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
699
700 /* OK to pass negative loff_t, we can catch out-of-range */
701 file->f_mode |= FMODE_UNSIGNED_OFFSET;
702
703 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700704}
705
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100706static ssize_t mem_rw(struct file *file, char __user *buf,
707 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
Linus Torvaldse2683372012-01-17 15:21:19 -0800709 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100710 unsigned long addr = *ppos;
711 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Linus Torvaldse2683372012-01-17 15:21:19 -0800714 if (!mm)
715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Mel Gormane12ba742007-10-16 01:25:52 -0700717 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800719 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700721 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100722 if (!atomic_inc_not_zero(&mm->mm_users))
723 goto free;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100726 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100728 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 copied = -EFAULT;
730 break;
731 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100732
733 this_len = access_remote_vm(mm, addr, page, this_len, write);
734 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!copied)
736 copied = -EIO;
737 break;
738 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100739
740 if (!write && copy_to_user(buf, page, this_len)) {
741 copied = -EFAULT;
742 break;
743 }
744
745 buf += this_len;
746 addr += this_len;
747 copied += this_len;
748 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100750 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700751
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100752 mmput(mm);
753free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700754 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return copied;
756}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100758static ssize_t mem_read(struct file *file, char __user *buf,
759 size_t count, loff_t *ppos)
760{
761 return mem_rw(file, buf, count, ppos, 0);
762}
763
764static ssize_t mem_write(struct file *file, const char __user *buf,
765 size_t count, loff_t *ppos)
766{
767 return mem_rw(file, (char __user*)buf, count, ppos, 1);
768}
769
Matt Mackall85863e42008-02-04 22:29:04 -0800770loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 switch (orig) {
773 case 0:
774 file->f_pos = offset;
775 break;
776 case 1:
777 file->f_pos += offset;
778 break;
779 default:
780 return -EINVAL;
781 }
782 force_successful_syscall_return();
783 return file->f_pos;
784}
785
Linus Torvaldse2683372012-01-17 15:21:19 -0800786static int mem_release(struct inode *inode, struct file *file)
787{
788 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100789 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100790 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800791 return 0;
792}
793
Arjan van de Ven00977a52007-02-12 00:55:34 -0800794static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .llseek = mem_lseek,
796 .read = mem_read,
797 .write = mem_write,
798 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800799 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800};
801
Cong Wangb409e572012-05-31 16:26:17 -0700802static int environ_open(struct inode *inode, struct file *file)
803{
804 return __mem_open(inode, file, PTRACE_MODE_READ);
805}
806
James Pearson315e28c2007-10-16 23:30:17 -0700807static ssize_t environ_read(struct file *file, char __user *buf,
808 size_t count, loff_t *ppos)
809{
James Pearson315e28c2007-10-16 23:30:17 -0700810 char *page;
811 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700812 int ret = 0;
813 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700814
Cong Wangb409e572012-05-31 16:26:17 -0700815 if (!mm)
816 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700817
James Pearson315e28c2007-10-16 23:30:17 -0700818 page = (char *)__get_free_page(GFP_TEMPORARY);
819 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700820 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700821
Al Virod6f64b82011-02-15 22:26:01 -0500822 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700823 if (!atomic_inc_not_zero(&mm->mm_users))
824 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700825 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700826 size_t this_len, max_len;
827 int retval;
828
829 if (src >= (mm->env_end - mm->env_start))
830 break;
James Pearson315e28c2007-10-16 23:30:17 -0700831
832 this_len = mm->env_end - (mm->env_start + src);
833
Djalal Harounie8905ec2012-07-30 14:42:26 -0700834 max_len = min_t(size_t, PAGE_SIZE, count);
835 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700836
Cong Wangb409e572012-05-31 16:26:17 -0700837 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700838 page, this_len, 0);
839
840 if (retval <= 0) {
841 ret = retval;
842 break;
843 }
844
845 if (copy_to_user(buf, page, retval)) {
846 ret = -EFAULT;
847 break;
848 }
849
850 ret += retval;
851 src += retval;
852 buf += retval;
853 count -= retval;
854 }
855 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700856 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700857
858free:
James Pearson315e28c2007-10-16 23:30:17 -0700859 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700860 return ret;
861}
862
863static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700864 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700865 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100866 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700867 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700868};
869
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800870static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
871 loff_t *ppos)
872{
Al Viro496ad9a2013-01-23 17:07:38 -0500873 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800874 char buffer[PROC_NUMBUF];
875 int oom_adj = OOM_ADJUST_MIN;
876 size_t len;
877 unsigned long flags;
878
879 if (!task)
880 return -ESRCH;
881 if (lock_task_sighand(task, &flags)) {
882 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
883 oom_adj = OOM_ADJUST_MAX;
884 else
885 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
886 OOM_SCORE_ADJ_MAX;
887 unlock_task_sighand(task, &flags);
888 }
889 put_task_struct(task);
890 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
891 return simple_read_from_buffer(buf, count, ppos, buffer, len);
892}
893
894static ssize_t oom_adj_write(struct file *file, const char __user *buf,
895 size_t count, loff_t *ppos)
896{
897 struct task_struct *task;
898 char buffer[PROC_NUMBUF];
899 int oom_adj;
900 unsigned long flags;
901 int err;
902
903 memset(buffer, 0, sizeof(buffer));
904 if (count > sizeof(buffer) - 1)
905 count = sizeof(buffer) - 1;
906 if (copy_from_user(buffer, buf, count)) {
907 err = -EFAULT;
908 goto out;
909 }
910
911 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
912 if (err)
913 goto out;
914 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
915 oom_adj != OOM_DISABLE) {
916 err = -EINVAL;
917 goto out;
918 }
919
Al Viro496ad9a2013-01-23 17:07:38 -0500920 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800921 if (!task) {
922 err = -ESRCH;
923 goto out;
924 }
925
926 task_lock(task);
927 if (!task->mm) {
928 err = -EINVAL;
929 goto err_task_lock;
930 }
931
932 if (!lock_task_sighand(task, &flags)) {
933 err = -ESRCH;
934 goto err_task_lock;
935 }
936
937 /*
938 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
939 * value is always attainable.
940 */
941 if (oom_adj == OOM_ADJUST_MAX)
942 oom_adj = OOM_SCORE_ADJ_MAX;
943 else
944 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
945
946 if (oom_adj < task->signal->oom_score_adj &&
947 !capable(CAP_SYS_RESOURCE)) {
948 err = -EACCES;
949 goto err_sighand;
950 }
951
952 /*
953 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
954 * /proc/pid/oom_score_adj instead.
955 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800956 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 -0800957 current->comm, task_pid_nr(current), task_pid_nr(task),
958 task_pid_nr(task));
959
960 task->signal->oom_score_adj = oom_adj;
961 trace_oom_score_adj_update(task);
962err_sighand:
963 unlock_task_sighand(task, &flags);
964err_task_lock:
965 task_unlock(task);
966 put_task_struct(task);
967out:
968 return err < 0 ? err : count;
969}
970
971static const struct file_operations proc_oom_adj_operations = {
972 .read = oom_adj_read,
973 .write = oom_adj_write,
974 .llseek = generic_file_llseek,
975};
976
David Rientjesa63d83f2010-08-09 17:19:46 -0700977static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
978 size_t count, loff_t *ppos)
979{
Al Viro496ad9a2013-01-23 17:07:38 -0500980 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700981 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800982 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700983 unsigned long flags;
984 size_t len;
985
986 if (!task)
987 return -ESRCH;
988 if (lock_task_sighand(task, &flags)) {
989 oom_score_adj = task->signal->oom_score_adj;
990 unlock_task_sighand(task, &flags);
991 }
992 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800993 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700994 return simple_read_from_buffer(buf, count, ppos, buffer, len);
995}
996
997static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
998 size_t count, loff_t *ppos)
999{
1000 struct task_struct *task;
1001 char buffer[PROC_NUMBUF];
1002 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001003 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001004 int err;
1005
1006 memset(buffer, 0, sizeof(buffer));
1007 if (count > sizeof(buffer) - 1)
1008 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001009 if (copy_from_user(buffer, buf, count)) {
1010 err = -EFAULT;
1011 goto out;
1012 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001013
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001014 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001015 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001016 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001017 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001018 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1019 err = -EINVAL;
1020 goto out;
1021 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001022
Al Viro496ad9a2013-01-23 17:07:38 -05001023 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001024 if (!task) {
1025 err = -ESRCH;
1026 goto out;
1027 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001028
Ying Han3d5992d2010-10-26 14:21:23 -07001029 task_lock(task);
1030 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001031 err = -EINVAL;
1032 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001033 }
David Rientjesd19d5472010-10-26 14:21:26 -07001034
1035 if (!lock_task_sighand(task, &flags)) {
1036 err = -ESRCH;
1037 goto err_task_lock;
1038 }
1039
David Rientjesa9c58b902012-12-11 16:02:54 -08001040 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001041 !capable(CAP_SYS_RESOURCE)) {
1042 err = -EACCES;
1043 goto err_sighand;
1044 }
1045
David Rientjesa9c58b902012-12-11 16:02:54 -08001046 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001047 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001048 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001049 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001050
David Rientjes723548b2010-10-26 14:21:25 -07001051err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001052 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001053err_task_lock:
1054 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001055 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001056out:
1057 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001058}
1059
1060static const struct file_operations proc_oom_score_adj_operations = {
1061 .read = oom_score_adj_read,
1062 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001063 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001064};
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066#ifdef CONFIG_AUDITSYSCALL
1067#define TMPBUFLEN 21
1068static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1069 size_t count, loff_t *ppos)
1070{
Al Viro496ad9a2013-01-23 17:07:38 -05001071 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001072 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 ssize_t length;
1074 char tmpbuf[TMPBUFLEN];
1075
Eric W. Biederman99f89552006-06-26 00:25:55 -07001076 if (!task)
1077 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001079 from_kuid(file->f_cred->user_ns,
1080 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001081 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1083}
1084
1085static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1086 size_t count, loff_t *ppos)
1087{
Al Viro496ad9a2013-01-23 17:07:38 -05001088 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 char *page, *tmp;
1090 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001092 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001094 rcu_read_lock();
1095 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1096 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001098 }
1099 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Al Viroe0182902006-05-18 08:28:02 -04001101 if (count >= PAGE_SIZE)
1102 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 if (*ppos != 0) {
1105 /* No partial writes. */
1106 return -EINVAL;
1107 }
Mel Gormane12ba742007-10-16 01:25:52 -07001108 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (!page)
1110 return -ENOMEM;
1111 length = -EFAULT;
1112 if (copy_from_user(page, buf, count))
1113 goto out_free_page;
1114
Al Viroe0182902006-05-18 08:28:02 -04001115 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 loginuid = simple_strtoul(page, &tmp, 10);
1117 if (tmp == page) {
1118 length = -EINVAL;
1119 goto out_free_page;
1120
1121 }
Eric Paris81407c82013-05-24 09:49:14 -04001122
1123 /* is userspace tring to explicitly UNSET the loginuid? */
1124 if (loginuid == AUDIT_UID_UNSET) {
1125 kloginuid = INVALID_UID;
1126 } else {
1127 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1128 if (!uid_valid(kloginuid)) {
1129 length = -EINVAL;
1130 goto out_free_page;
1131 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001132 }
1133
1134 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (likely(length == 0))
1136 length = count;
1137
1138out_free_page:
1139 free_page((unsigned long) page);
1140 return length;
1141}
1142
Arjan van de Ven00977a52007-02-12 00:55:34 -08001143static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 .read = proc_loginuid_read,
1145 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001146 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147};
Eric Paris1e0bd752008-03-13 08:15:31 -04001148
1149static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1150 size_t count, loff_t *ppos)
1151{
Al Viro496ad9a2013-01-23 17:07:38 -05001152 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001153 struct task_struct *task = get_proc_task(inode);
1154 ssize_t length;
1155 char tmpbuf[TMPBUFLEN];
1156
1157 if (!task)
1158 return -ESRCH;
1159 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1160 audit_get_sessionid(task));
1161 put_task_struct(task);
1162 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1163}
1164
1165static const struct file_operations proc_sessionid_operations = {
1166 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001167 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001168};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169#endif
1170
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001171#ifdef CONFIG_FAULT_INJECTION
1172static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1173 size_t count, loff_t *ppos)
1174{
Al Viro496ad9a2013-01-23 17:07:38 -05001175 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001176 char buffer[PROC_NUMBUF];
1177 size_t len;
1178 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001179
1180 if (!task)
1181 return -ESRCH;
1182 make_it_fail = task->make_it_fail;
1183 put_task_struct(task);
1184
1185 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001186
1187 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001188}
1189
1190static ssize_t proc_fault_inject_write(struct file * file,
1191 const char __user * buf, size_t count, loff_t *ppos)
1192{
1193 struct task_struct *task;
1194 char buffer[PROC_NUMBUF], *end;
1195 int make_it_fail;
1196
1197 if (!capable(CAP_SYS_RESOURCE))
1198 return -EPERM;
1199 memset(buffer, 0, sizeof(buffer));
1200 if (count > sizeof(buffer) - 1)
1201 count = sizeof(buffer) - 1;
1202 if (copy_from_user(buffer, buf, count))
1203 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001204 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1205 if (*end)
1206 return -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001207 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001208 if (!task)
1209 return -ESRCH;
1210 task->make_it_fail = make_it_fail;
1211 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001212
1213 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001214}
1215
Arjan van de Ven00977a52007-02-12 00:55:34 -08001216static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001217 .read = proc_fault_inject_read,
1218 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001219 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001220};
1221#endif
1222
Arjan van de Ven97455122008-01-25 21:08:34 +01001223
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001224#ifdef CONFIG_SCHED_DEBUG
1225/*
1226 * Print out various scheduling related per-task fields:
1227 */
1228static int sched_show(struct seq_file *m, void *v)
1229{
1230 struct inode *inode = m->private;
1231 struct task_struct *p;
1232
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001233 p = get_proc_task(inode);
1234 if (!p)
1235 return -ESRCH;
1236 proc_sched_show_task(p, m);
1237
1238 put_task_struct(p);
1239
1240 return 0;
1241}
1242
1243static ssize_t
1244sched_write(struct file *file, const char __user *buf,
1245 size_t count, loff_t *offset)
1246{
Al Viro496ad9a2013-01-23 17:07:38 -05001247 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001248 struct task_struct *p;
1249
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001250 p = get_proc_task(inode);
1251 if (!p)
1252 return -ESRCH;
1253 proc_sched_set_task(p);
1254
1255 put_task_struct(p);
1256
1257 return count;
1258}
1259
1260static int sched_open(struct inode *inode, struct file *filp)
1261{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001262 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001263}
1264
1265static const struct file_operations proc_pid_sched_operations = {
1266 .open = sched_open,
1267 .read = seq_read,
1268 .write = sched_write,
1269 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001270 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001271};
1272
1273#endif
1274
Mike Galbraith5091faa2010-11-30 14:18:03 +01001275#ifdef CONFIG_SCHED_AUTOGROUP
1276/*
1277 * Print out autogroup related information:
1278 */
1279static int sched_autogroup_show(struct seq_file *m, void *v)
1280{
1281 struct inode *inode = m->private;
1282 struct task_struct *p;
1283
1284 p = get_proc_task(inode);
1285 if (!p)
1286 return -ESRCH;
1287 proc_sched_autogroup_show_task(p, m);
1288
1289 put_task_struct(p);
1290
1291 return 0;
1292}
1293
1294static ssize_t
1295sched_autogroup_write(struct file *file, const char __user *buf,
1296 size_t count, loff_t *offset)
1297{
Al Viro496ad9a2013-01-23 17:07:38 -05001298 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001299 struct task_struct *p;
1300 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001301 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001302 int err;
1303
1304 memset(buffer, 0, sizeof(buffer));
1305 if (count > sizeof(buffer) - 1)
1306 count = sizeof(buffer) - 1;
1307 if (copy_from_user(buffer, buf, count))
1308 return -EFAULT;
1309
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001310 err = kstrtoint(strstrip(buffer), 0, &nice);
1311 if (err < 0)
1312 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001313
1314 p = get_proc_task(inode);
1315 if (!p)
1316 return -ESRCH;
1317
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001318 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001319 if (err)
1320 count = err;
1321
1322 put_task_struct(p);
1323
1324 return count;
1325}
1326
1327static int sched_autogroup_open(struct inode *inode, struct file *filp)
1328{
1329 int ret;
1330
1331 ret = single_open(filp, sched_autogroup_show, NULL);
1332 if (!ret) {
1333 struct seq_file *m = filp->private_data;
1334
1335 m->private = inode;
1336 }
1337 return ret;
1338}
1339
1340static const struct file_operations proc_pid_sched_autogroup_operations = {
1341 .open = sched_autogroup_open,
1342 .read = seq_read,
1343 .write = sched_autogroup_write,
1344 .llseek = seq_lseek,
1345 .release = single_release,
1346};
1347
1348#endif /* CONFIG_SCHED_AUTOGROUP */
1349
john stultz4614a696b2009-12-14 18:00:05 -08001350static ssize_t comm_write(struct file *file, const char __user *buf,
1351 size_t count, loff_t *offset)
1352{
Al Viro496ad9a2013-01-23 17:07:38 -05001353 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001354 struct task_struct *p;
1355 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001356 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001357
1358 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001359 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001360 return -EFAULT;
1361
1362 p = get_proc_task(inode);
1363 if (!p)
1364 return -ESRCH;
1365
1366 if (same_thread_group(current, p))
1367 set_task_comm(p, buffer);
1368 else
1369 count = -EINVAL;
1370
1371 put_task_struct(p);
1372
1373 return count;
1374}
1375
1376static int comm_show(struct seq_file *m, void *v)
1377{
1378 struct inode *inode = m->private;
1379 struct task_struct *p;
1380
1381 p = get_proc_task(inode);
1382 if (!p)
1383 return -ESRCH;
1384
1385 task_lock(p);
1386 seq_printf(m, "%s\n", p->comm);
1387 task_unlock(p);
1388
1389 put_task_struct(p);
1390
1391 return 0;
1392}
1393
1394static int comm_open(struct inode *inode, struct file *filp)
1395{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001396 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001397}
1398
1399static const struct file_operations proc_pid_set_comm_operations = {
1400 .open = comm_open,
1401 .read = seq_read,
1402 .write = comm_write,
1403 .llseek = seq_lseek,
1404 .release = single_release,
1405};
1406
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001407static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001408{
1409 struct task_struct *task;
1410 struct mm_struct *mm;
1411 struct file *exe_file;
1412
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001413 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001414 if (!task)
1415 return -ENOENT;
1416 mm = get_task_mm(task);
1417 put_task_struct(task);
1418 if (!mm)
1419 return -ENOENT;
1420 exe_file = get_mm_exe_file(mm);
1421 mmput(mm);
1422 if (exe_file) {
1423 *exe_path = exe_file->f_path;
1424 path_get(&exe_file->f_path);
1425 fput(exe_file);
1426 return 0;
1427 } else
1428 return -ENOENT;
1429}
1430
Al Viro008b1502005-08-20 00:17:39 +01001431static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001434 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 int error = -EACCES;
1436
Eric W. Biederman778c1142006-06-26 00:25:58 -07001437 /* Are we allowed to snoop on the tasks file descriptors? */
1438 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Christoph Hellwig408ef012012-06-18 10:47:03 -04001441 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1442 if (error)
1443 goto out;
1444
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001445 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001446 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447out:
Al Viro008b1502005-08-20 00:17:39 +01001448 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001451static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Mel Gormane12ba742007-10-16 01:25:52 -07001453 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001454 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 int len;
1456
1457 if (!tmp)
1458 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001459
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001460 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001461 len = PTR_ERR(pathname);
1462 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001464 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466 if (len > buflen)
1467 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001468 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 len = -EFAULT;
1470 out:
1471 free_page((unsigned long)tmp);
1472 return len;
1473}
1474
1475static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1476{
1477 int error = -EACCES;
1478 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001479 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Eric W. Biederman778c1142006-06-26 00:25:58 -07001481 /* Are we allowed to snoop on the tasks file descriptors? */
1482 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001485 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (error)
1487 goto out;
1488
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001489 error = do_proc_readlink(&path, buffer, buflen);
1490 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return error;
1493}
1494
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001495const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001497 .follow_link = proc_pid_follow_link,
1498 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499};
1500
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001501
1502/* building an inode */
1503
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001504struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001505{
1506 struct inode * inode;
1507 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001508 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001509
1510 /* We need a new inode */
1511
1512 inode = new_inode(sb);
1513 if (!inode)
1514 goto out;
1515
1516 /* Common stuff */
1517 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001518 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001519 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001520 inode->i_op = &proc_def_inode_operations;
1521
1522 /*
1523 * grab the reference to task.
1524 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001525 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001526 if (!ei->pid)
1527 goto out_unlock;
1528
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001529 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001530 rcu_read_lock();
1531 cred = __task_cred(task);
1532 inode->i_uid = cred->euid;
1533 inode->i_gid = cred->egid;
1534 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001535 }
1536 security_task_to_inode(task, inode);
1537
1538out:
1539 return inode;
1540
1541out_unlock:
1542 iput(inode);
1543 return NULL;
1544}
1545
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001546int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001547{
1548 struct inode *inode = dentry->d_inode;
1549 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001550 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001551 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001552
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001553 generic_fillattr(inode, stat);
1554
1555 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001556 stat->uid = GLOBAL_ROOT_UID;
1557 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001558 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1559 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001560 if (!has_pid_permissions(pid, task, 2)) {
1561 rcu_read_unlock();
1562 /*
1563 * This doesn't prevent learning whether PID exists,
1564 * it only makes getattr() consistent with readdir().
1565 */
1566 return -ENOENT;
1567 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001568 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1569 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001570 cred = __task_cred(task);
1571 stat->uid = cred->euid;
1572 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001573 }
1574 }
1575 rcu_read_unlock();
1576 return 0;
1577}
1578
1579/* dentry stuff */
1580
1581/*
1582 * Exceptional case: normally we are not allowed to unhash a busy
1583 * directory. In this case, however, we can do it - no aliasing problems
1584 * due to the way we treat inodes.
1585 *
1586 * Rewrite the inode's ownerships here because the owning task may have
1587 * performed a setuid(), etc.
1588 *
1589 * Before the /proc/pid/status file was created the only way to read
1590 * the effective uid of a /process was to stat /proc/pid. Reading
1591 * /proc/pid/status is slow enough that procps and other packages
1592 * kept stating /proc/pid. To keep the rules in /proc simple I have
1593 * made this apply to all per process world readable and executable
1594 * directories.
1595 */
Al Viro0b728e12012-06-10 16:03:43 -04001596int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001597{
Nick Piggin34286d62011-01-07 17:49:57 +11001598 struct inode *inode;
1599 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001600 const struct cred *cred;
1601
Al Viro0b728e12012-06-10 16:03:43 -04001602 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001603 return -ECHILD;
1604
1605 inode = dentry->d_inode;
1606 task = get_proc_task(inode);
1607
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001608 if (task) {
1609 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1610 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001611 rcu_read_lock();
1612 cred = __task_cred(task);
1613 inode->i_uid = cred->euid;
1614 inode->i_gid = cred->egid;
1615 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001616 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001617 inode->i_uid = GLOBAL_ROOT_UID;
1618 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001619 }
1620 inode->i_mode &= ~(S_ISUID | S_ISGID);
1621 security_task_to_inode(task, inode);
1622 put_task_struct(task);
1623 return 1;
1624 }
1625 d_drop(dentry);
1626 return 0;
1627}
1628
David Howells1dd704b2013-04-12 01:08:50 +01001629int pid_delete_dentry(const struct dentry *dentry)
1630{
1631 /* Is the task we represent dead?
1632 * If so, then don't put the dentry on the lru list,
1633 * kill it immediately.
1634 */
1635 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1636}
1637
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001638const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001639{
1640 .d_revalidate = pid_revalidate,
1641 .d_delete = pid_delete_dentry,
1642};
1643
1644/* Lookups */
1645
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001646/*
1647 * Fill a directory entry.
1648 *
1649 * If possible create the dcache entry and derive our inode number and
1650 * file type from dcache entry.
1651 *
1652 * Since all of the proc inode numbers are dynamically generated, the inode
1653 * numbers do not exist until the inode is cache. This means creating the
1654 * the dcache entry in readdir is necessary to keep the inode numbers
1655 * reported by readdir in sync with the inode numbers reported
1656 * by stat.
1657 */
Al Virof0c3b502013-05-16 12:07:31 -04001658bool proc_fill_cache(struct file *file, struct dir_context *ctx,
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001659 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001660 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001661{
Al Virof0c3b502013-05-16 12:07:31 -04001662 struct dentry *child, *dir = file->f_path.dentry;
Al Viro1df98b82013-06-15 11:33:10 +04001663 struct qstr qname = QSTR_INIT(name, len);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001664 struct inode *inode;
Al Viro1df98b82013-06-15 11:33:10 +04001665 unsigned type;
1666 ino_t ino;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001667
Al Viro1df98b82013-06-15 11:33:10 +04001668 child = d_hash_and_lookup(dir, &qname);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001669 if (!child) {
Al Viro1df98b82013-06-15 11:33:10 +04001670 child = d_alloc(dir, &qname);
1671 if (!child)
1672 goto end_instantiate;
1673 if (instantiate(dir->d_inode, child, task, ptr) < 0) {
1674 dput(child);
1675 goto end_instantiate;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001676 }
1677 }
Eric W. Biederman61a28782006-10-02 02:18:49 -07001678 inode = child->d_inode;
Al Viro147ce692013-06-15 10:26:35 +04001679 ino = inode->i_ino;
1680 type = inode->i_mode >> 12;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001681 dput(child);
Al Virof0c3b502013-05-16 12:07:31 -04001682 return dir_emit(ctx, name, len, ino, type);
Al Viro1df98b82013-06-15 11:33:10 +04001683
1684end_instantiate:
1685 return dir_emit(ctx, name, len, 1, DT_UNKNOWN);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001686}
1687
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001688#ifdef CONFIG_CHECKPOINT_RESTORE
1689
1690/*
1691 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1692 * which represent vma start and end addresses.
1693 */
1694static int dname_to_vma_addr(struct dentry *dentry,
1695 unsigned long *start, unsigned long *end)
1696{
1697 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1698 return -EINVAL;
1699
1700 return 0;
1701}
1702
Al Viro0b728e12012-06-10 16:03:43 -04001703static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001704{
1705 unsigned long vm_start, vm_end;
1706 bool exact_vma_exists = false;
1707 struct mm_struct *mm = NULL;
1708 struct task_struct *task;
1709 const struct cred *cred;
1710 struct inode *inode;
1711 int status = 0;
1712
Al Viro0b728e12012-06-10 16:03:43 -04001713 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001714 return -ECHILD;
1715
1716 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001717 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001718 goto out_notask;
1719 }
1720
1721 inode = dentry->d_inode;
1722 task = get_proc_task(inode);
1723 if (!task)
1724 goto out_notask;
1725
Cong Wang2344bec2012-05-31 16:26:18 -07001726 mm = mm_access(task, PTRACE_MODE_READ);
1727 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001728 goto out;
1729
1730 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1731 down_read(&mm->mmap_sem);
1732 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1733 up_read(&mm->mmap_sem);
1734 }
1735
1736 mmput(mm);
1737
1738 if (exact_vma_exists) {
1739 if (task_dumpable(task)) {
1740 rcu_read_lock();
1741 cred = __task_cred(task);
1742 inode->i_uid = cred->euid;
1743 inode->i_gid = cred->egid;
1744 rcu_read_unlock();
1745 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001746 inode->i_uid = GLOBAL_ROOT_UID;
1747 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001748 }
1749 security_task_to_inode(task, inode);
1750 status = 1;
1751 }
1752
1753out:
1754 put_task_struct(task);
1755
1756out_notask:
1757 if (status <= 0)
1758 d_drop(dentry);
1759
1760 return status;
1761}
1762
1763static const struct dentry_operations tid_map_files_dentry_operations = {
1764 .d_revalidate = map_files_d_revalidate,
1765 .d_delete = pid_delete_dentry,
1766};
1767
1768static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1769{
1770 unsigned long vm_start, vm_end;
1771 struct vm_area_struct *vma;
1772 struct task_struct *task;
1773 struct mm_struct *mm;
1774 int rc;
1775
1776 rc = -ENOENT;
1777 task = get_proc_task(dentry->d_inode);
1778 if (!task)
1779 goto out;
1780
1781 mm = get_task_mm(task);
1782 put_task_struct(task);
1783 if (!mm)
1784 goto out;
1785
1786 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1787 if (rc)
1788 goto out_mmput;
1789
1790 down_read(&mm->mmap_sem);
1791 vma = find_exact_vma(mm, vm_start, vm_end);
1792 if (vma && vma->vm_file) {
1793 *path = vma->vm_file->f_path;
1794 path_get(path);
1795 rc = 0;
1796 }
1797 up_read(&mm->mmap_sem);
1798
1799out_mmput:
1800 mmput(mm);
1801out:
1802 return rc;
1803}
1804
1805struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001806 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001807 unsigned long len;
1808 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1809};
1810
Al Viroc52a47a2013-06-15 11:15:20 +04001811static int
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001812proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1813 struct task_struct *task, const void *ptr)
1814{
Al Viro7b540d02012-08-27 14:55:26 -04001815 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001816 struct proc_inode *ei;
1817 struct inode *inode;
1818
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001819 inode = proc_pid_make_inode(dir->i_sb, task);
1820 if (!inode)
Al Viroc52a47a2013-06-15 11:15:20 +04001821 return -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001822
1823 ei = PROC_I(inode);
1824 ei->op.proc_get_link = proc_map_files_get_link;
1825
1826 inode->i_op = &proc_pid_link_inode_operations;
1827 inode->i_size = 64;
1828 inode->i_mode = S_IFLNK;
1829
Al Viro7b540d02012-08-27 14:55:26 -04001830 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001831 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001832 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001833 inode->i_mode |= S_IWUSR;
1834
1835 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1836 d_add(dentry, inode);
1837
Al Viroc52a47a2013-06-15 11:15:20 +04001838 return 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001839}
1840
1841static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001842 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001843{
1844 unsigned long vm_start, vm_end;
1845 struct vm_area_struct *vma;
1846 struct task_struct *task;
Al Viroc52a47a2013-06-15 11:15:20 +04001847 int result;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001848 struct mm_struct *mm;
1849
Al Viroc52a47a2013-06-15 11:15:20 +04001850 result = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001851 if (!capable(CAP_SYS_ADMIN))
1852 goto out;
1853
Al Viroc52a47a2013-06-15 11:15:20 +04001854 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001855 task = get_proc_task(dir);
1856 if (!task)
1857 goto out;
1858
Al Viroc52a47a2013-06-15 11:15:20 +04001859 result = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001860 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001861 goto out_put_task;
1862
Al Viroc52a47a2013-06-15 11:15:20 +04001863 result = -ENOENT;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001864 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001865 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001866
1867 mm = get_task_mm(task);
1868 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001869 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001870
1871 down_read(&mm->mmap_sem);
1872 vma = find_exact_vma(mm, vm_start, vm_end);
1873 if (!vma)
1874 goto out_no_vma;
1875
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001876 if (vma->vm_file)
1877 result = proc_map_files_instantiate(dir, dentry, task,
1878 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001879
1880out_no_vma:
1881 up_read(&mm->mmap_sem);
1882 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001883out_put_task:
1884 put_task_struct(task);
1885out:
Al Viroc52a47a2013-06-15 11:15:20 +04001886 return ERR_PTR(result);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001887}
1888
1889static const struct inode_operations proc_map_files_inode_operations = {
1890 .lookup = proc_map_files_lookup,
1891 .permission = proc_fd_permission,
1892 .setattr = proc_setattr,
1893};
1894
1895static int
Al Virof0c3b502013-05-16 12:07:31 -04001896proc_map_files_readdir(struct file *file, struct dir_context *ctx)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001897{
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001898 struct vm_area_struct *vma;
1899 struct task_struct *task;
1900 struct mm_struct *mm;
Al Virof0c3b502013-05-16 12:07:31 -04001901 unsigned long nr_files, pos, i;
1902 struct flex_array *fa = NULL;
1903 struct map_files_info info;
1904 struct map_files_info *p;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001905 int ret;
1906
Zhao Hongjiang41735812013-02-20 13:13:55 +11001907 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001908 if (!capable(CAP_SYS_ADMIN))
1909 goto out;
1910
1911 ret = -ENOENT;
Al Virof0c3b502013-05-16 12:07:31 -04001912 task = get_proc_task(file_inode(file));
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001913 if (!task)
1914 goto out;
1915
1916 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001917 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001918 goto out_put_task;
1919
1920 ret = 0;
Al Virof0c3b502013-05-16 12:07:31 -04001921 if (!dir_emit_dots(file, ctx))
1922 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001923
Al Virof0c3b502013-05-16 12:07:31 -04001924 mm = get_task_mm(task);
1925 if (!mm)
1926 goto out_put_task;
1927 down_read(&mm->mmap_sem);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001928
Al Virof0c3b502013-05-16 12:07:31 -04001929 nr_files = 0;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001930
Al Virof0c3b502013-05-16 12:07:31 -04001931 /*
1932 * We need two passes here:
1933 *
1934 * 1) Collect vmas of mapped files with mmap_sem taken
1935 * 2) Release mmap_sem and instantiate entries
1936 *
1937 * otherwise we get lockdep complained, since filldir()
1938 * routine might require mmap_sem taken in might_fault().
1939 */
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001940
Al Virof0c3b502013-05-16 12:07:31 -04001941 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1942 if (vma->vm_file && ++pos > ctx->pos)
1943 nr_files++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001944 }
Al Virof0c3b502013-05-16 12:07:31 -04001945
1946 if (nr_files) {
1947 fa = flex_array_alloc(sizeof(info), nr_files,
1948 GFP_KERNEL);
1949 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1950 GFP_KERNEL)) {
1951 ret = -ENOMEM;
1952 if (fa)
1953 flex_array_free(fa);
1954 up_read(&mm->mmap_sem);
1955 mmput(mm);
1956 goto out_put_task;
1957 }
1958 for (i = 0, vma = mm->mmap, pos = 2; vma;
1959 vma = vma->vm_next) {
1960 if (!vma->vm_file)
1961 continue;
1962 if (++pos <= ctx->pos)
1963 continue;
1964
1965 info.mode = vma->vm_file->f_mode;
1966 info.len = snprintf(info.name,
1967 sizeof(info.name), "%lx-%lx",
1968 vma->vm_start, vma->vm_end);
1969 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1970 BUG();
1971 }
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001972 }
Al Virof0c3b502013-05-16 12:07:31 -04001973 up_read(&mm->mmap_sem);
1974
1975 for (i = 0; i < nr_files; i++) {
1976 p = flex_array_get(fa, i);
1977 if (!proc_fill_cache(file, ctx,
1978 p->name, p->len,
1979 proc_map_files_instantiate,
1980 task,
1981 (void *)(unsigned long)p->mode))
1982 break;
1983 ctx->pos++;
1984 }
1985 if (fa)
1986 flex_array_free(fa);
1987 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001988
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001989out_put_task:
1990 put_task_struct(task);
1991out:
1992 return ret;
1993}
1994
1995static const struct file_operations proc_map_files_operations = {
1996 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04001997 .iterate = proc_map_files_readdir,
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001998 .llseek = default_llseek,
1999};
2000
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002001struct timers_private {
2002 struct pid *pid;
2003 struct task_struct *task;
2004 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002005 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002006 unsigned long flags;
2007};
2008
2009static void *timers_start(struct seq_file *m, loff_t *pos)
2010{
2011 struct timers_private *tp = m->private;
2012
2013 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2014 if (!tp->task)
2015 return ERR_PTR(-ESRCH);
2016
2017 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2018 if (!tp->sighand)
2019 return ERR_PTR(-ESRCH);
2020
2021 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2022}
2023
2024static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2025{
2026 struct timers_private *tp = m->private;
2027 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2028}
2029
2030static void timers_stop(struct seq_file *m, void *v)
2031{
2032 struct timers_private *tp = m->private;
2033
2034 if (tp->sighand) {
2035 unlock_task_sighand(tp->task, &tp->flags);
2036 tp->sighand = NULL;
2037 }
2038
2039 if (tp->task) {
2040 put_task_struct(tp->task);
2041 tp->task = NULL;
2042 }
2043}
2044
2045static int show_timer(struct seq_file *m, void *v)
2046{
2047 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002048 struct timers_private *tp = m->private;
2049 int notify;
2050 static char *nstr[] = {
2051 [SIGEV_SIGNAL] = "signal",
2052 [SIGEV_NONE] = "none",
2053 [SIGEV_THREAD] = "thread",
2054 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002055
2056 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002057 notify = timer->it_sigev_notify;
2058
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002059 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002060 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2061 timer->sigq->info.si_value.sival_ptr);
2062 seq_printf(m, "notify: %s/%s.%d\n",
2063 nstr[notify & ~SIGEV_THREAD_ID],
2064 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2065 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Tikhomirov15ef0292013-05-17 02:12:03 +04002066 seq_printf(m, "ClockID: %d\n", timer->it_clock);
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002067
2068 return 0;
2069}
2070
2071static const struct seq_operations proc_timers_seq_ops = {
2072 .start = timers_start,
2073 .next = timers_next,
2074 .stop = timers_stop,
2075 .show = show_timer,
2076};
2077
2078static int proc_timers_open(struct inode *inode, struct file *file)
2079{
2080 struct timers_private *tp;
2081
2082 tp = __seq_open_private(file, &proc_timers_seq_ops,
2083 sizeof(struct timers_private));
2084 if (!tp)
2085 return -ENOMEM;
2086
2087 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002088 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002089 return 0;
2090}
2091
2092static const struct file_operations proc_timers_operations = {
2093 .open = proc_timers_open,
2094 .read = seq_read,
2095 .llseek = seq_lseek,
2096 .release = seq_release_private,
2097};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002098#endif /* CONFIG_CHECKPOINT_RESTORE */
2099
Al Viroc52a47a2013-06-15 11:15:20 +04002100static int proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002101 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002102{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002103 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002104 struct inode *inode;
2105 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002106
Eric W. Biederman61a28782006-10-02 02:18:49 -07002107 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002108 if (!inode)
2109 goto out;
2110
2111 ei = PROC_I(inode);
2112 inode->i_mode = p->mode;
2113 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002114 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002115 if (p->iop)
2116 inode->i_op = p->iop;
2117 if (p->fop)
2118 inode->i_fop = p->fop;
2119 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002120 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002121 d_add(dentry, inode);
2122 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002123 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002124 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002125out:
Al Viroc52a47a2013-06-15 11:15:20 +04002126 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002127}
2128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129static struct dentry *proc_pident_lookup(struct inode *dir,
2130 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002131 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002132 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Al Viroc52a47a2013-06-15 11:15:20 +04002134 int error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002135 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002136 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Al Viroc52a47a2013-06-15 11:15:20 +04002138 error = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Eric W. Biederman99f89552006-06-26 00:25:55 -07002140 if (!task)
2141 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002143 /*
2144 * Yes, it does not scale. And it should not. Don't add
2145 * new entries into /proc/<tgid>/ without very good reasons.
2146 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002147 last = &ents[nents - 1];
2148 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (p->len != dentry->d_name.len)
2150 continue;
2151 if (!memcmp(dentry->d_name.name, p->name, p->len))
2152 break;
2153 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002154 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto out;
2156
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002157 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002159 put_task_struct(task);
2160out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04002161 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Al Virof0c3b502013-05-16 12:07:31 -04002164static int proc_pident_readdir(struct file *file, struct dir_context *ctx,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002165 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002166{
Al Virof0c3b502013-05-16 12:07:31 -04002167 struct task_struct *task = get_proc_task(file_inode(file));
2168 const struct pid_entry *p;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002169
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002170 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04002171 return -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002172
Al Virof0c3b502013-05-16 12:07:31 -04002173 if (!dir_emit_dots(file, ctx))
2174 goto out;
2175
2176 if (ctx->pos >= nents + 2)
2177 goto out;
2178
2179 for (p = ents + (ctx->pos - 2); p <= ents + nents - 1; p++) {
2180 if (!proc_fill_cache(file, ctx, p->name, p->len,
2181 proc_pident_instantiate, task, p))
2182 break;
2183 ctx->pos++;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002184 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002185out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002186 put_task_struct(task);
Al Virof0c3b502013-05-16 12:07:31 -04002187 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002191static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2192 size_t count, loff_t *ppos)
2193{
Al Viro496ad9a2013-01-23 17:07:38 -05002194 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002195 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002196 ssize_t length;
2197 struct task_struct *task = get_proc_task(inode);
2198
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002199 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002200 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002201
2202 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002203 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002204 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002206 if (length > 0)
2207 length = simple_read_from_buffer(buf, count, ppos, p, length);
2208 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002209 return length;
2210}
2211
2212static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2213 size_t count, loff_t *ppos)
2214{
Al Viro496ad9a2013-01-23 17:07:38 -05002215 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002216 char *page;
2217 ssize_t length;
2218 struct task_struct *task = get_proc_task(inode);
2219
2220 length = -ESRCH;
2221 if (!task)
2222 goto out_no_task;
2223 if (count > PAGE_SIZE)
2224 count = PAGE_SIZE;
2225
2226 /* No partial writes. */
2227 length = -EINVAL;
2228 if (*ppos != 0)
2229 goto out;
2230
2231 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002232 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002233 if (!page)
2234 goto out;
2235
2236 length = -EFAULT;
2237 if (copy_from_user(page, buf, count))
2238 goto out_free;
2239
David Howells107db7c2009-05-08 13:55:27 +01002240 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002241 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002242 if (length < 0)
2243 goto out_free;
2244
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002245 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002246 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002247 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002248 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002249out_free:
2250 free_page((unsigned long) page);
2251out:
2252 put_task_struct(task);
2253out_no_task:
2254 return length;
2255}
2256
Arjan van de Ven00977a52007-02-12 00:55:34 -08002257static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002258 .read = proc_pid_attr_read,
2259 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002260 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002261};
2262
Eric Dumazetc5141e62007-05-08 00:26:15 -07002263static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002264 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2265 REG("prev", S_IRUGO, proc_pid_attr_operations),
2266 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2267 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2268 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2269 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002270};
2271
Al Virof0c3b502013-05-16 12:07:31 -04002272static int proc_attr_dir_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Al Virof0c3b502013-05-16 12:07:31 -04002274 return proc_pident_readdir(file, ctx,
2275 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Arjan van de Ven00977a52007-02-12 00:55:34 -08002278static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002280 .iterate = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002281 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282};
2283
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002284static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002285 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002287 return proc_pident_lookup(dir, dentry,
2288 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002291static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002292 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002293 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002294 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295};
2296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297#endif
2298
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002299#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002300static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2301 size_t count, loff_t *ppos)
2302{
Al Viro496ad9a2013-01-23 17:07:38 -05002303 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002304 struct mm_struct *mm;
2305 char buffer[PROC_NUMBUF];
2306 size_t len;
2307 int ret;
2308
2309 if (!task)
2310 return -ESRCH;
2311
2312 ret = 0;
2313 mm = get_task_mm(task);
2314 if (mm) {
2315 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2316 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2317 MMF_DUMP_FILTER_SHIFT));
2318 mmput(mm);
2319 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2320 }
2321
2322 put_task_struct(task);
2323
2324 return ret;
2325}
2326
2327static ssize_t proc_coredump_filter_write(struct file *file,
2328 const char __user *buf,
2329 size_t count,
2330 loff_t *ppos)
2331{
2332 struct task_struct *task;
2333 struct mm_struct *mm;
2334 char buffer[PROC_NUMBUF], *end;
2335 unsigned int val;
2336 int ret;
2337 int i;
2338 unsigned long mask;
2339
2340 ret = -EFAULT;
2341 memset(buffer, 0, sizeof(buffer));
2342 if (count > sizeof(buffer) - 1)
2343 count = sizeof(buffer) - 1;
2344 if (copy_from_user(buffer, buf, count))
2345 goto out_no_task;
2346
2347 ret = -EINVAL;
2348 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2349 if (*end == '\n')
2350 end++;
2351 if (end - buffer == 0)
2352 goto out_no_task;
2353
2354 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002355 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002356 if (!task)
2357 goto out_no_task;
2358
2359 ret = end - buffer;
2360 mm = get_task_mm(task);
2361 if (!mm)
2362 goto out_no_mm;
2363
2364 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2365 if (val & mask)
2366 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2367 else
2368 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2369 }
2370
2371 mmput(mm);
2372 out_no_mm:
2373 put_task_struct(task);
2374 out_no_task:
2375 return ret;
2376}
2377
2378static const struct file_operations proc_coredump_filter_operations = {
2379 .read = proc_coredump_filter_read,
2380 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002381 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002382};
2383#endif
2384
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002385#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002386static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002387{
Andrea Righi940389b2008-07-28 00:48:12 +02002388 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002389 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002390 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002391
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002392 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2393 if (result)
2394 return result;
2395
2396 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2397 result = -EACCES;
2398 goto out_unlock;
2399 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002400
Andrea Righi59954772008-07-27 17:29:15 +02002401 if (whole && lock_task_sighand(task, &flags)) {
2402 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002403
Andrea Righi59954772008-07-27 17:29:15 +02002404 task_io_accounting_add(&acct, &task->signal->ioac);
2405 while_each_thread(task, t)
2406 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002407
Andrea Righi59954772008-07-27 17:29:15 +02002408 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002409 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002410 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002411 "rchar: %llu\n"
2412 "wchar: %llu\n"
2413 "syscr: %llu\n"
2414 "syscw: %llu\n"
2415 "read_bytes: %llu\n"
2416 "write_bytes: %llu\n"
2417 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002418 (unsigned long long)acct.rchar,
2419 (unsigned long long)acct.wchar,
2420 (unsigned long long)acct.syscr,
2421 (unsigned long long)acct.syscw,
2422 (unsigned long long)acct.read_bytes,
2423 (unsigned long long)acct.write_bytes,
2424 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002425out_unlock:
2426 mutex_unlock(&task->signal->cred_guard_mutex);
2427 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002428}
Andrea Righi297c5d92008-07-25 01:48:49 -07002429
2430static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2431{
2432 return do_io_accounting(task, buffer, 0);
2433}
2434
2435static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2436{
2437 return do_io_accounting(task, buffer, 1);
2438}
2439#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002440
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002441#ifdef CONFIG_USER_NS
2442static int proc_id_map_open(struct inode *inode, struct file *file,
2443 struct seq_operations *seq_ops)
2444{
2445 struct user_namespace *ns = NULL;
2446 struct task_struct *task;
2447 struct seq_file *seq;
2448 int ret = -EINVAL;
2449
2450 task = get_proc_task(inode);
2451 if (task) {
2452 rcu_read_lock();
2453 ns = get_user_ns(task_cred_xxx(task, user_ns));
2454 rcu_read_unlock();
2455 put_task_struct(task);
2456 }
2457 if (!ns)
2458 goto err;
2459
2460 ret = seq_open(file, seq_ops);
2461 if (ret)
2462 goto err_put_ns;
2463
2464 seq = file->private_data;
2465 seq->private = ns;
2466
2467 return 0;
2468err_put_ns:
2469 put_user_ns(ns);
2470err:
2471 return ret;
2472}
2473
2474static int proc_id_map_release(struct inode *inode, struct file *file)
2475{
2476 struct seq_file *seq = file->private_data;
2477 struct user_namespace *ns = seq->private;
2478 put_user_ns(ns);
2479 return seq_release(inode, file);
2480}
2481
2482static int proc_uid_map_open(struct inode *inode, struct file *file)
2483{
2484 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2485}
2486
2487static int proc_gid_map_open(struct inode *inode, struct file *file)
2488{
2489 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2490}
2491
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002492static int proc_projid_map_open(struct inode *inode, struct file *file)
2493{
2494 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2495}
2496
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002497static const struct file_operations proc_uid_map_operations = {
2498 .open = proc_uid_map_open,
2499 .write = proc_uid_map_write,
2500 .read = seq_read,
2501 .llseek = seq_lseek,
2502 .release = proc_id_map_release,
2503};
2504
2505static const struct file_operations proc_gid_map_operations = {
2506 .open = proc_gid_map_open,
2507 .write = proc_gid_map_write,
2508 .read = seq_read,
2509 .llseek = seq_lseek,
2510 .release = proc_id_map_release,
2511};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002512
2513static const struct file_operations proc_projid_map_operations = {
2514 .open = proc_projid_map_open,
2515 .write = proc_projid_map_write,
2516 .read = seq_read,
2517 .llseek = seq_lseek,
2518 .release = proc_id_map_release,
2519};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002520#endif /* CONFIG_USER_NS */
2521
Kees Cook47830722008-10-06 03:11:58 +04002522static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2523 struct pid *pid, struct task_struct *task)
2524{
Al Viroa9712bc2011-03-23 15:52:50 -04002525 int err = lock_trace(task);
2526 if (!err) {
2527 seq_printf(m, "%08x\n", task->personality);
2528 unlock_trace(task);
2529 }
2530 return err;
Kees Cook47830722008-10-06 03:11:58 +04002531}
2532
Eric W. Biederman801199c2006-10-02 02:18:48 -07002533/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534 * Thread groups
2535 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002536static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002537static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002538
Eric Dumazetc5141e62007-05-08 00:26:15 -07002539static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002540 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2541 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002542#ifdef CONFIG_CHECKPOINT_RESTORE
2543 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2544#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002545 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002546 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002547#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002548 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002549#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002550 REG("environ", S_IRUSR, proc_environ_operations),
2551 INF("auxv", S_IRUSR, proc_pid_auxv),
2552 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002553 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002554 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002555#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002556 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002557#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002558#ifdef CONFIG_SCHED_AUTOGROUP
2559 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2560#endif
john stultz4614a696b2009-12-14 18:00:05 -08002561 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002562#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002563 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002564#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002565 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2566 ONE("stat", S_IRUGO, proc_tgid_stat),
2567 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002568 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002569#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002570 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002571#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002572 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2573 LNK("cwd", proc_cwd_link),
2574 LNK("root", proc_root_link),
2575 LNK("exe", proc_exe_link),
2576 REG("mounts", S_IRUGO, proc_mounts_operations),
2577 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2578 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002579#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002580 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002581 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002582 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583#endif
2584#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002585 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002586#endif
2587#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002588 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002589#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002590#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002591 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002592#endif
2593#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002594 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002595#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002596#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002597 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002598#endif
Paul Menage8793d852007-10-18 23:39:39 -07002599#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002600 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002601#endif
Paul Menagea4243162007-10-18 23:39:35 -07002602#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002603 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002604#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002605 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002606 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002607 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002608#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002609 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2610 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002611#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002612#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002613 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002614#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002615#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002616 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002617#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002618#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002619 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002620#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002621#ifdef CONFIG_HARDWALL
2622 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2623#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002624#ifdef CONFIG_USER_NS
2625 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2626 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002627 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002628#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002629#ifdef CONFIG_CHECKPOINT_RESTORE
2630 REG("timers", S_IRUGO, proc_timers_operations),
2631#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002632};
2633
Al Virof0c3b502013-05-16 12:07:31 -04002634static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002635{
Al Virof0c3b502013-05-16 12:07:31 -04002636 return proc_pident_readdir(file, ctx,
2637 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002638}
2639
Arjan van de Ven00977a52007-02-12 00:55:34 -08002640static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002641 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002642 .iterate = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002643 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002644};
2645
Al Viro00cd8dd2012-06-10 17:13:09 -04002646static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2647{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002648 return proc_pident_lookup(dir, dentry,
2649 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002650}
2651
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002652static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002653 .lookup = proc_tgid_base_lookup,
2654 .getattr = pid_getattr,
2655 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002656 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657};
2658
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002659static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002661 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002662 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002663 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Eric W. Biederman48e64842006-06-26 00:25:48 -07002665 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002666 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002667 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002668 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002669 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002670 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002671 d_drop(dentry);
2672 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Eric W. Biederman48e64842006-06-26 00:25:48 -07002675 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002676 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2677 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002678 if (!leader)
2679 goto out;
2680
2681 name.name = "task";
2682 name.len = strlen(name.name);
2683 dir = d_hash_and_lookup(leader, &name);
2684 if (!dir)
2685 goto out_put_leader;
2686
2687 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002688 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002689 dentry = d_hash_and_lookup(dir, &name);
2690 if (dentry) {
2691 shrink_dcache_parent(dentry);
2692 d_drop(dentry);
2693 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002695
2696 dput(dir);
2697out_put_leader:
2698 dput(leader);
2699out:
2700 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Randy Dunlap0895e912007-10-21 21:00:10 -07002703/**
2704 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2705 * @task: task that should be flushed.
2706 *
2707 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002708 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002709 * in. This call is supposed to do all of this job.
2710 *
2711 * Looks in the dcache for
2712 * /proc/@pid
2713 * /proc/@tgid/task/@pid
2714 * if either directory is present flushes it and all of it'ts children
2715 * from the dcache.
2716 *
2717 * It is safe and reasonable to cache /proc entries for a task until
2718 * that task exits. After that they just clog up the dcache with
2719 * useless entries, possibly causing useful dcache entries to be
2720 * flushed instead. This routine is proved to flush those useless
2721 * dcache entries at process exit time.
2722 *
2723 * NOTE: This routine is just an optimization so it does not guarantee
2724 * that no dcache entries will exist at process exit time it
2725 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002726 */
2727
2728void proc_flush_task(struct task_struct *task)
2729{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002730 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002731 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002732 struct upid *upid;
2733
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002734 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002735 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002736
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002737 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002738 upid = &pid->numbers[i];
2739 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002740 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002741 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002742}
2743
Al Viroc52a47a2013-06-15 11:15:20 +04002744static int proc_pid_instantiate(struct inode *dir,
2745 struct dentry * dentry,
2746 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002747{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002748 struct inode *inode;
2749
Eric W. Biederman61a28782006-10-02 02:18:49 -07002750 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002751 if (!inode)
2752 goto out;
2753
2754 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2755 inode->i_op = &proc_tgid_base_inode_operations;
2756 inode->i_fop = &proc_tgid_base_operations;
2757 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002758
Miklos Szeredibfe86842011-10-28 14:13:29 +02002759 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2760 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002761
Nick Pigginfb045ad2011-01-07 17:49:55 +11002762 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002763
2764 d_add(dentry, inode);
2765 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002766 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04002767 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002768out:
Al Viroc52a47a2013-06-15 11:15:20 +04002769 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002770}
2771
Al Viro00cd8dd2012-06-10 17:13:09 -04002772struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Al Viroc52a47a2013-06-15 11:15:20 +04002774 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002777 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 tgid = name_to_int(dentry);
2780 if (tgid == ~0U)
2781 goto out;
2782
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002783 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002784 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002785 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (task)
2787 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002788 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (!task)
2790 goto out;
2791
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002792 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002793 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794out:
Al Viroc52a47a2013-06-15 11:15:20 +04002795 return ERR_PTR(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002799 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002800 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002802struct tgid_iter {
2803 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002804 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002805};
2806static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2807{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002808 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002810 if (iter.task)
2811 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002812 rcu_read_lock();
2813retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002814 iter.task = NULL;
2815 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002816 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002817 iter.tgid = pid_nr_ns(pid, ns);
2818 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002819 /* What we to know is if the pid we have find is the
2820 * pid of a thread_group_leader. Testing for task
2821 * being a thread_group_leader is the obvious thing
2822 * todo but there is a window when it fails, due to
2823 * the pid transfer logic in de_thread.
2824 *
2825 * So we perform the straight forward test of seeing
2826 * if the pid we have found is the pid of a thread
2827 * group leader, and don't worry if the task we have
2828 * found doesn't happen to be a thread group leader.
2829 * As we don't care in the case of readdir.
2830 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002831 if (!iter.task || !has_group_leader_pid(iter.task)) {
2832 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002833 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002834 }
2835 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002837 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002838 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839}
2840
Al Viro021ada72013-03-29 19:27:05 -04002841#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843/* for the /proc/ directory itself, after non-process stuff has been done */
Al Virof0c3b502013-05-16 12:07:31 -04002844int proc_pid_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002846 struct tgid_iter iter;
Al Virodb963162013-06-15 10:45:10 +04002847 struct pid_namespace *ns = file->f_dentry->d_sb->s_fs_info;
Al Virof0c3b502013-05-16 12:07:31 -04002848 loff_t pos = ctx->pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Al Viro021ada72013-03-29 19:27:05 -04002850 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Al Virof0c3b502013-05-16 12:07:31 -04002851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Al Viro021ada72013-03-29 19:27:05 -04002853 if (pos == TGID_OFFSET - 1) {
Al Virodb963162013-06-15 10:45:10 +04002854 struct inode *inode = ns->proc_self->d_inode;
2855 if (!dir_emit(ctx, "self", 4, inode->i_ino, DT_LNK))
Al Virof0c3b502013-05-16 12:07:31 -04002856 return 0;
Al Viro021ada72013-03-29 19:27:05 -04002857 iter.tgid = 0;
2858 } else {
2859 iter.tgid = pos - TGID_OFFSET;
2860 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002861 iter.task = NULL;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002862 for (iter = next_tgid(ns, iter);
2863 iter.task;
2864 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Al Virof0c3b502013-05-16 12:07:31 -04002865 char name[PROC_NUMBUF];
2866 int len;
2867 if (!has_pid_permissions(ns, iter.task, 2))
2868 continue;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002869
Al Virof0c3b502013-05-16 12:07:31 -04002870 len = snprintf(name, sizeof(name), "%d", iter.tgid);
2871 ctx->pos = iter.tgid + TGID_OFFSET;
2872 if (!proc_fill_cache(file, ctx, name, len,
2873 proc_pid_instantiate, iter.task, NULL)) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002874 put_task_struct(iter.task);
Al Virof0c3b502013-05-16 12:07:31 -04002875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 }
Al Virof0c3b502013-05-16 12:07:31 -04002878 ctx->pos = PID_MAX_LIMIT + TGID_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return 0;
2880}
2881
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002882/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002883 * Tasks
2884 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002885static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002886 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002887 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002888 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002889 REG("environ", S_IRUSR, proc_environ_operations),
2890 INF("auxv", S_IRUSR, proc_pid_auxv),
2891 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002892 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002893 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002894#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002895 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002896#endif
john stultz4614a696b2009-12-14 18:00:05 -08002897 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002898#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002899 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002900#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002901 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2902 ONE("stat", S_IRUGO, proc_tid_stat),
2903 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002904 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002905#ifdef CONFIG_CHECKPOINT_RESTORE
2906 REG("children", S_IRUGO, proc_tid_children_operations),
2907#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002908#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002909 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002910#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002911 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2912 LNK("cwd", proc_cwd_link),
2913 LNK("root", proc_root_link),
2914 LNK("exe", proc_exe_link),
2915 REG("mounts", S_IRUGO, proc_mounts_operations),
2916 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002917#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002918 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002919 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002920 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002921#endif
2922#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002924#endif
2925#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002926 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002927#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002928#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002929 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002930#endif
2931#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002932 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002934#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002935 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002936#endif
Paul Menage8793d852007-10-18 23:39:39 -07002937#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002938 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#endif
Paul Menagea4243162007-10-18 23:39:35 -07002940#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002941 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002942#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002943 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002944 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002945 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002946#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002948 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002949#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002950#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002951 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002952#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002953#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002954 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002955#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002956#ifdef CONFIG_HARDWALL
2957 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2958#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002959#ifdef CONFIG_USER_NS
2960 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2961 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002962 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002963#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002964};
2965
Al Virof0c3b502013-05-16 12:07:31 -04002966static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002967{
Al Virof0c3b502013-05-16 12:07:31 -04002968 return proc_pident_readdir(file, ctx,
2969 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002970}
2971
Al Viro00cd8dd2012-06-10 17:13:09 -04002972static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2973{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002974 return proc_pident_lookup(dir, dentry,
2975 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002976}
2977
Arjan van de Ven00977a52007-02-12 00:55:34 -08002978static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002979 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04002980 .iterate = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002981 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002982};
2983
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002984static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002985 .lookup = proc_tid_base_lookup,
2986 .getattr = pid_getattr,
2987 .setattr = proc_setattr,
2988};
2989
Al Viroc52a47a2013-06-15 11:15:20 +04002990static int proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002991 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002992{
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002993 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002994 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002995
2996 if (!inode)
2997 goto out;
2998 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2999 inode->i_op = &proc_tid_base_inode_operations;
3000 inode->i_fop = &proc_tid_base_operations;
3001 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003002
Miklos Szeredibfe86842011-10-28 14:13:29 +02003003 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3004 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003005
Nick Pigginfb045ad2011-01-07 17:49:55 +11003006 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003007
3008 d_add(dentry, inode);
3009 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003010 if (pid_revalidate(dentry, 0))
Al Viroc52a47a2013-06-15 11:15:20 +04003011 return 0;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003012out:
Al Viroc52a47a2013-06-15 11:15:20 +04003013 return -ENOENT;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003014}
3015
Al Viro00cd8dd2012-06-10 17:13:09 -04003016static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003017{
Al Viroc52a47a2013-06-15 11:15:20 +04003018 int result = -ENOENT;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003019 struct task_struct *task;
3020 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003021 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003022 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003023
3024 if (!leader)
3025 goto out_no_task;
3026
3027 tid = name_to_int(dentry);
3028 if (tid == ~0U)
3029 goto out;
3030
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003031 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003032 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003033 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003034 if (task)
3035 get_task_struct(task);
3036 rcu_read_unlock();
3037 if (!task)
3038 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003039 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003040 goto out_drop_task;
3041
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003042 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003043out_drop_task:
3044 put_task_struct(task);
3045out:
3046 put_task_struct(leader);
3047out_no_task:
Al Viroc52a47a2013-06-15 11:15:20 +04003048 return ERR_PTR(result);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003049}
3050
3051/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003052 * Find the first tid of a thread group to return to user space.
3053 *
3054 * Usually this is just the thread group leader, but if the users
3055 * buffer was too small or there was a seek into the middle of the
3056 * directory we have more work todo.
3057 *
3058 * In the case of a short read we start with find_task_by_pid.
3059 *
3060 * In the case of a seek we start with the leader and walk nr
3061 * threads past it.
3062 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003063static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003064 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003065{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003066 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003067
Eric W. Biedermancc288732006-06-26 00:26:01 -07003068 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003069 /* Attempt to start with the pid of a thread */
3070 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003071 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003072 if (pos && (pos->group_leader == leader))
3073 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003074 }
3075
3076 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003077 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003078 if (nr && nr >= get_nr_threads(leader))
3079 goto out;
3080
3081 /* If we haven't found our starting place yet start
3082 * with the leader and walk nr threads forward.
3083 */
3084 for (pos = leader; nr > 0; --nr) {
3085 pos = next_thread(pos);
3086 if (pos == leader) {
3087 pos = NULL;
3088 goto out;
3089 }
3090 }
3091found:
3092 get_task_struct(pos);
3093out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003094 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003095 return pos;
3096}
3097
3098/*
3099 * Find the next thread in the thread list.
3100 * Return NULL if there is an error or no next thread.
3101 *
3102 * The reference to the input task_struct is released.
3103 */
3104static struct task_struct *next_tid(struct task_struct *start)
3105{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003106 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003107 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003108 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003109 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003110 if (thread_group_leader(pos))
3111 pos = NULL;
3112 else
3113 get_task_struct(pos);
3114 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003115 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003116 put_task_struct(start);
3117 return pos;
3118}
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120/* for the /proc/TGID/task/ directories */
Al Virof0c3b502013-05-16 12:07:31 -04003121static int proc_task_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122{
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003123 struct task_struct *leader = NULL;
Al Virof0c3b502013-05-16 12:07:31 -04003124 struct task_struct *task = get_proc_task(file_inode(file));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003125 struct pid_namespace *ns;
Al Virof0c3b502013-05-16 12:07:31 -04003126 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003128 if (!task)
Al Virof0c3b502013-05-16 12:07:31 -04003129 return -ENOENT;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003130 rcu_read_lock();
3131 if (pid_alive(task)) {
3132 leader = task->group_leader;
3133 get_task_struct(leader);
3134 }
3135 rcu_read_unlock();
3136 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003137 if (!leader)
Al Virof0c3b502013-05-16 12:07:31 -04003138 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
Al Virof0c3b502013-05-16 12:07:31 -04003140 if (!dir_emit_dots(file, ctx))
3141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003143 /* f_version caches the tgid value that the last readdir call couldn't
3144 * return. lseek aka telldir automagically resets f_version to 0.
3145 */
Al Virof0c3b502013-05-16 12:07:31 -04003146 ns = file->f_dentry->d_sb->s_fs_info;
3147 tid = (int)file->f_version;
3148 file->f_version = 0;
3149 for (task = first_tid(leader, tid, ctx->pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003150 task;
Al Virof0c3b502013-05-16 12:07:31 -04003151 task = next_tid(task), ctx->pos++) {
3152 char name[PROC_NUMBUF];
3153 int len;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003154 tid = task_pid_nr_ns(task, ns);
Al Virof0c3b502013-05-16 12:07:31 -04003155 len = snprintf(name, sizeof(name), "%d", tid);
3156 if (!proc_fill_cache(file, ctx, name, len,
3157 proc_task_instantiate, task, NULL)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003158 /* returning this tgid failed, save it as the first
3159 * pid for the next readir call */
Al Virof0c3b502013-05-16 12:07:31 -04003160 file->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003161 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
3165out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003166 put_task_struct(leader);
Al Virof0c3b502013-05-16 12:07:31 -04003167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003169
3170static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3171{
3172 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003173 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003174 generic_fillattr(inode, stat);
3175
Eric W. Biederman99f89552006-06-26 00:25:55 -07003176 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003177 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003178 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003179 }
3180
3181 return 0;
3182}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003183
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003184static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003185 .lookup = proc_task_lookup,
3186 .getattr = proc_task_getattr,
3187 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003188 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003189};
3190
Arjan van de Ven00977a52007-02-12 00:55:34 -08003191static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003192 .read = generic_read_dir,
Al Virof0c3b502013-05-16 12:07:31 -04003193 .iterate = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003194 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003195};