blob: 85e06e4980780163740790e9d160a69bab6cfc7e [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>
59#include <linux/string.h>
60#include <linux/seq_file.h>
61#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080062#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070064#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070066#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070067#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/mount.h>
69#include <linux/security.h>
70#include <linux/ptrace.h>
Paul Menagea4243162007-10-18 23:39:35 -070071#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/cpuset.h>
73#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050074#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070075#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070076#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070077#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070078#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "internal.h"
80
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070081/* NOTE:
82 * Implementing inode permission operations in /proc is almost
83 * certainly an error. Permission checks need to happen during
84 * each system call not at open time. The reason is that most of
85 * what we wish to check for permissions in /proc varies at runtime.
86 *
87 * The classic example of a problem is opening file descriptors
88 * in /proc for a task before it execs a suid executable.
89 */
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070093 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080095 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080096 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070097 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
Eric W. Biederman61a28782006-10-02 02:18:49 -0700100#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700101 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700102 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .mode = MODE, \
104 .iop = IOP, \
105 .fop = FOP, \
106 .op = OP, \
107}
108
Eric W. Biederman61a28782006-10-02 02:18:49 -0700109#define DIR(NAME, MODE, OTYPE) \
110 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700111 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
112 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700113#define LNK(NAME, OTYPE) \
114 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700115 &proc_pid_link_inode_operations, NULL, \
116 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700117#define REG(NAME, MODE, OTYPE) \
118 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700119 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define INF(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 NULL, &proc_info_file_operations, \
123 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800124#define ONE(NAME, MODE, OTYPE) \
125 NOD(NAME, (S_IFREG|(MODE)), \
126 NULL, &proc_single_file_operations, \
127 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Kees Cook5096add2007-05-08 00:26:04 -0700129int maps_protect;
130EXPORT_SYMBOL(maps_protect);
131
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700132static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700135 task_lock(task);
136 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if(fs)
138 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700139 task_unlock(task);
140 return fs;
141}
142
Eric W. Biederman99f89552006-06-26 00:25:55 -0700143static int get_nr_threads(struct task_struct *tsk)
144{
145 /* Must be called with the rcu_read_lock held */
146 unsigned long flags;
147 int count = 0;
148
149 if (lock_task_sighand(tsk, &flags)) {
150 count = atomic_read(&tsk->signal->count);
151 unlock_task_sighand(tsk, &flags);
152 }
153 return count;
154}
155
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800156static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700158 struct task_struct *task = get_proc_task(inode);
159 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700160 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700161
162 if (task) {
163 fs = get_fs_struct(task);
164 put_task_struct(task);
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (fs) {
167 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800168 *path = fs->pwd;
169 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 read_unlock(&fs->lock);
171 result = 0;
172 put_fs_struct(fs);
173 }
174 return result;
175}
176
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800177static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -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->root;
190 path_get(&fs->root);
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
198#define MAY_PTRACE(task) \
199 (task == current || \
200 (task->parent == current && \
201 (task->ptrace & PT_PTRACED) && \
Matthew Wilcox6d8982d2007-12-06 11:04:01 -0500202 (task_is_stopped_or_traced(task)) && \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 security_ptrace(current,task) == 0))
204
Al Viro831830b2008-01-02 14:09:57 +0000205struct mm_struct *mm_for_maps(struct task_struct *task)
206{
207 struct mm_struct *mm = get_task_mm(task);
208 if (!mm)
209 return NULL;
210 down_read(&mm->mmap_sem);
211 task_lock(task);
212 if (task->mm != mm)
213 goto out;
214 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
215 goto out;
216 task_unlock(task);
217 return mm;
218out:
219 task_unlock(task);
220 up_read(&mm->mmap_sem);
221 mmput(mm);
222 return NULL;
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225static int proc_pid_cmdline(struct task_struct *task, char * buffer)
226{
227 int res = 0;
228 unsigned int len;
229 struct mm_struct *mm = get_task_mm(task);
230 if (!mm)
231 goto out;
232 if (!mm->arg_end)
233 goto out_mm; /* Shh! No looking before we're done */
234
235 len = mm->arg_end - mm->arg_start;
236
237 if (len > PAGE_SIZE)
238 len = PAGE_SIZE;
239
240 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
241
242 // If the nul at the end of args has been overwritten, then
243 // assume application is using setproctitle(3).
244 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
245 len = strnlen(buffer, res);
246 if (len < res) {
247 res = len;
248 } else {
249 len = mm->env_end - mm->env_start;
250 if (len > PAGE_SIZE - res)
251 len = PAGE_SIZE - res;
252 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
253 res = strnlen(buffer, res);
254 }
255 }
256out_mm:
257 mmput(mm);
258out:
259 return res;
260}
261
262static int proc_pid_auxv(struct task_struct *task, char *buffer)
263{
264 int res = 0;
265 struct mm_struct *mm = get_task_mm(task);
266 if (mm) {
267 unsigned int nwords = 0;
268 do
269 nwords += 2;
270 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
271 res = nwords * sizeof(mm->saved_auxv[0]);
272 if (res > PAGE_SIZE)
273 res = PAGE_SIZE;
274 memcpy(buffer, mm->saved_auxv, res);
275 mmput(mm);
276 }
277 return res;
278}
279
280
281#ifdef CONFIG_KALLSYMS
282/*
283 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
284 * Returns the resolved symbol. If that fails, simply return the address.
285 */
286static int proc_pid_wchan(struct task_struct *task, char *buffer)
287{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700288 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700289 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 wchan = get_wchan(task);
292
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700293 if (lookup_symbol_name(wchan, symname) < 0)
294 return sprintf(buffer, "%lu", wchan);
295 else
296 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298#endif /* CONFIG_KALLSYMS */
299
300#ifdef CONFIG_SCHEDSTATS
301/*
302 * Provides /proc/PID/schedstat
303 */
304static int proc_pid_schedstat(struct task_struct *task, char *buffer)
305{
Balbir Singh172ba842007-07-09 18:52:00 +0200306 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 task->sched_info.cpu_time,
308 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200309 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311#endif
312
Arjan van de Ven97455122008-01-25 21:08:34 +0100313#ifdef CONFIG_LATENCYTOP
314static int lstats_show_proc(struct seq_file *m, void *v)
315{
316 int i;
317 struct task_struct *task = m->private;
318 seq_puts(m, "Latency Top version : v0.1\n");
319
320 for (i = 0; i < 32; i++) {
321 if (task->latency_record[i].backtrace[0]) {
322 int q;
323 seq_printf(m, "%i %li %li ",
324 task->latency_record[i].count,
325 task->latency_record[i].time,
326 task->latency_record[i].max);
327 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
328 char sym[KSYM_NAME_LEN];
329 char *c;
330 if (!task->latency_record[i].backtrace[q])
331 break;
332 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
333 break;
334 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
335 c = strchr(sym, '+');
336 if (c)
337 *c = 0;
338 seq_printf(m, "%s ", sym);
339 }
340 seq_printf(m, "\n");
341 }
342
343 }
344 return 0;
345}
346
347static int lstats_open(struct inode *inode, struct file *file)
348{
349 int ret;
350 struct seq_file *m;
351 struct task_struct *task = get_proc_task(inode);
352
Hiroshi Shimamotoae002782008-02-14 10:26:24 -0800353 if (!task)
354 return -ENOENT;
Arjan van de Ven97455122008-01-25 21:08:34 +0100355 ret = single_open(file, lstats_show_proc, NULL);
356 if (!ret) {
357 m = file->private_data;
358 m->private = task;
359 }
360 return ret;
361}
362
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800363static int lstats_release(struct inode *inode, struct file *file)
364{
365 struct seq_file *m = file->private_data;
366 struct task_struct *task = m->private;
367
368 put_task_struct(task);
369 return single_release(inode, file);
370}
371
Arjan van de Ven97455122008-01-25 21:08:34 +0100372static ssize_t lstats_write(struct file *file, const char __user *buf,
373 size_t count, loff_t *offs)
374{
375 struct seq_file *m;
376 struct task_struct *task;
377
378 m = file->private_data;
379 task = m->private;
380 clear_all_latency_tracing(task);
381
382 return count;
383}
384
385static const struct file_operations proc_lstats_operations = {
386 .open = lstats_open,
387 .read = seq_read,
388 .write = lstats_write,
389 .llseek = seq_lseek,
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800390 .release = lstats_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100391};
392
393#endif
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/* The badness from the OOM killer */
396unsigned long badness(struct task_struct *p, unsigned long uptime);
397static int proc_oom_score(struct task_struct *task, char *buffer)
398{
399 unsigned long points;
400 struct timespec uptime;
401
402 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700403 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700405 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return sprintf(buffer, "%lu\n", points);
407}
408
Neil Hormand85f50d2007-10-18 23:40:37 -0700409struct limit_names {
410 char *name;
411 char *unit;
412};
413
414static const struct limit_names lnames[RLIM_NLIMITS] = {
415 [RLIMIT_CPU] = {"Max cpu time", "ms"},
416 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
417 [RLIMIT_DATA] = {"Max data size", "bytes"},
418 [RLIMIT_STACK] = {"Max stack size", "bytes"},
419 [RLIMIT_CORE] = {"Max core file size", "bytes"},
420 [RLIMIT_RSS] = {"Max resident set", "bytes"},
421 [RLIMIT_NPROC] = {"Max processes", "processes"},
422 [RLIMIT_NOFILE] = {"Max open files", "files"},
423 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
424 [RLIMIT_AS] = {"Max address space", "bytes"},
425 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
426 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
427 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
428 [RLIMIT_NICE] = {"Max nice priority", NULL},
429 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800430 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700431};
432
433/* Display limits for a process */
434static int proc_pid_limits(struct task_struct *task, char *buffer)
435{
436 unsigned int i;
437 int count = 0;
438 unsigned long flags;
439 char *bufptr = buffer;
440
441 struct rlimit rlim[RLIM_NLIMITS];
442
443 rcu_read_lock();
444 if (!lock_task_sighand(task,&flags)) {
445 rcu_read_unlock();
446 return 0;
447 }
448 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
449 unlock_task_sighand(task, &flags);
450 rcu_read_unlock();
451
452 /*
453 * print the file header
454 */
455 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
456 "Limit", "Soft Limit", "Hard Limit", "Units");
457
458 for (i = 0; i < RLIM_NLIMITS; i++) {
459 if (rlim[i].rlim_cur == RLIM_INFINITY)
460 count += sprintf(&bufptr[count], "%-25s %-20s ",
461 lnames[i].name, "unlimited");
462 else
463 count += sprintf(&bufptr[count], "%-25s %-20lu ",
464 lnames[i].name, rlim[i].rlim_cur);
465
466 if (rlim[i].rlim_max == RLIM_INFINITY)
467 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
468 else
469 count += sprintf(&bufptr[count], "%-20lu ",
470 rlim[i].rlim_max);
471
472 if (lnames[i].unit)
473 count += sprintf(&bufptr[count], "%-10s\n",
474 lnames[i].unit);
475 else
476 count += sprintf(&bufptr[count], "\n");
477 }
478
479 return count;
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/************************************************************************/
483/* Here the fs part begins */
484/************************************************************************/
485
486/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700487static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700489 struct task_struct *task;
490 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700491 /* Allow access to a task's file descriptors if it is us or we
492 * may use ptrace attach to the process and find out that
493 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700494 */
495 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700496 if (task) {
497 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700498 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700499 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700500 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700503static int proc_setattr(struct dentry *dentry, struct iattr *attr)
504{
505 int error;
506 struct inode *inode = dentry->d_inode;
507
508 if (attr->ia_valid & ATTR_MODE)
509 return -EPERM;
510
511 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700512 if (!error)
513 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700514 return error;
515}
516
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800517static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700518 .setattr = proc_setattr,
519};
520
Jan Engelhardt03a44822008-02-08 04:21:19 -0800521extern const struct seq_operations mounts_op;
Al Viro5addc5d2005-11-07 17:15:49 -0500522struct proc_mounts {
523 struct seq_file m;
524 int event;
525};
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527static int mounts_open(struct inode *inode, struct file *file)
528{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700529 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700530 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800531 struct mnt_namespace *ns = NULL;
Al Viro5addc5d2005-11-07 17:15:49 -0500532 struct proc_mounts *p;
533 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Eric W. Biederman99f89552006-06-26 00:25:55 -0700535 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700536 rcu_read_lock();
537 nsp = task_nsproxy(task);
538 if (nsp) {
539 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800540 if (ns)
541 get_mnt_ns(ns);
542 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700543 rcu_read_unlock();
544
Eric W. Biederman99f89552006-06-26 00:25:55 -0700545 put_task_struct(task);
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800548 if (ns) {
Al Viro5addc5d2005-11-07 17:15:49 -0500549 ret = -ENOMEM;
550 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
551 if (p) {
552 file->private_data = &p->m;
553 ret = seq_open(file, &mounts_op);
554 if (!ret) {
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800555 p->m.private = ns;
556 p->event = ns->event;
Al Viro5addc5d2005-11-07 17:15:49 -0500557 return 0;
558 }
559 kfree(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800561 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 return ret;
564}
565
566static int mounts_release(struct inode *inode, struct file *file)
567{
568 struct seq_file *m = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800569 struct mnt_namespace *ns = m->private;
570 put_mnt_ns(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return seq_release(inode, file);
572}
573
Al Viro5addc5d2005-11-07 17:15:49 -0500574static unsigned mounts_poll(struct file *file, poll_table *wait)
575{
576 struct proc_mounts *p = file->private_data;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800577 struct mnt_namespace *ns = p->m.private;
Al Viro5addc5d2005-11-07 17:15:49 -0500578 unsigned res = 0;
579
580 poll_wait(file, &ns->poll, wait);
581
582 spin_lock(&vfsmount_lock);
583 if (p->event != ns->event) {
584 p->event = ns->event;
585 res = POLLERR;
586 }
587 spin_unlock(&vfsmount_lock);
588
589 return res;
590}
591
Arjan van de Ven00977a52007-02-12 00:55:34 -0800592static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 .open = mounts_open,
594 .read = seq_read,
595 .llseek = seq_lseek,
596 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500597 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598};
599
Jan Engelhardt03a44822008-02-08 04:21:19 -0800600extern const struct seq_operations mountstats_op;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500601static int mountstats_open(struct inode *inode, struct file *file)
602{
Chuck Leverb4629fe2006-03-20 13:44:12 -0500603 int ret = seq_open(file, &mountstats_op);
604
605 if (!ret) {
606 struct seq_file *m = file->private_data;
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700607 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800608 struct mnt_namespace *mnt_ns = NULL;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700609 struct task_struct *task = get_proc_task(inode);
610
611 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700612 rcu_read_lock();
613 nsp = task_nsproxy(task);
614 if (nsp) {
615 mnt_ns = nsp->mnt_ns;
616 if (mnt_ns)
617 get_mnt_ns(mnt_ns);
618 }
619 rcu_read_unlock();
620
Eric W. Biederman99f89552006-06-26 00:25:55 -0700621 put_task_struct(task);
622 }
Chuck Leverb4629fe2006-03-20 13:44:12 -0500623
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800624 if (mnt_ns)
625 m->private = mnt_ns;
Chuck Leverb4629fe2006-03-20 13:44:12 -0500626 else {
627 seq_release(inode, file);
628 ret = -EINVAL;
629 }
630 }
631 return ret;
632}
633
Arjan van de Ven00977a52007-02-12 00:55:34 -0800634static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500635 .open = mountstats_open,
636 .read = seq_read,
637 .llseek = seq_lseek,
638 .release = mounts_release,
639};
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
642
643static ssize_t proc_info_read(struct file * file, char __user * buf,
644 size_t count, loff_t *ppos)
645{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800646 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 unsigned long page;
648 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700649 struct task_struct *task = get_proc_task(inode);
650
651 length = -ESRCH;
652 if (!task)
653 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (count > PROC_BLOCK_SIZE)
656 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700657
658 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700659 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700660 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 length = PROC_I(inode)->op.proc_read(task, (char*)page);
663
664 if (length >= 0)
665 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
666 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700667out:
668 put_task_struct(task);
669out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return length;
671}
672
Arjan van de Ven00977a52007-02-12 00:55:34 -0800673static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .read = proc_info_read,
675};
676
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800677static int proc_single_show(struct seq_file *m, void *v)
678{
679 struct inode *inode = m->private;
680 struct pid_namespace *ns;
681 struct pid *pid;
682 struct task_struct *task;
683 int ret;
684
685 ns = inode->i_sb->s_fs_info;
686 pid = proc_pid(inode);
687 task = get_pid_task(pid, PIDTYPE_PID);
688 if (!task)
689 return -ESRCH;
690
691 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
692
693 put_task_struct(task);
694 return ret;
695}
696
697static int proc_single_open(struct inode *inode, struct file *filp)
698{
699 int ret;
700 ret = single_open(filp, proc_single_show, NULL);
701 if (!ret) {
702 struct seq_file *m = filp->private_data;
703
704 m->private = inode;
705 }
706 return ret;
707}
708
709static const struct file_operations proc_single_file_operations = {
710 .open = proc_single_open,
711 .read = seq_read,
712 .llseek = seq_lseek,
713 .release = single_release,
714};
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716static int mem_open(struct inode* inode, struct file* file)
717{
718 file->private_data = (void*)((long)current->self_exec_id);
719 return 0;
720}
721
722static ssize_t mem_read(struct file * file, char __user * buf,
723 size_t count, loff_t *ppos)
724{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800725 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 char *page;
727 unsigned long src = *ppos;
728 int ret = -ESRCH;
729 struct mm_struct *mm;
730
Eric W. Biederman99f89552006-06-26 00:25:55 -0700731 if (!task)
732 goto out_no_task;
733
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700734 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 goto out;
736
737 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700738 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (!page)
740 goto out;
741
742 ret = 0;
743
744 mm = get_task_mm(task);
745 if (!mm)
746 goto out_free;
747
748 ret = -EIO;
749
750 if (file->private_data != (void*)((long)current->self_exec_id))
751 goto out_put;
752
753 ret = 0;
754
755 while (count > 0) {
756 int this_len, retval;
757
758 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
759 retval = access_process_vm(task, src, page, this_len, 0);
Miklos Szerediab8d11b2005-09-06 15:18:24 -0700760 if (!retval || !MAY_PTRACE(task) || !ptrace_may_attach(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!ret)
762 ret = -EIO;
763 break;
764 }
765
766 if (copy_to_user(buf, page, retval)) {
767 ret = -EFAULT;
768 break;
769 }
770
771 ret += retval;
772 src += retval;
773 buf += retval;
774 count -= retval;
775 }
776 *ppos = src;
777
778out_put:
779 mmput(mm);
780out_free:
781 free_page((unsigned long) page);
782out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700783 put_task_struct(task);
784out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return ret;
786}
787
788#define mem_write NULL
789
790#ifndef mem_write
791/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800792static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 size_t count, loff_t *ppos)
794{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700795 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800797 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 unsigned long dst = *ppos;
799
Eric W. Biederman99f89552006-06-26 00:25:55 -0700800 copied = -ESRCH;
801 if (!task)
802 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Eric W. Biederman99f89552006-06-26 00:25:55 -0700804 if (!MAY_PTRACE(task) || !ptrace_may_attach(task))
805 goto out;
806
807 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700808 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700810 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700812 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 while (count > 0) {
814 int this_len, retval;
815
816 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
817 if (copy_from_user(page, buf, this_len)) {
818 copied = -EFAULT;
819 break;
820 }
821 retval = access_process_vm(task, dst, page, this_len, 1);
822 if (!retval) {
823 if (!copied)
824 copied = -EIO;
825 break;
826 }
827 copied += retval;
828 buf += retval;
829 dst += retval;
830 count -= retval;
831 }
832 *ppos = dst;
833 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700834out:
835 put_task_struct(task);
836out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return copied;
838}
839#endif
840
Matt Mackall85863e42008-02-04 22:29:04 -0800841loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 switch (orig) {
844 case 0:
845 file->f_pos = offset;
846 break;
847 case 1:
848 file->f_pos += offset;
849 break;
850 default:
851 return -EINVAL;
852 }
853 force_successful_syscall_return();
854 return file->f_pos;
855}
856
Arjan van de Ven00977a52007-02-12 00:55:34 -0800857static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 .llseek = mem_lseek,
859 .read = mem_read,
860 .write = mem_write,
861 .open = mem_open,
862};
863
James Pearson315e28c2007-10-16 23:30:17 -0700864static ssize_t environ_read(struct file *file, char __user *buf,
865 size_t count, loff_t *ppos)
866{
867 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
868 char *page;
869 unsigned long src = *ppos;
870 int ret = -ESRCH;
871 struct mm_struct *mm;
872
873 if (!task)
874 goto out_no_task;
875
876 if (!ptrace_may_attach(task))
877 goto out;
878
879 ret = -ENOMEM;
880 page = (char *)__get_free_page(GFP_TEMPORARY);
881 if (!page)
882 goto out;
883
884 ret = 0;
885
886 mm = get_task_mm(task);
887 if (!mm)
888 goto out_free;
889
890 while (count > 0) {
891 int this_len, retval, max_len;
892
893 this_len = mm->env_end - (mm->env_start + src);
894
895 if (this_len <= 0)
896 break;
897
898 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
899 this_len = (this_len > max_len) ? max_len : this_len;
900
901 retval = access_process_vm(task, (mm->env_start + src),
902 page, this_len, 0);
903
904 if (retval <= 0) {
905 ret = retval;
906 break;
907 }
908
909 if (copy_to_user(buf, page, retval)) {
910 ret = -EFAULT;
911 break;
912 }
913
914 ret += retval;
915 src += retval;
916 buf += retval;
917 count -= retval;
918 }
919 *ppos = src;
920
921 mmput(mm);
922out_free:
923 free_page((unsigned long) page);
924out:
925 put_task_struct(task);
926out_no_task:
927 return ret;
928}
929
930static const struct file_operations proc_environ_operations = {
931 .read = environ_read,
932};
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static ssize_t oom_adjust_read(struct file *file, char __user *buf,
935 size_t count, loff_t *ppos)
936{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800937 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700938 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700940 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Eric W. Biederman99f89552006-06-26 00:25:55 -0700942 if (!task)
943 return -ESRCH;
944 oom_adjust = task->oomkilladj;
945 put_task_struct(task);
946
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700947 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700948
949 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
951
952static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
953 size_t count, loff_t *ppos)
954{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700955 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700956 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int oom_adjust;
958
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700959 memset(buffer, 0, sizeof(buffer));
960 if (count > sizeof(buffer) - 1)
961 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (copy_from_user(buffer, buf, count))
963 return -EFAULT;
964 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700965 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
966 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -EINVAL;
968 if (*end == '\n')
969 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800970 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700971 if (!task)
972 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800973 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
974 put_task_struct(task);
975 return -EACCES;
976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700978 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (end - buffer == 0)
980 return -EIO;
981 return end - buffer;
982}
983
Arjan van de Ven00977a52007-02-12 00:55:34 -0800984static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 .read = oom_adjust_read,
986 .write = oom_adjust_write,
987};
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989#ifdef CONFIG_AUDITSYSCALL
990#define TMPBUFLEN 21
991static ssize_t proc_loginuid_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 inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700995 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ssize_t length;
997 char tmpbuf[TMPBUFLEN];
998
Eric W. Biederman99f89552006-06-26 00:25:55 -0700999 if (!task)
1000 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001002 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001003 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1005}
1006
1007static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1008 size_t count, loff_t *ppos)
1009{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001010 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 char *page, *tmp;
1012 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 uid_t loginuid;
1014
1015 if (!capable(CAP_AUDIT_CONTROL))
1016 return -EPERM;
1017
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001018 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return -EPERM;
1020
Al Viroe0182902006-05-18 08:28:02 -04001021 if (count >= PAGE_SIZE)
1022 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 if (*ppos != 0) {
1025 /* No partial writes. */
1026 return -EINVAL;
1027 }
Mel Gormane12ba742007-10-16 01:25:52 -07001028 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!page)
1030 return -ENOMEM;
1031 length = -EFAULT;
1032 if (copy_from_user(page, buf, count))
1033 goto out_free_page;
1034
Al Viroe0182902006-05-18 08:28:02 -04001035 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 loginuid = simple_strtoul(page, &tmp, 10);
1037 if (tmp == page) {
1038 length = -EINVAL;
1039 goto out_free_page;
1040
1041 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001042 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (likely(length == 0))
1044 length = count;
1045
1046out_free_page:
1047 free_page((unsigned long) page);
1048 return length;
1049}
1050
Arjan van de Ven00977a52007-02-12 00:55:34 -08001051static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 .read = proc_loginuid_read,
1053 .write = proc_loginuid_write,
1054};
1055#endif
1056
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001057#ifdef CONFIG_FAULT_INJECTION
1058static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1059 size_t count, loff_t *ppos)
1060{
1061 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1062 char buffer[PROC_NUMBUF];
1063 size_t len;
1064 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001065
1066 if (!task)
1067 return -ESRCH;
1068 make_it_fail = task->make_it_fail;
1069 put_task_struct(task);
1070
1071 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001072
1073 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001074}
1075
1076static ssize_t proc_fault_inject_write(struct file * file,
1077 const char __user * buf, size_t count, loff_t *ppos)
1078{
1079 struct task_struct *task;
1080 char buffer[PROC_NUMBUF], *end;
1081 int make_it_fail;
1082
1083 if (!capable(CAP_SYS_RESOURCE))
1084 return -EPERM;
1085 memset(buffer, 0, sizeof(buffer));
1086 if (count > sizeof(buffer) - 1)
1087 count = sizeof(buffer) - 1;
1088 if (copy_from_user(buffer, buf, count))
1089 return -EFAULT;
1090 make_it_fail = simple_strtol(buffer, &end, 0);
1091 if (*end == '\n')
1092 end++;
1093 task = get_proc_task(file->f_dentry->d_inode);
1094 if (!task)
1095 return -ESRCH;
1096 task->make_it_fail = make_it_fail;
1097 put_task_struct(task);
1098 if (end - buffer == 0)
1099 return -EIO;
1100 return end - buffer;
1101}
1102
Arjan van de Ven00977a52007-02-12 00:55:34 -08001103static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001104 .read = proc_fault_inject_read,
1105 .write = proc_fault_inject_write,
1106};
1107#endif
1108
Arjan van de Ven97455122008-01-25 21:08:34 +01001109
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001110#ifdef CONFIG_SCHED_DEBUG
1111/*
1112 * Print out various scheduling related per-task fields:
1113 */
1114static int sched_show(struct seq_file *m, void *v)
1115{
1116 struct inode *inode = m->private;
1117 struct task_struct *p;
1118
1119 WARN_ON(!inode);
1120
1121 p = get_proc_task(inode);
1122 if (!p)
1123 return -ESRCH;
1124 proc_sched_show_task(p, m);
1125
1126 put_task_struct(p);
1127
1128 return 0;
1129}
1130
1131static ssize_t
1132sched_write(struct file *file, const char __user *buf,
1133 size_t count, loff_t *offset)
1134{
1135 struct inode *inode = file->f_path.dentry->d_inode;
1136 struct task_struct *p;
1137
1138 WARN_ON(!inode);
1139
1140 p = get_proc_task(inode);
1141 if (!p)
1142 return -ESRCH;
1143 proc_sched_set_task(p);
1144
1145 put_task_struct(p);
1146
1147 return count;
1148}
1149
1150static int sched_open(struct inode *inode, struct file *filp)
1151{
1152 int ret;
1153
1154 ret = single_open(filp, sched_show, NULL);
1155 if (!ret) {
1156 struct seq_file *m = filp->private_data;
1157
1158 m->private = inode;
1159 }
1160 return ret;
1161}
1162
1163static const struct file_operations proc_pid_sched_operations = {
1164 .open = sched_open,
1165 .read = seq_read,
1166 .write = sched_write,
1167 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001168 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001169};
1170
1171#endif
1172
Al Viro008b1502005-08-20 00:17:39 +01001173static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct inode *inode = dentry->d_inode;
1176 int error = -EACCES;
1177
1178 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001179 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Eric W. Biederman778c1142006-06-26 00:25:58 -07001181 /* Are we allowed to snoop on the tasks file descriptors? */
1182 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001185 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 nd->last_type = LAST_BIND;
1187out:
Al Viro008b1502005-08-20 00:17:39 +01001188 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001191static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
Mel Gormane12ba742007-10-16 01:25:52 -07001193 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001194 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 int len;
1196
1197 if (!tmp)
1198 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001199
Jan Blunckcf28b482008-02-14 19:38:44 -08001200 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001201 len = PTR_ERR(pathname);
1202 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001204 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 if (len > buflen)
1207 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001208 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 len = -EFAULT;
1210 out:
1211 free_page((unsigned long)tmp);
1212 return len;
1213}
1214
1215static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1216{
1217 int error = -EACCES;
1218 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001219 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Eric W. Biederman778c1142006-06-26 00:25:58 -07001221 /* Are we allowed to snoop on the tasks file descriptors? */
1222 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001225 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (error)
1227 goto out;
1228
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001229 error = do_proc_readlink(&path, buffer, buflen);
1230 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return error;
1233}
1234
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001235static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001237 .follow_link = proc_pid_follow_link,
1238 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001241
1242/* building an inode */
1243
1244static int task_dumpable(struct task_struct *task)
1245{
1246 int dumpable = 0;
1247 struct mm_struct *mm;
1248
1249 task_lock(task);
1250 mm = task->mm;
1251 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001252 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001253 task_unlock(task);
1254 if(dumpable == 1)
1255 return 1;
1256 return 0;
1257}
1258
1259
Eric W. Biederman61a28782006-10-02 02:18:49 -07001260static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001261{
1262 struct inode * inode;
1263 struct proc_inode *ei;
1264
1265 /* We need a new inode */
1266
1267 inode = new_inode(sb);
1268 if (!inode)
1269 goto out;
1270
1271 /* Common stuff */
1272 ei = PROC_I(inode);
1273 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001274 inode->i_op = &proc_def_inode_operations;
1275
1276 /*
1277 * grab the reference to task.
1278 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07001279 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001280 if (!ei->pid)
1281 goto out_unlock;
1282
1283 inode->i_uid = 0;
1284 inode->i_gid = 0;
1285 if (task_dumpable(task)) {
1286 inode->i_uid = task->euid;
1287 inode->i_gid = task->egid;
1288 }
1289 security_task_to_inode(task, inode);
1290
1291out:
1292 return inode;
1293
1294out_unlock:
1295 iput(inode);
1296 return NULL;
1297}
1298
1299static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1300{
1301 struct inode *inode = dentry->d_inode;
1302 struct task_struct *task;
1303 generic_fillattr(inode, stat);
1304
1305 rcu_read_lock();
1306 stat->uid = 0;
1307 stat->gid = 0;
1308 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1309 if (task) {
1310 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1311 task_dumpable(task)) {
1312 stat->uid = task->euid;
1313 stat->gid = task->egid;
1314 }
1315 }
1316 rcu_read_unlock();
1317 return 0;
1318}
1319
1320/* dentry stuff */
1321
1322/*
1323 * Exceptional case: normally we are not allowed to unhash a busy
1324 * directory. In this case, however, we can do it - no aliasing problems
1325 * due to the way we treat inodes.
1326 *
1327 * Rewrite the inode's ownerships here because the owning task may have
1328 * performed a setuid(), etc.
1329 *
1330 * Before the /proc/pid/status file was created the only way to read
1331 * the effective uid of a /process was to stat /proc/pid. Reading
1332 * /proc/pid/status is slow enough that procps and other packages
1333 * kept stating /proc/pid. To keep the rules in /proc simple I have
1334 * made this apply to all per process world readable and executable
1335 * directories.
1336 */
1337static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1338{
1339 struct inode *inode = dentry->d_inode;
1340 struct task_struct *task = get_proc_task(inode);
1341 if (task) {
1342 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1343 task_dumpable(task)) {
1344 inode->i_uid = task->euid;
1345 inode->i_gid = task->egid;
1346 } else {
1347 inode->i_uid = 0;
1348 inode->i_gid = 0;
1349 }
1350 inode->i_mode &= ~(S_ISUID | S_ISGID);
1351 security_task_to_inode(task, inode);
1352 put_task_struct(task);
1353 return 1;
1354 }
1355 d_drop(dentry);
1356 return 0;
1357}
1358
1359static int pid_delete_dentry(struct dentry * dentry)
1360{
1361 /* Is the task we represent dead?
1362 * If so, then don't put the dentry on the lru list,
1363 * kill it immediately.
1364 */
1365 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1366}
1367
1368static struct dentry_operations pid_dentry_operations =
1369{
1370 .d_revalidate = pid_revalidate,
1371 .d_delete = pid_delete_dentry,
1372};
1373
1374/* Lookups */
1375
Eric Dumazetc5141e62007-05-08 00:26:15 -07001376typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1377 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001378
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001379/*
1380 * Fill a directory entry.
1381 *
1382 * If possible create the dcache entry and derive our inode number and
1383 * file type from dcache entry.
1384 *
1385 * Since all of the proc inode numbers are dynamically generated, the inode
1386 * numbers do not exist until the inode is cache. This means creating the
1387 * the dcache entry in readdir is necessary to keep the inode numbers
1388 * reported by readdir in sync with the inode numbers reported
1389 * by stat.
1390 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001391static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1392 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001393 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001394{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001395 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001396 struct inode *inode;
1397 struct qstr qname;
1398 ino_t ino = 0;
1399 unsigned type = DT_UNKNOWN;
1400
1401 qname.name = name;
1402 qname.len = len;
1403 qname.hash = full_name_hash(name, len);
1404
1405 child = d_lookup(dir, &qname);
1406 if (!child) {
1407 struct dentry *new;
1408 new = d_alloc(dir, &qname);
1409 if (new) {
1410 child = instantiate(dir->d_inode, new, task, ptr);
1411 if (child)
1412 dput(new);
1413 else
1414 child = new;
1415 }
1416 }
1417 if (!child || IS_ERR(child) || !child->d_inode)
1418 goto end_instantiate;
1419 inode = child->d_inode;
1420 if (inode) {
1421 ino = inode->i_ino;
1422 type = inode->i_mode >> 12;
1423 }
1424 dput(child);
1425end_instantiate:
1426 if (!ino)
1427 ino = find_inode_number(dir, &qname);
1428 if (!ino)
1429 ino = 1;
1430 return filldir(dirent, name, len, filp->f_pos, ino, type);
1431}
1432
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001433static unsigned name_to_int(struct dentry *dentry)
1434{
1435 const char *name = dentry->d_name.name;
1436 int len = dentry->d_name.len;
1437 unsigned n = 0;
1438
1439 if (len > 1 && *name == '0')
1440 goto out;
1441 while (len-- > 0) {
1442 unsigned c = *name++ - '0';
1443 if (c > 9)
1444 goto out;
1445 if (n >= (~0U-9)/10)
1446 goto out;
1447 n *= 10;
1448 n += c;
1449 }
1450 return n;
1451out:
1452 return ~0U;
1453}
1454
Miklos Szeredi27932742007-05-08 00:26:17 -07001455#define PROC_FDINFO_MAX 64
1456
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001457static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001458{
1459 struct task_struct *task = get_proc_task(inode);
1460 struct files_struct *files = NULL;
1461 struct file *file;
1462 int fd = proc_fd(inode);
1463
1464 if (task) {
1465 files = get_files_struct(task);
1466 put_task_struct(task);
1467 }
1468 if (files) {
1469 /*
1470 * We are not taking a ref to the file structure, so we must
1471 * hold ->file_lock.
1472 */
1473 spin_lock(&files->file_lock);
1474 file = fcheck_files(files, fd);
1475 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001476 if (path) {
1477 *path = file->f_path;
1478 path_get(&file->f_path);
1479 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001480 if (info)
1481 snprintf(info, PROC_FDINFO_MAX,
1482 "pos:\t%lli\n"
1483 "flags:\t0%o\n",
1484 (long long) file->f_pos,
1485 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001486 spin_unlock(&files->file_lock);
1487 put_files_struct(files);
1488 return 0;
1489 }
1490 spin_unlock(&files->file_lock);
1491 put_files_struct(files);
1492 }
1493 return -ENOENT;
1494}
1495
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001496static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001497{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001498 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001499}
1500
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001501static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1502{
1503 struct inode *inode = dentry->d_inode;
1504 struct task_struct *task = get_proc_task(inode);
1505 int fd = proc_fd(inode);
1506 struct files_struct *files;
1507
1508 if (task) {
1509 files = get_files_struct(task);
1510 if (files) {
1511 rcu_read_lock();
1512 if (fcheck_files(files, fd)) {
1513 rcu_read_unlock();
1514 put_files_struct(files);
1515 if (task_dumpable(task)) {
1516 inode->i_uid = task->euid;
1517 inode->i_gid = task->egid;
1518 } else {
1519 inode->i_uid = 0;
1520 inode->i_gid = 0;
1521 }
1522 inode->i_mode &= ~(S_ISUID | S_ISGID);
1523 security_task_to_inode(task, inode);
1524 put_task_struct(task);
1525 return 1;
1526 }
1527 rcu_read_unlock();
1528 put_files_struct(files);
1529 }
1530 put_task_struct(task);
1531 }
1532 d_drop(dentry);
1533 return 0;
1534}
1535
1536static struct dentry_operations tid_fd_dentry_operations =
1537{
1538 .d_revalidate = tid_fd_revalidate,
1539 .d_delete = pid_delete_dentry,
1540};
1541
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001542static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001543 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001544{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001545 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001546 struct file *file;
1547 struct files_struct *files;
1548 struct inode *inode;
1549 struct proc_inode *ei;
1550 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001551
Eric W. Biederman61a28782006-10-02 02:18:49 -07001552 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001553 if (!inode)
1554 goto out;
1555 ei = PROC_I(inode);
1556 ei->fd = fd;
1557 files = get_files_struct(task);
1558 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001559 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001560 inode->i_mode = S_IFLNK;
1561
1562 /*
1563 * We are not taking a ref to the file structure, so we must
1564 * hold ->file_lock.
1565 */
1566 spin_lock(&files->file_lock);
1567 file = fcheck_files(files, fd);
1568 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001569 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001570 if (file->f_mode & 1)
1571 inode->i_mode |= S_IRUSR | S_IXUSR;
1572 if (file->f_mode & 2)
1573 inode->i_mode |= S_IWUSR | S_IXUSR;
1574 spin_unlock(&files->file_lock);
1575 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001576
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001577 inode->i_op = &proc_pid_link_inode_operations;
1578 inode->i_size = 64;
1579 ei->op.proc_get_link = proc_fd_link;
1580 dentry->d_op = &tid_fd_dentry_operations;
1581 d_add(dentry, inode);
1582 /* Close the race of the process dying before we return the dentry */
1583 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001584 error = NULL;
1585
1586 out:
1587 return error;
1588out_unlock:
1589 spin_unlock(&files->file_lock);
1590 put_files_struct(files);
1591out_iput:
1592 iput(inode);
1593 goto out;
1594}
1595
Miklos Szeredi27932742007-05-08 00:26:17 -07001596static struct dentry *proc_lookupfd_common(struct inode *dir,
1597 struct dentry *dentry,
1598 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001599{
1600 struct task_struct *task = get_proc_task(dir);
1601 unsigned fd = name_to_int(dentry);
1602 struct dentry *result = ERR_PTR(-ENOENT);
1603
1604 if (!task)
1605 goto out_no_task;
1606 if (fd == ~0U)
1607 goto out;
1608
Miklos Szeredi27932742007-05-08 00:26:17 -07001609 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001610out:
1611 put_task_struct(task);
1612out_no_task:
1613 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001614}
1615
Miklos Szeredi27932742007-05-08 00:26:17 -07001616static int proc_readfd_common(struct file * filp, void * dirent,
1617 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001619 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001620 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001621 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001622 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct files_struct * files;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001625 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001628 if (!p)
1629 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 fd = filp->f_pos;
1633 switch (fd) {
1634 case 0:
1635 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1636 goto out;
1637 filp->f_pos++;
1638 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001639 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1641 goto out;
1642 filp->f_pos++;
1643 default:
1644 files = get_files_struct(p);
1645 if (!files)
1646 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001647 rcu_read_lock();
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001648 fdt = files_fdtable(files);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 for (fd = filp->f_pos-2;
Dipankar Sarmabadf1662005-09-09 13:04:10 -07001650 fd < fdt->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001652 char name[PROC_NUMBUF];
1653 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 if (!fcheck_files(files, fd))
1656 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001657 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Miklos Szeredi27932742007-05-08 00:26:17 -07001659 len = snprintf(name, sizeof(name), "%d", fd);
1660 if (proc_fill_cache(filp, dirent, filldir,
1661 name, len, instantiate,
1662 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001663 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 break;
1665 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001666 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001668 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 put_files_struct(files);
1670 }
1671out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001672 put_task_struct(p);
1673out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return retval;
1675}
1676
Miklos Szeredi27932742007-05-08 00:26:17 -07001677static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1678 struct nameidata *nd)
1679{
1680 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1681}
1682
1683static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1684{
1685 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1686}
1687
1688static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1689 size_t len, loff_t *ppos)
1690{
1691 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001692 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001693 if (!err)
1694 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1695 return err;
1696}
1697
1698static const struct file_operations proc_fdinfo_file_operations = {
1699 .open = nonseekable_open,
1700 .read = proc_fdinfo_read,
1701};
1702
Arjan van de Ven00977a52007-02-12 00:55:34 -08001703static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 .read = generic_read_dir,
1705 .readdir = proc_readfd,
1706};
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001709 * /proc/pid/fd needs a special permission handler so that a process can still
1710 * access /proc/self/fd after it has executed a setuid().
1711 */
1712static int proc_fd_permission(struct inode *inode, int mask,
1713 struct nameidata *nd)
1714{
1715 int rv;
1716
1717 rv = generic_permission(inode, mask, NULL);
1718 if (rv == 0)
1719 return 0;
1720 if (task_pid(current) == proc_pid(inode))
1721 rv = 0;
1722 return rv;
1723}
1724
1725/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 * proc directories can do almost nothing..
1727 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001728static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001730 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001731 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732};
1733
Miklos Szeredi27932742007-05-08 00:26:17 -07001734static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1735 struct dentry *dentry, struct task_struct *task, const void *ptr)
1736{
1737 unsigned fd = *(unsigned *)ptr;
1738 struct inode *inode;
1739 struct proc_inode *ei;
1740 struct dentry *error = ERR_PTR(-ENOENT);
1741
1742 inode = proc_pid_make_inode(dir->i_sb, task);
1743 if (!inode)
1744 goto out;
1745 ei = PROC_I(inode);
1746 ei->fd = fd;
1747 inode->i_mode = S_IFREG | S_IRUSR;
1748 inode->i_fop = &proc_fdinfo_file_operations;
1749 dentry->d_op = &tid_fd_dentry_operations;
1750 d_add(dentry, inode);
1751 /* Close the race of the process dying before we return the dentry */
1752 if (tid_fd_revalidate(dentry, NULL))
1753 error = NULL;
1754
1755 out:
1756 return error;
1757}
1758
1759static struct dentry *proc_lookupfdinfo(struct inode *dir,
1760 struct dentry *dentry,
1761 struct nameidata *nd)
1762{
1763 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1764}
1765
1766static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1767{
1768 return proc_readfd_common(filp, dirent, filldir,
1769 proc_fdinfo_instantiate);
1770}
1771
1772static const struct file_operations proc_fdinfo_operations = {
1773 .read = generic_read_dir,
1774 .readdir = proc_readfdinfo,
1775};
1776
1777/*
1778 * proc directories can do almost nothing..
1779 */
1780static const struct inode_operations proc_fdinfo_inode_operations = {
1781 .lookup = proc_lookupfdinfo,
1782 .setattr = proc_setattr,
1783};
1784
1785
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001786static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001787 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001788{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001789 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001790 struct inode *inode;
1791 struct proc_inode *ei;
1792 struct dentry *error = ERR_PTR(-EINVAL);
1793
Eric W. Biederman61a28782006-10-02 02:18:49 -07001794 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001795 if (!inode)
1796 goto out;
1797
1798 ei = PROC_I(inode);
1799 inode->i_mode = p->mode;
1800 if (S_ISDIR(inode->i_mode))
1801 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1802 if (p->iop)
1803 inode->i_op = p->iop;
1804 if (p->fop)
1805 inode->i_fop = p->fop;
1806 ei->op = p->op;
1807 dentry->d_op = &pid_dentry_operations;
1808 d_add(dentry, inode);
1809 /* Close the race of the process dying before we return the dentry */
1810 if (pid_revalidate(dentry, NULL))
1811 error = NULL;
1812out:
1813 return error;
1814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816static struct dentry *proc_pident_lookup(struct inode *dir,
1817 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001818 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001819 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
1821 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001822 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001823 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001824 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001826 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 inode = NULL;
1828
Eric W. Biederman99f89552006-06-26 00:25:55 -07001829 if (!task)
1830 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001832 /*
1833 * Yes, it does not scale. And it should not. Don't add
1834 * new entries into /proc/<tgid>/ without very good reasons.
1835 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001836 last = &ents[nents - 1];
1837 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (p->len != dentry->d_name.len)
1839 continue;
1840 if (!memcmp(dentry->d_name.name, p->name, p->len))
1841 break;
1842 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001843 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto out;
1845
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001846 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001848 put_task_struct(task);
1849out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001850 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Eric Dumazetc5141e62007-05-08 00:26:15 -07001853static int proc_pident_fill_cache(struct file *filp, void *dirent,
1854 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001855{
1856 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1857 proc_pident_instantiate, task, p);
1858}
1859
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001860static int proc_pident_readdir(struct file *filp,
1861 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001862 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001863{
1864 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001865 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001866 struct inode *inode = dentry->d_inode;
1867 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001868 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001869 ino_t ino;
1870 int ret;
1871
1872 ret = -ENOENT;
1873 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001874 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001875
1876 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001877 i = filp->f_pos;
1878 switch (i) {
1879 case 0:
1880 ino = inode->i_ino;
1881 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1882 goto out;
1883 i++;
1884 filp->f_pos++;
1885 /* fall through */
1886 case 1:
1887 ino = parent_ino(dentry);
1888 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1889 goto out;
1890 i++;
1891 filp->f_pos++;
1892 /* fall through */
1893 default:
1894 i -= 2;
1895 if (i >= nents) {
1896 ret = 1;
1897 goto out;
1898 }
1899 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001900 last = &ents[nents - 1];
1901 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001902 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001903 goto out;
1904 filp->f_pos++;
1905 p++;
1906 }
1907 }
1908
1909 ret = 1;
1910out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07001911 put_task_struct(task);
1912out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001913 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001917static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
1918 size_t count, loff_t *ppos)
1919{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001920 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00001921 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001922 ssize_t length;
1923 struct task_struct *task = get_proc_task(inode);
1924
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001925 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00001926 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001927
1928 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001929 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00001930 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001931 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00001932 if (length > 0)
1933 length = simple_read_from_buffer(buf, count, ppos, p, length);
1934 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001935 return length;
1936}
1937
1938static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
1939 size_t count, loff_t *ppos)
1940{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001941 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001942 char *page;
1943 ssize_t length;
1944 struct task_struct *task = get_proc_task(inode);
1945
1946 length = -ESRCH;
1947 if (!task)
1948 goto out_no_task;
1949 if (count > PAGE_SIZE)
1950 count = PAGE_SIZE;
1951
1952 /* No partial writes. */
1953 length = -EINVAL;
1954 if (*ppos != 0)
1955 goto out;
1956
1957 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07001958 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001959 if (!page)
1960 goto out;
1961
1962 length = -EFAULT;
1963 if (copy_from_user(page, buf, count))
1964 goto out_free;
1965
1966 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001967 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001968 (void*)page, count);
1969out_free:
1970 free_page((unsigned long) page);
1971out:
1972 put_task_struct(task);
1973out_no_task:
1974 return length;
1975}
1976
Arjan van de Ven00977a52007-02-12 00:55:34 -08001977static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001978 .read = proc_pid_attr_read,
1979 .write = proc_pid_attr_write,
1980};
1981
Eric Dumazetc5141e62007-05-08 00:26:15 -07001982static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07001983 REG("current", S_IRUGO|S_IWUGO, pid_attr),
1984 REG("prev", S_IRUGO, pid_attr),
1985 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
1986 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
1987 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
1988 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001989};
1990
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001991static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 void * dirent, filldir_t filldir)
1993{
1994 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07001995 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Arjan van de Ven00977a52007-02-12 00:55:34 -08001998static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002000 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001};
2002
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002003static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 struct dentry *dentry, struct nameidata *nd)
2005{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002006 return proc_pident_lookup(dir, dentry,
2007 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}
2009
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002010static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002011 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002012 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002013 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014};
2015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016#endif
2017
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002018#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2019static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2020 size_t count, loff_t *ppos)
2021{
2022 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2023 struct mm_struct *mm;
2024 char buffer[PROC_NUMBUF];
2025 size_t len;
2026 int ret;
2027
2028 if (!task)
2029 return -ESRCH;
2030
2031 ret = 0;
2032 mm = get_task_mm(task);
2033 if (mm) {
2034 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2035 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2036 MMF_DUMP_FILTER_SHIFT));
2037 mmput(mm);
2038 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2039 }
2040
2041 put_task_struct(task);
2042
2043 return ret;
2044}
2045
2046static ssize_t proc_coredump_filter_write(struct file *file,
2047 const char __user *buf,
2048 size_t count,
2049 loff_t *ppos)
2050{
2051 struct task_struct *task;
2052 struct mm_struct *mm;
2053 char buffer[PROC_NUMBUF], *end;
2054 unsigned int val;
2055 int ret;
2056 int i;
2057 unsigned long mask;
2058
2059 ret = -EFAULT;
2060 memset(buffer, 0, sizeof(buffer));
2061 if (count > sizeof(buffer) - 1)
2062 count = sizeof(buffer) - 1;
2063 if (copy_from_user(buffer, buf, count))
2064 goto out_no_task;
2065
2066 ret = -EINVAL;
2067 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2068 if (*end == '\n')
2069 end++;
2070 if (end - buffer == 0)
2071 goto out_no_task;
2072
2073 ret = -ESRCH;
2074 task = get_proc_task(file->f_dentry->d_inode);
2075 if (!task)
2076 goto out_no_task;
2077
2078 ret = end - buffer;
2079 mm = get_task_mm(task);
2080 if (!mm)
2081 goto out_no_mm;
2082
2083 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2084 if (val & mask)
2085 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2086 else
2087 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2088 }
2089
2090 mmput(mm);
2091 out_no_mm:
2092 put_task_struct(task);
2093 out_no_task:
2094 return ret;
2095}
2096
2097static const struct file_operations proc_coredump_filter_operations = {
2098 .read = proc_coredump_filter_read,
2099 .write = proc_coredump_filter_write,
2100};
2101#endif
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103/*
2104 * /proc/self:
2105 */
2106static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2107 int buflen)
2108{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002109 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002110 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002111 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002112 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002113 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002114 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return vfs_readlink(dentry,buffer,buflen,tmp);
2116}
2117
Al Viro008b1502005-08-20 00:17:39 +01002118static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002120 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002121 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002122 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002123 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002124 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002125 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002126 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002129static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 .readlink = proc_self_readlink,
2131 .follow_link = proc_self_follow_link,
2132};
2133
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002134/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002135 * proc base
2136 *
2137 * These are the directory entries in the root directory of /proc
2138 * that properly belong to the /proc filesystem, as they describe
2139 * describe something that is process related.
2140 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002141static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002142 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002143 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002144};
2145
2146/*
2147 * Exceptional case: normally we are not allowed to unhash a busy
2148 * directory. In this case, however, we can do it - no aliasing problems
2149 * due to the way we treat inodes.
2150 */
2151static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2152{
2153 struct inode *inode = dentry->d_inode;
2154 struct task_struct *task = get_proc_task(inode);
2155 if (task) {
2156 put_task_struct(task);
2157 return 1;
2158 }
2159 d_drop(dentry);
2160 return 0;
2161}
2162
2163static struct dentry_operations proc_base_dentry_operations =
2164{
2165 .d_revalidate = proc_base_revalidate,
2166 .d_delete = pid_delete_dentry,
2167};
2168
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002169static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002170 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002171{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002172 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002173 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002174 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002175 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002176
2177 /* Allocate the inode */
2178 error = ERR_PTR(-ENOMEM);
2179 inode = new_inode(dir->i_sb);
2180 if (!inode)
2181 goto out;
2182
2183 /* Initialize the inode */
2184 ei = PROC_I(inode);
2185 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002186
2187 /*
2188 * grab the reference to the task.
2189 */
Oleg Nesterov1a657f72006-10-02 02:18:59 -07002190 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002191 if (!ei->pid)
2192 goto out_iput;
2193
2194 inode->i_uid = 0;
2195 inode->i_gid = 0;
2196 inode->i_mode = p->mode;
2197 if (S_ISDIR(inode->i_mode))
2198 inode->i_nlink = 2;
2199 if (S_ISLNK(inode->i_mode))
2200 inode->i_size = 64;
2201 if (p->iop)
2202 inode->i_op = p->iop;
2203 if (p->fop)
2204 inode->i_fop = p->fop;
2205 ei->op = p->op;
2206 dentry->d_op = &proc_base_dentry_operations;
2207 d_add(dentry, inode);
2208 error = NULL;
2209out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002210 return error;
2211out_iput:
2212 iput(inode);
2213 goto out;
2214}
2215
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002216static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2217{
2218 struct dentry *error;
2219 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002220 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002221
2222 error = ERR_PTR(-ENOENT);
2223
2224 if (!task)
2225 goto out_no_task;
2226
2227 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002228 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2229 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002230 if (p->len != dentry->d_name.len)
2231 continue;
2232 if (!memcmp(dentry->d_name.name, p->name, p->len))
2233 break;
2234 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002235 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002236 goto out;
2237
2238 error = proc_base_instantiate(dir, dentry, task, p);
2239
2240out:
2241 put_task_struct(task);
2242out_no_task:
2243 return error;
2244}
2245
Eric Dumazetc5141e62007-05-08 00:26:15 -07002246static int proc_base_fill_cache(struct file *filp, void *dirent,
2247 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002248{
2249 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2250 proc_base_instantiate, task, p);
2251}
2252
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002253#ifdef CONFIG_TASK_IO_ACCOUNTING
2254static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2255{
2256 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002257#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002258 "rchar: %llu\n"
2259 "wchar: %llu\n"
2260 "syscr: %llu\n"
2261 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002262#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002263 "read_bytes: %llu\n"
2264 "write_bytes: %llu\n"
2265 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002266#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002267 (unsigned long long)task->rchar,
2268 (unsigned long long)task->wchar,
2269 (unsigned long long)task->syscr,
2270 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002271#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002272 (unsigned long long)task->ioac.read_bytes,
2273 (unsigned long long)task->ioac.write_bytes,
2274 (unsigned long long)task->ioac.cancelled_write_bytes);
2275}
2276#endif
2277
Eric W. Biederman801199c2006-10-02 02:18:48 -07002278/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002279 * Thread groups
2280 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002281static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002282static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002283
Eric Dumazetc5141e62007-05-08 00:26:15 -07002284static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002285 DIR("task", S_IRUGO|S_IXUGO, task),
2286 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002287 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002288 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002289 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002290 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002291 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002292#ifdef CONFIG_SCHED_DEBUG
2293 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2294#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002295 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002296 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002297 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002298 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002299#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002300 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002301#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002302 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002303 LNK("cwd", cwd),
2304 LNK("root", root),
2305 LNK("exe", exe),
2306 REG("mounts", S_IRUGO, mounts),
2307 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002308#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002309 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002310 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002311 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002312#endif
2313#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002314 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002315#endif
2316#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002317 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002318#endif
2319#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002320 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002321#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002322#ifdef CONFIG_LATENCYTOP
2323 REG("latency", S_IRUGO, lstats),
2324#endif
Paul Menage8793d852007-10-18 23:39:39 -07002325#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002326 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002327#endif
Paul Menagea4243162007-10-18 23:39:35 -07002328#ifdef CONFIG_CGROUPS
2329 REG("cgroup", S_IRUGO, cgroup),
2330#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002331 INF("oom_score", S_IRUGO, oom_score),
2332 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002333#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002334 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002335#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002336#ifdef CONFIG_FAULT_INJECTION
2337 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2338#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002339#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2340 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2341#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002342#ifdef CONFIG_TASK_IO_ACCOUNTING
2343 INF("io", S_IRUGO, pid_io_accounting),
2344#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002345};
2346
2347static int proc_tgid_base_readdir(struct file * filp,
2348 void * dirent, filldir_t filldir)
2349{
2350 return proc_pident_readdir(filp,dirent,filldir,
2351 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2352}
2353
Arjan van de Ven00977a52007-02-12 00:55:34 -08002354static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002355 .read = generic_read_dir,
2356 .readdir = proc_tgid_base_readdir,
2357};
2358
2359static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002360 return proc_pident_lookup(dir, dentry,
2361 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002362}
2363
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002364static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002365 .lookup = proc_tgid_base_lookup,
2366 .getattr = pid_getattr,
2367 .setattr = proc_setattr,
2368};
2369
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002370static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002372 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002373 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002374 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Eric W. Biederman48e64842006-06-26 00:25:48 -07002376 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002377 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2378 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002379 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002380 if (!(current->flags & PF_EXITING))
2381 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002382 d_drop(dentry);
2383 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002386 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002387 goto out;
2388
2389 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002390 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2391 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002392 if (!leader)
2393 goto out;
2394
2395 name.name = "task";
2396 name.len = strlen(name.name);
2397 dir = d_hash_and_lookup(leader, &name);
2398 if (!dir)
2399 goto out_put_leader;
2400
2401 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002402 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002403 dentry = d_hash_and_lookup(dir, &name);
2404 if (dentry) {
2405 shrink_dcache_parent(dentry);
2406 d_drop(dentry);
2407 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002409
2410 dput(dir);
2411out_put_leader:
2412 dput(leader);
2413out:
2414 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415}
2416
Randy Dunlap0895e912007-10-21 21:00:10 -07002417/**
2418 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2419 * @task: task that should be flushed.
2420 *
2421 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002422 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002423 * in. This call is supposed to do all of this job.
2424 *
2425 * Looks in the dcache for
2426 * /proc/@pid
2427 * /proc/@tgid/task/@pid
2428 * if either directory is present flushes it and all of it'ts children
2429 * from the dcache.
2430 *
2431 * It is safe and reasonable to cache /proc entries for a task until
2432 * that task exits. After that they just clog up the dcache with
2433 * useless entries, possibly causing useful dcache entries to be
2434 * flushed instead. This routine is proved to flush those useless
2435 * dcache entries at process exit time.
2436 *
2437 * NOTE: This routine is just an optimization so it does not guarantee
2438 * that no dcache entries will exist at process exit time it
2439 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002440 */
2441
2442void proc_flush_task(struct task_struct *task)
2443{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002444 int i;
2445 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002446 struct upid *upid;
2447
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002448 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002449 if (thread_group_leader(task))
2450 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002451
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002452 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002453 upid = &pid->numbers[i];
2454 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002455 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002456 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002457
2458 upid = &pid->numbers[pid->level];
2459 if (upid->nr == 1)
2460 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002461}
2462
Adrian Bunk9711ef92006-12-06 20:38:31 -08002463static struct dentry *proc_pid_instantiate(struct inode *dir,
2464 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002465 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002466{
2467 struct dentry *error = ERR_PTR(-ENOENT);
2468 struct inode *inode;
2469
Eric W. Biederman61a28782006-10-02 02:18:49 -07002470 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002471 if (!inode)
2472 goto out;
2473
2474 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2475 inode->i_op = &proc_tgid_base_inode_operations;
2476 inode->i_fop = &proc_tgid_base_operations;
2477 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002478 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002479#ifdef CONFIG_SECURITY
2480 inode->i_nlink += 1;
2481#endif
2482
2483 dentry->d_op = &pid_dentry_operations;
2484
2485 d_add(dentry, inode);
2486 /* Close the race of the process dying before we return the dentry */
2487 if (pid_revalidate(dentry, NULL))
2488 error = NULL;
2489out:
2490 return error;
2491}
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2494{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002495 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002498 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Eric W. Biederman801199c2006-10-02 02:18:48 -07002500 result = proc_base_lookup(dir, dentry);
2501 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2502 goto out;
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 tgid = name_to_int(dentry);
2505 if (tgid == ~0U)
2506 goto out;
2507
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002508 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002509 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002510 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (task)
2512 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002513 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (!task)
2515 goto out;
2516
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002517 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002518 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002520 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002524 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002527struct tgid_iter {
2528 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002529 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002530};
2531static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2532{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002533 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002535 if (iter.task)
2536 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002537 rcu_read_lock();
2538retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002539 iter.task = NULL;
2540 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002541 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002542 iter.tgid = pid_nr_ns(pid, ns);
2543 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002544 /* What we to know is if the pid we have find is the
2545 * pid of a thread_group_leader. Testing for task
2546 * being a thread_group_leader is the obvious thing
2547 * todo but there is a window when it fails, due to
2548 * the pid transfer logic in de_thread.
2549 *
2550 * So we perform the straight forward test of seeing
2551 * if the pid we have found is the pid of a thread
2552 * group leader, and don't worry if the task we have
2553 * found doesn't happen to be a thread group leader.
2554 * As we don't care in the case of readdir.
2555 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002556 if (!iter.task || !has_group_leader_pid(iter.task)) {
2557 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002558 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002559 }
2560 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002562 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002563 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002566#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Eric W. Biederman61a28782006-10-02 02:18:49 -07002568static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002569 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002570{
2571 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002572 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002573 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002574 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002575}
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577/* for the /proc/ directory itself, after non-process stuff has been done */
2578int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2579{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002581 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002582 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002583 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Eric W. Biederman61a28782006-10-02 02:18:49 -07002585 if (!reaper)
2586 goto out_no_task;
2587
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002588 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002589 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002590 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
2593
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002594 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002595 iter.task = NULL;
2596 iter.tgid = filp->f_pos - TGID_OFFSET;
2597 for (iter = next_tgid(ns, iter);
2598 iter.task;
2599 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2600 filp->f_pos = iter.tgid + TGID_OFFSET;
2601 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2602 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002603 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002606 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2607out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002608 put_task_struct(reaper);
2609out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 return 0;
2611}
2612
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002613/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002614 * Tasks
2615 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002616static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002617 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002618 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002619 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002620 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002621 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002622 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002623#ifdef CONFIG_SCHED_DEBUG
2624 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2625#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002626 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002627 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002628 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002629 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002630#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002631 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002632#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002633 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002634 LNK("cwd", cwd),
2635 LNK("root", root),
2636 LNK("exe", exe),
2637 REG("mounts", S_IRUGO, mounts),
Matt Mackall1e883282008-02-04 22:29:07 -08002638#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002639 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002640 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002641 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002642#endif
2643#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002644 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002645#endif
2646#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002647 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002648#endif
2649#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002650 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002651#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002652#ifdef CONFIG_LATENCYTOP
2653 REG("latency", S_IRUGO, lstats),
2654#endif
Paul Menage8793d852007-10-18 23:39:39 -07002655#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002656 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002657#endif
Paul Menagea4243162007-10-18 23:39:35 -07002658#ifdef CONFIG_CGROUPS
2659 REG("cgroup", S_IRUGO, cgroup),
2660#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002661 INF("oom_score", S_IRUGO, oom_score),
2662 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002663#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002664 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002665#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002666#ifdef CONFIG_FAULT_INJECTION
2667 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2668#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002669};
2670
2671static int proc_tid_base_readdir(struct file * filp,
2672 void * dirent, filldir_t filldir)
2673{
2674 return proc_pident_readdir(filp,dirent,filldir,
2675 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2676}
2677
2678static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002679 return proc_pident_lookup(dir, dentry,
2680 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002681}
2682
Arjan van de Ven00977a52007-02-12 00:55:34 -08002683static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002684 .read = generic_read_dir,
2685 .readdir = proc_tid_base_readdir,
2686};
2687
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002688static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002689 .lookup = proc_tid_base_lookup,
2690 .getattr = pid_getattr,
2691 .setattr = proc_setattr,
2692};
2693
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002694static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002695 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002696{
2697 struct dentry *error = ERR_PTR(-ENOENT);
2698 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002699 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002700
2701 if (!inode)
2702 goto out;
2703 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2704 inode->i_op = &proc_tid_base_inode_operations;
2705 inode->i_fop = &proc_tid_base_operations;
2706 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002707 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002708#ifdef CONFIG_SECURITY
2709 inode->i_nlink += 1;
2710#endif
2711
2712 dentry->d_op = &pid_dentry_operations;
2713
2714 d_add(dentry, inode);
2715 /* Close the race of the process dying before we return the dentry */
2716 if (pid_revalidate(dentry, NULL))
2717 error = NULL;
2718out:
2719 return error;
2720}
2721
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002722static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2723{
2724 struct dentry *result = ERR_PTR(-ENOENT);
2725 struct task_struct *task;
2726 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002727 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002728 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002729
2730 if (!leader)
2731 goto out_no_task;
2732
2733 tid = name_to_int(dentry);
2734 if (tid == ~0U)
2735 goto out;
2736
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002737 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002738 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002739 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002740 if (task)
2741 get_task_struct(task);
2742 rcu_read_unlock();
2743 if (!task)
2744 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002745 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002746 goto out_drop_task;
2747
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002748 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002749out_drop_task:
2750 put_task_struct(task);
2751out:
2752 put_task_struct(leader);
2753out_no_task:
2754 return result;
2755}
2756
2757/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002758 * Find the first tid of a thread group to return to user space.
2759 *
2760 * Usually this is just the thread group leader, but if the users
2761 * buffer was too small or there was a seek into the middle of the
2762 * directory we have more work todo.
2763 *
2764 * In the case of a short read we start with find_task_by_pid.
2765 *
2766 * In the case of a seek we start with the leader and walk nr
2767 * threads past it.
2768 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002769static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002770 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002771{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002772 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002773
Eric W. Biedermancc288732006-06-26 00:26:01 -07002774 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002775 /* Attempt to start with the pid of a thread */
2776 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002777 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002778 if (pos && (pos->group_leader == leader))
2779 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002780 }
2781
2782 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002783 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002784 if (nr && nr >= get_nr_threads(leader))
2785 goto out;
2786
2787 /* If we haven't found our starting place yet start
2788 * with the leader and walk nr threads forward.
2789 */
2790 for (pos = leader; nr > 0; --nr) {
2791 pos = next_thread(pos);
2792 if (pos == leader) {
2793 pos = NULL;
2794 goto out;
2795 }
2796 }
2797found:
2798 get_task_struct(pos);
2799out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002800 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002801 return pos;
2802}
2803
2804/*
2805 * Find the next thread in the thread list.
2806 * Return NULL if there is an error or no next thread.
2807 *
2808 * The reference to the input task_struct is released.
2809 */
2810static struct task_struct *next_tid(struct task_struct *start)
2811{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002812 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002813 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002814 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002815 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002816 if (thread_group_leader(pos))
2817 pos = NULL;
2818 else
2819 get_task_struct(pos);
2820 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002821 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002822 put_task_struct(start);
2823 return pos;
2824}
2825
Eric W. Biederman61a28782006-10-02 02:18:49 -07002826static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2827 struct task_struct *task, int tid)
2828{
2829 char name[PROC_NUMBUF];
2830 int len = snprintf(name, sizeof(name), "%d", tid);
2831 return proc_fill_cache(filp, dirent, filldir, name, len,
2832 proc_task_instantiate, task, NULL);
2833}
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835/* for the /proc/TGID/task/ directories */
2836static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2837{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002838 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002840 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002841 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 int retval = -ENOENT;
2843 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002844 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002846 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002848 task = get_proc_task(inode);
2849 if (!task)
2850 goto out_no_task;
2851 rcu_read_lock();
2852 if (pid_alive(task)) {
2853 leader = task->group_leader;
2854 get_task_struct(leader);
2855 }
2856 rcu_read_unlock();
2857 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002858 if (!leader)
2859 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 retval = 0;
2861
2862 switch (pos) {
2863 case 0:
2864 ino = inode->i_ino;
2865 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2866 goto out;
2867 pos++;
2868 /* fall through */
2869 case 1:
2870 ino = parent_ino(dentry);
2871 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2872 goto out;
2873 pos++;
2874 /* fall through */
2875 }
2876
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002877 /* f_version caches the tgid value that the last readdir call couldn't
2878 * return. lseek aka telldir automagically resets f_version to 0.
2879 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002880 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002881 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002882 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002883 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002884 task;
2885 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002886 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002887 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002888 /* returning this tgid failed, save it as the first
2889 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002890 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002891 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895out:
2896 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002897 put_task_struct(leader);
2898out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 return retval;
2900}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002901
2902static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
2903{
2904 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002905 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002906 generic_fillattr(inode, stat);
2907
Eric W. Biederman99f89552006-06-26 00:25:55 -07002908 if (p) {
2909 rcu_read_lock();
2910 stat->nlink += get_nr_threads(p);
2911 rcu_read_unlock();
2912 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07002913 }
2914
2915 return 0;
2916}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002917
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002918static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002919 .lookup = proc_task_lookup,
2920 .getattr = proc_task_getattr,
2921 .setattr = proc_setattr,
2922};
2923
Arjan van de Ven00977a52007-02-12 00:55:34 -08002924static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002925 .read = generic_read_dir,
2926 .readdir = proc_task_readdir,
2927};