blob: 808cbdc193d309572c68b03d1b81b318511a4efc [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>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040059#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/string.h>
61#include <linux/seq_file.h>
62#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080063#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070067#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070068#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/mount.h>
70#include <linux/security.h>
71#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070072#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/cpuset.h>
74#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050075#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070076#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070077#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070078#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070079#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include "internal.h"
81
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070082/* NOTE:
83 * Implementing inode permission operations in /proc is almost
84 * certainly an error. Permission checks need to happen during
85 * each system call not at open time. The reason is that most of
86 * what we wish to check for permissions in /proc varies at runtime.
87 *
88 * The classic example of a problem is opening file descriptors
89 * in /proc for a task before it execs a suid executable.
90 */
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070094 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080096 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080097 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070098 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Eric W. Biederman61a28782006-10-02 02:18:49 -0700101#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700102 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700103 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .mode = MODE, \
105 .iop = IOP, \
106 .fop = FOP, \
107 .op = OP, \
108}
109
Eric W. Biederman61a28782006-10-02 02:18:49 -0700110#define DIR(NAME, MODE, OTYPE) \
111 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700112 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
113 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700114#define LNK(NAME, OTYPE) \
115 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700116 &proc_pid_link_inode_operations, NULL, \
117 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700118#define REG(NAME, MODE, OTYPE) \
119 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700121#define INF(NAME, MODE, OTYPE) \
122 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700123 NULL, &proc_info_file_operations, \
124 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800125#define ONE(NAME, MODE, OTYPE) \
126 NOD(NAME, (S_IFREG|(MODE)), \
127 NULL, &proc_single_file_operations, \
128 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Kees Cook5096add2007-05-08 00:26:04 -0700130int maps_protect;
131EXPORT_SYMBOL(maps_protect);
132
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700133static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700136 task_lock(task);
137 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if(fs)
139 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700140 task_unlock(task);
141 return fs;
142}
143
Eric W. Biederman99f89552006-06-26 00:25:55 -0700144static int get_nr_threads(struct task_struct *tsk)
145{
146 /* Must be called with the rcu_read_lock held */
147 unsigned long flags;
148 int count = 0;
149
150 if (lock_task_sighand(tsk, &flags)) {
151 count = atomic_read(&tsk->signal->count);
152 unlock_task_sighand(tsk, &flags);
153 }
154 return count;
155}
156
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800157static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700158{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700159 struct task_struct *task = get_proc_task(inode);
160 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700162
163 if (task) {
164 fs = get_fs_struct(task);
165 put_task_struct(task);
166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (fs) {
168 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800169 *path = fs->pwd;
170 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 read_unlock(&fs->lock);
172 result = 0;
173 put_fs_struct(fs);
174 }
175 return result;
176}
177
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800178static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700180 struct task_struct *task = get_proc_task(inode);
181 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700183
184 if (task) {
185 fs = get_fs_struct(task);
186 put_task_struct(task);
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (fs) {
189 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800190 *path = fs->root;
191 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 read_unlock(&fs->lock);
193 result = 0;
194 put_fs_struct(fs);
195 }
196 return result;
197}
198
Roland McGrath638fa2022008-04-29 01:01:38 -0700199/*
200 * Return zero if current may access user memory in @task, -error if not.
201 */
202static int check_mem_permission(struct task_struct *task)
203{
204 /*
205 * A task can always look at itself, in case it chooses
206 * to use system calls instead of load instructions.
207 */
208 if (task == current)
209 return 0;
210
211 /*
212 * If current is actively ptrace'ing, and would also be
213 * permitted to freshly attach with ptrace now, permit it.
214 */
215 if (task->parent == current && (task->ptrace & PT_PTRACED) &&
216 task_is_stopped_or_traced(task) &&
217 ptrace_may_attach(task))
218 return 0;
219
220 /*
221 * Noone else is allowed.
222 */
223 return -EPERM;
224}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Al Viro831830b2008-01-02 14:09:57 +0000226struct mm_struct *mm_for_maps(struct task_struct *task)
227{
228 struct mm_struct *mm = get_task_mm(task);
229 if (!mm)
230 return NULL;
231 down_read(&mm->mmap_sem);
232 task_lock(task);
233 if (task->mm != mm)
234 goto out;
235 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
236 goto out;
237 task_unlock(task);
238 return mm;
239out:
240 task_unlock(task);
241 up_read(&mm->mmap_sem);
242 mmput(mm);
243 return NULL;
244}
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static int proc_pid_cmdline(struct task_struct *task, char * buffer)
247{
248 int res = 0;
249 unsigned int len;
250 struct mm_struct *mm = get_task_mm(task);
251 if (!mm)
252 goto out;
253 if (!mm->arg_end)
254 goto out_mm; /* Shh! No looking before we're done */
255
256 len = mm->arg_end - mm->arg_start;
257
258 if (len > PAGE_SIZE)
259 len = PAGE_SIZE;
260
261 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
262
263 // If the nul at the end of args has been overwritten, then
264 // assume application is using setproctitle(3).
265 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
266 len = strnlen(buffer, res);
267 if (len < res) {
268 res = len;
269 } else {
270 len = mm->env_end - mm->env_start;
271 if (len > PAGE_SIZE - res)
272 len = PAGE_SIZE - res;
273 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
274 res = strnlen(buffer, res);
275 }
276 }
277out_mm:
278 mmput(mm);
279out:
280 return res;
281}
282
283static int proc_pid_auxv(struct task_struct *task, char *buffer)
284{
285 int res = 0;
286 struct mm_struct *mm = get_task_mm(task);
287 if (mm) {
288 unsigned int nwords = 0;
289 do
290 nwords += 2;
291 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
292 res = nwords * sizeof(mm->saved_auxv[0]);
293 if (res > PAGE_SIZE)
294 res = PAGE_SIZE;
295 memcpy(buffer, mm->saved_auxv, res);
296 mmput(mm);
297 }
298 return res;
299}
300
301
302#ifdef CONFIG_KALLSYMS
303/*
304 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
305 * Returns the resolved symbol. If that fails, simply return the address.
306 */
307static int proc_pid_wchan(struct task_struct *task, char *buffer)
308{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700309 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700310 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 wchan = get_wchan(task);
313
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700314 if (lookup_symbol_name(wchan, symname) < 0)
315 return sprintf(buffer, "%lu", wchan);
316 else
317 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319#endif /* CONFIG_KALLSYMS */
320
321#ifdef CONFIG_SCHEDSTATS
322/*
323 * Provides /proc/PID/schedstat
324 */
325static int proc_pid_schedstat(struct task_struct *task, char *buffer)
326{
Balbir Singh172ba842007-07-09 18:52:00 +0200327 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 task->sched_info.cpu_time,
329 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200330 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332#endif
333
Arjan van de Ven97455122008-01-25 21:08:34 +0100334#ifdef CONFIG_LATENCYTOP
335static int lstats_show_proc(struct seq_file *m, void *v)
336{
337 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800338 struct inode *inode = m->private;
339 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100340
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800341 if (!task)
342 return -ESRCH;
343 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100344 for (i = 0; i < 32; i++) {
345 if (task->latency_record[i].backtrace[0]) {
346 int q;
347 seq_printf(m, "%i %li %li ",
348 task->latency_record[i].count,
349 task->latency_record[i].time,
350 task->latency_record[i].max);
351 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
352 char sym[KSYM_NAME_LEN];
353 char *c;
354 if (!task->latency_record[i].backtrace[q])
355 break;
356 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
357 break;
358 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
359 c = strchr(sym, '+');
360 if (c)
361 *c = 0;
362 seq_printf(m, "%s ", sym);
363 }
364 seq_printf(m, "\n");
365 }
366
367 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800368 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100369 return 0;
370}
371
372static int lstats_open(struct inode *inode, struct file *file)
373{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800374 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800375}
376
Arjan van de Ven97455122008-01-25 21:08:34 +0100377static ssize_t lstats_write(struct file *file, const char __user *buf,
378 size_t count, loff_t *offs)
379{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800380 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100381
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800382 if (!task)
383 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100384 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800385 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100386
387 return count;
388}
389
390static const struct file_operations proc_lstats_operations = {
391 .open = lstats_open,
392 .read = seq_read,
393 .write = lstats_write,
394 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800395 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100396};
397
398#endif
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/* The badness from the OOM killer */
401unsigned long badness(struct task_struct *p, unsigned long uptime);
402static int proc_oom_score(struct task_struct *task, char *buffer)
403{
404 unsigned long points;
405 struct timespec uptime;
406
407 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700408 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700410 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return sprintf(buffer, "%lu\n", points);
412}
413
Neil Hormand85f50d2007-10-18 23:40:37 -0700414struct limit_names {
415 char *name;
416 char *unit;
417};
418
419static const struct limit_names lnames[RLIM_NLIMITS] = {
420 [RLIMIT_CPU] = {"Max cpu time", "ms"},
421 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
422 [RLIMIT_DATA] = {"Max data size", "bytes"},
423 [RLIMIT_STACK] = {"Max stack size", "bytes"},
424 [RLIMIT_CORE] = {"Max core file size", "bytes"},
425 [RLIMIT_RSS] = {"Max resident set", "bytes"},
426 [RLIMIT_NPROC] = {"Max processes", "processes"},
427 [RLIMIT_NOFILE] = {"Max open files", "files"},
428 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
429 [RLIMIT_AS] = {"Max address space", "bytes"},
430 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
431 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
432 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
433 [RLIMIT_NICE] = {"Max nice priority", NULL},
434 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800435 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700436};
437
438/* Display limits for a process */
439static int proc_pid_limits(struct task_struct *task, char *buffer)
440{
441 unsigned int i;
442 int count = 0;
443 unsigned long flags;
444 char *bufptr = buffer;
445
446 struct rlimit rlim[RLIM_NLIMITS];
447
448 rcu_read_lock();
449 if (!lock_task_sighand(task,&flags)) {
450 rcu_read_unlock();
451 return 0;
452 }
453 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
454 unlock_task_sighand(task, &flags);
455 rcu_read_unlock();
456
457 /*
458 * print the file header
459 */
460 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
461 "Limit", "Soft Limit", "Hard Limit", "Units");
462
463 for (i = 0; i < RLIM_NLIMITS; i++) {
464 if (rlim[i].rlim_cur == RLIM_INFINITY)
465 count += sprintf(&bufptr[count], "%-25s %-20s ",
466 lnames[i].name, "unlimited");
467 else
468 count += sprintf(&bufptr[count], "%-25s %-20lu ",
469 lnames[i].name, rlim[i].rlim_cur);
470
471 if (rlim[i].rlim_max == RLIM_INFINITY)
472 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
473 else
474 count += sprintf(&bufptr[count], "%-20lu ",
475 rlim[i].rlim_max);
476
477 if (lnames[i].unit)
478 count += sprintf(&bufptr[count], "%-10s\n",
479 lnames[i].unit);
480 else
481 count += sprintf(&bufptr[count], "\n");
482 }
483
484 return count;
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487/************************************************************************/
488/* Here the fs part begins */
489/************************************************************************/
490
491/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700492static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700494 struct task_struct *task;
495 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700496 /* Allow access to a task's file descriptors if it is us or we
497 * may use ptrace attach to the process and find out that
498 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700499 */
500 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700501 if (task) {
502 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700503 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700504 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700505 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700508static int proc_setattr(struct dentry *dentry, struct iattr *attr)
509{
510 int error;
511 struct inode *inode = dentry->d_inode;
512
513 if (attr->ia_valid & ATTR_MODE)
514 return -EPERM;
515
516 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700517 if (!error)
518 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700519 return error;
520}
521
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800522static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700523 .setattr = proc_setattr,
524};
525
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100526static int mounts_open_common(struct inode *inode, struct file *file,
527 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700529 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700530 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800531 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100532 struct fs_struct *fs = NULL;
533 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500534 struct proc_mounts *p;
535 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Eric W. Biederman99f89552006-06-26 00:25:55 -0700537 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700538 rcu_read_lock();
539 nsp = task_nsproxy(task);
540 if (nsp) {
541 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800542 if (ns)
543 get_mnt_ns(ns);
544 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700545 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100546 if (ns)
547 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700548 put_task_struct(task);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100551 if (!ns)
552 goto err;
553 if (!fs)
554 goto err_put_ns;
555
556 read_lock(&fs->lock);
557 root = fs->root;
558 path_get(&root);
559 read_unlock(&fs->lock);
560 put_fs_struct(fs);
561
562 ret = -ENOMEM;
563 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
564 if (!p)
565 goto err_put_path;
566
567 file->private_data = &p->m;
568 ret = seq_open(file, op);
569 if (ret)
570 goto err_free;
571
572 p->m.private = p;
573 p->ns = ns;
574 p->root = root;
575 p->event = ns->event;
576
577 return 0;
578
579 err_free:
580 kfree(p);
581 err_put_path:
582 path_put(&root);
583 err_put_ns:
584 put_mnt_ns(ns);
585 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return ret;
587}
588
589static int mounts_release(struct inode *inode, struct file *file)
590{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100591 struct proc_mounts *p = file->private_data;
592 path_put(&p->root);
593 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return seq_release(inode, file);
595}
596
Al Viro5addc5d2005-11-07 17:15:49 -0500597static unsigned mounts_poll(struct file *file, poll_table *wait)
598{
599 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100600 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500601 unsigned res = 0;
602
603 poll_wait(file, &ns->poll, wait);
604
605 spin_lock(&vfsmount_lock);
606 if (p->event != ns->event) {
607 p->event = ns->event;
608 res = POLLERR;
609 }
610 spin_unlock(&vfsmount_lock);
611
612 return res;
613}
614
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100615static int mounts_open(struct inode *inode, struct file *file)
616{
617 return mounts_open_common(inode, file, &mounts_op);
618}
619
Arjan van de Ven00977a52007-02-12 00:55:34 -0800620static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 .open = mounts_open,
622 .read = seq_read,
623 .llseek = seq_lseek,
624 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500625 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626};
627
Ram Pai2d4d4862008-03-27 13:06:25 +0100628static int mountinfo_open(struct inode *inode, struct file *file)
629{
630 return mounts_open_common(inode, file, &mountinfo_op);
631}
632
633static const struct file_operations proc_mountinfo_operations = {
634 .open = mountinfo_open,
635 .read = seq_read,
636 .llseek = seq_lseek,
637 .release = mounts_release,
638 .poll = mounts_poll,
639};
640
Chuck Leverb4629fe2006-03-20 13:44:12 -0500641static int mountstats_open(struct inode *inode, struct file *file)
642{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100643 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500644}
645
Arjan van de Ven00977a52007-02-12 00:55:34 -0800646static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500647 .open = mountstats_open,
648 .read = seq_read,
649 .llseek = seq_lseek,
650 .release = mounts_release,
651};
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
654
655static ssize_t proc_info_read(struct file * file, char __user * buf,
656 size_t count, loff_t *ppos)
657{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800658 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 unsigned long page;
660 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700661 struct task_struct *task = get_proc_task(inode);
662
663 length = -ESRCH;
664 if (!task)
665 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (count > PROC_BLOCK_SIZE)
668 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700669
670 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700671 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 length = PROC_I(inode)->op.proc_read(task, (char*)page);
675
676 if (length >= 0)
677 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
678 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700679out:
680 put_task_struct(task);
681out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return length;
683}
684
Arjan van de Ven00977a52007-02-12 00:55:34 -0800685static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 .read = proc_info_read,
687};
688
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800689static int proc_single_show(struct seq_file *m, void *v)
690{
691 struct inode *inode = m->private;
692 struct pid_namespace *ns;
693 struct pid *pid;
694 struct task_struct *task;
695 int ret;
696
697 ns = inode->i_sb->s_fs_info;
698 pid = proc_pid(inode);
699 task = get_pid_task(pid, PIDTYPE_PID);
700 if (!task)
701 return -ESRCH;
702
703 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
704
705 put_task_struct(task);
706 return ret;
707}
708
709static int proc_single_open(struct inode *inode, struct file *filp)
710{
711 int ret;
712 ret = single_open(filp, proc_single_show, NULL);
713 if (!ret) {
714 struct seq_file *m = filp->private_data;
715
716 m->private = inode;
717 }
718 return ret;
719}
720
721static const struct file_operations proc_single_file_operations = {
722 .open = proc_single_open,
723 .read = seq_read,
724 .llseek = seq_lseek,
725 .release = single_release,
726};
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static int mem_open(struct inode* inode, struct file* file)
729{
730 file->private_data = (void*)((long)current->self_exec_id);
731 return 0;
732}
733
734static ssize_t mem_read(struct file * file, char __user * buf,
735 size_t count, loff_t *ppos)
736{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800737 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 char *page;
739 unsigned long src = *ppos;
740 int ret = -ESRCH;
741 struct mm_struct *mm;
742
Eric W. Biederman99f89552006-06-26 00:25:55 -0700743 if (!task)
744 goto out_no_task;
745
Roland McGrath638fa2022008-04-29 01:01:38 -0700746 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto out;
748
749 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700750 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (!page)
752 goto out;
753
754 ret = 0;
755
756 mm = get_task_mm(task);
757 if (!mm)
758 goto out_free;
759
760 ret = -EIO;
761
762 if (file->private_data != (void*)((long)current->self_exec_id))
763 goto out_put;
764
765 ret = 0;
766
767 while (count > 0) {
768 int this_len, retval;
769
770 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
771 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa2022008-04-29 01:01:38 -0700772 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!ret)
774 ret = -EIO;
775 break;
776 }
777
778 if (copy_to_user(buf, page, retval)) {
779 ret = -EFAULT;
780 break;
781 }
782
783 ret += retval;
784 src += retval;
785 buf += retval;
786 count -= retval;
787 }
788 *ppos = src;
789
790out_put:
791 mmput(mm);
792out_free:
793 free_page((unsigned long) page);
794out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700795 put_task_struct(task);
796out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return ret;
798}
799
800#define mem_write NULL
801
802#ifndef mem_write
803/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800804static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 size_t count, loff_t *ppos)
806{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700807 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800809 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 unsigned long dst = *ppos;
811
Eric W. Biederman99f89552006-06-26 00:25:55 -0700812 copied = -ESRCH;
813 if (!task)
814 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Roland McGrath638fa2022008-04-29 01:01:38 -0700816 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700817 goto out;
818
819 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700820 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700822 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700824 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 while (count > 0) {
826 int this_len, retval;
827
828 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
829 if (copy_from_user(page, buf, this_len)) {
830 copied = -EFAULT;
831 break;
832 }
833 retval = access_process_vm(task, dst, page, this_len, 1);
834 if (!retval) {
835 if (!copied)
836 copied = -EIO;
837 break;
838 }
839 copied += retval;
840 buf += retval;
841 dst += retval;
842 count -= retval;
843 }
844 *ppos = dst;
845 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700846out:
847 put_task_struct(task);
848out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return copied;
850}
851#endif
852
Matt Mackall85863e42008-02-04 22:29:04 -0800853loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 switch (orig) {
856 case 0:
857 file->f_pos = offset;
858 break;
859 case 1:
860 file->f_pos += offset;
861 break;
862 default:
863 return -EINVAL;
864 }
865 force_successful_syscall_return();
866 return file->f_pos;
867}
868
Arjan van de Ven00977a52007-02-12 00:55:34 -0800869static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 .llseek = mem_lseek,
871 .read = mem_read,
872 .write = mem_write,
873 .open = mem_open,
874};
875
James Pearson315e28c2007-10-16 23:30:17 -0700876static ssize_t environ_read(struct file *file, char __user *buf,
877 size_t count, loff_t *ppos)
878{
879 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
880 char *page;
881 unsigned long src = *ppos;
882 int ret = -ESRCH;
883 struct mm_struct *mm;
884
885 if (!task)
886 goto out_no_task;
887
888 if (!ptrace_may_attach(task))
889 goto out;
890
891 ret = -ENOMEM;
892 page = (char *)__get_free_page(GFP_TEMPORARY);
893 if (!page)
894 goto out;
895
896 ret = 0;
897
898 mm = get_task_mm(task);
899 if (!mm)
900 goto out_free;
901
902 while (count > 0) {
903 int this_len, retval, max_len;
904
905 this_len = mm->env_end - (mm->env_start + src);
906
907 if (this_len <= 0)
908 break;
909
910 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
911 this_len = (this_len > max_len) ? max_len : this_len;
912
913 retval = access_process_vm(task, (mm->env_start + src),
914 page, this_len, 0);
915
916 if (retval <= 0) {
917 ret = retval;
918 break;
919 }
920
921 if (copy_to_user(buf, page, retval)) {
922 ret = -EFAULT;
923 break;
924 }
925
926 ret += retval;
927 src += retval;
928 buf += retval;
929 count -= retval;
930 }
931 *ppos = src;
932
933 mmput(mm);
934out_free:
935 free_page((unsigned long) page);
936out:
937 put_task_struct(task);
938out_no_task:
939 return ret;
940}
941
942static const struct file_operations proc_environ_operations = {
943 .read = environ_read,
944};
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946static ssize_t oom_adjust_read(struct file *file, char __user *buf,
947 size_t count, loff_t *ppos)
948{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800949 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700950 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700952 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Eric W. Biederman99f89552006-06-26 00:25:55 -0700954 if (!task)
955 return -ESRCH;
956 oom_adjust = task->oomkilladj;
957 put_task_struct(task);
958
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700959 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700960
961 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
965 size_t count, loff_t *ppos)
966{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700967 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700968 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int oom_adjust;
970
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700971 memset(buffer, 0, sizeof(buffer));
972 if (count > sizeof(buffer) - 1)
973 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (copy_from_user(buffer, buf, count))
975 return -EFAULT;
976 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700977 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
978 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return -EINVAL;
980 if (*end == '\n')
981 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800982 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700983 if (!task)
984 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800985 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
986 put_task_struct(task);
987 return -EACCES;
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700990 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (end - buffer == 0)
992 return -EIO;
993 return end - buffer;
994}
995
Arjan van de Ven00977a52007-02-12 00:55:34 -0800996static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 .read = oom_adjust_read,
998 .write = oom_adjust_write,
999};
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001#ifdef CONFIG_AUDITSYSCALL
1002#define TMPBUFLEN 21
1003static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1004 size_t count, loff_t *ppos)
1005{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001006 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001007 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 ssize_t length;
1009 char tmpbuf[TMPBUFLEN];
1010
Eric W. Biederman99f89552006-06-26 00:25:55 -07001011 if (!task)
1012 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001014 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001015 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1017}
1018
1019static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1020 size_t count, loff_t *ppos)
1021{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001022 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 char *page, *tmp;
1024 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 uid_t loginuid;
1026
1027 if (!capable(CAP_AUDIT_CONTROL))
1028 return -EPERM;
1029
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001030 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return -EPERM;
1032
Al Viroe0182902006-05-18 08:28:02 -04001033 if (count >= PAGE_SIZE)
1034 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 if (*ppos != 0) {
1037 /* No partial writes. */
1038 return -EINVAL;
1039 }
Mel Gormane12ba742007-10-16 01:25:52 -07001040 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (!page)
1042 return -ENOMEM;
1043 length = -EFAULT;
1044 if (copy_from_user(page, buf, count))
1045 goto out_free_page;
1046
Al Viroe0182902006-05-18 08:28:02 -04001047 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 loginuid = simple_strtoul(page, &tmp, 10);
1049 if (tmp == page) {
1050 length = -EINVAL;
1051 goto out_free_page;
1052
1053 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001054 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (likely(length == 0))
1056 length = count;
1057
1058out_free_page:
1059 free_page((unsigned long) page);
1060 return length;
1061}
1062
Arjan van de Ven00977a52007-02-12 00:55:34 -08001063static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 .read = proc_loginuid_read,
1065 .write = proc_loginuid_write,
1066};
Eric Paris1e0bd752008-03-13 08:15:31 -04001067
1068static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1069 size_t count, loff_t *ppos)
1070{
1071 struct inode * inode = file->f_path.dentry->d_inode;
1072 struct task_struct *task = get_proc_task(inode);
1073 ssize_t length;
1074 char tmpbuf[TMPBUFLEN];
1075
1076 if (!task)
1077 return -ESRCH;
1078 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1079 audit_get_sessionid(task));
1080 put_task_struct(task);
1081 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1082}
1083
1084static const struct file_operations proc_sessionid_operations = {
1085 .read = proc_sessionid_read,
1086};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087#endif
1088
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001089#ifdef CONFIG_FAULT_INJECTION
1090static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1091 size_t count, loff_t *ppos)
1092{
1093 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1094 char buffer[PROC_NUMBUF];
1095 size_t len;
1096 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001097
1098 if (!task)
1099 return -ESRCH;
1100 make_it_fail = task->make_it_fail;
1101 put_task_struct(task);
1102
1103 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001104
1105 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001106}
1107
1108static ssize_t proc_fault_inject_write(struct file * file,
1109 const char __user * buf, size_t count, loff_t *ppos)
1110{
1111 struct task_struct *task;
1112 char buffer[PROC_NUMBUF], *end;
1113 int make_it_fail;
1114
1115 if (!capable(CAP_SYS_RESOURCE))
1116 return -EPERM;
1117 memset(buffer, 0, sizeof(buffer));
1118 if (count > sizeof(buffer) - 1)
1119 count = sizeof(buffer) - 1;
1120 if (copy_from_user(buffer, buf, count))
1121 return -EFAULT;
1122 make_it_fail = simple_strtol(buffer, &end, 0);
1123 if (*end == '\n')
1124 end++;
1125 task = get_proc_task(file->f_dentry->d_inode);
1126 if (!task)
1127 return -ESRCH;
1128 task->make_it_fail = make_it_fail;
1129 put_task_struct(task);
1130 if (end - buffer == 0)
1131 return -EIO;
1132 return end - buffer;
1133}
1134
Arjan van de Ven00977a52007-02-12 00:55:34 -08001135static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001136 .read = proc_fault_inject_read,
1137 .write = proc_fault_inject_write,
1138};
1139#endif
1140
Arjan van de Ven97455122008-01-25 21:08:34 +01001141
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001142#ifdef CONFIG_SCHED_DEBUG
1143/*
1144 * Print out various scheduling related per-task fields:
1145 */
1146static int sched_show(struct seq_file *m, void *v)
1147{
1148 struct inode *inode = m->private;
1149 struct task_struct *p;
1150
1151 WARN_ON(!inode);
1152
1153 p = get_proc_task(inode);
1154 if (!p)
1155 return -ESRCH;
1156 proc_sched_show_task(p, m);
1157
1158 put_task_struct(p);
1159
1160 return 0;
1161}
1162
1163static ssize_t
1164sched_write(struct file *file, const char __user *buf,
1165 size_t count, loff_t *offset)
1166{
1167 struct inode *inode = file->f_path.dentry->d_inode;
1168 struct task_struct *p;
1169
1170 WARN_ON(!inode);
1171
1172 p = get_proc_task(inode);
1173 if (!p)
1174 return -ESRCH;
1175 proc_sched_set_task(p);
1176
1177 put_task_struct(p);
1178
1179 return count;
1180}
1181
1182static int sched_open(struct inode *inode, struct file *filp)
1183{
1184 int ret;
1185
1186 ret = single_open(filp, sched_show, NULL);
1187 if (!ret) {
1188 struct seq_file *m = filp->private_data;
1189
1190 m->private = inode;
1191 }
1192 return ret;
1193}
1194
1195static const struct file_operations proc_pid_sched_operations = {
1196 .open = sched_open,
1197 .read = seq_read,
1198 .write = sched_write,
1199 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001200 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001201};
1202
1203#endif
1204
Matt Helsley925d1c42008-04-29 01:01:36 -07001205/*
1206 * We added or removed a vma mapping the executable. The vmas are only mapped
1207 * during exec and are not mapped with the mmap system call.
1208 * Callers must hold down_write() on the mm's mmap_sem for these
1209 */
1210void added_exe_file_vma(struct mm_struct *mm)
1211{
1212 mm->num_exe_file_vmas++;
1213}
1214
1215void removed_exe_file_vma(struct mm_struct *mm)
1216{
1217 mm->num_exe_file_vmas--;
1218 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1219 fput(mm->exe_file);
1220 mm->exe_file = NULL;
1221 }
1222
1223}
1224
1225void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1226{
1227 if (new_exe_file)
1228 get_file(new_exe_file);
1229 if (mm->exe_file)
1230 fput(mm->exe_file);
1231 mm->exe_file = new_exe_file;
1232 mm->num_exe_file_vmas = 0;
1233}
1234
1235struct file *get_mm_exe_file(struct mm_struct *mm)
1236{
1237 struct file *exe_file;
1238
1239 /* We need mmap_sem to protect against races with removal of
1240 * VM_EXECUTABLE vmas */
1241 down_read(&mm->mmap_sem);
1242 exe_file = mm->exe_file;
1243 if (exe_file)
1244 get_file(exe_file);
1245 up_read(&mm->mmap_sem);
1246 return exe_file;
1247}
1248
1249void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1250{
1251 /* It's safe to write the exe_file pointer without exe_file_lock because
1252 * this is called during fork when the task is not yet in /proc */
1253 newmm->exe_file = get_mm_exe_file(oldmm);
1254}
1255
1256static int proc_exe_link(struct inode *inode, struct path *exe_path)
1257{
1258 struct task_struct *task;
1259 struct mm_struct *mm;
1260 struct file *exe_file;
1261
1262 task = get_proc_task(inode);
1263 if (!task)
1264 return -ENOENT;
1265 mm = get_task_mm(task);
1266 put_task_struct(task);
1267 if (!mm)
1268 return -ENOENT;
1269 exe_file = get_mm_exe_file(mm);
1270 mmput(mm);
1271 if (exe_file) {
1272 *exe_path = exe_file->f_path;
1273 path_get(&exe_file->f_path);
1274 fput(exe_file);
1275 return 0;
1276 } else
1277 return -ENOENT;
1278}
1279
Al Viro008b1502005-08-20 00:17:39 +01001280static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct inode *inode = dentry->d_inode;
1283 int error = -EACCES;
1284
1285 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001286 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Eric W. Biederman778c1142006-06-26 00:25:58 -07001288 /* Are we allowed to snoop on the tasks file descriptors? */
1289 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001292 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 nd->last_type = LAST_BIND;
1294out:
Al Viro008b1502005-08-20 00:17:39 +01001295 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001298static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Mel Gormane12ba742007-10-16 01:25:52 -07001300 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001301 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 int len;
1303
1304 if (!tmp)
1305 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001306
Jan Blunckcf28b482008-02-14 19:38:44 -08001307 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001308 len = PTR_ERR(pathname);
1309 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001311 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 if (len > buflen)
1314 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001315 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 len = -EFAULT;
1317 out:
1318 free_page((unsigned long)tmp);
1319 return len;
1320}
1321
1322static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1323{
1324 int error = -EACCES;
1325 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001326 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Eric W. Biederman778c1142006-06-26 00:25:58 -07001328 /* Are we allowed to snoop on the tasks file descriptors? */
1329 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001332 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 if (error)
1334 goto out;
1335
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001336 error = do_proc_readlink(&path, buffer, buflen);
1337 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return error;
1340}
1341
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001342static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001344 .follow_link = proc_pid_follow_link,
1345 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346};
1347
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001348
1349/* building an inode */
1350
1351static int task_dumpable(struct task_struct *task)
1352{
1353 int dumpable = 0;
1354 struct mm_struct *mm;
1355
1356 task_lock(task);
1357 mm = task->mm;
1358 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001359 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001360 task_unlock(task);
1361 if(dumpable == 1)
1362 return 1;
1363 return 0;
1364}
1365
1366
Eric W. Biederman61a28782006-10-02 02:18:49 -07001367static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001368{
1369 struct inode * inode;
1370 struct proc_inode *ei;
1371
1372 /* We need a new inode */
1373
1374 inode = new_inode(sb);
1375 if (!inode)
1376 goto out;
1377
1378 /* Common stuff */
1379 ei = PROC_I(inode);
1380 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001381 inode->i_op = &proc_def_inode_operations;
1382
1383 /*
1384 * grab the reference to task.
1385 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001386 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001387 if (!ei->pid)
1388 goto out_unlock;
1389
1390 inode->i_uid = 0;
1391 inode->i_gid = 0;
1392 if (task_dumpable(task)) {
1393 inode->i_uid = task->euid;
1394 inode->i_gid = task->egid;
1395 }
1396 security_task_to_inode(task, inode);
1397
1398out:
1399 return inode;
1400
1401out_unlock:
1402 iput(inode);
1403 return NULL;
1404}
1405
1406static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1407{
1408 struct inode *inode = dentry->d_inode;
1409 struct task_struct *task;
1410 generic_fillattr(inode, stat);
1411
1412 rcu_read_lock();
1413 stat->uid = 0;
1414 stat->gid = 0;
1415 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1416 if (task) {
1417 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1418 task_dumpable(task)) {
1419 stat->uid = task->euid;
1420 stat->gid = task->egid;
1421 }
1422 }
1423 rcu_read_unlock();
1424 return 0;
1425}
1426
1427/* dentry stuff */
1428
1429/*
1430 * Exceptional case: normally we are not allowed to unhash a busy
1431 * directory. In this case, however, we can do it - no aliasing problems
1432 * due to the way we treat inodes.
1433 *
1434 * Rewrite the inode's ownerships here because the owning task may have
1435 * performed a setuid(), etc.
1436 *
1437 * Before the /proc/pid/status file was created the only way to read
1438 * the effective uid of a /process was to stat /proc/pid. Reading
1439 * /proc/pid/status is slow enough that procps and other packages
1440 * kept stating /proc/pid. To keep the rules in /proc simple I have
1441 * made this apply to all per process world readable and executable
1442 * directories.
1443 */
1444static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1445{
1446 struct inode *inode = dentry->d_inode;
1447 struct task_struct *task = get_proc_task(inode);
1448 if (task) {
1449 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1450 task_dumpable(task)) {
1451 inode->i_uid = task->euid;
1452 inode->i_gid = task->egid;
1453 } else {
1454 inode->i_uid = 0;
1455 inode->i_gid = 0;
1456 }
1457 inode->i_mode &= ~(S_ISUID | S_ISGID);
1458 security_task_to_inode(task, inode);
1459 put_task_struct(task);
1460 return 1;
1461 }
1462 d_drop(dentry);
1463 return 0;
1464}
1465
1466static int pid_delete_dentry(struct dentry * dentry)
1467{
1468 /* Is the task we represent dead?
1469 * If so, then don't put the dentry on the lru list,
1470 * kill it immediately.
1471 */
1472 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1473}
1474
1475static struct dentry_operations pid_dentry_operations =
1476{
1477 .d_revalidate = pid_revalidate,
1478 .d_delete = pid_delete_dentry,
1479};
1480
1481/* Lookups */
1482
Eric Dumazetc5141e62007-05-08 00:26:15 -07001483typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1484 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001485
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001486/*
1487 * Fill a directory entry.
1488 *
1489 * If possible create the dcache entry and derive our inode number and
1490 * file type from dcache entry.
1491 *
1492 * Since all of the proc inode numbers are dynamically generated, the inode
1493 * numbers do not exist until the inode is cache. This means creating the
1494 * the dcache entry in readdir is necessary to keep the inode numbers
1495 * reported by readdir in sync with the inode numbers reported
1496 * by stat.
1497 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001498static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1499 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001500 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001501{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001502 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001503 struct inode *inode;
1504 struct qstr qname;
1505 ino_t ino = 0;
1506 unsigned type = DT_UNKNOWN;
1507
1508 qname.name = name;
1509 qname.len = len;
1510 qname.hash = full_name_hash(name, len);
1511
1512 child = d_lookup(dir, &qname);
1513 if (!child) {
1514 struct dentry *new;
1515 new = d_alloc(dir, &qname);
1516 if (new) {
1517 child = instantiate(dir->d_inode, new, task, ptr);
1518 if (child)
1519 dput(new);
1520 else
1521 child = new;
1522 }
1523 }
1524 if (!child || IS_ERR(child) || !child->d_inode)
1525 goto end_instantiate;
1526 inode = child->d_inode;
1527 if (inode) {
1528 ino = inode->i_ino;
1529 type = inode->i_mode >> 12;
1530 }
1531 dput(child);
1532end_instantiate:
1533 if (!ino)
1534 ino = find_inode_number(dir, &qname);
1535 if (!ino)
1536 ino = 1;
1537 return filldir(dirent, name, len, filp->f_pos, ino, type);
1538}
1539
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001540static unsigned name_to_int(struct dentry *dentry)
1541{
1542 const char *name = dentry->d_name.name;
1543 int len = dentry->d_name.len;
1544 unsigned n = 0;
1545
1546 if (len > 1 && *name == '0')
1547 goto out;
1548 while (len-- > 0) {
1549 unsigned c = *name++ - '0';
1550 if (c > 9)
1551 goto out;
1552 if (n >= (~0U-9)/10)
1553 goto out;
1554 n *= 10;
1555 n += c;
1556 }
1557 return n;
1558out:
1559 return ~0U;
1560}
1561
Miklos Szeredi27932742007-05-08 00:26:17 -07001562#define PROC_FDINFO_MAX 64
1563
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001564static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001565{
1566 struct task_struct *task = get_proc_task(inode);
1567 struct files_struct *files = NULL;
1568 struct file *file;
1569 int fd = proc_fd(inode);
1570
1571 if (task) {
1572 files = get_files_struct(task);
1573 put_task_struct(task);
1574 }
1575 if (files) {
1576 /*
1577 * We are not taking a ref to the file structure, so we must
1578 * hold ->file_lock.
1579 */
1580 spin_lock(&files->file_lock);
1581 file = fcheck_files(files, fd);
1582 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001583 if (path) {
1584 *path = file->f_path;
1585 path_get(&file->f_path);
1586 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001587 if (info)
1588 snprintf(info, PROC_FDINFO_MAX,
1589 "pos:\t%lli\n"
1590 "flags:\t0%o\n",
1591 (long long) file->f_pos,
1592 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001593 spin_unlock(&files->file_lock);
1594 put_files_struct(files);
1595 return 0;
1596 }
1597 spin_unlock(&files->file_lock);
1598 put_files_struct(files);
1599 }
1600 return -ENOENT;
1601}
1602
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001603static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001604{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001605 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001606}
1607
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001608static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1609{
1610 struct inode *inode = dentry->d_inode;
1611 struct task_struct *task = get_proc_task(inode);
1612 int fd = proc_fd(inode);
1613 struct files_struct *files;
1614
1615 if (task) {
1616 files = get_files_struct(task);
1617 if (files) {
1618 rcu_read_lock();
1619 if (fcheck_files(files, fd)) {
1620 rcu_read_unlock();
1621 put_files_struct(files);
1622 if (task_dumpable(task)) {
1623 inode->i_uid = task->euid;
1624 inode->i_gid = task->egid;
1625 } else {
1626 inode->i_uid = 0;
1627 inode->i_gid = 0;
1628 }
1629 inode->i_mode &= ~(S_ISUID | S_ISGID);
1630 security_task_to_inode(task, inode);
1631 put_task_struct(task);
1632 return 1;
1633 }
1634 rcu_read_unlock();
1635 put_files_struct(files);
1636 }
1637 put_task_struct(task);
1638 }
1639 d_drop(dentry);
1640 return 0;
1641}
1642
1643static struct dentry_operations tid_fd_dentry_operations =
1644{
1645 .d_revalidate = tid_fd_revalidate,
1646 .d_delete = pid_delete_dentry,
1647};
1648
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001649static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001650 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001651{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001652 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001653 struct file *file;
1654 struct files_struct *files;
1655 struct inode *inode;
1656 struct proc_inode *ei;
1657 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001658
Eric W. Biederman61a28782006-10-02 02:18:49 -07001659 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001660 if (!inode)
1661 goto out;
1662 ei = PROC_I(inode);
1663 ei->fd = fd;
1664 files = get_files_struct(task);
1665 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001666 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001667 inode->i_mode = S_IFLNK;
1668
1669 /*
1670 * We are not taking a ref to the file structure, so we must
1671 * hold ->file_lock.
1672 */
1673 spin_lock(&files->file_lock);
1674 file = fcheck_files(files, fd);
1675 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001676 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001677 if (file->f_mode & 1)
1678 inode->i_mode |= S_IRUSR | S_IXUSR;
1679 if (file->f_mode & 2)
1680 inode->i_mode |= S_IWUSR | S_IXUSR;
1681 spin_unlock(&files->file_lock);
1682 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001683
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001684 inode->i_op = &proc_pid_link_inode_operations;
1685 inode->i_size = 64;
1686 ei->op.proc_get_link = proc_fd_link;
1687 dentry->d_op = &tid_fd_dentry_operations;
1688 d_add(dentry, inode);
1689 /* Close the race of the process dying before we return the dentry */
1690 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001691 error = NULL;
1692
1693 out:
1694 return error;
1695out_unlock:
1696 spin_unlock(&files->file_lock);
1697 put_files_struct(files);
1698out_iput:
1699 iput(inode);
1700 goto out;
1701}
1702
Miklos Szeredi27932742007-05-08 00:26:17 -07001703static struct dentry *proc_lookupfd_common(struct inode *dir,
1704 struct dentry *dentry,
1705 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001706{
1707 struct task_struct *task = get_proc_task(dir);
1708 unsigned fd = name_to_int(dentry);
1709 struct dentry *result = ERR_PTR(-ENOENT);
1710
1711 if (!task)
1712 goto out_no_task;
1713 if (fd == ~0U)
1714 goto out;
1715
Miklos Szeredi27932742007-05-08 00:26:17 -07001716 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001717out:
1718 put_task_struct(task);
1719out_no_task:
1720 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001721}
1722
Miklos Szeredi27932742007-05-08 00:26:17 -07001723static int proc_readfd_common(struct file * filp, void * dirent,
1724 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001726 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001727 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001728 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001729 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct files_struct * files;
1732
1733 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001734 if (!p)
1735 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 fd = filp->f_pos;
1739 switch (fd) {
1740 case 0:
1741 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1742 goto out;
1743 filp->f_pos++;
1744 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001745 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1747 goto out;
1748 filp->f_pos++;
1749 default:
1750 files = get_files_struct(p);
1751 if (!files)
1752 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001753 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001755 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001757 char name[PROC_NUMBUF];
1758 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 if (!fcheck_files(files, fd))
1761 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001762 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Miklos Szeredi27932742007-05-08 00:26:17 -07001764 len = snprintf(name, sizeof(name), "%d", fd);
1765 if (proc_fill_cache(filp, dirent, filldir,
1766 name, len, instantiate,
1767 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001768 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 break;
1770 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001771 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001773 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 put_files_struct(files);
1775 }
1776out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001777 put_task_struct(p);
1778out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return retval;
1780}
1781
Miklos Szeredi27932742007-05-08 00:26:17 -07001782static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1783 struct nameidata *nd)
1784{
1785 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1786}
1787
1788static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1789{
1790 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1791}
1792
1793static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1794 size_t len, loff_t *ppos)
1795{
1796 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001797 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001798 if (!err)
1799 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1800 return err;
1801}
1802
1803static const struct file_operations proc_fdinfo_file_operations = {
1804 .open = nonseekable_open,
1805 .read = proc_fdinfo_read,
1806};
1807
Arjan van de Ven00977a52007-02-12 00:55:34 -08001808static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 .read = generic_read_dir,
1810 .readdir = proc_readfd,
1811};
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001814 * /proc/pid/fd needs a special permission handler so that a process can still
1815 * access /proc/self/fd after it has executed a setuid().
1816 */
1817static int proc_fd_permission(struct inode *inode, int mask,
1818 struct nameidata *nd)
1819{
1820 int rv;
1821
1822 rv = generic_permission(inode, mask, NULL);
1823 if (rv == 0)
1824 return 0;
1825 if (task_pid(current) == proc_pid(inode))
1826 rv = 0;
1827 return rv;
1828}
1829
1830/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 * proc directories can do almost nothing..
1832 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001833static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001835 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001836 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837};
1838
Miklos Szeredi27932742007-05-08 00:26:17 -07001839static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1840 struct dentry *dentry, struct task_struct *task, const void *ptr)
1841{
1842 unsigned fd = *(unsigned *)ptr;
1843 struct inode *inode;
1844 struct proc_inode *ei;
1845 struct dentry *error = ERR_PTR(-ENOENT);
1846
1847 inode = proc_pid_make_inode(dir->i_sb, task);
1848 if (!inode)
1849 goto out;
1850 ei = PROC_I(inode);
1851 ei->fd = fd;
1852 inode->i_mode = S_IFREG | S_IRUSR;
1853 inode->i_fop = &proc_fdinfo_file_operations;
1854 dentry->d_op = &tid_fd_dentry_operations;
1855 d_add(dentry, inode);
1856 /* Close the race of the process dying before we return the dentry */
1857 if (tid_fd_revalidate(dentry, NULL))
1858 error = NULL;
1859
1860 out:
1861 return error;
1862}
1863
1864static struct dentry *proc_lookupfdinfo(struct inode *dir,
1865 struct dentry *dentry,
1866 struct nameidata *nd)
1867{
1868 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1869}
1870
1871static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1872{
1873 return proc_readfd_common(filp, dirent, filldir,
1874 proc_fdinfo_instantiate);
1875}
1876
1877static const struct file_operations proc_fdinfo_operations = {
1878 .read = generic_read_dir,
1879 .readdir = proc_readfdinfo,
1880};
1881
1882/*
1883 * proc directories can do almost nothing..
1884 */
1885static const struct inode_operations proc_fdinfo_inode_operations = {
1886 .lookup = proc_lookupfdinfo,
1887 .setattr = proc_setattr,
1888};
1889
1890
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001891static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001892 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001893{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001894 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001895 struct inode *inode;
1896 struct proc_inode *ei;
1897 struct dentry *error = ERR_PTR(-EINVAL);
1898
Eric W. Biederman61a28782006-10-02 02:18:49 -07001899 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001900 if (!inode)
1901 goto out;
1902
1903 ei = PROC_I(inode);
1904 inode->i_mode = p->mode;
1905 if (S_ISDIR(inode->i_mode))
1906 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1907 if (p->iop)
1908 inode->i_op = p->iop;
1909 if (p->fop)
1910 inode->i_fop = p->fop;
1911 ei->op = p->op;
1912 dentry->d_op = &pid_dentry_operations;
1913 d_add(dentry, inode);
1914 /* Close the race of the process dying before we return the dentry */
1915 if (pid_revalidate(dentry, NULL))
1916 error = NULL;
1917out:
1918 return error;
1919}
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921static struct dentry *proc_pident_lookup(struct inode *dir,
1922 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001923 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001924 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001927 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001928 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001929 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001931 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 inode = NULL;
1933
Eric W. Biederman99f89552006-06-26 00:25:55 -07001934 if (!task)
1935 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001937 /*
1938 * Yes, it does not scale. And it should not. Don't add
1939 * new entries into /proc/<tgid>/ without very good reasons.
1940 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001941 last = &ents[nents - 1];
1942 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (p->len != dentry->d_name.len)
1944 continue;
1945 if (!memcmp(dentry->d_name.name, p->name, p->len))
1946 break;
1947 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001948 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto out;
1950
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001951 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001953 put_task_struct(task);
1954out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001955 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Eric Dumazetc5141e62007-05-08 00:26:15 -07001958static int proc_pident_fill_cache(struct file *filp, void *dirent,
1959 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001960{
1961 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1962 proc_pident_instantiate, task, p);
1963}
1964
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001965static int proc_pident_readdir(struct file *filp,
1966 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001967 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001968{
1969 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001970 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001971 struct inode *inode = dentry->d_inode;
1972 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001973 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001974 ino_t ino;
1975 int ret;
1976
1977 ret = -ENOENT;
1978 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001979 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001980
1981 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001982 i = filp->f_pos;
1983 switch (i) {
1984 case 0:
1985 ino = inode->i_ino;
1986 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1987 goto out;
1988 i++;
1989 filp->f_pos++;
1990 /* fall through */
1991 case 1:
1992 ino = parent_ino(dentry);
1993 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1994 goto out;
1995 i++;
1996 filp->f_pos++;
1997 /* fall through */
1998 default:
1999 i -= 2;
2000 if (i >= nents) {
2001 ret = 1;
2002 goto out;
2003 }
2004 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002005 last = &ents[nents - 1];
2006 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002007 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002008 goto out;
2009 filp->f_pos++;
2010 p++;
2011 }
2012 }
2013
2014 ret = 1;
2015out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002016 put_task_struct(task);
2017out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002018 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019}
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002022static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2023 size_t count, loff_t *ppos)
2024{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002025 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002026 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002027 ssize_t length;
2028 struct task_struct *task = get_proc_task(inode);
2029
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002030 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002031 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002032
2033 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002034 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002035 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002036 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002037 if (length > 0)
2038 length = simple_read_from_buffer(buf, count, ppos, p, length);
2039 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002040 return length;
2041}
2042
2043static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2044 size_t count, loff_t *ppos)
2045{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002046 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002047 char *page;
2048 ssize_t length;
2049 struct task_struct *task = get_proc_task(inode);
2050
2051 length = -ESRCH;
2052 if (!task)
2053 goto out_no_task;
2054 if (count > PAGE_SIZE)
2055 count = PAGE_SIZE;
2056
2057 /* No partial writes. */
2058 length = -EINVAL;
2059 if (*ppos != 0)
2060 goto out;
2061
2062 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002063 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002064 if (!page)
2065 goto out;
2066
2067 length = -EFAULT;
2068 if (copy_from_user(page, buf, count))
2069 goto out_free;
2070
2071 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002072 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002073 (void*)page, count);
2074out_free:
2075 free_page((unsigned long) page);
2076out:
2077 put_task_struct(task);
2078out_no_task:
2079 return length;
2080}
2081
Arjan van de Ven00977a52007-02-12 00:55:34 -08002082static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002083 .read = proc_pid_attr_read,
2084 .write = proc_pid_attr_write,
2085};
2086
Eric Dumazetc5141e62007-05-08 00:26:15 -07002087static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002088 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2089 REG("prev", S_IRUGO, pid_attr),
2090 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2091 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2092 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2093 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002094};
2095
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002096static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 void * dirent, filldir_t filldir)
2098{
2099 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002100 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Arjan van de Ven00977a52007-02-12 00:55:34 -08002103static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002105 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106};
2107
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002108static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct dentry *dentry, struct nameidata *nd)
2110{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002111 return proc_pident_lookup(dir, dentry,
2112 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002115static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002116 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002117 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002118 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119};
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121#endif
2122
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002123#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2124static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2125 size_t count, loff_t *ppos)
2126{
2127 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2128 struct mm_struct *mm;
2129 char buffer[PROC_NUMBUF];
2130 size_t len;
2131 int ret;
2132
2133 if (!task)
2134 return -ESRCH;
2135
2136 ret = 0;
2137 mm = get_task_mm(task);
2138 if (mm) {
2139 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2140 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2141 MMF_DUMP_FILTER_SHIFT));
2142 mmput(mm);
2143 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2144 }
2145
2146 put_task_struct(task);
2147
2148 return ret;
2149}
2150
2151static ssize_t proc_coredump_filter_write(struct file *file,
2152 const char __user *buf,
2153 size_t count,
2154 loff_t *ppos)
2155{
2156 struct task_struct *task;
2157 struct mm_struct *mm;
2158 char buffer[PROC_NUMBUF], *end;
2159 unsigned int val;
2160 int ret;
2161 int i;
2162 unsigned long mask;
2163
2164 ret = -EFAULT;
2165 memset(buffer, 0, sizeof(buffer));
2166 if (count > sizeof(buffer) - 1)
2167 count = sizeof(buffer) - 1;
2168 if (copy_from_user(buffer, buf, count))
2169 goto out_no_task;
2170
2171 ret = -EINVAL;
2172 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2173 if (*end == '\n')
2174 end++;
2175 if (end - buffer == 0)
2176 goto out_no_task;
2177
2178 ret = -ESRCH;
2179 task = get_proc_task(file->f_dentry->d_inode);
2180 if (!task)
2181 goto out_no_task;
2182
2183 ret = end - buffer;
2184 mm = get_task_mm(task);
2185 if (!mm)
2186 goto out_no_mm;
2187
2188 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2189 if (val & mask)
2190 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2191 else
2192 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2193 }
2194
2195 mmput(mm);
2196 out_no_mm:
2197 put_task_struct(task);
2198 out_no_task:
2199 return ret;
2200}
2201
2202static const struct file_operations proc_coredump_filter_operations = {
2203 .read = proc_coredump_filter_read,
2204 .write = proc_coredump_filter_write,
2205};
2206#endif
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208/*
2209 * /proc/self:
2210 */
2211static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2212 int buflen)
2213{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002214 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002215 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002216 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002217 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002218 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002219 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return vfs_readlink(dentry,buffer,buflen,tmp);
2221}
2222
Al Viro008b1502005-08-20 00:17:39 +01002223static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002225 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002226 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002227 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002228 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002229 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002230 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002231 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002232}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002234static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 .readlink = proc_self_readlink,
2236 .follow_link = proc_self_follow_link,
2237};
2238
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002239/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002240 * proc base
2241 *
2242 * These are the directory entries in the root directory of /proc
2243 * that properly belong to the /proc filesystem, as they describe
2244 * describe something that is process related.
2245 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002246static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002247 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002248 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002249};
2250
2251/*
2252 * Exceptional case: normally we are not allowed to unhash a busy
2253 * directory. In this case, however, we can do it - no aliasing problems
2254 * due to the way we treat inodes.
2255 */
2256static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2257{
2258 struct inode *inode = dentry->d_inode;
2259 struct task_struct *task = get_proc_task(inode);
2260 if (task) {
2261 put_task_struct(task);
2262 return 1;
2263 }
2264 d_drop(dentry);
2265 return 0;
2266}
2267
2268static struct dentry_operations proc_base_dentry_operations =
2269{
2270 .d_revalidate = proc_base_revalidate,
2271 .d_delete = pid_delete_dentry,
2272};
2273
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002274static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002275 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002276{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002277 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002278 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002279 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002280 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002281
2282 /* Allocate the inode */
2283 error = ERR_PTR(-ENOMEM);
2284 inode = new_inode(dir->i_sb);
2285 if (!inode)
2286 goto out;
2287
2288 /* Initialize the inode */
2289 ei = PROC_I(inode);
2290 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002291
2292 /*
2293 * grab the reference to the task.
2294 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002295 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002296 if (!ei->pid)
2297 goto out_iput;
2298
2299 inode->i_uid = 0;
2300 inode->i_gid = 0;
2301 inode->i_mode = p->mode;
2302 if (S_ISDIR(inode->i_mode))
2303 inode->i_nlink = 2;
2304 if (S_ISLNK(inode->i_mode))
2305 inode->i_size = 64;
2306 if (p->iop)
2307 inode->i_op = p->iop;
2308 if (p->fop)
2309 inode->i_fop = p->fop;
2310 ei->op = p->op;
2311 dentry->d_op = &proc_base_dentry_operations;
2312 d_add(dentry, inode);
2313 error = NULL;
2314out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002315 return error;
2316out_iput:
2317 iput(inode);
2318 goto out;
2319}
2320
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002321static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2322{
2323 struct dentry *error;
2324 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002325 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002326
2327 error = ERR_PTR(-ENOENT);
2328
2329 if (!task)
2330 goto out_no_task;
2331
2332 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002333 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2334 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002335 if (p->len != dentry->d_name.len)
2336 continue;
2337 if (!memcmp(dentry->d_name.name, p->name, p->len))
2338 break;
2339 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002340 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002341 goto out;
2342
2343 error = proc_base_instantiate(dir, dentry, task, p);
2344
2345out:
2346 put_task_struct(task);
2347out_no_task:
2348 return error;
2349}
2350
Eric Dumazetc5141e62007-05-08 00:26:15 -07002351static int proc_base_fill_cache(struct file *filp, void *dirent,
2352 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002353{
2354 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2355 proc_base_instantiate, task, p);
2356}
2357
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002358#ifdef CONFIG_TASK_IO_ACCOUNTING
2359static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2360{
2361 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002362#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002363 "rchar: %llu\n"
2364 "wchar: %llu\n"
2365 "syscr: %llu\n"
2366 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002367#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002368 "read_bytes: %llu\n"
2369 "write_bytes: %llu\n"
2370 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002371#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002372 (unsigned long long)task->rchar,
2373 (unsigned long long)task->wchar,
2374 (unsigned long long)task->syscr,
2375 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002376#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002377 (unsigned long long)task->ioac.read_bytes,
2378 (unsigned long long)task->ioac.write_bytes,
2379 (unsigned long long)task->ioac.cancelled_write_bytes);
2380}
2381#endif
2382
Eric W. Biederman801199c2006-10-02 02:18:48 -07002383/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002384 * Thread groups
2385 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002386static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002387static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002388
Eric Dumazetc5141e62007-05-08 00:26:15 -07002389static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002390 DIR("task", S_IRUGO|S_IXUGO, task),
2391 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002392 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002393#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002394 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002395#endif
James Pearson315e28c2007-10-16 23:30:17 -07002396 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002397 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002398 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002399 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002400#ifdef CONFIG_SCHED_DEBUG
2401 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2402#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002403 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002404 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002405 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002406 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002407#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002408 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002409#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002410 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002411 LNK("cwd", cwd),
2412 LNK("root", root),
2413 LNK("exe", exe),
2414 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002415 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002416 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002417#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002418 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002419 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002420 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002421#endif
2422#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002423 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002424#endif
2425#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002426 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002427#endif
2428#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002429 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002430#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002431#ifdef CONFIG_LATENCYTOP
2432 REG("latency", S_IRUGO, lstats),
2433#endif
Paul Menage8793d852007-10-18 23:39:39 -07002434#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002435 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002436#endif
Paul Menagea4243162007-10-18 23:39:35 -07002437#ifdef CONFIG_CGROUPS
2438 REG("cgroup", S_IRUGO, cgroup),
2439#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002440 INF("oom_score", S_IRUGO, oom_score),
2441 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002442#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002443 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002444 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002445#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002446#ifdef CONFIG_FAULT_INJECTION
2447 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2448#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002449#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2450 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2451#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002452#ifdef CONFIG_TASK_IO_ACCOUNTING
2453 INF("io", S_IRUGO, pid_io_accounting),
2454#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002455};
2456
2457static int proc_tgid_base_readdir(struct file * filp,
2458 void * dirent, filldir_t filldir)
2459{
2460 return proc_pident_readdir(filp,dirent,filldir,
2461 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2462}
2463
Arjan van de Ven00977a52007-02-12 00:55:34 -08002464static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002465 .read = generic_read_dir,
2466 .readdir = proc_tgid_base_readdir,
2467};
2468
2469static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002470 return proc_pident_lookup(dir, dentry,
2471 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002472}
2473
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002474static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002475 .lookup = proc_tgid_base_lookup,
2476 .getattr = pid_getattr,
2477 .setattr = proc_setattr,
2478};
2479
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002480static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002482 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002483 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002484 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Eric W. Biederman48e64842006-06-26 00:25:48 -07002486 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002487 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2488 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002489 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002490 if (!(current->flags & PF_EXITING))
2491 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002492 d_drop(dentry);
2493 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002496 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002497 goto out;
2498
2499 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002500 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2501 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002502 if (!leader)
2503 goto out;
2504
2505 name.name = "task";
2506 name.len = strlen(name.name);
2507 dir = d_hash_and_lookup(leader, &name);
2508 if (!dir)
2509 goto out_put_leader;
2510
2511 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002512 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002513 dentry = d_hash_and_lookup(dir, &name);
2514 if (dentry) {
2515 shrink_dcache_parent(dentry);
2516 d_drop(dentry);
2517 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002519
2520 dput(dir);
2521out_put_leader:
2522 dput(leader);
2523out:
2524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525}
2526
Randy Dunlap0895e912007-10-21 21:00:10 -07002527/**
2528 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2529 * @task: task that should be flushed.
2530 *
2531 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002532 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002533 * in. This call is supposed to do all of this job.
2534 *
2535 * Looks in the dcache for
2536 * /proc/@pid
2537 * /proc/@tgid/task/@pid
2538 * if either directory is present flushes it and all of it'ts children
2539 * from the dcache.
2540 *
2541 * It is safe and reasonable to cache /proc entries for a task until
2542 * that task exits. After that they just clog up the dcache with
2543 * useless entries, possibly causing useful dcache entries to be
2544 * flushed instead. This routine is proved to flush those useless
2545 * dcache entries at process exit time.
2546 *
2547 * NOTE: This routine is just an optimization so it does not guarantee
2548 * that no dcache entries will exist at process exit time it
2549 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002550 */
2551
2552void proc_flush_task(struct task_struct *task)
2553{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002554 int i;
2555 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002556 struct upid *upid;
2557
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002558 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002559 if (thread_group_leader(task))
2560 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002561
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002562 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002563 upid = &pid->numbers[i];
2564 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002565 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002566 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002567
2568 upid = &pid->numbers[pid->level];
2569 if (upid->nr == 1)
2570 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002571}
2572
Adrian Bunk9711ef92006-12-06 20:38:31 -08002573static struct dentry *proc_pid_instantiate(struct inode *dir,
2574 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002575 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002576{
2577 struct dentry *error = ERR_PTR(-ENOENT);
2578 struct inode *inode;
2579
Eric W. Biederman61a28782006-10-02 02:18:49 -07002580 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002581 if (!inode)
2582 goto out;
2583
2584 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2585 inode->i_op = &proc_tgid_base_inode_operations;
2586 inode->i_fop = &proc_tgid_base_operations;
2587 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002588 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002589#ifdef CONFIG_SECURITY
2590 inode->i_nlink += 1;
2591#endif
2592
2593 dentry->d_op = &pid_dentry_operations;
2594
2595 d_add(dentry, inode);
2596 /* Close the race of the process dying before we return the dentry */
2597 if (pid_revalidate(dentry, NULL))
2598 error = NULL;
2599out:
2600 return error;
2601}
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2604{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002605 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002608 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Eric W. Biederman801199c2006-10-02 02:18:48 -07002610 result = proc_base_lookup(dir, dentry);
2611 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2612 goto out;
2613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 tgid = name_to_int(dentry);
2615 if (tgid == ~0U)
2616 goto out;
2617
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002618 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002619 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002620 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (task)
2622 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002623 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (!task)
2625 goto out;
2626
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002627 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002628 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002630 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002634 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002635 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002637struct tgid_iter {
2638 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002639 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002640};
2641static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2642{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002643 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002645 if (iter.task)
2646 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002647 rcu_read_lock();
2648retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002649 iter.task = NULL;
2650 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002651 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002652 iter.tgid = pid_nr_ns(pid, ns);
2653 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002654 /* What we to know is if the pid we have find is the
2655 * pid of a thread_group_leader. Testing for task
2656 * being a thread_group_leader is the obvious thing
2657 * todo but there is a window when it fails, due to
2658 * the pid transfer logic in de_thread.
2659 *
2660 * So we perform the straight forward test of seeing
2661 * if the pid we have found is the pid of a thread
2662 * group leader, and don't worry if the task we have
2663 * found doesn't happen to be a thread group leader.
2664 * As we don't care in the case of readdir.
2665 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002666 if (!iter.task || !has_group_leader_pid(iter.task)) {
2667 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002668 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002669 }
2670 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002672 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002673 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002676#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Eric W. Biederman61a28782006-10-02 02:18:49 -07002678static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002679 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002680{
2681 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002682 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002683 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002684 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002685}
2686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687/* for the /proc/ directory itself, after non-process stuff has been done */
2688int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002691 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002692 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002693 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Eric W. Biederman61a28782006-10-02 02:18:49 -07002695 if (!reaper)
2696 goto out_no_task;
2697
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002698 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002699 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002700 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002701 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
2703
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002704 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002705 iter.task = NULL;
2706 iter.tgid = filp->f_pos - TGID_OFFSET;
2707 for (iter = next_tgid(ns, iter);
2708 iter.task;
2709 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2710 filp->f_pos = iter.tgid + TGID_OFFSET;
2711 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2712 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002713 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002716 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2717out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002718 put_task_struct(reaper);
2719out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return 0;
2721}
2722
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002723/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002724 * Tasks
2725 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002726static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002727 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002728 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002729 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002730 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002731 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002732 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002733#ifdef CONFIG_SCHED_DEBUG
2734 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2735#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002736 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002737 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002738 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002739 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002740#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002741 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002742#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002743 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002744 LNK("cwd", cwd),
2745 LNK("root", root),
2746 LNK("exe", exe),
2747 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002748 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002749#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002750 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002751 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002752 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002753#endif
2754#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002755 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002756#endif
2757#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002758 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002759#endif
2760#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002761 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002762#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002763#ifdef CONFIG_LATENCYTOP
2764 REG("latency", S_IRUGO, lstats),
2765#endif
Paul Menage8793d852007-10-18 23:39:39 -07002766#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002767 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002768#endif
Paul Menagea4243162007-10-18 23:39:35 -07002769#ifdef CONFIG_CGROUPS
2770 REG("cgroup", S_IRUGO, cgroup),
2771#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002772 INF("oom_score", S_IRUGO, oom_score),
2773 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002774#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002775 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002776 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002777#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002778#ifdef CONFIG_FAULT_INJECTION
2779 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2780#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002781};
2782
2783static int proc_tid_base_readdir(struct file * filp,
2784 void * dirent, filldir_t filldir)
2785{
2786 return proc_pident_readdir(filp,dirent,filldir,
2787 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2788}
2789
2790static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002791 return proc_pident_lookup(dir, dentry,
2792 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002793}
2794
Arjan van de Ven00977a52007-02-12 00:55:34 -08002795static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002796 .read = generic_read_dir,
2797 .readdir = proc_tid_base_readdir,
2798};
2799
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002800static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002801 .lookup = proc_tid_base_lookup,
2802 .getattr = pid_getattr,
2803 .setattr = proc_setattr,
2804};
2805
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002806static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002807 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002808{
2809 struct dentry *error = ERR_PTR(-ENOENT);
2810 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002811 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002812
2813 if (!inode)
2814 goto out;
2815 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2816 inode->i_op = &proc_tid_base_inode_operations;
2817 inode->i_fop = &proc_tid_base_operations;
2818 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002819 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002820#ifdef CONFIG_SECURITY
2821 inode->i_nlink += 1;
2822#endif
2823
2824 dentry->d_op = &pid_dentry_operations;
2825
2826 d_add(dentry, inode);
2827 /* Close the race of the process dying before we return the dentry */
2828 if (pid_revalidate(dentry, NULL))
2829 error = NULL;
2830out:
2831 return error;
2832}
2833
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002834static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2835{
2836 struct dentry *result = ERR_PTR(-ENOENT);
2837 struct task_struct *task;
2838 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002839 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002840 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002841
2842 if (!leader)
2843 goto out_no_task;
2844
2845 tid = name_to_int(dentry);
2846 if (tid == ~0U)
2847 goto out;
2848
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002849 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002850 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002851 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002852 if (task)
2853 get_task_struct(task);
2854 rcu_read_unlock();
2855 if (!task)
2856 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002857 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002858 goto out_drop_task;
2859
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002860 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002861out_drop_task:
2862 put_task_struct(task);
2863out:
2864 put_task_struct(leader);
2865out_no_task:
2866 return result;
2867}
2868
2869/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002870 * Find the first tid of a thread group to return to user space.
2871 *
2872 * Usually this is just the thread group leader, but if the users
2873 * buffer was too small or there was a seek into the middle of the
2874 * directory we have more work todo.
2875 *
2876 * In the case of a short read we start with find_task_by_pid.
2877 *
2878 * In the case of a seek we start with the leader and walk nr
2879 * threads past it.
2880 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002881static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002882 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002883{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002884 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002885
Eric W. Biedermancc288732006-06-26 00:26:01 -07002886 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002887 /* Attempt to start with the pid of a thread */
2888 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002889 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002890 if (pos && (pos->group_leader == leader))
2891 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002892 }
2893
2894 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002895 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002896 if (nr && nr >= get_nr_threads(leader))
2897 goto out;
2898
2899 /* If we haven't found our starting place yet start
2900 * with the leader and walk nr threads forward.
2901 */
2902 for (pos = leader; nr > 0; --nr) {
2903 pos = next_thread(pos);
2904 if (pos == leader) {
2905 pos = NULL;
2906 goto out;
2907 }
2908 }
2909found:
2910 get_task_struct(pos);
2911out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002912 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002913 return pos;
2914}
2915
2916/*
2917 * Find the next thread in the thread list.
2918 * Return NULL if there is an error or no next thread.
2919 *
2920 * The reference to the input task_struct is released.
2921 */
2922static struct task_struct *next_tid(struct task_struct *start)
2923{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002924 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002925 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002926 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002927 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002928 if (thread_group_leader(pos))
2929 pos = NULL;
2930 else
2931 get_task_struct(pos);
2932 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002933 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002934 put_task_struct(start);
2935 return pos;
2936}
2937
Eric W. Biederman61a28782006-10-02 02:18:49 -07002938static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2939 struct task_struct *task, int tid)
2940{
2941 char name[PROC_NUMBUF];
2942 int len = snprintf(name, sizeof(name), "%d", tid);
2943 return proc_fill_cache(filp, dirent, filldir, name, len,
2944 proc_task_instantiate, task, NULL);
2945}
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947/* for the /proc/TGID/task/ directories */
2948static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2949{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002950 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002952 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002953 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 int retval = -ENOENT;
2955 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002956 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002958 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002960 task = get_proc_task(inode);
2961 if (!task)
2962 goto out_no_task;
2963 rcu_read_lock();
2964 if (pid_alive(task)) {
2965 leader = task->group_leader;
2966 get_task_struct(leader);
2967 }
2968 rcu_read_unlock();
2969 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002970 if (!leader)
2971 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 retval = 0;
2973
2974 switch (pos) {
2975 case 0:
2976 ino = inode->i_ino;
2977 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2978 goto out;
2979 pos++;
2980 /* fall through */
2981 case 1:
2982 ino = parent_ino(dentry);
2983 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2984 goto out;
2985 pos++;
2986 /* fall through */
2987 }
2988
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002989 /* f_version caches the tgid value that the last readdir call couldn't
2990 * return. lseek aka telldir automagically resets f_version to 0.
2991 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002992 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002993 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002994 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002995 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002996 task;
2997 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002998 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002999 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003000 /* returning this tgid failed, save it as the first
3001 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003002 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003003 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
3007out:
3008 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003009 put_task_struct(leader);
3010out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return retval;
3012}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003013
3014static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3015{
3016 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003017 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003018 generic_fillattr(inode, stat);
3019
Eric W. Biederman99f89552006-06-26 00:25:55 -07003020 if (p) {
3021 rcu_read_lock();
3022 stat->nlink += get_nr_threads(p);
3023 rcu_read_unlock();
3024 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003025 }
3026
3027 return 0;
3028}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003029
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003030static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003031 .lookup = proc_task_lookup,
3032 .getattr = proc_task_getattr,
3033 .setattr = proc_setattr,
3034};
3035
Arjan van de Ven00977a52007-02-12 00:55:34 -08003036static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003037 .read = generic_read_dir,
3038 .readdir = proc_task_readdir,
3039};