blob: d744aa3c9f746ac527b4c1d6e46f8d625bc69f78 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040059#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/string.h>
61#include <linux/seq_file.h>
62#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080063#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070067#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070068#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/mount.h>
70#include <linux/security.h>
71#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070072#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070073#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/cpuset.h>
75#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050076#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070077#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070078#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070079#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070080#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include "internal.h"
82
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070083/* NOTE:
84 * Implementing inode permission operations in /proc is almost
85 * certainly an error. Permission checks need to happen during
86 * each system call not at open time. The reason is that most of
87 * what we wish to check for permissions in /proc varies at runtime.
88 *
89 * The classic example of a problem is opening file descriptors
90 * in /proc for a task before it execs a suid executable.
91 */
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070095 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080097 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080098 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070099 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Eric W. Biederman61a28782006-10-02 02:18:49 -0700102#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700104 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700105 .mode = MODE, \
106 .iop = IOP, \
107 .fop = FOP, \
108 .op = OP, \
109}
110
Eric W. Biederman61a28782006-10-02 02:18:49 -0700111#define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700115#define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700121 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700122#define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800126#define ONE(NAME, MODE, OTYPE) \
127 NOD(NAME, (S_IFREG|(MODE)), \
128 NULL, &proc_single_file_operations, \
129 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Vegard Nossumaed54172008-06-05 22:46:53 -0700131/*
132 * Count the number of hardlinks for the pid_entry table, excluding the .
133 * and .. links.
134 */
135static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
136 unsigned int n)
137{
138 unsigned int i;
139 unsigned int count;
140
141 count = 0;
142 for (i = 0; i < n; ++i) {
143 if (S_ISDIR(entries[i].mode))
144 ++count;
145 }
146
147 return count;
148}
149
Kees Cook5096add2007-05-08 00:26:04 -0700150int maps_protect;
151EXPORT_SYMBOL(maps_protect);
152
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700153static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156 task_lock(task);
157 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if(fs)
159 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700160 task_unlock(task);
161 return fs;
162}
163
Eric W. Biederman99f89552006-06-26 00:25:55 -0700164static int get_nr_threads(struct task_struct *tsk)
165{
166 /* Must be called with the rcu_read_lock held */
167 unsigned long flags;
168 int count = 0;
169
170 if (lock_task_sighand(tsk, &flags)) {
171 count = atomic_read(&tsk->signal->count);
172 unlock_task_sighand(tsk, &flags);
173 }
174 return count;
175}
176
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800177static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700178{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700181 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182
183 if (task) {
184 fs = get_fs_struct(task);
185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (fs) {
188 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800189 *path = fs->pwd;
190 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
194 }
195 return result;
196}
197
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800198static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700200 struct task_struct *task = get_proc_task(inode);
201 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700203
204 if (task) {
205 fs = get_fs_struct(task);
206 put_task_struct(task);
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (fs) {
209 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800210 *path = fs->root;
211 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 read_unlock(&fs->lock);
213 result = 0;
214 put_fs_struct(fs);
215 }
216 return result;
217}
218
Roland McGrath638fa202008-04-29 01:01:38 -0700219/*
220 * Return zero if current may access user memory in @task, -error if not.
221 */
222static int check_mem_permission(struct task_struct *task)
223{
224 /*
225 * A task can always look at itself, in case it chooses
226 * to use system calls instead of load instructions.
227 */
228 if (task == current)
229 return 0;
230
231 /*
232 * If current is actively ptrace'ing, and would also be
233 * permitted to freshly attach with ptrace now, permit it.
234 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700235 if (task_is_stopped_or_traced(task)) {
236 int match;
237 rcu_read_lock();
238 match = (tracehook_tracer_task(task) == current);
239 rcu_read_unlock();
240 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
241 return 0;
242 }
Roland McGrath638fa202008-04-29 01:01:38 -0700243
244 /*
245 * Noone else is allowed.
246 */
247 return -EPERM;
248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Al Viro831830b2008-01-02 14:09:57 +0000250struct mm_struct *mm_for_maps(struct task_struct *task)
251{
252 struct mm_struct *mm = get_task_mm(task);
253 if (!mm)
254 return NULL;
255 down_read(&mm->mmap_sem);
256 task_lock(task);
257 if (task->mm != mm)
258 goto out;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400259 if (task->mm != current->mm &&
260 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
Al Viro831830b2008-01-02 14:09:57 +0000261 goto out;
262 task_unlock(task);
263 return mm;
264out:
265 task_unlock(task);
266 up_read(&mm->mmap_sem);
267 mmput(mm);
268 return NULL;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static int proc_pid_cmdline(struct task_struct *task, char * buffer)
272{
273 int res = 0;
274 unsigned int len;
275 struct mm_struct *mm = get_task_mm(task);
276 if (!mm)
277 goto out;
278 if (!mm->arg_end)
279 goto out_mm; /* Shh! No looking before we're done */
280
281 len = mm->arg_end - mm->arg_start;
282
283 if (len > PAGE_SIZE)
284 len = PAGE_SIZE;
285
286 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
287
288 // If the nul at the end of args has been overwritten, then
289 // assume application is using setproctitle(3).
290 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
291 len = strnlen(buffer, res);
292 if (len < res) {
293 res = len;
294 } else {
295 len = mm->env_end - mm->env_start;
296 if (len > PAGE_SIZE - res)
297 len = PAGE_SIZE - res;
298 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
299 res = strnlen(buffer, res);
300 }
301 }
302out_mm:
303 mmput(mm);
304out:
305 return res;
306}
307
308static int proc_pid_auxv(struct task_struct *task, char *buffer)
309{
310 int res = 0;
311 struct mm_struct *mm = get_task_mm(task);
312 if (mm) {
313 unsigned int nwords = 0;
314 do
315 nwords += 2;
316 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
317 res = nwords * sizeof(mm->saved_auxv[0]);
318 if (res > PAGE_SIZE)
319 res = PAGE_SIZE;
320 memcpy(buffer, mm->saved_auxv, res);
321 mmput(mm);
322 }
323 return res;
324}
325
326
327#ifdef CONFIG_KALLSYMS
328/*
329 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
330 * Returns the resolved symbol. If that fails, simply return the address.
331 */
332static int proc_pid_wchan(struct task_struct *task, char *buffer)
333{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700334 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700335 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 wchan = get_wchan(task);
338
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700339 if (lookup_symbol_name(wchan, symname) < 0)
340 return sprintf(buffer, "%lu", wchan);
341 else
342 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344#endif /* CONFIG_KALLSYMS */
345
346#ifdef CONFIG_SCHEDSTATS
347/*
348 * Provides /proc/PID/schedstat
349 */
350static int proc_pid_schedstat(struct task_struct *task, char *buffer)
351{
Balbir Singh172ba842007-07-09 18:52:00 +0200352 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 task->sched_info.cpu_time,
354 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200355 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357#endif
358
Arjan van de Ven97455122008-01-25 21:08:34 +0100359#ifdef CONFIG_LATENCYTOP
360static int lstats_show_proc(struct seq_file *m, void *v)
361{
362 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800363 struct inode *inode = m->private;
364 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100365
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800366 if (!task)
367 return -ESRCH;
368 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100369 for (i = 0; i < 32; i++) {
370 if (task->latency_record[i].backtrace[0]) {
371 int q;
372 seq_printf(m, "%i %li %li ",
373 task->latency_record[i].count,
374 task->latency_record[i].time,
375 task->latency_record[i].max);
376 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
377 char sym[KSYM_NAME_LEN];
378 char *c;
379 if (!task->latency_record[i].backtrace[q])
380 break;
381 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
382 break;
383 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
384 c = strchr(sym, '+');
385 if (c)
386 *c = 0;
387 seq_printf(m, "%s ", sym);
388 }
389 seq_printf(m, "\n");
390 }
391
392 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800393 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100394 return 0;
395}
396
397static int lstats_open(struct inode *inode, struct file *file)
398{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800399 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800400}
401
Arjan van de Ven97455122008-01-25 21:08:34 +0100402static ssize_t lstats_write(struct file *file, const char __user *buf,
403 size_t count, loff_t *offs)
404{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800405 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100406
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800407 if (!task)
408 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100409 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800410 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100411
412 return count;
413}
414
415static const struct file_operations proc_lstats_operations = {
416 .open = lstats_open,
417 .read = seq_read,
418 .write = lstats_write,
419 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800420 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100421};
422
423#endif
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/* The badness from the OOM killer */
426unsigned long badness(struct task_struct *p, unsigned long uptime);
427static int proc_oom_score(struct task_struct *task, char *buffer)
428{
429 unsigned long points;
430 struct timespec uptime;
431
432 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700433 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700435 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return sprintf(buffer, "%lu\n", points);
437}
438
Neil Hormand85f50d2007-10-18 23:40:37 -0700439struct limit_names {
440 char *name;
441 char *unit;
442};
443
444static const struct limit_names lnames[RLIM_NLIMITS] = {
445 [RLIMIT_CPU] = {"Max cpu time", "ms"},
446 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
447 [RLIMIT_DATA] = {"Max data size", "bytes"},
448 [RLIMIT_STACK] = {"Max stack size", "bytes"},
449 [RLIMIT_CORE] = {"Max core file size", "bytes"},
450 [RLIMIT_RSS] = {"Max resident set", "bytes"},
451 [RLIMIT_NPROC] = {"Max processes", "processes"},
452 [RLIMIT_NOFILE] = {"Max open files", "files"},
453 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
454 [RLIMIT_AS] = {"Max address space", "bytes"},
455 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
456 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
457 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
458 [RLIMIT_NICE] = {"Max nice priority", NULL},
459 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800460 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700461};
462
463/* Display limits for a process */
464static int proc_pid_limits(struct task_struct *task, char *buffer)
465{
466 unsigned int i;
467 int count = 0;
468 unsigned long flags;
469 char *bufptr = buffer;
470
471 struct rlimit rlim[RLIM_NLIMITS];
472
473 rcu_read_lock();
474 if (!lock_task_sighand(task,&flags)) {
475 rcu_read_unlock();
476 return 0;
477 }
478 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
479 unlock_task_sighand(task, &flags);
480 rcu_read_unlock();
481
482 /*
483 * print the file header
484 */
485 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
487
488 for (i = 0; i < RLIM_NLIMITS; i++) {
489 if (rlim[i].rlim_cur == RLIM_INFINITY)
490 count += sprintf(&bufptr[count], "%-25s %-20s ",
491 lnames[i].name, "unlimited");
492 else
493 count += sprintf(&bufptr[count], "%-25s %-20lu ",
494 lnames[i].name, rlim[i].rlim_cur);
495
496 if (rlim[i].rlim_max == RLIM_INFINITY)
497 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498 else
499 count += sprintf(&bufptr[count], "%-20lu ",
500 rlim[i].rlim_max);
501
502 if (lnames[i].unit)
503 count += sprintf(&bufptr[count], "%-10s\n",
504 lnames[i].unit);
505 else
506 count += sprintf(&bufptr[count], "\n");
507 }
508
509 return count;
510}
511
Roland McGrathebcb6732008-07-25 19:46:00 -0700512#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513static int proc_pid_syscall(struct task_struct *task, char *buffer)
514{
515 long nr;
516 unsigned long args[6], sp, pc;
517
518 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
519 return sprintf(buffer, "running\n");
520
521 if (nr < 0)
522 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
523
524 return sprintf(buffer,
525 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
526 nr,
527 args[0], args[1], args[2], args[3], args[4], args[5],
528 sp, pc);
529}
530#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/************************************************************************/
533/* Here the fs part begins */
534/************************************************************************/
535
536/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700537static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700539 struct task_struct *task;
540 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700541 /* Allow access to a task's file descriptors if it is us or we
542 * may use ptrace attach to the process and find out that
543 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700544 */
545 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700546 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400547 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700548 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700549 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700550 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700553static int proc_setattr(struct dentry *dentry, struct iattr *attr)
554{
555 int error;
556 struct inode *inode = dentry->d_inode;
557
558 if (attr->ia_valid & ATTR_MODE)
559 return -EPERM;
560
561 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700562 if (!error)
563 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700564 return error;
565}
566
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800567static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700568 .setattr = proc_setattr,
569};
570
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100571static int mounts_open_common(struct inode *inode, struct file *file,
572 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700574 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700575 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800576 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100577 struct fs_struct *fs = NULL;
578 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500579 struct proc_mounts *p;
580 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Eric W. Biederman99f89552006-06-26 00:25:55 -0700582 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700583 rcu_read_lock();
584 nsp = task_nsproxy(task);
585 if (nsp) {
586 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800587 if (ns)
588 get_mnt_ns(ns);
589 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700590 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100591 if (ns)
592 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700593 put_task_struct(task);
594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100596 if (!ns)
597 goto err;
598 if (!fs)
599 goto err_put_ns;
600
601 read_lock(&fs->lock);
602 root = fs->root;
603 path_get(&root);
604 read_unlock(&fs->lock);
605 put_fs_struct(fs);
606
607 ret = -ENOMEM;
608 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
609 if (!p)
610 goto err_put_path;
611
612 file->private_data = &p->m;
613 ret = seq_open(file, op);
614 if (ret)
615 goto err_free;
616
617 p->m.private = p;
618 p->ns = ns;
619 p->root = root;
620 p->event = ns->event;
621
622 return 0;
623
624 err_free:
625 kfree(p);
626 err_put_path:
627 path_put(&root);
628 err_put_ns:
629 put_mnt_ns(ns);
630 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return ret;
632}
633
634static int mounts_release(struct inode *inode, struct file *file)
635{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100636 struct proc_mounts *p = file->private_data;
637 path_put(&p->root);
638 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 return seq_release(inode, file);
640}
641
Al Viro5addc5d2005-11-07 17:15:49 -0500642static unsigned mounts_poll(struct file *file, poll_table *wait)
643{
644 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100645 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500646 unsigned res = 0;
647
648 poll_wait(file, &ns->poll, wait);
649
650 spin_lock(&vfsmount_lock);
651 if (p->event != ns->event) {
652 p->event = ns->event;
653 res = POLLERR;
654 }
655 spin_unlock(&vfsmount_lock);
656
657 return res;
658}
659
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100660static int mounts_open(struct inode *inode, struct file *file)
661{
662 return mounts_open_common(inode, file, &mounts_op);
663}
664
Arjan van de Ven00977a52007-02-12 00:55:34 -0800665static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .open = mounts_open,
667 .read = seq_read,
668 .llseek = seq_lseek,
669 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500670 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671};
672
Ram Pai2d4d4862008-03-27 13:06:25 +0100673static int mountinfo_open(struct inode *inode, struct file *file)
674{
675 return mounts_open_common(inode, file, &mountinfo_op);
676}
677
678static const struct file_operations proc_mountinfo_operations = {
679 .open = mountinfo_open,
680 .read = seq_read,
681 .llseek = seq_lseek,
682 .release = mounts_release,
683 .poll = mounts_poll,
684};
685
Chuck Leverb4629fe2006-03-20 13:44:12 -0500686static int mountstats_open(struct inode *inode, struct file *file)
687{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100688 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500689}
690
Arjan van de Ven00977a52007-02-12 00:55:34 -0800691static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500692 .open = mountstats_open,
693 .read = seq_read,
694 .llseek = seq_lseek,
695 .release = mounts_release,
696};
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
699
700static ssize_t proc_info_read(struct file * file, char __user * buf,
701 size_t count, loff_t *ppos)
702{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800703 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 unsigned long page;
705 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700706 struct task_struct *task = get_proc_task(inode);
707
708 length = -ESRCH;
709 if (!task)
710 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (count > PROC_BLOCK_SIZE)
713 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700714
715 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700716 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700717 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 length = PROC_I(inode)->op.proc_read(task, (char*)page);
720
721 if (length >= 0)
722 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
723 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700724out:
725 put_task_struct(task);
726out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return length;
728}
729
Arjan van de Ven00977a52007-02-12 00:55:34 -0800730static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 .read = proc_info_read,
732};
733
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800734static int proc_single_show(struct seq_file *m, void *v)
735{
736 struct inode *inode = m->private;
737 struct pid_namespace *ns;
738 struct pid *pid;
739 struct task_struct *task;
740 int ret;
741
742 ns = inode->i_sb->s_fs_info;
743 pid = proc_pid(inode);
744 task = get_pid_task(pid, PIDTYPE_PID);
745 if (!task)
746 return -ESRCH;
747
748 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
749
750 put_task_struct(task);
751 return ret;
752}
753
754static int proc_single_open(struct inode *inode, struct file *filp)
755{
756 int ret;
757 ret = single_open(filp, proc_single_show, NULL);
758 if (!ret) {
759 struct seq_file *m = filp->private_data;
760
761 m->private = inode;
762 }
763 return ret;
764}
765
766static const struct file_operations proc_single_file_operations = {
767 .open = proc_single_open,
768 .read = seq_read,
769 .llseek = seq_lseek,
770 .release = single_release,
771};
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static int mem_open(struct inode* inode, struct file* file)
774{
775 file->private_data = (void*)((long)current->self_exec_id);
776 return 0;
777}
778
779static ssize_t mem_read(struct file * file, char __user * buf,
780 size_t count, loff_t *ppos)
781{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800782 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 char *page;
784 unsigned long src = *ppos;
785 int ret = -ESRCH;
786 struct mm_struct *mm;
787
Eric W. Biederman99f89552006-06-26 00:25:55 -0700788 if (!task)
789 goto out_no_task;
790
Roland McGrath638fa202008-04-29 01:01:38 -0700791 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto out;
793
794 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700795 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!page)
797 goto out;
798
799 ret = 0;
800
801 mm = get_task_mm(task);
802 if (!mm)
803 goto out_free;
804
805 ret = -EIO;
806
807 if (file->private_data != (void*)((long)current->self_exec_id))
808 goto out_put;
809
810 ret = 0;
811
812 while (count > 0) {
813 int this_len, retval;
814
815 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
816 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa202008-04-29 01:01:38 -0700817 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (!ret)
819 ret = -EIO;
820 break;
821 }
822
823 if (copy_to_user(buf, page, retval)) {
824 ret = -EFAULT;
825 break;
826 }
827
828 ret += retval;
829 src += retval;
830 buf += retval;
831 count -= retval;
832 }
833 *ppos = src;
834
835out_put:
836 mmput(mm);
837out_free:
838 free_page((unsigned long) page);
839out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700840 put_task_struct(task);
841out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return ret;
843}
844
845#define mem_write NULL
846
847#ifndef mem_write
848/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800849static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 size_t count, loff_t *ppos)
851{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700852 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800854 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned long dst = *ppos;
856
Eric W. Biederman99f89552006-06-26 00:25:55 -0700857 copied = -ESRCH;
858 if (!task)
859 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Roland McGrath638fa202008-04-29 01:01:38 -0700861 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700862 goto out;
863
864 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700865 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700869 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 while (count > 0) {
871 int this_len, retval;
872
873 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
874 if (copy_from_user(page, buf, this_len)) {
875 copied = -EFAULT;
876 break;
877 }
878 retval = access_process_vm(task, dst, page, this_len, 1);
879 if (!retval) {
880 if (!copied)
881 copied = -EIO;
882 break;
883 }
884 copied += retval;
885 buf += retval;
886 dst += retval;
887 count -= retval;
888 }
889 *ppos = dst;
890 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700891out:
892 put_task_struct(task);
893out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return copied;
895}
896#endif
897
Matt Mackall85863e42008-02-04 22:29:04 -0800898loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 switch (orig) {
901 case 0:
902 file->f_pos = offset;
903 break;
904 case 1:
905 file->f_pos += offset;
906 break;
907 default:
908 return -EINVAL;
909 }
910 force_successful_syscall_return();
911 return file->f_pos;
912}
913
Arjan van de Ven00977a52007-02-12 00:55:34 -0800914static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 .llseek = mem_lseek,
916 .read = mem_read,
917 .write = mem_write,
918 .open = mem_open,
919};
920
James Pearson315e28c2007-10-16 23:30:17 -0700921static ssize_t environ_read(struct file *file, char __user *buf,
922 size_t count, loff_t *ppos)
923{
924 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
925 char *page;
926 unsigned long src = *ppos;
927 int ret = -ESRCH;
928 struct mm_struct *mm;
929
930 if (!task)
931 goto out_no_task;
932
Stephen Smalley006ebb42008-05-19 08:32:49 -0400933 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700934 goto out;
935
936 ret = -ENOMEM;
937 page = (char *)__get_free_page(GFP_TEMPORARY);
938 if (!page)
939 goto out;
940
941 ret = 0;
942
943 mm = get_task_mm(task);
944 if (!mm)
945 goto out_free;
946
947 while (count > 0) {
948 int this_len, retval, max_len;
949
950 this_len = mm->env_end - (mm->env_start + src);
951
952 if (this_len <= 0)
953 break;
954
955 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
956 this_len = (this_len > max_len) ? max_len : this_len;
957
958 retval = access_process_vm(task, (mm->env_start + src),
959 page, this_len, 0);
960
961 if (retval <= 0) {
962 ret = retval;
963 break;
964 }
965
966 if (copy_to_user(buf, page, retval)) {
967 ret = -EFAULT;
968 break;
969 }
970
971 ret += retval;
972 src += retval;
973 buf += retval;
974 count -= retval;
975 }
976 *ppos = src;
977
978 mmput(mm);
979out_free:
980 free_page((unsigned long) page);
981out:
982 put_task_struct(task);
983out_no_task:
984 return ret;
985}
986
987static const struct file_operations proc_environ_operations = {
988 .read = environ_read,
989};
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991static ssize_t oom_adjust_read(struct file *file, char __user *buf,
992 size_t count, loff_t *ppos)
993{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800994 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700995 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700997 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Eric W. Biederman99f89552006-06-26 00:25:55 -0700999 if (!task)
1000 return -ESRCH;
1001 oom_adjust = task->oomkilladj;
1002 put_task_struct(task);
1003
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001004 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001005
1006 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
1009static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1010 size_t count, loff_t *ppos)
1011{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001012 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001013 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int oom_adjust;
1015
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001016 memset(buffer, 0, sizeof(buffer));
1017 if (count > sizeof(buffer) - 1)
1018 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (copy_from_user(buffer, buf, count))
1020 return -EFAULT;
1021 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001022 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1023 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -EINVAL;
1025 if (*end == '\n')
1026 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001027 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001028 if (!task)
1029 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001030 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1031 put_task_struct(task);
1032 return -EACCES;
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001035 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (end - buffer == 0)
1037 return -EIO;
1038 return end - buffer;
1039}
1040
Arjan van de Ven00977a52007-02-12 00:55:34 -08001041static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 .read = oom_adjust_read,
1043 .write = oom_adjust_write,
1044};
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046#ifdef CONFIG_AUDITSYSCALL
1047#define TMPBUFLEN 21
1048static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1049 size_t count, loff_t *ppos)
1050{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001051 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001052 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 ssize_t length;
1054 char tmpbuf[TMPBUFLEN];
1055
Eric W. Biederman99f89552006-06-26 00:25:55 -07001056 if (!task)
1057 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001059 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001060 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1062}
1063
1064static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1065 size_t count, loff_t *ppos)
1066{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001067 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 char *page, *tmp;
1069 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 uid_t loginuid;
1071
1072 if (!capable(CAP_AUDIT_CONTROL))
1073 return -EPERM;
1074
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001075 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return -EPERM;
1077
Al Viroe0182902006-05-18 08:28:02 -04001078 if (count >= PAGE_SIZE)
1079 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (*ppos != 0) {
1082 /* No partial writes. */
1083 return -EINVAL;
1084 }
Mel Gormane12ba742007-10-16 01:25:52 -07001085 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (!page)
1087 return -ENOMEM;
1088 length = -EFAULT;
1089 if (copy_from_user(page, buf, count))
1090 goto out_free_page;
1091
Al Viroe0182902006-05-18 08:28:02 -04001092 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 loginuid = simple_strtoul(page, &tmp, 10);
1094 if (tmp == page) {
1095 length = -EINVAL;
1096 goto out_free_page;
1097
1098 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001099 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (likely(length == 0))
1101 length = count;
1102
1103out_free_page:
1104 free_page((unsigned long) page);
1105 return length;
1106}
1107
Arjan van de Ven00977a52007-02-12 00:55:34 -08001108static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 .read = proc_loginuid_read,
1110 .write = proc_loginuid_write,
1111};
Eric Paris1e0bd752008-03-13 08:15:31 -04001112
1113static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1114 size_t count, loff_t *ppos)
1115{
1116 struct inode * inode = file->f_path.dentry->d_inode;
1117 struct task_struct *task = get_proc_task(inode);
1118 ssize_t length;
1119 char tmpbuf[TMPBUFLEN];
1120
1121 if (!task)
1122 return -ESRCH;
1123 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1124 audit_get_sessionid(task));
1125 put_task_struct(task);
1126 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1127}
1128
1129static const struct file_operations proc_sessionid_operations = {
1130 .read = proc_sessionid_read,
1131};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132#endif
1133
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001134#ifdef CONFIG_FAULT_INJECTION
1135static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1136 size_t count, loff_t *ppos)
1137{
1138 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1139 char buffer[PROC_NUMBUF];
1140 size_t len;
1141 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001142
1143 if (!task)
1144 return -ESRCH;
1145 make_it_fail = task->make_it_fail;
1146 put_task_struct(task);
1147
1148 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001149
1150 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001151}
1152
1153static ssize_t proc_fault_inject_write(struct file * file,
1154 const char __user * buf, size_t count, loff_t *ppos)
1155{
1156 struct task_struct *task;
1157 char buffer[PROC_NUMBUF], *end;
1158 int make_it_fail;
1159
1160 if (!capable(CAP_SYS_RESOURCE))
1161 return -EPERM;
1162 memset(buffer, 0, sizeof(buffer));
1163 if (count > sizeof(buffer) - 1)
1164 count = sizeof(buffer) - 1;
1165 if (copy_from_user(buffer, buf, count))
1166 return -EFAULT;
1167 make_it_fail = simple_strtol(buffer, &end, 0);
1168 if (*end == '\n')
1169 end++;
1170 task = get_proc_task(file->f_dentry->d_inode);
1171 if (!task)
1172 return -ESRCH;
1173 task->make_it_fail = make_it_fail;
1174 put_task_struct(task);
1175 if (end - buffer == 0)
1176 return -EIO;
1177 return end - buffer;
1178}
1179
Arjan van de Ven00977a52007-02-12 00:55:34 -08001180static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001181 .read = proc_fault_inject_read,
1182 .write = proc_fault_inject_write,
1183};
1184#endif
1185
Arjan van de Ven97455122008-01-25 21:08:34 +01001186
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001187#ifdef CONFIG_SCHED_DEBUG
1188/*
1189 * Print out various scheduling related per-task fields:
1190 */
1191static int sched_show(struct seq_file *m, void *v)
1192{
1193 struct inode *inode = m->private;
1194 struct task_struct *p;
1195
1196 WARN_ON(!inode);
1197
1198 p = get_proc_task(inode);
1199 if (!p)
1200 return -ESRCH;
1201 proc_sched_show_task(p, m);
1202
1203 put_task_struct(p);
1204
1205 return 0;
1206}
1207
1208static ssize_t
1209sched_write(struct file *file, const char __user *buf,
1210 size_t count, loff_t *offset)
1211{
1212 struct inode *inode = file->f_path.dentry->d_inode;
1213 struct task_struct *p;
1214
1215 WARN_ON(!inode);
1216
1217 p = get_proc_task(inode);
1218 if (!p)
1219 return -ESRCH;
1220 proc_sched_set_task(p);
1221
1222 put_task_struct(p);
1223
1224 return count;
1225}
1226
1227static int sched_open(struct inode *inode, struct file *filp)
1228{
1229 int ret;
1230
1231 ret = single_open(filp, sched_show, NULL);
1232 if (!ret) {
1233 struct seq_file *m = filp->private_data;
1234
1235 m->private = inode;
1236 }
1237 return ret;
1238}
1239
1240static const struct file_operations proc_pid_sched_operations = {
1241 .open = sched_open,
1242 .read = seq_read,
1243 .write = sched_write,
1244 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001245 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001246};
1247
1248#endif
1249
Matt Helsley925d1c42008-04-29 01:01:36 -07001250/*
1251 * We added or removed a vma mapping the executable. The vmas are only mapped
1252 * during exec and are not mapped with the mmap system call.
1253 * Callers must hold down_write() on the mm's mmap_sem for these
1254 */
1255void added_exe_file_vma(struct mm_struct *mm)
1256{
1257 mm->num_exe_file_vmas++;
1258}
1259
1260void removed_exe_file_vma(struct mm_struct *mm)
1261{
1262 mm->num_exe_file_vmas--;
1263 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1264 fput(mm->exe_file);
1265 mm->exe_file = NULL;
1266 }
1267
1268}
1269
1270void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1271{
1272 if (new_exe_file)
1273 get_file(new_exe_file);
1274 if (mm->exe_file)
1275 fput(mm->exe_file);
1276 mm->exe_file = new_exe_file;
1277 mm->num_exe_file_vmas = 0;
1278}
1279
1280struct file *get_mm_exe_file(struct mm_struct *mm)
1281{
1282 struct file *exe_file;
1283
1284 /* We need mmap_sem to protect against races with removal of
1285 * VM_EXECUTABLE vmas */
1286 down_read(&mm->mmap_sem);
1287 exe_file = mm->exe_file;
1288 if (exe_file)
1289 get_file(exe_file);
1290 up_read(&mm->mmap_sem);
1291 return exe_file;
1292}
1293
1294void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1295{
1296 /* It's safe to write the exe_file pointer without exe_file_lock because
1297 * this is called during fork when the task is not yet in /proc */
1298 newmm->exe_file = get_mm_exe_file(oldmm);
1299}
1300
1301static int proc_exe_link(struct inode *inode, struct path *exe_path)
1302{
1303 struct task_struct *task;
1304 struct mm_struct *mm;
1305 struct file *exe_file;
1306
1307 task = get_proc_task(inode);
1308 if (!task)
1309 return -ENOENT;
1310 mm = get_task_mm(task);
1311 put_task_struct(task);
1312 if (!mm)
1313 return -ENOENT;
1314 exe_file = get_mm_exe_file(mm);
1315 mmput(mm);
1316 if (exe_file) {
1317 *exe_path = exe_file->f_path;
1318 path_get(&exe_file->f_path);
1319 fput(exe_file);
1320 return 0;
1321 } else
1322 return -ENOENT;
1323}
1324
Al Viro008b1502005-08-20 00:17:39 +01001325static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326{
1327 struct inode *inode = dentry->d_inode;
1328 int error = -EACCES;
1329
1330 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001331 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Eric W. Biederman778c1142006-06-26 00:25:58 -07001333 /* Are we allowed to snoop on the tasks file descriptors? */
1334 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001337 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 nd->last_type = LAST_BIND;
1339out:
Al Viro008b1502005-08-20 00:17:39 +01001340 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001343static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344{
Mel Gormane12ba742007-10-16 01:25:52 -07001345 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001346 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int len;
1348
1349 if (!tmp)
1350 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001351
Jan Blunckcf28b482008-02-14 19:38:44 -08001352 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001353 len = PTR_ERR(pathname);
1354 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001356 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 if (len > buflen)
1359 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001360 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 len = -EFAULT;
1362 out:
1363 free_page((unsigned long)tmp);
1364 return len;
1365}
1366
1367static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1368{
1369 int error = -EACCES;
1370 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001371 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Eric W. Biederman778c1142006-06-26 00:25:58 -07001373 /* Are we allowed to snoop on the tasks file descriptors? */
1374 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001377 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (error)
1379 goto out;
1380
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001381 error = do_proc_readlink(&path, buffer, buflen);
1382 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return error;
1385}
1386
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001387static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001389 .follow_link = proc_pid_follow_link,
1390 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391};
1392
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001393
1394/* building an inode */
1395
1396static int task_dumpable(struct task_struct *task)
1397{
1398 int dumpable = 0;
1399 struct mm_struct *mm;
1400
1401 task_lock(task);
1402 mm = task->mm;
1403 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001404 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001405 task_unlock(task);
1406 if(dumpable == 1)
1407 return 1;
1408 return 0;
1409}
1410
1411
Eric W. Biederman61a28782006-10-02 02:18:49 -07001412static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001413{
1414 struct inode * inode;
1415 struct proc_inode *ei;
1416
1417 /* We need a new inode */
1418
1419 inode = new_inode(sb);
1420 if (!inode)
1421 goto out;
1422
1423 /* Common stuff */
1424 ei = PROC_I(inode);
1425 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001426 inode->i_op = &proc_def_inode_operations;
1427
1428 /*
1429 * grab the reference to task.
1430 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001431 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001432 if (!ei->pid)
1433 goto out_unlock;
1434
1435 inode->i_uid = 0;
1436 inode->i_gid = 0;
1437 if (task_dumpable(task)) {
1438 inode->i_uid = task->euid;
1439 inode->i_gid = task->egid;
1440 }
1441 security_task_to_inode(task, inode);
1442
1443out:
1444 return inode;
1445
1446out_unlock:
1447 iput(inode);
1448 return NULL;
1449}
1450
1451static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1452{
1453 struct inode *inode = dentry->d_inode;
1454 struct task_struct *task;
1455 generic_fillattr(inode, stat);
1456
1457 rcu_read_lock();
1458 stat->uid = 0;
1459 stat->gid = 0;
1460 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1461 if (task) {
1462 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1463 task_dumpable(task)) {
1464 stat->uid = task->euid;
1465 stat->gid = task->egid;
1466 }
1467 }
1468 rcu_read_unlock();
1469 return 0;
1470}
1471
1472/* dentry stuff */
1473
1474/*
1475 * Exceptional case: normally we are not allowed to unhash a busy
1476 * directory. In this case, however, we can do it - no aliasing problems
1477 * due to the way we treat inodes.
1478 *
1479 * Rewrite the inode's ownerships here because the owning task may have
1480 * performed a setuid(), etc.
1481 *
1482 * Before the /proc/pid/status file was created the only way to read
1483 * the effective uid of a /process was to stat /proc/pid. Reading
1484 * /proc/pid/status is slow enough that procps and other packages
1485 * kept stating /proc/pid. To keep the rules in /proc simple I have
1486 * made this apply to all per process world readable and executable
1487 * directories.
1488 */
1489static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1490{
1491 struct inode *inode = dentry->d_inode;
1492 struct task_struct *task = get_proc_task(inode);
1493 if (task) {
1494 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1495 task_dumpable(task)) {
1496 inode->i_uid = task->euid;
1497 inode->i_gid = task->egid;
1498 } else {
1499 inode->i_uid = 0;
1500 inode->i_gid = 0;
1501 }
1502 inode->i_mode &= ~(S_ISUID | S_ISGID);
1503 security_task_to_inode(task, inode);
1504 put_task_struct(task);
1505 return 1;
1506 }
1507 d_drop(dentry);
1508 return 0;
1509}
1510
1511static int pid_delete_dentry(struct dentry * dentry)
1512{
1513 /* Is the task we represent dead?
1514 * If so, then don't put the dentry on the lru list,
1515 * kill it immediately.
1516 */
1517 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1518}
1519
1520static struct dentry_operations pid_dentry_operations =
1521{
1522 .d_revalidate = pid_revalidate,
1523 .d_delete = pid_delete_dentry,
1524};
1525
1526/* Lookups */
1527
Eric Dumazetc5141e62007-05-08 00:26:15 -07001528typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1529 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001530
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001531/*
1532 * Fill a directory entry.
1533 *
1534 * If possible create the dcache entry and derive our inode number and
1535 * file type from dcache entry.
1536 *
1537 * Since all of the proc inode numbers are dynamically generated, the inode
1538 * numbers do not exist until the inode is cache. This means creating the
1539 * the dcache entry in readdir is necessary to keep the inode numbers
1540 * reported by readdir in sync with the inode numbers reported
1541 * by stat.
1542 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001543static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1544 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001545 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001546{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001547 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001548 struct inode *inode;
1549 struct qstr qname;
1550 ino_t ino = 0;
1551 unsigned type = DT_UNKNOWN;
1552
1553 qname.name = name;
1554 qname.len = len;
1555 qname.hash = full_name_hash(name, len);
1556
1557 child = d_lookup(dir, &qname);
1558 if (!child) {
1559 struct dentry *new;
1560 new = d_alloc(dir, &qname);
1561 if (new) {
1562 child = instantiate(dir->d_inode, new, task, ptr);
1563 if (child)
1564 dput(new);
1565 else
1566 child = new;
1567 }
1568 }
1569 if (!child || IS_ERR(child) || !child->d_inode)
1570 goto end_instantiate;
1571 inode = child->d_inode;
1572 if (inode) {
1573 ino = inode->i_ino;
1574 type = inode->i_mode >> 12;
1575 }
1576 dput(child);
1577end_instantiate:
1578 if (!ino)
1579 ino = find_inode_number(dir, &qname);
1580 if (!ino)
1581 ino = 1;
1582 return filldir(dirent, name, len, filp->f_pos, ino, type);
1583}
1584
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001585static unsigned name_to_int(struct dentry *dentry)
1586{
1587 const char *name = dentry->d_name.name;
1588 int len = dentry->d_name.len;
1589 unsigned n = 0;
1590
1591 if (len > 1 && *name == '0')
1592 goto out;
1593 while (len-- > 0) {
1594 unsigned c = *name++ - '0';
1595 if (c > 9)
1596 goto out;
1597 if (n >= (~0U-9)/10)
1598 goto out;
1599 n *= 10;
1600 n += c;
1601 }
1602 return n;
1603out:
1604 return ~0U;
1605}
1606
Miklos Szeredi27932742007-05-08 00:26:17 -07001607#define PROC_FDINFO_MAX 64
1608
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001609static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001610{
1611 struct task_struct *task = get_proc_task(inode);
1612 struct files_struct *files = NULL;
1613 struct file *file;
1614 int fd = proc_fd(inode);
1615
1616 if (task) {
1617 files = get_files_struct(task);
1618 put_task_struct(task);
1619 }
1620 if (files) {
1621 /*
1622 * We are not taking a ref to the file structure, so we must
1623 * hold ->file_lock.
1624 */
1625 spin_lock(&files->file_lock);
1626 file = fcheck_files(files, fd);
1627 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001628 if (path) {
1629 *path = file->f_path;
1630 path_get(&file->f_path);
1631 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001632 if (info)
1633 snprintf(info, PROC_FDINFO_MAX,
1634 "pos:\t%lli\n"
1635 "flags:\t0%o\n",
1636 (long long) file->f_pos,
1637 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001638 spin_unlock(&files->file_lock);
1639 put_files_struct(files);
1640 return 0;
1641 }
1642 spin_unlock(&files->file_lock);
1643 put_files_struct(files);
1644 }
1645 return -ENOENT;
1646}
1647
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001648static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001649{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001650 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001651}
1652
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001653static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1654{
1655 struct inode *inode = dentry->d_inode;
1656 struct task_struct *task = get_proc_task(inode);
1657 int fd = proc_fd(inode);
1658 struct files_struct *files;
1659
1660 if (task) {
1661 files = get_files_struct(task);
1662 if (files) {
1663 rcu_read_lock();
1664 if (fcheck_files(files, fd)) {
1665 rcu_read_unlock();
1666 put_files_struct(files);
1667 if (task_dumpable(task)) {
1668 inode->i_uid = task->euid;
1669 inode->i_gid = task->egid;
1670 } else {
1671 inode->i_uid = 0;
1672 inode->i_gid = 0;
1673 }
1674 inode->i_mode &= ~(S_ISUID | S_ISGID);
1675 security_task_to_inode(task, inode);
1676 put_task_struct(task);
1677 return 1;
1678 }
1679 rcu_read_unlock();
1680 put_files_struct(files);
1681 }
1682 put_task_struct(task);
1683 }
1684 d_drop(dentry);
1685 return 0;
1686}
1687
1688static struct dentry_operations tid_fd_dentry_operations =
1689{
1690 .d_revalidate = tid_fd_revalidate,
1691 .d_delete = pid_delete_dentry,
1692};
1693
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001694static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001695 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001696{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001697 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001698 struct file *file;
1699 struct files_struct *files;
1700 struct inode *inode;
1701 struct proc_inode *ei;
1702 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001703
Eric W. Biederman61a28782006-10-02 02:18:49 -07001704 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001705 if (!inode)
1706 goto out;
1707 ei = PROC_I(inode);
1708 ei->fd = fd;
1709 files = get_files_struct(task);
1710 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001711 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001712 inode->i_mode = S_IFLNK;
1713
1714 /*
1715 * We are not taking a ref to the file structure, so we must
1716 * hold ->file_lock.
1717 */
1718 spin_lock(&files->file_lock);
1719 file = fcheck_files(files, fd);
1720 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001721 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001722 if (file->f_mode & 1)
1723 inode->i_mode |= S_IRUSR | S_IXUSR;
1724 if (file->f_mode & 2)
1725 inode->i_mode |= S_IWUSR | S_IXUSR;
1726 spin_unlock(&files->file_lock);
1727 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001728
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001729 inode->i_op = &proc_pid_link_inode_operations;
1730 inode->i_size = 64;
1731 ei->op.proc_get_link = proc_fd_link;
1732 dentry->d_op = &tid_fd_dentry_operations;
1733 d_add(dentry, inode);
1734 /* Close the race of the process dying before we return the dentry */
1735 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001736 error = NULL;
1737
1738 out:
1739 return error;
1740out_unlock:
1741 spin_unlock(&files->file_lock);
1742 put_files_struct(files);
1743out_iput:
1744 iput(inode);
1745 goto out;
1746}
1747
Miklos Szeredi27932742007-05-08 00:26:17 -07001748static struct dentry *proc_lookupfd_common(struct inode *dir,
1749 struct dentry *dentry,
1750 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001751{
1752 struct task_struct *task = get_proc_task(dir);
1753 unsigned fd = name_to_int(dentry);
1754 struct dentry *result = ERR_PTR(-ENOENT);
1755
1756 if (!task)
1757 goto out_no_task;
1758 if (fd == ~0U)
1759 goto out;
1760
Miklos Szeredi27932742007-05-08 00:26:17 -07001761 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001762out:
1763 put_task_struct(task);
1764out_no_task:
1765 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001766}
1767
Miklos Szeredi27932742007-05-08 00:26:17 -07001768static int proc_readfd_common(struct file * filp, void * dirent,
1769 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001771 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001772 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001773 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001774 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct files_struct * files;
1777
1778 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001779 if (!p)
1780 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 fd = filp->f_pos;
1784 switch (fd) {
1785 case 0:
1786 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1787 goto out;
1788 filp->f_pos++;
1789 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001790 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1792 goto out;
1793 filp->f_pos++;
1794 default:
1795 files = get_files_struct(p);
1796 if (!files)
1797 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001798 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001800 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001802 char name[PROC_NUMBUF];
1803 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 if (!fcheck_files(files, fd))
1806 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001807 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Miklos Szeredi27932742007-05-08 00:26:17 -07001809 len = snprintf(name, sizeof(name), "%d", fd);
1810 if (proc_fill_cache(filp, dirent, filldir,
1811 name, len, instantiate,
1812 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001813 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 break;
1815 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001816 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001818 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 put_files_struct(files);
1820 }
1821out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001822 put_task_struct(p);
1823out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return retval;
1825}
1826
Miklos Szeredi27932742007-05-08 00:26:17 -07001827static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1828 struct nameidata *nd)
1829{
1830 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1831}
1832
1833static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1834{
1835 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1836}
1837
1838static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1839 size_t len, loff_t *ppos)
1840{
1841 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001842 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001843 if (!err)
1844 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1845 return err;
1846}
1847
1848static const struct file_operations proc_fdinfo_file_operations = {
1849 .open = nonseekable_open,
1850 .read = proc_fdinfo_read,
1851};
1852
Arjan van de Ven00977a52007-02-12 00:55:34 -08001853static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 .read = generic_read_dir,
1855 .readdir = proc_readfd,
1856};
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001859 * /proc/pid/fd needs a special permission handler so that a process can still
1860 * access /proc/self/fd after it has executed a setuid().
1861 */
1862static int proc_fd_permission(struct inode *inode, int mask,
1863 struct nameidata *nd)
1864{
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 Righi297c5d92008-07-25 01:48:49 -07002406 u64 rchar, wchar, syscr, syscw;
2407 struct task_io_accounting ioac;
2408
Andrea Righib2d002d2008-07-26 15:22:27 -07002409 rchar = task->rchar;
2410 wchar = task->wchar;
2411 syscr = task->syscr;
2412 syscw = task->syscw;
2413 memcpy(&ioac, &task->ioac, sizeof(ioac));
2414
2415 if (whole) {
Andrea Righi297c5d92008-07-25 01:48:49 -07002416 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002417
2418 if (lock_task_sighand(task, &flags)) {
2419 struct signal_struct *sig = task->signal;
Andrea Righib2d002d2008-07-26 15:22:27 -07002420 struct task_struct *t = task;
Andrea Righi297c5d92008-07-25 01:48:49 -07002421
2422 rchar += sig->rchar;
2423 wchar += sig->wchar;
2424 syscr += sig->syscr;
2425 syscw += sig->syscw;
2426
2427 ioac.read_bytes += sig->ioac.read_bytes;
2428 ioac.write_bytes += sig->ioac.write_bytes;
2429 ioac.cancelled_write_bytes +=
2430 sig->ioac.cancelled_write_bytes;
Andrea Righib2d002d2008-07-26 15:22:27 -07002431 while_each_thread(task, t) {
2432 rchar += t->rchar;
2433 wchar += t->wchar;
2434 syscr += t->syscr;
2435 syscw += t->syscw;
Andrea Righi297c5d92008-07-25 01:48:49 -07002436
Andrea Righib2d002d2008-07-26 15:22:27 -07002437 ioac.read_bytes += t->ioac.read_bytes;
2438 ioac.write_bytes += t->ioac.write_bytes;
2439 ioac.cancelled_write_bytes +=
2440 t->ioac.cancelled_write_bytes;
2441 }
Andrea Righi297c5d92008-07-25 01:48:49 -07002442 unlock_task_sighand(task, &flags);
2443 }
2444 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002445 return sprintf(buffer,
2446 "rchar: %llu\n"
2447 "wchar: %llu\n"
2448 "syscr: %llu\n"
2449 "syscw: %llu\n"
2450 "read_bytes: %llu\n"
2451 "write_bytes: %llu\n"
2452 "cancelled_write_bytes: %llu\n",
Andrea Righib2d002d2008-07-26 15:22:27 -07002453 rchar, wchar, syscr, syscw,
2454 ioac.read_bytes, ioac.write_bytes,
2455 ioac.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002456}
Andrea Righi297c5d92008-07-25 01:48:49 -07002457
2458static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2459{
2460 return do_io_accounting(task, buffer, 0);
2461}
2462
2463static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2464{
2465 return do_io_accounting(task, buffer, 1);
2466}
2467#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002468
Eric W. Biederman801199c2006-10-02 02:18:48 -07002469/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002470 * Thread groups
2471 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002472static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002473static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002474
Eric Dumazetc5141e62007-05-08 00:26:15 -07002475static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002476 DIR("task", S_IRUGO|S_IXUGO, task),
2477 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002478 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002479#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002480 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002481#endif
James Pearson315e28c2007-10-16 23:30:17 -07002482 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002483 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002484 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002485 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002486#ifdef CONFIG_SCHED_DEBUG
2487 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2488#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002489#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2490 INF("syscall", S_IRUSR, pid_syscall),
2491#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002492 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002493 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002494 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002495 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002497 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002498#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002499 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002500 LNK("cwd", cwd),
2501 LNK("root", root),
2502 LNK("exe", exe),
2503 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002504 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002505 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002506#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002507 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002508 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002509 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002510#endif
2511#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002512 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002513#endif
2514#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002515 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002516#endif
2517#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002518 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002519#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002520#ifdef CONFIG_LATENCYTOP
2521 REG("latency", S_IRUGO, lstats),
2522#endif
Paul Menage8793d852007-10-18 23:39:39 -07002523#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002524 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002525#endif
Paul Menagea4243162007-10-18 23:39:35 -07002526#ifdef CONFIG_CGROUPS
2527 REG("cgroup", S_IRUGO, cgroup),
2528#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002529 INF("oom_score", S_IRUGO, oom_score),
2530 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002531#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002532 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Steve Grubb6ee65042008-04-29 15:01:13 -04002533 REG("sessionid", S_IRUGO, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002535#ifdef CONFIG_FAULT_INJECTION
2536 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2537#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002538#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2539 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2540#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002541#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002542 INF("io", S_IRUGO, tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002543#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002544};
2545
2546static int proc_tgid_base_readdir(struct file * filp,
2547 void * dirent, filldir_t filldir)
2548{
2549 return proc_pident_readdir(filp,dirent,filldir,
2550 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2551}
2552
Arjan van de Ven00977a52007-02-12 00:55:34 -08002553static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002554 .read = generic_read_dir,
2555 .readdir = proc_tgid_base_readdir,
2556};
2557
2558static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002559 return proc_pident_lookup(dir, dentry,
2560 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002561}
2562
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002563static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002564 .lookup = proc_tgid_base_lookup,
2565 .getattr = pid_getattr,
2566 .setattr = proc_setattr,
2567};
2568
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002569static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002571 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002572 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002573 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Eric W. Biederman48e64842006-06-26 00:25:48 -07002575 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002576 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2577 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002578 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002579 if (!(current->flags & PF_EXITING))
2580 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002581 d_drop(dentry);
2582 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002585 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002586 goto out;
2587
2588 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002589 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2590 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002591 if (!leader)
2592 goto out;
2593
2594 name.name = "task";
2595 name.len = strlen(name.name);
2596 dir = d_hash_and_lookup(leader, &name);
2597 if (!dir)
2598 goto out_put_leader;
2599
2600 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002601 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002602 dentry = d_hash_and_lookup(dir, &name);
2603 if (dentry) {
2604 shrink_dcache_parent(dentry);
2605 d_drop(dentry);
2606 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002608
2609 dput(dir);
2610out_put_leader:
2611 dput(leader);
2612out:
2613 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
Randy Dunlap0895e912007-10-21 21:00:10 -07002616/**
2617 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2618 * @task: task that should be flushed.
2619 *
2620 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002621 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002622 * in. This call is supposed to do all of this job.
2623 *
2624 * Looks in the dcache for
2625 * /proc/@pid
2626 * /proc/@tgid/task/@pid
2627 * if either directory is present flushes it and all of it'ts children
2628 * from the dcache.
2629 *
2630 * It is safe and reasonable to cache /proc entries for a task until
2631 * that task exits. After that they just clog up the dcache with
2632 * useless entries, possibly causing useful dcache entries to be
2633 * flushed instead. This routine is proved to flush those useless
2634 * dcache entries at process exit time.
2635 *
2636 * NOTE: This routine is just an optimization so it does not guarantee
2637 * that no dcache entries will exist at process exit time it
2638 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002639 */
2640
2641void proc_flush_task(struct task_struct *task)
2642{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002643 int i;
2644 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002645 struct upid *upid;
2646
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002647 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002648 if (thread_group_leader(task))
2649 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002650
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002651 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002652 upid = &pid->numbers[i];
2653 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002654 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002655 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002656
2657 upid = &pid->numbers[pid->level];
2658 if (upid->nr == 1)
2659 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002660}
2661
Adrian Bunk9711ef92006-12-06 20:38:31 -08002662static struct dentry *proc_pid_instantiate(struct inode *dir,
2663 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002664 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002665{
2666 struct dentry *error = ERR_PTR(-ENOENT);
2667 struct inode *inode;
2668
Eric W. Biederman61a28782006-10-02 02:18:49 -07002669 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002670 if (!inode)
2671 goto out;
2672
2673 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2674 inode->i_op = &proc_tgid_base_inode_operations;
2675 inode->i_fop = &proc_tgid_base_operations;
2676 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002677
2678 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2679 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002680
2681 dentry->d_op = &pid_dentry_operations;
2682
2683 d_add(dentry, inode);
2684 /* Close the race of the process dying before we return the dentry */
2685 if (pid_revalidate(dentry, NULL))
2686 error = NULL;
2687out:
2688 return error;
2689}
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2692{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002693 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002696 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Eric W. Biederman801199c2006-10-02 02:18:48 -07002698 result = proc_base_lookup(dir, dentry);
2699 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2700 goto out;
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 tgid = name_to_int(dentry);
2703 if (tgid == ~0U)
2704 goto out;
2705
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002706 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002707 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002708 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (task)
2710 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002711 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (!task)
2713 goto out;
2714
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002715 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002716 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002718 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002722 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002723 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002725struct tgid_iter {
2726 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002727 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002728};
2729static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2730{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002731 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002733 if (iter.task)
2734 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002735 rcu_read_lock();
2736retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002737 iter.task = NULL;
2738 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002739 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002740 iter.tgid = pid_nr_ns(pid, ns);
2741 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002742 /* What we to know is if the pid we have find is the
2743 * pid of a thread_group_leader. Testing for task
2744 * being a thread_group_leader is the obvious thing
2745 * todo but there is a window when it fails, due to
2746 * the pid transfer logic in de_thread.
2747 *
2748 * So we perform the straight forward test of seeing
2749 * if the pid we have found is the pid of a thread
2750 * group leader, and don't worry if the task we have
2751 * found doesn't happen to be a thread group leader.
2752 * As we don't care in the case of readdir.
2753 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002754 if (!iter.task || !has_group_leader_pid(iter.task)) {
2755 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002756 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002757 }
2758 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002760 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002761 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002764#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Eric W. Biederman61a28782006-10-02 02:18:49 -07002766static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002767 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002768{
2769 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002770 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002771 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002772 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002773}
2774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775/* for the /proc/ directory itself, after non-process stuff has been done */
2776int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002779 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002780 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002781 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Eric W. Biederman61a28782006-10-02 02:18:49 -07002783 if (!reaper)
2784 goto out_no_task;
2785
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002786 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002787 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002788 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002789 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002792 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002793 iter.task = NULL;
2794 iter.tgid = filp->f_pos - TGID_OFFSET;
2795 for (iter = next_tgid(ns, iter);
2796 iter.task;
2797 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2798 filp->f_pos = iter.tgid + TGID_OFFSET;
2799 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2800 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002801 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002804 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2805out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002806 put_task_struct(reaper);
2807out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 return 0;
2809}
2810
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002811/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002812 * Tasks
2813 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002814static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002815 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002816 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002817 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002818 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002819 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002820 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002821#ifdef CONFIG_SCHED_DEBUG
2822 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2823#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002824#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2825 INF("syscall", S_IRUSR, pid_syscall),
2826#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002827 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002828 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002829 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002830 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002831#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002832 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002833#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002834 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002835 LNK("cwd", cwd),
2836 LNK("root", root),
2837 LNK("exe", exe),
2838 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002839 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002840#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002841 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002842 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002843 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002844#endif
2845#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002846 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002847#endif
2848#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002849 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002850#endif
2851#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002852 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002853#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002854#ifdef CONFIG_LATENCYTOP
2855 REG("latency", S_IRUGO, lstats),
2856#endif
Paul Menage8793d852007-10-18 23:39:39 -07002857#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002858 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002859#endif
Paul Menagea4243162007-10-18 23:39:35 -07002860#ifdef CONFIG_CGROUPS
2861 REG("cgroup", S_IRUGO, cgroup),
2862#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002863 INF("oom_score", S_IRUGO, oom_score),
2864 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002865#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002866 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002867 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002868#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002869#ifdef CONFIG_FAULT_INJECTION
2870 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2871#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002872#ifdef CONFIG_TASK_IO_ACCOUNTING
2873 INF("io", S_IRUGO, tid_io_accounting),
2874#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002875};
2876
2877static int proc_tid_base_readdir(struct file * filp,
2878 void * dirent, filldir_t filldir)
2879{
2880 return proc_pident_readdir(filp,dirent,filldir,
2881 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2882}
2883
2884static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002885 return proc_pident_lookup(dir, dentry,
2886 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002887}
2888
Arjan van de Ven00977a52007-02-12 00:55:34 -08002889static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002890 .read = generic_read_dir,
2891 .readdir = proc_tid_base_readdir,
2892};
2893
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002894static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002895 .lookup = proc_tid_base_lookup,
2896 .getattr = pid_getattr,
2897 .setattr = proc_setattr,
2898};
2899
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002900static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002901 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002902{
2903 struct dentry *error = ERR_PTR(-ENOENT);
2904 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002905 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002906
2907 if (!inode)
2908 goto out;
2909 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2910 inode->i_op = &proc_tid_base_inode_operations;
2911 inode->i_fop = &proc_tid_base_operations;
2912 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002913
2914 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2915 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002916
2917 dentry->d_op = &pid_dentry_operations;
2918
2919 d_add(dentry, inode);
2920 /* Close the race of the process dying before we return the dentry */
2921 if (pid_revalidate(dentry, NULL))
2922 error = NULL;
2923out:
2924 return error;
2925}
2926
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002927static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2928{
2929 struct dentry *result = ERR_PTR(-ENOENT);
2930 struct task_struct *task;
2931 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002933 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002934
2935 if (!leader)
2936 goto out_no_task;
2937
2938 tid = name_to_int(dentry);
2939 if (tid == ~0U)
2940 goto out;
2941
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002942 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002943 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002944 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002945 if (task)
2946 get_task_struct(task);
2947 rcu_read_unlock();
2948 if (!task)
2949 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002950 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002951 goto out_drop_task;
2952
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002953 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002954out_drop_task:
2955 put_task_struct(task);
2956out:
2957 put_task_struct(leader);
2958out_no_task:
2959 return result;
2960}
2961
2962/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002963 * Find the first tid of a thread group to return to user space.
2964 *
2965 * Usually this is just the thread group leader, but if the users
2966 * buffer was too small or there was a seek into the middle of the
2967 * directory we have more work todo.
2968 *
2969 * In the case of a short read we start with find_task_by_pid.
2970 *
2971 * In the case of a seek we start with the leader and walk nr
2972 * threads past it.
2973 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002974static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002975 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002976{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002977 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002978
Eric W. Biedermancc288732006-06-26 00:26:01 -07002979 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002980 /* Attempt to start with the pid of a thread */
2981 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002982 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002983 if (pos && (pos->group_leader == leader))
2984 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002985 }
2986
2987 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002988 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002989 if (nr && nr >= get_nr_threads(leader))
2990 goto out;
2991
2992 /* If we haven't found our starting place yet start
2993 * with the leader and walk nr threads forward.
2994 */
2995 for (pos = leader; nr > 0; --nr) {
2996 pos = next_thread(pos);
2997 if (pos == leader) {
2998 pos = NULL;
2999 goto out;
3000 }
3001 }
3002found:
3003 get_task_struct(pos);
3004out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003005 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003006 return pos;
3007}
3008
3009/*
3010 * Find the next thread in the thread list.
3011 * Return NULL if there is an error or no next thread.
3012 *
3013 * The reference to the input task_struct is released.
3014 */
3015static struct task_struct *next_tid(struct task_struct *start)
3016{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003017 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003018 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003019 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003020 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003021 if (thread_group_leader(pos))
3022 pos = NULL;
3023 else
3024 get_task_struct(pos);
3025 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003026 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003027 put_task_struct(start);
3028 return pos;
3029}
3030
Eric W. Biederman61a28782006-10-02 02:18:49 -07003031static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3032 struct task_struct *task, int tid)
3033{
3034 char name[PROC_NUMBUF];
3035 int len = snprintf(name, sizeof(name), "%d", tid);
3036 return proc_fill_cache(filp, dirent, filldir, name, len,
3037 proc_task_instantiate, task, NULL);
3038}
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040/* for the /proc/TGID/task/ directories */
3041static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3042{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003043 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003045 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003046 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 int retval = -ENOENT;
3048 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003049 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003051 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003053 task = get_proc_task(inode);
3054 if (!task)
3055 goto out_no_task;
3056 rcu_read_lock();
3057 if (pid_alive(task)) {
3058 leader = task->group_leader;
3059 get_task_struct(leader);
3060 }
3061 rcu_read_unlock();
3062 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003063 if (!leader)
3064 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 retval = 0;
3066
3067 switch (pos) {
3068 case 0:
3069 ino = inode->i_ino;
3070 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3071 goto out;
3072 pos++;
3073 /* fall through */
3074 case 1:
3075 ino = parent_ino(dentry);
3076 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3077 goto out;
3078 pos++;
3079 /* fall through */
3080 }
3081
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003082 /* f_version caches the tgid value that the last readdir call couldn't
3083 * return. lseek aka telldir automagically resets f_version to 0.
3084 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003085 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003086 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003087 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003088 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003089 task;
3090 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003091 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003092 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003093 /* returning this tgid failed, save it as the first
3094 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003095 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003096 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 }
3100out:
3101 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003102 put_task_struct(leader);
3103out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 return retval;
3105}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003106
3107static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3108{
3109 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003110 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003111 generic_fillattr(inode, stat);
3112
Eric W. Biederman99f89552006-06-26 00:25:55 -07003113 if (p) {
3114 rcu_read_lock();
3115 stat->nlink += get_nr_threads(p);
3116 rcu_read_unlock();
3117 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003118 }
3119
3120 return 0;
3121}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003122
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003123static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003124 .lookup = proc_task_lookup,
3125 .getattr = proc_task_getattr,
3126 .setattr = proc_setattr,
3127};
3128
Arjan van de Ven00977a52007-02-12 00:55:34 -08003129static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003130 .read = generic_read_dir,
3131 .readdir = proc_task_readdir,
3132};