blob: 8281986693be137530e1cc7f8e063e892e4b3568 [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>
Chris Metcalff133ecc2011-05-26 12:40:09 -040089#ifdef CONFIG_HARDWALL
90#include <asm/hardwall.h>
91#endif
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -080092#include <trace/events/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include "internal.h"
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +040094#include "fd.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070096/* NOTE:
97 * Implementing inode permission operations in /proc is almost
98 * certainly an error. Permission checks need to happen during
99 * each system call not at open time. The reason is that most of
100 * what we wish to check for permissions in /proc varies at runtime.
101 *
102 * The classic example of a problem is opening file descriptors
103 * in /proc for a task before it execs a suid executable.
104 */
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700108 int len;
Al Virod161a132011-07-24 03:36:29 -0400109 umode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800110 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800111 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700112 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Eric W. Biederman61a28782006-10-02 02:18:49 -0700115#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700117 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700118 .mode = MODE, \
119 .iop = IOP, \
120 .fop = FOP, \
121 .op = OP, \
122}
123
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300124#define DIR(NAME, MODE, iops, fops) \
125 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
126#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700127 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700128 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300129 { .proc_get_link = get_link } )
130#define REG(NAME, MODE, fops) \
131 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
132#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700133 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700134 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300135 { .proc_read = read } )
136#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800137 NOD(NAME, (S_IFREG|(MODE)), \
138 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300139 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Vegard Nossumaed54172008-06-05 22:46:53 -0700141/*
142 * Count the number of hardlinks for the pid_entry table, excluding the .
143 * and .. links.
144 */
145static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
146 unsigned int n)
147{
148 unsigned int i;
149 unsigned int count;
150
151 count = 0;
152 for (i = 0; i < n; ++i) {
153 if (S_ISDIR(entries[i].mode))
154 ++count;
155 }
156
157 return count;
158}
159
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200160static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000162 int result = -ENOENT;
163
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700164 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200165 if (task->fs) {
166 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 result = 0;
168 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700169 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000170 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171}
172
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800173static int proc_cwd_link(struct dentry *dentry, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700174{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800175 struct task_struct *task = get_proc_task(dentry->d_inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700176 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177
178 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200179 task_lock(task);
180 if (task->fs) {
181 get_fs_pwd(task->fs, path);
182 result = 0;
183 }
184 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return result;
188}
189
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800190static int proc_root_link(struct dentry *dentry, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -0800192 struct task_struct *task = get_proc_task(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194
195 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200196 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700197 put_task_struct(task);
198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return result;
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int proc_pid_cmdline(struct task_struct *task, char * buffer)
203{
204 int res = 0;
205 unsigned int len;
206 struct mm_struct *mm = get_task_mm(task);
207 if (!mm)
208 goto out;
209 if (!mm->arg_end)
210 goto out_mm; /* Shh! No looking before we're done */
211
212 len = mm->arg_end - mm->arg_start;
213
214 if (len > PAGE_SIZE)
215 len = PAGE_SIZE;
216
217 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
218
219 // If the nul at the end of args has been overwritten, then
220 // assume application is using setproctitle(3).
221 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
222 len = strnlen(buffer, res);
223 if (len < res) {
224 res = len;
225 } else {
226 len = mm->env_end - mm->env_start;
227 if (len > PAGE_SIZE - res)
228 len = PAGE_SIZE - res;
229 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
230 res = strnlen(buffer, res);
231 }
232 }
233out_mm:
234 mmput(mm);
235out:
236 return res;
237}
238
239static int proc_pid_auxv(struct task_struct *task, char *buffer)
240{
Cong Wange7dcd992012-05-31 16:26:17 -0700241 struct mm_struct *mm = mm_access(task, PTRACE_MODE_READ);
Al Viro2fadaef2011-02-15 22:52:11 -0500242 int res = PTR_ERR(mm);
243 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300245 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300247 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 res = nwords * sizeof(mm->saved_auxv[0]);
249 if (res > PAGE_SIZE)
250 res = PAGE_SIZE;
251 memcpy(buffer, mm->saved_auxv, res);
252 mmput(mm);
253 }
254 return res;
255}
256
257
258#ifdef CONFIG_KALLSYMS
259/*
260 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
261 * Returns the resolved symbol. If that fails, simply return the address.
262 */
263static int proc_pid_wchan(struct task_struct *task, char *buffer)
264{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700265 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700266 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 wchan = get_wchan(task);
269
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700270 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600271 if (!ptrace_may_access(task, PTRACE_MODE_READ))
272 return 0;
273 else
274 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700275 else
276 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278#endif /* CONFIG_KALLSYMS */
279
Al Viroa9712bc2011-03-23 15:52:50 -0400280static int lock_trace(struct task_struct *task)
281{
282 int err = mutex_lock_killable(&task->signal->cred_guard_mutex);
283 if (err)
284 return err;
285 if (!ptrace_may_access(task, PTRACE_MODE_ATTACH)) {
286 mutex_unlock(&task->signal->cred_guard_mutex);
287 return -EPERM;
288 }
289 return 0;
290}
291
292static void unlock_trace(struct task_struct *task)
293{
294 mutex_unlock(&task->signal->cred_guard_mutex);
295}
296
Ken Chen2ec220e2008-11-10 11:26:08 +0300297#ifdef CONFIG_STACKTRACE
298
299#define MAX_STACK_TRACE_DEPTH 64
300
301static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
302 struct pid *pid, struct task_struct *task)
303{
304 struct stack_trace trace;
305 unsigned long *entries;
Al Viroa9712bc2011-03-23 15:52:50 -0400306 int err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300307 int i;
308
309 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
310 if (!entries)
311 return -ENOMEM;
312
313 trace.nr_entries = 0;
314 trace.max_entries = MAX_STACK_TRACE_DEPTH;
315 trace.entries = entries;
316 trace.skip = 0;
Ken Chen2ec220e2008-11-10 11:26:08 +0300317
Al Viroa9712bc2011-03-23 15:52:50 -0400318 err = lock_trace(task);
319 if (!err) {
320 save_stack_trace_tsk(task, &trace);
321
322 for (i = 0; i < trace.nr_entries; i++) {
Linus Torvaldsb81a6182011-03-23 20:51:42 -0700323 seq_printf(m, "[<%pK>] %pS\n",
Al Viroa9712bc2011-03-23 15:52:50 -0400324 (void *)entries[i], (void *)entries[i]);
325 }
326 unlock_trace(task);
Ken Chen2ec220e2008-11-10 11:26:08 +0300327 }
328 kfree(entries);
329
Al Viroa9712bc2011-03-23 15:52:50 -0400330 return err;
Ken Chen2ec220e2008-11-10 11:26:08 +0300331}
332#endif
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#ifdef CONFIG_SCHEDSTATS
335/*
336 * Provides /proc/PID/schedstat
337 */
338static int proc_pid_schedstat(struct task_struct *task, char *buffer)
339{
Balbir Singh172ba842007-07-09 18:52:00 +0200340 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100341 (unsigned long long)task->se.sum_exec_runtime,
342 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200343 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345#endif
346
Arjan van de Ven97455122008-01-25 21:08:34 +0100347#ifdef CONFIG_LATENCYTOP
348static int lstats_show_proc(struct seq_file *m, void *v)
349{
350 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800351 struct inode *inode = m->private;
352 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100353
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800354 if (!task)
355 return -ESRCH;
356 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100357 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800358 struct latency_record *lr = &task->latency_record[i];
359 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100360 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800361 seq_printf(m, "%i %li %li",
362 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100363 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800364 unsigned long bt = lr->backtrace[q];
365 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100366 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800367 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100368 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800369 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100370 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800371 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100372 }
373
374 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800375 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100376 return 0;
377}
378
379static int lstats_open(struct inode *inode, struct file *file)
380{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800381 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800382}
383
Arjan van de Ven97455122008-01-25 21:08:34 +0100384static ssize_t lstats_write(struct file *file, const char __user *buf,
385 size_t count, loff_t *offs)
386{
Al Viro496ad9a2013-01-23 17:07:38 -0500387 struct task_struct *task = get_proc_task(file_inode(file));
Arjan van de Ven97455122008-01-25 21:08:34 +0100388
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800389 if (!task)
390 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100391 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800392 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100393
394 return count;
395}
396
397static const struct file_operations proc_lstats_operations = {
398 .open = lstats_open,
399 .read = seq_read,
400 .write = lstats_write,
401 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800402 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100403};
404
405#endif
406
Al Viro8d8b97b2013-04-19 23:11:24 -0400407#ifdef CONFIG_CGROUPS
408static int cgroup_open(struct inode *inode, struct file *file)
409{
410 struct pid *pid = PROC_I(inode)->pid;
411 return single_open(file, proc_cgroup_show, pid);
412}
413
414static const struct file_operations proc_cgroup_operations = {
415 .open = cgroup_open,
416 .read = seq_read,
417 .llseek = seq_lseek,
418 .release = single_release,
419};
420#endif
421
422#ifdef CONFIG_PROC_PID_CPUSET
423
424static int cpuset_open(struct inode *inode, struct file *file)
425{
426 struct pid *pid = PROC_I(inode)->pid;
427 return single_open(file, proc_cpuset_show, pid);
428}
429
430static const struct file_operations proc_cpuset_operations = {
431 .open = cpuset_open,
432 .read = seq_read,
433 .llseek = seq_lseek,
434 .release = single_release,
435};
436#endif
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438static int proc_oom_score(struct task_struct *task, char *buffer)
439{
David Rientjesa7f638f2012-05-29 15:06:47 -0700440 unsigned long totalpages = totalram_pages + total_swap_pages;
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200441 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700443 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200444 if (pid_alive(task))
David Rientjesa7f638f2012-05-29 15:06:47 -0700445 points = oom_badness(task, NULL, NULL, totalpages) *
446 1000 / totalpages;
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700447 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return sprintf(buffer, "%lu\n", points);
449}
450
Neil Hormand85f50d2007-10-18 23:40:37 -0700451struct limit_names {
452 char *name;
453 char *unit;
454};
455
456static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700457 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700458 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
459 [RLIMIT_DATA] = {"Max data size", "bytes"},
460 [RLIMIT_STACK] = {"Max stack size", "bytes"},
461 [RLIMIT_CORE] = {"Max core file size", "bytes"},
462 [RLIMIT_RSS] = {"Max resident set", "bytes"},
463 [RLIMIT_NPROC] = {"Max processes", "processes"},
464 [RLIMIT_NOFILE] = {"Max open files", "files"},
465 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
466 [RLIMIT_AS] = {"Max address space", "bytes"},
467 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
468 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
469 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
470 [RLIMIT_NICE] = {"Max nice priority", NULL},
471 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800472 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700473};
474
475/* Display limits for a process */
476static int proc_pid_limits(struct task_struct *task, char *buffer)
477{
478 unsigned int i;
479 int count = 0;
480 unsigned long flags;
481 char *bufptr = buffer;
482
483 struct rlimit rlim[RLIM_NLIMITS];
484
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400485 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700486 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700487 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
488 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700489
490 /*
491 * print the file header
492 */
493 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
494 "Limit", "Soft Limit", "Hard Limit", "Units");
495
496 for (i = 0; i < RLIM_NLIMITS; i++) {
497 if (rlim[i].rlim_cur == RLIM_INFINITY)
498 count += sprintf(&bufptr[count], "%-25s %-20s ",
499 lnames[i].name, "unlimited");
500 else
501 count += sprintf(&bufptr[count], "%-25s %-20lu ",
502 lnames[i].name, rlim[i].rlim_cur);
503
504 if (rlim[i].rlim_max == RLIM_INFINITY)
505 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
506 else
507 count += sprintf(&bufptr[count], "%-20lu ",
508 rlim[i].rlim_max);
509
510 if (lnames[i].unit)
511 count += sprintf(&bufptr[count], "%-10s\n",
512 lnames[i].unit);
513 else
514 count += sprintf(&bufptr[count], "\n");
515 }
516
517 return count;
518}
519
Roland McGrathebcb6732008-07-25 19:46:00 -0700520#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
521static int proc_pid_syscall(struct task_struct *task, char *buffer)
522{
523 long nr;
524 unsigned long args[6], sp, pc;
Al Viroa9712bc2011-03-23 15:52:50 -0400525 int res = lock_trace(task);
526 if (res)
527 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700528
529 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
Al Viroa9712bc2011-03-23 15:52:50 -0400530 res = sprintf(buffer, "running\n");
531 else if (nr < 0)
532 res = sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
533 else
534 res = sprintf(buffer,
Roland McGrathebcb6732008-07-25 19:46:00 -0700535 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
536 nr,
537 args[0], args[1], args[2], args[3], args[4], args[5],
538 sp, pc);
Al Viroa9712bc2011-03-23 15:52:50 -0400539 unlock_trace(task);
540 return res;
Roland McGrathebcb6732008-07-25 19:46:00 -0700541}
542#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544/************************************************************************/
545/* Here the fs part begins */
546/************************************************************************/
547
548/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700549static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700551 struct task_struct *task;
552 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700553 /* Allow access to a task's file descriptors if it is us or we
554 * may use ptrace attach to the process and find out that
555 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700556 */
557 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700558 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400559 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700560 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700561 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700562 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Eric W. Biederman6b4e3062010-03-07 16:41:34 -0800565int proc_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700566{
567 int error;
568 struct inode *inode = dentry->d_inode;
569
570 if (attr->ia_valid & ATTR_MODE)
571 return -EPERM;
572
573 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200574 if (error)
575 return error;
576
Christoph Hellwig10257742010-06-04 11:30:02 +0200577 setattr_copy(inode, attr);
578 mark_inode_dirty(inode);
579 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700580}
581
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800582/*
583 * May current process learn task's sched/cmdline info (for hide_pid_min=1)
584 * or euid/egid (for hide_pid_min=2)?
585 */
586static bool has_pid_permissions(struct pid_namespace *pid,
587 struct task_struct *task,
588 int hide_pid_min)
589{
590 if (pid->hide_pid < hide_pid_min)
591 return true;
592 if (in_group_p(pid->pid_gid))
593 return true;
594 return ptrace_may_access(task, PTRACE_MODE_READ);
595}
596
597
598static int proc_pid_permission(struct inode *inode, int mask)
599{
600 struct pid_namespace *pid = inode->i_sb->s_fs_info;
601 struct task_struct *task;
602 bool has_perms;
603
604 task = get_proc_task(inode);
Xiaotian Fenga2ef9902012-01-12 17:17:08 -0800605 if (!task)
606 return -ESRCH;
Vasiliy Kulikov04996802012-01-10 15:11:31 -0800607 has_perms = has_pid_permissions(pid, task, 1);
608 put_task_struct(task);
609
610 if (!has_perms) {
611 if (pid->hide_pid == 2) {
612 /*
613 * Let's make getdents(), stat(), and open()
614 * consistent with each other. If a process
615 * may not stat() a file, it shouldn't be seen
616 * in procfs at all.
617 */
618 return -ENOENT;
619 }
620
621 return -EPERM;
622 }
623 return generic_permission(inode, mask);
624}
625
626
627
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800628static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700629 .setattr = proc_setattr,
630};
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
633
634static ssize_t proc_info_read(struct file * file, char __user * buf,
635 size_t count, loff_t *ppos)
636{
Al Viro496ad9a2013-01-23 17:07:38 -0500637 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 unsigned long page;
639 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700640 struct task_struct *task = get_proc_task(inode);
641
642 length = -ESRCH;
643 if (!task)
644 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 if (count > PROC_BLOCK_SIZE)
647 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700648
649 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700650 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700651 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 length = PROC_I(inode)->op.proc_read(task, (char*)page);
654
655 if (length >= 0)
656 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
657 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700658out:
659 put_task_struct(task);
660out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 return length;
662}
663
Arjan van de Ven00977a52007-02-12 00:55:34 -0800664static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100666 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667};
668
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800669static int proc_single_show(struct seq_file *m, void *v)
670{
671 struct inode *inode = m->private;
672 struct pid_namespace *ns;
673 struct pid *pid;
674 struct task_struct *task;
675 int ret;
676
677 ns = inode->i_sb->s_fs_info;
678 pid = proc_pid(inode);
679 task = get_pid_task(pid, PIDTYPE_PID);
680 if (!task)
681 return -ESRCH;
682
683 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
684
685 put_task_struct(task);
686 return ret;
687}
688
689static int proc_single_open(struct inode *inode, struct file *filp)
690{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800691 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800692}
693
694static const struct file_operations proc_single_file_operations = {
695 .open = proc_single_open,
696 .read = seq_read,
697 .llseek = seq_lseek,
698 .release = single_release,
699};
700
Cong Wangb409e572012-05-31 16:26:17 -0700701static int __mem_open(struct inode *inode, struct file *file, unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Al Viro496ad9a2013-01-23 17:07:38 -0500703 struct task_struct *task = get_proc_task(file_inode(file));
Linus Torvaldse2683372012-01-17 15:21:19 -0800704 struct mm_struct *mm;
705
706 if (!task)
707 return -ESRCH;
708
Cong Wangb409e572012-05-31 16:26:17 -0700709 mm = mm_access(task, mode);
Linus Torvaldse2683372012-01-17 15:21:19 -0800710 put_task_struct(task);
711
712 if (IS_ERR(mm))
713 return PTR_ERR(mm);
714
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100715 if (mm) {
716 /* ensure this mm_struct can't be freed */
717 atomic_inc(&mm->mm_count);
718 /* but do not pin its memory */
719 mmput(mm);
720 }
721
Linus Torvaldse2683372012-01-17 15:21:19 -0800722 file->private_data = mm;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return 0;
725}
726
Cong Wangb409e572012-05-31 16:26:17 -0700727static int mem_open(struct inode *inode, struct file *file)
728{
Djalal Harounibc452b42012-07-30 14:42:28 -0700729 int ret = __mem_open(inode, file, PTRACE_MODE_ATTACH);
730
731 /* OK to pass negative loff_t, we can catch out-of-range */
732 file->f_mode |= FMODE_UNSIGNED_OFFSET;
733
734 return ret;
Cong Wangb409e572012-05-31 16:26:17 -0700735}
736
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100737static ssize_t mem_rw(struct file *file, char __user *buf,
738 size_t count, loff_t *ppos, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Linus Torvaldse2683372012-01-17 15:21:19 -0800740 struct mm_struct *mm = file->private_data;
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100741 unsigned long addr = *ppos;
742 ssize_t copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 char *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Linus Torvaldse2683372012-01-17 15:21:19 -0800745 if (!mm)
746 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Mel Gormane12ba742007-10-16 01:25:52 -0700748 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (!page)
Linus Torvaldse2683372012-01-17 15:21:19 -0800750 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700752 copied = 0;
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100753 if (!atomic_inc_not_zero(&mm->mm_users))
754 goto free;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 while (count > 0) {
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100757 int this_len = min_t(int, count, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100759 if (write && copy_from_user(page, buf, this_len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 copied = -EFAULT;
761 break;
762 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100763
764 this_len = access_remote_vm(mm, addr, page, this_len, write);
765 if (!this_len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!copied)
767 copied = -EIO;
768 break;
769 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100770
771 if (!write && copy_to_user(buf, page, this_len)) {
772 copied = -EFAULT;
773 break;
774 }
775
776 buf += this_len;
777 addr += this_len;
778 copied += this_len;
779 count -= this_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100781 *ppos = addr;
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700782
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100783 mmput(mm);
784free:
KOSAKI Motohiro30cd8902011-05-26 16:25:52 -0700785 free_page((unsigned long) page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return copied;
787}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Oleg Nesterov572d34b2012-01-31 17:14:54 +0100789static ssize_t mem_read(struct file *file, char __user *buf,
790 size_t count, loff_t *ppos)
791{
792 return mem_rw(file, buf, count, ppos, 0);
793}
794
795static ssize_t mem_write(struct file *file, const char __user *buf,
796 size_t count, loff_t *ppos)
797{
798 return mem_rw(file, (char __user*)buf, count, ppos, 1);
799}
800
Matt Mackall85863e42008-02-04 22:29:04 -0800801loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 switch (orig) {
804 case 0:
805 file->f_pos = offset;
806 break;
807 case 1:
808 file->f_pos += offset;
809 break;
810 default:
811 return -EINVAL;
812 }
813 force_successful_syscall_return();
814 return file->f_pos;
815}
816
Linus Torvaldse2683372012-01-17 15:21:19 -0800817static int mem_release(struct inode *inode, struct file *file)
818{
819 struct mm_struct *mm = file->private_data;
Oleg Nesterov71879d32012-01-31 17:14:38 +0100820 if (mm)
Oleg Nesterov6d08f2c2012-01-31 17:15:11 +0100821 mmdrop(mm);
Linus Torvaldse2683372012-01-17 15:21:19 -0800822 return 0;
823}
824
Arjan van de Ven00977a52007-02-12 00:55:34 -0800825static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 .llseek = mem_lseek,
827 .read = mem_read,
828 .write = mem_write,
829 .open = mem_open,
Linus Torvaldse2683372012-01-17 15:21:19 -0800830 .release = mem_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831};
832
Cong Wangb409e572012-05-31 16:26:17 -0700833static int environ_open(struct inode *inode, struct file *file)
834{
835 return __mem_open(inode, file, PTRACE_MODE_READ);
836}
837
James Pearson315e28c2007-10-16 23:30:17 -0700838static ssize_t environ_read(struct file *file, char __user *buf,
839 size_t count, loff_t *ppos)
840{
James Pearson315e28c2007-10-16 23:30:17 -0700841 char *page;
842 unsigned long src = *ppos;
Cong Wangb409e572012-05-31 16:26:17 -0700843 int ret = 0;
844 struct mm_struct *mm = file->private_data;
James Pearson315e28c2007-10-16 23:30:17 -0700845
Cong Wangb409e572012-05-31 16:26:17 -0700846 if (!mm)
847 return 0;
James Pearson315e28c2007-10-16 23:30:17 -0700848
James Pearson315e28c2007-10-16 23:30:17 -0700849 page = (char *)__get_free_page(GFP_TEMPORARY);
850 if (!page)
Cong Wangb409e572012-05-31 16:26:17 -0700851 return -ENOMEM;
James Pearson315e28c2007-10-16 23:30:17 -0700852
Al Virod6f64b82011-02-15 22:26:01 -0500853 ret = 0;
Cong Wangb409e572012-05-31 16:26:17 -0700854 if (!atomic_inc_not_zero(&mm->mm_users))
855 goto free;
James Pearson315e28c2007-10-16 23:30:17 -0700856 while (count > 0) {
Djalal Harounie8905ec2012-07-30 14:42:26 -0700857 size_t this_len, max_len;
858 int retval;
859
860 if (src >= (mm->env_end - mm->env_start))
861 break;
James Pearson315e28c2007-10-16 23:30:17 -0700862
863 this_len = mm->env_end - (mm->env_start + src);
864
Djalal Harounie8905ec2012-07-30 14:42:26 -0700865 max_len = min_t(size_t, PAGE_SIZE, count);
866 this_len = min(max_len, this_len);
James Pearson315e28c2007-10-16 23:30:17 -0700867
Cong Wangb409e572012-05-31 16:26:17 -0700868 retval = access_remote_vm(mm, (mm->env_start + src),
James Pearson315e28c2007-10-16 23:30:17 -0700869 page, this_len, 0);
870
871 if (retval <= 0) {
872 ret = retval;
873 break;
874 }
875
876 if (copy_to_user(buf, page, retval)) {
877 ret = -EFAULT;
878 break;
879 }
880
881 ret += retval;
882 src += retval;
883 buf += retval;
884 count -= retval;
885 }
886 *ppos = src;
James Pearson315e28c2007-10-16 23:30:17 -0700887 mmput(mm);
Cong Wangb409e572012-05-31 16:26:17 -0700888
889free:
James Pearson315e28c2007-10-16 23:30:17 -0700890 free_page((unsigned long) page);
James Pearson315e28c2007-10-16 23:30:17 -0700891 return ret;
892}
893
894static const struct file_operations proc_environ_operations = {
Cong Wangb409e572012-05-31 16:26:17 -0700895 .open = environ_open,
James Pearson315e28c2007-10-16 23:30:17 -0700896 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100897 .llseek = generic_file_llseek,
Cong Wangb409e572012-05-31 16:26:17 -0700898 .release = mem_release,
James Pearson315e28c2007-10-16 23:30:17 -0700899};
900
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800901static ssize_t oom_adj_read(struct file *file, char __user *buf, size_t count,
902 loff_t *ppos)
903{
Al Viro496ad9a2013-01-23 17:07:38 -0500904 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800905 char buffer[PROC_NUMBUF];
906 int oom_adj = OOM_ADJUST_MIN;
907 size_t len;
908 unsigned long flags;
909
910 if (!task)
911 return -ESRCH;
912 if (lock_task_sighand(task, &flags)) {
913 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MAX)
914 oom_adj = OOM_ADJUST_MAX;
915 else
916 oom_adj = (task->signal->oom_score_adj * -OOM_DISABLE) /
917 OOM_SCORE_ADJ_MAX;
918 unlock_task_sighand(task, &flags);
919 }
920 put_task_struct(task);
921 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_adj);
922 return simple_read_from_buffer(buf, count, ppos, buffer, len);
923}
924
925static ssize_t oom_adj_write(struct file *file, const char __user *buf,
926 size_t count, loff_t *ppos)
927{
928 struct task_struct *task;
929 char buffer[PROC_NUMBUF];
930 int oom_adj;
931 unsigned long flags;
932 int err;
933
934 memset(buffer, 0, sizeof(buffer));
935 if (count > sizeof(buffer) - 1)
936 count = sizeof(buffer) - 1;
937 if (copy_from_user(buffer, buf, count)) {
938 err = -EFAULT;
939 goto out;
940 }
941
942 err = kstrtoint(strstrip(buffer), 0, &oom_adj);
943 if (err)
944 goto out;
945 if ((oom_adj < OOM_ADJUST_MIN || oom_adj > OOM_ADJUST_MAX) &&
946 oom_adj != OOM_DISABLE) {
947 err = -EINVAL;
948 goto out;
949 }
950
Al Viro496ad9a2013-01-23 17:07:38 -0500951 task = get_proc_task(file_inode(file));
David Rientjesfa0cbbf2012-11-12 17:53:04 -0800952 if (!task) {
953 err = -ESRCH;
954 goto out;
955 }
956
957 task_lock(task);
958 if (!task->mm) {
959 err = -EINVAL;
960 goto err_task_lock;
961 }
962
963 if (!lock_task_sighand(task, &flags)) {
964 err = -ESRCH;
965 goto err_task_lock;
966 }
967
968 /*
969 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
970 * value is always attainable.
971 */
972 if (oom_adj == OOM_ADJUST_MAX)
973 oom_adj = OOM_SCORE_ADJ_MAX;
974 else
975 oom_adj = (oom_adj * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE;
976
977 if (oom_adj < task->signal->oom_score_adj &&
978 !capable(CAP_SYS_RESOURCE)) {
979 err = -EACCES;
980 goto err_sighand;
981 }
982
983 /*
984 * /proc/pid/oom_adj is provided for legacy purposes, ask users to use
985 * /proc/pid/oom_score_adj instead.
986 */
Andrew Morton87ebdc02013-02-27 17:03:16 -0800987 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 -0800988 current->comm, task_pid_nr(current), task_pid_nr(task),
989 task_pid_nr(task));
990
991 task->signal->oom_score_adj = oom_adj;
992 trace_oom_score_adj_update(task);
993err_sighand:
994 unlock_task_sighand(task, &flags);
995err_task_lock:
996 task_unlock(task);
997 put_task_struct(task);
998out:
999 return err < 0 ? err : count;
1000}
1001
1002static const struct file_operations proc_oom_adj_operations = {
1003 .read = oom_adj_read,
1004 .write = oom_adj_write,
1005 .llseek = generic_file_llseek,
1006};
1007
David Rientjesa63d83f2010-08-09 17:19:46 -07001008static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1009 size_t count, loff_t *ppos)
1010{
Al Viro496ad9a2013-01-23 17:07:38 -05001011 struct task_struct *task = get_proc_task(file_inode(file));
David Rientjesa63d83f2010-08-09 17:19:46 -07001012 char buffer[PROC_NUMBUF];
David Rientjesa9c58b902012-12-11 16:02:54 -08001013 short oom_score_adj = OOM_SCORE_ADJ_MIN;
David Rientjesa63d83f2010-08-09 17:19:46 -07001014 unsigned long flags;
1015 size_t len;
1016
1017 if (!task)
1018 return -ESRCH;
1019 if (lock_task_sighand(task, &flags)) {
1020 oom_score_adj = task->signal->oom_score_adj;
1021 unlock_task_sighand(task, &flags);
1022 }
1023 put_task_struct(task);
David Rientjesa9c58b902012-12-11 16:02:54 -08001024 len = snprintf(buffer, sizeof(buffer), "%hd\n", oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001025 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1026}
1027
1028static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1029 size_t count, loff_t *ppos)
1030{
1031 struct task_struct *task;
1032 char buffer[PROC_NUMBUF];
1033 unsigned long flags;
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001034 int oom_score_adj;
David Rientjesa63d83f2010-08-09 17:19:46 -07001035 int err;
1036
1037 memset(buffer, 0, sizeof(buffer));
1038 if (count > sizeof(buffer) - 1)
1039 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001040 if (copy_from_user(buffer, buf, count)) {
1041 err = -EFAULT;
1042 goto out;
1043 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001044
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001045 err = kstrtoint(strstrip(buffer), 0, &oom_score_adj);
David Rientjesa63d83f2010-08-09 17:19:46 -07001046 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001047 goto out;
David Rientjesa63d83f2010-08-09 17:19:46 -07001048 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001049 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1050 err = -EINVAL;
1051 goto out;
1052 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001053
Al Viro496ad9a2013-01-23 17:07:38 -05001054 task = get_proc_task(file_inode(file));
David Rientjes723548b2010-10-26 14:21:25 -07001055 if (!task) {
1056 err = -ESRCH;
1057 goto out;
1058 }
David Rientjesa63d83f2010-08-09 17:19:46 -07001059
Ying Han3d5992d2010-10-26 14:21:23 -07001060 task_lock(task);
1061 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001062 err = -EINVAL;
1063 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001064 }
David Rientjesd19d5472010-10-26 14:21:26 -07001065
1066 if (!lock_task_sighand(task, &flags)) {
1067 err = -ESRCH;
1068 goto err_task_lock;
1069 }
1070
David Rientjesa9c58b902012-12-11 16:02:54 -08001071 if ((short)oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001072 !capable(CAP_SYS_RESOURCE)) {
1073 err = -EACCES;
1074 goto err_sighand;
1075 }
1076
David Rientjesa9c58b902012-12-11 16:02:54 -08001077 task->signal->oom_score_adj = (short)oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001078 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
David Rientjesa9c58b902012-12-11 16:02:54 -08001079 task->signal->oom_score_adj_min = (short)oom_score_adj;
KAMEZAWA Hiroyuki43d2b112012-01-10 15:08:09 -08001080 trace_oom_score_adj_update(task);
Davidlohr Bueso01dc52e2012-10-08 16:29:30 -07001081
David Rientjes723548b2010-10-26 14:21:25 -07001082err_sighand:
David Rientjesa63d83f2010-08-09 17:19:46 -07001083 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001084err_task_lock:
1085 task_unlock(task);
David Rientjesa63d83f2010-08-09 17:19:46 -07001086 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001087out:
1088 return err < 0 ? err : count;
David Rientjesa63d83f2010-08-09 17:19:46 -07001089}
1090
1091static const struct file_operations proc_oom_score_adj_operations = {
1092 .read = oom_score_adj_read,
1093 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001094 .llseek = default_llseek,
David Rientjesa63d83f2010-08-09 17:19:46 -07001095};
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097#ifdef CONFIG_AUDITSYSCALL
1098#define TMPBUFLEN 21
1099static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1100 size_t count, loff_t *ppos)
1101{
Al Viro496ad9a2013-01-23 17:07:38 -05001102 struct inode * inode = file_inode(file);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001103 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 ssize_t length;
1105 char tmpbuf[TMPBUFLEN];
1106
Eric W. Biederman99f89552006-06-26 00:25:55 -07001107 if (!task)
1108 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001110 from_kuid(file->f_cred->user_ns,
1111 audit_get_loginuid(task)));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001112 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1114}
1115
1116static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1117 size_t count, loff_t *ppos)
1118{
Al Viro496ad9a2013-01-23 17:07:38 -05001119 struct inode * inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 char *page, *tmp;
1121 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 uid_t loginuid;
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001123 kuid_t kloginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001125 rcu_read_lock();
1126 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1127 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001129 }
1130 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Al Viroe0182902006-05-18 08:28:02 -04001132 if (count >= PAGE_SIZE)
1133 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 if (*ppos != 0) {
1136 /* No partial writes. */
1137 return -EINVAL;
1138 }
Mel Gormane12ba742007-10-16 01:25:52 -07001139 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 if (!page)
1141 return -ENOMEM;
1142 length = -EFAULT;
1143 if (copy_from_user(page, buf, count))
1144 goto out_free_page;
1145
Al Viroe0182902006-05-18 08:28:02 -04001146 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 loginuid = simple_strtoul(page, &tmp, 10);
1148 if (tmp == page) {
1149 length = -EINVAL;
1150 goto out_free_page;
1151
1152 }
Eric W. Biedermane1760bd2012-09-10 22:39:43 -07001153 kloginuid = make_kuid(file->f_cred->user_ns, loginuid);
1154 if (!uid_valid(kloginuid)) {
1155 length = -EINVAL;
1156 goto out_free_page;
1157 }
1158
1159 length = audit_set_loginuid(kloginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (likely(length == 0))
1161 length = count;
1162
1163out_free_page:
1164 free_page((unsigned long) page);
1165 return length;
1166}
1167
Arjan van de Ven00977a52007-02-12 00:55:34 -08001168static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 .read = proc_loginuid_read,
1170 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001171 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172};
Eric Paris1e0bd752008-03-13 08:15:31 -04001173
1174static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1175 size_t count, loff_t *ppos)
1176{
Al Viro496ad9a2013-01-23 17:07:38 -05001177 struct inode * inode = file_inode(file);
Eric Paris1e0bd752008-03-13 08:15:31 -04001178 struct task_struct *task = get_proc_task(inode);
1179 ssize_t length;
1180 char tmpbuf[TMPBUFLEN];
1181
1182 if (!task)
1183 return -ESRCH;
1184 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1185 audit_get_sessionid(task));
1186 put_task_struct(task);
1187 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1188}
1189
1190static const struct file_operations proc_sessionid_operations = {
1191 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001192 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001193};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194#endif
1195
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001196#ifdef CONFIG_FAULT_INJECTION
1197static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1198 size_t count, loff_t *ppos)
1199{
Al Viro496ad9a2013-01-23 17:07:38 -05001200 struct task_struct *task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001201 char buffer[PROC_NUMBUF];
1202 size_t len;
1203 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001204
1205 if (!task)
1206 return -ESRCH;
1207 make_it_fail = task->make_it_fail;
1208 put_task_struct(task);
1209
1210 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001211
1212 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001213}
1214
1215static ssize_t proc_fault_inject_write(struct file * file,
1216 const char __user * buf, size_t count, loff_t *ppos)
1217{
1218 struct task_struct *task;
1219 char buffer[PROC_NUMBUF], *end;
1220 int make_it_fail;
1221
1222 if (!capable(CAP_SYS_RESOURCE))
1223 return -EPERM;
1224 memset(buffer, 0, sizeof(buffer));
1225 if (count > sizeof(buffer) - 1)
1226 count = sizeof(buffer) - 1;
1227 if (copy_from_user(buffer, buf, count))
1228 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001229 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1230 if (*end)
1231 return -EINVAL;
Al Viro496ad9a2013-01-23 17:07:38 -05001232 task = get_proc_task(file_inode(file));
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001233 if (!task)
1234 return -ESRCH;
1235 task->make_it_fail = make_it_fail;
1236 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001237
1238 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001239}
1240
Arjan van de Ven00977a52007-02-12 00:55:34 -08001241static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001242 .read = proc_fault_inject_read,
1243 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001244 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001245};
1246#endif
1247
Arjan van de Ven97455122008-01-25 21:08:34 +01001248
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001249#ifdef CONFIG_SCHED_DEBUG
1250/*
1251 * Print out various scheduling related per-task fields:
1252 */
1253static int sched_show(struct seq_file *m, void *v)
1254{
1255 struct inode *inode = m->private;
1256 struct task_struct *p;
1257
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001258 p = get_proc_task(inode);
1259 if (!p)
1260 return -ESRCH;
1261 proc_sched_show_task(p, m);
1262
1263 put_task_struct(p);
1264
1265 return 0;
1266}
1267
1268static ssize_t
1269sched_write(struct file *file, const char __user *buf,
1270 size_t count, loff_t *offset)
1271{
Al Viro496ad9a2013-01-23 17:07:38 -05001272 struct inode *inode = file_inode(file);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001273 struct task_struct *p;
1274
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001275 p = get_proc_task(inode);
1276 if (!p)
1277 return -ESRCH;
1278 proc_sched_set_task(p);
1279
1280 put_task_struct(p);
1281
1282 return count;
1283}
1284
1285static int sched_open(struct inode *inode, struct file *filp)
1286{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001287 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001288}
1289
1290static const struct file_operations proc_pid_sched_operations = {
1291 .open = sched_open,
1292 .read = seq_read,
1293 .write = sched_write,
1294 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001295 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001296};
1297
1298#endif
1299
Mike Galbraith5091faa2010-11-30 14:18:03 +01001300#ifdef CONFIG_SCHED_AUTOGROUP
1301/*
1302 * Print out autogroup related information:
1303 */
1304static int sched_autogroup_show(struct seq_file *m, void *v)
1305{
1306 struct inode *inode = m->private;
1307 struct task_struct *p;
1308
1309 p = get_proc_task(inode);
1310 if (!p)
1311 return -ESRCH;
1312 proc_sched_autogroup_show_task(p, m);
1313
1314 put_task_struct(p);
1315
1316 return 0;
1317}
1318
1319static ssize_t
1320sched_autogroup_write(struct file *file, const char __user *buf,
1321 size_t count, loff_t *offset)
1322{
Al Viro496ad9a2013-01-23 17:07:38 -05001323 struct inode *inode = file_inode(file);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001324 struct task_struct *p;
1325 char buffer[PROC_NUMBUF];
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001326 int nice;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001327 int err;
1328
1329 memset(buffer, 0, sizeof(buffer));
1330 if (count > sizeof(buffer) - 1)
1331 count = sizeof(buffer) - 1;
1332 if (copy_from_user(buffer, buf, count))
1333 return -EFAULT;
1334
Alexey Dobriyan0a8cb8e2011-05-26 16:25:50 -07001335 err = kstrtoint(strstrip(buffer), 0, &nice);
1336 if (err < 0)
1337 return err;
Mike Galbraith5091faa2010-11-30 14:18:03 +01001338
1339 p = get_proc_task(inode);
1340 if (!p)
1341 return -ESRCH;
1342
Hiroshi Shimamoto2e5b5b32012-02-23 17:41:27 +09001343 err = proc_sched_autogroup_set_nice(p, nice);
Mike Galbraith5091faa2010-11-30 14:18:03 +01001344 if (err)
1345 count = err;
1346
1347 put_task_struct(p);
1348
1349 return count;
1350}
1351
1352static int sched_autogroup_open(struct inode *inode, struct file *filp)
1353{
1354 int ret;
1355
1356 ret = single_open(filp, sched_autogroup_show, NULL);
1357 if (!ret) {
1358 struct seq_file *m = filp->private_data;
1359
1360 m->private = inode;
1361 }
1362 return ret;
1363}
1364
1365static const struct file_operations proc_pid_sched_autogroup_operations = {
1366 .open = sched_autogroup_open,
1367 .read = seq_read,
1368 .write = sched_autogroup_write,
1369 .llseek = seq_lseek,
1370 .release = single_release,
1371};
1372
1373#endif /* CONFIG_SCHED_AUTOGROUP */
1374
john stultz4614a696b2009-12-14 18:00:05 -08001375static ssize_t comm_write(struct file *file, const char __user *buf,
1376 size_t count, loff_t *offset)
1377{
Al Viro496ad9a2013-01-23 17:07:38 -05001378 struct inode *inode = file_inode(file);
john stultz4614a696b2009-12-14 18:00:05 -08001379 struct task_struct *p;
1380 char buffer[TASK_COMM_LEN];
1381
1382 memset(buffer, 0, sizeof(buffer));
1383 if (count > sizeof(buffer) - 1)
1384 count = sizeof(buffer) - 1;
1385 if (copy_from_user(buffer, buf, count))
1386 return -EFAULT;
1387
1388 p = get_proc_task(inode);
1389 if (!p)
1390 return -ESRCH;
1391
1392 if (same_thread_group(current, p))
1393 set_task_comm(p, buffer);
1394 else
1395 count = -EINVAL;
1396
1397 put_task_struct(p);
1398
1399 return count;
1400}
1401
1402static int comm_show(struct seq_file *m, void *v)
1403{
1404 struct inode *inode = m->private;
1405 struct task_struct *p;
1406
1407 p = get_proc_task(inode);
1408 if (!p)
1409 return -ESRCH;
1410
1411 task_lock(p);
1412 seq_printf(m, "%s\n", p->comm);
1413 task_unlock(p);
1414
1415 put_task_struct(p);
1416
1417 return 0;
1418}
1419
1420static int comm_open(struct inode *inode, struct file *filp)
1421{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001422 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001423}
1424
1425static const struct file_operations proc_pid_set_comm_operations = {
1426 .open = comm_open,
1427 .read = seq_read,
1428 .write = comm_write,
1429 .llseek = seq_lseek,
1430 .release = single_release,
1431};
1432
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001433static int proc_exe_link(struct dentry *dentry, struct path *exe_path)
Matt Helsley925d1c42008-04-29 01:01:36 -07001434{
1435 struct task_struct *task;
1436 struct mm_struct *mm;
1437 struct file *exe_file;
1438
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001439 task = get_proc_task(dentry->d_inode);
Matt Helsley925d1c42008-04-29 01:01:36 -07001440 if (!task)
1441 return -ENOENT;
1442 mm = get_task_mm(task);
1443 put_task_struct(task);
1444 if (!mm)
1445 return -ENOENT;
1446 exe_file = get_mm_exe_file(mm);
1447 mmput(mm);
1448 if (exe_file) {
1449 *exe_path = exe_file->f_path;
1450 path_get(&exe_file->f_path);
1451 fput(exe_file);
1452 return 0;
1453 } else
1454 return -ENOENT;
1455}
1456
Al Viro008b1502005-08-20 00:17:39 +01001457static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 struct inode *inode = dentry->d_inode;
Christoph Hellwig408ef012012-06-18 10:47:03 -04001460 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 int error = -EACCES;
1462
Eric W. Biederman778c1142006-06-26 00:25:58 -07001463 /* Are we allowed to snoop on the tasks file descriptors? */
1464 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Christoph Hellwig408ef012012-06-18 10:47:03 -04001467 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
1468 if (error)
1469 goto out;
1470
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001471 nd_jump_link(nd, &path);
Christoph Hellwig408ef012012-06-18 10:47:03 -04001472 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473out:
Al Viro008b1502005-08-20 00:17:39 +01001474 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001477static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Mel Gormane12ba742007-10-16 01:25:52 -07001479 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001480 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 int len;
1482
1483 if (!tmp)
1484 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001485
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001486 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001487 len = PTR_ERR(pathname);
1488 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001490 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 if (len > buflen)
1493 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001494 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 len = -EFAULT;
1496 out:
1497 free_page((unsigned long)tmp);
1498 return len;
1499}
1500
1501static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1502{
1503 int error = -EACCES;
1504 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001505 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Eric W. Biederman778c1142006-06-26 00:25:58 -07001507 /* Are we allowed to snoop on the tasks file descriptors? */
1508 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Cyrill Gorcunov7773fbc2012-01-10 15:11:20 -08001511 error = PROC_I(inode)->op.proc_get_link(dentry, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (error)
1513 goto out;
1514
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001515 error = do_proc_readlink(&path, buffer, buflen);
1516 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return error;
1519}
1520
Cyrill Gorcunovfaf60af2012-08-23 14:43:24 +04001521const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001523 .follow_link = proc_pid_follow_link,
1524 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525};
1526
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001527
1528/* building an inode */
1529
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001530struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001531{
1532 struct inode * inode;
1533 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001534 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001535
1536 /* We need a new inode */
1537
1538 inode = new_inode(sb);
1539 if (!inode)
1540 goto out;
1541
1542 /* Common stuff */
1543 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001544 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001545 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001546 inode->i_op = &proc_def_inode_operations;
1547
1548 /*
1549 * grab the reference to task.
1550 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001551 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001552 if (!ei->pid)
1553 goto out_unlock;
1554
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001555 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001556 rcu_read_lock();
1557 cred = __task_cred(task);
1558 inode->i_uid = cred->euid;
1559 inode->i_gid = cred->egid;
1560 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001561 }
1562 security_task_to_inode(task, inode);
1563
1564out:
1565 return inode;
1566
1567out_unlock:
1568 iput(inode);
1569 return NULL;
1570}
1571
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001572int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001573{
1574 struct inode *inode = dentry->d_inode;
1575 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001576 const struct cred *cred;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001577 struct pid_namespace *pid = dentry->d_sb->s_fs_info;
David Howellsc69e8d92008-11-14 10:39:19 +11001578
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001579 generic_fillattr(inode, stat);
1580
1581 rcu_read_lock();
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001582 stat->uid = GLOBAL_ROOT_UID;
1583 stat->gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001584 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1585 if (task) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08001586 if (!has_pid_permissions(pid, task, 2)) {
1587 rcu_read_unlock();
1588 /*
1589 * This doesn't prevent learning whether PID exists,
1590 * it only makes getattr() consistent with readdir().
1591 */
1592 return -ENOENT;
1593 }
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001594 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1595 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001596 cred = __task_cred(task);
1597 stat->uid = cred->euid;
1598 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001599 }
1600 }
1601 rcu_read_unlock();
1602 return 0;
1603}
1604
1605/* dentry stuff */
1606
1607/*
1608 * Exceptional case: normally we are not allowed to unhash a busy
1609 * directory. In this case, however, we can do it - no aliasing problems
1610 * due to the way we treat inodes.
1611 *
1612 * Rewrite the inode's ownerships here because the owning task may have
1613 * performed a setuid(), etc.
1614 *
1615 * Before the /proc/pid/status file was created the only way to read
1616 * the effective uid of a /process was to stat /proc/pid. Reading
1617 * /proc/pid/status is slow enough that procps and other packages
1618 * kept stating /proc/pid. To keep the rules in /proc simple I have
1619 * made this apply to all per process world readable and executable
1620 * directories.
1621 */
Al Viro0b728e12012-06-10 16:03:43 -04001622int pid_revalidate(struct dentry *dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001623{
Nick Piggin34286d62011-01-07 17:49:57 +11001624 struct inode *inode;
1625 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001626 const struct cred *cred;
1627
Al Viro0b728e12012-06-10 16:03:43 -04001628 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +11001629 return -ECHILD;
1630
1631 inode = dentry->d_inode;
1632 task = get_proc_task(inode);
1633
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001634 if (task) {
1635 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1636 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001637 rcu_read_lock();
1638 cred = __task_cred(task);
1639 inode->i_uid = cred->euid;
1640 inode->i_gid = cred->egid;
1641 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001642 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001643 inode->i_uid = GLOBAL_ROOT_UID;
1644 inode->i_gid = GLOBAL_ROOT_GID;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001645 }
1646 inode->i_mode &= ~(S_ISUID | S_ISGID);
1647 security_task_to_inode(task, inode);
1648 put_task_struct(task);
1649 return 1;
1650 }
1651 d_drop(dentry);
1652 return 0;
1653}
1654
David Howells1dd704b2013-04-12 01:08:50 +01001655int pid_delete_dentry(const struct dentry *dentry)
1656{
1657 /* Is the task we represent dead?
1658 * If so, then don't put the dentry on the lru list,
1659 * kill it immediately.
1660 */
1661 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1662}
1663
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001664const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001665{
1666 .d_revalidate = pid_revalidate,
1667 .d_delete = pid_delete_dentry,
1668};
1669
1670/* Lookups */
1671
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001672/*
1673 * Fill a directory entry.
1674 *
1675 * If possible create the dcache entry and derive our inode number and
1676 * file type from dcache entry.
1677 *
1678 * Since all of the proc inode numbers are dynamically generated, the inode
1679 * numbers do not exist until the inode is cache. This means creating the
1680 * the dcache entry in readdir is necessary to keep the inode numbers
1681 * reported by readdir in sync with the inode numbers reported
1682 * by stat.
1683 */
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08001684int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1685 const char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001686 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001687{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001688 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001689 struct inode *inode;
1690 struct qstr qname;
1691 ino_t ino = 0;
1692 unsigned type = DT_UNKNOWN;
1693
1694 qname.name = name;
1695 qname.len = len;
1696 qname.hash = full_name_hash(name, len);
1697
1698 child = d_lookup(dir, &qname);
1699 if (!child) {
1700 struct dentry *new;
1701 new = d_alloc(dir, &qname);
1702 if (new) {
1703 child = instantiate(dir->d_inode, new, task, ptr);
1704 if (child)
1705 dput(new);
1706 else
1707 child = new;
1708 }
1709 }
1710 if (!child || IS_ERR(child) || !child->d_inode)
1711 goto end_instantiate;
1712 inode = child->d_inode;
1713 if (inode) {
1714 ino = inode->i_ino;
1715 type = inode->i_mode >> 12;
1716 }
1717 dput(child);
1718end_instantiate:
1719 if (!ino)
1720 ino = find_inode_number(dir, &qname);
1721 if (!ino)
1722 ino = 1;
1723 return filldir(dirent, name, len, filp->f_pos, ino, type);
1724}
1725
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001726#ifdef CONFIG_CHECKPOINT_RESTORE
1727
1728/*
1729 * dname_to_vma_addr - maps a dentry name into two unsigned longs
1730 * which represent vma start and end addresses.
1731 */
1732static int dname_to_vma_addr(struct dentry *dentry,
1733 unsigned long *start, unsigned long *end)
1734{
1735 if (sscanf(dentry->d_name.name, "%lx-%lx", start, end) != 2)
1736 return -EINVAL;
1737
1738 return 0;
1739}
1740
Al Viro0b728e12012-06-10 16:03:43 -04001741static int map_files_d_revalidate(struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001742{
1743 unsigned long vm_start, vm_end;
1744 bool exact_vma_exists = false;
1745 struct mm_struct *mm = NULL;
1746 struct task_struct *task;
1747 const struct cred *cred;
1748 struct inode *inode;
1749 int status = 0;
1750
Al Viro0b728e12012-06-10 16:03:43 -04001751 if (flags & LOOKUP_RCU)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001752 return -ECHILD;
1753
1754 if (!capable(CAP_SYS_ADMIN)) {
Zhao Hongjiang41735812013-02-20 13:13:55 +11001755 status = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001756 goto out_notask;
1757 }
1758
1759 inode = dentry->d_inode;
1760 task = get_proc_task(inode);
1761 if (!task)
1762 goto out_notask;
1763
Cong Wang2344bec2012-05-31 16:26:18 -07001764 mm = mm_access(task, PTRACE_MODE_READ);
1765 if (IS_ERR_OR_NULL(mm))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001766 goto out;
1767
1768 if (!dname_to_vma_addr(dentry, &vm_start, &vm_end)) {
1769 down_read(&mm->mmap_sem);
1770 exact_vma_exists = !!find_exact_vma(mm, vm_start, vm_end);
1771 up_read(&mm->mmap_sem);
1772 }
1773
1774 mmput(mm);
1775
1776 if (exact_vma_exists) {
1777 if (task_dumpable(task)) {
1778 rcu_read_lock();
1779 cred = __task_cred(task);
1780 inode->i_uid = cred->euid;
1781 inode->i_gid = cred->egid;
1782 rcu_read_unlock();
1783 } else {
Eric W. Biedermandcb0f222012-02-09 08:48:21 -08001784 inode->i_uid = GLOBAL_ROOT_UID;
1785 inode->i_gid = GLOBAL_ROOT_GID;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001786 }
1787 security_task_to_inode(task, inode);
1788 status = 1;
1789 }
1790
1791out:
1792 put_task_struct(task);
1793
1794out_notask:
1795 if (status <= 0)
1796 d_drop(dentry);
1797
1798 return status;
1799}
1800
1801static const struct dentry_operations tid_map_files_dentry_operations = {
1802 .d_revalidate = map_files_d_revalidate,
1803 .d_delete = pid_delete_dentry,
1804};
1805
1806static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
1807{
1808 unsigned long vm_start, vm_end;
1809 struct vm_area_struct *vma;
1810 struct task_struct *task;
1811 struct mm_struct *mm;
1812 int rc;
1813
1814 rc = -ENOENT;
1815 task = get_proc_task(dentry->d_inode);
1816 if (!task)
1817 goto out;
1818
1819 mm = get_task_mm(task);
1820 put_task_struct(task);
1821 if (!mm)
1822 goto out;
1823
1824 rc = dname_to_vma_addr(dentry, &vm_start, &vm_end);
1825 if (rc)
1826 goto out_mmput;
1827
1828 down_read(&mm->mmap_sem);
1829 vma = find_exact_vma(mm, vm_start, vm_end);
1830 if (vma && vma->vm_file) {
1831 *path = vma->vm_file->f_path;
1832 path_get(path);
1833 rc = 0;
1834 }
1835 up_read(&mm->mmap_sem);
1836
1837out_mmput:
1838 mmput(mm);
1839out:
1840 return rc;
1841}
1842
1843struct map_files_info {
Al Viro7b540d02012-08-27 14:55:26 -04001844 fmode_t mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001845 unsigned long len;
1846 unsigned char name[4*sizeof(long)+2]; /* max: %lx-%lx\0 */
1847};
1848
1849static struct dentry *
1850proc_map_files_instantiate(struct inode *dir, struct dentry *dentry,
1851 struct task_struct *task, const void *ptr)
1852{
Al Viro7b540d02012-08-27 14:55:26 -04001853 fmode_t mode = (fmode_t)(unsigned long)ptr;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001854 struct proc_inode *ei;
1855 struct inode *inode;
1856
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001857 inode = proc_pid_make_inode(dir->i_sb, task);
1858 if (!inode)
1859 return ERR_PTR(-ENOENT);
1860
1861 ei = PROC_I(inode);
1862 ei->op.proc_get_link = proc_map_files_get_link;
1863
1864 inode->i_op = &proc_pid_link_inode_operations;
1865 inode->i_size = 64;
1866 inode->i_mode = S_IFLNK;
1867
Al Viro7b540d02012-08-27 14:55:26 -04001868 if (mode & FMODE_READ)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001869 inode->i_mode |= S_IRUSR;
Al Viro7b540d02012-08-27 14:55:26 -04001870 if (mode & FMODE_WRITE)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001871 inode->i_mode |= S_IWUSR;
1872
1873 d_set_d_op(dentry, &tid_map_files_dentry_operations);
1874 d_add(dentry, inode);
1875
1876 return NULL;
1877}
1878
1879static struct dentry *proc_map_files_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04001880 struct dentry *dentry, unsigned int flags)
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001881{
1882 unsigned long vm_start, vm_end;
1883 struct vm_area_struct *vma;
1884 struct task_struct *task;
1885 struct dentry *result;
1886 struct mm_struct *mm;
1887
Zhao Hongjiang41735812013-02-20 13:13:55 +11001888 result = ERR_PTR(-EPERM);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001889 if (!capable(CAP_SYS_ADMIN))
1890 goto out;
1891
1892 result = ERR_PTR(-ENOENT);
1893 task = get_proc_task(dir);
1894 if (!task)
1895 goto out;
1896
1897 result = ERR_PTR(-EACCES);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001898 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001899 goto out_put_task;
1900
1901 result = ERR_PTR(-ENOENT);
1902 if (dname_to_vma_addr(dentry, &vm_start, &vm_end))
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001903 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001904
1905 mm = get_task_mm(task);
1906 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001907 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001908
1909 down_read(&mm->mmap_sem);
1910 vma = find_exact_vma(mm, vm_start, vm_end);
1911 if (!vma)
1912 goto out_no_vma;
1913
Stanislav Kinsbursky05f56482012-11-26 16:29:42 -08001914 if (vma->vm_file)
1915 result = proc_map_files_instantiate(dir, dentry, task,
1916 (void *)(unsigned long)vma->vm_file->f_mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001917
1918out_no_vma:
1919 up_read(&mm->mmap_sem);
1920 mmput(mm);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001921out_put_task:
1922 put_task_struct(task);
1923out:
1924 return result;
1925}
1926
1927static const struct inode_operations proc_map_files_inode_operations = {
1928 .lookup = proc_map_files_lookup,
1929 .permission = proc_fd_permission,
1930 .setattr = proc_setattr,
1931};
1932
1933static int
1934proc_map_files_readdir(struct file *filp, void *dirent, filldir_t filldir)
1935{
1936 struct dentry *dentry = filp->f_path.dentry;
1937 struct inode *inode = dentry->d_inode;
1938 struct vm_area_struct *vma;
1939 struct task_struct *task;
1940 struct mm_struct *mm;
1941 ino_t ino;
1942 int ret;
1943
Zhao Hongjiang41735812013-02-20 13:13:55 +11001944 ret = -EPERM;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001945 if (!capable(CAP_SYS_ADMIN))
1946 goto out;
1947
1948 ret = -ENOENT;
1949 task = get_proc_task(inode);
1950 if (!task)
1951 goto out;
1952
1953 ret = -EACCES;
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001954 if (!ptrace_may_access(task, PTRACE_MODE_READ))
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001955 goto out_put_task;
1956
1957 ret = 0;
1958 switch (filp->f_pos) {
1959 case 0:
1960 ino = inode->i_ino;
1961 if (filldir(dirent, ".", 1, 0, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001962 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001963 filp->f_pos++;
1964 case 1:
1965 ino = parent_ino(dentry);
1966 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001967 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001968 filp->f_pos++;
1969 default:
1970 {
1971 unsigned long nr_files, pos, i;
1972 struct flex_array *fa = NULL;
1973 struct map_files_info info;
1974 struct map_files_info *p;
1975
1976 mm = get_task_mm(task);
1977 if (!mm)
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07001978 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08001979 down_read(&mm->mmap_sem);
1980
1981 nr_files = 0;
1982
1983 /*
1984 * We need two passes here:
1985 *
1986 * 1) Collect vmas of mapped files with mmap_sem taken
1987 * 2) Release mmap_sem and instantiate entries
1988 *
1989 * otherwise we get lockdep complained, since filldir()
1990 * routine might require mmap_sem taken in might_fault().
1991 */
1992
1993 for (vma = mm->mmap, pos = 2; vma; vma = vma->vm_next) {
1994 if (vma->vm_file && ++pos > filp->f_pos)
1995 nr_files++;
1996 }
1997
1998 if (nr_files) {
1999 fa = flex_array_alloc(sizeof(info), nr_files,
2000 GFP_KERNEL);
2001 if (!fa || flex_array_prealloc(fa, 0, nr_files,
2002 GFP_KERNEL)) {
2003 ret = -ENOMEM;
2004 if (fa)
2005 flex_array_free(fa);
2006 up_read(&mm->mmap_sem);
2007 mmput(mm);
Cyrill Gorcunoveb94cd92012-05-17 17:03:25 -07002008 goto out_put_task;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002009 }
2010 for (i = 0, vma = mm->mmap, pos = 2; vma;
2011 vma = vma->vm_next) {
2012 if (!vma->vm_file)
2013 continue;
2014 if (++pos <= filp->f_pos)
2015 continue;
2016
Al Viro7b540d02012-08-27 14:55:26 -04002017 info.mode = vma->vm_file->f_mode;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002018 info.len = snprintf(info.name,
2019 sizeof(info.name), "%lx-%lx",
2020 vma->vm_start, vma->vm_end);
2021 if (flex_array_put(fa, i++, &info, GFP_KERNEL))
2022 BUG();
2023 }
2024 }
2025 up_read(&mm->mmap_sem);
2026
2027 for (i = 0; i < nr_files; i++) {
2028 p = flex_array_get(fa, i);
2029 ret = proc_fill_cache(filp, dirent, filldir,
2030 p->name, p->len,
2031 proc_map_files_instantiate,
Al Viro7b540d02012-08-27 14:55:26 -04002032 task,
2033 (void *)(unsigned long)p->mode);
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002034 if (ret)
2035 break;
2036 filp->f_pos++;
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002037 }
2038 if (fa)
2039 flex_array_free(fa);
2040 mmput(mm);
2041 }
2042 }
2043
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002044out_put_task:
2045 put_task_struct(task);
2046out:
2047 return ret;
2048}
2049
2050static const struct file_operations proc_map_files_operations = {
2051 .read = generic_read_dir,
2052 .readdir = proc_map_files_readdir,
2053 .llseek = default_llseek,
2054};
2055
2056#endif /* CONFIG_CHECKPOINT_RESTORE */
2057
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002058static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002059 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002060{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002061 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002062 struct inode *inode;
2063 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002064 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002065
Eric W. Biederman61a28782006-10-02 02:18:49 -07002066 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002067 if (!inode)
2068 goto out;
2069
2070 ei = PROC_I(inode);
2071 inode->i_mode = p->mode;
2072 if (S_ISDIR(inode->i_mode))
Miklos Szeredibfe86842011-10-28 14:13:29 +02002073 set_nlink(inode, 2); /* Use getattr to fix if necessary */
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002074 if (p->iop)
2075 inode->i_op = p->iop;
2076 if (p->fop)
2077 inode->i_fop = p->fop;
2078 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002079 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002080 d_add(dentry, inode);
2081 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002082 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002083 error = NULL;
2084out:
2085 return error;
2086}
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static struct dentry *proc_pident_lookup(struct inode *dir,
2089 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002090 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002091 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002093 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002094 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002095 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002097 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Eric W. Biederman99f89552006-06-26 00:25:55 -07002099 if (!task)
2100 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002102 /*
2103 * Yes, it does not scale. And it should not. Don't add
2104 * new entries into /proc/<tgid>/ without very good reasons.
2105 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002106 last = &ents[nents - 1];
2107 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (p->len != dentry->d_name.len)
2109 continue;
2110 if (!memcmp(dentry->d_name.name, p->name, p->len))
2111 break;
2112 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002113 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 goto out;
2115
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002116 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002118 put_task_struct(task);
2119out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002120 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Eric Dumazetc5141e62007-05-08 00:26:15 -07002123static int proc_pident_fill_cache(struct file *filp, void *dirent,
2124 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002125{
2126 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2127 proc_pident_instantiate, task, p);
2128}
2129
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002130static int proc_pident_readdir(struct file *filp,
2131 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002132 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002133{
2134 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002135 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002136 struct inode *inode = dentry->d_inode;
2137 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002138 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002139 ino_t ino;
2140 int ret;
2141
2142 ret = -ENOENT;
2143 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002144 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002145
2146 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002147 i = filp->f_pos;
2148 switch (i) {
2149 case 0:
2150 ino = inode->i_ino;
2151 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2152 goto out;
2153 i++;
2154 filp->f_pos++;
2155 /* fall through */
2156 case 1:
2157 ino = parent_ino(dentry);
2158 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2159 goto out;
2160 i++;
2161 filp->f_pos++;
2162 /* fall through */
2163 default:
2164 i -= 2;
2165 if (i >= nents) {
2166 ret = 1;
2167 goto out;
2168 }
2169 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002170 last = &ents[nents - 1];
2171 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002172 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002173 goto out;
2174 filp->f_pos++;
2175 p++;
2176 }
2177 }
2178
2179 ret = 1;
2180out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002181 put_task_struct(task);
2182out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002183 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002187static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2188 size_t count, loff_t *ppos)
2189{
Al Viro496ad9a2013-01-23 17:07:38 -05002190 struct inode * inode = file_inode(file);
Al Viro04ff9702007-03-12 16:17:58 +00002191 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002192 ssize_t length;
2193 struct task_struct *task = get_proc_task(inode);
2194
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002195 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002196 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002197
2198 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002199 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002200 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002201 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002202 if (length > 0)
2203 length = simple_read_from_buffer(buf, count, ppos, p, length);
2204 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205 return length;
2206}
2207
2208static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2209 size_t count, loff_t *ppos)
2210{
Al Viro496ad9a2013-01-23 17:07:38 -05002211 struct inode * inode = file_inode(file);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002212 char *page;
2213 ssize_t length;
2214 struct task_struct *task = get_proc_task(inode);
2215
2216 length = -ESRCH;
2217 if (!task)
2218 goto out_no_task;
2219 if (count > PAGE_SIZE)
2220 count = PAGE_SIZE;
2221
2222 /* No partial writes. */
2223 length = -EINVAL;
2224 if (*ppos != 0)
2225 goto out;
2226
2227 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002228 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002229 if (!page)
2230 goto out;
2231
2232 length = -EFAULT;
2233 if (copy_from_user(page, buf, count))
2234 goto out_free;
2235
David Howells107db7c2009-05-08 13:55:27 +01002236 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002237 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002238 if (length < 0)
2239 goto out_free;
2240
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002241 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002242 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002243 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002244 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002245out_free:
2246 free_page((unsigned long) page);
2247out:
2248 put_task_struct(task);
2249out_no_task:
2250 return length;
2251}
2252
Arjan van de Ven00977a52007-02-12 00:55:34 -08002253static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002254 .read = proc_pid_attr_read,
2255 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002256 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002257};
2258
Eric Dumazetc5141e62007-05-08 00:26:15 -07002259static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002260 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2261 REG("prev", S_IRUGO, proc_pid_attr_operations),
2262 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2263 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2264 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2265 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002266};
2267
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002268static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 void * dirent, filldir_t filldir)
2270{
2271 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002272 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274
Arjan van de Ven00977a52007-02-12 00:55:34 -08002275static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002277 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002278 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279};
2280
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002281static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Al Viro00cd8dd2012-06-10 17:13:09 -04002282 struct dentry *dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002284 return proc_pident_lookup(dir, dentry,
2285 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002288static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002289 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002290 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002291 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292};
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294#endif
2295
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002296#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002297static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2298 size_t count, loff_t *ppos)
2299{
Al Viro496ad9a2013-01-23 17:07:38 -05002300 struct task_struct *task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002301 struct mm_struct *mm;
2302 char buffer[PROC_NUMBUF];
2303 size_t len;
2304 int ret;
2305
2306 if (!task)
2307 return -ESRCH;
2308
2309 ret = 0;
2310 mm = get_task_mm(task);
2311 if (mm) {
2312 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2313 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2314 MMF_DUMP_FILTER_SHIFT));
2315 mmput(mm);
2316 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2317 }
2318
2319 put_task_struct(task);
2320
2321 return ret;
2322}
2323
2324static ssize_t proc_coredump_filter_write(struct file *file,
2325 const char __user *buf,
2326 size_t count,
2327 loff_t *ppos)
2328{
2329 struct task_struct *task;
2330 struct mm_struct *mm;
2331 char buffer[PROC_NUMBUF], *end;
2332 unsigned int val;
2333 int ret;
2334 int i;
2335 unsigned long mask;
2336
2337 ret = -EFAULT;
2338 memset(buffer, 0, sizeof(buffer));
2339 if (count > sizeof(buffer) - 1)
2340 count = sizeof(buffer) - 1;
2341 if (copy_from_user(buffer, buf, count))
2342 goto out_no_task;
2343
2344 ret = -EINVAL;
2345 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2346 if (*end == '\n')
2347 end++;
2348 if (end - buffer == 0)
2349 goto out_no_task;
2350
2351 ret = -ESRCH;
Al Viro496ad9a2013-01-23 17:07:38 -05002352 task = get_proc_task(file_inode(file));
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002353 if (!task)
2354 goto out_no_task;
2355
2356 ret = end - buffer;
2357 mm = get_task_mm(task);
2358 if (!mm)
2359 goto out_no_mm;
2360
2361 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2362 if (val & mask)
2363 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2364 else
2365 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2366 }
2367
2368 mmput(mm);
2369 out_no_mm:
2370 put_task_struct(task);
2371 out_no_task:
2372 return ret;
2373}
2374
2375static const struct file_operations proc_coredump_filter_operations = {
2376 .read = proc_coredump_filter_read,
2377 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002378 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002379};
2380#endif
2381
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002382#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002383static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002384{
Andrea Righi940389b2008-07-28 00:48:12 +02002385 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002386 unsigned long flags;
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002387 int result;
Andrea Righi297c5d92008-07-25 01:48:49 -07002388
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002389 result = mutex_lock_killable(&task->signal->cred_guard_mutex);
2390 if (result)
2391 return result;
2392
2393 if (!ptrace_may_access(task, PTRACE_MODE_READ)) {
2394 result = -EACCES;
2395 goto out_unlock;
2396 }
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002397
Andrea Righi59954772008-07-27 17:29:15 +02002398 if (whole && lock_task_sighand(task, &flags)) {
2399 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002400
Andrea Righi59954772008-07-27 17:29:15 +02002401 task_io_accounting_add(&acct, &task->signal->ioac);
2402 while_each_thread(task, t)
2403 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002404
Andrea Righi59954772008-07-27 17:29:15 +02002405 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002406 }
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002407 result = sprintf(buffer,
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002408 "rchar: %llu\n"
2409 "wchar: %llu\n"
2410 "syscr: %llu\n"
2411 "syscw: %llu\n"
2412 "read_bytes: %llu\n"
2413 "write_bytes: %llu\n"
2414 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002415 (unsigned long long)acct.rchar,
2416 (unsigned long long)acct.wchar,
2417 (unsigned long long)acct.syscr,
2418 (unsigned long long)acct.syscw,
2419 (unsigned long long)acct.read_bytes,
2420 (unsigned long long)acct.write_bytes,
2421 (unsigned long long)acct.cancelled_write_bytes);
Vasiliy Kulikov293eb1e2011-07-26 16:08:38 -07002422out_unlock:
2423 mutex_unlock(&task->signal->cred_guard_mutex);
2424 return result;
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002425}
Andrea Righi297c5d92008-07-25 01:48:49 -07002426
2427static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2428{
2429 return do_io_accounting(task, buffer, 0);
2430}
2431
2432static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2433{
2434 return do_io_accounting(task, buffer, 1);
2435}
2436#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002437
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002438#ifdef CONFIG_USER_NS
2439static int proc_id_map_open(struct inode *inode, struct file *file,
2440 struct seq_operations *seq_ops)
2441{
2442 struct user_namespace *ns = NULL;
2443 struct task_struct *task;
2444 struct seq_file *seq;
2445 int ret = -EINVAL;
2446
2447 task = get_proc_task(inode);
2448 if (task) {
2449 rcu_read_lock();
2450 ns = get_user_ns(task_cred_xxx(task, user_ns));
2451 rcu_read_unlock();
2452 put_task_struct(task);
2453 }
2454 if (!ns)
2455 goto err;
2456
2457 ret = seq_open(file, seq_ops);
2458 if (ret)
2459 goto err_put_ns;
2460
2461 seq = file->private_data;
2462 seq->private = ns;
2463
2464 return 0;
2465err_put_ns:
2466 put_user_ns(ns);
2467err:
2468 return ret;
2469}
2470
2471static int proc_id_map_release(struct inode *inode, struct file *file)
2472{
2473 struct seq_file *seq = file->private_data;
2474 struct user_namespace *ns = seq->private;
2475 put_user_ns(ns);
2476 return seq_release(inode, file);
2477}
2478
2479static int proc_uid_map_open(struct inode *inode, struct file *file)
2480{
2481 return proc_id_map_open(inode, file, &proc_uid_seq_operations);
2482}
2483
2484static int proc_gid_map_open(struct inode *inode, struct file *file)
2485{
2486 return proc_id_map_open(inode, file, &proc_gid_seq_operations);
2487}
2488
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002489static int proc_projid_map_open(struct inode *inode, struct file *file)
2490{
2491 return proc_id_map_open(inode, file, &proc_projid_seq_operations);
2492}
2493
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002494static const struct file_operations proc_uid_map_operations = {
2495 .open = proc_uid_map_open,
2496 .write = proc_uid_map_write,
2497 .read = seq_read,
2498 .llseek = seq_lseek,
2499 .release = proc_id_map_release,
2500};
2501
2502static const struct file_operations proc_gid_map_operations = {
2503 .open = proc_gid_map_open,
2504 .write = proc_gid_map_write,
2505 .read = seq_read,
2506 .llseek = seq_lseek,
2507 .release = proc_id_map_release,
2508};
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002509
2510static const struct file_operations proc_projid_map_operations = {
2511 .open = proc_projid_map_open,
2512 .write = proc_projid_map_write,
2513 .read = seq_read,
2514 .llseek = seq_lseek,
2515 .release = proc_id_map_release,
2516};
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002517#endif /* CONFIG_USER_NS */
2518
Kees Cook47830722008-10-06 03:11:58 +04002519static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2520 struct pid *pid, struct task_struct *task)
2521{
Al Viroa9712bc2011-03-23 15:52:50 -04002522 int err = lock_trace(task);
2523 if (!err) {
2524 seq_printf(m, "%08x\n", task->personality);
2525 unlock_trace(task);
2526 }
2527 return err;
Kees Cook47830722008-10-06 03:11:58 +04002528}
2529
Eric W. Biederman801199c2006-10-02 02:18:48 -07002530/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531 * Thread groups
2532 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002533static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002534static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002535
Eric Dumazetc5141e62007-05-08 00:26:15 -07002536static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002537 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2538 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Pavel Emelyanov640708a2012-01-10 15:11:23 -08002539#ifdef CONFIG_CHECKPOINT_RESTORE
2540 DIR("map_files", S_IRUSR|S_IXUSR, proc_map_files_inode_operations, proc_map_files_operations),
2541#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002542 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002543 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002544#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002545 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002546#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002547 REG("environ", S_IRUSR, proc_environ_operations),
2548 INF("auxv", S_IRUSR, proc_pid_auxv),
2549 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002550 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002551 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002552#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002553 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002554#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002555#ifdef CONFIG_SCHED_AUTOGROUP
2556 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2557#endif
john stultz4614a696b2009-12-14 18:00:05 -08002558 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002559#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002560 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002561#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002562 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2563 ONE("stat", S_IRUGO, proc_tgid_stat),
2564 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002565 REG("maps", S_IRUGO, proc_pid_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002566#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002567 REG("numa_maps", S_IRUGO, proc_pid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002568#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002569 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2570 LNK("cwd", proc_cwd_link),
2571 LNK("root", proc_root_link),
2572 LNK("exe", proc_exe_link),
2573 REG("mounts", S_IRUGO, proc_mounts_operations),
2574 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2575 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002576#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002577 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002578 REG("smaps", S_IRUGO, proc_pid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002579 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002580#endif
2581#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002582 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002583#endif
2584#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002585 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002586#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002587#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002588 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002589#endif
2590#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002591 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002592#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002593#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002594 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002595#endif
Paul Menage8793d852007-10-18 23:39:39 -07002596#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002597 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002598#endif
Paul Menagea4243162007-10-18 23:39:35 -07002599#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002600 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002601#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002602 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002603 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002604 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002605#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002606 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2607 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002608#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002609#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002610 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002611#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002612#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002613 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002614#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002615#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002616 INF("io", S_IRUSR, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002617#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002618#ifdef CONFIG_HARDWALL
2619 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2620#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002621#ifdef CONFIG_USER_NS
2622 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2623 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002624 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002625#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002626};
2627
2628static int proc_tgid_base_readdir(struct file * filp,
2629 void * dirent, filldir_t filldir)
2630{
2631 return proc_pident_readdir(filp,dirent,filldir,
2632 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2633}
2634
Arjan van de Ven00977a52007-02-12 00:55:34 -08002635static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002636 .read = generic_read_dir,
2637 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002638 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002639};
2640
Al Viro00cd8dd2012-06-10 17:13:09 -04002641static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2642{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002643 return proc_pident_lookup(dir, dentry,
2644 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002645}
2646
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002647static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648 .lookup = proc_tgid_base_lookup,
2649 .getattr = pid_getattr,
2650 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002651 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002652};
2653
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002654static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002656 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002657 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002658 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Eric W. Biederman48e64842006-06-26 00:25:48 -07002660 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002661 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Al Viro4f522a22013-02-11 23:20:37 -05002662 /* no ->d_hash() rejects on procfs */
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002663 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002664 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002665 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002666 d_drop(dentry);
2667 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Eric W. Biederman48e64842006-06-26 00:25:48 -07002670 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002671 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2672 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002673 if (!leader)
2674 goto out;
2675
2676 name.name = "task";
2677 name.len = strlen(name.name);
2678 dir = d_hash_and_lookup(leader, &name);
2679 if (!dir)
2680 goto out_put_leader;
2681
2682 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002683 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002684 dentry = d_hash_and_lookup(dir, &name);
2685 if (dentry) {
2686 shrink_dcache_parent(dentry);
2687 d_drop(dentry);
2688 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002690
2691 dput(dir);
2692out_put_leader:
2693 dput(leader);
2694out:
2695 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
Randy Dunlap0895e912007-10-21 21:00:10 -07002698/**
2699 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2700 * @task: task that should be flushed.
2701 *
2702 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002703 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002704 * in. This call is supposed to do all of this job.
2705 *
2706 * Looks in the dcache for
2707 * /proc/@pid
2708 * /proc/@tgid/task/@pid
2709 * if either directory is present flushes it and all of it'ts children
2710 * from the dcache.
2711 *
2712 * It is safe and reasonable to cache /proc entries for a task until
2713 * that task exits. After that they just clog up the dcache with
2714 * useless entries, possibly causing useful dcache entries to be
2715 * flushed instead. This routine is proved to flush those useless
2716 * dcache entries at process exit time.
2717 *
2718 * NOTE: This routine is just an optimization so it does not guarantee
2719 * that no dcache entries will exist at process exit time it
2720 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002721 */
2722
2723void proc_flush_task(struct task_struct *task)
2724{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002725 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002726 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002727 struct upid *upid;
2728
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002729 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002730 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002731
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002732 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002733 upid = &pid->numbers[i];
2734 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002735 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002736 }
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002737}
2738
Adrian Bunk9711ef92006-12-06 20:38:31 -08002739static struct dentry *proc_pid_instantiate(struct inode *dir,
2740 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002741 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002742{
2743 struct dentry *error = ERR_PTR(-ENOENT);
2744 struct inode *inode;
2745
Eric W. Biederman61a28782006-10-02 02:18:49 -07002746 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002747 if (!inode)
2748 goto out;
2749
2750 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2751 inode->i_op = &proc_tgid_base_inode_operations;
2752 inode->i_fop = &proc_tgid_base_operations;
2753 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002754
Miklos Szeredibfe86842011-10-28 14:13:29 +02002755 set_nlink(inode, 2 + pid_entry_count_dirs(tgid_base_stuff,
2756 ARRAY_SIZE(tgid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002757
Nick Pigginfb045ad2011-01-07 17:49:55 +11002758 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002759
2760 d_add(dentry, inode);
2761 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04002762 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002763 error = NULL;
2764out:
2765 return error;
2766}
2767
Al Viro00cd8dd2012-06-10 17:13:09 -04002768struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Eric W. Biedermane656d8a2010-07-10 14:52:49 -07002770 struct dentry *result = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002773 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 tgid = name_to_int(dentry);
2776 if (tgid == ~0U)
2777 goto out;
2778
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002779 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002780 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002781 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (task)
2783 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002784 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (!task)
2786 goto out;
2787
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002788 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002789 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002791 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792}
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002795 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002796 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002798struct tgid_iter {
2799 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002800 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002801};
2802static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2803{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002804 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002806 if (iter.task)
2807 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002808 rcu_read_lock();
2809retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002810 iter.task = NULL;
2811 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002812 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002813 iter.tgid = pid_nr_ns(pid, ns);
2814 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002815 /* What we to know is if the pid we have find is the
2816 * pid of a thread_group_leader. Testing for task
2817 * being a thread_group_leader is the obvious thing
2818 * todo but there is a window when it fails, due to
2819 * the pid transfer logic in de_thread.
2820 *
2821 * So we perform the straight forward test of seeing
2822 * if the pid we have found is the pid of a thread
2823 * group leader, and don't worry if the task we have
2824 * found doesn't happen to be a thread group leader.
2825 * As we don't care in the case of readdir.
2826 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002827 if (!iter.task || !has_group_leader_pid(iter.task)) {
2828 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002829 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002830 }
2831 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002833 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002834 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
Al Viro021ada72013-03-29 19:27:05 -04002837#define TGID_OFFSET (FIRST_PROCESS_ENTRY + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Eric W. Biederman61a28782006-10-02 02:18:49 -07002839static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002840 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002841{
2842 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002843 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002844 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002845 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002846}
2847
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002848static int fake_filldir(void *buf, const char *name, int namelen,
2849 loff_t offset, u64 ino, unsigned d_type)
2850{
2851 return 0;
2852}
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854/* for the /proc/ directory itself, after non-process stuff has been done */
2855int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2856{
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002857 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002858 struct pid_namespace *ns;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002859 filldir_t __filldir;
Al Viro021ada72013-03-29 19:27:05 -04002860 loff_t pos = filp->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Al Viro021ada72013-03-29 19:27:05 -04002862 if (pos >= PID_MAX_LIMIT + TGID_OFFSET)
Eric W. Biedermanae06c7c2010-07-10 15:23:34 -07002863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Al Viro021ada72013-03-29 19:27:05 -04002865 if (pos == TGID_OFFSET - 1) {
2866 if (proc_fill_cache(filp, dirent, filldir, "self", 4,
2867 NULL, NULL, NULL) < 0)
2868 goto out;
2869 iter.tgid = 0;
2870 } else {
2871 iter.tgid = pos - TGID_OFFSET;
2872 }
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002873 iter.task = NULL;
Al Viro021ada72013-03-29 19:27:05 -04002874 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002875 for (iter = next_tgid(ns, iter);
2876 iter.task;
2877 iter.tgid += 1, iter = next_tgid(ns, iter)) {
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002878 if (has_pid_permissions(ns, iter.task, 2))
2879 __filldir = filldir;
2880 else
2881 __filldir = fake_filldir;
2882
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002883 filp->f_pos = iter.tgid + TGID_OFFSET;
Vasiliy Kulikov04996802012-01-10 15:11:31 -08002884 if (proc_pid_fill_cache(filp, dirent, __filldir, iter) < 0) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002885 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002886 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002889 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2890out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 return 0;
2892}
2893
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002894/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002895 * Tasks
2896 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002897static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002898 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002899 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Eric W. Biederman6b4e3062010-03-07 16:41:34 -08002900 DIR("ns", S_IRUSR|S_IXUGO, proc_ns_dir_inode_operations, proc_ns_dir_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002901 REG("environ", S_IRUSR, proc_environ_operations),
2902 INF("auxv", S_IRUSR, proc_pid_auxv),
2903 ONE("status", S_IRUGO, proc_pid_status),
Al Viroa9712bc2011-03-23 15:52:50 -04002904 ONE("personality", S_IRUGO, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002905 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002906#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002907 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002908#endif
john stultz4614a696b2009-12-14 18:00:05 -08002909 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002910#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Al Viroa9712bc2011-03-23 15:52:50 -04002911 INF("syscall", S_IRUGO, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002912#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002913 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2914 ONE("stat", S_IRUGO, proc_tid_stat),
2915 ONE("statm", S_IRUGO, proc_pid_statm),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002916 REG("maps", S_IRUGO, proc_tid_maps_operations),
Cyrill Gorcunov818411612012-05-31 16:26:43 -07002917#ifdef CONFIG_CHECKPOINT_RESTORE
2918 REG("children", S_IRUGO, proc_tid_children_operations),
2919#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002920#ifdef CONFIG_NUMA
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002921 REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002922#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002923 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2924 LNK("cwd", proc_cwd_link),
2925 LNK("root", proc_root_link),
2926 LNK("exe", proc_exe_link),
2927 REG("mounts", S_IRUGO, proc_mounts_operations),
2928 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002929#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002930 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
Siddhesh Poyarekarb7643752012-03-21 16:34:04 -07002931 REG("smaps", S_IRUGO, proc_tid_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002932 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933#endif
2934#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002935 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002936#endif
2937#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002938 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002939#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002940#ifdef CONFIG_STACKTRACE
Al Viroa9712bc2011-03-23 15:52:50 -04002941 ONE("stack", S_IRUGO, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002942#endif
2943#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002944 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002946#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002947 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002948#endif
Paul Menage8793d852007-10-18 23:39:39 -07002949#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002950 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951#endif
Paul Menagea4243162007-10-18 23:39:35 -07002952#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002953 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002954#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002955 INF("oom_score", S_IRUGO, proc_oom_score),
David Rientjesfa0cbbf2012-11-12 17:53:04 -08002956 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adj_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002957 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002958#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002959 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05002960 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002961#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002962#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002963 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002964#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002965#ifdef CONFIG_TASK_IO_ACCOUNTING
Vasiliy Kulikov1d1221f2011-06-24 16:08:38 +04002966 INF("io", S_IRUSR, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002967#endif
Chris Metcalff133ecc2011-05-26 12:40:09 -04002968#ifdef CONFIG_HARDWALL
2969 INF("hardwall", S_IRUGO, proc_pid_hardwall),
2970#endif
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002971#ifdef CONFIG_USER_NS
2972 REG("uid_map", S_IRUGO|S_IWUSR, proc_uid_map_operations),
2973 REG("gid_map", S_IRUGO|S_IWUSR, proc_gid_map_operations),
Eric W. Biedermanf76d2072012-08-30 01:24:05 -07002974 REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
Eric W. Biederman22d917d2011-11-17 00:11:58 -08002975#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002976};
2977
2978static int proc_tid_base_readdir(struct file * filp,
2979 void * dirent, filldir_t filldir)
2980{
2981 return proc_pident_readdir(filp,dirent,filldir,
2982 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2983}
2984
Al Viro00cd8dd2012-06-10 17:13:09 -04002985static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
2986{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002987 return proc_pident_lookup(dir, dentry,
2988 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002989}
2990
Arjan van de Ven00977a52007-02-12 00:55:34 -08002991static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002992 .read = generic_read_dir,
2993 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002994 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002995};
2996
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002997static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002998 .lookup = proc_tid_base_lookup,
2999 .getattr = pid_getattr,
3000 .setattr = proc_setattr,
3001};
3002
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003003static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003004 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003005{
3006 struct dentry *error = ERR_PTR(-ENOENT);
3007 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003008 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003009
3010 if (!inode)
3011 goto out;
3012 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3013 inode->i_op = &proc_tid_base_inode_operations;
3014 inode->i_fop = &proc_tid_base_operations;
3015 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003016
Miklos Szeredibfe86842011-10-28 14:13:29 +02003017 set_nlink(inode, 2 + pid_entry_count_dirs(tid_base_stuff,
3018 ARRAY_SIZE(tid_base_stuff)));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003019
Nick Pigginfb045ad2011-01-07 17:49:55 +11003020 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003021
3022 d_add(dentry, inode);
3023 /* Close the race of the process dying before we return the dentry */
Al Viro0b728e12012-06-10 16:03:43 -04003024 if (pid_revalidate(dentry, 0))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003025 error = NULL;
3026out:
3027 return error;
3028}
3029
Al Viro00cd8dd2012-06-10 17:13:09 -04003030static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031{
3032 struct dentry *result = ERR_PTR(-ENOENT);
3033 struct task_struct *task;
3034 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003035 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003036 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003037
3038 if (!leader)
3039 goto out_no_task;
3040
3041 tid = name_to_int(dentry);
3042 if (tid == ~0U)
3043 goto out;
3044
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003045 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003046 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003047 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003048 if (task)
3049 get_task_struct(task);
3050 rcu_read_unlock();
3051 if (!task)
3052 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003053 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003054 goto out_drop_task;
3055
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003056 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003057out_drop_task:
3058 put_task_struct(task);
3059out:
3060 put_task_struct(leader);
3061out_no_task:
3062 return result;
3063}
3064
3065/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003066 * Find the first tid of a thread group to return to user space.
3067 *
3068 * Usually this is just the thread group leader, but if the users
3069 * buffer was too small or there was a seek into the middle of the
3070 * directory we have more work todo.
3071 *
3072 * In the case of a short read we start with find_task_by_pid.
3073 *
3074 * In the case of a seek we start with the leader and walk nr
3075 * threads past it.
3076 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003077static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003078 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003079{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003080 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003081
Eric W. Biedermancc288732006-06-26 00:26:01 -07003082 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003083 /* Attempt to start with the pid of a thread */
3084 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003085 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003086 if (pos && (pos->group_leader == leader))
3087 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003088 }
3089
3090 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003091 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003092 if (nr && nr >= get_nr_threads(leader))
3093 goto out;
3094
3095 /* If we haven't found our starting place yet start
3096 * with the leader and walk nr threads forward.
3097 */
3098 for (pos = leader; nr > 0; --nr) {
3099 pos = next_thread(pos);
3100 if (pos == leader) {
3101 pos = NULL;
3102 goto out;
3103 }
3104 }
3105found:
3106 get_task_struct(pos);
3107out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003108 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003109 return pos;
3110}
3111
3112/*
3113 * Find the next thread in the thread list.
3114 * Return NULL if there is an error or no next thread.
3115 *
3116 * The reference to the input task_struct is released.
3117 */
3118static struct task_struct *next_tid(struct task_struct *start)
3119{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003120 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003121 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003122 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003123 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003124 if (thread_group_leader(pos))
3125 pos = NULL;
3126 else
3127 get_task_struct(pos);
3128 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003129 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003130 put_task_struct(start);
3131 return pos;
3132}
3133
Eric W. Biederman61a28782006-10-02 02:18:49 -07003134static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3135 struct task_struct *task, int tid)
3136{
3137 char name[PROC_NUMBUF];
3138 int len = snprintf(name, sizeof(name), "%d", tid);
3139 return proc_fill_cache(filp, dirent, filldir, name, len,
3140 proc_task_instantiate, task, NULL);
3141}
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143/* for the /proc/TGID/task/ directories */
3144static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3145{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003146 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003148 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003149 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 int retval = -ENOENT;
3151 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003152 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003153 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003155 task = get_proc_task(inode);
3156 if (!task)
3157 goto out_no_task;
3158 rcu_read_lock();
3159 if (pid_alive(task)) {
3160 leader = task->group_leader;
3161 get_task_struct(leader);
3162 }
3163 rcu_read_unlock();
3164 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003165 if (!leader)
3166 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 retval = 0;
3168
Linus Torvaldsee568b22009-03-17 10:02:35 -07003169 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 case 0:
3171 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003172 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003174 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 /* fall through */
3176 case 1:
3177 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003178 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003180 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 /* fall through */
3182 }
3183
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003184 /* f_version caches the tgid value that the last readdir call couldn't
3185 * return. lseek aka telldir automagically resets f_version to 0.
3186 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003187 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003188 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003189 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003190 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003191 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003192 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003193 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003194 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003195 /* returning this tgid failed, save it as the first
3196 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003197 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003198 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 }
3202out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003203 put_task_struct(leader);
3204out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 return retval;
3206}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003207
3208static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3209{
3210 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003211 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003212 generic_fillattr(inode, stat);
3213
Eric W. Biederman99f89552006-06-26 00:25:55 -07003214 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003215 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003216 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003217 }
3218
3219 return 0;
3220}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003221
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003222static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003223 .lookup = proc_task_lookup,
3224 .getattr = proc_task_getattr,
3225 .setattr = proc_setattr,
Vasiliy Kulikov04996802012-01-10 15:11:31 -08003226 .permission = proc_pid_permission,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003227};
3228
Arjan van de Ven00977a52007-02-12 00:55:34 -08003229static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003230 .read = generic_read_dir,
3231 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003232 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003233};