blob: c1332dd2575d16c33d5bd3b7075cef755e9cb976 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/proc/base.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * proc base directory handling functions
7 *
8 * 1999, Al Viro. Rewritten. Now it covers the whole per-process part.
9 * Instead of using magical inumbers to determine the kind of object
10 * we allocate and fill in-core inodes upon lookup. They don't even
11 * go into icache. We cache the reference to task_struct upon lookup too.
12 * Eventually it should become a filesystem in its own. We don't use the
13 * rest of procfs anymore.
Mauricio Line070ad42005-09-03 15:55:10 -070014 *
15 *
16 * Changelog:
17 * 17-Jan-2005
18 * Allan Bezerra
19 * Bruna Moreira <bruna.moreira@indt.org.br>
20 * Edjard Mota <edjard.mota@indt.org.br>
21 * Ilias Biris <ilias.biris@indt.org.br>
22 * Mauricio Lin <mauricio.lin@indt.org.br>
23 *
24 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
25 *
26 * A new process specific entry (smaps) included in /proc. It shows the
27 * size of rss for each memory area. The maps entry lacks information
28 * about physical memory size (rss) for each mapped file, i.e.,
29 * rss information for executables and library files.
30 * This additional information is useful for any tools that need to know
31 * about physical memory consumption for a process specific library.
32 *
33 * Changelog:
34 * 21-Feb-2005
35 * Embedded Linux Lab - 10LE Instituto Nokia de Tecnologia - INdT
36 * Pud inclusion in the page table walking.
37 *
38 * ChangeLog:
39 * 10-Mar-2005
40 * 10LE Instituto Nokia de Tecnologia - INdT:
41 * A better way to walks through the page table as suggested by Hugh Dickins.
42 *
43 * Simo Piiroinen <simo.piiroinen@nokia.com>:
44 * Smaps information related to shared, private, clean and dirty pages.
45 *
46 * Paul Mundt <paul.mundt@nokia.com>:
47 * Overall revision about smaps.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
50#include <asm/uaccess.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/errno.h>
53#include <linux/time.h>
54#include <linux/proc_fs.h>
55#include <linux/stat.h>
Andrea Righi59954772008-07-27 17:29:15 +020056#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/init.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080058#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040060#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/string.h>
62#include <linux/seq_file.h>
63#include <linux/namei.h>
Kirill Korotaev6b3286e2006-12-08 02:37:56 -080064#include <linux/mnt_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/mm.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070066#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kallsyms.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070068#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070069#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#include <linux/mount.h>
71#include <linux/security.h>
72#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070073#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070074#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/cpuset.h>
76#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050077#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070078#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070079#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070080#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070081#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include "internal.h"
83
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070084/* NOTE:
85 * Implementing inode permission operations in /proc is almost
86 * certainly an error. Permission checks need to happen during
87 * each system call not at open time. The reason is that most of
88 * what we wish to check for permissions in /proc varies at runtime.
89 *
90 * The classic example of a problem is opening file descriptors
91 * in /proc for a task before it execs a suid executable.
92 */
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -070096 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -080098 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -080099 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700100 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Eric W. Biederman61a28782006-10-02 02:18:49 -0700103#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700105 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700106 .mode = MODE, \
107 .iop = IOP, \
108 .fop = FOP, \
109 .op = OP, \
110}
111
Eric W. Biederman61a28782006-10-02 02:18:49 -0700112#define DIR(NAME, MODE, OTYPE) \
113 NOD(NAME, (S_IFDIR|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700114 &proc_##OTYPE##_inode_operations, &proc_##OTYPE##_operations, \
115 {} )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700116#define LNK(NAME, OTYPE) \
117 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700118 &proc_pid_link_inode_operations, NULL, \
119 { .proc_get_link = &proc_##OTYPE##_link } )
Eric W. Biederman61a28782006-10-02 02:18:49 -0700120#define REG(NAME, MODE, OTYPE) \
121 NOD(NAME, (S_IFREG|(MODE)), NULL, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700122 &proc_##OTYPE##_operations, {})
Eric W. Biederman61a28782006-10-02 02:18:49 -0700123#define INF(NAME, MODE, OTYPE) \
124 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700125 NULL, &proc_info_file_operations, \
126 { .proc_read = &proc_##OTYPE } )
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800127#define ONE(NAME, MODE, OTYPE) \
128 NOD(NAME, (S_IFREG|(MODE)), \
129 NULL, &proc_single_file_operations, \
130 { .proc_show = &proc_##OTYPE } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Vegard Nossumaed54172008-06-05 22:46:53 -0700132/*
133 * Count the number of hardlinks for the pid_entry table, excluding the .
134 * and .. links.
135 */
136static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
137 unsigned int n)
138{
139 unsigned int i;
140 unsigned int count;
141
142 count = 0;
143 for (i = 0; i < n; ++i) {
144 if (S_ISDIR(entries[i].mode))
145 ++count;
146 }
147
148 return count;
149}
150
Kees Cook5096add2007-05-08 00:26:04 -0700151int maps_protect;
152EXPORT_SYMBOL(maps_protect);
153
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700154static struct fs_struct *get_fs_struct(struct task_struct *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct fs_struct *fs;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157 task_lock(task);
158 fs = task->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if(fs)
160 atomic_inc(&fs->count);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700161 task_unlock(task);
162 return fs;
163}
164
Eric W. Biederman99f89552006-06-26 00:25:55 -0700165static int get_nr_threads(struct task_struct *tsk)
166{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700167 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
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400473 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700474 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700475 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
476 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700477
478 /*
479 * print the file header
480 */
481 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
482 "Limit", "Soft Limit", "Hard Limit", "Units");
483
484 for (i = 0; i < RLIM_NLIMITS; i++) {
485 if (rlim[i].rlim_cur == RLIM_INFINITY)
486 count += sprintf(&bufptr[count], "%-25s %-20s ",
487 lnames[i].name, "unlimited");
488 else
489 count += sprintf(&bufptr[count], "%-25s %-20lu ",
490 lnames[i].name, rlim[i].rlim_cur);
491
492 if (rlim[i].rlim_max == RLIM_INFINITY)
493 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
494 else
495 count += sprintf(&bufptr[count], "%-20lu ",
496 rlim[i].rlim_max);
497
498 if (lnames[i].unit)
499 count += sprintf(&bufptr[count], "%-10s\n",
500 lnames[i].unit);
501 else
502 count += sprintf(&bufptr[count], "\n");
503 }
504
505 return count;
506}
507
Roland McGrathebcb6732008-07-25 19:46:00 -0700508#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
509static int proc_pid_syscall(struct task_struct *task, char *buffer)
510{
511 long nr;
512 unsigned long args[6], sp, pc;
513
514 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
515 return sprintf(buffer, "running\n");
516
517 if (nr < 0)
518 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
519
520 return sprintf(buffer,
521 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
522 nr,
523 args[0], args[1], args[2], args[3], args[4], args[5],
524 sp, pc);
525}
526#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528/************************************************************************/
529/* Here the fs part begins */
530/************************************************************************/
531
532/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700533static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700535 struct task_struct *task;
536 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700537 /* Allow access to a task's file descriptors if it is us or we
538 * may use ptrace attach to the process and find out that
539 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700540 */
541 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700542 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400543 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700544 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700545 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700546 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700549static int proc_setattr(struct dentry *dentry, struct iattr *attr)
550{
551 int error;
552 struct inode *inode = dentry->d_inode;
553
554 if (attr->ia_valid & ATTR_MODE)
555 return -EPERM;
556
557 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700558 if (!error)
559 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700560 return error;
561}
562
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800563static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700564 .setattr = proc_setattr,
565};
566
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100567static int mounts_open_common(struct inode *inode, struct file *file,
568 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700570 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700571 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800572 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100573 struct fs_struct *fs = NULL;
574 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500575 struct proc_mounts *p;
576 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Eric W. Biederman99f89552006-06-26 00:25:55 -0700578 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700579 rcu_read_lock();
580 nsp = task_nsproxy(task);
581 if (nsp) {
582 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800583 if (ns)
584 get_mnt_ns(ns);
585 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700586 rcu_read_unlock();
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100587 if (ns)
588 fs = get_fs_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700589 put_task_struct(task);
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100592 if (!ns)
593 goto err;
594 if (!fs)
595 goto err_put_ns;
596
597 read_lock(&fs->lock);
598 root = fs->root;
599 path_get(&root);
600 read_unlock(&fs->lock);
601 put_fs_struct(fs);
602
603 ret = -ENOMEM;
604 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
605 if (!p)
606 goto err_put_path;
607
608 file->private_data = &p->m;
609 ret = seq_open(file, op);
610 if (ret)
611 goto err_free;
612
613 p->m.private = p;
614 p->ns = ns;
615 p->root = root;
616 p->event = ns->event;
617
618 return 0;
619
620 err_free:
621 kfree(p);
622 err_put_path:
623 path_put(&root);
624 err_put_ns:
625 put_mnt_ns(ns);
626 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return ret;
628}
629
630static int mounts_release(struct inode *inode, struct file *file)
631{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100632 struct proc_mounts *p = file->private_data;
633 path_put(&p->root);
634 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return seq_release(inode, file);
636}
637
Al Viro5addc5d2005-11-07 17:15:49 -0500638static unsigned mounts_poll(struct file *file, poll_table *wait)
639{
640 struct proc_mounts *p = file->private_data;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100641 struct mnt_namespace *ns = p->ns;
Al Viro5addc5d2005-11-07 17:15:49 -0500642 unsigned res = 0;
643
644 poll_wait(file, &ns->poll, wait);
645
646 spin_lock(&vfsmount_lock);
647 if (p->event != ns->event) {
648 p->event = ns->event;
649 res = POLLERR;
650 }
651 spin_unlock(&vfsmount_lock);
652
653 return res;
654}
655
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100656static int mounts_open(struct inode *inode, struct file *file)
657{
658 return mounts_open_common(inode, file, &mounts_op);
659}
660
Arjan van de Ven00977a52007-02-12 00:55:34 -0800661static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .open = mounts_open,
663 .read = seq_read,
664 .llseek = seq_lseek,
665 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500666 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667};
668
Ram Pai2d4d4862008-03-27 13:06:25 +0100669static int mountinfo_open(struct inode *inode, struct file *file)
670{
671 return mounts_open_common(inode, file, &mountinfo_op);
672}
673
674static const struct file_operations proc_mountinfo_operations = {
675 .open = mountinfo_open,
676 .read = seq_read,
677 .llseek = seq_lseek,
678 .release = mounts_release,
679 .poll = mounts_poll,
680};
681
Chuck Leverb4629fe2006-03-20 13:44:12 -0500682static int mountstats_open(struct inode *inode, struct file *file)
683{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100684 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500685}
686
Arjan van de Ven00977a52007-02-12 00:55:34 -0800687static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500688 .open = mountstats_open,
689 .read = seq_read,
690 .llseek = seq_lseek,
691 .release = mounts_release,
692};
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
695
696static ssize_t proc_info_read(struct file * file, char __user * buf,
697 size_t count, loff_t *ppos)
698{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800699 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 unsigned long page;
701 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700702 struct task_struct *task = get_proc_task(inode);
703
704 length = -ESRCH;
705 if (!task)
706 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (count > PROC_BLOCK_SIZE)
709 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700710
711 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700712 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700713 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 length = PROC_I(inode)->op.proc_read(task, (char*)page);
716
717 if (length >= 0)
718 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
719 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700720out:
721 put_task_struct(task);
722out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return length;
724}
725
Arjan van de Ven00977a52007-02-12 00:55:34 -0800726static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 .read = proc_info_read,
728};
729
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800730static int proc_single_show(struct seq_file *m, void *v)
731{
732 struct inode *inode = m->private;
733 struct pid_namespace *ns;
734 struct pid *pid;
735 struct task_struct *task;
736 int ret;
737
738 ns = inode->i_sb->s_fs_info;
739 pid = proc_pid(inode);
740 task = get_pid_task(pid, PIDTYPE_PID);
741 if (!task)
742 return -ESRCH;
743
744 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
745
746 put_task_struct(task);
747 return ret;
748}
749
750static int proc_single_open(struct inode *inode, struct file *filp)
751{
752 int ret;
753 ret = single_open(filp, proc_single_show, NULL);
754 if (!ret) {
755 struct seq_file *m = filp->private_data;
756
757 m->private = inode;
758 }
759 return ret;
760}
761
762static const struct file_operations proc_single_file_operations = {
763 .open = proc_single_open,
764 .read = seq_read,
765 .llseek = seq_lseek,
766 .release = single_release,
767};
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769static int mem_open(struct inode* inode, struct file* file)
770{
771 file->private_data = (void*)((long)current->self_exec_id);
772 return 0;
773}
774
775static ssize_t mem_read(struct file * file, char __user * buf,
776 size_t count, loff_t *ppos)
777{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800778 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 char *page;
780 unsigned long src = *ppos;
781 int ret = -ESRCH;
782 struct mm_struct *mm;
783
Eric W. Biederman99f89552006-06-26 00:25:55 -0700784 if (!task)
785 goto out_no_task;
786
Roland McGrath638fa202008-04-29 01:01:38 -0700787 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto out;
789
790 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700791 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!page)
793 goto out;
794
795 ret = 0;
796
797 mm = get_task_mm(task);
798 if (!mm)
799 goto out_free;
800
801 ret = -EIO;
802
803 if (file->private_data != (void*)((long)current->self_exec_id))
804 goto out_put;
805
806 ret = 0;
807
808 while (count > 0) {
809 int this_len, retval;
810
811 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
812 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa202008-04-29 01:01:38 -0700813 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!ret)
815 ret = -EIO;
816 break;
817 }
818
819 if (copy_to_user(buf, page, retval)) {
820 ret = -EFAULT;
821 break;
822 }
823
824 ret += retval;
825 src += retval;
826 buf += retval;
827 count -= retval;
828 }
829 *ppos = src;
830
831out_put:
832 mmput(mm);
833out_free:
834 free_page((unsigned long) page);
835out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700836 put_task_struct(task);
837out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return ret;
839}
840
841#define mem_write NULL
842
843#ifndef mem_write
844/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800845static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 size_t count, loff_t *ppos)
847{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700848 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800850 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 unsigned long dst = *ppos;
852
Eric W. Biederman99f89552006-06-26 00:25:55 -0700853 copied = -ESRCH;
854 if (!task)
855 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Roland McGrath638fa202008-04-29 01:01:38 -0700857 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700858 goto out;
859
860 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700861 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700865 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 while (count > 0) {
867 int this_len, retval;
868
869 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
870 if (copy_from_user(page, buf, this_len)) {
871 copied = -EFAULT;
872 break;
873 }
874 retval = access_process_vm(task, dst, page, this_len, 1);
875 if (!retval) {
876 if (!copied)
877 copied = -EIO;
878 break;
879 }
880 copied += retval;
881 buf += retval;
882 dst += retval;
883 count -= retval;
884 }
885 *ppos = dst;
886 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700887out:
888 put_task_struct(task);
889out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return copied;
891}
892#endif
893
Matt Mackall85863e42008-02-04 22:29:04 -0800894loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 switch (orig) {
897 case 0:
898 file->f_pos = offset;
899 break;
900 case 1:
901 file->f_pos += offset;
902 break;
903 default:
904 return -EINVAL;
905 }
906 force_successful_syscall_return();
907 return file->f_pos;
908}
909
Arjan van de Ven00977a52007-02-12 00:55:34 -0800910static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 .llseek = mem_lseek,
912 .read = mem_read,
913 .write = mem_write,
914 .open = mem_open,
915};
916
James Pearson315e28c2007-10-16 23:30:17 -0700917static ssize_t environ_read(struct file *file, char __user *buf,
918 size_t count, loff_t *ppos)
919{
920 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
921 char *page;
922 unsigned long src = *ppos;
923 int ret = -ESRCH;
924 struct mm_struct *mm;
925
926 if (!task)
927 goto out_no_task;
928
Stephen Smalley006ebb42008-05-19 08:32:49 -0400929 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700930 goto out;
931
932 ret = -ENOMEM;
933 page = (char *)__get_free_page(GFP_TEMPORARY);
934 if (!page)
935 goto out;
936
937 ret = 0;
938
939 mm = get_task_mm(task);
940 if (!mm)
941 goto out_free;
942
943 while (count > 0) {
944 int this_len, retval, max_len;
945
946 this_len = mm->env_end - (mm->env_start + src);
947
948 if (this_len <= 0)
949 break;
950
951 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
952 this_len = (this_len > max_len) ? max_len : this_len;
953
954 retval = access_process_vm(task, (mm->env_start + src),
955 page, this_len, 0);
956
957 if (retval <= 0) {
958 ret = retval;
959 break;
960 }
961
962 if (copy_to_user(buf, page, retval)) {
963 ret = -EFAULT;
964 break;
965 }
966
967 ret += retval;
968 src += retval;
969 buf += retval;
970 count -= retval;
971 }
972 *ppos = src;
973
974 mmput(mm);
975out_free:
976 free_page((unsigned long) page);
977out:
978 put_task_struct(task);
979out_no_task:
980 return ret;
981}
982
983static const struct file_operations proc_environ_operations = {
984 .read = environ_read,
985};
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987static ssize_t oom_adjust_read(struct file *file, char __user *buf,
988 size_t count, loff_t *ppos)
989{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800990 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700991 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 size_t len;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700993 int oom_adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Eric W. Biederman99f89552006-06-26 00:25:55 -0700995 if (!task)
996 return -ESRCH;
997 oom_adjust = task->oomkilladj;
998 put_task_struct(task);
999
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001000 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001001
1002 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003}
1004
1005static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1006 size_t count, loff_t *ppos)
1007{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001008 struct task_struct *task;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001009 char buffer[PROC_NUMBUF], *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int oom_adjust;
1011
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001012 memset(buffer, 0, sizeof(buffer));
1013 if (count > sizeof(buffer) - 1)
1014 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (copy_from_user(buffer, buf, count))
1016 return -EFAULT;
1017 oom_adjust = simple_strtol(buffer, &end, 0);
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001018 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1019 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 return -EINVAL;
1021 if (*end == '\n')
1022 end++;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001023 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001024 if (!task)
1025 return -ESRCH;
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001026 if (oom_adjust < task->oomkilladj && !capable(CAP_SYS_RESOURCE)) {
1027 put_task_struct(task);
1028 return -EACCES;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 task->oomkilladj = oom_adjust;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001031 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (end - buffer == 0)
1033 return -EIO;
1034 return end - buffer;
1035}
1036
Arjan van de Ven00977a52007-02-12 00:55:34 -08001037static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 .read = oom_adjust_read,
1039 .write = oom_adjust_write,
1040};
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042#ifdef CONFIG_AUDITSYSCALL
1043#define TMPBUFLEN 21
1044static ssize_t proc_loginuid_read(struct file * file, 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;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001048 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 ssize_t length;
1050 char tmpbuf[TMPBUFLEN];
1051
Eric W. Biederman99f89552006-06-26 00:25:55 -07001052 if (!task)
1053 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001055 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001056 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1058}
1059
1060static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1061 size_t count, loff_t *ppos)
1062{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001063 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 char *page, *tmp;
1065 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 uid_t loginuid;
1067
1068 if (!capable(CAP_AUDIT_CONTROL))
1069 return -EPERM;
1070
Eric W. Biederman13b41b02006-06-26 00:25:56 -07001071 if (current != pid_task(proc_pid(inode), PIDTYPE_PID))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -EPERM;
1073
Al Viroe0182902006-05-18 08:28:02 -04001074 if (count >= PAGE_SIZE)
1075 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 if (*ppos != 0) {
1078 /* No partial writes. */
1079 return -EINVAL;
1080 }
Mel Gormane12ba742007-10-16 01:25:52 -07001081 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (!page)
1083 return -ENOMEM;
1084 length = -EFAULT;
1085 if (copy_from_user(page, buf, count))
1086 goto out_free_page;
1087
Al Viroe0182902006-05-18 08:28:02 -04001088 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 loginuid = simple_strtoul(page, &tmp, 10);
1090 if (tmp == page) {
1091 length = -EINVAL;
1092 goto out_free_page;
1093
1094 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001095 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (likely(length == 0))
1097 length = count;
1098
1099out_free_page:
1100 free_page((unsigned long) page);
1101 return length;
1102}
1103
Arjan van de Ven00977a52007-02-12 00:55:34 -08001104static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 .read = proc_loginuid_read,
1106 .write = proc_loginuid_write,
1107};
Eric Paris1e0bd752008-03-13 08:15:31 -04001108
1109static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1110 size_t count, loff_t *ppos)
1111{
1112 struct inode * inode = file->f_path.dentry->d_inode;
1113 struct task_struct *task = get_proc_task(inode);
1114 ssize_t length;
1115 char tmpbuf[TMPBUFLEN];
1116
1117 if (!task)
1118 return -ESRCH;
1119 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1120 audit_get_sessionid(task));
1121 put_task_struct(task);
1122 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1123}
1124
1125static const struct file_operations proc_sessionid_operations = {
1126 .read = proc_sessionid_read,
1127};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#endif
1129
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001130#ifdef CONFIG_FAULT_INJECTION
1131static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1132 size_t count, loff_t *ppos)
1133{
1134 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1135 char buffer[PROC_NUMBUF];
1136 size_t len;
1137 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001138
1139 if (!task)
1140 return -ESRCH;
1141 make_it_fail = task->make_it_fail;
1142 put_task_struct(task);
1143
1144 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001145
1146 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001147}
1148
1149static ssize_t proc_fault_inject_write(struct file * file,
1150 const char __user * buf, size_t count, loff_t *ppos)
1151{
1152 struct task_struct *task;
1153 char buffer[PROC_NUMBUF], *end;
1154 int make_it_fail;
1155
1156 if (!capable(CAP_SYS_RESOURCE))
1157 return -EPERM;
1158 memset(buffer, 0, sizeof(buffer));
1159 if (count > sizeof(buffer) - 1)
1160 count = sizeof(buffer) - 1;
1161 if (copy_from_user(buffer, buf, count))
1162 return -EFAULT;
1163 make_it_fail = simple_strtol(buffer, &end, 0);
1164 if (*end == '\n')
1165 end++;
1166 task = get_proc_task(file->f_dentry->d_inode);
1167 if (!task)
1168 return -ESRCH;
1169 task->make_it_fail = make_it_fail;
1170 put_task_struct(task);
1171 if (end - buffer == 0)
1172 return -EIO;
1173 return end - buffer;
1174}
1175
Arjan van de Ven00977a52007-02-12 00:55:34 -08001176static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001177 .read = proc_fault_inject_read,
1178 .write = proc_fault_inject_write,
1179};
1180#endif
1181
Arjan van de Ven97455122008-01-25 21:08:34 +01001182
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001183#ifdef CONFIG_SCHED_DEBUG
1184/*
1185 * Print out various scheduling related per-task fields:
1186 */
1187static int sched_show(struct seq_file *m, void *v)
1188{
1189 struct inode *inode = m->private;
1190 struct task_struct *p;
1191
1192 WARN_ON(!inode);
1193
1194 p = get_proc_task(inode);
1195 if (!p)
1196 return -ESRCH;
1197 proc_sched_show_task(p, m);
1198
1199 put_task_struct(p);
1200
1201 return 0;
1202}
1203
1204static ssize_t
1205sched_write(struct file *file, const char __user *buf,
1206 size_t count, loff_t *offset)
1207{
1208 struct inode *inode = file->f_path.dentry->d_inode;
1209 struct task_struct *p;
1210
1211 WARN_ON(!inode);
1212
1213 p = get_proc_task(inode);
1214 if (!p)
1215 return -ESRCH;
1216 proc_sched_set_task(p);
1217
1218 put_task_struct(p);
1219
1220 return count;
1221}
1222
1223static int sched_open(struct inode *inode, struct file *filp)
1224{
1225 int ret;
1226
1227 ret = single_open(filp, sched_show, NULL);
1228 if (!ret) {
1229 struct seq_file *m = filp->private_data;
1230
1231 m->private = inode;
1232 }
1233 return ret;
1234}
1235
1236static const struct file_operations proc_pid_sched_operations = {
1237 .open = sched_open,
1238 .read = seq_read,
1239 .write = sched_write,
1240 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001241 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001242};
1243
1244#endif
1245
Matt Helsley925d1c42008-04-29 01:01:36 -07001246/*
1247 * We added or removed a vma mapping the executable. The vmas are only mapped
1248 * during exec and are not mapped with the mmap system call.
1249 * Callers must hold down_write() on the mm's mmap_sem for these
1250 */
1251void added_exe_file_vma(struct mm_struct *mm)
1252{
1253 mm->num_exe_file_vmas++;
1254}
1255
1256void removed_exe_file_vma(struct mm_struct *mm)
1257{
1258 mm->num_exe_file_vmas--;
1259 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1260 fput(mm->exe_file);
1261 mm->exe_file = NULL;
1262 }
1263
1264}
1265
1266void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1267{
1268 if (new_exe_file)
1269 get_file(new_exe_file);
1270 if (mm->exe_file)
1271 fput(mm->exe_file);
1272 mm->exe_file = new_exe_file;
1273 mm->num_exe_file_vmas = 0;
1274}
1275
1276struct file *get_mm_exe_file(struct mm_struct *mm)
1277{
1278 struct file *exe_file;
1279
1280 /* We need mmap_sem to protect against races with removal of
1281 * VM_EXECUTABLE vmas */
1282 down_read(&mm->mmap_sem);
1283 exe_file = mm->exe_file;
1284 if (exe_file)
1285 get_file(exe_file);
1286 up_read(&mm->mmap_sem);
1287 return exe_file;
1288}
1289
1290void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1291{
1292 /* It's safe to write the exe_file pointer without exe_file_lock because
1293 * this is called during fork when the task is not yet in /proc */
1294 newmm->exe_file = get_mm_exe_file(oldmm);
1295}
1296
1297static int proc_exe_link(struct inode *inode, struct path *exe_path)
1298{
1299 struct task_struct *task;
1300 struct mm_struct *mm;
1301 struct file *exe_file;
1302
1303 task = get_proc_task(inode);
1304 if (!task)
1305 return -ENOENT;
1306 mm = get_task_mm(task);
1307 put_task_struct(task);
1308 if (!mm)
1309 return -ENOENT;
1310 exe_file = get_mm_exe_file(mm);
1311 mmput(mm);
1312 if (exe_file) {
1313 *exe_path = exe_file->f_path;
1314 path_get(&exe_file->f_path);
1315 fput(exe_file);
1316 return 0;
1317 } else
1318 return -ENOENT;
1319}
1320
Al Viro008b1502005-08-20 00:17:39 +01001321static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct inode *inode = dentry->d_inode;
1324 int error = -EACCES;
1325
1326 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001327 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Eric W. Biederman778c1142006-06-26 00:25:58 -07001329 /* Are we allowed to snoop on the tasks file descriptors? */
1330 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001333 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 nd->last_type = LAST_BIND;
1335out:
Al Viro008b1502005-08-20 00:17:39 +01001336 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001339static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Mel Gormane12ba742007-10-16 01:25:52 -07001341 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001342 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 int len;
1344
1345 if (!tmp)
1346 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001347
Jan Blunckcf28b482008-02-14 19:38:44 -08001348 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001349 len = PTR_ERR(pathname);
1350 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001352 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 if (len > buflen)
1355 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001356 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 len = -EFAULT;
1358 out:
1359 free_page((unsigned long)tmp);
1360 return len;
1361}
1362
1363static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1364{
1365 int error = -EACCES;
1366 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001367 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Eric W. Biederman778c1142006-06-26 00:25:58 -07001369 /* Are we allowed to snoop on the tasks file descriptors? */
1370 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001373 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (error)
1375 goto out;
1376
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001377 error = do_proc_readlink(&path, buffer, buflen);
1378 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 return error;
1381}
1382
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001383static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001385 .follow_link = proc_pid_follow_link,
1386 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387};
1388
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001389
1390/* building an inode */
1391
1392static int task_dumpable(struct task_struct *task)
1393{
1394 int dumpable = 0;
1395 struct mm_struct *mm;
1396
1397 task_lock(task);
1398 mm = task->mm;
1399 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001400 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001401 task_unlock(task);
1402 if(dumpable == 1)
1403 return 1;
1404 return 0;
1405}
1406
1407
Eric W. Biederman61a28782006-10-02 02:18:49 -07001408static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001409{
1410 struct inode * inode;
1411 struct proc_inode *ei;
1412
1413 /* We need a new inode */
1414
1415 inode = new_inode(sb);
1416 if (!inode)
1417 goto out;
1418
1419 /* Common stuff */
1420 ei = PROC_I(inode);
1421 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001422 inode->i_op = &proc_def_inode_operations;
1423
1424 /*
1425 * grab the reference to task.
1426 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001427 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001428 if (!ei->pid)
1429 goto out_unlock;
1430
1431 inode->i_uid = 0;
1432 inode->i_gid = 0;
1433 if (task_dumpable(task)) {
1434 inode->i_uid = task->euid;
1435 inode->i_gid = task->egid;
1436 }
1437 security_task_to_inode(task, inode);
1438
1439out:
1440 return inode;
1441
1442out_unlock:
1443 iput(inode);
1444 return NULL;
1445}
1446
1447static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1448{
1449 struct inode *inode = dentry->d_inode;
1450 struct task_struct *task;
1451 generic_fillattr(inode, stat);
1452
1453 rcu_read_lock();
1454 stat->uid = 0;
1455 stat->gid = 0;
1456 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1457 if (task) {
1458 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1459 task_dumpable(task)) {
1460 stat->uid = task->euid;
1461 stat->gid = task->egid;
1462 }
1463 }
1464 rcu_read_unlock();
1465 return 0;
1466}
1467
1468/* dentry stuff */
1469
1470/*
1471 * Exceptional case: normally we are not allowed to unhash a busy
1472 * directory. In this case, however, we can do it - no aliasing problems
1473 * due to the way we treat inodes.
1474 *
1475 * Rewrite the inode's ownerships here because the owning task may have
1476 * performed a setuid(), etc.
1477 *
1478 * Before the /proc/pid/status file was created the only way to read
1479 * the effective uid of a /process was to stat /proc/pid. Reading
1480 * /proc/pid/status is slow enough that procps and other packages
1481 * kept stating /proc/pid. To keep the rules in /proc simple I have
1482 * made this apply to all per process world readable and executable
1483 * directories.
1484 */
1485static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1486{
1487 struct inode *inode = dentry->d_inode;
1488 struct task_struct *task = get_proc_task(inode);
1489 if (task) {
1490 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1491 task_dumpable(task)) {
1492 inode->i_uid = task->euid;
1493 inode->i_gid = task->egid;
1494 } else {
1495 inode->i_uid = 0;
1496 inode->i_gid = 0;
1497 }
1498 inode->i_mode &= ~(S_ISUID | S_ISGID);
1499 security_task_to_inode(task, inode);
1500 put_task_struct(task);
1501 return 1;
1502 }
1503 d_drop(dentry);
1504 return 0;
1505}
1506
1507static int pid_delete_dentry(struct dentry * dentry)
1508{
1509 /* Is the task we represent dead?
1510 * If so, then don't put the dentry on the lru list,
1511 * kill it immediately.
1512 */
1513 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1514}
1515
1516static struct dentry_operations pid_dentry_operations =
1517{
1518 .d_revalidate = pid_revalidate,
1519 .d_delete = pid_delete_dentry,
1520};
1521
1522/* Lookups */
1523
Eric Dumazetc5141e62007-05-08 00:26:15 -07001524typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1525 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001526
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001527/*
1528 * Fill a directory entry.
1529 *
1530 * If possible create the dcache entry and derive our inode number and
1531 * file type from dcache entry.
1532 *
1533 * Since all of the proc inode numbers are dynamically generated, the inode
1534 * numbers do not exist until the inode is cache. This means creating the
1535 * the dcache entry in readdir is necessary to keep the inode numbers
1536 * reported by readdir in sync with the inode numbers reported
1537 * by stat.
1538 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001539static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1540 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001541 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001542{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001543 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001544 struct inode *inode;
1545 struct qstr qname;
1546 ino_t ino = 0;
1547 unsigned type = DT_UNKNOWN;
1548
1549 qname.name = name;
1550 qname.len = len;
1551 qname.hash = full_name_hash(name, len);
1552
1553 child = d_lookup(dir, &qname);
1554 if (!child) {
1555 struct dentry *new;
1556 new = d_alloc(dir, &qname);
1557 if (new) {
1558 child = instantiate(dir->d_inode, new, task, ptr);
1559 if (child)
1560 dput(new);
1561 else
1562 child = new;
1563 }
1564 }
1565 if (!child || IS_ERR(child) || !child->d_inode)
1566 goto end_instantiate;
1567 inode = child->d_inode;
1568 if (inode) {
1569 ino = inode->i_ino;
1570 type = inode->i_mode >> 12;
1571 }
1572 dput(child);
1573end_instantiate:
1574 if (!ino)
1575 ino = find_inode_number(dir, &qname);
1576 if (!ino)
1577 ino = 1;
1578 return filldir(dirent, name, len, filp->f_pos, ino, type);
1579}
1580
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001581static unsigned name_to_int(struct dentry *dentry)
1582{
1583 const char *name = dentry->d_name.name;
1584 int len = dentry->d_name.len;
1585 unsigned n = 0;
1586
1587 if (len > 1 && *name == '0')
1588 goto out;
1589 while (len-- > 0) {
1590 unsigned c = *name++ - '0';
1591 if (c > 9)
1592 goto out;
1593 if (n >= (~0U-9)/10)
1594 goto out;
1595 n *= 10;
1596 n += c;
1597 }
1598 return n;
1599out:
1600 return ~0U;
1601}
1602
Miklos Szeredi27932742007-05-08 00:26:17 -07001603#define PROC_FDINFO_MAX 64
1604
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001605static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001606{
1607 struct task_struct *task = get_proc_task(inode);
1608 struct files_struct *files = NULL;
1609 struct file *file;
1610 int fd = proc_fd(inode);
1611
1612 if (task) {
1613 files = get_files_struct(task);
1614 put_task_struct(task);
1615 }
1616 if (files) {
1617 /*
1618 * We are not taking a ref to the file structure, so we must
1619 * hold ->file_lock.
1620 */
1621 spin_lock(&files->file_lock);
1622 file = fcheck_files(files, fd);
1623 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001624 if (path) {
1625 *path = file->f_path;
1626 path_get(&file->f_path);
1627 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001628 if (info)
1629 snprintf(info, PROC_FDINFO_MAX,
1630 "pos:\t%lli\n"
1631 "flags:\t0%o\n",
1632 (long long) file->f_pos,
1633 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001634 spin_unlock(&files->file_lock);
1635 put_files_struct(files);
1636 return 0;
1637 }
1638 spin_unlock(&files->file_lock);
1639 put_files_struct(files);
1640 }
1641 return -ENOENT;
1642}
1643
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001644static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001645{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001646 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001647}
1648
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001649static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1650{
1651 struct inode *inode = dentry->d_inode;
1652 struct task_struct *task = get_proc_task(inode);
1653 int fd = proc_fd(inode);
1654 struct files_struct *files;
1655
1656 if (task) {
1657 files = get_files_struct(task);
1658 if (files) {
1659 rcu_read_lock();
1660 if (fcheck_files(files, fd)) {
1661 rcu_read_unlock();
1662 put_files_struct(files);
1663 if (task_dumpable(task)) {
1664 inode->i_uid = task->euid;
1665 inode->i_gid = task->egid;
1666 } else {
1667 inode->i_uid = 0;
1668 inode->i_gid = 0;
1669 }
1670 inode->i_mode &= ~(S_ISUID | S_ISGID);
1671 security_task_to_inode(task, inode);
1672 put_task_struct(task);
1673 return 1;
1674 }
1675 rcu_read_unlock();
1676 put_files_struct(files);
1677 }
1678 put_task_struct(task);
1679 }
1680 d_drop(dentry);
1681 return 0;
1682}
1683
1684static struct dentry_operations tid_fd_dentry_operations =
1685{
1686 .d_revalidate = tid_fd_revalidate,
1687 .d_delete = pid_delete_dentry,
1688};
1689
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001690static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001691 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001692{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001693 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001694 struct file *file;
1695 struct files_struct *files;
1696 struct inode *inode;
1697 struct proc_inode *ei;
1698 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001699
Eric W. Biederman61a28782006-10-02 02:18:49 -07001700 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001701 if (!inode)
1702 goto out;
1703 ei = PROC_I(inode);
1704 ei->fd = fd;
1705 files = get_files_struct(task);
1706 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001707 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001708 inode->i_mode = S_IFLNK;
1709
1710 /*
1711 * We are not taking a ref to the file structure, so we must
1712 * hold ->file_lock.
1713 */
1714 spin_lock(&files->file_lock);
1715 file = fcheck_files(files, fd);
1716 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001717 goto out_unlock;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001718 if (file->f_mode & 1)
1719 inode->i_mode |= S_IRUSR | S_IXUSR;
1720 if (file->f_mode & 2)
1721 inode->i_mode |= S_IWUSR | S_IXUSR;
1722 spin_unlock(&files->file_lock);
1723 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001724
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001725 inode->i_op = &proc_pid_link_inode_operations;
1726 inode->i_size = 64;
1727 ei->op.proc_get_link = proc_fd_link;
1728 dentry->d_op = &tid_fd_dentry_operations;
1729 d_add(dentry, inode);
1730 /* Close the race of the process dying before we return the dentry */
1731 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001732 error = NULL;
1733
1734 out:
1735 return error;
1736out_unlock:
1737 spin_unlock(&files->file_lock);
1738 put_files_struct(files);
1739out_iput:
1740 iput(inode);
1741 goto out;
1742}
1743
Miklos Szeredi27932742007-05-08 00:26:17 -07001744static struct dentry *proc_lookupfd_common(struct inode *dir,
1745 struct dentry *dentry,
1746 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001747{
1748 struct task_struct *task = get_proc_task(dir);
1749 unsigned fd = name_to_int(dentry);
1750 struct dentry *result = ERR_PTR(-ENOENT);
1751
1752 if (!task)
1753 goto out_no_task;
1754 if (fd == ~0U)
1755 goto out;
1756
Miklos Szeredi27932742007-05-08 00:26:17 -07001757 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001758out:
1759 put_task_struct(task);
1760out_no_task:
1761 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001762}
1763
Miklos Szeredi27932742007-05-08 00:26:17 -07001764static int proc_readfd_common(struct file * filp, void * dirent,
1765 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001767 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001768 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001769 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001770 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct files_struct * files;
1773
1774 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001775 if (!p)
1776 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 fd = filp->f_pos;
1780 switch (fd) {
1781 case 0:
1782 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1783 goto out;
1784 filp->f_pos++;
1785 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001786 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1788 goto out;
1789 filp->f_pos++;
1790 default:
1791 files = get_files_struct(p);
1792 if (!files)
1793 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001794 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001796 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001798 char name[PROC_NUMBUF];
1799 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 if (!fcheck_files(files, fd))
1802 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001803 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Miklos Szeredi27932742007-05-08 00:26:17 -07001805 len = snprintf(name, sizeof(name), "%d", fd);
1806 if (proc_fill_cache(filp, dirent, filldir,
1807 name, len, instantiate,
1808 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001809 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
1811 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001812 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001814 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 put_files_struct(files);
1816 }
1817out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001818 put_task_struct(p);
1819out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return retval;
1821}
1822
Miklos Szeredi27932742007-05-08 00:26:17 -07001823static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
1824 struct nameidata *nd)
1825{
1826 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
1827}
1828
1829static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
1830{
1831 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
1832}
1833
1834static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
1835 size_t len, loff_t *ppos)
1836{
1837 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001838 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07001839 if (!err)
1840 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
1841 return err;
1842}
1843
1844static const struct file_operations proc_fdinfo_file_operations = {
1845 .open = nonseekable_open,
1846 .read = proc_fdinfo_read,
1847};
1848
Arjan van de Ven00977a52007-02-12 00:55:34 -08001849static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 .read = generic_read_dir,
1851 .readdir = proc_readfd,
1852};
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001855 * /proc/pid/fd needs a special permission handler so that a process can still
1856 * access /proc/self/fd after it has executed a setuid().
1857 */
Al Viroe6305c42008-07-15 21:03:57 -04001858static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001859{
1860 int rv;
1861
1862 rv = generic_permission(inode, mask, NULL);
1863 if (rv == 0)
1864 return 0;
1865 if (task_pid(current) == proc_pid(inode))
1866 rv = 0;
1867 return rv;
1868}
1869
1870/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * proc directories can do almost nothing..
1872 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001873static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07001875 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001876 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877};
1878
Miklos Szeredi27932742007-05-08 00:26:17 -07001879static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
1880 struct dentry *dentry, struct task_struct *task, const void *ptr)
1881{
1882 unsigned fd = *(unsigned *)ptr;
1883 struct inode *inode;
1884 struct proc_inode *ei;
1885 struct dentry *error = ERR_PTR(-ENOENT);
1886
1887 inode = proc_pid_make_inode(dir->i_sb, task);
1888 if (!inode)
1889 goto out;
1890 ei = PROC_I(inode);
1891 ei->fd = fd;
1892 inode->i_mode = S_IFREG | S_IRUSR;
1893 inode->i_fop = &proc_fdinfo_file_operations;
1894 dentry->d_op = &tid_fd_dentry_operations;
1895 d_add(dentry, inode);
1896 /* Close the race of the process dying before we return the dentry */
1897 if (tid_fd_revalidate(dentry, NULL))
1898 error = NULL;
1899
1900 out:
1901 return error;
1902}
1903
1904static struct dentry *proc_lookupfdinfo(struct inode *dir,
1905 struct dentry *dentry,
1906 struct nameidata *nd)
1907{
1908 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
1909}
1910
1911static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
1912{
1913 return proc_readfd_common(filp, dirent, filldir,
1914 proc_fdinfo_instantiate);
1915}
1916
1917static const struct file_operations proc_fdinfo_operations = {
1918 .read = generic_read_dir,
1919 .readdir = proc_readfdinfo,
1920};
1921
1922/*
1923 * proc directories can do almost nothing..
1924 */
1925static const struct inode_operations proc_fdinfo_inode_operations = {
1926 .lookup = proc_lookupfdinfo,
1927 .setattr = proc_setattr,
1928};
1929
1930
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001931static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001932 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001933{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001934 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001935 struct inode *inode;
1936 struct proc_inode *ei;
1937 struct dentry *error = ERR_PTR(-EINVAL);
1938
Eric W. Biederman61a28782006-10-02 02:18:49 -07001939 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001940 if (!inode)
1941 goto out;
1942
1943 ei = PROC_I(inode);
1944 inode->i_mode = p->mode;
1945 if (S_ISDIR(inode->i_mode))
1946 inode->i_nlink = 2; /* Use getattr to fix if necessary */
1947 if (p->iop)
1948 inode->i_op = p->iop;
1949 if (p->fop)
1950 inode->i_fop = p->fop;
1951 ei->op = p->op;
1952 dentry->d_op = &pid_dentry_operations;
1953 d_add(dentry, inode);
1954 /* Close the race of the process dying before we return the dentry */
1955 if (pid_revalidate(dentry, NULL))
1956 error = NULL;
1957out:
1958 return error;
1959}
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961static struct dentry *proc_pident_lookup(struct inode *dir,
1962 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001963 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001964 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
1966 struct inode *inode;
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001967 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001968 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07001969 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001971 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 inode = NULL;
1973
Eric W. Biederman99f89552006-06-26 00:25:55 -07001974 if (!task)
1975 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Eric W. Biederman20cdc892006-10-02 02:17:07 -07001977 /*
1978 * Yes, it does not scale. And it should not. Don't add
1979 * new entries into /proc/<tgid>/ without very good reasons.
1980 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001981 last = &ents[nents - 1];
1982 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (p->len != dentry->d_name.len)
1984 continue;
1985 if (!memcmp(dentry->d_name.name, p->name, p->len))
1986 break;
1987 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07001988 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto out;
1990
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001991 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07001993 put_task_struct(task);
1994out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07001995 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Eric Dumazetc5141e62007-05-08 00:26:15 -07001998static int proc_pident_fill_cache(struct file *filp, void *dirent,
1999 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002000{
2001 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2002 proc_pident_instantiate, task, p);
2003}
2004
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002005static int proc_pident_readdir(struct file *filp,
2006 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002007 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002008{
2009 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002010 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002011 struct inode *inode = dentry->d_inode;
2012 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002013 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002014 ino_t ino;
2015 int ret;
2016
2017 ret = -ENOENT;
2018 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002019 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002020
2021 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002022 i = filp->f_pos;
2023 switch (i) {
2024 case 0:
2025 ino = inode->i_ino;
2026 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2027 goto out;
2028 i++;
2029 filp->f_pos++;
2030 /* fall through */
2031 case 1:
2032 ino = parent_ino(dentry);
2033 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2034 goto out;
2035 i++;
2036 filp->f_pos++;
2037 /* fall through */
2038 default:
2039 i -= 2;
2040 if (i >= nents) {
2041 ret = 1;
2042 goto out;
2043 }
2044 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002045 last = &ents[nents - 1];
2046 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002047 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002048 goto out;
2049 filp->f_pos++;
2050 p++;
2051 }
2052 }
2053
2054 ret = 1;
2055out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002056 put_task_struct(task);
2057out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002058 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002062static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2063 size_t count, loff_t *ppos)
2064{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002065 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002066 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002067 ssize_t length;
2068 struct task_struct *task = get_proc_task(inode);
2069
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002070 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002071 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002072
2073 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002074 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002075 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002076 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002077 if (length > 0)
2078 length = simple_read_from_buffer(buf, count, ppos, p, length);
2079 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002080 return length;
2081}
2082
2083static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2084 size_t count, loff_t *ppos)
2085{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002086 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002087 char *page;
2088 ssize_t length;
2089 struct task_struct *task = get_proc_task(inode);
2090
2091 length = -ESRCH;
2092 if (!task)
2093 goto out_no_task;
2094 if (count > PAGE_SIZE)
2095 count = PAGE_SIZE;
2096
2097 /* No partial writes. */
2098 length = -EINVAL;
2099 if (*ppos != 0)
2100 goto out;
2101
2102 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002103 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002104 if (!page)
2105 goto out;
2106
2107 length = -EFAULT;
2108 if (copy_from_user(page, buf, count))
2109 goto out_free;
2110
2111 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002112 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002113 (void*)page, count);
2114out_free:
2115 free_page((unsigned long) page);
2116out:
2117 put_task_struct(task);
2118out_no_task:
2119 return length;
2120}
2121
Arjan van de Ven00977a52007-02-12 00:55:34 -08002122static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002123 .read = proc_pid_attr_read,
2124 .write = proc_pid_attr_write,
2125};
2126
Eric Dumazetc5141e62007-05-08 00:26:15 -07002127static const struct pid_entry attr_dir_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002128 REG("current", S_IRUGO|S_IWUGO, pid_attr),
2129 REG("prev", S_IRUGO, pid_attr),
2130 REG("exec", S_IRUGO|S_IWUGO, pid_attr),
2131 REG("fscreate", S_IRUGO|S_IWUGO, pid_attr),
2132 REG("keycreate", S_IRUGO|S_IWUGO, pid_attr),
2133 REG("sockcreate", S_IRUGO|S_IWUGO, pid_attr),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002134};
2135
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002136static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 void * dirent, filldir_t filldir)
2138{
2139 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002140 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
Arjan van de Ven00977a52007-02-12 00:55:34 -08002143static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002145 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146};
2147
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002148static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct dentry *dentry, struct nameidata *nd)
2150{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002151 return proc_pident_lookup(dir, dentry,
2152 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002155static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002156 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002157 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002158 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159};
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161#endif
2162
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002163#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2164static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2165 size_t count, loff_t *ppos)
2166{
2167 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2168 struct mm_struct *mm;
2169 char buffer[PROC_NUMBUF];
2170 size_t len;
2171 int ret;
2172
2173 if (!task)
2174 return -ESRCH;
2175
2176 ret = 0;
2177 mm = get_task_mm(task);
2178 if (mm) {
2179 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2180 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2181 MMF_DUMP_FILTER_SHIFT));
2182 mmput(mm);
2183 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2184 }
2185
2186 put_task_struct(task);
2187
2188 return ret;
2189}
2190
2191static ssize_t proc_coredump_filter_write(struct file *file,
2192 const char __user *buf,
2193 size_t count,
2194 loff_t *ppos)
2195{
2196 struct task_struct *task;
2197 struct mm_struct *mm;
2198 char buffer[PROC_NUMBUF], *end;
2199 unsigned int val;
2200 int ret;
2201 int i;
2202 unsigned long mask;
2203
2204 ret = -EFAULT;
2205 memset(buffer, 0, sizeof(buffer));
2206 if (count > sizeof(buffer) - 1)
2207 count = sizeof(buffer) - 1;
2208 if (copy_from_user(buffer, buf, count))
2209 goto out_no_task;
2210
2211 ret = -EINVAL;
2212 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2213 if (*end == '\n')
2214 end++;
2215 if (end - buffer == 0)
2216 goto out_no_task;
2217
2218 ret = -ESRCH;
2219 task = get_proc_task(file->f_dentry->d_inode);
2220 if (!task)
2221 goto out_no_task;
2222
2223 ret = end - buffer;
2224 mm = get_task_mm(task);
2225 if (!mm)
2226 goto out_no_mm;
2227
2228 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2229 if (val & mask)
2230 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2231 else
2232 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2233 }
2234
2235 mmput(mm);
2236 out_no_mm:
2237 put_task_struct(task);
2238 out_no_task:
2239 return ret;
2240}
2241
2242static const struct file_operations proc_coredump_filter_operations = {
2243 .read = proc_coredump_filter_read,
2244 .write = proc_coredump_filter_write,
2245};
2246#endif
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/*
2249 * /proc/self:
2250 */
2251static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2252 int buflen)
2253{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002254 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002255 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002256 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002257 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002258 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002259 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return vfs_readlink(dentry,buffer,buflen,tmp);
2261}
2262
Al Viro008b1502005-08-20 00:17:39 +01002263static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002265 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002266 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002267 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002268 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002269 return ERR_PTR(-ENOENT);
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002270 sprintf(tmp, "%d", task_tgid_nr_ns(current, ns));
Al Viro008b1502005-08-20 00:17:39 +01002271 return ERR_PTR(vfs_follow_link(nd,tmp));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002272}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002274static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 .readlink = proc_self_readlink,
2276 .follow_link = proc_self_follow_link,
2277};
2278
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002279/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002280 * proc base
2281 *
2282 * These are the directory entries in the root directory of /proc
2283 * that properly belong to the /proc filesystem, as they describe
2284 * describe something that is process related.
2285 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002286static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002287 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002288 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002289};
2290
2291/*
2292 * Exceptional case: normally we are not allowed to unhash a busy
2293 * directory. In this case, however, we can do it - no aliasing problems
2294 * due to the way we treat inodes.
2295 */
2296static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2297{
2298 struct inode *inode = dentry->d_inode;
2299 struct task_struct *task = get_proc_task(inode);
2300 if (task) {
2301 put_task_struct(task);
2302 return 1;
2303 }
2304 d_drop(dentry);
2305 return 0;
2306}
2307
2308static struct dentry_operations proc_base_dentry_operations =
2309{
2310 .d_revalidate = proc_base_revalidate,
2311 .d_delete = pid_delete_dentry,
2312};
2313
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002314static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002315 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002316{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002317 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002318 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002319 struct proc_inode *ei;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002320 struct dentry *error = ERR_PTR(-EINVAL);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002321
2322 /* Allocate the inode */
2323 error = ERR_PTR(-ENOMEM);
2324 inode = new_inode(dir->i_sb);
2325 if (!inode)
2326 goto out;
2327
2328 /* Initialize the inode */
2329 ei = PROC_I(inode);
2330 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002331
2332 /*
2333 * grab the reference to the task.
2334 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002335 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002336 if (!ei->pid)
2337 goto out_iput;
2338
2339 inode->i_uid = 0;
2340 inode->i_gid = 0;
2341 inode->i_mode = p->mode;
2342 if (S_ISDIR(inode->i_mode))
2343 inode->i_nlink = 2;
2344 if (S_ISLNK(inode->i_mode))
2345 inode->i_size = 64;
2346 if (p->iop)
2347 inode->i_op = p->iop;
2348 if (p->fop)
2349 inode->i_fop = p->fop;
2350 ei->op = p->op;
2351 dentry->d_op = &proc_base_dentry_operations;
2352 d_add(dentry, inode);
2353 error = NULL;
2354out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002355 return error;
2356out_iput:
2357 iput(inode);
2358 goto out;
2359}
2360
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002361static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2362{
2363 struct dentry *error;
2364 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002365 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002366
2367 error = ERR_PTR(-ENOENT);
2368
2369 if (!task)
2370 goto out_no_task;
2371
2372 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002373 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2374 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002375 if (p->len != dentry->d_name.len)
2376 continue;
2377 if (!memcmp(dentry->d_name.name, p->name, p->len))
2378 break;
2379 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002380 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002381 goto out;
2382
2383 error = proc_base_instantiate(dir, dentry, task, p);
2384
2385out:
2386 put_task_struct(task);
2387out_no_task:
2388 return error;
2389}
2390
Eric Dumazetc5141e62007-05-08 00:26:15 -07002391static int proc_base_fill_cache(struct file *filp, void *dirent,
2392 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002393{
2394 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2395 proc_base_instantiate, task, p);
2396}
2397
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002398#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002399static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002400{
Andrea Righi940389b2008-07-28 00:48:12 +02002401 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002402 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002403
Andrea Righi59954772008-07-27 17:29:15 +02002404 if (whole && lock_task_sighand(task, &flags)) {
2405 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002406
Andrea Righi59954772008-07-27 17:29:15 +02002407 task_io_accounting_add(&acct, &task->signal->ioac);
2408 while_each_thread(task, t)
2409 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002410
Andrea Righi59954772008-07-27 17:29:15 +02002411 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002412 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002413 return sprintf(buffer,
2414 "rchar: %llu\n"
2415 "wchar: %llu\n"
2416 "syscr: %llu\n"
2417 "syscw: %llu\n"
2418 "read_bytes: %llu\n"
2419 "write_bytes: %llu\n"
2420 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002421 (unsigned long long)acct.rchar,
2422 (unsigned long long)acct.wchar,
2423 (unsigned long long)acct.syscr,
2424 (unsigned long long)acct.syscw,
2425 (unsigned long long)acct.read_bytes,
2426 (unsigned long long)acct.write_bytes,
2427 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002428}
Andrea Righi297c5d92008-07-25 01:48:49 -07002429
2430static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2431{
2432 return do_io_accounting(task, buffer, 0);
2433}
2434
2435static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2436{
2437 return do_io_accounting(task, buffer, 1);
2438}
2439#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002440
Kees Cook47830722008-10-06 03:11:58 +04002441static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2442 struct pid *pid, struct task_struct *task)
2443{
2444 seq_printf(m, "%08x\n", task->personality);
2445 return 0;
2446}
2447
Eric W. Biederman801199c2006-10-02 02:18:48 -07002448/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002449 * Thread groups
2450 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002451static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002452static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002453
Eric Dumazetc5141e62007-05-08 00:26:15 -07002454static const struct pid_entry tgid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002455 DIR("task", S_IRUGO|S_IXUGO, task),
2456 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002457 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002458#ifdef CONFIG_NET
Andre Noll4f42c282008-03-20 15:27:28 -07002459 DIR("net", S_IRUGO|S_IXUGO, net),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002460#endif
James Pearson315e28c2007-10-16 23:30:17 -07002461 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002462 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002463 ONE("status", S_IRUGO, pid_status),
Kees Cook47830722008-10-06 03:11:58 +04002464 ONE("personality", S_IRUSR, pid_personality),
Neil Hormand85f50d2007-10-18 23:40:37 -07002465 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002466#ifdef CONFIG_SCHED_DEBUG
2467 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2468#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002469#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2470 INF("syscall", S_IRUSR, pid_syscall),
2471#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002472 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002473 ONE("stat", S_IRUGO, tgid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002474 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002475 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002476#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002477 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002478#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002479 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002480 LNK("cwd", cwd),
2481 LNK("root", root),
2482 LNK("exe", exe),
2483 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002484 REG("mountinfo", S_IRUGO, mountinfo),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002485 REG("mountstats", S_IRUSR, mountstats),
Matt Mackall1e883282008-02-04 22:29:07 -08002486#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002487 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002488 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002489 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002490#endif
2491#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002492 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002493#endif
2494#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002495 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002496#endif
2497#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002498 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002499#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002500#ifdef CONFIG_LATENCYTOP
2501 REG("latency", S_IRUGO, lstats),
2502#endif
Paul Menage8793d852007-10-18 23:39:39 -07002503#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002504 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002505#endif
Paul Menagea4243162007-10-18 23:39:35 -07002506#ifdef CONFIG_CGROUPS
2507 REG("cgroup", S_IRUGO, cgroup),
2508#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002509 INF("oom_score", S_IRUGO, oom_score),
2510 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002511#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002512 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Steve Grubb6ee65042008-04-29 15:01:13 -04002513 REG("sessionid", S_IRUGO, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002514#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002515#ifdef CONFIG_FAULT_INJECTION
2516 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2517#endif
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002518#if defined(USE_ELF_CORE_DUMP) && defined(CONFIG_ELF_CORE)
2519 REG("coredump_filter", S_IRUGO|S_IWUSR, coredump_filter),
2520#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002521#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002522 INF("io", S_IRUGO, tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002523#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002524};
2525
2526static int proc_tgid_base_readdir(struct file * filp,
2527 void * dirent, filldir_t filldir)
2528{
2529 return proc_pident_readdir(filp,dirent,filldir,
2530 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2531}
2532
Arjan van de Ven00977a52007-02-12 00:55:34 -08002533static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002534 .read = generic_read_dir,
2535 .readdir = proc_tgid_base_readdir,
2536};
2537
2538static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002539 return proc_pident_lookup(dir, dentry,
2540 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002541}
2542
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002543static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002544 .lookup = proc_tgid_base_lookup,
2545 .getattr = pid_getattr,
2546 .setattr = proc_setattr,
2547};
2548
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002549static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002551 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002552 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002553 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Eric W. Biederman48e64842006-06-26 00:25:48 -07002555 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002556 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2557 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002558 if (dentry) {
Andrea Arcangeli77667552008-02-04 22:29:21 -08002559 if (!(current->flags & PF_EXITING))
2560 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002561 d_drop(dentry);
2562 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002565 if (tgid == 0)
Eric W. Biederman48e64842006-06-26 00:25:48 -07002566 goto out;
2567
2568 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002569 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2570 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002571 if (!leader)
2572 goto out;
2573
2574 name.name = "task";
2575 name.len = strlen(name.name);
2576 dir = d_hash_and_lookup(leader, &name);
2577 if (!dir)
2578 goto out_put_leader;
2579
2580 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002581 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002582 dentry = d_hash_and_lookup(dir, &name);
2583 if (dentry) {
2584 shrink_dcache_parent(dentry);
2585 d_drop(dentry);
2586 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002588
2589 dput(dir);
2590out_put_leader:
2591 dput(leader);
2592out:
2593 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Randy Dunlap0895e912007-10-21 21:00:10 -07002596/**
2597 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2598 * @task: task that should be flushed.
2599 *
2600 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002601 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002602 * in. This call is supposed to do all of this job.
2603 *
2604 * Looks in the dcache for
2605 * /proc/@pid
2606 * /proc/@tgid/task/@pid
2607 * if either directory is present flushes it and all of it'ts children
2608 * from the dcache.
2609 *
2610 * It is safe and reasonable to cache /proc entries for a task until
2611 * that task exits. After that they just clog up the dcache with
2612 * useless entries, possibly causing useful dcache entries to be
2613 * flushed instead. This routine is proved to flush those useless
2614 * dcache entries at process exit time.
2615 *
2616 * NOTE: This routine is just an optimization so it does not guarantee
2617 * that no dcache entries will exist at process exit time it
2618 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002619 */
2620
2621void proc_flush_task(struct task_struct *task)
2622{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002623 int i;
2624 struct pid *pid, *tgid = NULL;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002625 struct upid *upid;
2626
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002627 pid = task_pid(task);
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002628 if (thread_group_leader(task))
2629 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002630
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002631 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002632 upid = &pid->numbers[i];
2633 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002634 tgid ? tgid->numbers[i].nr : 0);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002635 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002636
2637 upid = &pid->numbers[pid->level];
2638 if (upid->nr == 1)
2639 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002640}
2641
Adrian Bunk9711ef92006-12-06 20:38:31 -08002642static struct dentry *proc_pid_instantiate(struct inode *dir,
2643 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002644 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002645{
2646 struct dentry *error = ERR_PTR(-ENOENT);
2647 struct inode *inode;
2648
Eric W. Biederman61a28782006-10-02 02:18:49 -07002649 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002650 if (!inode)
2651 goto out;
2652
2653 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2654 inode->i_op = &proc_tgid_base_inode_operations;
2655 inode->i_fop = &proc_tgid_base_operations;
2656 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002657
2658 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2659 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002660
2661 dentry->d_op = &pid_dentry_operations;
2662
2663 d_add(dentry, inode);
2664 /* Close the race of the process dying before we return the dentry */
2665 if (pid_revalidate(dentry, NULL))
2666 error = NULL;
2667out:
2668 return error;
2669}
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2672{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002673 struct dentry *result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002676 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Eric W. Biederman801199c2006-10-02 02:18:48 -07002678 result = proc_base_lookup(dir, dentry);
2679 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2680 goto out;
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 tgid = name_to_int(dentry);
2683 if (tgid == ~0U)
2684 goto out;
2685
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002686 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002687 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002688 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (task)
2690 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002691 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (!task)
2693 goto out;
2694
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002695 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002696 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002698 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699}
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002702 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002703 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002705struct tgid_iter {
2706 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002707 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002708};
2709static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2710{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002711 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002713 if (iter.task)
2714 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002715 rcu_read_lock();
2716retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002717 iter.task = NULL;
2718 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002719 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002720 iter.tgid = pid_nr_ns(pid, ns);
2721 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002722 /* What we to know is if the pid we have find is the
2723 * pid of a thread_group_leader. Testing for task
2724 * being a thread_group_leader is the obvious thing
2725 * todo but there is a window when it fails, due to
2726 * the pid transfer logic in de_thread.
2727 *
2728 * So we perform the straight forward test of seeing
2729 * if the pid we have found is the pid of a thread
2730 * group leader, and don't worry if the task we have
2731 * found doesn't happen to be a thread group leader.
2732 * As we don't care in the case of readdir.
2733 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002734 if (!iter.task || !has_group_leader_pid(iter.task)) {
2735 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002736 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002737 }
2738 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002740 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002741 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002744#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Eric W. Biederman61a28782006-10-02 02:18:49 -07002746static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002747 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002748{
2749 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002750 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002751 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002752 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002753}
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755/* for the /proc/ directory itself, after non-process stuff has been done */
2756int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2757{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002759 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002760 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002761 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Eric W. Biederman61a28782006-10-02 02:18:49 -07002763 if (!reaper)
2764 goto out_no_task;
2765
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002766 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002767 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002768 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002769 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002772 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002773 iter.task = NULL;
2774 iter.tgid = filp->f_pos - TGID_OFFSET;
2775 for (iter = next_tgid(ns, iter);
2776 iter.task;
2777 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2778 filp->f_pos = iter.tgid + TGID_OFFSET;
2779 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2780 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002784 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2785out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002786 put_task_struct(reaper);
2787out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return 0;
2789}
2790
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002791/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002792 * Tasks
2793 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002794static const struct pid_entry tid_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002795 DIR("fd", S_IRUSR|S_IXUSR, fd),
Miklos Szeredi27932742007-05-08 00:26:17 -07002796 DIR("fdinfo", S_IRUSR|S_IXUSR, fdinfo),
James Pearson315e28c2007-10-16 23:30:17 -07002797 REG("environ", S_IRUSR, environ),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002798 INF("auxv", S_IRUSR, pid_auxv),
Eric W. Biedermandf5f8312008-02-08 04:18:33 -08002799 ONE("status", S_IRUGO, pid_status),
Kees Cook47830722008-10-06 03:11:58 +04002800 ONE("personality", S_IRUSR, pid_personality),
Neil Hormand85f50d2007-10-18 23:40:37 -07002801 INF("limits", S_IRUSR, pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002802#ifdef CONFIG_SCHED_DEBUG
2803 REG("sched", S_IRUGO|S_IWUSR, pid_sched),
2804#endif
Roland McGrathebcb6732008-07-25 19:46:00 -07002805#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
2806 INF("syscall", S_IRUSR, pid_syscall),
2807#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002808 INF("cmdline", S_IRUGO, pid_cmdline),
Eric W. Biedermanee992742008-02-08 04:18:31 -08002809 ONE("stat", S_IRUGO, tid_stat),
Eric W. Biedermana56d3fc2008-02-08 04:18:32 -08002810 ONE("statm", S_IRUGO, pid_statm),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002811 REG("maps", S_IRUGO, maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002812#ifdef CONFIG_NUMA
Eric W. Biederman61a28782006-10-02 02:18:49 -07002813 REG("numa_maps", S_IRUGO, numa_maps),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002814#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002815 REG("mem", S_IRUSR|S_IWUSR, mem),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002816 LNK("cwd", cwd),
2817 LNK("root", root),
2818 LNK("exe", exe),
2819 REG("mounts", S_IRUGO, mounts),
Ram Pai2d4d4862008-03-27 13:06:25 +01002820 REG("mountinfo", S_IRUGO, mountinfo),
Matt Mackall1e883282008-02-04 22:29:07 -08002821#ifdef CONFIG_PROC_PAGE_MONITOR
David Rientjesb813e932007-05-06 14:49:24 -07002822 REG("clear_refs", S_IWUSR, clear_refs),
Eric W. Biederman61a28782006-10-02 02:18:49 -07002823 REG("smaps", S_IRUGO, smaps),
Matt Mackall85863e42008-02-04 22:29:04 -08002824 REG("pagemap", S_IRUSR, pagemap),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002825#endif
2826#ifdef CONFIG_SECURITY
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002827 DIR("attr", S_IRUGO|S_IXUGO, attr_dir),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002828#endif
2829#ifdef CONFIG_KALLSYMS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002830 INF("wchan", S_IRUGO, pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002831#endif
2832#ifdef CONFIG_SCHEDSTATS
Eric W. Biederman61a28782006-10-02 02:18:49 -07002833 INF("schedstat", S_IRUGO, pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002834#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002835#ifdef CONFIG_LATENCYTOP
2836 REG("latency", S_IRUGO, lstats),
2837#endif
Paul Menage8793d852007-10-18 23:39:39 -07002838#ifdef CONFIG_PROC_PID_CPUSET
Eric W. Biederman61a28782006-10-02 02:18:49 -07002839 REG("cpuset", S_IRUGO, cpuset),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002840#endif
Paul Menagea4243162007-10-18 23:39:35 -07002841#ifdef CONFIG_CGROUPS
2842 REG("cgroup", S_IRUGO, cgroup),
2843#endif
Eric W. Biederman61a28782006-10-02 02:18:49 -07002844 INF("oom_score", S_IRUGO, oom_score),
2845 REG("oom_adj", S_IRUGO|S_IWUSR, oom_adjust),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002846#ifdef CONFIG_AUDITSYSCALL
Eric W. Biederman61a28782006-10-02 02:18:49 -07002847 REG("loginuid", S_IWUSR|S_IRUGO, loginuid),
Eric Paris1e0bd752008-03-13 08:15:31 -04002848 REG("sessionid", S_IRUSR, sessionid),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002849#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002850#ifdef CONFIG_FAULT_INJECTION
2851 REG("make-it-fail", S_IRUGO|S_IWUSR, fault_inject),
2852#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07002853#ifdef CONFIG_TASK_IO_ACCOUNTING
2854 INF("io", S_IRUGO, tid_io_accounting),
2855#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002856};
2857
2858static int proc_tid_base_readdir(struct file * filp,
2859 void * dirent, filldir_t filldir)
2860{
2861 return proc_pident_readdir(filp,dirent,filldir,
2862 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
2863}
2864
2865static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002866 return proc_pident_lookup(dir, dentry,
2867 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002868}
2869
Arjan van de Ven00977a52007-02-12 00:55:34 -08002870static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002871 .read = generic_read_dir,
2872 .readdir = proc_tid_base_readdir,
2873};
2874
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002875static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002876 .lookup = proc_tid_base_lookup,
2877 .getattr = pid_getattr,
2878 .setattr = proc_setattr,
2879};
2880
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002881static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002882 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002883{
2884 struct dentry *error = ERR_PTR(-ENOENT);
2885 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07002886 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002887
2888 if (!inode)
2889 goto out;
2890 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2891 inode->i_op = &proc_tid_base_inode_operations;
2892 inode->i_fop = &proc_tid_base_operations;
2893 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002894
2895 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
2896 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002897
2898 dentry->d_op = &pid_dentry_operations;
2899
2900 d_add(dentry, inode);
2901 /* Close the race of the process dying before we return the dentry */
2902 if (pid_revalidate(dentry, NULL))
2903 error = NULL;
2904out:
2905 return error;
2906}
2907
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002908static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2909{
2910 struct dentry *result = ERR_PTR(-ENOENT);
2911 struct task_struct *task;
2912 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002913 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002914 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002915
2916 if (!leader)
2917 goto out_no_task;
2918
2919 tid = name_to_int(dentry);
2920 if (tid == ~0U)
2921 goto out;
2922
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002923 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002924 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002925 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002926 if (task)
2927 get_task_struct(task);
2928 rcu_read_unlock();
2929 if (!task)
2930 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07002931 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002932 goto out_drop_task;
2933
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002934 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002935out_drop_task:
2936 put_task_struct(task);
2937out:
2938 put_task_struct(leader);
2939out_no_task:
2940 return result;
2941}
2942
2943/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002944 * Find the first tid of a thread group to return to user space.
2945 *
2946 * Usually this is just the thread group leader, but if the users
2947 * buffer was too small or there was a seek into the middle of the
2948 * directory we have more work todo.
2949 *
2950 * In the case of a short read we start with find_task_by_pid.
2951 *
2952 * In the case of a seek we start with the leader and walk nr
2953 * threads past it.
2954 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07002955static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002956 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002957{
Oleg Nesterova872ff02006-06-26 00:26:01 -07002958 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002959
Eric W. Biedermancc288732006-06-26 00:26:01 -07002960 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002961 /* Attempt to start with the pid of a thread */
2962 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002963 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07002964 if (pos && (pos->group_leader == leader))
2965 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002966 }
2967
2968 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002969 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07002970 if (nr && nr >= get_nr_threads(leader))
2971 goto out;
2972
2973 /* If we haven't found our starting place yet start
2974 * with the leader and walk nr threads forward.
2975 */
2976 for (pos = leader; nr > 0; --nr) {
2977 pos = next_thread(pos);
2978 if (pos == leader) {
2979 pos = NULL;
2980 goto out;
2981 }
2982 }
2983found:
2984 get_task_struct(pos);
2985out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07002986 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002987 return pos;
2988}
2989
2990/*
2991 * Find the next thread in the thread list.
2992 * Return NULL if there is an error or no next thread.
2993 *
2994 * The reference to the input task_struct is released.
2995 */
2996static struct task_struct *next_tid(struct task_struct *start)
2997{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07002998 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07002999 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003000 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003001 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003002 if (thread_group_leader(pos))
3003 pos = NULL;
3004 else
3005 get_task_struct(pos);
3006 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003007 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003008 put_task_struct(start);
3009 return pos;
3010}
3011
Eric W. Biederman61a28782006-10-02 02:18:49 -07003012static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3013 struct task_struct *task, int tid)
3014{
3015 char name[PROC_NUMBUF];
3016 int len = snprintf(name, sizeof(name), "%d", tid);
3017 return proc_fill_cache(filp, dirent, filldir, name, len,
3018 proc_task_instantiate, task, NULL);
3019}
3020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021/* for the /proc/TGID/task/ directories */
3022static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3023{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003024 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003026 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003027 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 int retval = -ENOENT;
3029 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003030 int tid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 unsigned long pos = filp->f_pos; /* avoiding "long long" filp->f_pos */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003032 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003034 task = get_proc_task(inode);
3035 if (!task)
3036 goto out_no_task;
3037 rcu_read_lock();
3038 if (pid_alive(task)) {
3039 leader = task->group_leader;
3040 get_task_struct(leader);
3041 }
3042 rcu_read_unlock();
3043 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003044 if (!leader)
3045 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 retval = 0;
3047
3048 switch (pos) {
3049 case 0:
3050 ino = inode->i_ino;
3051 if (filldir(dirent, ".", 1, pos, ino, DT_DIR) < 0)
3052 goto out;
3053 pos++;
3054 /* fall through */
3055 case 1:
3056 ino = parent_ino(dentry);
3057 if (filldir(dirent, "..", 2, pos, ino, DT_DIR) < 0)
3058 goto out;
3059 pos++;
3060 /* fall through */
3061 }
3062
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003063 /* f_version caches the tgid value that the last readdir call couldn't
3064 * return. lseek aka telldir automagically resets f_version to 0.
3065 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003066 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003067 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003068 filp->f_version = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003069 for (task = first_tid(leader, tid, pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003070 task;
3071 task = next_tid(task), pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003072 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003073 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003074 /* returning this tgid failed, save it as the first
3075 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003076 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003077 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
3081out:
3082 filp->f_pos = pos;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003083 put_task_struct(leader);
3084out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return retval;
3086}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003087
3088static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3089{
3090 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003091 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003092 generic_fillattr(inode, stat);
3093
Eric W. Biederman99f89552006-06-26 00:25:55 -07003094 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003095 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003096 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003097 }
3098
3099 return 0;
3100}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003101
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003102static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003103 .lookup = proc_task_lookup,
3104 .getattr = proc_task_getattr,
3105 .setattr = proc_setattr,
3106};
3107
Arjan van de Ven00977a52007-02-12 00:55:34 -08003108static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003109 .read = generic_read_dir,
3110 .readdir = proc_task_readdir,
3111};