blob: 4b74dba69a6d6761aeed6595ba543fbcfc76730a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
56#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080057#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040059#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/string.h>
61#include <linux/seq_file.h>
62#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080063#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070065#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070067#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070068#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/mount.h>
70#include <linux/security.h>
71#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070072#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070073#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/cpuset.h>
75#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050076#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070077#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070078#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070079#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070080#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070081#include "internal.h"
82
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070083/* NOTE:
84 * Implementing inode permission operations in /proc is almost
85 * certainly an error. Permission checks need to happen during
86 * each system call not at open time. The reason is that most of
87 * what we wish to check for permissions in /proc varies at runtime.
88 *
89 * The classic example of a problem is opening file descriptors
90 * in /proc for a task before it execs a suid executable.
91 */
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070095 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080097 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080098 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -070099 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Eric W. Biederman61a28782006-10-02 02:18:49 -0700102#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700103 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700104 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700105 .mode = MODE, \
106 .iop = IOP, \
107 .fop = FOP, \
108 .op = OP, \
109}
110
Eric W. Biederman61a28782006-10-02 02:18:49 -0700111#define DIR(NAME, MODE, OTYPE) \
112 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700113 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
114 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700115#define LNK(NAME, OTYPE) \
116 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700117 &proc_pid_link_inode_operations, NULL, \
118 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119#define REG(NAME, MODE, OTYPE) \
120 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700121 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700122#define INF(NAME, MODE, OTYPE) \
123 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700124 NULL, &proc_info_file_operations, \
125 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800126#define ONE(NAME, MODE, OTYPE) \
127 NOD(NAME, (S_IFREG|(MODE)), \
128 NULL, &proc_single_file_operations, \
129 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Vegard Nossumaed54172008-06-05 22:46:53 -0700131/*
132 * Count the number of hardlinks for the pid_entry table, excluding the .
133 * and .. links.
134 */
135static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
136 unsigned int n)
137{
138 unsigned int i;
139 unsigned int count;
140
141 count = 0;
142 for (i = 0; i < n; ++i) {
143 if (S_ISDIR(entries[i].mode))
144 ++count;
145 }
146
147 return count;
148}
149
Kees Cook5096add2007-05-08 00:26:04 -0700150int maps_protect;
151EXPORT_SYMBOL(maps_protect);
152
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700153static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700156 task_lock(task);
157 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if(fs)
159 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700160 task_unlock(task);
161 return fs;
162}
163
Eric W. Biederman99f89552006-06-26 00:25:55 -0700164static int get_nr_threads(struct task_struct *tsk)
165{
166 /* Must be called with the rcu_read_lock held */
167 unsigned long flags;
168 int count = 0;
169
170 if (lock_task_sighand(tsk, &flags)) {
171 count = atomic_read(&tsk->signal->count);
172 unlock_task_sighand(tsk, &flags);
173 }
174 return count;
175}
176
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800177static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700178{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700179 struct task_struct *task = get_proc_task(inode);
180 struct fs_struct *fs = NULL;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700181 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700182
183 if (task) {
184 fs = get_fs_struct(task);
185 put_task_struct(task);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (fs) {
188 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800189 *path = fs->pwd;
190 path_get(&fs->pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 read_unlock(&fs->lock);
192 result = 0;
193 put_fs_struct(fs);
194 }
195 return result;
196}
197
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800198static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700200 struct task_struct *task = get_proc_task(inode);
201 struct fs_struct *fs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700203
204 if (task) {
205 fs = get_fs_struct(task);
206 put_task_struct(task);
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (fs) {
209 read_lock(&fs->lock);
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800210 *path = fs->root;
211 path_get(&fs->root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 read_unlock(&fs->lock);
213 result = 0;
214 put_fs_struct(fs);
215 }
216 return result;
217}
218
Roland McGrath638fa202008-04-29 01:01:38 -0700219/*
220 * Return zero if current may access user memory in @task, -error if not.
221 */
222static int check_mem_permission(struct task_struct *task)
223{
224 /*
225 * A task can always look at itself, in case it chooses
226 * to use system calls instead of load instructions.
227 */
228 if (task == current)
229 return 0;
230
231 /*
232 * If current is actively ptrace'ing, and would also be
233 * permitted to freshly attach with ptrace now, permit it.
234 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700235 if (task_is_stopped_or_traced(task)) {
236 int match;
237 rcu_read_lock();
238 match = (tracehook_tracer_task(task) == current);
239 rcu_read_unlock();
240 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
241 return 0;
242 }
Roland McGrath638fa202008-04-29 01:01:38 -0700243
244 /*
245 * Noone else is allowed.
246 */
247 return -EPERM;
248}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Al Viro831830b2008-01-02 14:09:57 +0000250struct mm_struct *mm_for_maps(struct task_struct *task)
251{
252 struct mm_struct *mm = get_task_mm(task);
253 if (!mm)
254 return NULL;
255 down_read(&mm->mmap_sem);
256 task_lock(task);
257 if (task->mm != mm)
258 goto out;
Stephen Smalley006ebb42008-05-19 08:32:49 -0400259 if (task->mm != current->mm &&
260 __ptrace_may_access(task, PTRACE_MODE_READ) < 0)
Al Viro831830b2008-01-02 14:09:57 +0000261 goto out;
262 task_unlock(task);
263 return mm;
264out:
265 task_unlock(task);
266 up_read(&mm->mmap_sem);
267 mmput(mm);
268 return NULL;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static int proc_pid_cmdline(struct task_struct *task, char * buffer)
272{
273 int res = 0;
274 unsigned int len;
275 struct mm_struct *mm = get_task_mm(task);
276 if (!mm)
277 goto out;
278 if (!mm->arg_end)
279 goto out_mm; /* Shh! No looking before we're done */
280
281 len = mm->arg_end - mm->arg_start;
282
283 if (len > PAGE_SIZE)
284 len = PAGE_SIZE;
285
286 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
287
288 // If the nul at the end of args has been overwritten, then
289 // assume application is using setproctitle(3).
290 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
291 len = strnlen(buffer, res);
292 if (len < res) {
293 res = len;
294 } else {
295 len = mm->env_end - mm->env_start;
296 if (len > PAGE_SIZE - res)
297 len = PAGE_SIZE - res;
298 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
299 res = strnlen(buffer, res);
300 }
301 }
302out_mm:
303 mmput(mm);
304out:
305 return res;
306}
307
308static int proc_pid_auxv(struct task_struct *task, char *buffer)
309{
310 int res = 0;
311 struct mm_struct *mm = get_task_mm(task);
312 if (mm) {
313 unsigned int nwords = 0;
314 do
315 nwords += 2;
316 while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
317 res = nwords * sizeof(mm->saved_auxv[0]);
318 if (res > PAGE_SIZE)
319 res = PAGE_SIZE;
320 memcpy(buffer, mm->saved_auxv, res);
321 mmput(mm);
322 }
323 return res;
324}
325
326
327#ifdef CONFIG_KALLSYMS
328/*
329 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
330 * Returns the resolved symbol. If that fails, simply return the address.
331 */
332static int proc_pid_wchan(struct task_struct *task, char *buffer)
333{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700334 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700335 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 wchan = get_wchan(task);
338
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700339 if (lookup_symbol_name(wchan, symname) < 0)
340 return sprintf(buffer, "%lu", wchan);
341 else
342 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344#endif /* CONFIG_KALLSYMS */
345
346#ifdef CONFIG_SCHEDSTATS
347/*
348 * Provides /proc/PID/schedstat
349 */
350static int proc_pid_schedstat(struct task_struct *task, char *buffer)
351{
Balbir Singh172ba842007-07-09 18:52:00 +0200352 return sprintf(buffer, "%llu %llu %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 task->sched_info.cpu_time,
354 task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200355 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357#endif
358
Arjan van de Ven97455122008-01-25 21:08:34 +0100359#ifdef CONFIG_LATENCYTOP
360static int lstats_show_proc(struct seq_file *m, void *v)
361{
362 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800363 struct inode *inode = m->private;
364 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100365
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800366 if (!task)
367 return -ESRCH;
368 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100369 for (i = 0; i < 32; i++) {
370 if (task->latency_record[i].backtrace[0]) {
371 int q;
372 seq_printf(m, "%i %li %li ",
373 task->latency_record[i].count,
374 task->latency_record[i].time,
375 task->latency_record[i].max);
376 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
377 char sym[KSYM_NAME_LEN];
378 char *c;
379 if (!task->latency_record[i].backtrace[q])
380 break;
381 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
382 break;
383 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
384 c = strchr(sym, '+');
385 if (c)
386 *c = 0;
387 seq_printf(m, "%s ", sym);
388 }
389 seq_printf(m, "\n");
390 }
391
392 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800393 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100394 return 0;
395}
396
397static int lstats_open(struct inode *inode, struct file *file)
398{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800399 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800400}
401
Arjan van de Ven97455122008-01-25 21:08:34 +0100402static ssize_t lstats_write(struct file *file, const char __user *buf,
403 size_t count, loff_t *offs)
404{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800405 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100406
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800407 if (!task)
408 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100409 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800410 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100411
412 return count;
413}
414
415static const struct file_operations proc_lstats_operations = {
416 .open = lstats_open,
417 .read = seq_read,
418 .write = lstats_write,
419 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800420 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100421};
422
423#endif
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/* The badness from the OOM killer */
426unsigned long badness(struct task_struct *p, unsigned long uptime);
427static int proc_oom_score(struct task_struct *task, char *buffer)
428{
429 unsigned long points;
430 struct timespec uptime;
431
432 do_posix_clock_monotonic_gettime(&uptime);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700433 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 points = badness(task, uptime.tv_sec);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700435 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return sprintf(buffer, "%lu\n", points);
437}
438
Neil Hormand85f50d2007-10-18 23:40:37 -0700439struct limit_names {
440 char *name;
441 char *unit;
442};
443
444static const struct limit_names lnames[RLIM_NLIMITS] = {
445 [RLIMIT_CPU] = {"Max cpu time", "ms"},
446 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
447 [RLIMIT_DATA] = {"Max data size", "bytes"},
448 [RLIMIT_STACK] = {"Max stack size", "bytes"},
449 [RLIMIT_CORE] = {"Max core file size", "bytes"},
450 [RLIMIT_RSS] = {"Max resident set", "bytes"},
451 [RLIMIT_NPROC] = {"Max processes", "processes"},
452 [RLIMIT_NOFILE] = {"Max open files", "files"},
453 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
454 [RLIMIT_AS] = {"Max address space", "bytes"},
455 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
456 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
457 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
458 [RLIMIT_NICE] = {"Max nice priority", NULL},
459 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800460 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700461};
462
463/* Display limits for a process */
464static int proc_pid_limits(struct task_struct *task, char *buffer)
465{
466 unsigned int i;
467 int count = 0;
468 unsigned long flags;
469 char *bufptr = buffer;
470
471 struct rlimit rlim[RLIM_NLIMITS];
472
473 rcu_read_lock();
474 if (!lock_task_sighand(task,&flags)) {
475 rcu_read_unlock();
476 return 0;
477 }
478 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
479 unlock_task_sighand(task, &flags);
480 rcu_read_unlock();
481
482 /*
483 * print the file header
484 */
485 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
487
488 for (i = 0; i < RLIM_NLIMITS; i++) {
489 if (rlim[i].rlim_cur == RLIM_INFINITY)
490 count += sprintf(&bufptr[count], "%-25s %-20s ",
491 lnames[i].name, "unlimited");
492 else
493 count += sprintf(&bufptr[count], "%-25s %-20lu ",
494 lnames[i].name, rlim[i].rlim_cur);
495
496 if (rlim[i].rlim_max == RLIM_INFINITY)
497 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498 else
499 count += sprintf(&bufptr[count], "%-20lu ",
500 rlim[i].rlim_max);
501
502 if (lnames[i].unit)
503 count += sprintf(&bufptr[count], "%-10s\n",
504 lnames[i].unit);
505 else
506 count += sprintf(&bufptr[count], "\n");
507 }
508
509 return count;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/************************************************************************/
513/* Here the fs part begins */
514/************************************************************************/
515
516/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700517static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700519 struct task_struct *task;
520 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700521 /* Allow access to a task's file descriptors if it is us or we
522 * may use ptrace attach to the process and find out that
523 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700524 */
525 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700526 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400527 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700528 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700529 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700530 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700533static int proc_setattr(struct dentry *dentry, struct iattr *attr)
534{
535 int error;
536 struct inode *inode = dentry->d_inode;
537
538 if (attr->ia_valid & ATTR_MODE)
539 return -EPERM;
540
541 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700542 if (!error)
543 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700544 return error;
545}
546
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800547static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700548 .setattr = proc_setattr,
549};
550
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100551static int mounts_open_common(struct inode *inode, struct file *file,
552 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700554 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700555 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800556 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100557 struct fs_struct *fs = NULL;
558 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500559 struct proc_mounts *p;
560 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Eric W. Biederman99f89552006-06-26 00:25:55 -0700562 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700563 rcu_read_lock();
564 nsp = task_nsproxy(task);
565 if (nsp) {
566 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800567 if (ns)
568 get_mnt_ns(ns);
569 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700570 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100571 if (ns)
572 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700573 put_task_struct(task);
574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100576 if (!ns)
577 goto err;
578 if (!fs)
579 goto err_put_ns;
580
581 read_lock(&fs->lock);
582 root = fs->root;
583 path_get(&root);
584 read_unlock(&fs->lock);
585 put_fs_struct(fs);
586
587 ret = -ENOMEM;
588 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
589 if (!p)
590 goto err_put_path;
591
592 file->private_data = &p->m;
593 ret = seq_open(file, op);
594 if (ret)
595 goto err_free;
596
597 p->m.private = p;
598 p->ns = ns;
599 p->root = root;
600 p->event = ns->event;
601
602 return 0;
603
604 err_free:
605 kfree(p);
606 err_put_path:
607 path_put(&root);
608 err_put_ns:
609 put_mnt_ns(ns);
610 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return ret;
612}
613
614static int mounts_release(struct inode *inode, struct file *file)
615{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100616 struct proc_mounts *p = file->private_data;
617 path_put(&p->root);
618 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return seq_release(inode, file);
620}
621
Al Viro5addc5d2005-11-07 17:15:49 -0500622static unsigned mounts_poll(struct file *file, poll_table *wait)
623{
624 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100625 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500626 unsigned res = 0;
627
628 poll_wait(file, &ns->poll, wait);
629
630 spin_lock(&vfsmount_lock);
631 if (p->event != ns->event) {
632 p->event = ns->event;
633 res = POLLERR;
634 }
635 spin_unlock(&vfsmount_lock);
636
637 return res;
638}
639
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100640static int mounts_open(struct inode *inode, struct file *file)
641{
642 return mounts_open_common(inode, file, &mounts_op);
643}
644
Arjan van de Ven00977a52007-02-12 00:55:34 -0800645static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 .open = mounts_open,
647 .read = seq_read,
648 .llseek = seq_lseek,
649 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500650 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651};
652
Ram Pai2d4d4862008-03-27 13:06:25 +0100653static int mountinfo_open(struct inode *inode, struct file *file)
654{
655 return mounts_open_common(inode, file, &mountinfo_op);
656}
657
658static const struct file_operations proc_mountinfo_operations = {
659 .open = mountinfo_open,
660 .read = seq_read,
661 .llseek = seq_lseek,
662 .release = mounts_release,
663 .poll = mounts_poll,
664};
665
Chuck Leverb4629fe2006-03-20 13:44:12 -0500666static int mountstats_open(struct inode *inode, struct file *file)
667{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100668 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500669}
670
Arjan van de Ven00977a52007-02-12 00:55:34 -0800671static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500672 .open = mountstats_open,
673 .read = seq_read,
674 .llseek = seq_lseek,
675 .release = mounts_release,
676};
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
679
680static ssize_t proc_info_read(struct file * file, char __user * buf,
681 size_t count, loff_t *ppos)
682{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800683 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned long page;
685 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700686 struct task_struct *task = get_proc_task(inode);
687
688 length = -ESRCH;
689 if (!task)
690 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (count > PROC_BLOCK_SIZE)
693 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700694
695 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700696 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700697 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 length = PROC_I(inode)->op.proc_read(task, (char*)page);
700
701 if (length >= 0)
702 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
703 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700704out:
705 put_task_struct(task);
706out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return length;
708}
709
Arjan van de Ven00977a52007-02-12 00:55:34 -0800710static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 .read = proc_info_read,
712};
713
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800714static int proc_single_show(struct seq_file *m, void *v)
715{
716 struct inode *inode = m->private;
717 struct pid_namespace *ns;
718 struct pid *pid;
719 struct task_struct *task;
720 int ret;
721
722 ns = inode->i_sb->s_fs_info;
723 pid = proc_pid(inode);
724 task = get_pid_task(pid, PIDTYPE_PID);
725 if (!task)
726 return -ESRCH;
727
728 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
729
730 put_task_struct(task);
731 return ret;
732}
733
734static int proc_single_open(struct inode *inode, struct file *filp)
735{
736 int ret;
737 ret = single_open(filp, proc_single_show, NULL);
738 if (!ret) {
739 struct seq_file *m = filp->private_data;
740
741 m->private = inode;
742 }
743 return ret;
744}
745
746static const struct file_operations proc_single_file_operations = {
747 .open = proc_single_open,
748 .read = seq_read,
749 .llseek = seq_lseek,
750 .release = single_release,
751};
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753static int mem_open(struct inode* inode, struct file* file)
754{
755 file->private_data = (void*)((long)current->self_exec_id);
756 return 0;
757}
758
759static ssize_t mem_read(struct file * file, char __user * buf,
760 size_t count, loff_t *ppos)
761{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800762 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 char *page;
764 unsigned long src = *ppos;
765 int ret = -ESRCH;
766 struct mm_struct *mm;
767
Eric W. Biederman99f89552006-06-26 00:25:55 -0700768 if (!task)
769 goto out_no_task;
770
Roland McGrath638fa202008-04-29 01:01:38 -0700771 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto out;
773
774 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700775 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (!page)
777 goto out;
778
779 ret = 0;
780
781 mm = get_task_mm(task);
782 if (!mm)
783 goto out_free;
784
785 ret = -EIO;
786
787 if (file->private_data != (void*)((long)current->self_exec_id))
788 goto out_put;
789
790 ret = 0;
791
792 while (count > 0) {
793 int this_len, retval;
794
795 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
796 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa202008-04-29 01:01:38 -0700797 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!ret)
799 ret = -EIO;
800 break;
801 }
802
803 if (copy_to_user(buf, page, retval)) {
804 ret = -EFAULT;
805 break;
806 }
807
808 ret += retval;
809 src += retval;
810 buf += retval;
811 count -= retval;
812 }
813 *ppos = src;
814
815out_put:
816 mmput(mm);
817out_free:
818 free_page((unsigned long) page);
819out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700820 put_task_struct(task);
821out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return ret;
823}
824
825#define mem_write NULL
826
827#ifndef mem_write
828/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800829static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 size_t count, loff_t *ppos)
831{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700832 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800834 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 unsigned long dst = *ppos;
836
Eric W. Biederman99f89552006-06-26 00:25:55 -0700837 copied = -ESRCH;
838 if (!task)
839 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Roland McGrath638fa202008-04-29 01:01:38 -0700841 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700842 goto out;
843
844 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700845 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700849 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 while (count > 0) {
851 int this_len, retval;
852
853 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
854 if (copy_from_user(page, buf, this_len)) {
855 copied = -EFAULT;
856 break;
857 }
858 retval = access_process_vm(task, dst, page, this_len, 1);
859 if (!retval) {
860 if (!copied)
861 copied = -EIO;
862 break;
863 }
864 copied += retval;
865 buf += retval;
866 dst += retval;
867 count -= retval;
868 }
869 *ppos = dst;
870 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700871out:
872 put_task_struct(task);
873out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return copied;
875}
876#endif
877
Matt Mackall85863e42008-02-04 22:29:04 -0800878loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
880 switch (orig) {
881 case 0:
882 file->f_pos = offset;
883 break;
884 case 1:
885 file->f_pos += offset;
886 break;
887 default:
888 return -EINVAL;
889 }
890 force_successful_syscall_return();
891 return file->f_pos;
892}
893
Arjan van de Ven00977a52007-02-12 00:55:34 -0800894static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 .llseek = mem_lseek,
896 .read = mem_read,
897 .write = mem_write,
898 .open = mem_open,
899};
900
James Pearson315e28c2007-10-16 23:30:17 -0700901static ssize_t environ_read(struct file *file, char __user *buf,
902 size_t count, loff_t *ppos)
903{
904 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
905 char *page;
906 unsigned long src = *ppos;
907 int ret = -ESRCH;
908 struct mm_struct *mm;
909
910 if (!task)
911 goto out_no_task;
912
Stephen Smalley006ebb42008-05-19 08:32:49 -0400913 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700914 goto out;
915
916 ret = -ENOMEM;
917 page = (char *)__get_free_page(GFP_TEMPORARY);
918 if (!page)
919 goto out;
920
921 ret = 0;
922
923 mm = get_task_mm(task);
924 if (!mm)
925 goto out_free;
926
927 while (count > 0) {
928 int this_len, retval, max_len;
929
930 this_len = mm->env_end - (mm->env_start + src);
931
932 if (this_len <= 0)
933 break;
934
935 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
936 this_len = (this_len > max_len) ? max_len : this_len;
937
938 retval = access_process_vm(task, (mm->env_start + src),
939 page, this_len, 0);
940
941 if (retval <= 0) {
942 ret = retval;
943 break;
944 }
945
946 if (copy_to_user(buf, page, retval)) {
947 ret = -EFAULT;
948 break;
949 }
950
951 ret += retval;
952 src += retval;
953 buf += retval;
954 count -= retval;
955 }
956 *ppos = src;
957
958 mmput(mm);
959out_free:
960 free_page((unsigned long) page);
961out:
962 put_task_struct(task);
963out_no_task:
964 return ret;
965}
966
967static const struct file_operations proc_environ_operations = {
968 .read = environ_read,
969};
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971static ssize_t oom_adjust_read(struct file *file, char __user *buf,
972 size_t count, loff_t *ppos)
973{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800974 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700975 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700977 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Eric W. Biederman99f89552006-06-26 00:25:55 -0700979 if (!task)
980 return -ESRCH;
981 oom_adjust = task->oomkilladj;
982 put_task_struct(task);
983
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700984 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -0700985
986 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
990 size_t count, loff_t *ppos)
991{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700992 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700993 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 int oom_adjust;
995
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700996 memset(buffer, 0, sizeof(buffer));
997 if (count > sizeof(buffer) - 1)
998 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (copy_from_user(buffer, buf, count))
1000 return -EFAULT;
1001 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001002 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1003 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return -EINVAL;
1005 if (*end == '\n')
1006 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001007 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001008 if (!task)
1009 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001010 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1011 put_task_struct(task);
1012 return -EACCES;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001015 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (end - buffer == 0)
1017 return -EIO;
1018 return end - buffer;
1019}
1020
Arjan van de Ven00977a52007-02-12 00:55:34 -08001021static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 .read = oom_adjust_read,
1023 .write = oom_adjust_write,
1024};
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026#ifdef CONFIG_AUDITSYSCALL
1027#define TMPBUFLEN 21
1028static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1029 size_t count, loff_t *ppos)
1030{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001031 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001032 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 ssize_t length;
1034 char tmpbuf[TMPBUFLEN];
1035
Eric W. Biederman99f89552006-06-26 00:25:55 -07001036 if (!task)
1037 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001039 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001040 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1042}
1043
1044static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1045 size_t count, loff_t *ppos)
1046{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001047 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 char *page, *tmp;
1049 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 uid_t loginuid;
1051
1052 if (!capable(CAP_AUDIT_CONTROL))
1053 return -EPERM;
1054
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001055 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return -EPERM;
1057
Al Viroe0182902006-05-18 08:28:02 -04001058 if (count >= PAGE_SIZE)
1059 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 if (*ppos != 0) {
1062 /* No partial writes. */
1063 return -EINVAL;
1064 }
Mel Gormane12ba742007-10-16 01:25:52 -07001065 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!page)
1067 return -ENOMEM;
1068 length = -EFAULT;
1069 if (copy_from_user(page, buf, count))
1070 goto out_free_page;
1071
Al Viroe0182902006-05-18 08:28:02 -04001072 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 loginuid = simple_strtoul(page, &tmp, 10);
1074 if (tmp == page) {
1075 length = -EINVAL;
1076 goto out_free_page;
1077
1078 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001079 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (likely(length == 0))
1081 length = count;
1082
1083out_free_page:
1084 free_page((unsigned long) page);
1085 return length;
1086}
1087
Arjan van de Ven00977a52007-02-12 00:55:34 -08001088static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 .read = proc_loginuid_read,
1090 .write = proc_loginuid_write,
1091};
Eric Paris1e0bd752008-03-13 08:15:31 -04001092
1093static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1094 size_t count, loff_t *ppos)
1095{
1096 struct inode * inode = file->f_path.dentry->d_inode;
1097 struct task_struct *task = get_proc_task(inode);
1098 ssize_t length;
1099 char tmpbuf[TMPBUFLEN];
1100
1101 if (!task)
1102 return -ESRCH;
1103 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1104 audit_get_sessionid(task));
1105 put_task_struct(task);
1106 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1107}
1108
1109static const struct file_operations proc_sessionid_operations = {
1110 .read = proc_sessionid_read,
1111};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112#endif
1113
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001114#ifdef CONFIG_FAULT_INJECTION
1115static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1116 size_t count, loff_t *ppos)
1117{
1118 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1119 char buffer[PROC_NUMBUF];
1120 size_t len;
1121 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001122
1123 if (!task)
1124 return -ESRCH;
1125 make_it_fail = task->make_it_fail;
1126 put_task_struct(task);
1127
1128 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001129
1130 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001131}
1132
1133static ssize_t proc_fault_inject_write(struct file * file,
1134 const char __user * buf, size_t count, loff_t *ppos)
1135{
1136 struct task_struct *task;
1137 char buffer[PROC_NUMBUF], *end;
1138 int make_it_fail;
1139
1140 if (!capable(CAP_SYS_RESOURCE))
1141 return -EPERM;
1142 memset(buffer, 0, sizeof(buffer));
1143 if (count > sizeof(buffer) - 1)
1144 count = sizeof(buffer) - 1;
1145 if (copy_from_user(buffer, buf, count))
1146 return -EFAULT;
1147 make_it_fail = simple_strtol(buffer, &end, 0);
1148 if (*end == '\n')
1149 end++;
1150 task = get_proc_task(file->f_dentry->d_inode);
1151 if (!task)
1152 return -ESRCH;
1153 task->make_it_fail = make_it_fail;
1154 put_task_struct(task);
1155 if (end - buffer == 0)
1156 return -EIO;
1157 return end - buffer;
1158}
1159
Arjan van de Ven00977a52007-02-12 00:55:34 -08001160static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001161 .read = proc_fault_inject_read,
1162 .write = proc_fault_inject_write,
1163};
1164#endif
1165
Arjan van de Ven97455122008-01-25 21:08:34 +01001166
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001167#ifdef CONFIG_SCHED_DEBUG
1168/*
1169 * Print out various scheduling related per-task fields:
1170 */
1171static int sched_show(struct seq_file *m, void *v)
1172{
1173 struct inode *inode = m->private;
1174 struct task_struct *p;
1175
1176 WARN_ON(!inode);
1177
1178 p = get_proc_task(inode);
1179 if (!p)
1180 return -ESRCH;
1181 proc_sched_show_task(p, m);
1182
1183 put_task_struct(p);
1184
1185 return 0;
1186}
1187
1188static ssize_t
1189sched_write(struct file *file, const char __user *buf,
1190 size_t count, loff_t *offset)
1191{
1192 struct inode *inode = file->f_path.dentry->d_inode;
1193 struct task_struct *p;
1194
1195 WARN_ON(!inode);
1196
1197 p = get_proc_task(inode);
1198 if (!p)
1199 return -ESRCH;
1200 proc_sched_set_task(p);
1201
1202 put_task_struct(p);
1203
1204 return count;
1205}
1206
1207static int sched_open(struct inode *inode, struct file *filp)
1208{
1209 int ret;
1210
1211 ret = single_open(filp, sched_show, NULL);
1212 if (!ret) {
1213 struct seq_file *m = filp->private_data;
1214
1215 m->private = inode;
1216 }
1217 return ret;
1218}
1219
1220static const struct file_operations proc_pid_sched_operations = {
1221 .open = sched_open,
1222 .read = seq_read,
1223 .write = sched_write,
1224 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001225 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001226};
1227
1228#endif
1229
Matt Helsley925d1c42008-04-29 01:01:36 -07001230/*
1231 * We added or removed a vma mapping the executable. The vmas are only mapped
1232 * during exec and are not mapped with the mmap system call.
1233 * Callers must hold down_write() on the mm's mmap_sem for these
1234 */
1235void added_exe_file_vma(struct mm_struct *mm)
1236{
1237 mm->num_exe_file_vmas++;
1238}
1239
1240void removed_exe_file_vma(struct mm_struct *mm)
1241{
1242 mm->num_exe_file_vmas--;
1243 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1244 fput(mm->exe_file);
1245 mm->exe_file = NULL;
1246 }
1247
1248}
1249
1250void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1251{
1252 if (new_exe_file)
1253 get_file(new_exe_file);
1254 if (mm->exe_file)
1255 fput(mm->exe_file);
1256 mm->exe_file = new_exe_file;
1257 mm->num_exe_file_vmas = 0;
1258}
1259
1260struct file *get_mm_exe_file(struct mm_struct *mm)
1261{
1262 struct file *exe_file;
1263
1264 /* We need mmap_sem to protect against races with removal of
1265 * VM_EXECUTABLE vmas */
1266 down_read(&mm->mmap_sem);
1267 exe_file = mm->exe_file;
1268 if (exe_file)
1269 get_file(exe_file);
1270 up_read(&mm->mmap_sem);
1271 return exe_file;
1272}
1273
1274void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1275{
1276 /* It's safe to write the exe_file pointer without exe_file_lock because
1277 * this is called during fork when the task is not yet in /proc */
1278 newmm->exe_file = get_mm_exe_file(oldmm);
1279}
1280
1281static int proc_exe_link(struct inode *inode, struct path *exe_path)
1282{
1283 struct task_struct *task;
1284 struct mm_struct *mm;
1285 struct file *exe_file;
1286
1287 task = get_proc_task(inode);
1288 if (!task)
1289 return -ENOENT;
1290 mm = get_task_mm(task);
1291 put_task_struct(task);
1292 if (!mm)
1293 return -ENOENT;
1294 exe_file = get_mm_exe_file(mm);
1295 mmput(mm);
1296 if (exe_file) {
1297 *exe_path = exe_file->f_path;
1298 path_get(&exe_file->f_path);
1299 fput(exe_file);
1300 return 0;
1301 } else
1302 return -ENOENT;
1303}
1304
Al Viro008b1502005-08-20 00:17:39 +01001305static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 struct inode *inode = dentry->d_inode;
1308 int error = -EACCES;
1309
1310 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001311 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Eric W. Biederman778c1142006-06-26 00:25:58 -07001313 /* Are we allowed to snoop on the tasks file descriptors? */
1314 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001317 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 nd->last_type = LAST_BIND;
1319out:
Al Viro008b1502005-08-20 00:17:39 +01001320 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321}
1322
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001323static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Mel Gormane12ba742007-10-16 01:25:52 -07001325 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001326 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int len;
1328
1329 if (!tmp)
1330 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001331
Jan Blunckcf28b482008-02-14 19:38:44 -08001332 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001333 len = PTR_ERR(pathname);
1334 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001336 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 if (len > buflen)
1339 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001340 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 len = -EFAULT;
1342 out:
1343 free_page((unsigned long)tmp);
1344 return len;
1345}
1346
1347static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1348{
1349 int error = -EACCES;
1350 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001351 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Eric W. Biederman778c1142006-06-26 00:25:58 -07001353 /* Are we allowed to snoop on the tasks file descriptors? */
1354 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001357 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (error)
1359 goto out;
1360
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001361 error = do_proc_readlink(&path, buffer, buflen);
1362 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return error;
1365}
1366
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001367static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001369 .follow_link = proc_pid_follow_link,
1370 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371};
1372
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001373
1374/* building an inode */
1375
1376static int task_dumpable(struct task_struct *task)
1377{
1378 int dumpable = 0;
1379 struct mm_struct *mm;
1380
1381 task_lock(task);
1382 mm = task->mm;
1383 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001384 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001385 task_unlock(task);
1386 if(dumpable == 1)
1387 return 1;
1388 return 0;
1389}
1390
1391
Eric W. Biederman61a28782006-10-02 02:18:49 -07001392static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001393{
1394 struct inode * inode;
1395 struct proc_inode *ei;
1396
1397 /* We need a new inode */
1398
1399 inode = new_inode(sb);
1400 if (!inode)
1401 goto out;
1402
1403 /* Common stuff */
1404 ei = PROC_I(inode);
1405 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001406 inode->i_op = &proc_def_inode_operations;
1407
1408 /*
1409 * grab the reference to task.
1410 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001411 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001412 if (!ei->pid)
1413 goto out_unlock;
1414
1415 inode->i_uid = 0;
1416 inode->i_gid = 0;
1417 if (task_dumpable(task)) {
1418 inode->i_uid = task->euid;
1419 inode->i_gid = task->egid;
1420 }
1421 security_task_to_inode(task, inode);
1422
1423out:
1424 return inode;
1425
1426out_unlock:
1427 iput(inode);
1428 return NULL;
1429}
1430
1431static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1432{
1433 struct inode *inode = dentry->d_inode;
1434 struct task_struct *task;
1435 generic_fillattr(inode, stat);
1436
1437 rcu_read_lock();
1438 stat->uid = 0;
1439 stat->gid = 0;
1440 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1441 if (task) {
1442 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1443 task_dumpable(task)) {
1444 stat->uid = task->euid;
1445 stat->gid = task->egid;
1446 }
1447 }
1448 rcu_read_unlock();
1449 return 0;
1450}
1451
1452/* dentry stuff */
1453
1454/*
1455 * Exceptional case: normally we are not allowed to unhash a busy
1456 * directory. In this case, however, we can do it - no aliasing problems
1457 * due to the way we treat inodes.
1458 *
1459 * Rewrite the inode's ownerships here because the owning task may have
1460 * performed a setuid(), etc.
1461 *
1462 * Before the /proc/pid/status file was created the only way to read
1463 * the effective uid of a /process was to stat /proc/pid. Reading
1464 * /proc/pid/status is slow enough that procps and other packages
1465 * kept stating /proc/pid. To keep the rules in /proc simple I have
1466 * made this apply to all per process world readable and executable
1467 * directories.
1468 */
1469static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1470{
1471 struct inode *inode = dentry->d_inode;
1472 struct task_struct *task = get_proc_task(inode);
1473 if (task) {
1474 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1475 task_dumpable(task)) {
1476 inode->i_uid = task->euid;
1477 inode->i_gid = task->egid;
1478 } else {
1479 inode->i_uid = 0;
1480 inode->i_gid = 0;
1481 }
1482 inode->i_mode &= ~(S_ISUID | S_ISGID);
1483 security_task_to_inode(task, inode);
1484 put_task_struct(task);
1485 return 1;
1486 }
1487 d_drop(dentry);
1488 return 0;
1489}
1490
1491static int pid_delete_dentry(struct dentry * dentry)
1492{
1493 /* Is the task we represent dead?
1494 * If so, then don't put the dentry on the lru list,
1495 * kill it immediately.
1496 */
1497 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1498}
1499
1500static struct dentry_operations pid_dentry_operations =
1501{
1502 .d_revalidate = pid_revalidate,
1503 .d_delete = pid_delete_dentry,
1504};
1505
1506/* Lookups */
1507
Eric Dumazetc5141e62007-05-08 00:26:15 -07001508typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1509 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001510
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001511/*
1512 * Fill a directory entry.
1513 *
1514 * If possible create the dcache entry and derive our inode number and
1515 * file type from dcache entry.
1516 *
1517 * Since all of the proc inode numbers are dynamically generated, the inode
1518 * numbers do not exist until the inode is cache. This means creating the
1519 * the dcache entry in readdir is necessary to keep the inode numbers
1520 * reported by readdir in sync with the inode numbers reported
1521 * by stat.
1522 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001523static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1524 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001525 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001526{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001527 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001528 struct inode *inode;
1529 struct qstr qname;
1530 ino_t ino = 0;
1531 unsigned type = DT_UNKNOWN;
1532
1533 qname.name = name;
1534 qname.len = len;
1535 qname.hash = full_name_hash(name, len);
1536
1537 child = d_lookup(dir, &qname);
1538 if (!child) {
1539 struct dentry *new;
1540 new = d_alloc(dir, &qname);
1541 if (new) {
1542 child = instantiate(dir->d_inode, new, task, ptr);
1543 if (child)
1544 dput(new);
1545 else
1546 child = new;
1547 }
1548 }
1549 if (!child || IS_ERR(child) || !child->d_inode)
1550 goto end_instantiate;
1551 inode = child->d_inode;
1552 if (inode) {
1553 ino = inode->i_ino;
1554 type = inode->i_mode >> 12;
1555 }
1556 dput(child);
1557end_instantiate:
1558 if (!ino)
1559 ino = find_inode_number(dir, &qname);
1560 if (!ino)
1561 ino = 1;
1562 return filldir(dirent, name, len, filp->f_pos, ino, type);
1563}
1564
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001565static unsigned name_to_int(struct dentry *dentry)
1566{
1567 const char *name = dentry->d_name.name;
1568 int len = dentry->d_name.len;
1569 unsigned n = 0;
1570
1571 if (len > 1 && *name == '0')
1572 goto out;
1573 while (len-- > 0) {
1574 unsigned c = *name++ - '0';
1575 if (c > 9)
1576 goto out;
1577 if (n >= (~0U-9)/10)
1578 goto out;
1579 n *= 10;
1580 n += c;
1581 }
1582 return n;
1583out:
1584 return ~0U;
1585}
1586
Miklos Szeredi27932742007-05-08 00:26:17 -07001587#define PROC_FDINFO_MAX 64
1588
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001589static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001590{
1591 struct task_struct *task = get_proc_task(inode);
1592 struct files_struct *files = NULL;
1593 struct file *file;
1594 int fd = proc_fd(inode);
1595
1596 if (task) {
1597 files = get_files_struct(task);
1598 put_task_struct(task);
1599 }
1600 if (files) {
1601 /*
1602 * We are not taking a ref to the file structure, so we must
1603 * hold ->file_lock.
1604 */
1605 spin_lock(&files->file_lock);
1606 file = fcheck_files(files, fd);
1607 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001608 if (path) {
1609 *path = file->f_path;
1610 path_get(&file->f_path);
1611 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001612 if (info)
1613 snprintf(info, PROC_FDINFO_MAX,
1614 "pos:\t%lli\n"
1615 "flags:\t0%o\n",
1616 (long long) file->f_pos,
1617 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001618 spin_unlock(&files->file_lock);
1619 put_files_struct(files);
1620 return 0;
1621 }
1622 spin_unlock(&files->file_lock);
1623 put_files_struct(files);
1624 }
1625 return -ENOENT;
1626}
1627
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001628static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001629{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001630 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001631}
1632
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001633static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1634{
1635 struct inode *inode = dentry->d_inode;
1636 struct task_struct *task = get_proc_task(inode);
1637 int fd = proc_fd(inode);
1638 struct files_struct *files;
1639
1640 if (task) {
1641 files = get_files_struct(task);
1642 if (files) {
1643 rcu_read_lock();
1644 if (fcheck_files(files, fd)) {
1645 rcu_read_unlock();
1646 put_files_struct(files);
1647 if (task_dumpable(task)) {
1648 inode->i_uid = task->euid;
1649 inode->i_gid = task->egid;
1650 } else {
1651 inode->i_uid = 0;
1652 inode->i_gid = 0;
1653 }
1654 inode->i_mode &= ~(S_ISUID | S_ISGID);
1655 security_task_to_inode(task, inode);
1656 put_task_struct(task);
1657 return 1;
1658 }
1659 rcu_read_unlock();
1660 put_files_struct(files);
1661 }
1662 put_task_struct(task);
1663 }
1664 d_drop(dentry);
1665 return 0;
1666}
1667
1668static struct dentry_operations tid_fd_dentry_operations =
1669{
1670 .d_revalidate = tid_fd_revalidate,
1671 .d_delete = pid_delete_dentry,
1672};
1673
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001674static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001675 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001676{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001677 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001678 struct file *file;
1679 struct files_struct *files;
1680 struct inode *inode;
1681 struct proc_inode *ei;
1682 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001683
Eric W. Biederman61a28782006-10-02 02:18:49 -07001684 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001685 if (!inode)
1686 goto out;
1687 ei = PROC_I(inode);
1688 ei->fd = fd;
1689 files = get_files_struct(task);
1690 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001691 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001692 inode->i_mode = S_IFLNK;
1693
1694 /*
1695 * We are not taking a ref to the file structure, so we must
1696 * hold ->file_lock.
1697 */
1698 spin_lock(&files->file_lock);
1699 file = fcheck_files(files, fd);
1700 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001701 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001702 if (file->f_mode & 1)
1703 inode->i_mode |= S_IRUSR | S_IXUSR;
1704 if (file->f_mode & 2)
1705 inode->i_mode |= S_IWUSR | S_IXUSR;
1706 spin_unlock(&files->file_lock);
1707 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001708
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001709 inode->i_op = &proc_pid_link_inode_operations;
1710 inode->i_size = 64;
1711 ei->op.proc_get_link = proc_fd_link;
1712 dentry->d_op = &tid_fd_dentry_operations;
1713 d_add(dentry, inode);
1714 /* Close the race of the process dying before we return the dentry */
1715 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001716 error = NULL;
1717
1718 out:
1719 return error;
1720out_unlock:
1721 spin_unlock(&files->file_lock);
1722 put_files_struct(files);
1723out_iput:
1724 iput(inode);
1725 goto out;
1726}
1727
Miklos Szeredi27932742007-05-08 00:26:17 -07001728static struct dentry *proc_lookupfd_common(struct inode *dir,
1729 struct dentry *dentry,
1730 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001731{
1732 struct task_struct *task = get_proc_task(dir);
1733 unsigned fd = name_to_int(dentry);
1734 struct dentry *result = ERR_PTR(-ENOENT);
1735
1736 if (!task)
1737 goto out_no_task;
1738 if (fd == ~0U)
1739 goto out;
1740
Miklos Szeredi27932742007-05-08 00:26:17 -07001741 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001742out:
1743 put_task_struct(task);
1744out_no_task:
1745 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001746}
1747
Miklos Szeredi27932742007-05-08 00:26:17 -07001748static int proc_readfd_common(struct file * filp, void * dirent,
1749 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001751 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001752 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001753 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001754 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 struct files_struct * files;
1757
1758 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001759 if (!p)
1760 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 fd = filp->f_pos;
1764 switch (fd) {
1765 case 0:
1766 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1767 goto out;
1768 filp->f_pos++;
1769 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001770 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1772 goto out;
1773 filp->f_pos++;
1774 default:
1775 files = get_files_struct(p);
1776 if (!files)
1777 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001778 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001780 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001782 char name[PROC_NUMBUF];
1783 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (!fcheck_files(files, fd))
1786 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001787 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Miklos Szeredi27932742007-05-08 00:26:17 -07001789 len = snprintf(name, sizeof(name), "%d", fd);
1790 if (proc_fill_cache(filp, dirent, filldir,
1791 name, len, instantiate,
1792 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001793 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 break;
1795 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001796 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001798 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 put_files_struct(files);
1800 }
1801out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001802 put_task_struct(p);
1803out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return retval;
1805}
1806
Miklos Szeredi27932742007-05-08 00:26:17 -07001807static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1808 struct nameidata *nd)
1809{
1810 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1811}
1812
1813static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1814{
1815 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1816}
1817
1818static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1819 size_t len, loff_t *ppos)
1820{
1821 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001822 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001823 if (!err)
1824 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1825 return err;
1826}
1827
1828static const struct file_operations proc_fdinfo_file_operations = {
1829 .open = nonseekable_open,
1830 .read = proc_fdinfo_read,
1831};
1832
Arjan van de Ven00977a52007-02-12 00:55:34 -08001833static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 .read = generic_read_dir,
1835 .readdir = proc_readfd,
1836};
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001839 * /proc/pid/fd needs a special permission handler so that a process can still
1840 * access /proc/self/fd after it has executed a setuid().
1841 */
1842static int proc_fd_permission(struct inode *inode, int mask,
1843 struct nameidata *nd)
1844{
1845 int rv;
1846
1847 rv = generic_permission(inode, mask, NULL);
1848 if (rv == 0)
1849 return 0;
1850 if (task_pid(current) == proc_pid(inode))
1851 rv = 0;
1852 return rv;
1853}
1854
1855/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 * proc directories can do almost nothing..
1857 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001858static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001860 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001861 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862};
1863
Miklos Szeredi27932742007-05-08 00:26:17 -07001864static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1865 struct dentry *dentry, struct task_struct *task, const void *ptr)
1866{
1867 unsigned fd = *(unsigned *)ptr;
1868 struct inode *inode;
1869 struct proc_inode *ei;
1870 struct dentry *error = ERR_PTR(-ENOENT);
1871
1872 inode = proc_pid_make_inode(dir->i_sb, task);
1873 if (!inode)
1874 goto out;
1875 ei = PROC_I(inode);
1876 ei->fd = fd;
1877 inode->i_mode = S_IFREG | S_IRUSR;
1878 inode->i_fop = &proc_fdinfo_file_operations;
1879 dentry->d_op = &tid_fd_dentry_operations;
1880 d_add(dentry, inode);
1881 /* Close the race of the process dying before we return the dentry */
1882 if (tid_fd_revalidate(dentry, NULL))
1883 error = NULL;
1884
1885 out:
1886 return error;
1887}
1888
1889static struct dentry *proc_lookupfdinfo(struct inode *dir,
1890 struct dentry *dentry,
1891 struct nameidata *nd)
1892{
1893 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1894}
1895
1896static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1897{
1898 return proc_readfd_common(filp, dirent, filldir,
1899 proc_fdinfo_instantiate);
1900}
1901
1902static const struct file_operations proc_fdinfo_operations = {
1903 .read = generic_read_dir,
1904 .readdir = proc_readfdinfo,
1905};
1906
1907/*
1908 * proc directories can do almost nothing..
1909 */
1910static const struct inode_operations proc_fdinfo_inode_operations = {
1911 .lookup = proc_lookupfdinfo,
1912 .setattr = proc_setattr,
1913};
1914
1915
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001916static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001917 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001918{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001919 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001920 struct inode *inode;
1921 struct proc_inode *ei;
1922 struct dentry *error = ERR_PTR(-EINVAL);
1923
Eric W. Biederman61a28782006-10-02 02:18:49 -07001924 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001925 if (!inode)
1926 goto out;
1927
1928 ei = PROC_I(inode);
1929 inode->i_mode = p->mode;
1930 if (S_ISDIR(inode->i_mode))
1931 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1932 if (p->iop)
1933 inode->i_op = p->iop;
1934 if (p->fop)
1935 inode->i_fop = p->fop;
1936 ei->op = p->op;
1937 dentry->d_op = &pid_dentry_operations;
1938 d_add(dentry, inode);
1939 /* Close the race of the process dying before we return the dentry */
1940 if (pid_revalidate(dentry, NULL))
1941 error = NULL;
1942out:
1943 return error;
1944}
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946static struct dentry *proc_pident_lookup(struct inode *dir,
1947 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001948 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001949 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001952 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001953 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001954 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001956 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 inode = NULL;
1958
Eric W. Biederman99f89552006-06-26 00:25:55 -07001959 if (!task)
1960 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001962 /*
1963 * Yes, it does not scale. And it should not. Don't add
1964 * new entries into /proc/<tgid>/ without very good reasons.
1965 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001966 last = &ents[nents - 1];
1967 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (p->len != dentry->d_name.len)
1969 continue;
1970 if (!memcmp(dentry->d_name.name, p->name, p->len))
1971 break;
1972 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001973 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto out;
1975
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001976 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001978 put_task_struct(task);
1979out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001980 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Eric Dumazetc5141e62007-05-08 00:26:15 -07001983static int proc_pident_fill_cache(struct file *filp, void *dirent,
1984 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001985{
1986 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
1987 proc_pident_instantiate, task, p);
1988}
1989
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001990static int proc_pident_readdir(struct file *filp,
1991 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001992 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001993{
1994 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001995 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001996 struct inode *inode = dentry->d_inode;
1997 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001998 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001999 ino_t ino;
2000 int ret;
2001
2002 ret = -ENOENT;
2003 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002004 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002005
2006 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002007 i = filp->f_pos;
2008 switch (i) {
2009 case 0:
2010 ino = inode->i_ino;
2011 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2012 goto out;
2013 i++;
2014 filp->f_pos++;
2015 /* fall through */
2016 case 1:
2017 ino = parent_ino(dentry);
2018 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2019 goto out;
2020 i++;
2021 filp->f_pos++;
2022 /* fall through */
2023 default:
2024 i -= 2;
2025 if (i >= nents) {
2026 ret = 1;
2027 goto out;
2028 }
2029 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002030 last = &ents[nents - 1];
2031 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002032 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002033 goto out;
2034 filp->f_pos++;
2035 p++;
2036 }
2037 }
2038
2039 ret = 1;
2040out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002041 put_task_struct(task);
2042out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002043 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002047static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2048 size_t count, loff_t *ppos)
2049{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002050 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002051 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002052 ssize_t length;
2053 struct task_struct *task = get_proc_task(inode);
2054
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002055 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002056 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002057
2058 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002059 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002060 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002061 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002062 if (length > 0)
2063 length = simple_read_from_buffer(buf, count, ppos, p, length);
2064 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002065 return length;
2066}
2067
2068static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2069 size_t count, loff_t *ppos)
2070{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002071 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002072 char *page;
2073 ssize_t length;
2074 struct task_struct *task = get_proc_task(inode);
2075
2076 length = -ESRCH;
2077 if (!task)
2078 goto out_no_task;
2079 if (count > PAGE_SIZE)
2080 count = PAGE_SIZE;
2081
2082 /* No partial writes. */
2083 length = -EINVAL;
2084 if (*ppos != 0)
2085 goto out;
2086
2087 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002088 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002089 if (!page)
2090 goto out;
2091
2092 length = -EFAULT;
2093 if (copy_from_user(page, buf, count))
2094 goto out_free;
2095
2096 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002097 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002098 (void*)page, count);
2099out_free:
2100 free_page((unsigned long) page);
2101out:
2102 put_task_struct(task);
2103out_no_task:
2104 return length;
2105}
2106
Arjan van de Ven00977a52007-02-12 00:55:34 -08002107static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002108 .read = proc_pid_attr_read,
2109 .write = proc_pid_attr_write,
2110};
2111
Eric Dumazetc5141e62007-05-08 00:26:15 -07002112static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002113 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2114 REG("prev", S_IRUGO, pid_attr),
2115 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2116 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2117 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2118 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002119};
2120
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002121static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 void * dirent, filldir_t filldir)
2123{
2124 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002125 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Arjan van de Ven00977a52007-02-12 00:55:34 -08002128static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002130 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131};
2132
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002133static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct dentry *dentry, struct nameidata *nd)
2135{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002136 return proc_pident_lookup(dir, dentry,
2137 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002140static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002141 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002142 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002143 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144};
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146#endif
2147
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002148#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2149static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2150 size_t count, loff_t *ppos)
2151{
2152 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2153 struct mm_struct *mm;
2154 char buffer[PROC_NUMBUF];
2155 size_t len;
2156 int ret;
2157
2158 if (!task)
2159 return -ESRCH;
2160
2161 ret = 0;
2162 mm = get_task_mm(task);
2163 if (mm) {
2164 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2165 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2166 MMF_DUMP_FILTER_SHIFT));
2167 mmput(mm);
2168 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2169 }
2170
2171 put_task_struct(task);
2172
2173 return ret;
2174}
2175
2176static ssize_t proc_coredump_filter_write(struct file *file,
2177 const char __user *buf,
2178 size_t count,
2179 loff_t *ppos)
2180{
2181 struct task_struct *task;
2182 struct mm_struct *mm;
2183 char buffer[PROC_NUMBUF], *end;
2184 unsigned int val;
2185 int ret;
2186 int i;
2187 unsigned long mask;
2188
2189 ret = -EFAULT;
2190 memset(buffer, 0, sizeof(buffer));
2191 if (count > sizeof(buffer) - 1)
2192 count = sizeof(buffer) - 1;
2193 if (copy_from_user(buffer, buf, count))
2194 goto out_no_task;
2195
2196 ret = -EINVAL;
2197 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2198 if (*end == '\n')
2199 end++;
2200 if (end - buffer == 0)
2201 goto out_no_task;
2202
2203 ret = -ESRCH;
2204 task = get_proc_task(file->f_dentry->d_inode);
2205 if (!task)
2206 goto out_no_task;
2207
2208 ret = end - buffer;
2209 mm = get_task_mm(task);
2210 if (!mm)
2211 goto out_no_mm;
2212
2213 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2214 if (val & mask)
2215 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2216 else
2217 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2218 }
2219
2220 mmput(mm);
2221 out_no_mm:
2222 put_task_struct(task);
2223 out_no_task:
2224 return ret;
2225}
2226
2227static const struct file_operations proc_coredump_filter_operations = {
2228 .read = proc_coredump_filter_read,
2229 .write = proc_coredump_filter_write,
2230};
2231#endif
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233/*
2234 * /proc/self:
2235 */
2236static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2237 int buflen)
2238{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002239 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002240 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002241 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002242 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002243 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002244 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 return vfs_readlink(dentry,buffer,buflen,tmp);
2246}
2247
Al Viro008b1502005-08-20 00:17:39 +01002248static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002250 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002251 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002252 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002253 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002254 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002255 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002256 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002257}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002259static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 .readlink = proc_self_readlink,
2261 .follow_link = proc_self_follow_link,
2262};
2263
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002264/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002265 * proc base
2266 *
2267 * These are the directory entries in the root directory of /proc
2268 * that properly belong to the /proc filesystem, as they describe
2269 * describe something that is process related.
2270 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002271static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002272 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002273 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002274};
2275
2276/*
2277 * Exceptional case: normally we are not allowed to unhash a busy
2278 * directory. In this case, however, we can do it - no aliasing problems
2279 * due to the way we treat inodes.
2280 */
2281static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2282{
2283 struct inode *inode = dentry->d_inode;
2284 struct task_struct *task = get_proc_task(inode);
2285 if (task) {
2286 put_task_struct(task);
2287 return 1;
2288 }
2289 d_drop(dentry);
2290 return 0;
2291}
2292
2293static struct dentry_operations proc_base_dentry_operations =
2294{
2295 .d_revalidate = proc_base_revalidate,
2296 .d_delete = pid_delete_dentry,
2297};
2298
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002299static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002300 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002301{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002302 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002303 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002304 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002305 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002306
2307 /* Allocate the inode */
2308 error = ERR_PTR(-ENOMEM);
2309 inode = new_inode(dir->i_sb);
2310 if (!inode)
2311 goto out;
2312
2313 /* Initialize the inode */
2314 ei = PROC_I(inode);
2315 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002316
2317 /*
2318 * grab the reference to the task.
2319 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002320 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002321 if (!ei->pid)
2322 goto out_iput;
2323
2324 inode->i_uid = 0;
2325 inode->i_gid = 0;
2326 inode->i_mode = p->mode;
2327 if (S_ISDIR(inode->i_mode))
2328 inode->i_nlink = 2;
2329 if (S_ISLNK(inode->i_mode))
2330 inode->i_size = 64;
2331 if (p->iop)
2332 inode->i_op = p->iop;
2333 if (p->fop)
2334 inode->i_fop = p->fop;
2335 ei->op = p->op;
2336 dentry->d_op = &proc_base_dentry_operations;
2337 d_add(dentry, inode);
2338 error = NULL;
2339out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002340 return error;
2341out_iput:
2342 iput(inode);
2343 goto out;
2344}
2345
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002346static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2347{
2348 struct dentry *error;
2349 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002350 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002351
2352 error = ERR_PTR(-ENOENT);
2353
2354 if (!task)
2355 goto out_no_task;
2356
2357 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002358 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2359 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002360 if (p->len != dentry->d_name.len)
2361 continue;
2362 if (!memcmp(dentry->d_name.name, p->name, p->len))
2363 break;
2364 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002365 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002366 goto out;
2367
2368 error = proc_base_instantiate(dir, dentry, task, p);
2369
2370out:
2371 put_task_struct(task);
2372out_no_task:
2373 return error;
2374}
2375
Eric Dumazetc5141e62007-05-08 00:26:15 -07002376static int proc_base_fill_cache(struct file *filp, void *dirent,
2377 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002378{
2379 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2380 proc_base_instantiate, task, p);
2381}
2382
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002383#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002384static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002385{
Andrea Righi297c5d92008-07-25 01:48:49 -07002386 u64 rchar, wchar, syscr, syscw;
2387 struct task_io_accounting ioac;
2388
2389 if (!whole) {
2390 rchar = task->rchar;
2391 wchar = task->wchar;
2392 syscr = task->syscr;
2393 syscw = task->syscw;
2394 memcpy(&ioac, &task->ioac, sizeof(ioac));
2395 } else {
2396 unsigned long flags;
2397 struct task_struct *t = task;
2398 rchar = wchar = syscr = syscw = 0;
2399 memset(&ioac, 0, sizeof(ioac));
2400
2401 rcu_read_lock();
2402 do {
2403 rchar += t->rchar;
2404 wchar += t->wchar;
2405 syscr += t->syscr;
2406 syscw += t->syscw;
2407
2408 ioac.read_bytes += t->ioac.read_bytes;
2409 ioac.write_bytes += t->ioac.write_bytes;
2410 ioac.cancelled_write_bytes +=
2411 t->ioac.cancelled_write_bytes;
2412 t = next_thread(t);
2413 } while (t != task);
2414 rcu_read_unlock();
2415
2416 if (lock_task_sighand(task, &flags)) {
2417 struct signal_struct *sig = task->signal;
2418
2419 rchar += sig->rchar;
2420 wchar += sig->wchar;
2421 syscr += sig->syscr;
2422 syscw += sig->syscw;
2423
2424 ioac.read_bytes += sig->ioac.read_bytes;
2425 ioac.write_bytes += sig->ioac.write_bytes;
2426 ioac.cancelled_write_bytes +=
2427 sig->ioac.cancelled_write_bytes;
2428
2429 unlock_task_sighand(task, &flags);
2430 }
2431 }
2432
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002433 return sprintf(buffer,
2434 "rchar: %llu\n"
2435 "wchar: %llu\n"
2436 "syscr: %llu\n"
2437 "syscw: %llu\n"
2438 "read_bytes: %llu\n"
2439 "write_bytes: %llu\n"
2440 "cancelled_write_bytes: %llu\n",
Andrea Righi297c5d92008-07-25 01:48:49 -07002441 (unsigned long long)rchar,
2442 (unsigned long long)wchar,
2443 (unsigned long long)syscr,
2444 (unsigned long long)syscw,
2445 (unsigned long long)ioac.read_bytes,
2446 (unsigned long long)ioac.write_bytes,
2447 (unsigned long long)ioac.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002448}
Andrea Righi297c5d92008-07-25 01:48:49 -07002449
2450static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2451{
2452 return do_io_accounting(task, buffer, 0);
2453}
2454
2455static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2456{
2457 return do_io_accounting(task, buffer, 1);
2458}
2459#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002460
Eric W. Biederman801199c2006-10-02 02:18:48 -07002461/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002462 * Thread groups
2463 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002464static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002465static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002466
Eric Dumazetc5141e62007-05-08 00:26:15 -07002467static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002468 DIR("task", S_IRUGO|S_IXUGO, task),
2469 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002470 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002471#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002472 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002473#endif
James Pearson315e28c2007-10-16 23:30:17 -07002474 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002475 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002476 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002477 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002478#ifdef CONFIG_SCHED_DEBUG
2479 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2480#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002481 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002482 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002483 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002484 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002485#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002486 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002487#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002488 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002489 LNK("cwd", cwd),
2490 LNK("root", root),
2491 LNK("exe", exe),
2492 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002493 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002494 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002495#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002496 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002497 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002498 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002499#endif
2500#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002501 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002502#endif
2503#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002504 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505#endif
2506#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002507 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002508#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002509#ifdef CONFIG_LATENCYTOP
2510 REG("latency", S_IRUGO, lstats),
2511#endif
Paul Menage8793d852007-10-18 23:39:39 -07002512#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002513 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514#endif
Paul Menagea4243162007-10-18 23:39:35 -07002515#ifdef CONFIG_CGROUPS
2516 REG("cgroup", S_IRUGO, cgroup),
2517#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002518 INF("oom_score", S_IRUGO, oom_score),
2519 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002520#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002521 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Steve Grubb6ee65042008-04-29 15:01:13 -04002522 REG("sessionid", S_IRUGO, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002523#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002524#ifdef CONFIG_FAULT_INJECTION
2525 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2526#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002527#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2528 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2529#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002530#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002531 INF("io", S_IRUGO, tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002532#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002533};
2534
2535static int proc_tgid_base_readdir(struct file * filp,
2536 void * dirent, filldir_t filldir)
2537{
2538 return proc_pident_readdir(filp,dirent,filldir,
2539 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2540}
2541
Arjan van de Ven00977a52007-02-12 00:55:34 -08002542static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002543 .read = generic_read_dir,
2544 .readdir = proc_tgid_base_readdir,
2545};
2546
2547static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002548 return proc_pident_lookup(dir, dentry,
2549 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002550}
2551
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002552static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002553 .lookup = proc_tgid_base_lookup,
2554 .getattr = pid_getattr,
2555 .setattr = proc_setattr,
2556};
2557
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002558static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002560 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002561 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002562 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Eric W. Biederman48e64842006-06-26 00:25:48 -07002564 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002565 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2566 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002567 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002568 if (!(current->flags & PF_EXITING))
2569 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002570 d_drop(dentry);
2571 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002574 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002575 goto out;
2576
2577 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002578 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2579 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002580 if (!leader)
2581 goto out;
2582
2583 name.name = "task";
2584 name.len = strlen(name.name);
2585 dir = d_hash_and_lookup(leader, &name);
2586 if (!dir)
2587 goto out_put_leader;
2588
2589 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002590 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002591 dentry = d_hash_and_lookup(dir, &name);
2592 if (dentry) {
2593 shrink_dcache_parent(dentry);
2594 d_drop(dentry);
2595 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002597
2598 dput(dir);
2599out_put_leader:
2600 dput(leader);
2601out:
2602 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
Randy Dunlap0895e912007-10-21 21:00:10 -07002605/**
2606 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2607 * @task: task that should be flushed.
2608 *
2609 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002610 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002611 * in. This call is supposed to do all of this job.
2612 *
2613 * Looks in the dcache for
2614 * /proc/@pid
2615 * /proc/@tgid/task/@pid
2616 * if either directory is present flushes it and all of it'ts children
2617 * from the dcache.
2618 *
2619 * It is safe and reasonable to cache /proc entries for a task until
2620 * that task exits. After that they just clog up the dcache with
2621 * useless entries, possibly causing useful dcache entries to be
2622 * flushed instead. This routine is proved to flush those useless
2623 * dcache entries at process exit time.
2624 *
2625 * NOTE: This routine is just an optimization so it does not guarantee
2626 * that no dcache entries will exist at process exit time it
2627 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002628 */
2629
2630void proc_flush_task(struct task_struct *task)
2631{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002632 int i;
2633 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002634 struct upid *upid;
2635
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002636 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002637 if (thread_group_leader(task))
2638 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002639
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002640 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002641 upid = &pid->numbers[i];
2642 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002643 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002644 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002645
2646 upid = &pid->numbers[pid->level];
2647 if (upid->nr == 1)
2648 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002649}
2650
Adrian Bunk9711ef92006-12-06 20:38:31 -08002651static struct dentry *proc_pid_instantiate(struct inode *dir,
2652 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002653 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002654{
2655 struct dentry *error = ERR_PTR(-ENOENT);
2656 struct inode *inode;
2657
Eric W. Biederman61a28782006-10-02 02:18:49 -07002658 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002659 if (!inode)
2660 goto out;
2661
2662 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2663 inode->i_op = &proc_tgid_base_inode_operations;
2664 inode->i_fop = &proc_tgid_base_operations;
2665 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002666
2667 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2668 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002669
2670 dentry->d_op = &pid_dentry_operations;
2671
2672 d_add(dentry, inode);
2673 /* Close the race of the process dying before we return the dentry */
2674 if (pid_revalidate(dentry, NULL))
2675 error = NULL;
2676out:
2677 return error;
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2681{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002682 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002685 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Eric W. Biederman801199c2006-10-02 02:18:48 -07002687 result = proc_base_lookup(dir, dentry);
2688 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2689 goto out;
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 tgid = name_to_int(dentry);
2692 if (tgid == ~0U)
2693 goto out;
2694
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002695 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002696 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002697 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 if (task)
2699 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (!task)
2702 goto out;
2703
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002704 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002705 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002707 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002711 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002712 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002714struct tgid_iter {
2715 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002716 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002717};
2718static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2719{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002720 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002722 if (iter.task)
2723 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002724 rcu_read_lock();
2725retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002726 iter.task = NULL;
2727 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002728 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002729 iter.tgid = pid_nr_ns(pid, ns);
2730 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002731 /* What we to know is if the pid we have find is the
2732 * pid of a thread_group_leader. Testing for task
2733 * being a thread_group_leader is the obvious thing
2734 * todo but there is a window when it fails, due to
2735 * the pid transfer logic in de_thread.
2736 *
2737 * So we perform the straight forward test of seeing
2738 * if the pid we have found is the pid of a thread
2739 * group leader, and don't worry if the task we have
2740 * found doesn't happen to be a thread group leader.
2741 * As we don't care in the case of readdir.
2742 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002743 if (!iter.task || !has_group_leader_pid(iter.task)) {
2744 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002745 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002746 }
2747 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002749 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002750 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002753#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
Eric W. Biederman61a28782006-10-02 02:18:49 -07002755static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002756 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002757{
2758 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002759 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002760 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002761 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764/* for the /proc/ directory itself, after non-process stuff has been done */
2765int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002768 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002769 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002770 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Eric W. Biederman61a28782006-10-02 02:18:49 -07002772 if (!reaper)
2773 goto out_no_task;
2774
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002775 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002776 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002777 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002781 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002782 iter.task = NULL;
2783 iter.tgid = filp->f_pos - TGID_OFFSET;
2784 for (iter = next_tgid(ns, iter);
2785 iter.task;
2786 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2787 filp->f_pos = iter.tgid + TGID_OFFSET;
2788 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2789 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002790 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002793 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2794out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002795 put_task_struct(reaper);
2796out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return 0;
2798}
2799
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002800/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002801 * Tasks
2802 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002803static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002804 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002805 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002806 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002807 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002808 ONE("status", S_IRUGO, pid_status),
Neil Hormand85f50d2007-10-18 23:40:37 -07002809 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002810#ifdef CONFIG_SCHED_DEBUG
2811 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2812#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002813 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002814 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002815 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002816 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002817#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002818 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002819#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002820 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002821 LNK("cwd", cwd),
2822 LNK("root", root),
2823 LNK("exe", exe),
2824 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002825 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002826#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002827 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002828 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002829 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002830#endif
2831#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002832 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002833#endif
2834#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002835 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002836#endif
2837#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002838 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002839#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002840#ifdef CONFIG_LATENCYTOP
2841 REG("latency", S_IRUGO, lstats),
2842#endif
Paul Menage8793d852007-10-18 23:39:39 -07002843#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002844 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002845#endif
Paul Menagea4243162007-10-18 23:39:35 -07002846#ifdef CONFIG_CGROUPS
2847 REG("cgroup", S_IRUGO, cgroup),
2848#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002849 INF("oom_score", S_IRUGO, oom_score),
2850 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002851#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002852 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002853 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002854#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002855#ifdef CONFIG_FAULT_INJECTION
2856 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2857#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002858#ifdef CONFIG_TASK_IO_ACCOUNTING
2859 INF("io", S_IRUGO, tid_io_accounting),
2860#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002861};
2862
2863static int proc_tid_base_readdir(struct file * filp,
2864 void * dirent, filldir_t filldir)
2865{
2866 return proc_pident_readdir(filp,dirent,filldir,
2867 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2868}
2869
2870static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002871 return proc_pident_lookup(dir, dentry,
2872 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002873}
2874
Arjan van de Ven00977a52007-02-12 00:55:34 -08002875static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002876 .read = generic_read_dir,
2877 .readdir = proc_tid_base_readdir,
2878};
2879
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002880static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002881 .lookup = proc_tid_base_lookup,
2882 .getattr = pid_getattr,
2883 .setattr = proc_setattr,
2884};
2885
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002886static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002887 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002888{
2889 struct dentry *error = ERR_PTR(-ENOENT);
2890 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002891 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002892
2893 if (!inode)
2894 goto out;
2895 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2896 inode->i_op = &proc_tid_base_inode_operations;
2897 inode->i_fop = &proc_tid_base_operations;
2898 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002899
2900 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2901 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002902
2903 dentry->d_op = &pid_dentry_operations;
2904
2905 d_add(dentry, inode);
2906 /* Close the race of the process dying before we return the dentry */
2907 if (pid_revalidate(dentry, NULL))
2908 error = NULL;
2909out:
2910 return error;
2911}
2912
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002913static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2914{
2915 struct dentry *result = ERR_PTR(-ENOENT);
2916 struct task_struct *task;
2917 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002918 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002919 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002920
2921 if (!leader)
2922 goto out_no_task;
2923
2924 tid = name_to_int(dentry);
2925 if (tid == ~0U)
2926 goto out;
2927
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002928 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002929 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002930 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002931 if (task)
2932 get_task_struct(task);
2933 rcu_read_unlock();
2934 if (!task)
2935 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002936 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002937 goto out_drop_task;
2938
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002939 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002940out_drop_task:
2941 put_task_struct(task);
2942out:
2943 put_task_struct(leader);
2944out_no_task:
2945 return result;
2946}
2947
2948/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002949 * Find the first tid of a thread group to return to user space.
2950 *
2951 * Usually this is just the thread group leader, but if the users
2952 * buffer was too small or there was a seek into the middle of the
2953 * directory we have more work todo.
2954 *
2955 * In the case of a short read we start with find_task_by_pid.
2956 *
2957 * In the case of a seek we start with the leader and walk nr
2958 * threads past it.
2959 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002960static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002961 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002962{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002963 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002964
Eric W. Biedermancc288732006-06-26 00:26:01 -07002965 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002966 /* Attempt to start with the pid of a thread */
2967 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002968 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002969 if (pos && (pos->group_leader == leader))
2970 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002971 }
2972
2973 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002974 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002975 if (nr && nr >= get_nr_threads(leader))
2976 goto out;
2977
2978 /* If we haven't found our starting place yet start
2979 * with the leader and walk nr threads forward.
2980 */
2981 for (pos = leader; nr > 0; --nr) {
2982 pos = next_thread(pos);
2983 if (pos == leader) {
2984 pos = NULL;
2985 goto out;
2986 }
2987 }
2988found:
2989 get_task_struct(pos);
2990out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002991 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002992 return pos;
2993}
2994
2995/*
2996 * Find the next thread in the thread list.
2997 * Return NULL if there is an error or no next thread.
2998 *
2999 * The reference to the input task_struct is released.
3000 */
3001static struct task_struct *next_tid(struct task_struct *start)
3002{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003003 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003004 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003005 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003006 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003007 if (thread_group_leader(pos))
3008 pos = NULL;
3009 else
3010 get_task_struct(pos);
3011 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003012 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003013 put_task_struct(start);
3014 return pos;
3015}
3016
Eric W. Biederman61a28782006-10-02 02:18:49 -07003017static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3018 struct task_struct *task, int tid)
3019{
3020 char name[PROC_NUMBUF];
3021 int len = snprintf(name, sizeof(name), "%d", tid);
3022 return proc_fill_cache(filp, dirent, filldir, name, len,
3023 proc_task_instantiate, task, NULL);
3024}
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026/* for the /proc/TGID/task/ directories */
3027static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3028{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003029 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003031 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003032 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 int retval = -ENOENT;
3034 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003035 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003037 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003039 task = get_proc_task(inode);
3040 if (!task)
3041 goto out_no_task;
3042 rcu_read_lock();
3043 if (pid_alive(task)) {
3044 leader = task->group_leader;
3045 get_task_struct(leader);
3046 }
3047 rcu_read_unlock();
3048 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003049 if (!leader)
3050 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 retval = 0;
3052
3053 switch (pos) {
3054 case 0:
3055 ino = inode->i_ino;
3056 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3057 goto out;
3058 pos++;
3059 /* fall through */
3060 case 1:
3061 ino = parent_ino(dentry);
3062 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3063 goto out;
3064 pos++;
3065 /* fall through */
3066 }
3067
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003068 /* f_version caches the tgid value that the last readdir call couldn't
3069 * return. lseek aka telldir automagically resets f_version to 0.
3070 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003071 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003072 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003073 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003074 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003075 task;
3076 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003077 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003078 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003079 /* returning this tgid failed, save it as the first
3080 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003081 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003082 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086out:
3087 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003088 put_task_struct(leader);
3089out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 return retval;
3091}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003092
3093static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3094{
3095 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003096 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003097 generic_fillattr(inode, stat);
3098
Eric W. Biederman99f89552006-06-26 00:25:55 -07003099 if (p) {
3100 rcu_read_lock();
3101 stat->nlink += get_nr_threads(p);
3102 rcu_read_unlock();
3103 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003104 }
3105
3106 return 0;
3107}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003108
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003109static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003110 .lookup = proc_task_lookup,
3111 .getattr = proc_task_getattr,
3112 .setattr = proc_setattr,
3113};
3114
Arjan van de Ven00977a52007-02-12 00:55:34 -08003115static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003116 .read = generic_read_dir,
3117 .readdir = proc_task_readdir,
3118};