blob: 69254a365ce2e1a0797cd37bdf623381fbb0119b [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>
David Rientjesa63d83f2010-08-09 17:19:46 -070066#include <linux/swap.h>
Dipankar Sarmab8359962005-09-09 13:04:14 -070067#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/kallsyms.h>
Ken Chen2ec220e2008-11-10 11:26:08 +030069#include <linux/stacktrace.h>
Neil Hormand85f50d2007-10-18 23:40:37 -070070#include <linux/resource.h>
Kees Cook5096add2007-05-08 00:26:04 -070071#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/mount.h>
73#include <linux/security.h>
74#include <linux/ptrace.h>
Roland McGrath0d094ef2008-07-25 19:45:49 -070075#include <linux/tracehook.h>
Paul Menagea4243162007-10-18 23:39:35 -070076#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/cpuset.h>
78#include <linux/audit.h>
Al Viro5addc5d2005-11-07 17:15:49 -050079#include <linux/poll.h>
Serge E. Hallyn1651e142006-10-02 02:18:08 -070080#include <linux/nsproxy.h>
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -070081#include <linux/oom.h>
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -070082#include <linux/elf.h>
Pavel Emelyanov60347f62007-10-18 23:40:03 -070083#include <linux/pid_namespace.h>
Al Viro5ad4e532009-03-29 19:50:06 -040084#include <linux/fs_struct.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090085#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include "internal.h"
87
Eric W. Biederman0f2fe202006-06-26 00:25:46 -070088/* NOTE:
89 * Implementing inode permission operations in /proc is almost
90 * certainly an error. Permission checks need to happen during
91 * each system call not at open time. The reason is that most of
92 * what we wish to check for permissions in /proc varies at runtime.
93 *
94 * The classic example of a problem is opening file descriptors
95 * in /proc for a task before it execs a suid executable.
96 */
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct pid_entry {
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 char *name;
Eric Dumazetc5141e62007-05-08 00:26:15 -0700100 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 mode_t mode;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800102 const struct inode_operations *iop;
Arjan van de Ven00977a52007-02-12 00:55:34 -0800103 const struct file_operations *fop;
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700104 union proc_op op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Eric W. Biederman61a28782006-10-02 02:18:49 -0700107#define NOD(NAME, MODE, IOP, FOP, OP) { \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700108 .name = (NAME), \
Eric Dumazetc5141e62007-05-08 00:26:15 -0700109 .len = sizeof(NAME) - 1, \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700110 .mode = MODE, \
111 .iop = IOP, \
112 .fop = FOP, \
113 .op = OP, \
114}
115
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300116#define DIR(NAME, MODE, iops, fops) \
117 NOD(NAME, (S_IFDIR|(MODE)), &iops, &fops, {} )
118#define LNK(NAME, get_link) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700119 NOD(NAME, (S_IFLNK|S_IRWXUGO), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700120 &proc_pid_link_inode_operations, NULL, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300121 { .proc_get_link = get_link } )
122#define REG(NAME, MODE, fops) \
123 NOD(NAME, (S_IFREG|(MODE)), NULL, &fops, {})
124#define INF(NAME, MODE, read) \
Eric W. Biederman61a28782006-10-02 02:18:49 -0700125 NOD(NAME, (S_IFREG|(MODE)), \
Eric W. Biederman20cdc892006-10-02 02:17:07 -0700126 NULL, &proc_info_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300127 { .proc_read = read } )
128#define ONE(NAME, MODE, show) \
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800129 NOD(NAME, (S_IFREG|(MODE)), \
130 NULL, &proc_single_file_operations, \
Alexey Dobriyan631f9c12008-11-10 01:32:52 +0300131 { .proc_show = show } )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Vegard Nossumaed54172008-06-05 22:46:53 -0700133/*
134 * Count the number of hardlinks for the pid_entry table, excluding the .
135 * and .. links.
136 */
137static unsigned int pid_entry_count_dirs(const struct pid_entry *entries,
138 unsigned int n)
139{
140 unsigned int i;
141 unsigned int count;
142
143 count = 0;
144 for (i = 0; i < n; ++i) {
145 if (S_ISDIR(entries[i].mode))
146 ++count;
147 }
148
149 return count;
150}
151
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000152static int get_fs_path(struct task_struct *task, struct path *path, bool root)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct fs_struct *fs;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000155 int result = -ENOENT;
156
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700157 task_lock(task);
158 fs = task->fs;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000159 if (fs) {
160 read_lock(&fs->lock);
161 *path = root ? fs->root : fs->pwd;
162 path_get(path);
163 read_unlock(&fs->lock);
164 result = 0;
165 }
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700166 task_unlock(task);
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000167 return result;
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700168}
169
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800170static int proc_cwd_link(struct inode *inode, struct path *path)
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700171{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700172 struct task_struct *task = get_proc_task(inode);
Miklos Szeredi0494f6e2005-09-06 15:18:22 -0700173 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700174
175 if (task) {
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000176 result = get_fs_path(task, path, 0);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700177 put_task_struct(task);
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return result;
180}
181
Jan Blunck3dcd25f2008-02-14 19:38:35 -0800182static int proc_root_link(struct inode *inode, struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700184 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int result = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700186
187 if (task) {
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000188 result = get_fs_path(task, path, 1);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700189 put_task_struct(task);
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return result;
192}
193
Roland McGrath638fa2022008-04-29 01:01:38 -0700194/*
195 * Return zero if current may access user memory in @task, -error if not.
196 */
197static int check_mem_permission(struct task_struct *task)
198{
199 /*
200 * A task can always look at itself, in case it chooses
201 * to use system calls instead of load instructions.
202 */
203 if (task == current)
204 return 0;
205
206 /*
207 * If current is actively ptrace'ing, and would also be
208 * permitted to freshly attach with ptrace now, permit it.
209 */
Roland McGrath0d094ef2008-07-25 19:45:49 -0700210 if (task_is_stopped_or_traced(task)) {
211 int match;
212 rcu_read_lock();
213 match = (tracehook_tracer_task(task) == current);
214 rcu_read_unlock();
215 if (match && ptrace_may_access(task, PTRACE_MODE_ATTACH))
216 return 0;
217 }
Roland McGrath638fa2022008-04-29 01:01:38 -0700218
219 /*
220 * Noone else is allowed.
221 */
222 return -EPERM;
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Al Viro831830b2008-01-02 14:09:57 +0000225struct mm_struct *mm_for_maps(struct task_struct *task)
226{
Oleg Nesterov704b8362009-07-10 03:27:40 +0200227 struct mm_struct *mm;
Oleg Nesterov00f89d22009-07-10 03:27:38 +0200228
Oleg Nesterov704b8362009-07-10 03:27:40 +0200229 if (mutex_lock_killable(&task->cred_guard_mutex))
230 return NULL;
231
232 mm = get_task_mm(task);
233 if (mm && mm != current->mm &&
234 !ptrace_may_access(task, PTRACE_MODE_READ)) {
235 mmput(mm);
236 mm = NULL;
Oleg Nesterov13f0fea2009-06-23 21:25:32 +0200237 }
Oleg Nesterov704b8362009-07-10 03:27:40 +0200238 mutex_unlock(&task->cred_guard_mutex);
239
Al Viro831830b2008-01-02 14:09:57 +0000240 return mm;
Al Viro831830b2008-01-02 14:09:57 +0000241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static int proc_pid_cmdline(struct task_struct *task, char * buffer)
244{
245 int res = 0;
246 unsigned int len;
247 struct mm_struct *mm = get_task_mm(task);
248 if (!mm)
249 goto out;
250 if (!mm->arg_end)
251 goto out_mm; /* Shh! No looking before we're done */
252
253 len = mm->arg_end - mm->arg_start;
254
255 if (len > PAGE_SIZE)
256 len = PAGE_SIZE;
257
258 res = access_process_vm(task, mm->arg_start, buffer, len, 0);
259
260 // If the nul at the end of args has been overwritten, then
261 // assume application is using setproctitle(3).
262 if (res > 0 && buffer[res-1] != '\0' && len < PAGE_SIZE) {
263 len = strnlen(buffer, res);
264 if (len < res) {
265 res = len;
266 } else {
267 len = mm->env_end - mm->env_start;
268 if (len > PAGE_SIZE - res)
269 len = PAGE_SIZE - res;
270 res += access_process_vm(task, mm->env_start, buffer+res, len, 0);
271 res = strnlen(buffer, res);
272 }
273 }
274out_mm:
275 mmput(mm);
276out:
277 return res;
278}
279
280static int proc_pid_auxv(struct task_struct *task, char *buffer)
281{
282 int res = 0;
283 struct mm_struct *mm = get_task_mm(task);
284 if (mm) {
285 unsigned int nwords = 0;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300286 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 nwords += 2;
Hannes Ederdfe6b7d2008-12-30 18:49:13 +0300288 } while (mm->saved_auxv[nwords - 2] != 0); /* AT_NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 res = nwords * sizeof(mm->saved_auxv[0]);
290 if (res > PAGE_SIZE)
291 res = PAGE_SIZE;
292 memcpy(buffer, mm->saved_auxv, res);
293 mmput(mm);
294 }
295 return res;
296}
297
298
299#ifdef CONFIG_KALLSYMS
300/*
301 * Provides a wchan file via kallsyms in a proper one-value-per-file format.
302 * Returns the resolved symbol. If that fails, simply return the address.
303 */
304static int proc_pid_wchan(struct task_struct *task, char *buffer)
305{
Alexey Dobriyanffb45122007-05-08 00:28:41 -0700306 unsigned long wchan;
Tejun Heo9281ace2007-07-17 04:03:51 -0700307 char symname[KSYM_NAME_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 wchan = get_wchan(task);
310
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700311 if (lookup_symbol_name(wchan, symname) < 0)
Jake Edgef83ce3e2009-05-04 12:51:14 -0600312 if (!ptrace_may_access(task, PTRACE_MODE_READ))
313 return 0;
314 else
315 return sprintf(buffer, "%lu", wchan);
Alexey Dobriyan9d65cb42007-05-08 00:28:43 -0700316 else
317 return sprintf(buffer, "%s", symname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319#endif /* CONFIG_KALLSYMS */
320
Ken Chen2ec220e2008-11-10 11:26:08 +0300321#ifdef CONFIG_STACKTRACE
322
323#define MAX_STACK_TRACE_DEPTH 64
324
325static int proc_pid_stack(struct seq_file *m, struct pid_namespace *ns,
326 struct pid *pid, struct task_struct *task)
327{
328 struct stack_trace trace;
329 unsigned long *entries;
330 int i;
331
332 entries = kmalloc(MAX_STACK_TRACE_DEPTH * sizeof(*entries), GFP_KERNEL);
333 if (!entries)
334 return -ENOMEM;
335
336 trace.nr_entries = 0;
337 trace.max_entries = MAX_STACK_TRACE_DEPTH;
338 trace.entries = entries;
339 trace.skip = 0;
340 save_stack_trace_tsk(task, &trace);
341
342 for (i = 0; i < trace.nr_entries; i++) {
343 seq_printf(m, "[<%p>] %pS\n",
344 (void *)entries[i], (void *)entries[i]);
345 }
346 kfree(entries);
347
348 return 0;
349}
350#endif
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352#ifdef CONFIG_SCHEDSTATS
353/*
354 * Provides /proc/PID/schedstat
355 */
356static int proc_pid_schedstat(struct task_struct *task, char *buffer)
357{
Balbir Singh172ba842007-07-09 18:52:00 +0200358 return sprintf(buffer, "%llu %llu %lu\n",
Ingo Molnar826e08b2008-12-22 07:37:41 +0100359 (unsigned long long)task->se.sum_exec_runtime,
360 (unsigned long long)task->sched_info.run_delay,
Ingo Molnar2d723762007-10-15 17:00:12 +0200361 task->sched_info.pcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363#endif
364
Arjan van de Ven97455122008-01-25 21:08:34 +0100365#ifdef CONFIG_LATENCYTOP
366static int lstats_show_proc(struct seq_file *m, void *v)
367{
368 int i;
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800369 struct inode *inode = m->private;
370 struct task_struct *task = get_proc_task(inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100371
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800372 if (!task)
373 return -ESRCH;
374 seq_puts(m, "Latency Top version : v0.1\n");
Arjan van de Ven97455122008-01-25 21:08:34 +0100375 for (i = 0; i < 32; i++) {
376 if (task->latency_record[i].backtrace[0]) {
377 int q;
378 seq_printf(m, "%i %li %li ",
379 task->latency_record[i].count,
380 task->latency_record[i].time,
381 task->latency_record[i].max);
382 for (q = 0; q < LT_BACKTRACEDEPTH; q++) {
Hugh Dickins9c246242008-12-09 13:14:27 -0800383 char sym[KSYM_SYMBOL_LEN];
Arjan van de Ven97455122008-01-25 21:08:34 +0100384 char *c;
385 if (!task->latency_record[i].backtrace[q])
386 break;
387 if (task->latency_record[i].backtrace[q] == ULONG_MAX)
388 break;
389 sprint_symbol(sym, task->latency_record[i].backtrace[q]);
390 c = strchr(sym, '+');
391 if (c)
392 *c = 0;
393 seq_printf(m, "%s ", sym);
394 }
395 seq_printf(m, "\n");
396 }
397
398 }
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800399 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100400 return 0;
401}
402
403static int lstats_open(struct inode *inode, struct file *file)
404{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800405 return single_open(file, lstats_show_proc, inode);
Hiroshi Shimamotod6643d12008-02-14 10:27:00 -0800406}
407
Arjan van de Ven97455122008-01-25 21:08:34 +0100408static ssize_t lstats_write(struct file *file, const char __user *buf,
409 size_t count, loff_t *offs)
410{
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800411 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
Arjan van de Ven97455122008-01-25 21:08:34 +0100412
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800413 if (!task)
414 return -ESRCH;
Arjan van de Ven97455122008-01-25 21:08:34 +0100415 clear_all_latency_tracing(task);
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800416 put_task_struct(task);
Arjan van de Ven97455122008-01-25 21:08:34 +0100417
418 return count;
419}
420
421static const struct file_operations proc_lstats_operations = {
422 .open = lstats_open,
423 .read = seq_read,
424 .write = lstats_write,
425 .llseek = seq_lseek,
Hiroshi Shimamoto13d77c32008-02-20 16:53:29 -0800426 .release = single_release,
Arjan van de Ven97455122008-01-25 21:08:34 +0100427};
428
429#endif
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431static int proc_oom_score(struct task_struct *task, char *buffer)
432{
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200433 unsigned long points = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700435 read_lock(&tasklist_lock);
Oleg Nesterovb95c35e2010-04-01 15:13:57 +0200436 if (pid_alive(task))
David Rientjesa63d83f2010-08-09 17:19:46 -0700437 points = oom_badness(task, NULL, NULL,
438 totalram_pages + total_swap_pages);
Alexey Dobriyan19c5d452007-05-08 00:26:46 -0700439 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return sprintf(buffer, "%lu\n", points);
441}
442
Neil Hormand85f50d2007-10-18 23:40:37 -0700443struct limit_names {
444 char *name;
445 char *unit;
446};
447
448static const struct limit_names lnames[RLIM_NLIMITS] = {
Kees Cookcff4edb2009-09-22 16:45:32 -0700449 [RLIMIT_CPU] = {"Max cpu time", "seconds"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700450 [RLIMIT_FSIZE] = {"Max file size", "bytes"},
451 [RLIMIT_DATA] = {"Max data size", "bytes"},
452 [RLIMIT_STACK] = {"Max stack size", "bytes"},
453 [RLIMIT_CORE] = {"Max core file size", "bytes"},
454 [RLIMIT_RSS] = {"Max resident set", "bytes"},
455 [RLIMIT_NPROC] = {"Max processes", "processes"},
456 [RLIMIT_NOFILE] = {"Max open files", "files"},
457 [RLIMIT_MEMLOCK] = {"Max locked memory", "bytes"},
458 [RLIMIT_AS] = {"Max address space", "bytes"},
459 [RLIMIT_LOCKS] = {"Max file locks", "locks"},
460 [RLIMIT_SIGPENDING] = {"Max pending signals", "signals"},
461 [RLIMIT_MSGQUEUE] = {"Max msgqueue size", "bytes"},
462 [RLIMIT_NICE] = {"Max nice priority", NULL},
463 [RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
Eugene Teo88081172008-02-23 15:23:52 -0800464 [RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
Neil Hormand85f50d2007-10-18 23:40:37 -0700465};
466
467/* Display limits for a process */
468static int proc_pid_limits(struct task_struct *task, char *buffer)
469{
470 unsigned int i;
471 int count = 0;
472 unsigned long flags;
473 char *bufptr = buffer;
474
475 struct rlimit rlim[RLIM_NLIMITS];
476
Lai Jiangshana6bebbc2008-10-05 00:51:15 +0400477 if (!lock_task_sighand(task, &flags))
Neil Hormand85f50d2007-10-18 23:40:37 -0700478 return 0;
Neil Hormand85f50d2007-10-18 23:40:37 -0700479 memcpy(rlim, task->signal->rlim, sizeof(struct rlimit) * RLIM_NLIMITS);
480 unlock_task_sighand(task, &flags);
Neil Hormand85f50d2007-10-18 23:40:37 -0700481
482 /*
483 * print the file header
484 */
485 count += sprintf(&bufptr[count], "%-25s %-20s %-20s %-10s\n",
486 "Limit", "Soft Limit", "Hard Limit", "Units");
487
488 for (i = 0; i < RLIM_NLIMITS; i++) {
489 if (rlim[i].rlim_cur == RLIM_INFINITY)
490 count += sprintf(&bufptr[count], "%-25s %-20s ",
491 lnames[i].name, "unlimited");
492 else
493 count += sprintf(&bufptr[count], "%-25s %-20lu ",
494 lnames[i].name, rlim[i].rlim_cur);
495
496 if (rlim[i].rlim_max == RLIM_INFINITY)
497 count += sprintf(&bufptr[count], "%-20s ", "unlimited");
498 else
499 count += sprintf(&bufptr[count], "%-20lu ",
500 rlim[i].rlim_max);
501
502 if (lnames[i].unit)
503 count += sprintf(&bufptr[count], "%-10s\n",
504 lnames[i].unit);
505 else
506 count += sprintf(&bufptr[count], "\n");
507 }
508
509 return count;
510}
511
Roland McGrathebcb6732008-07-25 19:46:00 -0700512#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
513static int proc_pid_syscall(struct task_struct *task, char *buffer)
514{
515 long nr;
516 unsigned long args[6], sp, pc;
517
518 if (task_current_syscall(task, &nr, args, 6, &sp, &pc))
519 return sprintf(buffer, "running\n");
520
521 if (nr < 0)
522 return sprintf(buffer, "%ld 0x%lx 0x%lx\n", nr, sp, pc);
523
524 return sprintf(buffer,
525 "%ld 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx\n",
526 nr,
527 args[0], args[1], args[2], args[3], args[4], args[5],
528 sp, pc);
529}
530#endif /* CONFIG_HAVE_ARCH_TRACEHOOK */
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/************************************************************************/
533/* Here the fs part begins */
534/************************************************************************/
535
536/* permission checks */
Eric W. Biederman778c1142006-06-26 00:25:58 -0700537static int proc_fd_access_allowed(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Eric W. Biederman778c1142006-06-26 00:25:58 -0700539 struct task_struct *task;
540 int allowed = 0;
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700541 /* Allow access to a task's file descriptors if it is us or we
542 * may use ptrace attach to the process and find out that
543 * information.
Eric W. Biederman778c1142006-06-26 00:25:58 -0700544 */
545 task = get_proc_task(inode);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700546 if (task) {
Stephen Smalley006ebb42008-05-19 08:32:49 -0400547 allowed = ptrace_may_access(task, PTRACE_MODE_READ);
Eric W. Biederman778c1142006-06-26 00:25:58 -0700548 put_task_struct(task);
Eric W. Biedermandf26c402006-06-26 00:25:59 -0700549 }
Eric W. Biederman778c1142006-06-26 00:25:58 -0700550 return allowed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700553static int proc_setattr(struct dentry *dentry, struct iattr *attr)
554{
555 int error;
556 struct inode *inode = dentry->d_inode;
557
558 if (attr->ia_valid & ATTR_MODE)
559 return -EPERM;
560
561 error = inode_change_ok(inode, attr);
John Johansen1e8123f2007-05-08 00:29:41 -0700562 if (!error)
563 error = inode_setattr(inode, attr);
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700564 return error;
565}
566
Arjan van de Venc5ef1c42007-02-12 00:55:40 -0800567static const struct inode_operations proc_def_inode_operations = {
Linus Torvalds6d76fa52006-07-15 12:26:45 -0700568 .setattr = proc_setattr,
569};
570
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100571static int mounts_open_common(struct inode *inode, struct file *file,
572 const struct seq_operations *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Eric W. Biederman99f89552006-06-26 00:25:55 -0700574 struct task_struct *task = get_proc_task(inode);
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700575 struct nsproxy *nsp;
Kirill Korotaev6b3286e2006-12-08 02:37:56 -0800576 struct mnt_namespace *ns = NULL;
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100577 struct path root;
Al Viro5addc5d2005-11-07 17:15:49 -0500578 struct proc_mounts *p;
579 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Eric W. Biederman99f89552006-06-26 00:25:55 -0700581 if (task) {
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700582 rcu_read_lock();
583 nsp = task_nsproxy(task);
584 if (nsp) {
585 ns = nsp->mnt_ns;
Alexey Dobriyan863c4702007-01-26 00:56:53 -0800586 if (ns)
587 get_mnt_ns(ns);
588 }
Pavel Emelyanovcf7b7082007-10-18 23:39:54 -0700589 rcu_read_unlock();
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000590 if (ns && get_fs_path(task, &root, 1) == 0)
591 ret = 0;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700592 put_task_struct(task);
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100595 if (!ns)
596 goto err;
Hugh Dickins7c2c7d92009-03-28 23:21:27 +0000597 if (ret)
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100598 goto err_put_ns;
599
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100600 ret = -ENOMEM;
601 p = kmalloc(sizeof(struct proc_mounts), GFP_KERNEL);
602 if (!p)
603 goto err_put_path;
604
605 file->private_data = &p->m;
606 ret = seq_open(file, op);
607 if (ret)
608 goto err_free;
609
610 p->m.private = p;
611 p->ns = ns;
612 p->root = root;
613 p->event = ns->event;
614
615 return 0;
616
617 err_free:
618 kfree(p);
619 err_put_path:
620 path_put(&root);
621 err_put_ns:
622 put_mnt_ns(ns);
623 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return ret;
625}
626
627static int mounts_release(struct inode *inode, struct file *file)
628{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100629 struct proc_mounts *p = file->private_data;
630 path_put(&p->root);
631 put_mnt_ns(p->ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return seq_release(inode, file);
633}
634
Al Viro5addc5d2005-11-07 17:15:49 -0500635static unsigned mounts_poll(struct file *file, poll_table *wait)
636{
637 struct proc_mounts *p = file->private_data;
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900638 unsigned res = POLLIN | POLLRDNORM;
Al Viro5addc5d2005-11-07 17:15:49 -0500639
Al Viro9f5596a2010-02-05 00:40:25 -0500640 poll_wait(file, &p->ns->poll, wait);
641 if (mnt_had_events(p))
KOSAKI Motohiro31b07092009-04-09 13:57:59 +0900642 res |= POLLERR | POLLPRI;
Al Viro5addc5d2005-11-07 17:15:49 -0500643
644 return res;
645}
646
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100647static int mounts_open(struct inode *inode, struct file *file)
648{
649 return mounts_open_common(inode, file, &mounts_op);
650}
651
Arjan van de Ven00977a52007-02-12 00:55:34 -0800652static const struct file_operations proc_mounts_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 .open = mounts_open,
654 .read = seq_read,
655 .llseek = seq_lseek,
656 .release = mounts_release,
Al Viro5addc5d2005-11-07 17:15:49 -0500657 .poll = mounts_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658};
659
Ram Pai2d4d4862008-03-27 13:06:25 +0100660static int mountinfo_open(struct inode *inode, struct file *file)
661{
662 return mounts_open_common(inode, file, &mountinfo_op);
663}
664
665static const struct file_operations proc_mountinfo_operations = {
666 .open = mountinfo_open,
667 .read = seq_read,
668 .llseek = seq_lseek,
669 .release = mounts_release,
670 .poll = mounts_poll,
671};
672
Chuck Leverb4629fe2006-03-20 13:44:12 -0500673static int mountstats_open(struct inode *inode, struct file *file)
674{
Miklos Szeredia1a2c402008-03-27 13:06:24 +0100675 return mounts_open_common(inode, file, &mountstats_op);
Chuck Leverb4629fe2006-03-20 13:44:12 -0500676}
677
Arjan van de Ven00977a52007-02-12 00:55:34 -0800678static const struct file_operations proc_mountstats_operations = {
Chuck Leverb4629fe2006-03-20 13:44:12 -0500679 .open = mountstats_open,
680 .read = seq_read,
681 .llseek = seq_lseek,
682 .release = mounts_release,
683};
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#define PROC_BLOCK_SIZE (3*1024) /* 4K page size but our output routines use some slack for overruns */
686
687static ssize_t proc_info_read(struct file * file, char __user * buf,
688 size_t count, loff_t *ppos)
689{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800690 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 unsigned long page;
692 ssize_t length;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700693 struct task_struct *task = get_proc_task(inode);
694
695 length = -ESRCH;
696 if (!task)
697 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (count > PROC_BLOCK_SIZE)
700 count = PROC_BLOCK_SIZE;
Eric W. Biederman99f89552006-06-26 00:25:55 -0700701
702 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700703 if (!(page = __get_free_page(GFP_TEMPORARY)))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700704 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 length = PROC_I(inode)->op.proc_read(task, (char*)page);
707
708 if (length >= 0)
709 length = simple_read_from_buffer(buf, count, ppos, (char *)page, length);
710 free_page(page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700711out:
712 put_task_struct(task);
713out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return length;
715}
716
Arjan van de Ven00977a52007-02-12 00:55:34 -0800717static const struct file_operations proc_info_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 .read = proc_info_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100719 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720};
721
Eric W. Biedermanbe614082008-02-08 04:18:30 -0800722static int proc_single_show(struct seq_file *m, void *v)
723{
724 struct inode *inode = m->private;
725 struct pid_namespace *ns;
726 struct pid *pid;
727 struct task_struct *task;
728 int ret;
729
730 ns = inode->i_sb->s_fs_info;
731 pid = proc_pid(inode);
732 task = get_pid_task(pid, PIDTYPE_PID);
733 if (!task)
734 return -ESRCH;
735
736 ret = PROC_I(inode)->op.proc_show(m, ns, pid, task);
737
738 put_task_struct(task);
739 return ret;
740}
741
742static int proc_single_open(struct inode *inode, struct file *filp)
743{
744 int ret;
745 ret = single_open(filp, proc_single_show, NULL);
746 if (!ret) {
747 struct seq_file *m = filp->private_data;
748
749 m->private = inode;
750 }
751 return ret;
752}
753
754static const struct file_operations proc_single_file_operations = {
755 .open = proc_single_open,
756 .read = seq_read,
757 .llseek = seq_lseek,
758 .release = single_release,
759};
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static int mem_open(struct inode* inode, struct file* file)
762{
763 file->private_data = (void*)((long)current->self_exec_id);
764 return 0;
765}
766
767static ssize_t mem_read(struct file * file, char __user * buf,
768 size_t count, loff_t *ppos)
769{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800770 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 char *page;
772 unsigned long src = *ppos;
773 int ret = -ESRCH;
774 struct mm_struct *mm;
775
Eric W. Biederman99f89552006-06-26 00:25:55 -0700776 if (!task)
777 goto out_no_task;
778
Roland McGrath638fa2022008-04-29 01:01:38 -0700779 if (check_mem_permission(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 goto out;
781
782 ret = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700783 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!page)
785 goto out;
786
787 ret = 0;
788
789 mm = get_task_mm(task);
790 if (!mm)
791 goto out_free;
792
793 ret = -EIO;
794
795 if (file->private_data != (void*)((long)current->self_exec_id))
796 goto out_put;
797
798 ret = 0;
799
800 while (count > 0) {
801 int this_len, retval;
802
803 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
804 retval = access_process_vm(task, src, page, this_len, 0);
Roland McGrath638fa2022008-04-29 01:01:38 -0700805 if (!retval || check_mem_permission(task)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (!ret)
807 ret = -EIO;
808 break;
809 }
810
811 if (copy_to_user(buf, page, retval)) {
812 ret = -EFAULT;
813 break;
814 }
815
816 ret += retval;
817 src += retval;
818 buf += retval;
819 count -= retval;
820 }
821 *ppos = src;
822
823out_put:
824 mmput(mm);
825out_free:
826 free_page((unsigned long) page);
827out:
Eric W. Biederman99f89552006-06-26 00:25:55 -0700828 put_task_struct(task);
829out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return ret;
831}
832
833#define mem_write NULL
834
835#ifndef mem_write
836/* This is a security hazard */
Glauber de Oliveira Costa63967fa2007-02-20 13:58:12 -0800837static ssize_t mem_write(struct file * file, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 size_t count, loff_t *ppos)
839{
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700840 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 char *page;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800842 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 unsigned long dst = *ppos;
844
Eric W. Biederman99f89552006-06-26 00:25:55 -0700845 copied = -ESRCH;
846 if (!task)
847 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Roland McGrath638fa2022008-04-29 01:01:38 -0700849 if (check_mem_permission(task))
Eric W. Biederman99f89552006-06-26 00:25:55 -0700850 goto out;
851
852 copied = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -0700853 page = (char *)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!page)
Eric W. Biederman99f89552006-06-26 00:25:55 -0700855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Frederik Deweerdtf7ca54f2006-09-29 02:01:02 -0700857 copied = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 while (count > 0) {
859 int this_len, retval;
860
861 this_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
862 if (copy_from_user(page, buf, this_len)) {
863 copied = -EFAULT;
864 break;
865 }
866 retval = access_process_vm(task, dst, page, this_len, 1);
867 if (!retval) {
868 if (!copied)
869 copied = -EIO;
870 break;
871 }
872 copied += retval;
873 buf += retval;
874 dst += retval;
875 count -= retval;
876 }
877 *ppos = dst;
878 free_page((unsigned long) page);
Eric W. Biederman99f89552006-06-26 00:25:55 -0700879out:
880 put_task_struct(task);
881out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return copied;
883}
884#endif
885
Matt Mackall85863e42008-02-04 22:29:04 -0800886loff_t mem_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 switch (orig) {
889 case 0:
890 file->f_pos = offset;
891 break;
892 case 1:
893 file->f_pos += offset;
894 break;
895 default:
896 return -EINVAL;
897 }
898 force_successful_syscall_return();
899 return file->f_pos;
900}
901
Arjan van de Ven00977a52007-02-12 00:55:34 -0800902static const struct file_operations proc_mem_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 .llseek = mem_lseek,
904 .read = mem_read,
905 .write = mem_write,
906 .open = mem_open,
907};
908
James Pearson315e28c2007-10-16 23:30:17 -0700909static ssize_t environ_read(struct file *file, char __user *buf,
910 size_t count, loff_t *ppos)
911{
912 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
913 char *page;
914 unsigned long src = *ppos;
915 int ret = -ESRCH;
916 struct mm_struct *mm;
917
918 if (!task)
919 goto out_no_task;
920
Stephen Smalley006ebb42008-05-19 08:32:49 -0400921 if (!ptrace_may_access(task, PTRACE_MODE_READ))
James Pearson315e28c2007-10-16 23:30:17 -0700922 goto out;
923
924 ret = -ENOMEM;
925 page = (char *)__get_free_page(GFP_TEMPORARY);
926 if (!page)
927 goto out;
928
929 ret = 0;
930
931 mm = get_task_mm(task);
932 if (!mm)
933 goto out_free;
934
935 while (count > 0) {
936 int this_len, retval, max_len;
937
938 this_len = mm->env_end - (mm->env_start + src);
939
940 if (this_len <= 0)
941 break;
942
943 max_len = (count > PAGE_SIZE) ? PAGE_SIZE : count;
944 this_len = (this_len > max_len) ? max_len : this_len;
945
946 retval = access_process_vm(task, (mm->env_start + src),
947 page, this_len, 0);
948
949 if (retval <= 0) {
950 ret = retval;
951 break;
952 }
953
954 if (copy_to_user(buf, page, retval)) {
955 ret = -EFAULT;
956 break;
957 }
958
959 ret += retval;
960 src += retval;
961 buf += retval;
962 count -= retval;
963 }
964 *ppos = src;
965
966 mmput(mm);
967out_free:
968 free_page((unsigned long) page);
969out:
970 put_task_struct(task);
971out_no_task:
972 return ret;
973}
974
975static const struct file_operations proc_environ_operations = {
976 .read = environ_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +0100977 .llseek = generic_file_llseek,
James Pearson315e28c2007-10-16 23:30:17 -0700978};
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980static ssize_t oom_adjust_read(struct file *file, char __user *buf,
981 size_t count, loff_t *ppos)
982{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -0800983 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700984 char buffer[PROC_NUMBUF];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 size_t len;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700986 int oom_adjust = OOM_DISABLE;
987 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Eric W. Biederman99f89552006-06-26 00:25:55 -0700989 if (!task)
990 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -0700991
992 if (lock_task_sighand(task, &flags)) {
993 oom_adjust = task->signal->oom_adj;
994 unlock_task_sighand(task, &flags);
995 }
996
Eric W. Biederman99f89552006-06-26 00:25:55 -0700997 put_task_struct(task);
998
Eric W. Biederman8578cea2006-06-26 00:25:54 -0700999 len = snprintf(buffer, sizeof(buffer), "%i\n", oom_adjust);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001000
1001 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004static ssize_t oom_adjust_write(struct file *file, const char __user *buf,
1005 size_t count, loff_t *ppos)
1006{
Eric W. Biederman99f89552006-06-26 00:25:55 -07001007 struct task_struct *task;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001008 char buffer[PROC_NUMBUF];
1009 long oom_adjust;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001010 unsigned long flags;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Eric W. Biederman8578cea2006-06-26 00:25:54 -07001013 memset(buffer, 0, sizeof(buffer));
1014 if (count > sizeof(buffer) - 1)
1015 count = sizeof(buffer) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (copy_from_user(buffer, buf, count))
1017 return -EFAULT;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001018
1019 err = strict_strtol(strstrip(buffer), 0, &oom_adjust);
1020 if (err)
1021 return -EINVAL;
Alexey Dobriyan8ac773b2006-10-19 23:28:32 -07001022 if ((oom_adjust < OOM_ADJUST_MIN || oom_adjust > OOM_ADJUST_MAX) &&
1023 oom_adjust != OOM_DISABLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -EINVAL;
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001025
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001026 task = get_proc_task(file->f_path.dentry->d_inode);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001027 if (!task)
1028 return -ESRCH;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001029 if (!lock_task_sighand(task, &flags)) {
1030 put_task_struct(task);
1031 return -ESRCH;
1032 }
1033
1034 if (oom_adjust < task->signal->oom_adj && !capable(CAP_SYS_RESOURCE)) {
1035 unlock_task_sighand(task, &flags);
Guillem Jover8fb4fc62006-12-06 20:32:24 -08001036 put_task_struct(task);
1037 return -EACCES;
1038 }
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001039
David Rientjes51b1bd22010-08-09 17:19:47 -07001040 /*
1041 * Warn that /proc/pid/oom_adj is deprecated, see
1042 * Documentation/feature-removal-schedule.txt.
1043 */
1044 printk_once(KERN_WARNING "%s (%d): /proc/%d/oom_adj is deprecated, "
1045 "please use /proc/%d/oom_score_adj instead.\n",
1046 current->comm, task_pid_nr(current),
1047 task_pid_nr(task), task_pid_nr(task));
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001048 task->signal->oom_adj = oom_adjust;
David Rientjesa63d83f2010-08-09 17:19:46 -07001049 /*
1050 * Scale /proc/pid/oom_score_adj appropriately ensuring that a maximum
1051 * value is always attainable.
1052 */
1053 if (task->signal->oom_adj == OOM_ADJUST_MAX)
1054 task->signal->oom_score_adj = OOM_SCORE_ADJ_MAX;
1055 else
1056 task->signal->oom_score_adj = (oom_adjust * OOM_SCORE_ADJ_MAX) /
1057 -OOM_DISABLE;
KOSAKI Motohiro28b83c52009-09-21 17:03:13 -07001058 unlock_task_sighand(task, &flags);
Eric W. Biederman99f89552006-06-26 00:25:55 -07001059 put_task_struct(task);
KOSAKI Motohiro5d863b82009-09-21 17:03:16 -07001060
1061 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
Arjan van de Ven00977a52007-02-12 00:55:34 -08001064static const struct file_operations proc_oom_adjust_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 .read = oom_adjust_read,
1066 .write = oom_adjust_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001067 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068};
1069
David Rientjesa63d83f2010-08-09 17:19:46 -07001070static ssize_t oom_score_adj_read(struct file *file, char __user *buf,
1071 size_t count, loff_t *ppos)
1072{
1073 struct task_struct *task = get_proc_task(file->f_path.dentry->d_inode);
1074 char buffer[PROC_NUMBUF];
1075 int oom_score_adj = OOM_SCORE_ADJ_MIN;
1076 unsigned long flags;
1077 size_t len;
1078
1079 if (!task)
1080 return -ESRCH;
1081 if (lock_task_sighand(task, &flags)) {
1082 oom_score_adj = task->signal->oom_score_adj;
1083 unlock_task_sighand(task, &flags);
1084 }
1085 put_task_struct(task);
1086 len = snprintf(buffer, sizeof(buffer), "%d\n", oom_score_adj);
1087 return simple_read_from_buffer(buf, count, ppos, buffer, len);
1088}
1089
1090static ssize_t oom_score_adj_write(struct file *file, const char __user *buf,
1091 size_t count, loff_t *ppos)
1092{
1093 struct task_struct *task;
1094 char buffer[PROC_NUMBUF];
1095 unsigned long flags;
1096 long oom_score_adj;
1097 int err;
1098
1099 memset(buffer, 0, sizeof(buffer));
1100 if (count > sizeof(buffer) - 1)
1101 count = sizeof(buffer) - 1;
1102 if (copy_from_user(buffer, buf, count))
1103 return -EFAULT;
1104
1105 err = strict_strtol(strstrip(buffer), 0, &oom_score_adj);
1106 if (err)
1107 return -EINVAL;
1108 if (oom_score_adj < OOM_SCORE_ADJ_MIN ||
1109 oom_score_adj > OOM_SCORE_ADJ_MAX)
1110 return -EINVAL;
1111
1112 task = get_proc_task(file->f_path.dentry->d_inode);
1113 if (!task)
1114 return -ESRCH;
1115 if (!lock_task_sighand(task, &flags)) {
1116 put_task_struct(task);
1117 return -ESRCH;
1118 }
1119 if (oom_score_adj < task->signal->oom_score_adj &&
1120 !capable(CAP_SYS_RESOURCE)) {
1121 unlock_task_sighand(task, &flags);
1122 put_task_struct(task);
1123 return -EACCES;
1124 }
1125
1126 task->signal->oom_score_adj = oom_score_adj;
1127 /*
1128 * Scale /proc/pid/oom_adj appropriately ensuring that OOM_DISABLE is
1129 * always attainable.
1130 */
1131 if (task->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
1132 task->signal->oom_adj = OOM_DISABLE;
1133 else
1134 task->signal->oom_adj = (oom_score_adj * OOM_ADJUST_MAX) /
1135 OOM_SCORE_ADJ_MAX;
1136 unlock_task_sighand(task, &flags);
1137 put_task_struct(task);
1138 return count;
1139}
1140
1141static const struct file_operations proc_oom_score_adj_operations = {
1142 .read = oom_score_adj_read,
1143 .write = oom_score_adj_write,
1144};
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146#ifdef CONFIG_AUDITSYSCALL
1147#define TMPBUFLEN 21
1148static ssize_t proc_loginuid_read(struct file * file, char __user * buf,
1149 size_t count, loff_t *ppos)
1150{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001151 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001152 struct task_struct *task = get_proc_task(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ssize_t length;
1154 char tmpbuf[TMPBUFLEN];
1155
Eric W. Biederman99f89552006-06-26 00:25:55 -07001156 if (!task)
1157 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
Al Viro0c11b942008-01-10 04:20:52 -05001159 audit_get_loginuid(task));
Eric W. Biederman99f89552006-06-26 00:25:55 -07001160 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1162}
1163
1164static ssize_t proc_loginuid_write(struct file * file, const char __user * buf,
1165 size_t count, loff_t *ppos)
1166{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001167 struct inode * inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 char *page, *tmp;
1169 ssize_t length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 uid_t loginuid;
1171
1172 if (!capable(CAP_AUDIT_CONTROL))
1173 return -EPERM;
1174
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001175 rcu_read_lock();
1176 if (current != pid_task(proc_pid(inode), PIDTYPE_PID)) {
1177 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -EPERM;
Paul E. McKenney7dc52152010-02-22 17:04:52 -08001179 }
1180 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
Al Viroe0182902006-05-18 08:28:02 -04001182 if (count >= PAGE_SIZE)
1183 count = PAGE_SIZE - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (*ppos != 0) {
1186 /* No partial writes. */
1187 return -EINVAL;
1188 }
Mel Gormane12ba742007-10-16 01:25:52 -07001189 page = (char*)__get_free_page(GFP_TEMPORARY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (!page)
1191 return -ENOMEM;
1192 length = -EFAULT;
1193 if (copy_from_user(page, buf, count))
1194 goto out_free_page;
1195
Al Viroe0182902006-05-18 08:28:02 -04001196 page[count] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 loginuid = simple_strtoul(page, &tmp, 10);
1198 if (tmp == page) {
1199 length = -EINVAL;
1200 goto out_free_page;
1201
1202 }
Eric W. Biederman99f89552006-06-26 00:25:55 -07001203 length = audit_set_loginuid(current, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (likely(length == 0))
1205 length = count;
1206
1207out_free_page:
1208 free_page((unsigned long) page);
1209 return length;
1210}
1211
Arjan van de Ven00977a52007-02-12 00:55:34 -08001212static const struct file_operations proc_loginuid_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 .read = proc_loginuid_read,
1214 .write = proc_loginuid_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001215 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216};
Eric Paris1e0bd752008-03-13 08:15:31 -04001217
1218static ssize_t proc_sessionid_read(struct file * file, char __user * buf,
1219 size_t count, loff_t *ppos)
1220{
1221 struct inode * inode = file->f_path.dentry->d_inode;
1222 struct task_struct *task = get_proc_task(inode);
1223 ssize_t length;
1224 char tmpbuf[TMPBUFLEN];
1225
1226 if (!task)
1227 return -ESRCH;
1228 length = scnprintf(tmpbuf, TMPBUFLEN, "%u",
1229 audit_get_sessionid(task));
1230 put_task_struct(task);
1231 return simple_read_from_buffer(buf, count, ppos, tmpbuf, length);
1232}
1233
1234static const struct file_operations proc_sessionid_operations = {
1235 .read = proc_sessionid_read,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001236 .llseek = generic_file_llseek,
Eric Paris1e0bd752008-03-13 08:15:31 -04001237};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238#endif
1239
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001240#ifdef CONFIG_FAULT_INJECTION
1241static ssize_t proc_fault_inject_read(struct file * file, char __user * buf,
1242 size_t count, loff_t *ppos)
1243{
1244 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
1245 char buffer[PROC_NUMBUF];
1246 size_t len;
1247 int make_it_fail;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001248
1249 if (!task)
1250 return -ESRCH;
1251 make_it_fail = task->make_it_fail;
1252 put_task_struct(task);
1253
1254 len = snprintf(buffer, sizeof(buffer), "%i\n", make_it_fail);
Akinobu Mita0c28f282007-05-08 00:31:41 -07001255
1256 return simple_read_from_buffer(buf, count, ppos, buffer, len);
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001257}
1258
1259static ssize_t proc_fault_inject_write(struct file * file,
1260 const char __user * buf, size_t count, loff_t *ppos)
1261{
1262 struct task_struct *task;
1263 char buffer[PROC_NUMBUF], *end;
1264 int make_it_fail;
1265
1266 if (!capable(CAP_SYS_RESOURCE))
1267 return -EPERM;
1268 memset(buffer, 0, sizeof(buffer));
1269 if (count > sizeof(buffer) - 1)
1270 count = sizeof(buffer) - 1;
1271 if (copy_from_user(buffer, buf, count))
1272 return -EFAULT;
Vincent Licba8aaf2009-09-22 16:45:38 -07001273 make_it_fail = simple_strtol(strstrip(buffer), &end, 0);
1274 if (*end)
1275 return -EINVAL;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001276 task = get_proc_task(file->f_dentry->d_inode);
1277 if (!task)
1278 return -ESRCH;
1279 task->make_it_fail = make_it_fail;
1280 put_task_struct(task);
Vincent Licba8aaf2009-09-22 16:45:38 -07001281
1282 return count;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001283}
1284
Arjan van de Ven00977a52007-02-12 00:55:34 -08001285static const struct file_operations proc_fault_inject_operations = {
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001286 .read = proc_fault_inject_read,
1287 .write = proc_fault_inject_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01001288 .llseek = generic_file_llseek,
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08001289};
1290#endif
1291
Arjan van de Ven97455122008-01-25 21:08:34 +01001292
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001293#ifdef CONFIG_SCHED_DEBUG
1294/*
1295 * Print out various scheduling related per-task fields:
1296 */
1297static int sched_show(struct seq_file *m, void *v)
1298{
1299 struct inode *inode = m->private;
1300 struct task_struct *p;
1301
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001302 p = get_proc_task(inode);
1303 if (!p)
1304 return -ESRCH;
1305 proc_sched_show_task(p, m);
1306
1307 put_task_struct(p);
1308
1309 return 0;
1310}
1311
1312static ssize_t
1313sched_write(struct file *file, const char __user *buf,
1314 size_t count, loff_t *offset)
1315{
1316 struct inode *inode = file->f_path.dentry->d_inode;
1317 struct task_struct *p;
1318
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001319 p = get_proc_task(inode);
1320 if (!p)
1321 return -ESRCH;
1322 proc_sched_set_task(p);
1323
1324 put_task_struct(p);
1325
1326 return count;
1327}
1328
1329static int sched_open(struct inode *inode, struct file *filp)
1330{
1331 int ret;
1332
1333 ret = single_open(filp, sched_show, NULL);
1334 if (!ret) {
1335 struct seq_file *m = filp->private_data;
1336
1337 m->private = inode;
1338 }
1339 return ret;
1340}
1341
1342static const struct file_operations proc_pid_sched_operations = {
1343 .open = sched_open,
1344 .read = seq_read,
1345 .write = sched_write,
1346 .llseek = seq_lseek,
Alexey Dobriyan5ea473a2007-07-31 00:38:50 -07001347 .release = single_release,
Ingo Molnar43ae34c2007-07-09 18:52:00 +02001348};
1349
1350#endif
1351
john stultz4614a696b2009-12-14 18:00:05 -08001352static ssize_t comm_write(struct file *file, const char __user *buf,
1353 size_t count, loff_t *offset)
1354{
1355 struct inode *inode = file->f_path.dentry->d_inode;
1356 struct task_struct *p;
1357 char buffer[TASK_COMM_LEN];
1358
1359 memset(buffer, 0, sizeof(buffer));
1360 if (count > sizeof(buffer) - 1)
1361 count = sizeof(buffer) - 1;
1362 if (copy_from_user(buffer, buf, count))
1363 return -EFAULT;
1364
1365 p = get_proc_task(inode);
1366 if (!p)
1367 return -ESRCH;
1368
1369 if (same_thread_group(current, p))
1370 set_task_comm(p, buffer);
1371 else
1372 count = -EINVAL;
1373
1374 put_task_struct(p);
1375
1376 return count;
1377}
1378
1379static int comm_show(struct seq_file *m, void *v)
1380{
1381 struct inode *inode = m->private;
1382 struct task_struct *p;
1383
1384 p = get_proc_task(inode);
1385 if (!p)
1386 return -ESRCH;
1387
1388 task_lock(p);
1389 seq_printf(m, "%s\n", p->comm);
1390 task_unlock(p);
1391
1392 put_task_struct(p);
1393
1394 return 0;
1395}
1396
1397static int comm_open(struct inode *inode, struct file *filp)
1398{
1399 int ret;
1400
1401 ret = single_open(filp, comm_show, NULL);
1402 if (!ret) {
1403 struct seq_file *m = filp->private_data;
1404
1405 m->private = inode;
1406 }
1407 return ret;
1408}
1409
1410static const struct file_operations proc_pid_set_comm_operations = {
1411 .open = comm_open,
1412 .read = seq_read,
1413 .write = comm_write,
1414 .llseek = seq_lseek,
1415 .release = single_release,
1416};
1417
Matt Helsley925d1c42008-04-29 01:01:36 -07001418/*
1419 * We added or removed a vma mapping the executable. The vmas are only mapped
1420 * during exec and are not mapped with the mmap system call.
1421 * Callers must hold down_write() on the mm's mmap_sem for these
1422 */
1423void added_exe_file_vma(struct mm_struct *mm)
1424{
1425 mm->num_exe_file_vmas++;
1426}
1427
1428void removed_exe_file_vma(struct mm_struct *mm)
1429{
1430 mm->num_exe_file_vmas--;
1431 if ((mm->num_exe_file_vmas == 0) && mm->exe_file){
1432 fput(mm->exe_file);
1433 mm->exe_file = NULL;
1434 }
1435
1436}
1437
1438void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
1439{
1440 if (new_exe_file)
1441 get_file(new_exe_file);
1442 if (mm->exe_file)
1443 fput(mm->exe_file);
1444 mm->exe_file = new_exe_file;
1445 mm->num_exe_file_vmas = 0;
1446}
1447
1448struct file *get_mm_exe_file(struct mm_struct *mm)
1449{
1450 struct file *exe_file;
1451
1452 /* We need mmap_sem to protect against races with removal of
1453 * VM_EXECUTABLE vmas */
1454 down_read(&mm->mmap_sem);
1455 exe_file = mm->exe_file;
1456 if (exe_file)
1457 get_file(exe_file);
1458 up_read(&mm->mmap_sem);
1459 return exe_file;
1460}
1461
1462void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
1463{
1464 /* It's safe to write the exe_file pointer without exe_file_lock because
1465 * this is called during fork when the task is not yet in /proc */
1466 newmm->exe_file = get_mm_exe_file(oldmm);
1467}
1468
1469static int proc_exe_link(struct inode *inode, struct path *exe_path)
1470{
1471 struct task_struct *task;
1472 struct mm_struct *mm;
1473 struct file *exe_file;
1474
1475 task = get_proc_task(inode);
1476 if (!task)
1477 return -ENOENT;
1478 mm = get_task_mm(task);
1479 put_task_struct(task);
1480 if (!mm)
1481 return -ENOENT;
1482 exe_file = get_mm_exe_file(mm);
1483 mmput(mm);
1484 if (exe_file) {
1485 *exe_path = exe_file->f_path;
1486 path_get(&exe_file->f_path);
1487 fput(exe_file);
1488 return 0;
1489 } else
1490 return -ENOENT;
1491}
1492
Al Viro008b1502005-08-20 00:17:39 +01001493static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
1495 struct inode *inode = dentry->d_inode;
1496 int error = -EACCES;
1497
1498 /* We don't need a base pointer in the /proc filesystem */
Jan Blunck1d957f92008-02-14 19:34:35 -08001499 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Eric W. Biederman778c1142006-06-26 00:25:58 -07001501 /* Are we allowed to snoop on the tasks file descriptors? */
1502 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001505 error = PROC_I(inode)->op.proc_get_link(inode, &nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506out:
Al Viro008b1502005-08-20 00:17:39 +01001507 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001510static int do_proc_readlink(struct path *path, char __user *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
Mel Gormane12ba742007-10-16 01:25:52 -07001512 char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001513 char *pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 int len;
1515
1516 if (!tmp)
1517 return -ENOMEM;
Akinobu Mita0c28f282007-05-08 00:31:41 -07001518
Jan Blunckcf28b482008-02-14 19:38:44 -08001519 pathname = d_path(path, tmp, PAGE_SIZE);
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001520 len = PTR_ERR(pathname);
1521 if (IS_ERR(pathname))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 goto out;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001523 len = tmp + PAGE_SIZE - 1 - pathname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 if (len > buflen)
1526 len = buflen;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001527 if (copy_to_user(buffer, pathname, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 len = -EFAULT;
1529 out:
1530 free_page((unsigned long)tmp);
1531 return len;
1532}
1533
1534static int proc_pid_readlink(struct dentry * dentry, char __user * buffer, int buflen)
1535{
1536 int error = -EACCES;
1537 struct inode *inode = dentry->d_inode;
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001538 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Eric W. Biederman778c1142006-06-26 00:25:58 -07001540 /* Are we allowed to snoop on the tasks file descriptors? */
1541 if (!proc_fd_access_allowed(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001544 error = PROC_I(inode)->op.proc_get_link(inode, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 if (error)
1546 goto out;
1547
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001548 error = do_proc_readlink(&path, buffer, buflen);
1549 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return error;
1552}
1553
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08001554static const struct inode_operations proc_pid_link_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 .readlink = proc_pid_readlink,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07001556 .follow_link = proc_pid_follow_link,
1557 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558};
1559
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001560
1561/* building an inode */
1562
1563static int task_dumpable(struct task_struct *task)
1564{
1565 int dumpable = 0;
1566 struct mm_struct *mm;
1567
1568 task_lock(task);
1569 mm = task->mm;
1570 if (mm)
Kawai, Hidehiro6c5d5232007-07-19 01:48:27 -07001571 dumpable = get_dumpable(mm);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001572 task_unlock(task);
1573 if(dumpable == 1)
1574 return 1;
1575 return 0;
1576}
1577
1578
Eric W. Biederman61a28782006-10-02 02:18:49 -07001579static struct inode *proc_pid_make_inode(struct super_block * sb, struct task_struct *task)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001580{
1581 struct inode * inode;
1582 struct proc_inode *ei;
David Howellsc69e8d92008-11-14 10:39:19 +11001583 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001584
1585 /* We need a new inode */
1586
1587 inode = new_inode(sb);
1588 if (!inode)
1589 goto out;
1590
1591 /* Common stuff */
1592 ei = PROC_I(inode);
1593 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001594 inode->i_op = &proc_def_inode_operations;
1595
1596 /*
1597 * grab the reference to task.
1598 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07001599 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001600 if (!ei->pid)
1601 goto out_unlock;
1602
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001603 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001604 rcu_read_lock();
1605 cred = __task_cred(task);
1606 inode->i_uid = cred->euid;
1607 inode->i_gid = cred->egid;
1608 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001609 }
1610 security_task_to_inode(task, inode);
1611
1612out:
1613 return inode;
1614
1615out_unlock:
1616 iput(inode);
1617 return NULL;
1618}
1619
1620static int pid_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
1621{
1622 struct inode *inode = dentry->d_inode;
1623 struct task_struct *task;
David Howellsc69e8d92008-11-14 10:39:19 +11001624 const struct cred *cred;
1625
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001626 generic_fillattr(inode, stat);
1627
1628 rcu_read_lock();
1629 stat->uid = 0;
1630 stat->gid = 0;
1631 task = pid_task(proc_pid(inode), PIDTYPE_PID);
1632 if (task) {
1633 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1634 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001635 cred = __task_cred(task);
1636 stat->uid = cred->euid;
1637 stat->gid = cred->egid;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001638 }
1639 }
1640 rcu_read_unlock();
1641 return 0;
1642}
1643
1644/* dentry stuff */
1645
1646/*
1647 * Exceptional case: normally we are not allowed to unhash a busy
1648 * directory. In this case, however, we can do it - no aliasing problems
1649 * due to the way we treat inodes.
1650 *
1651 * Rewrite the inode's ownerships here because the owning task may have
1652 * performed a setuid(), etc.
1653 *
1654 * Before the /proc/pid/status file was created the only way to read
1655 * the effective uid of a /process was to stat /proc/pid. Reading
1656 * /proc/pid/status is slow enough that procps and other packages
1657 * kept stating /proc/pid. To keep the rules in /proc simple I have
1658 * made this apply to all per process world readable and executable
1659 * directories.
1660 */
1661static int pid_revalidate(struct dentry *dentry, struct nameidata *nd)
1662{
1663 struct inode *inode = dentry->d_inode;
1664 struct task_struct *task = get_proc_task(inode);
David Howellsc69e8d92008-11-14 10:39:19 +11001665 const struct cred *cred;
1666
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001667 if (task) {
1668 if ((inode->i_mode == (S_IFDIR|S_IRUGO|S_IXUGO)) ||
1669 task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001670 rcu_read_lock();
1671 cred = __task_cred(task);
1672 inode->i_uid = cred->euid;
1673 inode->i_gid = cred->egid;
1674 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001675 } else {
1676 inode->i_uid = 0;
1677 inode->i_gid = 0;
1678 }
1679 inode->i_mode &= ~(S_ISUID | S_ISGID);
1680 security_task_to_inode(task, inode);
1681 put_task_struct(task);
1682 return 1;
1683 }
1684 d_drop(dentry);
1685 return 0;
1686}
1687
1688static int pid_delete_dentry(struct dentry * dentry)
1689{
1690 /* Is the task we represent dead?
1691 * If so, then don't put the dentry on the lru list,
1692 * kill it immediately.
1693 */
1694 return !proc_pid(dentry->d_inode)->tasks[PIDTYPE_PID].first;
1695}
1696
Al Virod72f71e2009-02-20 05:58:47 +00001697static const struct dentry_operations pid_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001698{
1699 .d_revalidate = pid_revalidate,
1700 .d_delete = pid_delete_dentry,
1701};
1702
1703/* Lookups */
1704
Eric Dumazetc5141e62007-05-08 00:26:15 -07001705typedef struct dentry *instantiate_t(struct inode *, struct dentry *,
1706 struct task_struct *, const void *);
Eric W. Biederman61a28782006-10-02 02:18:49 -07001707
Eric W. Biederman1c0d04c2006-10-02 02:18:57 -07001708/*
1709 * Fill a directory entry.
1710 *
1711 * If possible create the dcache entry and derive our inode number and
1712 * file type from dcache entry.
1713 *
1714 * Since all of the proc inode numbers are dynamically generated, the inode
1715 * numbers do not exist until the inode is cache. This means creating the
1716 * the dcache entry in readdir is necessary to keep the inode numbers
1717 * reported by readdir in sync with the inode numbers reported
1718 * by stat.
1719 */
Eric W. Biederman61a28782006-10-02 02:18:49 -07001720static int proc_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
1721 char *name, int len,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001722 instantiate_t instantiate, struct task_struct *task, const void *ptr)
Eric W. Biederman61a28782006-10-02 02:18:49 -07001723{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001724 struct dentry *child, *dir = filp->f_path.dentry;
Eric W. Biederman61a28782006-10-02 02:18:49 -07001725 struct inode *inode;
1726 struct qstr qname;
1727 ino_t ino = 0;
1728 unsigned type = DT_UNKNOWN;
1729
1730 qname.name = name;
1731 qname.len = len;
1732 qname.hash = full_name_hash(name, len);
1733
1734 child = d_lookup(dir, &qname);
1735 if (!child) {
1736 struct dentry *new;
1737 new = d_alloc(dir, &qname);
1738 if (new) {
1739 child = instantiate(dir->d_inode, new, task, ptr);
1740 if (child)
1741 dput(new);
1742 else
1743 child = new;
1744 }
1745 }
1746 if (!child || IS_ERR(child) || !child->d_inode)
1747 goto end_instantiate;
1748 inode = child->d_inode;
1749 if (inode) {
1750 ino = inode->i_ino;
1751 type = inode->i_mode >> 12;
1752 }
1753 dput(child);
1754end_instantiate:
1755 if (!ino)
1756 ino = find_inode_number(dir, &qname);
1757 if (!ino)
1758 ino = 1;
1759 return filldir(dirent, name, len, filp->f_pos, ino, type);
1760}
1761
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001762static unsigned name_to_int(struct dentry *dentry)
1763{
1764 const char *name = dentry->d_name.name;
1765 int len = dentry->d_name.len;
1766 unsigned n = 0;
1767
1768 if (len > 1 && *name == '0')
1769 goto out;
1770 while (len-- > 0) {
1771 unsigned c = *name++ - '0';
1772 if (c > 9)
1773 goto out;
1774 if (n >= (~0U-9)/10)
1775 goto out;
1776 n *= 10;
1777 n += c;
1778 }
1779 return n;
1780out:
1781 return ~0U;
1782}
1783
Miklos Szeredi27932742007-05-08 00:26:17 -07001784#define PROC_FDINFO_MAX 64
1785
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001786static int proc_fd_info(struct inode *inode, struct path *path, char *info)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001787{
1788 struct task_struct *task = get_proc_task(inode);
1789 struct files_struct *files = NULL;
1790 struct file *file;
1791 int fd = proc_fd(inode);
1792
1793 if (task) {
1794 files = get_files_struct(task);
1795 put_task_struct(task);
1796 }
1797 if (files) {
1798 /*
1799 * We are not taking a ref to the file structure, so we must
1800 * hold ->file_lock.
1801 */
1802 spin_lock(&files->file_lock);
1803 file = fcheck_files(files, fd);
1804 if (file) {
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001805 if (path) {
1806 *path = file->f_path;
1807 path_get(&file->f_path);
1808 }
Miklos Szeredi27932742007-05-08 00:26:17 -07001809 if (info)
1810 snprintf(info, PROC_FDINFO_MAX,
1811 "pos:\t%lli\n"
1812 "flags:\t0%o\n",
1813 (long long) file->f_pos,
1814 file->f_flags);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001815 spin_unlock(&files->file_lock);
1816 put_files_struct(files);
1817 return 0;
1818 }
1819 spin_unlock(&files->file_lock);
1820 put_files_struct(files);
1821 }
1822 return -ENOENT;
1823}
1824
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001825static int proc_fd_link(struct inode *inode, struct path *path)
Miklos Szeredi27932742007-05-08 00:26:17 -07001826{
Jan Blunck3dcd25f2008-02-14 19:38:35 -08001827 return proc_fd_info(inode, path, NULL);
Miklos Szeredi27932742007-05-08 00:26:17 -07001828}
1829
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001830static int tid_fd_revalidate(struct dentry *dentry, struct nameidata *nd)
1831{
1832 struct inode *inode = dentry->d_inode;
1833 struct task_struct *task = get_proc_task(inode);
1834 int fd = proc_fd(inode);
1835 struct files_struct *files;
David Howellsc69e8d92008-11-14 10:39:19 +11001836 const struct cred *cred;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001837
1838 if (task) {
1839 files = get_files_struct(task);
1840 if (files) {
1841 rcu_read_lock();
1842 if (fcheck_files(files, fd)) {
1843 rcu_read_unlock();
1844 put_files_struct(files);
1845 if (task_dumpable(task)) {
David Howellsc69e8d92008-11-14 10:39:19 +11001846 rcu_read_lock();
1847 cred = __task_cred(task);
1848 inode->i_uid = cred->euid;
1849 inode->i_gid = cred->egid;
1850 rcu_read_unlock();
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001851 } else {
1852 inode->i_uid = 0;
1853 inode->i_gid = 0;
1854 }
1855 inode->i_mode &= ~(S_ISUID | S_ISGID);
1856 security_task_to_inode(task, inode);
1857 put_task_struct(task);
1858 return 1;
1859 }
1860 rcu_read_unlock();
1861 put_files_struct(files);
1862 }
1863 put_task_struct(task);
1864 }
1865 d_drop(dentry);
1866 return 0;
1867}
1868
Al Virod72f71e2009-02-20 05:58:47 +00001869static const struct dentry_operations tid_fd_dentry_operations =
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001870{
1871 .d_revalidate = tid_fd_revalidate,
1872 .d_delete = pid_delete_dentry,
1873};
1874
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001875static struct dentry *proc_fd_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07001876 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001877{
Eric Dumazetc5141e62007-05-08 00:26:15 -07001878 unsigned fd = *(const unsigned *)ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001879 struct file *file;
1880 struct files_struct *files;
1881 struct inode *inode;
1882 struct proc_inode *ei;
1883 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001884
Eric W. Biederman61a28782006-10-02 02:18:49 -07001885 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001886 if (!inode)
1887 goto out;
1888 ei = PROC_I(inode);
1889 ei->fd = fd;
1890 files = get_files_struct(task);
1891 if (!files)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001892 goto out_iput;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001893 inode->i_mode = S_IFLNK;
1894
1895 /*
1896 * We are not taking a ref to the file structure, so we must
1897 * hold ->file_lock.
1898 */
1899 spin_lock(&files->file_lock);
1900 file = fcheck_files(files, fd);
1901 if (!file)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001902 goto out_unlock;
Al Viroaeb5d722008-09-02 15:28:45 -04001903 if (file->f_mode & FMODE_READ)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001904 inode->i_mode |= S_IRUSR | S_IXUSR;
Al Viroaeb5d722008-09-02 15:28:45 -04001905 if (file->f_mode & FMODE_WRITE)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001906 inode->i_mode |= S_IWUSR | S_IXUSR;
1907 spin_unlock(&files->file_lock);
1908 put_files_struct(files);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001909
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001910 inode->i_op = &proc_pid_link_inode_operations;
1911 inode->i_size = 64;
1912 ei->op.proc_get_link = proc_fd_link;
1913 dentry->d_op = &tid_fd_dentry_operations;
1914 d_add(dentry, inode);
1915 /* Close the race of the process dying before we return the dentry */
1916 if (tid_fd_revalidate(dentry, NULL))
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001917 error = NULL;
1918
1919 out:
1920 return error;
1921out_unlock:
1922 spin_unlock(&files->file_lock);
1923 put_files_struct(files);
1924out_iput:
1925 iput(inode);
1926 goto out;
1927}
1928
Miklos Szeredi27932742007-05-08 00:26:17 -07001929static struct dentry *proc_lookupfd_common(struct inode *dir,
1930 struct dentry *dentry,
1931 instantiate_t instantiate)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07001932{
1933 struct task_struct *task = get_proc_task(dir);
1934 unsigned fd = name_to_int(dentry);
1935 struct dentry *result = ERR_PTR(-ENOENT);
1936
1937 if (!task)
1938 goto out_no_task;
1939 if (fd == ~0U)
1940 goto out;
1941
Miklos Szeredi27932742007-05-08 00:26:17 -07001942 result = instantiate(dir, dentry, task, &fd);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001943out:
1944 put_task_struct(task);
1945out_no_task:
1946 return result;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07001947}
1948
Miklos Szeredi27932742007-05-08 00:26:17 -07001949static int proc_readfd_common(struct file * filp, void * dirent,
1950 filldir_t filldir, instantiate_t instantiate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08001952 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman56347082006-06-26 00:25:40 -07001953 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001954 struct task_struct *p = get_proc_task(inode);
Pavel Emelyanov457c2512007-10-18 23:40:43 -07001955 unsigned int fd, ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct files_struct * files;
1958
1959 retval = -ENOENT;
Eric W. Biederman99f89552006-06-26 00:25:55 -07001960 if (!p)
1961 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 fd = filp->f_pos;
1965 switch (fd) {
1966 case 0:
1967 if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
1968 goto out;
1969 filp->f_pos++;
1970 case 1:
Eric W. Biederman56347082006-06-26 00:25:40 -07001971 ino = parent_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (filldir(dirent, "..", 2, 1, ino, DT_DIR) < 0)
1973 goto out;
1974 filp->f_pos++;
1975 default:
1976 files = get_files_struct(p);
1977 if (!files)
1978 goto out;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001979 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 for (fd = filp->f_pos-2;
Al Viro9b4f5262008-04-22 01:32:44 -04001981 fd < files_fdtable(files)->max_fds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 fd++, filp->f_pos++) {
Miklos Szeredi27932742007-05-08 00:26:17 -07001983 char name[PROC_NUMBUF];
1984 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 if (!fcheck_files(files, fd))
1987 continue;
Dipankar Sarmab8359962005-09-09 13:04:14 -07001988 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Miklos Szeredi27932742007-05-08 00:26:17 -07001990 len = snprintf(name, sizeof(name), "%d", fd);
1991 if (proc_fill_cache(filp, dirent, filldir,
1992 name, len, instantiate,
1993 p, &fd) < 0) {
Dipankar Sarmab8359962005-09-09 13:04:14 -07001994 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001997 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
Dipankar Sarmab8359962005-09-09 13:04:14 -07001999 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 put_files_struct(files);
2001 }
2002out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002003 put_task_struct(p);
2004out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return retval;
2006}
2007
Miklos Szeredi27932742007-05-08 00:26:17 -07002008static struct dentry *proc_lookupfd(struct inode *dir, struct dentry *dentry,
2009 struct nameidata *nd)
2010{
2011 return proc_lookupfd_common(dir, dentry, proc_fd_instantiate);
2012}
2013
2014static int proc_readfd(struct file *filp, void *dirent, filldir_t filldir)
2015{
2016 return proc_readfd_common(filp, dirent, filldir, proc_fd_instantiate);
2017}
2018
2019static ssize_t proc_fdinfo_read(struct file *file, char __user *buf,
2020 size_t len, loff_t *ppos)
2021{
2022 char tmp[PROC_FDINFO_MAX];
Jan Blunck3dcd25f2008-02-14 19:38:35 -08002023 int err = proc_fd_info(file->f_path.dentry->d_inode, NULL, tmp);
Miklos Szeredi27932742007-05-08 00:26:17 -07002024 if (!err)
2025 err = simple_read_from_buffer(buf, len, ppos, tmp, strlen(tmp));
2026 return err;
2027}
2028
2029static const struct file_operations proc_fdinfo_file_operations = {
Arnd Bergmann87df8422010-03-17 23:06:02 +01002030 .open = nonseekable_open,
Miklos Szeredi27932742007-05-08 00:26:17 -07002031 .read = proc_fdinfo_read,
2032};
2033
Arjan van de Ven00977a52007-02-12 00:55:34 -08002034static const struct file_operations proc_fd_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 .read = generic_read_dir,
2036 .readdir = proc_readfd,
2037};
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/*
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002040 * /proc/pid/fd needs a special permission handler so that a process can still
2041 * access /proc/self/fd after it has executed a setuid().
2042 */
Al Viroe6305c42008-07-15 21:03:57 -04002043static int proc_fd_permission(struct inode *inode, int mask)
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002044{
2045 int rv;
2046
2047 rv = generic_permission(inode, mask, NULL);
2048 if (rv == 0)
2049 return 0;
2050 if (task_pid(current) == proc_pid(inode))
2051 rv = 0;
2052 return rv;
2053}
2054
2055/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 * proc directories can do almost nothing..
2057 */
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002058static const struct inode_operations proc_fd_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 .lookup = proc_lookupfd,
Alexey Dobriyan8948e112007-05-08 00:23:35 -07002060 .permission = proc_fd_permission,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002061 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062};
2063
Miklos Szeredi27932742007-05-08 00:26:17 -07002064static struct dentry *proc_fdinfo_instantiate(struct inode *dir,
2065 struct dentry *dentry, struct task_struct *task, const void *ptr)
2066{
2067 unsigned fd = *(unsigned *)ptr;
2068 struct inode *inode;
2069 struct proc_inode *ei;
2070 struct dentry *error = ERR_PTR(-ENOENT);
2071
2072 inode = proc_pid_make_inode(dir->i_sb, task);
2073 if (!inode)
2074 goto out;
2075 ei = PROC_I(inode);
2076 ei->fd = fd;
2077 inode->i_mode = S_IFREG | S_IRUSR;
2078 inode->i_fop = &proc_fdinfo_file_operations;
2079 dentry->d_op = &tid_fd_dentry_operations;
2080 d_add(dentry, inode);
2081 /* Close the race of the process dying before we return the dentry */
2082 if (tid_fd_revalidate(dentry, NULL))
2083 error = NULL;
2084
2085 out:
2086 return error;
2087}
2088
2089static struct dentry *proc_lookupfdinfo(struct inode *dir,
2090 struct dentry *dentry,
2091 struct nameidata *nd)
2092{
2093 return proc_lookupfd_common(dir, dentry, proc_fdinfo_instantiate);
2094}
2095
2096static int proc_readfdinfo(struct file *filp, void *dirent, filldir_t filldir)
2097{
2098 return proc_readfd_common(filp, dirent, filldir,
2099 proc_fdinfo_instantiate);
2100}
2101
2102static const struct file_operations proc_fdinfo_operations = {
2103 .read = generic_read_dir,
2104 .readdir = proc_readfdinfo,
2105};
2106
2107/*
2108 * proc directories can do almost nothing..
2109 */
2110static const struct inode_operations proc_fdinfo_inode_operations = {
2111 .lookup = proc_lookupfdinfo,
2112 .setattr = proc_setattr,
2113};
2114
2115
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002116static struct dentry *proc_pident_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002117 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002118{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002119 const struct pid_entry *p = ptr;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002120 struct inode *inode;
2121 struct proc_inode *ei;
KOSAKI Motohirobd6daba2009-05-28 14:34:21 -07002122 struct dentry *error = ERR_PTR(-ENOENT);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002123
Eric W. Biederman61a28782006-10-02 02:18:49 -07002124 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002125 if (!inode)
2126 goto out;
2127
2128 ei = PROC_I(inode);
2129 inode->i_mode = p->mode;
2130 if (S_ISDIR(inode->i_mode))
2131 inode->i_nlink = 2; /* Use getattr to fix if necessary */
2132 if (p->iop)
2133 inode->i_op = p->iop;
2134 if (p->fop)
2135 inode->i_fop = p->fop;
2136 ei->op = p->op;
2137 dentry->d_op = &pid_dentry_operations;
2138 d_add(dentry, inode);
2139 /* Close the race of the process dying before we return the dentry */
2140 if (pid_revalidate(dentry, NULL))
2141 error = NULL;
2142out:
2143 return error;
2144}
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146static struct dentry *proc_pident_lookup(struct inode *dir,
2147 struct dentry *dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002148 const struct pid_entry *ents,
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002149 unsigned int nents)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002151 struct dentry *error;
Eric W. Biederman99f89552006-06-26 00:25:55 -07002152 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002153 const struct pid_entry *p, *last;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002155 error = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Eric W. Biederman99f89552006-06-26 00:25:55 -07002157 if (!task)
2158 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002160 /*
2161 * Yes, it does not scale. And it should not. Don't add
2162 * new entries into /proc/<tgid>/ without very good reasons.
2163 */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002164 last = &ents[nents - 1];
2165 for (p = ents; p <= last; p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (p->len != dentry->d_name.len)
2167 continue;
2168 if (!memcmp(dentry->d_name.name, p->name, p->len))
2169 break;
2170 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002171 if (p > last)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto out;
2173
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002174 error = proc_pident_instantiate(dir, dentry, task, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07002176 put_task_struct(task);
2177out_no_task:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002178 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Eric Dumazetc5141e62007-05-08 00:26:15 -07002181static int proc_pident_fill_cache(struct file *filp, void *dirent,
2182 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002183{
2184 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2185 proc_pident_instantiate, task, p);
2186}
2187
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002188static int proc_pident_readdir(struct file *filp,
2189 void *dirent, filldir_t filldir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002190 const struct pid_entry *ents, unsigned int nents)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002191{
2192 int i;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002193 struct dentry *dentry = filp->f_path.dentry;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002194 struct inode *inode = dentry->d_inode;
2195 struct task_struct *task = get_proc_task(inode);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002196 const struct pid_entry *p, *last;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002197 ino_t ino;
2198 int ret;
2199
2200 ret = -ENOENT;
2201 if (!task)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002202 goto out_no_task;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002203
2204 ret = 0;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002205 i = filp->f_pos;
2206 switch (i) {
2207 case 0:
2208 ino = inode->i_ino;
2209 if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
2210 goto out;
2211 i++;
2212 filp->f_pos++;
2213 /* fall through */
2214 case 1:
2215 ino = parent_ino(dentry);
2216 if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
2217 goto out;
2218 i++;
2219 filp->f_pos++;
2220 /* fall through */
2221 default:
2222 i -= 2;
2223 if (i >= nents) {
2224 ret = 1;
2225 goto out;
2226 }
2227 p = ents + i;
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002228 last = &ents[nents - 1];
2229 while (p <= last) {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002230 if (proc_pident_fill_cache(filp, dirent, filldir, task, p) < 0)
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002231 goto out;
2232 filp->f_pos++;
2233 p++;
2234 }
2235 }
2236
2237 ret = 1;
2238out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002239 put_task_struct(task);
2240out_no_task:
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244#ifdef CONFIG_SECURITY
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002245static ssize_t proc_pid_attr_read(struct file * file, char __user * buf,
2246 size_t count, loff_t *ppos)
2247{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002248 struct inode * inode = file->f_path.dentry->d_inode;
Al Viro04ff9702007-03-12 16:17:58 +00002249 char *p = NULL;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002250 ssize_t length;
2251 struct task_struct *task = get_proc_task(inode);
2252
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002253 if (!task)
Al Viro04ff9702007-03-12 16:17:58 +00002254 return -ESRCH;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002255
2256 length = security_getprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002257 (char*)file->f_path.dentry->d_name.name,
Al Viro04ff9702007-03-12 16:17:58 +00002258 &p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002259 put_task_struct(task);
Al Viro04ff9702007-03-12 16:17:58 +00002260 if (length > 0)
2261 length = simple_read_from_buffer(buf, count, ppos, p, length);
2262 kfree(p);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002263 return length;
2264}
2265
2266static ssize_t proc_pid_attr_write(struct file * file, const char __user * buf,
2267 size_t count, loff_t *ppos)
2268{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002269 struct inode * inode = file->f_path.dentry->d_inode;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002270 char *page;
2271 ssize_t length;
2272 struct task_struct *task = get_proc_task(inode);
2273
2274 length = -ESRCH;
2275 if (!task)
2276 goto out_no_task;
2277 if (count > PAGE_SIZE)
2278 count = PAGE_SIZE;
2279
2280 /* No partial writes. */
2281 length = -EINVAL;
2282 if (*ppos != 0)
2283 goto out;
2284
2285 length = -ENOMEM;
Mel Gormane12ba742007-10-16 01:25:52 -07002286 page = (char*)__get_free_page(GFP_TEMPORARY);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002287 if (!page)
2288 goto out;
2289
2290 length = -EFAULT;
2291 if (copy_from_user(page, buf, count))
2292 goto out_free;
2293
David Howells107db7c2009-05-08 13:55:27 +01002294 /* Guard against adverse ptrace interaction */
2295 length = mutex_lock_interruptible(&task->cred_guard_mutex);
2296 if (length < 0)
2297 goto out_free;
2298
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002299 length = security_setprocattr(task,
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002300 (char*)file->f_path.dentry->d_name.name,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002301 (void*)page, count);
David Howells107db7c2009-05-08 13:55:27 +01002302 mutex_unlock(&task->cred_guard_mutex);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002303out_free:
2304 free_page((unsigned long) page);
2305out:
2306 put_task_struct(task);
2307out_no_task:
2308 return length;
2309}
2310
Arjan van de Ven00977a52007-02-12 00:55:34 -08002311static const struct file_operations proc_pid_attr_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002312 .read = proc_pid_attr_read,
2313 .write = proc_pid_attr_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002314 .llseek = generic_file_llseek,
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002315};
2316
Eric Dumazetc5141e62007-05-08 00:26:15 -07002317static const struct pid_entry attr_dir_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002318 REG("current", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2319 REG("prev", S_IRUGO, proc_pid_attr_operations),
2320 REG("exec", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2321 REG("fscreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2322 REG("keycreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
2323 REG("sockcreate", S_IRUGO|S_IWUGO, proc_pid_attr_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002324};
2325
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002326static int proc_attr_dir_readdir(struct file * filp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 void * dirent, filldir_t filldir)
2328{
2329 return proc_pident_readdir(filp,dirent,filldir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002330 attr_dir_stuff,ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
Arjan van de Ven00977a52007-02-12 00:55:34 -08002333static const struct file_operations proc_attr_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 .read = generic_read_dir,
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002335 .readdir = proc_attr_dir_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336};
2337
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002338static struct dentry *proc_attr_dir_lookup(struct inode *dir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 struct dentry *dentry, struct nameidata *nd)
2340{
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002341 return proc_pident_lookup(dir, dentry,
2342 attr_dir_stuff, ARRAY_SIZE(attr_dir_stuff));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002345static const struct inode_operations proc_attr_dir_inode_operations = {
Eric W. Biederman72d9dcf2006-10-02 02:18:50 -07002346 .lookup = proc_attr_dir_lookup,
Eric W. Biederman99f89552006-06-26 00:25:55 -07002347 .getattr = pid_getattr,
Linus Torvalds6d76fa52006-07-15 12:26:45 -07002348 .setattr = proc_setattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349};
2350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351#endif
2352
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002353#ifdef CONFIG_ELF_CORE
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002354static ssize_t proc_coredump_filter_read(struct file *file, char __user *buf,
2355 size_t count, loff_t *ppos)
2356{
2357 struct task_struct *task = get_proc_task(file->f_dentry->d_inode);
2358 struct mm_struct *mm;
2359 char buffer[PROC_NUMBUF];
2360 size_t len;
2361 int ret;
2362
2363 if (!task)
2364 return -ESRCH;
2365
2366 ret = 0;
2367 mm = get_task_mm(task);
2368 if (mm) {
2369 len = snprintf(buffer, sizeof(buffer), "%08lx\n",
2370 ((mm->flags & MMF_DUMP_FILTER_MASK) >>
2371 MMF_DUMP_FILTER_SHIFT));
2372 mmput(mm);
2373 ret = simple_read_from_buffer(buf, count, ppos, buffer, len);
2374 }
2375
2376 put_task_struct(task);
2377
2378 return ret;
2379}
2380
2381static ssize_t proc_coredump_filter_write(struct file *file,
2382 const char __user *buf,
2383 size_t count,
2384 loff_t *ppos)
2385{
2386 struct task_struct *task;
2387 struct mm_struct *mm;
2388 char buffer[PROC_NUMBUF], *end;
2389 unsigned int val;
2390 int ret;
2391 int i;
2392 unsigned long mask;
2393
2394 ret = -EFAULT;
2395 memset(buffer, 0, sizeof(buffer));
2396 if (count > sizeof(buffer) - 1)
2397 count = sizeof(buffer) - 1;
2398 if (copy_from_user(buffer, buf, count))
2399 goto out_no_task;
2400
2401 ret = -EINVAL;
2402 val = (unsigned int)simple_strtoul(buffer, &end, 0);
2403 if (*end == '\n')
2404 end++;
2405 if (end - buffer == 0)
2406 goto out_no_task;
2407
2408 ret = -ESRCH;
2409 task = get_proc_task(file->f_dentry->d_inode);
2410 if (!task)
2411 goto out_no_task;
2412
2413 ret = end - buffer;
2414 mm = get_task_mm(task);
2415 if (!mm)
2416 goto out_no_mm;
2417
2418 for (i = 0, mask = 1; i < MMF_DUMP_FILTER_BITS; i++, mask <<= 1) {
2419 if (val & mask)
2420 set_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2421 else
2422 clear_bit(i + MMF_DUMP_FILTER_SHIFT, &mm->flags);
2423 }
2424
2425 mmput(mm);
2426 out_no_mm:
2427 put_task_struct(task);
2428 out_no_task:
2429 return ret;
2430}
2431
2432static const struct file_operations proc_coredump_filter_operations = {
2433 .read = proc_coredump_filter_read,
2434 .write = proc_coredump_filter_write,
Arnd Bergmann87df8422010-03-17 23:06:02 +01002435 .llseek = generic_file_llseek,
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002436};
2437#endif
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439/*
2440 * /proc/self:
2441 */
2442static int proc_self_readlink(struct dentry *dentry, char __user *buffer,
2443 int buflen)
2444{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002445 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002446 pid_t tgid = task_tgid_nr_ns(current, ns);
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002447 char tmp[PROC_NUMBUF];
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002448 if (!tgid)
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002449 return -ENOENT;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002450 sprintf(tmp, "%d", tgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return vfs_readlink(dentry,buffer,buflen,tmp);
2452}
2453
Al Viro008b1502005-08-20 00:17:39 +01002454static void *proc_self_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Eric W. Biederman488e5bc2008-02-08 04:18:34 -08002456 struct pid_namespace *ns = dentry->d_sb->s_fs_info;
Andrew Mortonb55fcb22008-02-08 15:00:43 -08002457 pid_t tgid = task_tgid_nr_ns(current, ns);
Al Viro7fee4862010-01-14 01:03:28 -05002458 char *name = ERR_PTR(-ENOENT);
2459 if (tgid) {
2460 name = __getname();
2461 if (!name)
2462 name = ERR_PTR(-ENOMEM);
2463 else
2464 sprintf(name, "%d", tgid);
2465 }
2466 nd_set_link(nd, name);
2467 return NULL;
2468}
2469
2470static void proc_self_put_link(struct dentry *dentry, struct nameidata *nd,
2471 void *cookie)
2472{
2473 char *s = nd_get_link(nd);
2474 if (!IS_ERR(s))
2475 __putname(s);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002478static const struct inode_operations proc_self_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 .readlink = proc_self_readlink,
2480 .follow_link = proc_self_follow_link,
Al Viro7fee4862010-01-14 01:03:28 -05002481 .put_link = proc_self_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482};
2483
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002484/*
Eric W. Biederman801199c2006-10-02 02:18:48 -07002485 * proc base
2486 *
2487 * These are the directory entries in the root directory of /proc
2488 * that properly belong to the /proc filesystem, as they describe
2489 * describe something that is process related.
2490 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002491static const struct pid_entry proc_base_stuff[] = {
Eric W. Biederman61a28782006-10-02 02:18:49 -07002492 NOD("self", S_IFLNK|S_IRWXUGO,
Eric W. Biederman801199c2006-10-02 02:18:48 -07002493 &proc_self_inode_operations, NULL, {}),
Eric W. Biederman801199c2006-10-02 02:18:48 -07002494};
2495
2496/*
2497 * Exceptional case: normally we are not allowed to unhash a busy
2498 * directory. In this case, however, we can do it - no aliasing problems
2499 * due to the way we treat inodes.
2500 */
2501static int proc_base_revalidate(struct dentry *dentry, struct nameidata *nd)
2502{
2503 struct inode *inode = dentry->d_inode;
2504 struct task_struct *task = get_proc_task(inode);
2505 if (task) {
2506 put_task_struct(task);
2507 return 1;
2508 }
2509 d_drop(dentry);
2510 return 0;
2511}
2512
Al Virod72f71e2009-02-20 05:58:47 +00002513static const struct dentry_operations proc_base_dentry_operations =
Eric W. Biederman801199c2006-10-02 02:18:48 -07002514{
2515 .d_revalidate = proc_base_revalidate,
2516 .d_delete = pid_delete_dentry,
2517};
2518
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002519static struct dentry *proc_base_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002520 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002521{
Eric Dumazetc5141e62007-05-08 00:26:15 -07002522 const struct pid_entry *p = ptr;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002523 struct inode *inode;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002524 struct proc_inode *ei;
Dan Carpenter73d36462010-05-26 14:43:25 -07002525 struct dentry *error;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002526
2527 /* Allocate the inode */
2528 error = ERR_PTR(-ENOMEM);
2529 inode = new_inode(dir->i_sb);
2530 if (!inode)
2531 goto out;
2532
2533 /* Initialize the inode */
2534 ei = PROC_I(inode);
2535 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Eric W. Biederman801199c2006-10-02 02:18:48 -07002536
2537 /*
2538 * grab the reference to the task.
2539 */
Oleg Nesterov1a657f782006-10-02 02:18:59 -07002540 ei->pid = get_task_pid(task, PIDTYPE_PID);
Eric W. Biederman801199c2006-10-02 02:18:48 -07002541 if (!ei->pid)
2542 goto out_iput;
2543
Eric W. Biederman801199c2006-10-02 02:18:48 -07002544 inode->i_mode = p->mode;
2545 if (S_ISDIR(inode->i_mode))
2546 inode->i_nlink = 2;
2547 if (S_ISLNK(inode->i_mode))
2548 inode->i_size = 64;
2549 if (p->iop)
2550 inode->i_op = p->iop;
2551 if (p->fop)
2552 inode->i_fop = p->fop;
2553 ei->op = p->op;
2554 dentry->d_op = &proc_base_dentry_operations;
2555 d_add(dentry, inode);
2556 error = NULL;
2557out:
Eric W. Biederman801199c2006-10-02 02:18:48 -07002558 return error;
2559out_iput:
2560 iput(inode);
2561 goto out;
2562}
2563
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002564static struct dentry *proc_base_lookup(struct inode *dir, struct dentry *dentry)
2565{
2566 struct dentry *error;
2567 struct task_struct *task = get_proc_task(dir);
Eric Dumazetc5141e62007-05-08 00:26:15 -07002568 const struct pid_entry *p, *last;
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002569
2570 error = ERR_PTR(-ENOENT);
2571
2572 if (!task)
2573 goto out_no_task;
2574
2575 /* Lookup the directory entry */
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002576 last = &proc_base_stuff[ARRAY_SIZE(proc_base_stuff) - 1];
2577 for (p = proc_base_stuff; p <= last; p++) {
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002578 if (p->len != dentry->d_name.len)
2579 continue;
2580 if (!memcmp(dentry->d_name.name, p->name, p->len))
2581 break;
2582 }
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002583 if (p > last)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002584 goto out;
2585
2586 error = proc_base_instantiate(dir, dentry, task, p);
2587
2588out:
2589 put_task_struct(task);
2590out_no_task:
2591 return error;
2592}
2593
Eric Dumazetc5141e62007-05-08 00:26:15 -07002594static int proc_base_fill_cache(struct file *filp, void *dirent,
2595 filldir_t filldir, struct task_struct *task, const struct pid_entry *p)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002596{
2597 return proc_fill_cache(filp, dirent, filldir, p->name, p->len,
2598 proc_base_instantiate, task, p);
2599}
2600
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002601#ifdef CONFIG_TASK_IO_ACCOUNTING
Andrea Righi297c5d92008-07-25 01:48:49 -07002602static int do_io_accounting(struct task_struct *task, char *buffer, int whole)
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002603{
Andrea Righi940389b2008-07-28 00:48:12 +02002604 struct task_io_accounting acct = task->ioac;
Andrea Righi59954772008-07-27 17:29:15 +02002605 unsigned long flags;
Andrea Righi297c5d92008-07-25 01:48:49 -07002606
Andrea Righi59954772008-07-27 17:29:15 +02002607 if (whole && lock_task_sighand(task, &flags)) {
2608 struct task_struct *t = task;
Andrea Righib2d002d2008-07-26 15:22:27 -07002609
Andrea Righi59954772008-07-27 17:29:15 +02002610 task_io_accounting_add(&acct, &task->signal->ioac);
2611 while_each_thread(task, t)
2612 task_io_accounting_add(&acct, &t->ioac);
Andrea Righi297c5d92008-07-25 01:48:49 -07002613
Andrea Righi59954772008-07-27 17:29:15 +02002614 unlock_task_sighand(task, &flags);
Andrea Righi297c5d92008-07-25 01:48:49 -07002615 }
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002616 return sprintf(buffer,
2617 "rchar: %llu\n"
2618 "wchar: %llu\n"
2619 "syscr: %llu\n"
2620 "syscw: %llu\n"
2621 "read_bytes: %llu\n"
2622 "write_bytes: %llu\n"
2623 "cancelled_write_bytes: %llu\n",
Alexander Beregalov7c443192008-08-05 13:01:34 -07002624 (unsigned long long)acct.rchar,
2625 (unsigned long long)acct.wchar,
2626 (unsigned long long)acct.syscr,
2627 (unsigned long long)acct.syscw,
2628 (unsigned long long)acct.read_bytes,
2629 (unsigned long long)acct.write_bytes,
2630 (unsigned long long)acct.cancelled_write_bytes);
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002631}
Andrea Righi297c5d92008-07-25 01:48:49 -07002632
2633static int proc_tid_io_accounting(struct task_struct *task, char *buffer)
2634{
2635 return do_io_accounting(task, buffer, 0);
2636}
2637
2638static int proc_tgid_io_accounting(struct task_struct *task, char *buffer)
2639{
2640 return do_io_accounting(task, buffer, 1);
2641}
2642#endif /* CONFIG_TASK_IO_ACCOUNTING */
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002643
Kees Cook47830722008-10-06 03:11:58 +04002644static int proc_pid_personality(struct seq_file *m, struct pid_namespace *ns,
2645 struct pid *pid, struct task_struct *task)
2646{
2647 seq_printf(m, "%08x\n", task->personality);
2648 return 0;
2649}
2650
Eric W. Biederman801199c2006-10-02 02:18:48 -07002651/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002652 * Thread groups
2653 */
Arjan van de Ven00977a52007-02-12 00:55:34 -08002654static const struct file_operations proc_task_operations;
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002655static const struct inode_operations proc_task_inode_operations;
Eric W. Biederman20cdc892006-10-02 02:17:07 -07002656
Eric Dumazetc5141e62007-05-08 00:26:15 -07002657static const struct pid_entry tgid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002658 DIR("task", S_IRUGO|S_IXUGO, proc_task_inode_operations, proc_task_operations),
2659 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
2660 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002661#ifdef CONFIG_NET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002662 DIR("net", S_IRUGO|S_IXUGO, proc_net_inode_operations, proc_net_operations),
Andrew Mortonb2211a32008-03-11 18:03:35 -07002663#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002664 REG("environ", S_IRUSR, proc_environ_operations),
2665 INF("auxv", S_IRUSR, proc_pid_auxv),
2666 ONE("status", S_IRUGO, proc_pid_status),
2667 ONE("personality", S_IRUSR, proc_pid_personality),
2668 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002669#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002670 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02002671#endif
john stultz4614a696b2009-12-14 18:00:05 -08002672 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07002673#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002674 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07002675#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002676 INF("cmdline", S_IRUGO, proc_pid_cmdline),
2677 ONE("stat", S_IRUGO, proc_tgid_stat),
2678 ONE("statm", S_IRUGO, proc_pid_statm),
2679 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002680#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002681 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002682#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002683 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
2684 LNK("cwd", proc_cwd_link),
2685 LNK("root", proc_root_link),
2686 LNK("exe", proc_exe_link),
2687 REG("mounts", S_IRUGO, proc_mounts_operations),
2688 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
2689 REG("mountstats", S_IRUSR, proc_mountstats_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08002690#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002691 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
2692 REG("smaps", S_IRUGO, proc_smaps_operations),
2693 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002694#endif
2695#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002696 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002697#endif
2698#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002699 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002700#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03002701#ifdef CONFIG_STACKTRACE
2702 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002703#endif
2704#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002705 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002706#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01002707#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002708 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01002709#endif
Paul Menage8793d852007-10-18 23:39:39 -07002710#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002711 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002712#endif
Paul Menagea4243162007-10-18 23:39:35 -07002713#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002714 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07002715#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002716 INF("oom_score", S_IRUGO, proc_oom_score),
2717 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07002718 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002719#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002720 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
2721 REG("sessionid", S_IRUGO, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002722#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002723#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002724 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08002725#endif
Christoph Hellwig698ba7b2009-12-15 16:47:37 -08002726#ifdef CONFIG_ELF_CORE
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002727 REG("coredump_filter", S_IRUGO|S_IWUSR, proc_coredump_filter_operations),
Kawai, Hidehiro3cb4a0b2007-07-19 01:48:28 -07002728#endif
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002729#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002730 INF("io", S_IRUGO, proc_tgid_io_accounting),
Andrew Mortonaba76fd2006-12-10 02:19:48 -08002731#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002732};
2733
2734static int proc_tgid_base_readdir(struct file * filp,
2735 void * dirent, filldir_t filldir)
2736{
2737 return proc_pident_readdir(filp,dirent,filldir,
2738 tgid_base_stuff,ARRAY_SIZE(tgid_base_stuff));
2739}
2740
Arjan van de Ven00977a52007-02-12 00:55:34 -08002741static const struct file_operations proc_tgid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002742 .read = generic_read_dir,
2743 .readdir = proc_tgid_base_readdir,
2744};
2745
2746static struct dentry *proc_tgid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002747 return proc_pident_lookup(dir, dentry,
2748 tgid_base_stuff, ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002749}
2750
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08002751static const struct inode_operations proc_tgid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002752 .lookup = proc_tgid_base_lookup,
2753 .getattr = pid_getattr,
2754 .setattr = proc_setattr,
2755};
2756
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002757static void proc_flush_task_mnt(struct vfsmount *mnt, pid_t pid, pid_t tgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Eric W. Biederman48e64842006-06-26 00:25:48 -07002759 struct dentry *dentry, *leader, *dir;
Eric W. Biederman8578cea2006-06-26 00:25:54 -07002760 char buf[PROC_NUMBUF];
Eric W. Biederman48e64842006-06-26 00:25:48 -07002761 struct qstr name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
Eric W. Biederman48e64842006-06-26 00:25:48 -07002763 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002764 name.len = snprintf(buf, sizeof(buf), "%d", pid);
2765 dentry = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002766 if (dentry) {
Sukadev Bhattiprolu29f12ca2009-11-11 14:26:32 -08002767 shrink_dcache_parent(dentry);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002768 d_drop(dentry);
2769 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Eric W. Biederman48e64842006-06-26 00:25:48 -07002772 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002773 name.len = snprintf(buf, sizeof(buf), "%d", tgid);
2774 leader = d_hash_and_lookup(mnt->mnt_root, &name);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002775 if (!leader)
2776 goto out;
2777
2778 name.name = "task";
2779 name.len = strlen(name.name);
2780 dir = d_hash_and_lookup(leader, &name);
2781 if (!dir)
2782 goto out_put_leader;
2783
2784 name.name = buf;
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002785 name.len = snprintf(buf, sizeof(buf), "%d", pid);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002786 dentry = d_hash_and_lookup(dir, &name);
2787 if (dentry) {
2788 shrink_dcache_parent(dentry);
2789 d_drop(dentry);
2790 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
Eric W. Biederman48e64842006-06-26 00:25:48 -07002792
2793 dput(dir);
2794out_put_leader:
2795 dput(leader);
2796out:
2797 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
Randy Dunlap0895e912007-10-21 21:00:10 -07002800/**
2801 * proc_flush_task - Remove dcache entries for @task from the /proc dcache.
2802 * @task: task that should be flushed.
2803 *
2804 * When flushing dentries from proc, one needs to flush them from global
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002805 * proc (proc_mnt) and from all the namespaces' procs this task was seen
Randy Dunlap0895e912007-10-21 21:00:10 -07002806 * in. This call is supposed to do all of this job.
2807 *
2808 * Looks in the dcache for
2809 * /proc/@pid
2810 * /proc/@tgid/task/@pid
2811 * if either directory is present flushes it and all of it'ts children
2812 * from the dcache.
2813 *
2814 * It is safe and reasonable to cache /proc entries for a task until
2815 * that task exits. After that they just clog up the dcache with
2816 * useless entries, possibly causing useful dcache entries to be
2817 * flushed instead. This routine is proved to flush those useless
2818 * dcache entries at process exit time.
2819 *
2820 * NOTE: This routine is just an optimization so it does not guarantee
2821 * that no dcache entries will exist at process exit time it
2822 * just makes it very unlikely that any will persist.
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002823 */
2824
2825void proc_flush_task(struct task_struct *task)
2826{
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002827 int i;
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002828 struct pid *pid, *tgid;
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002829 struct upid *upid;
2830
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002831 pid = task_pid(task);
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002832 tgid = task_tgid(task);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002833
Eric W. Biederman9fcc2d12007-11-14 17:00:07 -08002834 for (i = 0; i <= pid->level; i++) {
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002835 upid = &pid->numbers[i];
2836 proc_flush_task_mnt(upid->ns->proc_mnt, upid->nr,
Oleg Nesterov9b4d1cbe2009-09-22 16:45:34 -07002837 tgid->numbers[i].nr);
Pavel Emelyanov130f77e2007-10-18 23:40:11 -07002838 }
Pavel Emelyanov6f4e6432007-10-18 23:40:11 -07002839
2840 upid = &pid->numbers[pid->level];
2841 if (upid->nr == 1)
2842 pid_ns_release_proc(upid->ns);
Pavel Emelyanov60347f62007-10-18 23:40:03 -07002843}
2844
Adrian Bunk9711ef92006-12-06 20:38:31 -08002845static struct dentry *proc_pid_instantiate(struct inode *dir,
2846 struct dentry * dentry,
Eric Dumazetc5141e62007-05-08 00:26:15 -07002847 struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002848{
2849 struct dentry *error = ERR_PTR(-ENOENT);
2850 struct inode *inode;
2851
Eric W. Biederman61a28782006-10-02 02:18:49 -07002852 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002853 if (!inode)
2854 goto out;
2855
2856 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
2857 inode->i_op = &proc_tgid_base_inode_operations;
2858 inode->i_fop = &proc_tgid_base_operations;
2859 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07002860
2861 inode->i_nlink = 2 + pid_entry_count_dirs(tgid_base_stuff,
2862 ARRAY_SIZE(tgid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002863
2864 dentry->d_op = &pid_dentry_operations;
2865
2866 d_add(dentry, inode);
2867 /* Close the race of the process dying before we return the dentry */
2868 if (pid_revalidate(dentry, NULL))
2869 error = NULL;
2870out:
2871 return error;
2872}
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874struct dentry *proc_pid_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
2875{
Dan Carpenter73d36462010-05-26 14:43:25 -07002876 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 unsigned tgid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002879 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Eric W. Biederman801199c2006-10-02 02:18:48 -07002881 result = proc_base_lookup(dir, dentry);
2882 if (!IS_ERR(result) || PTR_ERR(result) != -ENOENT)
2883 goto out;
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 tgid = name_to_int(dentry);
2886 if (tgid == ~0U)
2887 goto out;
2888
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002889 ns = dentry->d_sb->s_fs_info;
Eric W. Biedermande758732006-06-26 00:25:51 -07002890 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002891 task = find_task_by_pid_ns(tgid, ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (task)
2893 get_task_struct(task);
Eric W. Biedermande758732006-06-26 00:25:51 -07002894 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 if (!task)
2896 goto out;
2897
Eric W. Biederman444ceed2006-10-02 02:18:49 -07002898 result = proc_pid_instantiate(dir, dentry, task, NULL);
Eric W. Biederman48e64842006-06-26 00:25:48 -07002899 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900out:
Eric W. Biedermancd6a3ce2006-06-26 00:25:49 -07002901 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904/*
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002905 * Find the first task with tgid >= tgid
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002906 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002908struct tgid_iter {
2909 unsigned int tgid;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002910 struct task_struct *task;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002911};
2912static struct tgid_iter next_tgid(struct pid_namespace *ns, struct tgid_iter iter)
2913{
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002914 struct pid *pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002916 if (iter.task)
2917 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002918 rcu_read_lock();
2919retry:
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002920 iter.task = NULL;
2921 pid = find_ge_pid(iter.tgid, ns);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002922 if (pid) {
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002923 iter.tgid = pid_nr_ns(pid, ns);
2924 iter.task = pid_task(pid, PIDTYPE_PID);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002925 /* What we to know is if the pid we have find is the
2926 * pid of a thread_group_leader. Testing for task
2927 * being a thread_group_leader is the obvious thing
2928 * todo but there is a window when it fails, due to
2929 * the pid transfer logic in de_thread.
2930 *
2931 * So we perform the straight forward test of seeing
2932 * if the pid we have found is the pid of a thread
2933 * group leader, and don't worry if the task we have
2934 * found doesn't happen to be a thread group leader.
2935 * As we don't care in the case of readdir.
2936 */
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002937 if (!iter.task || !has_group_leader_pid(iter.task)) {
2938 iter.tgid += 1;
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002939 goto retry;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002940 }
2941 get_task_struct(iter.task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
Eric W. Biederman454cc102006-06-26 00:25:51 -07002943 rcu_read_unlock();
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002944 return iter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002947#define TGID_OFFSET (FIRST_PROCESS_ENTRY + ARRAY_SIZE(proc_base_stuff))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Eric W. Biederman61a28782006-10-02 02:18:49 -07002949static int proc_pid_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002950 struct tgid_iter iter)
Eric W. Biederman61a28782006-10-02 02:18:49 -07002951{
2952 char name[PROC_NUMBUF];
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002953 int len = snprintf(name, sizeof(name), "%d", iter.tgid);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002954 return proc_fill_cache(filp, dirent, filldir, name, len,
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002955 proc_pid_instantiate, iter.task, NULL);
Eric W. Biederman61a28782006-10-02 02:18:49 -07002956}
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958/* for the /proc/ directory itself, after non-process stuff has been done */
2959int proc_pid_readdir(struct file * filp, void * dirent, filldir_t filldir)
2960{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 unsigned int nr = filp->f_pos - FIRST_PROCESS_ENTRY;
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08002962 struct task_struct *reaper = get_proc_task(filp->f_path.dentry->d_inode);
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002963 struct tgid_iter iter;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002964 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Eric W. Biederman61a28782006-10-02 02:18:49 -07002966 if (!reaper)
2967 goto out_no_task;
2968
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07002969 for (; nr < ARRAY_SIZE(proc_base_stuff); filp->f_pos++, nr++) {
Eric Dumazetc5141e62007-05-08 00:26:15 -07002970 const struct pid_entry *p = &proc_base_stuff[nr];
Eric W. Biederman61a28782006-10-02 02:18:49 -07002971 if (proc_base_fill_cache(filp, dirent, filldir, reaper, p) < 0)
Eric W. Biederman801199c2006-10-02 02:18:48 -07002972 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 }
2974
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002975 ns = filp->f_dentry->d_sb->s_fs_info;
Eric W. Biederman19fd4bb2007-11-28 16:21:26 -08002976 iter.task = NULL;
2977 iter.tgid = filp->f_pos - TGID_OFFSET;
2978 for (iter = next_tgid(ns, iter);
2979 iter.task;
2980 iter.tgid += 1, iter = next_tgid(ns, iter)) {
2981 filp->f_pos = iter.tgid + TGID_OFFSET;
2982 if (proc_pid_fill_cache(filp, dirent, filldir, iter) < 0) {
2983 put_task_struct(iter.task);
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002984 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
Eric W. Biederman0804ef42006-10-02 02:17:04 -07002987 filp->f_pos = PID_MAX_LIMIT + TGID_OFFSET;
2988out:
Eric W. Biederman61a28782006-10-02 02:18:49 -07002989 put_task_struct(reaper);
2990out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 return 0;
2992}
2993
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07002994/*
Eric W. Biederman28a6d672006-10-02 02:17:05 -07002995 * Tasks
2996 */
Eric Dumazetc5141e62007-05-08 00:26:15 -07002997static const struct pid_entry tid_base_stuff[] = {
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03002998 DIR("fd", S_IRUSR|S_IXUSR, proc_fd_inode_operations, proc_fd_operations),
Jerome Marchand38355412010-04-27 13:13:06 -07002999 DIR("fdinfo", S_IRUSR|S_IXUSR, proc_fdinfo_inode_operations, proc_fdinfo_operations),
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003000 REG("environ", S_IRUSR, proc_environ_operations),
3001 INF("auxv", S_IRUSR, proc_pid_auxv),
3002 ONE("status", S_IRUGO, proc_pid_status),
3003 ONE("personality", S_IRUSR, proc_pid_personality),
3004 INF("limits", S_IRUSR, proc_pid_limits),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003005#ifdef CONFIG_SCHED_DEBUG
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003006 REG("sched", S_IRUGO|S_IWUSR, proc_pid_sched_operations),
Ingo Molnar43ae34c2007-07-09 18:52:00 +02003007#endif
john stultz4614a696b2009-12-14 18:00:05 -08003008 REG("comm", S_IRUGO|S_IWUSR, proc_pid_set_comm_operations),
Roland McGrathebcb6732008-07-25 19:46:00 -07003009#ifdef CONFIG_HAVE_ARCH_TRACEHOOK
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003010 INF("syscall", S_IRUSR, proc_pid_syscall),
Roland McGrathebcb6732008-07-25 19:46:00 -07003011#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003012 INF("cmdline", S_IRUGO, proc_pid_cmdline),
3013 ONE("stat", S_IRUGO, proc_tid_stat),
3014 ONE("statm", S_IRUGO, proc_pid_statm),
3015 REG("maps", S_IRUGO, proc_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003016#ifdef CONFIG_NUMA
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003017 REG("numa_maps", S_IRUGO, proc_numa_maps_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003018#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003019 REG("mem", S_IRUSR|S_IWUSR, proc_mem_operations),
3020 LNK("cwd", proc_cwd_link),
3021 LNK("root", proc_root_link),
3022 LNK("exe", proc_exe_link),
3023 REG("mounts", S_IRUGO, proc_mounts_operations),
3024 REG("mountinfo", S_IRUGO, proc_mountinfo_operations),
Matt Mackall1e883282008-02-04 22:29:07 -08003025#ifdef CONFIG_PROC_PAGE_MONITOR
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003026 REG("clear_refs", S_IWUSR, proc_clear_refs_operations),
3027 REG("smaps", S_IRUGO, proc_smaps_operations),
3028 REG("pagemap", S_IRUSR, proc_pagemap_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003029#endif
3030#ifdef CONFIG_SECURITY
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003031 DIR("attr", S_IRUGO|S_IXUGO, proc_attr_dir_inode_operations, proc_attr_dir_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003032#endif
3033#ifdef CONFIG_KALLSYMS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003034 INF("wchan", S_IRUGO, proc_pid_wchan),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003035#endif
Ken Chen2ec220e2008-11-10 11:26:08 +03003036#ifdef CONFIG_STACKTRACE
3037 ONE("stack", S_IRUSR, proc_pid_stack),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003038#endif
3039#ifdef CONFIG_SCHEDSTATS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003040 INF("schedstat", S_IRUGO, proc_pid_schedstat),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003041#endif
Arjan van de Ven97455122008-01-25 21:08:34 +01003042#ifdef CONFIG_LATENCYTOP
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003043 REG("latency", S_IRUGO, proc_lstats_operations),
Arjan van de Ven97455122008-01-25 21:08:34 +01003044#endif
Paul Menage8793d852007-10-18 23:39:39 -07003045#ifdef CONFIG_PROC_PID_CPUSET
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003046 REG("cpuset", S_IRUGO, proc_cpuset_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003047#endif
Paul Menagea4243162007-10-18 23:39:35 -07003048#ifdef CONFIG_CGROUPS
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003049 REG("cgroup", S_IRUGO, proc_cgroup_operations),
Paul Menagea4243162007-10-18 23:39:35 -07003050#endif
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003051 INF("oom_score", S_IRUGO, proc_oom_score),
3052 REG("oom_adj", S_IRUGO|S_IWUSR, proc_oom_adjust_operations),
David Rientjesa63d83f2010-08-09 17:19:46 -07003053 REG("oom_score_adj", S_IRUGO|S_IWUSR, proc_oom_score_adj_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003054#ifdef CONFIG_AUDITSYSCALL
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003055 REG("loginuid", S_IWUSR|S_IRUGO, proc_loginuid_operations),
3056 REG("sessionid", S_IRUSR, proc_sessionid_operations),
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003057#endif
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003058#ifdef CONFIG_FAULT_INJECTION
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003059 REG("make-it-fail", S_IRUGO|S_IWUSR, proc_fault_inject_operations),
Akinobu Mitaf4f154f2006-12-08 02:39:47 -08003060#endif
Andrea Righi297c5d92008-07-25 01:48:49 -07003061#ifdef CONFIG_TASK_IO_ACCOUNTING
Alexey Dobriyan631f9c12008-11-10 01:32:52 +03003062 INF("io", S_IRUGO, proc_tid_io_accounting),
Andrea Righi297c5d92008-07-25 01:48:49 -07003063#endif
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003064};
3065
3066static int proc_tid_base_readdir(struct file * filp,
3067 void * dirent, filldir_t filldir)
3068{
3069 return proc_pident_readdir(filp,dirent,filldir,
3070 tid_base_stuff,ARRAY_SIZE(tid_base_stuff));
3071}
3072
3073static struct dentry *proc_tid_base_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd){
Eric W. Biederman7bcd6b02006-10-02 02:18:56 -07003074 return proc_pident_lookup(dir, dentry,
3075 tid_base_stuff, ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003076}
3077
Arjan van de Ven00977a52007-02-12 00:55:34 -08003078static const struct file_operations proc_tid_base_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003079 .read = generic_read_dir,
3080 .readdir = proc_tid_base_readdir,
3081};
3082
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003083static const struct inode_operations proc_tid_base_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003084 .lookup = proc_tid_base_lookup,
3085 .getattr = pid_getattr,
3086 .setattr = proc_setattr,
3087};
3088
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003089static struct dentry *proc_task_instantiate(struct inode *dir,
Eric Dumazetc5141e62007-05-08 00:26:15 -07003090 struct dentry *dentry, struct task_struct *task, const void *ptr)
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003091{
3092 struct dentry *error = ERR_PTR(-ENOENT);
3093 struct inode *inode;
Eric W. Biederman61a28782006-10-02 02:18:49 -07003094 inode = proc_pid_make_inode(dir->i_sb, task);
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003095
3096 if (!inode)
3097 goto out;
3098 inode->i_mode = S_IFDIR|S_IRUGO|S_IXUGO;
3099 inode->i_op = &proc_tid_base_inode_operations;
3100 inode->i_fop = &proc_tid_base_operations;
3101 inode->i_flags|=S_IMMUTABLE;
Vegard Nossumaed54172008-06-05 22:46:53 -07003102
3103 inode->i_nlink = 2 + pid_entry_count_dirs(tid_base_stuff,
3104 ARRAY_SIZE(tid_base_stuff));
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003105
3106 dentry->d_op = &pid_dentry_operations;
3107
3108 d_add(dentry, inode);
3109 /* Close the race of the process dying before we return the dentry */
3110 if (pid_revalidate(dentry, NULL))
3111 error = NULL;
3112out:
3113 return error;
3114}
3115
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003116static struct dentry *proc_task_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
3117{
3118 struct dentry *result = ERR_PTR(-ENOENT);
3119 struct task_struct *task;
3120 struct task_struct *leader = get_proc_task(dir);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003121 unsigned tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003122 struct pid_namespace *ns;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003123
3124 if (!leader)
3125 goto out_no_task;
3126
3127 tid = name_to_int(dentry);
3128 if (tid == ~0U)
3129 goto out;
3130
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003131 ns = dentry->d_sb->s_fs_info;
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003132 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003133 task = find_task_by_pid_ns(tid, ns);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003134 if (task)
3135 get_task_struct(task);
3136 rcu_read_unlock();
3137 if (!task)
3138 goto out;
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07003139 if (!same_thread_group(leader, task))
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003140 goto out_drop_task;
3141
Eric W. Biederman444ceed2006-10-02 02:18:49 -07003142 result = proc_task_instantiate(dir, dentry, task, NULL);
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003143out_drop_task:
3144 put_task_struct(task);
3145out:
3146 put_task_struct(leader);
3147out_no_task:
3148 return result;
3149}
3150
3151/*
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003152 * Find the first tid of a thread group to return to user space.
3153 *
3154 * Usually this is just the thread group leader, but if the users
3155 * buffer was too small or there was a seek into the middle of the
3156 * directory we have more work todo.
3157 *
3158 * In the case of a short read we start with find_task_by_pid.
3159 *
3160 * In the case of a seek we start with the leader and walk nr
3161 * threads past it.
3162 */
Eric W. Biedermancc288732006-06-26 00:26:01 -07003163static struct task_struct *first_tid(struct task_struct *leader,
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003164 int tid, int nr, struct pid_namespace *ns)
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003165{
Oleg Nesterova872ff02006-06-26 00:26:01 -07003166 struct task_struct *pos;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003167
Eric W. Biedermancc288732006-06-26 00:26:01 -07003168 rcu_read_lock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003169 /* Attempt to start with the pid of a thread */
3170 if (tid && (nr > 0)) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003171 pos = find_task_by_pid_ns(tid, ns);
Oleg Nesterova872ff02006-06-26 00:26:01 -07003172 if (pos && (pos->group_leader == leader))
3173 goto found;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003174 }
3175
3176 /* If nr exceeds the number of threads there is nothing todo */
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003177 pos = NULL;
Oleg Nesterova872ff02006-06-26 00:26:01 -07003178 if (nr && nr >= get_nr_threads(leader))
3179 goto out;
3180
3181 /* If we haven't found our starting place yet start
3182 * with the leader and walk nr threads forward.
3183 */
3184 for (pos = leader; nr > 0; --nr) {
3185 pos = next_thread(pos);
3186 if (pos == leader) {
3187 pos = NULL;
3188 goto out;
3189 }
3190 }
3191found:
3192 get_task_struct(pos);
3193out:
Eric W. Biedermancc288732006-06-26 00:26:01 -07003194 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003195 return pos;
3196}
3197
3198/*
3199 * Find the next thread in the thread list.
3200 * Return NULL if there is an error or no next thread.
3201 *
3202 * The reference to the input task_struct is released.
3203 */
3204static struct task_struct *next_tid(struct task_struct *start)
3205{
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003206 struct task_struct *pos = NULL;
Eric W. Biedermancc288732006-06-26 00:26:01 -07003207 rcu_read_lock();
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003208 if (pid_alive(start)) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003209 pos = next_thread(start);
Oleg Nesterovc1df7fb2006-06-26 00:26:02 -07003210 if (thread_group_leader(pos))
3211 pos = NULL;
3212 else
3213 get_task_struct(pos);
3214 }
Eric W. Biedermancc288732006-06-26 00:26:01 -07003215 rcu_read_unlock();
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003216 put_task_struct(start);
3217 return pos;
3218}
3219
Eric W. Biederman61a28782006-10-02 02:18:49 -07003220static int proc_task_fill_cache(struct file *filp, void *dirent, filldir_t filldir,
3221 struct task_struct *task, int tid)
3222{
3223 char name[PROC_NUMBUF];
3224 int len = snprintf(name, sizeof(name), "%d", tid);
3225 return proc_fill_cache(filp, dirent, filldir, name, len,
3226 proc_task_instantiate, task, NULL);
3227}
3228
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229/* for the /proc/TGID/task/ directories */
3230static int proc_task_readdir(struct file * filp, void * dirent, filldir_t filldir)
3231{
Josef "Jeff" Sipek2fddfee2006-12-08 02:36:36 -08003232 struct dentry *dentry = filp->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 struct inode *inode = dentry->d_inode;
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003234 struct task_struct *leader = NULL;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003235 struct task_struct *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 int retval = -ENOENT;
3237 ino_t ino;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003238 int tid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003239 struct pid_namespace *ns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Guillaume Chazarain7d895242007-01-31 23:48:14 -08003241 task = get_proc_task(inode);
3242 if (!task)
3243 goto out_no_task;
3244 rcu_read_lock();
3245 if (pid_alive(task)) {
3246 leader = task->group_leader;
3247 get_task_struct(leader);
3248 }
3249 rcu_read_unlock();
3250 put_task_struct(task);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003251 if (!leader)
3252 goto out_no_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 retval = 0;
3254
Linus Torvaldsee568b22009-03-17 10:02:35 -07003255 switch ((unsigned long)filp->f_pos) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 case 0:
3257 ino = inode->i_ino;
Zhang Leee6f7792009-03-16 14:44:31 +08003258 if (filldir(dirent, ".", 1, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003260 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 /* fall through */
3262 case 1:
3263 ino = parent_ino(dentry);
Zhang Leee6f7792009-03-16 14:44:31 +08003264 if (filldir(dirent, "..", 2, filp->f_pos, ino, DT_DIR) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 goto out;
Zhang Leee6f7792009-03-16 14:44:31 +08003266 filp->f_pos++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 /* fall through */
3268 }
3269
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003270 /* f_version caches the tgid value that the last readdir call couldn't
3271 * return. lseek aka telldir automagically resets f_version to 0.
3272 */
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003273 ns = filp->f_dentry->d_sb->s_fs_info;
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003274 tid = (int)filp->f_version;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003275 filp->f_version = 0;
Zhang Leee6f7792009-03-16 14:44:31 +08003276 for (task = first_tid(leader, tid, filp->f_pos - 2, ns);
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003277 task;
Zhang Leee6f7792009-03-16 14:44:31 +08003278 task = next_tid(task), filp->f_pos++) {
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003279 tid = task_pid_nr_ns(task, ns);
Eric W. Biederman61a28782006-10-02 02:18:49 -07003280 if (proc_task_fill_cache(filp, dirent, filldir, task, tid) < 0) {
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003281 /* returning this tgid failed, save it as the first
3282 * pid for the next readir call */
Mathieu Desnoyers2b47c362007-10-16 23:27:21 -07003283 filp->f_version = (u64)tid;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003284 put_task_struct(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 break;
Eric W. Biederman0bc58a92006-06-26 00:25:50 -07003286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 }
3288out:
Eric W. Biederman99f89552006-06-26 00:25:55 -07003289 put_task_struct(leader);
3290out_no_task:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return retval;
3292}
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003293
3294static int proc_task_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
3295{
3296 struct inode *inode = dentry->d_inode;
Eric W. Biederman99f89552006-06-26 00:25:55 -07003297 struct task_struct *p = get_proc_task(inode);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003298 generic_fillattr(inode, stat);
3299
Eric W. Biederman99f89552006-06-26 00:25:55 -07003300 if (p) {
Eric W. Biederman99f89552006-06-26 00:25:55 -07003301 stat->nlink += get_nr_threads(p);
Eric W. Biederman99f89552006-06-26 00:25:55 -07003302 put_task_struct(p);
Eric W. Biederman6e66b522006-06-26 00:25:47 -07003303 }
3304
3305 return 0;
3306}
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003307
Arjan van de Venc5ef1c42007-02-12 00:55:40 -08003308static const struct inode_operations proc_task_inode_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003309 .lookup = proc_task_lookup,
3310 .getattr = proc_task_getattr,
3311 .setattr = proc_setattr,
3312};
3313
Arjan van de Ven00977a52007-02-12 00:55:34 -08003314static const struct file_operations proc_task_operations = {
Eric W. Biederman28a6d672006-10-02 02:17:05 -07003315 .read = generic_read_dir,
3316 .readdir = proc_task_readdir,
3317};