blob: e0afd326b6881b7ff2506e1d9041ed12c7ce1e0d [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>
Dipankar Sarmab8359962005-09-09 13:04:14 -070066#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030068#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070069#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070070#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/mount.h>
72#include <linux/security.h>
73#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070074#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070075#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/cpuset.h>
77#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050078#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070079#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070080#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070081#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070082#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include "internal.h"
84
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070085/* NOTE:
86 * Implementing inode permission operations in /proc is almost
87 * certainly an error. Permission checks need to happen during
88 * each system call not at open time. The reason is that most of
89 * what we wish to check for permissions in /proc varies at runtime.
90 *
91 * The classic example of a problem is opening file descriptors
92 * in /proc for a task before it execs a suid executable.
93 */
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070097 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080099 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800100 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
Eric W. Biederman61a28782006-10-02 02:18:49 -0700104#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700105 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700106 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700107 .mode = MODE, \
108 .iop = IOP, \
109 .fop = FOP, \
110 .op = OP, \
111}
112
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300113#define DIR(NAME, MODE, iops, fops) \
114 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
115#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300118 { .proc_get_link = get_link } )
119#define REG(NAME, MODE, fops) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
121#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700122 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300124 { .proc_read = read } )
125#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300128 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Vegard Nossumaed54172008-06-05 22:46:53 -0700130/*
131 * Count the number of hardlinks for the pid_entry table, excluding the .
132 * and .. links.
133 */
134static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
135 unsigned int n)
136{
137 unsigned int i;
138 unsigned int count;
139
140 count = 0;
141 for (i = 0; i < n; ++i) {
142 if (S_ISDIR(entries[i].mode))
143 ++count;
144 }
145
146 return count;
147}
148
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000149static int get_fs_path(struct task_struct *task, struct path *path, bool root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 struct fs_struct *fs;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000152 int result = -ENOENT;
153
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154 task_lock(task);
155 fs = task->fs;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000156 if (fs) {
157 read_lock(&fs->lock);
158 *path = root ? fs->root : fs->pwd;
159 path_get(path);
160 read_unlock(&fs->lock);
161 result = 0;
162 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700163 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000164 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700165}
166
Eric W. Biederman99f89552006-06-26 00:25:55 -0700167static int get_nr_threads(struct task_struct *tsk)
168{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700169 unsigned long flags;
170 int count = 0;
171
172 if (lock_task_sighand(tsk, &flags)) {
173 count = atomic_read(&tsk->signal->count);
174 unlock_task_sighand(tsk, &flags);
175 }
176 return count;
177}
178
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800179static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700180{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700181 struct task_struct *task = get_proc_task(inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700182 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700183
184 if (task) {
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000185 result = get_fs_path(task, path, 0);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186 put_task_struct(task);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return result;
189}
190
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800191static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700193 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700195
196 if (task) {
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000197 result = get_fs_path(task, path, 1);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700198 put_task_struct(task);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return result;
201}
202
Roland McGrath638fa202008-04-29 01:01:38 -0700203/*
204 * Return zero if current may access user memory in @task, -error if not.
205 */
206static int check_mem_permission(struct task_struct *task)
207{
208 /*
209 * A task can always look at itself, in case it chooses
210 * to use system calls instead of load instructions.
211 */
212 if (task == current)
213 return 0;
214
215 /*
216 * If current is actively ptrace'ing, and would also be
217 * permitted to freshly attach with ptrace now, permit it.
218 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700219 if (task_is_stopped_or_traced(task)) {
220 int match;
221 rcu_read_lock();
222 match = (tracehook_tracer_task(task) == current);
223 rcu_read_unlock();
224 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
225 return 0;
226 }
Roland McGrath638fa202008-04-29 01:01:38 -0700227
228 /*
229 * Noone else is allowed.
230 */
231 return -EPERM;
232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Al Viro831830b2008-01-02 14:09:57 +0000234struct mm_struct *mm_for_maps(struct task_struct *task)
235{
236 struct mm_struct *mm = get_task_mm(task);
237 if (!mm)
238 return NULL;
239 down_read(&mm->mmap_sem);
240 task_lock(task);
241 if (task->mm != mm)
242 goto out;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400243 if (task->mm != current->mm &&
244 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
Al Viro831830b2008-01-02 14:09:57 +0000245 goto out;
246 task_unlock(task);
247 return mm;
248out:
249 task_unlock(task);
250 up_read(&mm->mmap_sem);
251 mmput(mm);
252 return NULL;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static int proc_pid_cmdline(struct task_struct *task, char * buffer)
256{
257 int res = 0;
258 unsigned int len;
259 struct mm_struct *mm = get_task_mm(task);
260 if (!mm)
261 goto out;
262 if (!mm->arg_end)
263 goto out_mm; /* Shh! No looking before we're done */
264
265 len = mm->arg_end - mm->arg_start;
266
267 if (len > PAGE_SIZE)
268 len = PAGE_SIZE;
269
270 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
271
272 // If the nul at the end of args has been overwritten, then
273 // assume application is using setproctitle(3).
274 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
275 len = strnlen(buffer, res);
276 if (len < res) {
277 res = len;
278 } else {
279 len = mm->env_end - mm->env_start;
280 if (len > PAGE_SIZE - res)
281 len = PAGE_SIZE - res;
282 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
283 res = strnlen(buffer, res);
284 }
285 }
286out_mm:
287 mmput(mm);
288out:
289 return res;
290}
291
292static int proc_pid_auxv(struct task_struct *task, char *buffer)
293{
294 int res = 0;
295 struct mm_struct *mm = get_task_mm(task);
296 if (mm) {
297 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300298 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300300 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 res = nwords * sizeof(mm->saved_auxv[0]);
302 if (res > PAGE_SIZE)
303 res = PAGE_SIZE;
304 memcpy(buffer, mm->saved_auxv, res);
305 mmput(mm);
306 }
307 return res;
308}
309
310
311#ifdef CONFIG_KALLSYMS
312/*
313 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
314 * Returns the resolved symbol. If that fails, simply return the address.
315 */
316static int proc_pid_wchan(struct task_struct *task, char *buffer)
317{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700318 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700319 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 wchan = get_wchan(task);
322
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700323 if (lookup_symbol_name(wchan, symname) < 0)
324 return sprintf(buffer, "%lu", wchan);
325 else
326 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328#endif /* CONFIG_KALLSYMS */
329
Ken Chen2ec220e2008-11-10 11:26:08 +0300330#ifdef CONFIG_STACKTRACE
331
332#define MAX_STACK_TRACE_DEPTH 64
333
334static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
335 struct pid *pid, struct task_struct *task)
336{
337 struct stack_trace trace;
338 unsigned long *entries;
339 int i;
340
341 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
342 if (!entries)
343 return -ENOMEM;
344
345 trace.nr_entries = 0;
346 trace.max_entries = MAX_STACK_TRACE_DEPTH;
347 trace.entries = entries;
348 trace.skip = 0;
349 save_stack_trace_tsk(task, &trace);
350
351 for (i = 0; i < trace.nr_entries; i++) {
352 seq_printf(m, "[<%p>] %pS\n",
353 (void *)entries[i], (void *)entries[i]);
354 }
355 kfree(entries);
356
357 return 0;
358}
359#endif
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#ifdef CONFIG_SCHEDSTATS
362/*
363 * Provides /proc/PID/schedstat
364 */
365static int proc_pid_schedstat(struct task_struct *task, char *buffer)
366{
Balbir Singh172ba842007-07-09 18:52:00 +0200367 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100368 (unsigned long long)task->se.sum_exec_runtime,
369 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200370 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372#endif
373
Arjan van de Ven97455122008-01-25 21:08:34 +0100374#ifdef CONFIG_LATENCYTOP
375static int lstats_show_proc(struct seq_file *m, void *v)
376{
377 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800378 struct inode *inode = m->private;
379 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100380
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800381 if (!task)
382 return -ESRCH;
383 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100384 for (i = 0; i < 32; i++) {
385 if (task->latency_record[i].backtrace[0]) {
386 int q;
387 seq_printf(m, "%i %li %li ",
388 task->latency_record[i].count,
389 task->latency_record[i].time,
390 task->latency_record[i].max);
391 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Hugh Dickins9c246242008-12-09 13:14:27 -0800392 char sym[KSYM_SYMBOL_LEN];
Arjan van de Ven97455122008-01-25 21:08:34 +0100393 char *c;
394 if (!task->latency_record[i].backtrace[q])
395 break;
396 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
397 break;
398 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
399 c = strchr(sym, '+');
400 if (c)
401 *c = 0;
402 seq_printf(m, "%s ", sym);
403 }
404 seq_printf(m, "\n");
405 }
406
407 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800408 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100409 return 0;
410}
411
412static int lstats_open(struct inode *inode, struct file *file)
413{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800414 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800415}
416
Arjan van de Ven97455122008-01-25 21:08:34 +0100417static ssize_t lstats_write(struct file *file, const char __user *buf,
418 size_t count, loff_t *offs)
419{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800420 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100421
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800422 if (!task)
423 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100424 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800425 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100426
427 return count;
428}
429
430static const struct file_operations proc_lstats_operations = {
431 .open = lstats_open,
432 .read = seq_read,
433 .write = lstats_write,
434 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800435 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100436};
437
438#endif
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/* The badness from the OOM killer */
441unsigned long badness(struct task_struct *p, unsigned long uptime);
442static int proc_oom_score(struct task_struct *task, char *buffer)
443{
444 unsigned long points;
445 struct timespec uptime;
446
447 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700448 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700450 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return sprintf(buffer, "%lu\n", points);
452}
453
Neil Hormand85f50d2007-10-18 23:40:37 -0700454struct limit_names {
455 char *name;
456 char *unit;
457};
458
459static const struct limit_names lnames[RLIM_NLIMITS] = {
460 [RLIMIT_CPU] = {"Max cpu time", "ms"},
461 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
462 [RLIMIT_DATA] = {"Max data size", "bytes"},
463 [RLIMIT_STACK] = {"Max stack size", "bytes"},
464 [RLIMIT_CORE] = {"Max core file size", "bytes"},
465 [RLIMIT_RSS] = {"Max resident set", "bytes"},
466 [RLIMIT_NPROC] = {"Max processes", "processes"},
467 [RLIMIT_NOFILE] = {"Max open files", "files"},
468 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
469 [RLIMIT_AS] = {"Max address space", "bytes"},
470 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
471 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
472 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
473 [RLIMIT_NICE] = {"Max nice priority", NULL},
474 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800475 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700476};
477
478/* Display limits for a process */
479static int proc_pid_limits(struct task_struct *task, char *buffer)
480{
481 unsigned int i;
482 int count = 0;
483 unsigned long flags;
484 char *bufptr = buffer;
485
486 struct rlimit rlim[RLIM_NLIMITS];
487
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400488 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700489 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700490 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
491 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700492
493 /*
494 * print the file header
495 */
496 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
497 "Limit", "Soft Limit", "Hard Limit", "Units");
498
499 for (i = 0; i < RLIM_NLIMITS; i++) {
500 if (rlim[i].rlim_cur == RLIM_INFINITY)
501 count += sprintf(&bufptr[count], "%-25s %-20s ",
502 lnames[i].name, "unlimited");
503 else
504 count += sprintf(&bufptr[count], "%-25s %-20lu ",
505 lnames[i].name, rlim[i].rlim_cur);
506
507 if (rlim[i].rlim_max == RLIM_INFINITY)
508 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
509 else
510 count += sprintf(&bufptr[count], "%-20lu ",
511 rlim[i].rlim_max);
512
513 if (lnames[i].unit)
514 count += sprintf(&bufptr[count], "%-10s\n",
515 lnames[i].unit);
516 else
517 count += sprintf(&bufptr[count], "\n");
518 }
519
520 return count;
521}
522
Roland McGrathebcb6732008-07-25 19:46:00 -0700523#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
524static int proc_pid_syscall(struct task_struct *task, char *buffer)
525{
526 long nr;
527 unsigned long args[6], sp, pc;
528
529 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
530 return sprintf(buffer, "running\n");
531
532 if (nr < 0)
533 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
534
535 return sprintf(buffer,
536 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
537 nr,
538 args[0], args[1], args[2], args[3], args[4], args[5],
539 sp, pc);
540}
541#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/************************************************************************/
544/* Here the fs part begins */
545/************************************************************************/
546
547/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700548static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700550 struct task_struct *task;
551 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700552 /* Allow access to a task's file descriptors if it is us or we
553 * may use ptrace attach to the process and find out that
554 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700555 */
556 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700557 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400558 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700559 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700560 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700561 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700564static int proc_setattr(struct dentry *dentry, struct iattr *attr)
565{
566 int error;
567 struct inode *inode = dentry->d_inode;
568
569 if (attr->ia_valid & ATTR_MODE)
570 return -EPERM;
571
572 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700573 if (!error)
574 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700575 return error;
576}
577
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800578static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700579 .setattr = proc_setattr,
580};
581
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100582static int mounts_open_common(struct inode *inode, struct file *file,
583 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700585 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700586 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800587 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100588 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500589 struct proc_mounts *p;
590 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Eric W. Biederman99f89552006-06-26 00:25:55 -0700592 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700593 rcu_read_lock();
594 nsp = task_nsproxy(task);
595 if (nsp) {
596 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800597 if (ns)
598 get_mnt_ns(ns);
599 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700600 rcu_read_unlock();
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000601 if (ns && get_fs_path(task, &root, 1) == 0)
602 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700603 put_task_struct(task);
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100606 if (!ns)
607 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000608 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100609 goto err_put_ns;
610
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100611 ret = -ENOMEM;
612 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
613 if (!p)
614 goto err_put_path;
615
616 file->private_data = &p->m;
617 ret = seq_open(file, op);
618 if (ret)
619 goto err_free;
620
621 p->m.private = p;
622 p->ns = ns;
623 p->root = root;
624 p->event = ns->event;
625
626 return 0;
627
628 err_free:
629 kfree(p);
630 err_put_path:
631 path_put(&root);
632 err_put_ns:
633 put_mnt_ns(ns);
634 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return ret;
636}
637
638static int mounts_release(struct inode *inode, struct file *file)
639{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100640 struct proc_mounts *p = file->private_data;
641 path_put(&p->root);
642 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return seq_release(inode, file);
644}
645
Al Viro5addc5d2005-11-07 17:15:49 -0500646static unsigned mounts_poll(struct file *file, poll_table *wait)
647{
648 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100649 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500650 unsigned res = 0;
651
652 poll_wait(file, &ns->poll, wait);
653
654 spin_lock(&vfsmount_lock);
655 if (p->event != ns->event) {
656 p->event = ns->event;
657 res = POLLERR;
658 }
659 spin_unlock(&vfsmount_lock);
660
661 return res;
662}
663
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100664static int mounts_open(struct inode *inode, struct file *file)
665{
666 return mounts_open_common(inode, file, &mounts_op);
667}
668
Arjan van de Ven00977a52007-02-12 00:55:34 -0800669static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 .open = mounts_open,
671 .read = seq_read,
672 .llseek = seq_lseek,
673 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500674 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675};
676
Ram Pai2d4d4862008-03-27 13:06:25 +0100677static int mountinfo_open(struct inode *inode, struct file *file)
678{
679 return mounts_open_common(inode, file, &mountinfo_op);
680}
681
682static const struct file_operations proc_mountinfo_operations = {
683 .open = mountinfo_open,
684 .read = seq_read,
685 .llseek = seq_lseek,
686 .release = mounts_release,
687 .poll = mounts_poll,
688};
689
Chuck Leverb4629fe2006-03-20 13:44:12 -0500690static int mountstats_open(struct inode *inode, struct file *file)
691{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100692 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500693}
694
Arjan van de Ven00977a52007-02-12 00:55:34 -0800695static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500696 .open = mountstats_open,
697 .read = seq_read,
698 .llseek = seq_lseek,
699 .release = mounts_release,
700};
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
703
704static ssize_t proc_info_read(struct file * file, char __user * buf,
705 size_t count, loff_t *ppos)
706{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800707 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned long page;
709 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700710 struct task_struct *task = get_proc_task(inode);
711
712 length = -ESRCH;
713 if (!task)
714 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (count > PROC_BLOCK_SIZE)
717 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700718
719 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700720 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700721 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 length = PROC_I(inode)->op.proc_read(task, (char*)page);
724
725 if (length >= 0)
726 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
727 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700728out:
729 put_task_struct(task);
730out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 return length;
732}
733
Arjan van de Ven00977a52007-02-12 00:55:34 -0800734static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 .read = proc_info_read,
736};
737
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800738static int proc_single_show(struct seq_file *m, void *v)
739{
740 struct inode *inode = m->private;
741 struct pid_namespace *ns;
742 struct pid *pid;
743 struct task_struct *task;
744 int ret;
745
746 ns = inode->i_sb->s_fs_info;
747 pid = proc_pid(inode);
748 task = get_pid_task(pid, PIDTYPE_PID);
749 if (!task)
750 return -ESRCH;
751
752 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
753
754 put_task_struct(task);
755 return ret;
756}
757
758static int proc_single_open(struct inode *inode, struct file *filp)
759{
760 int ret;
761 ret = single_open(filp, proc_single_show, NULL);
762 if (!ret) {
763 struct seq_file *m = filp->private_data;
764
765 m->private = inode;
766 }
767 return ret;
768}
769
770static const struct file_operations proc_single_file_operations = {
771 .open = proc_single_open,
772 .read = seq_read,
773 .llseek = seq_lseek,
774 .release = single_release,
775};
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static int mem_open(struct inode* inode, struct file* file)
778{
779 file->private_data = (void*)((long)current->self_exec_id);
780 return 0;
781}
782
783static ssize_t mem_read(struct file * file, char __user * buf,
784 size_t count, loff_t *ppos)
785{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800786 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 char *page;
788 unsigned long src = *ppos;
789 int ret = -ESRCH;
790 struct mm_struct *mm;
791
Eric W. Biederman99f89552006-06-26 00:25:55 -0700792 if (!task)
793 goto out_no_task;
794
Roland McGrath638fa202008-04-29 01:01:38 -0700795 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto out;
797
798 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700799 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!page)
801 goto out;
802
803 ret = 0;
804
805 mm = get_task_mm(task);
806 if (!mm)
807 goto out_free;
808
809 ret = -EIO;
810
811 if (file->private_data != (void*)((long)current->self_exec_id))
812 goto out_put;
813
814 ret = 0;
815
816 while (count > 0) {
817 int this_len, retval;
818
819 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
820 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa202008-04-29 01:01:38 -0700821 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 if (!ret)
823 ret = -EIO;
824 break;
825 }
826
827 if (copy_to_user(buf, page, retval)) {
828 ret = -EFAULT;
829 break;
830 }
831
832 ret += retval;
833 src += retval;
834 buf += retval;
835 count -= retval;
836 }
837 *ppos = src;
838
839out_put:
840 mmput(mm);
841out_free:
842 free_page((unsigned long) page);
843out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700844 put_task_struct(task);
845out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return ret;
847}
848
849#define mem_write NULL
850
851#ifndef mem_write
852/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800853static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 size_t count, loff_t *ppos)
855{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700856 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800858 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 unsigned long dst = *ppos;
860
Eric W. Biederman99f89552006-06-26 00:25:55 -0700861 copied = -ESRCH;
862 if (!task)
863 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Roland McGrath638fa202008-04-29 01:01:38 -0700865 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700866 goto out;
867
868 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700869 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700871 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700873 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 while (count > 0) {
875 int this_len, retval;
876
877 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
878 if (copy_from_user(page, buf, this_len)) {
879 copied = -EFAULT;
880 break;
881 }
882 retval = access_process_vm(task, dst, page, this_len, 1);
883 if (!retval) {
884 if (!copied)
885 copied = -EIO;
886 break;
887 }
888 copied += retval;
889 buf += retval;
890 dst += retval;
891 count -= retval;
892 }
893 *ppos = dst;
894 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700895out:
896 put_task_struct(task);
897out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return copied;
899}
900#endif
901
Matt Mackall85863e42008-02-04 22:29:04 -0800902loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
904 switch (orig) {
905 case 0:
906 file->f_pos = offset;
907 break;
908 case 1:
909 file->f_pos += offset;
910 break;
911 default:
912 return -EINVAL;
913 }
914 force_successful_syscall_return();
915 return file->f_pos;
916}
917
Arjan van de Ven00977a52007-02-12 00:55:34 -0800918static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 .llseek = mem_lseek,
920 .read = mem_read,
921 .write = mem_write,
922 .open = mem_open,
923};
924
James Pearson315e28c2007-10-16 23:30:17 -0700925static ssize_t environ_read(struct file *file, char __user *buf,
926 size_t count, loff_t *ppos)
927{
928 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
929 char *page;
930 unsigned long src = *ppos;
931 int ret = -ESRCH;
932 struct mm_struct *mm;
933
934 if (!task)
935 goto out_no_task;
936
Stephen Smalley006ebb42008-05-19 08:32:49 -0400937 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700938 goto out;
939
940 ret = -ENOMEM;
941 page = (char *)__get_free_page(GFP_TEMPORARY);
942 if (!page)
943 goto out;
944
945 ret = 0;
946
947 mm = get_task_mm(task);
948 if (!mm)
949 goto out_free;
950
951 while (count > 0) {
952 int this_len, retval, max_len;
953
954 this_len = mm->env_end - (mm->env_start + src);
955
956 if (this_len <= 0)
957 break;
958
959 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
960 this_len = (this_len > max_len) ? max_len : this_len;
961
962 retval = access_process_vm(task, (mm->env_start + src),
963 page, this_len, 0);
964
965 if (retval <= 0) {
966 ret = retval;
967 break;
968 }
969
970 if (copy_to_user(buf, page, retval)) {
971 ret = -EFAULT;
972 break;
973 }
974
975 ret += retval;
976 src += retval;
977 buf += retval;
978 count -= retval;
979 }
980 *ppos = src;
981
982 mmput(mm);
983out_free:
984 free_page((unsigned long) page);
985out:
986 put_task_struct(task);
987out_no_task:
988 return ret;
989}
990
991static const struct file_operations proc_environ_operations = {
992 .read = environ_read,
993};
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static ssize_t oom_adjust_read(struct file *file, char __user *buf,
996 size_t count, loff_t *ppos)
997{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800998 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700999 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001001 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Eric W. Biederman99f89552006-06-26 00:25:55 -07001003 if (!task)
1004 return -ESRCH;
1005 oom_adjust = task->oomkilladj;
1006 put_task_struct(task);
1007
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001008 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001009
1010 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1014 size_t count, loff_t *ppos)
1015{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001016 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001017 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int oom_adjust;
1019
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001020 memset(buffer, 0, sizeof(buffer));
1021 if (count > sizeof(buffer) - 1)
1022 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (copy_from_user(buffer, buf, count))
1024 return -EFAULT;
1025 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001026 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1027 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return -EINVAL;
1029 if (*end == '\n')
1030 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001031 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001032 if (!task)
1033 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001034 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1035 put_task_struct(task);
1036 return -EACCES;
1037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001039 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (end - buffer == 0)
1041 return -EIO;
1042 return end - buffer;
1043}
1044
Arjan van de Ven00977a52007-02-12 00:55:34 -08001045static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 .read = oom_adjust_read,
1047 .write = oom_adjust_write,
1048};
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#ifdef CONFIG_AUDITSYSCALL
1051#define TMPBUFLEN 21
1052static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1053 size_t count, loff_t *ppos)
1054{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001055 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001056 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 ssize_t length;
1058 char tmpbuf[TMPBUFLEN];
1059
Eric W. Biederman99f89552006-06-26 00:25:55 -07001060 if (!task)
1061 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001063 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001064 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1066}
1067
1068static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1069 size_t count, loff_t *ppos)
1070{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001071 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 char *page, *tmp;
1073 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 uid_t loginuid;
1075
1076 if (!capable(CAP_AUDIT_CONTROL))
1077 return -EPERM;
1078
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001079 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return -EPERM;
1081
Al Viroe0182902006-05-18 08:28:02 -04001082 if (count >= PAGE_SIZE)
1083 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 if (*ppos != 0) {
1086 /* No partial writes. */
1087 return -EINVAL;
1088 }
Mel Gormane12ba742007-10-16 01:25:52 -07001089 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!page)
1091 return -ENOMEM;
1092 length = -EFAULT;
1093 if (copy_from_user(page, buf, count))
1094 goto out_free_page;
1095
Al Viroe0182902006-05-18 08:28:02 -04001096 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 loginuid = simple_strtoul(page, &tmp, 10);
1098 if (tmp == page) {
1099 length = -EINVAL;
1100 goto out_free_page;
1101
1102 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001103 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (likely(length == 0))
1105 length = count;
1106
1107out_free_page:
1108 free_page((unsigned long) page);
1109 return length;
1110}
1111
Arjan van de Ven00977a52007-02-12 00:55:34 -08001112static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 .read = proc_loginuid_read,
1114 .write = proc_loginuid_write,
1115};
Eric Paris1e0bd752008-03-13 08:15:31 -04001116
1117static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1118 size_t count, loff_t *ppos)
1119{
1120 struct inode * inode = file->f_path.dentry->d_inode;
1121 struct task_struct *task = get_proc_task(inode);
1122 ssize_t length;
1123 char tmpbuf[TMPBUFLEN];
1124
1125 if (!task)
1126 return -ESRCH;
1127 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1128 audit_get_sessionid(task));
1129 put_task_struct(task);
1130 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1131}
1132
1133static const struct file_operations proc_sessionid_operations = {
1134 .read = proc_sessionid_read,
1135};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136#endif
1137
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001138#ifdef CONFIG_FAULT_INJECTION
1139static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1140 size_t count, loff_t *ppos)
1141{
1142 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1143 char buffer[PROC_NUMBUF];
1144 size_t len;
1145 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001146
1147 if (!task)
1148 return -ESRCH;
1149 make_it_fail = task->make_it_fail;
1150 put_task_struct(task);
1151
1152 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001153
1154 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001155}
1156
1157static ssize_t proc_fault_inject_write(struct file * file,
1158 const char __user * buf, size_t count, loff_t *ppos)
1159{
1160 struct task_struct *task;
1161 char buffer[PROC_NUMBUF], *end;
1162 int make_it_fail;
1163
1164 if (!capable(CAP_SYS_RESOURCE))
1165 return -EPERM;
1166 memset(buffer, 0, sizeof(buffer));
1167 if (count > sizeof(buffer) - 1)
1168 count = sizeof(buffer) - 1;
1169 if (copy_from_user(buffer, buf, count))
1170 return -EFAULT;
1171 make_it_fail = simple_strtol(buffer, &end, 0);
1172 if (*end == '\n')
1173 end++;
1174 task = get_proc_task(file->f_dentry->d_inode);
1175 if (!task)
1176 return -ESRCH;
1177 task->make_it_fail = make_it_fail;
1178 put_task_struct(task);
1179 if (end - buffer == 0)
1180 return -EIO;
1181 return end - buffer;
1182}
1183
Arjan van de Ven00977a52007-02-12 00:55:34 -08001184static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001185 .read = proc_fault_inject_read,
1186 .write = proc_fault_inject_write,
1187};
1188#endif
1189
Arjan van de Ven97455122008-01-25 21:08:34 +01001190
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001191#ifdef CONFIG_SCHED_DEBUG
1192/*
1193 * Print out various scheduling related per-task fields:
1194 */
1195static int sched_show(struct seq_file *m, void *v)
1196{
1197 struct inode *inode = m->private;
1198 struct task_struct *p;
1199
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001200 p = get_proc_task(inode);
1201 if (!p)
1202 return -ESRCH;
1203 proc_sched_show_task(p, m);
1204
1205 put_task_struct(p);
1206
1207 return 0;
1208}
1209
1210static ssize_t
1211sched_write(struct file *file, const char __user *buf,
1212 size_t count, loff_t *offset)
1213{
1214 struct inode *inode = file->f_path.dentry->d_inode;
1215 struct task_struct *p;
1216
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001217 p = get_proc_task(inode);
1218 if (!p)
1219 return -ESRCH;
1220 proc_sched_set_task(p);
1221
1222 put_task_struct(p);
1223
1224 return count;
1225}
1226
1227static int sched_open(struct inode *inode, struct file *filp)
1228{
1229 int ret;
1230
1231 ret = single_open(filp, sched_show, NULL);
1232 if (!ret) {
1233 struct seq_file *m = filp->private_data;
1234
1235 m->private = inode;
1236 }
1237 return ret;
1238}
1239
1240static const struct file_operations proc_pid_sched_operations = {
1241 .open = sched_open,
1242 .read = seq_read,
1243 .write = sched_write,
1244 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001245 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001246};
1247
1248#endif
1249
Matt Helsley925d1c42008-04-29 01:01:36 -07001250/*
1251 * We added or removed a vma mapping the executable. The vmas are only mapped
1252 * during exec and are not mapped with the mmap system call.
1253 * Callers must hold down_write() on the mm's mmap_sem for these
1254 */
1255void added_exe_file_vma(struct mm_struct *mm)
1256{
1257 mm->num_exe_file_vmas++;
1258}
1259
1260void removed_exe_file_vma(struct mm_struct *mm)
1261{
1262 mm->num_exe_file_vmas--;
1263 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1264 fput(mm->exe_file);
1265 mm->exe_file = NULL;
1266 }
1267
1268}
1269
1270void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1271{
1272 if (new_exe_file)
1273 get_file(new_exe_file);
1274 if (mm->exe_file)
1275 fput(mm->exe_file);
1276 mm->exe_file = new_exe_file;
1277 mm->num_exe_file_vmas = 0;
1278}
1279
1280struct file *get_mm_exe_file(struct mm_struct *mm)
1281{
1282 struct file *exe_file;
1283
1284 /* We need mmap_sem to protect against races with removal of
1285 * VM_EXECUTABLE vmas */
1286 down_read(&mm->mmap_sem);
1287 exe_file = mm->exe_file;
1288 if (exe_file)
1289 get_file(exe_file);
1290 up_read(&mm->mmap_sem);
1291 return exe_file;
1292}
1293
1294void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1295{
1296 /* It's safe to write the exe_file pointer without exe_file_lock because
1297 * this is called during fork when the task is not yet in /proc */
1298 newmm->exe_file = get_mm_exe_file(oldmm);
1299}
1300
1301static int proc_exe_link(struct inode *inode, struct path *exe_path)
1302{
1303 struct task_struct *task;
1304 struct mm_struct *mm;
1305 struct file *exe_file;
1306
1307 task = get_proc_task(inode);
1308 if (!task)
1309 return -ENOENT;
1310 mm = get_task_mm(task);
1311 put_task_struct(task);
1312 if (!mm)
1313 return -ENOENT;
1314 exe_file = get_mm_exe_file(mm);
1315 mmput(mm);
1316 if (exe_file) {
1317 *exe_path = exe_file->f_path;
1318 path_get(&exe_file->f_path);
1319 fput(exe_file);
1320 return 0;
1321 } else
1322 return -ENOENT;
1323}
1324
Al Viro008b1502005-08-20 00:17:39 +01001325static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct inode *inode = dentry->d_inode;
1328 int error = -EACCES;
1329
1330 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001331 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Eric W. Biederman778c1142006-06-26 00:25:58 -07001333 /* Are we allowed to snoop on the tasks file descriptors? */
1334 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001337 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 nd->last_type = LAST_BIND;
1339out:
Al Viro008b1502005-08-20 00:17:39 +01001340 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001343static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Mel Gormane12ba742007-10-16 01:25:52 -07001345 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001346 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int len;
1348
1349 if (!tmp)
1350 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001351
Jan Blunckcf28b482008-02-14 19:38:44 -08001352 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001353 len = PTR_ERR(pathname);
1354 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001356 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 if (len > buflen)
1359 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001360 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 len = -EFAULT;
1362 out:
1363 free_page((unsigned long)tmp);
1364 return len;
1365}
1366
1367static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1368{
1369 int error = -EACCES;
1370 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001371 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Eric W. Biederman778c1142006-06-26 00:25:58 -07001373 /* Are we allowed to snoop on the tasks file descriptors? */
1374 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001377 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (error)
1379 goto out;
1380
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001381 error = do_proc_readlink(&path, buffer, buflen);
1382 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return error;
1385}
1386
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001387static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001389 .follow_link = proc_pid_follow_link,
1390 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391};
1392
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001393
1394/* building an inode */
1395
1396static int task_dumpable(struct task_struct *task)
1397{
1398 int dumpable = 0;
1399 struct mm_struct *mm;
1400
1401 task_lock(task);
1402 mm = task->mm;
1403 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001404 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001405 task_unlock(task);
1406 if(dumpable == 1)
1407 return 1;
1408 return 0;
1409}
1410
1411
Eric W. Biederman61a28782006-10-02 02:18:49 -07001412static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001413{
1414 struct inode * inode;
1415 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001416 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001417
1418 /* We need a new inode */
1419
1420 inode = new_inode(sb);
1421 if (!inode)
1422 goto out;
1423
1424 /* Common stuff */
1425 ei = PROC_I(inode);
1426 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001427 inode->i_op = &proc_def_inode_operations;
1428
1429 /*
1430 * grab the reference to task.
1431 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001432 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001433 if (!ei->pid)
1434 goto out_unlock;
1435
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001436 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001437 rcu_read_lock();
1438 cred = __task_cred(task);
1439 inode->i_uid = cred->euid;
1440 inode->i_gid = cred->egid;
1441 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001442 }
1443 security_task_to_inode(task, inode);
1444
1445out:
1446 return inode;
1447
1448out_unlock:
1449 iput(inode);
1450 return NULL;
1451}
1452
1453static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1454{
1455 struct inode *inode = dentry->d_inode;
1456 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001457 const struct cred *cred;
1458
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001459 generic_fillattr(inode, stat);
1460
1461 rcu_read_lock();
1462 stat->uid = 0;
1463 stat->gid = 0;
1464 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1465 if (task) {
1466 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1467 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001468 cred = __task_cred(task);
1469 stat->uid = cred->euid;
1470 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001471 }
1472 }
1473 rcu_read_unlock();
1474 return 0;
1475}
1476
1477/* dentry stuff */
1478
1479/*
1480 * Exceptional case: normally we are not allowed to unhash a busy
1481 * directory. In this case, however, we can do it - no aliasing problems
1482 * due to the way we treat inodes.
1483 *
1484 * Rewrite the inode's ownerships here because the owning task may have
1485 * performed a setuid(), etc.
1486 *
1487 * Before the /proc/pid/status file was created the only way to read
1488 * the effective uid of a /process was to stat /proc/pid. Reading
1489 * /proc/pid/status is slow enough that procps and other packages
1490 * kept stating /proc/pid. To keep the rules in /proc simple I have
1491 * made this apply to all per process world readable and executable
1492 * directories.
1493 */
1494static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1495{
1496 struct inode *inode = dentry->d_inode;
1497 struct task_struct *task = get_proc_task(inode);
David Howellsc69e8d92008-11-14 10:39:19 +11001498 const struct cred *cred;
1499
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001500 if (task) {
1501 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1502 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001503 rcu_read_lock();
1504 cred = __task_cred(task);
1505 inode->i_uid = cred->euid;
1506 inode->i_gid = cred->egid;
1507 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001508 } else {
1509 inode->i_uid = 0;
1510 inode->i_gid = 0;
1511 }
1512 inode->i_mode &= ~(S_ISUID | S_ISGID);
1513 security_task_to_inode(task, inode);
1514 put_task_struct(task);
1515 return 1;
1516 }
1517 d_drop(dentry);
1518 return 0;
1519}
1520
1521static int pid_delete_dentry(struct dentry * dentry)
1522{
1523 /* Is the task we represent dead?
1524 * If so, then don't put the dentry on the lru list,
1525 * kill it immediately.
1526 */
1527 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1528}
1529
Al Virod72f71e2009-02-20 05:58:47 +00001530static const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001531{
1532 .d_revalidate = pid_revalidate,
1533 .d_delete = pid_delete_dentry,
1534};
1535
1536/* Lookups */
1537
Eric Dumazetc5141e62007-05-08 00:26:15 -07001538typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1539 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001540
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001541/*
1542 * Fill a directory entry.
1543 *
1544 * If possible create the dcache entry and derive our inode number and
1545 * file type from dcache entry.
1546 *
1547 * Since all of the proc inode numbers are dynamically generated, the inode
1548 * numbers do not exist until the inode is cache. This means creating the
1549 * the dcache entry in readdir is necessary to keep the inode numbers
1550 * reported by readdir in sync with the inode numbers reported
1551 * by stat.
1552 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001553static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1554 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001555 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001556{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001557 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001558 struct inode *inode;
1559 struct qstr qname;
1560 ino_t ino = 0;
1561 unsigned type = DT_UNKNOWN;
1562
1563 qname.name = name;
1564 qname.len = len;
1565 qname.hash = full_name_hash(name, len);
1566
1567 child = d_lookup(dir, &qname);
1568 if (!child) {
1569 struct dentry *new;
1570 new = d_alloc(dir, &qname);
1571 if (new) {
1572 child = instantiate(dir->d_inode, new, task, ptr);
1573 if (child)
1574 dput(new);
1575 else
1576 child = new;
1577 }
1578 }
1579 if (!child || IS_ERR(child) || !child->d_inode)
1580 goto end_instantiate;
1581 inode = child->d_inode;
1582 if (inode) {
1583 ino = inode->i_ino;
1584 type = inode->i_mode >> 12;
1585 }
1586 dput(child);
1587end_instantiate:
1588 if (!ino)
1589 ino = find_inode_number(dir, &qname);
1590 if (!ino)
1591 ino = 1;
1592 return filldir(dirent, name, len, filp->f_pos, ino, type);
1593}
1594
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001595static unsigned name_to_int(struct dentry *dentry)
1596{
1597 const char *name = dentry->d_name.name;
1598 int len = dentry->d_name.len;
1599 unsigned n = 0;
1600
1601 if (len > 1 && *name == '0')
1602 goto out;
1603 while (len-- > 0) {
1604 unsigned c = *name++ - '0';
1605 if (c > 9)
1606 goto out;
1607 if (n >= (~0U-9)/10)
1608 goto out;
1609 n *= 10;
1610 n += c;
1611 }
1612 return n;
1613out:
1614 return ~0U;
1615}
1616
Miklos Szeredi27932742007-05-08 00:26:17 -07001617#define PROC_FDINFO_MAX 64
1618
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001619static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001620{
1621 struct task_struct *task = get_proc_task(inode);
1622 struct files_struct *files = NULL;
1623 struct file *file;
1624 int fd = proc_fd(inode);
1625
1626 if (task) {
1627 files = get_files_struct(task);
1628 put_task_struct(task);
1629 }
1630 if (files) {
1631 /*
1632 * We are not taking a ref to the file structure, so we must
1633 * hold ->file_lock.
1634 */
1635 spin_lock(&files->file_lock);
1636 file = fcheck_files(files, fd);
1637 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001638 if (path) {
1639 *path = file->f_path;
1640 path_get(&file->f_path);
1641 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001642 if (info)
1643 snprintf(info, PROC_FDINFO_MAX,
1644 "pos:\t%lli\n"
1645 "flags:\t0%o\n",
1646 (long long) file->f_pos,
1647 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001648 spin_unlock(&files->file_lock);
1649 put_files_struct(files);
1650 return 0;
1651 }
1652 spin_unlock(&files->file_lock);
1653 put_files_struct(files);
1654 }
1655 return -ENOENT;
1656}
1657
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001658static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001659{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001660 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001661}
1662
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001663static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1664{
1665 struct inode *inode = dentry->d_inode;
1666 struct task_struct *task = get_proc_task(inode);
1667 int fd = proc_fd(inode);
1668 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001669 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001670
1671 if (task) {
1672 files = get_files_struct(task);
1673 if (files) {
1674 rcu_read_lock();
1675 if (fcheck_files(files, fd)) {
1676 rcu_read_unlock();
1677 put_files_struct(files);
1678 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001679 rcu_read_lock();
1680 cred = __task_cred(task);
1681 inode->i_uid = cred->euid;
1682 inode->i_gid = cred->egid;
1683 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001684 } else {
1685 inode->i_uid = 0;
1686 inode->i_gid = 0;
1687 }
1688 inode->i_mode &= ~(S_ISUID | S_ISGID);
1689 security_task_to_inode(task, inode);
1690 put_task_struct(task);
1691 return 1;
1692 }
1693 rcu_read_unlock();
1694 put_files_struct(files);
1695 }
1696 put_task_struct(task);
1697 }
1698 d_drop(dentry);
1699 return 0;
1700}
1701
Al Virod72f71e2009-02-20 05:58:47 +00001702static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001703{
1704 .d_revalidate = tid_fd_revalidate,
1705 .d_delete = pid_delete_dentry,
1706};
1707
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001708static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001709 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001711 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001712 struct file *file;
1713 struct files_struct *files;
1714 struct inode *inode;
1715 struct proc_inode *ei;
1716 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001717
Eric W. Biederman61a28782006-10-02 02:18:49 -07001718 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001719 if (!inode)
1720 goto out;
1721 ei = PROC_I(inode);
1722 ei->fd = fd;
1723 files = get_files_struct(task);
1724 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001725 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001726 inode->i_mode = S_IFLNK;
1727
1728 /*
1729 * We are not taking a ref to the file structure, so we must
1730 * hold ->file_lock.
1731 */
1732 spin_lock(&files->file_lock);
1733 file = fcheck_files(files, fd);
1734 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001735 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001736 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001737 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001738 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001739 inode->i_mode |= S_IWUSR | S_IXUSR;
1740 spin_unlock(&files->file_lock);
1741 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001742
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001743 inode->i_op = &proc_pid_link_inode_operations;
1744 inode->i_size = 64;
1745 ei->op.proc_get_link = proc_fd_link;
1746 dentry->d_op = &tid_fd_dentry_operations;
1747 d_add(dentry, inode);
1748 /* Close the race of the process dying before we return the dentry */
1749 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001750 error = NULL;
1751
1752 out:
1753 return error;
1754out_unlock:
1755 spin_unlock(&files->file_lock);
1756 put_files_struct(files);
1757out_iput:
1758 iput(inode);
1759 goto out;
1760}
1761
Miklos Szeredi27932742007-05-08 00:26:17 -07001762static struct dentry *proc_lookupfd_common(struct inode *dir,
1763 struct dentry *dentry,
1764 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001765{
1766 struct task_struct *task = get_proc_task(dir);
1767 unsigned fd = name_to_int(dentry);
1768 struct dentry *result = ERR_PTR(-ENOENT);
1769
1770 if (!task)
1771 goto out_no_task;
1772 if (fd == ~0U)
1773 goto out;
1774
Miklos Szeredi27932742007-05-08 00:26:17 -07001775 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001776out:
1777 put_task_struct(task);
1778out_no_task:
1779 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001780}
1781
Miklos Szeredi27932742007-05-08 00:26:17 -07001782static int proc_readfd_common(struct file * filp, void * dirent,
1783 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001785 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001786 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001787 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001788 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct files_struct * files;
1791
1792 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001793 if (!p)
1794 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 fd = filp->f_pos;
1798 switch (fd) {
1799 case 0:
1800 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1801 goto out;
1802 filp->f_pos++;
1803 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001804 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1806 goto out;
1807 filp->f_pos++;
1808 default:
1809 files = get_files_struct(p);
1810 if (!files)
1811 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001812 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001814 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001816 char name[PROC_NUMBUF];
1817 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (!fcheck_files(files, fd))
1820 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001821 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Miklos Szeredi27932742007-05-08 00:26:17 -07001823 len = snprintf(name, sizeof(name), "%d", fd);
1824 if (proc_fill_cache(filp, dirent, filldir,
1825 name, len, instantiate,
1826 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001827 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 break;
1829 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001830 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001832 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 put_files_struct(files);
1834 }
1835out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001836 put_task_struct(p);
1837out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return retval;
1839}
1840
Miklos Szeredi27932742007-05-08 00:26:17 -07001841static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1842 struct nameidata *nd)
1843{
1844 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1845}
1846
1847static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1848{
1849 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1850}
1851
1852static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1853 size_t len, loff_t *ppos)
1854{
1855 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001856 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001857 if (!err)
1858 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1859 return err;
1860}
1861
1862static const struct file_operations proc_fdinfo_file_operations = {
1863 .open = nonseekable_open,
1864 .read = proc_fdinfo_read,
1865};
1866
Arjan van de Ven00977a52007-02-12 00:55:34 -08001867static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 .read = generic_read_dir,
1869 .readdir = proc_readfd,
1870};
1871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001873 * /proc/pid/fd needs a special permission handler so that a process can still
1874 * access /proc/self/fd after it has executed a setuid().
1875 */
Al Viroe6305c42008-07-15 21:03:57 -04001876static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001877{
1878 int rv;
1879
1880 rv = generic_permission(inode, mask, NULL);
1881 if (rv == 0)
1882 return 0;
1883 if (task_pid(current) == proc_pid(inode))
1884 rv = 0;
1885 return rv;
1886}
1887
1888/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 * proc directories can do almost nothing..
1890 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001891static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001893 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001894 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895};
1896
Miklos Szeredi27932742007-05-08 00:26:17 -07001897static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1898 struct dentry *dentry, struct task_struct *task, const void *ptr)
1899{
1900 unsigned fd = *(unsigned *)ptr;
1901 struct inode *inode;
1902 struct proc_inode *ei;
1903 struct dentry *error = ERR_PTR(-ENOENT);
1904
1905 inode = proc_pid_make_inode(dir->i_sb, task);
1906 if (!inode)
1907 goto out;
1908 ei = PROC_I(inode);
1909 ei->fd = fd;
1910 inode->i_mode = S_IFREG | S_IRUSR;
1911 inode->i_fop = &proc_fdinfo_file_operations;
1912 dentry->d_op = &tid_fd_dentry_operations;
1913 d_add(dentry, inode);
1914 /* Close the race of the process dying before we return the dentry */
1915 if (tid_fd_revalidate(dentry, NULL))
1916 error = NULL;
1917
1918 out:
1919 return error;
1920}
1921
1922static struct dentry *proc_lookupfdinfo(struct inode *dir,
1923 struct dentry *dentry,
1924 struct nameidata *nd)
1925{
1926 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1927}
1928
1929static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1930{
1931 return proc_readfd_common(filp, dirent, filldir,
1932 proc_fdinfo_instantiate);
1933}
1934
1935static const struct file_operations proc_fdinfo_operations = {
1936 .read = generic_read_dir,
1937 .readdir = proc_readfdinfo,
1938};
1939
1940/*
1941 * proc directories can do almost nothing..
1942 */
1943static const struct inode_operations proc_fdinfo_inode_operations = {
1944 .lookup = proc_lookupfdinfo,
1945 .setattr = proc_setattr,
1946};
1947
1948
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001949static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001950 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001951{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001952 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001953 struct inode *inode;
1954 struct proc_inode *ei;
1955 struct dentry *error = ERR_PTR(-EINVAL);
1956
Eric W. Biederman61a28782006-10-02 02:18:49 -07001957 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001958 if (!inode)
1959 goto out;
1960
1961 ei = PROC_I(inode);
1962 inode->i_mode = p->mode;
1963 if (S_ISDIR(inode->i_mode))
1964 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1965 if (p->iop)
1966 inode->i_op = p->iop;
1967 if (p->fop)
1968 inode->i_fop = p->fop;
1969 ei->op = p->op;
1970 dentry->d_op = &pid_dentry_operations;
1971 d_add(dentry, inode);
1972 /* Close the race of the process dying before we return the dentry */
1973 if (pid_revalidate(dentry, NULL))
1974 error = NULL;
1975out:
1976 return error;
1977}
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979static struct dentry *proc_pident_lookup(struct inode *dir,
1980 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001981 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001982 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001984 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001985 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001986 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001988 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Eric W. Biederman99f89552006-06-26 00:25:55 -07001990 if (!task)
1991 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001993 /*
1994 * Yes, it does not scale. And it should not. Don't add
1995 * new entries into /proc/<tgid>/ without very good reasons.
1996 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001997 last = &ents[nents - 1];
1998 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (p->len != dentry->d_name.len)
2000 continue;
2001 if (!memcmp(dentry->d_name.name, p->name, p->len))
2002 break;
2003 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002004 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 goto out;
2006
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002007 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002009 put_task_struct(task);
2010out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002011 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Eric Dumazetc5141e62007-05-08 00:26:15 -07002014static int proc_pident_fill_cache(struct file *filp, void *dirent,
2015 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002016{
2017 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2018 proc_pident_instantiate, task, p);
2019}
2020
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002021static int proc_pident_readdir(struct file *filp,
2022 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002023 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002024{
2025 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002026 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002027 struct inode *inode = dentry->d_inode;
2028 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002029 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002030 ino_t ino;
2031 int ret;
2032
2033 ret = -ENOENT;
2034 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002035 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002036
2037 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002038 i = filp->f_pos;
2039 switch (i) {
2040 case 0:
2041 ino = inode->i_ino;
2042 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2043 goto out;
2044 i++;
2045 filp->f_pos++;
2046 /* fall through */
2047 case 1:
2048 ino = parent_ino(dentry);
2049 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2050 goto out;
2051 i++;
2052 filp->f_pos++;
2053 /* fall through */
2054 default:
2055 i -= 2;
2056 if (i >= nents) {
2057 ret = 1;
2058 goto out;
2059 }
2060 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002061 last = &ents[nents - 1];
2062 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002063 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002064 goto out;
2065 filp->f_pos++;
2066 p++;
2067 }
2068 }
2069
2070 ret = 1;
2071out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002072 put_task_struct(task);
2073out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002074 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002078static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2079 size_t count, loff_t *ppos)
2080{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002081 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002082 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002083 ssize_t length;
2084 struct task_struct *task = get_proc_task(inode);
2085
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002086 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002087 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002088
2089 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002090 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002091 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002092 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002093 if (length > 0)
2094 length = simple_read_from_buffer(buf, count, ppos, p, length);
2095 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002096 return length;
2097}
2098
2099static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2100 size_t count, loff_t *ppos)
2101{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002102 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002103 char *page;
2104 ssize_t length;
2105 struct task_struct *task = get_proc_task(inode);
2106
2107 length = -ESRCH;
2108 if (!task)
2109 goto out_no_task;
2110 if (count > PAGE_SIZE)
2111 count = PAGE_SIZE;
2112
2113 /* No partial writes. */
2114 length = -EINVAL;
2115 if (*ppos != 0)
2116 goto out;
2117
2118 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002119 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002120 if (!page)
2121 goto out;
2122
2123 length = -EFAULT;
2124 if (copy_from_user(page, buf, count))
2125 goto out_free;
2126
2127 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002128 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002129 (void*)page, count);
2130out_free:
2131 free_page((unsigned long) page);
2132out:
2133 put_task_struct(task);
2134out_no_task:
2135 return length;
2136}
2137
Arjan van de Ven00977a52007-02-12 00:55:34 -08002138static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002139 .read = proc_pid_attr_read,
2140 .write = proc_pid_attr_write,
2141};
2142
Eric Dumazetc5141e62007-05-08 00:26:15 -07002143static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002144 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2145 REG("prev", S_IRUGO, proc_pid_attr_operations),
2146 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2147 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2148 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2149 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002150};
2151
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002152static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 void * dirent, filldir_t filldir)
2154{
2155 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002156 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Arjan van de Ven00977a52007-02-12 00:55:34 -08002159static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002161 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162};
2163
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002164static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct dentry *dentry, struct nameidata *nd)
2166{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002167 return proc_pident_lookup(dir, dentry,
2168 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169}
2170
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002171static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002172 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002173 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002174 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175};
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177#endif
2178
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002179#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2180static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2181 size_t count, loff_t *ppos)
2182{
2183 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2184 struct mm_struct *mm;
2185 char buffer[PROC_NUMBUF];
2186 size_t len;
2187 int ret;
2188
2189 if (!task)
2190 return -ESRCH;
2191
2192 ret = 0;
2193 mm = get_task_mm(task);
2194 if (mm) {
2195 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2196 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2197 MMF_DUMP_FILTER_SHIFT));
2198 mmput(mm);
2199 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2200 }
2201
2202 put_task_struct(task);
2203
2204 return ret;
2205}
2206
2207static ssize_t proc_coredump_filter_write(struct file *file,
2208 const char __user *buf,
2209 size_t count,
2210 loff_t *ppos)
2211{
2212 struct task_struct *task;
2213 struct mm_struct *mm;
2214 char buffer[PROC_NUMBUF], *end;
2215 unsigned int val;
2216 int ret;
2217 int i;
2218 unsigned long mask;
2219
2220 ret = -EFAULT;
2221 memset(buffer, 0, sizeof(buffer));
2222 if (count > sizeof(buffer) - 1)
2223 count = sizeof(buffer) - 1;
2224 if (copy_from_user(buffer, buf, count))
2225 goto out_no_task;
2226
2227 ret = -EINVAL;
2228 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2229 if (*end == '\n')
2230 end++;
2231 if (end - buffer == 0)
2232 goto out_no_task;
2233
2234 ret = -ESRCH;
2235 task = get_proc_task(file->f_dentry->d_inode);
2236 if (!task)
2237 goto out_no_task;
2238
2239 ret = end - buffer;
2240 mm = get_task_mm(task);
2241 if (!mm)
2242 goto out_no_mm;
2243
2244 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2245 if (val & mask)
2246 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2247 else
2248 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2249 }
2250
2251 mmput(mm);
2252 out_no_mm:
2253 put_task_struct(task);
2254 out_no_task:
2255 return ret;
2256}
2257
2258static const struct file_operations proc_coredump_filter_operations = {
2259 .read = proc_coredump_filter_read,
2260 .write = proc_coredump_filter_write,
2261};
2262#endif
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264/*
2265 * /proc/self:
2266 */
2267static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2268 int buflen)
2269{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002270 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002271 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002272 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002273 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002274 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002275 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return vfs_readlink(dentry,buffer,buflen,tmp);
2277}
2278
Al Viro008b1502005-08-20 00:17:39 +01002279static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002281 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002282 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002283 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002284 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002285 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002286 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002287 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002288}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002290static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 .readlink = proc_self_readlink,
2292 .follow_link = proc_self_follow_link,
2293};
2294
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002295/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002296 * proc base
2297 *
2298 * These are the directory entries in the root directory of /proc
2299 * that properly belong to the /proc filesystem, as they describe
2300 * describe something that is process related.
2301 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002302static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002303 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002304 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002305};
2306
2307/*
2308 * Exceptional case: normally we are not allowed to unhash a busy
2309 * directory. In this case, however, we can do it - no aliasing problems
2310 * due to the way we treat inodes.
2311 */
2312static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2313{
2314 struct inode *inode = dentry->d_inode;
2315 struct task_struct *task = get_proc_task(inode);
2316 if (task) {
2317 put_task_struct(task);
2318 return 1;
2319 }
2320 d_drop(dentry);
2321 return 0;
2322}
2323
Al Virod72f71e2009-02-20 05:58:47 +00002324static const struct dentry_operations proc_base_dentry_operations =
Eric W. Biederman801199c2006-10-02 02:18:48 -07002325{
2326 .d_revalidate = proc_base_revalidate,
2327 .d_delete = pid_delete_dentry,
2328};
2329
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002330static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002331 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002332{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002333 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002334 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002335 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002336 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002337
2338 /* Allocate the inode */
2339 error = ERR_PTR(-ENOMEM);
2340 inode = new_inode(dir->i_sb);
2341 if (!inode)
2342 goto out;
2343
2344 /* Initialize the inode */
2345 ei = PROC_I(inode);
2346 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002347
2348 /*
2349 * grab the reference to the task.
2350 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002351 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002352 if (!ei->pid)
2353 goto out_iput;
2354
Eric W. Biederman801199c2006-10-02 02:18:48 -07002355 inode->i_mode = p->mode;
2356 if (S_ISDIR(inode->i_mode))
2357 inode->i_nlink = 2;
2358 if (S_ISLNK(inode->i_mode))
2359 inode->i_size = 64;
2360 if (p->iop)
2361 inode->i_op = p->iop;
2362 if (p->fop)
2363 inode->i_fop = p->fop;
2364 ei->op = p->op;
2365 dentry->d_op = &proc_base_dentry_operations;
2366 d_add(dentry, inode);
2367 error = NULL;
2368out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002369 return error;
2370out_iput:
2371 iput(inode);
2372 goto out;
2373}
2374
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002375static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2376{
2377 struct dentry *error;
2378 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002379 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002380
2381 error = ERR_PTR(-ENOENT);
2382
2383 if (!task)
2384 goto out_no_task;
2385
2386 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002387 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2388 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002389 if (p->len != dentry->d_name.len)
2390 continue;
2391 if (!memcmp(dentry->d_name.name, p->name, p->len))
2392 break;
2393 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002394 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002395 goto out;
2396
2397 error = proc_base_instantiate(dir, dentry, task, p);
2398
2399out:
2400 put_task_struct(task);
2401out_no_task:
2402 return error;
2403}
2404
Eric Dumazetc5141e62007-05-08 00:26:15 -07002405static int proc_base_fill_cache(struct file *filp, void *dirent,
2406 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002407{
2408 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2409 proc_base_instantiate, task, p);
2410}
2411
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002412#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002413static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002414{
Andrea Righi940389b2008-07-28 00:48:12 +02002415 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002416 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002417
Andrea Righi59954772008-07-27 17:29:15 +02002418 if (whole && lock_task_sighand(task, &flags)) {
2419 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002420
Andrea Righi59954772008-07-27 17:29:15 +02002421 task_io_accounting_add(&acct, &task->signal->ioac);
2422 while_each_thread(task, t)
2423 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002424
Andrea Righi59954772008-07-27 17:29:15 +02002425 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002426 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002427 return sprintf(buffer,
2428 "rchar: %llu\n"
2429 "wchar: %llu\n"
2430 "syscr: %llu\n"
2431 "syscw: %llu\n"
2432 "read_bytes: %llu\n"
2433 "write_bytes: %llu\n"
2434 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002435 (unsigned long long)acct.rchar,
2436 (unsigned long long)acct.wchar,
2437 (unsigned long long)acct.syscr,
2438 (unsigned long long)acct.syscw,
2439 (unsigned long long)acct.read_bytes,
2440 (unsigned long long)acct.write_bytes,
2441 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002442}
Andrea Righi297c5d92008-07-25 01:48:49 -07002443
2444static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2445{
2446 return do_io_accounting(task, buffer, 0);
2447}
2448
2449static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2450{
2451 return do_io_accounting(task, buffer, 1);
2452}
2453#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002454
Kees Cook47830722008-10-06 03:11:58 +04002455static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2456 struct pid *pid, struct task_struct *task)
2457{
2458 seq_printf(m, "%08x\n", task->personality);
2459 return 0;
2460}
2461
Eric W. Biederman801199c2006-10-02 02:18:48 -07002462/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002463 * Thread groups
2464 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002465static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002466static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002467
Eric Dumazetc5141e62007-05-08 00:26:15 -07002468static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002469 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2470 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2471 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002472#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002473 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002474#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002475 REG("environ", S_IRUSR, proc_environ_operations),
2476 INF("auxv", S_IRUSR, proc_pid_auxv),
2477 ONE("status", S_IRUGO, proc_pid_status),
2478 ONE("personality", S_IRUSR, proc_pid_personality),
2479 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002480#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002481 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002482#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002483#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002484 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002485#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002486 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2487 ONE("stat", S_IRUGO, proc_tgid_stat),
2488 ONE("statm", S_IRUGO, proc_pid_statm),
2489 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002491 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002492#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002493 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2494 LNK("cwd", proc_cwd_link),
2495 LNK("root", proc_root_link),
2496 LNK("exe", proc_exe_link),
2497 REG("mounts", S_IRUGO, proc_mounts_operations),
2498 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2499 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002500#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002501 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2502 REG("smaps", S_IRUGO, proc_smaps_operations),
2503 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002504#endif
2505#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002506 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002507#endif
2508#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002509 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002510#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002511#ifdef CONFIG_STACKTRACE
2512 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002513#endif
2514#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002515 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002517#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002518 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002519#endif
Paul Menage8793d852007-10-18 23:39:39 -07002520#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002521 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002522#endif
Paul Menagea4243162007-10-18 23:39:35 -07002523#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002524 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002525#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002526 INF("oom_score", S_IRUGO, proc_oom_score),
2527 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002528#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002529 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2530 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002532#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002533 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002534#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002535#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002536 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002537#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002538#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002539 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002540#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002541};
2542
2543static int proc_tgid_base_readdir(struct file * filp,
2544 void * dirent, filldir_t filldir)
2545{
2546 return proc_pident_readdir(filp,dirent,filldir,
2547 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2548}
2549
Arjan van de Ven00977a52007-02-12 00:55:34 -08002550static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002551 .read = generic_read_dir,
2552 .readdir = proc_tgid_base_readdir,
2553};
2554
2555static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002556 return proc_pident_lookup(dir, dentry,
2557 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002558}
2559
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002560static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002561 .lookup = proc_tgid_base_lookup,
2562 .getattr = pid_getattr,
2563 .setattr = proc_setattr,
2564};
2565
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002566static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002568 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002569 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002570 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Eric W. Biederman48e64842006-06-26 00:25:48 -07002572 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002573 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2574 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002575 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002576 if (!(current->flags & PF_EXITING))
2577 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002578 d_drop(dentry);
2579 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002582 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002583 goto out;
2584
2585 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002586 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2587 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002588 if (!leader)
2589 goto out;
2590
2591 name.name = "task";
2592 name.len = strlen(name.name);
2593 dir = d_hash_and_lookup(leader, &name);
2594 if (!dir)
2595 goto out_put_leader;
2596
2597 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002598 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002599 dentry = d_hash_and_lookup(dir, &name);
2600 if (dentry) {
2601 shrink_dcache_parent(dentry);
2602 d_drop(dentry);
2603 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002605
2606 dput(dir);
2607out_put_leader:
2608 dput(leader);
2609out:
2610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
2612
Randy Dunlap0895e912007-10-21 21:00:10 -07002613/**
2614 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2615 * @task: task that should be flushed.
2616 *
2617 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002618 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002619 * in. This call is supposed to do all of this job.
2620 *
2621 * Looks in the dcache for
2622 * /proc/@pid
2623 * /proc/@tgid/task/@pid
2624 * if either directory is present flushes it and all of it'ts children
2625 * from the dcache.
2626 *
2627 * It is safe and reasonable to cache /proc entries for a task until
2628 * that task exits. After that they just clog up the dcache with
2629 * useless entries, possibly causing useful dcache entries to be
2630 * flushed instead. This routine is proved to flush those useless
2631 * dcache entries at process exit time.
2632 *
2633 * NOTE: This routine is just an optimization so it does not guarantee
2634 * that no dcache entries will exist at process exit time it
2635 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002636 */
2637
2638void proc_flush_task(struct task_struct *task)
2639{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002640 int i;
2641 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002642 struct upid *upid;
2643
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002644 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002645 if (thread_group_leader(task))
2646 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002647
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002648 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002649 upid = &pid->numbers[i];
2650 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002651 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002652 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002653
2654 upid = &pid->numbers[pid->level];
2655 if (upid->nr == 1)
2656 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002657}
2658
Adrian Bunk9711ef92006-12-06 20:38:31 -08002659static struct dentry *proc_pid_instantiate(struct inode *dir,
2660 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002661 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002662{
2663 struct dentry *error = ERR_PTR(-ENOENT);
2664 struct inode *inode;
2665
Eric W. Biederman61a28782006-10-02 02:18:49 -07002666 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002667 if (!inode)
2668 goto out;
2669
2670 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2671 inode->i_op = &proc_tgid_base_inode_operations;
2672 inode->i_fop = &proc_tgid_base_operations;
2673 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002674
2675 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2676 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002677
2678 dentry->d_op = &pid_dentry_operations;
2679
2680 d_add(dentry, inode);
2681 /* Close the race of the process dying before we return the dentry */
2682 if (pid_revalidate(dentry, NULL))
2683 error = NULL;
2684out:
2685 return error;
2686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2689{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002690 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002693 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Eric W. Biederman801199c2006-10-02 02:18:48 -07002695 result = proc_base_lookup(dir, dentry);
2696 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2697 goto out;
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 tgid = name_to_int(dentry);
2700 if (tgid == ~0U)
2701 goto out;
2702
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002703 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002704 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002705 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 if (task)
2707 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002708 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (!task)
2710 goto out;
2711
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002712 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002713 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002715 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002719 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002720 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002722struct tgid_iter {
2723 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002724 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002725};
2726static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2727{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002728 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002730 if (iter.task)
2731 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002732 rcu_read_lock();
2733retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002734 iter.task = NULL;
2735 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002736 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002737 iter.tgid = pid_nr_ns(pid, ns);
2738 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002739 /* What we to know is if the pid we have find is the
2740 * pid of a thread_group_leader. Testing for task
2741 * being a thread_group_leader is the obvious thing
2742 * todo but there is a window when it fails, due to
2743 * the pid transfer logic in de_thread.
2744 *
2745 * So we perform the straight forward test of seeing
2746 * if the pid we have found is the pid of a thread
2747 * group leader, and don't worry if the task we have
2748 * found doesn't happen to be a thread group leader.
2749 * As we don't care in the case of readdir.
2750 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002751 if (!iter.task || !has_group_leader_pid(iter.task)) {
2752 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002753 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002754 }
2755 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002757 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002758 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759}
2760
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002761#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Eric W. Biederman61a28782006-10-02 02:18:49 -07002763static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002764 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002765{
2766 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002767 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002768 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002769 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772/* for the /proc/ directory itself, after non-process stuff has been done */
2773int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2774{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002776 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002777 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002778 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Eric W. Biederman61a28782006-10-02 02:18:49 -07002780 if (!reaper)
2781 goto out_no_task;
2782
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002783 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002784 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002785 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002786 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
2788
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002789 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002790 iter.task = NULL;
2791 iter.tgid = filp->f_pos - TGID_OFFSET;
2792 for (iter = next_tgid(ns, iter);
2793 iter.task;
2794 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2795 filp->f_pos = iter.tgid + TGID_OFFSET;
2796 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2797 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002798 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002801 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2802out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002803 put_task_struct(reaper);
2804out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 return 0;
2806}
2807
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002808/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002809 * Tasks
2810 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002811static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002812 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2813 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2814 REG("environ", S_IRUSR, proc_environ_operations),
2815 INF("auxv", S_IRUSR, proc_pid_auxv),
2816 ONE("status", S_IRUGO, proc_pid_status),
2817 ONE("personality", S_IRUSR, proc_pid_personality),
2818 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002819#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002820 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002821#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002822#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002823 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002824#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002825 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2826 ONE("stat", S_IRUGO, proc_tid_stat),
2827 ONE("statm", S_IRUGO, proc_pid_statm),
2828 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002829#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002830 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002831#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002832 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2833 LNK("cwd", proc_cwd_link),
2834 LNK("root", proc_root_link),
2835 LNK("exe", proc_exe_link),
2836 REG("mounts", S_IRUGO, proc_mounts_operations),
2837 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002838#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002839 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2840 REG("smaps", S_IRUGO, proc_smaps_operations),
2841 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002842#endif
2843#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002844 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002845#endif
2846#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002847 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002848#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002849#ifdef CONFIG_STACKTRACE
2850 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002851#endif
2852#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002853 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002854#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002855#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002856 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002857#endif
Paul Menage8793d852007-10-18 23:39:39 -07002858#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002859 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002860#endif
Paul Menagea4243162007-10-18 23:39:35 -07002861#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002862 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002863#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002864 INF("oom_score", S_IRUGO, proc_oom_score),
2865 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002866#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002867 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2868 REG("sessionid", S_IRUSR, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002869#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002870#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002871 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002872#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002873#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002874 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002875#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002876};
2877
2878static int proc_tid_base_readdir(struct file * filp,
2879 void * dirent, filldir_t filldir)
2880{
2881 return proc_pident_readdir(filp,dirent,filldir,
2882 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2883}
2884
2885static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002886 return proc_pident_lookup(dir, dentry,
2887 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002888}
2889
Arjan van de Ven00977a52007-02-12 00:55:34 -08002890static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002891 .read = generic_read_dir,
2892 .readdir = proc_tid_base_readdir,
2893};
2894
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002895static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002896 .lookup = proc_tid_base_lookup,
2897 .getattr = pid_getattr,
2898 .setattr = proc_setattr,
2899};
2900
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002901static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002902 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002903{
2904 struct dentry *error = ERR_PTR(-ENOENT);
2905 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002906 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002907
2908 if (!inode)
2909 goto out;
2910 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2911 inode->i_op = &proc_tid_base_inode_operations;
2912 inode->i_fop = &proc_tid_base_operations;
2913 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002914
2915 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2916 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002917
2918 dentry->d_op = &pid_dentry_operations;
2919
2920 d_add(dentry, inode);
2921 /* Close the race of the process dying before we return the dentry */
2922 if (pid_revalidate(dentry, NULL))
2923 error = NULL;
2924out:
2925 return error;
2926}
2927
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002928static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2929{
2930 struct dentry *result = ERR_PTR(-ENOENT);
2931 struct task_struct *task;
2932 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002934 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935
2936 if (!leader)
2937 goto out_no_task;
2938
2939 tid = name_to_int(dentry);
2940 if (tid == ~0U)
2941 goto out;
2942
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002943 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002945 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002946 if (task)
2947 get_task_struct(task);
2948 rcu_read_unlock();
2949 if (!task)
2950 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002951 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002952 goto out_drop_task;
2953
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002954 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002955out_drop_task:
2956 put_task_struct(task);
2957out:
2958 put_task_struct(leader);
2959out_no_task:
2960 return result;
2961}
2962
2963/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002964 * Find the first tid of a thread group to return to user space.
2965 *
2966 * Usually this is just the thread group leader, but if the users
2967 * buffer was too small or there was a seek into the middle of the
2968 * directory we have more work todo.
2969 *
2970 * In the case of a short read we start with find_task_by_pid.
2971 *
2972 * In the case of a seek we start with the leader and walk nr
2973 * threads past it.
2974 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002975static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002976 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002977{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002978 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002979
Eric W. Biedermancc288732006-06-26 00:26:01 -07002980 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002981 /* Attempt to start with the pid of a thread */
2982 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002983 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002984 if (pos && (pos->group_leader == leader))
2985 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002986 }
2987
2988 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002989 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002990 if (nr && nr >= get_nr_threads(leader))
2991 goto out;
2992
2993 /* If we haven't found our starting place yet start
2994 * with the leader and walk nr threads forward.
2995 */
2996 for (pos = leader; nr > 0; --nr) {
2997 pos = next_thread(pos);
2998 if (pos == leader) {
2999 pos = NULL;
3000 goto out;
3001 }
3002 }
3003found:
3004 get_task_struct(pos);
3005out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003006 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003007 return pos;
3008}
3009
3010/*
3011 * Find the next thread in the thread list.
3012 * Return NULL if there is an error or no next thread.
3013 *
3014 * The reference to the input task_struct is released.
3015 */
3016static struct task_struct *next_tid(struct task_struct *start)
3017{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003018 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003019 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003020 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003021 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003022 if (thread_group_leader(pos))
3023 pos = NULL;
3024 else
3025 get_task_struct(pos);
3026 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003027 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003028 put_task_struct(start);
3029 return pos;
3030}
3031
Eric W. Biederman61a28782006-10-02 02:18:49 -07003032static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3033 struct task_struct *task, int tid)
3034{
3035 char name[PROC_NUMBUF];
3036 int len = snprintf(name, sizeof(name), "%d", tid);
3037 return proc_fill_cache(filp, dirent, filldir, name, len,
3038 proc_task_instantiate, task, NULL);
3039}
3040
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041/* for the /proc/TGID/task/ directories */
3042static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3043{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003044 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003046 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003047 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 int retval = -ENOENT;
3049 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003050 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003051 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003053 task = get_proc_task(inode);
3054 if (!task)
3055 goto out_no_task;
3056 rcu_read_lock();
3057 if (pid_alive(task)) {
3058 leader = task->group_leader;
3059 get_task_struct(leader);
3060 }
3061 rcu_read_unlock();
3062 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003063 if (!leader)
3064 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 retval = 0;
3066
Linus Torvaldsee568b22009-03-17 10:02:35 -07003067 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 case 0:
3069 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003070 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003072 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /* fall through */
3074 case 1:
3075 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003076 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003078 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /* fall through */
3080 }
3081
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003082 /* f_version caches the tgid value that the last readdir call couldn't
3083 * return. lseek aka telldir automagically resets f_version to 0.
3084 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003085 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003086 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003087 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003088 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003089 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003090 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003091 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003092 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003093 /* returning this tgid failed, save it as the first
3094 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003095 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003096 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003101 put_task_struct(leader);
3102out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 return retval;
3104}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003105
3106static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3107{
3108 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003109 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003110 generic_fillattr(inode, stat);
3111
Eric W. Biederman99f89552006-06-26 00:25:55 -07003112 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003113 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003114 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003115 }
3116
3117 return 0;
3118}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003119
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003120static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003121 .lookup = proc_task_lookup,
3122 .getattr = proc_task_getattr,
3123 .setattr = proc_setattr,
3124};
3125
Arjan van de Ven00977a52007-02-12 00:55:34 -08003126static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003127 .read = generic_read_dir,
3128 .readdir = proc_task_readdir,
3129};