blob: a28840b11b89b5ca0b544f44ce58182544f51eef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070066#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070068#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070069#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/mount.h>
71#include <linux/security.h>
72#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070073#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070074#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/cpuset.h>
76#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050077#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070078#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070079#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070080#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070081#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include "internal.h"
83
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070084/* NOTE:
85 * Implementing inode permission operations in /proc is almost
86 * certainly an error. Permission checks need to happen during
87 * each system call not at open time. The reason is that most of
88 * what we wish to check for permissions in /proc varies at runtime.
89 *
90 * The classic example of a problem is opening file descriptors
91 * in /proc for a task before it execs a suid executable.
92 */
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070096 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080098 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080099 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700100 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Eric W. Biederman61a28782006-10-02 02:18:49 -0700103#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700105 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700106 .mode = MODE, \
107 .iop = IOP, \
108 .fop = FOP, \
109 .op = OP, \
110}
111
Eric W. Biederman61a28782006-10-02 02:18:49 -0700112#define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700123#define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800127#define ONE(NAME, MODE, OTYPE) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
130 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Vegard Nossumaed54172008-06-05 22:46:53 -0700132/*
133 * Count the number of hardlinks for the pid_entry table, excluding the .
134 * and .. links.
135 */
136static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
137 unsigned int n)
138{
139 unsigned int i;
140 unsigned int count;
141
142 count = 0;
143 for (i = 0; i < n; ++i) {
144 if (S_ISDIR(entries[i].mode))
145 ++count;
146 }
147
148 return count;
149}
150
Kees Cook5096add2007-05-08 00:26:04 -0700151int maps_protect;
152EXPORT_SYMBOL(maps_protect);
153
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157 task_lock(task);
158 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if(fs)
160 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_unlock(task);
162 return fs;
163}
164
Eric W. Biederman99f89552006-06-26 00:25:55 -0700165static int get_nr_threads(struct task_struct *tsk)
166{
167 /* Must be called with the rcu_read_lock held */
168 unsigned long flags;
169 int count = 0;
170
171 if (lock_task_sighand(tsk, &flags)) {
172 count = atomic_read(&tsk->signal->count);
173 unlock_task_sighand(tsk, &flags);
174 }
175 return count;
176}
177
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800178static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700179{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700180 struct task_struct *task = get_proc_task(inode);
181 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -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->pwd;
191 path_get(&fs->pwd);
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
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800199static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700201 struct task_struct *task = get_proc_task(inode);
202 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700204
205 if (task) {
206 fs = get_fs_struct(task);
207 put_task_struct(task);
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (fs) {
210 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800211 *path = fs->root;
212 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 read_unlock(&fs->lock);
214 result = 0;
215 put_fs_struct(fs);
216 }
217 return result;
218}
219
Roland McGrath638fa202008-04-29 01:01:38 -0700220/*
221 * Return zero if current may access user memory in @task, -error if not.
222 */
223static int check_mem_permission(struct task_struct *task)
224{
225 /*
226 * A task can always look at itself, in case it chooses
227 * to use system calls instead of load instructions.
228 */
229 if (task == current)
230 return 0;
231
232 /*
233 * If current is actively ptrace'ing, and would also be
234 * permitted to freshly attach with ptrace now, permit it.
235 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700236 if (task_is_stopped_or_traced(task)) {
237 int match;
238 rcu_read_lock();
239 match = (tracehook_tracer_task(task) == current);
240 rcu_read_unlock();
241 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
242 return 0;
243 }
Roland McGrath638fa202008-04-29 01:01:38 -0700244
245 /*
246 * Noone else is allowed.
247 */
248 return -EPERM;
249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Al Viro831830b2008-01-02 14:09:57 +0000251struct mm_struct *mm_for_maps(struct task_struct *task)
252{
253 struct mm_struct *mm = get_task_mm(task);
254 if (!mm)
255 return NULL;
256 down_read(&mm->mmap_sem);
257 task_lock(task);
258 if (task->mm != mm)
259 goto out;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400260 if (task->mm != current->mm &&
261 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
Al Viro831830b2008-01-02 14:09:57 +0000262 goto out;
263 task_unlock(task);
264 return mm;
265out:
266 task_unlock(task);
267 up_read(&mm->mmap_sem);
268 mmput(mm);
269 return NULL;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static int proc_pid_cmdline(struct task_struct *task, char * buffer)
273{
274 int res = 0;
275 unsigned int len;
276 struct mm_struct *mm = get_task_mm(task);
277 if (!mm)
278 goto out;
279 if (!mm->arg_end)
280 goto out_mm; /* Shh! No looking before we're done */
281
282 len = mm->arg_end - mm->arg_start;
283
284 if (len > PAGE_SIZE)
285 len = PAGE_SIZE;
286
287 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
288
289 // If the nul at the end of args has been overwritten, then
290 // assume application is using setproctitle(3).
291 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
292 len = strnlen(buffer, res);
293 if (len < res) {
294 res = len;
295 } else {
296 len = mm->env_end - mm->env_start;
297 if (len > PAGE_SIZE - res)
298 len = PAGE_SIZE - res;
299 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
300 res = strnlen(buffer, res);
301 }
302 }
303out_mm:
304 mmput(mm);
305out:
306 return res;
307}
308
309static int proc_pid_auxv(struct task_struct *task, char *buffer)
310{
311 int res = 0;
312 struct mm_struct *mm = get_task_mm(task);
313 if (mm) {
314 unsigned int nwords = 0;
315 do
316 nwords += 2;
317 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
318 res = nwords * sizeof(mm->saved_auxv[0]);
319 if (res > PAGE_SIZE)
320 res = PAGE_SIZE;
321 memcpy(buffer, mm->saved_auxv, res);
322 mmput(mm);
323 }
324 return res;
325}
326
327
328#ifdef CONFIG_KALLSYMS
329/*
330 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
331 * Returns the resolved symbol. If that fails, simply return the address.
332 */
333static int proc_pid_wchan(struct task_struct *task, char *buffer)
334{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700335 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700336 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 wchan = get_wchan(task);
339
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700340 if (lookup_symbol_name(wchan, symname) < 0)
341 return sprintf(buffer, "%lu", wchan);
342 else
343 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345#endif /* CONFIG_KALLSYMS */
346
347#ifdef CONFIG_SCHEDSTATS
348/*
349 * Provides /proc/PID/schedstat
350 */
351static int proc_pid_schedstat(struct task_struct *task, char *buffer)
352{
Balbir Singh172ba842007-07-09 18:52:00 +0200353 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 task->sched_info.cpu_time,
355 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200356 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358#endif
359
Arjan van de Ven97455122008-01-25 21:08:34 +0100360#ifdef CONFIG_LATENCYTOP
361static int lstats_show_proc(struct seq_file *m, void *v)
362{
363 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800364 struct inode *inode = m->private;
365 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100366
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800367 if (!task)
368 return -ESRCH;
369 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100370 for (i = 0; i < 32; i++) {
371 if (task->latency_record[i].backtrace[0]) {
372 int q;
373 seq_printf(m, "%i %li %li ",
374 task->latency_record[i].count,
375 task->latency_record[i].time,
376 task->latency_record[i].max);
377 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
378 char sym[KSYM_NAME_LEN];
379 char *c;
380 if (!task->latency_record[i].backtrace[q])
381 break;
382 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
383 break;
384 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
385 c = strchr(sym, '+');
386 if (c)
387 *c = 0;
388 seq_printf(m, "%s ", sym);
389 }
390 seq_printf(m, "\n");
391 }
392
393 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800394 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100395 return 0;
396}
397
398static int lstats_open(struct inode *inode, struct file *file)
399{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800400 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800401}
402
Arjan van de Ven97455122008-01-25 21:08:34 +0100403static ssize_t lstats_write(struct file *file, const char __user *buf,
404 size_t count, loff_t *offs)
405{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800406 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100407
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800408 if (!task)
409 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100410 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800411 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100412
413 return count;
414}
415
416static const struct file_operations proc_lstats_operations = {
417 .open = lstats_open,
418 .read = seq_read,
419 .write = lstats_write,
420 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800421 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100422};
423
424#endif
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/* The badness from the OOM killer */
427unsigned long badness(struct task_struct *p, unsigned long uptime);
428static int proc_oom_score(struct task_struct *task, char *buffer)
429{
430 unsigned long points;
431 struct timespec uptime;
432
433 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700434 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700436 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return sprintf(buffer, "%lu\n", points);
438}
439
Neil Hormand85f50d2007-10-18 23:40:37 -0700440struct limit_names {
441 char *name;
442 char *unit;
443};
444
445static const struct limit_names lnames[RLIM_NLIMITS] = {
446 [RLIMIT_CPU] = {"Max cpu time", "ms"},
447 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
448 [RLIMIT_DATA] = {"Max data size", "bytes"},
449 [RLIMIT_STACK] = {"Max stack size", "bytes"},
450 [RLIMIT_CORE] = {"Max core file size", "bytes"},
451 [RLIMIT_RSS] = {"Max resident set", "bytes"},
452 [RLIMIT_NPROC] = {"Max processes", "processes"},
453 [RLIMIT_NOFILE] = {"Max open files", "files"},
454 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
455 [RLIMIT_AS] = {"Max address space", "bytes"},
456 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
457 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
458 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
459 [RLIMIT_NICE] = {"Max nice priority", NULL},
460 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800461 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700462};
463
464/* Display limits for a process */
465static int proc_pid_limits(struct task_struct *task, char *buffer)
466{
467 unsigned int i;
468 int count = 0;
469 unsigned long flags;
470 char *bufptr = buffer;
471
472 struct rlimit rlim[RLIM_NLIMITS];
473
474 rcu_read_lock();
475 if (!lock_task_sighand(task,&flags)) {
476 rcu_read_unlock();
477 return 0;
478 }
479 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480 unlock_task_sighand(task, &flags);
481 rcu_read_unlock();
482
483 /*
484 * print the file header
485 */
486 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
487 "Limit", "Soft Limit", "Hard Limit", "Units");
488
489 for (i = 0; i < RLIM_NLIMITS; i++) {
490 if (rlim[i].rlim_cur == RLIM_INFINITY)
491 count += sprintf(&bufptr[count], "%-25s %-20s ",
492 lnames[i].name, "unlimited");
493 else
494 count += sprintf(&bufptr[count], "%-25s %-20lu ",
495 lnames[i].name, rlim[i].rlim_cur);
496
497 if (rlim[i].rlim_max == RLIM_INFINITY)
498 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
499 else
500 count += sprintf(&bufptr[count], "%-20lu ",
501 rlim[i].rlim_max);
502
503 if (lnames[i].unit)
504 count += sprintf(&bufptr[count], "%-10s\n",
505 lnames[i].unit);
506 else
507 count += sprintf(&bufptr[count], "\n");
508 }
509
510 return count;
511}
512
Roland McGrathebcb6732008-07-25 19:46:00 -0700513#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
514static int proc_pid_syscall(struct task_struct *task, char *buffer)
515{
516 long nr;
517 unsigned long args[6], sp, pc;
518
519 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
520 return sprintf(buffer, "running\n");
521
522 if (nr < 0)
523 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
524
525 return sprintf(buffer,
526 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
527 nr,
528 args[0], args[1], args[2], args[3], args[4], args[5],
529 sp, pc);
530}
531#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/************************************************************************/
534/* Here the fs part begins */
535/************************************************************************/
536
537/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700538static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700540 struct task_struct *task;
541 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700542 /* Allow access to a task's file descriptors if it is us or we
543 * may use ptrace attach to the process and find out that
544 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700545 */
546 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700547 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400548 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700549 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700550 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700551 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700554static int proc_setattr(struct dentry *dentry, struct iattr *attr)
555{
556 int error;
557 struct inode *inode = dentry->d_inode;
558
559 if (attr->ia_valid & ATTR_MODE)
560 return -EPERM;
561
562 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700563 if (!error)
564 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700565 return error;
566}
567
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800568static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700569 .setattr = proc_setattr,
570};
571
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100572static int mounts_open_common(struct inode *inode, struct file *file,
573 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700575 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700576 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800577 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100578 struct fs_struct *fs = NULL;
579 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500580 struct proc_mounts *p;
581 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Eric W. Biederman99f89552006-06-26 00:25:55 -0700583 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700584 rcu_read_lock();
585 nsp = task_nsproxy(task);
586 if (nsp) {
587 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800588 if (ns)
589 get_mnt_ns(ns);
590 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700591 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100592 if (ns)
593 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700594 put_task_struct(task);
595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100597 if (!ns)
598 goto err;
599 if (!fs)
600 goto err_put_ns;
601
602 read_lock(&fs->lock);
603 root = fs->root;
604 path_get(&root);
605 read_unlock(&fs->lock);
606 put_fs_struct(fs);
607
608 ret = -ENOMEM;
609 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
610 if (!p)
611 goto err_put_path;
612
613 file->private_data = &p->m;
614 ret = seq_open(file, op);
615 if (ret)
616 goto err_free;
617
618 p->m.private = p;
619 p->ns = ns;
620 p->root = root;
621 p->event = ns->event;
622
623 return 0;
624
625 err_free:
626 kfree(p);
627 err_put_path:
628 path_put(&root);
629 err_put_ns:
630 put_mnt_ns(ns);
631 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return ret;
633}
634
635static int mounts_release(struct inode *inode, struct file *file)
636{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100637 struct proc_mounts *p = file->private_data;
638 path_put(&p->root);
639 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return seq_release(inode, file);
641}
642
Al Viro5addc5d2005-11-07 17:15:49 -0500643static unsigned mounts_poll(struct file *file, poll_table *wait)
644{
645 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100646 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500647 unsigned res = 0;
648
649 poll_wait(file, &ns->poll, wait);
650
651 spin_lock(&vfsmount_lock);
652 if (p->event != ns->event) {
653 p->event = ns->event;
654 res = POLLERR;
655 }
656 spin_unlock(&vfsmount_lock);
657
658 return res;
659}
660
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100661static int mounts_open(struct inode *inode, struct file *file)
662{
663 return mounts_open_common(inode, file, &mounts_op);
664}
665
Arjan van de Ven00977a52007-02-12 00:55:34 -0800666static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 .open = mounts_open,
668 .read = seq_read,
669 .llseek = seq_lseek,
670 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500671 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672};
673
Ram Pai2d4d4862008-03-27 13:06:25 +0100674static int mountinfo_open(struct inode *inode, struct file *file)
675{
676 return mounts_open_common(inode, file, &mountinfo_op);
677}
678
679static const struct file_operations proc_mountinfo_operations = {
680 .open = mountinfo_open,
681 .read = seq_read,
682 .llseek = seq_lseek,
683 .release = mounts_release,
684 .poll = mounts_poll,
685};
686
Chuck Leverb4629fe2006-03-20 13:44:12 -0500687static int mountstats_open(struct inode *inode, struct file *file)
688{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100689 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500690}
691
Arjan van de Ven00977a52007-02-12 00:55:34 -0800692static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500693 .open = mountstats_open,
694 .read = seq_read,
695 .llseek = seq_lseek,
696 .release = mounts_release,
697};
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
700
701static ssize_t proc_info_read(struct file * file, char __user * buf,
702 size_t count, loff_t *ppos)
703{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800704 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 unsigned long page;
706 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700707 struct task_struct *task = get_proc_task(inode);
708
709 length = -ESRCH;
710 if (!task)
711 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 if (count > PROC_BLOCK_SIZE)
714 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700715
716 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700717 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 length = PROC_I(inode)->op.proc_read(task, (char*)page);
721
722 if (length >= 0)
723 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
724 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700725out:
726 put_task_struct(task);
727out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return length;
729}
730
Arjan van de Ven00977a52007-02-12 00:55:34 -0800731static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 .read = proc_info_read,
733};
734
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800735static int proc_single_show(struct seq_file *m, void *v)
736{
737 struct inode *inode = m->private;
738 struct pid_namespace *ns;
739 struct pid *pid;
740 struct task_struct *task;
741 int ret;
742
743 ns = inode->i_sb->s_fs_info;
744 pid = proc_pid(inode);
745 task = get_pid_task(pid, PIDTYPE_PID);
746 if (!task)
747 return -ESRCH;
748
749 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
750
751 put_task_struct(task);
752 return ret;
753}
754
755static int proc_single_open(struct inode *inode, struct file *filp)
756{
757 int ret;
758 ret = single_open(filp, proc_single_show, NULL);
759 if (!ret) {
760 struct seq_file *m = filp->private_data;
761
762 m->private = inode;
763 }
764 return ret;
765}
766
767static const struct file_operations proc_single_file_operations = {
768 .open = proc_single_open,
769 .read = seq_read,
770 .llseek = seq_lseek,
771 .release = single_release,
772};
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static int mem_open(struct inode* inode, struct file* file)
775{
776 file->private_data = (void*)((long)current->self_exec_id);
777 return 0;
778}
779
780static ssize_t mem_read(struct file * file, char __user * buf,
781 size_t count, loff_t *ppos)
782{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800783 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 char *page;
785 unsigned long src = *ppos;
786 int ret = -ESRCH;
787 struct mm_struct *mm;
788
Eric W. Biederman99f89552006-06-26 00:25:55 -0700789 if (!task)
790 goto out_no_task;
791
Roland McGrath638fa202008-04-29 01:01:38 -0700792 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto out;
794
795 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700796 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (!page)
798 goto out;
799
800 ret = 0;
801
802 mm = get_task_mm(task);
803 if (!mm)
804 goto out_free;
805
806 ret = -EIO;
807
808 if (file->private_data != (void*)((long)current->self_exec_id))
809 goto out_put;
810
811 ret = 0;
812
813 while (count > 0) {
814 int this_len, retval;
815
816 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
817 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa202008-04-29 01:01:38 -0700818 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (!ret)
820 ret = -EIO;
821 break;
822 }
823
824 if (copy_to_user(buf, page, retval)) {
825 ret = -EFAULT;
826 break;
827 }
828
829 ret += retval;
830 src += retval;
831 buf += retval;
832 count -= retval;
833 }
834 *ppos = src;
835
836out_put:
837 mmput(mm);
838out_free:
839 free_page((unsigned long) page);
840out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700841 put_task_struct(task);
842out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return ret;
844}
845
846#define mem_write NULL
847
848#ifndef mem_write
849/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800850static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 size_t count, loff_t *ppos)
852{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700853 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800855 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 unsigned long dst = *ppos;
857
Eric W. Biederman99f89552006-06-26 00:25:55 -0700858 copied = -ESRCH;
859 if (!task)
860 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Roland McGrath638fa202008-04-29 01:01:38 -0700862 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700863 goto out;
864
865 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700866 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700870 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 while (count > 0) {
872 int this_len, retval;
873
874 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
875 if (copy_from_user(page, buf, this_len)) {
876 copied = -EFAULT;
877 break;
878 }
879 retval = access_process_vm(task, dst, page, this_len, 1);
880 if (!retval) {
881 if (!copied)
882 copied = -EIO;
883 break;
884 }
885 copied += retval;
886 buf += retval;
887 dst += retval;
888 count -= retval;
889 }
890 *ppos = dst;
891 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700892out:
893 put_task_struct(task);
894out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return copied;
896}
897#endif
898
Matt Mackall85863e42008-02-04 22:29:04 -0800899loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
901 switch (orig) {
902 case 0:
903 file->f_pos = offset;
904 break;
905 case 1:
906 file->f_pos += offset;
907 break;
908 default:
909 return -EINVAL;
910 }
911 force_successful_syscall_return();
912 return file->f_pos;
913}
914
Arjan van de Ven00977a52007-02-12 00:55:34 -0800915static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 .llseek = mem_lseek,
917 .read = mem_read,
918 .write = mem_write,
919 .open = mem_open,
920};
921
James Pearson315e28c2007-10-16 23:30:17 -0700922static ssize_t environ_read(struct file *file, char __user *buf,
923 size_t count, loff_t *ppos)
924{
925 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
926 char *page;
927 unsigned long src = *ppos;
928 int ret = -ESRCH;
929 struct mm_struct *mm;
930
931 if (!task)
932 goto out_no_task;
933
Stephen Smalley006ebb42008-05-19 08:32:49 -0400934 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700935 goto out;
936
937 ret = -ENOMEM;
938 page = (char *)__get_free_page(GFP_TEMPORARY);
939 if (!page)
940 goto out;
941
942 ret = 0;
943
944 mm = get_task_mm(task);
945 if (!mm)
946 goto out_free;
947
948 while (count > 0) {
949 int this_len, retval, max_len;
950
951 this_len = mm->env_end - (mm->env_start + src);
952
953 if (this_len <= 0)
954 break;
955
956 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
957 this_len = (this_len > max_len) ? max_len : this_len;
958
959 retval = access_process_vm(task, (mm->env_start + src),
960 page, this_len, 0);
961
962 if (retval <= 0) {
963 ret = retval;
964 break;
965 }
966
967 if (copy_to_user(buf, page, retval)) {
968 ret = -EFAULT;
969 break;
970 }
971
972 ret += retval;
973 src += retval;
974 buf += retval;
975 count -= retval;
976 }
977 *ppos = src;
978
979 mmput(mm);
980out_free:
981 free_page((unsigned long) page);
982out:
983 put_task_struct(task);
984out_no_task:
985 return ret;
986}
987
988static const struct file_operations proc_environ_operations = {
989 .read = environ_read,
990};
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992static ssize_t oom_adjust_read(struct file *file, char __user *buf,
993 size_t count, loff_t *ppos)
994{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800995 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700996 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700998 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Eric W. Biederman99f89552006-06-26 00:25:55 -07001000 if (!task)
1001 return -ESRCH;
1002 oom_adjust = task->oomkilladj;
1003 put_task_struct(task);
1004
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001005 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001006
1007 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
1010static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1011 size_t count, loff_t *ppos)
1012{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001013 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001014 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int oom_adjust;
1016
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001017 memset(buffer, 0, sizeof(buffer));
1018 if (count > sizeof(buffer) - 1)
1019 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (copy_from_user(buffer, buf, count))
1021 return -EFAULT;
1022 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001023 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1024 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return -EINVAL;
1026 if (*end == '\n')
1027 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001028 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001029 if (!task)
1030 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001031 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1032 put_task_struct(task);
1033 return -EACCES;
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001036 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (end - buffer == 0)
1038 return -EIO;
1039 return end - buffer;
1040}
1041
Arjan van de Ven00977a52007-02-12 00:55:34 -08001042static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .read = oom_adjust_read,
1044 .write = oom_adjust_write,
1045};
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047#ifdef CONFIG_AUDITSYSCALL
1048#define TMPBUFLEN 21
1049static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1050 size_t count, loff_t *ppos)
1051{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001052 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001053 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 ssize_t length;
1055 char tmpbuf[TMPBUFLEN];
1056
Eric W. Biederman99f89552006-06-26 00:25:55 -07001057 if (!task)
1058 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001060 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001061 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1063}
1064
1065static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1066 size_t count, loff_t *ppos)
1067{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001068 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 char *page, *tmp;
1070 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 uid_t loginuid;
1072
1073 if (!capable(CAP_AUDIT_CONTROL))
1074 return -EPERM;
1075
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001076 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EPERM;
1078
Al Viroe0182902006-05-18 08:28:02 -04001079 if (count >= PAGE_SIZE)
1080 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 if (*ppos != 0) {
1083 /* No partial writes. */
1084 return -EINVAL;
1085 }
Mel Gormane12ba742007-10-16 01:25:52 -07001086 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!page)
1088 return -ENOMEM;
1089 length = -EFAULT;
1090 if (copy_from_user(page, buf, count))
1091 goto out_free_page;
1092
Al Viroe0182902006-05-18 08:28:02 -04001093 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 loginuid = simple_strtoul(page, &tmp, 10);
1095 if (tmp == page) {
1096 length = -EINVAL;
1097 goto out_free_page;
1098
1099 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001100 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (likely(length == 0))
1102 length = count;
1103
1104out_free_page:
1105 free_page((unsigned long) page);
1106 return length;
1107}
1108
Arjan van de Ven00977a52007-02-12 00:55:34 -08001109static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 .read = proc_loginuid_read,
1111 .write = proc_loginuid_write,
1112};
Eric Paris1e0bd752008-03-13 08:15:31 -04001113
1114static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1115 size_t count, loff_t *ppos)
1116{
1117 struct inode * inode = file->f_path.dentry->d_inode;
1118 struct task_struct *task = get_proc_task(inode);
1119 ssize_t length;
1120 char tmpbuf[TMPBUFLEN];
1121
1122 if (!task)
1123 return -ESRCH;
1124 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1125 audit_get_sessionid(task));
1126 put_task_struct(task);
1127 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1128}
1129
1130static const struct file_operations proc_sessionid_operations = {
1131 .read = proc_sessionid_read,
1132};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133#endif
1134
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001135#ifdef CONFIG_FAULT_INJECTION
1136static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1137 size_t count, loff_t *ppos)
1138{
1139 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1140 char buffer[PROC_NUMBUF];
1141 size_t len;
1142 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001143
1144 if (!task)
1145 return -ESRCH;
1146 make_it_fail = task->make_it_fail;
1147 put_task_struct(task);
1148
1149 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001150
1151 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001152}
1153
1154static ssize_t proc_fault_inject_write(struct file * file,
1155 const char __user * buf, size_t count, loff_t *ppos)
1156{
1157 struct task_struct *task;
1158 char buffer[PROC_NUMBUF], *end;
1159 int make_it_fail;
1160
1161 if (!capable(CAP_SYS_RESOURCE))
1162 return -EPERM;
1163 memset(buffer, 0, sizeof(buffer));
1164 if (count > sizeof(buffer) - 1)
1165 count = sizeof(buffer) - 1;
1166 if (copy_from_user(buffer, buf, count))
1167 return -EFAULT;
1168 make_it_fail = simple_strtol(buffer, &end, 0);
1169 if (*end == '\n')
1170 end++;
1171 task = get_proc_task(file->f_dentry->d_inode);
1172 if (!task)
1173 return -ESRCH;
1174 task->make_it_fail = make_it_fail;
1175 put_task_struct(task);
1176 if (end - buffer == 0)
1177 return -EIO;
1178 return end - buffer;
1179}
1180
Arjan van de Ven00977a52007-02-12 00:55:34 -08001181static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001182 .read = proc_fault_inject_read,
1183 .write = proc_fault_inject_write,
1184};
1185#endif
1186
Arjan van de Ven97455122008-01-25 21:08:34 +01001187
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001188#ifdef CONFIG_SCHED_DEBUG
1189/*
1190 * Print out various scheduling related per-task fields:
1191 */
1192static int sched_show(struct seq_file *m, void *v)
1193{
1194 struct inode *inode = m->private;
1195 struct task_struct *p;
1196
1197 WARN_ON(!inode);
1198
1199 p = get_proc_task(inode);
1200 if (!p)
1201 return -ESRCH;
1202 proc_sched_show_task(p, m);
1203
1204 put_task_struct(p);
1205
1206 return 0;
1207}
1208
1209static ssize_t
1210sched_write(struct file *file, const char __user *buf,
1211 size_t count, loff_t *offset)
1212{
1213 struct inode *inode = file->f_path.dentry->d_inode;
1214 struct task_struct *p;
1215
1216 WARN_ON(!inode);
1217
1218 p = get_proc_task(inode);
1219 if (!p)
1220 return -ESRCH;
1221 proc_sched_set_task(p);
1222
1223 put_task_struct(p);
1224
1225 return count;
1226}
1227
1228static int sched_open(struct inode *inode, struct file *filp)
1229{
1230 int ret;
1231
1232 ret = single_open(filp, sched_show, NULL);
1233 if (!ret) {
1234 struct seq_file *m = filp->private_data;
1235
1236 m->private = inode;
1237 }
1238 return ret;
1239}
1240
1241static const struct file_operations proc_pid_sched_operations = {
1242 .open = sched_open,
1243 .read = seq_read,
1244 .write = sched_write,
1245 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001246 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001247};
1248
1249#endif
1250
Matt Helsley925d1c42008-04-29 01:01:36 -07001251/*
1252 * We added or removed a vma mapping the executable. The vmas are only mapped
1253 * during exec and are not mapped with the mmap system call.
1254 * Callers must hold down_write() on the mm's mmap_sem for these
1255 */
1256void added_exe_file_vma(struct mm_struct *mm)
1257{
1258 mm->num_exe_file_vmas++;
1259}
1260
1261void removed_exe_file_vma(struct mm_struct *mm)
1262{
1263 mm->num_exe_file_vmas--;
1264 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1265 fput(mm->exe_file);
1266 mm->exe_file = NULL;
1267 }
1268
1269}
1270
1271void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1272{
1273 if (new_exe_file)
1274 get_file(new_exe_file);
1275 if (mm->exe_file)
1276 fput(mm->exe_file);
1277 mm->exe_file = new_exe_file;
1278 mm->num_exe_file_vmas = 0;
1279}
1280
1281struct file *get_mm_exe_file(struct mm_struct *mm)
1282{
1283 struct file *exe_file;
1284
1285 /* We need mmap_sem to protect against races with removal of
1286 * VM_EXECUTABLE vmas */
1287 down_read(&mm->mmap_sem);
1288 exe_file = mm->exe_file;
1289 if (exe_file)
1290 get_file(exe_file);
1291 up_read(&mm->mmap_sem);
1292 return exe_file;
1293}
1294
1295void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1296{
1297 /* It's safe to write the exe_file pointer without exe_file_lock because
1298 * this is called during fork when the task is not yet in /proc */
1299 newmm->exe_file = get_mm_exe_file(oldmm);
1300}
1301
1302static int proc_exe_link(struct inode *inode, struct path *exe_path)
1303{
1304 struct task_struct *task;
1305 struct mm_struct *mm;
1306 struct file *exe_file;
1307
1308 task = get_proc_task(inode);
1309 if (!task)
1310 return -ENOENT;
1311 mm = get_task_mm(task);
1312 put_task_struct(task);
1313 if (!mm)
1314 return -ENOENT;
1315 exe_file = get_mm_exe_file(mm);
1316 mmput(mm);
1317 if (exe_file) {
1318 *exe_path = exe_file->f_path;
1319 path_get(&exe_file->f_path);
1320 fput(exe_file);
1321 return 0;
1322 } else
1323 return -ENOENT;
1324}
1325
Al Viro008b1502005-08-20 00:17:39 +01001326static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
1328 struct inode *inode = dentry->d_inode;
1329 int error = -EACCES;
1330
1331 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001332 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Eric W. Biederman778c1142006-06-26 00:25:58 -07001334 /* Are we allowed to snoop on the tasks file descriptors? */
1335 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001338 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 nd->last_type = LAST_BIND;
1340out:
Al Viro008b1502005-08-20 00:17:39 +01001341 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001344static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Mel Gormane12ba742007-10-16 01:25:52 -07001346 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001347 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int len;
1349
1350 if (!tmp)
1351 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001352
Jan Blunckcf28b482008-02-14 19:38:44 -08001353 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001354 len = PTR_ERR(pathname);
1355 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001357 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 if (len > buflen)
1360 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001361 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 len = -EFAULT;
1363 out:
1364 free_page((unsigned long)tmp);
1365 return len;
1366}
1367
1368static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1369{
1370 int error = -EACCES;
1371 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001372 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Eric W. Biederman778c1142006-06-26 00:25:58 -07001374 /* Are we allowed to snoop on the tasks file descriptors? */
1375 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001378 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (error)
1380 goto out;
1381
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001382 error = do_proc_readlink(&path, buffer, buflen);
1383 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return error;
1386}
1387
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001388static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001390 .follow_link = proc_pid_follow_link,
1391 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392};
1393
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001394
1395/* building an inode */
1396
1397static int task_dumpable(struct task_struct *task)
1398{
1399 int dumpable = 0;
1400 struct mm_struct *mm;
1401
1402 task_lock(task);
1403 mm = task->mm;
1404 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001405 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001406 task_unlock(task);
1407 if(dumpable == 1)
1408 return 1;
1409 return 0;
1410}
1411
1412
Eric W. Biederman61a28782006-10-02 02:18:49 -07001413static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001414{
1415 struct inode * inode;
1416 struct proc_inode *ei;
1417
1418 /* We need a new inode */
1419
1420 inode = new_inode(sb);
1421 if (!inode)
1422 goto out;
1423
1424 /* Common stuff */
1425 ei = PROC_I(inode);
1426 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001427 inode->i_op = &proc_def_inode_operations;
1428
1429 /*
1430 * grab the reference to task.
1431 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001432 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001433 if (!ei->pid)
1434 goto out_unlock;
1435
1436 inode->i_uid = 0;
1437 inode->i_gid = 0;
1438 if (task_dumpable(task)) {
1439 inode->i_uid = task->euid;
1440 inode->i_gid = task->egid;
1441 }
1442 security_task_to_inode(task, inode);
1443
1444out:
1445 return inode;
1446
1447out_unlock:
1448 iput(inode);
1449 return NULL;
1450}
1451
1452static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1453{
1454 struct inode *inode = dentry->d_inode;
1455 struct task_struct *task;
1456 generic_fillattr(inode, stat);
1457
1458 rcu_read_lock();
1459 stat->uid = 0;
1460 stat->gid = 0;
1461 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1462 if (task) {
1463 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1464 task_dumpable(task)) {
1465 stat->uid = task->euid;
1466 stat->gid = task->egid;
1467 }
1468 }
1469 rcu_read_unlock();
1470 return 0;
1471}
1472
1473/* dentry stuff */
1474
1475/*
1476 * Exceptional case: normally we are not allowed to unhash a busy
1477 * directory. In this case, however, we can do it - no aliasing problems
1478 * due to the way we treat inodes.
1479 *
1480 * Rewrite the inode's ownerships here because the owning task may have
1481 * performed a setuid(), etc.
1482 *
1483 * Before the /proc/pid/status file was created the only way to read
1484 * the effective uid of a /process was to stat /proc/pid. Reading
1485 * /proc/pid/status is slow enough that procps and other packages
1486 * kept stating /proc/pid. To keep the rules in /proc simple I have
1487 * made this apply to all per process world readable and executable
1488 * directories.
1489 */
1490static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1491{
1492 struct inode *inode = dentry->d_inode;
1493 struct task_struct *task = get_proc_task(inode);
1494 if (task) {
1495 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1496 task_dumpable(task)) {
1497 inode->i_uid = task->euid;
1498 inode->i_gid = task->egid;
1499 } else {
1500 inode->i_uid = 0;
1501 inode->i_gid = 0;
1502 }
1503 inode->i_mode &= ~(S_ISUID | S_ISGID);
1504 security_task_to_inode(task, inode);
1505 put_task_struct(task);
1506 return 1;
1507 }
1508 d_drop(dentry);
1509 return 0;
1510}
1511
1512static int pid_delete_dentry(struct dentry * dentry)
1513{
1514 /* Is the task we represent dead?
1515 * If so, then don't put the dentry on the lru list,
1516 * kill it immediately.
1517 */
1518 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1519}
1520
1521static struct dentry_operations pid_dentry_operations =
1522{
1523 .d_revalidate = pid_revalidate,
1524 .d_delete = pid_delete_dentry,
1525};
1526
1527/* Lookups */
1528
Eric Dumazetc5141e62007-05-08 00:26:15 -07001529typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1530 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001531
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001532/*
1533 * Fill a directory entry.
1534 *
1535 * If possible create the dcache entry and derive our inode number and
1536 * file type from dcache entry.
1537 *
1538 * Since all of the proc inode numbers are dynamically generated, the inode
1539 * numbers do not exist until the inode is cache. This means creating the
1540 * the dcache entry in readdir is necessary to keep the inode numbers
1541 * reported by readdir in sync with the inode numbers reported
1542 * by stat.
1543 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001544static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1545 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001546 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001547{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001548 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001549 struct inode *inode;
1550 struct qstr qname;
1551 ino_t ino = 0;
1552 unsigned type = DT_UNKNOWN;
1553
1554 qname.name = name;
1555 qname.len = len;
1556 qname.hash = full_name_hash(name, len);
1557
1558 child = d_lookup(dir, &qname);
1559 if (!child) {
1560 struct dentry *new;
1561 new = d_alloc(dir, &qname);
1562 if (new) {
1563 child = instantiate(dir->d_inode, new, task, ptr);
1564 if (child)
1565 dput(new);
1566 else
1567 child = new;
1568 }
1569 }
1570 if (!child || IS_ERR(child) || !child->d_inode)
1571 goto end_instantiate;
1572 inode = child->d_inode;
1573 if (inode) {
1574 ino = inode->i_ino;
1575 type = inode->i_mode >> 12;
1576 }
1577 dput(child);
1578end_instantiate:
1579 if (!ino)
1580 ino = find_inode_number(dir, &qname);
1581 if (!ino)
1582 ino = 1;
1583 return filldir(dirent, name, len, filp->f_pos, ino, type);
1584}
1585
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001586static unsigned name_to_int(struct dentry *dentry)
1587{
1588 const char *name = dentry->d_name.name;
1589 int len = dentry->d_name.len;
1590 unsigned n = 0;
1591
1592 if (len > 1 && *name == '0')
1593 goto out;
1594 while (len-- > 0) {
1595 unsigned c = *name++ - '0';
1596 if (c > 9)
1597 goto out;
1598 if (n >= (~0U-9)/10)
1599 goto out;
1600 n *= 10;
1601 n += c;
1602 }
1603 return n;
1604out:
1605 return ~0U;
1606}
1607
Miklos Szeredi27932742007-05-08 00:26:17 -07001608#define PROC_FDINFO_MAX 64
1609
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001610static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001611{
1612 struct task_struct *task = get_proc_task(inode);
1613 struct files_struct *files = NULL;
1614 struct file *file;
1615 int fd = proc_fd(inode);
1616
1617 if (task) {
1618 files = get_files_struct(task);
1619 put_task_struct(task);
1620 }
1621 if (files) {
1622 /*
1623 * We are not taking a ref to the file structure, so we must
1624 * hold ->file_lock.
1625 */
1626 spin_lock(&files->file_lock);
1627 file = fcheck_files(files, fd);
1628 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001629 if (path) {
1630 *path = file->f_path;
1631 path_get(&file->f_path);
1632 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001633 if (info)
1634 snprintf(info, PROC_FDINFO_MAX,
1635 "pos:\t%lli\n"
1636 "flags:\t0%o\n",
1637 (long long) file->f_pos,
1638 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001639 spin_unlock(&files->file_lock);
1640 put_files_struct(files);
1641 return 0;
1642 }
1643 spin_unlock(&files->file_lock);
1644 put_files_struct(files);
1645 }
1646 return -ENOENT;
1647}
1648
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001649static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001650{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001651 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001652}
1653
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001654static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1655{
1656 struct inode *inode = dentry->d_inode;
1657 struct task_struct *task = get_proc_task(inode);
1658 int fd = proc_fd(inode);
1659 struct files_struct *files;
1660
1661 if (task) {
1662 files = get_files_struct(task);
1663 if (files) {
1664 rcu_read_lock();
1665 if (fcheck_files(files, fd)) {
1666 rcu_read_unlock();
1667 put_files_struct(files);
1668 if (task_dumpable(task)) {
1669 inode->i_uid = task->euid;
1670 inode->i_gid = task->egid;
1671 } else {
1672 inode->i_uid = 0;
1673 inode->i_gid = 0;
1674 }
1675 inode->i_mode &= ~(S_ISUID | S_ISGID);
1676 security_task_to_inode(task, inode);
1677 put_task_struct(task);
1678 return 1;
1679 }
1680 rcu_read_unlock();
1681 put_files_struct(files);
1682 }
1683 put_task_struct(task);
1684 }
1685 d_drop(dentry);
1686 return 0;
1687}
1688
1689static struct dentry_operations tid_fd_dentry_operations =
1690{
1691 .d_revalidate = tid_fd_revalidate,
1692 .d_delete = pid_delete_dentry,
1693};
1694
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001695static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001696 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001697{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001698 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001699 struct file *file;
1700 struct files_struct *files;
1701 struct inode *inode;
1702 struct proc_inode *ei;
1703 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001704
Eric W. Biederman61a28782006-10-02 02:18:49 -07001705 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001706 if (!inode)
1707 goto out;
1708 ei = PROC_I(inode);
1709 ei->fd = fd;
1710 files = get_files_struct(task);
1711 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001712 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001713 inode->i_mode = S_IFLNK;
1714
1715 /*
1716 * We are not taking a ref to the file structure, so we must
1717 * hold ->file_lock.
1718 */
1719 spin_lock(&files->file_lock);
1720 file = fcheck_files(files, fd);
1721 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001722 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001723 if (file->f_mode & 1)
1724 inode->i_mode |= S_IRUSR | S_IXUSR;
1725 if (file->f_mode & 2)
1726 inode->i_mode |= S_IWUSR | S_IXUSR;
1727 spin_unlock(&files->file_lock);
1728 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001729
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001730 inode->i_op = &proc_pid_link_inode_operations;
1731 inode->i_size = 64;
1732 ei->op.proc_get_link = proc_fd_link;
1733 dentry->d_op = &tid_fd_dentry_operations;
1734 d_add(dentry, inode);
1735 /* Close the race of the process dying before we return the dentry */
1736 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001737 error = NULL;
1738
1739 out:
1740 return error;
1741out_unlock:
1742 spin_unlock(&files->file_lock);
1743 put_files_struct(files);
1744out_iput:
1745 iput(inode);
1746 goto out;
1747}
1748
Miklos Szeredi27932742007-05-08 00:26:17 -07001749static struct dentry *proc_lookupfd_common(struct inode *dir,
1750 struct dentry *dentry,
1751 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001752{
1753 struct task_struct *task = get_proc_task(dir);
1754 unsigned fd = name_to_int(dentry);
1755 struct dentry *result = ERR_PTR(-ENOENT);
1756
1757 if (!task)
1758 goto out_no_task;
1759 if (fd == ~0U)
1760 goto out;
1761
Miklos Szeredi27932742007-05-08 00:26:17 -07001762 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001763out:
1764 put_task_struct(task);
1765out_no_task:
1766 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001767}
1768
Miklos Szeredi27932742007-05-08 00:26:17 -07001769static int proc_readfd_common(struct file * filp, void * dirent,
1770 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001772 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001773 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001774 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001775 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct files_struct * files;
1778
1779 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001780 if (!p)
1781 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 fd = filp->f_pos;
1785 switch (fd) {
1786 case 0:
1787 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1788 goto out;
1789 filp->f_pos++;
1790 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001791 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1793 goto out;
1794 filp->f_pos++;
1795 default:
1796 files = get_files_struct(p);
1797 if (!files)
1798 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001799 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001801 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001803 char name[PROC_NUMBUF];
1804 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 if (!fcheck_files(files, fd))
1807 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001808 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Miklos Szeredi27932742007-05-08 00:26:17 -07001810 len = snprintf(name, sizeof(name), "%d", fd);
1811 if (proc_fill_cache(filp, dirent, filldir,
1812 name, len, instantiate,
1813 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001814 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 break;
1816 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001817 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001819 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 put_files_struct(files);
1821 }
1822out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001823 put_task_struct(p);
1824out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return retval;
1826}
1827
Miklos Szeredi27932742007-05-08 00:26:17 -07001828static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1829 struct nameidata *nd)
1830{
1831 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1832}
1833
1834static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1835{
1836 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1837}
1838
1839static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1840 size_t len, loff_t *ppos)
1841{
1842 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001843 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001844 if (!err)
1845 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1846 return err;
1847}
1848
1849static const struct file_operations proc_fdinfo_file_operations = {
1850 .open = nonseekable_open,
1851 .read = proc_fdinfo_read,
1852};
1853
Arjan van de Ven00977a52007-02-12 00:55:34 -08001854static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .read = generic_read_dir,
1856 .readdir = proc_readfd,
1857};
1858
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001860 * /proc/pid/fd needs a special permission handler so that a process can still
1861 * access /proc/self/fd after it has executed a setuid().
1862 */
Al Viroe6305c42008-07-15 21:03:57 -04001863static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001864{
1865 int rv;
1866
1867 rv = generic_permission(inode, mask, NULL);
1868 if (rv == 0)
1869 return 0;
1870 if (task_pid(current) == proc_pid(inode))
1871 rv = 0;
1872 return rv;
1873}
1874
1875/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * proc directories can do almost nothing..
1877 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001878static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001880 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001881 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882};
1883
Miklos Szeredi27932742007-05-08 00:26:17 -07001884static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1885 struct dentry *dentry, struct task_struct *task, const void *ptr)
1886{
1887 unsigned fd = *(unsigned *)ptr;
1888 struct inode *inode;
1889 struct proc_inode *ei;
1890 struct dentry *error = ERR_PTR(-ENOENT);
1891
1892 inode = proc_pid_make_inode(dir->i_sb, task);
1893 if (!inode)
1894 goto out;
1895 ei = PROC_I(inode);
1896 ei->fd = fd;
1897 inode->i_mode = S_IFREG | S_IRUSR;
1898 inode->i_fop = &proc_fdinfo_file_operations;
1899 dentry->d_op = &tid_fd_dentry_operations;
1900 d_add(dentry, inode);
1901 /* Close the race of the process dying before we return the dentry */
1902 if (tid_fd_revalidate(dentry, NULL))
1903 error = NULL;
1904
1905 out:
1906 return error;
1907}
1908
1909static struct dentry *proc_lookupfdinfo(struct inode *dir,
1910 struct dentry *dentry,
1911 struct nameidata *nd)
1912{
1913 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1914}
1915
1916static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1917{
1918 return proc_readfd_common(filp, dirent, filldir,
1919 proc_fdinfo_instantiate);
1920}
1921
1922static const struct file_operations proc_fdinfo_operations = {
1923 .read = generic_read_dir,
1924 .readdir = proc_readfdinfo,
1925};
1926
1927/*
1928 * proc directories can do almost nothing..
1929 */
1930static const struct inode_operations proc_fdinfo_inode_operations = {
1931 .lookup = proc_lookupfdinfo,
1932 .setattr = proc_setattr,
1933};
1934
1935
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001936static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001937 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001938{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001939 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001940 struct inode *inode;
1941 struct proc_inode *ei;
1942 struct dentry *error = ERR_PTR(-EINVAL);
1943
Eric W. Biederman61a28782006-10-02 02:18:49 -07001944 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001945 if (!inode)
1946 goto out;
1947
1948 ei = PROC_I(inode);
1949 inode->i_mode = p->mode;
1950 if (S_ISDIR(inode->i_mode))
1951 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1952 if (p->iop)
1953 inode->i_op = p->iop;
1954 if (p->fop)
1955 inode->i_fop = p->fop;
1956 ei->op = p->op;
1957 dentry->d_op = &pid_dentry_operations;
1958 d_add(dentry, inode);
1959 /* Close the race of the process dying before we return the dentry */
1960 if (pid_revalidate(dentry, NULL))
1961 error = NULL;
1962out:
1963 return error;
1964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966static struct dentry *proc_pident_lookup(struct inode *dir,
1967 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001968 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001969 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001972 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001973 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001974 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001976 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 inode = NULL;
1978
Eric W. Biederman99f89552006-06-26 00:25:55 -07001979 if (!task)
1980 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001982 /*
1983 * Yes, it does not scale. And it should not. Don't add
1984 * new entries into /proc/<tgid>/ without very good reasons.
1985 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001986 last = &ents[nents - 1];
1987 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (p->len != dentry->d_name.len)
1989 continue;
1990 if (!memcmp(dentry->d_name.name, p->name, p->len))
1991 break;
1992 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001993 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto out;
1995
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001996 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001998 put_task_struct(task);
1999out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002000 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
Eric Dumazetc5141e62007-05-08 00:26:15 -07002003static int proc_pident_fill_cache(struct file *filp, void *dirent,
2004 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002005{
2006 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2007 proc_pident_instantiate, task, p);
2008}
2009
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002010static int proc_pident_readdir(struct file *filp,
2011 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002012 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002013{
2014 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002015 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002016 struct inode *inode = dentry->d_inode;
2017 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002018 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002019 ino_t ino;
2020 int ret;
2021
2022 ret = -ENOENT;
2023 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002024 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002025
2026 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002027 i = filp->f_pos;
2028 switch (i) {
2029 case 0:
2030 ino = inode->i_ino;
2031 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2032 goto out;
2033 i++;
2034 filp->f_pos++;
2035 /* fall through */
2036 case 1:
2037 ino = parent_ino(dentry);
2038 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2039 goto out;
2040 i++;
2041 filp->f_pos++;
2042 /* fall through */
2043 default:
2044 i -= 2;
2045 if (i >= nents) {
2046 ret = 1;
2047 goto out;
2048 }
2049 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002050 last = &ents[nents - 1];
2051 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002052 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002053 goto out;
2054 filp->f_pos++;
2055 p++;
2056 }
2057 }
2058
2059 ret = 1;
2060out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002061 put_task_struct(task);
2062out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002063 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002067static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2068 size_t count, loff_t *ppos)
2069{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002070 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002071 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002072 ssize_t length;
2073 struct task_struct *task = get_proc_task(inode);
2074
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002075 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002076 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002077
2078 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002079 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002080 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002081 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002082 if (length > 0)
2083 length = simple_read_from_buffer(buf, count, ppos, p, length);
2084 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002085 return length;
2086}
2087
2088static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2089 size_t count, loff_t *ppos)
2090{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002091 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002092 char *page;
2093 ssize_t length;
2094 struct task_struct *task = get_proc_task(inode);
2095
2096 length = -ESRCH;
2097 if (!task)
2098 goto out_no_task;
2099 if (count > PAGE_SIZE)
2100 count = PAGE_SIZE;
2101
2102 /* No partial writes. */
2103 length = -EINVAL;
2104 if (*ppos != 0)
2105 goto out;
2106
2107 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002108 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002109 if (!page)
2110 goto out;
2111
2112 length = -EFAULT;
2113 if (copy_from_user(page, buf, count))
2114 goto out_free;
2115
2116 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002117 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002118 (void*)page, count);
2119out_free:
2120 free_page((unsigned long) page);
2121out:
2122 put_task_struct(task);
2123out_no_task:
2124 return length;
2125}
2126
Arjan van de Ven00977a52007-02-12 00:55:34 -08002127static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002128 .read = proc_pid_attr_read,
2129 .write = proc_pid_attr_write,
2130};
2131
Eric Dumazetc5141e62007-05-08 00:26:15 -07002132static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002133 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2134 REG("prev", S_IRUGO, pid_attr),
2135 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2136 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2137 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2138 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002139};
2140
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002141static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 void * dirent, filldir_t filldir)
2143{
2144 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002145 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
Arjan van de Ven00977a52007-02-12 00:55:34 -08002148static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002150 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151};
2152
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002153static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 struct dentry *dentry, struct nameidata *nd)
2155{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002156 return proc_pident_lookup(dir, dentry,
2157 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002160static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002161 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002162 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002163 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164};
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166#endif
2167
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002168#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2169static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2170 size_t count, loff_t *ppos)
2171{
2172 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2173 struct mm_struct *mm;
2174 char buffer[PROC_NUMBUF];
2175 size_t len;
2176 int ret;
2177
2178 if (!task)
2179 return -ESRCH;
2180
2181 ret = 0;
2182 mm = get_task_mm(task);
2183 if (mm) {
2184 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2185 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2186 MMF_DUMP_FILTER_SHIFT));
2187 mmput(mm);
2188 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2189 }
2190
2191 put_task_struct(task);
2192
2193 return ret;
2194}
2195
2196static ssize_t proc_coredump_filter_write(struct file *file,
2197 const char __user *buf,
2198 size_t count,
2199 loff_t *ppos)
2200{
2201 struct task_struct *task;
2202 struct mm_struct *mm;
2203 char buffer[PROC_NUMBUF], *end;
2204 unsigned int val;
2205 int ret;
2206 int i;
2207 unsigned long mask;
2208
2209 ret = -EFAULT;
2210 memset(buffer, 0, sizeof(buffer));
2211 if (count > sizeof(buffer) - 1)
2212 count = sizeof(buffer) - 1;
2213 if (copy_from_user(buffer, buf, count))
2214 goto out_no_task;
2215
2216 ret = -EINVAL;
2217 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2218 if (*end == '\n')
2219 end++;
2220 if (end - buffer == 0)
2221 goto out_no_task;
2222
2223 ret = -ESRCH;
2224 task = get_proc_task(file->f_dentry->d_inode);
2225 if (!task)
2226 goto out_no_task;
2227
2228 ret = end - buffer;
2229 mm = get_task_mm(task);
2230 if (!mm)
2231 goto out_no_mm;
2232
2233 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2234 if (val & mask)
2235 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2236 else
2237 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2238 }
2239
2240 mmput(mm);
2241 out_no_mm:
2242 put_task_struct(task);
2243 out_no_task:
2244 return ret;
2245}
2246
2247static const struct file_operations proc_coredump_filter_operations = {
2248 .read = proc_coredump_filter_read,
2249 .write = proc_coredump_filter_write,
2250};
2251#endif
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253/*
2254 * /proc/self:
2255 */
2256static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2257 int buflen)
2258{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002259 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002260 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002261 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002262 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002263 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002264 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return vfs_readlink(dentry,buffer,buflen,tmp);
2266}
2267
Al Viro008b1502005-08-20 00:17:39 +01002268static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002270 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002271 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002272 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002273 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002274 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002275 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002276 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002277}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002279static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 .readlink = proc_self_readlink,
2281 .follow_link = proc_self_follow_link,
2282};
2283
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002284/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002285 * proc base
2286 *
2287 * These are the directory entries in the root directory of /proc
2288 * that properly belong to the /proc filesystem, as they describe
2289 * describe something that is process related.
2290 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002291static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002292 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002293 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002294};
2295
2296/*
2297 * Exceptional case: normally we are not allowed to unhash a busy
2298 * directory. In this case, however, we can do it - no aliasing problems
2299 * due to the way we treat inodes.
2300 */
2301static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2302{
2303 struct inode *inode = dentry->d_inode;
2304 struct task_struct *task = get_proc_task(inode);
2305 if (task) {
2306 put_task_struct(task);
2307 return 1;
2308 }
2309 d_drop(dentry);
2310 return 0;
2311}
2312
2313static struct dentry_operations proc_base_dentry_operations =
2314{
2315 .d_revalidate = proc_base_revalidate,
2316 .d_delete = pid_delete_dentry,
2317};
2318
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002319static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002320 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002321{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002322 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002323 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002324 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002325 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002326
2327 /* Allocate the inode */
2328 error = ERR_PTR(-ENOMEM);
2329 inode = new_inode(dir->i_sb);
2330 if (!inode)
2331 goto out;
2332
2333 /* Initialize the inode */
2334 ei = PROC_I(inode);
2335 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002336
2337 /*
2338 * grab the reference to the task.
2339 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002340 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002341 if (!ei->pid)
2342 goto out_iput;
2343
2344 inode->i_uid = 0;
2345 inode->i_gid = 0;
2346 inode->i_mode = p->mode;
2347 if (S_ISDIR(inode->i_mode))
2348 inode->i_nlink = 2;
2349 if (S_ISLNK(inode->i_mode))
2350 inode->i_size = 64;
2351 if (p->iop)
2352 inode->i_op = p->iop;
2353 if (p->fop)
2354 inode->i_fop = p->fop;
2355 ei->op = p->op;
2356 dentry->d_op = &proc_base_dentry_operations;
2357 d_add(dentry, inode);
2358 error = NULL;
2359out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002360 return error;
2361out_iput:
2362 iput(inode);
2363 goto out;
2364}
2365
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002366static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2367{
2368 struct dentry *error;
2369 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002370 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002371
2372 error = ERR_PTR(-ENOENT);
2373
2374 if (!task)
2375 goto out_no_task;
2376
2377 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002378 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2379 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002380 if (p->len != dentry->d_name.len)
2381 continue;
2382 if (!memcmp(dentry->d_name.name, p->name, p->len))
2383 break;
2384 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002385 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002386 goto out;
2387
2388 error = proc_base_instantiate(dir, dentry, task, p);
2389
2390out:
2391 put_task_struct(task);
2392out_no_task:
2393 return error;
2394}
2395
Eric Dumazetc5141e62007-05-08 00:26:15 -07002396static int proc_base_fill_cache(struct file *filp, void *dirent,
2397 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002398{
2399 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2400 proc_base_instantiate, task, p);
2401}
2402
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002403#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002404static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002405{
Andrea Righi940389b2008-07-28 00:48:12 +02002406 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002407 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002408
Andrea Righi59954772008-07-27 17:29:15 +02002409 if (whole && lock_task_sighand(task, &flags)) {
2410 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002411
Andrea Righi59954772008-07-27 17:29:15 +02002412 task_io_accounting_add(&acct, &task->signal->ioac);
2413 while_each_thread(task, t)
2414 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002415
Andrea Righi59954772008-07-27 17:29:15 +02002416 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002417 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002418 return sprintf(buffer,
2419 "rchar: %llu\n"
2420 "wchar: %llu\n"
2421 "syscr: %llu\n"
2422 "syscw: %llu\n"
2423 "read_bytes: %llu\n"
2424 "write_bytes: %llu\n"
2425 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002426 (unsigned long long)acct.rchar,
2427 (unsigned long long)acct.wchar,
2428 (unsigned long long)acct.syscr,
2429 (unsigned long long)acct.syscw,
2430 (unsigned long long)acct.read_bytes,
2431 (unsigned long long)acct.write_bytes,
2432 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002433}
Andrea Righi297c5d92008-07-25 01:48:49 -07002434
2435static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2436{
2437 return do_io_accounting(task, buffer, 0);
2438}
2439
2440static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2441{
2442 return do_io_accounting(task, buffer, 1);
2443}
2444#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002445
Eric W. Biederman801199c2006-10-02 02:18:48 -07002446/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002447 * Thread groups
2448 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002449static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002450static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002451
Eric Dumazetc5141e62007-05-08 00:26:15 -07002452static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002453 DIR("task", S_IRUGO|S_IXUGO, task),
2454 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002455 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002456#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002457 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002458#endif
James Pearson315e28c2007-10-16 23:30:17 -07002459 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002460 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002461 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002462 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002463#ifdef CONFIG_SCHED_DEBUG
2464 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2465#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002466#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2467 INF("syscall", S_IRUSR, pid_syscall),
2468#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002469 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002470 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002471 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002472 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002473#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002474 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002475#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002476 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002477 LNK("cwd", cwd),
2478 LNK("root", root),
2479 LNK("exe", exe),
2480 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002481 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002482 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002483#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002484 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002485 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002486 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002487#endif
2488#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002489 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490#endif
2491#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002492 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493#endif
2494#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002495 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002497#ifdef CONFIG_LATENCYTOP
2498 REG("latency", S_IRUGO, lstats),
2499#endif
Paul Menage8793d852007-10-18 23:39:39 -07002500#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002501 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502#endif
Paul Menagea4243162007-10-18 23:39:35 -07002503#ifdef CONFIG_CGROUPS
2504 REG("cgroup", S_IRUGO, cgroup),
2505#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002506 INF("oom_score", S_IRUGO, oom_score),
2507 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002508#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002509 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Steve Grubb6ee65042008-04-29 15:01:13 -04002510 REG("sessionid", S_IRUGO, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002512#ifdef CONFIG_FAULT_INJECTION
2513 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2514#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002515#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2516 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2517#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002518#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002519 INF("io", S_IRUGO, tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002520#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002521};
2522
2523static int proc_tgid_base_readdir(struct file * filp,
2524 void * dirent, filldir_t filldir)
2525{
2526 return proc_pident_readdir(filp,dirent,filldir,
2527 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2528}
2529
Arjan van de Ven00977a52007-02-12 00:55:34 -08002530static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531 .read = generic_read_dir,
2532 .readdir = proc_tgid_base_readdir,
2533};
2534
2535static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002536 return proc_pident_lookup(dir, dentry,
2537 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002538}
2539
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002540static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002541 .lookup = proc_tgid_base_lookup,
2542 .getattr = pid_getattr,
2543 .setattr = proc_setattr,
2544};
2545
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002546static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002548 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002549 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002550 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Eric W. Biederman48e64842006-06-26 00:25:48 -07002552 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002553 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2554 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002555 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002556 if (!(current->flags & PF_EXITING))
2557 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002558 d_drop(dentry);
2559 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002562 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002563 goto out;
2564
2565 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002566 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2567 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002568 if (!leader)
2569 goto out;
2570
2571 name.name = "task";
2572 name.len = strlen(name.name);
2573 dir = d_hash_and_lookup(leader, &name);
2574 if (!dir)
2575 goto out_put_leader;
2576
2577 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002578 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002579 dentry = d_hash_and_lookup(dir, &name);
2580 if (dentry) {
2581 shrink_dcache_parent(dentry);
2582 d_drop(dentry);
2583 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002585
2586 dput(dir);
2587out_put_leader:
2588 dput(leader);
2589out:
2590 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Randy Dunlap0895e912007-10-21 21:00:10 -07002593/**
2594 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2595 * @task: task that should be flushed.
2596 *
2597 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002598 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002599 * in. This call is supposed to do all of this job.
2600 *
2601 * Looks in the dcache for
2602 * /proc/@pid
2603 * /proc/@tgid/task/@pid
2604 * if either directory is present flushes it and all of it'ts children
2605 * from the dcache.
2606 *
2607 * It is safe and reasonable to cache /proc entries for a task until
2608 * that task exits. After that they just clog up the dcache with
2609 * useless entries, possibly causing useful dcache entries to be
2610 * flushed instead. This routine is proved to flush those useless
2611 * dcache entries at process exit time.
2612 *
2613 * NOTE: This routine is just an optimization so it does not guarantee
2614 * that no dcache entries will exist at process exit time it
2615 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002616 */
2617
2618void proc_flush_task(struct task_struct *task)
2619{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002620 int i;
2621 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002622 struct upid *upid;
2623
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002624 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002625 if (thread_group_leader(task))
2626 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002627
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002628 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002629 upid = &pid->numbers[i];
2630 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002631 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002632 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002633
2634 upid = &pid->numbers[pid->level];
2635 if (upid->nr == 1)
2636 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002637}
2638
Adrian Bunk9711ef92006-12-06 20:38:31 -08002639static struct dentry *proc_pid_instantiate(struct inode *dir,
2640 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002641 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002642{
2643 struct dentry *error = ERR_PTR(-ENOENT);
2644 struct inode *inode;
2645
Eric W. Biederman61a28782006-10-02 02:18:49 -07002646 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002647 if (!inode)
2648 goto out;
2649
2650 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2651 inode->i_op = &proc_tgid_base_inode_operations;
2652 inode->i_fop = &proc_tgid_base_operations;
2653 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002654
2655 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2656 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002657
2658 dentry->d_op = &pid_dentry_operations;
2659
2660 d_add(dentry, inode);
2661 /* Close the race of the process dying before we return the dentry */
2662 if (pid_revalidate(dentry, NULL))
2663 error = NULL;
2664out:
2665 return error;
2666}
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2669{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002670 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002673 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Eric W. Biederman801199c2006-10-02 02:18:48 -07002675 result = proc_base_lookup(dir, dentry);
2676 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2677 goto out;
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 tgid = name_to_int(dentry);
2680 if (tgid == ~0U)
2681 goto out;
2682
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002683 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002684 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002685 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (task)
2687 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002688 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (!task)
2690 goto out;
2691
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002692 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002693 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002695 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002699 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002700 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002702struct tgid_iter {
2703 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002704 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002705};
2706static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2707{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002708 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002710 if (iter.task)
2711 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002712 rcu_read_lock();
2713retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002714 iter.task = NULL;
2715 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002716 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002717 iter.tgid = pid_nr_ns(pid, ns);
2718 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002719 /* What we to know is if the pid we have find is the
2720 * pid of a thread_group_leader. Testing for task
2721 * being a thread_group_leader is the obvious thing
2722 * todo but there is a window when it fails, due to
2723 * the pid transfer logic in de_thread.
2724 *
2725 * So we perform the straight forward test of seeing
2726 * if the pid we have found is the pid of a thread
2727 * group leader, and don't worry if the task we have
2728 * found doesn't happen to be a thread group leader.
2729 * As we don't care in the case of readdir.
2730 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002731 if (!iter.task || !has_group_leader_pid(iter.task)) {
2732 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002733 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002734 }
2735 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002737 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002738 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002741#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Eric W. Biederman61a28782006-10-02 02:18:49 -07002743static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002744 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002745{
2746 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002747 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002748 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002749 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752/* for the /proc/ directory itself, after non-process stuff has been done */
2753int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2754{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002756 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002757 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002758 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Eric W. Biederman61a28782006-10-02 02:18:49 -07002760 if (!reaper)
2761 goto out_no_task;
2762
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002763 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002764 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002765 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002766 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002769 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002770 iter.task = NULL;
2771 iter.tgid = filp->f_pos - TGID_OFFSET;
2772 for (iter = next_tgid(ns, iter);
2773 iter.task;
2774 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2775 filp->f_pos = iter.tgid + TGID_OFFSET;
2776 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2777 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002781 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2782out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002783 put_task_struct(reaper);
2784out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return 0;
2786}
2787
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002788/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002789 * Tasks
2790 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002791static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002792 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002793 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002794 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002795 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002796 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002797 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002798#ifdef CONFIG_SCHED_DEBUG
2799 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2800#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002801#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2802 INF("syscall", S_IRUSR, pid_syscall),
2803#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002804 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002805 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002806 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002807 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002808#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002809 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002810#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002811 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002812 LNK("cwd", cwd),
2813 LNK("root", root),
2814 LNK("exe", exe),
2815 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002816 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002817#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002818 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002819 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002820 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002821#endif
2822#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002823 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002824#endif
2825#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002826 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002827#endif
2828#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002829 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002830#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002831#ifdef CONFIG_LATENCYTOP
2832 REG("latency", S_IRUGO, lstats),
2833#endif
Paul Menage8793d852007-10-18 23:39:39 -07002834#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002835 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002836#endif
Paul Menagea4243162007-10-18 23:39:35 -07002837#ifdef CONFIG_CGROUPS
2838 REG("cgroup", S_IRUGO, cgroup),
2839#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002840 INF("oom_score", S_IRUGO, oom_score),
2841 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002842#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002843 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002844 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002845#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002846#ifdef CONFIG_FAULT_INJECTION
2847 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2848#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002849#ifdef CONFIG_TASK_IO_ACCOUNTING
2850 INF("io", S_IRUGO, tid_io_accounting),
2851#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002852};
2853
2854static int proc_tid_base_readdir(struct file * filp,
2855 void * dirent, filldir_t filldir)
2856{
2857 return proc_pident_readdir(filp,dirent,filldir,
2858 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2859}
2860
2861static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002862 return proc_pident_lookup(dir, dentry,
2863 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002864}
2865
Arjan van de Ven00977a52007-02-12 00:55:34 -08002866static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002867 .read = generic_read_dir,
2868 .readdir = proc_tid_base_readdir,
2869};
2870
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002871static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002872 .lookup = proc_tid_base_lookup,
2873 .getattr = pid_getattr,
2874 .setattr = proc_setattr,
2875};
2876
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002877static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002878 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002879{
2880 struct dentry *error = ERR_PTR(-ENOENT);
2881 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002882 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002883
2884 if (!inode)
2885 goto out;
2886 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2887 inode->i_op = &proc_tid_base_inode_operations;
2888 inode->i_fop = &proc_tid_base_operations;
2889 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002890
2891 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2892 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002893
2894 dentry->d_op = &pid_dentry_operations;
2895
2896 d_add(dentry, inode);
2897 /* Close the race of the process dying before we return the dentry */
2898 if (pid_revalidate(dentry, NULL))
2899 error = NULL;
2900out:
2901 return error;
2902}
2903
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002904static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2905{
2906 struct dentry *result = ERR_PTR(-ENOENT);
2907 struct task_struct *task;
2908 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002909 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002910 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002911
2912 if (!leader)
2913 goto out_no_task;
2914
2915 tid = name_to_int(dentry);
2916 if (tid == ~0U)
2917 goto out;
2918
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002919 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002920 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002921 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002922 if (task)
2923 get_task_struct(task);
2924 rcu_read_unlock();
2925 if (!task)
2926 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002927 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002928 goto out_drop_task;
2929
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002930 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002931out_drop_task:
2932 put_task_struct(task);
2933out:
2934 put_task_struct(leader);
2935out_no_task:
2936 return result;
2937}
2938
2939/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002940 * Find the first tid of a thread group to return to user space.
2941 *
2942 * Usually this is just the thread group leader, but if the users
2943 * buffer was too small or there was a seek into the middle of the
2944 * directory we have more work todo.
2945 *
2946 * In the case of a short read we start with find_task_by_pid.
2947 *
2948 * In the case of a seek we start with the leader and walk nr
2949 * threads past it.
2950 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002951static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002952 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002953{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002954 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002955
Eric W. Biedermancc288732006-06-26 00:26:01 -07002956 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002957 /* Attempt to start with the pid of a thread */
2958 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002959 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002960 if (pos && (pos->group_leader == leader))
2961 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002962 }
2963
2964 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002965 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002966 if (nr && nr >= get_nr_threads(leader))
2967 goto out;
2968
2969 /* If we haven't found our starting place yet start
2970 * with the leader and walk nr threads forward.
2971 */
2972 for (pos = leader; nr > 0; --nr) {
2973 pos = next_thread(pos);
2974 if (pos == leader) {
2975 pos = NULL;
2976 goto out;
2977 }
2978 }
2979found:
2980 get_task_struct(pos);
2981out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002982 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002983 return pos;
2984}
2985
2986/*
2987 * Find the next thread in the thread list.
2988 * Return NULL if there is an error or no next thread.
2989 *
2990 * The reference to the input task_struct is released.
2991 */
2992static struct task_struct *next_tid(struct task_struct *start)
2993{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002994 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002995 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002996 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002997 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002998 if (thread_group_leader(pos))
2999 pos = NULL;
3000 else
3001 get_task_struct(pos);
3002 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003003 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003004 put_task_struct(start);
3005 return pos;
3006}
3007
Eric W. Biederman61a28782006-10-02 02:18:49 -07003008static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3009 struct task_struct *task, int tid)
3010{
3011 char name[PROC_NUMBUF];
3012 int len = snprintf(name, sizeof(name), "%d", tid);
3013 return proc_fill_cache(filp, dirent, filldir, name, len,
3014 proc_task_instantiate, task, NULL);
3015}
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017/* for the /proc/TGID/task/ directories */
3018static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3019{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003020 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003022 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003023 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 int retval = -ENOENT;
3025 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003026 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003028 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003030 task = get_proc_task(inode);
3031 if (!task)
3032 goto out_no_task;
3033 rcu_read_lock();
3034 if (pid_alive(task)) {
3035 leader = task->group_leader;
3036 get_task_struct(leader);
3037 }
3038 rcu_read_unlock();
3039 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003040 if (!leader)
3041 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 retval = 0;
3043
3044 switch (pos) {
3045 case 0:
3046 ino = inode->i_ino;
3047 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3048 goto out;
3049 pos++;
3050 /* fall through */
3051 case 1:
3052 ino = parent_ino(dentry);
3053 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3054 goto out;
3055 pos++;
3056 /* fall through */
3057 }
3058
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003059 /* f_version caches the tgid value that the last readdir call couldn't
3060 * return. lseek aka telldir automagically resets f_version to 0.
3061 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003062 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003063 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003064 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003065 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003066 task;
3067 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003068 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003069 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003070 /* returning this tgid failed, save it as the first
3071 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003072 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003073 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 }
3077out:
3078 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003079 put_task_struct(leader);
3080out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 return retval;
3082}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003083
3084static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3085{
3086 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003087 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003088 generic_fillattr(inode, stat);
3089
Eric W. Biederman99f89552006-06-26 00:25:55 -07003090 if (p) {
3091 rcu_read_lock();
3092 stat->nlink += get_nr_threads(p);
3093 rcu_read_unlock();
3094 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003095 }
3096
3097 return 0;
3098}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003099
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003100static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003101 .lookup = proc_task_lookup,
3102 .getattr = proc_task_getattr,
3103 .setattr = proc_setattr,
3104};
3105
Arjan van de Ven00977a52007-02-12 00:55:34 -08003106static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003107 .read = generic_read_dir,
3108 .readdir = proc_task_readdir,
3109};