blob: 10fd5223d600aec609a2adb1ea8e1693dd923562 [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>
Neil Hormand85f50d2007-10-18 23:40:37 -070068#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070069#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/mount.h>
71#include <linux/security.h>
72#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070073#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070074#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/cpuset.h>
76#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050077#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070078#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070079#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070080#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070081#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include "internal.h"
83
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070084/* NOTE:
85 * Implementing inode permission operations in /proc is almost
86 * certainly an error. Permission checks need to happen during
87 * each system call not at open time. The reason is that most of
88 * what we wish to check for permissions in /proc varies at runtime.
89 *
90 * The classic example of a problem is opening file descriptors
91 * in /proc for a task before it execs a suid executable.
92 */
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070096 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080098 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080099 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700100 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Eric W. Biederman61a28782006-10-02 02:18:49 -0700103#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700105 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700106 .mode = MODE, \
107 .iop = IOP, \
108 .fop = FOP, \
109 .op = OP, \
110}
111
Eric W. Biederman61a28782006-10-02 02:18:49 -0700112#define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700123#define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800127#define ONE(NAME, MODE, OTYPE) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
130 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Vegard Nossumaed54172008-06-05 22:46:53 -0700132/*
133 * Count the number of hardlinks for the pid_entry table, excluding the .
134 * and .. links.
135 */
136static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
137 unsigned int n)
138{
139 unsigned int i;
140 unsigned int count;
141
142 count = 0;
143 for (i = 0; i < n; ++i) {
144 if (S_ISDIR(entries[i].mode))
145 ++count;
146 }
147
148 return count;
149}
150
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700151static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
153 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154 task_lock(task);
155 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if(fs)
157 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700158 task_unlock(task);
159 return fs;
160}
161
Eric W. Biederman99f89552006-06-26 00:25:55 -0700162static int get_nr_threads(struct task_struct *tsk)
163{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700164 unsigned long flags;
165 int count = 0;
166
167 if (lock_task_sighand(tsk, &flags)) {
168 count = atomic_read(&tsk->signal->count);
169 unlock_task_sighand(tsk, &flags);
170 }
171 return count;
172}
173
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800174static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176 struct task_struct *task = get_proc_task(inode);
177 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700178 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179
180 if (task) {
181 fs = get_fs_struct(task);
182 put_task_struct(task);
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (fs) {
185 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800186 *path = fs->pwd;
187 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 read_unlock(&fs->lock);
189 result = 0;
190 put_fs_struct(fs);
191 }
192 return result;
193}
194
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800195static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700197 struct task_struct *task = get_proc_task(inode);
198 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700200
201 if (task) {
202 fs = get_fs_struct(task);
203 put_task_struct(task);
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (fs) {
206 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800207 *path = fs->root;
208 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 read_unlock(&fs->lock);
210 result = 0;
211 put_fs_struct(fs);
212 }
213 return result;
214}
215
Roland McGrath638fa202008-04-29 01:01:38 -0700216/*
217 * Return zero if current may access user memory in @task, -error if not.
218 */
219static int check_mem_permission(struct task_struct *task)
220{
221 /*
222 * A task can always look at itself, in case it chooses
223 * to use system calls instead of load instructions.
224 */
225 if (task == current)
226 return 0;
227
228 /*
229 * If current is actively ptrace'ing, and would also be
230 * permitted to freshly attach with ptrace now, permit it.
231 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700232 if (task_is_stopped_or_traced(task)) {
233 int match;
234 rcu_read_lock();
235 match = (tracehook_tracer_task(task) == current);
236 rcu_read_unlock();
237 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
238 return 0;
239 }
Roland McGrath638fa202008-04-29 01:01:38 -0700240
241 /*
242 * Noone else is allowed.
243 */
244 return -EPERM;
245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Al Viro831830b2008-01-02 14:09:57 +0000247struct mm_struct *mm_for_maps(struct task_struct *task)
248{
249 struct mm_struct *mm = get_task_mm(task);
250 if (!mm)
251 return NULL;
252 down_read(&mm->mmap_sem);
253 task_lock(task);
254 if (task->mm != mm)
255 goto out;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400256 if (task->mm != current->mm &&
257 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
Al Viro831830b2008-01-02 14:09:57 +0000258 goto out;
259 task_unlock(task);
260 return mm;
261out:
262 task_unlock(task);
263 up_read(&mm->mmap_sem);
264 mmput(mm);
265 return NULL;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static int proc_pid_cmdline(struct task_struct *task, char * buffer)
269{
270 int res = 0;
271 unsigned int len;
272 struct mm_struct *mm = get_task_mm(task);
273 if (!mm)
274 goto out;
275 if (!mm->arg_end)
276 goto out_mm; /* Shh! No looking before we're done */
277
278 len = mm->arg_end - mm->arg_start;
279
280 if (len > PAGE_SIZE)
281 len = PAGE_SIZE;
282
283 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
284
285 // If the nul at the end of args has been overwritten, then
286 // assume application is using setproctitle(3).
287 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
288 len = strnlen(buffer, res);
289 if (len < res) {
290 res = len;
291 } else {
292 len = mm->env_end - mm->env_start;
293 if (len > PAGE_SIZE - res)
294 len = PAGE_SIZE - res;
295 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
296 res = strnlen(buffer, res);
297 }
298 }
299out_mm:
300 mmput(mm);
301out:
302 return res;
303}
304
305static int proc_pid_auxv(struct task_struct *task, char *buffer)
306{
307 int res = 0;
308 struct mm_struct *mm = get_task_mm(task);
309 if (mm) {
310 unsigned int nwords = 0;
311 do
312 nwords += 2;
313 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
314 res = nwords * sizeof(mm->saved_auxv[0]);
315 if (res > PAGE_SIZE)
316 res = PAGE_SIZE;
317 memcpy(buffer, mm->saved_auxv, res);
318 mmput(mm);
319 }
320 return res;
321}
322
323
324#ifdef CONFIG_KALLSYMS
325/*
326 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
327 * Returns the resolved symbol. If that fails, simply return the address.
328 */
329static int proc_pid_wchan(struct task_struct *task, char *buffer)
330{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700331 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700332 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 wchan = get_wchan(task);
335
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700336 if (lookup_symbol_name(wchan, symname) < 0)
337 return sprintf(buffer, "%lu", wchan);
338 else
339 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341#endif /* CONFIG_KALLSYMS */
342
343#ifdef CONFIG_SCHEDSTATS
344/*
345 * Provides /proc/PID/schedstat
346 */
347static int proc_pid_schedstat(struct task_struct *task, char *buffer)
348{
Balbir Singh172ba842007-07-09 18:52:00 +0200349 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100350 (unsigned long long)task->se.sum_exec_runtime,
351 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200352 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354#endif
355
Arjan van de Ven97455122008-01-25 21:08:34 +0100356#ifdef CONFIG_LATENCYTOP
357static int lstats_show_proc(struct seq_file *m, void *v)
358{
359 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800360 struct inode *inode = m->private;
361 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100362
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800363 if (!task)
364 return -ESRCH;
365 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100366 for (i = 0; i < 32; i++) {
367 if (task->latency_record[i].backtrace[0]) {
368 int q;
369 seq_printf(m, "%i %li %li ",
370 task->latency_record[i].count,
371 task->latency_record[i].time,
372 task->latency_record[i].max);
373 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Hugh Dickins9c246242008-12-09 13:14:27 -0800374 char sym[KSYM_SYMBOL_LEN];
Arjan van de Ven97455122008-01-25 21:08:34 +0100375 char *c;
376 if (!task->latency_record[i].backtrace[q])
377 break;
378 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
379 break;
380 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
381 c = strchr(sym, '+');
382 if (c)
383 *c = 0;
384 seq_printf(m, "%s ", sym);
385 }
386 seq_printf(m, "\n");
387 }
388
389 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800390 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100391 return 0;
392}
393
394static int lstats_open(struct inode *inode, struct file *file)
395{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800396 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800397}
398
Arjan van de Ven97455122008-01-25 21:08:34 +0100399static ssize_t lstats_write(struct file *file, const char __user *buf,
400 size_t count, loff_t *offs)
401{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800402 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100403
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800404 if (!task)
405 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100406 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800407 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100408
409 return count;
410}
411
412static const struct file_operations proc_lstats_operations = {
413 .open = lstats_open,
414 .read = seq_read,
415 .write = lstats_write,
416 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800417 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100418};
419
420#endif
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/* The badness from the OOM killer */
423unsigned long badness(struct task_struct *p, unsigned long uptime);
424static int proc_oom_score(struct task_struct *task, char *buffer)
425{
426 unsigned long points;
427 struct timespec uptime;
428
429 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700430 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700432 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return sprintf(buffer, "%lu\n", points);
434}
435
Neil Hormand85f50d2007-10-18 23:40:37 -0700436struct limit_names {
437 char *name;
438 char *unit;
439};
440
441static const struct limit_names lnames[RLIM_NLIMITS] = {
442 [RLIMIT_CPU] = {"Max cpu time", "ms"},
443 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
444 [RLIMIT_DATA] = {"Max data size", "bytes"},
445 [RLIMIT_STACK] = {"Max stack size", "bytes"},
446 [RLIMIT_CORE] = {"Max core file size", "bytes"},
447 [RLIMIT_RSS] = {"Max resident set", "bytes"},
448 [RLIMIT_NPROC] = {"Max processes", "processes"},
449 [RLIMIT_NOFILE] = {"Max open files", "files"},
450 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
451 [RLIMIT_AS] = {"Max address space", "bytes"},
452 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
453 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
454 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
455 [RLIMIT_NICE] = {"Max nice priority", NULL},
456 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800457 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700458};
459
460/* Display limits for a process */
461static int proc_pid_limits(struct task_struct *task, char *buffer)
462{
463 unsigned int i;
464 int count = 0;
465 unsigned long flags;
466 char *bufptr = buffer;
467
468 struct rlimit rlim[RLIM_NLIMITS];
469
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400470 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700471 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700472 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
473 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700474
475 /*
476 * print the file header
477 */
478 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
479 "Limit", "Soft Limit", "Hard Limit", "Units");
480
481 for (i = 0; i < RLIM_NLIMITS; i++) {
482 if (rlim[i].rlim_cur == RLIM_INFINITY)
483 count += sprintf(&bufptr[count], "%-25s %-20s ",
484 lnames[i].name, "unlimited");
485 else
486 count += sprintf(&bufptr[count], "%-25s %-20lu ",
487 lnames[i].name, rlim[i].rlim_cur);
488
489 if (rlim[i].rlim_max == RLIM_INFINITY)
490 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
491 else
492 count += sprintf(&bufptr[count], "%-20lu ",
493 rlim[i].rlim_max);
494
495 if (lnames[i].unit)
496 count += sprintf(&bufptr[count], "%-10s\n",
497 lnames[i].unit);
498 else
499 count += sprintf(&bufptr[count], "\n");
500 }
501
502 return count;
503}
504
Roland McGrathebcb6732008-07-25 19:46:00 -0700505#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
506static int proc_pid_syscall(struct task_struct *task, char *buffer)
507{
508 long nr;
509 unsigned long args[6], sp, pc;
510
511 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
512 return sprintf(buffer, "running\n");
513
514 if (nr < 0)
515 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
516
517 return sprintf(buffer,
518 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
519 nr,
520 args[0], args[1], args[2], args[3], args[4], args[5],
521 sp, pc);
522}
523#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/************************************************************************/
526/* Here the fs part begins */
527/************************************************************************/
528
529/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700530static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700532 struct task_struct *task;
533 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700534 /* Allow access to a task's file descriptors if it is us or we
535 * may use ptrace attach to the process and find out that
536 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700537 */
538 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700539 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400540 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700541 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700542 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700543 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700546static int proc_setattr(struct dentry *dentry, struct iattr *attr)
547{
548 int error;
549 struct inode *inode = dentry->d_inode;
550
551 if (attr->ia_valid & ATTR_MODE)
552 return -EPERM;
553
554 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700555 if (!error)
556 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700557 return error;
558}
559
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800560static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700561 .setattr = proc_setattr,
562};
563
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100564static int mounts_open_common(struct inode *inode, struct file *file,
565 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700567 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700568 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800569 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100570 struct fs_struct *fs = NULL;
571 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500572 struct proc_mounts *p;
573 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Eric W. Biederman99f89552006-06-26 00:25:55 -0700575 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700576 rcu_read_lock();
577 nsp = task_nsproxy(task);
578 if (nsp) {
579 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800580 if (ns)
581 get_mnt_ns(ns);
582 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700583 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100584 if (ns)
585 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700586 put_task_struct(task);
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100589 if (!ns)
590 goto err;
591 if (!fs)
592 goto err_put_ns;
593
594 read_lock(&fs->lock);
595 root = fs->root;
596 path_get(&root);
597 read_unlock(&fs->lock);
598 put_fs_struct(fs);
599
600 ret = -ENOMEM;
601 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
602 if (!p)
603 goto err_put_path;
604
605 file->private_data = &p->m;
606 ret = seq_open(file, op);
607 if (ret)
608 goto err_free;
609
610 p->m.private = p;
611 p->ns = ns;
612 p->root = root;
613 p->event = ns->event;
614
615 return 0;
616
617 err_free:
618 kfree(p);
619 err_put_path:
620 path_put(&root);
621 err_put_ns:
622 put_mnt_ns(ns);
623 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ret;
625}
626
627static int mounts_release(struct inode *inode, struct file *file)
628{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100629 struct proc_mounts *p = file->private_data;
630 path_put(&p->root);
631 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return seq_release(inode, file);
633}
634
Al Viro5addc5d2005-11-07 17:15:49 -0500635static unsigned mounts_poll(struct file *file, poll_table *wait)
636{
637 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100638 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500639 unsigned res = 0;
640
641 poll_wait(file, &ns->poll, wait);
642
643 spin_lock(&vfsmount_lock);
644 if (p->event != ns->event) {
645 p->event = ns->event;
646 res = POLLERR;
647 }
648 spin_unlock(&vfsmount_lock);
649
650 return res;
651}
652
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100653static int mounts_open(struct inode *inode, struct file *file)
654{
655 return mounts_open_common(inode, file, &mounts_op);
656}
657
Arjan van de Ven00977a52007-02-12 00:55:34 -0800658static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 .open = mounts_open,
660 .read = seq_read,
661 .llseek = seq_lseek,
662 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500663 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664};
665
Ram Pai2d4d4862008-03-27 13:06:25 +0100666static int mountinfo_open(struct inode *inode, struct file *file)
667{
668 return mounts_open_common(inode, file, &mountinfo_op);
669}
670
671static const struct file_operations proc_mountinfo_operations = {
672 .open = mountinfo_open,
673 .read = seq_read,
674 .llseek = seq_lseek,
675 .release = mounts_release,
676 .poll = mounts_poll,
677};
678
Chuck Leverb4629fe2006-03-20 13:44:12 -0500679static int mountstats_open(struct inode *inode, struct file *file)
680{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100681 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500682}
683
Arjan van de Ven00977a52007-02-12 00:55:34 -0800684static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500685 .open = mountstats_open,
686 .read = seq_read,
687 .llseek = seq_lseek,
688 .release = mounts_release,
689};
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
692
693static ssize_t proc_info_read(struct file * file, char __user * buf,
694 size_t count, loff_t *ppos)
695{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800696 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 unsigned long page;
698 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700699 struct task_struct *task = get_proc_task(inode);
700
701 length = -ESRCH;
702 if (!task)
703 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (count > PROC_BLOCK_SIZE)
706 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700707
708 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700709 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700710 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 length = PROC_I(inode)->op.proc_read(task, (char*)page);
713
714 if (length >= 0)
715 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
716 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700717out:
718 put_task_struct(task);
719out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return length;
721}
722
Arjan van de Ven00977a52007-02-12 00:55:34 -0800723static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 .read = proc_info_read,
725};
726
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800727static int proc_single_show(struct seq_file *m, void *v)
728{
729 struct inode *inode = m->private;
730 struct pid_namespace *ns;
731 struct pid *pid;
732 struct task_struct *task;
733 int ret;
734
735 ns = inode->i_sb->s_fs_info;
736 pid = proc_pid(inode);
737 task = get_pid_task(pid, PIDTYPE_PID);
738 if (!task)
739 return -ESRCH;
740
741 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
742
743 put_task_struct(task);
744 return ret;
745}
746
747static int proc_single_open(struct inode *inode, struct file *filp)
748{
749 int ret;
750 ret = single_open(filp, proc_single_show, NULL);
751 if (!ret) {
752 struct seq_file *m = filp->private_data;
753
754 m->private = inode;
755 }
756 return ret;
757}
758
759static const struct file_operations proc_single_file_operations = {
760 .open = proc_single_open,
761 .read = seq_read,
762 .llseek = seq_lseek,
763 .release = single_release,
764};
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766static int mem_open(struct inode* inode, struct file* file)
767{
768 file->private_data = (void*)((long)current->self_exec_id);
769 return 0;
770}
771
772static ssize_t mem_read(struct file * file, char __user * buf,
773 size_t count, loff_t *ppos)
774{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800775 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 char *page;
777 unsigned long src = *ppos;
778 int ret = -ESRCH;
779 struct mm_struct *mm;
780
Eric W. Biederman99f89552006-06-26 00:25:55 -0700781 if (!task)
782 goto out_no_task;
783
Roland McGrath638fa202008-04-29 01:01:38 -0700784 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto out;
786
787 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700788 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (!page)
790 goto out;
791
792 ret = 0;
793
794 mm = get_task_mm(task);
795 if (!mm)
796 goto out_free;
797
798 ret = -EIO;
799
800 if (file->private_data != (void*)((long)current->self_exec_id))
801 goto out_put;
802
803 ret = 0;
804
805 while (count > 0) {
806 int this_len, retval;
807
808 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
809 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa202008-04-29 01:01:38 -0700810 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (!ret)
812 ret = -EIO;
813 break;
814 }
815
816 if (copy_to_user(buf, page, retval)) {
817 ret = -EFAULT;
818 break;
819 }
820
821 ret += retval;
822 src += retval;
823 buf += retval;
824 count -= retval;
825 }
826 *ppos = src;
827
828out_put:
829 mmput(mm);
830out_free:
831 free_page((unsigned long) page);
832out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700833 put_task_struct(task);
834out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
836}
837
838#define mem_write NULL
839
840#ifndef mem_write
841/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800842static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 size_t count, loff_t *ppos)
844{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700845 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800847 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned long dst = *ppos;
849
Eric W. Biederman99f89552006-06-26 00:25:55 -0700850 copied = -ESRCH;
851 if (!task)
852 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Roland McGrath638fa202008-04-29 01:01:38 -0700854 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700855 goto out;
856
857 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700858 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700860 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700862 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 while (count > 0) {
864 int this_len, retval;
865
866 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
867 if (copy_from_user(page, buf, this_len)) {
868 copied = -EFAULT;
869 break;
870 }
871 retval = access_process_vm(task, dst, page, this_len, 1);
872 if (!retval) {
873 if (!copied)
874 copied = -EIO;
875 break;
876 }
877 copied += retval;
878 buf += retval;
879 dst += retval;
880 count -= retval;
881 }
882 *ppos = dst;
883 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700884out:
885 put_task_struct(task);
886out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return copied;
888}
889#endif
890
Matt Mackall85863e42008-02-04 22:29:04 -0800891loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 switch (orig) {
894 case 0:
895 file->f_pos = offset;
896 break;
897 case 1:
898 file->f_pos += offset;
899 break;
900 default:
901 return -EINVAL;
902 }
903 force_successful_syscall_return();
904 return file->f_pos;
905}
906
Arjan van de Ven00977a52007-02-12 00:55:34 -0800907static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 .llseek = mem_lseek,
909 .read = mem_read,
910 .write = mem_write,
911 .open = mem_open,
912};
913
James Pearson315e28c2007-10-16 23:30:17 -0700914static ssize_t environ_read(struct file *file, char __user *buf,
915 size_t count, loff_t *ppos)
916{
917 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
918 char *page;
919 unsigned long src = *ppos;
920 int ret = -ESRCH;
921 struct mm_struct *mm;
922
923 if (!task)
924 goto out_no_task;
925
Stephen Smalley006ebb42008-05-19 08:32:49 -0400926 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700927 goto out;
928
929 ret = -ENOMEM;
930 page = (char *)__get_free_page(GFP_TEMPORARY);
931 if (!page)
932 goto out;
933
934 ret = 0;
935
936 mm = get_task_mm(task);
937 if (!mm)
938 goto out_free;
939
940 while (count > 0) {
941 int this_len, retval, max_len;
942
943 this_len = mm->env_end - (mm->env_start + src);
944
945 if (this_len <= 0)
946 break;
947
948 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
949 this_len = (this_len > max_len) ? max_len : this_len;
950
951 retval = access_process_vm(task, (mm->env_start + src),
952 page, this_len, 0);
953
954 if (retval <= 0) {
955 ret = retval;
956 break;
957 }
958
959 if (copy_to_user(buf, page, retval)) {
960 ret = -EFAULT;
961 break;
962 }
963
964 ret += retval;
965 src += retval;
966 buf += retval;
967 count -= retval;
968 }
969 *ppos = src;
970
971 mmput(mm);
972out_free:
973 free_page((unsigned long) page);
974out:
975 put_task_struct(task);
976out_no_task:
977 return ret;
978}
979
980static const struct file_operations proc_environ_operations = {
981 .read = environ_read,
982};
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984static ssize_t oom_adjust_read(struct file *file, char __user *buf,
985 size_t count, loff_t *ppos)
986{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800987 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700988 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700990 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Eric W. Biederman99f89552006-06-26 00:25:55 -0700992 if (!task)
993 return -ESRCH;
994 oom_adjust = task->oomkilladj;
995 put_task_struct(task);
996
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700997 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700998
999 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1003 size_t count, loff_t *ppos)
1004{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001005 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001006 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int oom_adjust;
1008
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001009 memset(buffer, 0, sizeof(buffer));
1010 if (count > sizeof(buffer) - 1)
1011 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (copy_from_user(buffer, buf, count))
1013 return -EFAULT;
1014 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001015 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1016 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return -EINVAL;
1018 if (*end == '\n')
1019 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001020 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001021 if (!task)
1022 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001023 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1024 put_task_struct(task);
1025 return -EACCES;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001028 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (end - buffer == 0)
1030 return -EIO;
1031 return end - buffer;
1032}
1033
Arjan van de Ven00977a52007-02-12 00:55:34 -08001034static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 .read = oom_adjust_read,
1036 .write = oom_adjust_write,
1037};
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039#ifdef CONFIG_AUDITSYSCALL
1040#define TMPBUFLEN 21
1041static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1042 size_t count, loff_t *ppos)
1043{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001044 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001045 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 ssize_t length;
1047 char tmpbuf[TMPBUFLEN];
1048
Eric W. Biederman99f89552006-06-26 00:25:55 -07001049 if (!task)
1050 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001052 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001053 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1055}
1056
1057static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1058 size_t count, loff_t *ppos)
1059{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001060 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 char *page, *tmp;
1062 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 uid_t loginuid;
1064
1065 if (!capable(CAP_AUDIT_CONTROL))
1066 return -EPERM;
1067
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001068 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return -EPERM;
1070
Al Viroe0182902006-05-18 08:28:02 -04001071 if (count >= PAGE_SIZE)
1072 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 if (*ppos != 0) {
1075 /* No partial writes. */
1076 return -EINVAL;
1077 }
Mel Gormane12ba742007-10-16 01:25:52 -07001078 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!page)
1080 return -ENOMEM;
1081 length = -EFAULT;
1082 if (copy_from_user(page, buf, count))
1083 goto out_free_page;
1084
Al Viroe0182902006-05-18 08:28:02 -04001085 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 loginuid = simple_strtoul(page, &tmp, 10);
1087 if (tmp == page) {
1088 length = -EINVAL;
1089 goto out_free_page;
1090
1091 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001092 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (likely(length == 0))
1094 length = count;
1095
1096out_free_page:
1097 free_page((unsigned long) page);
1098 return length;
1099}
1100
Arjan van de Ven00977a52007-02-12 00:55:34 -08001101static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 .read = proc_loginuid_read,
1103 .write = proc_loginuid_write,
1104};
Eric Paris1e0bd752008-03-13 08:15:31 -04001105
1106static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1107 size_t count, loff_t *ppos)
1108{
1109 struct inode * inode = file->f_path.dentry->d_inode;
1110 struct task_struct *task = get_proc_task(inode);
1111 ssize_t length;
1112 char tmpbuf[TMPBUFLEN];
1113
1114 if (!task)
1115 return -ESRCH;
1116 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1117 audit_get_sessionid(task));
1118 put_task_struct(task);
1119 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1120}
1121
1122static const struct file_operations proc_sessionid_operations = {
1123 .read = proc_sessionid_read,
1124};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125#endif
1126
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001127#ifdef CONFIG_FAULT_INJECTION
1128static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1129 size_t count, loff_t *ppos)
1130{
1131 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1132 char buffer[PROC_NUMBUF];
1133 size_t len;
1134 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001135
1136 if (!task)
1137 return -ESRCH;
1138 make_it_fail = task->make_it_fail;
1139 put_task_struct(task);
1140
1141 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001142
1143 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001144}
1145
1146static ssize_t proc_fault_inject_write(struct file * file,
1147 const char __user * buf, size_t count, loff_t *ppos)
1148{
1149 struct task_struct *task;
1150 char buffer[PROC_NUMBUF], *end;
1151 int make_it_fail;
1152
1153 if (!capable(CAP_SYS_RESOURCE))
1154 return -EPERM;
1155 memset(buffer, 0, sizeof(buffer));
1156 if (count > sizeof(buffer) - 1)
1157 count = sizeof(buffer) - 1;
1158 if (copy_from_user(buffer, buf, count))
1159 return -EFAULT;
1160 make_it_fail = simple_strtol(buffer, &end, 0);
1161 if (*end == '\n')
1162 end++;
1163 task = get_proc_task(file->f_dentry->d_inode);
1164 if (!task)
1165 return -ESRCH;
1166 task->make_it_fail = make_it_fail;
1167 put_task_struct(task);
1168 if (end - buffer == 0)
1169 return -EIO;
1170 return end - buffer;
1171}
1172
Arjan van de Ven00977a52007-02-12 00:55:34 -08001173static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001174 .read = proc_fault_inject_read,
1175 .write = proc_fault_inject_write,
1176};
1177#endif
1178
Arjan van de Ven97455122008-01-25 21:08:34 +01001179
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001180#ifdef CONFIG_SCHED_DEBUG
1181/*
1182 * Print out various scheduling related per-task fields:
1183 */
1184static int sched_show(struct seq_file *m, void *v)
1185{
1186 struct inode *inode = m->private;
1187 struct task_struct *p;
1188
1189 WARN_ON(!inode);
1190
1191 p = get_proc_task(inode);
1192 if (!p)
1193 return -ESRCH;
1194 proc_sched_show_task(p, m);
1195
1196 put_task_struct(p);
1197
1198 return 0;
1199}
1200
1201static ssize_t
1202sched_write(struct file *file, const char __user *buf,
1203 size_t count, loff_t *offset)
1204{
1205 struct inode *inode = file->f_path.dentry->d_inode;
1206 struct task_struct *p;
1207
1208 WARN_ON(!inode);
1209
1210 p = get_proc_task(inode);
1211 if (!p)
1212 return -ESRCH;
1213 proc_sched_set_task(p);
1214
1215 put_task_struct(p);
1216
1217 return count;
1218}
1219
1220static int sched_open(struct inode *inode, struct file *filp)
1221{
1222 int ret;
1223
1224 ret = single_open(filp, sched_show, NULL);
1225 if (!ret) {
1226 struct seq_file *m = filp->private_data;
1227
1228 m->private = inode;
1229 }
1230 return ret;
1231}
1232
1233static const struct file_operations proc_pid_sched_operations = {
1234 .open = sched_open,
1235 .read = seq_read,
1236 .write = sched_write,
1237 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001238 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001239};
1240
1241#endif
1242
Matt Helsley925d1c42008-04-29 01:01:36 -07001243/*
1244 * We added or removed a vma mapping the executable. The vmas are only mapped
1245 * during exec and are not mapped with the mmap system call.
1246 * Callers must hold down_write() on the mm's mmap_sem for these
1247 */
1248void added_exe_file_vma(struct mm_struct *mm)
1249{
1250 mm->num_exe_file_vmas++;
1251}
1252
1253void removed_exe_file_vma(struct mm_struct *mm)
1254{
1255 mm->num_exe_file_vmas--;
1256 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1257 fput(mm->exe_file);
1258 mm->exe_file = NULL;
1259 }
1260
1261}
1262
1263void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1264{
1265 if (new_exe_file)
1266 get_file(new_exe_file);
1267 if (mm->exe_file)
1268 fput(mm->exe_file);
1269 mm->exe_file = new_exe_file;
1270 mm->num_exe_file_vmas = 0;
1271}
1272
1273struct file *get_mm_exe_file(struct mm_struct *mm)
1274{
1275 struct file *exe_file;
1276
1277 /* We need mmap_sem to protect against races with removal of
1278 * VM_EXECUTABLE vmas */
1279 down_read(&mm->mmap_sem);
1280 exe_file = mm->exe_file;
1281 if (exe_file)
1282 get_file(exe_file);
1283 up_read(&mm->mmap_sem);
1284 return exe_file;
1285}
1286
1287void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1288{
1289 /* It's safe to write the exe_file pointer without exe_file_lock because
1290 * this is called during fork when the task is not yet in /proc */
1291 newmm->exe_file = get_mm_exe_file(oldmm);
1292}
1293
1294static int proc_exe_link(struct inode *inode, struct path *exe_path)
1295{
1296 struct task_struct *task;
1297 struct mm_struct *mm;
1298 struct file *exe_file;
1299
1300 task = get_proc_task(inode);
1301 if (!task)
1302 return -ENOENT;
1303 mm = get_task_mm(task);
1304 put_task_struct(task);
1305 if (!mm)
1306 return -ENOENT;
1307 exe_file = get_mm_exe_file(mm);
1308 mmput(mm);
1309 if (exe_file) {
1310 *exe_path = exe_file->f_path;
1311 path_get(&exe_file->f_path);
1312 fput(exe_file);
1313 return 0;
1314 } else
1315 return -ENOENT;
1316}
1317
Al Viro008b1502005-08-20 00:17:39 +01001318static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct inode *inode = dentry->d_inode;
1321 int error = -EACCES;
1322
1323 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001324 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Eric W. Biederman778c1142006-06-26 00:25:58 -07001326 /* Are we allowed to snoop on the tasks file descriptors? */
1327 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001330 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 nd->last_type = LAST_BIND;
1332out:
Al Viro008b1502005-08-20 00:17:39 +01001333 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001336static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Mel Gormane12ba742007-10-16 01:25:52 -07001338 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001339 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int len;
1341
1342 if (!tmp)
1343 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001344
Jan Blunckcf28b482008-02-14 19:38:44 -08001345 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001346 len = PTR_ERR(pathname);
1347 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001349 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 if (len > buflen)
1352 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001353 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 len = -EFAULT;
1355 out:
1356 free_page((unsigned long)tmp);
1357 return len;
1358}
1359
1360static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1361{
1362 int error = -EACCES;
1363 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001364 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric W. Biederman778c1142006-06-26 00:25:58 -07001366 /* Are we allowed to snoop on the tasks file descriptors? */
1367 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001370 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (error)
1372 goto out;
1373
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001374 error = do_proc_readlink(&path, buffer, buflen);
1375 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return error;
1378}
1379
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001380static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001382 .follow_link = proc_pid_follow_link,
1383 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384};
1385
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001386
1387/* building an inode */
1388
1389static int task_dumpable(struct task_struct *task)
1390{
1391 int dumpable = 0;
1392 struct mm_struct *mm;
1393
1394 task_lock(task);
1395 mm = task->mm;
1396 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001397 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001398 task_unlock(task);
1399 if(dumpable == 1)
1400 return 1;
1401 return 0;
1402}
1403
1404
Eric W. Biederman61a28782006-10-02 02:18:49 -07001405static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001406{
1407 struct inode * inode;
1408 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001409 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001410
1411 /* We need a new inode */
1412
1413 inode = new_inode(sb);
1414 if (!inode)
1415 goto out;
1416
1417 /* Common stuff */
1418 ei = PROC_I(inode);
1419 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001420 inode->i_op = &proc_def_inode_operations;
1421
1422 /*
1423 * grab the reference to task.
1424 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001425 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001426 if (!ei->pid)
1427 goto out_unlock;
1428
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001429 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001430 rcu_read_lock();
1431 cred = __task_cred(task);
1432 inode->i_uid = cred->euid;
1433 inode->i_gid = cred->egid;
1434 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001435 }
1436 security_task_to_inode(task, inode);
1437
1438out:
1439 return inode;
1440
1441out_unlock:
1442 iput(inode);
1443 return NULL;
1444}
1445
1446static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1447{
1448 struct inode *inode = dentry->d_inode;
1449 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001450 const struct cred *cred;
1451
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001452 generic_fillattr(inode, stat);
1453
1454 rcu_read_lock();
1455 stat->uid = 0;
1456 stat->gid = 0;
1457 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1458 if (task) {
1459 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1460 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001461 cred = __task_cred(task);
1462 stat->uid = cred->euid;
1463 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001464 }
1465 }
1466 rcu_read_unlock();
1467 return 0;
1468}
1469
1470/* dentry stuff */
1471
1472/*
1473 * Exceptional case: normally we are not allowed to unhash a busy
1474 * directory. In this case, however, we can do it - no aliasing problems
1475 * due to the way we treat inodes.
1476 *
1477 * Rewrite the inode's ownerships here because the owning task may have
1478 * performed a setuid(), etc.
1479 *
1480 * Before the /proc/pid/status file was created the only way to read
1481 * the effective uid of a /process was to stat /proc/pid. Reading
1482 * /proc/pid/status is slow enough that procps and other packages
1483 * kept stating /proc/pid. To keep the rules in /proc simple I have
1484 * made this apply to all per process world readable and executable
1485 * directories.
1486 */
1487static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1488{
1489 struct inode *inode = dentry->d_inode;
1490 struct task_struct *task = get_proc_task(inode);
David Howellsc69e8d92008-11-14 10:39:19 +11001491 const struct cred *cred;
1492
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001493 if (task) {
1494 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1495 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001496 rcu_read_lock();
1497 cred = __task_cred(task);
1498 inode->i_uid = cred->euid;
1499 inode->i_gid = cred->egid;
1500 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001501 } else {
1502 inode->i_uid = 0;
1503 inode->i_gid = 0;
1504 }
1505 inode->i_mode &= ~(S_ISUID | S_ISGID);
1506 security_task_to_inode(task, inode);
1507 put_task_struct(task);
1508 return 1;
1509 }
1510 d_drop(dentry);
1511 return 0;
1512}
1513
1514static int pid_delete_dentry(struct dentry * dentry)
1515{
1516 /* Is the task we represent dead?
1517 * If so, then don't put the dentry on the lru list,
1518 * kill it immediately.
1519 */
1520 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1521}
1522
1523static struct dentry_operations pid_dentry_operations =
1524{
1525 .d_revalidate = pid_revalidate,
1526 .d_delete = pid_delete_dentry,
1527};
1528
1529/* Lookups */
1530
Eric Dumazetc5141e62007-05-08 00:26:15 -07001531typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1532 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001533
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001534/*
1535 * Fill a directory entry.
1536 *
1537 * If possible create the dcache entry and derive our inode number and
1538 * file type from dcache entry.
1539 *
1540 * Since all of the proc inode numbers are dynamically generated, the inode
1541 * numbers do not exist until the inode is cache. This means creating the
1542 * the dcache entry in readdir is necessary to keep the inode numbers
1543 * reported by readdir in sync with the inode numbers reported
1544 * by stat.
1545 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001546static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1547 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001548 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001549{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001550 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001551 struct inode *inode;
1552 struct qstr qname;
1553 ino_t ino = 0;
1554 unsigned type = DT_UNKNOWN;
1555
1556 qname.name = name;
1557 qname.len = len;
1558 qname.hash = full_name_hash(name, len);
1559
1560 child = d_lookup(dir, &qname);
1561 if (!child) {
1562 struct dentry *new;
1563 new = d_alloc(dir, &qname);
1564 if (new) {
1565 child = instantiate(dir->d_inode, new, task, ptr);
1566 if (child)
1567 dput(new);
1568 else
1569 child = new;
1570 }
1571 }
1572 if (!child || IS_ERR(child) || !child->d_inode)
1573 goto end_instantiate;
1574 inode = child->d_inode;
1575 if (inode) {
1576 ino = inode->i_ino;
1577 type = inode->i_mode >> 12;
1578 }
1579 dput(child);
1580end_instantiate:
1581 if (!ino)
1582 ino = find_inode_number(dir, &qname);
1583 if (!ino)
1584 ino = 1;
1585 return filldir(dirent, name, len, filp->f_pos, ino, type);
1586}
1587
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001588static unsigned name_to_int(struct dentry *dentry)
1589{
1590 const char *name = dentry->d_name.name;
1591 int len = dentry->d_name.len;
1592 unsigned n = 0;
1593
1594 if (len > 1 && *name == '0')
1595 goto out;
1596 while (len-- > 0) {
1597 unsigned c = *name++ - '0';
1598 if (c > 9)
1599 goto out;
1600 if (n >= (~0U-9)/10)
1601 goto out;
1602 n *= 10;
1603 n += c;
1604 }
1605 return n;
1606out:
1607 return ~0U;
1608}
1609
Miklos Szeredi27932742007-05-08 00:26:17 -07001610#define PROC_FDINFO_MAX 64
1611
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001612static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001613{
1614 struct task_struct *task = get_proc_task(inode);
1615 struct files_struct *files = NULL;
1616 struct file *file;
1617 int fd = proc_fd(inode);
1618
1619 if (task) {
1620 files = get_files_struct(task);
1621 put_task_struct(task);
1622 }
1623 if (files) {
1624 /*
1625 * We are not taking a ref to the file structure, so we must
1626 * hold ->file_lock.
1627 */
1628 spin_lock(&files->file_lock);
1629 file = fcheck_files(files, fd);
1630 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001631 if (path) {
1632 *path = file->f_path;
1633 path_get(&file->f_path);
1634 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001635 if (info)
1636 snprintf(info, PROC_FDINFO_MAX,
1637 "pos:\t%lli\n"
1638 "flags:\t0%o\n",
1639 (long long) file->f_pos,
1640 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001641 spin_unlock(&files->file_lock);
1642 put_files_struct(files);
1643 return 0;
1644 }
1645 spin_unlock(&files->file_lock);
1646 put_files_struct(files);
1647 }
1648 return -ENOENT;
1649}
1650
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001651static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001652{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001653 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001654}
1655
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001656static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1657{
1658 struct inode *inode = dentry->d_inode;
1659 struct task_struct *task = get_proc_task(inode);
1660 int fd = proc_fd(inode);
1661 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001662 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001663
1664 if (task) {
1665 files = get_files_struct(task);
1666 if (files) {
1667 rcu_read_lock();
1668 if (fcheck_files(files, fd)) {
1669 rcu_read_unlock();
1670 put_files_struct(files);
1671 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001672 rcu_read_lock();
1673 cred = __task_cred(task);
1674 inode->i_uid = cred->euid;
1675 inode->i_gid = cred->egid;
1676 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001677 } else {
1678 inode->i_uid = 0;
1679 inode->i_gid = 0;
1680 }
1681 inode->i_mode &= ~(S_ISUID | S_ISGID);
1682 security_task_to_inode(task, inode);
1683 put_task_struct(task);
1684 return 1;
1685 }
1686 rcu_read_unlock();
1687 put_files_struct(files);
1688 }
1689 put_task_struct(task);
1690 }
1691 d_drop(dentry);
1692 return 0;
1693}
1694
1695static struct dentry_operations tid_fd_dentry_operations =
1696{
1697 .d_revalidate = tid_fd_revalidate,
1698 .d_delete = pid_delete_dentry,
1699};
1700
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001701static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001702 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001703{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001704 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001705 struct file *file;
1706 struct files_struct *files;
1707 struct inode *inode;
1708 struct proc_inode *ei;
1709 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001710
Eric W. Biederman61a28782006-10-02 02:18:49 -07001711 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001712 if (!inode)
1713 goto out;
1714 ei = PROC_I(inode);
1715 ei->fd = fd;
1716 files = get_files_struct(task);
1717 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001718 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001719 inode->i_mode = S_IFLNK;
1720
1721 /*
1722 * We are not taking a ref to the file structure, so we must
1723 * hold ->file_lock.
1724 */
1725 spin_lock(&files->file_lock);
1726 file = fcheck_files(files, fd);
1727 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001728 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001729 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001730 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001731 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001732 inode->i_mode |= S_IWUSR | S_IXUSR;
1733 spin_unlock(&files->file_lock);
1734 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001735
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001736 inode->i_op = &proc_pid_link_inode_operations;
1737 inode->i_size = 64;
1738 ei->op.proc_get_link = proc_fd_link;
1739 dentry->d_op = &tid_fd_dentry_operations;
1740 d_add(dentry, inode);
1741 /* Close the race of the process dying before we return the dentry */
1742 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001743 error = NULL;
1744
1745 out:
1746 return error;
1747out_unlock:
1748 spin_unlock(&files->file_lock);
1749 put_files_struct(files);
1750out_iput:
1751 iput(inode);
1752 goto out;
1753}
1754
Miklos Szeredi27932742007-05-08 00:26:17 -07001755static struct dentry *proc_lookupfd_common(struct inode *dir,
1756 struct dentry *dentry,
1757 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001758{
1759 struct task_struct *task = get_proc_task(dir);
1760 unsigned fd = name_to_int(dentry);
1761 struct dentry *result = ERR_PTR(-ENOENT);
1762
1763 if (!task)
1764 goto out_no_task;
1765 if (fd == ~0U)
1766 goto out;
1767
Miklos Szeredi27932742007-05-08 00:26:17 -07001768 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001769out:
1770 put_task_struct(task);
1771out_no_task:
1772 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001773}
1774
Miklos Szeredi27932742007-05-08 00:26:17 -07001775static int proc_readfd_common(struct file * filp, void * dirent,
1776 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001778 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001779 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001780 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001781 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct files_struct * files;
1784
1785 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001786 if (!p)
1787 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
1790 fd = filp->f_pos;
1791 switch (fd) {
1792 case 0:
1793 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1794 goto out;
1795 filp->f_pos++;
1796 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001797 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1799 goto out;
1800 filp->f_pos++;
1801 default:
1802 files = get_files_struct(p);
1803 if (!files)
1804 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001805 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001807 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001809 char name[PROC_NUMBUF];
1810 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 if (!fcheck_files(files, fd))
1813 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001814 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Miklos Szeredi27932742007-05-08 00:26:17 -07001816 len = snprintf(name, sizeof(name), "%d", fd);
1817 if (proc_fill_cache(filp, dirent, filldir,
1818 name, len, instantiate,
1819 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001820 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 break;
1822 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001823 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001825 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 put_files_struct(files);
1827 }
1828out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001829 put_task_struct(p);
1830out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 return retval;
1832}
1833
Miklos Szeredi27932742007-05-08 00:26:17 -07001834static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1835 struct nameidata *nd)
1836{
1837 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1838}
1839
1840static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1841{
1842 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1843}
1844
1845static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1846 size_t len, loff_t *ppos)
1847{
1848 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001849 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001850 if (!err)
1851 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1852 return err;
1853}
1854
1855static const struct file_operations proc_fdinfo_file_operations = {
1856 .open = nonseekable_open,
1857 .read = proc_fdinfo_read,
1858};
1859
Arjan van de Ven00977a52007-02-12 00:55:34 -08001860static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 .read = generic_read_dir,
1862 .readdir = proc_readfd,
1863};
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001866 * /proc/pid/fd needs a special permission handler so that a process can still
1867 * access /proc/self/fd after it has executed a setuid().
1868 */
Al Viroe6305c42008-07-15 21:03:57 -04001869static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001870{
1871 int rv;
1872
1873 rv = generic_permission(inode, mask, NULL);
1874 if (rv == 0)
1875 return 0;
1876 if (task_pid(current) == proc_pid(inode))
1877 rv = 0;
1878 return rv;
1879}
1880
1881/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 * proc directories can do almost nothing..
1883 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001884static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001886 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001887 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888};
1889
Miklos Szeredi27932742007-05-08 00:26:17 -07001890static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1891 struct dentry *dentry, struct task_struct *task, const void *ptr)
1892{
1893 unsigned fd = *(unsigned *)ptr;
1894 struct inode *inode;
1895 struct proc_inode *ei;
1896 struct dentry *error = ERR_PTR(-ENOENT);
1897
1898 inode = proc_pid_make_inode(dir->i_sb, task);
1899 if (!inode)
1900 goto out;
1901 ei = PROC_I(inode);
1902 ei->fd = fd;
1903 inode->i_mode = S_IFREG | S_IRUSR;
1904 inode->i_fop = &proc_fdinfo_file_operations;
1905 dentry->d_op = &tid_fd_dentry_operations;
1906 d_add(dentry, inode);
1907 /* Close the race of the process dying before we return the dentry */
1908 if (tid_fd_revalidate(dentry, NULL))
1909 error = NULL;
1910
1911 out:
1912 return error;
1913}
1914
1915static struct dentry *proc_lookupfdinfo(struct inode *dir,
1916 struct dentry *dentry,
1917 struct nameidata *nd)
1918{
1919 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1920}
1921
1922static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1923{
1924 return proc_readfd_common(filp, dirent, filldir,
1925 proc_fdinfo_instantiate);
1926}
1927
1928static const struct file_operations proc_fdinfo_operations = {
1929 .read = generic_read_dir,
1930 .readdir = proc_readfdinfo,
1931};
1932
1933/*
1934 * proc directories can do almost nothing..
1935 */
1936static const struct inode_operations proc_fdinfo_inode_operations = {
1937 .lookup = proc_lookupfdinfo,
1938 .setattr = proc_setattr,
1939};
1940
1941
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001942static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001943 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001944{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001945 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001946 struct inode *inode;
1947 struct proc_inode *ei;
1948 struct dentry *error = ERR_PTR(-EINVAL);
1949
Eric W. Biederman61a28782006-10-02 02:18:49 -07001950 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001951 if (!inode)
1952 goto out;
1953
1954 ei = PROC_I(inode);
1955 inode->i_mode = p->mode;
1956 if (S_ISDIR(inode->i_mode))
1957 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1958 if (p->iop)
1959 inode->i_op = p->iop;
1960 if (p->fop)
1961 inode->i_fop = p->fop;
1962 ei->op = p->op;
1963 dentry->d_op = &pid_dentry_operations;
1964 d_add(dentry, inode);
1965 /* Close the race of the process dying before we return the dentry */
1966 if (pid_revalidate(dentry, NULL))
1967 error = NULL;
1968out:
1969 return error;
1970}
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972static struct dentry *proc_pident_lookup(struct inode *dir,
1973 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001974 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001975 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001978 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001979 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001980 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001982 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 inode = NULL;
1984
Eric W. Biederman99f89552006-06-26 00:25:55 -07001985 if (!task)
1986 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001988 /*
1989 * Yes, it does not scale. And it should not. Don't add
1990 * new entries into /proc/<tgid>/ without very good reasons.
1991 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001992 last = &ents[nents - 1];
1993 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (p->len != dentry->d_name.len)
1995 continue;
1996 if (!memcmp(dentry->d_name.name, p->name, p->len))
1997 break;
1998 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001999 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 goto out;
2001
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002002 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002004 put_task_struct(task);
2005out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002006 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Eric Dumazetc5141e62007-05-08 00:26:15 -07002009static int proc_pident_fill_cache(struct file *filp, void *dirent,
2010 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002011{
2012 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2013 proc_pident_instantiate, task, p);
2014}
2015
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002016static int proc_pident_readdir(struct file *filp,
2017 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002018 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002019{
2020 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002021 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002022 struct inode *inode = dentry->d_inode;
2023 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002024 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002025 ino_t ino;
2026 int ret;
2027
2028 ret = -ENOENT;
2029 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002030 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002031
2032 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002033 i = filp->f_pos;
2034 switch (i) {
2035 case 0:
2036 ino = inode->i_ino;
2037 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2038 goto out;
2039 i++;
2040 filp->f_pos++;
2041 /* fall through */
2042 case 1:
2043 ino = parent_ino(dentry);
2044 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2045 goto out;
2046 i++;
2047 filp->f_pos++;
2048 /* fall through */
2049 default:
2050 i -= 2;
2051 if (i >= nents) {
2052 ret = 1;
2053 goto out;
2054 }
2055 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002056 last = &ents[nents - 1];
2057 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002058 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002059 goto out;
2060 filp->f_pos++;
2061 p++;
2062 }
2063 }
2064
2065 ret = 1;
2066out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002067 put_task_struct(task);
2068out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002069 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002073static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2074 size_t count, loff_t *ppos)
2075{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002076 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002077 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002078 ssize_t length;
2079 struct task_struct *task = get_proc_task(inode);
2080
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002081 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002082 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002083
2084 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002085 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002086 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002087 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002088 if (length > 0)
2089 length = simple_read_from_buffer(buf, count, ppos, p, length);
2090 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002091 return length;
2092}
2093
2094static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2095 size_t count, loff_t *ppos)
2096{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002097 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002098 char *page;
2099 ssize_t length;
2100 struct task_struct *task = get_proc_task(inode);
2101
2102 length = -ESRCH;
2103 if (!task)
2104 goto out_no_task;
2105 if (count > PAGE_SIZE)
2106 count = PAGE_SIZE;
2107
2108 /* No partial writes. */
2109 length = -EINVAL;
2110 if (*ppos != 0)
2111 goto out;
2112
2113 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002114 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002115 if (!page)
2116 goto out;
2117
2118 length = -EFAULT;
2119 if (copy_from_user(page, buf, count))
2120 goto out_free;
2121
2122 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002123 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002124 (void*)page, count);
2125out_free:
2126 free_page((unsigned long) page);
2127out:
2128 put_task_struct(task);
2129out_no_task:
2130 return length;
2131}
2132
Arjan van de Ven00977a52007-02-12 00:55:34 -08002133static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002134 .read = proc_pid_attr_read,
2135 .write = proc_pid_attr_write,
2136};
2137
Eric Dumazetc5141e62007-05-08 00:26:15 -07002138static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002139 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2140 REG("prev", S_IRUGO, pid_attr),
2141 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2142 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2143 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2144 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002145};
2146
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002147static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 void * dirent, filldir_t filldir)
2149{
2150 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002151 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Arjan van de Ven00977a52007-02-12 00:55:34 -08002154static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002156 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157};
2158
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002159static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct dentry *dentry, struct nameidata *nd)
2161{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002162 return proc_pident_lookup(dir, dentry,
2163 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002166static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002167 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002168 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002169 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170};
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172#endif
2173
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002174#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2175static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2176 size_t count, loff_t *ppos)
2177{
2178 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2179 struct mm_struct *mm;
2180 char buffer[PROC_NUMBUF];
2181 size_t len;
2182 int ret;
2183
2184 if (!task)
2185 return -ESRCH;
2186
2187 ret = 0;
2188 mm = get_task_mm(task);
2189 if (mm) {
2190 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2191 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2192 MMF_DUMP_FILTER_SHIFT));
2193 mmput(mm);
2194 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2195 }
2196
2197 put_task_struct(task);
2198
2199 return ret;
2200}
2201
2202static ssize_t proc_coredump_filter_write(struct file *file,
2203 const char __user *buf,
2204 size_t count,
2205 loff_t *ppos)
2206{
2207 struct task_struct *task;
2208 struct mm_struct *mm;
2209 char buffer[PROC_NUMBUF], *end;
2210 unsigned int val;
2211 int ret;
2212 int i;
2213 unsigned long mask;
2214
2215 ret = -EFAULT;
2216 memset(buffer, 0, sizeof(buffer));
2217 if (count > sizeof(buffer) - 1)
2218 count = sizeof(buffer) - 1;
2219 if (copy_from_user(buffer, buf, count))
2220 goto out_no_task;
2221
2222 ret = -EINVAL;
2223 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2224 if (*end == '\n')
2225 end++;
2226 if (end - buffer == 0)
2227 goto out_no_task;
2228
2229 ret = -ESRCH;
2230 task = get_proc_task(file->f_dentry->d_inode);
2231 if (!task)
2232 goto out_no_task;
2233
2234 ret = end - buffer;
2235 mm = get_task_mm(task);
2236 if (!mm)
2237 goto out_no_mm;
2238
2239 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2240 if (val & mask)
2241 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2242 else
2243 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2244 }
2245
2246 mmput(mm);
2247 out_no_mm:
2248 put_task_struct(task);
2249 out_no_task:
2250 return ret;
2251}
2252
2253static const struct file_operations proc_coredump_filter_operations = {
2254 .read = proc_coredump_filter_read,
2255 .write = proc_coredump_filter_write,
2256};
2257#endif
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259/*
2260 * /proc/self:
2261 */
2262static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2263 int buflen)
2264{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002265 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002266 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002267 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002268 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002269 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002270 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return vfs_readlink(dentry,buffer,buflen,tmp);
2272}
2273
Al Viro008b1502005-08-20 00:17:39 +01002274static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002276 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002277 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002278 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002279 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002280 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002281 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002282 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002283}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002285static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 .readlink = proc_self_readlink,
2287 .follow_link = proc_self_follow_link,
2288};
2289
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002290/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002291 * proc base
2292 *
2293 * These are the directory entries in the root directory of /proc
2294 * that properly belong to the /proc filesystem, as they describe
2295 * describe something that is process related.
2296 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002297static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002298 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002299 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002300};
2301
2302/*
2303 * Exceptional case: normally we are not allowed to unhash a busy
2304 * directory. In this case, however, we can do it - no aliasing problems
2305 * due to the way we treat inodes.
2306 */
2307static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2308{
2309 struct inode *inode = dentry->d_inode;
2310 struct task_struct *task = get_proc_task(inode);
2311 if (task) {
2312 put_task_struct(task);
2313 return 1;
2314 }
2315 d_drop(dentry);
2316 return 0;
2317}
2318
2319static struct dentry_operations proc_base_dentry_operations =
2320{
2321 .d_revalidate = proc_base_revalidate,
2322 .d_delete = pid_delete_dentry,
2323};
2324
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002325static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002326 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002327{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002328 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002329 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002330 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002331 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002332
2333 /* Allocate the inode */
2334 error = ERR_PTR(-ENOMEM);
2335 inode = new_inode(dir->i_sb);
2336 if (!inode)
2337 goto out;
2338
2339 /* Initialize the inode */
2340 ei = PROC_I(inode);
2341 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002342
2343 /*
2344 * grab the reference to the task.
2345 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002346 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002347 if (!ei->pid)
2348 goto out_iput;
2349
Eric W. Biederman801199c2006-10-02 02:18:48 -07002350 inode->i_mode = p->mode;
2351 if (S_ISDIR(inode->i_mode))
2352 inode->i_nlink = 2;
2353 if (S_ISLNK(inode->i_mode))
2354 inode->i_size = 64;
2355 if (p->iop)
2356 inode->i_op = p->iop;
2357 if (p->fop)
2358 inode->i_fop = p->fop;
2359 ei->op = p->op;
2360 dentry->d_op = &proc_base_dentry_operations;
2361 d_add(dentry, inode);
2362 error = NULL;
2363out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002364 return error;
2365out_iput:
2366 iput(inode);
2367 goto out;
2368}
2369
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002370static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2371{
2372 struct dentry *error;
2373 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002374 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002375
2376 error = ERR_PTR(-ENOENT);
2377
2378 if (!task)
2379 goto out_no_task;
2380
2381 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002382 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2383 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002384 if (p->len != dentry->d_name.len)
2385 continue;
2386 if (!memcmp(dentry->d_name.name, p->name, p->len))
2387 break;
2388 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002389 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002390 goto out;
2391
2392 error = proc_base_instantiate(dir, dentry, task, p);
2393
2394out:
2395 put_task_struct(task);
2396out_no_task:
2397 return error;
2398}
2399
Eric Dumazetc5141e62007-05-08 00:26:15 -07002400static int proc_base_fill_cache(struct file *filp, void *dirent,
2401 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002402{
2403 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2404 proc_base_instantiate, task, p);
2405}
2406
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002407#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002408static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002409{
Andrea Righi940389b2008-07-28 00:48:12 +02002410 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002411 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002412
Andrea Righi59954772008-07-27 17:29:15 +02002413 if (whole && lock_task_sighand(task, &flags)) {
2414 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002415
Andrea Righi59954772008-07-27 17:29:15 +02002416 task_io_accounting_add(&acct, &task->signal->ioac);
2417 while_each_thread(task, t)
2418 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002419
Andrea Righi59954772008-07-27 17:29:15 +02002420 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002421 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002422 return sprintf(buffer,
2423 "rchar: %llu\n"
2424 "wchar: %llu\n"
2425 "syscr: %llu\n"
2426 "syscw: %llu\n"
2427 "read_bytes: %llu\n"
2428 "write_bytes: %llu\n"
2429 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002430 (unsigned long long)acct.rchar,
2431 (unsigned long long)acct.wchar,
2432 (unsigned long long)acct.syscr,
2433 (unsigned long long)acct.syscw,
2434 (unsigned long long)acct.read_bytes,
2435 (unsigned long long)acct.write_bytes,
2436 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002437}
Andrea Righi297c5d92008-07-25 01:48:49 -07002438
2439static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2440{
2441 return do_io_accounting(task, buffer, 0);
2442}
2443
2444static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2445{
2446 return do_io_accounting(task, buffer, 1);
2447}
2448#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002449
Kees Cook47830722008-10-06 03:11:58 +04002450static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2451 struct pid *pid, struct task_struct *task)
2452{
2453 seq_printf(m, "%08x\n", task->personality);
2454 return 0;
2455}
2456
Eric W. Biederman801199c2006-10-02 02:18:48 -07002457/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002458 * Thread groups
2459 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002460static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002461static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002462
Eric Dumazetc5141e62007-05-08 00:26:15 -07002463static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002464 DIR("task", S_IRUGO|S_IXUGO, task),
2465 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002466 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002467#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002468 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002469#endif
James Pearson315e28c2007-10-16 23:30:17 -07002470 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002471 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002472 ONE("status", S_IRUGO, pid_status),
Kees Cook47830722008-10-06 03:11:58 +04002473 ONE("personality", S_IRUSR, pid_personality),
Neil Hormand85f50d2007-10-18 23:40:37 -07002474 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002475#ifdef CONFIG_SCHED_DEBUG
2476 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2477#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002478#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2479 INF("syscall", S_IRUSR, pid_syscall),
2480#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002481 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002482 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002483 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002484 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002485#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002486 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002487#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002488 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002489 LNK("cwd", cwd),
2490 LNK("root", root),
2491 LNK("exe", exe),
2492 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002493 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002494 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002495#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002496 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002497 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002498 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002499#endif
2500#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002501 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502#endif
2503#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002504 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505#endif
2506#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002507 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002508#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002509#ifdef CONFIG_LATENCYTOP
2510 REG("latency", S_IRUGO, lstats),
2511#endif
Paul Menage8793d852007-10-18 23:39:39 -07002512#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002513 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514#endif
Paul Menagea4243162007-10-18 23:39:35 -07002515#ifdef CONFIG_CGROUPS
2516 REG("cgroup", S_IRUGO, cgroup),
2517#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002518 INF("oom_score", S_IRUGO, oom_score),
2519 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002520#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002521 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Steve Grubb6ee65042008-04-29 15:01:13 -04002522 REG("sessionid", S_IRUGO, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002524#ifdef CONFIG_FAULT_INJECTION
2525 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2526#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002527#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2528 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2529#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002530#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002531 INF("io", S_IRUGO, tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002532#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002533};
2534
2535static int proc_tgid_base_readdir(struct file * filp,
2536 void * dirent, filldir_t filldir)
2537{
2538 return proc_pident_readdir(filp,dirent,filldir,
2539 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2540}
2541
Arjan van de Ven00977a52007-02-12 00:55:34 -08002542static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543 .read = generic_read_dir,
2544 .readdir = proc_tgid_base_readdir,
2545};
2546
2547static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002548 return proc_pident_lookup(dir, dentry,
2549 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002550}
2551
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002552static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553 .lookup = proc_tgid_base_lookup,
2554 .getattr = pid_getattr,
2555 .setattr = proc_setattr,
2556};
2557
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002558static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002560 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002561 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002562 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Eric W. Biederman48e64842006-06-26 00:25:48 -07002564 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002565 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2566 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002567 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002568 if (!(current->flags & PF_EXITING))
2569 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002570 d_drop(dentry);
2571 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002574 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002575 goto out;
2576
2577 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002578 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2579 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002580 if (!leader)
2581 goto out;
2582
2583 name.name = "task";
2584 name.len = strlen(name.name);
2585 dir = d_hash_and_lookup(leader, &name);
2586 if (!dir)
2587 goto out_put_leader;
2588
2589 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002590 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002591 dentry = d_hash_and_lookup(dir, &name);
2592 if (dentry) {
2593 shrink_dcache_parent(dentry);
2594 d_drop(dentry);
2595 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002597
2598 dput(dir);
2599out_put_leader:
2600 dput(leader);
2601out:
2602 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
Randy Dunlap0895e912007-10-21 21:00:10 -07002605/**
2606 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2607 * @task: task that should be flushed.
2608 *
2609 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002610 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002611 * in. This call is supposed to do all of this job.
2612 *
2613 * Looks in the dcache for
2614 * /proc/@pid
2615 * /proc/@tgid/task/@pid
2616 * if either directory is present flushes it and all of it'ts children
2617 * from the dcache.
2618 *
2619 * It is safe and reasonable to cache /proc entries for a task until
2620 * that task exits. After that they just clog up the dcache with
2621 * useless entries, possibly causing useful dcache entries to be
2622 * flushed instead. This routine is proved to flush those useless
2623 * dcache entries at process exit time.
2624 *
2625 * NOTE: This routine is just an optimization so it does not guarantee
2626 * that no dcache entries will exist at process exit time it
2627 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002628 */
2629
2630void proc_flush_task(struct task_struct *task)
2631{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002632 int i;
2633 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002634 struct upid *upid;
2635
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002636 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002637 if (thread_group_leader(task))
2638 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002639
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002640 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002641 upid = &pid->numbers[i];
2642 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002643 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002644 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002645
2646 upid = &pid->numbers[pid->level];
2647 if (upid->nr == 1)
2648 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002649}
2650
Adrian Bunk9711ef92006-12-06 20:38:31 -08002651static struct dentry *proc_pid_instantiate(struct inode *dir,
2652 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002653 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002654{
2655 struct dentry *error = ERR_PTR(-ENOENT);
2656 struct inode *inode;
2657
Eric W. Biederman61a28782006-10-02 02:18:49 -07002658 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002659 if (!inode)
2660 goto out;
2661
2662 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2663 inode->i_op = &proc_tgid_base_inode_operations;
2664 inode->i_fop = &proc_tgid_base_operations;
2665 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002666
2667 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2668 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002669
2670 dentry->d_op = &pid_dentry_operations;
2671
2672 d_add(dentry, inode);
2673 /* Close the race of the process dying before we return the dentry */
2674 if (pid_revalidate(dentry, NULL))
2675 error = NULL;
2676out:
2677 return error;
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2681{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002682 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002685 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Eric W. Biederman801199c2006-10-02 02:18:48 -07002687 result = proc_base_lookup(dir, dentry);
2688 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2689 goto out;
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 tgid = name_to_int(dentry);
2692 if (tgid == ~0U)
2693 goto out;
2694
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002695 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002696 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002697 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (task)
2699 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (!task)
2702 goto out;
2703
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002704 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002705 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002707 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002711 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002712 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002714struct tgid_iter {
2715 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002716 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002717};
2718static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2719{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002720 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002722 if (iter.task)
2723 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002724 rcu_read_lock();
2725retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002726 iter.task = NULL;
2727 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002728 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002729 iter.tgid = pid_nr_ns(pid, ns);
2730 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002731 /* What we to know is if the pid we have find is the
2732 * pid of a thread_group_leader. Testing for task
2733 * being a thread_group_leader is the obvious thing
2734 * todo but there is a window when it fails, due to
2735 * the pid transfer logic in de_thread.
2736 *
2737 * So we perform the straight forward test of seeing
2738 * if the pid we have found is the pid of a thread
2739 * group leader, and don't worry if the task we have
2740 * found doesn't happen to be a thread group leader.
2741 * As we don't care in the case of readdir.
2742 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002743 if (!iter.task || !has_group_leader_pid(iter.task)) {
2744 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002745 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002746 }
2747 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002749 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002750 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002753#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Eric W. Biederman61a28782006-10-02 02:18:49 -07002755static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002756 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002757{
2758 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002759 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002760 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002761 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764/* for the /proc/ directory itself, after non-process stuff has been done */
2765int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002768 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002769 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002770 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Eric W. Biederman61a28782006-10-02 02:18:49 -07002772 if (!reaper)
2773 goto out_no_task;
2774
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002775 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002776 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002777 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002781 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002782 iter.task = NULL;
2783 iter.tgid = filp->f_pos - TGID_OFFSET;
2784 for (iter = next_tgid(ns, iter);
2785 iter.task;
2786 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2787 filp->f_pos = iter.tgid + TGID_OFFSET;
2788 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2789 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002790 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002793 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2794out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002795 put_task_struct(reaper);
2796out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return 0;
2798}
2799
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002800/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002801 * Tasks
2802 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002803static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002804 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002805 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002806 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002807 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002808 ONE("status", S_IRUGO, pid_status),
Kees Cook47830722008-10-06 03:11:58 +04002809 ONE("personality", S_IRUSR, pid_personality),
Neil Hormand85f50d2007-10-18 23:40:37 -07002810 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002811#ifdef CONFIG_SCHED_DEBUG
2812 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2813#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002814#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2815 INF("syscall", S_IRUSR, pid_syscall),
2816#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002817 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002818 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002819 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002820 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002821#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002822 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002823#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002824 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002825 LNK("cwd", cwd),
2826 LNK("root", root),
2827 LNK("exe", exe),
2828 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002829 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002830#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002831 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002832 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002833 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002834#endif
2835#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002836 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002837#endif
2838#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002839 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002840#endif
2841#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002842 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002843#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002844#ifdef CONFIG_LATENCYTOP
2845 REG("latency", S_IRUGO, lstats),
2846#endif
Paul Menage8793d852007-10-18 23:39:39 -07002847#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002848 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002849#endif
Paul Menagea4243162007-10-18 23:39:35 -07002850#ifdef CONFIG_CGROUPS
2851 REG("cgroup", S_IRUGO, cgroup),
2852#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002853 INF("oom_score", S_IRUGO, oom_score),
2854 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002855#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002856 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002857 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002858#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002859#ifdef CONFIG_FAULT_INJECTION
2860 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2861#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002862#ifdef CONFIG_TASK_IO_ACCOUNTING
2863 INF("io", S_IRUGO, tid_io_accounting),
2864#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002865};
2866
2867static int proc_tid_base_readdir(struct file * filp,
2868 void * dirent, filldir_t filldir)
2869{
2870 return proc_pident_readdir(filp,dirent,filldir,
2871 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2872}
2873
2874static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002875 return proc_pident_lookup(dir, dentry,
2876 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002877}
2878
Arjan van de Ven00977a52007-02-12 00:55:34 -08002879static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002880 .read = generic_read_dir,
2881 .readdir = proc_tid_base_readdir,
2882};
2883
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002884static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002885 .lookup = proc_tid_base_lookup,
2886 .getattr = pid_getattr,
2887 .setattr = proc_setattr,
2888};
2889
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002890static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002891 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002892{
2893 struct dentry *error = ERR_PTR(-ENOENT);
2894 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002895 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002896
2897 if (!inode)
2898 goto out;
2899 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2900 inode->i_op = &proc_tid_base_inode_operations;
2901 inode->i_fop = &proc_tid_base_operations;
2902 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002903
2904 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2905 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002906
2907 dentry->d_op = &pid_dentry_operations;
2908
2909 d_add(dentry, inode);
2910 /* Close the race of the process dying before we return the dentry */
2911 if (pid_revalidate(dentry, NULL))
2912 error = NULL;
2913out:
2914 return error;
2915}
2916
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2918{
2919 struct dentry *result = ERR_PTR(-ENOENT);
2920 struct task_struct *task;
2921 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002922 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002923 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002924
2925 if (!leader)
2926 goto out_no_task;
2927
2928 tid = name_to_int(dentry);
2929 if (tid == ~0U)
2930 goto out;
2931
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002932 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002933 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002934 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935 if (task)
2936 get_task_struct(task);
2937 rcu_read_unlock();
2938 if (!task)
2939 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002940 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941 goto out_drop_task;
2942
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002943 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002944out_drop_task:
2945 put_task_struct(task);
2946out:
2947 put_task_struct(leader);
2948out_no_task:
2949 return result;
2950}
2951
2952/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002953 * Find the first tid of a thread group to return to user space.
2954 *
2955 * Usually this is just the thread group leader, but if the users
2956 * buffer was too small or there was a seek into the middle of the
2957 * directory we have more work todo.
2958 *
2959 * In the case of a short read we start with find_task_by_pid.
2960 *
2961 * In the case of a seek we start with the leader and walk nr
2962 * threads past it.
2963 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002964static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002965 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002966{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002967 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002968
Eric W. Biedermancc288732006-06-26 00:26:01 -07002969 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002970 /* Attempt to start with the pid of a thread */
2971 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002972 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002973 if (pos && (pos->group_leader == leader))
2974 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002975 }
2976
2977 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002978 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002979 if (nr && nr >= get_nr_threads(leader))
2980 goto out;
2981
2982 /* If we haven't found our starting place yet start
2983 * with the leader and walk nr threads forward.
2984 */
2985 for (pos = leader; nr > 0; --nr) {
2986 pos = next_thread(pos);
2987 if (pos == leader) {
2988 pos = NULL;
2989 goto out;
2990 }
2991 }
2992found:
2993 get_task_struct(pos);
2994out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002995 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002996 return pos;
2997}
2998
2999/*
3000 * Find the next thread in the thread list.
3001 * Return NULL if there is an error or no next thread.
3002 *
3003 * The reference to the input task_struct is released.
3004 */
3005static struct task_struct *next_tid(struct task_struct *start)
3006{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003007 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003008 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003009 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003010 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003011 if (thread_group_leader(pos))
3012 pos = NULL;
3013 else
3014 get_task_struct(pos);
3015 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003016 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003017 put_task_struct(start);
3018 return pos;
3019}
3020
Eric W. Biederman61a28782006-10-02 02:18:49 -07003021static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3022 struct task_struct *task, int tid)
3023{
3024 char name[PROC_NUMBUF];
3025 int len = snprintf(name, sizeof(name), "%d", tid);
3026 return proc_fill_cache(filp, dirent, filldir, name, len,
3027 proc_task_instantiate, task, NULL);
3028}
3029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030/* for the /proc/TGID/task/ directories */
3031static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3032{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003033 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003035 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003036 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 int retval = -ENOENT;
3038 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003039 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003041 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003043 task = get_proc_task(inode);
3044 if (!task)
3045 goto out_no_task;
3046 rcu_read_lock();
3047 if (pid_alive(task)) {
3048 leader = task->group_leader;
3049 get_task_struct(leader);
3050 }
3051 rcu_read_unlock();
3052 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003053 if (!leader)
3054 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 retval = 0;
3056
3057 switch (pos) {
3058 case 0:
3059 ino = inode->i_ino;
3060 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3061 goto out;
3062 pos++;
3063 /* fall through */
3064 case 1:
3065 ino = parent_ino(dentry);
3066 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3067 goto out;
3068 pos++;
3069 /* fall through */
3070 }
3071
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003072 /* f_version caches the tgid value that the last readdir call couldn't
3073 * return. lseek aka telldir automagically resets f_version to 0.
3074 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003075 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003076 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003077 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003078 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003079 task;
3080 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003081 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003082 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003083 /* returning this tgid failed, save it as the first
3084 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003085 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003086 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090out:
3091 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003092 put_task_struct(leader);
3093out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return retval;
3095}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003096
3097static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3098{
3099 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003100 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003101 generic_fillattr(inode, stat);
3102
Eric W. Biederman99f89552006-06-26 00:25:55 -07003103 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003104 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003105 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003106 }
3107
3108 return 0;
3109}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003110
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003111static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003112 .lookup = proc_task_lookup,
3113 .getattr = proc_task_getattr,
3114 .setattr = proc_setattr,
3115};
3116
Arjan van de Ven00977a52007-02-12 00:55:34 -08003117static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003118 .read = generic_read_dir,
3119 .readdir = proc_task_readdir,
3120};