blob: 3861bcec41ff99e26d4fd80d7ed78448a7169610 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Andrew Morton87ebdc02013-02-27 17:03:16 -080076#include <linux/printk.h>
Paul Menagea4243162007-10-18 23:39:35 -070077#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/cpuset.h>
79#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050080#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070081#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070082#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070083#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070084#include <linux/pid_namespace.h>
Eric W. Biederman22d917d2011-11-17 00:11:58 -080085#include <linux/user_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040086#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090087#include <linux/slab.h>
Pavel Emelyanov640708a2012-01-10 15:11:23 -080088#include <linux/flex_array.h>
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +040089#include <linux/posix-timers.h>
Chris Metcalff133ecc2011-05-26 12:40:09 -040090#ifdef CONFIG_HARDWALL
91#include <asm/hardwall.h>
92#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080093#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040095#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070097/* NOTE:
98 * Implementing inode permission operations in /proc is almost
99 * certainly an error. Permission checks need to happen during
100 * each system call not at open time. The reason is that most of
101 * what we wish to check for permissions in /proc varies at runtime.
102 *
103 * The classic example of a problem is opening file descriptors
104 * in /proc for a task before it execs a suid executable.
105 */
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 int len;
Al Virod161a132011-07-24 03:36:29 -0400110 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800111 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800112 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700118 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 .mode = MODE, \
120 .iop = IOP, \
121 .fop = FOP, \
122 .op = OP, \
123}
124
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300125#define DIR(NAME, MODE, iops, fops) \
126 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
127#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700128 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700129 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300130 { .proc_get_link = get_link } )
131#define REG(NAME, MODE, fops) \
132 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
133#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700134 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700135 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300136 { .proc_read = read } )
137#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800138 NOD(NAME, (S_IFREG|(MODE)), \
139 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300140 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Vegard Nossumaed54172008-06-05 22:46:53 -0700142/*
143 * Count the number of hardlinks for the pid_entry table, excluding the .
144 * and .. links.
145 */
146static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
147 unsigned int n)
148{
149 unsigned int i;
150 unsigned int count;
151
152 count = 0;
153 for (i = 0; i < n; ++i) {
154 if (S_ISDIR(entries[i].mode))
155 ++count;
156 }
157
158 return count;
159}
160
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200161static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000163 int result = -ENOENT;
164
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700165 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200166 if (task->fs) {
167 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000168 result = 0;
169 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700170 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000171 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700172}
173
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800174static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800176 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700177 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700178
179 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200180 task_lock(task);
181 if (task->fs) {
182 get_fs_pwd(task->fs, path);
183 result = 0;
184 }
185 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186 put_task_struct(task);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return result;
189}
190
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800191static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800193 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700195
196 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200197 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700198 put_task_struct(task);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return result;
201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203static int proc_pid_cmdline(struct task_struct *task, char * buffer)
204{
205 int res = 0;
206 unsigned int len;
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 goto out;
210 if (!mm->arg_end)
211 goto out_mm; /* Shh! No looking before we're done */
212
213 len = mm->arg_end - mm->arg_start;
214
215 if (len > PAGE_SIZE)
216 len = PAGE_SIZE;
217
218 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
219
220 // If the nul at the end of args has been overwritten, then
221 // assume application is using setproctitle(3).
222 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
223 len = strnlen(buffer, res);
224 if (len < res) {
225 res = len;
226 } else {
227 len = mm->env_end - mm->env_start;
228 if (len > PAGE_SIZE - res)
229 len = PAGE_SIZE - res;
230 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
231 res = strnlen(buffer, res);
232 }
233 }
234out_mm:
235 mmput(mm);
236out:
237 return res;
238}
239
240static int proc_pid_auxv(struct task_struct *task, char *buffer)
241{
Cong Wange7dcd992012-05-31 16:26:17 -0700242 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500243 int res = PTR_ERR(mm);
244 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300246 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300248 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 res = nwords * sizeof(mm->saved_auxv[0]);
250 if (res > PAGE_SIZE)
251 res = PAGE_SIZE;
252 memcpy(buffer, mm->saved_auxv, res);
253 mmput(mm);
254 }
255 return res;
256}
257
258
259#ifdef CONFIG_KALLSYMS
260/*
261 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
262 * Returns the resolved symbol. If that fails, simply return the address.
263 */
264static int proc_pid_wchan(struct task_struct *task, char *buffer)
265{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700266 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700267 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 wchan = get_wchan(task);
270
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700271 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600272 if (!ptrace_may_access(task, PTRACE_MODE_READ))
273 return 0;
274 else
275 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700276 else
277 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279#endif /* CONFIG_KALLSYMS */
280
Al Viroa9712bc2011-03-23 15:52:50 -0400281static int lock_trace(struct task_struct *task)
282{
283 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
284 if (err)
285 return err;
286 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
287 mutex_unlock(&task->signal->cred_guard_mutex);
288 return -EPERM;
289 }
290 return 0;
291}
292
293static void unlock_trace(struct task_struct *task)
294{
295 mutex_unlock(&task->signal->cred_guard_mutex);
296}
297
Ken Chen2ec220e2008-11-10 11:26:08 +0300298#ifdef CONFIG_STACKTRACE
299
300#define MAX_STACK_TRACE_DEPTH 64
301
302static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
303 struct pid *pid, struct task_struct *task)
304{
305 struct stack_trace trace;
306 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400307 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300308 int i;
309
310 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
311 if (!entries)
312 return -ENOMEM;
313
314 trace.nr_entries = 0;
315 trace.max_entries = MAX_STACK_TRACE_DEPTH;
316 trace.entries = entries;
317 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300318
Al Viroa9712bc2011-03-23 15:52:50 -0400319 err = lock_trace(task);
320 if (!err) {
321 save_stack_trace_tsk(task, &trace);
322
323 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700324 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400325 (void *)entries[i], (void *)entries[i]);
326 }
327 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300328 }
329 kfree(entries);
330
Al Viroa9712bc2011-03-23 15:52:50 -0400331 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300332}
333#endif
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#ifdef CONFIG_SCHEDSTATS
336/*
337 * Provides /proc/PID/schedstat
338 */
339static int proc_pid_schedstat(struct task_struct *task, char *buffer)
340{
Balbir Singh172ba842007-07-09 18:52:00 +0200341 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100342 (unsigned long long)task->se.sum_exec_runtime,
343 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200344 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346#endif
347
Arjan van de Ven97455122008-01-25 21:08:34 +0100348#ifdef CONFIG_LATENCYTOP
349static int lstats_show_proc(struct seq_file *m, void *v)
350{
351 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800352 struct inode *inode = m->private;
353 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100354
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800355 if (!task)
356 return -ESRCH;
357 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100358 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800359 struct latency_record *lr = &task->latency_record[i];
360 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100361 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800362 seq_printf(m, "%i %li %li",
363 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100364 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800365 unsigned long bt = lr->backtrace[q];
366 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100367 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800368 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100369 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800370 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100371 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800372 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100373 }
374
375 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800376 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100377 return 0;
378}
379
380static int lstats_open(struct inode *inode, struct file *file)
381{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800382 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800383}
384
Arjan van de Ven97455122008-01-25 21:08:34 +0100385static ssize_t lstats_write(struct file *file, const char __user *buf,
386 size_t count, loff_t *offs)
387{
Al Viro496ad9a2013-01-23 17:07:38 -0500388 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100389
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800390 if (!task)
391 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100392 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800393 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100394
395 return count;
396}
397
398static const struct file_operations proc_lstats_operations = {
399 .open = lstats_open,
400 .read = seq_read,
401 .write = lstats_write,
402 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800403 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100404};
405
406#endif
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static int proc_oom_score(struct task_struct *task, char *buffer)
409{
David Rientjesa7f638f2012-05-29 15:06:47 -0700410 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200411 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700413 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200414 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700415 points = oom_badness(task, NULL, NULL, totalpages) *
416 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700417 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return sprintf(buffer, "%lu\n", points);
419}
420
Neil Hormand85f50d2007-10-18 23:40:37 -0700421struct limit_names {
422 char *name;
423 char *unit;
424};
425
426static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700427 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700428 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
429 [RLIMIT_DATA] = {"Max data size", "bytes"},
430 [RLIMIT_STACK] = {"Max stack size", "bytes"},
431 [RLIMIT_CORE] = {"Max core file size", "bytes"},
432 [RLIMIT_RSS] = {"Max resident set", "bytes"},
433 [RLIMIT_NPROC] = {"Max processes", "processes"},
434 [RLIMIT_NOFILE] = {"Max open files", "files"},
435 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
436 [RLIMIT_AS] = {"Max address space", "bytes"},
437 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
438 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
439 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
440 [RLIMIT_NICE] = {"Max nice priority", NULL},
441 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800442 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700443};
444
445/* Display limits for a process */
446static int proc_pid_limits(struct task_struct *task, char *buffer)
447{
448 unsigned int i;
449 int count = 0;
450 unsigned long flags;
451 char *bufptr = buffer;
452
453 struct rlimit rlim[RLIM_NLIMITS];
454
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400455 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700456 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700457 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
458 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700459
460 /*
461 * print the file header
462 */
463 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
464 "Limit", "Soft Limit", "Hard Limit", "Units");
465
466 for (i = 0; i < RLIM_NLIMITS; i++) {
467 if (rlim[i].rlim_cur == RLIM_INFINITY)
468 count += sprintf(&bufptr[count], "%-25s %-20s ",
469 lnames[i].name, "unlimited");
470 else
471 count += sprintf(&bufptr[count], "%-25s %-20lu ",
472 lnames[i].name, rlim[i].rlim_cur);
473
474 if (rlim[i].rlim_max == RLIM_INFINITY)
475 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
476 else
477 count += sprintf(&bufptr[count], "%-20lu ",
478 rlim[i].rlim_max);
479
480 if (lnames[i].unit)
481 count += sprintf(&bufptr[count], "%-10s\n",
482 lnames[i].unit);
483 else
484 count += sprintf(&bufptr[count], "\n");
485 }
486
487 return count;
488}
489
Roland McGrathebcb6732008-07-25 19:46:00 -0700490#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
491static int proc_pid_syscall(struct task_struct *task, char *buffer)
492{
493 long nr;
494 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400495 int res = lock_trace(task);
496 if (res)
497 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700498
499 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400500 res = sprintf(buffer, "running\n");
501 else if (nr < 0)
502 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
503 else
504 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700505 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
506 nr,
507 args[0], args[1], args[2], args[3], args[4], args[5],
508 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400509 unlock_trace(task);
510 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700511}
512#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/************************************************************************/
515/* Here the fs part begins */
516/************************************************************************/
517
518/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700519static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700521 struct task_struct *task;
522 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700523 /* Allow access to a task's file descriptors if it is us or we
524 * may use ptrace attach to the process and find out that
525 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700526 */
527 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700528 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400529 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700530 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700531 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700532 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800535int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700536{
537 int error;
538 struct inode *inode = dentry->d_inode;
539
540 if (attr->ia_valid & ATTR_MODE)
541 return -EPERM;
542
543 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200544 if (error)
545 return error;
546
Christoph Hellwig10257742010-06-04 11:30:02 +0200547 setattr_copy(inode, attr);
548 mark_inode_dirty(inode);
549 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700550}
551
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800552/*
553 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
554 * or euid/egid (for hide_pid_min=2)?
555 */
556static bool has_pid_permissions(struct pid_namespace *pid,
557 struct task_struct *task,
558 int hide_pid_min)
559{
560 if (pid->hide_pid < hide_pid_min)
561 return true;
562 if (in_group_p(pid->pid_gid))
563 return true;
564 return ptrace_may_access(task, PTRACE_MODE_READ);
565}
566
567
568static int proc_pid_permission(struct inode *inode, int mask)
569{
570 struct pid_namespace *pid = inode->i_sb->s_fs_info;
571 struct task_struct *task;
572 bool has_perms;
573
574 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800575 if (!task)
576 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800577 has_perms = has_pid_permissions(pid, task, 1);
578 put_task_struct(task);
579
580 if (!has_perms) {
581 if (pid->hide_pid == 2) {
582 /*
583 * Let's make getdents(), stat(), and open()
584 * consistent with each other. If a process
585 * may not stat() a file, it shouldn't be seen
586 * in procfs at all.
587 */
588 return -ENOENT;
589 }
590
591 return -EPERM;
592 }
593 return generic_permission(inode, mask);
594}
595
596
597
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800598static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700599 .setattr = proc_setattr,
600};
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
603
604static ssize_t proc_info_read(struct file * file, char __user * buf,
605 size_t count, loff_t *ppos)
606{
Al Viro496ad9a2013-01-23 17:07:38 -0500607 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 unsigned long page;
609 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700610 struct task_struct *task = get_proc_task(inode);
611
612 length = -ESRCH;
613 if (!task)
614 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (count > PROC_BLOCK_SIZE)
617 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700618
619 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700620 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 length = PROC_I(inode)->op.proc_read(task, (char*)page);
624
625 if (length >= 0)
626 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
627 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700628out:
629 put_task_struct(task);
630out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return length;
632}
633
Arjan van de Ven00977a52007-02-12 00:55:34 -0800634static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100636 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637};
638
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800639static int proc_single_show(struct seq_file *m, void *v)
640{
641 struct inode *inode = m->private;
642 struct pid_namespace *ns;
643 struct pid *pid;
644 struct task_struct *task;
645 int ret;
646
647 ns = inode->i_sb->s_fs_info;
648 pid = proc_pid(inode);
649 task = get_pid_task(pid, PIDTYPE_PID);
650 if (!task)
651 return -ESRCH;
652
653 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
654
655 put_task_struct(task);
656 return ret;
657}
658
659static int proc_single_open(struct inode *inode, struct file *filp)
660{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800661 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800662}
663
664static const struct file_operations proc_single_file_operations = {
665 .open = proc_single_open,
666 .read = seq_read,
667 .llseek = seq_lseek,
668 .release = single_release,
669};
670
Cong Wangb409e572012-05-31 16:26:17 -0700671static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Al Viro496ad9a2013-01-23 17:07:38 -0500673 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800674 struct mm_struct *mm;
675
676 if (!task)
677 return -ESRCH;
678
Cong Wangb409e572012-05-31 16:26:17 -0700679 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800680 put_task_struct(task);
681
682 if (IS_ERR(mm))
683 return PTR_ERR(mm);
684
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100685 if (mm) {
686 /* ensure this mm_struct can't be freed */
687 atomic_inc(&mm->mm_count);
688 /* but do not pin its memory */
689 mmput(mm);
690 }
691
Linus Torvaldse2683372012-01-17 15:21:19 -0800692 file->private_data = mm;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return 0;
695}
696
Cong Wangb409e572012-05-31 16:26:17 -0700697static int mem_open(struct inode *inode, struct file *file)
698{
Djalal Harounibc452b42012-07-30 14:42:28 -0700699 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
700
701 /* OK to pass negative loff_t, we can catch out-of-range */
702 file->f_mode |= FMODE_UNSIGNED_OFFSET;
703
704 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700705}
706
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100707static ssize_t mem_rw(struct file *file, char __user *buf,
708 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Linus Torvaldse2683372012-01-17 15:21:19 -0800710 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100711 unsigned long addr = *ppos;
712 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Linus Torvaldse2683372012-01-17 15:21:19 -0800715 if (!mm)
716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Mel Gormane12ba742007-10-16 01:25:52 -0700718 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700722 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100723 if (!atomic_inc_not_zero(&mm->mm_users))
724 goto free;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100727 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100729 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 copied = -EFAULT;
731 break;
732 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100733
734 this_len = access_remote_vm(mm, addr, page, this_len, write);
735 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (!copied)
737 copied = -EIO;
738 break;
739 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100740
741 if (!write && copy_to_user(buf, page, this_len)) {
742 copied = -EFAULT;
743 break;
744 }
745
746 buf += this_len;
747 addr += this_len;
748 copied += this_len;
749 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100751 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700752
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100753 mmput(mm);
754free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700755 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return copied;
757}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100759static ssize_t mem_read(struct file *file, char __user *buf,
760 size_t count, loff_t *ppos)
761{
762 return mem_rw(file, buf, count, ppos, 0);
763}
764
765static ssize_t mem_write(struct file *file, const char __user *buf,
766 size_t count, loff_t *ppos)
767{
768 return mem_rw(file, (char __user*)buf, count, ppos, 1);
769}
770
Matt Mackall85863e42008-02-04 22:29:04 -0800771loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 switch (orig) {
774 case 0:
775 file->f_pos = offset;
776 break;
777 case 1:
778 file->f_pos += offset;
779 break;
780 default:
781 return -EINVAL;
782 }
783 force_successful_syscall_return();
784 return file->f_pos;
785}
786
Linus Torvaldse2683372012-01-17 15:21:19 -0800787static int mem_release(struct inode *inode, struct file *file)
788{
789 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100790 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100791 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800792 return 0;
793}
794
Arjan van de Ven00977a52007-02-12 00:55:34 -0800795static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 .llseek = mem_lseek,
797 .read = mem_read,
798 .write = mem_write,
799 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800800 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801};
802
Cong Wangb409e572012-05-31 16:26:17 -0700803static int environ_open(struct inode *inode, struct file *file)
804{
805 return __mem_open(inode, file, PTRACE_MODE_READ);
806}
807
James Pearson315e28c2007-10-16 23:30:17 -0700808static ssize_t environ_read(struct file *file, char __user *buf,
809 size_t count, loff_t *ppos)
810{
James Pearson315e28c2007-10-16 23:30:17 -0700811 char *page;
812 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700813 int ret = 0;
814 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700815
Cong Wangb409e572012-05-31 16:26:17 -0700816 if (!mm)
817 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700818
James Pearson315e28c2007-10-16 23:30:17 -0700819 page = (char *)__get_free_page(GFP_TEMPORARY);
820 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700821 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700822
Al Virod6f64b82011-02-15 22:26:01 -0500823 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700824 if (!atomic_inc_not_zero(&mm->mm_users))
825 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700826 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700827 size_t this_len, max_len;
828 int retval;
829
830 if (src >= (mm->env_end - mm->env_start))
831 break;
James Pearson315e28c2007-10-16 23:30:17 -0700832
833 this_len = mm->env_end - (mm->env_start + src);
834
Djalal Harounie8905ec2012-07-30 14:42:26 -0700835 max_len = min_t(size_t, PAGE_SIZE, count);
836 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700837
Cong Wangb409e572012-05-31 16:26:17 -0700838 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700839 page, this_len, 0);
840
841 if (retval <= 0) {
842 ret = retval;
843 break;
844 }
845
846 if (copy_to_user(buf, page, retval)) {
847 ret = -EFAULT;
848 break;
849 }
850
851 ret += retval;
852 src += retval;
853 buf += retval;
854 count -= retval;
855 }
856 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700857 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700858
859free:
James Pearson315e28c2007-10-16 23:30:17 -0700860 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700861 return ret;
862}
863
864static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700865 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700866 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100867 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700868 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700869};
870
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800871static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
872 loff_t *ppos)
873{
Al Viro496ad9a2013-01-23 17:07:38 -0500874 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800875 char buffer[PROC_NUMBUF];
876 int oom_adj = OOM_ADJUST_MIN;
877 size_t len;
878 unsigned long flags;
879
880 if (!task)
881 return -ESRCH;
882 if (lock_task_sighand(task, &flags)) {
883 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
884 oom_adj = OOM_ADJUST_MAX;
885 else
886 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
887 OOM_SCORE_ADJ_MAX;
888 unlock_task_sighand(task, &flags);
889 }
890 put_task_struct(task);
891 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
892 return simple_read_from_buffer(buf, count, ppos, buffer, len);
893}
894
895static ssize_t oom_adj_write(struct file *file, const char __user *buf,
896 size_t count, loff_t *ppos)
897{
898 struct task_struct *task;
899 char buffer[PROC_NUMBUF];
900 int oom_adj;
901 unsigned long flags;
902 int err;
903
904 memset(buffer, 0, sizeof(buffer));
905 if (count > sizeof(buffer) - 1)
906 count = sizeof(buffer) - 1;
907 if (copy_from_user(buffer, buf, count)) {
908 err = -EFAULT;
909 goto out;
910 }
911
912 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
913 if (err)
914 goto out;
915 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
916 oom_adj != OOM_DISABLE) {
917 err = -EINVAL;
918 goto out;
919 }
920
Al Viro496ad9a2013-01-23 17:07:38 -0500921 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800922 if (!task) {
923 err = -ESRCH;
924 goto out;
925 }
926
927 task_lock(task);
928 if (!task->mm) {
929 err = -EINVAL;
930 goto err_task_lock;
931 }
932
933 if (!lock_task_sighand(task, &flags)) {
934 err = -ESRCH;
935 goto err_task_lock;
936 }
937
938 /*
939 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
940 * value is always attainable.
941 */
942 if (oom_adj == OOM_ADJUST_MAX)
943 oom_adj = OOM_SCORE_ADJ_MAX;
944 else
945 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
946
947 if (oom_adj < task->signal->oom_score_adj &&
948 !capable(CAP_SYS_RESOURCE)) {
949 err = -EACCES;
950 goto err_sighand;
951 }
952
953 /*
954 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
955 * /proc/pid/oom_score_adj instead.
956 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800957 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 -0800958 current->comm, task_pid_nr(current), task_pid_nr(task),
959 task_pid_nr(task));
960
961 task->signal->oom_score_adj = oom_adj;
962 trace_oom_score_adj_update(task);
963err_sighand:
964 unlock_task_sighand(task, &flags);
965err_task_lock:
966 task_unlock(task);
967 put_task_struct(task);
968out:
969 return err < 0 ? err : count;
970}
971
972static const struct file_operations proc_oom_adj_operations = {
973 .read = oom_adj_read,
974 .write = oom_adj_write,
975 .llseek = generic_file_llseek,
976};
977
David Rientjesa63d83f2010-08-09 17:19:46 -0700978static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
979 size_t count, loff_t *ppos)
980{
Al Viro496ad9a2013-01-23 17:07:38 -0500981 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -0700982 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -0800983 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -0700984 unsigned long flags;
985 size_t len;
986
987 if (!task)
988 return -ESRCH;
989 if (lock_task_sighand(task, &flags)) {
990 oom_score_adj = task->signal->oom_score_adj;
991 unlock_task_sighand(task, &flags);
992 }
993 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -0800994 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -0700995 return simple_read_from_buffer(buf, count, ppos, buffer, len);
996}
997
998static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
999 size_t count, loff_t *ppos)
1000{
1001 struct task_struct *task;
1002 char buffer[PROC_NUMBUF];
1003 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001004 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001005 int err;
1006
1007 memset(buffer, 0, sizeof(buffer));
1008 if (count > sizeof(buffer) - 1)
1009 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001010 if (copy_from_user(buffer, buf, count)) {
1011 err = -EFAULT;
1012 goto out;
1013 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001014
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001015 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001016 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001017 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001018 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001019 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1020 err = -EINVAL;
1021 goto out;
1022 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001023
Al Viro496ad9a2013-01-23 17:07:38 -05001024 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001025 if (!task) {
1026 err = -ESRCH;
1027 goto out;
1028 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001029
Ying Han3d5992d2010-10-26 14:21:23 -07001030 task_lock(task);
1031 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001032 err = -EINVAL;
1033 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001034 }
David Rientjesd19d5472010-10-26 14:21:26 -07001035
1036 if (!lock_task_sighand(task, &flags)) {
1037 err = -ESRCH;
1038 goto err_task_lock;
1039 }
1040
David Rientjesa9c58b902012-12-11 16:02:54 -08001041 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001042 !capable(CAP_SYS_RESOURCE)) {
1043 err = -EACCES;
1044 goto err_sighand;
1045 }
1046
David Rientjesa9c58b902012-12-11 16:02:54 -08001047 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001048 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001049 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001050 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001051
David Rientjes723548b2010-10-26 14:21:25 -07001052err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001053 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001054err_task_lock:
1055 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001056 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001057out:
1058 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001059}
1060
1061static const struct file_operations proc_oom_score_adj_operations = {
1062 .read = oom_score_adj_read,
1063 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001064 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001065};
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067#ifdef CONFIG_AUDITSYSCALL
1068#define TMPBUFLEN 21
1069static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1070 size_t count, loff_t *ppos)
1071{
Al Viro496ad9a2013-01-23 17:07:38 -05001072 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001073 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ssize_t length;
1075 char tmpbuf[TMPBUFLEN];
1076
Eric W. Biederman99f89552006-06-26 00:25:55 -07001077 if (!task)
1078 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001080 from_kuid(file->f_cred->user_ns,
1081 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001082 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1084}
1085
1086static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1087 size_t count, loff_t *ppos)
1088{
Al Viro496ad9a2013-01-23 17:07:38 -05001089 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 char *page, *tmp;
1091 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001093 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001095 rcu_read_lock();
1096 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1097 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001099 }
1100 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Al Viroe0182902006-05-18 08:28:02 -04001102 if (count >= PAGE_SIZE)
1103 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (*ppos != 0) {
1106 /* No partial writes. */
1107 return -EINVAL;
1108 }
Mel Gormane12ba742007-10-16 01:25:52 -07001109 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!page)
1111 return -ENOMEM;
1112 length = -EFAULT;
1113 if (copy_from_user(page, buf, count))
1114 goto out_free_page;
1115
Al Viroe0182902006-05-18 08:28:02 -04001116 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 loginuid = simple_strtoul(page, &tmp, 10);
1118 if (tmp == page) {
1119 length = -EINVAL;
1120 goto out_free_page;
1121
1122 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001123 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1124 if (!uid_valid(kloginuid)) {
1125 length = -EINVAL;
1126 goto out_free_page;
1127 }
1128
1129 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (likely(length == 0))
1131 length = count;
1132
1133out_free_page:
1134 free_page((unsigned long) page);
1135 return length;
1136}
1137
Arjan van de Ven00977a52007-02-12 00:55:34 -08001138static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 .read = proc_loginuid_read,
1140 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001141 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142};
Eric Paris1e0bd752008-03-13 08:15:31 -04001143
1144static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1145 size_t count, loff_t *ppos)
1146{
Al Viro496ad9a2013-01-23 17:07:38 -05001147 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001148 struct task_struct *task = get_proc_task(inode);
1149 ssize_t length;
1150 char tmpbuf[TMPBUFLEN];
1151
1152 if (!task)
1153 return -ESRCH;
1154 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1155 audit_get_sessionid(task));
1156 put_task_struct(task);
1157 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1158}
1159
1160static const struct file_operations proc_sessionid_operations = {
1161 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001162 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001163};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164#endif
1165
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001166#ifdef CONFIG_FAULT_INJECTION
1167static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1168 size_t count, loff_t *ppos)
1169{
Al Viro496ad9a2013-01-23 17:07:38 -05001170 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001171 char buffer[PROC_NUMBUF];
1172 size_t len;
1173 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001174
1175 if (!task)
1176 return -ESRCH;
1177 make_it_fail = task->make_it_fail;
1178 put_task_struct(task);
1179
1180 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001181
1182 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001183}
1184
1185static ssize_t proc_fault_inject_write(struct file * file,
1186 const char __user * buf, size_t count, loff_t *ppos)
1187{
1188 struct task_struct *task;
1189 char buffer[PROC_NUMBUF], *end;
1190 int make_it_fail;
1191
1192 if (!capable(CAP_SYS_RESOURCE))
1193 return -EPERM;
1194 memset(buffer, 0, sizeof(buffer));
1195 if (count > sizeof(buffer) - 1)
1196 count = sizeof(buffer) - 1;
1197 if (copy_from_user(buffer, buf, count))
1198 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001199 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1200 if (*end)
1201 return -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001202 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001203 if (!task)
1204 return -ESRCH;
1205 task->make_it_fail = make_it_fail;
1206 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001207
1208 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001209}
1210
Arjan van de Ven00977a52007-02-12 00:55:34 -08001211static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001212 .read = proc_fault_inject_read,
1213 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001214 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001215};
1216#endif
1217
Arjan van de Ven97455122008-01-25 21:08:34 +01001218
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001219#ifdef CONFIG_SCHED_DEBUG
1220/*
1221 * Print out various scheduling related per-task fields:
1222 */
1223static int sched_show(struct seq_file *m, void *v)
1224{
1225 struct inode *inode = m->private;
1226 struct task_struct *p;
1227
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001228 p = get_proc_task(inode);
1229 if (!p)
1230 return -ESRCH;
1231 proc_sched_show_task(p, m);
1232
1233 put_task_struct(p);
1234
1235 return 0;
1236}
1237
1238static ssize_t
1239sched_write(struct file *file, const char __user *buf,
1240 size_t count, loff_t *offset)
1241{
Al Viro496ad9a2013-01-23 17:07:38 -05001242 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001243 struct task_struct *p;
1244
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001245 p = get_proc_task(inode);
1246 if (!p)
1247 return -ESRCH;
1248 proc_sched_set_task(p);
1249
1250 put_task_struct(p);
1251
1252 return count;
1253}
1254
1255static int sched_open(struct inode *inode, struct file *filp)
1256{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001257 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001258}
1259
1260static const struct file_operations proc_pid_sched_operations = {
1261 .open = sched_open,
1262 .read = seq_read,
1263 .write = sched_write,
1264 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001265 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001266};
1267
1268#endif
1269
Mike Galbraith5091faa2010-11-30 14:18:03 +01001270#ifdef CONFIG_SCHED_AUTOGROUP
1271/*
1272 * Print out autogroup related information:
1273 */
1274static int sched_autogroup_show(struct seq_file *m, void *v)
1275{
1276 struct inode *inode = m->private;
1277 struct task_struct *p;
1278
1279 p = get_proc_task(inode);
1280 if (!p)
1281 return -ESRCH;
1282 proc_sched_autogroup_show_task(p, m);
1283
1284 put_task_struct(p);
1285
1286 return 0;
1287}
1288
1289static ssize_t
1290sched_autogroup_write(struct file *file, const char __user *buf,
1291 size_t count, loff_t *offset)
1292{
Al Viro496ad9a2013-01-23 17:07:38 -05001293 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001294 struct task_struct *p;
1295 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001296 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001297 int err;
1298
1299 memset(buffer, 0, sizeof(buffer));
1300 if (count > sizeof(buffer) - 1)
1301 count = sizeof(buffer) - 1;
1302 if (copy_from_user(buffer, buf, count))
1303 return -EFAULT;
1304
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001305 err = kstrtoint(strstrip(buffer), 0, &nice);
1306 if (err < 0)
1307 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001308
1309 p = get_proc_task(inode);
1310 if (!p)
1311 return -ESRCH;
1312
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001313 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001314 if (err)
1315 count = err;
1316
1317 put_task_struct(p);
1318
1319 return count;
1320}
1321
1322static int sched_autogroup_open(struct inode *inode, struct file *filp)
1323{
1324 int ret;
1325
1326 ret = single_open(filp, sched_autogroup_show, NULL);
1327 if (!ret) {
1328 struct seq_file *m = filp->private_data;
1329
1330 m->private = inode;
1331 }
1332 return ret;
1333}
1334
1335static const struct file_operations proc_pid_sched_autogroup_operations = {
1336 .open = sched_autogroup_open,
1337 .read = seq_read,
1338 .write = sched_autogroup_write,
1339 .llseek = seq_lseek,
1340 .release = single_release,
1341};
1342
1343#endif /* CONFIG_SCHED_AUTOGROUP */
1344
john stultz4614a696b2009-12-14 18:00:05 -08001345static ssize_t comm_write(struct file *file, const char __user *buf,
1346 size_t count, loff_t *offset)
1347{
Al Viro496ad9a2013-01-23 17:07:38 -05001348 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001349 struct task_struct *p;
1350 char buffer[TASK_COMM_LEN];
David Rientjes830e0fc2013-04-30 15:28:18 -07001351 const size_t maxlen = sizeof(buffer) - 1;
john stultz4614a696b2009-12-14 18:00:05 -08001352
1353 memset(buffer, 0, sizeof(buffer));
David Rientjes830e0fc2013-04-30 15:28:18 -07001354 if (copy_from_user(buffer, buf, count > maxlen ? maxlen : count))
john stultz4614a696b2009-12-14 18:00:05 -08001355 return -EFAULT;
1356
1357 p = get_proc_task(inode);
1358 if (!p)
1359 return -ESRCH;
1360
1361 if (same_thread_group(current, p))
1362 set_task_comm(p, buffer);
1363 else
1364 count = -EINVAL;
1365
1366 put_task_struct(p);
1367
1368 return count;
1369}
1370
1371static int comm_show(struct seq_file *m, void *v)
1372{
1373 struct inode *inode = m->private;
1374 struct task_struct *p;
1375
1376 p = get_proc_task(inode);
1377 if (!p)
1378 return -ESRCH;
1379
1380 task_lock(p);
1381 seq_printf(m, "%s\n", p->comm);
1382 task_unlock(p);
1383
1384 put_task_struct(p);
1385
1386 return 0;
1387}
1388
1389static int comm_open(struct inode *inode, struct file *filp)
1390{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001391 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001392}
1393
1394static const struct file_operations proc_pid_set_comm_operations = {
1395 .open = comm_open,
1396 .read = seq_read,
1397 .write = comm_write,
1398 .llseek = seq_lseek,
1399 .release = single_release,
1400};
1401
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001402static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001403{
1404 struct task_struct *task;
1405 struct mm_struct *mm;
1406 struct file *exe_file;
1407
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001408 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001409 if (!task)
1410 return -ENOENT;
1411 mm = get_task_mm(task);
1412 put_task_struct(task);
1413 if (!mm)
1414 return -ENOENT;
1415 exe_file = get_mm_exe_file(mm);
1416 mmput(mm);
1417 if (exe_file) {
1418 *exe_path = exe_file->f_path;
1419 path_get(&exe_file->f_path);
1420 fput(exe_file);
1421 return 0;
1422 } else
1423 return -ENOENT;
1424}
1425
Al Viro008b1502005-08-20 00:17:39 +01001426static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001429 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 int error = -EACCES;
1431
Eric W. Biederman778c1142006-06-26 00:25:58 -07001432 /* Are we allowed to snoop on the tasks file descriptors? */
1433 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Christoph Hellwig408ef012012-06-18 10:47:03 -04001436 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1437 if (error)
1438 goto out;
1439
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001440 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001441 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442out:
Al Viro008b1502005-08-20 00:17:39 +01001443 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001446static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Mel Gormane12ba742007-10-16 01:25:52 -07001448 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001449 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int len;
1451
1452 if (!tmp)
1453 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001454
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001455 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001456 len = PTR_ERR(pathname);
1457 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001459 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 if (len > buflen)
1462 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001463 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 len = -EFAULT;
1465 out:
1466 free_page((unsigned long)tmp);
1467 return len;
1468}
1469
1470static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1471{
1472 int error = -EACCES;
1473 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001474 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Eric W. Biederman778c1142006-06-26 00:25:58 -07001476 /* Are we allowed to snoop on the tasks file descriptors? */
1477 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001480 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (error)
1482 goto out;
1483
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001484 error = do_proc_readlink(&path, buffer, buflen);
1485 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return error;
1488}
1489
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001490const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001492 .follow_link = proc_pid_follow_link,
1493 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494};
1495
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001496
1497/* building an inode */
1498
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001499struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001500{
1501 struct inode * inode;
1502 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001503 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001504
1505 /* We need a new inode */
1506
1507 inode = new_inode(sb);
1508 if (!inode)
1509 goto out;
1510
1511 /* Common stuff */
1512 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001513 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001514 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001515 inode->i_op = &proc_def_inode_operations;
1516
1517 /*
1518 * grab the reference to task.
1519 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001520 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001521 if (!ei->pid)
1522 goto out_unlock;
1523
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001524 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001525 rcu_read_lock();
1526 cred = __task_cred(task);
1527 inode->i_uid = cred->euid;
1528 inode->i_gid = cred->egid;
1529 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001530 }
1531 security_task_to_inode(task, inode);
1532
1533out:
1534 return inode;
1535
1536out_unlock:
1537 iput(inode);
1538 return NULL;
1539}
1540
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001541int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001542{
1543 struct inode *inode = dentry->d_inode;
1544 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001545 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001546 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001547
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001548 generic_fillattr(inode, stat);
1549
1550 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001551 stat->uid = GLOBAL_ROOT_UID;
1552 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001553 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1554 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001555 if (!has_pid_permissions(pid, task, 2)) {
1556 rcu_read_unlock();
1557 /*
1558 * This doesn't prevent learning whether PID exists,
1559 * it only makes getattr() consistent with readdir().
1560 */
1561 return -ENOENT;
1562 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001563 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1564 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001565 cred = __task_cred(task);
1566 stat->uid = cred->euid;
1567 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001568 }
1569 }
1570 rcu_read_unlock();
1571 return 0;
1572}
1573
1574/* dentry stuff */
1575
1576/*
1577 * Exceptional case: normally we are not allowed to unhash a busy
1578 * directory. In this case, however, we can do it - no aliasing problems
1579 * due to the way we treat inodes.
1580 *
1581 * Rewrite the inode's ownerships here because the owning task may have
1582 * performed a setuid(), etc.
1583 *
1584 * Before the /proc/pid/status file was created the only way to read
1585 * the effective uid of a /process was to stat /proc/pid. Reading
1586 * /proc/pid/status is slow enough that procps and other packages
1587 * kept stating /proc/pid. To keep the rules in /proc simple I have
1588 * made this apply to all per process world readable and executable
1589 * directories.
1590 */
Al Viro0b728e12012-06-10 16:03:43 -04001591int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001592{
Nick Piggin34286d62011-01-07 17:49:57 +11001593 struct inode *inode;
1594 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001595 const struct cred *cred;
1596
Al Viro0b728e12012-06-10 16:03:43 -04001597 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001598 return -ECHILD;
1599
1600 inode = dentry->d_inode;
1601 task = get_proc_task(inode);
1602
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001603 if (task) {
1604 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1605 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001606 rcu_read_lock();
1607 cred = __task_cred(task);
1608 inode->i_uid = cred->euid;
1609 inode->i_gid = cred->egid;
1610 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001611 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001612 inode->i_uid = GLOBAL_ROOT_UID;
1613 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614 }
1615 inode->i_mode &= ~(S_ISUID | S_ISGID);
1616 security_task_to_inode(task, inode);
1617 put_task_struct(task);
1618 return 1;
1619 }
1620 d_drop(dentry);
1621 return 0;
1622}
1623
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001624const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001625{
1626 .d_revalidate = pid_revalidate,
1627 .d_delete = pid_delete_dentry,
1628};
1629
1630/* Lookups */
1631
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001632/*
1633 * Fill a directory entry.
1634 *
1635 * If possible create the dcache entry and derive our inode number and
1636 * file type from dcache entry.
1637 *
1638 * Since all of the proc inode numbers are dynamically generated, the inode
1639 * numbers do not exist until the inode is cache. This means creating the
1640 * the dcache entry in readdir is necessary to keep the inode numbers
1641 * reported by readdir in sync with the inode numbers reported
1642 * by stat.
1643 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001644int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1645 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001646 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001647{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001648 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001649 struct inode *inode;
1650 struct qstr qname;
1651 ino_t ino = 0;
1652 unsigned type = DT_UNKNOWN;
1653
1654 qname.name = name;
1655 qname.len = len;
1656 qname.hash = full_name_hash(name, len);
1657
1658 child = d_lookup(dir, &qname);
1659 if (!child) {
1660 struct dentry *new;
1661 new = d_alloc(dir, &qname);
1662 if (new) {
1663 child = instantiate(dir->d_inode, new, task, ptr);
1664 if (child)
1665 dput(new);
1666 else
1667 child = new;
1668 }
1669 }
1670 if (!child || IS_ERR(child) || !child->d_inode)
1671 goto end_instantiate;
1672 inode = child->d_inode;
1673 if (inode) {
1674 ino = inode->i_ino;
1675 type = inode->i_mode >> 12;
1676 }
1677 dput(child);
1678end_instantiate:
1679 if (!ino)
1680 ino = find_inode_number(dir, &qname);
1681 if (!ino)
1682 ino = 1;
1683 return filldir(dirent, name, len, filp->f_pos, ino, type);
1684}
1685
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001686#ifdef CONFIG_CHECKPOINT_RESTORE
1687
1688/*
1689 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1690 * which represent vma start and end addresses.
1691 */
1692static int dname_to_vma_addr(struct dentry *dentry,
1693 unsigned long *start, unsigned long *end)
1694{
1695 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1696 return -EINVAL;
1697
1698 return 0;
1699}
1700
Al Viro0b728e12012-06-10 16:03:43 -04001701static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001702{
1703 unsigned long vm_start, vm_end;
1704 bool exact_vma_exists = false;
1705 struct mm_struct *mm = NULL;
1706 struct task_struct *task;
1707 const struct cred *cred;
1708 struct inode *inode;
1709 int status = 0;
1710
Al Viro0b728e12012-06-10 16:03:43 -04001711 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001712 return -ECHILD;
1713
1714 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001715 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001716 goto out_notask;
1717 }
1718
1719 inode = dentry->d_inode;
1720 task = get_proc_task(inode);
1721 if (!task)
1722 goto out_notask;
1723
Cong Wang2344bec2012-05-31 16:26:18 -07001724 mm = mm_access(task, PTRACE_MODE_READ);
1725 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001726 goto out;
1727
1728 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1729 down_read(&mm->mmap_sem);
1730 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1731 up_read(&mm->mmap_sem);
1732 }
1733
1734 mmput(mm);
1735
1736 if (exact_vma_exists) {
1737 if (task_dumpable(task)) {
1738 rcu_read_lock();
1739 cred = __task_cred(task);
1740 inode->i_uid = cred->euid;
1741 inode->i_gid = cred->egid;
1742 rcu_read_unlock();
1743 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001744 inode->i_uid = GLOBAL_ROOT_UID;
1745 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001746 }
1747 security_task_to_inode(task, inode);
1748 status = 1;
1749 }
1750
1751out:
1752 put_task_struct(task);
1753
1754out_notask:
1755 if (status <= 0)
1756 d_drop(dentry);
1757
1758 return status;
1759}
1760
1761static const struct dentry_operations tid_map_files_dentry_operations = {
1762 .d_revalidate = map_files_d_revalidate,
1763 .d_delete = pid_delete_dentry,
1764};
1765
1766static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1767{
1768 unsigned long vm_start, vm_end;
1769 struct vm_area_struct *vma;
1770 struct task_struct *task;
1771 struct mm_struct *mm;
1772 int rc;
1773
1774 rc = -ENOENT;
1775 task = get_proc_task(dentry->d_inode);
1776 if (!task)
1777 goto out;
1778
1779 mm = get_task_mm(task);
1780 put_task_struct(task);
1781 if (!mm)
1782 goto out;
1783
1784 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1785 if (rc)
1786 goto out_mmput;
1787
1788 down_read(&mm->mmap_sem);
1789 vma = find_exact_vma(mm, vm_start, vm_end);
1790 if (vma && vma->vm_file) {
1791 *path = vma->vm_file->f_path;
1792 path_get(path);
1793 rc = 0;
1794 }
1795 up_read(&mm->mmap_sem);
1796
1797out_mmput:
1798 mmput(mm);
1799out:
1800 return rc;
1801}
1802
1803struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001804 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001805 unsigned long len;
1806 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1807};
1808
1809static struct dentry *
1810proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1811 struct task_struct *task, const void *ptr)
1812{
Al Viro7b540d02012-08-27 14:55:26 -04001813 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001814 struct proc_inode *ei;
1815 struct inode *inode;
1816
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001817 inode = proc_pid_make_inode(dir->i_sb, task);
1818 if (!inode)
1819 return ERR_PTR(-ENOENT);
1820
1821 ei = PROC_I(inode);
1822 ei->op.proc_get_link = proc_map_files_get_link;
1823
1824 inode->i_op = &proc_pid_link_inode_operations;
1825 inode->i_size = 64;
1826 inode->i_mode = S_IFLNK;
1827
Al Viro7b540d02012-08-27 14:55:26 -04001828 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001829 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001830 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001831 inode->i_mode |= S_IWUSR;
1832
1833 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1834 d_add(dentry, inode);
1835
1836 return NULL;
1837}
1838
1839static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001840 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001841{
1842 unsigned long vm_start, vm_end;
1843 struct vm_area_struct *vma;
1844 struct task_struct *task;
1845 struct dentry *result;
1846 struct mm_struct *mm;
1847
Zhao Hongjiang41735812013-02-20 13:13:55 +11001848 result = ERR_PTR(-EPERM);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001849 if (!capable(CAP_SYS_ADMIN))
1850 goto out;
1851
1852 result = ERR_PTR(-ENOENT);
1853 task = get_proc_task(dir);
1854 if (!task)
1855 goto out;
1856
1857 result = ERR_PTR(-EACCES);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001858 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001859 goto out_put_task;
1860
1861 result = ERR_PTR(-ENOENT);
1862 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001863 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001864
1865 mm = get_task_mm(task);
1866 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001867 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001868
1869 down_read(&mm->mmap_sem);
1870 vma = find_exact_vma(mm, vm_start, vm_end);
1871 if (!vma)
1872 goto out_no_vma;
1873
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001874 if (vma->vm_file)
1875 result = proc_map_files_instantiate(dir, dentry, task,
1876 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001877
1878out_no_vma:
1879 up_read(&mm->mmap_sem);
1880 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001881out_put_task:
1882 put_task_struct(task);
1883out:
1884 return result;
1885}
1886
1887static const struct inode_operations proc_map_files_inode_operations = {
1888 .lookup = proc_map_files_lookup,
1889 .permission = proc_fd_permission,
1890 .setattr = proc_setattr,
1891};
1892
1893static int
1894proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1895{
1896 struct dentry *dentry = filp->f_path.dentry;
1897 struct inode *inode = dentry->d_inode;
1898 struct vm_area_struct *vma;
1899 struct task_struct *task;
1900 struct mm_struct *mm;
1901 ino_t ino;
1902 int ret;
1903
Zhao Hongjiang41735812013-02-20 13:13:55 +11001904 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001905 if (!capable(CAP_SYS_ADMIN))
1906 goto out;
1907
1908 ret = -ENOENT;
1909 task = get_proc_task(inode);
1910 if (!task)
1911 goto out;
1912
1913 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001914 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001915 goto out_put_task;
1916
1917 ret = 0;
1918 switch (filp->f_pos) {
1919 case 0:
1920 ino = inode->i_ino;
1921 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001922 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001923 filp->f_pos++;
1924 case 1:
1925 ino = parent_ino(dentry);
1926 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001927 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001928 filp->f_pos++;
1929 default:
1930 {
1931 unsigned long nr_files, pos, i;
1932 struct flex_array *fa = NULL;
1933 struct map_files_info info;
1934 struct map_files_info *p;
1935
1936 mm = get_task_mm(task);
1937 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001938 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001939 down_read(&mm->mmap_sem);
1940
1941 nr_files = 0;
1942
1943 /*
1944 * We need two passes here:
1945 *
1946 * 1) Collect vmas of mapped files with mmap_sem taken
1947 * 2) Release mmap_sem and instantiate entries
1948 *
1949 * otherwise we get lockdep complained, since filldir()
1950 * routine might require mmap_sem taken in might_fault().
1951 */
1952
1953 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1954 if (vma->vm_file && ++pos > filp->f_pos)
1955 nr_files++;
1956 }
1957
1958 if (nr_files) {
1959 fa = flex_array_alloc(sizeof(info), nr_files,
1960 GFP_KERNEL);
1961 if (!fa || flex_array_prealloc(fa, 0, nr_files,
1962 GFP_KERNEL)) {
1963 ret = -ENOMEM;
1964 if (fa)
1965 flex_array_free(fa);
1966 up_read(&mm->mmap_sem);
1967 mmput(mm);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001968 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001969 }
1970 for (i = 0, vma = mm->mmap, pos = 2; vma;
1971 vma = vma->vm_next) {
1972 if (!vma->vm_file)
1973 continue;
1974 if (++pos <= filp->f_pos)
1975 continue;
1976
Al Viro7b540d02012-08-27 14:55:26 -04001977 info.mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001978 info.len = snprintf(info.name,
1979 sizeof(info.name), "%lx-%lx",
1980 vma->vm_start, vma->vm_end);
1981 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
1982 BUG();
1983 }
1984 }
1985 up_read(&mm->mmap_sem);
1986
1987 for (i = 0; i < nr_files; i++) {
1988 p = flex_array_get(fa, i);
1989 ret = proc_fill_cache(filp, dirent, filldir,
1990 p->name, p->len,
1991 proc_map_files_instantiate,
Al Viro7b540d02012-08-27 14:55:26 -04001992 task,
1993 (void *)(unsigned long)p->mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001994 if (ret)
1995 break;
1996 filp->f_pos++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001997 }
1998 if (fa)
1999 flex_array_free(fa);
2000 mmput(mm);
2001 }
2002 }
2003
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002004out_put_task:
2005 put_task_struct(task);
2006out:
2007 return ret;
2008}
2009
2010static const struct file_operations proc_map_files_operations = {
2011 .read = generic_read_dir,
2012 .readdir = proc_map_files_readdir,
2013 .llseek = default_llseek,
2014};
2015
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002016struct timers_private {
2017 struct pid *pid;
2018 struct task_struct *task;
2019 struct sighand_struct *sighand;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002020 struct pid_namespace *ns;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002021 unsigned long flags;
2022};
2023
2024static void *timers_start(struct seq_file *m, loff_t *pos)
2025{
2026 struct timers_private *tp = m->private;
2027
2028 tp->task = get_pid_task(tp->pid, PIDTYPE_PID);
2029 if (!tp->task)
2030 return ERR_PTR(-ESRCH);
2031
2032 tp->sighand = lock_task_sighand(tp->task, &tp->flags);
2033 if (!tp->sighand)
2034 return ERR_PTR(-ESRCH);
2035
2036 return seq_list_start(&tp->task->signal->posix_timers, *pos);
2037}
2038
2039static void *timers_next(struct seq_file *m, void *v, loff_t *pos)
2040{
2041 struct timers_private *tp = m->private;
2042 return seq_list_next(v, &tp->task->signal->posix_timers, pos);
2043}
2044
2045static void timers_stop(struct seq_file *m, void *v)
2046{
2047 struct timers_private *tp = m->private;
2048
2049 if (tp->sighand) {
2050 unlock_task_sighand(tp->task, &tp->flags);
2051 tp->sighand = NULL;
2052 }
2053
2054 if (tp->task) {
2055 put_task_struct(tp->task);
2056 tp->task = NULL;
2057 }
2058}
2059
2060static int show_timer(struct seq_file *m, void *v)
2061{
2062 struct k_itimer *timer;
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002063 struct timers_private *tp = m->private;
2064 int notify;
2065 static char *nstr[] = {
2066 [SIGEV_SIGNAL] = "signal",
2067 [SIGEV_NONE] = "none",
2068 [SIGEV_THREAD] = "thread",
2069 };
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002070
2071 timer = list_entry((struct list_head *)v, struct k_itimer, list);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002072 notify = timer->it_sigev_notify;
2073
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002074 seq_printf(m, "ID: %d\n", timer->it_id);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002075 seq_printf(m, "signal: %d/%p\n", timer->sigq->info.si_signo,
2076 timer->sigq->info.si_value.sival_ptr);
2077 seq_printf(m, "notify: %s/%s.%d\n",
2078 nstr[notify & ~SIGEV_THREAD_ID],
2079 (notify & SIGEV_THREAD_ID) ? "tid" : "pid",
2080 pid_nr_ns(timer->it_pid, tp->ns));
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002081
2082 return 0;
2083}
2084
2085static const struct seq_operations proc_timers_seq_ops = {
2086 .start = timers_start,
2087 .next = timers_next,
2088 .stop = timers_stop,
2089 .show = show_timer,
2090};
2091
2092static int proc_timers_open(struct inode *inode, struct file *file)
2093{
2094 struct timers_private *tp;
2095
2096 tp = __seq_open_private(file, &proc_timers_seq_ops,
2097 sizeof(struct timers_private));
2098 if (!tp)
2099 return -ENOMEM;
2100
2101 tp->pid = proc_pid(inode);
Pavel Emelyanov57b80152013-03-11 13:13:08 +04002102 tp->ns = inode->i_sb->s_fs_info;
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002103 return 0;
2104}
2105
2106static const struct file_operations proc_timers_operations = {
2107 .open = proc_timers_open,
2108 .read = seq_read,
2109 .llseek = seq_lseek,
2110 .release = seq_release_private,
2111};
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002112#endif /* CONFIG_CHECKPOINT_RESTORE */
2113
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002114static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002115 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002116{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002117 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002118 struct inode *inode;
2119 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002120 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002121
Eric W. Biederman61a28782006-10-02 02:18:49 -07002122 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002123 if (!inode)
2124 goto out;
2125
2126 ei = PROC_I(inode);
2127 inode->i_mode = p->mode;
2128 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002129 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002130 if (p->iop)
2131 inode->i_op = p->iop;
2132 if (p->fop)
2133 inode->i_fop = p->fop;
2134 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002135 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002136 d_add(dentry, inode);
2137 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002138 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002139 error = NULL;
2140out:
2141 return error;
2142}
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144static struct dentry *proc_pident_lookup(struct inode *dir,
2145 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002146 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002147 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002149 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002150 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002151 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002153 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Eric W. Biederman99f89552006-06-26 00:25:55 -07002155 if (!task)
2156 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002158 /*
2159 * Yes, it does not scale. And it should not. Don't add
2160 * new entries into /proc/<tgid>/ without very good reasons.
2161 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002162 last = &ents[nents - 1];
2163 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (p->len != dentry->d_name.len)
2165 continue;
2166 if (!memcmp(dentry->d_name.name, p->name, p->len))
2167 break;
2168 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002169 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 goto out;
2171
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002172 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002174 put_task_struct(task);
2175out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002176 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Eric Dumazetc5141e62007-05-08 00:26:15 -07002179static int proc_pident_fill_cache(struct file *filp, void *dirent,
2180 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002181{
2182 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2183 proc_pident_instantiate, task, p);
2184}
2185
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002186static int proc_pident_readdir(struct file *filp,
2187 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002188 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002189{
2190 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002191 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002192 struct inode *inode = dentry->d_inode;
2193 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002194 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002195 ino_t ino;
2196 int ret;
2197
2198 ret = -ENOENT;
2199 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002200 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002201
2202 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002203 i = filp->f_pos;
2204 switch (i) {
2205 case 0:
2206 ino = inode->i_ino;
2207 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2208 goto out;
2209 i++;
2210 filp->f_pos++;
2211 /* fall through */
2212 case 1:
2213 ino = parent_ino(dentry);
2214 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2215 goto out;
2216 i++;
2217 filp->f_pos++;
2218 /* fall through */
2219 default:
2220 i -= 2;
2221 if (i >= nents) {
2222 ret = 1;
2223 goto out;
2224 }
2225 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002226 last = &ents[nents - 1];
2227 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002228 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002229 goto out;
2230 filp->f_pos++;
2231 p++;
2232 }
2233 }
2234
2235 ret = 1;
2236out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002237 put_task_struct(task);
2238out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002243static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2244 size_t count, loff_t *ppos)
2245{
Al Viro496ad9a2013-01-23 17:07:38 -05002246 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002247 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002248 ssize_t length;
2249 struct task_struct *task = get_proc_task(inode);
2250
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002251 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002252 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002253
2254 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002255 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002256 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002257 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002258 if (length > 0)
2259 length = simple_read_from_buffer(buf, count, ppos, p, length);
2260 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002261 return length;
2262}
2263
2264static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2265 size_t count, loff_t *ppos)
2266{
Al Viro496ad9a2013-01-23 17:07:38 -05002267 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002268 char *page;
2269 ssize_t length;
2270 struct task_struct *task = get_proc_task(inode);
2271
2272 length = -ESRCH;
2273 if (!task)
2274 goto out_no_task;
2275 if (count > PAGE_SIZE)
2276 count = PAGE_SIZE;
2277
2278 /* No partial writes. */
2279 length = -EINVAL;
2280 if (*ppos != 0)
2281 goto out;
2282
2283 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002284 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002285 if (!page)
2286 goto out;
2287
2288 length = -EFAULT;
2289 if (copy_from_user(page, buf, count))
2290 goto out_free;
2291
David Howells107db7c2009-05-08 13:55:27 +01002292 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002293 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002294 if (length < 0)
2295 goto out_free;
2296
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002297 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002298 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002299 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002300 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002301out_free:
2302 free_page((unsigned long) page);
2303out:
2304 put_task_struct(task);
2305out_no_task:
2306 return length;
2307}
2308
Arjan van de Ven00977a52007-02-12 00:55:34 -08002309static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002310 .read = proc_pid_attr_read,
2311 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002312 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002313};
2314
Eric Dumazetc5141e62007-05-08 00:26:15 -07002315static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002316 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2317 REG("prev", S_IRUGO, proc_pid_attr_operations),
2318 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2319 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2320 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2321 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002322};
2323
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002324static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 void * dirent, filldir_t filldir)
2326{
2327 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002328 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Arjan van de Ven00977a52007-02-12 00:55:34 -08002331static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002333 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002334 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335};
2336
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002337static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002338 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002340 return proc_pident_lookup(dir, dentry,
2341 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002344static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002345 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002346 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002347 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348};
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350#endif
2351
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002352#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002353static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2354 size_t count, loff_t *ppos)
2355{
Al Viro496ad9a2013-01-23 17:07:38 -05002356 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002357 struct mm_struct *mm;
2358 char buffer[PROC_NUMBUF];
2359 size_t len;
2360 int ret;
2361
2362 if (!task)
2363 return -ESRCH;
2364
2365 ret = 0;
2366 mm = get_task_mm(task);
2367 if (mm) {
2368 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2369 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2370 MMF_DUMP_FILTER_SHIFT));
2371 mmput(mm);
2372 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2373 }
2374
2375 put_task_struct(task);
2376
2377 return ret;
2378}
2379
2380static ssize_t proc_coredump_filter_write(struct file *file,
2381 const char __user *buf,
2382 size_t count,
2383 loff_t *ppos)
2384{
2385 struct task_struct *task;
2386 struct mm_struct *mm;
2387 char buffer[PROC_NUMBUF], *end;
2388 unsigned int val;
2389 int ret;
2390 int i;
2391 unsigned long mask;
2392
2393 ret = -EFAULT;
2394 memset(buffer, 0, sizeof(buffer));
2395 if (count > sizeof(buffer) - 1)
2396 count = sizeof(buffer) - 1;
2397 if (copy_from_user(buffer, buf, count))
2398 goto out_no_task;
2399
2400 ret = -EINVAL;
2401 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2402 if (*end == '\n')
2403 end++;
2404 if (end - buffer == 0)
2405 goto out_no_task;
2406
2407 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002408 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002409 if (!task)
2410 goto out_no_task;
2411
2412 ret = end - buffer;
2413 mm = get_task_mm(task);
2414 if (!mm)
2415 goto out_no_mm;
2416
2417 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2418 if (val & mask)
2419 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2420 else
2421 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2422 }
2423
2424 mmput(mm);
2425 out_no_mm:
2426 put_task_struct(task);
2427 out_no_task:
2428 return ret;
2429}
2430
2431static const struct file_operations proc_coredump_filter_operations = {
2432 .read = proc_coredump_filter_read,
2433 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002434 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002435};
2436#endif
2437
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002438#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002439static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002440{
Andrea Righi940389b2008-07-28 00:48:12 +02002441 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002442 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002443 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002444
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002445 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2446 if (result)
2447 return result;
2448
2449 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2450 result = -EACCES;
2451 goto out_unlock;
2452 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002453
Andrea Righi59954772008-07-27 17:29:15 +02002454 if (whole && lock_task_sighand(task, &flags)) {
2455 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002456
Andrea Righi59954772008-07-27 17:29:15 +02002457 task_io_accounting_add(&acct, &task->signal->ioac);
2458 while_each_thread(task, t)
2459 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002460
Andrea Righi59954772008-07-27 17:29:15 +02002461 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002462 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002463 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002464 "rchar: %llu\n"
2465 "wchar: %llu\n"
2466 "syscr: %llu\n"
2467 "syscw: %llu\n"
2468 "read_bytes: %llu\n"
2469 "write_bytes: %llu\n"
2470 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002471 (unsigned long long)acct.rchar,
2472 (unsigned long long)acct.wchar,
2473 (unsigned long long)acct.syscr,
2474 (unsigned long long)acct.syscw,
2475 (unsigned long long)acct.read_bytes,
2476 (unsigned long long)acct.write_bytes,
2477 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002478out_unlock:
2479 mutex_unlock(&task->signal->cred_guard_mutex);
2480 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002481}
Andrea Righi297c5d92008-07-25 01:48:49 -07002482
2483static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2484{
2485 return do_io_accounting(task, buffer, 0);
2486}
2487
2488static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2489{
2490 return do_io_accounting(task, buffer, 1);
2491}
2492#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002493
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002494#ifdef CONFIG_USER_NS
2495static int proc_id_map_open(struct inode *inode, struct file *file,
2496 struct seq_operations *seq_ops)
2497{
2498 struct user_namespace *ns = NULL;
2499 struct task_struct *task;
2500 struct seq_file *seq;
2501 int ret = -EINVAL;
2502
2503 task = get_proc_task(inode);
2504 if (task) {
2505 rcu_read_lock();
2506 ns = get_user_ns(task_cred_xxx(task, user_ns));
2507 rcu_read_unlock();
2508 put_task_struct(task);
2509 }
2510 if (!ns)
2511 goto err;
2512
2513 ret = seq_open(file, seq_ops);
2514 if (ret)
2515 goto err_put_ns;
2516
2517 seq = file->private_data;
2518 seq->private = ns;
2519
2520 return 0;
2521err_put_ns:
2522 put_user_ns(ns);
2523err:
2524 return ret;
2525}
2526
2527static int proc_id_map_release(struct inode *inode, struct file *file)
2528{
2529 struct seq_file *seq = file->private_data;
2530 struct user_namespace *ns = seq->private;
2531 put_user_ns(ns);
2532 return seq_release(inode, file);
2533}
2534
2535static int proc_uid_map_open(struct inode *inode, struct file *file)
2536{
2537 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2538}
2539
2540static int proc_gid_map_open(struct inode *inode, struct file *file)
2541{
2542 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2543}
2544
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002545static int proc_projid_map_open(struct inode *inode, struct file *file)
2546{
2547 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2548}
2549
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002550static const struct file_operations proc_uid_map_operations = {
2551 .open = proc_uid_map_open,
2552 .write = proc_uid_map_write,
2553 .read = seq_read,
2554 .llseek = seq_lseek,
2555 .release = proc_id_map_release,
2556};
2557
2558static const struct file_operations proc_gid_map_operations = {
2559 .open = proc_gid_map_open,
2560 .write = proc_gid_map_write,
2561 .read = seq_read,
2562 .llseek = seq_lseek,
2563 .release = proc_id_map_release,
2564};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002565
2566static const struct file_operations proc_projid_map_operations = {
2567 .open = proc_projid_map_open,
2568 .write = proc_projid_map_write,
2569 .read = seq_read,
2570 .llseek = seq_lseek,
2571 .release = proc_id_map_release,
2572};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002573#endif /* CONFIG_USER_NS */
2574
Kees Cook47830722008-10-06 03:11:58 +04002575static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2576 struct pid *pid, struct task_struct *task)
2577{
Al Viroa9712bc2011-03-23 15:52:50 -04002578 int err = lock_trace(task);
2579 if (!err) {
2580 seq_printf(m, "%08x\n", task->personality);
2581 unlock_trace(task);
2582 }
2583 return err;
Kees Cook47830722008-10-06 03:11:58 +04002584}
2585
Eric W. Biederman801199c2006-10-02 02:18:48 -07002586/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002587 * Thread groups
2588 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002589static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002590static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002591
Eric Dumazetc5141e62007-05-08 00:26:15 -07002592static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002593 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2594 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002595#ifdef CONFIG_CHECKPOINT_RESTORE
2596 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2597#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002598 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002599 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002600#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002601 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002602#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002603 REG("environ", S_IRUSR, proc_environ_operations),
2604 INF("auxv", S_IRUSR, proc_pid_auxv),
2605 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002606 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002607 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002608#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002609 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002610#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002611#ifdef CONFIG_SCHED_AUTOGROUP
2612 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2613#endif
john stultz4614a696b2009-12-14 18:00:05 -08002614 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002615#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002616 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002617#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002618 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2619 ONE("stat", S_IRUGO, proc_tgid_stat),
2620 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002621 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002622#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002623 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002624#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002625 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2626 LNK("cwd", proc_cwd_link),
2627 LNK("root", proc_root_link),
2628 LNK("exe", proc_exe_link),
2629 REG("mounts", S_IRUGO, proc_mounts_operations),
2630 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2631 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002632#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002633 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002634 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002635 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002636#endif
2637#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002638 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002639#endif
2640#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002641 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002642#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002643#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002644 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002645#endif
2646#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002647 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002649#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002650 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002651#endif
Paul Menage8793d852007-10-18 23:39:39 -07002652#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002653 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002654#endif
Paul Menagea4243162007-10-18 23:39:35 -07002655#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002656 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002657#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002658 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002659 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002660 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002661#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002662 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2663 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002664#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002665#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002666 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002667#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002668#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002669 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002670#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002671#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002672 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002673#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002674#ifdef CONFIG_HARDWALL
2675 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2676#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002677#ifdef CONFIG_USER_NS
2678 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2679 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002680 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002681#endif
Pavel Emelyanov48f6a7a2013-03-11 13:12:45 +04002682#ifdef CONFIG_CHECKPOINT_RESTORE
2683 REG("timers", S_IRUGO, proc_timers_operations),
2684#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002685};
2686
2687static int proc_tgid_base_readdir(struct file * filp,
2688 void * dirent, filldir_t filldir)
2689{
2690 return proc_pident_readdir(filp,dirent,filldir,
2691 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2692}
2693
Arjan van de Ven00977a52007-02-12 00:55:34 -08002694static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002695 .read = generic_read_dir,
2696 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002697 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002698};
2699
Al Viro00cd8dd2012-06-10 17:13:09 -04002700static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2701{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002702 return proc_pident_lookup(dir, dentry,
2703 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002704}
2705
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002706static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002707 .lookup = proc_tgid_base_lookup,
2708 .getattr = pid_getattr,
2709 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002710 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002711};
2712
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002713static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002715 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002716 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002717 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Eric W. Biederman48e64842006-06-26 00:25:48 -07002719 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002720 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002721 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002722 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002723 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002724 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002725 d_drop(dentry);
2726 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Eric W. Biederman48e64842006-06-26 00:25:48 -07002729 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002730 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2731 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002732 if (!leader)
2733 goto out;
2734
2735 name.name = "task";
2736 name.len = strlen(name.name);
2737 dir = d_hash_and_lookup(leader, &name);
2738 if (!dir)
2739 goto out_put_leader;
2740
2741 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002742 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002743 dentry = d_hash_and_lookup(dir, &name);
2744 if (dentry) {
2745 shrink_dcache_parent(dentry);
2746 d_drop(dentry);
2747 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002749
2750 dput(dir);
2751out_put_leader:
2752 dput(leader);
2753out:
2754 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
Randy Dunlap0895e912007-10-21 21:00:10 -07002757/**
2758 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2759 * @task: task that should be flushed.
2760 *
2761 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002762 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002763 * in. This call is supposed to do all of this job.
2764 *
2765 * Looks in the dcache for
2766 * /proc/@pid
2767 * /proc/@tgid/task/@pid
2768 * if either directory is present flushes it and all of it'ts children
2769 * from the dcache.
2770 *
2771 * It is safe and reasonable to cache /proc entries for a task until
2772 * that task exits. After that they just clog up the dcache with
2773 * useless entries, possibly causing useful dcache entries to be
2774 * flushed instead. This routine is proved to flush those useless
2775 * dcache entries at process exit time.
2776 *
2777 * NOTE: This routine is just an optimization so it does not guarantee
2778 * that no dcache entries will exist at process exit time it
2779 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002780 */
2781
2782void proc_flush_task(struct task_struct *task)
2783{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002784 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002785 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002786 struct upid *upid;
2787
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002788 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002789 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002790
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002791 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002792 upid = &pid->numbers[i];
2793 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002794 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002795 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002796}
2797
Adrian Bunk9711ef92006-12-06 20:38:31 -08002798static struct dentry *proc_pid_instantiate(struct inode *dir,
2799 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002800 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002801{
2802 struct dentry *error = ERR_PTR(-ENOENT);
2803 struct inode *inode;
2804
Eric W. Biederman61a28782006-10-02 02:18:49 -07002805 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002806 if (!inode)
2807 goto out;
2808
2809 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2810 inode->i_op = &proc_tgid_base_inode_operations;
2811 inode->i_fop = &proc_tgid_base_operations;
2812 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002813
Miklos Szeredibfe86842011-10-28 14:13:29 +02002814 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2815 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002816
Nick Pigginfb045ad2011-01-07 17:49:55 +11002817 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002818
2819 d_add(dentry, inode);
2820 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002821 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002822 error = NULL;
2823out:
2824 return error;
2825}
2826
Al Viro00cd8dd2012-06-10 17:13:09 -04002827struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828{
Eric W. Biedermane656d8a2010-07-10 14:52:49 -07002829 struct dentry *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002832 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 tgid = name_to_int(dentry);
2835 if (tgid == ~0U)
2836 goto out;
2837
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002838 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002839 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002840 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (task)
2842 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002843 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (!task)
2845 goto out;
2846
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002847 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002848 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002850 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002854 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002855 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002857struct tgid_iter {
2858 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002859 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002860};
2861static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2862{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002863 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002865 if (iter.task)
2866 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002867 rcu_read_lock();
2868retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002869 iter.task = NULL;
2870 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002871 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002872 iter.tgid = pid_nr_ns(pid, ns);
2873 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002874 /* What we to know is if the pid we have find is the
2875 * pid of a thread_group_leader. Testing for task
2876 * being a thread_group_leader is the obvious thing
2877 * todo but there is a window when it fails, due to
2878 * the pid transfer logic in de_thread.
2879 *
2880 * So we perform the straight forward test of seeing
2881 * if the pid we have found is the pid of a thread
2882 * group leader, and don't worry if the task we have
2883 * found doesn't happen to be a thread group leader.
2884 * As we don't care in the case of readdir.
2885 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002886 if (!iter.task || !has_group_leader_pid(iter.task)) {
2887 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002888 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002889 }
2890 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002892 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002893 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894}
2895
Eric W. Biedermane656d8a2010-07-10 14:52:49 -07002896#define TGID_OFFSET (FIRST_PROCESS_ENTRY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Eric W. Biederman61a28782006-10-02 02:18:49 -07002898static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002899 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002900{
2901 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002902 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002903 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002904 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002905}
2906
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002907static int fake_filldir(void *buf, const char *name, int namelen,
2908 loff_t offset, u64 ino, unsigned d_type)
2909{
2910 return 0;
2911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913/* for the /proc/ directory itself, after non-process stuff has been done */
2914int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2915{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002916 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002917 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002918 filldir_t __filldir;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Linus Torvaldsd8bdc592011-04-18 10:36:54 -07002920 if (filp->f_pos >= PID_MAX_LIMIT + TGID_OFFSET)
Eric W. Biedermanae06c7c2010-07-10 15:23:34 -07002921 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002923 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002924 iter.task = NULL;
2925 iter.tgid = filp->f_pos - TGID_OFFSET;
2926 for (iter = next_tgid(ns, iter);
2927 iter.task;
2928 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002929 if (has_pid_permissions(ns, iter.task, 2))
2930 __filldir = filldir;
2931 else
2932 __filldir = fake_filldir;
2933
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002934 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002935 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002936 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002937 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002940 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2941out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return 0;
2943}
2944
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002945/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002946 * Tasks
2947 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002948static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002949 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002950 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002951 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002952 REG("environ", S_IRUSR, proc_environ_operations),
2953 INF("auxv", S_IRUSR, proc_pid_auxv),
2954 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002955 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002956 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002957#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002958 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002959#endif
john stultz4614a696b2009-12-14 18:00:05 -08002960 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002961#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002962 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002963#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002964 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2965 ONE("stat", S_IRUGO, proc_tid_stat),
2966 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002967 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002968#ifdef CONFIG_CHECKPOINT_RESTORE
2969 REG("children", S_IRUGO, proc_tid_children_operations),
2970#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002971#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002972 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002973#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002974 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2975 LNK("cwd", proc_cwd_link),
2976 LNK("root", proc_root_link),
2977 LNK("exe", proc_exe_link),
2978 REG("mounts", S_IRUGO, proc_mounts_operations),
2979 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002980#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002981 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002982 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002983 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002984#endif
2985#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002986 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002987#endif
2988#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002989 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002990#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002991#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002992 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002993#endif
2994#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002995 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002996#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002997#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002998 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002999#endif
Paul Menage8793d852007-10-18 23:39:39 -07003000#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003001 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003002#endif
Paul Menagea4243162007-10-18 23:39:35 -07003003#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003004 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003005#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003006 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08003007 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003008 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003009#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003010 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003011 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003012#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003013#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003014 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003015#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003016#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04003017 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003018#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04003019#ifdef CONFIG_HARDWALL
3020 INF("hardwall", S_IRUGO, proc_pid_hardwall),
3021#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003022#ifdef CONFIG_USER_NS
3023 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
3024 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07003025 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08003026#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003027};
3028
3029static int proc_tid_base_readdir(struct file * filp,
3030 void * dirent, filldir_t filldir)
3031{
3032 return proc_pident_readdir(filp,dirent,filldir,
3033 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3034}
3035
Al Viro00cd8dd2012-06-10 17:13:09 -04003036static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
3037{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003038 return proc_pident_lookup(dir, dentry,
3039 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003040}
3041
Arjan van de Ven00977a52007-02-12 00:55:34 -08003042static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003043 .read = generic_read_dir,
3044 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003045 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003046};
3047
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003048static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003049 .lookup = proc_tid_base_lookup,
3050 .getattr = pid_getattr,
3051 .setattr = proc_setattr,
3052};
3053
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003054static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003055 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003056{
3057 struct dentry *error = ERR_PTR(-ENOENT);
3058 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003059 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003060
3061 if (!inode)
3062 goto out;
3063 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3064 inode->i_op = &proc_tid_base_inode_operations;
3065 inode->i_fop = &proc_tid_base_operations;
3066 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003067
Miklos Szeredibfe86842011-10-28 14:13:29 +02003068 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3069 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003070
Nick Pigginfb045ad2011-01-07 17:49:55 +11003071 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003072
3073 d_add(dentry, inode);
3074 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003075 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003076 error = NULL;
3077out:
3078 return error;
3079}
3080
Al Viro00cd8dd2012-06-10 17:13:09 -04003081static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003082{
3083 struct dentry *result = ERR_PTR(-ENOENT);
3084 struct task_struct *task;
3085 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003086 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003087 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003088
3089 if (!leader)
3090 goto out_no_task;
3091
3092 tid = name_to_int(dentry);
3093 if (tid == ~0U)
3094 goto out;
3095
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003096 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003097 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003098 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003099 if (task)
3100 get_task_struct(task);
3101 rcu_read_unlock();
3102 if (!task)
3103 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003104 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003105 goto out_drop_task;
3106
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003107 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003108out_drop_task:
3109 put_task_struct(task);
3110out:
3111 put_task_struct(leader);
3112out_no_task:
3113 return result;
3114}
3115
3116/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003117 * Find the first tid of a thread group to return to user space.
3118 *
3119 * Usually this is just the thread group leader, but if the users
3120 * buffer was too small or there was a seek into the middle of the
3121 * directory we have more work todo.
3122 *
3123 * In the case of a short read we start with find_task_by_pid.
3124 *
3125 * In the case of a seek we start with the leader and walk nr
3126 * threads past it.
3127 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003128static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003129 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003130{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003131 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003132
Eric W. Biedermancc288732006-06-26 00:26:01 -07003133 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003134 /* Attempt to start with the pid of a thread */
3135 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003136 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003137 if (pos && (pos->group_leader == leader))
3138 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003139 }
3140
3141 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003142 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003143 if (nr && nr >= get_nr_threads(leader))
3144 goto out;
3145
3146 /* If we haven't found our starting place yet start
3147 * with the leader and walk nr threads forward.
3148 */
3149 for (pos = leader; nr > 0; --nr) {
3150 pos = next_thread(pos);
3151 if (pos == leader) {
3152 pos = NULL;
3153 goto out;
3154 }
3155 }
3156found:
3157 get_task_struct(pos);
3158out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003159 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003160 return pos;
3161}
3162
3163/*
3164 * Find the next thread in the thread list.
3165 * Return NULL if there is an error or no next thread.
3166 *
3167 * The reference to the input task_struct is released.
3168 */
3169static struct task_struct *next_tid(struct task_struct *start)
3170{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003171 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003172 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003173 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003174 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003175 if (thread_group_leader(pos))
3176 pos = NULL;
3177 else
3178 get_task_struct(pos);
3179 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003180 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003181 put_task_struct(start);
3182 return pos;
3183}
3184
Eric W. Biederman61a28782006-10-02 02:18:49 -07003185static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3186 struct task_struct *task, int tid)
3187{
3188 char name[PROC_NUMBUF];
3189 int len = snprintf(name, sizeof(name), "%d", tid);
3190 return proc_fill_cache(filp, dirent, filldir, name, len,
3191 proc_task_instantiate, task, NULL);
3192}
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194/* for the /proc/TGID/task/ directories */
3195static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3196{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003197 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003199 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003200 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 int retval = -ENOENT;
3202 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003203 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003204 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003206 task = get_proc_task(inode);
3207 if (!task)
3208 goto out_no_task;
3209 rcu_read_lock();
3210 if (pid_alive(task)) {
3211 leader = task->group_leader;
3212 get_task_struct(leader);
3213 }
3214 rcu_read_unlock();
3215 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003216 if (!leader)
3217 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 retval = 0;
3219
Linus Torvaldsee568b22009-03-17 10:02:35 -07003220 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 case 0:
3222 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003223 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003225 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 /* fall through */
3227 case 1:
3228 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003229 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003231 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 /* fall through */
3233 }
3234
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003235 /* f_version caches the tgid value that the last readdir call couldn't
3236 * return. lseek aka telldir automagically resets f_version to 0.
3237 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003238 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003239 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003240 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003241 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003242 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003243 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003244 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003245 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003246 /* returning this tgid failed, save it as the first
3247 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003248 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003249 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 }
3253out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003254 put_task_struct(leader);
3255out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return retval;
3257}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003258
3259static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3260{
3261 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003262 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003263 generic_fillattr(inode, stat);
3264
Eric W. Biederman99f89552006-06-26 00:25:55 -07003265 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003266 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003267 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003268 }
3269
3270 return 0;
3271}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003272
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003273static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003274 .lookup = proc_task_lookup,
3275 .getattr = proc_task_getattr,
3276 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003277 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003278};
3279
Arjan van de Ven00977a52007-02-12 00:55:34 -08003280static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003281 .read = generic_read_dir,
3282 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003283 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003284};