blob: e34c3c33b2de5e948461d4f76360d619de39e938 [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 Rientjesa63d83f42010-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>
Paul Menagea4243162007-10-18 23:39:35 -070076#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/cpuset.h>
78#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050079#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070080#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070081#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070082#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070083#include <linux/pid_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040084#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include "internal.h"
87
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070088/* NOTE:
89 * Implementing inode permission operations in /proc is almost
90 * certainly an error. Permission checks need to happen during
91 * each system call not at open time. The reason is that most of
92 * what we wish to check for permissions in /proc varies at runtime.
93 *
94 * The classic example of a problem is opening file descriptors
95 * in /proc for a task before it execs a suid executable.
96 */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700100 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800102 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800103 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Eric W. Biederman61a28782006-10-02 02:18:49 -0700107#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700108 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700110 .mode = MODE, \
111 .iop = IOP, \
112 .fop = FOP, \
113 .op = OP, \
114}
115
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300116#define DIR(NAME, MODE, iops, fops) \
117 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300121 { .proc_get_link = get_link } )
122#define REG(NAME, MODE, fops) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700125 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700126 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300127 { .proc_read = read } )
128#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300131 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Vegard Nossumaed54172008-06-05 22:46:53 -0700133/*
134 * Count the number of hardlinks for the pid_entry table, excluding the .
135 * and .. links.
136 */
137static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
138 unsigned int n)
139{
140 unsigned int i;
141 unsigned int count;
142
143 count = 0;
144 for (i = 0; i < n; ++i) {
145 if (S_ISDIR(entries[i].mode))
146 ++count;
147 }
148
149 return count;
150}
151
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200152static int get_task_root(struct task_struct *task, struct path *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000154 int result = -ENOENT;
155
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156 task_lock(task);
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200157 if (task->fs) {
158 get_fs_root(task->fs, root);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000159 result = 0;
160 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000162 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700163}
164
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800165static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700167 struct task_struct *task = get_proc_task(inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700169
170 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200171 task_lock(task);
172 if (task->fs) {
173 get_fs_pwd(task->fs, path);
174 result = 0;
175 }
176 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177 put_task_struct(task);
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return result;
180}
181
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800182static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700184 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186
187 if (task) {
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200188 result = get_task_root(task, path);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700189 put_task_struct(task);
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return result;
192}
193
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400194static struct mm_struct *__check_mem_permission(struct task_struct *task)
Roland McGrath638fa202008-04-29 01:01:38 -0700195{
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400196 struct mm_struct *mm;
197
198 mm = get_task_mm(task);
199 if (!mm)
200 return ERR_PTR(-EINVAL);
201
Roland McGrath638fa202008-04-29 01:01:38 -0700202 /*
203 * A task can always look at itself, in case it chooses
204 * to use system calls instead of load instructions.
205 */
206 if (task == current)
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400207 return mm;
Roland McGrath638fa202008-04-29 01:01:38 -0700208
209 /*
210 * If current is actively ptrace'ing, and would also be
211 * permitted to freshly attach with ptrace now, permit it.
212 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700213 if (task_is_stopped_or_traced(task)) {
214 int match;
215 rcu_read_lock();
216 match = (tracehook_tracer_task(task) == current);
217 rcu_read_unlock();
218 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400219 return mm;
Roland McGrath0d094ef2008-07-25 19:45:49 -0700220 }
Roland McGrath638fa202008-04-29 01:01:38 -0700221
222 /*
223 * Noone else is allowed.
224 */
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400225 mmput(mm);
226 return ERR_PTR(-EPERM);
Roland McGrath638fa202008-04-29 01:01:38 -0700227}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Stephen Wilson18f661b2011-03-13 15:49:22 -0400229/*
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400230 * If current may access user memory in @task return a reference to the
231 * corresponding mm, otherwise ERR_PTR.
Stephen Wilson18f661b2011-03-13 15:49:22 -0400232 */
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400233static struct mm_struct *check_mem_permission(struct task_struct *task)
Stephen Wilson18f661b2011-03-13 15:49:22 -0400234{
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400235 struct mm_struct *mm;
Stephen Wilson18f661b2011-03-13 15:49:22 -0400236 int err;
237
238 /*
239 * Avoid racing if task exec's as we might get a new mm but validate
240 * against old credentials.
241 */
242 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
243 if (err)
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400244 return ERR_PTR(err);
Stephen Wilson18f661b2011-03-13 15:49:22 -0400245
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400246 mm = __check_mem_permission(task);
Stephen Wilson18f661b2011-03-13 15:49:22 -0400247 mutex_unlock(&task->signal->cred_guard_mutex);
248
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400249 return mm;
Stephen Wilson18f661b2011-03-13 15:49:22 -0400250}
251
Al Viro831830b2008-01-02 14:09:57 +0000252struct mm_struct *mm_for_maps(struct task_struct *task)
253{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200254 struct mm_struct *mm;
Al Viroec6fd8a2011-02-15 22:22:54 -0500255 int err;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200256
Al Viroec6fd8a2011-02-15 22:22:54 -0500257 err = mutex_lock_killable(&task->signal->cred_guard_mutex);
258 if (err)
259 return ERR_PTR(err);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200260
261 mm = get_task_mm(task);
262 if (mm && mm != current->mm &&
263 !ptrace_may_access(task, PTRACE_MODE_READ)) {
264 mmput(mm);
Al Viroec6fd8a2011-02-15 22:22:54 -0500265 mm = ERR_PTR(-EACCES);
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200266 }
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700267 mutex_unlock(&task->signal->cred_guard_mutex);
Oleg Nesterov704b8362009-07-10 03:27:40 +0200268
Al Viro831830b2008-01-02 14:09:57 +0000269 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static int proc_pid_cmdline(struct task_struct *task, char * buffer)
273{
274 int res = 0;
275 unsigned int len;
276 struct mm_struct *mm = get_task_mm(task);
277 if (!mm)
278 goto out;
279 if (!mm->arg_end)
280 goto out_mm; /* Shh! No looking before we're done */
281
282 len = mm->arg_end - mm->arg_start;
283
284 if (len > PAGE_SIZE)
285 len = PAGE_SIZE;
286
287 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
288
289 // If the nul at the end of args has been overwritten, then
290 // assume application is using setproctitle(3).
291 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
292 len = strnlen(buffer, res);
293 if (len < res) {
294 res = len;
295 } else {
296 len = mm->env_end - mm->env_start;
297 if (len > PAGE_SIZE - res)
298 len = PAGE_SIZE - res;
299 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
300 res = strnlen(buffer, res);
301 }
302 }
303out_mm:
304 mmput(mm);
305out:
306 return res;
307}
308
309static int proc_pid_auxv(struct task_struct *task, char *buffer)
310{
Al Viro2fadaef2011-02-15 22:52:11 -0500311 struct mm_struct *mm = mm_for_maps(task);
312 int res = PTR_ERR(mm);
313 if (mm && !IS_ERR(mm)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300315 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300317 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 res = nwords * sizeof(mm->saved_auxv[0]);
319 if (res > PAGE_SIZE)
320 res = PAGE_SIZE;
321 memcpy(buffer, mm->saved_auxv, res);
322 mmput(mm);
323 }
324 return res;
325}
326
327
328#ifdef CONFIG_KALLSYMS
329/*
330 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
331 * Returns the resolved symbol. If that fails, simply return the address.
332 */
333static int proc_pid_wchan(struct task_struct *task, char *buffer)
334{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700335 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700336 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 wchan = get_wchan(task);
339
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700340 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600341 if (!ptrace_may_access(task, PTRACE_MODE_READ))
342 return 0;
343 else
344 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700345 else
346 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348#endif /* CONFIG_KALLSYMS */
349
Ken Chen2ec220e2008-11-10 11:26:08 +0300350#ifdef CONFIG_STACKTRACE
351
352#define MAX_STACK_TRACE_DEPTH 64
353
354static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
355 struct pid *pid, struct task_struct *task)
356{
357 struct stack_trace trace;
358 unsigned long *entries;
359 int i;
360
361 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
362 if (!entries)
363 return -ENOMEM;
364
365 trace.nr_entries = 0;
366 trace.max_entries = MAX_STACK_TRACE_DEPTH;
367 trace.entries = entries;
368 trace.skip = 0;
369 save_stack_trace_tsk(task, &trace);
370
371 for (i = 0; i < trace.nr_entries; i++) {
372 seq_printf(m, "[<%p>] %pS\n",
373 (void *)entries[i], (void *)entries[i]);
374 }
375 kfree(entries);
376
377 return 0;
378}
379#endif
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#ifdef CONFIG_SCHEDSTATS
382/*
383 * Provides /proc/PID/schedstat
384 */
385static int proc_pid_schedstat(struct task_struct *task, char *buffer)
386{
Balbir Singh172ba842007-07-09 18:52:00 +0200387 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100388 (unsigned long long)task->se.sum_exec_runtime,
389 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200390 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392#endif
393
Arjan van de Ven97455122008-01-25 21:08:34 +0100394#ifdef CONFIG_LATENCYTOP
395static int lstats_show_proc(struct seq_file *m, void *v)
396{
397 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800398 struct inode *inode = m->private;
399 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100400
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800401 if (!task)
402 return -ESRCH;
403 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100404 for (i = 0; i < 32; i++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800405 struct latency_record *lr = &task->latency_record[i];
406 if (lr->backtrace[0]) {
Arjan van de Ven97455122008-01-25 21:08:34 +0100407 int q;
Joe Perches34e49d42011-01-12 17:00:30 -0800408 seq_printf(m, "%i %li %li",
409 lr->count, lr->time, lr->max);
Arjan van de Ven97455122008-01-25 21:08:34 +0100410 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Joe Perches34e49d42011-01-12 17:00:30 -0800411 unsigned long bt = lr->backtrace[q];
412 if (!bt)
Arjan van de Ven97455122008-01-25 21:08:34 +0100413 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800414 if (bt == ULONG_MAX)
Arjan van de Ven97455122008-01-25 21:08:34 +0100415 break;
Joe Perches34e49d42011-01-12 17:00:30 -0800416 seq_printf(m, " %ps", (void *)bt);
Arjan van de Ven97455122008-01-25 21:08:34 +0100417 }
Alexey Dobriyan9d6de122011-01-12 17:00:32 -0800418 seq_putc(m, '\n');
Arjan van de Ven97455122008-01-25 21:08:34 +0100419 }
420
421 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800422 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100423 return 0;
424}
425
426static int lstats_open(struct inode *inode, struct file *file)
427{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800428 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800429}
430
Arjan van de Ven97455122008-01-25 21:08:34 +0100431static ssize_t lstats_write(struct file *file, const char __user *buf,
432 size_t count, loff_t *offs)
433{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800434 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100435
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800436 if (!task)
437 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100438 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800439 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100440
441 return count;
442}
443
444static const struct file_operations proc_lstats_operations = {
445 .open = lstats_open,
446 .read = seq_read,
447 .write = lstats_write,
448 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800449 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100450};
451
452#endif
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454static int proc_oom_score(struct task_struct *task, char *buffer)
455{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200456 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700458 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200459 if (pid_alive(task))
David Rientjesa63d83f42010-08-09 17:19:46 -0700460 points = oom_badness(task, NULL, NULL,
461 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700462 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return sprintf(buffer, "%lu\n", points);
464}
465
Neil Hormand85f50d2007-10-18 23:40:37 -0700466struct limit_names {
467 char *name;
468 char *unit;
469};
470
471static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700472 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700473 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
474 [RLIMIT_DATA] = {"Max data size", "bytes"},
475 [RLIMIT_STACK] = {"Max stack size", "bytes"},
476 [RLIMIT_CORE] = {"Max core file size", "bytes"},
477 [RLIMIT_RSS] = {"Max resident set", "bytes"},
478 [RLIMIT_NPROC] = {"Max processes", "processes"},
479 [RLIMIT_NOFILE] = {"Max open files", "files"},
480 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
481 [RLIMIT_AS] = {"Max address space", "bytes"},
482 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
483 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
484 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
485 [RLIMIT_NICE] = {"Max nice priority", NULL},
486 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800487 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700488};
489
490/* Display limits for a process */
491static int proc_pid_limits(struct task_struct *task, char *buffer)
492{
493 unsigned int i;
494 int count = 0;
495 unsigned long flags;
496 char *bufptr = buffer;
497
498 struct rlimit rlim[RLIM_NLIMITS];
499
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400500 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700501 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700502 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
503 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700504
505 /*
506 * print the file header
507 */
508 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
509 "Limit", "Soft Limit", "Hard Limit", "Units");
510
511 for (i = 0; i < RLIM_NLIMITS; i++) {
512 if (rlim[i].rlim_cur == RLIM_INFINITY)
513 count += sprintf(&bufptr[count], "%-25s %-20s ",
514 lnames[i].name, "unlimited");
515 else
516 count += sprintf(&bufptr[count], "%-25s %-20lu ",
517 lnames[i].name, rlim[i].rlim_cur);
518
519 if (rlim[i].rlim_max == RLIM_INFINITY)
520 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
521 else
522 count += sprintf(&bufptr[count], "%-20lu ",
523 rlim[i].rlim_max);
524
525 if (lnames[i].unit)
526 count += sprintf(&bufptr[count], "%-10s\n",
527 lnames[i].unit);
528 else
529 count += sprintf(&bufptr[count], "\n");
530 }
531
532 return count;
533}
534
Roland McGrathebcb6732008-07-25 19:46:00 -0700535#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
536static int proc_pid_syscall(struct task_struct *task, char *buffer)
537{
538 long nr;
539 unsigned long args[6], sp, pc;
540
541 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
542 return sprintf(buffer, "running\n");
543
544 if (nr < 0)
545 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
546
547 return sprintf(buffer,
548 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
549 nr,
550 args[0], args[1], args[2], args[3], args[4], args[5],
551 sp, pc);
552}
553#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555/************************************************************************/
556/* Here the fs part begins */
557/************************************************************************/
558
559/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700560static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700562 struct task_struct *task;
563 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700564 /* Allow access to a task's file descriptors if it is us or we
565 * may use ptrace attach to the process and find out that
566 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700567 */
568 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700569 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400570 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700571 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700572 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700573 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700576static int proc_setattr(struct dentry *dentry, struct iattr *attr)
577{
578 int error;
579 struct inode *inode = dentry->d_inode;
580
581 if (attr->ia_valid & ATTR_MODE)
582 return -EPERM;
583
584 error = inode_change_ok(inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +0200585 if (error)
586 return error;
587
588 if ((attr->ia_valid & ATTR_SIZE) &&
589 attr->ia_size != i_size_read(inode)) {
590 error = vmtruncate(inode, attr->ia_size);
591 if (error)
592 return error;
593 }
594
595 setattr_copy(inode, attr);
596 mark_inode_dirty(inode);
597 return 0;
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700598}
599
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800600static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700601 .setattr = proc_setattr,
602};
603
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100604static int mounts_open_common(struct inode *inode, struct file *file,
605 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700607 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700608 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800609 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100610 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500611 struct proc_mounts *p;
612 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Eric W. Biederman99f89552006-06-26 00:25:55 -0700614 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700615 rcu_read_lock();
616 nsp = task_nsproxy(task);
617 if (nsp) {
618 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800619 if (ns)
620 get_mnt_ns(ns);
621 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700622 rcu_read_unlock();
Miklos Szeredif7ad3c62010-08-10 11:41:36 +0200623 if (ns && get_task_root(task, &root) == 0)
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000624 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700625 put_task_struct(task);
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100628 if (!ns)
629 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000630 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100631 goto err_put_ns;
632
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100633 ret = -ENOMEM;
634 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
635 if (!p)
636 goto err_put_path;
637
638 file->private_data = &p->m;
639 ret = seq_open(file, op);
640 if (ret)
641 goto err_free;
642
643 p->m.private = p;
644 p->ns = ns;
645 p->root = root;
646 p->event = ns->event;
647
648 return 0;
649
650 err_free:
651 kfree(p);
652 err_put_path:
653 path_put(&root);
654 err_put_ns:
655 put_mnt_ns(ns);
656 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return ret;
658}
659
660static int mounts_release(struct inode *inode, struct file *file)
661{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100662 struct proc_mounts *p = file->private_data;
663 path_put(&p->root);
664 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return seq_release(inode, file);
666}
667
Al Viro5addc5d2005-11-07 17:15:49 -0500668static unsigned mounts_poll(struct file *file, poll_table *wait)
669{
670 struct proc_mounts *p = file->private_data;
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900671 unsigned res = POLLIN | POLLRDNORM;
Al Viro5addc5d2005-11-07 17:15:49 -0500672
Al Viro9f5596a2010-02-05 00:40:25 -0500673 poll_wait(file, &p->ns->poll, wait);
674 if (mnt_had_events(p))
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900675 res |= POLLERR | POLLPRI;
Al Viro5addc5d2005-11-07 17:15:49 -0500676
677 return res;
678}
679
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100680static int mounts_open(struct inode *inode, struct file *file)
681{
682 return mounts_open_common(inode, file, &mounts_op);
683}
684
Arjan van de Ven00977a52007-02-12 00:55:34 -0800685static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 .open = mounts_open,
687 .read = seq_read,
688 .llseek = seq_lseek,
689 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500690 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691};
692
Ram Pai2d4d4862008-03-27 13:06:25 +0100693static int mountinfo_open(struct inode *inode, struct file *file)
694{
695 return mounts_open_common(inode, file, &mountinfo_op);
696}
697
698static const struct file_operations proc_mountinfo_operations = {
699 .open = mountinfo_open,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = mounts_release,
703 .poll = mounts_poll,
704};
705
Chuck Leverb4629fe2006-03-20 13:44:12 -0500706static int mountstats_open(struct inode *inode, struct file *file)
707{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100708 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500709}
710
Arjan van de Ven00977a52007-02-12 00:55:34 -0800711static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500712 .open = mountstats_open,
713 .read = seq_read,
714 .llseek = seq_lseek,
715 .release = mounts_release,
716};
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
719
720static ssize_t proc_info_read(struct file * file, char __user * buf,
721 size_t count, loff_t *ppos)
722{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800723 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned long page;
725 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700726 struct task_struct *task = get_proc_task(inode);
727
728 length = -ESRCH;
729 if (!task)
730 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 if (count > PROC_BLOCK_SIZE)
733 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700734
735 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700736 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700737 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 length = PROC_I(inode)->op.proc_read(task, (char*)page);
740
741 if (length >= 0)
742 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
743 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700744out:
745 put_task_struct(task);
746out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return length;
748}
749
Arjan van de Ven00977a52007-02-12 00:55:34 -0800750static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100752 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753};
754
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800755static int proc_single_show(struct seq_file *m, void *v)
756{
757 struct inode *inode = m->private;
758 struct pid_namespace *ns;
759 struct pid *pid;
760 struct task_struct *task;
761 int ret;
762
763 ns = inode->i_sb->s_fs_info;
764 pid = proc_pid(inode);
765 task = get_pid_task(pid, PIDTYPE_PID);
766 if (!task)
767 return -ESRCH;
768
769 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
770
771 put_task_struct(task);
772 return ret;
773}
774
775static int proc_single_open(struct inode *inode, struct file *filp)
776{
Jovi Zhangc6a34052011-01-12 17:00:34 -0800777 return single_open(filp, proc_single_show, inode);
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800778}
779
780static const struct file_operations proc_single_file_operations = {
781 .open = proc_single_open,
782 .read = seq_read,
783 .llseek = seq_lseek,
784 .release = single_release,
785};
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static int mem_open(struct inode* inode, struct file* file)
788{
789 file->private_data = (void*)((long)current->self_exec_id);
KAMEZAWA Hiroyuki4a3956c2010-10-01 14:20:22 -0700790 /* OK to pass negative loff_t, we can catch out-of-range */
791 file->f_mode |= FMODE_UNSIGNED_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
795static ssize_t mem_read(struct file * file, char __user * buf,
796 size_t count, loff_t *ppos)
797{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800798 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 char *page;
800 unsigned long src = *ppos;
801 int ret = -ESRCH;
802 struct mm_struct *mm;
803
Eric W. Biederman99f89552006-06-26 00:25:55 -0700804 if (!task)
805 goto out_no_task;
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700808 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!page)
810 goto out;
811
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400812 mm = check_mem_permission(task);
813 ret = PTR_ERR(mm);
814 if (IS_ERR(mm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto out_free;
816
817 ret = -EIO;
818
819 if (file->private_data != (void*)((long)current->self_exec_id))
820 goto out_put;
821
822 ret = 0;
823
824 while (count > 0) {
825 int this_len, retval;
826
827 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400828 retval = access_remote_vm(mm, src, page, this_len, 0);
829 if (!retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (!ret)
831 ret = -EIO;
832 break;
833 }
834
835 if (copy_to_user(buf, page, retval)) {
836 ret = -EFAULT;
837 break;
838 }
839
840 ret += retval;
841 src += retval;
842 buf += retval;
843 count -= retval;
844 }
845 *ppos = src;
846
847out_put:
848 mmput(mm);
849out_free:
850 free_page((unsigned long) page);
851out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700852 put_task_struct(task);
853out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return ret;
855}
856
857#define mem_write NULL
858
859#ifndef mem_write
860/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800861static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 size_t count, loff_t *ppos)
863{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700864 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800866 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 unsigned long dst = *ppos;
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400868 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Eric W. Biederman99f89552006-06-26 00:25:55 -0700870 copied = -ESRCH;
871 if (!task)
872 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400874 mm = check_mem_permission(task);
875 copied = PTR_ERR(mm);
876 if (IS_ERR(mm))
877 goto out_task;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700878
Stephen Wilson26947f82011-03-13 15:49:21 -0400879 copied = -EIO;
880 if (file->private_data != (void *)((long)current->self_exec_id))
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400881 goto out_mm;
Stephen Wilson26947f82011-03-13 15:49:21 -0400882
Eric W. Biederman99f89552006-06-26 00:25:55 -0700883 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700884 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (!page)
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400886 goto out_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700888 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 while (count > 0) {
890 int this_len, retval;
891
892 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
893 if (copy_from_user(page, buf, this_len)) {
894 copied = -EFAULT;
895 break;
896 }
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400897 retval = access_remote_vm(mm, dst, page, this_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!retval) {
899 if (!copied)
900 copied = -EIO;
901 break;
902 }
903 copied += retval;
904 buf += retval;
905 dst += retval;
906 count -= retval;
907 }
908 *ppos = dst;
909 free_page((unsigned long) page);
Stephen Wilson8b0db9d2011-03-13 15:49:23 -0400910out_mm:
911 mmput(mm);
912out_task:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700913 put_task_struct(task);
914out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return copied;
916}
917#endif
918
Matt Mackall85863e42008-02-04 22:29:04 -0800919loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 switch (orig) {
922 case 0:
923 file->f_pos = offset;
924 break;
925 case 1:
926 file->f_pos += offset;
927 break;
928 default:
929 return -EINVAL;
930 }
931 force_successful_syscall_return();
932 return file->f_pos;
933}
934
Arjan van de Ven00977a52007-02-12 00:55:34 -0800935static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 .llseek = mem_lseek,
937 .read = mem_read,
938 .write = mem_write,
939 .open = mem_open,
940};
941
James Pearson315e28c2007-10-16 23:30:17 -0700942static ssize_t environ_read(struct file *file, char __user *buf,
943 size_t count, loff_t *ppos)
944{
945 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
946 char *page;
947 unsigned long src = *ppos;
948 int ret = -ESRCH;
949 struct mm_struct *mm;
950
951 if (!task)
952 goto out_no_task;
953
James Pearson315e28c2007-10-16 23:30:17 -0700954 ret = -ENOMEM;
955 page = (char *)__get_free_page(GFP_TEMPORARY);
956 if (!page)
957 goto out;
958
James Pearson315e28c2007-10-16 23:30:17 -0700959
Al Virod6f64b82011-02-15 22:26:01 -0500960 mm = mm_for_maps(task);
961 ret = PTR_ERR(mm);
962 if (!mm || IS_ERR(mm))
James Pearson315e28c2007-10-16 23:30:17 -0700963 goto out_free;
964
Al Virod6f64b82011-02-15 22:26:01 -0500965 ret = 0;
James Pearson315e28c2007-10-16 23:30:17 -0700966 while (count > 0) {
967 int this_len, retval, max_len;
968
969 this_len = mm->env_end - (mm->env_start + src);
970
971 if (this_len <= 0)
972 break;
973
974 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
975 this_len = (this_len > max_len) ? max_len : this_len;
976
977 retval = access_process_vm(task, (mm->env_start + src),
978 page, this_len, 0);
979
980 if (retval <= 0) {
981 ret = retval;
982 break;
983 }
984
985 if (copy_to_user(buf, page, retval)) {
986 ret = -EFAULT;
987 break;
988 }
989
990 ret += retval;
991 src += retval;
992 buf += retval;
993 count -= retval;
994 }
995 *ppos = src;
996
997 mmput(mm);
998out_free:
999 free_page((unsigned long) page);
1000out:
1001 put_task_struct(task);
1002out_no_task:
1003 return ret;
1004}
1005
1006static const struct file_operations proc_environ_operations = {
1007 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001008 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -07001009};
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011static ssize_t oom_adjust_read(struct file *file, char __user *buf,
1012 size_t count, loff_t *ppos)
1013{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001014 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001015 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001017 int oom_adjust = OOM_DISABLE;
1018 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Eric W. Biederman99f89552006-06-26 00:25:55 -07001020 if (!task)
1021 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001022
1023 if (lock_task_sighand(task, &flags)) {
1024 oom_adjust = task->signal->oom_adj;
1025 unlock_task_sighand(task, &flags);
1026 }
1027
Eric W. Biederman99f89552006-06-26 00:25:55 -07001028 put_task_struct(task);
1029
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001030 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001031
1032 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1036 size_t count, loff_t *ppos)
1037{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001038 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001039 char buffer[PROC_NUMBUF];
1040 long oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001041 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001042 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001044 memset(buffer, 0, sizeof(buffer));
1045 if (count > sizeof(buffer) - 1)
1046 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001047 if (copy_from_user(buffer, buf, count)) {
1048 err = -EFAULT;
1049 goto out;
1050 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001051
1052 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1053 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001054 goto out;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001055 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
David Rientjes723548b2010-10-26 14:21:25 -07001056 oom_adjust != OOM_DISABLE) {
1057 err = -EINVAL;
1058 goto out;
1059 }
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001060
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001061 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001062 if (!task) {
1063 err = -ESRCH;
1064 goto out;
1065 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001066
Ying Han3d5992d2010-10-26 14:21:23 -07001067 task_lock(task);
1068 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001069 err = -EINVAL;
1070 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001071 }
1072
David Rientjesd19d5472010-10-26 14:21:26 -07001073 if (!lock_task_sighand(task, &flags)) {
1074 err = -ESRCH;
1075 goto err_task_lock;
1076 }
1077
1078 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1079 err = -EACCES;
1080 goto err_sighand;
1081 }
1082
Ying Han3d5992d2010-10-26 14:21:23 -07001083 if (oom_adjust != task->signal->oom_adj) {
1084 if (oom_adjust == OOM_DISABLE)
1085 atomic_inc(&task->mm->oom_disable_count);
1086 if (task->signal->oom_adj == OOM_DISABLE)
1087 atomic_dec(&task->mm->oom_disable_count);
1088 }
1089
David Rientjes51b1bd22010-08-09 17:19:47 -07001090 /*
1091 * Warn that /proc/pid/oom_adj is deprecated, see
1092 * Documentation/feature-removal-schedule.txt.
1093 */
1094 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1095 "please use /proc/%d/oom_score_adj instead.\n",
1096 current->comm, task_pid_nr(current),
1097 task_pid_nr(task), task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001098 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f42010-08-09 17:19:46 -07001099 /*
1100 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1101 * value is always attainable.
1102 */
1103 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1104 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1105 else
1106 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1107 -OOM_DISABLE;
David Rientjes723548b2010-10-26 14:21:25 -07001108err_sighand:
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001109 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001110err_task_lock:
1111 task_unlock(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001112 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001113out:
1114 return err < 0 ? err : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Arjan van de Ven00977a52007-02-12 00:55:34 -08001117static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .read = oom_adjust_read,
1119 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001120 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121};
1122
David Rientjesa63d83f42010-08-09 17:19:46 -07001123static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1124 size_t count, loff_t *ppos)
1125{
1126 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1127 char buffer[PROC_NUMBUF];
1128 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1129 unsigned long flags;
1130 size_t len;
1131
1132 if (!task)
1133 return -ESRCH;
1134 if (lock_task_sighand(task, &flags)) {
1135 oom_score_adj = task->signal->oom_score_adj;
1136 unlock_task_sighand(task, &flags);
1137 }
1138 put_task_struct(task);
1139 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1140 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1141}
1142
1143static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1144 size_t count, loff_t *ppos)
1145{
1146 struct task_struct *task;
1147 char buffer[PROC_NUMBUF];
1148 unsigned long flags;
1149 long oom_score_adj;
1150 int err;
1151
1152 memset(buffer, 0, sizeof(buffer));
1153 if (count > sizeof(buffer) - 1)
1154 count = sizeof(buffer) - 1;
David Rientjes723548b2010-10-26 14:21:25 -07001155 if (copy_from_user(buffer, buf, count)) {
1156 err = -EFAULT;
1157 goto out;
1158 }
David Rientjesa63d83f42010-08-09 17:19:46 -07001159
1160 err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1161 if (err)
David Rientjes723548b2010-10-26 14:21:25 -07001162 goto out;
David Rientjesa63d83f42010-08-09 17:19:46 -07001163 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
David Rientjes723548b2010-10-26 14:21:25 -07001164 oom_score_adj > OOM_SCORE_ADJ_MAX) {
1165 err = -EINVAL;
1166 goto out;
1167 }
David Rientjesa63d83f42010-08-09 17:19:46 -07001168
1169 task = get_proc_task(file->f_path.dentry->d_inode);
David Rientjes723548b2010-10-26 14:21:25 -07001170 if (!task) {
1171 err = -ESRCH;
1172 goto out;
1173 }
David Rientjesa63d83f42010-08-09 17:19:46 -07001174
Ying Han3d5992d2010-10-26 14:21:23 -07001175 task_lock(task);
1176 if (!task->mm) {
David Rientjes723548b2010-10-26 14:21:25 -07001177 err = -EINVAL;
1178 goto err_task_lock;
Ying Han3d5992d2010-10-26 14:21:23 -07001179 }
David Rientjesd19d5472010-10-26 14:21:26 -07001180
1181 if (!lock_task_sighand(task, &flags)) {
1182 err = -ESRCH;
1183 goto err_task_lock;
1184 }
1185
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001186 if (oom_score_adj < task->signal->oom_score_adj_min &&
David Rientjesd19d5472010-10-26 14:21:26 -07001187 !capable(CAP_SYS_RESOURCE)) {
1188 err = -EACCES;
1189 goto err_sighand;
1190 }
1191
Ying Han3d5992d2010-10-26 14:21:23 -07001192 if (oom_score_adj != task->signal->oom_score_adj) {
1193 if (oom_score_adj == OOM_SCORE_ADJ_MIN)
1194 atomic_inc(&task->mm->oom_disable_count);
1195 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1196 atomic_dec(&task->mm->oom_disable_count);
1197 }
David Rientjesa63d83f42010-08-09 17:19:46 -07001198 task->signal->oom_score_adj = oom_score_adj;
Mandeep Singh Bainesdabb16f2011-01-13 15:46:05 -08001199 if (has_capability_noaudit(current, CAP_SYS_RESOURCE))
1200 task->signal->oom_score_adj_min = oom_score_adj;
David Rientjesa63d83f42010-08-09 17:19:46 -07001201 /*
1202 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1203 * always attainable.
1204 */
1205 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1206 task->signal->oom_adj = OOM_DISABLE;
1207 else
1208 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1209 OOM_SCORE_ADJ_MAX;
David Rientjes723548b2010-10-26 14:21:25 -07001210err_sighand:
David Rientjesa63d83f42010-08-09 17:19:46 -07001211 unlock_task_sighand(task, &flags);
David Rientjesd19d5472010-10-26 14:21:26 -07001212err_task_lock:
1213 task_unlock(task);
David Rientjesa63d83f42010-08-09 17:19:46 -07001214 put_task_struct(task);
David Rientjes723548b2010-10-26 14:21:25 -07001215out:
1216 return err < 0 ? err : count;
David Rientjesa63d83f42010-08-09 17:19:46 -07001217}
1218
1219static const struct file_operations proc_oom_score_adj_operations = {
1220 .read = oom_score_adj_read,
1221 .write = oom_score_adj_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001222 .llseek = default_llseek,
David Rientjesa63d83f42010-08-09 17:19:46 -07001223};
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225#ifdef CONFIG_AUDITSYSCALL
1226#define TMPBUFLEN 21
1227static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1228 size_t count, loff_t *ppos)
1229{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001230 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001231 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 ssize_t length;
1233 char tmpbuf[TMPBUFLEN];
1234
Eric W. Biederman99f89552006-06-26 00:25:55 -07001235 if (!task)
1236 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001238 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001239 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1241}
1242
1243static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1244 size_t count, loff_t *ppos)
1245{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001246 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 char *page, *tmp;
1248 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 uid_t loginuid;
1250
1251 if (!capable(CAP_AUDIT_CONTROL))
1252 return -EPERM;
1253
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001254 rcu_read_lock();
1255 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1256 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001258 }
1259 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Al Viroe0182902006-05-18 08:28:02 -04001261 if (count >= PAGE_SIZE)
1262 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (*ppos != 0) {
1265 /* No partial writes. */
1266 return -EINVAL;
1267 }
Mel Gormane12ba742007-10-16 01:25:52 -07001268 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (!page)
1270 return -ENOMEM;
1271 length = -EFAULT;
1272 if (copy_from_user(page, buf, count))
1273 goto out_free_page;
1274
Al Viroe0182902006-05-18 08:28:02 -04001275 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 loginuid = simple_strtoul(page, &tmp, 10);
1277 if (tmp == page) {
1278 length = -EINVAL;
1279 goto out_free_page;
1280
1281 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001282 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (likely(length == 0))
1284 length = count;
1285
1286out_free_page:
1287 free_page((unsigned long) page);
1288 return length;
1289}
1290
Arjan van de Ven00977a52007-02-12 00:55:34 -08001291static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 .read = proc_loginuid_read,
1293 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001294 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295};
Eric Paris1e0bd752008-03-13 08:15:31 -04001296
1297static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1298 size_t count, loff_t *ppos)
1299{
1300 struct inode * inode = file->f_path.dentry->d_inode;
1301 struct task_struct *task = get_proc_task(inode);
1302 ssize_t length;
1303 char tmpbuf[TMPBUFLEN];
1304
1305 if (!task)
1306 return -ESRCH;
1307 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1308 audit_get_sessionid(task));
1309 put_task_struct(task);
1310 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1311}
1312
1313static const struct file_operations proc_sessionid_operations = {
1314 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001315 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001316};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317#endif
1318
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001319#ifdef CONFIG_FAULT_INJECTION
1320static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1321 size_t count, loff_t *ppos)
1322{
1323 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1324 char buffer[PROC_NUMBUF];
1325 size_t len;
1326 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001327
1328 if (!task)
1329 return -ESRCH;
1330 make_it_fail = task->make_it_fail;
1331 put_task_struct(task);
1332
1333 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001334
1335 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001336}
1337
1338static ssize_t proc_fault_inject_write(struct file * file,
1339 const char __user * buf, size_t count, loff_t *ppos)
1340{
1341 struct task_struct *task;
1342 char buffer[PROC_NUMBUF], *end;
1343 int make_it_fail;
1344
1345 if (!capable(CAP_SYS_RESOURCE))
1346 return -EPERM;
1347 memset(buffer, 0, sizeof(buffer));
1348 if (count > sizeof(buffer) - 1)
1349 count = sizeof(buffer) - 1;
1350 if (copy_from_user(buffer, buf, count))
1351 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001352 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1353 if (*end)
1354 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001355 task = get_proc_task(file->f_dentry->d_inode);
1356 if (!task)
1357 return -ESRCH;
1358 task->make_it_fail = make_it_fail;
1359 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001360
1361 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001362}
1363
Arjan van de Ven00977a52007-02-12 00:55:34 -08001364static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001365 .read = proc_fault_inject_read,
1366 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001367 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001368};
1369#endif
1370
Arjan van de Ven97455122008-01-25 21:08:34 +01001371
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001372#ifdef CONFIG_SCHED_DEBUG
1373/*
1374 * Print out various scheduling related per-task fields:
1375 */
1376static int sched_show(struct seq_file *m, void *v)
1377{
1378 struct inode *inode = m->private;
1379 struct task_struct *p;
1380
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001381 p = get_proc_task(inode);
1382 if (!p)
1383 return -ESRCH;
1384 proc_sched_show_task(p, m);
1385
1386 put_task_struct(p);
1387
1388 return 0;
1389}
1390
1391static ssize_t
1392sched_write(struct file *file, const char __user *buf,
1393 size_t count, loff_t *offset)
1394{
1395 struct inode *inode = file->f_path.dentry->d_inode;
1396 struct task_struct *p;
1397
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001398 p = get_proc_task(inode);
1399 if (!p)
1400 return -ESRCH;
1401 proc_sched_set_task(p);
1402
1403 put_task_struct(p);
1404
1405 return count;
1406}
1407
1408static int sched_open(struct inode *inode, struct file *filp)
1409{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001410 return single_open(filp, sched_show, inode);
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001411}
1412
1413static const struct file_operations proc_pid_sched_operations = {
1414 .open = sched_open,
1415 .read = seq_read,
1416 .write = sched_write,
1417 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001418 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001419};
1420
1421#endif
1422
Mike Galbraith5091faa2010-11-30 14:18:03 +01001423#ifdef CONFIG_SCHED_AUTOGROUP
1424/*
1425 * Print out autogroup related information:
1426 */
1427static int sched_autogroup_show(struct seq_file *m, void *v)
1428{
1429 struct inode *inode = m->private;
1430 struct task_struct *p;
1431
1432 p = get_proc_task(inode);
1433 if (!p)
1434 return -ESRCH;
1435 proc_sched_autogroup_show_task(p, m);
1436
1437 put_task_struct(p);
1438
1439 return 0;
1440}
1441
1442static ssize_t
1443sched_autogroup_write(struct file *file, const char __user *buf,
1444 size_t count, loff_t *offset)
1445{
1446 struct inode *inode = file->f_path.dentry->d_inode;
1447 struct task_struct *p;
1448 char buffer[PROC_NUMBUF];
1449 long nice;
1450 int err;
1451
1452 memset(buffer, 0, sizeof(buffer));
1453 if (count > sizeof(buffer) - 1)
1454 count = sizeof(buffer) - 1;
1455 if (copy_from_user(buffer, buf, count))
1456 return -EFAULT;
1457
1458 err = strict_strtol(strstrip(buffer), 0, &nice);
1459 if (err)
1460 return -EINVAL;
1461
1462 p = get_proc_task(inode);
1463 if (!p)
1464 return -ESRCH;
1465
1466 err = nice;
1467 err = proc_sched_autogroup_set_nice(p, &err);
1468 if (err)
1469 count = err;
1470
1471 put_task_struct(p);
1472
1473 return count;
1474}
1475
1476static int sched_autogroup_open(struct inode *inode, struct file *filp)
1477{
1478 int ret;
1479
1480 ret = single_open(filp, sched_autogroup_show, NULL);
1481 if (!ret) {
1482 struct seq_file *m = filp->private_data;
1483
1484 m->private = inode;
1485 }
1486 return ret;
1487}
1488
1489static const struct file_operations proc_pid_sched_autogroup_operations = {
1490 .open = sched_autogroup_open,
1491 .read = seq_read,
1492 .write = sched_autogroup_write,
1493 .llseek = seq_lseek,
1494 .release = single_release,
1495};
1496
1497#endif /* CONFIG_SCHED_AUTOGROUP */
1498
john stultz4614a696b2009-12-14 18:00:05 -08001499static ssize_t comm_write(struct file *file, const char __user *buf,
1500 size_t count, loff_t *offset)
1501{
1502 struct inode *inode = file->f_path.dentry->d_inode;
1503 struct task_struct *p;
1504 char buffer[TASK_COMM_LEN];
1505
1506 memset(buffer, 0, sizeof(buffer));
1507 if (count > sizeof(buffer) - 1)
1508 count = sizeof(buffer) - 1;
1509 if (copy_from_user(buffer, buf, count))
1510 return -EFAULT;
1511
1512 p = get_proc_task(inode);
1513 if (!p)
1514 return -ESRCH;
1515
1516 if (same_thread_group(current, p))
1517 set_task_comm(p, buffer);
1518 else
1519 count = -EINVAL;
1520
1521 put_task_struct(p);
1522
1523 return count;
1524}
1525
1526static int comm_show(struct seq_file *m, void *v)
1527{
1528 struct inode *inode = m->private;
1529 struct task_struct *p;
1530
1531 p = get_proc_task(inode);
1532 if (!p)
1533 return -ESRCH;
1534
1535 task_lock(p);
1536 seq_printf(m, "%s\n", p->comm);
1537 task_unlock(p);
1538
1539 put_task_struct(p);
1540
1541 return 0;
1542}
1543
1544static int comm_open(struct inode *inode, struct file *filp)
1545{
Jovi Zhangc6a34052011-01-12 17:00:34 -08001546 return single_open(filp, comm_show, inode);
john stultz4614a696b2009-12-14 18:00:05 -08001547}
1548
1549static const struct file_operations proc_pid_set_comm_operations = {
1550 .open = comm_open,
1551 .read = seq_read,
1552 .write = comm_write,
1553 .llseek = seq_lseek,
1554 .release = single_release,
1555};
1556
Matt Helsley925d1c42008-04-29 01:01:36 -07001557/*
1558 * We added or removed a vma mapping the executable. The vmas are only mapped
1559 * during exec and are not mapped with the mmap system call.
1560 * Callers must hold down_write() on the mm's mmap_sem for these
1561 */
1562void added_exe_file_vma(struct mm_struct *mm)
1563{
1564 mm->num_exe_file_vmas++;
1565}
1566
1567void removed_exe_file_vma(struct mm_struct *mm)
1568{
1569 mm->num_exe_file_vmas--;
1570 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1571 fput(mm->exe_file);
1572 mm->exe_file = NULL;
1573 }
1574
1575}
1576
1577void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1578{
1579 if (new_exe_file)
1580 get_file(new_exe_file);
1581 if (mm->exe_file)
1582 fput(mm->exe_file);
1583 mm->exe_file = new_exe_file;
1584 mm->num_exe_file_vmas = 0;
1585}
1586
1587struct file *get_mm_exe_file(struct mm_struct *mm)
1588{
1589 struct file *exe_file;
1590
1591 /* We need mmap_sem to protect against races with removal of
1592 * VM_EXECUTABLE vmas */
1593 down_read(&mm->mmap_sem);
1594 exe_file = mm->exe_file;
1595 if (exe_file)
1596 get_file(exe_file);
1597 up_read(&mm->mmap_sem);
1598 return exe_file;
1599}
1600
1601void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1602{
1603 /* It's safe to write the exe_file pointer without exe_file_lock because
1604 * this is called during fork when the task is not yet in /proc */
1605 newmm->exe_file = get_mm_exe_file(oldmm);
1606}
1607
1608static int proc_exe_link(struct inode *inode, struct path *exe_path)
1609{
1610 struct task_struct *task;
1611 struct mm_struct *mm;
1612 struct file *exe_file;
1613
1614 task = get_proc_task(inode);
1615 if (!task)
1616 return -ENOENT;
1617 mm = get_task_mm(task);
1618 put_task_struct(task);
1619 if (!mm)
1620 return -ENOENT;
1621 exe_file = get_mm_exe_file(mm);
1622 mmput(mm);
1623 if (exe_file) {
1624 *exe_path = exe_file->f_path;
1625 path_get(&exe_file->f_path);
1626 fput(exe_file);
1627 return 0;
1628 } else
1629 return -ENOENT;
1630}
1631
Al Viro008b1502005-08-20 00:17:39 +01001632static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 struct inode *inode = dentry->d_inode;
1635 int error = -EACCES;
1636
1637 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001638 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Eric W. Biederman778c1142006-06-26 00:25:58 -07001640 /* Are we allowed to snoop on the tasks file descriptors? */
1641 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001644 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645out:
Al Viro008b1502005-08-20 00:17:39 +01001646 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001649static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Mel Gormane12ba742007-10-16 01:25:52 -07001651 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001652 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int len;
1654
1655 if (!tmp)
1656 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001657
Eric W. Biederman7b2a69b2010-12-05 15:51:21 -08001658 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001659 len = PTR_ERR(pathname);
1660 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001662 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (len > buflen)
1665 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001666 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 len = -EFAULT;
1668 out:
1669 free_page((unsigned long)tmp);
1670 return len;
1671}
1672
1673static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1674{
1675 int error = -EACCES;
1676 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001677 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Eric W. Biederman778c1142006-06-26 00:25:58 -07001679 /* Are we allowed to snoop on the tasks file descriptors? */
1680 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001683 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (error)
1685 goto out;
1686
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001687 error = do_proc_readlink(&path, buffer, buflen);
1688 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return error;
1691}
1692
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001693static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001695 .follow_link = proc_pid_follow_link,
1696 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697};
1698
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001699
1700/* building an inode */
1701
1702static int task_dumpable(struct task_struct *task)
1703{
1704 int dumpable = 0;
1705 struct mm_struct *mm;
1706
1707 task_lock(task);
1708 mm = task->mm;
1709 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001710 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001711 task_unlock(task);
1712 if(dumpable == 1)
1713 return 1;
1714 return 0;
1715}
1716
1717
Eric W. Biederman61a28782006-10-02 02:18:49 -07001718static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001719{
1720 struct inode * inode;
1721 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001722 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001723
1724 /* We need a new inode */
1725
1726 inode = new_inode(sb);
1727 if (!inode)
1728 goto out;
1729
1730 /* Common stuff */
1731 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04001732 inode->i_ino = get_next_ino();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001733 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001734 inode->i_op = &proc_def_inode_operations;
1735
1736 /*
1737 * grab the reference to task.
1738 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001739 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001740 if (!ei->pid)
1741 goto out_unlock;
1742
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001744 rcu_read_lock();
1745 cred = __task_cred(task);
1746 inode->i_uid = cred->euid;
1747 inode->i_gid = cred->egid;
1748 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001749 }
1750 security_task_to_inode(task, inode);
1751
1752out:
1753 return inode;
1754
1755out_unlock:
1756 iput(inode);
1757 return NULL;
1758}
1759
1760static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1761{
1762 struct inode *inode = dentry->d_inode;
1763 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001764 const struct cred *cred;
1765
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001766 generic_fillattr(inode, stat);
1767
1768 rcu_read_lock();
1769 stat->uid = 0;
1770 stat->gid = 0;
1771 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1772 if (task) {
1773 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1774 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001775 cred = __task_cred(task);
1776 stat->uid = cred->euid;
1777 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001778 }
1779 }
1780 rcu_read_unlock();
1781 return 0;
1782}
1783
1784/* dentry stuff */
1785
1786/*
1787 * Exceptional case: normally we are not allowed to unhash a busy
1788 * directory. In this case, however, we can do it - no aliasing problems
1789 * due to the way we treat inodes.
1790 *
1791 * Rewrite the inode's ownerships here because the owning task may have
1792 * performed a setuid(), etc.
1793 *
1794 * Before the /proc/pid/status file was created the only way to read
1795 * the effective uid of a /process was to stat /proc/pid. Reading
1796 * /proc/pid/status is slow enough that procps and other packages
1797 * kept stating /proc/pid. To keep the rules in /proc simple I have
1798 * made this apply to all per process world readable and executable
1799 * directories.
1800 */
1801static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1802{
Nick Piggin34286d62011-01-07 17:49:57 +11001803 struct inode *inode;
1804 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001805 const struct cred *cred;
1806
Nick Piggin34286d62011-01-07 17:49:57 +11001807 if (nd && nd->flags & LOOKUP_RCU)
1808 return -ECHILD;
1809
1810 inode = dentry->d_inode;
1811 task = get_proc_task(inode);
1812
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001813 if (task) {
1814 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1815 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001816 rcu_read_lock();
1817 cred = __task_cred(task);
1818 inode->i_uid = cred->euid;
1819 inode->i_gid = cred->egid;
1820 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001821 } else {
1822 inode->i_uid = 0;
1823 inode->i_gid = 0;
1824 }
1825 inode->i_mode &= ~(S_ISUID | S_ISGID);
1826 security_task_to_inode(task, inode);
1827 put_task_struct(task);
1828 return 1;
1829 }
1830 d_drop(dentry);
1831 return 0;
1832}
1833
Nick Pigginfe15ce42011-01-07 17:49:23 +11001834static int pid_delete_dentry(const struct dentry * dentry)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001835{
1836 /* Is the task we represent dead?
1837 * If so, then don't put the dentry on the lru list,
1838 * kill it immediately.
1839 */
1840 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1841}
1842
Al Virod72f71e2009-02-20 05:58:47 +00001843static const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001844{
1845 .d_revalidate = pid_revalidate,
1846 .d_delete = pid_delete_dentry,
1847};
1848
1849/* Lookups */
1850
Eric Dumazetc5141e62007-05-08 00:26:15 -07001851typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1852 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001853
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001854/*
1855 * Fill a directory entry.
1856 *
1857 * If possible create the dcache entry and derive our inode number and
1858 * file type from dcache entry.
1859 *
1860 * Since all of the proc inode numbers are dynamically generated, the inode
1861 * numbers do not exist until the inode is cache. This means creating the
1862 * the dcache entry in readdir is necessary to keep the inode numbers
1863 * reported by readdir in sync with the inode numbers reported
1864 * by stat.
1865 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001866static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1867 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001868 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001869{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001870 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001871 struct inode *inode;
1872 struct qstr qname;
1873 ino_t ino = 0;
1874 unsigned type = DT_UNKNOWN;
1875
1876 qname.name = name;
1877 qname.len = len;
1878 qname.hash = full_name_hash(name, len);
1879
1880 child = d_lookup(dir, &qname);
1881 if (!child) {
1882 struct dentry *new;
1883 new = d_alloc(dir, &qname);
1884 if (new) {
1885 child = instantiate(dir->d_inode, new, task, ptr);
1886 if (child)
1887 dput(new);
1888 else
1889 child = new;
1890 }
1891 }
1892 if (!child || IS_ERR(child) || !child->d_inode)
1893 goto end_instantiate;
1894 inode = child->d_inode;
1895 if (inode) {
1896 ino = inode->i_ino;
1897 type = inode->i_mode >> 12;
1898 }
1899 dput(child);
1900end_instantiate:
1901 if (!ino)
1902 ino = find_inode_number(dir, &qname);
1903 if (!ino)
1904 ino = 1;
1905 return filldir(dirent, name, len, filp->f_pos, ino, type);
1906}
1907
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001908static unsigned name_to_int(struct dentry *dentry)
1909{
1910 const char *name = dentry->d_name.name;
1911 int len = dentry->d_name.len;
1912 unsigned n = 0;
1913
1914 if (len > 1 && *name == '0')
1915 goto out;
1916 while (len-- > 0) {
1917 unsigned c = *name++ - '0';
1918 if (c > 9)
1919 goto out;
1920 if (n >= (~0U-9)/10)
1921 goto out;
1922 n *= 10;
1923 n += c;
1924 }
1925 return n;
1926out:
1927 return ~0U;
1928}
1929
Miklos Szeredi27932742007-05-08 00:26:17 -07001930#define PROC_FDINFO_MAX 64
1931
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001932static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001933{
1934 struct task_struct *task = get_proc_task(inode);
1935 struct files_struct *files = NULL;
1936 struct file *file;
1937 int fd = proc_fd(inode);
1938
1939 if (task) {
1940 files = get_files_struct(task);
1941 put_task_struct(task);
1942 }
1943 if (files) {
1944 /*
1945 * We are not taking a ref to the file structure, so we must
1946 * hold ->file_lock.
1947 */
1948 spin_lock(&files->file_lock);
1949 file = fcheck_files(files, fd);
1950 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001951 if (path) {
1952 *path = file->f_path;
1953 path_get(&file->f_path);
1954 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001955 if (info)
1956 snprintf(info, PROC_FDINFO_MAX,
1957 "pos:\t%lli\n"
1958 "flags:\t0%o\n",
1959 (long long) file->f_pos,
1960 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001961 spin_unlock(&files->file_lock);
1962 put_files_struct(files);
1963 return 0;
1964 }
1965 spin_unlock(&files->file_lock);
1966 put_files_struct(files);
1967 }
1968 return -ENOENT;
1969}
1970
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001971static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001972{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001973 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001974}
1975
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001976static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1977{
Nick Piggin34286d62011-01-07 17:49:57 +11001978 struct inode *inode;
1979 struct task_struct *task;
1980 int fd;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001981 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001982 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001983
Nick Piggin34286d62011-01-07 17:49:57 +11001984 if (nd && nd->flags & LOOKUP_RCU)
1985 return -ECHILD;
1986
1987 inode = dentry->d_inode;
1988 task = get_proc_task(inode);
1989 fd = proc_fd(inode);
1990
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001991 if (task) {
1992 files = get_files_struct(task);
1993 if (files) {
1994 rcu_read_lock();
1995 if (fcheck_files(files, fd)) {
1996 rcu_read_unlock();
1997 put_files_struct(files);
1998 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001999 rcu_read_lock();
2000 cred = __task_cred(task);
2001 inode->i_uid = cred->euid;
2002 inode->i_gid = cred->egid;
2003 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002004 } else {
2005 inode->i_uid = 0;
2006 inode->i_gid = 0;
2007 }
2008 inode->i_mode &= ~(S_ISUID | S_ISGID);
2009 security_task_to_inode(task, inode);
2010 put_task_struct(task);
2011 return 1;
2012 }
2013 rcu_read_unlock();
2014 put_files_struct(files);
2015 }
2016 put_task_struct(task);
2017 }
2018 d_drop(dentry);
2019 return 0;
2020}
2021
Al Virod72f71e2009-02-20 05:58:47 +00002022static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002023{
2024 .d_revalidate = tid_fd_revalidate,
2025 .d_delete = pid_delete_dentry,
2026};
2027
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002028static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002029 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002030{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002031 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002032 struct file *file;
2033 struct files_struct *files;
2034 struct inode *inode;
2035 struct proc_inode *ei;
2036 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002037
Eric W. Biederman61a28782006-10-02 02:18:49 -07002038 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002039 if (!inode)
2040 goto out;
2041 ei = PROC_I(inode);
2042 ei->fd = fd;
2043 files = get_files_struct(task);
2044 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002045 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002046 inode->i_mode = S_IFLNK;
2047
2048 /*
2049 * We are not taking a ref to the file structure, so we must
2050 * hold ->file_lock.
2051 */
2052 spin_lock(&files->file_lock);
2053 file = fcheck_files(files, fd);
2054 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002055 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04002056 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002057 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04002058 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002059 inode->i_mode |= S_IWUSR | S_IXUSR;
2060 spin_unlock(&files->file_lock);
2061 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002062
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002063 inode->i_op = &proc_pid_link_inode_operations;
2064 inode->i_size = 64;
2065 ei->op.proc_get_link = proc_fd_link;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002066 d_set_d_op(dentry, &tid_fd_dentry_operations);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002067 d_add(dentry, inode);
2068 /* Close the race of the process dying before we return the dentry */
2069 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002070 error = NULL;
2071
2072 out:
2073 return error;
2074out_unlock:
2075 spin_unlock(&files->file_lock);
2076 put_files_struct(files);
2077out_iput:
2078 iput(inode);
2079 goto out;
2080}
2081
Miklos Szeredi27932742007-05-08 00:26:17 -07002082static struct dentry *proc_lookupfd_common(struct inode *dir,
2083 struct dentry *dentry,
2084 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002085{
2086 struct task_struct *task = get_proc_task(dir);
2087 unsigned fd = name_to_int(dentry);
2088 struct dentry *result = ERR_PTR(-ENOENT);
2089
2090 if (!task)
2091 goto out_no_task;
2092 if (fd == ~0U)
2093 goto out;
2094
Miklos Szeredi27932742007-05-08 00:26:17 -07002095 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002096out:
2097 put_task_struct(task);
2098out_no_task:
2099 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002100}
2101
Miklos Szeredi27932742007-05-08 00:26:17 -07002102static int proc_readfd_common(struct file * filp, void * dirent,
2103 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002105 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07002106 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002107 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07002108 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct files_struct * files;
2111
2112 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002113 if (!p)
2114 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 fd = filp->f_pos;
2118 switch (fd) {
2119 case 0:
2120 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
2121 goto out;
2122 filp->f_pos++;
2123 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07002124 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
2126 goto out;
2127 filp->f_pos++;
2128 default:
2129 files = get_files_struct(p);
2130 if (!files)
2131 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002132 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04002134 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07002136 char name[PROC_NUMBUF];
2137 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 if (!fcheck_files(files, fd))
2140 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07002141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Miklos Szeredi27932742007-05-08 00:26:17 -07002143 len = snprintf(name, sizeof(name), "%d", fd);
2144 if (proc_fill_cache(filp, dirent, filldir,
2145 name, len, instantiate,
2146 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07002147 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 break;
2149 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002150 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07002152 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 put_files_struct(files);
2154 }
2155out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002156 put_task_struct(p);
2157out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return retval;
2159}
2160
Miklos Szeredi27932742007-05-08 00:26:17 -07002161static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2162 struct nameidata *nd)
2163{
2164 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2165}
2166
2167static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2168{
2169 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2170}
2171
2172static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2173 size_t len, loff_t *ppos)
2174{
2175 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002176 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002177 if (!err)
2178 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2179 return err;
2180}
2181
2182static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002183 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002184 .read = proc_fdinfo_read,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002185 .llseek = no_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002186};
2187
Arjan van de Ven00977a52007-02-12 00:55:34 -08002188static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 .read = generic_read_dir,
2190 .readdir = proc_readfd,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002191 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192};
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002195 * /proc/pid/fd needs a special permission handler so that a process can still
2196 * access /proc/self/fd after it has executed a setuid().
2197 */
Nick Pigginb74c79e2011-01-07 17:49:58 +11002198static int proc_fd_permission(struct inode *inode, int mask, unsigned int flags)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002199{
2200 int rv;
2201
Nick Pigginb74c79e2011-01-07 17:49:58 +11002202 if (flags & IPERM_FLAG_RCU)
2203 return -ECHILD;
2204 rv = generic_permission(inode, mask, flags, NULL);
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002205 if (rv == 0)
2206 return 0;
2207 if (task_pid(current) == proc_pid(inode))
2208 rv = 0;
2209 return rv;
2210}
2211
2212/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 * proc directories can do almost nothing..
2214 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002215static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002217 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002218 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219};
2220
Miklos Szeredi27932742007-05-08 00:26:17 -07002221static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2222 struct dentry *dentry, struct task_struct *task, const void *ptr)
2223{
2224 unsigned fd = *(unsigned *)ptr;
2225 struct inode *inode;
2226 struct proc_inode *ei;
2227 struct dentry *error = ERR_PTR(-ENOENT);
2228
2229 inode = proc_pid_make_inode(dir->i_sb, task);
2230 if (!inode)
2231 goto out;
2232 ei = PROC_I(inode);
2233 ei->fd = fd;
2234 inode->i_mode = S_IFREG | S_IRUSR;
2235 inode->i_fop = &proc_fdinfo_file_operations;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002236 d_set_d_op(dentry, &tid_fd_dentry_operations);
Miklos Szeredi27932742007-05-08 00:26:17 -07002237 d_add(dentry, inode);
2238 /* Close the race of the process dying before we return the dentry */
2239 if (tid_fd_revalidate(dentry, NULL))
2240 error = NULL;
2241
2242 out:
2243 return error;
2244}
2245
2246static struct dentry *proc_lookupfdinfo(struct inode *dir,
2247 struct dentry *dentry,
2248 struct nameidata *nd)
2249{
2250 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2251}
2252
2253static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2254{
2255 return proc_readfd_common(filp, dirent, filldir,
2256 proc_fdinfo_instantiate);
2257}
2258
2259static const struct file_operations proc_fdinfo_operations = {
2260 .read = generic_read_dir,
2261 .readdir = proc_readfdinfo,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002262 .llseek = default_llseek,
Miklos Szeredi27932742007-05-08 00:26:17 -07002263};
2264
2265/*
2266 * proc directories can do almost nothing..
2267 */
2268static const struct inode_operations proc_fdinfo_inode_operations = {
2269 .lookup = proc_lookupfdinfo,
2270 .setattr = proc_setattr,
2271};
2272
2273
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002274static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002275 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002276{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002277 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002278 struct inode *inode;
2279 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002280 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002281
Eric W. Biederman61a28782006-10-02 02:18:49 -07002282 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002283 if (!inode)
2284 goto out;
2285
2286 ei = PROC_I(inode);
2287 inode->i_mode = p->mode;
2288 if (S_ISDIR(inode->i_mode))
2289 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2290 if (p->iop)
2291 inode->i_op = p->iop;
2292 if (p->fop)
2293 inode->i_fop = p->fop;
2294 ei->op = p->op;
Nick Pigginfb045ad2011-01-07 17:49:55 +11002295 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002296 d_add(dentry, inode);
2297 /* Close the race of the process dying before we return the dentry */
2298 if (pid_revalidate(dentry, NULL))
2299 error = NULL;
2300out:
2301 return error;
2302}
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304static struct dentry *proc_pident_lookup(struct inode *dir,
2305 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002306 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002307 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002309 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002310 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002311 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002313 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Eric W. Biederman99f89552006-06-26 00:25:55 -07002315 if (!task)
2316 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002318 /*
2319 * Yes, it does not scale. And it should not. Don't add
2320 * new entries into /proc/<tgid>/ without very good reasons.
2321 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002322 last = &ents[nents - 1];
2323 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (p->len != dentry->d_name.len)
2325 continue;
2326 if (!memcmp(dentry->d_name.name, p->name, p->len))
2327 break;
2328 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002329 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 goto out;
2331
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002332 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002334 put_task_struct(task);
2335out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002336 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Eric Dumazetc5141e62007-05-08 00:26:15 -07002339static int proc_pident_fill_cache(struct file *filp, void *dirent,
2340 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002341{
2342 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2343 proc_pident_instantiate, task, p);
2344}
2345
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002346static int proc_pident_readdir(struct file *filp,
2347 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002348 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002349{
2350 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002351 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002352 struct inode *inode = dentry->d_inode;
2353 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002354 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002355 ino_t ino;
2356 int ret;
2357
2358 ret = -ENOENT;
2359 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002360 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002361
2362 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002363 i = filp->f_pos;
2364 switch (i) {
2365 case 0:
2366 ino = inode->i_ino;
2367 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2368 goto out;
2369 i++;
2370 filp->f_pos++;
2371 /* fall through */
2372 case 1:
2373 ino = parent_ino(dentry);
2374 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2375 goto out;
2376 i++;
2377 filp->f_pos++;
2378 /* fall through */
2379 default:
2380 i -= 2;
2381 if (i >= nents) {
2382 ret = 1;
2383 goto out;
2384 }
2385 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002386 last = &ents[nents - 1];
2387 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002388 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002389 goto out;
2390 filp->f_pos++;
2391 p++;
2392 }
2393 }
2394
2395 ret = 1;
2396out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002397 put_task_struct(task);
2398out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400}
2401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002403static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2404 size_t count, loff_t *ppos)
2405{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002406 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002407 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002408 ssize_t length;
2409 struct task_struct *task = get_proc_task(inode);
2410
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002411 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002412 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002413
2414 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002415 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002416 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002417 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002418 if (length > 0)
2419 length = simple_read_from_buffer(buf, count, ppos, p, length);
2420 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002421 return length;
2422}
2423
2424static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2425 size_t count, loff_t *ppos)
2426{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002427 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002428 char *page;
2429 ssize_t length;
2430 struct task_struct *task = get_proc_task(inode);
2431
2432 length = -ESRCH;
2433 if (!task)
2434 goto out_no_task;
2435 if (count > PAGE_SIZE)
2436 count = PAGE_SIZE;
2437
2438 /* No partial writes. */
2439 length = -EINVAL;
2440 if (*ppos != 0)
2441 goto out;
2442
2443 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002444 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002445 if (!page)
2446 goto out;
2447
2448 length = -EFAULT;
2449 if (copy_from_user(page, buf, count))
2450 goto out_free;
2451
David Howells107db7c2009-05-08 13:55:27 +01002452 /* Guard against adverse ptrace interaction */
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002453 length = mutex_lock_interruptible(&task->signal->cred_guard_mutex);
David Howells107db7c2009-05-08 13:55:27 +01002454 if (length < 0)
2455 goto out_free;
2456
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002457 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002458 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002459 (void*)page, count);
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -07002460 mutex_unlock(&task->signal->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002461out_free:
2462 free_page((unsigned long) page);
2463out:
2464 put_task_struct(task);
2465out_no_task:
2466 return length;
2467}
2468
Arjan van de Ven00977a52007-02-12 00:55:34 -08002469static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002470 .read = proc_pid_attr_read,
2471 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002472 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002473};
2474
Eric Dumazetc5141e62007-05-08 00:26:15 -07002475static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002476 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2477 REG("prev", S_IRUGO, proc_pid_attr_operations),
2478 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2479 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2480 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2481 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002482};
2483
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002484static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 void * dirent, filldir_t filldir)
2486{
2487 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002488 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Arjan van de Ven00977a52007-02-12 00:55:34 -08002491static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002493 .readdir = proc_attr_dir_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002494 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495};
2496
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002497static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct dentry *dentry, struct nameidata *nd)
2499{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002500 return proc_pident_lookup(dir, dentry,
2501 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002504static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002505 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002506 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002507 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508};
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510#endif
2511
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002512#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002513static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2514 size_t count, loff_t *ppos)
2515{
2516 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2517 struct mm_struct *mm;
2518 char buffer[PROC_NUMBUF];
2519 size_t len;
2520 int ret;
2521
2522 if (!task)
2523 return -ESRCH;
2524
2525 ret = 0;
2526 mm = get_task_mm(task);
2527 if (mm) {
2528 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2529 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2530 MMF_DUMP_FILTER_SHIFT));
2531 mmput(mm);
2532 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2533 }
2534
2535 put_task_struct(task);
2536
2537 return ret;
2538}
2539
2540static ssize_t proc_coredump_filter_write(struct file *file,
2541 const char __user *buf,
2542 size_t count,
2543 loff_t *ppos)
2544{
2545 struct task_struct *task;
2546 struct mm_struct *mm;
2547 char buffer[PROC_NUMBUF], *end;
2548 unsigned int val;
2549 int ret;
2550 int i;
2551 unsigned long mask;
2552
2553 ret = -EFAULT;
2554 memset(buffer, 0, sizeof(buffer));
2555 if (count > sizeof(buffer) - 1)
2556 count = sizeof(buffer) - 1;
2557 if (copy_from_user(buffer, buf, count))
2558 goto out_no_task;
2559
2560 ret = -EINVAL;
2561 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2562 if (*end == '\n')
2563 end++;
2564 if (end - buffer == 0)
2565 goto out_no_task;
2566
2567 ret = -ESRCH;
2568 task = get_proc_task(file->f_dentry->d_inode);
2569 if (!task)
2570 goto out_no_task;
2571
2572 ret = end - buffer;
2573 mm = get_task_mm(task);
2574 if (!mm)
2575 goto out_no_mm;
2576
2577 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2578 if (val & mask)
2579 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2580 else
2581 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2582 }
2583
2584 mmput(mm);
2585 out_no_mm:
2586 put_task_struct(task);
2587 out_no_task:
2588 return ret;
2589}
2590
2591static const struct file_operations proc_coredump_filter_operations = {
2592 .read = proc_coredump_filter_read,
2593 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002594 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002595};
2596#endif
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598/*
2599 * /proc/self:
2600 */
2601static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2602 int buflen)
2603{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002604 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002605 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002606 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002607 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002608 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002609 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return vfs_readlink(dentry,buffer,buflen,tmp);
2611}
2612
Al Viro008b1502005-08-20 00:17:39 +01002613static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002615 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002616 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002617 char *name = ERR_PTR(-ENOENT);
2618 if (tgid) {
2619 name = __getname();
2620 if (!name)
2621 name = ERR_PTR(-ENOMEM);
2622 else
2623 sprintf(name, "%d", tgid);
2624 }
2625 nd_set_link(nd, name);
2626 return NULL;
2627}
2628
2629static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2630 void *cookie)
2631{
2632 char *s = nd_get_link(nd);
2633 if (!IS_ERR(s))
2634 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002635}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002637static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 .readlink = proc_self_readlink,
2639 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002640 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641};
2642
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002643/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002644 * proc base
2645 *
2646 * These are the directory entries in the root directory of /proc
2647 * that properly belong to the /proc filesystem, as they describe
2648 * describe something that is process related.
2649 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002650static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002651 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002652 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002653};
2654
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002655static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002656 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002657{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002658 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002659 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002660 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002661 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002662
2663 /* Allocate the inode */
2664 error = ERR_PTR(-ENOMEM);
2665 inode = new_inode(dir->i_sb);
2666 if (!inode)
2667 goto out;
2668
2669 /* Initialize the inode */
2670 ei = PROC_I(inode);
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002671 inode->i_ino = get_next_ino();
Eric W. Biederman801199c2006-10-02 02:18:48 -07002672 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002673
2674 /*
2675 * grab the reference to the task.
2676 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002677 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002678 if (!ei->pid)
2679 goto out_iput;
2680
Eric W. Biederman801199c2006-10-02 02:18:48 -07002681 inode->i_mode = p->mode;
2682 if (S_ISDIR(inode->i_mode))
2683 inode->i_nlink = 2;
2684 if (S_ISLNK(inode->i_mode))
2685 inode->i_size = 64;
2686 if (p->iop)
2687 inode->i_op = p->iop;
2688 if (p->fop)
2689 inode->i_fop = p->fop;
2690 ei->op = p->op;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002691 d_add(dentry, inode);
2692 error = NULL;
2693out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002694 return error;
2695out_iput:
2696 iput(inode);
2697 goto out;
2698}
2699
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002700static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2701{
2702 struct dentry *error;
2703 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002704 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002705
2706 error = ERR_PTR(-ENOENT);
2707
2708 if (!task)
2709 goto out_no_task;
2710
2711 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002712 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2713 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002714 if (p->len != dentry->d_name.len)
2715 continue;
2716 if (!memcmp(dentry->d_name.name, p->name, p->len))
2717 break;
2718 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002719 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002720 goto out;
2721
2722 error = proc_base_instantiate(dir, dentry, task, p);
2723
2724out:
2725 put_task_struct(task);
2726out_no_task:
2727 return error;
2728}
2729
Eric Dumazetc5141e62007-05-08 00:26:15 -07002730static int proc_base_fill_cache(struct file *filp, void *dirent,
2731 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002732{
2733 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2734 proc_base_instantiate, task, p);
2735}
2736
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002737#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002738static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002739{
Andrea Righi940389b2008-07-28 00:48:12 +02002740 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002741 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002742
Andrea Righi59954772008-07-27 17:29:15 +02002743 if (whole && lock_task_sighand(task, &flags)) {
2744 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002745
Andrea Righi59954772008-07-27 17:29:15 +02002746 task_io_accounting_add(&acct, &task->signal->ioac);
2747 while_each_thread(task, t)
2748 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002749
Andrea Righi59954772008-07-27 17:29:15 +02002750 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002751 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002752 return sprintf(buffer,
2753 "rchar: %llu\n"
2754 "wchar: %llu\n"
2755 "syscr: %llu\n"
2756 "syscw: %llu\n"
2757 "read_bytes: %llu\n"
2758 "write_bytes: %llu\n"
2759 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002760 (unsigned long long)acct.rchar,
2761 (unsigned long long)acct.wchar,
2762 (unsigned long long)acct.syscr,
2763 (unsigned long long)acct.syscw,
2764 (unsigned long long)acct.read_bytes,
2765 (unsigned long long)acct.write_bytes,
2766 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002767}
Andrea Righi297c5d92008-07-25 01:48:49 -07002768
2769static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2770{
2771 return do_io_accounting(task, buffer, 0);
2772}
2773
2774static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2775{
2776 return do_io_accounting(task, buffer, 1);
2777}
2778#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002779
Kees Cook47830722008-10-06 03:11:58 +04002780static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2781 struct pid *pid, struct task_struct *task)
2782{
2783 seq_printf(m, "%08x\n", task->personality);
2784 return 0;
2785}
2786
Eric W. Biederman801199c2006-10-02 02:18:48 -07002787/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002788 * Thread groups
2789 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002790static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002791static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002792
Eric Dumazetc5141e62007-05-08 00:26:15 -07002793static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002794 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2795 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2796 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002797#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002798 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002799#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002800 REG("environ", S_IRUSR, proc_environ_operations),
2801 INF("auxv", S_IRUSR, proc_pid_auxv),
2802 ONE("status", S_IRUGO, proc_pid_status),
2803 ONE("personality", S_IRUSR, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07002804 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002805#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002806 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002807#endif
Mike Galbraith5091faa2010-11-30 14:18:03 +01002808#ifdef CONFIG_SCHED_AUTOGROUP
2809 REG("autogroup", S_IRUGO|S_IWUSR, proc_pid_sched_autogroup_operations),
2810#endif
john stultz4614a696b2009-12-14 18:00:05 -08002811 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002812#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002813 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002814#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002815 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2816 ONE("stat", S_IRUGO, proc_tgid_stat),
2817 ONE("statm", S_IRUGO, proc_pid_statm),
2818 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002819#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002820 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002821#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002822 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2823 LNK("cwd", proc_cwd_link),
2824 LNK("root", proc_root_link),
2825 LNK("exe", proc_exe_link),
2826 REG("mounts", S_IRUGO, proc_mounts_operations),
2827 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2828 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002829#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002830 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2831 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05002832 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002833#endif
2834#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002835 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002836#endif
2837#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002838 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002839#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002840#ifdef CONFIG_STACKTRACE
2841 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002842#endif
2843#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002844 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002845#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002846#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002847 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002848#endif
Paul Menage8793d852007-10-18 23:39:39 -07002849#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002850 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002851#endif
Paul Menagea4243162007-10-18 23:39:35 -07002852#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002853 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002854#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002855 INF("oom_score", S_IRUGO, proc_oom_score),
2856 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f42010-08-09 17:19:46 -07002857 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002858#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002859 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2860 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002861#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002862#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002863 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002864#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002865#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002866 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002867#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002868#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002869 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002870#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002871};
2872
2873static int proc_tgid_base_readdir(struct file * filp,
2874 void * dirent, filldir_t filldir)
2875{
2876 return proc_pident_readdir(filp,dirent,filldir,
2877 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2878}
2879
Arjan van de Ven00977a52007-02-12 00:55:34 -08002880static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002881 .read = generic_read_dir,
2882 .readdir = proc_tgid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002883 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002884};
2885
2886static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002887 return proc_pident_lookup(dir, dentry,
2888 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002889}
2890
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002891static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002892 .lookup = proc_tgid_base_lookup,
2893 .getattr = pid_getattr,
2894 .setattr = proc_setattr,
2895};
2896
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002897static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002899 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002900 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002901 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Eric W. Biederman48e64842006-06-26 00:25:48 -07002903 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002904 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2905 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002906 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002907 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002908 d_drop(dentry);
2909 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Eric W. Biederman48e64842006-06-26 00:25:48 -07002912 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002913 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2914 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002915 if (!leader)
2916 goto out;
2917
2918 name.name = "task";
2919 name.len = strlen(name.name);
2920 dir = d_hash_and_lookup(leader, &name);
2921 if (!dir)
2922 goto out_put_leader;
2923
2924 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002925 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002926 dentry = d_hash_and_lookup(dir, &name);
2927 if (dentry) {
2928 shrink_dcache_parent(dentry);
2929 d_drop(dentry);
2930 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002932
2933 dput(dir);
2934out_put_leader:
2935 dput(leader);
2936out:
2937 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939
Randy Dunlap0895e912007-10-21 21:00:10 -07002940/**
2941 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2942 * @task: task that should be flushed.
2943 *
2944 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002945 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002946 * in. This call is supposed to do all of this job.
2947 *
2948 * Looks in the dcache for
2949 * /proc/@pid
2950 * /proc/@tgid/task/@pid
2951 * if either directory is present flushes it and all of it'ts children
2952 * from the dcache.
2953 *
2954 * It is safe and reasonable to cache /proc entries for a task until
2955 * that task exits. After that they just clog up the dcache with
2956 * useless entries, possibly causing useful dcache entries to be
2957 * flushed instead. This routine is proved to flush those useless
2958 * dcache entries at process exit time.
2959 *
2960 * NOTE: This routine is just an optimization so it does not guarantee
2961 * that no dcache entries will exist at process exit time it
2962 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002963 */
2964
2965void proc_flush_task(struct task_struct *task)
2966{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002967 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002968 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002969 struct upid *upid;
2970
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002971 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002972 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002973
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002974 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002975 upid = &pid->numbers[i];
2976 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002977 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002978 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002979
2980 upid = &pid->numbers[pid->level];
2981 if (upid->nr == 1)
2982 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002983}
2984
Adrian Bunk9711ef92006-12-06 20:38:31 -08002985static struct dentry *proc_pid_instantiate(struct inode *dir,
2986 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002987 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002988{
2989 struct dentry *error = ERR_PTR(-ENOENT);
2990 struct inode *inode;
2991
Eric W. Biederman61a28782006-10-02 02:18:49 -07002992 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002993 if (!inode)
2994 goto out;
2995
2996 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2997 inode->i_op = &proc_tgid_base_inode_operations;
2998 inode->i_fop = &proc_tgid_base_operations;
2999 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003000
3001 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
3002 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003003
Nick Pigginfb045ad2011-01-07 17:49:55 +11003004 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003005
3006 d_add(dentry, inode);
3007 /* Close the race of the process dying before we return the dentry */
3008 if (pid_revalidate(dentry, NULL))
3009 error = NULL;
3010out:
3011 return error;
3012}
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3015{
Dan Carpenter73d36462010-05-26 14:43:25 -07003016 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003019 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Eric W. Biederman801199c2006-10-02 02:18:48 -07003021 result = proc_base_lookup(dir, dentry);
3022 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
3023 goto out;
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 tgid = name_to_int(dentry);
3026 if (tgid == ~0U)
3027 goto out;
3028
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003029 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07003030 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003031 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 if (task)
3033 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07003034 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 if (!task)
3036 goto out;
3037
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003038 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07003039 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07003041 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003045 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003046 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003048struct tgid_iter {
3049 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003050 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003051};
3052static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
3053{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003054 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003056 if (iter.task)
3057 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003058 rcu_read_lock();
3059retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003060 iter.task = NULL;
3061 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003062 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003063 iter.tgid = pid_nr_ns(pid, ns);
3064 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003065 /* What we to know is if the pid we have find is the
3066 * pid of a thread_group_leader. Testing for task
3067 * being a thread_group_leader is the obvious thing
3068 * todo but there is a window when it fails, due to
3069 * the pid transfer logic in de_thread.
3070 *
3071 * So we perform the straight forward test of seeing
3072 * if the pid we have found is the pid of a thread
3073 * group leader, and don't worry if the task we have
3074 * found doesn't happen to be a thread group leader.
3075 * As we don't care in the case of readdir.
3076 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003077 if (!iter.task || !has_group_leader_pid(iter.task)) {
3078 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003079 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003080 }
3081 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07003083 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003084 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003087#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Eric W. Biederman61a28782006-10-02 02:18:49 -07003089static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003090 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07003091{
3092 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003093 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003094 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003095 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003096}
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098/* for the /proc/ directory itself, after non-process stuff has been done */
3099int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
3100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003102 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003103 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003104 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Eric W. Biederman61a28782006-10-02 02:18:49 -07003106 if (!reaper)
3107 goto out_no_task;
3108
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003109 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07003110 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07003111 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07003112 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
3114
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003115 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08003116 iter.task = NULL;
3117 iter.tgid = filp->f_pos - TGID_OFFSET;
3118 for (iter = next_tgid(ns, iter);
3119 iter.task;
3120 iter.tgid += 1, iter = next_tgid(ns, iter)) {
3121 filp->f_pos = iter.tgid + TGID_OFFSET;
3122 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
3123 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07003127 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
3128out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07003129 put_task_struct(reaper);
3130out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return 0;
3132}
3133
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003134/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003135 * Tasks
3136 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07003137static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003138 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07003139 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003140 REG("environ", S_IRUSR, proc_environ_operations),
3141 INF("auxv", S_IRUSR, proc_pid_auxv),
3142 ONE("status", S_IRUGO, proc_pid_status),
3143 ONE("personality", S_IRUSR, proc_pid_personality),
Jiri Olsa3036e7b2010-09-30 15:15:33 -07003144 INF("limits", S_IRUGO, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003145#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003146 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003147#endif
john stultz4614a696b2009-12-14 18:00:05 -08003148 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003149#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003150 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003151#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003152 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3153 ONE("stat", S_IRUGO, proc_tid_stat),
3154 ONE("statm", S_IRUGO, proc_pid_statm),
3155 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003156#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003157 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003158#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003159 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3160 LNK("cwd", proc_cwd_link),
3161 LNK("root", proc_root_link),
3162 LNK("exe", proc_exe_link),
3163 REG("mounts", S_IRUGO, proc_mounts_operations),
3164 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003165#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003166 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3167 REG("smaps", S_IRUGO, proc_smaps_operations),
Al Viroca6b0bf2011-02-15 22:04:37 -05003168 REG("pagemap", S_IRUGO, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003169#endif
3170#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003171 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003172#endif
3173#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003174 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003175#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003176#ifdef CONFIG_STACKTRACE
3177 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003178#endif
3179#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003180 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003181#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003182#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003183 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003184#endif
Paul Menage8793d852007-10-18 23:39:39 -07003185#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003186 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003187#endif
Paul Menagea4243162007-10-18 23:39:35 -07003188#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003189 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003190#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003191 INF("oom_score", S_IRUGO, proc_oom_score),
3192 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f42010-08-09 17:19:46 -07003193 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003194#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003195 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
Al Viro26ec3c62011-02-15 21:24:05 -05003196 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003197#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003198#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003199 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003200#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003201#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003202 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003203#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003204};
3205
3206static int proc_tid_base_readdir(struct file * filp,
3207 void * dirent, filldir_t filldir)
3208{
3209 return proc_pident_readdir(filp,dirent,filldir,
3210 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3211}
3212
3213static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003214 return proc_pident_lookup(dir, dentry,
3215 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003216}
3217
Arjan van de Ven00977a52007-02-12 00:55:34 -08003218static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003219 .read = generic_read_dir,
3220 .readdir = proc_tid_base_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003221 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003222};
3223
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003224static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003225 .lookup = proc_tid_base_lookup,
3226 .getattr = pid_getattr,
3227 .setattr = proc_setattr,
3228};
3229
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003230static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003231 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003232{
3233 struct dentry *error = ERR_PTR(-ENOENT);
3234 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003235 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003236
3237 if (!inode)
3238 goto out;
3239 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3240 inode->i_op = &proc_tid_base_inode_operations;
3241 inode->i_fop = &proc_tid_base_operations;
3242 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003243
3244 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3245 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003246
Nick Pigginfb045ad2011-01-07 17:49:55 +11003247 d_set_d_op(dentry, &pid_dentry_operations);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003248
3249 d_add(dentry, inode);
3250 /* Close the race of the process dying before we return the dentry */
3251 if (pid_revalidate(dentry, NULL))
3252 error = NULL;
3253out:
3254 return error;
3255}
3256
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003257static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3258{
3259 struct dentry *result = ERR_PTR(-ENOENT);
3260 struct task_struct *task;
3261 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003262 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003263 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003264
3265 if (!leader)
3266 goto out_no_task;
3267
3268 tid = name_to_int(dentry);
3269 if (tid == ~0U)
3270 goto out;
3271
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003272 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003273 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003274 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003275 if (task)
3276 get_task_struct(task);
3277 rcu_read_unlock();
3278 if (!task)
3279 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003280 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003281 goto out_drop_task;
3282
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003283 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003284out_drop_task:
3285 put_task_struct(task);
3286out:
3287 put_task_struct(leader);
3288out_no_task:
3289 return result;
3290}
3291
3292/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003293 * Find the first tid of a thread group to return to user space.
3294 *
3295 * Usually this is just the thread group leader, but if the users
3296 * buffer was too small or there was a seek into the middle of the
3297 * directory we have more work todo.
3298 *
3299 * In the case of a short read we start with find_task_by_pid.
3300 *
3301 * In the case of a seek we start with the leader and walk nr
3302 * threads past it.
3303 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003304static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003305 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003306{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003307 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003308
Eric W. Biedermancc288732006-06-26 00:26:01 -07003309 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003310 /* Attempt to start with the pid of a thread */
3311 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003312 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003313 if (pos && (pos->group_leader == leader))
3314 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003315 }
3316
3317 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003318 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003319 if (nr && nr >= get_nr_threads(leader))
3320 goto out;
3321
3322 /* If we haven't found our starting place yet start
3323 * with the leader and walk nr threads forward.
3324 */
3325 for (pos = leader; nr > 0; --nr) {
3326 pos = next_thread(pos);
3327 if (pos == leader) {
3328 pos = NULL;
3329 goto out;
3330 }
3331 }
3332found:
3333 get_task_struct(pos);
3334out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003335 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003336 return pos;
3337}
3338
3339/*
3340 * Find the next thread in the thread list.
3341 * Return NULL if there is an error or no next thread.
3342 *
3343 * The reference to the input task_struct is released.
3344 */
3345static struct task_struct *next_tid(struct task_struct *start)
3346{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003347 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003348 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003349 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003350 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003351 if (thread_group_leader(pos))
3352 pos = NULL;
3353 else
3354 get_task_struct(pos);
3355 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003356 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003357 put_task_struct(start);
3358 return pos;
3359}
3360
Eric W. Biederman61a28782006-10-02 02:18:49 -07003361static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3362 struct task_struct *task, int tid)
3363{
3364 char name[PROC_NUMBUF];
3365 int len = snprintf(name, sizeof(name), "%d", tid);
3366 return proc_fill_cache(filp, dirent, filldir, name, len,
3367 proc_task_instantiate, task, NULL);
3368}
3369
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370/* for the /proc/TGID/task/ directories */
3371static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3372{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003373 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003375 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003376 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 int retval = -ENOENT;
3378 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003379 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003380 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003382 task = get_proc_task(inode);
3383 if (!task)
3384 goto out_no_task;
3385 rcu_read_lock();
3386 if (pid_alive(task)) {
3387 leader = task->group_leader;
3388 get_task_struct(leader);
3389 }
3390 rcu_read_unlock();
3391 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003392 if (!leader)
3393 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 retval = 0;
3395
Linus Torvaldsee568b22009-03-17 10:02:35 -07003396 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 case 0:
3398 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003399 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003401 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /* fall through */
3403 case 1:
3404 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003405 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003407 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 /* fall through */
3409 }
3410
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003411 /* f_version caches the tgid value that the last readdir call couldn't
3412 * return. lseek aka telldir automagically resets f_version to 0.
3413 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003414 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003415 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003416 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003417 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003418 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003419 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003420 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003421 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003422 /* returning this tgid failed, save it as the first
3423 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003424 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003425 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 }
3429out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003430 put_task_struct(leader);
3431out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 return retval;
3433}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003434
3435static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3436{
3437 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003438 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003439 generic_fillattr(inode, stat);
3440
Eric W. Biederman99f89552006-06-26 00:25:55 -07003441 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003442 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003443 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003444 }
3445
3446 return 0;
3447}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003448
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003449static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003450 .lookup = proc_task_lookup,
3451 .getattr = proc_task_getattr,
3452 .setattr = proc_setattr,
3453};
3454
Arjan van de Ven00977a52007-02-12 00:55:34 -08003455static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003456 .read = generic_read_dir,
3457 .readdir = proc_task_readdir,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003458 .llseek = default_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003459};