blob: fcf02f2deeba398f790a4657574cfcbed9b55bc9 [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
Roland McGrath638fa2022008-04-29 01:01:38 -0700198/*
199 * Return zero if current may access user memory in @task, -error if not.
200 */
201static int check_mem_permission(struct task_struct *task)
202{
203 /*
204 * A task can always look at itself, in case it chooses
205 * to use system calls instead of load instructions.
206 */
207 if (task == current)
208 return 0;
209
210 /*
211 * If current is actively ptrace'ing, and would also be
212 * permitted to freshly attach with ptrace now, permit it.
213 */
214 if (task->parent == current && (task->ptrace & PT_PTRACED) &&
215 task_is_stopped_or_traced(task) &&
216 ptrace_may_attach(task))
217 return 0;
218
219 /*
220 * Noone else is allowed.
221 */
222 return -EPERM;
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Al Viro831830b2008-01-02 14:09:57 +0000225struct mm_struct *mm_for_maps(struct task_struct *task)
226{
227 struct mm_struct *mm = get_task_mm(task);
228 if (!mm)
229 return NULL;
230 down_read(&mm->mmap_sem);
231 task_lock(task);
232 if (task->mm != mm)
233 goto out;
234 if (task->mm != current->mm && __ptrace_may_attach(task) < 0)
235 goto out;
236 task_unlock(task);
237 return mm;
238out:
239 task_unlock(task);
240 up_read(&mm->mmap_sem);
241 mmput(mm);
242 return NULL;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static int proc_pid_cmdline(struct task_struct *task, char * buffer)
246{
247 int res = 0;
248 unsigned int len;
249 struct mm_struct *mm = get_task_mm(task);
250 if (!mm)
251 goto out;
252 if (!mm->arg_end)
253 goto out_mm; /* Shh! No looking before we're done */
254
255 len = mm->arg_end - mm->arg_start;
256
257 if (len > PAGE_SIZE)
258 len = PAGE_SIZE;
259
260 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
261
262 // If the nul at the end of args has been overwritten, then
263 // assume application is using setproctitle(3).
264 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
265 len = strnlen(buffer, res);
266 if (len < res) {
267 res = len;
268 } else {
269 len = mm->env_end - mm->env_start;
270 if (len > PAGE_SIZE - res)
271 len = PAGE_SIZE - res;
272 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
273 res = strnlen(buffer, res);
274 }
275 }
276out_mm:
277 mmput(mm);
278out:
279 return res;
280}
281
282static int proc_pid_auxv(struct task_struct *task, char *buffer)
283{
284 int res = 0;
285 struct mm_struct *mm = get_task_mm(task);
286 if (mm) {
287 unsigned int nwords = 0;
288 do
289 nwords += 2;
290 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
291 res = nwords * sizeof(mm->saved_auxv[0]);
292 if (res > PAGE_SIZE)
293 res = PAGE_SIZE;
294 memcpy(buffer, mm->saved_auxv, res);
295 mmput(mm);
296 }
297 return res;
298}
299
300
301#ifdef CONFIG_KALLSYMS
302/*
303 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
304 * Returns the resolved symbol. If that fails, simply return the address.
305 */
306static int proc_pid_wchan(struct task_struct *task, char *buffer)
307{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700308 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700309 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 wchan = get_wchan(task);
312
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700313 if (lookup_symbol_name(wchan, symname) < 0)
314 return sprintf(buffer, "%lu", wchan);
315 else
316 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318#endif /* CONFIG_KALLSYMS */
319
320#ifdef CONFIG_SCHEDSTATS
321/*
322 * Provides /proc/PID/schedstat
323 */
324static int proc_pid_schedstat(struct task_struct *task, char *buffer)
325{
Balbir Singh172ba842007-07-09 18:52:00 +0200326 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 task->sched_info.cpu_time,
328 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200329 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331#endif
332
Arjan van de Ven97455122008-01-25 21:08:34 +0100333#ifdef CONFIG_LATENCYTOP
334static int lstats_show_proc(struct seq_file *m, void *v)
335{
336 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800337 struct inode *inode = m->private;
338 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100339
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800340 if (!task)
341 return -ESRCH;
342 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100343 for (i = 0; i < 32; i++) {
344 if (task->latency_record[i].backtrace[0]) {
345 int q;
346 seq_printf(m, "%i %li %li ",
347 task->latency_record[i].count,
348 task->latency_record[i].time,
349 task->latency_record[i].max);
350 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
351 char sym[KSYM_NAME_LEN];
352 char *c;
353 if (!task->latency_record[i].backtrace[q])
354 break;
355 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
356 break;
357 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
358 c = strchr(sym, '+');
359 if (c)
360 *c = 0;
361 seq_printf(m, "%s ", sym);
362 }
363 seq_printf(m, "\n");
364 }
365
366 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800367 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100368 return 0;
369}
370
371static int lstats_open(struct inode *inode, struct file *file)
372{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800373 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800374}
375
Arjan van de Ven97455122008-01-25 21:08:34 +0100376static ssize_t lstats_write(struct file *file, const char __user *buf,
377 size_t count, loff_t *offs)
378{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800379 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100380
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800381 if (!task)
382 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100383 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800384 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100385
386 return count;
387}
388
389static const struct file_operations proc_lstats_operations = {
390 .open = lstats_open,
391 .read = seq_read,
392 .write = lstats_write,
393 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800394 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100395};
396
397#endif
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399/* The badness from the OOM killer */
400unsigned long badness(struct task_struct *p, unsigned long uptime);
401static int proc_oom_score(struct task_struct *task, char *buffer)
402{
403 unsigned long points;
404 struct timespec uptime;
405
406 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700407 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700409 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return sprintf(buffer, "%lu\n", points);
411}
412
Neil Hormand85f50d2007-10-18 23:40:37 -0700413struct limit_names {
414 char *name;
415 char *unit;
416};
417
418static const struct limit_names lnames[RLIM_NLIMITS] = {
419 [RLIMIT_CPU] = {"Max cpu time", "ms"},
420 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
421 [RLIMIT_DATA] = {"Max data size", "bytes"},
422 [RLIMIT_STACK] = {"Max stack size", "bytes"},
423 [RLIMIT_CORE] = {"Max core file size", "bytes"},
424 [RLIMIT_RSS] = {"Max resident set", "bytes"},
425 [RLIMIT_NPROC] = {"Max processes", "processes"},
426 [RLIMIT_NOFILE] = {"Max open files", "files"},
427 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
428 [RLIMIT_AS] = {"Max address space", "bytes"},
429 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
430 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
431 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
432 [RLIMIT_NICE] = {"Max nice priority", NULL},
433 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800434 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700435};
436
437/* Display limits for a process */
438static int proc_pid_limits(struct task_struct *task, char *buffer)
439{
440 unsigned int i;
441 int count = 0;
442 unsigned long flags;
443 char *bufptr = buffer;
444
445 struct rlimit rlim[RLIM_NLIMITS];
446
447 rcu_read_lock();
448 if (!lock_task_sighand(task,&flags)) {
449 rcu_read_unlock();
450 return 0;
451 }
452 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
453 unlock_task_sighand(task, &flags);
454 rcu_read_unlock();
455
456 /*
457 * print the file header
458 */
459 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
460 "Limit", "Soft Limit", "Hard Limit", "Units");
461
462 for (i = 0; i < RLIM_NLIMITS; i++) {
463 if (rlim[i].rlim_cur == RLIM_INFINITY)
464 count += sprintf(&bufptr[count], "%-25s %-20s ",
465 lnames[i].name, "unlimited");
466 else
467 count += sprintf(&bufptr[count], "%-25s %-20lu ",
468 lnames[i].name, rlim[i].rlim_cur);
469
470 if (rlim[i].rlim_max == RLIM_INFINITY)
471 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
472 else
473 count += sprintf(&bufptr[count], "%-20lu ",
474 rlim[i].rlim_max);
475
476 if (lnames[i].unit)
477 count += sprintf(&bufptr[count], "%-10s\n",
478 lnames[i].unit);
479 else
480 count += sprintf(&bufptr[count], "\n");
481 }
482
483 return count;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/************************************************************************/
487/* Here the fs part begins */
488/************************************************************************/
489
490/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700491static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700493 struct task_struct *task;
494 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700495 /* Allow access to a task's file descriptors if it is us or we
496 * may use ptrace attach to the process and find out that
497 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700498 */
499 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700500 if (task) {
501 allowed = ptrace_may_attach(task);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700502 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700503 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700504 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700507static int proc_setattr(struct dentry *dentry, struct iattr *attr)
508{
509 int error;
510 struct inode *inode = dentry->d_inode;
511
512 if (attr->ia_valid & ATTR_MODE)
513 return -EPERM;
514
515 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700516 if (!error)
517 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700518 return error;
519}
520
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800521static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700522 .setattr = proc_setattr,
523};
524
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100525static int mounts_open_common(struct inode *inode, struct file *file,
526 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700528 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700529 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800530 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100531 struct fs_struct *fs = NULL;
532 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500533 struct proc_mounts *p;
534 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Eric W. Biederman99f89552006-06-26 00:25:55 -0700536 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700537 rcu_read_lock();
538 nsp = task_nsproxy(task);
539 if (nsp) {
540 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800541 if (ns)
542 get_mnt_ns(ns);
543 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700544 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100545 if (ns)
546 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700547 put_task_struct(task);
548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100550 if (!ns)
551 goto err;
552 if (!fs)
553 goto err_put_ns;
554
555 read_lock(&fs->lock);
556 root = fs->root;
557 path_get(&root);
558 read_unlock(&fs->lock);
559 put_fs_struct(fs);
560
561 ret = -ENOMEM;
562 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
563 if (!p)
564 goto err_put_path;
565
566 file->private_data = &p->m;
567 ret = seq_open(file, op);
568 if (ret)
569 goto err_free;
570
571 p->m.private = p;
572 p->ns = ns;
573 p->root = root;
574 p->event = ns->event;
575
576 return 0;
577
578 err_free:
579 kfree(p);
580 err_put_path:
581 path_put(&root);
582 err_put_ns:
583 put_mnt_ns(ns);
584 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return ret;
586}
587
588static int mounts_release(struct inode *inode, struct file *file)
589{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100590 struct proc_mounts *p = file->private_data;
591 path_put(&p->root);
592 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return seq_release(inode, file);
594}
595
Al Viro5addc5d2005-11-07 17:15:49 -0500596static unsigned mounts_poll(struct file *file, poll_table *wait)
597{
598 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100599 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500600 unsigned res = 0;
601
602 poll_wait(file, &ns->poll, wait);
603
604 spin_lock(&vfsmount_lock);
605 if (p->event != ns->event) {
606 p->event = ns->event;
607 res = POLLERR;
608 }
609 spin_unlock(&vfsmount_lock);
610
611 return res;
612}
613
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100614static int mounts_open(struct inode *inode, struct file *file)
615{
616 return mounts_open_common(inode, file, &mounts_op);
617}
618
Arjan van de Ven00977a52007-02-12 00:55:34 -0800619static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 .open = mounts_open,
621 .read = seq_read,
622 .llseek = seq_lseek,
623 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500624 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625};
626
Ram Pai2d4d4862008-03-27 13:06:25 +0100627static int mountinfo_open(struct inode *inode, struct file *file)
628{
629 return mounts_open_common(inode, file, &mountinfo_op);
630}
631
632static const struct file_operations proc_mountinfo_operations = {
633 .open = mountinfo_open,
634 .read = seq_read,
635 .llseek = seq_lseek,
636 .release = mounts_release,
637 .poll = mounts_poll,
638};
639
Chuck Leverb4629fe2006-03-20 13:44:12 -0500640static int mountstats_open(struct inode *inode, struct file *file)
641{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100642 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500643}
644
Arjan van de Ven00977a52007-02-12 00:55:34 -0800645static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500646 .open = mountstats_open,
647 .read = seq_read,
648 .llseek = seq_lseek,
649 .release = mounts_release,
650};
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
653
654static ssize_t proc_info_read(struct file * file, char __user * buf,
655 size_t count, loff_t *ppos)
656{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800657 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 unsigned long page;
659 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700660 struct task_struct *task = get_proc_task(inode);
661
662 length = -ESRCH;
663 if (!task)
664 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (count > PROC_BLOCK_SIZE)
667 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700668
669 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700670 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 length = PROC_I(inode)->op.proc_read(task, (char*)page);
674
675 if (length >= 0)
676 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
677 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700678out:
679 put_task_struct(task);
680out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return length;
682}
683
Arjan van de Ven00977a52007-02-12 00:55:34 -0800684static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 .read = proc_info_read,
686};
687
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800688static int proc_single_show(struct seq_file *m, void *v)
689{
690 struct inode *inode = m->private;
691 struct pid_namespace *ns;
692 struct pid *pid;
693 struct task_struct *task;
694 int ret;
695
696 ns = inode->i_sb->s_fs_info;
697 pid = proc_pid(inode);
698 task = get_pid_task(pid, PIDTYPE_PID);
699 if (!task)
700 return -ESRCH;
701
702 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
703
704 put_task_struct(task);
705 return ret;
706}
707
708static int proc_single_open(struct inode *inode, struct file *filp)
709{
710 int ret;
711 ret = single_open(filp, proc_single_show, NULL);
712 if (!ret) {
713 struct seq_file *m = filp->private_data;
714
715 m->private = inode;
716 }
717 return ret;
718}
719
720static const struct file_operations proc_single_file_operations = {
721 .open = proc_single_open,
722 .read = seq_read,
723 .llseek = seq_lseek,
724 .release = single_release,
725};
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static int mem_open(struct inode* inode, struct file* file)
728{
729 file->private_data = (void*)((long)current->self_exec_id);
730 return 0;
731}
732
733static ssize_t mem_read(struct file * file, char __user * buf,
734 size_t count, loff_t *ppos)
735{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800736 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 char *page;
738 unsigned long src = *ppos;
739 int ret = -ESRCH;
740 struct mm_struct *mm;
741
Eric W. Biederman99f89552006-06-26 00:25:55 -0700742 if (!task)
743 goto out_no_task;
744
Roland McGrath638fa2022008-04-29 01:01:38 -0700745 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto out;
747
748 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700749 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!page)
751 goto out;
752
753 ret = 0;
754
755 mm = get_task_mm(task);
756 if (!mm)
757 goto out_free;
758
759 ret = -EIO;
760
761 if (file->private_data != (void*)((long)current->self_exec_id))
762 goto out_put;
763
764 ret = 0;
765
766 while (count > 0) {
767 int this_len, retval;
768
769 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
770 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa2022008-04-29 01:01:38 -0700771 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (!ret)
773 ret = -EIO;
774 break;
775 }
776
777 if (copy_to_user(buf, page, retval)) {
778 ret = -EFAULT;
779 break;
780 }
781
782 ret += retval;
783 src += retval;
784 buf += retval;
785 count -= retval;
786 }
787 *ppos = src;
788
789out_put:
790 mmput(mm);
791out_free:
792 free_page((unsigned long) page);
793out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700794 put_task_struct(task);
795out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return ret;
797}
798
799#define mem_write NULL
800
801#ifndef mem_write
802/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800803static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 size_t count, loff_t *ppos)
805{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700806 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800808 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned long dst = *ppos;
810
Eric W. Biederman99f89552006-06-26 00:25:55 -0700811 copied = -ESRCH;
812 if (!task)
813 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Roland McGrath638fa2022008-04-29 01:01:38 -0700815 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700816 goto out;
817
818 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700819 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700823 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 while (count > 0) {
825 int this_len, retval;
826
827 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
828 if (copy_from_user(page, buf, this_len)) {
829 copied = -EFAULT;
830 break;
831 }
832 retval = access_process_vm(task, dst, page, this_len, 1);
833 if (!retval) {
834 if (!copied)
835 copied = -EIO;
836 break;
837 }
838 copied += retval;
839 buf += retval;
840 dst += retval;
841 count -= retval;
842 }
843 *ppos = dst;
844 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700845out:
846 put_task_struct(task);
847out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return copied;
849}
850#endif
851
Matt Mackall85863e42008-02-04 22:29:04 -0800852loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
854 switch (orig) {
855 case 0:
856 file->f_pos = offset;
857 break;
858 case 1:
859 file->f_pos += offset;
860 break;
861 default:
862 return -EINVAL;
863 }
864 force_successful_syscall_return();
865 return file->f_pos;
866}
867
Arjan van de Ven00977a52007-02-12 00:55:34 -0800868static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 .llseek = mem_lseek,
870 .read = mem_read,
871 .write = mem_write,
872 .open = mem_open,
873};
874
James Pearson315e28c2007-10-16 23:30:17 -0700875static ssize_t environ_read(struct file *file, char __user *buf,
876 size_t count, loff_t *ppos)
877{
878 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
879 char *page;
880 unsigned long src = *ppos;
881 int ret = -ESRCH;
882 struct mm_struct *mm;
883
884 if (!task)
885 goto out_no_task;
886
887 if (!ptrace_may_attach(task))
888 goto out;
889
890 ret = -ENOMEM;
891 page = (char *)__get_free_page(GFP_TEMPORARY);
892 if (!page)
893 goto out;
894
895 ret = 0;
896
897 mm = get_task_mm(task);
898 if (!mm)
899 goto out_free;
900
901 while (count > 0) {
902 int this_len, retval, max_len;
903
904 this_len = mm->env_end - (mm->env_start + src);
905
906 if (this_len <= 0)
907 break;
908
909 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
910 this_len = (this_len > max_len) ? max_len : this_len;
911
912 retval = access_process_vm(task, (mm->env_start + src),
913 page, this_len, 0);
914
915 if (retval <= 0) {
916 ret = retval;
917 break;
918 }
919
920 if (copy_to_user(buf, page, retval)) {
921 ret = -EFAULT;
922 break;
923 }
924
925 ret += retval;
926 src += retval;
927 buf += retval;
928 count -= retval;
929 }
930 *ppos = src;
931
932 mmput(mm);
933out_free:
934 free_page((unsigned long) page);
935out:
936 put_task_struct(task);
937out_no_task:
938 return ret;
939}
940
941static const struct file_operations proc_environ_operations = {
942 .read = environ_read,
943};
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static ssize_t oom_adjust_read(struct file *file, char __user *buf,
946 size_t count, loff_t *ppos)
947{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800948 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700949 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700951 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Eric W. Biederman99f89552006-06-26 00:25:55 -0700953 if (!task)
954 return -ESRCH;
955 oom_adjust = task->oomkilladj;
956 put_task_struct(task);
957
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700958 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700959
960 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
963static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
964 size_t count, loff_t *ppos)
965{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700966 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700967 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 int oom_adjust;
969
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700970 memset(buffer, 0, sizeof(buffer));
971 if (count > sizeof(buffer) - 1)
972 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (copy_from_user(buffer, buf, count))
974 return -EFAULT;
975 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -0700976 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
977 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return -EINVAL;
979 if (*end == '\n')
980 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800981 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700982 if (!task)
983 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -0800984 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
985 put_task_struct(task);
986 return -EACCES;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700989 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (end - buffer == 0)
991 return -EIO;
992 return end - buffer;
993}
994
Arjan van de Ven00977a52007-02-12 00:55:34 -0800995static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 .read = oom_adjust_read,
997 .write = oom_adjust_write,
998};
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000#ifdef CONFIG_AUDITSYSCALL
1001#define TMPBUFLEN 21
1002static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1003 size_t count, loff_t *ppos)
1004{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001005 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001006 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 ssize_t length;
1008 char tmpbuf[TMPBUFLEN];
1009
Eric W. Biederman99f89552006-06-26 00:25:55 -07001010 if (!task)
1011 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001013 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001014 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1016}
1017
1018static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1019 size_t count, loff_t *ppos)
1020{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001021 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 char *page, *tmp;
1023 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 uid_t loginuid;
1025
1026 if (!capable(CAP_AUDIT_CONTROL))
1027 return -EPERM;
1028
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001029 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return -EPERM;
1031
Al Viroe0182902006-05-18 08:28:02 -04001032 if (count >= PAGE_SIZE)
1033 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (*ppos != 0) {
1036 /* No partial writes. */
1037 return -EINVAL;
1038 }
Mel Gormane12ba742007-10-16 01:25:52 -07001039 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!page)
1041 return -ENOMEM;
1042 length = -EFAULT;
1043 if (copy_from_user(page, buf, count))
1044 goto out_free_page;
1045
Al Viroe0182902006-05-18 08:28:02 -04001046 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 loginuid = simple_strtoul(page, &tmp, 10);
1048 if (tmp == page) {
1049 length = -EINVAL;
1050 goto out_free_page;
1051
1052 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001053 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (likely(length == 0))
1055 length = count;
1056
1057out_free_page:
1058 free_page((unsigned long) page);
1059 return length;
1060}
1061
Arjan van de Ven00977a52007-02-12 00:55:34 -08001062static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 .read = proc_loginuid_read,
1064 .write = proc_loginuid_write,
1065};
Eric Paris1e0bd752008-03-13 08:15:31 -04001066
1067static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1068 size_t count, loff_t *ppos)
1069{
1070 struct inode * inode = file->f_path.dentry->d_inode;
1071 struct task_struct *task = get_proc_task(inode);
1072 ssize_t length;
1073 char tmpbuf[TMPBUFLEN];
1074
1075 if (!task)
1076 return -ESRCH;
1077 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1078 audit_get_sessionid(task));
1079 put_task_struct(task);
1080 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1081}
1082
1083static const struct file_operations proc_sessionid_operations = {
1084 .read = proc_sessionid_read,
1085};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086#endif
1087
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001088#ifdef CONFIG_FAULT_INJECTION
1089static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1090 size_t count, loff_t *ppos)
1091{
1092 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1093 char buffer[PROC_NUMBUF];
1094 size_t len;
1095 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001096
1097 if (!task)
1098 return -ESRCH;
1099 make_it_fail = task->make_it_fail;
1100 put_task_struct(task);
1101
1102 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001103
1104 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001105}
1106
1107static ssize_t proc_fault_inject_write(struct file * file,
1108 const char __user * buf, size_t count, loff_t *ppos)
1109{
1110 struct task_struct *task;
1111 char buffer[PROC_NUMBUF], *end;
1112 int make_it_fail;
1113
1114 if (!capable(CAP_SYS_RESOURCE))
1115 return -EPERM;
1116 memset(buffer, 0, sizeof(buffer));
1117 if (count > sizeof(buffer) - 1)
1118 count = sizeof(buffer) - 1;
1119 if (copy_from_user(buffer, buf, count))
1120 return -EFAULT;
1121 make_it_fail = simple_strtol(buffer, &end, 0);
1122 if (*end == '\n')
1123 end++;
1124 task = get_proc_task(file->f_dentry->d_inode);
1125 if (!task)
1126 return -ESRCH;
1127 task->make_it_fail = make_it_fail;
1128 put_task_struct(task);
1129 if (end - buffer == 0)
1130 return -EIO;
1131 return end - buffer;
1132}
1133
Arjan van de Ven00977a52007-02-12 00:55:34 -08001134static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001135 .read = proc_fault_inject_read,
1136 .write = proc_fault_inject_write,
1137};
1138#endif
1139
Arjan van de Ven97455122008-01-25 21:08:34 +01001140
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001141#ifdef CONFIG_SCHED_DEBUG
1142/*
1143 * Print out various scheduling related per-task fields:
1144 */
1145static int sched_show(struct seq_file *m, void *v)
1146{
1147 struct inode *inode = m->private;
1148 struct task_struct *p;
1149
1150 WARN_ON(!inode);
1151
1152 p = get_proc_task(inode);
1153 if (!p)
1154 return -ESRCH;
1155 proc_sched_show_task(p, m);
1156
1157 put_task_struct(p);
1158
1159 return 0;
1160}
1161
1162static ssize_t
1163sched_write(struct file *file, const char __user *buf,
1164 size_t count, loff_t *offset)
1165{
1166 struct inode *inode = file->f_path.dentry->d_inode;
1167 struct task_struct *p;
1168
1169 WARN_ON(!inode);
1170
1171 p = get_proc_task(inode);
1172 if (!p)
1173 return -ESRCH;
1174 proc_sched_set_task(p);
1175
1176 put_task_struct(p);
1177
1178 return count;
1179}
1180
1181static int sched_open(struct inode *inode, struct file *filp)
1182{
1183 int ret;
1184
1185 ret = single_open(filp, sched_show, NULL);
1186 if (!ret) {
1187 struct seq_file *m = filp->private_data;
1188
1189 m->private = inode;
1190 }
1191 return ret;
1192}
1193
1194static const struct file_operations proc_pid_sched_operations = {
1195 .open = sched_open,
1196 .read = seq_read,
1197 .write = sched_write,
1198 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001199 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001200};
1201
1202#endif
1203
Matt Helsley925d1c42008-04-29 01:01:36 -07001204/*
1205 * We added or removed a vma mapping the executable. The vmas are only mapped
1206 * during exec and are not mapped with the mmap system call.
1207 * Callers must hold down_write() on the mm's mmap_sem for these
1208 */
1209void added_exe_file_vma(struct mm_struct *mm)
1210{
1211 mm->num_exe_file_vmas++;
1212}
1213
1214void removed_exe_file_vma(struct mm_struct *mm)
1215{
1216 mm->num_exe_file_vmas--;
1217 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1218 fput(mm->exe_file);
1219 mm->exe_file = NULL;
1220 }
1221
1222}
1223
1224void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1225{
1226 if (new_exe_file)
1227 get_file(new_exe_file);
1228 if (mm->exe_file)
1229 fput(mm->exe_file);
1230 mm->exe_file = new_exe_file;
1231 mm->num_exe_file_vmas = 0;
1232}
1233
1234struct file *get_mm_exe_file(struct mm_struct *mm)
1235{
1236 struct file *exe_file;
1237
1238 /* We need mmap_sem to protect against races with removal of
1239 * VM_EXECUTABLE vmas */
1240 down_read(&mm->mmap_sem);
1241 exe_file = mm->exe_file;
1242 if (exe_file)
1243 get_file(exe_file);
1244 up_read(&mm->mmap_sem);
1245 return exe_file;
1246}
1247
1248void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1249{
1250 /* It's safe to write the exe_file pointer without exe_file_lock because
1251 * this is called during fork when the task is not yet in /proc */
1252 newmm->exe_file = get_mm_exe_file(oldmm);
1253}
1254
1255static int proc_exe_link(struct inode *inode, struct path *exe_path)
1256{
1257 struct task_struct *task;
1258 struct mm_struct *mm;
1259 struct file *exe_file;
1260
1261 task = get_proc_task(inode);
1262 if (!task)
1263 return -ENOENT;
1264 mm = get_task_mm(task);
1265 put_task_struct(task);
1266 if (!mm)
1267 return -ENOENT;
1268 exe_file = get_mm_exe_file(mm);
1269 mmput(mm);
1270 if (exe_file) {
1271 *exe_path = exe_file->f_path;
1272 path_get(&exe_file->f_path);
1273 fput(exe_file);
1274 return 0;
1275 } else
1276 return -ENOENT;
1277}
1278
Al Viro008b1502005-08-20 00:17:39 +01001279static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct inode *inode = dentry->d_inode;
1282 int error = -EACCES;
1283
1284 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001285 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Eric W. Biederman778c1142006-06-26 00:25:58 -07001287 /* Are we allowed to snoop on the tasks file descriptors? */
1288 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001291 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 nd->last_type = LAST_BIND;
1293out:
Al Viro008b1502005-08-20 00:17:39 +01001294 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001297static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298{
Mel Gormane12ba742007-10-16 01:25:52 -07001299 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001300 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int len;
1302
1303 if (!tmp)
1304 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001305
Jan Blunckcf28b482008-02-14 19:38:44 -08001306 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001307 len = PTR_ERR(pathname);
1308 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001310 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 if (len > buflen)
1313 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001314 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 len = -EFAULT;
1316 out:
1317 free_page((unsigned long)tmp);
1318 return len;
1319}
1320
1321static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1322{
1323 int error = -EACCES;
1324 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001325 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Eric W. Biederman778c1142006-06-26 00:25:58 -07001327 /* Are we allowed to snoop on the tasks file descriptors? */
1328 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001331 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (error)
1333 goto out;
1334
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001335 error = do_proc_readlink(&path, buffer, buflen);
1336 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return error;
1339}
1340
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001341static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001343 .follow_link = proc_pid_follow_link,
1344 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345};
1346
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001347
1348/* building an inode */
1349
1350static int task_dumpable(struct task_struct *task)
1351{
1352 int dumpable = 0;
1353 struct mm_struct *mm;
1354
1355 task_lock(task);
1356 mm = task->mm;
1357 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001358 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001359 task_unlock(task);
1360 if(dumpable == 1)
1361 return 1;
1362 return 0;
1363}
1364
1365
Eric W. Biederman61a28782006-10-02 02:18:49 -07001366static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001367{
1368 struct inode * inode;
1369 struct proc_inode *ei;
1370
1371 /* We need a new inode */
1372
1373 inode = new_inode(sb);
1374 if (!inode)
1375 goto out;
1376
1377 /* Common stuff */
1378 ei = PROC_I(inode);
1379 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001380 inode->i_op = &proc_def_inode_operations;
1381
1382 /*
1383 * grab the reference to task.
1384 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001385 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001386 if (!ei->pid)
1387 goto out_unlock;
1388
1389 inode->i_uid = 0;
1390 inode->i_gid = 0;
1391 if (task_dumpable(task)) {
1392 inode->i_uid = task->euid;
1393 inode->i_gid = task->egid;
1394 }
1395 security_task_to_inode(task, inode);
1396
1397out:
1398 return inode;
1399
1400out_unlock:
1401 iput(inode);
1402 return NULL;
1403}
1404
1405static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1406{
1407 struct inode *inode = dentry->d_inode;
1408 struct task_struct *task;
1409 generic_fillattr(inode, stat);
1410
1411 rcu_read_lock();
1412 stat->uid = 0;
1413 stat->gid = 0;
1414 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1415 if (task) {
1416 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1417 task_dumpable(task)) {
1418 stat->uid = task->euid;
1419 stat->gid = task->egid;
1420 }
1421 }
1422 rcu_read_unlock();
1423 return 0;
1424}
1425
1426/* dentry stuff */
1427
1428/*
1429 * Exceptional case: normally we are not allowed to unhash a busy
1430 * directory. In this case, however, we can do it - no aliasing problems
1431 * due to the way we treat inodes.
1432 *
1433 * Rewrite the inode's ownerships here because the owning task may have
1434 * performed a setuid(), etc.
1435 *
1436 * Before the /proc/pid/status file was created the only way to read
1437 * the effective uid of a /process was to stat /proc/pid. Reading
1438 * /proc/pid/status is slow enough that procps and other packages
1439 * kept stating /proc/pid. To keep the rules in /proc simple I have
1440 * made this apply to all per process world readable and executable
1441 * directories.
1442 */
1443static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1444{
1445 struct inode *inode = dentry->d_inode;
1446 struct task_struct *task = get_proc_task(inode);
1447 if (task) {
1448 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1449 task_dumpable(task)) {
1450 inode->i_uid = task->euid;
1451 inode->i_gid = task->egid;
1452 } else {
1453 inode->i_uid = 0;
1454 inode->i_gid = 0;
1455 }
1456 inode->i_mode &= ~(S_ISUID | S_ISGID);
1457 security_task_to_inode(task, inode);
1458 put_task_struct(task);
1459 return 1;
1460 }
1461 d_drop(dentry);
1462 return 0;
1463}
1464
1465static int pid_delete_dentry(struct dentry * dentry)
1466{
1467 /* Is the task we represent dead?
1468 * If so, then don't put the dentry on the lru list,
1469 * kill it immediately.
1470 */
1471 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1472}
1473
1474static struct dentry_operations pid_dentry_operations =
1475{
1476 .d_revalidate = pid_revalidate,
1477 .d_delete = pid_delete_dentry,
1478};
1479
1480/* Lookups */
1481
Eric Dumazetc5141e62007-05-08 00:26:15 -07001482typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1483 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001484
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001485/*
1486 * Fill a directory entry.
1487 *
1488 * If possible create the dcache entry and derive our inode number and
1489 * file type from dcache entry.
1490 *
1491 * Since all of the proc inode numbers are dynamically generated, the inode
1492 * numbers do not exist until the inode is cache. This means creating the
1493 * the dcache entry in readdir is necessary to keep the inode numbers
1494 * reported by readdir in sync with the inode numbers reported
1495 * by stat.
1496 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001497static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1498 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001499 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001500{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001501 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001502 struct inode *inode;
1503 struct qstr qname;
1504 ino_t ino = 0;
1505 unsigned type = DT_UNKNOWN;
1506
1507 qname.name = name;
1508 qname.len = len;
1509 qname.hash = full_name_hash(name, len);
1510
1511 child = d_lookup(dir, &qname);
1512 if (!child) {
1513 struct dentry *new;
1514 new = d_alloc(dir, &qname);
1515 if (new) {
1516 child = instantiate(dir->d_inode, new, task, ptr);
1517 if (child)
1518 dput(new);
1519 else
1520 child = new;
1521 }
1522 }
1523 if (!child || IS_ERR(child) || !child->d_inode)
1524 goto end_instantiate;
1525 inode = child->d_inode;
1526 if (inode) {
1527 ino = inode->i_ino;
1528 type = inode->i_mode >> 12;
1529 }
1530 dput(child);
1531end_instantiate:
1532 if (!ino)
1533 ino = find_inode_number(dir, &qname);
1534 if (!ino)
1535 ino = 1;
1536 return filldir(dirent, name, len, filp->f_pos, ino, type);
1537}
1538
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001539static unsigned name_to_int(struct dentry *dentry)
1540{
1541 const char *name = dentry->d_name.name;
1542 int len = dentry->d_name.len;
1543 unsigned n = 0;
1544
1545 if (len > 1 && *name == '0')
1546 goto out;
1547 while (len-- > 0) {
1548 unsigned c = *name++ - '0';
1549 if (c > 9)
1550 goto out;
1551 if (n >= (~0U-9)/10)
1552 goto out;
1553 n *= 10;
1554 n += c;
1555 }
1556 return n;
1557out:
1558 return ~0U;
1559}
1560
Miklos Szeredi27932742007-05-08 00:26:17 -07001561#define PROC_FDINFO_MAX 64
1562
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001563static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001564{
1565 struct task_struct *task = get_proc_task(inode);
1566 struct files_struct *files = NULL;
1567 struct file *file;
1568 int fd = proc_fd(inode);
1569
1570 if (task) {
1571 files = get_files_struct(task);
1572 put_task_struct(task);
1573 }
1574 if (files) {
1575 /*
1576 * We are not taking a ref to the file structure, so we must
1577 * hold ->file_lock.
1578 */
1579 spin_lock(&files->file_lock);
1580 file = fcheck_files(files, fd);
1581 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001582 if (path) {
1583 *path = file->f_path;
1584 path_get(&file->f_path);
1585 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001586 if (info)
1587 snprintf(info, PROC_FDINFO_MAX,
1588 "pos:\t%lli\n"
1589 "flags:\t0%o\n",
1590 (long long) file->f_pos,
1591 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001592 spin_unlock(&files->file_lock);
1593 put_files_struct(files);
1594 return 0;
1595 }
1596 spin_unlock(&files->file_lock);
1597 put_files_struct(files);
1598 }
1599 return -ENOENT;
1600}
1601
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001602static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001603{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001604 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001605}
1606
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001607static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1608{
1609 struct inode *inode = dentry->d_inode;
1610 struct task_struct *task = get_proc_task(inode);
1611 int fd = proc_fd(inode);
1612 struct files_struct *files;
1613
1614 if (task) {
1615 files = get_files_struct(task);
1616 if (files) {
1617 rcu_read_lock();
1618 if (fcheck_files(files, fd)) {
1619 rcu_read_unlock();
1620 put_files_struct(files);
1621 if (task_dumpable(task)) {
1622 inode->i_uid = task->euid;
1623 inode->i_gid = task->egid;
1624 } else {
1625 inode->i_uid = 0;
1626 inode->i_gid = 0;
1627 }
1628 inode->i_mode &= ~(S_ISUID | S_ISGID);
1629 security_task_to_inode(task, inode);
1630 put_task_struct(task);
1631 return 1;
1632 }
1633 rcu_read_unlock();
1634 put_files_struct(files);
1635 }
1636 put_task_struct(task);
1637 }
1638 d_drop(dentry);
1639 return 0;
1640}
1641
1642static struct dentry_operations tid_fd_dentry_operations =
1643{
1644 .d_revalidate = tid_fd_revalidate,
1645 .d_delete = pid_delete_dentry,
1646};
1647
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001648static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001649 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001650{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001651 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001652 struct file *file;
1653 struct files_struct *files;
1654 struct inode *inode;
1655 struct proc_inode *ei;
1656 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001657
Eric W. Biederman61a28782006-10-02 02:18:49 -07001658 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001659 if (!inode)
1660 goto out;
1661 ei = PROC_I(inode);
1662 ei->fd = fd;
1663 files = get_files_struct(task);
1664 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001665 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001666 inode->i_mode = S_IFLNK;
1667
1668 /*
1669 * We are not taking a ref to the file structure, so we must
1670 * hold ->file_lock.
1671 */
1672 spin_lock(&files->file_lock);
1673 file = fcheck_files(files, fd);
1674 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001675 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001676 if (file->f_mode & 1)
1677 inode->i_mode |= S_IRUSR | S_IXUSR;
1678 if (file->f_mode & 2)
1679 inode->i_mode |= S_IWUSR | S_IXUSR;
1680 spin_unlock(&files->file_lock);
1681 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001682
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001683 inode->i_op = &proc_pid_link_inode_operations;
1684 inode->i_size = 64;
1685 ei->op.proc_get_link = proc_fd_link;
1686 dentry->d_op = &tid_fd_dentry_operations;
1687 d_add(dentry, inode);
1688 /* Close the race of the process dying before we return the dentry */
1689 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001690 error = NULL;
1691
1692 out:
1693 return error;
1694out_unlock:
1695 spin_unlock(&files->file_lock);
1696 put_files_struct(files);
1697out_iput:
1698 iput(inode);
1699 goto out;
1700}
1701
Miklos Szeredi27932742007-05-08 00:26:17 -07001702static struct dentry *proc_lookupfd_common(struct inode *dir,
1703 struct dentry *dentry,
1704 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001705{
1706 struct task_struct *task = get_proc_task(dir);
1707 unsigned fd = name_to_int(dentry);
1708 struct dentry *result = ERR_PTR(-ENOENT);
1709
1710 if (!task)
1711 goto out_no_task;
1712 if (fd == ~0U)
1713 goto out;
1714
Miklos Szeredi27932742007-05-08 00:26:17 -07001715 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001716out:
1717 put_task_struct(task);
1718out_no_task:
1719 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001720}
1721
Miklos Szeredi27932742007-05-08 00:26:17 -07001722static int proc_readfd_common(struct file * filp, void * dirent,
1723 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001725 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001726 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001727 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001728 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct files_struct * files;
1731
1732 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001733 if (!p)
1734 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 fd = filp->f_pos;
1738 switch (fd) {
1739 case 0:
1740 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1741 goto out;
1742 filp->f_pos++;
1743 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001744 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1746 goto out;
1747 filp->f_pos++;
1748 default:
1749 files = get_files_struct(p);
1750 if (!files)
1751 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001752 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001754 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001756 char name[PROC_NUMBUF];
1757 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 if (!fcheck_files(files, fd))
1760 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001761 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Miklos Szeredi27932742007-05-08 00:26:17 -07001763 len = snprintf(name, sizeof(name), "%d", fd);
1764 if (proc_fill_cache(filp, dirent, filldir,
1765 name, len, instantiate,
1766 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001767 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 break;
1769 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001770 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001772 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 put_files_struct(files);
1774 }
1775out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001776 put_task_struct(p);
1777out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return retval;
1779}
1780
Miklos Szeredi27932742007-05-08 00:26:17 -07001781static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1782 struct nameidata *nd)
1783{
1784 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1785}
1786
1787static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1788{
1789 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1790}
1791
1792static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1793 size_t len, loff_t *ppos)
1794{
1795 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001796 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001797 if (!err)
1798 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1799 return err;
1800}
1801
1802static const struct file_operations proc_fdinfo_file_operations = {
1803 .open = nonseekable_open,
1804 .read = proc_fdinfo_read,
1805};
1806
Arjan van de Ven00977a52007-02-12 00:55:34 -08001807static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 .read = generic_read_dir,
1809 .readdir = proc_readfd,
1810};
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001813 * /proc/pid/fd needs a special permission handler so that a process can still
1814 * access /proc/self/fd after it has executed a setuid().
1815 */
1816static int proc_fd_permission(struct inode *inode, int mask,
1817 struct nameidata *nd)
1818{
1819 int rv;
1820
1821 rv = generic_permission(inode, mask, NULL);
1822 if (rv == 0)
1823 return 0;
1824 if (task_pid(current) == proc_pid(inode))
1825 rv = 0;
1826 return rv;
1827}
1828
1829/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 * proc directories can do almost nothing..
1831 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001832static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001834 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001835 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836};
1837
Miklos Szeredi27932742007-05-08 00:26:17 -07001838static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1839 struct dentry *dentry, struct task_struct *task, const void *ptr)
1840{
1841 unsigned fd = *(unsigned *)ptr;
1842 struct inode *inode;
1843 struct proc_inode *ei;
1844 struct dentry *error = ERR_PTR(-ENOENT);
1845
1846 inode = proc_pid_make_inode(dir->i_sb, task);
1847 if (!inode)
1848 goto out;
1849 ei = PROC_I(inode);
1850 ei->fd = fd;
1851 inode->i_mode = S_IFREG | S_IRUSR;
1852 inode->i_fop = &proc_fdinfo_file_operations;
1853 dentry->d_op = &tid_fd_dentry_operations;
1854 d_add(dentry, inode);
1855 /* Close the race of the process dying before we return the dentry */
1856 if (tid_fd_revalidate(dentry, NULL))
1857 error = NULL;
1858
1859 out:
1860 return error;
1861}
1862
1863static struct dentry *proc_lookupfdinfo(struct inode *dir,
1864 struct dentry *dentry,
1865 struct nameidata *nd)
1866{
1867 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1868}
1869
1870static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1871{
1872 return proc_readfd_common(filp, dirent, filldir,
1873 proc_fdinfo_instantiate);
1874}
1875
1876static const struct file_operations proc_fdinfo_operations = {
1877 .read = generic_read_dir,
1878 .readdir = proc_readfdinfo,
1879};
1880
1881/*
1882 * proc directories can do almost nothing..
1883 */
1884static const struct inode_operations proc_fdinfo_inode_operations = {
1885 .lookup = proc_lookupfdinfo,
1886 .setattr = proc_setattr,
1887};
1888
1889
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001890static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001891 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001892{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001893 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001894 struct inode *inode;
1895 struct proc_inode *ei;
1896 struct dentry *error = ERR_PTR(-EINVAL);
1897
Eric W. Biederman61a28782006-10-02 02:18:49 -07001898 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001899 if (!inode)
1900 goto out;
1901
1902 ei = PROC_I(inode);
1903 inode->i_mode = p->mode;
1904 if (S_ISDIR(inode->i_mode))
1905 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1906 if (p->iop)
1907 inode->i_op = p->iop;
1908 if (p->fop)
1909 inode->i_fop = p->fop;
1910 ei->op = p->op;
1911 dentry->d_op = &pid_dentry_operations;
1912 d_add(dentry, inode);
1913 /* Close the race of the process dying before we return the dentry */
1914 if (pid_revalidate(dentry, NULL))
1915 error = NULL;
1916out:
1917 return error;
1918}
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static struct dentry *proc_pident_lookup(struct inode *dir,
1921 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001922 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001923 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001926 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001927 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001928 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001930 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 inode = NULL;
1932
Eric W. Biederman99f89552006-06-26 00:25:55 -07001933 if (!task)
1934 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001936 /*
1937 * Yes, it does not scale. And it should not. Don't add
1938 * new entries into /proc/<tgid>/ without very good reasons.
1939 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001940 last = &ents[nents - 1];
1941 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (p->len != dentry->d_name.len)
1943 continue;
1944 if (!memcmp(dentry->d_name.name, p->name, p->len))
1945 break;
1946 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001947 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto out;
1949
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001950 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001952 put_task_struct(task);
1953out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001954 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Eric Dumazetc5141e62007-05-08 00:26:15 -07001957static int proc_pident_fill_cache(struct file *filp, void *dirent,
1958 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001959{
1960 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1961 proc_pident_instantiate, task, p);
1962}
1963
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001964static int proc_pident_readdir(struct file *filp,
1965 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001966 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001967{
1968 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001969 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001970 struct inode *inode = dentry->d_inode;
1971 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001972 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001973 ino_t ino;
1974 int ret;
1975
1976 ret = -ENOENT;
1977 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001978 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001979
1980 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001981 i = filp->f_pos;
1982 switch (i) {
1983 case 0:
1984 ino = inode->i_ino;
1985 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1986 goto out;
1987 i++;
1988 filp->f_pos++;
1989 /* fall through */
1990 case 1:
1991 ino = parent_ino(dentry);
1992 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1993 goto out;
1994 i++;
1995 filp->f_pos++;
1996 /* fall through */
1997 default:
1998 i -= 2;
1999 if (i >= nents) {
2000 ret = 1;
2001 goto out;
2002 }
2003 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002004 last = &ents[nents - 1];
2005 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002006 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002007 goto out;
2008 filp->f_pos++;
2009 p++;
2010 }
2011 }
2012
2013 ret = 1;
2014out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002015 put_task_struct(task);
2016out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002017 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002021static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2022 size_t count, loff_t *ppos)
2023{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002024 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002025 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002026 ssize_t length;
2027 struct task_struct *task = get_proc_task(inode);
2028
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002029 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002030 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002031
2032 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002033 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002034 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002035 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002036 if (length > 0)
2037 length = simple_read_from_buffer(buf, count, ppos, p, length);
2038 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002039 return length;
2040}
2041
2042static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2043 size_t count, loff_t *ppos)
2044{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002045 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002046 char *page;
2047 ssize_t length;
2048 struct task_struct *task = get_proc_task(inode);
2049
2050 length = -ESRCH;
2051 if (!task)
2052 goto out_no_task;
2053 if (count > PAGE_SIZE)
2054 count = PAGE_SIZE;
2055
2056 /* No partial writes. */
2057 length = -EINVAL;
2058 if (*ppos != 0)
2059 goto out;
2060
2061 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002062 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002063 if (!page)
2064 goto out;
2065
2066 length = -EFAULT;
2067 if (copy_from_user(page, buf, count))
2068 goto out_free;
2069
2070 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002071 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002072 (void*)page, count);
2073out_free:
2074 free_page((unsigned long) page);
2075out:
2076 put_task_struct(task);
2077out_no_task:
2078 return length;
2079}
2080
Arjan van de Ven00977a52007-02-12 00:55:34 -08002081static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002082 .read = proc_pid_attr_read,
2083 .write = proc_pid_attr_write,
2084};
2085
Eric Dumazetc5141e62007-05-08 00:26:15 -07002086static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002087 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2088 REG("prev", S_IRUGO, pid_attr),
2089 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2090 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2091 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2092 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002093};
2094
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002095static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 void * dirent, filldir_t filldir)
2097{
2098 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002099 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
Arjan van de Ven00977a52007-02-12 00:55:34 -08002102static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002104 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105};
2106
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002107static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 struct dentry *dentry, struct nameidata *nd)
2109{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002110 return proc_pident_lookup(dir, dentry,
2111 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002114static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002115 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002116 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002117 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118};
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#endif
2121
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002122#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2123static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2124 size_t count, loff_t *ppos)
2125{
2126 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2127 struct mm_struct *mm;
2128 char buffer[PROC_NUMBUF];
2129 size_t len;
2130 int ret;
2131
2132 if (!task)
2133 return -ESRCH;
2134
2135 ret = 0;
2136 mm = get_task_mm(task);
2137 if (mm) {
2138 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2139 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2140 MMF_DUMP_FILTER_SHIFT));
2141 mmput(mm);
2142 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2143 }
2144
2145 put_task_struct(task);
2146
2147 return ret;
2148}
2149
2150static ssize_t proc_coredump_filter_write(struct file *file,
2151 const char __user *buf,
2152 size_t count,
2153 loff_t *ppos)
2154{
2155 struct task_struct *task;
2156 struct mm_struct *mm;
2157 char buffer[PROC_NUMBUF], *end;
2158 unsigned int val;
2159 int ret;
2160 int i;
2161 unsigned long mask;
2162
2163 ret = -EFAULT;
2164 memset(buffer, 0, sizeof(buffer));
2165 if (count > sizeof(buffer) - 1)
2166 count = sizeof(buffer) - 1;
2167 if (copy_from_user(buffer, buf, count))
2168 goto out_no_task;
2169
2170 ret = -EINVAL;
2171 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2172 if (*end == '\n')
2173 end++;
2174 if (end - buffer == 0)
2175 goto out_no_task;
2176
2177 ret = -ESRCH;
2178 task = get_proc_task(file->f_dentry->d_inode);
2179 if (!task)
2180 goto out_no_task;
2181
2182 ret = end - buffer;
2183 mm = get_task_mm(task);
2184 if (!mm)
2185 goto out_no_mm;
2186
2187 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2188 if (val & mask)
2189 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2190 else
2191 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2192 }
2193
2194 mmput(mm);
2195 out_no_mm:
2196 put_task_struct(task);
2197 out_no_task:
2198 return ret;
2199}
2200
2201static const struct file_operations proc_coredump_filter_operations = {
2202 .read = proc_coredump_filter_read,
2203 .write = proc_coredump_filter_write,
2204};
2205#endif
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/*
2208 * /proc/self:
2209 */
2210static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2211 int buflen)
2212{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002213 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002214 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002215 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002216 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002217 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002218 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return vfs_readlink(dentry,buffer,buflen,tmp);
2220}
2221
Al Viro008b1502005-08-20 00:17:39 +01002222static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002224 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002225 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002226 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002227 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002228 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002229 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002230 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002231}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002233static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 .readlink = proc_self_readlink,
2235 .follow_link = proc_self_follow_link,
2236};
2237
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002238/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002239 * proc base
2240 *
2241 * These are the directory entries in the root directory of /proc
2242 * that properly belong to the /proc filesystem, as they describe
2243 * describe something that is process related.
2244 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002245static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002246 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002247 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002248};
2249
2250/*
2251 * Exceptional case: normally we are not allowed to unhash a busy
2252 * directory. In this case, however, we can do it - no aliasing problems
2253 * due to the way we treat inodes.
2254 */
2255static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2256{
2257 struct inode *inode = dentry->d_inode;
2258 struct task_struct *task = get_proc_task(inode);
2259 if (task) {
2260 put_task_struct(task);
2261 return 1;
2262 }
2263 d_drop(dentry);
2264 return 0;
2265}
2266
2267static struct dentry_operations proc_base_dentry_operations =
2268{
2269 .d_revalidate = proc_base_revalidate,
2270 .d_delete = pid_delete_dentry,
2271};
2272
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002273static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002274 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002275{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002276 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002277 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002278 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002279 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002280
2281 /* Allocate the inode */
2282 error = ERR_PTR(-ENOMEM);
2283 inode = new_inode(dir->i_sb);
2284 if (!inode)
2285 goto out;
2286
2287 /* Initialize the inode */
2288 ei = PROC_I(inode);
2289 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002290
2291 /*
2292 * grab the reference to the task.
2293 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002294 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002295 if (!ei->pid)
2296 goto out_iput;
2297
2298 inode->i_uid = 0;
2299 inode->i_gid = 0;
2300 inode->i_mode = p->mode;
2301 if (S_ISDIR(inode->i_mode))
2302 inode->i_nlink = 2;
2303 if (S_ISLNK(inode->i_mode))
2304 inode->i_size = 64;
2305 if (p->iop)
2306 inode->i_op = p->iop;
2307 if (p->fop)
2308 inode->i_fop = p->fop;
2309 ei->op = p->op;
2310 dentry->d_op = &proc_base_dentry_operations;
2311 d_add(dentry, inode);
2312 error = NULL;
2313out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002314 return error;
2315out_iput:
2316 iput(inode);
2317 goto out;
2318}
2319
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002320static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2321{
2322 struct dentry *error;
2323 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002324 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002325
2326 error = ERR_PTR(-ENOENT);
2327
2328 if (!task)
2329 goto out_no_task;
2330
2331 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002332 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2333 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002334 if (p->len != dentry->d_name.len)
2335 continue;
2336 if (!memcmp(dentry->d_name.name, p->name, p->len))
2337 break;
2338 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002339 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002340 goto out;
2341
2342 error = proc_base_instantiate(dir, dentry, task, p);
2343
2344out:
2345 put_task_struct(task);
2346out_no_task:
2347 return error;
2348}
2349
Eric Dumazetc5141e62007-05-08 00:26:15 -07002350static int proc_base_fill_cache(struct file *filp, void *dirent,
2351 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002352{
2353 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2354 proc_base_instantiate, task, p);
2355}
2356
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002357#ifdef CONFIG_TASK_IO_ACCOUNTING
2358static int proc_pid_io_accounting(struct task_struct *task, char *buffer)
2359{
2360 return sprintf(buffer,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002361#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002362 "rchar: %llu\n"
2363 "wchar: %llu\n"
2364 "syscr: %llu\n"
2365 "syscw: %llu\n"
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002366#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002367 "read_bytes: %llu\n"
2368 "write_bytes: %llu\n"
2369 "cancelled_write_bytes: %llu\n",
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002370#ifdef CONFIG_TASK_XACCT
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002371 (unsigned long long)task->rchar,
2372 (unsigned long long)task->wchar,
2373 (unsigned long long)task->syscr,
2374 (unsigned long long)task->syscw,
Alexey Dobriyan4b98d112007-02-10 01:46:45 -08002375#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002376 (unsigned long long)task->ioac.read_bytes,
2377 (unsigned long long)task->ioac.write_bytes,
2378 (unsigned long long)task->ioac.cancelled_write_bytes);
2379}
2380#endif
2381
Eric W. Biederman801199c2006-10-02 02:18:48 -07002382/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002383 * Thread groups
2384 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002385static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002386static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002387
Eric Dumazetc5141e62007-05-08 00:26:15 -07002388static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002389 DIR("task", S_IRUGO|S_IXUGO, task),
2390 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002391 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002392#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002393 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002394#endif
James Pearson315e28c2007-10-16 23:30:17 -07002395 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002396 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002397 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002398 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002399#ifdef CONFIG_SCHED_DEBUG
2400 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2401#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002402 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002403 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002404 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002405 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002406#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002407 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002408#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002409 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002410 LNK("cwd", cwd),
2411 LNK("root", root),
2412 LNK("exe", exe),
2413 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002414 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002415 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002416#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002417 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002418 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002419 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002420#endif
2421#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002422 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002423#endif
2424#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002425 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002426#endif
2427#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002428 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002429#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002430#ifdef CONFIG_LATENCYTOP
2431 REG("latency", S_IRUGO, lstats),
2432#endif
Paul Menage8793d852007-10-18 23:39:39 -07002433#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002434 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002435#endif
Paul Menagea4243162007-10-18 23:39:35 -07002436#ifdef CONFIG_CGROUPS
2437 REG("cgroup", S_IRUGO, cgroup),
2438#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002439 INF("oom_score", S_IRUGO, oom_score),
2440 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002441#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002442 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002443 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002444#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002445#ifdef CONFIG_FAULT_INJECTION
2446 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2447#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002448#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2449 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2450#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002451#ifdef CONFIG_TASK_IO_ACCOUNTING
2452 INF("io", S_IRUGO, pid_io_accounting),
2453#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002454};
2455
2456static int proc_tgid_base_readdir(struct file * filp,
2457 void * dirent, filldir_t filldir)
2458{
2459 return proc_pident_readdir(filp,dirent,filldir,
2460 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2461}
2462
Arjan van de Ven00977a52007-02-12 00:55:34 -08002463static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002464 .read = generic_read_dir,
2465 .readdir = proc_tgid_base_readdir,
2466};
2467
2468static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002469 return proc_pident_lookup(dir, dentry,
2470 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002471}
2472
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002473static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002474 .lookup = proc_tgid_base_lookup,
2475 .getattr = pid_getattr,
2476 .setattr = proc_setattr,
2477};
2478
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002479static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002481 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002482 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002483 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Eric W. Biederman48e64842006-06-26 00:25:48 -07002485 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002486 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2487 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002488 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002489 if (!(current->flags & PF_EXITING))
2490 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002491 d_drop(dentry);
2492 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002495 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002496 goto out;
2497
2498 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002499 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2500 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002501 if (!leader)
2502 goto out;
2503
2504 name.name = "task";
2505 name.len = strlen(name.name);
2506 dir = d_hash_and_lookup(leader, &name);
2507 if (!dir)
2508 goto out_put_leader;
2509
2510 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002511 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002512 dentry = d_hash_and_lookup(dir, &name);
2513 if (dentry) {
2514 shrink_dcache_parent(dentry);
2515 d_drop(dentry);
2516 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002518
2519 dput(dir);
2520out_put_leader:
2521 dput(leader);
2522out:
2523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
Randy Dunlap0895e912007-10-21 21:00:10 -07002526/**
2527 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2528 * @task: task that should be flushed.
2529 *
2530 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002531 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002532 * in. This call is supposed to do all of this job.
2533 *
2534 * Looks in the dcache for
2535 * /proc/@pid
2536 * /proc/@tgid/task/@pid
2537 * if either directory is present flushes it and all of it'ts children
2538 * from the dcache.
2539 *
2540 * It is safe and reasonable to cache /proc entries for a task until
2541 * that task exits. After that they just clog up the dcache with
2542 * useless entries, possibly causing useful dcache entries to be
2543 * flushed instead. This routine is proved to flush those useless
2544 * dcache entries at process exit time.
2545 *
2546 * NOTE: This routine is just an optimization so it does not guarantee
2547 * that no dcache entries will exist at process exit time it
2548 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002549 */
2550
2551void proc_flush_task(struct task_struct *task)
2552{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002553 int i;
2554 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002555 struct upid *upid;
2556
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002557 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002558 if (thread_group_leader(task))
2559 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002560
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002561 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002562 upid = &pid->numbers[i];
2563 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002564 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002565 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002566
2567 upid = &pid->numbers[pid->level];
2568 if (upid->nr == 1)
2569 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002570}
2571
Adrian Bunk9711ef92006-12-06 20:38:31 -08002572static struct dentry *proc_pid_instantiate(struct inode *dir,
2573 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002574 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002575{
2576 struct dentry *error = ERR_PTR(-ENOENT);
2577 struct inode *inode;
2578
Eric W. Biederman61a28782006-10-02 02:18:49 -07002579 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002580 if (!inode)
2581 goto out;
2582
2583 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2584 inode->i_op = &proc_tgid_base_inode_operations;
2585 inode->i_fop = &proc_tgid_base_operations;
2586 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002587 inode->i_nlink = 5;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002588#ifdef CONFIG_SECURITY
2589 inode->i_nlink += 1;
2590#endif
2591
2592 dentry->d_op = &pid_dentry_operations;
2593
2594 d_add(dentry, inode);
2595 /* Close the race of the process dying before we return the dentry */
2596 if (pid_revalidate(dentry, NULL))
2597 error = NULL;
2598out:
2599 return error;
2600}
2601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2603{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002604 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002607 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Eric W. Biederman801199c2006-10-02 02:18:48 -07002609 result = proc_base_lookup(dir, dentry);
2610 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2611 goto out;
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 tgid = name_to_int(dentry);
2614 if (tgid == ~0U)
2615 goto out;
2616
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002617 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002618 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002619 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 if (task)
2621 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002622 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if (!task)
2624 goto out;
2625
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002626 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002627 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002629 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002633 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002634 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002636struct tgid_iter {
2637 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002638 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002639};
2640static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2641{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002642 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002644 if (iter.task)
2645 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002646 rcu_read_lock();
2647retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002648 iter.task = NULL;
2649 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002650 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002651 iter.tgid = pid_nr_ns(pid, ns);
2652 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002653 /* What we to know is if the pid we have find is the
2654 * pid of a thread_group_leader. Testing for task
2655 * being a thread_group_leader is the obvious thing
2656 * todo but there is a window when it fails, due to
2657 * the pid transfer logic in de_thread.
2658 *
2659 * So we perform the straight forward test of seeing
2660 * if the pid we have found is the pid of a thread
2661 * group leader, and don't worry if the task we have
2662 * found doesn't happen to be a thread group leader.
2663 * As we don't care in the case of readdir.
2664 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002665 if (!iter.task || !has_group_leader_pid(iter.task)) {
2666 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002667 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002668 }
2669 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002671 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002672 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002675#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Eric W. Biederman61a28782006-10-02 02:18:49 -07002677static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002678 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002679{
2680 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002681 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002682 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002683 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002684}
2685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686/* for the /proc/ directory itself, after non-process stuff has been done */
2687int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002690 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002691 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002692 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Eric W. Biederman61a28782006-10-02 02:18:49 -07002694 if (!reaper)
2695 goto out_no_task;
2696
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002697 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002698 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002699 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002700 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
2702
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002703 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002704 iter.task = NULL;
2705 iter.tgid = filp->f_pos - TGID_OFFSET;
2706 for (iter = next_tgid(ns, iter);
2707 iter.task;
2708 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2709 filp->f_pos = iter.tgid + TGID_OFFSET;
2710 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2711 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002712 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002715 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2716out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002717 put_task_struct(reaper);
2718out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return 0;
2720}
2721
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002722/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002723 * Tasks
2724 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002725static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002726 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002727 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002728 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002729 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002730 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002731 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002732#ifdef CONFIG_SCHED_DEBUG
2733 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2734#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002735 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002736 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002737 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002738 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002739#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002740 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002741#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002742 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002743 LNK("cwd", cwd),
2744 LNK("root", root),
2745 LNK("exe", exe),
2746 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002747 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002748#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002749 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002750 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002751 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002752#endif
2753#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002754 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002755#endif
2756#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002757 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002758#endif
2759#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002760 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002761#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002762#ifdef CONFIG_LATENCYTOP
2763 REG("latency", S_IRUGO, lstats),
2764#endif
Paul Menage8793d852007-10-18 23:39:39 -07002765#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002766 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002767#endif
Paul Menagea4243162007-10-18 23:39:35 -07002768#ifdef CONFIG_CGROUPS
2769 REG("cgroup", S_IRUGO, cgroup),
2770#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002771 INF("oom_score", S_IRUGO, oom_score),
2772 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002773#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002774 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002775 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002776#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002777#ifdef CONFIG_FAULT_INJECTION
2778 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2779#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002780};
2781
2782static int proc_tid_base_readdir(struct file * filp,
2783 void * dirent, filldir_t filldir)
2784{
2785 return proc_pident_readdir(filp,dirent,filldir,
2786 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2787}
2788
2789static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002790 return proc_pident_lookup(dir, dentry,
2791 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002792}
2793
Arjan van de Ven00977a52007-02-12 00:55:34 -08002794static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002795 .read = generic_read_dir,
2796 .readdir = proc_tid_base_readdir,
2797};
2798
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002799static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002800 .lookup = proc_tid_base_lookup,
2801 .getattr = pid_getattr,
2802 .setattr = proc_setattr,
2803};
2804
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002805static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002806 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002807{
2808 struct dentry *error = ERR_PTR(-ENOENT);
2809 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002810 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002811
2812 if (!inode)
2813 goto out;
2814 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2815 inode->i_op = &proc_tid_base_inode_operations;
2816 inode->i_fop = &proc_tid_base_operations;
2817 inode->i_flags|=S_IMMUTABLE;
Miklos Szeredi27932742007-05-08 00:26:17 -07002818 inode->i_nlink = 4;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002819#ifdef CONFIG_SECURITY
2820 inode->i_nlink += 1;
2821#endif
2822
2823 dentry->d_op = &pid_dentry_operations;
2824
2825 d_add(dentry, inode);
2826 /* Close the race of the process dying before we return the dentry */
2827 if (pid_revalidate(dentry, NULL))
2828 error = NULL;
2829out:
2830 return error;
2831}
2832
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002833static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2834{
2835 struct dentry *result = ERR_PTR(-ENOENT);
2836 struct task_struct *task;
2837 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002838 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002839 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002840
2841 if (!leader)
2842 goto out_no_task;
2843
2844 tid = name_to_int(dentry);
2845 if (tid == ~0U)
2846 goto out;
2847
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002848 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002849 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002850 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002851 if (task)
2852 get_task_struct(task);
2853 rcu_read_unlock();
2854 if (!task)
2855 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002856 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002857 goto out_drop_task;
2858
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002859 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002860out_drop_task:
2861 put_task_struct(task);
2862out:
2863 put_task_struct(leader);
2864out_no_task:
2865 return result;
2866}
2867
2868/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002869 * Find the first tid of a thread group to return to user space.
2870 *
2871 * Usually this is just the thread group leader, but if the users
2872 * buffer was too small or there was a seek into the middle of the
2873 * directory we have more work todo.
2874 *
2875 * In the case of a short read we start with find_task_by_pid.
2876 *
2877 * In the case of a seek we start with the leader and walk nr
2878 * threads past it.
2879 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002880static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002881 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002882{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002883 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002884
Eric W. Biedermancc288732006-06-26 00:26:01 -07002885 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002886 /* Attempt to start with the pid of a thread */
2887 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002888 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002889 if (pos && (pos->group_leader == leader))
2890 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002891 }
2892
2893 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002894 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002895 if (nr && nr >= get_nr_threads(leader))
2896 goto out;
2897
2898 /* If we haven't found our starting place yet start
2899 * with the leader and walk nr threads forward.
2900 */
2901 for (pos = leader; nr > 0; --nr) {
2902 pos = next_thread(pos);
2903 if (pos == leader) {
2904 pos = NULL;
2905 goto out;
2906 }
2907 }
2908found:
2909 get_task_struct(pos);
2910out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002911 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002912 return pos;
2913}
2914
2915/*
2916 * Find the next thread in the thread list.
2917 * Return NULL if there is an error or no next thread.
2918 *
2919 * The reference to the input task_struct is released.
2920 */
2921static struct task_struct *next_tid(struct task_struct *start)
2922{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002923 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002924 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002925 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002926 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002927 if (thread_group_leader(pos))
2928 pos = NULL;
2929 else
2930 get_task_struct(pos);
2931 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07002932 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002933 put_task_struct(start);
2934 return pos;
2935}
2936
Eric W. Biederman61a28782006-10-02 02:18:49 -07002937static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
2938 struct task_struct *task, int tid)
2939{
2940 char name[PROC_NUMBUF];
2941 int len = snprintf(name, sizeof(name), "%d", tid);
2942 return proc_fill_cache(filp, dirent, filldir, name, len,
2943 proc_task_instantiate, task, NULL);
2944}
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946/* for the /proc/TGID/task/ directories */
2947static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
2948{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002949 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002951 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002952 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 int retval = -ENOENT;
2954 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002955 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002957 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
Guillaume Chazarain7d895242007-01-31 23:48:14 -08002959 task = get_proc_task(inode);
2960 if (!task)
2961 goto out_no_task;
2962 rcu_read_lock();
2963 if (pid_alive(task)) {
2964 leader = task->group_leader;
2965 get_task_struct(leader);
2966 }
2967 rcu_read_unlock();
2968 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07002969 if (!leader)
2970 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 retval = 0;
2972
2973 switch (pos) {
2974 case 0:
2975 ino = inode->i_ino;
2976 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
2977 goto out;
2978 pos++;
2979 /* fall through */
2980 case 1:
2981 ino = parent_ino(dentry);
2982 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
2983 goto out;
2984 pos++;
2985 /* fall through */
2986 }
2987
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002988 /* f_version caches the tgid value that the last readdir call couldn't
2989 * return. lseek aka telldir automagically resets f_version to 0.
2990 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002991 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07002992 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002993 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002994 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002995 task;
2996 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002997 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002998 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002999 /* returning this tgid failed, save it as the first
3000 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003001 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003002 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006out:
3007 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003008 put_task_struct(leader);
3009out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return retval;
3011}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003012
3013static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3014{
3015 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003016 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003017 generic_fillattr(inode, stat);
3018
Eric W. Biederman99f89552006-06-26 00:25:55 -07003019 if (p) {
3020 rcu_read_lock();
3021 stat->nlink += get_nr_threads(p);
3022 rcu_read_unlock();
3023 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003024 }
3025
3026 return 0;
3027}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003028
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003029static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003030 .lookup = proc_task_lookup,
3031 .getattr = proc_task_getattr,
3032 .setattr = proc_setattr,
3033};
3034
Arjan van de Ven00977a52007-02-12 00:55:34 -08003035static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003036 .read = generic_read_dir,
3037 .readdir = proc_task_readdir,
3038};