blob: ce7a6da1b6a00a2771a103bdbcd6d5aebb51b81f [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
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300112#define DIR(NAME, MODE, iops, fops) \
113 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
114#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300117 { .proc_get_link = get_link } )
118#define REG(NAME, MODE, fops) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
120#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300123 { .proc_read = read } )
124#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300127 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Vegard Nossumaed54172008-06-05 22:46:53 -0700129/*
130 * Count the number of hardlinks for the pid_entry table, excluding the .
131 * and .. links.
132 */
133static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
134 unsigned int n)
135{
136 unsigned int i;
137 unsigned int count;
138
139 count = 0;
140 for (i = 0; i < n; ++i) {
141 if (S_ISDIR(entries[i].mode))
142 ++count;
143 }
144
145 return count;
146}
147
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700148static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700151 task_lock(task);
152 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if(fs)
154 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700155 task_unlock(task);
156 return fs;
157}
158
Eric W. Biederman99f89552006-06-26 00:25:55 -0700159static int get_nr_threads(struct task_struct *tsk)
160{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700161 unsigned long flags;
162 int count = 0;
163
164 if (lock_task_sighand(tsk, &flags)) {
165 count = atomic_read(&tsk->signal->count);
166 unlock_task_sighand(tsk, &flags);
167 }
168 return count;
169}
170
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800171static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700172{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700173 struct task_struct *task = get_proc_task(inode);
174 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700175 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700176
177 if (task) {
178 fs = get_fs_struct(task);
179 put_task_struct(task);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (fs) {
182 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800183 *path = fs->pwd;
184 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 read_unlock(&fs->lock);
186 result = 0;
187 put_fs_struct(fs);
188 }
189 return result;
190}
191
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800192static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700194 struct task_struct *task = get_proc_task(inode);
195 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700197
198 if (task) {
199 fs = get_fs_struct(task);
200 put_task_struct(task);
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (fs) {
203 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800204 *path = fs->root;
205 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 read_unlock(&fs->lock);
207 result = 0;
208 put_fs_struct(fs);
209 }
210 return result;
211}
212
Roland McGrath638fa2022008-04-29 01:01:38 -0700213/*
214 * Return zero if current may access user memory in @task, -error if not.
215 */
216static int check_mem_permission(struct task_struct *task)
217{
218 /*
219 * A task can always look at itself, in case it chooses
220 * to use system calls instead of load instructions.
221 */
222 if (task == current)
223 return 0;
224
225 /*
226 * If current is actively ptrace'ing, and would also be
227 * permitted to freshly attach with ptrace now, permit it.
228 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700229 if (task_is_stopped_or_traced(task)) {
230 int match;
231 rcu_read_lock();
232 match = (tracehook_tracer_task(task) == current);
233 rcu_read_unlock();
234 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
235 return 0;
236 }
Roland McGrath638fa2022008-04-29 01:01:38 -0700237
238 /*
239 * Noone else is allowed.
240 */
241 return -EPERM;
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Al Viro831830b2008-01-02 14:09:57 +0000244struct mm_struct *mm_for_maps(struct task_struct *task)
245{
246 struct mm_struct *mm = get_task_mm(task);
247 if (!mm)
248 return NULL;
249 down_read(&mm->mmap_sem);
250 task_lock(task);
251 if (task->mm != mm)
252 goto out;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400253 if (task->mm != current->mm &&
254 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
Al Viro831830b2008-01-02 14:09:57 +0000255 goto out;
256 task_unlock(task);
257 return mm;
258out:
259 task_unlock(task);
260 up_read(&mm->mmap_sem);
261 mmput(mm);
262 return NULL;
263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265static int proc_pid_cmdline(struct task_struct *task, char * buffer)
266{
267 int res = 0;
268 unsigned int len;
269 struct mm_struct *mm = get_task_mm(task);
270 if (!mm)
271 goto out;
272 if (!mm->arg_end)
273 goto out_mm; /* Shh! No looking before we're done */
274
275 len = mm->arg_end - mm->arg_start;
276
277 if (len > PAGE_SIZE)
278 len = PAGE_SIZE;
279
280 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
281
282 // If the nul at the end of args has been overwritten, then
283 // assume application is using setproctitle(3).
284 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
285 len = strnlen(buffer, res);
286 if (len < res) {
287 res = len;
288 } else {
289 len = mm->env_end - mm->env_start;
290 if (len > PAGE_SIZE - res)
291 len = PAGE_SIZE - res;
292 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
293 res = strnlen(buffer, res);
294 }
295 }
296out_mm:
297 mmput(mm);
298out:
299 return res;
300}
301
302static int proc_pid_auxv(struct task_struct *task, char *buffer)
303{
304 int res = 0;
305 struct mm_struct *mm = get_task_mm(task);
306 if (mm) {
307 unsigned int nwords = 0;
308 do
309 nwords += 2;
310 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
311 res = nwords * sizeof(mm->saved_auxv[0]);
312 if (res > PAGE_SIZE)
313 res = PAGE_SIZE;
314 memcpy(buffer, mm->saved_auxv, res);
315 mmput(mm);
316 }
317 return res;
318}
319
320
321#ifdef CONFIG_KALLSYMS
322/*
323 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
324 * Returns the resolved symbol. If that fails, simply return the address.
325 */
326static int proc_pid_wchan(struct task_struct *task, char *buffer)
327{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700328 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700329 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 wchan = get_wchan(task);
332
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700333 if (lookup_symbol_name(wchan, symname) < 0)
334 return sprintf(buffer, "%lu", wchan);
335 else
336 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338#endif /* CONFIG_KALLSYMS */
339
340#ifdef CONFIG_SCHEDSTATS
341/*
342 * Provides /proc/PID/schedstat
343 */
344static int proc_pid_schedstat(struct task_struct *task, char *buffer)
345{
Balbir Singh172ba842007-07-09 18:52:00 +0200346 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100347 (unsigned long long)task->se.sum_exec_runtime,
348 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200349 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351#endif
352
Arjan van de Ven97455122008-01-25 21:08:34 +0100353#ifdef CONFIG_LATENCYTOP
354static int lstats_show_proc(struct seq_file *m, void *v)
355{
356 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800357 struct inode *inode = m->private;
358 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100359
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800360 if (!task)
361 return -ESRCH;
362 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100363 for (i = 0; i < 32; i++) {
364 if (task->latency_record[i].backtrace[0]) {
365 int q;
366 seq_printf(m, "%i %li %li ",
367 task->latency_record[i].count,
368 task->latency_record[i].time,
369 task->latency_record[i].max);
370 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Hugh Dickins9c246242008-12-09 13:14:27 -0800371 char sym[KSYM_SYMBOL_LEN];
Arjan van de Ven97455122008-01-25 21:08:34 +0100372 char *c;
373 if (!task->latency_record[i].backtrace[q])
374 break;
375 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
376 break;
377 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
378 c = strchr(sym, '+');
379 if (c)
380 *c = 0;
381 seq_printf(m, "%s ", sym);
382 }
383 seq_printf(m, "\n");
384 }
385
386 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800387 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100388 return 0;
389}
390
391static int lstats_open(struct inode *inode, struct file *file)
392{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800393 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800394}
395
Arjan van de Ven97455122008-01-25 21:08:34 +0100396static ssize_t lstats_write(struct file *file, const char __user *buf,
397 size_t count, loff_t *offs)
398{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800399 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100400
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800401 if (!task)
402 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100403 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800404 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100405
406 return count;
407}
408
409static const struct file_operations proc_lstats_operations = {
410 .open = lstats_open,
411 .read = seq_read,
412 .write = lstats_write,
413 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800414 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100415};
416
417#endif
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/* The badness from the OOM killer */
420unsigned long badness(struct task_struct *p, unsigned long uptime);
421static int proc_oom_score(struct task_struct *task, char *buffer)
422{
423 unsigned long points;
424 struct timespec uptime;
425
426 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700427 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700429 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return sprintf(buffer, "%lu\n", points);
431}
432
Neil Hormand85f50d2007-10-18 23:40:37 -0700433struct limit_names {
434 char *name;
435 char *unit;
436};
437
438static const struct limit_names lnames[RLIM_NLIMITS] = {
439 [RLIMIT_CPU] = {"Max cpu time", "ms"},
440 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
441 [RLIMIT_DATA] = {"Max data size", "bytes"},
442 [RLIMIT_STACK] = {"Max stack size", "bytes"},
443 [RLIMIT_CORE] = {"Max core file size", "bytes"},
444 [RLIMIT_RSS] = {"Max resident set", "bytes"},
445 [RLIMIT_NPROC] = {"Max processes", "processes"},
446 [RLIMIT_NOFILE] = {"Max open files", "files"},
447 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
448 [RLIMIT_AS] = {"Max address space", "bytes"},
449 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
450 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
451 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
452 [RLIMIT_NICE] = {"Max nice priority", NULL},
453 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800454 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700455};
456
457/* Display limits for a process */
458static int proc_pid_limits(struct task_struct *task, char *buffer)
459{
460 unsigned int i;
461 int count = 0;
462 unsigned long flags;
463 char *bufptr = buffer;
464
465 struct rlimit rlim[RLIM_NLIMITS];
466
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400467 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700468 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700469 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
470 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700471
472 /*
473 * print the file header
474 */
475 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
476 "Limit", "Soft Limit", "Hard Limit", "Units");
477
478 for (i = 0; i < RLIM_NLIMITS; i++) {
479 if (rlim[i].rlim_cur == RLIM_INFINITY)
480 count += sprintf(&bufptr[count], "%-25s %-20s ",
481 lnames[i].name, "unlimited");
482 else
483 count += sprintf(&bufptr[count], "%-25s %-20lu ",
484 lnames[i].name, rlim[i].rlim_cur);
485
486 if (rlim[i].rlim_max == RLIM_INFINITY)
487 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
488 else
489 count += sprintf(&bufptr[count], "%-20lu ",
490 rlim[i].rlim_max);
491
492 if (lnames[i].unit)
493 count += sprintf(&bufptr[count], "%-10s\n",
494 lnames[i].unit);
495 else
496 count += sprintf(&bufptr[count], "\n");
497 }
498
499 return count;
500}
501
Roland McGrathebcb6732008-07-25 19:46:00 -0700502#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
503static int proc_pid_syscall(struct task_struct *task, char *buffer)
504{
505 long nr;
506 unsigned long args[6], sp, pc;
507
508 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
509 return sprintf(buffer, "running\n");
510
511 if (nr < 0)
512 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
513
514 return sprintf(buffer,
515 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
516 nr,
517 args[0], args[1], args[2], args[3], args[4], args[5],
518 sp, pc);
519}
520#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/************************************************************************/
523/* Here the fs part begins */
524/************************************************************************/
525
526/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700527static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700529 struct task_struct *task;
530 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700531 /* Allow access to a task's file descriptors if it is us or we
532 * may use ptrace attach to the process and find out that
533 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700534 */
535 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700536 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400537 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700538 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700539 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700540 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700543static int proc_setattr(struct dentry *dentry, struct iattr *attr)
544{
545 int error;
546 struct inode *inode = dentry->d_inode;
547
548 if (attr->ia_valid & ATTR_MODE)
549 return -EPERM;
550
551 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700552 if (!error)
553 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700554 return error;
555}
556
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800557static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700558 .setattr = proc_setattr,
559};
560
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100561static int mounts_open_common(struct inode *inode, struct file *file,
562 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700564 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700565 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800566 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100567 struct fs_struct *fs = NULL;
568 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500569 struct proc_mounts *p;
570 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Eric W. Biederman99f89552006-06-26 00:25:55 -0700572 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700573 rcu_read_lock();
574 nsp = task_nsproxy(task);
575 if (nsp) {
576 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800577 if (ns)
578 get_mnt_ns(ns);
579 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700580 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100581 if (ns)
582 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700583 put_task_struct(task);
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100586 if (!ns)
587 goto err;
588 if (!fs)
589 goto err_put_ns;
590
591 read_lock(&fs->lock);
592 root = fs->root;
593 path_get(&root);
594 read_unlock(&fs->lock);
595 put_fs_struct(fs);
596
597 ret = -ENOMEM;
598 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
599 if (!p)
600 goto err_put_path;
601
602 file->private_data = &p->m;
603 ret = seq_open(file, op);
604 if (ret)
605 goto err_free;
606
607 p->m.private = p;
608 p->ns = ns;
609 p->root = root;
610 p->event = ns->event;
611
612 return 0;
613
614 err_free:
615 kfree(p);
616 err_put_path:
617 path_put(&root);
618 err_put_ns:
619 put_mnt_ns(ns);
620 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return ret;
622}
623
624static int mounts_release(struct inode *inode, struct file *file)
625{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100626 struct proc_mounts *p = file->private_data;
627 path_put(&p->root);
628 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return seq_release(inode, file);
630}
631
Al Viro5addc5d2005-11-07 17:15:49 -0500632static unsigned mounts_poll(struct file *file, poll_table *wait)
633{
634 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100635 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500636 unsigned res = 0;
637
638 poll_wait(file, &ns->poll, wait);
639
640 spin_lock(&vfsmount_lock);
641 if (p->event != ns->event) {
642 p->event = ns->event;
643 res = POLLERR;
644 }
645 spin_unlock(&vfsmount_lock);
646
647 return res;
648}
649
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100650static int mounts_open(struct inode *inode, struct file *file)
651{
652 return mounts_open_common(inode, file, &mounts_op);
653}
654
Arjan van de Ven00977a52007-02-12 00:55:34 -0800655static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 .open = mounts_open,
657 .read = seq_read,
658 .llseek = seq_lseek,
659 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500660 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661};
662
Ram Pai2d4d4862008-03-27 13:06:25 +0100663static int mountinfo_open(struct inode *inode, struct file *file)
664{
665 return mounts_open_common(inode, file, &mountinfo_op);
666}
667
668static const struct file_operations proc_mountinfo_operations = {
669 .open = mountinfo_open,
670 .read = seq_read,
671 .llseek = seq_lseek,
672 .release = mounts_release,
673 .poll = mounts_poll,
674};
675
Chuck Leverb4629fe2006-03-20 13:44:12 -0500676static int mountstats_open(struct inode *inode, struct file *file)
677{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100678 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500679}
680
Arjan van de Ven00977a52007-02-12 00:55:34 -0800681static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500682 .open = mountstats_open,
683 .read = seq_read,
684 .llseek = seq_lseek,
685 .release = mounts_release,
686};
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
689
690static ssize_t proc_info_read(struct file * file, char __user * buf,
691 size_t count, loff_t *ppos)
692{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800693 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 unsigned long page;
695 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700696 struct task_struct *task = get_proc_task(inode);
697
698 length = -ESRCH;
699 if (!task)
700 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 if (count > PROC_BLOCK_SIZE)
703 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700704
705 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700706 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700707 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 length = PROC_I(inode)->op.proc_read(task, (char*)page);
710
711 if (length >= 0)
712 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
713 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700714out:
715 put_task_struct(task);
716out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return length;
718}
719
Arjan van de Ven00977a52007-02-12 00:55:34 -0800720static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 .read = proc_info_read,
722};
723
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800724static int proc_single_show(struct seq_file *m, void *v)
725{
726 struct inode *inode = m->private;
727 struct pid_namespace *ns;
728 struct pid *pid;
729 struct task_struct *task;
730 int ret;
731
732 ns = inode->i_sb->s_fs_info;
733 pid = proc_pid(inode);
734 task = get_pid_task(pid, PIDTYPE_PID);
735 if (!task)
736 return -ESRCH;
737
738 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
739
740 put_task_struct(task);
741 return ret;
742}
743
744static int proc_single_open(struct inode *inode, struct file *filp)
745{
746 int ret;
747 ret = single_open(filp, proc_single_show, NULL);
748 if (!ret) {
749 struct seq_file *m = filp->private_data;
750
751 m->private = inode;
752 }
753 return ret;
754}
755
756static const struct file_operations proc_single_file_operations = {
757 .open = proc_single_open,
758 .read = seq_read,
759 .llseek = seq_lseek,
760 .release = single_release,
761};
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763static int mem_open(struct inode* inode, struct file* file)
764{
765 file->private_data = (void*)((long)current->self_exec_id);
766 return 0;
767}
768
769static ssize_t mem_read(struct file * file, char __user * buf,
770 size_t count, loff_t *ppos)
771{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800772 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 char *page;
774 unsigned long src = *ppos;
775 int ret = -ESRCH;
776 struct mm_struct *mm;
777
Eric W. Biederman99f89552006-06-26 00:25:55 -0700778 if (!task)
779 goto out_no_task;
780
Roland McGrath638fa2022008-04-29 01:01:38 -0700781 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto out;
783
784 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700785 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!page)
787 goto out;
788
789 ret = 0;
790
791 mm = get_task_mm(task);
792 if (!mm)
793 goto out_free;
794
795 ret = -EIO;
796
797 if (file->private_data != (void*)((long)current->self_exec_id))
798 goto out_put;
799
800 ret = 0;
801
802 while (count > 0) {
803 int this_len, retval;
804
805 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
806 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa2022008-04-29 01:01:38 -0700807 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!ret)
809 ret = -EIO;
810 break;
811 }
812
813 if (copy_to_user(buf, page, retval)) {
814 ret = -EFAULT;
815 break;
816 }
817
818 ret += retval;
819 src += retval;
820 buf += retval;
821 count -= retval;
822 }
823 *ppos = src;
824
825out_put:
826 mmput(mm);
827out_free:
828 free_page((unsigned long) page);
829out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700830 put_task_struct(task);
831out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return ret;
833}
834
835#define mem_write NULL
836
837#ifndef mem_write
838/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800839static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 size_t count, loff_t *ppos)
841{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700842 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800844 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 unsigned long dst = *ppos;
846
Eric W. Biederman99f89552006-06-26 00:25:55 -0700847 copied = -ESRCH;
848 if (!task)
849 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Roland McGrath638fa2022008-04-29 01:01:38 -0700851 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700852 goto out;
853
854 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700855 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700857 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700859 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 while (count > 0) {
861 int this_len, retval;
862
863 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
864 if (copy_from_user(page, buf, this_len)) {
865 copied = -EFAULT;
866 break;
867 }
868 retval = access_process_vm(task, dst, page, this_len, 1);
869 if (!retval) {
870 if (!copied)
871 copied = -EIO;
872 break;
873 }
874 copied += retval;
875 buf += retval;
876 dst += retval;
877 count -= retval;
878 }
879 *ppos = dst;
880 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700881out:
882 put_task_struct(task);
883out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return copied;
885}
886#endif
887
Matt Mackall85863e42008-02-04 22:29:04 -0800888loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 switch (orig) {
891 case 0:
892 file->f_pos = offset;
893 break;
894 case 1:
895 file->f_pos += offset;
896 break;
897 default:
898 return -EINVAL;
899 }
900 force_successful_syscall_return();
901 return file->f_pos;
902}
903
Arjan van de Ven00977a52007-02-12 00:55:34 -0800904static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 .llseek = mem_lseek,
906 .read = mem_read,
907 .write = mem_write,
908 .open = mem_open,
909};
910
James Pearson315e28c2007-10-16 23:30:17 -0700911static ssize_t environ_read(struct file *file, char __user *buf,
912 size_t count, loff_t *ppos)
913{
914 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
915 char *page;
916 unsigned long src = *ppos;
917 int ret = -ESRCH;
918 struct mm_struct *mm;
919
920 if (!task)
921 goto out_no_task;
922
Stephen Smalley006ebb42008-05-19 08:32:49 -0400923 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700924 goto out;
925
926 ret = -ENOMEM;
927 page = (char *)__get_free_page(GFP_TEMPORARY);
928 if (!page)
929 goto out;
930
931 ret = 0;
932
933 mm = get_task_mm(task);
934 if (!mm)
935 goto out_free;
936
937 while (count > 0) {
938 int this_len, retval, max_len;
939
940 this_len = mm->env_end - (mm->env_start + src);
941
942 if (this_len <= 0)
943 break;
944
945 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
946 this_len = (this_len > max_len) ? max_len : this_len;
947
948 retval = access_process_vm(task, (mm->env_start + src),
949 page, this_len, 0);
950
951 if (retval <= 0) {
952 ret = retval;
953 break;
954 }
955
956 if (copy_to_user(buf, page, retval)) {
957 ret = -EFAULT;
958 break;
959 }
960
961 ret += retval;
962 src += retval;
963 buf += retval;
964 count -= retval;
965 }
966 *ppos = src;
967
968 mmput(mm);
969out_free:
970 free_page((unsigned long) page);
971out:
972 put_task_struct(task);
973out_no_task:
974 return ret;
975}
976
977static const struct file_operations proc_environ_operations = {
978 .read = environ_read,
979};
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static ssize_t oom_adjust_read(struct file *file, char __user *buf,
982 size_t count, loff_t *ppos)
983{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800984 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700985 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700987 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Eric W. Biederman99f89552006-06-26 00:25:55 -0700989 if (!task)
990 return -ESRCH;
991 oom_adjust = task->oomkilladj;
992 put_task_struct(task);
993
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700994 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700995
996 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1000 size_t count, loff_t *ppos)
1001{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001002 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001003 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 int oom_adjust;
1005
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001006 memset(buffer, 0, sizeof(buffer));
1007 if (count > sizeof(buffer) - 1)
1008 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (copy_from_user(buffer, buf, count))
1010 return -EFAULT;
1011 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001012 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1013 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -EINVAL;
1015 if (*end == '\n')
1016 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001017 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001018 if (!task)
1019 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001020 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1021 put_task_struct(task);
1022 return -EACCES;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001025 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (end - buffer == 0)
1027 return -EIO;
1028 return end - buffer;
1029}
1030
Arjan van de Ven00977a52007-02-12 00:55:34 -08001031static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 .read = oom_adjust_read,
1033 .write = oom_adjust_write,
1034};
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036#ifdef CONFIG_AUDITSYSCALL
1037#define TMPBUFLEN 21
1038static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1039 size_t count, loff_t *ppos)
1040{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001041 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001042 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 ssize_t length;
1044 char tmpbuf[TMPBUFLEN];
1045
Eric W. Biederman99f89552006-06-26 00:25:55 -07001046 if (!task)
1047 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001049 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001050 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1052}
1053
1054static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1055 size_t count, loff_t *ppos)
1056{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001057 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 char *page, *tmp;
1059 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 uid_t loginuid;
1061
1062 if (!capable(CAP_AUDIT_CONTROL))
1063 return -EPERM;
1064
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001065 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return -EPERM;
1067
Al Viroe0182902006-05-18 08:28:02 -04001068 if (count >= PAGE_SIZE)
1069 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 if (*ppos != 0) {
1072 /* No partial writes. */
1073 return -EINVAL;
1074 }
Mel Gormane12ba742007-10-16 01:25:52 -07001075 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (!page)
1077 return -ENOMEM;
1078 length = -EFAULT;
1079 if (copy_from_user(page, buf, count))
1080 goto out_free_page;
1081
Al Viroe0182902006-05-18 08:28:02 -04001082 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 loginuid = simple_strtoul(page, &tmp, 10);
1084 if (tmp == page) {
1085 length = -EINVAL;
1086 goto out_free_page;
1087
1088 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001089 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (likely(length == 0))
1091 length = count;
1092
1093out_free_page:
1094 free_page((unsigned long) page);
1095 return length;
1096}
1097
Arjan van de Ven00977a52007-02-12 00:55:34 -08001098static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 .read = proc_loginuid_read,
1100 .write = proc_loginuid_write,
1101};
Eric Paris1e0bd752008-03-13 08:15:31 -04001102
1103static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1104 size_t count, loff_t *ppos)
1105{
1106 struct inode * inode = file->f_path.dentry->d_inode;
1107 struct task_struct *task = get_proc_task(inode);
1108 ssize_t length;
1109 char tmpbuf[TMPBUFLEN];
1110
1111 if (!task)
1112 return -ESRCH;
1113 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1114 audit_get_sessionid(task));
1115 put_task_struct(task);
1116 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1117}
1118
1119static const struct file_operations proc_sessionid_operations = {
1120 .read = proc_sessionid_read,
1121};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122#endif
1123
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001124#ifdef CONFIG_FAULT_INJECTION
1125static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1126 size_t count, loff_t *ppos)
1127{
1128 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1129 char buffer[PROC_NUMBUF];
1130 size_t len;
1131 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001132
1133 if (!task)
1134 return -ESRCH;
1135 make_it_fail = task->make_it_fail;
1136 put_task_struct(task);
1137
1138 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001139
1140 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001141}
1142
1143static ssize_t proc_fault_inject_write(struct file * file,
1144 const char __user * buf, size_t count, loff_t *ppos)
1145{
1146 struct task_struct *task;
1147 char buffer[PROC_NUMBUF], *end;
1148 int make_it_fail;
1149
1150 if (!capable(CAP_SYS_RESOURCE))
1151 return -EPERM;
1152 memset(buffer, 0, sizeof(buffer));
1153 if (count > sizeof(buffer) - 1)
1154 count = sizeof(buffer) - 1;
1155 if (copy_from_user(buffer, buf, count))
1156 return -EFAULT;
1157 make_it_fail = simple_strtol(buffer, &end, 0);
1158 if (*end == '\n')
1159 end++;
1160 task = get_proc_task(file->f_dentry->d_inode);
1161 if (!task)
1162 return -ESRCH;
1163 task->make_it_fail = make_it_fail;
1164 put_task_struct(task);
1165 if (end - buffer == 0)
1166 return -EIO;
1167 return end - buffer;
1168}
1169
Arjan van de Ven00977a52007-02-12 00:55:34 -08001170static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001171 .read = proc_fault_inject_read,
1172 .write = proc_fault_inject_write,
1173};
1174#endif
1175
Arjan van de Ven97455122008-01-25 21:08:34 +01001176
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001177#ifdef CONFIG_SCHED_DEBUG
1178/*
1179 * Print out various scheduling related per-task fields:
1180 */
1181static int sched_show(struct seq_file *m, void *v)
1182{
1183 struct inode *inode = m->private;
1184 struct task_struct *p;
1185
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001186 p = get_proc_task(inode);
1187 if (!p)
1188 return -ESRCH;
1189 proc_sched_show_task(p, m);
1190
1191 put_task_struct(p);
1192
1193 return 0;
1194}
1195
1196static ssize_t
1197sched_write(struct file *file, const char __user *buf,
1198 size_t count, loff_t *offset)
1199{
1200 struct inode *inode = file->f_path.dentry->d_inode;
1201 struct task_struct *p;
1202
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001203 p = get_proc_task(inode);
1204 if (!p)
1205 return -ESRCH;
1206 proc_sched_set_task(p);
1207
1208 put_task_struct(p);
1209
1210 return count;
1211}
1212
1213static int sched_open(struct inode *inode, struct file *filp)
1214{
1215 int ret;
1216
1217 ret = single_open(filp, sched_show, NULL);
1218 if (!ret) {
1219 struct seq_file *m = filp->private_data;
1220
1221 m->private = inode;
1222 }
1223 return ret;
1224}
1225
1226static const struct file_operations proc_pid_sched_operations = {
1227 .open = sched_open,
1228 .read = seq_read,
1229 .write = sched_write,
1230 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001231 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001232};
1233
1234#endif
1235
Matt Helsley925d1c42008-04-29 01:01:36 -07001236/*
1237 * We added or removed a vma mapping the executable. The vmas are only mapped
1238 * during exec and are not mapped with the mmap system call.
1239 * Callers must hold down_write() on the mm's mmap_sem for these
1240 */
1241void added_exe_file_vma(struct mm_struct *mm)
1242{
1243 mm->num_exe_file_vmas++;
1244}
1245
1246void removed_exe_file_vma(struct mm_struct *mm)
1247{
1248 mm->num_exe_file_vmas--;
1249 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1250 fput(mm->exe_file);
1251 mm->exe_file = NULL;
1252 }
1253
1254}
1255
1256void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1257{
1258 if (new_exe_file)
1259 get_file(new_exe_file);
1260 if (mm->exe_file)
1261 fput(mm->exe_file);
1262 mm->exe_file = new_exe_file;
1263 mm->num_exe_file_vmas = 0;
1264}
1265
1266struct file *get_mm_exe_file(struct mm_struct *mm)
1267{
1268 struct file *exe_file;
1269
1270 /* We need mmap_sem to protect against races with removal of
1271 * VM_EXECUTABLE vmas */
1272 down_read(&mm->mmap_sem);
1273 exe_file = mm->exe_file;
1274 if (exe_file)
1275 get_file(exe_file);
1276 up_read(&mm->mmap_sem);
1277 return exe_file;
1278}
1279
1280void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1281{
1282 /* It's safe to write the exe_file pointer without exe_file_lock because
1283 * this is called during fork when the task is not yet in /proc */
1284 newmm->exe_file = get_mm_exe_file(oldmm);
1285}
1286
1287static int proc_exe_link(struct inode *inode, struct path *exe_path)
1288{
1289 struct task_struct *task;
1290 struct mm_struct *mm;
1291 struct file *exe_file;
1292
1293 task = get_proc_task(inode);
1294 if (!task)
1295 return -ENOENT;
1296 mm = get_task_mm(task);
1297 put_task_struct(task);
1298 if (!mm)
1299 return -ENOENT;
1300 exe_file = get_mm_exe_file(mm);
1301 mmput(mm);
1302 if (exe_file) {
1303 *exe_path = exe_file->f_path;
1304 path_get(&exe_file->f_path);
1305 fput(exe_file);
1306 return 0;
1307 } else
1308 return -ENOENT;
1309}
1310
Al Viro008b1502005-08-20 00:17:39 +01001311static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct inode *inode = dentry->d_inode;
1314 int error = -EACCES;
1315
1316 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001317 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Eric W. Biederman778c1142006-06-26 00:25:58 -07001319 /* Are we allowed to snoop on the tasks file descriptors? */
1320 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001323 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 nd->last_type = LAST_BIND;
1325out:
Al Viro008b1502005-08-20 00:17:39 +01001326 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001329static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Mel Gormane12ba742007-10-16 01:25:52 -07001331 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001332 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 int len;
1334
1335 if (!tmp)
1336 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001337
Jan Blunckcf28b482008-02-14 19:38:44 -08001338 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001339 len = PTR_ERR(pathname);
1340 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001342 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 if (len > buflen)
1345 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001346 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 len = -EFAULT;
1348 out:
1349 free_page((unsigned long)tmp);
1350 return len;
1351}
1352
1353static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1354{
1355 int error = -EACCES;
1356 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001357 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Eric W. Biederman778c1142006-06-26 00:25:58 -07001359 /* Are we allowed to snoop on the tasks file descriptors? */
1360 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001363 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (error)
1365 goto out;
1366
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001367 error = do_proc_readlink(&path, buffer, buflen);
1368 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return error;
1371}
1372
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001373static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001375 .follow_link = proc_pid_follow_link,
1376 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377};
1378
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001379
1380/* building an inode */
1381
1382static int task_dumpable(struct task_struct *task)
1383{
1384 int dumpable = 0;
1385 struct mm_struct *mm;
1386
1387 task_lock(task);
1388 mm = task->mm;
1389 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001390 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001391 task_unlock(task);
1392 if(dumpable == 1)
1393 return 1;
1394 return 0;
1395}
1396
1397
Eric W. Biederman61a28782006-10-02 02:18:49 -07001398static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001399{
1400 struct inode * inode;
1401 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001402 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001403
1404 /* We need a new inode */
1405
1406 inode = new_inode(sb);
1407 if (!inode)
1408 goto out;
1409
1410 /* Common stuff */
1411 ei = PROC_I(inode);
1412 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001413 inode->i_op = &proc_def_inode_operations;
1414
1415 /*
1416 * grab the reference to task.
1417 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001418 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001419 if (!ei->pid)
1420 goto out_unlock;
1421
1422 inode->i_uid = 0;
1423 inode->i_gid = 0;
1424 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001425 rcu_read_lock();
1426 cred = __task_cred(task);
1427 inode->i_uid = cred->euid;
1428 inode->i_gid = cred->egid;
1429 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001430 }
1431 security_task_to_inode(task, inode);
1432
1433out:
1434 return inode;
1435
1436out_unlock:
1437 iput(inode);
1438 return NULL;
1439}
1440
1441static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1442{
1443 struct inode *inode = dentry->d_inode;
1444 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001445 const struct cred *cred;
1446
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001447 generic_fillattr(inode, stat);
1448
1449 rcu_read_lock();
1450 stat->uid = 0;
1451 stat->gid = 0;
1452 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1453 if (task) {
1454 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1455 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001456 cred = __task_cred(task);
1457 stat->uid = cred->euid;
1458 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001459 }
1460 }
1461 rcu_read_unlock();
1462 return 0;
1463}
1464
1465/* dentry stuff */
1466
1467/*
1468 * Exceptional case: normally we are not allowed to unhash a busy
1469 * directory. In this case, however, we can do it - no aliasing problems
1470 * due to the way we treat inodes.
1471 *
1472 * Rewrite the inode's ownerships here because the owning task may have
1473 * performed a setuid(), etc.
1474 *
1475 * Before the /proc/pid/status file was created the only way to read
1476 * the effective uid of a /process was to stat /proc/pid. Reading
1477 * /proc/pid/status is slow enough that procps and other packages
1478 * kept stating /proc/pid. To keep the rules in /proc simple I have
1479 * made this apply to all per process world readable and executable
1480 * directories.
1481 */
1482static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1483{
1484 struct inode *inode = dentry->d_inode;
1485 struct task_struct *task = get_proc_task(inode);
David Howellsc69e8d92008-11-14 10:39:19 +11001486 const struct cred *cred;
1487
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001488 if (task) {
1489 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1490 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001491 rcu_read_lock();
1492 cred = __task_cred(task);
1493 inode->i_uid = cred->euid;
1494 inode->i_gid = cred->egid;
1495 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001496 } else {
1497 inode->i_uid = 0;
1498 inode->i_gid = 0;
1499 }
1500 inode->i_mode &= ~(S_ISUID | S_ISGID);
1501 security_task_to_inode(task, inode);
1502 put_task_struct(task);
1503 return 1;
1504 }
1505 d_drop(dentry);
1506 return 0;
1507}
1508
1509static int pid_delete_dentry(struct dentry * dentry)
1510{
1511 /* Is the task we represent dead?
1512 * If so, then don't put the dentry on the lru list,
1513 * kill it immediately.
1514 */
1515 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1516}
1517
1518static struct dentry_operations pid_dentry_operations =
1519{
1520 .d_revalidate = pid_revalidate,
1521 .d_delete = pid_delete_dentry,
1522};
1523
1524/* Lookups */
1525
Eric Dumazetc5141e62007-05-08 00:26:15 -07001526typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1527 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001528
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001529/*
1530 * Fill a directory entry.
1531 *
1532 * If possible create the dcache entry and derive our inode number and
1533 * file type from dcache entry.
1534 *
1535 * Since all of the proc inode numbers are dynamically generated, the inode
1536 * numbers do not exist until the inode is cache. This means creating the
1537 * the dcache entry in readdir is necessary to keep the inode numbers
1538 * reported by readdir in sync with the inode numbers reported
1539 * by stat.
1540 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001541static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1542 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001543 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001544{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001545 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001546 struct inode *inode;
1547 struct qstr qname;
1548 ino_t ino = 0;
1549 unsigned type = DT_UNKNOWN;
1550
1551 qname.name = name;
1552 qname.len = len;
1553 qname.hash = full_name_hash(name, len);
1554
1555 child = d_lookup(dir, &qname);
1556 if (!child) {
1557 struct dentry *new;
1558 new = d_alloc(dir, &qname);
1559 if (new) {
1560 child = instantiate(dir->d_inode, new, task, ptr);
1561 if (child)
1562 dput(new);
1563 else
1564 child = new;
1565 }
1566 }
1567 if (!child || IS_ERR(child) || !child->d_inode)
1568 goto end_instantiate;
1569 inode = child->d_inode;
1570 if (inode) {
1571 ino = inode->i_ino;
1572 type = inode->i_mode >> 12;
1573 }
1574 dput(child);
1575end_instantiate:
1576 if (!ino)
1577 ino = find_inode_number(dir, &qname);
1578 if (!ino)
1579 ino = 1;
1580 return filldir(dirent, name, len, filp->f_pos, ino, type);
1581}
1582
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001583static unsigned name_to_int(struct dentry *dentry)
1584{
1585 const char *name = dentry->d_name.name;
1586 int len = dentry->d_name.len;
1587 unsigned n = 0;
1588
1589 if (len > 1 && *name == '0')
1590 goto out;
1591 while (len-- > 0) {
1592 unsigned c = *name++ - '0';
1593 if (c > 9)
1594 goto out;
1595 if (n >= (~0U-9)/10)
1596 goto out;
1597 n *= 10;
1598 n += c;
1599 }
1600 return n;
1601out:
1602 return ~0U;
1603}
1604
Miklos Szeredi27932742007-05-08 00:26:17 -07001605#define PROC_FDINFO_MAX 64
1606
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001607static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001608{
1609 struct task_struct *task = get_proc_task(inode);
1610 struct files_struct *files = NULL;
1611 struct file *file;
1612 int fd = proc_fd(inode);
1613
1614 if (task) {
1615 files = get_files_struct(task);
1616 put_task_struct(task);
1617 }
1618 if (files) {
1619 /*
1620 * We are not taking a ref to the file structure, so we must
1621 * hold ->file_lock.
1622 */
1623 spin_lock(&files->file_lock);
1624 file = fcheck_files(files, fd);
1625 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001626 if (path) {
1627 *path = file->f_path;
1628 path_get(&file->f_path);
1629 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001630 if (info)
1631 snprintf(info, PROC_FDINFO_MAX,
1632 "pos:\t%lli\n"
1633 "flags:\t0%o\n",
1634 (long long) file->f_pos,
1635 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001636 spin_unlock(&files->file_lock);
1637 put_files_struct(files);
1638 return 0;
1639 }
1640 spin_unlock(&files->file_lock);
1641 put_files_struct(files);
1642 }
1643 return -ENOENT;
1644}
1645
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001646static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001647{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001648 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001649}
1650
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1652{
1653 struct inode *inode = dentry->d_inode;
1654 struct task_struct *task = get_proc_task(inode);
1655 int fd = proc_fd(inode);
1656 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001657 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001658
1659 if (task) {
1660 files = get_files_struct(task);
1661 if (files) {
1662 rcu_read_lock();
1663 if (fcheck_files(files, fd)) {
1664 rcu_read_unlock();
1665 put_files_struct(files);
1666 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001667 rcu_read_lock();
1668 cred = __task_cred(task);
1669 inode->i_uid = cred->euid;
1670 inode->i_gid = cred->egid;
1671 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001672 } else {
1673 inode->i_uid = 0;
1674 inode->i_gid = 0;
1675 }
1676 inode->i_mode &= ~(S_ISUID | S_ISGID);
1677 security_task_to_inode(task, inode);
1678 put_task_struct(task);
1679 return 1;
1680 }
1681 rcu_read_unlock();
1682 put_files_struct(files);
1683 }
1684 put_task_struct(task);
1685 }
1686 d_drop(dentry);
1687 return 0;
1688}
1689
1690static struct dentry_operations tid_fd_dentry_operations =
1691{
1692 .d_revalidate = tid_fd_revalidate,
1693 .d_delete = pid_delete_dentry,
1694};
1695
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001696static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001697 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001698{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001699 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001700 struct file *file;
1701 struct files_struct *files;
1702 struct inode *inode;
1703 struct proc_inode *ei;
1704 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001705
Eric W. Biederman61a28782006-10-02 02:18:49 -07001706 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001707 if (!inode)
1708 goto out;
1709 ei = PROC_I(inode);
1710 ei->fd = fd;
1711 files = get_files_struct(task);
1712 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001713 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001714 inode->i_mode = S_IFLNK;
1715
1716 /*
1717 * We are not taking a ref to the file structure, so we must
1718 * hold ->file_lock.
1719 */
1720 spin_lock(&files->file_lock);
1721 file = fcheck_files(files, fd);
1722 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001723 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001724 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001725 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001726 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001727 inode->i_mode |= S_IWUSR | S_IXUSR;
1728 spin_unlock(&files->file_lock);
1729 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001730
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001731 inode->i_op = &proc_pid_link_inode_operations;
1732 inode->i_size = 64;
1733 ei->op.proc_get_link = proc_fd_link;
1734 dentry->d_op = &tid_fd_dentry_operations;
1735 d_add(dentry, inode);
1736 /* Close the race of the process dying before we return the dentry */
1737 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001738 error = NULL;
1739
1740 out:
1741 return error;
1742out_unlock:
1743 spin_unlock(&files->file_lock);
1744 put_files_struct(files);
1745out_iput:
1746 iput(inode);
1747 goto out;
1748}
1749
Miklos Szeredi27932742007-05-08 00:26:17 -07001750static struct dentry *proc_lookupfd_common(struct inode *dir,
1751 struct dentry *dentry,
1752 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001753{
1754 struct task_struct *task = get_proc_task(dir);
1755 unsigned fd = name_to_int(dentry);
1756 struct dentry *result = ERR_PTR(-ENOENT);
1757
1758 if (!task)
1759 goto out_no_task;
1760 if (fd == ~0U)
1761 goto out;
1762
Miklos Szeredi27932742007-05-08 00:26:17 -07001763 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001764out:
1765 put_task_struct(task);
1766out_no_task:
1767 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001768}
1769
Miklos Szeredi27932742007-05-08 00:26:17 -07001770static int proc_readfd_common(struct file * filp, void * dirent,
1771 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001773 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001774 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001775 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001776 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct files_struct * files;
1779
1780 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001781 if (!p)
1782 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 fd = filp->f_pos;
1786 switch (fd) {
1787 case 0:
1788 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1789 goto out;
1790 filp->f_pos++;
1791 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001792 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1794 goto out;
1795 filp->f_pos++;
1796 default:
1797 files = get_files_struct(p);
1798 if (!files)
1799 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001800 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001802 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001804 char name[PROC_NUMBUF];
1805 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 if (!fcheck_files(files, fd))
1808 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001809 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Miklos Szeredi27932742007-05-08 00:26:17 -07001811 len = snprintf(name, sizeof(name), "%d", fd);
1812 if (proc_fill_cache(filp, dirent, filldir,
1813 name, len, instantiate,
1814 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001815 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 break;
1817 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001818 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001820 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 put_files_struct(files);
1822 }
1823out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001824 put_task_struct(p);
1825out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return retval;
1827}
1828
Miklos Szeredi27932742007-05-08 00:26:17 -07001829static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1830 struct nameidata *nd)
1831{
1832 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1833}
1834
1835static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1836{
1837 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1838}
1839
1840static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1841 size_t len, loff_t *ppos)
1842{
1843 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001844 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001845 if (!err)
1846 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1847 return err;
1848}
1849
1850static const struct file_operations proc_fdinfo_file_operations = {
1851 .open = nonseekable_open,
1852 .read = proc_fdinfo_read,
1853};
1854
Arjan van de Ven00977a52007-02-12 00:55:34 -08001855static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 .read = generic_read_dir,
1857 .readdir = proc_readfd,
1858};
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001861 * /proc/pid/fd needs a special permission handler so that a process can still
1862 * access /proc/self/fd after it has executed a setuid().
1863 */
Al Viroe6305c42008-07-15 21:03:57 -04001864static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001865{
1866 int rv;
1867
1868 rv = generic_permission(inode, mask, NULL);
1869 if (rv == 0)
1870 return 0;
1871 if (task_pid(current) == proc_pid(inode))
1872 rv = 0;
1873 return rv;
1874}
1875
1876/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 * proc directories can do almost nothing..
1878 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001879static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001881 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001882 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883};
1884
Miklos Szeredi27932742007-05-08 00:26:17 -07001885static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1886 struct dentry *dentry, struct task_struct *task, const void *ptr)
1887{
1888 unsigned fd = *(unsigned *)ptr;
1889 struct inode *inode;
1890 struct proc_inode *ei;
1891 struct dentry *error = ERR_PTR(-ENOENT);
1892
1893 inode = proc_pid_make_inode(dir->i_sb, task);
1894 if (!inode)
1895 goto out;
1896 ei = PROC_I(inode);
1897 ei->fd = fd;
1898 inode->i_mode = S_IFREG | S_IRUSR;
1899 inode->i_fop = &proc_fdinfo_file_operations;
1900 dentry->d_op = &tid_fd_dentry_operations;
1901 d_add(dentry, inode);
1902 /* Close the race of the process dying before we return the dentry */
1903 if (tid_fd_revalidate(dentry, NULL))
1904 error = NULL;
1905
1906 out:
1907 return error;
1908}
1909
1910static struct dentry *proc_lookupfdinfo(struct inode *dir,
1911 struct dentry *dentry,
1912 struct nameidata *nd)
1913{
1914 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1915}
1916
1917static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1918{
1919 return proc_readfd_common(filp, dirent, filldir,
1920 proc_fdinfo_instantiate);
1921}
1922
1923static const struct file_operations proc_fdinfo_operations = {
1924 .read = generic_read_dir,
1925 .readdir = proc_readfdinfo,
1926};
1927
1928/*
1929 * proc directories can do almost nothing..
1930 */
1931static const struct inode_operations proc_fdinfo_inode_operations = {
1932 .lookup = proc_lookupfdinfo,
1933 .setattr = proc_setattr,
1934};
1935
1936
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001937static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001938 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001939{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001940 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001941 struct inode *inode;
1942 struct proc_inode *ei;
1943 struct dentry *error = ERR_PTR(-EINVAL);
1944
Eric W. Biederman61a28782006-10-02 02:18:49 -07001945 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001946 if (!inode)
1947 goto out;
1948
1949 ei = PROC_I(inode);
1950 inode->i_mode = p->mode;
1951 if (S_ISDIR(inode->i_mode))
1952 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1953 if (p->iop)
1954 inode->i_op = p->iop;
1955 if (p->fop)
1956 inode->i_fop = p->fop;
1957 ei->op = p->op;
1958 dentry->d_op = &pid_dentry_operations;
1959 d_add(dentry, inode);
1960 /* Close the race of the process dying before we return the dentry */
1961 if (pid_revalidate(dentry, NULL))
1962 error = NULL;
1963out:
1964 return error;
1965}
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967static struct dentry *proc_pident_lookup(struct inode *dir,
1968 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001969 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001970 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001973 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001974 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001975 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001977 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 inode = NULL;
1979
Eric W. Biederman99f89552006-06-26 00:25:55 -07001980 if (!task)
1981 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001983 /*
1984 * Yes, it does not scale. And it should not. Don't add
1985 * new entries into /proc/<tgid>/ without very good reasons.
1986 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001987 last = &ents[nents - 1];
1988 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (p->len != dentry->d_name.len)
1990 continue;
1991 if (!memcmp(dentry->d_name.name, p->name, p->len))
1992 break;
1993 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001994 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 goto out;
1996
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001997 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001999 put_task_struct(task);
2000out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002001 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Eric Dumazetc5141e62007-05-08 00:26:15 -07002004static int proc_pident_fill_cache(struct file *filp, void *dirent,
2005 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002006{
2007 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2008 proc_pident_instantiate, task, p);
2009}
2010
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002011static int proc_pident_readdir(struct file *filp,
2012 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002013 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002014{
2015 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002016 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002017 struct inode *inode = dentry->d_inode;
2018 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002019 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002020 ino_t ino;
2021 int ret;
2022
2023 ret = -ENOENT;
2024 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002025 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002026
2027 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002028 i = filp->f_pos;
2029 switch (i) {
2030 case 0:
2031 ino = inode->i_ino;
2032 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2033 goto out;
2034 i++;
2035 filp->f_pos++;
2036 /* fall through */
2037 case 1:
2038 ino = parent_ino(dentry);
2039 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2040 goto out;
2041 i++;
2042 filp->f_pos++;
2043 /* fall through */
2044 default:
2045 i -= 2;
2046 if (i >= nents) {
2047 ret = 1;
2048 goto out;
2049 }
2050 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002051 last = &ents[nents - 1];
2052 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002053 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002054 goto out;
2055 filp->f_pos++;
2056 p++;
2057 }
2058 }
2059
2060 ret = 1;
2061out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002062 put_task_struct(task);
2063out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002064 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002068static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2069 size_t count, loff_t *ppos)
2070{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002071 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002072 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002073 ssize_t length;
2074 struct task_struct *task = get_proc_task(inode);
2075
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002076 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002077 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002078
2079 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002080 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002081 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002082 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002083 if (length > 0)
2084 length = simple_read_from_buffer(buf, count, ppos, p, length);
2085 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002086 return length;
2087}
2088
2089static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2090 size_t count, loff_t *ppos)
2091{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002092 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002093 char *page;
2094 ssize_t length;
2095 struct task_struct *task = get_proc_task(inode);
2096
2097 length = -ESRCH;
2098 if (!task)
2099 goto out_no_task;
2100 if (count > PAGE_SIZE)
2101 count = PAGE_SIZE;
2102
2103 /* No partial writes. */
2104 length = -EINVAL;
2105 if (*ppos != 0)
2106 goto out;
2107
2108 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002109 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002110 if (!page)
2111 goto out;
2112
2113 length = -EFAULT;
2114 if (copy_from_user(page, buf, count))
2115 goto out_free;
2116
2117 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002118 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002119 (void*)page, count);
2120out_free:
2121 free_page((unsigned long) page);
2122out:
2123 put_task_struct(task);
2124out_no_task:
2125 return length;
2126}
2127
Arjan van de Ven00977a52007-02-12 00:55:34 -08002128static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002129 .read = proc_pid_attr_read,
2130 .write = proc_pid_attr_write,
2131};
2132
Eric Dumazetc5141e62007-05-08 00:26:15 -07002133static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002134 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2135 REG("prev", S_IRUGO, proc_pid_attr_operations),
2136 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2137 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2138 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2139 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002140};
2141
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002142static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 void * dirent, filldir_t filldir)
2144{
2145 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002146 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Arjan van de Ven00977a52007-02-12 00:55:34 -08002149static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002151 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152};
2153
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002154static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 struct dentry *dentry, struct nameidata *nd)
2156{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002157 return proc_pident_lookup(dir, dentry,
2158 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002161static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002162 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002163 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002164 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165};
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167#endif
2168
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002169#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2170static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2171 size_t count, loff_t *ppos)
2172{
2173 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2174 struct mm_struct *mm;
2175 char buffer[PROC_NUMBUF];
2176 size_t len;
2177 int ret;
2178
2179 if (!task)
2180 return -ESRCH;
2181
2182 ret = 0;
2183 mm = get_task_mm(task);
2184 if (mm) {
2185 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2186 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2187 MMF_DUMP_FILTER_SHIFT));
2188 mmput(mm);
2189 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2190 }
2191
2192 put_task_struct(task);
2193
2194 return ret;
2195}
2196
2197static ssize_t proc_coredump_filter_write(struct file *file,
2198 const char __user *buf,
2199 size_t count,
2200 loff_t *ppos)
2201{
2202 struct task_struct *task;
2203 struct mm_struct *mm;
2204 char buffer[PROC_NUMBUF], *end;
2205 unsigned int val;
2206 int ret;
2207 int i;
2208 unsigned long mask;
2209
2210 ret = -EFAULT;
2211 memset(buffer, 0, sizeof(buffer));
2212 if (count > sizeof(buffer) - 1)
2213 count = sizeof(buffer) - 1;
2214 if (copy_from_user(buffer, buf, count))
2215 goto out_no_task;
2216
2217 ret = -EINVAL;
2218 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2219 if (*end == '\n')
2220 end++;
2221 if (end - buffer == 0)
2222 goto out_no_task;
2223
2224 ret = -ESRCH;
2225 task = get_proc_task(file->f_dentry->d_inode);
2226 if (!task)
2227 goto out_no_task;
2228
2229 ret = end - buffer;
2230 mm = get_task_mm(task);
2231 if (!mm)
2232 goto out_no_mm;
2233
2234 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2235 if (val & mask)
2236 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2237 else
2238 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2239 }
2240
2241 mmput(mm);
2242 out_no_mm:
2243 put_task_struct(task);
2244 out_no_task:
2245 return ret;
2246}
2247
2248static const struct file_operations proc_coredump_filter_operations = {
2249 .read = proc_coredump_filter_read,
2250 .write = proc_coredump_filter_write,
2251};
2252#endif
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254/*
2255 * /proc/self:
2256 */
2257static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2258 int buflen)
2259{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002260 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002261 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002262 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002263 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002264 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002265 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return vfs_readlink(dentry,buffer,buflen,tmp);
2267}
2268
Al Viro008b1502005-08-20 00:17:39 +01002269static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002271 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002272 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002273 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002274 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002275 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002276 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002277 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002280static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 .readlink = proc_self_readlink,
2282 .follow_link = proc_self_follow_link,
2283};
2284
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002285/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002286 * proc base
2287 *
2288 * These are the directory entries in the root directory of /proc
2289 * that properly belong to the /proc filesystem, as they describe
2290 * describe something that is process related.
2291 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002292static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002293 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002294 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002295};
2296
2297/*
2298 * Exceptional case: normally we are not allowed to unhash a busy
2299 * directory. In this case, however, we can do it - no aliasing problems
2300 * due to the way we treat inodes.
2301 */
2302static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2303{
2304 struct inode *inode = dentry->d_inode;
2305 struct task_struct *task = get_proc_task(inode);
2306 if (task) {
2307 put_task_struct(task);
2308 return 1;
2309 }
2310 d_drop(dentry);
2311 return 0;
2312}
2313
2314static struct dentry_operations proc_base_dentry_operations =
2315{
2316 .d_revalidate = proc_base_revalidate,
2317 .d_delete = pid_delete_dentry,
2318};
2319
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002320static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002321 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002322{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002323 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002324 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002325 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002326 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002327
2328 /* Allocate the inode */
2329 error = ERR_PTR(-ENOMEM);
2330 inode = new_inode(dir->i_sb);
2331 if (!inode)
2332 goto out;
2333
2334 /* Initialize the inode */
2335 ei = PROC_I(inode);
2336 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002337
2338 /*
2339 * grab the reference to the task.
2340 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002341 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002342 if (!ei->pid)
2343 goto out_iput;
2344
2345 inode->i_uid = 0;
2346 inode->i_gid = 0;
2347 inode->i_mode = p->mode;
2348 if (S_ISDIR(inode->i_mode))
2349 inode->i_nlink = 2;
2350 if (S_ISLNK(inode->i_mode))
2351 inode->i_size = 64;
2352 if (p->iop)
2353 inode->i_op = p->iop;
2354 if (p->fop)
2355 inode->i_fop = p->fop;
2356 ei->op = p->op;
2357 dentry->d_op = &proc_base_dentry_operations;
2358 d_add(dentry, inode);
2359 error = NULL;
2360out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002361 return error;
2362out_iput:
2363 iput(inode);
2364 goto out;
2365}
2366
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002367static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2368{
2369 struct dentry *error;
2370 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002371 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002372
2373 error = ERR_PTR(-ENOENT);
2374
2375 if (!task)
2376 goto out_no_task;
2377
2378 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002379 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2380 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002381 if (p->len != dentry->d_name.len)
2382 continue;
2383 if (!memcmp(dentry->d_name.name, p->name, p->len))
2384 break;
2385 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002386 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002387 goto out;
2388
2389 error = proc_base_instantiate(dir, dentry, task, p);
2390
2391out:
2392 put_task_struct(task);
2393out_no_task:
2394 return error;
2395}
2396
Eric Dumazetc5141e62007-05-08 00:26:15 -07002397static int proc_base_fill_cache(struct file *filp, void *dirent,
2398 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002399{
2400 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2401 proc_base_instantiate, task, p);
2402}
2403
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002404#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002405static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002406{
Andrea Righi940389b2008-07-28 00:48:12 +02002407 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002408 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002409
Andrea Righi59954772008-07-27 17:29:15 +02002410 if (whole && lock_task_sighand(task, &flags)) {
2411 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002412
Andrea Righi59954772008-07-27 17:29:15 +02002413 task_io_accounting_add(&acct, &task->signal->ioac);
2414 while_each_thread(task, t)
2415 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002416
Andrea Righi59954772008-07-27 17:29:15 +02002417 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002418 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002419 return sprintf(buffer,
2420 "rchar: %llu\n"
2421 "wchar: %llu\n"
2422 "syscr: %llu\n"
2423 "syscw: %llu\n"
2424 "read_bytes: %llu\n"
2425 "write_bytes: %llu\n"
2426 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002427 (unsigned long long)acct.rchar,
2428 (unsigned long long)acct.wchar,
2429 (unsigned long long)acct.syscr,
2430 (unsigned long long)acct.syscw,
2431 (unsigned long long)acct.read_bytes,
2432 (unsigned long long)acct.write_bytes,
2433 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002434}
Andrea Righi297c5d92008-07-25 01:48:49 -07002435
2436static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2437{
2438 return do_io_accounting(task, buffer, 0);
2439}
2440
2441static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2442{
2443 return do_io_accounting(task, buffer, 1);
2444}
2445#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002446
Kees Cook47830722008-10-06 03:11:58 +04002447static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2448 struct pid *pid, struct task_struct *task)
2449{
2450 seq_printf(m, "%08x\n", task->personality);
2451 return 0;
2452}
2453
Eric W. Biederman801199c2006-10-02 02:18:48 -07002454/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002455 * Thread groups
2456 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002457static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002458static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002459
Eric Dumazetc5141e62007-05-08 00:26:15 -07002460static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002461 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2462 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2463 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002464#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002465 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002466#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002467 REG("environ", S_IRUSR, proc_environ_operations),
2468 INF("auxv", S_IRUSR, proc_pid_auxv),
2469 ONE("status", S_IRUGO, proc_pid_status),
2470 ONE("personality", S_IRUSR, proc_pid_personality),
2471 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002472#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002473 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002474#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002475#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002476 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002477#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002478 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2479 ONE("stat", S_IRUGO, proc_tgid_stat),
2480 ONE("statm", S_IRUGO, proc_pid_statm),
2481 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002482#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002483 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002484#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002485 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2486 LNK("cwd", proc_cwd_link),
2487 LNK("root", proc_root_link),
2488 LNK("exe", proc_exe_link),
2489 REG("mounts", S_IRUGO, proc_mounts_operations),
2490 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2491 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002492#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002493 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2494 REG("smaps", S_IRUGO, proc_smaps_operations),
2495 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496#endif
2497#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002498 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002499#endif
2500#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002501 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502#endif
2503#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002504 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002506#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002507 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002508#endif
Paul Menage8793d852007-10-18 23:39:39 -07002509#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002510 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511#endif
Paul Menagea4243162007-10-18 23:39:35 -07002512#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002513 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002514#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002515 INF("oom_score", S_IRUGO, proc_oom_score),
2516 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002517#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002518 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2519 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002520#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002521#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002522 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002523#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002524#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002525 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002526#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002527#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002528 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002529#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002530};
2531
2532static int proc_tgid_base_readdir(struct file * filp,
2533 void * dirent, filldir_t filldir)
2534{
2535 return proc_pident_readdir(filp,dirent,filldir,
2536 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2537}
2538
Arjan van de Ven00977a52007-02-12 00:55:34 -08002539static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002540 .read = generic_read_dir,
2541 .readdir = proc_tgid_base_readdir,
2542};
2543
2544static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002545 return proc_pident_lookup(dir, dentry,
2546 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002547}
2548
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002549static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002550 .lookup = proc_tgid_base_lookup,
2551 .getattr = pid_getattr,
2552 .setattr = proc_setattr,
2553};
2554
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002555static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002557 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002558 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002559 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
Eric W. Biederman48e64842006-06-26 00:25:48 -07002561 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002562 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2563 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002564 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002565 if (!(current->flags & PF_EXITING))
2566 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002567 d_drop(dentry);
2568 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002571 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002572 goto out;
2573
2574 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002575 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2576 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002577 if (!leader)
2578 goto out;
2579
2580 name.name = "task";
2581 name.len = strlen(name.name);
2582 dir = d_hash_and_lookup(leader, &name);
2583 if (!dir)
2584 goto out_put_leader;
2585
2586 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002587 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002588 dentry = d_hash_and_lookup(dir, &name);
2589 if (dentry) {
2590 shrink_dcache_parent(dentry);
2591 d_drop(dentry);
2592 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002594
2595 dput(dir);
2596out_put_leader:
2597 dput(leader);
2598out:
2599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
Randy Dunlap0895e912007-10-21 21:00:10 -07002602/**
2603 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2604 * @task: task that should be flushed.
2605 *
2606 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002607 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002608 * in. This call is supposed to do all of this job.
2609 *
2610 * Looks in the dcache for
2611 * /proc/@pid
2612 * /proc/@tgid/task/@pid
2613 * if either directory is present flushes it and all of it'ts children
2614 * from the dcache.
2615 *
2616 * It is safe and reasonable to cache /proc entries for a task until
2617 * that task exits. After that they just clog up the dcache with
2618 * useless entries, possibly causing useful dcache entries to be
2619 * flushed instead. This routine is proved to flush those useless
2620 * dcache entries at process exit time.
2621 *
2622 * NOTE: This routine is just an optimization so it does not guarantee
2623 * that no dcache entries will exist at process exit time it
2624 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002625 */
2626
2627void proc_flush_task(struct task_struct *task)
2628{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002629 int i;
2630 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002631 struct upid *upid;
2632
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002633 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002634 if (thread_group_leader(task))
2635 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002636
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002637 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002638 upid = &pid->numbers[i];
2639 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002640 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002641 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002642
2643 upid = &pid->numbers[pid->level];
2644 if (upid->nr == 1)
2645 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002646}
2647
Adrian Bunk9711ef92006-12-06 20:38:31 -08002648static struct dentry *proc_pid_instantiate(struct inode *dir,
2649 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002650 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002651{
2652 struct dentry *error = ERR_PTR(-ENOENT);
2653 struct inode *inode;
2654
Eric W. Biederman61a28782006-10-02 02:18:49 -07002655 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002656 if (!inode)
2657 goto out;
2658
2659 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2660 inode->i_op = &proc_tgid_base_inode_operations;
2661 inode->i_fop = &proc_tgid_base_operations;
2662 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002663
2664 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2665 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002666
2667 dentry->d_op = &pid_dentry_operations;
2668
2669 d_add(dentry, inode);
2670 /* Close the race of the process dying before we return the dentry */
2671 if (pid_revalidate(dentry, NULL))
2672 error = NULL;
2673out:
2674 return error;
2675}
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2678{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002679 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002682 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Eric W. Biederman801199c2006-10-02 02:18:48 -07002684 result = proc_base_lookup(dir, dentry);
2685 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2686 goto out;
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 tgid = name_to_int(dentry);
2689 if (tgid == ~0U)
2690 goto out;
2691
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002692 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002693 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002694 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (task)
2696 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002697 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (!task)
2699 goto out;
2700
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002701 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002702 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002704 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002708 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002709 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002711struct tgid_iter {
2712 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002713 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002714};
2715static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2716{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002717 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002719 if (iter.task)
2720 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002721 rcu_read_lock();
2722retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002723 iter.task = NULL;
2724 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002725 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002726 iter.tgid = pid_nr_ns(pid, ns);
2727 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002728 /* What we to know is if the pid we have find is the
2729 * pid of a thread_group_leader. Testing for task
2730 * being a thread_group_leader is the obvious thing
2731 * todo but there is a window when it fails, due to
2732 * the pid transfer logic in de_thread.
2733 *
2734 * So we perform the straight forward test of seeing
2735 * if the pid we have found is the pid of a thread
2736 * group leader, and don't worry if the task we have
2737 * found doesn't happen to be a thread group leader.
2738 * As we don't care in the case of readdir.
2739 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002740 if (!iter.task || !has_group_leader_pid(iter.task)) {
2741 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002742 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002743 }
2744 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002746 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002747 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
2749
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002750#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Eric W. Biederman61a28782006-10-02 02:18:49 -07002752static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002753 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002754{
2755 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002756 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002757 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002758 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002759}
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761/* for the /proc/ directory itself, after non-process stuff has been done */
2762int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002765 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002766 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002767 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Eric W. Biederman61a28782006-10-02 02:18:49 -07002769 if (!reaper)
2770 goto out_no_task;
2771
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002772 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002773 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002774 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002775 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
2777
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002778 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002779 iter.task = NULL;
2780 iter.tgid = filp->f_pos - TGID_OFFSET;
2781 for (iter = next_tgid(ns, iter);
2782 iter.task;
2783 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2784 filp->f_pos = iter.tgid + TGID_OFFSET;
2785 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2786 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002787 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002790 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2791out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002792 put_task_struct(reaper);
2793out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return 0;
2795}
2796
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002797/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002798 * Tasks
2799 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002800static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002801 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2802 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fd_operations),
2803 REG("environ", S_IRUSR, proc_environ_operations),
2804 INF("auxv", S_IRUSR, proc_pid_auxv),
2805 ONE("status", S_IRUGO, proc_pid_status),
2806 ONE("personality", S_IRUSR, proc_pid_personality),
2807 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002808#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002809 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002810#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002811#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002812 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002813#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002814 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2815 ONE("stat", S_IRUGO, proc_tid_stat),
2816 ONE("statm", S_IRUGO, proc_pid_statm),
2817 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002818#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002819 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002820#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002821 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2822 LNK("cwd", proc_cwd_link),
2823 LNK("root", proc_root_link),
2824 LNK("exe", proc_exe_link),
2825 REG("mounts", S_IRUGO, proc_mounts_operations),
2826 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002827#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002828 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2829 REG("smaps", S_IRUGO, proc_smaps_operations),
2830 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002831#endif
2832#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002833 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002834#endif
2835#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002836 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002837#endif
2838#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002839 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002840#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002841#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002842 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002843#endif
Paul Menage8793d852007-10-18 23:39:39 -07002844#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002845 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002846#endif
Paul Menagea4243162007-10-18 23:39:35 -07002847#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002848 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002849#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002850 INF("oom_score", S_IRUGO, proc_oom_score),
2851 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002852#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002853 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2854 REG("sessionid", S_IRUSR, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002855#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002856#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002857 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002858#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002859#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002860 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07002861#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002862};
2863
2864static int proc_tid_base_readdir(struct file * filp,
2865 void * dirent, filldir_t filldir)
2866{
2867 return proc_pident_readdir(filp,dirent,filldir,
2868 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2869}
2870
2871static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002872 return proc_pident_lookup(dir, dentry,
2873 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002874}
2875
Arjan van de Ven00977a52007-02-12 00:55:34 -08002876static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002877 .read = generic_read_dir,
2878 .readdir = proc_tid_base_readdir,
2879};
2880
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002881static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002882 .lookup = proc_tid_base_lookup,
2883 .getattr = pid_getattr,
2884 .setattr = proc_setattr,
2885};
2886
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002887static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002888 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002889{
2890 struct dentry *error = ERR_PTR(-ENOENT);
2891 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002892 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002893
2894 if (!inode)
2895 goto out;
2896 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2897 inode->i_op = &proc_tid_base_inode_operations;
2898 inode->i_fop = &proc_tid_base_operations;
2899 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002900
2901 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2902 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002903
2904 dentry->d_op = &pid_dentry_operations;
2905
2906 d_add(dentry, inode);
2907 /* Close the race of the process dying before we return the dentry */
2908 if (pid_revalidate(dentry, NULL))
2909 error = NULL;
2910out:
2911 return error;
2912}
2913
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002914static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2915{
2916 struct dentry *result = ERR_PTR(-ENOENT);
2917 struct task_struct *task;
2918 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002919 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002920 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002921
2922 if (!leader)
2923 goto out_no_task;
2924
2925 tid = name_to_int(dentry);
2926 if (tid == ~0U)
2927 goto out;
2928
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002929 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002930 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002931 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932 if (task)
2933 get_task_struct(task);
2934 rcu_read_unlock();
2935 if (!task)
2936 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002937 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002938 goto out_drop_task;
2939
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002940 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002941out_drop_task:
2942 put_task_struct(task);
2943out:
2944 put_task_struct(leader);
2945out_no_task:
2946 return result;
2947}
2948
2949/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002950 * Find the first tid of a thread group to return to user space.
2951 *
2952 * Usually this is just the thread group leader, but if the users
2953 * buffer was too small or there was a seek into the middle of the
2954 * directory we have more work todo.
2955 *
2956 * In the case of a short read we start with find_task_by_pid.
2957 *
2958 * In the case of a seek we start with the leader and walk nr
2959 * threads past it.
2960 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002961static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002962 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002963{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002964 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002965
Eric W. Biedermancc288732006-06-26 00:26:01 -07002966 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002967 /* Attempt to start with the pid of a thread */
2968 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002969 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002970 if (pos && (pos->group_leader == leader))
2971 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002972 }
2973
2974 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002975 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002976 if (nr && nr >= get_nr_threads(leader))
2977 goto out;
2978
2979 /* If we haven't found our starting place yet start
2980 * with the leader and walk nr threads forward.
2981 */
2982 for (pos = leader; nr > 0; --nr) {
2983 pos = next_thread(pos);
2984 if (pos == leader) {
2985 pos = NULL;
2986 goto out;
2987 }
2988 }
2989found:
2990 get_task_struct(pos);
2991out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002992 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002993 return pos;
2994}
2995
2996/*
2997 * Find the next thread in the thread list.
2998 * Return NULL if there is an error or no next thread.
2999 *
3000 * The reference to the input task_struct is released.
3001 */
3002static struct task_struct *next_tid(struct task_struct *start)
3003{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003004 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003005 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003006 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003007 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003008 if (thread_group_leader(pos))
3009 pos = NULL;
3010 else
3011 get_task_struct(pos);
3012 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003013 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003014 put_task_struct(start);
3015 return pos;
3016}
3017
Eric W. Biederman61a28782006-10-02 02:18:49 -07003018static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3019 struct task_struct *task, int tid)
3020{
3021 char name[PROC_NUMBUF];
3022 int len = snprintf(name, sizeof(name), "%d", tid);
3023 return proc_fill_cache(filp, dirent, filldir, name, len,
3024 proc_task_instantiate, task, NULL);
3025}
3026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027/* for the /proc/TGID/task/ directories */
3028static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3029{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003030 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003032 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003033 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 int retval = -ENOENT;
3035 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003036 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003038 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003040 task = get_proc_task(inode);
3041 if (!task)
3042 goto out_no_task;
3043 rcu_read_lock();
3044 if (pid_alive(task)) {
3045 leader = task->group_leader;
3046 get_task_struct(leader);
3047 }
3048 rcu_read_unlock();
3049 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003050 if (!leader)
3051 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 retval = 0;
3053
3054 switch (pos) {
3055 case 0:
3056 ino = inode->i_ino;
3057 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3058 goto out;
3059 pos++;
3060 /* fall through */
3061 case 1:
3062 ino = parent_ino(dentry);
3063 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3064 goto out;
3065 pos++;
3066 /* fall through */
3067 }
3068
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003069 /* f_version caches the tgid value that the last readdir call couldn't
3070 * return. lseek aka telldir automagically resets f_version to 0.
3071 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003072 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003073 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003074 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003075 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003076 task;
3077 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003078 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003079 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003080 /* returning this tgid failed, save it as the first
3081 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003082 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003083 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
3087out:
3088 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003089 put_task_struct(leader);
3090out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return retval;
3092}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003093
3094static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3095{
3096 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003097 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003098 generic_fillattr(inode, stat);
3099
Eric W. Biederman99f89552006-06-26 00:25:55 -07003100 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003101 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003102 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003103 }
3104
3105 return 0;
3106}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003107
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003108static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003109 .lookup = proc_task_lookup,
3110 .getattr = proc_task_getattr,
3111 .setattr = proc_setattr,
3112};
3113
Arjan van de Ven00977a52007-02-12 00:55:34 -08003114static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003115 .read = generic_read_dir,
3116 .readdir = proc_task_readdir,
3117};